diff options
| author | Lorenz Kästle <12514511+RincewindsHat@users.noreply.github.com> | 2025-03-10 21:37:46 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-10 21:37:46 +0100 |
| commit | 205384194dc5334aa3d8d8162f1e17e10595fdb3 (patch) | |
| tree | 8c630b47badc0a3b189457550475990003782d78 /plugins | |
| parent | f930f1745b350903efa42933c04f8a2362d66d74 (diff) | |
| parent | 6cd097921f0c5016fcae60b38dfb88c412e4bb20 (diff) | |
| download | monitoring-plugins-205384194dc5334aa3d8d8162f1e17e10595fdb3.tar.gz | |
Merge pull request #2079 from RincewindsHat/refactor/check_smtp
Refactor/check smtp
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/Makefile.am | 3 | ||||
| -rw-r--r-- | plugins/check_smtp.c | 1153 | ||||
| -rw-r--r-- | plugins/check_smtp.d/config.h | 92 |
3 files changed, 648 insertions, 600 deletions
diff --git a/plugins/Makefile.am b/plugins/Makefile.am index 3d5ad1a9..d9269691 100644 --- a/plugins/Makefile.am +++ b/plugins/Makefile.am | |||
| @@ -55,7 +55,8 @@ EXTRA_DIST = t \ | |||
| 55 | check_ssh.d \ | 55 | check_ssh.d \ |
| 56 | check_dns.d \ | 56 | check_dns.d \ |
| 57 | check_apt.d \ | 57 | check_apt.d \ |
| 58 | check_by_ssh.d | 58 | check_by_ssh.d \ |
| 59 | check_smtp.d | ||
| 59 | 60 | ||
| 60 | PLUGINHDRS = common.h | 61 | PLUGINHDRS = common.h |
| 61 | 62 | ||
diff --git a/plugins/check_smtp.c b/plugins/check_smtp.c index e6369e63..44b735f9 100644 --- a/plugins/check_smtp.c +++ b/plugins/check_smtp.c | |||
| @@ -1,32 +1,32 @@ | |||
| 1 | /***************************************************************************** | 1 | /***************************************************************************** |
| 2 | * | 2 | * |
| 3 | * Monitoring check_smtp plugin | 3 | * Monitoring check_smtp plugin |
| 4 | * | 4 | * |
| 5 | * License: GPL | 5 | * License: GPL |
| 6 | * Copyright (c) 2000-2024 Monitoring Plugins Development Team | 6 | * Copyright (c) 2000-2024 Monitoring Plugins Development Team |
| 7 | * | 7 | * |
| 8 | * Description: | 8 | * Description: |
| 9 | * | 9 | * |
| 10 | * This file contains the check_smtp plugin | 10 | * This file contains the check_smtp plugin |
| 11 | * | 11 | * |
| 12 | * This plugin will attempt to open an SMTP connection with the host. | 12 | * This plugin will attempt to open an SMTP connection with the host. |
| 13 | * | 13 | * |
| 14 | * | 14 | * |
| 15 | * This program is free software: you can redistribute it and/or modify | 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 | 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 | 17 | * the Free Software Foundation, either version 3 of the License, or |
| 18 | * (at your option) any later version. | 18 | * (at your option) any later version. |
| 19 | * | 19 | * |
| 20 | * This program is distributed in the hope that it will be useful, | 20 | * This program is distributed in the hope that it will be useful, |
| 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 21 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 22 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 23 | * GNU General Public License for more details. | 23 | * GNU General Public License for more details. |
| 24 | * | 24 | * |
| 25 | * You should have received a copy of the GNU General Public License | 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/>. | 26 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 27 | * | 27 | * |
| 28 | * | 28 | * |
| 29 | *****************************************************************************/ | 29 | *****************************************************************************/ |
| 30 | 30 | ||
| 31 | const char *progname = "check_smtp"; | 31 | const char *progname = "check_smtp"; |
| 32 | const char *copyright = "2000-2024"; | 32 | const char *copyright = "2000-2024"; |
| @@ -36,400 +36,394 @@ const char *email = "devel@monitoring-plugins.org"; | |||
| 36 | #include "netutils.h" | 36 | #include "netutils.h" |
| 37 | #include "utils.h" | 37 | #include "utils.h" |
| 38 | #include "base64.h" | 38 | #include "base64.h" |
| 39 | #include "regex.h" | ||
| 39 | 40 | ||
| 40 | #include <ctype.h> | 41 | #include <ctype.h> |
| 42 | #include "check_smtp.d/config.h" | ||
| 43 | #include "../lib/states.h" | ||
| 44 | |||
| 45 | #define PROXY_PREFIX "PROXY TCP4 0.0.0.0 0.0.0.0 25 25\r\n" | ||
| 46 | #define SMTP_HELO "HELO " | ||
| 47 | #define SMTP_EHLO "EHLO " | ||
| 48 | #define SMTP_LHLO "LHLO " | ||
| 49 | #define SMTP_QUIT "QUIT\r\n" | ||
| 50 | #define SMTP_STARTTLS "STARTTLS\r\n" | ||
| 51 | #define SMTP_AUTH_LOGIN "AUTH LOGIN\r\n" | ||
| 41 | 52 | ||
| 53 | #define EHLO_SUPPORTS_STARTTLS 1 | ||
| 54 | |||
| 55 | typedef struct { | ||
| 56 | int errorcode; | ||
| 57 | check_smtp_config config; | ||
| 58 | } check_smtp_config_wrapper; | ||
| 59 | static check_smtp_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); | ||
| 60 | |||
| 61 | int my_recv(check_smtp_config config, void *buf, int num, int socket_descriptor, bool ssl_established) { | ||
| 42 | #ifdef HAVE_SSL | 62 | #ifdef HAVE_SSL |
| 43 | static bool check_cert = false; | 63 | if ((config.use_starttls || config.use_ssl) && ssl_established) { |
| 44 | static int days_till_exp_warn, days_till_exp_crit; | 64 | return np_net_ssl_read(buf, num); |
| 45 | # define my_recv(buf, len) (((use_starttls || use_ssl) && ssl_established) ? np_net_ssl_read(buf, len) : read(sd, buf, len)) | 65 | } |
| 46 | # define my_send(buf, len) (((use_starttls || use_ssl) && ssl_established) ? np_net_ssl_write(buf, len) : send(sd, buf, len, 0)) | 66 | return (int)read(socket_descriptor, buf, (size_t)num); |
| 47 | #else /* ifndef HAVE_SSL */ | 67 | #else /* ifndef HAVE_SSL */ |
| 48 | # define my_recv(buf, len) read(sd, buf, len) | 68 | return read(socket_descriptor, buf, len) |
| 49 | # define my_send(buf, len) send(sd, buf, len, 0) | ||
| 50 | #endif | 69 | #endif |
| 70 | } | ||
| 51 | 71 | ||
| 52 | enum { | 72 | int my_send(check_smtp_config config, void *buf, int num, int socket_descriptor, bool ssl_established) { |
| 53 | SMTP_PORT = 25, | 73 | #ifdef HAVE_SSL |
| 54 | SMTPS_PORT = 465 | 74 | if ((config.use_starttls || config.use_ssl) && ssl_established) { |
| 55 | }; | ||
| 56 | #define PROXY_PREFIX "PROXY TCP4 0.0.0.0 0.0.0.0 25 25\r\n" | ||
| 57 | #define SMTP_EXPECT "220" | ||
| 58 | #define SMTP_HELO "HELO " | ||
| 59 | #define SMTP_EHLO "EHLO " | ||
| 60 | #define SMTP_LHLO "LHLO " | ||
| 61 | #define SMTP_QUIT "QUIT\r\n" | ||
| 62 | #define SMTP_STARTTLS "STARTTLS\r\n" | ||
| 63 | #define SMTP_AUTH_LOGIN "AUTH LOGIN\r\n" | ||
| 64 | 75 | ||
| 65 | #define EHLO_SUPPORTS_STARTTLS 1 | 76 | return np_net_ssl_write(buf, num); |
| 77 | } | ||
| 78 | return (int)send(socket_descriptor, buf, (size_t)num, 0); | ||
| 79 | #else /* ifndef HAVE_SSL */ | ||
| 80 | return send(socket_descriptor, buf, len, 0); | ||
| 81 | #endif | ||
| 82 | } | ||
| 66 | 83 | ||
| 67 | static int process_arguments (int, char **); | 84 | static void print_help(void); |
| 68 | static int validate_arguments (void); | 85 | void print_usage(void); |
| 69 | static void print_help (void); | 86 | static char *smtp_quit(check_smtp_config /*config*/, char /*buffer*/[MAX_INPUT_BUFFER], int /*socket_descriptor*/, |
| 70 | void print_usage (void); | 87 | bool /*ssl_established*/); |
| 71 | static void smtp_quit(void); | 88 | static int recvline(char * /*buf*/, size_t /*bufsize*/, check_smtp_config /*config*/, int /*socket_descriptor*/, bool /*ssl_established*/); |
| 72 | static int recvline(char *, size_t); | 89 | static int recvlines(check_smtp_config /*config*/, char * /*buf*/, size_t /*bufsize*/, int /*socket_descriptor*/, bool /*ssl_established*/); |
| 73 | static int recvlines(char *, size_t); | 90 | static int my_close(int /*socket_descriptor*/); |
| 74 | static int my_close(void); | ||
| 75 | 91 | ||
| 76 | #include "regex.h" | ||
| 77 | static regex_t preg; | ||
| 78 | static regmatch_t pmatch[10]; | ||
| 79 | static char errbuf[MAX_INPUT_BUFFER]; | ||
| 80 | static int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE; | ||
| 81 | static int eflags = 0; | ||
| 82 | static int errcode, excode; | ||
| 83 | |||
| 84 | static int server_port = SMTP_PORT; | ||
| 85 | static int server_port_option = 0; | ||
| 86 | static char *server_address = NULL; | ||
| 87 | static char *server_expect = NULL; | ||
| 88 | static char *mail_command = NULL; | ||
| 89 | static char *from_arg = NULL; | ||
| 90 | static int send_mail_from=0; | ||
| 91 | static int ncommands=0; | ||
| 92 | static int command_size=0; | ||
| 93 | static int nresponses=0; | ||
| 94 | static int response_size=0; | ||
| 95 | static char **commands = NULL; | ||
| 96 | static char **responses = NULL; | ||
| 97 | static char *authtype = NULL; | ||
| 98 | static char *authuser = NULL; | ||
| 99 | static char *authpass = NULL; | ||
| 100 | static double warning_time = 0; | ||
| 101 | static bool check_warning_time = false; | ||
| 102 | static double critical_time = 0; | ||
| 103 | static bool check_critical_time = false; | ||
| 104 | static int verbose = 0; | 92 | static int verbose = 0; |
| 105 | static bool use_ssl = false; | ||
| 106 | static bool use_starttls = false; | ||
| 107 | static bool use_sni = false; | ||
| 108 | static bool use_proxy_prefix = false; | ||
| 109 | static bool use_ehlo = false; | ||
| 110 | static bool use_lhlo = false; | ||
| 111 | static bool ssl_established = false; | ||
| 112 | static char *localhostname = NULL; | ||
| 113 | static int sd; | ||
| 114 | static char buffer[MAX_INPUT_BUFFER]; | ||
| 115 | enum { | ||
| 116 | TCP_PROTOCOL = 1, | ||
| 117 | UDP_PROTOCOL = 2, | ||
| 118 | }; | ||
| 119 | static bool ignore_send_quit_failure = false; | ||
| 120 | |||
| 121 | |||
| 122 | int | ||
| 123 | main (int argc, char **argv) | ||
| 124 | { | ||
| 125 | bool supports_tls = false; | ||
| 126 | int n = 0; | ||
| 127 | double elapsed_time; | ||
| 128 | long microsec; | ||
| 129 | int result = STATE_UNKNOWN; | ||
| 130 | char *cmd_str = NULL; | ||
| 131 | char *helocmd = NULL; | ||
| 132 | char *error_msg = ""; | ||
| 133 | char *server_response = NULL; | ||
| 134 | struct timeval tv; | ||
| 135 | 93 | ||
| 136 | /* Catch pipe errors in read/write - sometimes occurs when writing QUIT */ | 94 | int main(int argc, char **argv) { |
| 137 | (void) signal (SIGPIPE, SIG_IGN); | 95 | setlocale(LC_ALL, ""); |
| 138 | 96 | bindtextdomain(PACKAGE, LOCALEDIR); | |
| 139 | setlocale (LC_ALL, ""); | 97 | textdomain(PACKAGE); |
| 140 | bindtextdomain (PACKAGE, LOCALEDIR); | ||
| 141 | textdomain (PACKAGE); | ||
| 142 | 98 | ||
| 143 | /* Parse extra opts if any */ | 99 | /* Parse extra opts if any */ |
| 144 | argv=np_extra_opts (&argc, argv, progname); | 100 | argv = np_extra_opts(&argc, argv, progname); |
| 145 | 101 | ||
| 146 | if (process_arguments (argc, argv) == ERROR) | 102 | check_smtp_config_wrapper tmp_config = process_arguments(argc, argv); |
| 147 | usage4 (_("Could not parse arguments")); | 103 | |
| 104 | if (tmp_config.errorcode == ERROR) { | ||
| 105 | usage4(_("Could not parse arguments")); | ||
| 106 | } | ||
| 107 | |||
| 108 | const check_smtp_config config = tmp_config.config; | ||
| 148 | 109 | ||
| 149 | /* If localhostname not set on command line, use gethostname to set */ | 110 | /* If localhostname not set on command line, use gethostname to set */ |
| 150 | if(! localhostname){ | 111 | char *localhostname = config.localhostname; |
| 151 | localhostname = malloc (HOST_MAX_BYTES); | 112 | if (!localhostname) { |
| 152 | if(!localhostname){ | 113 | localhostname = malloc(HOST_MAX_BYTES); |
| 114 | if (!localhostname) { | ||
| 153 | printf(_("malloc() failed!\n")); | 115 | printf(_("malloc() failed!\n")); |
| 154 | return STATE_CRITICAL; | 116 | exit(STATE_CRITICAL); |
| 155 | } | 117 | } |
| 156 | if(gethostname(localhostname, HOST_MAX_BYTES)){ | 118 | if (gethostname(localhostname, HOST_MAX_BYTES)) { |
| 157 | printf(_("gethostname() failed!\n")); | 119 | printf(_("gethostname() failed!\n")); |
| 158 | return STATE_CRITICAL; | 120 | exit(STATE_CRITICAL); |
| 159 | } | 121 | } |
| 160 | } | 122 | } |
| 161 | if(use_lhlo) | 123 | |
| 162 | xasprintf (&helocmd, "%s%s%s", SMTP_LHLO, localhostname, "\r\n"); | 124 | char *helocmd = NULL; |
| 163 | else if(use_ehlo) | 125 | if (config.use_lhlo) { |
| 164 | xasprintf (&helocmd, "%s%s%s", SMTP_EHLO, localhostname, "\r\n"); | 126 | xasprintf(&helocmd, "%s%s%s", SMTP_LHLO, localhostname, "\r\n"); |
| 165 | else | 127 | } else if (config.use_ehlo) { |
| 166 | xasprintf (&helocmd, "%s%s%s", SMTP_HELO, localhostname, "\r\n"); | 128 | xasprintf(&helocmd, "%s%s%s", SMTP_EHLO, localhostname, "\r\n"); |
| 167 | 129 | } else { | |
| 168 | if (verbose) | 130 | xasprintf(&helocmd, "%s%s%s", SMTP_HELO, localhostname, "\r\n"); |
| 131 | } | ||
| 132 | |||
| 133 | if (verbose) { | ||
| 169 | printf("HELOCMD: %s", helocmd); | 134 | printf("HELOCMD: %s", helocmd); |
| 135 | } | ||
| 170 | 136 | ||
| 137 | char *mail_command = strdup("MAIL "); | ||
| 138 | char *cmd_str = NULL; | ||
| 171 | /* initialize the MAIL command with optional FROM command */ | 139 | /* initialize the MAIL command with optional FROM command */ |
| 172 | xasprintf (&cmd_str, "%sFROM:<%s>%s", mail_command, from_arg, "\r\n"); | 140 | xasprintf(&cmd_str, "%sFROM:<%s>%s", mail_command, config.from_arg, "\r\n"); |
| 173 | 141 | ||
| 174 | if (verbose && send_mail_from) | 142 | if (verbose && config.send_mail_from) { |
| 175 | printf ("FROM CMD: %s", cmd_str); | 143 | printf("FROM CMD: %s", cmd_str); |
| 144 | } | ||
| 145 | |||
| 146 | /* Catch pipe errors in read/write - sometimes occurs when writing QUIT */ | ||
| 147 | (void)signal(SIGPIPE, SIG_IGN); | ||
| 176 | 148 | ||
| 177 | /* initialize alarm signal handling */ | 149 | /* initialize alarm signal handling */ |
| 178 | (void) signal (SIGALRM, socket_timeout_alarm_handler); | 150 | (void)signal(SIGALRM, socket_timeout_alarm_handler); |
| 179 | 151 | ||
| 180 | /* set socket timeout */ | 152 | /* set socket timeout */ |
| 181 | (void) alarm (socket_timeout); | 153 | (void)alarm(socket_timeout); |
| 182 | 154 | ||
| 155 | struct timeval start_time; | ||
| 183 | /* start timer */ | 156 | /* start timer */ |
| 184 | gettimeofday (&tv, NULL); | 157 | gettimeofday(&start_time, NULL); |
| 185 | 158 | ||
| 159 | int socket_descriptor = 0; | ||
| 186 | /* try to connect to the host at the given port number */ | 160 | /* try to connect to the host at the given port number */ |
| 187 | result = my_tcp_connect (server_address, server_port, &sd); | 161 | mp_state_enum result = my_tcp_connect(config.server_address, config.server_port, &socket_descriptor); |
| 188 | 162 | ||
| 163 | char *error_msg = ""; | ||
| 164 | char buffer[MAX_INPUT_BUFFER]; | ||
| 165 | bool ssl_established = false; | ||
| 189 | if (result == STATE_OK) { /* we connected */ | 166 | if (result == STATE_OK) { /* we connected */ |
| 190 | /* If requested, send PROXY header */ | 167 | /* If requested, send PROXY header */ |
| 191 | if (use_proxy_prefix) { | 168 | if (config.use_proxy_prefix) { |
| 192 | if (verbose) | 169 | if (verbose) { |
| 193 | printf ("Sending header %s\n", PROXY_PREFIX); | 170 | printf("Sending header %s\n", PROXY_PREFIX); |
| 194 | my_send(PROXY_PREFIX, strlen(PROXY_PREFIX)); | 171 | } |
| 172 | my_send(config, PROXY_PREFIX, strlen(PROXY_PREFIX), socket_descriptor, ssl_established); | ||
| 195 | } | 173 | } |
| 196 | 174 | ||
| 197 | #ifdef HAVE_SSL | 175 | #ifdef HAVE_SSL |
| 198 | if (use_ssl) { | 176 | if (config.use_ssl) { |
| 199 | result = np_net_ssl_init_with_hostname(sd, (use_sni ? server_address : NULL)); | 177 | result = np_net_ssl_init_with_hostname(socket_descriptor, (config.use_sni ? config.server_address : NULL)); |
| 200 | if (result != STATE_OK) { | 178 | if (result != STATE_OK) { |
| 201 | printf (_("CRITICAL - Cannot create SSL context.\n")); | 179 | printf(_("CRITICAL - Cannot create SSL context.\n")); |
| 202 | close(sd); | 180 | close(socket_descriptor); |
| 203 | np_net_ssl_cleanup(); | 181 | np_net_ssl_cleanup(); |
| 204 | return STATE_CRITICAL; | 182 | exit(STATE_CRITICAL); |
| 205 | } else { | ||
| 206 | ssl_established = 1; | ||
| 207 | } | 183 | } |
| 184 | ssl_established = true; | ||
| 208 | } | 185 | } |
| 209 | #endif | 186 | #endif |
| 210 | 187 | ||
| 211 | /* watch for the SMTP connection string and */ | 188 | /* watch for the SMTP connection string and */ |
| 212 | /* return a WARNING status if we couldn't read any data */ | 189 | /* return a WARNING status if we couldn't read any data */ |
| 213 | if (recvlines(buffer, MAX_INPUT_BUFFER) <= 0) { | 190 | if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) <= 0) { |
| 214 | printf (_("recv() failed\n")); | 191 | printf(_("recv() failed\n")); |
| 215 | return STATE_WARNING; | 192 | exit(STATE_WARNING); |
| 216 | } | 193 | } |
| 217 | 194 | ||
| 195 | char *server_response = NULL; | ||
| 218 | /* save connect return (220 hostname ..) for later use */ | 196 | /* save connect return (220 hostname ..) for later use */ |
| 219 | xasprintf(&server_response, "%s", buffer); | 197 | xasprintf(&server_response, "%s", buffer); |
| 220 | 198 | ||
| 221 | /* send the HELO/EHLO command */ | 199 | /* send the HELO/EHLO command */ |
| 222 | my_send(helocmd, strlen(helocmd)); | 200 | my_send(config, helocmd, (int)strlen(helocmd), socket_descriptor, ssl_established); |
| 223 | 201 | ||
| 224 | /* allow for response to helo command to reach us */ | 202 | /* allow for response to helo command to reach us */ |
| 225 | if (recvlines(buffer, MAX_INPUT_BUFFER) <= 0) { | 203 | if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) <= 0) { |
| 226 | printf (_("recv() failed\n")); | 204 | printf(_("recv() failed\n")); |
| 227 | return STATE_WARNING; | 205 | exit(STATE_WARNING); |
| 228 | } else if(use_ehlo || use_lhlo){ | 206 | } |
| 229 | if(strstr(buffer, "250 STARTTLS") != NULL || | 207 | |
| 230 | strstr(buffer, "250-STARTTLS") != NULL){ | 208 | bool supports_tls = false; |
| 231 | supports_tls=true; | 209 | if (config.use_ehlo || config.use_lhlo) { |
| 210 | if (strstr(buffer, "250 STARTTLS") != NULL || strstr(buffer, "250-STARTTLS") != NULL) { | ||
| 211 | supports_tls = true; | ||
| 232 | } | 212 | } |
| 233 | } | 213 | } |
| 234 | 214 | ||
| 235 | if(use_starttls && ! supports_tls){ | 215 | if (config.use_starttls && !supports_tls) { |
| 236 | printf(_("WARNING - TLS not supported by server\n")); | 216 | printf(_("WARNING - TLS not supported by server\n")); |
| 237 | smtp_quit(); | 217 | smtp_quit(config, buffer, socket_descriptor, ssl_established); |
| 238 | return STATE_WARNING; | 218 | exit(STATE_WARNING); |
| 239 | } | 219 | } |
| 240 | 220 | ||
| 241 | #ifdef HAVE_SSL | 221 | #ifdef HAVE_SSL |
| 242 | if(use_starttls) { | 222 | if (config.use_starttls) { |
| 243 | /* send the STARTTLS command */ | 223 | /* send the STARTTLS command */ |
| 244 | send(sd, SMTP_STARTTLS, strlen(SMTP_STARTTLS), 0); | 224 | send(socket_descriptor, SMTP_STARTTLS, strlen(SMTP_STARTTLS), 0); |
| 245 | 225 | ||
| 246 | recvlines(buffer, MAX_INPUT_BUFFER); /* wait for it */ | 226 | recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established); /* wait for it */ |
| 247 | if (!strstr (buffer, SMTP_EXPECT)) { | 227 | if (!strstr(buffer, SMTP_EXPECT)) { |
| 248 | printf (_("Server does not support STARTTLS\n")); | 228 | printf(_("Server does not support STARTTLS\n")); |
| 249 | smtp_quit(); | 229 | smtp_quit(config, buffer, socket_descriptor, ssl_established); |
| 250 | return STATE_UNKNOWN; | 230 | exit(STATE_UNKNOWN); |
| 251 | } | 231 | } |
| 252 | result = np_net_ssl_init_with_hostname(sd, (use_sni ? server_address : NULL)); | 232 | |
| 253 | if(result != STATE_OK) { | 233 | result = np_net_ssl_init_with_hostname(socket_descriptor, (config.use_sni ? config.server_address : NULL)); |
| 254 | printf (_("CRITICAL - Cannot create SSL context.\n")); | 234 | if (result != STATE_OK) { |
| 255 | close(sd); | 235 | printf(_("CRITICAL - Cannot create SSL context.\n")); |
| 256 | np_net_ssl_cleanup(); | 236 | close(socket_descriptor); |
| 257 | return STATE_CRITICAL; | 237 | np_net_ssl_cleanup(); |
| 258 | } else { | 238 | exit(STATE_CRITICAL); |
| 259 | ssl_established = 1; | 239 | } |
| 260 | } | 240 | |
| 261 | 241 | ssl_established = true; | |
| 262 | /* | 242 | |
| 263 | * Resend the EHLO command. | 243 | /* |
| 264 | * | 244 | * Resend the EHLO command. |
| 265 | * RFC 3207 (4.2) says: ``The client MUST discard any knowledge | 245 | * |
| 266 | * obtained from the server, such as the list of SMTP service | 246 | * RFC 3207 (4.2) says: ``The client MUST discard any knowledge |
| 267 | * extensions, which was not obtained from the TLS negotiation | 247 | * obtained from the server, such as the list of SMTP service |
| 268 | * itself. The client SHOULD send an EHLO command as the first | 248 | * extensions, which was not obtained from the TLS negotiation |
| 269 | * command after a successful TLS negotiation.'' For this | 249 | * itself. The client SHOULD send an EHLO command as the first |
| 270 | * reason, some MTAs will not allow an AUTH LOGIN command before | 250 | * command after a successful TLS negotiation.'' For this |
| 271 | * we resent EHLO via TLS. | 251 | * reason, some MTAs will not allow an AUTH LOGIN command before |
| 272 | */ | 252 | * we resent EHLO via TLS. |
| 273 | if (my_send(helocmd, strlen(helocmd)) <= 0) { | 253 | */ |
| 274 | printf("%s\n", _("SMTP UNKNOWN - Cannot send EHLO command via TLS.")); | 254 | if (my_send(config, helocmd, strlen(helocmd), socket_descriptor, ssl_established) <= 0) { |
| 275 | my_close(); | 255 | printf("%s\n", _("SMTP UNKNOWN - Cannot send EHLO command via TLS.")); |
| 276 | return STATE_UNKNOWN; | 256 | my_close(socket_descriptor); |
| 277 | } | 257 | exit(STATE_UNKNOWN); |
| 278 | if (verbose) | 258 | } |
| 279 | printf(_("sent %s"), helocmd); | 259 | |
| 280 | if ((n = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) { | 260 | if (verbose) { |
| 281 | printf("%s\n", _("SMTP UNKNOWN - Cannot read EHLO response via TLS.")); | 261 | printf(_("sent %s"), helocmd); |
| 282 | my_close(); | 262 | } |
| 283 | return STATE_UNKNOWN; | ||
| 284 | } | ||
| 285 | if (verbose) { | ||
| 286 | printf("%s", buffer); | ||
| 287 | } | ||
| 288 | 263 | ||
| 289 | # ifdef USE_OPENSSL | 264 | if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) <= 0) { |
| 290 | if ( check_cert ) { | 265 | printf("%s\n", _("SMTP UNKNOWN - Cannot read EHLO response via TLS.")); |
| 291 | result = np_net_ssl_check_cert(days_till_exp_warn, days_till_exp_crit); | 266 | my_close(socket_descriptor); |
| 292 | smtp_quit(); | 267 | exit(STATE_UNKNOWN); |
| 293 | my_close(); | 268 | } |
| 294 | return result; | 269 | |
| 295 | } | 270 | if (verbose) { |
| 296 | # endif /* USE_OPENSSL */ | 271 | printf("%s", buffer); |
| 272 | } | ||
| 273 | |||
| 274 | # ifdef USE_OPENSSL | ||
| 275 | if (config.check_cert) { | ||
| 276 | result = np_net_ssl_check_cert(config.days_till_exp_warn, config.days_till_exp_crit); | ||
| 277 | smtp_quit(config, buffer, socket_descriptor, ssl_established); | ||
| 278 | my_close(socket_descriptor); | ||
| 279 | exit(result); | ||
| 280 | } | ||
| 281 | # endif /* USE_OPENSSL */ | ||
| 297 | } | 282 | } |
| 298 | #endif | 283 | #endif |
| 299 | 284 | ||
| 300 | if (verbose) | 285 | if (verbose) { |
| 301 | printf ("%s", buffer); | 286 | printf("%s", buffer); |
| 287 | } | ||
| 302 | 288 | ||
| 303 | /* save buffer for later use */ | 289 | /* save buffer for later use */ |
| 304 | xasprintf(&server_response, "%s%s", server_response, buffer); | 290 | xasprintf(&server_response, "%s%s", server_response, buffer); |
| 305 | /* strip the buffer of carriage returns */ | 291 | /* strip the buffer of carriage returns */ |
| 306 | strip (server_response); | 292 | strip(server_response); |
| 307 | 293 | ||
| 308 | /* make sure we find the droids we are looking for */ | 294 | /* make sure we find the droids we are looking for */ |
| 309 | if (!strstr (server_response, server_expect)) { | 295 | if (!strstr(server_response, config.server_expect)) { |
| 310 | if (server_port == SMTP_PORT) | 296 | if (config.server_port == SMTP_PORT) { |
| 311 | printf (_("Invalid SMTP response received from host: %s\n"), server_response); | 297 | printf(_("Invalid SMTP response received from host: %s\n"), server_response); |
| 312 | else | 298 | } else { |
| 313 | printf (_("Invalid SMTP response received from host on port %d: %s\n"), | 299 | printf(_("Invalid SMTP response received from host on port %d: %s\n"), config.server_port, server_response); |
| 314 | server_port, server_response); | 300 | } |
| 315 | return STATE_WARNING; | 301 | exit(STATE_WARNING); |
| 316 | } | 302 | } |
| 317 | 303 | ||
| 318 | if (send_mail_from) { | 304 | if (config.send_mail_from) { |
| 319 | my_send(cmd_str, strlen(cmd_str)); | 305 | my_send(config, cmd_str, (int)strlen(cmd_str), socket_descriptor, ssl_established); |
| 320 | if (recvlines(buffer, MAX_INPUT_BUFFER) >= 1 && verbose) | 306 | if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) >= 1 && verbose) { |
| 321 | printf("%s", buffer); | 307 | printf("%s", buffer); |
| 308 | } | ||
| 322 | } | 309 | } |
| 323 | 310 | ||
| 324 | n = 0; | 311 | int counter = 0; |
| 325 | while (n < ncommands) { | 312 | while (counter < config.ncommands) { |
| 326 | xasprintf (&cmd_str, "%s%s", commands[n], "\r\n"); | 313 | xasprintf(&cmd_str, "%s%s", config.commands[counter], "\r\n"); |
| 327 | my_send(cmd_str, strlen(cmd_str)); | 314 | my_send(config, cmd_str, (int)strlen(cmd_str), socket_descriptor, ssl_established); |
| 328 | if (recvlines(buffer, MAX_INPUT_BUFFER) >= 1 && verbose) | 315 | if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) >= 1 && verbose) { |
| 329 | printf("%s", buffer); | 316 | printf("%s", buffer); |
| 330 | strip (buffer); | 317 | } |
| 331 | if (n < nresponses) { | 318 | strip(buffer); |
| 332 | cflags |= REG_EXTENDED | REG_NOSUB | REG_NEWLINE; | 319 | if (counter < config.nresponses) { |
| 333 | errcode = regcomp (&preg, responses[n], cflags); | 320 | int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE; |
| 321 | regex_t preg; | ||
| 322 | int errcode = regcomp(&preg, config.responses[counter], cflags); | ||
| 323 | char errbuf[MAX_INPUT_BUFFER]; | ||
| 334 | if (errcode != 0) { | 324 | if (errcode != 0) { |
| 335 | regerror (errcode, &preg, errbuf, MAX_INPUT_BUFFER); | 325 | regerror(errcode, &preg, errbuf, MAX_INPUT_BUFFER); |
| 336 | printf (_("Could Not Compile Regular Expression")); | 326 | printf(_("Could Not Compile Regular Expression")); |
| 337 | return ERROR; | 327 | exit(STATE_UNKNOWN); |
| 338 | } | 328 | } |
| 339 | excode = regexec (&preg, buffer, 10, pmatch, eflags); | 329 | |
| 330 | regmatch_t pmatch[10]; | ||
| 331 | int eflags = 0; | ||
| 332 | int excode = regexec(&preg, buffer, 10, pmatch, eflags); | ||
| 340 | if (excode == 0) { | 333 | if (excode == 0) { |
| 341 | result = STATE_OK; | 334 | result = STATE_OK; |
| 342 | } | 335 | } else if (excode == REG_NOMATCH) { |
| 343 | else if (excode == REG_NOMATCH) { | ||
| 344 | result = STATE_WARNING; | 336 | result = STATE_WARNING; |
| 345 | printf (_("SMTP %s - Invalid response '%s' to command '%s'\n"), state_text (result), buffer, commands[n]); | 337 | printf(_("SMTP %s - Invalid response '%s' to command '%s'\n"), state_text(result), buffer, config.commands[counter]); |
| 346 | } | 338 | } else { |
| 347 | else { | 339 | regerror(excode, &preg, errbuf, MAX_INPUT_BUFFER); |
| 348 | regerror (excode, &preg, errbuf, MAX_INPUT_BUFFER); | 340 | printf(_("Execute Error: %s\n"), errbuf); |
| 349 | printf (_("Execute Error: %s\n"), errbuf); | ||
| 350 | result = STATE_UNKNOWN; | 341 | result = STATE_UNKNOWN; |
| 351 | } | 342 | } |
| 352 | } | 343 | } |
| 353 | n++; | 344 | counter++; |
| 354 | } | 345 | } |
| 355 | 346 | ||
| 356 | if (authtype != NULL) { | 347 | if (config.authtype != NULL) { |
| 357 | if (strcmp (authtype, "LOGIN") == 0) { | 348 | if (strcmp(config.authtype, "LOGIN") == 0) { |
| 358 | char *abuf; | 349 | char *abuf; |
| 359 | int ret; | 350 | int ret; |
| 360 | do { | 351 | do { |
| 361 | if (authuser == NULL) { | 352 | if (config.authuser == NULL) { |
| 362 | result = STATE_CRITICAL; | 353 | result = STATE_CRITICAL; |
| 363 | xasprintf(&error_msg, _("no authuser specified, ")); | 354 | xasprintf(&error_msg, _("no authuser specified, ")); |
| 364 | break; | 355 | break; |
| 365 | } | 356 | } |
| 366 | if (authpass == NULL) { | 357 | if (config.authpass == NULL) { |
| 367 | result = STATE_CRITICAL; | 358 | result = STATE_CRITICAL; |
| 368 | xasprintf(&error_msg, _("no authpass specified, ")); | 359 | xasprintf(&error_msg, _("no authpass specified, ")); |
| 369 | break; | 360 | break; |
| 370 | } | 361 | } |
| 371 | 362 | ||
| 372 | /* send AUTH LOGIN */ | 363 | /* send AUTH LOGIN */ |
| 373 | my_send(SMTP_AUTH_LOGIN, strlen(SMTP_AUTH_LOGIN)); | 364 | my_send(config, SMTP_AUTH_LOGIN, strlen(SMTP_AUTH_LOGIN), socket_descriptor, ssl_established); |
| 374 | if (verbose) | 365 | if (verbose) { |
| 375 | printf (_("sent %s\n"), "AUTH LOGIN"); | 366 | printf(_("sent %s\n"), "AUTH LOGIN"); |
| 367 | } | ||
| 376 | 368 | ||
| 377 | if ((ret = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) { | 369 | if ((ret = recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established)) <= 0) { |
| 378 | xasprintf(&error_msg, _("recv() failed after AUTH LOGIN, ")); | 370 | xasprintf(&error_msg, _("recv() failed after AUTH LOGIN, ")); |
| 379 | result = STATE_WARNING; | 371 | result = STATE_WARNING; |
| 380 | break; | 372 | break; |
| 381 | } | 373 | } |
| 382 | if (verbose) | 374 | if (verbose) { |
| 383 | printf (_("received %s\n"), buffer); | 375 | printf(_("received %s\n"), buffer); |
| 376 | } | ||
| 384 | 377 | ||
| 385 | if (strncmp (buffer, "334", 3) != 0) { | 378 | if (strncmp(buffer, "334", 3) != 0) { |
| 386 | result = STATE_CRITICAL; | 379 | result = STATE_CRITICAL; |
| 387 | xasprintf(&error_msg, _("invalid response received after AUTH LOGIN, ")); | 380 | xasprintf(&error_msg, _("invalid response received after AUTH LOGIN, ")); |
| 388 | break; | 381 | break; |
| 389 | } | 382 | } |
| 390 | 383 | ||
| 391 | /* encode authuser with base64 */ | 384 | /* encode authuser with base64 */ |
| 392 | base64_encode_alloc (authuser, strlen(authuser), &abuf); | 385 | base64_encode_alloc(config.authuser, strlen(config.authuser), &abuf); |
| 393 | xasprintf(&abuf, "%s\r\n", abuf); | 386 | xasprintf(&abuf, "%s\r\n", abuf); |
| 394 | my_send(abuf, strlen(abuf)); | 387 | my_send(config, abuf, (int)strlen(abuf), socket_descriptor, ssl_established); |
| 395 | if (verbose) | 388 | if (verbose) { |
| 396 | printf (_("sent %s\n"), abuf); | 389 | printf(_("sent %s\n"), abuf); |
| 390 | } | ||
| 397 | 391 | ||
| 398 | if ((ret = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) { | 392 | if ((ret = recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established)) <= 0) { |
| 399 | result = STATE_CRITICAL; | 393 | result = STATE_CRITICAL; |
| 400 | xasprintf(&error_msg, _("recv() failed after sending authuser, ")); | 394 | xasprintf(&error_msg, _("recv() failed after sending authuser, ")); |
| 401 | break; | 395 | break; |
| 402 | } | 396 | } |
| 403 | if (verbose) { | 397 | if (verbose) { |
| 404 | printf (_("received %s\n"), buffer); | 398 | printf(_("received %s\n"), buffer); |
| 405 | } | 399 | } |
| 406 | if (strncmp (buffer, "334", 3) != 0) { | 400 | if (strncmp(buffer, "334", 3) != 0) { |
| 407 | result = STATE_CRITICAL; | 401 | result = STATE_CRITICAL; |
| 408 | xasprintf(&error_msg, _("invalid response received after authuser, ")); | 402 | xasprintf(&error_msg, _("invalid response received after authuser, ")); |
| 409 | break; | 403 | break; |
| 410 | } | 404 | } |
| 411 | /* encode authpass with base64 */ | 405 | /* encode authpass with base64 */ |
| 412 | base64_encode_alloc (authpass, strlen(authpass), &abuf); | 406 | base64_encode_alloc(config.authpass, strlen(config.authpass), &abuf); |
| 413 | xasprintf(&abuf, "%s\r\n", abuf); | 407 | xasprintf(&abuf, "%s\r\n", abuf); |
| 414 | my_send(abuf, strlen(abuf)); | 408 | my_send(config, abuf, (int)strlen(abuf), socket_descriptor, ssl_established); |
| 415 | if (verbose) { | 409 | if (verbose) { |
| 416 | printf (_("sent %s\n"), abuf); | 410 | printf(_("sent %s\n"), abuf); |
| 417 | } | 411 | } |
| 418 | if ((ret = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) { | 412 | if ((ret = recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established)) <= 0) { |
| 419 | result = STATE_CRITICAL; | 413 | result = STATE_CRITICAL; |
| 420 | xasprintf(&error_msg, _("recv() failed after sending authpass, ")); | 414 | xasprintf(&error_msg, _("recv() failed after sending authpass, ")); |
| 421 | break; | 415 | break; |
| 422 | } | 416 | } |
| 423 | if (verbose) { | 417 | if (verbose) { |
| 424 | printf (_("received %s\n"), buffer); | 418 | printf(_("received %s\n"), buffer); |
| 425 | } | 419 | } |
| 426 | if (strncmp (buffer, "235", 3) != 0) { | 420 | if (strncmp(buffer, "235", 3) != 0) { |
| 427 | result = STATE_CRITICAL; | 421 | result = STATE_CRITICAL; |
| 428 | xasprintf(&error_msg, _("invalid response received after authpass, ")); | 422 | xasprintf(&error_msg, _("invalid response received after authpass, ")); |
| 429 | break; | 423 | break; |
| 430 | } | 424 | } |
| 431 | break; | 425 | break; |
| 432 | } while (0); | 426 | } while (false); |
| 433 | } else { | 427 | } else { |
| 434 | result = STATE_CRITICAL; | 428 | result = STATE_CRITICAL; |
| 435 | xasprintf(&error_msg, _("only authtype LOGIN is supported, ")); | 429 | xasprintf(&error_msg, _("only authtype LOGIN is supported, ")); |
| @@ -437,243 +431,243 @@ main (int argc, char **argv) | |||
| 437 | } | 431 | } |
| 438 | 432 | ||
| 439 | /* tell the server we're done */ | 433 | /* tell the server we're done */ |
| 440 | smtp_quit(); | 434 | smtp_quit(config, buffer, socket_descriptor, ssl_established); |
| 441 | 435 | ||
| 442 | /* finally close the connection */ | 436 | /* finally close the connection */ |
| 443 | close (sd); | 437 | close(socket_descriptor); |
| 444 | } | 438 | } |
| 445 | 439 | ||
| 446 | /* reset the alarm */ | 440 | /* reset the alarm */ |
| 447 | alarm (0); | 441 | alarm(0); |
| 448 | 442 | ||
| 449 | microsec = deltime (tv); | 443 | long microsec = deltime(start_time); |
| 450 | elapsed_time = (double)microsec / 1.0e6; | 444 | double elapsed_time = (double)microsec / 1.0e6; |
| 451 | 445 | ||
| 452 | if (result == STATE_OK) { | 446 | if (result == STATE_OK) { |
| 453 | if (check_critical_time && elapsed_time > critical_time) | 447 | if (config.check_critical_time && elapsed_time > config.critical_time) { |
| 454 | result = STATE_CRITICAL; | 448 | result = STATE_CRITICAL; |
| 455 | else if (check_warning_time && elapsed_time > warning_time) | 449 | } else if (config.check_warning_time && elapsed_time > config.warning_time) { |
| 456 | result = STATE_WARNING; | 450 | result = STATE_WARNING; |
| 451 | } | ||
| 457 | } | 452 | } |
| 458 | 453 | ||
| 459 | printf (_("SMTP %s - %s%.3f sec. response time%s%s|%s\n"), | 454 | printf(_("SMTP %s - %s%.3f sec. response time%s%s|%s\n"), state_text(result), error_msg, elapsed_time, verbose ? ", " : "", |
| 460 | state_text (result), | 455 | verbose ? buffer : "", |
| 461 | error_msg, | 456 | fperfdata("time", elapsed_time, "s", config.check_warning_time, config.warning_time, config.check_critical_time, |
| 462 | elapsed_time, | 457 | config.critical_time, true, 0, false, 0)); |
| 463 | verbose?", ":"", verbose?buffer:"", | ||
| 464 | fperfdata ("time", elapsed_time, "s", | ||
| 465 | (int)check_warning_time, warning_time, | ||
| 466 | (int)check_critical_time, critical_time, | ||
| 467 | true, 0, false, 0)); | ||
| 468 | 458 | ||
| 469 | return result; | 459 | exit(result); |
| 470 | } | 460 | } |
| 471 | 461 | ||
| 472 | |||
| 473 | |||
| 474 | /* process command-line arguments */ | 462 | /* process command-line arguments */ |
| 475 | int | 463 | check_smtp_config_wrapper process_arguments(int argc, char **argv) { |
| 476 | process_arguments (int argc, char **argv) | ||
| 477 | { | ||
| 478 | int c; | ||
| 479 | char* temp; | ||
| 480 | |||
| 481 | bool implicit_tls = false; | ||
| 482 | |||
| 483 | enum { | 464 | enum { |
| 484 | SNI_OPTION | 465 | SNI_OPTION = CHAR_MAX + 1 |
| 485 | }; | 466 | }; |
| 486 | 467 | ||
| 487 | int option = 0; | 468 | int option = 0; |
| 488 | static struct option longopts[] = { | 469 | static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, |
| 489 | {"hostname", required_argument, 0, 'H'}, | 470 | {"expect", required_argument, 0, 'e'}, |
| 490 | {"expect", required_argument, 0, 'e'}, | 471 | {"critical", required_argument, 0, 'c'}, |
| 491 | {"critical", required_argument, 0, 'c'}, | 472 | {"warning", required_argument, 0, 'w'}, |
| 492 | {"warning", required_argument, 0, 'w'}, | 473 | {"timeout", required_argument, 0, 't'}, |
| 493 | {"timeout", required_argument, 0, 't'}, | 474 | {"port", required_argument, 0, 'p'}, |
| 494 | {"port", required_argument, 0, 'p'}, | 475 | {"from", required_argument, 0, 'f'}, |
| 495 | {"from", required_argument, 0, 'f'}, | 476 | {"fqdn", required_argument, 0, 'F'}, |
| 496 | {"fqdn", required_argument, 0, 'F'}, | 477 | {"authtype", required_argument, 0, 'A'}, |
| 497 | {"authtype", required_argument, 0, 'A'}, | 478 | {"authuser", required_argument, 0, 'U'}, |
| 498 | {"authuser", required_argument, 0, 'U'}, | 479 | {"authpass", required_argument, 0, 'P'}, |
| 499 | {"authpass", required_argument, 0, 'P'}, | 480 | {"command", required_argument, 0, 'C'}, |
| 500 | {"command", required_argument, 0, 'C'}, | 481 | {"response", required_argument, 0, 'R'}, |
| 501 | {"response", required_argument, 0, 'R'}, | 482 | {"verbose", no_argument, 0, 'v'}, |
| 502 | {"verbose", no_argument, 0, 'v'}, | 483 | {"version", no_argument, 0, 'V'}, |
| 503 | {"version", no_argument, 0, 'V'}, | 484 | {"use-ipv4", no_argument, 0, '4'}, |
| 504 | {"use-ipv4", no_argument, 0, '4'}, | 485 | {"use-ipv6", no_argument, 0, '6'}, |
| 505 | {"use-ipv6", no_argument, 0, '6'}, | 486 | {"help", no_argument, 0, 'h'}, |
| 506 | {"help", no_argument, 0, 'h'}, | 487 | {"lmtp", no_argument, 0, 'L'}, |
| 507 | {"lmtp", no_argument, 0, 'L'}, | 488 | {"ssl", no_argument, 0, 's'}, |
| 508 | {"ssl", no_argument, 0, 's'}, | 489 | {"tls", no_argument, 0, 's'}, |
| 509 | {"tls", no_argument, 0, 's'}, | 490 | {"starttls", no_argument, 0, 'S'}, |
| 510 | {"starttls",no_argument,0,'S'}, | 491 | {"sni", no_argument, 0, SNI_OPTION}, |
| 511 | {"sni", no_argument, 0, SNI_OPTION}, | 492 | {"certificate", required_argument, 0, 'D'}, |
| 512 | {"certificate",required_argument,0,'D'}, | 493 | {"ignore-quit-failure", no_argument, 0, 'q'}, |
| 513 | {"ignore-quit-failure",no_argument,0,'q'}, | 494 | {"proxy", no_argument, 0, 'r'}, |
| 514 | {"proxy",no_argument,0,'r'}, | 495 | {0, 0, 0, 0}}; |
| 515 | {0, 0, 0, 0} | 496 | |
| 497 | check_smtp_config_wrapper result = { | ||
| 498 | .config = check_smtp_config_init(), | ||
| 499 | .errorcode = OK, | ||
| 516 | }; | 500 | }; |
| 517 | 501 | ||
| 518 | if (argc < 2) | 502 | if (argc < 2) { |
| 519 | return ERROR; | 503 | result.errorcode = ERROR; |
| 504 | return result; | ||
| 505 | } | ||
| 520 | 506 | ||
| 521 | for (c = 1; c < argc; c++) { | 507 | for (int index = 1; index < argc; index++) { |
| 522 | if (strcmp ("-to", argv[c]) == 0) | 508 | if (strcmp("-to", argv[index]) == 0) { |
| 523 | strcpy (argv[c], "-t"); | 509 | strcpy(argv[index], "-t"); |
| 524 | else if (strcmp ("-wt", argv[c]) == 0) | 510 | } else if (strcmp("-wt", argv[index]) == 0) { |
| 525 | strcpy (argv[c], "-w"); | 511 | strcpy(argv[index], "-w"); |
| 526 | else if (strcmp ("-ct", argv[c]) == 0) | 512 | } else if (strcmp("-ct", argv[index]) == 0) { |
| 527 | strcpy (argv[c], "-c"); | 513 | strcpy(argv[index], "-c"); |
| 514 | } | ||
| 528 | } | 515 | } |
| 529 | 516 | ||
| 530 | while (1) { | 517 | int command_size = 0; |
| 531 | c = getopt_long (argc, argv, "+hVv46Lrt:p:f:e:c:w:H:C:R:sSD:F:A:U:P:q", | 518 | int response_size = 0; |
| 532 | longopts, &option); | 519 | bool implicit_tls = false; |
| 520 | int server_port_option = 0; | ||
| 521 | while (true) { | ||
| 522 | int opt_index = getopt_long(argc, argv, "+hVv46Lrt:p:f:e:c:w:H:C:R:sSD:F:A:U:P:q", longopts, &option); | ||
| 533 | 523 | ||
| 534 | if (c == -1 || c == EOF) | 524 | if (opt_index == -1 || opt_index == EOF) { |
| 535 | break; | 525 | break; |
| 526 | } | ||
| 536 | 527 | ||
| 537 | switch (c) { | 528 | switch (opt_index) { |
| 538 | case 'H': /* hostname */ | 529 | case 'H': /* hostname */ |
| 539 | if (is_host (optarg)) { | 530 | if (is_host(optarg)) { |
| 540 | server_address = optarg; | 531 | result.config.server_address = optarg; |
| 541 | } | 532 | } else { |
| 542 | else { | 533 | usage2(_("Invalid hostname/address"), optarg); |
| 543 | usage2 (_("Invalid hostname/address"), optarg); | ||
| 544 | } | 534 | } |
| 545 | break; | 535 | break; |
| 546 | case 'p': /* port */ | 536 | case 'p': /* port */ |
| 547 | if (is_intpos (optarg)) | 537 | if (is_intpos(optarg)) { |
| 548 | server_port_option = atoi (optarg); | 538 | server_port_option = atoi(optarg); |
| 549 | else | 539 | } else { |
| 550 | usage4 (_("Port must be a positive integer")); | 540 | usage4(_("Port must be a positive integer")); |
| 541 | } | ||
| 551 | break; | 542 | break; |
| 552 | case 'F': | 543 | case 'F': |
| 553 | /* localhostname */ | 544 | /* localhostname */ |
| 554 | localhostname = strdup(optarg); | 545 | result.config.localhostname = strdup(optarg); |
| 555 | break; | 546 | break; |
| 556 | case 'f': /* from argument */ | 547 | case 'f': /* from argument */ |
| 557 | from_arg = optarg + strspn(optarg, "<"); | 548 | result.config.from_arg = optarg + strspn(optarg, "<"); |
| 558 | from_arg = strndup(from_arg, strcspn(from_arg, ">")); | 549 | result.config.from_arg = strndup(result.config.from_arg, strcspn(result.config.from_arg, ">")); |
| 559 | send_mail_from = 1; | 550 | result.config.send_mail_from = true; |
| 560 | break; | 551 | break; |
| 561 | case 'A': | 552 | case 'A': |
| 562 | authtype = optarg; | 553 | result.config.authtype = optarg; |
| 563 | use_ehlo = true; | 554 | result.config.use_ehlo = true; |
| 564 | break; | 555 | break; |
| 565 | case 'U': | 556 | case 'U': |
| 566 | authuser = optarg; | 557 | result.config.authuser = optarg; |
| 567 | break; | 558 | break; |
| 568 | case 'P': | 559 | case 'P': |
| 569 | authpass = optarg; | 560 | result.config.authpass = optarg; |
| 570 | break; | 561 | break; |
| 571 | case 'e': /* server expect string on 220 */ | 562 | case 'e': /* server expect string on 220 */ |
| 572 | server_expect = optarg; | 563 | result.config.server_expect = optarg; |
| 573 | break; | 564 | break; |
| 574 | case 'C': /* commands */ | 565 | case 'C': /* commands */ |
| 575 | if (ncommands >= command_size) { | 566 | if (result.config.ncommands >= command_size) { |
| 576 | command_size+=8; | 567 | command_size += 8; |
| 577 | commands = realloc (commands, sizeof(char *) * command_size); | 568 | result.config.commands = realloc(result.config.commands, sizeof(char *) * command_size); |
| 578 | if (commands == NULL) | 569 | if (result.config.commands == NULL) { |
| 579 | die (STATE_UNKNOWN, | 570 | die(STATE_UNKNOWN, _("Could not realloc() units [%d]\n"), result.config.ncommands); |
| 580 | _("Could not realloc() units [%d]\n"), ncommands); | 571 | } |
| 581 | } | 572 | } |
| 582 | commands[ncommands] = (char *) malloc (sizeof(char) * 255); | 573 | result.config.commands[result.config.ncommands] = (char *)malloc(sizeof(char) * 255); |
| 583 | strncpy (commands[ncommands], optarg, 255); | 574 | strncpy(result.config.commands[result.config.ncommands], optarg, 255); |
| 584 | ncommands++; | 575 | result.config.ncommands++; |
| 585 | break; | 576 | break; |
| 586 | case 'R': /* server responses */ | 577 | case 'R': /* server responses */ |
| 587 | if (nresponses >= response_size) { | 578 | if (result.config.nresponses >= response_size) { |
| 588 | response_size += 8; | 579 | response_size += 8; |
| 589 | responses = realloc (responses, sizeof(char *) * response_size); | 580 | result.config.responses = realloc(result.config.responses, sizeof(char *) * response_size); |
| 590 | if (responses == NULL) | 581 | if (result.config.responses == NULL) { |
| 591 | die (STATE_UNKNOWN, | 582 | die(STATE_UNKNOWN, _("Could not realloc() units [%d]\n"), result.config.nresponses); |
| 592 | _("Could not realloc() units [%d]\n"), nresponses); | 583 | } |
| 593 | } | 584 | } |
| 594 | responses[nresponses] = (char *) malloc (sizeof(char) * 255); | 585 | result.config.responses[result.config.nresponses] = (char *)malloc(sizeof(char) * 255); |
| 595 | strncpy (responses[nresponses], optarg, 255); | 586 | strncpy(result.config.responses[result.config.nresponses], optarg, 255); |
| 596 | nresponses++; | 587 | result.config.nresponses++; |
| 597 | break; | 588 | break; |
| 598 | case 'c': /* critical time threshold */ | 589 | case 'c': /* critical time threshold */ |
| 599 | if (!is_nonnegative (optarg)) | 590 | if (!is_nonnegative(optarg)) { |
| 600 | usage4 (_("Critical time must be a positive")); | 591 | usage4(_("Critical time must be a positive")); |
| 601 | else { | 592 | } else { |
| 602 | critical_time = strtod (optarg, NULL); | 593 | result.config.critical_time = strtod(optarg, NULL); |
| 603 | check_critical_time = true; | 594 | result.config.check_critical_time = true; |
| 604 | } | 595 | } |
| 605 | break; | 596 | break; |
| 606 | case 'w': /* warning time threshold */ | 597 | case 'w': /* warning time threshold */ |
| 607 | if (!is_nonnegative (optarg)) | 598 | if (!is_nonnegative(optarg)) { |
| 608 | usage4 (_("Warning time must be a positive")); | 599 | usage4(_("Warning time must be a positive")); |
| 609 | else { | 600 | } else { |
| 610 | warning_time = strtod (optarg, NULL); | 601 | result.config.warning_time = strtod(optarg, NULL); |
| 611 | check_warning_time = true; | 602 | result.config.check_warning_time = true; |
| 612 | } | 603 | } |
| 613 | break; | 604 | break; |
| 614 | case 'v': /* verbose */ | 605 | case 'v': /* verbose */ |
| 615 | verbose++; | 606 | verbose++; |
| 616 | break; | 607 | break; |
| 617 | case 'q': | 608 | case 'q': |
| 618 | ignore_send_quit_failure = true; /* ignore problem sending QUIT */ | 609 | result.config.ignore_send_quit_failure = true; /* ignore problem sending QUIT */ |
| 619 | break; | 610 | break; |
| 620 | case 't': /* timeout */ | 611 | case 't': /* timeout */ |
| 621 | if (is_intnonneg (optarg)) { | 612 | if (is_intnonneg(optarg)) { |
| 622 | socket_timeout = atoi (optarg); | 613 | socket_timeout = atoi(optarg); |
| 623 | } | 614 | } else { |
| 624 | else { | 615 | usage4(_("Timeout interval must be a positive integer")); |
| 625 | usage4 (_("Timeout interval must be a positive integer")); | ||
| 626 | } | 616 | } |
| 627 | break; | 617 | break; |
| 628 | case 'D': | 618 | case 'D': { |
| 629 | /* Check SSL cert validity */ | 619 | /* Check SSL cert validity */ |
| 630 | #ifdef USE_OPENSSL | 620 | #ifdef USE_OPENSSL |
| 631 | if ((temp=strchr(optarg,','))!=NULL) { | 621 | char *temp; |
| 632 | *temp='\0'; | 622 | if ((temp = strchr(optarg, ',')) != NULL) { |
| 633 | if (!is_intnonneg (optarg)) | 623 | *temp = '\0'; |
| 634 | usage2 ("Invalid certificate expiration period", optarg); | 624 | if (!is_intnonneg(optarg)) { |
| 635 | days_till_exp_warn = atoi(optarg); | 625 | usage2("Invalid certificate expiration period", optarg); |
| 636 | *temp=','; | 626 | } |
| 637 | temp++; | 627 | result.config.days_till_exp_warn = atoi(optarg); |
| 638 | if (!is_intnonneg (temp)) | 628 | *temp = ','; |
| 639 | usage2 (_("Invalid certificate expiration period"), temp); | 629 | temp++; |
| 640 | days_till_exp_crit = atoi (temp); | 630 | if (!is_intnonneg(temp)) { |
| 641 | } | 631 | usage2(_("Invalid certificate expiration period"), temp); |
| 642 | else { | 632 | } |
| 643 | days_till_exp_crit=0; | 633 | result.config.days_till_exp_crit = atoi(temp); |
| 644 | if (!is_intnonneg (optarg)) | 634 | } else { |
| 645 | usage2 ("Invalid certificate expiration period", optarg); | 635 | result.config.days_till_exp_crit = 0; |
| 646 | days_till_exp_warn = atoi (optarg); | 636 | if (!is_intnonneg(optarg)) { |
| 647 | } | 637 | usage2("Invalid certificate expiration period", optarg); |
| 648 | check_cert = true; | 638 | } |
| 649 | ignore_send_quit_failure = true; | 639 | result.config.days_till_exp_warn = atoi(optarg); |
| 640 | } | ||
| 641 | result.config.check_cert = true; | ||
| 642 | result.config.ignore_send_quit_failure = true; | ||
| 650 | #else | 643 | #else |
| 651 | usage (_("SSL support not available - install OpenSSL and recompile")); | 644 | usage(_("SSL support not available - install OpenSSL and recompile")); |
| 652 | #endif | 645 | #endif |
| 653 | implicit_tls = true; | 646 | implicit_tls = true; |
| 654 | // fallthrough | 647 | // fallthrough |
| 655 | case 's': | 648 | case 's': |
| 656 | /* ssl */ | 649 | /* ssl */ |
| 657 | use_ssl = true; | 650 | result.config.use_ssl = true; |
| 658 | server_port = SMTPS_PORT; | 651 | result.config.server_port = SMTPS_PORT; |
| 659 | break; | 652 | break; |
| 660 | case 'S': | 653 | case 'S': |
| 661 | /* starttls */ | 654 | /* starttls */ |
| 662 | use_starttls = true; | 655 | result.config.use_starttls = true; |
| 663 | use_ehlo = true; | 656 | result.config.use_ehlo = true; |
| 664 | break; | 657 | break; |
| 658 | } | ||
| 665 | case SNI_OPTION: | 659 | case SNI_OPTION: |
| 666 | #ifdef HAVE_SSL | 660 | #ifdef HAVE_SSL |
| 667 | use_sni = true; | 661 | result.config.use_sni = true; |
| 668 | #else | 662 | #else |
| 669 | usage (_("SSL support not available - install OpenSSL and recompile")); | 663 | usage(_("SSL support not available - install OpenSSL and recompile")); |
| 670 | #endif | 664 | #endif |
| 671 | break; | 665 | break; |
| 672 | case 'r': | 666 | case 'r': |
| 673 | use_proxy_prefix = true; | 667 | result.config.use_proxy_prefix = true; |
| 674 | break; | 668 | break; |
| 675 | case 'L': | 669 | case 'L': |
| 676 | use_lhlo = true; | 670 | result.config.use_lhlo = true; |
| 677 | break; | 671 | break; |
| 678 | case '4': | 672 | case '4': |
| 679 | address_family = AF_INET; | 673 | address_family = AF_INET; |
| @@ -682,102 +676,79 @@ process_arguments (int argc, char **argv) | |||
| 682 | #ifdef USE_IPV6 | 676 | #ifdef USE_IPV6 |
| 683 | address_family = AF_INET6; | 677 | address_family = AF_INET6; |
| 684 | #else | 678 | #else |
| 685 | usage4 (_("IPv6 support not available")); | 679 | usage4(_("IPv6 support not available")); |
| 686 | #endif | 680 | #endif |
| 687 | break; | 681 | break; |
| 688 | case 'V': /* version */ | 682 | case 'V': /* version */ |
| 689 | print_revision (progname, NP_VERSION); | 683 | print_revision(progname, NP_VERSION); |
| 690 | exit (STATE_UNKNOWN); | 684 | exit(STATE_UNKNOWN); |
| 691 | case 'h': /* help */ | 685 | case 'h': /* help */ |
| 692 | print_help (); | 686 | print_help(); |
| 693 | exit (STATE_UNKNOWN); | 687 | exit(STATE_UNKNOWN); |
| 694 | case '?': /* help */ | 688 | case '?': /* help */ |
| 695 | usage5 (); | 689 | usage5(); |
| 696 | } | 690 | } |
| 697 | } | 691 | } |
| 698 | 692 | ||
| 699 | c = optind; | 693 | int c = optind; |
| 700 | if (server_address == NULL) { | 694 | if (result.config.server_address == NULL) { |
| 701 | if (argv[c]) { | 695 | if (argv[c]) { |
| 702 | if (is_host (argv[c])) | 696 | if (is_host(argv[c])) { |
| 703 | server_address = argv[c]; | 697 | result.config.server_address = argv[c]; |
| 704 | else | 698 | } else { |
| 705 | usage2 (_("Invalid hostname/address"), argv[c]); | 699 | usage2(_("Invalid hostname/address"), argv[c]); |
| 706 | } | 700 | } |
| 707 | else { | 701 | } else { |
| 708 | xasprintf (&server_address, "127.0.0.1"); | 702 | result.config.server_address = strdup("localhost"); |
| 709 | } | 703 | } |
| 710 | } | 704 | } |
| 711 | 705 | ||
| 712 | if (server_expect == NULL) | 706 | if (result.config.use_starttls && result.config.use_ssl) { |
| 713 | server_expect = strdup (SMTP_EXPECT); | ||
| 714 | |||
| 715 | if (mail_command == NULL) | ||
| 716 | mail_command = strdup("MAIL "); | ||
| 717 | |||
| 718 | if (from_arg==NULL) | ||
| 719 | from_arg = strdup(" "); | ||
| 720 | |||
| 721 | if (use_starttls && use_ssl) { | ||
| 722 | if (implicit_tls) { | 707 | if (implicit_tls) { |
| 723 | use_ssl = false; | 708 | result.config.use_ssl = false; |
| 724 | server_port = SMTP_PORT; | ||
| 725 | } else { | 709 | } else { |
| 726 | usage4 (_("Set either -s/--ssl/--tls or -S/--starttls")); | 710 | usage4(_("Set either -s/--ssl/--tls or -S/--starttls")); |
| 727 | } | 711 | } |
| 728 | } | 712 | } |
| 729 | 713 | ||
| 730 | if (server_port_option != 0) { | 714 | if (server_port_option != 0) { |
| 731 | server_port = server_port_option; | 715 | result.config.server_port = server_port_option; |
| 732 | } | 716 | } |
| 733 | 717 | ||
| 734 | return validate_arguments (); | 718 | return result; |
| 735 | } | ||
| 736 | |||
| 737 | |||
| 738 | |||
| 739 | int | ||
| 740 | validate_arguments (void) | ||
| 741 | { | ||
| 742 | return OK; | ||
| 743 | } | 719 | } |
| 744 | 720 | ||
| 745 | 721 | char *smtp_quit(check_smtp_config config, char buffer[MAX_INPUT_BUFFER], int socket_descriptor, bool ssl_established) { | |
| 746 | void | 722 | int sent_bytes = my_send(config, SMTP_QUIT, strlen(SMTP_QUIT), socket_descriptor, ssl_established); |
| 747 | smtp_quit(void) | 723 | if (sent_bytes < 0) { |
| 748 | { | 724 | if (config.ignore_send_quit_failure) { |
| 749 | int bytes; | 725 | if (verbose) { |
| 750 | int n; | ||
| 751 | |||
| 752 | n = my_send(SMTP_QUIT, strlen(SMTP_QUIT)); | ||
| 753 | if(n < 0) { | ||
| 754 | if(ignore_send_quit_failure) { | ||
| 755 | if(verbose) { | ||
| 756 | printf(_("Connection closed by server before sending QUIT command\n")); | 726 | printf(_("Connection closed by server before sending QUIT command\n")); |
| 757 | } | 727 | } |
| 758 | return; | 728 | return buffer; |
| 759 | } | 729 | } |
| 760 | die (STATE_UNKNOWN, | 730 | die(STATE_UNKNOWN, _("Connection closed by server before sending QUIT command\n")); |
| 761 | _("Connection closed by server before sending QUIT command\n")); | ||
| 762 | } | 731 | } |
| 763 | 732 | ||
| 764 | if (verbose) | 733 | if (verbose) { |
| 765 | printf(_("sent %s\n"), "QUIT"); | 734 | printf(_("sent %s\n"), "QUIT"); |
| 735 | } | ||
| 766 | 736 | ||
| 767 | /* read the response but don't care about problems */ | 737 | /* read the response but don't care about problems */ |
| 768 | bytes = recvlines(buffer, MAX_INPUT_BUFFER); | 738 | int bytes = recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established); |
| 769 | if (verbose) { | 739 | if (verbose) { |
| 770 | if (bytes < 0) | 740 | if (bytes < 0) { |
| 771 | printf(_("recv() failed after QUIT.")); | 741 | printf(_("recv() failed after QUIT.")); |
| 772 | else if (bytes == 0) | 742 | } else if (bytes == 0) { |
| 773 | printf(_("Connection reset by peer.")); | 743 | printf(_("Connection reset by peer.")); |
| 774 | else { | 744 | } else { |
| 775 | buffer[bytes] = '\0'; | 745 | buffer[bytes] = '\0'; |
| 776 | printf(_("received %s\n"), buffer); | 746 | printf(_("received %s\n"), buffer); |
| 777 | } | 747 | } |
| 778 | } | 748 | } |
| 779 | } | ||
| 780 | 749 | ||
| 750 | return buffer; | ||
| 751 | } | ||
| 781 | 752 | ||
| 782 | /* | 753 | /* |
| 783 | * Receive one line, copy it into buf and nul-terminate it. Returns the | 754 | * Receive one line, copy it into buf and nul-terminate it. Returns the |
| @@ -788,24 +759,22 @@ smtp_quit(void) | |||
| 788 | * function which buffers the data, move that to netutils.c and change | 759 | * function which buffers the data, move that to netutils.c and change |
| 789 | * check_smtp and other plugins to use that. Also, remove (\r)\n. | 760 | * check_smtp and other plugins to use that. Also, remove (\r)\n. |
| 790 | */ | 761 | */ |
| 791 | int | 762 | int recvline(char *buf, size_t bufsize, check_smtp_config config, int socket_descriptor, bool ssl_established) { |
| 792 | recvline(char *buf, size_t bufsize) | ||
| 793 | { | ||
| 794 | int result; | 763 | int result; |
| 795 | unsigned i; | 764 | int counter; |
| 796 | 765 | ||
| 797 | for (i = result = 0; i < bufsize - 1; i++) { | 766 | for (counter = result = 0; counter < bufsize - 1; counter++) { |
| 798 | if ((result = my_recv(&buf[i], 1)) != 1) | 767 | if ((result = my_recv(config, &buf[counter], 1, socket_descriptor, ssl_established)) != 1) { |
| 799 | break; | 768 | break; |
| 800 | if (buf[i] == '\n') { | 769 | } |
| 801 | buf[++i] = '\0'; | 770 | if (buf[counter] == '\n') { |
| 802 | return i; | 771 | buf[++counter] = '\0'; |
| 772 | return counter; | ||
| 803 | } | 773 | } |
| 804 | } | 774 | } |
| 805 | return (result == 1 || i == 0) ? -2 : result; /* -2 if out of space */ | 775 | return (result == 1 || counter == 0) ? -2 : result; /* -2 if out of space */ |
| 806 | } | 776 | } |
| 807 | 777 | ||
| 808 | |||
| 809 | /* | 778 | /* |
| 810 | * Receive one or more lines, copy them into buf and nul-terminate it. Returns | 779 | * Receive one or more lines, copy them into buf and nul-terminate it. Returns |
| 811 | * the number of bytes written to buf (excluding the '\0') or 0 on EOF or <0 on | 780 | * the number of bytes written to buf (excluding the '\0') or 0 on EOF or <0 on |
| @@ -820,117 +789,103 @@ recvline(char *buf, size_t bufsize) | |||
| 820 | * | 789 | * |
| 821 | * TODO: Move this to netutils.c. Also, remove \r and possibly the final \n. | 790 | * TODO: Move this to netutils.c. Also, remove \r and possibly the final \n. |
| 822 | */ | 791 | */ |
| 823 | int | 792 | int recvlines(check_smtp_config config, char *buf, size_t bufsize, int socket_descriptor, bool ssl_established) { |
| 824 | recvlines(char *buf, size_t bufsize) | 793 | int result; |
| 825 | { | 794 | int counter; |
| 826 | int result, i; | 795 | |
| 827 | 796 | for (counter = 0; /* forever */; counter += result) { | |
| 828 | for (i = 0; /* forever */; i += result) | 797 | if (!((result = recvline(buf + counter, bufsize - counter, config, socket_descriptor, ssl_established)) > 3 && |
| 829 | if (!((result = recvline(buf + i, bufsize - i)) > 3 && | 798 | isdigit((int)buf[counter]) && isdigit((int)buf[counter + 1]) && isdigit((int)buf[counter + 2]) && buf[counter + 3] == '-')) { |
| 830 | isdigit((int)buf[i]) && | ||
| 831 | isdigit((int)buf[i + 1]) && | ||
| 832 | isdigit((int)buf[i + 2]) && | ||
| 833 | buf[i + 3] == '-')) | ||
| 834 | break; | 799 | break; |
| 800 | } | ||
| 801 | } | ||
| 835 | 802 | ||
| 836 | return (result <= 0) ? result : result + i; | 803 | return (result <= 0) ? result : result + counter; |
| 837 | } | 804 | } |
| 838 | 805 | ||
| 839 | 806 | int my_close(int socket_descriptor) { | |
| 840 | int | ||
| 841 | my_close (void) | ||
| 842 | { | ||
| 843 | int result; | 807 | int result; |
| 844 | result = close(sd); | 808 | result = close(socket_descriptor); |
| 845 | #ifdef HAVE_SSL | 809 | #ifdef HAVE_SSL |
| 846 | np_net_ssl_cleanup(); | 810 | np_net_ssl_cleanup(); |
| 847 | #endif | 811 | #endif |
| 848 | return result; | 812 | return result; |
| 849 | } | 813 | } |
| 850 | 814 | ||
| 851 | 815 | void print_help(void) { | |
| 852 | void | ||
| 853 | print_help (void) | ||
| 854 | { | ||
| 855 | char *myport; | 816 | char *myport; |
| 856 | xasprintf (&myport, "%d", SMTP_PORT); | 817 | xasprintf(&myport, "%d", SMTP_PORT); |
| 857 | 818 | ||
| 858 | print_revision (progname, NP_VERSION); | 819 | print_revision(progname, NP_VERSION); |
| 859 | 820 | ||
| 860 | printf ("Copyright (c) 1999-2001 Ethan Galstad <nagios@nagios.org>\n"); | 821 | printf("Copyright (c) 1999-2001 Ethan Galstad <nagios@nagios.org>\n"); |
| 861 | printf (COPYRIGHT, copyright, email); | 822 | printf(COPYRIGHT, copyright, email); |
| 862 | 823 | ||
| 863 | printf("%s\n", _("This plugin will attempt to open an SMTP connection with the host.")); | 824 | printf("%s\n", _("This plugin will attempt to open an SMTP connection with the host.")); |
| 864 | 825 | ||
| 865 | printf ("\n\n"); | 826 | printf("\n\n"); |
| 866 | 827 | ||
| 867 | print_usage (); | 828 | print_usage(); |
| 868 | 829 | ||
| 869 | printf (UT_HELP_VRSN); | 830 | printf(UT_HELP_VRSN); |
| 870 | printf (UT_EXTRA_OPTS); | 831 | printf(UT_EXTRA_OPTS); |
| 871 | 832 | ||
| 872 | printf (UT_HOST_PORT, 'p', myport); | 833 | printf(UT_HOST_PORT, 'p', myport); |
| 873 | 834 | ||
| 874 | printf (UT_IPv46); | 835 | printf(UT_IPv46); |
| 875 | 836 | ||
| 876 | printf (" %s\n", "-e, --expect=STRING"); | 837 | printf(" %s\n", "-e, --expect=STRING"); |
| 877 | printf (_(" String to expect in first line of server response (default: '%s')\n"), SMTP_EXPECT); | 838 | printf(_(" String to expect in first line of server response (default: '%s')\n"), SMTP_EXPECT); |
| 878 | printf (" %s\n", "-C, --command=STRING"); | 839 | printf(" %s\n", "-C, --command=STRING"); |
| 879 | printf (" %s\n", _("SMTP command (may be used repeatedly)")); | 840 | printf(" %s\n", _("SMTP command (may be used repeatedly)")); |
| 880 | printf (" %s\n", "-R, --response=STRING"); | 841 | printf(" %s\n", "-R, --response=STRING"); |
| 881 | printf (" %s\n", _("Expected response to command (may be used repeatedly)")); | 842 | printf(" %s\n", _("Expected response to command (may be used repeatedly)")); |
| 882 | printf (" %s\n", "-f, --from=STRING"); | 843 | printf(" %s\n", "-f, --from=STRING"); |
| 883 | printf (" %s\n", _("FROM-address to include in MAIL command, required by Exchange 2000")), | 844 | printf(" %s\n", _("FROM-address to include in MAIL command, required by Exchange 2000")), printf(" %s\n", "-F, --fqdn=STRING"); |
| 884 | printf (" %s\n", "-F, --fqdn=STRING"); | 845 | printf(" %s\n", _("FQDN used for HELO")); |
| 885 | printf (" %s\n", _("FQDN used for HELO")); | 846 | printf(" %s\n", "-r, --proxy"); |
| 886 | printf (" %s\n", "-r, --proxy"); | 847 | printf(" %s\n", _("Use PROXY protocol prefix for the connection.")); |
| 887 | printf (" %s\n", _("Use PROXY protocol prefix for the connection.")); | ||
| 888 | #ifdef HAVE_SSL | 848 | #ifdef HAVE_SSL |
| 889 | printf (" %s\n", "-D, --certificate=INTEGER[,INTEGER]"); | 849 | printf(" %s\n", "-D, --certificate=INTEGER[,INTEGER]"); |
| 890 | printf (" %s\n", _("Minimum number of days a certificate has to be valid.")); | 850 | printf(" %s\n", _("Minimum number of days a certificate has to be valid.")); |
| 891 | printf (" %s\n", "-s, --ssl, --tls"); | 851 | printf(" %s\n", "-s, --ssl, --tls"); |
| 892 | printf (" %s\n", _("Use SSL/TLS for the connection.")); | 852 | printf(" %s\n", _("Use SSL/TLS for the connection.")); |
| 893 | printf (_(" Sets default port to %d.\n"), SMTPS_PORT); | 853 | printf(_(" Sets default port to %d.\n"), SMTPS_PORT); |
| 894 | printf (" %s\n", "-S, --starttls"); | 854 | printf(" %s\n", "-S, --starttls"); |
| 895 | printf (" %s\n", _("Use STARTTLS for the connection.")); | 855 | printf(" %s\n", _("Use STARTTLS for the connection.")); |
| 896 | printf (" %s\n", "--sni"); | 856 | printf(" %s\n", "--sni"); |
| 897 | printf (" %s\n", _("Enable SSL/TLS hostname extension support (SNI)")); | 857 | printf(" %s\n", _("Enable SSL/TLS hostname extension support (SNI)")); |
| 898 | #endif | 858 | #endif |
| 899 | 859 | ||
| 900 | printf (" %s\n", "-A, --authtype=STRING"); | 860 | printf(" %s\n", "-A, --authtype=STRING"); |
| 901 | printf (" %s\n", _("SMTP AUTH type to check (default none, only LOGIN supported)")); | 861 | printf(" %s\n", _("SMTP AUTH type to check (default none, only LOGIN supported)")); |
| 902 | printf (" %s\n", "-U, --authuser=STRING"); | 862 | printf(" %s\n", "-U, --authuser=STRING"); |
| 903 | printf (" %s\n", _("SMTP AUTH username")); | 863 | printf(" %s\n", _("SMTP AUTH username")); |
| 904 | printf (" %s\n", "-P, --authpass=STRING"); | 864 | printf(" %s\n", "-P, --authpass=STRING"); |
| 905 | printf (" %s\n", _("SMTP AUTH password")); | 865 | printf(" %s\n", _("SMTP AUTH password")); |
| 906 | printf (" %s\n", "-L, --lmtp"); | 866 | printf(" %s\n", "-L, --lmtp"); |
| 907 | printf (" %s\n", _("Send LHLO instead of HELO/EHLO")); | 867 | printf(" %s\n", _("Send LHLO instead of HELO/EHLO")); |
| 908 | printf (" %s\n", "-q, --ignore-quit-failure"); | 868 | printf(" %s\n", "-q, --ignore-quit-failure"); |
| 909 | printf (" %s\n", _("Ignore failure when sending QUIT command to server")); | 869 | printf(" %s\n", _("Ignore failure when sending QUIT command to server")); |
| 910 | |||
| 911 | printf (UT_WARN_CRIT); | ||
| 912 | 870 | ||
| 913 | printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); | 871 | printf(UT_WARN_CRIT); |
| 914 | 872 | ||
| 915 | printf (UT_VERBOSE); | 873 | printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); |
| 874 | |||
| 875 | printf(UT_VERBOSE); | ||
| 916 | 876 | ||
| 917 | printf("\n"); | 877 | printf("\n"); |
| 918 | printf ("%s\n", _("Successful connects return STATE_OK, refusals and timeouts return")); | 878 | printf("%s\n", _("Successful connects return STATE_OK, refusals and timeouts return")); |
| 919 | printf ("%s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful")); | 879 | printf("%s\n", _("STATE_CRITICAL, other errors return STATE_UNKNOWN. Successful")); |
| 920 | printf ("%s\n", _("connects, but incorrect response messages from the host result in")); | 880 | printf("%s\n", _("connects, but incorrect response messages from the host result in")); |
| 921 | printf ("%s\n", _("STATE_WARNING return values.")); | 881 | printf("%s\n", _("STATE_WARNING return values.")); |
| 922 | 882 | ||
| 923 | printf (UT_SUPPORT); | 883 | printf(UT_SUPPORT); |
| 924 | } | 884 | } |
| 925 | 885 | ||
| 926 | 886 | void print_usage(void) { | |
| 927 | 887 | printf("%s\n", _("Usage:")); | |
| 928 | void | 888 | printf("%s -H host [-p port] [-4|-6] [-e expect] [-C command] [-R response] [-f from addr]\n", progname); |
| 929 | print_usage (void) | 889 | printf("[-A authtype -U authuser -P authpass] [-w warn] [-c crit] [-t timeout] [-q]\n"); |
| 930 | { | 890 | printf("[-F fqdn] [-S] [-L] [-D warn days cert expire[,crit days cert expire]] [-r] [--sni] [-v] \n"); |
| 931 | printf ("%s\n", _("Usage:")); | ||
| 932 | printf ("%s -H host [-p port] [-4|-6] [-e expect] [-C command] [-R response] [-f from addr]\n", progname); | ||
| 933 | printf ("[-A authtype -U authuser -P authpass] [-w warn] [-c crit] [-t timeout] [-q]\n"); | ||
| 934 | printf ("[-F fqdn] [-S] [-L] [-D warn days cert expire[,crit days cert expire]] [-r] [--sni] [-v] \n"); | ||
| 935 | } | 891 | } |
| 936 | |||
diff --git a/plugins/check_smtp.d/config.h b/plugins/check_smtp.d/config.h new file mode 100644 index 00000000..0a6511ef --- /dev/null +++ b/plugins/check_smtp.d/config.h | |||
| @@ -0,0 +1,92 @@ | |||
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "../../config.h" | ||
| 4 | #include <stddef.h> | ||
| 5 | #include <string.h> | ||
| 6 | |||
| 7 | enum { | ||
| 8 | SMTP_PORT = 25, | ||
| 9 | SMTPS_PORT = 465 | ||
| 10 | }; | ||
| 11 | |||
| 12 | #define SMTP_EXPECT "220" | ||
| 13 | |||
| 14 | typedef struct { | ||
| 15 | int server_port; | ||
| 16 | char *server_address; | ||
| 17 | char *localhostname; | ||
| 18 | char *server_expect; | ||
| 19 | bool ignore_send_quit_failure; | ||
| 20 | |||
| 21 | double warning_time; | ||
| 22 | bool check_warning_time; | ||
| 23 | double critical_time; | ||
| 24 | bool check_critical_time; | ||
| 25 | bool use_ehlo; | ||
| 26 | bool use_lhlo; | ||
| 27 | |||
| 28 | char *from_arg; | ||
| 29 | bool send_mail_from; | ||
| 30 | |||
| 31 | int ncommands; | ||
| 32 | char **commands; | ||
| 33 | |||
| 34 | int nresponses; | ||
| 35 | char **responses; | ||
| 36 | |||
| 37 | char *authtype; | ||
| 38 | char *authuser; | ||
| 39 | char *authpass; | ||
| 40 | |||
| 41 | bool use_proxy_prefix; | ||
| 42 | #ifdef HAVE_SSL | ||
| 43 | bool check_cert; | ||
| 44 | int days_till_exp_warn; | ||
| 45 | int days_till_exp_crit; | ||
| 46 | bool use_ssl; | ||
| 47 | bool use_starttls; | ||
| 48 | bool use_sni; | ||
| 49 | #endif | ||
| 50 | } check_smtp_config; | ||
| 51 | |||
| 52 | check_smtp_config check_smtp_config_init() { | ||
| 53 | check_smtp_config tmp = { | ||
| 54 | .server_port = SMTP_PORT, | ||
| 55 | .server_address = NULL, | ||
| 56 | .localhostname = NULL, | ||
| 57 | |||
| 58 | .server_expect = SMTP_EXPECT, | ||
| 59 | .ignore_send_quit_failure = false, | ||
| 60 | |||
| 61 | .warning_time = 0, | ||
| 62 | .check_warning_time = false, | ||
| 63 | .critical_time = 0, | ||
| 64 | .check_critical_time = false, | ||
| 65 | .use_ehlo = false, | ||
| 66 | .use_lhlo = false, | ||
| 67 | |||
| 68 | .from_arg = strdup(" "), | ||
| 69 | .send_mail_from = false, | ||
| 70 | |||
| 71 | .ncommands = 0, | ||
| 72 | .commands = NULL, | ||
| 73 | |||
| 74 | .nresponses = 0, | ||
| 75 | .responses = NULL, | ||
| 76 | |||
| 77 | .authtype = NULL, | ||
| 78 | .authuser = NULL, | ||
| 79 | .authpass = NULL, | ||
| 80 | |||
| 81 | .use_proxy_prefix = false, | ||
| 82 | #ifdef HAVE_SSL | ||
| 83 | .check_cert = false, | ||
| 84 | .days_till_exp_warn = 0, | ||
| 85 | .days_till_exp_crit = 0, | ||
| 86 | .use_ssl = false, | ||
| 87 | .use_starttls = false, | ||
| 88 | .use_sni = false, | ||
| 89 | #endif | ||
| 90 | }; | ||
| 91 | return tmp; | ||
| 92 | } | ||
