X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=plugins%2Fcheck_dns.c;h=19649f50045bbfdde8c1a8f27239bc4b2dea11b6;hb=cbd684fa0ba4447dcdc3b6d5f9699aca7b5c9047;hp=eaff43728ae24f81dcf23235bde4019ca6e1d446;hpb=44a321cb8a42d6c0ea2d96a1086a17f2134c89cc;p=nagiosplug.git diff --git a/plugins/check_dns.c b/plugins/check_dns.c index eaff437..19649f5 100644 --- a/plugins/check_dns.c +++ b/plugins/check_dns.c @@ -10,8 +10,9 @@ * * Notes: * - Safe popen added by Karl DeBisschop 9-11-99 + * - expected-address parameter added by Alex Chaffee - 7 Oct 2002 * - * Command line: CHECK_DNS [dns_server] + * Command line: (see print_usage) * * Description: * @@ -47,11 +48,14 @@ #include "popen.h" #include "utils.h" +const char *progname = "check_dns"; +#define REVISION "$Revision$" +#define COPYRIGHT "2000-2002" + int process_arguments (int, char **); -int call_getopt (int, char **); int validate_arguments (void); -void print_usage (char *); -void print_help (char *); +void print_usage (void); +void print_help (void); int error_scan (char *); #define ADDRESS_LENGTH 256 @@ -59,6 +63,8 @@ char query_address[ADDRESS_LENGTH] = ""; char dns_server[ADDRESS_LENGTH] = ""; char ptr_server[ADDRESS_LENGTH] = ""; int verbose = FALSE; +char expected_address[ADDRESS_LENGTH] = ""; +int match_expected_address = FALSE; int main (int argc, char **argv) @@ -77,13 +83,12 @@ main (int argc, char **argv) } if (process_arguments (argc, argv) != OK) { - print_usage (my_basename (argv[0])); + print_usage (); return STATE_UNKNOWN; } /* get the command to run */ - command_line = ssprintf (command_line, "%s %s %s", NSLOOKUP_COMMAND, - query_address, dns_server); + asprintf (&command_line, "%s %s %s", NSLOOKUP_COMMAND, query_address, dns_server); alarm (timeout_interval); time (&start_time); @@ -127,7 +132,11 @@ main (int argc, char **argv) printf ("%s\n", input_buffer); if ((temp_buffer = index (input_buffer, ':'))) { - address = strscpy (address, temp_buffer + 2); + temp_buffer++; + /* Strip leading spaces */ + for (; *temp_buffer != '\0' && *temp_buffer == ' '; temp_buffer++) + /* NOOP */; + address = strscpy (address, temp_buffer); strip (address); result = STATE_OK; } @@ -150,7 +159,7 @@ main (int argc, char **argv) /* scan stderr */ while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) { if (error_scan (input_buffer) != STATE_OK) { - result = max (result, error_scan (input_buffer)); + result = max_state (result, error_scan (input_buffer)); output = strscpy (output, 1 + index (input_buffer, ':')); } } @@ -160,11 +169,17 @@ main (int argc, char **argv) /* close stdout */ if (spclose (child_process)) { - result = max (result, STATE_WARNING); + result = max_state (result, STATE_WARNING); if (!strcmp (output, "")) output = strscpy (output, "nslookup returned error status"); } + /* compare to expected address */ + if (result == STATE_OK && match_expected_address && strcmp(address, expected_address)) { + result = STATE_CRITICAL; + asprintf(&output, "expected %s but got %s", expected_address, address); + } + (void) time (&end_time); if (result == STATE_OK) @@ -247,46 +262,6 @@ process_arguments (int argc, char **argv) { int c; - if (argc < 2) - return ERROR; - - for (c = 1; c < argc; c++) - if (strcmp ("-to", argv[c]) == 0) - strcpy (argv[c], "-t"); - - c = 0; - while (c += (call_getopt (argc - c, &argv[c]))) { - if (argc <= c) - break; - if (query_address[0] == 0) { - if (is_host (argv[c]) == FALSE) { - printf ("Invalid name/address: %s\n\n", argv[c]); - return ERROR; - } - if (strlen (argv[c]) >= ADDRESS_LENGTH) - terminate (STATE_UNKNOWN, "Input buffer overflow\n"); - strcpy (query_address, argv[c]); - } - else if (dns_server[0] == 0) { - if (is_host (argv[c]) == FALSE) { - printf ("Invalid name/address: %s\n\n", argv[c]); - return ERROR; - } - if (strlen (argv[c]) >= ADDRESS_LENGTH) - terminate (STATE_UNKNOWN, "Input buffer overflow\n"); - strcpy (dns_server, argv[c]); - } - } - - return validate_arguments (); - -} - -int -call_getopt (int argc, char **argv) -{ - int c, i = 1; - #ifdef HAVE_GETOPT_H int opt_index = 0; static struct option long_opts[] = { @@ -297,40 +272,38 @@ call_getopt (int argc, char **argv) {"hostname", required_argument, 0, 'H'}, {"server", required_argument, 0, 's'}, {"reverse-server", required_argument, 0, 'r'}, + {"expected-address", required_argument, 0, 'a'}, {0, 0, 0, 0} }; #endif + if (argc < 2) + return ERROR; + + for (c = 1; c < argc; c++) + if (strcmp ("-to", argv[c]) == 0) + strcpy (argv[c], "-t"); while (1) { #ifdef HAVE_GETOPT_H - c = getopt_long (argc, argv, "+?hVvt:H:s:r:", long_opts, &opt_index); + c = getopt_long (argc, argv, "hVvt:H:s:r:a:", long_opts, &opt_index); #else - c = getopt (argc, argv, "+?hVvt:H:s:r:"); + c = getopt (argc, argv, "hVvt:H:s:r:a:"); #endif if (c == -1 || c == EOF) break; - i++; - switch (c) { - case 't': - case 'H': - case 's': - case 'r': - i++; - } - switch (c) { case '?': /* args not parsable */ - printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg); - print_usage (my_basename (argv[0])); + printf ("%s: Unknown argument: %s\n\n", progname, optarg); + print_usage (); exit (STATE_UNKNOWN); case 'h': /* help */ - print_help (my_basename (argv[0])); + print_help (); exit (STATE_OK); case 'V': /* version */ - print_revision (my_basename (argv[0]), "$Revision$"); + print_revision (progname, REVISION); exit (STATE_OK); case 'v': /* version */ verbose = TRUE; @@ -341,7 +314,7 @@ call_getopt (int argc, char **argv) case 'H': /* hostname */ if (is_host (optarg) == FALSE) { printf ("Invalid host name/address\n\n"); - print_usage (my_basename (argv[0])); + print_usage (); exit (STATE_UNKNOWN); } if (strlen (optarg) >= ADDRESS_LENGTH) @@ -351,7 +324,7 @@ call_getopt (int argc, char **argv) case 's': /* server name */ if (is_host (optarg) == FALSE) { printf ("Invalid server name/address\n\n"); - print_usage (my_basename (argv[0])); + print_usage (); exit (STATE_UNKNOWN); } if (strlen (optarg) >= ADDRESS_LENGTH) @@ -361,16 +334,49 @@ call_getopt (int argc, char **argv) case 'r': /* reverse server name */ if (is_host (optarg) == FALSE) { printf ("Invalid host name/address\n\n"); - print_usage (my_basename (argv[0])); + print_usage (); exit (STATE_UNKNOWN); } if (strlen (optarg) >= ADDRESS_LENGTH) terminate (STATE_UNKNOWN, "Input buffer overflow\n"); strcpy (ptr_server, optarg); break; + case 'a': /* expected address */ + if (is_dotted_quad (optarg) == FALSE) { + printf ("Invalid expected address\n\n"); + print_usage (); + exit (STATE_UNKNOWN); + } + if (strlen (optarg) >= ADDRESS_LENGTH) + terminate (STATE_UNKNOWN, "Input buffer overflow\n"); + strcpy (expected_address, optarg); + match_expected_address = TRUE; + break; + } + } + + c = optind; + if (strlen(query_address)==0 && c=ADDRESS_LENGTH) + terminate (STATE_UNKNOWN, "Input buffer overflow\n"); + strcpy (query_address, argv[c++]); } - return i; + + if (strlen(dns_server)==0 && c= ADDRESS_LENGTH) + terminate (STATE_UNKNOWN, "Input buffer overflow\n"); + strcpy (dns_server, argv[c++]); + } + + return validate_arguments (); } int @@ -383,24 +389,26 @@ validate_arguments () } void -print_usage (char *cmd) +print_usage (void) { - printf ("Usage: %s -H host [-s server] [-t timeout]\n" " %s --help\n" - " %s --version\n", cmd, cmd, cmd); + printf ("Usage: %s -H host [-s server] [-a expected-address] [-t timeout]\n" " %s --help\n" + " %s --version\n", progname, progname, progname); } void -print_help (char *cmd) +print_help (void) { - print_revision (cmd, "$Revision$"); + print_revision (progname, REVISION); printf ("Copyright (c) 1999 Ethan Galstad (nagios@nagios.org)\n\n"); - print_usage (cmd); - printf ("\n"); + print_usage (); printf - ("-H, --hostname=HOST\n" + ("\nOptions:\n" + "-H, --hostname=HOST\n" " The name or address you want to query\n" "-s, --server=HOST\n" " Optional DNS server you want to use for the lookup\n" + "-a, --expected-address=IP-ADDRESS\n" + " Optional IP address you expect the DNS server to return\n" "-t, --timeout=INTEGER\n" " Seconds before connection times out (default: %d)\n" "-h, --help\n"