Code

Import Gnulib floorf and base64 and removed our old base64 library.
[nagiosplug.git] / plugins / check_smtp.c
1 /*****************************************************************************
2
3 * Nagios check_smtp plugin
4
5 * License: GPL
6 * Copyright (c) 2000-2007 Nagios Plugins Development Team
7
8 * Last Modified: $Date$
9
10 * Description:
11
12 * This file contains the check_smtp plugin
13
14 * This plugin will attempt to open an SMTP connection with the host.
15
16
17 * This program is free software: you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation, either version 3 of the License, or
20 * (at your option) any later version.
21
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25 * GNU General Public License for more details.
26
27 * You should have received a copy of the GNU General Public License
28 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29
30 * $Id$
31
32 *****************************************************************************/
34 const char *progname = "check_smtp";
35 const char *revision = "$Revision$";
36 const char *copyright = "2000-2007";
37 const char *email = "nagiosplug-devel@lists.sourceforge.net";
39 #include <ctype.h>
41 #include "common.h"
42 #include "netutils.h"
43 #include "utils.h"
44 #include "base64.h"
46 #ifdef HAVE_SSL
47 int check_cert = FALSE;
48 int days_till_exp;
49 #  define my_recv(buf, len) ((use_ssl && ssl_established) ? np_net_ssl_read(buf, len) : read(sd, buf, len))
50 #  define my_send(buf, len) ((use_ssl && ssl_established) ? np_net_ssl_write(buf, len) : send(sd, buf, len, 0))
51 #else /* ifndef HAVE_SSL */
52 #  define my_recv(buf, len) read(sd, buf, len)
53 #  define my_send(buf, len) send(sd, buf, len, 0)
54 #endif
56 enum {
57         SMTP_PORT       = 25
58 };
59 #define SMTP_EXPECT "220"
60 #define SMTP_HELO "HELO "
61 #define SMTP_EHLO "EHLO "
62 #define SMTP_QUIT "QUIT\r\n"
63 #define SMTP_STARTTLS "STARTTLS\r\n"
64 #define SMTP_AUTH_LOGIN "AUTH LOGIN\r\n"
66 #ifndef HOST_MAX_BYTES
67 #define HOST_MAX_BYTES 255
68 #endif
70 #define EHLO_SUPPORTS_STARTTLS 1
72 int process_arguments (int, char **);
73 int validate_arguments (void);
74 void print_help (void);
75 void print_usage (void);
76 void smtp_quit(void);
77 int recvline(char *, size_t);
78 int recvlines(char *, size_t);
79 int my_close(void);
81 #include "regex.h"
82 char regex_expect[MAX_INPUT_BUFFER] = "";
83 regex_t preg;
84 regmatch_t pmatch[10];
85 char timestamp[20] = "";
86 char errbuf[MAX_INPUT_BUFFER];
87 int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
88 int eflags = 0;
89 int errcode, excode;
91 int server_port = SMTP_PORT;
92 char *server_address = NULL;
93 char *server_expect = NULL;
94 int smtp_use_dummycmd = 0;
95 char *mail_command = NULL;
96 char *from_arg = NULL;
97 int ncommands=0;
98 int command_size=0;
99 int nresponses=0;
100 int response_size=0;
101 char **commands = NULL;
102 char **responses = NULL;
103 char *authtype = NULL;
104 char *authuser = NULL;
105 char *authpass = NULL;
106 int warning_time = 0;
107 int check_warning_time = FALSE;
108 int critical_time = 0;
109 int check_critical_time = FALSE;
110 int verbose = 0;
111 int use_ssl = FALSE;
112 short use_ehlo = FALSE;
113 short ssl_established = 0;
114 char *localhostname = NULL;
115 int sd;
116 char buffer[MAX_INPUT_BUFFER];
117 enum {
118   TCP_PROTOCOL = 1,
119   UDP_PROTOCOL = 2,
120 };
123 int
124 main (int argc, char **argv)
126         short supports_tls=FALSE;
127         int n = 0;
128         double elapsed_time;
129         long microsec;
130         int result = STATE_UNKNOWN;
131         char *cmd_str = NULL;
132         char *helocmd = NULL;
133         char *error_msg = "";
134         struct timeval tv;
136         setlocale (LC_ALL, "");
137         bindtextdomain (PACKAGE, LOCALEDIR);
138         textdomain (PACKAGE);
140         if (process_arguments (argc, argv) == ERROR)
141                 usage4 (_("Could not parse arguments"));
143         /* If localhostname not set on command line, use gethostname to set */
144         if(! localhostname){
145                 localhostname = malloc (HOST_MAX_BYTES);
146                 if(!localhostname){
147                         printf(_("malloc() failed!\n"));
148                         return STATE_CRITICAL;
149                 }
150                 if(gethostname(localhostname, HOST_MAX_BYTES)){
151                         printf(_("gethostname() failed!\n"));
152                         return STATE_CRITICAL;
153                 }
154         }
155         if(use_ehlo)
156                 asprintf (&helocmd, "%s%s%s", SMTP_EHLO, localhostname, "\r\n");
157         else
158                 asprintf (&helocmd, "%s%s%s", SMTP_HELO, localhostname, "\r\n");
160         if (verbose)
161                 printf("HELOCMD: %s", helocmd);
163         /* initialize the MAIL command with optional FROM command  */
164         asprintf (&cmd_str, "%sFROM: %s%s", mail_command, from_arg, "\r\n");
166         if (verbose && smtp_use_dummycmd)
167                 printf ("FROM CMD: %s", cmd_str);
168         
169         /* initialize alarm signal handling */
170         (void) signal (SIGALRM, socket_timeout_alarm_handler);
172         /* set socket timeout */
173         (void) alarm (socket_timeout);
175         /* start timer */
176         gettimeofday (&tv, NULL);
178         /* try to connect to the host at the given port number */
179         result = my_tcp_connect (server_address, server_port, &sd);
181         if (result == STATE_OK) { /* we connected */
183                 /* watch for the SMTP connection string and */
184                 /* return a WARNING status if we couldn't read any data */
185                 if (recvlines(buffer, MAX_INPUT_BUFFER) <= 0) {
186                         printf (_("recv() failed\n"));
187                         result = STATE_WARNING;
188                 }
189                 else {
190                         if (verbose)
191                                 printf ("%s", buffer);
192                         /* strip the buffer of carriage returns */
193                         strip (buffer);
194                         /* make sure we find the response we are looking for */
195                         if (!strstr (buffer, server_expect)) {
196                                 if (server_port == SMTP_PORT)
197                                         printf (_("Invalid SMTP response received from host\n"));
198                                 else
199                                         printf (_("Invalid SMTP response received from host on port %d\n"),
200                                                                         server_port);
201                                 result = STATE_WARNING;
202                         }
203                 }
205                 /* send the HELO/EHLO command */
206                 send(sd, helocmd, strlen(helocmd), 0);
208                 /* allow for response to helo command to reach us */
209                 if (recvlines(buffer, MAX_INPUT_BUFFER) <= 0) {
210                         printf (_("recv() failed\n"));
211                         return STATE_WARNING;
212                 } else if(use_ehlo){
213                         if(strstr(buffer, "250 STARTTLS") != NULL ||
214                            strstr(buffer, "250-STARTTLS") != NULL){
215                                 supports_tls=TRUE;
216                         }
217                 }
219                 if(use_ssl && ! supports_tls){
220                         printf(_("WARNING - TLS not supported by server\n"));
221                         smtp_quit();
222                         return STATE_WARNING;
223                 }
225 #ifdef HAVE_SSL
226                 if(use_ssl) {
227                   /* send the STARTTLS command */
228                   send(sd, SMTP_STARTTLS, strlen(SMTP_STARTTLS), 0);
230                   recvlines(buffer, MAX_INPUT_BUFFER); /* wait for it */
231                   if (!strstr (buffer, server_expect)) {
232                     printf (_("Server does not support STARTTLS\n"));
233                     smtp_quit();
234                     return STATE_UNKNOWN;
235                   }
236                   result = np_net_ssl_init(sd);
237                   if(result != STATE_OK) {
238                     printf (_("CRITICAL - Cannot create SSL context.\n"));
239                     np_net_ssl_cleanup();
240                     close(sd);
241                     return STATE_CRITICAL;
242                   } else {
243                         ssl_established = 1;
244                   }
246                 /*
247                  * Resend the EHLO command.
248                  *
249                  * RFC 3207 (4.2) says: ``The client MUST discard any knowledge
250                  * obtained from the server, such as the list of SMTP service
251                  * extensions, which was not obtained from the TLS negotiation
252                  * itself.  The client SHOULD send an EHLO command as the first
253                  * command after a successful TLS negotiation.''  For this
254                  * reason, some MTAs will not allow an AUTH LOGIN command before
255                  * we resent EHLO via TLS.
256                  */
257                 if (my_send(helocmd, strlen(helocmd)) <= 0) {
258                         printf("%s\n", _("SMTP UNKNOWN - Cannot send EHLO command via TLS."));
259                         my_close();
260                         return STATE_UNKNOWN;
261                 }
262                 if (verbose)
263                         printf(_("sent %s"), helocmd);
264                 if ((n = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) {
265                         printf("%s\n", _("SMTP UNKNOWN - Cannot read EHLO response via TLS."));
266                         my_close();
267                         return STATE_UNKNOWN;
268                 }
269                 if (verbose) {
270                         printf("%s", buffer);
271                 }
273 #  ifdef USE_OPENSSL
274                   if ( check_cert ) {
275                     result = np_net_ssl_check_cert(days_till_exp);
276                     if(result != STATE_OK){
277                       printf ("%s\n", _("CRITICAL - Cannot retrieve server certificate."));
278                     }
279                     my_close();
280                     return result;
281                   }
282 #  endif /* USE_OPENSSL */
283                 }
284 #endif
285                                 
286                 /* sendmail will syslog a "NOQUEUE" error if session does not attempt
287                  * to do something useful. This can be prevented by giving a command
288                  * even if syntax is illegal (MAIL requires a FROM:<...> argument)
289                  *
290                  * According to rfc821 you can include a null reversepath in the from command
291                  * - but a log message is generated on the smtp server.
292                  *
293                  * You can disable sending mail_command with '--nocommand'
294                  * Use the -f option to provide a FROM address
295                  */
296                 if (smtp_use_dummycmd) {
297                   my_send(cmd_str, strlen(cmd_str));
298                   if (recvlines(buffer, MAX_INPUT_BUFFER) >= 1 && verbose)
299                     printf("%s", buffer);
300                 }
302                 while (n < ncommands) {
303                         asprintf (&cmd_str, "%s%s", commands[n], "\r\n");
304                         my_send(cmd_str, strlen(cmd_str));
305                         if (recvlines(buffer, MAX_INPUT_BUFFER) >= 1 && verbose)
306                                 printf("%s", buffer);
307                         strip (buffer);
308                         if (n < nresponses) {
309                                 cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
310                                 errcode = regcomp (&preg, responses[n], cflags);
311                                 if (errcode != 0) {
312                                         regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
313                                         printf (_("Could Not Compile Regular Expression"));
314                                         return ERROR;
315                                 }
316                                 excode = regexec (&preg, buffer, 10, pmatch, eflags);
317                                 if (excode == 0) {
318                                         result = STATE_OK;
319                                 }
320                                 else if (excode == REG_NOMATCH) {
321                                         result = STATE_WARNING;
322                                         printf (_("SMTP %s - Invalid response '%s' to command '%s'\n"), state_text (result), buffer, commands[n]);
323                                 }
324                                 else {
325                                         regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
326                                         printf (_("Execute Error: %s\n"), errbuf);
327                                         result = STATE_UNKNOWN;
328                                 }
329                         }
330                         n++;
331                 }
333                 if (authtype != NULL) {
334                         if (strcmp (authtype, "LOGIN") == 0) {
335                                 char *abuf;
336                                 int ret;
337                                 do {
338                                         if (authuser == NULL) {
339                                                 result = STATE_CRITICAL;
340                                                 asprintf(&error_msg, _("no authuser specified, "));
341                                                 break;
342                                         }
343                                         if (authpass == NULL) {
344                                                 result = STATE_CRITICAL;
345                                                 asprintf(&error_msg, _("no authpass specified, "));
346                                                 break;
347                                         }
349                                         /* send AUTH LOGIN */
350                                         my_send(SMTP_AUTH_LOGIN, strlen(SMTP_AUTH_LOGIN));
351                                         if (verbose)
352                                                 printf (_("sent %s\n"), "AUTH LOGIN");
354                                         if ((ret = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) {
355                                                 asprintf(&error_msg, _("recv() failed after AUTH LOGIN, "));
356                                                 result = STATE_WARNING;
357                                                 break;
358                                         }
359                                         if (verbose)
360                                                 printf (_("received %s\n"), buffer);
362                                         if (strncmp (buffer, "334", 3) != 0) {
363                                                 result = STATE_CRITICAL;
364                                                 asprintf(&error_msg, _("invalid response received after AUTH LOGIN, "));
365                                                 break;
366                                         }
368                                         /* encode authuser with base64 */
369                                         base64_encode_alloc (authuser, strlen(authuser), &abuf);
370                                         /* FIXME: abuf shouldn't have enough space to strcat a '\r\n' into it. */
371                                         strcat (abuf, "\r\n");
372                                         my_send(abuf, strlen(abuf));
373                                         if (verbose)
374                                                 printf (_("sent %s\n"), abuf);
376                                         if ((ret = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) {
377                                                 result = STATE_CRITICAL;
378                                                 asprintf(&error_msg, _("recv() failed after sending authuser, "));
379                                                 break;
380                                         }
381                                         if (verbose) {
382                                                 printf (_("received %s\n"), buffer);
383                                         }
384                                         if (strncmp (buffer, "334", 3) != 0) {
385                                                 result = STATE_CRITICAL;
386                                                 asprintf(&error_msg, _("invalid response received after authuser, "));
387                                                 break;
388                                         }
389                                         /* encode authpass with base64 */
390                                         base64_encode_alloc (authpass, strlen(authpass), &abuf);
391                                         /* FIXME: abuf shouldn't have enough space to strcat a '\r\n' into it. */
392                                         strcat (abuf, "\r\n");
393                                         my_send(abuf, strlen(abuf));
394                                         if (verbose) {
395                                                 printf (_("sent %s\n"), abuf);
396                                         }
397                                         if ((ret = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) {
398                                                 result = STATE_CRITICAL;
399                                                 asprintf(&error_msg, _("recv() failed after sending authpass, "));
400                                                 break;
401                                         }
402                                         if (verbose) {
403                                                 printf (_("received %s\n"), buffer);
404                                         }
405                                         if (strncmp (buffer, "235", 3) != 0) {
406                                                 result = STATE_CRITICAL;
407                                                 asprintf(&error_msg, _("invalid response received after authpass, "));
408                                                 break;
409                                         }
410                                         break;
411                                 } while (0);
412                         } else {
413                                 result = STATE_CRITICAL;
414                                 asprintf(&error_msg, _("only authtype LOGIN is supported, "));
415                         }
416                 }
418                 /* tell the server we're done */
419                 smtp_quit();
421                 /* finally close the connection */
422                 close (sd);
423         }
425         /* reset the alarm */
426         alarm (0);
428         microsec = deltime (tv);
429         elapsed_time = (double)microsec / 1.0e6;
431         if (result == STATE_OK) {
432                 if (check_critical_time && elapsed_time > (double) critical_time)
433                         result = STATE_CRITICAL;
434                 else if (check_warning_time && elapsed_time > (double) warning_time)
435                         result = STATE_WARNING;
436         }
438         printf (_("SMTP %s - %s%.3f sec. response time%s%s|%s\n"),
439                         state_text (result),
440                         error_msg,
441                         elapsed_time,
442                         verbose?", ":"", verbose?buffer:"",
443                         fperfdata ("time", elapsed_time, "s",
444                                 (int)check_warning_time, warning_time,
445                                 (int)check_critical_time, critical_time,
446                                 TRUE, 0, FALSE, 0));
448         return result;
453 /* process command-line arguments */
454 int
455 process_arguments (int argc, char **argv)
457         int c;
459         int option = 0;
460         static struct option longopts[] = {
461                 {"hostname", required_argument, 0, 'H'},
462                 {"expect", required_argument, 0, 'e'},
463                 {"critical", required_argument, 0, 'c'},
464                 {"warning", required_argument, 0, 'w'},
465                 {"timeout", required_argument, 0, 't'},
466                 {"port", required_argument, 0, 'p'},
467                 {"from", required_argument, 0, 'f'},
468                 {"fqdn", required_argument, 0, 'F'},
469                 {"authtype", required_argument, 0, 'A'},
470                 {"authuser", required_argument, 0, 'U'},
471                 {"authpass", required_argument, 0, 'P'},
472                 {"command", required_argument, 0, 'C'},
473                 {"response", required_argument, 0, 'R'},
474                 {"nocommand", required_argument, 0, 'n'},
475                 {"verbose", no_argument, 0, 'v'},
476                 {"version", no_argument, 0, 'V'},
477                 {"use-ipv4", no_argument, 0, '4'},
478                 {"use-ipv6", no_argument, 0, '6'},
479                 {"help", no_argument, 0, 'h'},
480                 {"starttls",no_argument,0,'S'},
481                 {"certificate",required_argument,0,'D'},
482                 {0, 0, 0, 0}
483         };
485         if (argc < 2)
486                 return ERROR;
488         for (c = 1; c < argc; c++) {
489                 if (strcmp ("-to", argv[c]) == 0)
490                         strcpy (argv[c], "-t");
491                 else if (strcmp ("-wt", argv[c]) == 0)
492                         strcpy (argv[c], "-w");
493                 else if (strcmp ("-ct", argv[c]) == 0)
494                         strcpy (argv[c], "-c");
495         }
497         while (1) {
498                 c = getopt_long (argc, argv, "+hVv46t:p:f:e:c:w:H:C:R:SD:F:A:U:P:",
499                                  longopts, &option);
501                 if (c == -1 || c == EOF)
502                         break;
504                 switch (c) {
505                 case 'H':                                                                       /* hostname */
506                         if (is_host (optarg)) {
507                                 server_address = optarg;
508                         }
509                         else {
510                                 usage2 (_("Invalid hostname/address"), optarg);
511                         }
512                         break;
513                 case 'p':                                                                       /* port */
514                         if (is_intpos (optarg))
515                                 server_port = atoi (optarg);
516                         else
517                                 usage4 (_("Port must be a positive integer"));
518                         break;
519                 case 'F':
520                 /* localhostname */
521                         localhostname = strdup(optarg);
522                         break;
523                 case 'f':                                                                       /* from argument */
524                         from_arg = optarg;
525                         smtp_use_dummycmd = 1;
526                         break;
527                 case 'A':
528                         authtype = optarg;
529                         use_ehlo = TRUE;
530                         break;
531                 case 'U':
532                         authuser = optarg;
533                         break;
534                 case 'P':
535                         authpass = optarg;
536                         break;
537                 case 'e':                                                                       /* server expect string on 220  */
538                         server_expect = optarg;
539                         break;
540                 case 'C':                                                                       /* commands  */
541                         if (ncommands >= command_size) {
542                                 command_size+=8;
543                                 commands = realloc (commands, sizeof(char *) * command_size);
544                                 if (commands == NULL)
545                                         die (STATE_UNKNOWN,
546                                              _("Could not realloc() units [%d]\n"), ncommands);
547                         }
548                         commands[ncommands] = (char *) malloc (sizeof(char) * 255);
549                         strncpy (commands[ncommands], optarg, 255);
550                         ncommands++;
551                         break;
552                 case 'R':                                                                       /* server responses */
553                         if (nresponses >= response_size) {
554                                 response_size += 8;
555                                 responses = realloc (responses, sizeof(char *) * response_size);
556                                 if (responses == NULL)
557                                         die (STATE_UNKNOWN,
558                                              _("Could not realloc() units [%d]\n"), nresponses);
559                         }
560                         responses[nresponses] = (char *) malloc (sizeof(char) * 255);
561                         strncpy (responses[nresponses], optarg, 255);
562                         nresponses++;
563                         break;
564                 case 'c':                                                                       /* critical time threshold */
565                         if (is_intnonneg (optarg)) {
566                                 critical_time = atoi (optarg);
567                                 check_critical_time = TRUE;
568                         }
569                         else {
570                                 usage4 (_("Critical time must be a positive integer"));
571                         }
572                         break;
573                 case 'w':                                                                       /* warning time threshold */
574                         if (is_intnonneg (optarg)) {
575                                 warning_time = atoi (optarg);
576                                 check_warning_time = TRUE;
577                         }
578                         else {
579                                 usage4 (_("Warning time must be a positive integer"));
580                         }
581                         break;
582                 case 'v':                                                                       /* verbose */
583                         verbose++;
584                         break;
585                 case 't':                                                                       /* timeout */
586                         if (is_intnonneg (optarg)) {
587                                 socket_timeout = atoi (optarg);
588                         }
589                         else {
590                                 usage4 (_("Timeout interval must be a positive integer"));
591                         }
592                         break;
593                 case 'S':
594                 /* starttls */
595                         use_ssl = TRUE;
596                         use_ehlo = TRUE;
597                         break;
598                 case 'D':
599                 /* Check SSL cert validity */
600 #ifdef USE_OPENSSL
601                         if (!is_intnonneg (optarg))
602                                 usage2 ("Invalid certificate expiration period",optarg);
603                                 days_till_exp = atoi (optarg);
604                                 check_cert = TRUE;
605 #else
606                                 usage (_("SSL support not available - install OpenSSL and recompile"));
607 #endif
608                         break;
609                 case '4':
610                         address_family = AF_INET;
611                         break;
612                 case '6':
613 #ifdef USE_IPV6
614                         address_family = AF_INET6;
615 #else
616                         usage4 (_("IPv6 support not available"));
617 #endif
618                         break;
619                 case 'V':                                                                       /* version */
620                         print_revision (progname, revision);
621                         exit (STATE_OK);
622                 case 'h':                                                                       /* help */
623                         print_help ();
624                         exit (STATE_OK);
625                 case '?':                                                                       /* help */
626                         usage5 ();
627                 }
628         }
630         c = optind;
631         if (server_address == NULL) {
632                 if (argv[c]) {
633                         if (is_host (argv[c]))
634                                 server_address = argv[c];
635                         else
636                                 usage2 (_("Invalid hostname/address"), argv[c]);
637                 }
638                 else {
639                         asprintf (&server_address, "127.0.0.1");
640                 }
641         }
643         if (server_expect == NULL)
644                 server_expect = strdup (SMTP_EXPECT);
646         if (mail_command == NULL)
647                 mail_command = strdup("MAIL ");
649         if (from_arg==NULL)
650                 from_arg = strdup(" ");
652         return validate_arguments ();
657 int
658 validate_arguments (void)
660         return OK;
664 void
665 smtp_quit(void)
667         int bytes;
669         my_send(SMTP_QUIT, strlen(SMTP_QUIT));
670         if (verbose)
671                 printf(_("sent %s\n"), "QUIT");
673         /* read the response but don't care about problems */
674         bytes = recvlines(buffer, MAX_INPUT_BUFFER);
675         if (verbose) {
676                 if (bytes < 0)
677                         printf(_("recv() failed after QUIT."));
678                 else if (bytes == 0)
679                         printf(_("Connection reset by peer."));
680                 else {
681                         buffer[bytes] = '\0';
682                         printf(_("received %s\n"), buffer);
683                 }
684         }
688 /*
689  * Receive one line, copy it into buf and nul-terminate it.  Returns the
690  * number of bytes written to buf (excluding the '\0') or 0 on EOF or <0 on
691  * error.
692  *
693  * TODO: Reading one byte at a time is very inefficient.  Replace this by a
694  * function which buffers the data, move that to netutils.c and change
695  * check_smtp and other plugins to use that.  Also, remove (\r)\n.
696  */
697 int
698 recvline(char *buf, size_t bufsize)
700         int result;
701         unsigned i;
703         for (i = result = 0; i < bufsize - 1; i++) {
704                 if ((result = my_recv(&buf[i], 1)) != 1)
705                         break;
706                 if (buf[i] == '\n') {
707                         buf[++i] = '\0';
708                         return i;
709                 }
710         }
711         return (result == 1 || i == 0) ? -2 : result;   /* -2 if out of space */
715 /*
716  * Receive one or more lines, copy them into buf and nul-terminate it.  Returns
717  * the number of bytes written to buf (excluding the '\0') or 0 on EOF or <0 on
718  * error.  Works for all protocols which format multiline replies as follows:
719  *
720  * ``The format for multiline replies requires that every line, except the last,
721  * begin with the reply code, followed immediately by a hyphen, `-' (also known
722  * as minus), followed by text.  The last line will begin with the reply code,
723  * followed immediately by <SP>, optionally some text, and <CRLF>.  As noted
724  * above, servers SHOULD send the <SP> if subsequent text is not sent, but
725  * clients MUST be prepared for it to be omitted.'' (RFC 2821, 4.2.1)
726  *
727  * TODO: Move this to netutils.c.  Also, remove \r and possibly the final \n.
728  */
729 int
730 recvlines(char *buf, size_t bufsize)
732         int result, i;
734         for (i = 0; /* forever */; i += result)
735                 if (!((result = recvline(buf + i, bufsize - i)) > 3 &&
736                     isdigit((int)buf[i]) &&
737                     isdigit((int)buf[i + 1]) &&
738                     isdigit((int)buf[i + 2]) &&
739                     buf[i + 3] == '-'))
740                         break;
742         return (result <= 0) ? result : result + i;
746 int 
747 my_close (void)
749 #ifdef HAVE_SSL
750   np_net_ssl_cleanup();
751 #endif
752   return close(sd);
756 void
757 print_help (void)
759         char *myport;
760         asprintf (&myport, "%d", SMTP_PORT);
762         print_revision (progname, revision);
764         printf ("Copyright (c) 1999-2001 Ethan Galstad <nagios@nagios.org>\n");
765         printf (COPYRIGHT, copyright, email);
767         printf("%s\n", _("This plugin will attempt to open an SMTP connection with the host."));
769   printf ("\n\n");
771         print_usage ();
773         printf (_(UT_HELP_VRSN));
775         printf (_(UT_HOST_PORT), 'p', myport);
777         printf (_(UT_IPv46));
779         printf (" %s\n", "-e, --expect=STRING");
780   printf (_("    String to expect in first line of server response (default: '%s')\n"), SMTP_EXPECT);
781   printf (" %s\n", "-n, nocommand");
782   printf ("    %s\n", _("Suppress SMTP command"));
783   printf (" %s\n", "-C, --command=STRING");
784   printf ("    %s\n", _("SMTP command (may be used repeatedly)"));
785   printf (" %s\n", "-R, --command=STRING");
786   printf ("    %s\n", _("Expected response to command (may be used repeatedly)"));
787   printf (" %s\n", "-f, --from=STRING");
788   printf ("    %s\n", _("FROM-address to include in MAIL command, required by Exchange 2000")),
789 #ifdef HAVE_SSL
790   printf (" %s\n", "-D, --certificate=INTEGER");
791   printf ("    %s\n", _("Minimum number of days a certificate has to be valid."));
792   printf (" %s\n", "-S, --starttls");
793   printf ("    %s\n", _("Use STARTTLS for the connection."));
794 #endif
796         printf (" %s\n", "-A, --authtype=STRING");
797   printf ("    %s\n", _("SMTP AUTH type to check (default none, only LOGIN supported)"));
798   printf (" %s\n", "-U, --authuser=STRING");
799   printf ("    %s\n", _("SMTP AUTH username"));
800   printf (" %s\n", "-P, --authpass=STRING");
801   printf ("    %s\n", _("SMTP AUTH password"));
803         printf (_(UT_WARN_CRIT));
805         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
807         printf (_(UT_VERBOSE));
809         printf("\n");
810         printf ("%s\n", _("Successul connects return STATE_OK, refusals and timeouts return"));
811   printf ("%s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN.  Successful"));
812   printf ("%s\n", _("connects, but incorrect reponse messages from the host result in"));
813   printf ("%s\n", _("STATE_WARNING return values."));
815         printf (_(UT_SUPPORT));
820 void
821 print_usage (void)
823   printf (_("Usage:"));
824         printf ("%s -H host [-p port] [-e expect] [-C command] [-f from addr]", progname);
825   printf ("[-A authtype -U authuser -P authpass] [-w warn] [-c crit] [-t timeout]\n");
826   printf ("[-S] [-D days] [-n] [-v] [-4|-6]\n");