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