Code

Fixed segfault in argument processing. Thanks to Christoph Schell (#1742066)
[nagiosplug.git] / plugins / check_smtp.c
index e7cf2ea26cfc21050a2672b7632e27fe0e309035..a7a07838f9b10f5d08063467045b5f27f0aeaf25 100644 (file)
@@ -11,6 +11,9 @@
 *
 * This file contains the check_smtp plugin
 *
+*  This plugin will attempt to open an SMTP connection with the host.
+*
+*
 * License Information:
 *
 * This program is free software; you can redistribute it and/or modify
@@ -166,9 +169,8 @@ main (int argc, char **argv)
        int result = STATE_UNKNOWN;
        char *cmd_str = NULL;
        char *helocmd = NULL;
-       char *error_msg = NULL;
+       char *error_msg = "";
        struct timeval tv;
-       struct hostent *hp;
 
        setlocale (LC_ALL, "");
        bindtextdomain (PACKAGE, LOCALEDIR);
@@ -177,7 +179,7 @@ main (int argc, char **argv)
        if (process_arguments (argc, argv) == ERROR)
                usage4 (_("Could not parse arguments"));
 
-       /* initialize the HELO command with the localhostname */
+       /* If localhostname not set on command line, use gethostname to set */
        if(! localhostname){
                localhostname = malloc (HOST_MAX_BYTES);
                if(!localhostname){
@@ -188,16 +190,14 @@ main (int argc, char **argv)
                        printf(_("gethostname() failed!\n"));
                        return STATE_CRITICAL;
                }
-               hp = gethostbyname(localhostname);
-               if(!hp) helocmd = localhostname;
-               else helocmd = hp->h_name;
-       } else {
-               helocmd = localhostname;
        }
        if(use_ehlo)
-               asprintf (&helocmd, "%s%s%s", SMTP_EHLO, helocmd, "\r\n");
+               asprintf (&helocmd, "%s%s%s", SMTP_EHLO, localhostname, "\r\n");
        else
-               asprintf (&helocmd, "%s%s%s", SMTP_HELO, helocmd, "\r\n");
+               asprintf (&helocmd, "%s%s%s", SMTP_HELO, localhostname, "\r\n");
+
+       if (verbose)
+               printf("HELOCMD: %s", helocmd);
 
        /* initialize the MAIL command with optional FROM command  */
        asprintf (&cmd_str, "%sFROM: %s%s", mail_command, from_arg, "\r\n");
@@ -282,11 +282,40 @@ main (int argc, char **argv)
                  } else {
                        ssl_established = 1;
                  }
+
+               /*
+                * Resend the EHLO command.
+                *
+                * RFC 3207 (4.2) says: ``The client MUST discard any knowledge
+                * obtained from the server, such as the list of SMTP service
+                * extensions, which was not obtained from the TLS negotiation
+                * itself.  The client SHOULD send an EHLO command as the first
+                * command after a successful TLS negotiation.''  For this
+                * reason, some MTAs will not allow an AUTH LOGIN command before
+                * we resent EHLO via TLS.
+                */
+               if (my_send(helocmd, strlen(helocmd)) <= 0) {
+                       printf("%s\n", _("SMTP UNKNOWN - Cannot send EHLO command via TLS."));
+                       my_close();
+                       return STATE_UNKNOWN;
+               }
+               if (verbose)
+                       printf(_("sent %s"), helocmd);
+               if ((n = my_recv(buffer, MAX_INPUT_BUFFER - 1)) <= 0) {
+                       printf("%s\n", _("SMTP UNKNOWN - Cannot read EHLO response via TLS."));
+                       my_close();
+                       return STATE_UNKNOWN;
+               }
+               if (verbose) {
+                       buffer[n] = '\0';
+                       printf("%s", buffer);
+               }
+
 #  ifdef USE_OPENSSL
                  if ( check_cert ) {
                    result = np_net_ssl_check_cert(days_till_exp);
                    if(result != STATE_OK){
-                     printf (_("CRITICAL - Cannot retrieve server certificate.\n"));
+                     printf ("%s\n", _("CRITICAL - Cannot retrieve server certificate."));
                    }
                    my_close();
                    return result;
@@ -351,12 +380,12 @@ main (int argc, char **argv)
                                do {
                                        if (authuser == NULL) {
                                                result = STATE_CRITICAL;
-                                               error_msg = _("no authuser specified, ");
+                                               asprintf(&error_msg, _("no authuser specified, "));
                                                break;
                                        }
                                        if (authpass == NULL) {
                                                result = STATE_CRITICAL;
-                                               error_msg = _("no authpass specified, ");
+                                               asprintf(&error_msg, _("no authpass specified, "));
                                                break;
                                        }
 
@@ -366,7 +395,7 @@ main (int argc, char **argv)
                                                printf (_("sent %s\n"), "AUTH LOGIN");
 
                                        if((ret = my_recv(buffer, MAXBUF - 1)) < 0){
-                                               error_msg = _("recv() failed after AUTH LOGIN, \n");
+                                               asprintf(&error_msg, _("recv() failed after AUTH LOGIN, "));
                                                result = STATE_WARNING;
                                                break;
                                        }
@@ -376,7 +405,7 @@ main (int argc, char **argv)
 
                                        if (strncmp (buffer, "334", 3) != 0) {
                                                result = STATE_CRITICAL;
-                                               error_msg = _("invalid response received after AUTH LOGIN, ");
+                                               asprintf(&error_msg, _("invalid response received after AUTH LOGIN, "));
                                                break;
                                        }
 
@@ -389,7 +418,7 @@ main (int argc, char **argv)
 
                                        if ((ret = my_recv(buffer, MAX_INPUT_BUFFER-1)) == -1) {
                                                result = STATE_CRITICAL;
-                                               error_msg = _("recv() failed after sending authuser, ");
+                                               asprintf(&error_msg, _("recv() failed after sending authuser, "));
                                                break;
                                        }
                                        buffer[ret] = 0;
@@ -398,7 +427,7 @@ main (int argc, char **argv)
                                        }
                                        if (strncmp (buffer, "334", 3) != 0) {
                                                result = STATE_CRITICAL;
-                                               error_msg = _("invalid response received after authuser, ");
+                                               asprintf(&error_msg, _("invalid response received after authuser, "));
                                                break;
                                        }
                                        /* encode authpass with base64 */
@@ -410,7 +439,7 @@ main (int argc, char **argv)
                                        }
                                        if ((ret = my_recv(buffer, MAX_INPUT_BUFFER-1)) == -1) {
                                                result = STATE_CRITICAL;
-                                               error_msg = _("recv() failed after sending authpass, ");
+                                               asprintf(&error_msg, _("recv() failed after sending authpass, "));
                                                break;
                                        }
                                        buffer[ret] = 0;
@@ -419,14 +448,14 @@ main (int argc, char **argv)
                                        }
                                        if (strncmp (buffer, "235", 3) != 0) {
                                                result = STATE_CRITICAL;
-                                               error_msg = _("invalid response received after authpass, ");
+                                               asprintf(&error_msg, _("invalid response received after authpass, "));
                                                break;
                                        }
                                        break;
                                } while (0);
                        } else {
                                result = STATE_CRITICAL;
-                               error_msg = _("only authtype LOGIN is supported, ");
+                               asprintf(&error_msg, _("only authtype LOGIN is supported, "));
                        }
                }
 
@@ -452,7 +481,7 @@ main (int argc, char **argv)
 
        printf (_("SMTP %s - %s%.3f sec. response time%s%s|%s\n"),
                        state_text (result),
-                       (error_msg == NULL ? "" : error_msg),
+                       error_msg,
                        elapsed_time,
                        verbose?", ":"", verbose?buffer:"",
                        fperfdata ("time", elapsed_time, "s",
@@ -553,22 +582,26 @@ process_arguments (int argc, char **argv)
                        break;
                case 'C':                                                                       /* commands  */
                        if (ncommands >= command_size) {
-                               commands = realloc (commands, command_size+8);
+                               command_size+=8;
+                               commands = realloc (commands, sizeof(char *) * command_size);
                                if (commands == NULL)
                                        die (STATE_UNKNOWN,
                                             _("Could not realloc() units [%d]\n"), ncommands);
                        }
-                       commands[ncommands] = optarg;
+                       commands[ncommands] = (char *) malloc (sizeof(char) * 255);
+                       strncpy (commands[ncommands], optarg, 255);
                        ncommands++;
                        break;
                case 'R':                                                                       /* server responses */
                        if (nresponses >= response_size) {
-                               responses = realloc (responses, response_size+8);
+                               response_size += 8;
+                               responses = realloc (responses, sizeof(char *) * response_size);
                                if (responses == NULL)
                                        die (STATE_UNKNOWN,
                                             _("Could not realloc() units [%d]\n"), nresponses);
                        }
-                       responses[nresponses] = optarg;
+                       responses[nresponses] = (char *) malloc (sizeof(char) * 255);
+                       strncpy (responses[nresponses], optarg, 255);
                        nresponses++;
                        break;
                case 'c':                                                                       /* critical time threshold */
@@ -633,7 +666,7 @@ process_arguments (int argc, char **argv)
                        print_help ();
                        exit (STATE_OK);
                case '?':                                                                       /* help */
-                       usage2 (_("Unknown argument"), optarg);
+                       usage5 ();
                }
        }
 
@@ -705,8 +738,8 @@ print_help (void)
        printf (_(UT_IPv46));
 
        printf (" %s\n", "-e, --expect=STRING");
-  printf (_("String to expect in first line of server response (default: '%s')"),SMTP_EXPECT);
-  printf (" %s\n\n", "-n, nocommand\n");
+  printf (_("    String to expect in first line of server response (default: '%s')\n"), SMTP_EXPECT);
+  printf (" %s\n", "-n, nocommand");
   printf ("    %s\n", _("Suppress SMTP command"));
   printf (" %s\n", "-C, --command=STRING");
   printf ("    %s\n", _("SMTP command (may be used repeatedly)"));
@@ -734,6 +767,7 @@ print_help (void)
 
        printf (_(UT_VERBOSE));
 
+       printf("\n");
        printf ("%s\n", _("Successul connects return STATE_OK, refusals and timeouts return"));
   printf ("%s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN.  Successful"));
   printf ("%s\n", _("connects, but incorrect reponse messages from the host result in"));
@@ -748,8 +782,8 @@ void
 print_usage (void)
 {
   printf (_("Usage:"));
-       printf ("%s -H host [-p port] [-e expect] [-C command] [-f from addr]\n\
-                  [-A authtype -U authuser -P authpass]\n\
-                  [-w warn] [-c crit] [-t timeout] [-S] [-D days] [-n] [-v] [-4|-6]\n", progname);
+       printf ("%s -H host [-p port] [-e expect] [-C command] [-f from addr]", progname);
+  printf ("[-A authtype -U authuser -P authpass] [-w warn] [-c crit] [-t timeout]\n");
+  printf ("[-S] [-D days] [-n] [-v] [-4|-6]\n");
 }