summaryrefslogtreecommitdiffstats
path: root/web/attachments/125109-tcp_bin.patch
blob: 1ac15e4077e68ca68293f486e505efc248e41144 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
diff -rNu nagios_orig/plugins/check_tcp.c nagios-plugins-1.4/plugins/check_tcp.c
--- nagios_orig/plugins/check_tcp.c	2004-12-30 01:41:40.000000000 +0100
+++ nagios-plugins-1.4/plugins/check_tcp.c	2005-03-09 15:46:03.000000000 +0100
@@ -80,6 +80,8 @@
 char *server_address = NULL;
 char *server_send = NULL;
 char *server_quit = NULL;
+char *input_file_name = NULL;
+char *output_file_name = NULL;
 char **server_expect = NULL;
 size_t server_expect_count = 0;
 int maxbytes = 0;
@@ -132,6 +134,15 @@
 		PROTOCOL = TCP_PROTOCOL;
 		PORT = 0;
 	}
+	else if (strstr (argv[0], "check_tcp_bin")) {
+		progname = strdup ("check_tcp_bin");
+		SERVICE = strdup ("TCP");
+		SEND = NULL;
+		EXPECT = NULL;
+		QUIT = NULL;
+		PROTOCOL = TCP_PROTOCOL;
+		PORT = 0;
+	}
 	else if (strstr (argv[0], "check_ftp")) {
 		progname = strdup ("check_ftp");
 		SERVICE = strdup ("FTP");
@@ -299,6 +310,42 @@
 			send (sd, server_send, strlen(server_send), 0);
 	}
 
+	/* send content of input file ? */
+	if (input_file_name != NULL) {
+		struct stat istat;
+		if (stat(input_file_name, &istat) != 0) {
+			printf("UNKNOWN - File not found %s\n", input_file_name);
+			return STATE_UNKNOWN;		
+		}
+		size_t in_len = istat.st_size;
+
+		FILE * in = fopen(input_file_name, "rb");
+		if (in ==  NULL) {
+			printf("UNKNOWN - Can not open file: %s\n", input_file_name);
+			return STATE_UNKNOWN;		
+		}
+		char * indata = malloc(in_len);
+		if (indata == NULL) {
+			printf(_("UNKNOWN - Not enough memory\n"));
+			return STATE_UNKNOWN;		
+		}
+		size_t bytes = fread(indata, 1, in_len, in);
+		if (bytes != in_len) {
+			printf("UNKNOWN - Error reading file: \n", input_file_name);
+			free(indata);
+			fclose(in);
+			return STATE_UNKNOWN;		
+		}
+		fclose(in);
+#ifdef HAVE_SSL
+		if (use_ssl)
+			SSL_write(ssl, indata, in_len);
+		else
+#endif
+			send (sd, indata, in_len, 0);
+		free(indata);
+	}
+
 	if (delay > 0) {
 		tv.tv_sec += delay;
 		sleep (delay);
@@ -345,6 +392,57 @@
 		}
 	}
 
+	/* Verify server response */
+	if (output_file_name != NULL) {
+		struct stat ostat;
+		if (stat(output_file_name, &ostat) != 0) {
+			printf("UNKNOWN - File not found %s\n", output_file_name);
+			return STATE_UNKNOWN;		
+		}
+		size_t out_len = ostat.st_size;
+
+		FILE * out = fopen(output_file_name, "rb");
+		if (out ==  NULL) {
+			printf("UNKNOWN - Can not open file: %s\n", output_file_name);
+			return STATE_UNKNOWN;		
+		}
+		char * outdata = malloc(out_len);
+		if (outdata == NULL) {
+			printf(_("UNKNOWN - Not enough memory\n"));
+			return STATE_UNKNOWN;		
+		}
+		size_t bytes = fread(outdata, 1, out_len, out);
+		if (bytes != out_len) {
+			printf("UNKNOWN - Error reading file: \n", output_file_name);
+			free(outdata);
+			fclose(out);
+			return STATE_UNKNOWN;		
+		}
+		fclose(out);
+	
+		char * rcvbuf = malloc(out_len);
+		if (rcvbuf != NULL) {
+			char *p = rcvbuf;
+			size_t count = 0;
+			
+			do {
+				count += my_bin_recv(p, out_len-count);
+				p += count;
+			} while (count < out_len);
+
+			if (memcmp(outdata, rcvbuf, out_len) == 0) {
+				result = STATE_OK;
+			} else {
+                printf(_("WARNING response does not match\n"));
+				result = expect_mismatch_state;
+			}
+			free(rcvbuf);
+		} else {
+			result = STATE_UNKNOWN;
+		}
+		
+	}
+
 	if (server_quit != NULL) {
 #ifdef HAVE_SSL
 		if (use_ssl) {
@@ -417,6 +515,8 @@
 		{"expect", required_argument, 0, 'e'},
 		{"maxbytes", required_argument, 0, 'm'},
 		{"quit", required_argument, 0, 'q'},
+		{"inputfile", required_argument, 0, 'i'},
+		{"outputfile", required_argument, 0, 'o'},
 		{"jail", required_argument, 0, 'j'},
 		{"delay", required_argument, 0, 'd'},
 		{"refuse", required_argument, 0, 'r'},
@@ -454,8 +554,8 @@
 	}
 
 	while (1) {
-		c = getopt_long (argc, argv, "+hVv46H:s:e:q:m:c:w:t:p:C:W:d:Sr:jD:M:",
-		                 longopts, &option);
+		c = getopt_long (argc, argv,
+			"+hVv46H:s:e:q:m:c:i:o:w:t:p:C:W:d:Sr:jD:M:", longopts, &option);
 
 		if (c == -1 || c == EOF || c == 1)
 			break;
@@ -527,6 +627,12 @@
 		case 's':
 			server_send = optarg;
 			break;
+		case 'i':
+			input_file_name = optarg;
+			break;
+		case 'o':
+			output_file_name = optarg;
+			break;
 		case 'e': /* expect string (may be repeated) */
 			EXPECT = NULL;
 			exact_matching = FALSE;
@@ -748,7 +854,18 @@
 	return i;
 }
 
-
+int my_bin_recv(char * buf, size_t len) {
+	int i;
+#ifdef HAVE_SSL
+	if (use_ssl) {
+		i = SSL_read (ssl, buf, len);
+	} else {
+#endif
+		i = read (sd, buf, len);
+#ifdef HAVE_SSL		
+	}
+#endif
+}
 
 void
 print_help (void)
@@ -786,6 +903,10 @@
     Hide output from TCP socket\n\
  -m, --maxbytes=INTEGER\n\
     Close connection once more than this number of bytes are received\n\
+ -i, --inputfile=file_name\n\
+    Input file to send to server\n\
+ -o, --outputfile=file_name\n\
+    Output file to binary compare with server response\n\
  -d, --delay=INTEGER\n\
     Seconds to wait between sending string and polling for response\n"));
 
@@ -816,5 +937,6 @@
                   [-s <send string>] [-e <expect string>] [-q <quit string>]\n\
                   [-m <maximum bytes>] [-d <delay>] [-t <timeout seconds>]\n\
                   [-r <refuse state>] [-M <mismatch state>] [-v] [-4|-6] [-j]\n\
+                  [-i <input file>] [-o <output file>]\n\
                   [-D <days to cert expiry>] [-S <use SSL>]\n", progname);		  
 }
diff -rNu nagios_orig/plugins/Makefile.am nagios-plugins-1.4/plugins/Makefile.am
--- nagios_orig/plugins/Makefile.am	2005-02-01 13:30:38.000000000 +0100
+++ nagios-plugins-1.4/plugins/Makefile.am	2005-03-09 17:25:07.000000000 +0100
@@ -16,8 +16,8 @@
 	check_udp check_ups check_users negate urlize check_icmp\
 	@EXTRAS@
 
-check_tcp_programs = check_ftp check_imap check_nntp check_pop check_udp2 \
-	@check_tcp_ssl@
+check_tcp_programs = check_ftp check_imap check_nntp check_pop check_tcp_bin \
+	check_udp2 @check_tcp_ssl@
 
 EXTRA_PROGRAMS = check_mysql check_radius check_pgsql check_snmp check_hpjd \
 	check_swap check_fping check_ldap check_game check_dig \