From e7f6da0211d1ee86f76ad5c3a36c6d58d53b1091 Mon Sep 17 00:00:00 2001
From: Ton Voon <ton.voon@opsera.com>
Date: Fri, 15 Jul 2011 16:48:45 +0100
Subject: New option to check_smtp to ignore failures when sending QUIT
 (#3358348 - Duncan Ferguson)


diff --git a/NEWS b/NEWS
index 8bb0fcb..9ff8755 100644
--- a/NEWS
+++ b/NEWS
@@ -7,6 +7,7 @@ This file documents the major additions and syntax changes between releases.
 	check_http now uses standard threshold functions (enables floating point and ranges)
 	check_http now checks for and prints the certificate cn (hostname) in SSL certificate checks (Stéphane Urbanovski)
 	Add perfdata to check_ssh (#3244097 - Marco Beck)
+	New option to check_smtp to ignore failures when sending QUIT (#3358348 - Duncan Ferguson)
 
 	FIXES
 	Fix check_disk free space calculation if blocksizes differ within a disk group (Bekar - #2973603)
diff --git a/plugins/check_smtp.c b/plugins/check_smtp.c
index ed49163..6c5994c 100644
--- a/plugins/check_smtp.c
+++ b/plugins/check_smtp.c
@@ -114,6 +114,7 @@ enum {
   TCP_PROTOCOL = 1,
   UDP_PROTOCOL = 2,
 };
+int ignore_send_quit_failure = FALSE;
 
 
 int
@@ -129,6 +130,9 @@ main (int argc, char **argv)
 	char *error_msg = "";
 	struct timeval tv;
 
+	/* Catch pipe errors in read/write - sometimes occurs when writing QUIT */
+	(void) signal (SIGPIPE, SIG_IGN);
+
 	setlocale (LC_ALL, "");
 	bindtextdomain (PACKAGE, LOCALEDIR);
 	textdomain (PACKAGE);
@@ -476,6 +480,7 @@ process_arguments (int argc, char **argv)
 		{"help", no_argument, 0, 'h'},
 		{"starttls",no_argument,0,'S'},
 		{"certificate",required_argument,0,'D'},
+		{"ignore-quit-failure",no_argument,0,'q'},
 		{0, 0, 0, 0}
 	};
 
@@ -492,7 +497,7 @@ process_arguments (int argc, char **argv)
 	}
 
 	while (1) {
-		c = getopt_long (argc, argv, "+hVv46t:p:f:e:c:w:H:C:R:SD:F:A:U:P:",
+		c = getopt_long (argc, argv, "+hVv46t:p:f:e:c:w:H:C:R:SD:F:A:U:P:q",
 		                 longopts, &option);
 
 		if (c == -1 || c == EOF)
@@ -579,6 +584,9 @@ process_arguments (int argc, char **argv)
 		case 'v':									/* verbose */
 			verbose++;
 			break;
+		case 'q':
+			ignore_send_quit_failure++;             /* ignore problem sending QUIT */
+			break;
 		case 't':									/* timeout */
 			if (is_intnonneg (optarg)) {
 				socket_timeout = atoi (optarg);
@@ -662,8 +670,20 @@ void
 smtp_quit(void)
 {
 	int bytes;
+	int n;
+
+	n = my_send(SMTP_QUIT, strlen(SMTP_QUIT));
+	if(n < 0) {
+		if(ignore_send_quit_failure) {
+			if(verbose) {
+				printf(_("Connection closed by server before sending QUIT command\n"));
+			}
+			return;
+		}
+		die (STATE_UNKNOWN,
+			_("Connection closed by server before sending QUIT command\n"));
+	}
 
-	my_send(SMTP_QUIT, strlen(SMTP_QUIT));
 	if (verbose)
 		printf(_("sent %s\n"), "QUIT");
 
@@ -797,7 +817,9 @@ print_help (void)
   printf ("    %s\n", _("SMTP AUTH username"));
   printf (" %s\n", "-P, --authpass=STRING");
   printf ("    %s\n", _("SMTP AUTH password"));
-
+  printf (" %s\n", "-q, --ignore-quit-failure");
+  printf ("    %s\n", _("Ignore failure when sending QUIT command to server"));
+   
 	printf (UT_WARN_CRIT);
 
 	printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
@@ -821,6 +843,6 @@ print_usage (void)
   printf ("%s\n", _("Usage:"));
   printf ("%s -H host [-p port] [-e expect] [-C command] [-f from addr]", progname);
   printf ("[-A authtype -U authuser -P authpass] [-w warn] [-c crit] [-t timeout]\n");
-  printf ("[-F fqdn] [-S] [-D days] [-v] [-4|-6]\n");
+  printf ("[-F fqdn] [-S] [-D days] [-v] [-4|-6] [-q]\n");
 }
 
-- 
cgit v0.10-9-g596f

