diff options
author | Karl DeBisschop <kdebisschop@users.sourceforge.net> | 2002-10-30 18:29:07 +0000 |
---|---|---|
committer | Karl DeBisschop <kdebisschop@users.sourceforge.net> | 2002-10-30 18:29:07 +0000 |
commit | 7bf8d5f9e19023ddd543b48a23cd39ca83332d18 (patch) | |
tree | c6c34c781acdf96bfb37bde82cff0bfab5d780de /plugins/check_smtp.c | |
parent | 794d3418fa473a8af9fed1f07481476a07a265d3 (diff) | |
download | monitoring-plugins-7bf8d5f9e19023ddd543b48a23cd39ca83332d18.tar.gz |
remove call_getopt, fix several buffer overruns possible due to use of fixed size buffers
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@165 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins/check_smtp.c')
-rw-r--r-- | plugins/check_smtp.c | 147 |
1 files changed, 55 insertions, 92 deletions
diff --git a/plugins/check_smtp.c b/plugins/check_smtp.c index 4e5d3c81..5afb6941 100644 --- a/plugins/check_smtp.c +++ b/plugins/check_smtp.c | |||
@@ -61,9 +61,7 @@ | |||
61 | #define SMTP_QUIT "QUIT\r\n" | 61 | #define SMTP_QUIT "QUIT\r\n" |
62 | 62 | ||
63 | int process_arguments (int, char **); | 63 | int process_arguments (int, char **); |
64 | int call_getopt (int, char **); | ||
65 | int validate_arguments (void); | 64 | int validate_arguments (void); |
66 | int check_disk (int usp, int free_disk); | ||
67 | void print_help (void); | 65 | void print_help (void); |
68 | void print_usage (void); | 66 | void print_usage (void); |
69 | 67 | ||
@@ -80,35 +78,28 @@ int verbose = FALSE; | |||
80 | int | 78 | int |
81 | main (int argc, char **argv) | 79 | main (int argc, char **argv) |
82 | { | 80 | { |
83 | int sd; | 81 | int sd, c; |
84 | int result; | 82 | int result; |
85 | char buffer[MAX_INPUT_BUFFER] = ""; | 83 | char buffer[MAX_INPUT_BUFFER] = ""; |
86 | char helocmd[255] = SMTP_HELO ; | 84 | char *from_str = NULL; |
87 | char from_str[255] = SMTP_DUMMYCMD ; | 85 | char *helocmd = NULL; |
88 | char myhostname[248]; | ||
89 | |||
90 | 86 | ||
91 | if (process_arguments (argc, argv) != OK) | 87 | if (process_arguments (argc, argv) != OK) |
92 | usage ("Invalid command arguments supplied\n"); | 88 | usage ("Invalid command arguments supplied\n"); |
93 | 89 | ||
94 | /* initialize the HELO command with the localhostname */ | 90 | /* initialize the HELO command with the localhostname */ |
95 | gethostname(myhostname, sizeof(myhostname)); | 91 | #ifndef HOST_MAX_BYTES |
96 | strcat(helocmd, myhostname); | 92 | #define HOST_MAX_BYTES 255 |
97 | strcat(helocmd, "\r\n"); | 93 | #endif |
94 | helocmd = malloc (HOST_MAX_BYTES); | ||
95 | gethostname(helocmd, HOST_MAX_BYTES); | ||
96 | asprintf (&helocmd, "%s%s%s", SMTP_HELO, helocmd, "\r\n"); | ||
98 | 97 | ||
99 | /* initialize the MAIL command with optional FROM command */ | 98 | /* initialize the MAIL command with optional FROM command */ |
100 | if (from_arg) { | 99 | asprintf (&from_str, "%sFROM: %s%s", SMTP_DUMMYCMD, from_arg, "\r\n"); |
101 | strcat(from_str, "FROM: "); | ||
102 | strcat(from_str, from_arg); | ||
103 | } | ||
104 | /* terminate line with a CRLF */ | ||
105 | strcat(from_str, "\r\n"); | ||
106 | |||
107 | if (verbose == TRUE){ | ||
108 | printf ("FROMCMD: %s\n", from_str); | ||
109 | } | ||
110 | 100 | ||
111 | 101 | if (verbose == TRUE) | |
102 | printf ("FROMCMD: %s\n", from_str); | ||
112 | 103 | ||
113 | /* initialize alarm signal handling */ | 104 | /* initialize alarm signal handling */ |
114 | signal (SIGALRM, socket_timeout_alarm_handler); | 105 | signal (SIGALRM, socket_timeout_alarm_handler); |
@@ -170,21 +161,27 @@ main (int argc, char **argv) | |||
170 | } | 161 | } |
171 | 162 | ||
172 | /* close the connection */ | 163 | /* close the connection */ |
164 | |||
173 | /* first send the HELO command */ | 165 | /* first send the HELO command */ |
174 | send(sd,helocmd,strlen(helocmd),0); | 166 | send(sd, helocmd, strlen(helocmd), 0); |
167 | |||
175 | /* allow for response to helo command to reach us */ | 168 | /* allow for response to helo command to reach us */ |
176 | recv(sd,buffer,MAX_INPUT_BUFFER-1,0); | 169 | recv(sd, buffer, MAX_INPUT_BUFFER-1, 0); |
177 | 170 | ||
178 | #ifdef SMTP_USE_DUMMYCMD | 171 | #ifdef SMTP_USE_DUMMYCMD |
179 | send(sd,from_str,strlen(from_str),0); | 172 | send(sd, from_str, strlen(from_str), 0); |
173 | |||
180 | /* allow for response to DUMMYCMD to reach us */ | 174 | /* allow for response to DUMMYCMD to reach us */ |
181 | recv(sd,buffer,MAX_INPUT_BUFFER-1,0); | 175 | recv(sd, buffer, MAX_INPUT_BUFFER-1, 0); |
176 | |||
182 | if (verbose == TRUE) | 177 | if (verbose == TRUE) |
183 | printf("DUMMYCMD: %s\n%s\n",from_str,buffer); | 178 | printf("DUMMYCMD: %s\n%s\n",from_str,buffer); |
184 | #endif /* SMTP_USE_DUMMYCMD */ | 179 | #endif /* SMTP_USE_DUMMYCMD */ |
185 | 180 | ||
186 | /* finally close the connection */ | 181 | /* tell the server we're done */ |
187 | send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0); | 182 | send (sd, SMTP_QUIT, strlen (SMTP_QUIT), 0); |
183 | |||
184 | /* finally close the connection */ | ||
188 | close (sd); | 185 | close (sd); |
189 | } | 186 | } |
190 | 187 | ||
@@ -205,55 +202,6 @@ process_arguments (int argc, char **argv) | |||
205 | { | 202 | { |
206 | int c; | 203 | int c; |
207 | 204 | ||
208 | if (argc < 2) | ||
209 | return ERROR; | ||
210 | |||
211 | for (c = 1; c < argc; c++) { | ||
212 | if (strcmp ("-to", argv[c]) == 0) | ||
213 | strcpy (argv[c], "-t"); | ||
214 | else if (strcmp ("-wt", argv[c]) == 0) | ||
215 | strcpy (argv[c], "-w"); | ||
216 | else if (strcmp ("-ct", argv[c]) == 0) | ||
217 | strcpy (argv[c], "-c"); | ||
218 | } | ||
219 | |||
220 | |||
221 | |||
222 | c = 0; | ||
223 | while ((c += (call_getopt (argc - c, &argv[c]))) < argc) { | ||
224 | |||
225 | if (is_option (argv[c])) | ||
226 | continue; | ||
227 | |||
228 | if (server_address == NULL) { | ||
229 | if (is_host (argv[c])) { | ||
230 | server_address = argv[c]; | ||
231 | } | ||
232 | else { | ||
233 | usage ("Invalid host name"); | ||
234 | } | ||
235 | } | ||
236 | } | ||
237 | |||
238 | if (server_address == NULL) | ||
239 | server_address = strscpy (NULL, "127.0.0.1"); | ||
240 | |||
241 | if (server_expect == NULL) | ||
242 | server_expect = strscpy (NULL, SMTP_EXPECT); | ||
243 | |||
244 | return validate_arguments (); | ||
245 | } | ||
246 | |||
247 | |||
248 | |||
249 | |||
250 | |||
251 | |||
252 | int | ||
253 | call_getopt (int argc, char **argv) | ||
254 | { | ||
255 | int c, i = 0; | ||
256 | |||
257 | #ifdef HAVE_GETOPT_H | 205 | #ifdef HAVE_GETOPT_H |
258 | int option_index = 0; | 206 | int option_index = 0; |
259 | static struct option long_options[] = { | 207 | static struct option long_options[] = { |
@@ -270,6 +218,18 @@ call_getopt (int argc, char **argv) | |||
270 | }; | 218 | }; |
271 | #endif | 219 | #endif |
272 | 220 | ||
221 | if (argc < 2) | ||
222 | return ERROR; | ||
223 | |||
224 | for (c = 1; c < argc; c++) { | ||
225 | if (strcmp ("-to", argv[c]) == 0) | ||
226 | strcpy (argv[c], "-t"); | ||
227 | else if (strcmp ("-wt", argv[c]) == 0) | ||
228 | strcpy (argv[c], "-w"); | ||
229 | else if (strcmp ("-ct", argv[c]) == 0) | ||
230 | strcpy (argv[c], "-c"); | ||
231 | } | ||
232 | |||
273 | while (1) { | 233 | while (1) { |
274 | #ifdef HAVE_GETOPT_H | 234 | #ifdef HAVE_GETOPT_H |
275 | c = | 235 | c = |
@@ -278,24 +238,10 @@ call_getopt (int argc, char **argv) | |||
278 | #else | 238 | #else |
279 | c = getopt (argc, argv, "+?hVvt:p:f:e:c:w:H:"); | 239 | c = getopt (argc, argv, "+?hVvt:p:f:e:c:w:H:"); |
280 | #endif | 240 | #endif |
281 | 241 | if (c == -1 || c == EOF) | |
282 | i++; | ||
283 | |||
284 | if (c == -1 || c == EOF || c == 1) | ||
285 | break; | 242 | break; |
286 | 243 | ||
287 | switch (c) { | 244 | switch (c) { |
288 | case 't': | ||
289 | case 'p': | ||
290 | case 'e': | ||
291 | case 'f': | ||
292 | case 'c': | ||
293 | case 'w': | ||
294 | case 'H': | ||
295 | i++; | ||
296 | } | ||
297 | |||
298 | switch (c) { | ||
299 | case 'H': /* hostname */ | 245 | case 'H': /* hostname */ |
300 | if (is_host (optarg)) { | 246 | if (is_host (optarg)) { |
301 | server_address = optarg; | 247 | server_address = optarg; |
@@ -357,7 +303,24 @@ call_getopt (int argc, char **argv) | |||
357 | usage ("Invalid argument\n"); | 303 | usage ("Invalid argument\n"); |
358 | } | 304 | } |
359 | } | 305 | } |
360 | return i; | 306 | |
307 | c = optind; | ||
308 | if (server_address == NULL) { | ||
309 | if (argv[c]) { | ||
310 | if (is_host (argv[c])) | ||
311 | server_address = argv[c]; | ||
312 | else | ||
313 | usage ("Invalid host name"); | ||
314 | } | ||
315 | else { | ||
316 | asprintf (&server_address, "127.0.0.1"); | ||
317 | } | ||
318 | } | ||
319 | |||
320 | if (server_expect == NULL) | ||
321 | asprintf (&server_expect, SMTP_EXPECT); | ||
322 | |||
323 | return validate_arguments (); | ||
361 | } | 324 | } |
362 | 325 | ||
363 | 326 | ||