Browse Source

New function to for escaped strings from command line for send/quit.
Adapted from Sebastian Wiesinger's patch (1292404)


git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1365 f882894a-f735-0410-b71e-b25c423dba1c

Ton Voon 20 years ago
parent
commit
f5c1cf6dd4
5 changed files with 86 additions and 33 deletions
  1. 1 0
      THANKS.in
  2. 23 32
      plugins/check_tcp.c
  3. 30 1
      plugins/tests/test_utils.c
  4. 30 0
      plugins/utils.c
  5. 2 0
      plugins/utils.h

+ 1 - 0
THANKS.in

@@ -179,3 +179,4 @@ Steven Kreuzer
 Johan Fischer
 Johan Fischer
 Sakari Lehtonen
 Sakari Lehtonen
 John Rouillard
 John Rouillard
+Sebastian Wiesinger

+ 23 - 32
plugins/check_tcp.c

@@ -54,8 +54,6 @@ static int server_port = 0;
 static char *server_address = NULL;
 static char *server_address = NULL;
 static char *server_send = NULL;
 static char *server_send = NULL;
 static char *server_quit = NULL;
 static char *server_quit = NULL;
-char *lineend = "";
-char *lineendquit = "\r\n";
 static char **server_expect;
 static char **server_expect;
 static size_t server_expect_count = 0;
 static size_t server_expect_count = 0;
 static size_t maxbytes = 0;
 static size_t maxbytes = 0;
@@ -246,6 +244,12 @@ main (int argc, char **argv)
 	}
 	}
 
 
 	if(flags & FLAG_VERBOSE) {
 	if(flags & FLAG_VERBOSE) {
+		if (server_send) {
+			printf("Send string: %s\n", server_send);
+		}
+		if (server_quit) {
+			printf("Quit string: %s\n", server_quit);
+		}
 		printf("server_expect_count: %d\n", (int)server_expect_count);
 		printf("server_expect_count: %d\n", (int)server_expect_count);
 		for(i = 0; i < server_expect_count; i++)
 		for(i = 0; i < server_expect_count; i++)
 			printf("\t%d: %s\n", i, server_expect[i]);
 			printf("\t%d: %s\n", i, server_expect[i]);
@@ -364,6 +368,7 @@ static int
 process_arguments (int argc, char **argv)
 process_arguments (int argc, char **argv)
 {
 {
 	int c;
 	int c;
+	int escape = 0;
 
 
 	int option = 0;
 	int option = 0;
 	static struct option longopts[] = {
 	static struct option longopts[] = {
@@ -375,7 +380,7 @@ process_arguments (int argc, char **argv)
 		{"timeout", required_argument, 0, 't'},
 		{"timeout", required_argument, 0, 't'},
 		{"protocol", required_argument, 0, 'P'},
 		{"protocol", required_argument, 0, 'P'},
 		{"port", required_argument, 0, 'p'},
 		{"port", required_argument, 0, 'p'},
-		{"lineend", required_argument, 0, 'l'},
+		{"escape", required_argument, 0, 'E'},
 		{"send", required_argument, 0, 's'},
 		{"send", required_argument, 0, 's'},
 		{"expect", required_argument, 0, 'e'},
 		{"expect", required_argument, 0, 'e'},
 		{"maxbytes", required_argument, 0, 'm'},
 		{"maxbytes", required_argument, 0, 'm'},
@@ -417,7 +422,7 @@ process_arguments (int argc, char **argv)
 	}
 	}
 
 
 	while (1) {
 	while (1) {
-		c = getopt_long (argc, argv, "+hVv46H:l:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:",
+		c = getopt_long (argc, argv, "+hVv46EH:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:",
 		                 longopts, &option);
 		                 longopts, &option);
 
 
 		if (c == -1 || c == EOF || c == 1)
 		if (c == -1 || c == EOF || c == 1)
@@ -485,30 +490,14 @@ process_arguments (int argc, char **argv)
 			else
 			else
 				server_port = atoi (optarg);
 				server_port = atoi (optarg);
 			break;
 			break;
-		case 'l':
-			switch (*optarg) {
-				case 'n':
-				  lineend = strdup("\n");
-				  lineendquit = lineend;
-				  break;
-				case 'r':
-				  lineend = strdup("\r");
-				  lineendquit = lineend;
-				  break;
-				case 'b':
-				  lineend = strdup("\r\n");
-				  lineendquit = lineend;
-				  break;
-				case 'e':
-				  lineend = strdup("");
-				  lineendquit = lineend;
-				  break;
-				default:
-				  usage4 (_("Unrecognized option to -l, must be r, n, b or e"));
-                        }
+		case 'E':
+			escape = 1;
 			break;
 			break;
 		case 's':
 		case 's':
-		        asprintf(&server_send, "%s%s", optarg, lineend);
+			if (escape)
+				server_send = np_escaped_string(optarg);
+			else
+				asprintf(&server_send, "%s", optarg);
 			break;
 			break;
 		case 'e': /* expect string (may be repeated) */
 		case 'e': /* expect string (may be repeated) */
 			EXPECT = NULL;
 			EXPECT = NULL;
@@ -525,7 +514,10 @@ process_arguments (int argc, char **argv)
 			else
 			else
 				maxbytes = strtol (optarg, NULL, 0);
 				maxbytes = strtol (optarg, NULL, 0);
 		case 'q':
 		case 'q':
-			asprintf(&server_quit, "%s%s", optarg, lineendquit);
+			if (escape)
+				server_quit = np_escaped_string(optarg);
+			else
+				asprintf(&server_quit, "%s\r\n", optarg);
 			break;
 			break;
 		case 'r':
 		case 'r':
 			if (!strncmp(optarg,"ok",2))
 			if (!strncmp(optarg,"ok",2))
@@ -604,10 +596,9 @@ print_help (void)
 	printf (_(UT_IPv46));
 	printf (_(UT_IPv46));
 
 
 	printf (_("\
 	printf (_("\
- -l, --lineend=b|e|n|r\n\
-    Ending on -s and -q strings. b - both: <cr><lf> style, e - empty no\n\
-    end, n - newline: newline end, r - return: carriage return end\n\
-    Default is \"-l e -s <send> -l b -q <quit>\".\n\
+ -E, --escape\n\
+    Can use \\n, \\r, \\t or \\ in send or quit string.\n\
+    Default: nothing added to send, \\r\\n added to end of quit\n\
  -s, --send=STRING\n\
  -s, --send=STRING\n\
     String to send to the server\n\
     String to send to the server\n\
  -e, --expect=STRING\n\
  -e, --expect=STRING\n\
@@ -653,6 +644,6 @@ Usage: %s -H host -p port [-w <warning time>] [-c <critical time>]\n\
                   [-s <send string>] [-e <expect string>] [-q <quit string>]\n\
                   [-s <send string>] [-e <expect string>] [-q <quit string>]\n\
                   [-m <maximum bytes>] [-d <delay>] [-t <timeout seconds>]\n\
                   [-m <maximum bytes>] [-d <delay>] [-t <timeout seconds>]\n\
                   [-r <refuse state>] [-M <mismatch state>] [-v] [-4|-6] [-j]\n\
                   [-r <refuse state>] [-M <mismatch state>] [-v] [-4|-6] [-j]\n\
-                  [-D <days to cert expiry>] [-S <use SSL>] [-l <n|r|b|e>]\n", progname);
+                  [-D <days to cert expiry>] [-S <use SSL>] [-E]\n", progname);
 }
 }
 
 

+ 30 - 1
plugins/tests/test_utils.c

@@ -34,7 +34,7 @@ main (int argc, char **argv)
 	thresholds *thresholds = NULL;
 	thresholds *thresholds = NULL;
 	int	rc;
 	int	rc;
 
 
-	plan_tests(66);
+	plan_tests(73);
 
 
 	range = parse_range_string("6");
 	range = parse_range_string("6");
 	ok( range != NULL, "'6' is valid range");
 	ok( range != NULL, "'6' is valid range");
@@ -136,6 +136,35 @@ main (int argc, char **argv)
 	ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning");
 	ok( get_status(30.0001, thresholds) == STATE_WARNING, "30.0001 - warning");
 	ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical");
 	ok( get_status(69, thresholds) == STATE_CRITICAL, "69 - critical");
 
 
+	char *test;
+	test = np_escaped_string("bob\\n");
+	ok( strcmp(test, "bob\n") == 0, "bob\\n ok");
+	free(test);
+
+	test = np_escaped_string("rhuba\\rb");
+	ok( strcmp(test, "rhuba\rb") == 0, "rhuba\\rb okay");
+	free(test);
+
+	test = np_escaped_string("ba\\nge\\r");
+	ok( strcmp(test, "ba\nge\r") == 0, "ba\\nge\\r okay");
+	free(test);
+
+	test = np_escaped_string("\\rabbi\\t");
+	ok( strcmp(test, "\rabbi\t") == 0, "\\rabbi\\t okay");
+	free(test);
+
+	test = np_escaped_string("and\\\\or");
+	ok( strcmp(test, "and\\or") == 0, "and\\\\or okay");
+	free(test);
+
+	test = np_escaped_string("bo\\gus");
+	ok( strcmp(test, "bogus") == 0, "bo\\gus okay");
+	free(test);
+
+	test = np_escaped_string("everything");
+	ok( strcmp(test, "everything") == 0, "everything okay");
+	free(test);
+
 	return exit_status();
 	return exit_status();
 }
 }
 
 

+ 30 - 0
plugins/utils.c

@@ -727,3 +727,33 @@ char *fperfdata (const char *label,
 
 
 	return data;
 	return data;
 }
 }
+
+char *np_escaped_string (const char *string) {
+	char *data;
+	int i, j=0;
+	data = strdup(string);
+	for (i=0; data[i]; i++) {
+		if (data[i] == '\\') {
+			switch(data[++i]) {
+				case 'n':
+					data[j++] = '\n';
+					break;
+				case 'r':
+					data[j++] = '\r';
+					break;
+				case 't':
+					data[j++] = '\t';
+					break;
+				case '\\':
+					data[j++] = '\\';
+					break;
+				default:
+					data[j++] = data[i];
+			}
+		} else {
+			data[j++] = data[i];
+		}
+	}
+	data[j] = '\0';
+	return data;
+}

+ 2 - 0
plugins/utils.h

@@ -132,6 +132,8 @@ char *fperfdata (const char *,
  int,
  int,
  double);
  double);
 
 
+char *np_escaped_string (const char *);
+
 /* The idea here is that, although not every plugin will use all of these, 
 /* The idea here is that, although not every plugin will use all of these, 
    most will or should.  Therefore, for consistency, these very common 
    most will or should.  Therefore, for consistency, these very common 
    options should have only these meanings throughout the overall suite */
    options should have only these meanings throughout the overall suite */