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.c811
1 files changed, 494 insertions, 317 deletions
diff --git a/plugins/check_smtp.c b/plugins/check_smtp.c
index 44b735f9..e806ad29 100644
--- a/plugins/check_smtp.c
+++ b/plugins/check_smtp.c
@@ -28,20 +28,25 @@
28 * 28 *
29 *****************************************************************************/ 29 *****************************************************************************/
30 30
31const char *progname = "check_smtp";
32const char *copyright = "2000-2024";
33const char *email = "devel@monitoring-plugins.org";
34
35#include "common.h" 31#include "common.h"
36#include "netutils.h" 32#include "netutils.h"
33#include "output.h"
34#include "perfdata.h"
35#include "thresholds.h"
37#include "utils.h" 36#include "utils.h"
38#include "base64.h" 37#include "base64.h"
39#include "regex.h" 38#include "regex.h"
40 39
40#include <bits/getopt_ext.h>
41#include <ctype.h> 41#include <ctype.h>
42#include <string.h>
42#include "check_smtp.d/config.h" 43#include "check_smtp.d/config.h"
43#include "../lib/states.h" 44#include "../lib/states.h"
44 45
46const char *progname = "check_smtp";
47const char *copyright = "2000-2024";
48const char *email = "devel@monitoring-plugins.org";
49
45#define PROXY_PREFIX "PROXY TCP4 0.0.0.0 0.0.0.0 25 25\r\n" 50#define PROXY_PREFIX "PROXY TCP4 0.0.0.0 0.0.0.0 25 25\r\n"
46#define SMTP_HELO "HELO " 51#define SMTP_HELO "HELO "
47#define SMTP_EHLO "EHLO " 52#define SMTP_EHLO "EHLO "
@@ -58,7 +63,8 @@ typedef struct {
58} check_smtp_config_wrapper; 63} check_smtp_config_wrapper;
59static check_smtp_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/); 64static check_smtp_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
60 65
61int my_recv(check_smtp_config config, void *buf, int num, int socket_descriptor, bool ssl_established) { 66int my_recv(check_smtp_config config, void *buf, int num, int socket_descriptor,
67 bool ssl_established) {
62#ifdef HAVE_SSL 68#ifdef HAVE_SSL
63 if ((config.use_starttls || config.use_ssl) && ssl_established) { 69 if ((config.use_starttls || config.use_ssl) && ssl_established) {
64 return np_net_ssl_read(buf, num); 70 return np_net_ssl_read(buf, num);
@@ -69,7 +75,8 @@ int my_recv(check_smtp_config config, void *buf, int num, int socket_descriptor,
69#endif 75#endif
70} 76}
71 77
72int my_send(check_smtp_config config, void *buf, int num, int socket_descriptor, bool ssl_established) { 78int my_send(check_smtp_config config, void *buf, int num, int socket_descriptor,
79 bool ssl_established) {
73#ifdef HAVE_SSL 80#ifdef HAVE_SSL
74 if ((config.use_starttls || config.use_ssl) && ssl_established) { 81 if ((config.use_starttls || config.use_ssl) && ssl_established) {
75 82
@@ -83,10 +90,12 @@ int my_send(check_smtp_config config, void *buf, int num, int socket_descriptor,
83 90
84static void print_help(void); 91static void print_help(void);
85void print_usage(void); 92void print_usage(void);
86static char *smtp_quit(check_smtp_config /*config*/, char /*buffer*/[MAX_INPUT_BUFFER], int /*socket_descriptor*/, 93static char *smtp_quit(check_smtp_config /*config*/, char /*buffer*/[MAX_INPUT_BUFFER],
87 bool /*ssl_established*/); 94 int /*socket_descriptor*/, bool /*ssl_established*/);
88static int recvline(char * /*buf*/, size_t /*bufsize*/, check_smtp_config /*config*/, int /*socket_descriptor*/, bool /*ssl_established*/); 95static int recvline(char * /*buf*/, size_t /*bufsize*/, check_smtp_config /*config*/,
89static int recvlines(check_smtp_config /*config*/, char * /*buf*/, size_t /*bufsize*/, int /*socket_descriptor*/, bool /*ssl_established*/); 96 int /*socket_descriptor*/, bool /*ssl_established*/);
97static int recvlines(check_smtp_config /*config*/, char * /*buf*/, size_t /*bufsize*/,
98 int /*socket_descriptor*/, bool /*ssl_established*/);
90static int my_close(int /*socket_descriptor*/); 99static int my_close(int /*socket_descriptor*/);
91 100
92static int verbose = 0; 101static int verbose = 0;
@@ -107,6 +116,10 @@ int main(int argc, char **argv) {
107 116
108 const check_smtp_config config = tmp_config.config; 117 const check_smtp_config config = tmp_config.config;
109 118
119 if (config.output_format_is_set) {
120 mp_set_format(config.output_format);
121 }
122
110 /* If localhostname not set on command line, use gethostname to set */ 123 /* If localhostname not set on command line, use gethostname to set */
111 char *localhostname = config.localhostname; 124 char *localhostname = config.localhostname;
112 if (!localhostname) { 125 if (!localhostname) {
@@ -157,342 +170,459 @@ int main(int argc, char **argv) {
157 gettimeofday(&start_time, NULL); 170 gettimeofday(&start_time, NULL);
158 171
159 int socket_descriptor = 0; 172 int socket_descriptor = 0;
173
160 /* try to connect to the host at the given port number */ 174 /* try to connect to the host at the given port number */
161 mp_state_enum result = my_tcp_connect(config.server_address, config.server_port, &socket_descriptor); 175 mp_state_enum tcp_result =
176 my_tcp_connect(config.server_address, config.server_port, &socket_descriptor);
162 177
163 char *error_msg = ""; 178 mp_check overall = mp_check_init();
179 mp_subcheck sc_tcp_connect = mp_subcheck_init();
164 char buffer[MAX_INPUT_BUFFER]; 180 char buffer[MAX_INPUT_BUFFER];
165 bool ssl_established = false; 181 bool ssl_established = false;
166 if (result == STATE_OK) { /* we connected */ 182
167 /* If requested, send PROXY header */ 183 if (tcp_result != STATE_OK) {
168 if (config.use_proxy_prefix) { 184 // Connect failed
169 if (verbose) { 185 sc_tcp_connect = mp_set_subcheck_state(sc_tcp_connect, STATE_CRITICAL);
170 printf("Sending header %s\n", PROXY_PREFIX); 186 xasprintf(&sc_tcp_connect.output, "TCP connect to '%s' failed", config.server_address);
171 } 187 mp_add_subcheck_to_check(&overall, sc_tcp_connect);
172 my_send(config, PROXY_PREFIX, strlen(PROXY_PREFIX), socket_descriptor, ssl_established); 188 mp_exit(overall);
189 }
190
191 /* we connected */
192 /* If requested, send PROXY header */
193 if (config.use_proxy_prefix) {
194 if (verbose) {
195 printf("Sending header %s\n", PROXY_PREFIX);
173 } 196 }
197 my_send(config, PROXY_PREFIX, strlen(PROXY_PREFIX), socket_descriptor, ssl_established);
198 }
174 199
175#ifdef HAVE_SSL 200#ifdef HAVE_SSL
176 if (config.use_ssl) { 201 if (config.use_ssl) {
177 result = np_net_ssl_init_with_hostname(socket_descriptor, (config.use_sni ? config.server_address : NULL)); 202 int tls_result = np_net_ssl_init_with_hostname(
178 if (result != STATE_OK) { 203 socket_descriptor, (config.use_sni ? config.server_address : NULL));
179 printf(_("CRITICAL - Cannot create SSL context.\n")); 204
180 close(socket_descriptor); 205 mp_subcheck sc_tls_connection = mp_subcheck_init();
181 np_net_ssl_cleanup(); 206
182 exit(STATE_CRITICAL); 207 if (tls_result != STATE_OK) {
183 } 208 close(socket_descriptor);
184 ssl_established = true; 209 np_net_ssl_cleanup();
210
211 sc_tls_connection = mp_set_subcheck_state(sc_tls_connection, STATE_CRITICAL);
212 xasprintf(&sc_tls_connection.output, "cannot create TLS context");
213 mp_add_subcheck_to_check(&overall, sc_tls_connection);
214 mp_exit(overall);
185 } 215 }
216
217 sc_tls_connection = mp_set_subcheck_state(sc_tls_connection, STATE_OK);
218 xasprintf(&sc_tls_connection.output, "TLS context established");
219 mp_add_subcheck_to_check(&overall, sc_tls_connection);
220 ssl_established = true;
221 }
186#endif 222#endif
187 223
188 /* watch for the SMTP connection string and */ 224 /* watch for the SMTP connection string and */
189 /* return a WARNING status if we couldn't read any data */ 225 /* return a WARNING status if we couldn't read any data */
190 if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) <= 0) { 226 if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) <= 0) {
191 printf(_("recv() failed\n")); 227 mp_subcheck sc_read_data = mp_subcheck_init();
192 exit(STATE_WARNING); 228 sc_read_data = mp_set_subcheck_state(sc_read_data, STATE_WARNING);
193 } 229 xasprintf(&sc_read_data.output, "recv() failed");
230 mp_add_subcheck_to_check(&overall, sc_read_data);
231 mp_exit(overall);
232 }
194 233
195 char *server_response = NULL; 234 char *server_response = NULL;
196 /* save connect return (220 hostname ..) for later use */ 235 /* save connect return (220 hostname ..) for later use */
197 xasprintf(&server_response, "%s", buffer); 236 xasprintf(&server_response, "%s", buffer);
198 237
199 /* send the HELO/EHLO command */ 238 /* send the HELO/EHLO command */
200 my_send(config, helocmd, (int)strlen(helocmd), socket_descriptor, ssl_established); 239 my_send(config, helocmd, (int)strlen(helocmd), socket_descriptor, ssl_established);
201 240
202 /* allow for response to helo command to reach us */ 241 /* allow for response to helo command to reach us */
203 if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) <= 0) { 242 if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) <= 0) {
204 printf(_("recv() failed\n")); 243 mp_subcheck sc_read_data = mp_subcheck_init();
205 exit(STATE_WARNING); 244 sc_read_data = mp_set_subcheck_state(sc_read_data, STATE_WARNING);
206 } 245 xasprintf(&sc_read_data.output, "recv() failed");
246 mp_add_subcheck_to_check(&overall, sc_read_data);
247 mp_exit(overall);
248 }
207 249
208 bool supports_tls = false; 250 bool supports_tls = false;
209 if (config.use_ehlo || config.use_lhlo) { 251 if (config.use_ehlo || config.use_lhlo) {
210 if (strstr(buffer, "250 STARTTLS") != NULL || strstr(buffer, "250-STARTTLS") != NULL) { 252 if (strstr(buffer, "250 STARTTLS") != NULL || strstr(buffer, "250-STARTTLS") != NULL) {
211 supports_tls = true; 253 supports_tls = true;
212 }
213 } 254 }
255 }
214 256
215 if (config.use_starttls && !supports_tls) { 257 if (config.use_starttls && !supports_tls) {
216 printf(_("WARNING - TLS not supported by server\n")); 258 smtp_quit(config, buffer, socket_descriptor, ssl_established);
259
260 mp_subcheck sc_read_data = mp_subcheck_init();
261 sc_read_data = mp_set_subcheck_state(sc_read_data, STATE_WARNING);
262 xasprintf(&sc_read_data.output, "StartTLS not supported by server");
263 mp_add_subcheck_to_check(&overall, sc_read_data);
264 mp_exit(overall);
265 }
266
267#ifdef HAVE_SSL
268 if (config.use_starttls) {
269 /* send the STARTTLS command */
270 send(socket_descriptor, SMTP_STARTTLS, strlen(SMTP_STARTTLS), 0);
271
272 mp_subcheck sc_starttls_init = mp_subcheck_init();
273 recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor,
274 ssl_established); /* wait for it */
275 if (!strstr(buffer, SMTP_EXPECT)) {
217 smtp_quit(config, buffer, socket_descriptor, ssl_established); 276 smtp_quit(config, buffer, socket_descriptor, ssl_established);
218 exit(STATE_WARNING); 277
278 xasprintf(&sc_starttls_init.output, "StartTLS not supported by server");
279 sc_starttls_init = mp_set_subcheck_state(sc_starttls_init, STATE_UNKNOWN);
280 mp_add_subcheck_to_check(&overall, sc_starttls_init);
281 mp_exit(overall);
219 } 282 }
220 283
221#ifdef HAVE_SSL 284 mp_state_enum starttls_result = np_net_ssl_init_with_hostname(
222 if (config.use_starttls) { 285 socket_descriptor, (config.use_sni ? config.server_address : NULL));
223 /* send the STARTTLS command */ 286 if (starttls_result != STATE_OK) {
224 send(socket_descriptor, SMTP_STARTTLS, strlen(SMTP_STARTTLS), 0); 287 close(socket_descriptor);
225 288 np_net_ssl_cleanup();
226 recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established); /* wait for it */
227 if (!strstr(buffer, SMTP_EXPECT)) {
228 printf(_("Server does not support STARTTLS\n"));
229 smtp_quit(config, buffer, socket_descriptor, ssl_established);
230 exit(STATE_UNKNOWN);
231 }
232 289
233 result = np_net_ssl_init_with_hostname(socket_descriptor, (config.use_sni ? config.server_address : NULL)); 290 sc_starttls_init = mp_set_subcheck_state(sc_starttls_init, STATE_CRITICAL);
234 if (result != STATE_OK) { 291 xasprintf(&sc_starttls_init.output, "failed to create StartTLS context");
235 printf(_("CRITICAL - Cannot create SSL context.\n")); 292 mp_add_subcheck_to_check(&overall, sc_starttls_init);
236 close(socket_descriptor); 293 mp_exit(overall);
237 np_net_ssl_cleanup(); 294 }
238 exit(STATE_CRITICAL); 295 sc_starttls_init = mp_set_subcheck_state(sc_starttls_init, STATE_OK);
239 } 296 xasprintf(&sc_starttls_init.output, "created StartTLS context");
297 mp_add_subcheck_to_check(&overall, sc_starttls_init);
298
299 ssl_established = true;
300
301 /*
302 * Resend the EHLO command.
303 *
304 * RFC 3207 (4.2) says: ``The client MUST discard any knowledge
305 * obtained from the server, such as the list of SMTP service
306 * extensions, which was not obtained from the TLS negotiation
307 * itself. The client SHOULD send an EHLO command as the first
308 * command after a successful TLS negotiation.'' For this
309 * reason, some MTAs will not allow an AUTH LOGIN command before
310 * we resent EHLO via TLS.
311 */
312 if (my_send(config, helocmd, (int)strlen(helocmd), socket_descriptor, ssl_established) <=
313 0) {
314 my_close(socket_descriptor);
315
316 mp_subcheck sc_ehlo = mp_subcheck_init();
317 sc_ehlo = mp_set_subcheck_state(sc_ehlo, STATE_UNKNOWN);
318 xasprintf(&sc_ehlo.output, "cannot send EHLO command via StartTLS");
319 mp_add_subcheck_to_check(&overall, sc_ehlo);
320 mp_exit(overall);
321 }
240 322
241 ssl_established = true; 323 if (verbose) {
242 324 printf(_("sent %s"), helocmd);
243 /* 325 }
244 * Resend the EHLO command.
245 *
246 * RFC 3207 (4.2) says: ``The client MUST discard any knowledge
247 * obtained from the server, such as the list of SMTP service
248 * extensions, which was not obtained from the TLS negotiation
249 * itself. The client SHOULD send an EHLO command as the first
250 * command after a successful TLS negotiation.'' For this
251 * reason, some MTAs will not allow an AUTH LOGIN command before
252 * we resent EHLO via TLS.
253 */
254 if (my_send(config, helocmd, strlen(helocmd), socket_descriptor, ssl_established) <= 0) {
255 printf("%s\n", _("SMTP UNKNOWN - Cannot send EHLO command via TLS."));
256 my_close(socket_descriptor);
257 exit(STATE_UNKNOWN);
258 }
259 326
260 if (verbose) { 327 if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) <= 0) {
261 printf(_("sent %s"), helocmd); 328 my_close(socket_descriptor);
262 }
263 329
264 if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) <= 0) { 330 mp_subcheck sc_ehlo = mp_subcheck_init();
265 printf("%s\n", _("SMTP UNKNOWN - Cannot read EHLO response via TLS.")); 331 sc_ehlo = mp_set_subcheck_state(sc_ehlo, STATE_UNKNOWN);
266 my_close(socket_descriptor); 332 xasprintf(&sc_ehlo.output, "cannot read EHLO response via StartTLS");
267 exit(STATE_UNKNOWN); 333 mp_add_subcheck_to_check(&overall, sc_ehlo);
268 } 334 mp_exit(overall);
335 }
269 336
270 if (verbose) { 337 if (verbose) {
271 printf("%s", buffer); 338 printf("%s", buffer);
272 } 339 }
340 }
273 341
274# ifdef USE_OPENSSL 342# ifdef USE_OPENSSL
275 if (config.check_cert) { 343 if (ssl_established) {
276 result = np_net_ssl_check_cert(config.days_till_exp_warn, config.days_till_exp_crit); 344 net_ssl_check_cert_result cert_check_result =
277 smtp_quit(config, buffer, socket_descriptor, ssl_established); 345 np_net_ssl_check_cert2(config.days_till_exp_warn, config.days_till_exp_crit);
278 my_close(socket_descriptor); 346
279 exit(result); 347 mp_subcheck sc_cert_check = mp_subcheck_init();
348
349 switch (cert_check_result.errors) {
350 case ALL_OK: {
351
352 if (cert_check_result.result_state != STATE_OK &&
353 config.ignore_certificate_expiration) {
354 xasprintf(&sc_cert_check.output,
355 "Remaining certificate lifetime: %d days. Expiration will be ignored",
356 (int)(cert_check_result.remaining_seconds / 86400));
357 sc_cert_check = mp_set_subcheck_state(sc_cert_check, STATE_OK);
358 } else {
359 xasprintf(&sc_cert_check.output, "Remaining certificate lifetime: %d days",
360 (int)(cert_check_result.remaining_seconds / 86400));
361 sc_cert_check =
362 mp_set_subcheck_state(sc_cert_check, cert_check_result.result_state);
280 } 363 }
364 } break;
365 case NO_SERVER_CERTIFICATE_PRESENT: {
366 xasprintf(&sc_cert_check.output, "no server certificate present");
367 sc_cert_check = mp_set_subcheck_state(sc_cert_check, cert_check_result.result_state);
368 } break;
369 case UNABLE_TO_RETRIEVE_CERTIFICATE_SUBJECT: {
370 xasprintf(&sc_cert_check.output, "can not retrieve certificate subject");
371 sc_cert_check = mp_set_subcheck_state(sc_cert_check, cert_check_result.result_state);
372 } break;
373 case WRONG_TIME_FORMAT_IN_CERTIFICATE: {
374 xasprintf(&sc_cert_check.output, "wrong time format in certificate");
375 sc_cert_check = mp_set_subcheck_state(sc_cert_check, cert_check_result.result_state);
376 } break;
377 };
378
379 mp_add_subcheck_to_check(&overall, sc_cert_check);
380 }
281# endif /* USE_OPENSSL */ 381# endif /* USE_OPENSSL */
282 } 382
283#endif 383#endif
284 384
285 if (verbose) { 385 if (verbose) {
386 printf("%s", buffer);
387 }
388
389 /* save buffer for later use */
390 xasprintf(&server_response, "%s%s", server_response, buffer);
391 /* strip the buffer of carriage returns */
392 strip(server_response);
393
394 /* make sure we find the droids we are looking for */
395 mp_subcheck sc_expect_response = mp_subcheck_init();
396
397 if (!strstr(server_response, config.server_expect)) {
398 sc_expect_response = mp_set_subcheck_state(sc_expect_response, STATE_WARNING);
399 if (config.server_port == SMTP_PORT) {
400 xasprintf(&sc_expect_response.output, _("invalid SMTP response received from host: %s"),
401 server_response);
402 } else {
403 xasprintf(&sc_expect_response.output,
404 _("invalid SMTP response received from host on port %d: %s"),
405 config.server_port, server_response);
406 }
407 exit(STATE_WARNING);
408 } else {
409 xasprintf(&sc_expect_response.output, "received valid SMTP response '%s' from host: '%s'",
410 config.server_expect, server_response);
411 sc_expect_response = mp_set_subcheck_state(sc_expect_response, STATE_OK);
412 }
413
414 mp_add_subcheck_to_check(&overall, sc_expect_response);
415
416 if (config.send_mail_from) {
417 my_send(config, cmd_str, (int)strlen(cmd_str), socket_descriptor, ssl_established);
418 if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) >= 1 &&
419 verbose) {
286 printf("%s", buffer); 420 printf("%s", buffer);
287 } 421 }
422 }
288 423
289 /* save buffer for later use */ 424 size_t counter = 0;
290 xasprintf(&server_response, "%s%s", server_response, buffer); 425 while (counter < config.ncommands) {
291 /* strip the buffer of carriage returns */ 426 xasprintf(&cmd_str, "%s%s", config.commands[counter], "\r\n");
292 strip(server_response); 427 my_send(config, cmd_str, (int)strlen(cmd_str), socket_descriptor, ssl_established);
428 if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) >= 1 &&
429 verbose) {
430 printf("%s", buffer);
431 }
293 432
294 /* make sure we find the droids we are looking for */ 433 strip(buffer);
295 if (!strstr(server_response, config.server_expect)) { 434
296 if (config.server_port == SMTP_PORT) { 435 if (counter < config.nresponses) {
297 printf(_("Invalid SMTP response received from host: %s\n"), server_response); 436 int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE;
298 } else { 437 regex_t preg;
299 printf(_("Invalid SMTP response received from host on port %d: %s\n"), config.server_port, server_response); 438 int errcode = regcomp(&preg, config.responses[counter], cflags);
439 char errbuf[MAX_INPUT_BUFFER];
440 if (errcode != 0) {
441 regerror(errcode, &preg, errbuf, MAX_INPUT_BUFFER);
442 printf(_("Could Not Compile Regular Expression"));
443 exit(STATE_UNKNOWN);
300 } 444 }
301 exit(STATE_WARNING);
302 }
303 445
304 if (config.send_mail_from) { 446 regmatch_t pmatch[10];
305 my_send(config, cmd_str, (int)strlen(cmd_str), socket_descriptor, ssl_established); 447 int eflags = 0;
306 if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) >= 1 && verbose) { 448 int excode = regexec(&preg, buffer, 10, pmatch, eflags);
307 printf("%s", buffer); 449 mp_subcheck sc_expected_responses = mp_subcheck_init();
450 if (excode == 0) {
451 xasprintf(&sc_expected_responses.output, "valid response '%s' to command '%s'",
452 buffer, config.commands[counter]);
453 sc_expected_responses = mp_set_subcheck_state(sc_expected_responses, STATE_OK);
454 } else if (excode == REG_NOMATCH) {
455 sc_expected_responses = mp_set_subcheck_state(sc_expected_responses, STATE_WARNING);
456 xasprintf(&sc_expected_responses.output, "invalid response '%s' to command '%s'",
457 buffer, config.commands[counter]);
458 } else {
459 regerror(excode, &preg, errbuf, MAX_INPUT_BUFFER);
460 xasprintf(&sc_expected_responses.output, "regexec execute error: %s", errbuf);
461 sc_expected_responses = mp_set_subcheck_state(sc_expected_responses, STATE_UNKNOWN);
308 } 462 }
309 } 463 }
464 counter++;
465 }
310 466
311 int counter = 0; 467 if (config.authtype != NULL) {
312 while (counter < config.ncommands) { 468 mp_subcheck sc_auth = mp_subcheck_init();
313 xasprintf(&cmd_str, "%s%s", config.commands[counter], "\r\n"); 469
314 my_send(config, cmd_str, (int)strlen(cmd_str), socket_descriptor, ssl_established); 470 if (strcmp(config.authtype, "LOGIN") == 0) {
315 if (recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established) >= 1 && verbose) { 471 char *abuf;
316 printf("%s", buffer); 472 int ret;
317 } 473 do {
318 strip(buffer); 474 /* send AUTH LOGIN */
319 if (counter < config.nresponses) { 475 my_send(config, SMTP_AUTH_LOGIN, strlen(SMTP_AUTH_LOGIN), socket_descriptor,
320 int cflags = REG_EXTENDED | REG_NOSUB | REG_NEWLINE; 476 ssl_established);
321 regex_t preg; 477
322 int errcode = regcomp(&preg, config.responses[counter], cflags); 478 if (verbose) {
323 char errbuf[MAX_INPUT_BUFFER]; 479 printf(_("sent %s\n"), "AUTH LOGIN");
324 if (errcode != 0) {
325 regerror(errcode, &preg, errbuf, MAX_INPUT_BUFFER);
326 printf(_("Could Not Compile Regular Expression"));
327 exit(STATE_UNKNOWN);
328 } 480 }
329 481
330 regmatch_t pmatch[10]; 482 if ((ret = recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor,
331 int eflags = 0; 483 ssl_established)) <= 0) {
332 int excode = regexec(&preg, buffer, 10, pmatch, eflags); 484 xasprintf(&sc_auth.output, _("recv() failed after AUTH LOGIN"));
333 if (excode == 0) { 485 sc_auth = mp_set_subcheck_state(sc_auth, STATE_WARNING);
334 result = STATE_OK; 486 break;
335 } else if (excode == REG_NOMATCH) {
336 result = STATE_WARNING;
337 printf(_("SMTP %s - Invalid response '%s' to command '%s'\n"), state_text(result), buffer, config.commands[counter]);
338 } else {
339 regerror(excode, &preg, errbuf, MAX_INPUT_BUFFER);
340 printf(_("Execute Error: %s\n"), errbuf);
341 result = STATE_UNKNOWN;
342 } 487 }
343 }
344 counter++;
345 }
346 488
347 if (config.authtype != NULL) { 489 if (verbose) {
348 if (strcmp(config.authtype, "LOGIN") == 0) { 490 printf(_("received %s\n"), buffer);
349 char *abuf; 491 }
350 int ret; 492
351 do { 493 if (strncmp(buffer, "334", 3) != 0) {
352 if (config.authuser == NULL) { 494 xasprintf(&sc_auth.output, "invalid response received after AUTH LOGIN");
353 result = STATE_CRITICAL; 495 sc_auth = mp_set_subcheck_state(sc_auth, STATE_CRITICAL);
354 xasprintf(&error_msg, _("no authuser specified, "));
355 break;
356 }
357 if (config.authpass == NULL) {
358 result = STATE_CRITICAL;
359 xasprintf(&error_msg, _("no authpass specified, "));
360 break;
361 }
362
363 /* send AUTH LOGIN */
364 my_send(config, SMTP_AUTH_LOGIN, strlen(SMTP_AUTH_LOGIN), socket_descriptor, ssl_established);
365 if (verbose) {
366 printf(_("sent %s\n"), "AUTH LOGIN");
367 }
368
369 if ((ret = recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established)) <= 0) {
370 xasprintf(&error_msg, _("recv() failed after AUTH LOGIN, "));
371 result = STATE_WARNING;
372 break;
373 }
374 if (verbose) {
375 printf(_("received %s\n"), buffer);
376 }
377
378 if (strncmp(buffer, "334", 3) != 0) {
379 result = STATE_CRITICAL;
380 xasprintf(&error_msg, _("invalid response received after AUTH LOGIN, "));
381 break;
382 }
383
384 /* encode authuser with base64 */
385 base64_encode_alloc(config.authuser, strlen(config.authuser), &abuf);
386 xasprintf(&abuf, "%s\r\n", abuf);
387 my_send(config, abuf, (int)strlen(abuf), socket_descriptor, ssl_established);
388 if (verbose) {
389 printf(_("sent %s\n"), abuf);
390 }
391
392 if ((ret = recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established)) <= 0) {
393 result = STATE_CRITICAL;
394 xasprintf(&error_msg, _("recv() failed after sending authuser, "));
395 break;
396 }
397 if (verbose) {
398 printf(_("received %s\n"), buffer);
399 }
400 if (strncmp(buffer, "334", 3) != 0) {
401 result = STATE_CRITICAL;
402 xasprintf(&error_msg, _("invalid response received after authuser, "));
403 break;
404 }
405 /* encode authpass with base64 */
406 base64_encode_alloc(config.authpass, strlen(config.authpass), &abuf);
407 xasprintf(&abuf, "%s\r\n", abuf);
408 my_send(config, abuf, (int)strlen(abuf), socket_descriptor, ssl_established);
409 if (verbose) {
410 printf(_("sent %s\n"), abuf);
411 }
412 if ((ret = recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor, ssl_established)) <= 0) {
413 result = STATE_CRITICAL;
414 xasprintf(&error_msg, _("recv() failed after sending authpass, "));
415 break;
416 }
417 if (verbose) {
418 printf(_("received %s\n"), buffer);
419 }
420 if (strncmp(buffer, "235", 3) != 0) {
421 result = STATE_CRITICAL;
422 xasprintf(&error_msg, _("invalid response received after authpass, "));
423 break;
424 }
425 break; 496 break;
426 } while (false); 497 }
427 } else {
428 result = STATE_CRITICAL;
429 xasprintf(&error_msg, _("only authtype LOGIN is supported, "));
430 }
431 }
432 498
433 /* tell the server we're done */ 499 /* encode authuser with base64 */
434 smtp_quit(config, buffer, socket_descriptor, ssl_established); 500 base64_encode_alloc(config.authuser, strlen(config.authuser), &abuf);
501 xasprintf(&abuf, "%s\r\n", abuf);
502 my_send(config, abuf, (int)strlen(abuf), socket_descriptor, ssl_established);
503 if (verbose) {
504 printf(_("sent %s\n"), abuf);
505 }
506
507 if ((ret = recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor,
508 ssl_established)) <= 0) {
509 xasprintf(&sc_auth.output, "recv() failed after sending authuser");
510 sc_auth = mp_set_subcheck_state(sc_auth, STATE_CRITICAL);
511 break;
512 }
513
514 if (verbose) {
515 printf(_("received %s\n"), buffer);
516 }
517
518 if (strncmp(buffer, "334", 3) != 0) {
519 xasprintf(&sc_auth.output, "invalid response received after authuser");
520 sc_auth = mp_set_subcheck_state(sc_auth, STATE_CRITICAL);
521 break;
522 }
435 523
436 /* finally close the connection */ 524 /* encode authpass with base64 */
437 close(socket_descriptor); 525 base64_encode_alloc(config.authpass, strlen(config.authpass), &abuf);
526 xasprintf(&abuf, "%s\r\n", abuf);
527 my_send(config, abuf, (int)strlen(abuf), socket_descriptor, ssl_established);
528
529 if (verbose) {
530 printf(_("sent %s\n"), abuf);
531 }
532
533 if ((ret = recvlines(config, buffer, MAX_INPUT_BUFFER, socket_descriptor,
534 ssl_established)) <= 0) {
535 xasprintf(&sc_auth.output, "recv() failed after sending authpass");
536 sc_auth = mp_set_subcheck_state(sc_auth, STATE_CRITICAL);
537 break;
538 }
539
540 if (verbose) {
541 printf(_("received %s\n"), buffer);
542 }
543
544 if (strncmp(buffer, "235", 3) != 0) {
545 xasprintf(&sc_auth.output, "invalid response received after authpass");
546 sc_auth = mp_set_subcheck_state(sc_auth, STATE_CRITICAL);
547 break;
548 }
549 break;
550 } while (false);
551 } else {
552 sc_auth = mp_set_subcheck_state(sc_auth, STATE_CRITICAL);
553 xasprintf(&sc_auth.output, "only authtype LOGIN is supported");
554 }
555
556 mp_add_subcheck_to_check(&overall, sc_auth);
438 } 557 }
439 558
559 /* tell the server we're done */
560 smtp_quit(config, buffer, socket_descriptor, ssl_established);
561
562 /* finally close the connection */
563 close(socket_descriptor);
564
440 /* reset the alarm */ 565 /* reset the alarm */
441 alarm(0); 566 alarm(0);
442 567
443 long microsec = deltime(start_time); 568 long microsec = deltime(start_time);
444 double elapsed_time = (double)microsec / 1.0e6; 569 double elapsed_time = (double)microsec / 1.0e6;
445 570
446 if (result == STATE_OK) { 571 mp_perfdata pd_elapsed_time = perfdata_init();
447 if (config.check_critical_time && elapsed_time > config.critical_time) { 572 pd_elapsed_time = mp_set_pd_value(pd_elapsed_time, elapsed_time);
448 result = STATE_CRITICAL; 573 pd_elapsed_time.label = "time";
449 } else if (config.check_warning_time && elapsed_time > config.warning_time) { 574 pd_elapsed_time.uom = "s";
450 result = STATE_WARNING; 575
451 } 576 pd_elapsed_time = mp_pd_set_thresholds(pd_elapsed_time, config.connection_time);
452 }
453 577
454 printf(_("SMTP %s - %s%.3f sec. response time%s%s|%s\n"), state_text(result), error_msg, elapsed_time, verbose ? ", " : "", 578 mp_subcheck sc_connection_time = mp_subcheck_init();
455 verbose ? buffer : "", 579 xasprintf(&sc_connection_time.output, "connection time: %.3gs", elapsed_time);
456 fperfdata("time", elapsed_time, "s", config.check_warning_time, config.warning_time, config.check_critical_time, 580 sc_connection_time =
457 config.critical_time, true, 0, false, 0)); 581 mp_set_subcheck_state(sc_connection_time, mp_get_pd_status(pd_elapsed_time));
582 mp_add_subcheck_to_check(&overall, sc_connection_time);
458 583
459 exit(result); 584 mp_exit(overall);
460} 585}
461 586
462/* process command-line arguments */ 587/* process command-line arguments */
463check_smtp_config_wrapper process_arguments(int argc, char **argv) { 588check_smtp_config_wrapper process_arguments(int argc, char **argv) {
464 enum { 589 enum {
465 SNI_OPTION = CHAR_MAX + 1 590 SNI_OPTION = CHAR_MAX + 1,
591 output_format_index,
592 ignore_certificate_expiration_index,
466 }; 593 };
467 594
468 int option = 0; 595 int option = 0;
469 static struct option longopts[] = {{"hostname", required_argument, 0, 'H'}, 596 static struct option longopts[] = {
470 {"expect", required_argument, 0, 'e'}, 597 {"hostname", required_argument, 0, 'H'},
471 {"critical", required_argument, 0, 'c'}, 598 {"expect", required_argument, 0, 'e'},
472 {"warning", required_argument, 0, 'w'}, 599 {"critical", required_argument, 0, 'c'},
473 {"timeout", required_argument, 0, 't'}, 600 {"warning", required_argument, 0, 'w'},
474 {"port", required_argument, 0, 'p'}, 601 {"timeout", required_argument, 0, 't'},
475 {"from", required_argument, 0, 'f'}, 602 {"port", required_argument, 0, 'p'},
476 {"fqdn", required_argument, 0, 'F'}, 603 {"from", required_argument, 0, 'f'},
477 {"authtype", required_argument, 0, 'A'}, 604 {"fqdn", required_argument, 0, 'F'},
478 {"authuser", required_argument, 0, 'U'}, 605 {"authtype", required_argument, 0, 'A'},
479 {"authpass", required_argument, 0, 'P'}, 606 {"authuser", required_argument, 0, 'U'},
480 {"command", required_argument, 0, 'C'}, 607 {"authpass", required_argument, 0, 'P'},
481 {"response", required_argument, 0, 'R'}, 608 {"command", required_argument, 0, 'C'},
482 {"verbose", no_argument, 0, 'v'}, 609 {"response", required_argument, 0, 'R'},
483 {"version", no_argument, 0, 'V'}, 610 {"verbose", no_argument, 0, 'v'},
484 {"use-ipv4", no_argument, 0, '4'}, 611 {"version", no_argument, 0, 'V'},
485 {"use-ipv6", no_argument, 0, '6'}, 612 {"use-ipv4", no_argument, 0, '4'},
486 {"help", no_argument, 0, 'h'}, 613 {"use-ipv6", no_argument, 0, '6'},
487 {"lmtp", no_argument, 0, 'L'}, 614 {"help", no_argument, 0, 'h'},
488 {"ssl", no_argument, 0, 's'}, 615 {"lmtp", no_argument, 0, 'L'},
489 {"tls", no_argument, 0, 's'}, 616 {"ssl", no_argument, 0, 's'},
490 {"starttls", no_argument, 0, 'S'}, 617 {"tls", no_argument, 0, 's'},
491 {"sni", no_argument, 0, SNI_OPTION}, 618 {"starttls", no_argument, 0, 'S'},
492 {"certificate", required_argument, 0, 'D'}, 619 {"sni", no_argument, 0, SNI_OPTION},
493 {"ignore-quit-failure", no_argument, 0, 'q'}, 620 {"certificate", required_argument, 0, 'D'},
494 {"proxy", no_argument, 0, 'r'}, 621 {"ignore-quit-failure", no_argument, 0, 'q'},
495 {0, 0, 0, 0}}; 622 {"proxy", no_argument, 0, 'r'},
623 {"ignore-certificate-expiration", no_argument, 0, ignore_certificate_expiration_index},
624 {"output-format", required_argument, 0, output_format_index},
625 {0, 0, 0, 0}};
496 626
497 check_smtp_config_wrapper result = { 627 check_smtp_config_wrapper result = {
498 .config = check_smtp_config_init(), 628 .config = check_smtp_config_init(),
@@ -514,12 +644,13 @@ check_smtp_config_wrapper process_arguments(int argc, char **argv) {
514 } 644 }
515 } 645 }
516 646
517 int command_size = 0; 647 unsigned long command_size = 0;
518 int response_size = 0; 648 unsigned long response_size = 0;
519 bool implicit_tls = false; 649 bool implicit_tls = false;
520 int server_port_option = 0; 650 int server_port_option = 0;
521 while (true) { 651 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); 652 int opt_index =
653 getopt_long(argc, argv, "+hVv46Lrt:p:f:e:c:w:H:C:R:sSD:F:A:U:P:q", longopts, &option);
523 654
524 if (opt_index == -1 || opt_index == EOF) { 655 if (opt_index == -1 || opt_index == EOF) {
525 break; 656 break;
@@ -546,7 +677,8 @@ check_smtp_config_wrapper process_arguments(int argc, char **argv) {
546 break; 677 break;
547 case 'f': /* from argument */ 678 case 'f': /* from argument */
548 result.config.from_arg = optarg + strspn(optarg, "<"); 679 result.config.from_arg = optarg + strspn(optarg, "<");
549 result.config.from_arg = strndup(result.config.from_arg, strcspn(result.config.from_arg, ">")); 680 result.config.from_arg =
681 strndup(result.config.from_arg, strcspn(result.config.from_arg, ">"));
550 result.config.send_mail_from = true; 682 result.config.send_mail_from = true;
551 break; 683 break;
552 case 'A': 684 case 'A':
@@ -565,9 +697,11 @@ check_smtp_config_wrapper process_arguments(int argc, char **argv) {
565 case 'C': /* commands */ 697 case 'C': /* commands */
566 if (result.config.ncommands >= command_size) { 698 if (result.config.ncommands >= command_size) {
567 command_size += 8; 699 command_size += 8;
568 result.config.commands = realloc(result.config.commands, sizeof(char *) * command_size); 700 result.config.commands =
701 realloc(result.config.commands, sizeof(char *) * command_size);
569 if (result.config.commands == NULL) { 702 if (result.config.commands == NULL) {
570 die(STATE_UNKNOWN, _("Could not realloc() units [%d]\n"), result.config.ncommands); 703 die(STATE_UNKNOWN, _("Could not realloc() units [%lu]\n"),
704 result.config.ncommands);
571 } 705 }
572 } 706 }
573 result.config.commands[result.config.ncommands] = (char *)malloc(sizeof(char) * 255); 707 result.config.commands[result.config.ncommands] = (char *)malloc(sizeof(char) * 255);
@@ -577,31 +711,33 @@ check_smtp_config_wrapper process_arguments(int argc, char **argv) {
577 case 'R': /* server responses */ 711 case 'R': /* server responses */
578 if (result.config.nresponses >= response_size) { 712 if (result.config.nresponses >= response_size) {
579 response_size += 8; 713 response_size += 8;
580 result.config.responses = realloc(result.config.responses, sizeof(char *) * response_size); 714 result.config.responses =
715 realloc(result.config.responses, sizeof(char *) * response_size);
581 if (result.config.responses == NULL) { 716 if (result.config.responses == NULL) {
582 die(STATE_UNKNOWN, _("Could not realloc() units [%d]\n"), result.config.nresponses); 717 die(STATE_UNKNOWN, _("Could not realloc() units [%lu]\n"),
718 result.config.nresponses);
583 } 719 }
584 } 720 }
585 result.config.responses[result.config.nresponses] = (char *)malloc(sizeof(char) * 255); 721 result.config.responses[result.config.nresponses] = (char *)malloc(sizeof(char) * 255);
586 strncpy(result.config.responses[result.config.nresponses], optarg, 255); 722 strncpy(result.config.responses[result.config.nresponses], optarg, 255);
587 result.config.nresponses++; 723 result.config.nresponses++;
588 break; 724 break;
589 case 'c': /* critical time threshold */ 725 case 'c': /* critical time threshold */ {
590 if (!is_nonnegative(optarg)) { 726 mp_range_parsed tmp = mp_parse_range_string(optarg);
591 usage4(_("Critical time must be a positive")); 727 if (tmp.error != MP_PARSING_SUCCES) {
592 } else { 728 die(STATE_UNKNOWN, "failed to parse critical time threshold");
593 result.config.critical_time = strtod(optarg, NULL);
594 result.config.check_critical_time = true;
595 } 729 }
596 break; 730 result.config.connection_time =
597 case 'w': /* warning time threshold */ 731 mp_thresholds_set_warn(result.config.connection_time, tmp.range);
598 if (!is_nonnegative(optarg)) { 732 } break;
599 usage4(_("Warning time must be a positive")); 733 case 'w': /* warning time threshold */ {
600 } else { 734 mp_range_parsed tmp = mp_parse_range_string(optarg);
601 result.config.warning_time = strtod(optarg, NULL); 735 if (tmp.error != MP_PARSING_SUCCES) {
602 result.config.check_warning_time = true; 736 die(STATE_UNKNOWN, "failed to parse warning time threshold");
603 } 737 }
604 break; 738 result.config.connection_time =
739 mp_thresholds_set_crit(result.config.connection_time, tmp.range);
740 } break;
605 case 'v': /* verbose */ 741 case 'v': /* verbose */
606 verbose++; 742 verbose++;
607 break; 743 break;
@@ -638,7 +774,6 @@ check_smtp_config_wrapper process_arguments(int argc, char **argv) {
638 } 774 }
639 result.config.days_till_exp_warn = atoi(optarg); 775 result.config.days_till_exp_warn = atoi(optarg);
640 } 776 }
641 result.config.check_cert = true;
642 result.config.ignore_send_quit_failure = true; 777 result.config.ignore_send_quit_failure = true;
643#else 778#else
644 usage(_("SSL support not available - install OpenSSL and recompile")); 779 usage(_("SSL support not available - install OpenSSL and recompile"));
@@ -687,6 +822,21 @@ check_smtp_config_wrapper process_arguments(int argc, char **argv) {
687 exit(STATE_UNKNOWN); 822 exit(STATE_UNKNOWN);
688 case '?': /* help */ 823 case '?': /* help */
689 usage5(); 824 usage5();
825 case output_format_index: {
826 parsed_output_format parser = mp_parse_output_format(optarg);
827 if (!parser.parsing_success) {
828 // TODO List all available formats here, maybe add anothoer usage function
829 printf("Invalid output format: %s\n", optarg);
830 exit(STATE_UNKNOWN);
831 }
832
833 result.config.output_format_is_set = true;
834 result.config.output_format = parser.output_format;
835 break;
836 }
837 case ignore_certificate_expiration_index: {
838 result.config.ignore_certificate_expiration = true;
839 }
690 } 840 }
691 } 841 }
692 842
@@ -715,11 +865,26 @@ check_smtp_config_wrapper process_arguments(int argc, char **argv) {
715 result.config.server_port = server_port_option; 865 result.config.server_port = server_port_option;
716 } 866 }
717 867
868 if (result.config.authtype) {
869 if (strcmp(result.config.authtype, "LOGIN") == 0) {
870 if (result.config.authuser == NULL) {
871 usage4("no authuser specified");
872 }
873 if (result.config.authpass == NULL) {
874 usage4("no authpass specified");
875 }
876 } else {
877 usage4("only authtype LOGIN is supported");
878 }
879 }
880
718 return result; 881 return result;
719} 882}
720 883
721char *smtp_quit(check_smtp_config config, char buffer[MAX_INPUT_BUFFER], int socket_descriptor, bool ssl_established) { 884char *smtp_quit(check_smtp_config config, char buffer[MAX_INPUT_BUFFER], int socket_descriptor,
722 int sent_bytes = my_send(config, SMTP_QUIT, strlen(SMTP_QUIT), socket_descriptor, ssl_established); 885 bool ssl_established) {
886 int sent_bytes =
887 my_send(config, SMTP_QUIT, strlen(SMTP_QUIT), socket_descriptor, ssl_established);
723 if (sent_bytes < 0) { 888 if (sent_bytes < 0) {
724 if (config.ignore_send_quit_failure) { 889 if (config.ignore_send_quit_failure) {
725 if (verbose) { 890 if (verbose) {
@@ -759,9 +924,10 @@ char *smtp_quit(check_smtp_config config, char buffer[MAX_INPUT_BUFFER], int soc
759 * function which buffers the data, move that to netutils.c and change 924 * function which buffers the data, move that to netutils.c and change
760 * check_smtp and other plugins to use that. Also, remove (\r)\n. 925 * check_smtp and other plugins to use that. Also, remove (\r)\n.
761 */ 926 */
762int recvline(char *buf, size_t bufsize, check_smtp_config config, int socket_descriptor, bool ssl_established) { 927int recvline(char *buf, size_t bufsize, check_smtp_config config, int socket_descriptor,
928 bool ssl_established) {
763 int result; 929 int result;
764 int counter; 930 size_t counter;
765 931
766 for (counter = result = 0; counter < bufsize - 1; counter++) { 932 for (counter = result = 0; counter < bufsize - 1; counter++) {
767 if ((result = my_recv(config, &buf[counter], 1, socket_descriptor, ssl_established)) != 1) { 933 if ((result = my_recv(config, &buf[counter], 1, socket_descriptor, ssl_established)) != 1) {
@@ -769,7 +935,7 @@ int recvline(char *buf, size_t bufsize, check_smtp_config config, int socket_des
769 } 935 }
770 if (buf[counter] == '\n') { 936 if (buf[counter] == '\n') {
771 buf[++counter] = '\0'; 937 buf[++counter] = '\0';
772 return counter; 938 return (int)counter;
773 } 939 }
774 } 940 }
775 return (result == 1 || counter == 0) ? -2 : result; /* -2 if out of space */ 941 return (result == 1 || counter == 0) ? -2 : result; /* -2 if out of space */
@@ -789,13 +955,16 @@ int recvline(char *buf, size_t bufsize, check_smtp_config config, int socket_des
789 * 955 *
790 * TODO: Move this to netutils.c. Also, remove \r and possibly the final \n. 956 * TODO: Move this to netutils.c. Also, remove \r and possibly the final \n.
791 */ 957 */
792int recvlines(check_smtp_config config, char *buf, size_t bufsize, int socket_descriptor, bool ssl_established) { 958int recvlines(check_smtp_config config, char *buf, size_t bufsize, int socket_descriptor,
959 bool ssl_established) {
793 int result; 960 int result;
794 int counter; 961 int counter;
795 962
796 for (counter = 0; /* forever */; counter += result) { 963 for (counter = 0; /* forever */; counter += result) {
797 if (!((result = recvline(buf + counter, bufsize - counter, config, socket_descriptor, ssl_established)) > 3 && 964 if (!((result = recvline(buf + counter, bufsize - counter, config, socket_descriptor,
798 isdigit((int)buf[counter]) && isdigit((int)buf[counter + 1]) && isdigit((int)buf[counter + 2]) && buf[counter + 3] == '-')) { 965 ssl_established)) > 3 &&
966 isdigit((int)buf[counter]) && isdigit((int)buf[counter + 1]) &&
967 isdigit((int)buf[counter + 2]) && buf[counter + 3] == '-')) {
799 break; 968 break;
800 } 969 }
801 } 970 }
@@ -835,13 +1004,15 @@ void print_help(void) {
835 printf(UT_IPv46); 1004 printf(UT_IPv46);
836 1005
837 printf(" %s\n", "-e, --expect=STRING"); 1006 printf(" %s\n", "-e, --expect=STRING");
838 printf(_(" String to expect in first line of server response (default: '%s')\n"), SMTP_EXPECT); 1007 printf(_(" String to expect in first line of server response (default: '%s')\n"),
1008 SMTP_EXPECT);
839 printf(" %s\n", "-C, --command=STRING"); 1009 printf(" %s\n", "-C, --command=STRING");
840 printf(" %s\n", _("SMTP command (may be used repeatedly)")); 1010 printf(" %s\n", _("SMTP command (may be used repeatedly)"));
841 printf(" %s\n", "-R, --response=STRING"); 1011 printf(" %s\n", "-R, --response=STRING");
842 printf(" %s\n", _("Expected response to command (may be used repeatedly)")); 1012 printf(" %s\n", _("Expected response to command (may be used repeatedly)"));
843 printf(" %s\n", "-f, --from=STRING"); 1013 printf(" %s\n", "-f, --from=STRING");
844 printf(" %s\n", _("FROM-address to include in MAIL command, required by Exchange 2000")), printf(" %s\n", "-F, --fqdn=STRING"); 1014 printf(" %s\n", _("FROM-address to include in MAIL command, required by Exchange 2000")),
1015 printf(" %s\n", "-F, --fqdn=STRING");
845 printf(" %s\n", _("FQDN used for HELO")); 1016 printf(" %s\n", _("FQDN used for HELO"));
846 printf(" %s\n", "-r, --proxy"); 1017 printf(" %s\n", "-r, --proxy");
847 printf(" %s\n", _("Use PROXY protocol prefix for the connection.")); 1018 printf(" %s\n", _("Use PROXY protocol prefix for the connection."));
@@ -867,11 +1038,15 @@ void print_help(void) {
867 printf(" %s\n", _("Send LHLO instead of HELO/EHLO")); 1038 printf(" %s\n", _("Send LHLO instead of HELO/EHLO"));
868 printf(" %s\n", "-q, --ignore-quit-failure"); 1039 printf(" %s\n", "-q, --ignore-quit-failure");
869 printf(" %s\n", _("Ignore failure when sending QUIT command to server")); 1040 printf(" %s\n", _("Ignore failure when sending QUIT command to server"));
1041 printf(" %s\n", "--ignore-certificate-expiration");
1042 printf(" %s\n", _("Ignore certificate expiration"));
870 1043
871 printf(UT_WARN_CRIT); 1044 printf(UT_WARN_CRIT);
872 1045
873 printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); 1046 printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
874 1047
1048 printf(UT_OUTPUT_FORMAT);
1049
875 printf(UT_VERBOSE); 1050 printf(UT_VERBOSE);
876 1051
877 printf("\n"); 1052 printf("\n");
@@ -885,7 +1060,9 @@ void print_help(void) {
885 1060
886void print_usage(void) { 1061void print_usage(void) {
887 printf("%s\n", _("Usage:")); 1062 printf("%s\n", _("Usage:"));
888 printf("%s -H host [-p port] [-4|-6] [-e expect] [-C command] [-R response] [-f from addr]\n", progname); 1063 printf("%s -H host [-p port] [-4|-6] [-e expect] [-C command] [-R response] [-f from addr]\n",
1064 progname);
889 printf("[-A authtype -U authuser -P authpass] [-w warn] [-c crit] [-t timeout] [-q]\n"); 1065 printf("[-A authtype -U authuser -P authpass] [-w warn] [-c crit] [-t timeout] [-q]\n");
890 printf("[-F fqdn] [-S] [-L] [-D warn days cert expire[,crit days cert expire]] [-r] [--sni] [-v] \n"); 1066 printf("[-F fqdn] [-S] [-L] [-D warn days cert expire[,crit days cert expire]] [-r] [--sni] "
1067 "[-v] \n");
891} 1068}