summaryrefslogtreecommitdiffstats
path: root/plugins/check_dns.c
diff options
context:
space:
mode:
authorKarl DeBisschop <kdebisschop@users.sourceforge.net>2002-11-13 11:50:54 (GMT)
committerKarl DeBisschop <kdebisschop@users.sourceforge.net>2002-11-13 11:50:54 (GMT)
commit9e009c4b1128352c6039d25b39213fd480e9b055 (patch)
treee1495ef4ada6f8e092e20bc474195719f1b2b4aa /plugins/check_dns.c
parent9728dcad931d1c442a6d8e3e6765e2d9870600d1 (diff)
downloadmonitoring-plugins-9e009c4b1128352c6039d25b39213fd480e9b055.tar.gz
remove call_getopt and asprintf
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@190 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins/check_dns.c')
-rw-r--r--plugins/check_dns.c130
1 files changed, 55 insertions, 75 deletions
diff --git a/plugins/check_dns.c b/plugins/check_dns.c
index bc0400f..f9cf288 100644
--- a/plugins/check_dns.c
+++ b/plugins/check_dns.c
@@ -48,11 +48,14 @@
48#include "popen.h" 48#include "popen.h"
49#include "utils.h" 49#include "utils.h"
50 50
51#define PROGNAME "check_dns"
52#define REVISION "$Revision$"
53#define COPYRIGHT "2000-2002"
54
51int process_arguments (int, char **); 55int process_arguments (int, char **);
52int call_getopt (int, char **);
53int validate_arguments (void); 56int validate_arguments (void);
54void print_usage (char *); 57void print_usage (void);
55void print_help (char *); 58void print_help (void);
56int error_scan (char *); 59int error_scan (char *);
57 60
58#define ADDRESS_LENGTH 256 61#define ADDRESS_LENGTH 256
@@ -80,13 +83,12 @@ main (int argc, char **argv)
80 } 83 }
81 84
82 if (process_arguments (argc, argv) != OK) { 85 if (process_arguments (argc, argv) != OK) {
83 print_usage (my_basename (argv[0])); 86 print_usage ();
84 return STATE_UNKNOWN; 87 return STATE_UNKNOWN;
85 } 88 }
86 89
87 /* get the command to run */ 90 /* get the command to run */
88 command_line = ssprintf (command_line, "%s %s %s", NSLOOKUP_COMMAND, 91 sprintf (&command_line, "%s %s %s", NSLOOKUP_COMMAND, query_address, dns_server);
89 query_address, dns_server);
90 92
91 alarm (timeout_interval); 93 alarm (timeout_interval);
92 time (&start_time); 94 time (&start_time);
@@ -171,7 +173,7 @@ main (int argc, char **argv)
171 /* compare to expected address */ 173 /* compare to expected address */
172 if (result == STATE_OK && match_expected_address && strcmp(address, expected_address)) { 174 if (result == STATE_OK && match_expected_address && strcmp(address, expected_address)) {
173 result = STATE_CRITICAL; 175 result = STATE_CRITICAL;
174 output = ssprintf(output, "expected %s but got %s", expected_address, address); 176 asprintf(&output, "expected %s but got %s", expected_address, address);
175 } 177 }
176 178
177 (void) time (&end_time); 179 (void) time (&end_time);
@@ -256,46 +258,6 @@ process_arguments (int argc, char **argv)
256{ 258{
257 int c; 259 int c;
258 260
259 if (argc < 2)
260 return ERROR;
261
262 for (c = 1; c < argc; c++)
263 if (strcmp ("-to", argv[c]) == 0)
264 strcpy (argv[c], "-t");
265
266 c = 0;
267 while (c += (call_getopt (argc - c, &argv[c]))) {
268 if (argc <= c)
269 break;
270 if (query_address[0] == 0) {
271 if (is_host (argv[c]) == FALSE) {
272 printf ("Invalid name/address: %s\n\n", argv[c]);
273 return ERROR;
274 }
275 if (strlen (argv[c]) >= ADDRESS_LENGTH)
276 terminate (STATE_UNKNOWN, "Input buffer overflow\n");
277 strcpy (query_address, argv[c]);
278 }
279 else if (dns_server[0] == 0) {
280 if (is_host (argv[c]) == FALSE) {
281 printf ("Invalid name/address: %s\n\n", argv[c]);
282 return ERROR;
283 }
284 if (strlen (argv[c]) >= ADDRESS_LENGTH)
285 terminate (STATE_UNKNOWN, "Input buffer overflow\n");
286 strcpy (dns_server, argv[c]);
287 }
288 }
289
290 return validate_arguments ();
291
292}
293
294int
295call_getopt (int argc, char **argv)
296{
297 int c, i = 1;
298
299#ifdef HAVE_GETOPT_H 261#ifdef HAVE_GETOPT_H
300 int opt_index = 0; 262 int opt_index = 0;
301 static struct option long_opts[] = { 263 static struct option long_opts[] = {
@@ -311,37 +273,33 @@ call_getopt (int argc, char **argv)
311 }; 273 };
312#endif 274#endif
313 275
276 if (argc < 2)
277 return ERROR;
278
279 for (c = 1; c < argc; c++)
280 if (strcmp ("-to", argv[c]) == 0)
281 strcpy (argv[c], "-t");
314 282
315 while (1) { 283 while (1) {
316#ifdef HAVE_GETOPT_H 284#ifdef HAVE_GETOPT_H
317 c = getopt_long (argc, argv, "+?hVvt:H:s:r:a:", long_opts, &opt_index); 285 c = getopt_long (argc, argv, "hVvt:H:s:r:a:", long_opts, &opt_index);
318#else 286#else
319 c = getopt (argc, argv, "+?hVvt:H:s:r:a:"); 287 c = getopt (argc, argv, "hVvt:H:s:r:a:");
320#endif 288#endif
321 289
322 if (c == -1 || c == EOF) 290 if (c == -1 || c == EOF)
323 break; 291 break;
324 292
325 i++;
326 switch (c) {
327 case 't':
328 case 'H':
329 case 's':
330 case 'r':
331 case 'a':
332 i++;
333 }
334
335 switch (c) { 293 switch (c) {
336 case '?': /* args not parsable */ 294 case '?': /* args not parsable */
337 printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg); 295 printf ("%s: Unknown argument: %s\n\n", PROGNAME, optarg);
338 print_usage (my_basename (argv[0])); 296 print_usage ();
339 exit (STATE_UNKNOWN); 297 exit (STATE_UNKNOWN);
340 case 'h': /* help */ 298 case 'h': /* help */
341 print_help (my_basename (argv[0])); 299 print_help ();
342 exit (STATE_OK); 300 exit (STATE_OK);
343 case 'V': /* version */ 301 case 'V': /* version */
344 print_revision (my_basename (argv[0]), "$Revision$"); 302 print_revision (PROGNAME, REVISION);
345 exit (STATE_OK); 303 exit (STATE_OK);
346 case 'v': /* version */ 304 case 'v': /* version */
347 verbose = TRUE; 305 verbose = TRUE;
@@ -352,7 +310,7 @@ call_getopt (int argc, char **argv)
352 case 'H': /* hostname */ 310 case 'H': /* hostname */
353 if (is_host (optarg) == FALSE) { 311 if (is_host (optarg) == FALSE) {
354 printf ("Invalid host name/address\n\n"); 312 printf ("Invalid host name/address\n\n");
355 print_usage (my_basename (argv[0])); 313 print_usage ();
356 exit (STATE_UNKNOWN); 314 exit (STATE_UNKNOWN);
357 } 315 }
358 if (strlen (optarg) >= ADDRESS_LENGTH) 316 if (strlen (optarg) >= ADDRESS_LENGTH)
@@ -362,7 +320,7 @@ call_getopt (int argc, char **argv)
362 case 's': /* server name */ 320 case 's': /* server name */
363 if (is_host (optarg) == FALSE) { 321 if (is_host (optarg) == FALSE) {
364 printf ("Invalid server name/address\n\n"); 322 printf ("Invalid server name/address\n\n");
365 print_usage (my_basename (argv[0])); 323 print_usage ();
366 exit (STATE_UNKNOWN); 324 exit (STATE_UNKNOWN);
367 } 325 }
368 if (strlen (optarg) >= ADDRESS_LENGTH) 326 if (strlen (optarg) >= ADDRESS_LENGTH)
@@ -372,7 +330,7 @@ call_getopt (int argc, char **argv)
372 case 'r': /* reverse server name */ 330 case 'r': /* reverse server name */
373 if (is_host (optarg) == FALSE) { 331 if (is_host (optarg) == FALSE) {
374 printf ("Invalid host name/address\n\n"); 332 printf ("Invalid host name/address\n\n");
375 print_usage (my_basename (argv[0])); 333 print_usage ();
376 exit (STATE_UNKNOWN); 334 exit (STATE_UNKNOWN);
377 } 335 }
378 if (strlen (optarg) >= ADDRESS_LENGTH) 336 if (strlen (optarg) >= ADDRESS_LENGTH)
@@ -382,7 +340,7 @@ call_getopt (int argc, char **argv)
382 case 'a': /* expected address */ 340 case 'a': /* expected address */
383 if (is_dotted_quad (optarg) == FALSE) { 341 if (is_dotted_quad (optarg) == FALSE) {
384 printf ("Invalid expected address\n\n"); 342 printf ("Invalid expected address\n\n");
385 print_usage (my_basename (argv[0])); 343 print_usage ();
386 exit (STATE_UNKNOWN); 344 exit (STATE_UNKNOWN);
387 } 345 }
388 if (strlen (optarg) >= ADDRESS_LENGTH) 346 if (strlen (optarg) >= ADDRESS_LENGTH)
@@ -392,7 +350,29 @@ call_getopt (int argc, char **argv)
392 break; 350 break;
393 } 351 }
394 } 352 }
395 return i; 353
354 c = optind;
355 if (query_address[0] == 0) {
356 if (is_host (argv[c]) == FALSE) {
357 printf ("Invalid name/address: %s\n\n", argv[c]);
358 return ERROR;
359 }
360 if (strlen (argv[c]) >= ADDRESS_LENGTH)
361 terminate (STATE_UNKNOWN, "Input buffer overflow\n");
362 strcpy (query_address, argv[c++]);
363 }
364
365 if (dns_server[0] == 0) {
366 if (is_host (argv[c]) == FALSE) {
367 printf ("Invalid name/address: %s\n\n", argv[c]);
368 return ERROR;
369 }
370 if (strlen (argv[c]) >= ADDRESS_LENGTH)
371 terminate (STATE_UNKNOWN, "Input buffer overflow\n");
372 strcpy (dns_server, argv[c++]);
373 }
374
375 return validate_arguments ();
396} 376}
397 377
398int 378int
@@ -405,21 +385,21 @@ validate_arguments ()
405} 385}
406 386
407void 387void
408print_usage (char *cmd) 388print_usage (void)
409{ 389{
410 printf ("Usage: %s -H host [-s server] [-a expected-address] [-t timeout]\n" " %s --help\n" 390 printf ("Usage: %s -H host [-s server] [-a expected-address] [-t timeout]\n" " %s --help\n"
411 " %s --version\n", cmd, cmd, cmd); 391 " %s --version\n", PROGNAME, PROGNAME, PROGNAME);
412} 392}
413 393
414void 394void
415print_help (char *cmd) 395print_help (void)
416{ 396{
417 print_revision (cmd, "$Revision$"); 397 print_revision (PROGNAME, REVISION);
418 printf ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n"); 398 printf ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n");
419 print_usage (cmd); 399 print_usage ();
420 printf ("\n");
421 printf 400 printf
422 ("-H, --hostname=HOST\n" 401 ("\nOptions:\n"
402 "-H, --hostname=HOST\n"
423 " The name or address you want to query\n" 403 " The name or address you want to query\n"
424 "-s, --server=HOST\n" 404 "-s, --server=HOST\n"
425 " Optional DNS server you want to use for the lookup\n" 405 " Optional DNS server you want to use for the lookup\n"