Code

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