Code

Fixed segfault in argument processing. Thanks to Christoph Schell (#1742066)
[nagiosplug.git] / plugins / check_smtp.c
1 /******************************************************************************
2 *
3 * Nagios check_smtp plugin
4 *
5 * License: GPL
6 * Copyright (c) 1999-2006 nagios-plugins 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 * License Information:
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
32 *
33 *
34 * $Id$
35
36 ******************************************************************************/
38 const char *progname = "check_smtp";
39 const char *revision = "$Revision$";
40 const char *copyright = "2000-2006";
41 const char *email = "nagiosplug-devel@lists.sourceforge.net";
43 #include "common.h"
44 #include "netutils.h"
45 #include "utils.h"
47 #ifdef HAVE_SSL
48 int check_cert = FALSE;
49 int days_till_exp;
50 #  define my_recv(buf, len) ((use_ssl && ssl_established) ? np_net_ssl_read(buf, len) : read(sd, buf, len))
51 #  define my_send(buf, len) ((use_ssl && ssl_established) ? np_net_ssl_write(buf, len) : send(sd, buf, len, 0))
52 #else /* ifndef HAVE_SSL */
53 #  define my_recv(buf, len) read(sd, buf, len)
54 #  define my_send(buf, len) send(sd, buf, len, 0)
55 #endif
57 enum {
58         SMTP_PORT       = 25
59 };
60 #define SMTP_EXPECT "220"
61 #define SMTP_HELO "HELO "
62 #define SMTP_EHLO "EHLO "
63 #define SMTP_QUIT "QUIT\r\n"
64 #define SMTP_STARTTLS "STARTTLS\r\n"
65 #define SMTP_AUTH_LOGIN "AUTH LOGIN\r\n"
67 #ifndef HOST_MAX_BYTES
68 #define HOST_MAX_BYTES 255
69 #endif
71 #define EHLO_SUPPORTS_STARTTLS 1
73 int process_arguments (int, char **);
74 int validate_arguments (void);
75 void print_help (void);
76 void print_usage (void);
77 int my_close(void);
79 #include "regex.h"
80 char regex_expect[MAX_INPUT_BUFFER] = "";
81 regex_t preg;
82 regmatch_t pmatch[10];
83 char timestamp[20] = "";
84 char errbuf[MAX_INPUT_BUFFER];
85 int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
86 int eflags = 0;
87 int errcode, excode;
89 int server_port = SMTP_PORT;
90 char *server_address = NULL;
91 char *server_expect = NULL;
92 int smtp_use_dummycmd = 0;
93 char *mail_command = NULL;
94 char *from_arg = NULL;
95 int ncommands=0;
96 int command_size=0;
97 int nresponses=0;
98 int response_size=0;
99 char **commands = NULL;
100 char **responses = NULL;
101 char *authtype = NULL;
102 char *authuser = NULL;
103 char *authpass = NULL;
104 int warning_time = 0;
105 int check_warning_time = FALSE;
106 int critical_time = 0;
107 int check_critical_time = FALSE;
108 int verbose = 0;
109 int use_ssl = FALSE;
110 short use_ehlo = FALSE;
111 short ssl_established = 0;
112 char *localhostname = NULL;
113 int sd;
114 char buffer[MAX_INPUT_BUFFER];
115 enum {
116   TCP_PROTOCOL = 1,
117   UDP_PROTOCOL = 2,
118   MAXBUF = 1024
119 };
121 /* written by lauri alanko */
122 static char *
123 base64 (const char *bin, size_t len)
126         char *buf = (char *) malloc ((len + 2) / 3 * 4 + 1);
127         size_t i = 0, j = 0;
129         char BASE64_END = '=';
130         char base64_table[64];
131         strncpy (base64_table, "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", 64);
133         while (j < len - 2) {
134                 buf[i++] = base64_table[bin[j] >> 2];
135                 buf[i++] = base64_table[((bin[j] & 3) << 4) | (bin[j + 1] >> 4)];
136                 buf[i++] = base64_table[((bin[j + 1] & 15) << 2) | (bin[j + 2] >> 6)];
137                 buf[i++] = base64_table[bin[j + 2] & 63];
138                 j += 3;
139         }
141         switch (len - j) {
142         case 1:
143                 buf[i++] = base64_table[bin[j] >> 2];
144                 buf[i++] = base64_table[(bin[j] & 3) << 4];
145                 buf[i++] = BASE64_END;
146                 buf[i++] = BASE64_END;
147                 break;
148         case 2:
149                 buf[i++] = base64_table[bin[j] >> 2];
150                 buf[i++] = base64_table[((bin[j] & 3) << 4) | (bin[j + 1] >> 4)];
151                 buf[i++] = base64_table[(bin[j + 1] & 15) << 2];
152                 buf[i++] = BASE64_END;
153                 break;
154         case 0:
155                 break;
156         }
158         buf[i] = '\0';
159         return buf;
162 int
163 main (int argc, char **argv)
165         short supports_tls=FALSE;
166         int n = 0;
167         double elapsed_time;
168         long microsec;
169         int result = STATE_UNKNOWN;
170         char *cmd_str = NULL;
171         char *helocmd = NULL;
172         char *error_msg = "";
173         struct timeval tv;
175         setlocale (LC_ALL, "");
176         bindtextdomain (PACKAGE, LOCALEDIR);
177         textdomain (PACKAGE);
179         if (process_arguments (argc, argv) == ERROR)
180                 usage4 (_("Could not parse arguments"));
182         /* If localhostname not set on command line, use gethostname to set */
183         if(! localhostname){
184                 localhostname = malloc (HOST_MAX_BYTES);
185                 if(!localhostname){
186                         printf(_("malloc() failed!\n"));
187                         return STATE_CRITICAL;
188                 }
189                 if(gethostname(localhostname, HOST_MAX_BYTES)){
190                         printf(_("gethostname() failed!\n"));
191                         return STATE_CRITICAL;
192                 }
193         }
194         if(use_ehlo)
195                 asprintf (&helocmd, "%s%s%s", SMTP_EHLO, localhostname, "\r\n");
196         else
197                 asprintf (&helocmd, "%s%s%s", SMTP_HELO, localhostname, "\r\n");
199         if (verbose)
200                 printf("HELOCMD: %s", helocmd);
202         /* initialize the MAIL command with optional FROM command  */
203         asprintf (&cmd_str, "%sFROM: %s%s", mail_command, from_arg, "\r\n");
205         if (verbose && smtp_use_dummycmd)
206                 printf ("FROM CMD: %s", cmd_str);
207         
208         /* initialize alarm signal handling */
209         (void) signal (SIGALRM, socket_timeout_alarm_handler);
211         /* set socket timeout */
212         (void) alarm (socket_timeout);
214         /* start timer */
215         gettimeofday (&tv, NULL);
217         /* try to connect to the host at the given port number */
218         result = my_tcp_connect (server_address, server_port, &sd);
220         if (result == STATE_OK) { /* we connected */
222                 /* watch for the SMTP connection string and */
223                 /* return a WARNING status if we couldn't read any data */
224                 if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) == -1) {
225                         printf (_("recv() failed\n"));
226                         result = STATE_WARNING;
227                 }
228                 else {
229                         if (verbose)
230                                 printf ("%s", buffer);
231                         /* strip the buffer of carriage returns */
232                         strip (buffer);
233                         /* make sure we find the response we are looking for */
234                         if (!strstr (buffer, server_expect)) {
235                                 if (server_port == SMTP_PORT)
236                                         printf (_("Invalid SMTP response received from host\n"));
237                                 else
238                                         printf (_("Invalid SMTP response received from host on port %d\n"),
239                                                                         server_port);
240                                 result = STATE_WARNING;
241                         }
242                 }
244                 /* send the HELO/EHLO command */
245                 send(sd, helocmd, strlen(helocmd), 0);
247                 /* allow for response to helo command to reach us */
248                 if(read (sd, buffer, MAXBUF - 1) < 0){
249                         printf (_("recv() failed\n"));
250                         return STATE_WARNING;
251                 } else if(use_ehlo){
252                         buffer[MAXBUF-1]='\0';
253                         if(strstr(buffer, "250 STARTTLS") != NULL ||
254                            strstr(buffer, "250-STARTTLS") != NULL){
255                                 supports_tls=TRUE;
256                         }
257                 }
259                 if(use_ssl && ! supports_tls){
260                         printf(_("WARNING - TLS not supported by server\n"));
261                         send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
262                         return STATE_WARNING;
263                 }
265 #ifdef HAVE_SSL
266                 if(use_ssl) {
267                   /* send the STARTTLS command */
268                   send(sd, SMTP_STARTTLS, strlen(SMTP_STARTTLS), 0);
270                   recv(sd,buffer, MAX_INPUT_BUFFER-1, 0); /* wait for it */
271                   if (!strstr (buffer, server_expect)) {
272                     printf (_("Server does not support STARTTLS\n"));
273                     send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
274                     return STATE_UNKNOWN;
275                   }
276                   result = np_net_ssl_init(sd);
277                   if(result != STATE_OK) {
278                     printf (_("CRITICAL - Cannot create SSL context.\n"));
279                     np_net_ssl_cleanup();
280                     close(sd);
281                     return STATE_CRITICAL;
282                   } else {
283                         ssl_established = 1;
284                   }
286                 /*
287                  * Resend the EHLO command.
288                  *
289                  * RFC 3207 (4.2) says: ``The client MUST discard any knowledge
290                  * obtained from the server, such as the list of SMTP service
291                  * extensions, which was not obtained from the TLS negotiation
292                  * itself.  The client SHOULD send an EHLO command as the first
293                  * command after a successful TLS negotiation.''  For this
294                  * reason, some MTAs will not allow an AUTH LOGIN command before
295                  * we resent EHLO via TLS.
296                  */
297                 if (my_send(helocmd, strlen(helocmd)) <= 0) {
298                         printf("%s\n", _("SMTP UNKNOWN - Cannot send EHLO command via TLS."));
299                         my_close();
300                         return STATE_UNKNOWN;
301                 }
302                 if (verbose)
303                         printf(_("sent %s"), helocmd);
304                 if ((n = my_recv(buffer, MAX_INPUT_BUFFER - 1)) <= 0) {
305                         printf("%s\n", _("SMTP UNKNOWN - Cannot read EHLO response via TLS."));
306                         my_close();
307                         return STATE_UNKNOWN;
308                 }
309                 if (verbose) {
310                         buffer[n] = '\0';
311                         printf("%s", buffer);
312                 }
314 #  ifdef USE_OPENSSL
315                   if ( check_cert ) {
316                     result = np_net_ssl_check_cert(days_till_exp);
317                     if(result != STATE_OK){
318                       printf ("%s\n", _("CRITICAL - Cannot retrieve server certificate."));
319                     }
320                     my_close();
321                     return result;
322                   }
323 #  endif /* USE_OPENSSL */
324                 }
325 #endif
326                                 
327                 /* sendmail will syslog a "NOQUEUE" error if session does not attempt
328                  * to do something useful. This can be prevented by giving a command
329                  * even if syntax is illegal (MAIL requires a FROM:<...> argument)
330                  *
331                  * According to rfc821 you can include a null reversepath in the from command
332                  * - but a log message is generated on the smtp server.
333                  *
334                  * You can disable sending mail_command with '--nocommand'
335                  * Use the -f option to provide a FROM address
336                  */
337                 if (smtp_use_dummycmd) {
338                   my_send(cmd_str, strlen(cmd_str));
339                   my_recv(buffer, MAX_INPUT_BUFFER-1);
340                   if (verbose) 
341                     printf("%s", buffer);
342                 }
344                 while (n < ncommands) {
345                         asprintf (&cmd_str, "%s%s", commands[n], "\r\n");
346                         my_send(cmd_str, strlen(cmd_str));
347                         my_recv(buffer, MAX_INPUT_BUFFER-1);
348                         if (verbose) 
349                                 printf("%s", buffer);
350                         strip (buffer);
351                         if (n < nresponses) {
352                                 cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
353                                 errcode = regcomp (&preg, responses[n], cflags);
354                                 if (errcode != 0) {
355                                         regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER);
356                                         printf (_("Could Not Compile Regular Expression"));
357                                         return ERROR;
358                                 }
359                                 excode = regexec (&preg, buffer, 10, pmatch, eflags);
360                                 if (excode == 0) {
361                                         result = STATE_OK;
362                                 }
363                                 else if (excode == REG_NOMATCH) {
364                                         result = STATE_WARNING;
365                                         printf (_("SMTP %s - Invalid response '%s' to command '%s'\n"), state_text (result), buffer, commands[n]);
366                                 }
367                                 else {
368                                         regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER);
369                                         printf (_("Execute Error: %s\n"), errbuf);
370                                         result = STATE_UNKNOWN;
371                                 }
372                         }
373                         n++;
374                 }
376                 if (authtype != NULL) {
377                         if (strcmp (authtype, "LOGIN") == 0) {
378                                 char *abuf;
379                                 int ret;
380                                 do {
381                                         if (authuser == NULL) {
382                                                 result = STATE_CRITICAL;
383                                                 asprintf(&error_msg, _("no authuser specified, "));
384                                                 break;
385                                         }
386                                         if (authpass == NULL) {
387                                                 result = STATE_CRITICAL;
388                                                 asprintf(&error_msg, _("no authpass specified, "));
389                                                 break;
390                                         }
392                                         /* send AUTH LOGIN */
393                                         my_send(SMTP_AUTH_LOGIN, strlen(SMTP_AUTH_LOGIN));
394                                         if (verbose)
395                                                 printf (_("sent %s\n"), "AUTH LOGIN");
397                                         if((ret = my_recv(buffer, MAXBUF - 1)) < 0){
398                                                 asprintf(&error_msg, _("recv() failed after AUTH LOGIN, "));
399                                                 result = STATE_WARNING;
400                                                 break;
401                                         }
402                                         buffer[ret] = 0;
403                                         if (verbose)
404                                                 printf (_("received %s\n"), buffer);
406                                         if (strncmp (buffer, "334", 3) != 0) {
407                                                 result = STATE_CRITICAL;
408                                                 asprintf(&error_msg, _("invalid response received after AUTH LOGIN, "));
409                                                 break;
410                                         }
412                                         /* encode authuser with base64 */
413                                         abuf = base64 (authuser, strlen(authuser));
414                                         strcat (abuf, "\r\n");
415                                         my_send(abuf, strlen(abuf));
416                                         if (verbose)
417                                                 printf (_("sent %s\n"), abuf);
419                                         if ((ret = my_recv(buffer, MAX_INPUT_BUFFER-1)) == -1) {
420                                                 result = STATE_CRITICAL;
421                                                 asprintf(&error_msg, _("recv() failed after sending authuser, "));
422                                                 break;
423                                         }
424                                         buffer[ret] = 0;
425                                         if (verbose) {
426                                                 printf (_("received %s\n"), buffer);
427                                         }
428                                         if (strncmp (buffer, "334", 3) != 0) {
429                                                 result = STATE_CRITICAL;
430                                                 asprintf(&error_msg, _("invalid response received after authuser, "));
431                                                 break;
432                                         }
433                                         /* encode authpass with base64 */
434                                         abuf = base64 (authpass, strlen(authpass));
435                                         strcat (abuf, "\r\n");
436                                         my_send(abuf, strlen(abuf));
437                                         if (verbose) {
438                                                 printf (_("sent %s\n"), abuf);
439                                         }
440                                         if ((ret = my_recv(buffer, MAX_INPUT_BUFFER-1)) == -1) {
441                                                 result = STATE_CRITICAL;
442                                                 asprintf(&error_msg, _("recv() failed after sending authpass, "));
443                                                 break;
444                                         }
445                                         buffer[ret] = 0;
446                                         if (verbose) {
447                                                 printf (_("received %s\n"), buffer);
448                                         }
449                                         if (strncmp (buffer, "235", 3) != 0) {
450                                                 result = STATE_CRITICAL;
451                                                 asprintf(&error_msg, _("invalid response received after authpass, "));
452                                                 break;
453                                         }
454                                         break;
455                                 } while (0);
456                         } else {
457                                 result = STATE_CRITICAL;
458                                 asprintf(&error_msg, _("only authtype LOGIN is supported, "));
459                         }
460                 }
462                 /* tell the server we're done */
463                 my_send (SMTP_QUIT, strlen (SMTP_QUIT));
465                 /* finally close the connection */
466                 close (sd);
467         }
469         /* reset the alarm */
470         alarm (0);
472         microsec = deltime (tv);
473         elapsed_time = (double)microsec / 1.0e6;
475         if (result == STATE_OK) {
476                 if (check_critical_time && elapsed_time > (double) critical_time)
477                         result = STATE_CRITICAL;
478                 else if (check_warning_time && elapsed_time > (double) warning_time)
479                         result = STATE_WARNING;
480         }
482         printf (_("SMTP %s - %s%.3f sec. response time%s%s|%s\n"),
483                         state_text (result),
484                         error_msg,
485                         elapsed_time,
486                         verbose?", ":"", verbose?buffer:"",
487                         fperfdata ("time", elapsed_time, "s",
488                                 (int)check_warning_time, warning_time,
489                                 (int)check_critical_time, critical_time,
490                                 TRUE, 0, FALSE, 0));
492         return result;
497 /* process command-line arguments */
498 int
499 process_arguments (int argc, char **argv)
501         int c;
503         int option = 0;
504         static struct option longopts[] = {
505                 {"hostname", required_argument, 0, 'H'},
506                 {"expect", required_argument, 0, 'e'},
507                 {"critical", required_argument, 0, 'c'},
508                 {"warning", required_argument, 0, 'w'},
509                 {"timeout", required_argument, 0, 't'},
510                 {"port", required_argument, 0, 'p'},
511                 {"from", required_argument, 0, 'f'},
512                 {"fqdn", required_argument, 0, 'F'},
513                 {"authtype", required_argument, 0, 'A'},
514                 {"authuser", required_argument, 0, 'U'},
515                 {"authpass", required_argument, 0, 'P'},
516                 {"command", required_argument, 0, 'C'},
517                 {"response", required_argument, 0, 'R'},
518                 {"nocommand", required_argument, 0, 'n'},
519                 {"verbose", no_argument, 0, 'v'},
520                 {"version", no_argument, 0, 'V'},
521                 {"use-ipv4", no_argument, 0, '4'},
522                 {"use-ipv6", no_argument, 0, '6'},
523                 {"help", no_argument, 0, 'h'},
524                 {"starttls",no_argument,0,'S'},
525                 {"certificate",required_argument,0,'D'},
526                 {0, 0, 0, 0}
527         };
529         if (argc < 2)
530                 return ERROR;
532         for (c = 1; c < argc; c++) {
533                 if (strcmp ("-to", argv[c]) == 0)
534                         strcpy (argv[c], "-t");
535                 else if (strcmp ("-wt", argv[c]) == 0)
536                         strcpy (argv[c], "-w");
537                 else if (strcmp ("-ct", argv[c]) == 0)
538                         strcpy (argv[c], "-c");
539         }
541         while (1) {
542                 c = getopt_long (argc, argv, "+hVv46t:p:f:e:c:w:H:C:R:SD:F:A:U:P:",
543                                  longopts, &option);
545                 if (c == -1 || c == EOF)
546                         break;
548                 switch (c) {
549                 case 'H':                                                                       /* hostname */
550                         if (is_host (optarg)) {
551                                 server_address = optarg;
552                         }
553                         else {
554                                 usage2 (_("Invalid hostname/address"), optarg);
555                         }
556                         break;
557                 case 'p':                                                                       /* port */
558                         if (is_intpos (optarg))
559                                 server_port = atoi (optarg);
560                         else
561                                 usage4 (_("Port must be a positive integer"));
562                         break;
563                 case 'F':
564                 /* localhostname */
565                         localhostname = strdup(optarg);
566                         break;
567                 case 'f':                                                                       /* from argument */
568                         from_arg = optarg;
569                         smtp_use_dummycmd = 1;
570                         break;
571                 case 'A':
572                         authtype = optarg;
573                         break;
574                 case 'U':
575                         authuser = optarg;
576                         break;
577                 case 'P':
578                         authpass = optarg;
579                         break;
580                 case 'e':                                                                       /* server expect string on 220  */
581                         server_expect = optarg;
582                         break;
583                 case 'C':                                                                       /* commands  */
584                         if (ncommands >= command_size) {
585                                 command_size+=8;
586                                 commands = realloc (commands, sizeof(char *) * command_size);
587                                 if (commands == NULL)
588                                         die (STATE_UNKNOWN,
589                                              _("Could not realloc() units [%d]\n"), ncommands);
590                         }
591                         commands[ncommands] = (char *) malloc (sizeof(char) * 255);
592                         strncpy (commands[ncommands], optarg, 255);
593                         ncommands++;
594                         break;
595                 case 'R':                                                                       /* server responses */
596                         if (nresponses >= response_size) {
597                                 response_size += 8;
598                                 responses = realloc (responses, sizeof(char *) * response_size);
599                                 if (responses == NULL)
600                                         die (STATE_UNKNOWN,
601                                              _("Could not realloc() units [%d]\n"), nresponses);
602                         }
603                         responses[nresponses] = (char *) malloc (sizeof(char) * 255);
604                         strncpy (responses[nresponses], optarg, 255);
605                         nresponses++;
606                         break;
607                 case 'c':                                                                       /* critical time threshold */
608                         if (is_intnonneg (optarg)) {
609                                 critical_time = atoi (optarg);
610                                 check_critical_time = TRUE;
611                         }
612                         else {
613                                 usage4 (_("Critical time must be a positive integer"));
614                         }
615                         break;
616                 case 'w':                                                                       /* warning time threshold */
617                         if (is_intnonneg (optarg)) {
618                                 warning_time = atoi (optarg);
619                                 check_warning_time = TRUE;
620                         }
621                         else {
622                                 usage4 (_("Warning time must be a positive integer"));
623                         }
624                         break;
625                 case 'v':                                                                       /* verbose */
626                         verbose++;
627                         break;
628                 case 't':                                                                       /* timeout */
629                         if (is_intnonneg (optarg)) {
630                                 socket_timeout = atoi (optarg);
631                         }
632                         else {
633                                 usage4 (_("Timeout interval must be a positive integer"));
634                         }
635                         break;
636                 case 'S':
637                 /* starttls */
638                         use_ssl = TRUE;
639                         use_ehlo = TRUE;
640                         break;
641                 case 'D':
642                 /* Check SSL cert validity */
643 #ifdef USE_OPENSSL
644                         if (!is_intnonneg (optarg))
645                                 usage2 ("Invalid certificate expiration period",optarg);
646                                 days_till_exp = atoi (optarg);
647                                 check_cert = TRUE;
648 #else
649                                 usage (_("SSL support not available - install OpenSSL and recompile"));
650 #endif
651                         break;
652                 case '4':
653                         address_family = AF_INET;
654                         break;
655                 case '6':
656 #ifdef USE_IPV6
657                         address_family = AF_INET6;
658 #else
659                         usage4 (_("IPv6 support not available"));
660 #endif
661                         break;
662                 case 'V':                                                                       /* version */
663                         print_revision (progname, revision);
664                         exit (STATE_OK);
665                 case 'h':                                                                       /* help */
666                         print_help ();
667                         exit (STATE_OK);
668                 case '?':                                                                       /* help */
669                         usage5 ();
670                 }
671         }
673         c = optind;
674         if (server_address == NULL) {
675                 if (argv[c]) {
676                         if (is_host (argv[c]))
677                                 server_address = argv[c];
678                         else
679                                 usage2 (_("Invalid hostname/address"), argv[c]);
680                 }
681                 else {
682                         asprintf (&server_address, "127.0.0.1");
683                 }
684         }
686         if (server_expect == NULL)
687                 server_expect = strdup (SMTP_EXPECT);
689         if (mail_command == NULL)
690                 mail_command = strdup("MAIL ");
692         if (from_arg==NULL)
693                 from_arg = strdup(" ");
695         return validate_arguments ();
700 int
701 validate_arguments (void)
703         return OK;
707 int 
708 my_close (void)
710 #ifdef HAVE_SSL
711   np_net_ssl_cleanup();
712 #endif
713   return close(sd);
717 void
718 print_help (void)
720         char *myport;
721         asprintf (&myport, "%d", SMTP_PORT);
723         print_revision (progname, revision);
725         printf ("Copyright (c) 1999-2001 Ethan Galstad <nagios@nagios.org>\n");
726         printf (COPYRIGHT, copyright, email);
728         printf("%s\n", _("This plugin will attempt to open an SMTP connection with the host."));
730   printf ("\n\n");
732         print_usage ();
734         printf (_(UT_HELP_VRSN));
736         printf (_(UT_HOST_PORT), 'p', myport);
738         printf (_(UT_IPv46));
740         printf (" %s\n", "-e, --expect=STRING");
741   printf (_("    String to expect in first line of server response (default: '%s')\n"), SMTP_EXPECT);
742   printf (" %s\n", "-n, nocommand");
743   printf ("    %s\n", _("Suppress SMTP command"));
744   printf (" %s\n", "-C, --command=STRING");
745   printf ("    %s\n", _("SMTP command (may be used repeatedly)"));
746   printf (" %s\n", "-R, --command=STRING");
747   printf ("    %s\n", _("Expected response to command (may be used repeatedly)"));
748   printf (" %s\n", "-f, --from=STRING");
749   printf ("    %s\n", _("FROM-address to include in MAIL command, required by Exchange 2000")),
750 #ifdef HAVE_SSL
751   printf (" %s\n", "-D, --certificate=INTEGER");
752   printf ("    %s\n", _("Minimum number of days a certificate has to be valid."));
753   printf (" %s\n", "-S, --starttls");
754   printf ("    %s\n", _("Use STARTTLS for the connection."));
755 #endif
757         printf (" %s\n", "-A, --authtype=STRING");
758   printf ("    %s\n", _("SMTP AUTH type to check (default none, only LOGIN supported)"));
759   printf (" %s\n", "-U, --authuser=STRING");
760   printf ("    %s\n", _("SMTP AUTH username"));
761   printf (" %s\n", "-P, --authpass=STRING");
762   printf ("    %s\n", _("SMTP AUTH password"));
764         printf (_(UT_WARN_CRIT));
766         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
768         printf (_(UT_VERBOSE));
770         printf("\n");
771         printf ("%s\n", _("Successul connects return STATE_OK, refusals and timeouts return"));
772   printf ("%s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN.  Successful"));
773   printf ("%s\n", _("connects, but incorrect reponse messages from the host result in"));
774   printf ("%s\n", _("STATE_WARNING return values."));
776         printf (_(UT_SUPPORT));
781 void
782 print_usage (void)
784   printf (_("Usage:"));
785         printf ("%s -H host [-p port] [-e expect] [-C command] [-f from addr]", progname);
786   printf ("[-A authtype -U authuser -P authpass] [-w warn] [-c crit] [-t timeout]\n");
787   printf ("[-S] [-D days] [-n] [-v] [-4|-6]\n");