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