diff options
author | Karl DeBisschop <kdebisschop@users.sourceforge.net> | 2002-11-25 12:00:17 +0000 |
---|---|---|
committer | Karl DeBisschop <kdebisschop@users.sourceforge.net> | 2002-11-25 12:00:17 +0000 |
commit | 3481294648b99121eb93f7b26d77594f86c9f582 (patch) | |
tree | ad81b6cd6a8f363d92d2c6c6d69eb87009986c1b /plugins | |
parent | f38d8b1f05a9cb68b516da52549efa04f96b476e (diff) | |
download | monitoring-plugins-3481294648b99121eb93f7b26d77594f86c9f582.tar.gz |
was segfaulting if no dtat was returnedr1_3_0-beta2
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@228 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins')
-rw-r--r-- | plugins/check_tcp.c | 93 |
1 files changed, 47 insertions, 46 deletions
diff --git a/plugins/check_tcp.c b/plugins/check_tcp.c index 228a5335..29a7ab57 100644 --- a/plugins/check_tcp.c +++ b/plugins/check_tcp.c | |||
@@ -91,20 +91,20 @@ int check_critical_time = FALSE; | |||
91 | double elapsed_time = 0; | 91 | double elapsed_time = 0; |
92 | int verbose = FALSE; | 92 | int verbose = FALSE; |
93 | int use_ssl = FALSE; | 93 | int use_ssl = FALSE; |
94 | int sd; | 94 | int sd = 0; |
95 | 95 | ||
96 | int | 96 | int |
97 | main (int argc, char **argv) | 97 | main (int argc, char **argv) |
98 | { | 98 | { |
99 | int result; | 99 | int result; |
100 | int i; | 100 | int i; |
101 | char buffer[MAX_INPUT_BUFFER] = ""; | 101 | char *buffer = ""; |
102 | char *status = ""; | 102 | char *status = ""; |
103 | struct timeval tv; | 103 | struct timeval tv; |
104 | 104 | ||
105 | if (strstr (argv[0], "check_udp")) { | 105 | if (strstr (argv[0], "check_udp")) { |
106 | PROGNAME = strscpy (PROGNAME, "check_udp"); | 106 | asprintf (&PROGNAME, "check_udp"); |
107 | SERVICE = strscpy (SERVICE, "UDP"); | 107 | asprintf (&SERVICE, "UDP"); |
108 | SEND = NULL; | 108 | SEND = NULL; |
109 | EXPECT = NULL; | 109 | EXPECT = NULL; |
110 | QUIT = NULL; | 110 | QUIT = NULL; |
@@ -112,8 +112,8 @@ main (int argc, char **argv) | |||
112 | PORT = 0; | 112 | PORT = 0; |
113 | } | 113 | } |
114 | else if (strstr (argv[0], "check_tcp")) { | 114 | else if (strstr (argv[0], "check_tcp")) { |
115 | PROGNAME = strscpy (PROGNAME, "check_tcp"); | 115 | asprintf (&PROGNAME, "check_tcp"); |
116 | SERVICE = strscpy (SERVICE, "TCP"); | 116 | asprintf (&SERVICE, "TCP"); |
117 | SEND = NULL; | 117 | SEND = NULL; |
118 | EXPECT = NULL; | 118 | EXPECT = NULL; |
119 | QUIT = NULL; | 119 | QUIT = NULL; |
@@ -121,73 +121,73 @@ main (int argc, char **argv) | |||
121 | PORT = 0; | 121 | PORT = 0; |
122 | } | 122 | } |
123 | else if (strstr (argv[0], "check_ftp")) { | 123 | else if (strstr (argv[0], "check_ftp")) { |
124 | PROGNAME = strscpy (PROGNAME, "check_ftp"); | 124 | asprintf (&PROGNAME, "check_ftp"); |
125 | SERVICE = strscpy (SERVICE, "FTP"); | 125 | asprintf (&SERVICE, "FTP"); |
126 | SEND = NULL; | 126 | SEND = NULL; |
127 | EXPECT = strscpy (EXPECT, "220"); | 127 | asprintf (&EXPECT, "220"); |
128 | QUIT = strscpy (QUIT, "QUIT\r\n"); | 128 | asprintf (&QUIT, "QUIT\r\n"); |
129 | PROTOCOL = TCP_PROTOCOL; | 129 | PROTOCOL = TCP_PROTOCOL; |
130 | PORT = 21; | 130 | PORT = 21; |
131 | } | 131 | } |
132 | else if (strstr (argv[0], "check_smtp")) { | 132 | else if (strstr (argv[0], "check_smtp")) { |
133 | PROGNAME = strscpy (PROGNAME, "check_smtp"); | 133 | asprintf (&PROGNAME, "check_smtp"); |
134 | SERVICE = strscpy (SERVICE, "SMTP"); | 134 | asprintf (&SERVICE, "SMTP"); |
135 | SEND = NULL; | 135 | SEND = NULL; |
136 | EXPECT = strscpy (EXPECT, "220"); | 136 | asprintf (&EXPECT, "220"); |
137 | QUIT = strscpy (QUIT, "QUIT\r\n"); | 137 | asprintf (&QUIT, "QUIT\r\n"); |
138 | PROTOCOL = TCP_PROTOCOL; | 138 | PROTOCOL = TCP_PROTOCOL; |
139 | PORT = 25; | 139 | PORT = 25; |
140 | } | 140 | } |
141 | else if (strstr (argv[0], "check_pop")) { | 141 | else if (strstr (argv[0], "check_pop")) { |
142 | PROGNAME = strscpy (PROGNAME, "check_pop"); | 142 | asprintf (&PROGNAME, "check_pop"); |
143 | SERVICE = strscpy (SERVICE, "POP"); | 143 | asprintf (&SERVICE, "POP"); |
144 | SEND = NULL; | 144 | SEND = NULL; |
145 | EXPECT = strscpy (EXPECT, "110"); | 145 | asprintf (&EXPECT, "110"); |
146 | QUIT = strscpy (QUIT, "QUIT\r\n"); | 146 | asprintf (&QUIT, "QUIT\r\n"); |
147 | PROTOCOL = TCP_PROTOCOL; | 147 | PROTOCOL = TCP_PROTOCOL; |
148 | PORT = 110; | 148 | PORT = 110; |
149 | } | 149 | } |
150 | else if (strstr (argv[0], "check_imap")) { | 150 | else if (strstr (argv[0], "check_imap")) { |
151 | PROGNAME = strscpy (PROGNAME, "check_imap"); | 151 | asprintf (&PROGNAME, "check_imap"); |
152 | SERVICE = strscpy (SERVICE, "IMAP"); | 152 | asprintf (&SERVICE, "IMAP"); |
153 | SEND = NULL; | 153 | SEND = NULL; |
154 | EXPECT = strscpy (EXPECT, "* OK"); | 154 | asprintf (&EXPECT, "* OK"); |
155 | QUIT = strscpy (QUIT, "a1 LOGOUT\r\n"); | 155 | asprintf (&QUIT, "a1 LOGOUT\r\n"); |
156 | PROTOCOL = TCP_PROTOCOL; | 156 | PROTOCOL = TCP_PROTOCOL; |
157 | PORT = 143; | 157 | PORT = 143; |
158 | } | 158 | } |
159 | #ifdef HAVE_SSL | 159 | #ifdef HAVE_SSL |
160 | else if (strstr(argv[0],"check_simap")) { | 160 | else if (strstr(argv[0],"check_simap")) { |
161 | PROGNAME=strscpy(PROGNAME,"check_simap"); | 161 | asprintf (&PROGNAME, "check_simap"); |
162 | SERVICE=strscpy(SERVICE,"SIMAP"); | 162 | asprintf (&SERVICE, "SIMAP"); |
163 | SEND=NULL; | 163 | SEND=NULL; |
164 | EXPECT=strscpy(EXPECT,"* OK"); | 164 | asprintf (&EXPECT, "* OK"); |
165 | QUIT=strscpy(QUIT,"a1 LOGOUT\r\n"); | 165 | asprintf (&QUIT, "a1 LOGOUT\r\n"); |
166 | PROTOCOL=TCP_PROTOCOL; | 166 | PROTOCOL=TCP_PROTOCOL; |
167 | use_ssl=TRUE; | 167 | use_ssl=TRUE; |
168 | PORT=993; | 168 | PORT=993; |
169 | } | 169 | } |
170 | else if (strstr(argv[0],"check_spop")) { | 170 | else if (strstr(argv[0],"check_spop")) { |
171 | PROGNAME=strscpy(PROGNAME,"check_spop"); | 171 | asprintf (&PROGNAME, "check_spop"); |
172 | SERVICE=strscpy(SERVICE,"SPOP"); | 172 | asprintf (&SERVICE, "SPOP"); |
173 | SEND=NULL; | 173 | SEND=NULL; |
174 | EXPECT=strscpy(EXPECT,"110"); | 174 | asprintf (&EXPECT, "110"); |
175 | QUIT=strscpy(QUIT,"QUIT\r\n"); | 175 | asprintf (&QUIT, "QUIT\r\n"); |
176 | PROTOCOL=TCP_PROTOCOL; | 176 | PROTOCOL=TCP_PROTOCOL; |
177 | use_ssl=TRUE; | 177 | use_ssl=TRUE; |
178 | PORT=995; | 178 | PORT=995; |
179 | } | 179 | } |
180 | #endif | 180 | #endif |
181 | else if (strstr (argv[0], "check_nntp")) { | 181 | else if (strstr (argv[0], "check_nntp")) { |
182 | PROGNAME = strscpy (PROGNAME, "check_nntp"); | 182 | asprintf (&PROGNAME, "check_nntp"); |
183 | SERVICE = strscpy (SERVICE, "NNTP"); | 183 | asprintf (&SERVICE, "NNTP"); |
184 | SEND = NULL; | 184 | SEND = NULL; |
185 | EXPECT = NULL; | 185 | EXPECT = NULL; |
186 | server_expect = realloc (server_expect, ++server_expect_count); | 186 | server_expect = realloc (server_expect, ++server_expect_count); |
187 | server_expect[server_expect_count - 1] = strscpy (EXPECT, "200"); | 187 | asprintf (&server_expect[server_expect_count - 1], "200"); |
188 | server_expect = realloc (server_expect, ++server_expect_count); | 188 | server_expect = realloc (server_expect, ++server_expect_count); |
189 | server_expect[server_expect_count - 1] = strscpy (NULL, "201"); | 189 | asprintf (&server_expect[server_expect_count - 1], "201"); |
190 | QUIT = strscpy (QUIT, "QUIT\r\n"); | 190 | asprintf (&QUIT, "QUIT\r\n"); |
191 | PROTOCOL = TCP_PROTOCOL; | 191 | PROTOCOL = TCP_PROTOCOL; |
192 | PORT = 119; | 192 | PORT = 119; |
193 | } | 193 | } |
@@ -195,7 +195,7 @@ main (int argc, char **argv) | |||
195 | usage ("ERROR: Generic check_tcp called with unknown service\n"); | 195 | usage ("ERROR: Generic check_tcp called with unknown service\n"); |
196 | } | 196 | } |
197 | 197 | ||
198 | server_address = strscpy (NULL, "127.0.0.1"); | 198 | asprintf (&server_address, "127.0.0.1"); |
199 | server_port = PORT; | 199 | server_port = PORT; |
200 | server_send = SEND; | 200 | server_send = SEND; |
201 | server_quit = QUIT; | 201 | server_quit = QUIT; |
@@ -249,22 +249,22 @@ main (int argc, char **argv) | |||
249 | 249 | ||
250 | if (server_send || server_expect_count > 0) { | 250 | if (server_send || server_expect_count > 0) { |
251 | 251 | ||
252 | buffer = malloc (MAX_INPUT_BUFFER); | ||
252 | /* watch for the expect string */ | 253 | /* watch for the expect string */ |
253 | #ifdef HAVE_SSL | 254 | #ifdef HAVE_SSL |
254 | if (use_ssl && SSL_read (ssl, buffer, MAX_INPUT_BUFFER - 1)>=0) | 255 | if (use_ssl && SSL_read (ssl, buffer, MAX_INPUT_BUFFER - 1) > 0) |
255 | asprintf (&status, "%s%s", status, buffer); | 256 | asprintf (&status, "%s%s", status, buffer); |
256 | else | 257 | else |
257 | #endif | 258 | #endif |
258 | { | 259 | if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) > 0) |
259 | if (recv (sd, buffer, MAX_INPUT_BUFFER - 1, 0) >= 0) | 260 | asprintf (&status, "%s%s", status, buffer); |
260 | asprintf (&status, "%s%s", status, buffer); | ||
261 | } | ||
262 | strip (status); | ||
263 | 261 | ||
264 | /* return a CRITICAL status if we couldn't read any data */ | 262 | /* return a CRITICAL status if we couldn't read any data */ |
265 | if (status == NULL) | 263 | if (status == NULL) |
266 | terminate (STATE_CRITICAL, "No data received from host\n"); | 264 | terminate (STATE_CRITICAL, "No data received from host\n"); |
267 | 265 | ||
266 | strip (status); | ||
267 | |||
268 | if (status && verbose) | 268 | if (status && verbose) |
269 | printf ("%s\n", status); | 269 | printf ("%s\n", status); |
270 | 270 | ||
@@ -285,15 +285,16 @@ main (int argc, char **argv) | |||
285 | if (use_ssl) { | 285 | if (use_ssl) { |
286 | SSL_write (ssl, QUIT, strlen (QUIT)); | 286 | SSL_write (ssl, QUIT, strlen (QUIT)); |
287 | SSL_shutdown (ssl); | 287 | SSL_shutdown (ssl); |
288 | SSL_free (ssl); | 288 | SSL_free (ssl); |
289 | SSL_CTX_free (ctx); | 289 | SSL_CTX_free (ctx); |
290 | } | 290 | } |
291 | else | 291 | else |
292 | #endif | 292 | #endif |
293 | send (sd, server_quit, strlen (server_quit), 0); | 293 | send (sd, server_quit, strlen (server_quit), 0); |
294 | 294 | ||
295 | /* close the connection */ | 295 | /* close the connection */ |
296 | close (sd); | 296 | if (sd) |
297 | close (sd); | ||
297 | 298 | ||
298 | elapsed_time = delta_time (tv); | 299 | elapsed_time = delta_time (tv); |
299 | 300 | ||
@@ -310,7 +311,7 @@ main (int argc, char **argv) | |||
310 | SERVICE, | 311 | SERVICE, |
311 | state_text (result), elapsed_time, server_port); | 312 | state_text (result), elapsed_time, server_port); |
312 | 313 | ||
313 | if (strlen (status)) | 314 | if (status && strlen(status) > 0) |
314 | printf (" [%s]", status); | 315 | printf (" [%s]", status); |
315 | 316 | ||
316 | printf ("|time=%7.3f\n", elapsed_time); | 317 | printf ("|time=%7.3f\n", elapsed_time); |