b4d4e3d
--- plugins/check_smtp.c	(revision 1769)
b4d4e3d
+++ plugins/check_smtp.c	(working copy)
91ba4da
@@ -40,6 +40,8 @@
91ba4da
 const char *copyright = "2000-2006";
91ba4da
 const char *email = "nagiosplug-devel@lists.sourceforge.net";
91ba4da
 
91ba4da
+#include <ctype.h>
91ba4da
+
91ba4da
 #include "common.h"
91ba4da
 #include "netutils.h"
91ba4da
 #include "utils.h"
91ba4da
@@ -74,6 +76,8 @@
91ba4da
 int validate_arguments (void);
91ba4da
 void print_help (void);
91ba4da
 void print_usage (void);
91ba4da
+int recvline(char *, size_t);
91ba4da
+int recvlines(char *, size_t);
91ba4da
 int my_close(void);
91ba4da
 
91ba4da
 #include "regex.h"
91ba4da
@@ -115,7 +119,6 @@
91ba4da
 enum {
91ba4da
   TCP_PROTOCOL = 1,
91ba4da
   UDP_PROTOCOL = 2,
91ba4da
-  MAXBUF = 1024
91ba4da
 };
91ba4da
 
91ba4da
 /* written by lauri alanko */
91ba4da
@@ -221,7 +224,7 @@
91ba4da
 
91ba4da
 		/* watch for the SMTP connection string and */
91ba4da
 		/* return a WARNING status if we couldn't read any data */
91ba4da
-		if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) == -1) {
91ba4da
+		if (recvlines(buffer, MAX_INPUT_BUFFER) <= 0) {
91ba4da
 			printf (_("recv() failed\n"));
91ba4da
 			result = STATE_WARNING;
91ba4da
 		}
91ba4da
@@ -245,11 +248,10 @@
91ba4da
 		send(sd, helocmd, strlen(helocmd), 0);
91ba4da
 
91ba4da
 		/* allow for response to helo command to reach us */
91ba4da
-		if(read (sd, buffer, MAXBUF - 1) < 0){
91ba4da
+		if (recvlines(buffer, MAX_INPUT_BUFFER) <= 0) {
91ba4da
 			printf (_("recv() failed\n"));
91ba4da
 			return STATE_WARNING;
91ba4da
 		} else if(use_ehlo){
91ba4da
-			buffer[MAXBUF-1]='\0';
91ba4da
 			if(strstr(buffer, "250 STARTTLS") != NULL ||
91ba4da
 			   strstr(buffer, "250-STARTTLS") != NULL){
91ba4da
 				supports_tls=TRUE;
91ba4da
@@ -267,7 +269,7 @@
91ba4da
 		  /* send the STARTTLS command */
91ba4da
 		  send(sd, SMTP_STARTTLS, strlen(SMTP_STARTTLS), 0);
91ba4da
 
91ba4da
-		  recv(sd,buffer, MAX_INPUT_BUFFER-1, 0); /* wait for it */
91ba4da
+		  recvlines(buffer, MAX_INPUT_BUFFER); /* wait for it */
91ba4da
 		  if (!strstr (buffer, server_expect)) {
91ba4da
 		    printf (_("Server does not support STARTTLS\n"));
91ba4da
 		    send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0);
91ba4da
@@ -301,13 +303,12 @@
91ba4da
 		}
91ba4da
 		if (verbose)
91ba4da
 			printf(_("sent %s"), helocmd);
91ba4da
-		if ((n = my_recv(buffer, MAX_INPUT_BUFFER - 1)) <= 0) {
91ba4da
+		if ((n = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) {
91ba4da
 			printf("%s\n", _("SMTP UNKNOWN - Cannot read EHLO response via TLS."));
91ba4da
 			my_close();
91ba4da
 			return STATE_UNKNOWN;
91ba4da
 		}
91ba4da
 		if (verbose) {
91ba4da
-			buffer[n] = '\0';
91ba4da
 			printf("%s", buffer);
91ba4da
 		}
91ba4da
 
91ba4da
@@ -336,16 +337,14 @@
91ba4da
 		 */
91ba4da
 		if (smtp_use_dummycmd) {
91ba4da
 		  my_send(cmd_str, strlen(cmd_str));
91ba4da
-		  my_recv(buffer, MAX_INPUT_BUFFER-1);
91ba4da
-		  if (verbose) 
91ba4da
+		  if (recvlines(buffer, MAX_INPUT_BUFFER) >= 1 && verbose)
91ba4da
 		    printf("%s", buffer);
91ba4da
 		}
91ba4da
 
91ba4da
 		while (n < ncommands) {
91ba4da
 			asprintf (&cmd_str, "%s%s", commands[n], "\r\n");
91ba4da
 			my_send(cmd_str, strlen(cmd_str));
91ba4da
-			my_recv(buffer, MAX_INPUT_BUFFER-1);
91ba4da
-			if (verbose) 
91ba4da
+			if (recvlines(buffer, MAX_INPUT_BUFFER) >= 1 && verbose)
91ba4da
 				printf("%s", buffer);
91ba4da
 			strip (buffer);
91ba4da
 			if (n < nresponses) {
91ba4da
@@ -394,12 +393,11 @@
91ba4da
 					if (verbose)
91ba4da
 						printf (_("sent %s\n"), "AUTH LOGIN");
91ba4da
 
91ba4da
-					if((ret = my_recv(buffer, MAXBUF - 1)) < 0){
91ba4da
+					if ((ret = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) {
91ba4da
 						asprintf(&error_msg, _("recv() failed after AUTH LOGIN, "));
91ba4da
 						result = STATE_WARNING;
91ba4da
 						break;
91ba4da
 					}
91ba4da
-					buffer[ret] = 0;
91ba4da
 					if (verbose)
91ba4da
 						printf (_("received %s\n"), buffer);
91ba4da
 
91ba4da
@@ -416,12 +414,11 @@
91ba4da
 					if (verbose)
91ba4da
 						printf (_("sent %s\n"), abuf);
91ba4da
 
91ba4da
-					if ((ret = my_recv(buffer, MAX_INPUT_BUFFER-1)) == -1) {
91ba4da
+					if ((ret = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) {
91ba4da
 						result = STATE_CRITICAL;
91ba4da
 						asprintf(&error_msg, _("recv() failed after sending authuser, "));
91ba4da
 						break;
91ba4da
 					}
91ba4da
-					buffer[ret] = 0;
91ba4da
 					if (verbose) {
91ba4da
 						printf (_("received %s\n"), buffer);
91ba4da
 					}
91ba4da
@@ -437,12 +434,11 @@
91ba4da
 					if (verbose) {
91ba4da
 						printf (_("sent %s\n"), abuf);
91ba4da
 					}
91ba4da
-					if ((ret = my_recv(buffer, MAX_INPUT_BUFFER-1)) == -1) {
91ba4da
+					if ((ret = recvlines(buffer, MAX_INPUT_BUFFER)) <= 0) {
91ba4da
 						result = STATE_CRITICAL;
91ba4da
 						asprintf(&error_msg, _("recv() failed after sending authpass, "));
91ba4da
 						break;
91ba4da
 					}
91ba4da
-					buffer[ret] = 0;
91ba4da
 					if (verbose) {
91ba4da
 						printf (_("received %s\n"), buffer);
91ba4da
 					}
91ba4da
@@ -700,6 +696,65 @@
91ba4da
 }
91ba4da
 
91ba4da
 
91ba4da
+/*
91ba4da
+ * Receive one line, copy it into buf and nul-terminate it.  Returns the
91ba4da
+ * number of bytes written to buf (excluding the '\0') or 0 on EOF or <0 on
91ba4da
+ * error.
91ba4da
+ *
91ba4da
+ * TODO: Reading one byte at a time is very inefficient.  Replace this by a
91ba4da
+ * function which buffers the data, move that to netutils.c and change
91ba4da
+ * check_smtp and other plugins to use that.  Also, remove (\r)\n.
91ba4da
+ */
91ba4da
+int
91ba4da
+recvline(char *buf, size_t bufsize)
91ba4da
+{
91ba4da
+	int result;
91ba4da
+	unsigned i;
91ba4da
+
91ba4da
+	for (i = result = 0; i < bufsize - 1; i++) {
91ba4da
+		if ((result = my_recv(&buf[i], 1)) != 1)
91ba4da
+			break;
91ba4da
+		if (buf[i] == '\n') {
91ba4da
+			buf[++i] = '\0';
91ba4da
+			return i;
91ba4da
+		}
91ba4da
+	}
91ba4da
+	return (result == 1 || i == 0) ? -2 : result;	/* -2 if out of space */
91ba4da
+}
91ba4da
+
91ba4da
+
91ba4da
+/*
91ba4da
+ * Receive one or more lines, copy them into buf and nul-terminate it.  Returns
91ba4da
+ * the number of bytes written to buf (excluding the '\0') or 0 on EOF or <0 on
91ba4da
+ * error.  Works for all protocols which format multiline replies as follows:
91ba4da
+ *
91ba4da
+ * ``The format for multiline replies requires that every line, except the last,
91ba4da
+ * begin with the reply code, followed immediately by a hyphen, `-' (also known
91ba4da
+ * as minus), followed by text.  The last line will begin with the reply code,
91ba4da
+ * followed immediately by <SP>, optionally some text, and <CRLF>.  As noted
91ba4da
+ * above, servers SHOULD send the <SP> if subsequent text is not sent, but
91ba4da
+ * clients MUST be prepared for it to be omitted.'' (RFC 2821, 4.2.1)
91ba4da
+ *
91ba4da
+ * TODO: Move this to netutils.c.  Also, remove \r and possibly the final \n.
91ba4da
+ */
91ba4da
+int
91ba4da
+recvlines(char *buf, size_t bufsize)
91ba4da
+{
91ba4da
+	int result;
91ba4da
+	unsigned i;
91ba4da
+
91ba4da
+	for (i = 0; /* forever */; i += result)
91ba4da
+		if (!((result = recvline(buf + i, bufsize - i)) > 3 &&
91ba4da
+		    isdigit(buf[i]) &&
91ba4da
+		    isdigit(buf[i + 1]) &&
91ba4da
+		    isdigit(buf[i + 2]) &&
91ba4da
+		    buf[i + 3] == '-'))
91ba4da
+			break;
91ba4da
+
91ba4da
+	return (result <= 0) ? result : result + (int)i;
91ba4da
+}
91ba4da
+
91ba4da
+
91ba4da
 int 
91ba4da
 my_close (void)
91ba4da
 {