check_dns.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /******************************************************************************
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  13. LIMITATION: nslookup on Solaris 7 can return output over 2 lines, which will not
  14. be picked up by this plugin
  15. ******************************************************************************/
  16. #include "common.h"
  17. #include "popen.h"
  18. #include "utils.h"
  19. #include "netutils.h"
  20. const char *progname = "check_dns";
  21. const char *revision = "$Revision$";
  22. const char *copyright = "2000-2004";
  23. const char *email = "nagiosplug-devel@lists.sourceforge.net";
  24. int process_arguments (int, char **);
  25. int validate_arguments (void);
  26. int error_scan (char *);
  27. void print_help (void);
  28. void print_usage (void);
  29. #define ADDRESS_LENGTH 256
  30. char query_address[ADDRESS_LENGTH] = "";
  31. char dns_server[ADDRESS_LENGTH] = "";
  32. char ptr_server[ADDRESS_LENGTH] = "";
  33. int verbose = FALSE;
  34. char expected_address[ADDRESS_LENGTH] = "";
  35. int match_expected_address = FALSE;
  36. int expect_authority = FALSE;
  37. int
  38. main (int argc, char **argv)
  39. {
  40. char *command_line = NULL;
  41. char input_buffer[MAX_INPUT_BUFFER];
  42. char *output = NULL;
  43. char *address = NULL;
  44. char *temp_buffer = NULL;
  45. int non_authoritative = FALSE;
  46. int result = STATE_UNKNOWN;
  47. double elapsed_time;
  48. long microsec;
  49. struct timeval tv;
  50. int multi_address;
  51. int parse_address = FALSE; /* This flag scans for Address: but only after Name: */
  52. setlocale (LC_ALL, "");
  53. bindtextdomain (PACKAGE, LOCALEDIR);
  54. textdomain (PACKAGE);
  55. /* Set signal handling and alarm */
  56. if (signal (SIGALRM, popen_timeout_alarm_handler) == SIG_ERR) {
  57. printf (_("Cannot catch SIGALRM"));
  58. return STATE_UNKNOWN;
  59. }
  60. if (process_arguments (argc, argv) != OK) {
  61. print_usage ();
  62. return STATE_UNKNOWN;
  63. }
  64. /* get the command to run */
  65. asprintf (&command_line, "%s %s %s", NSLOOKUP_COMMAND, query_address, dns_server);
  66. alarm (timeout_interval);
  67. gettimeofday (&tv, NULL);
  68. if (verbose)
  69. printf ("%s\n", command_line);
  70. /* run the command */
  71. child_process = spopen (command_line);
  72. if (child_process == NULL) {
  73. printf (_("Could not open pipe: %s\n"), command_line);
  74. return STATE_UNKNOWN;
  75. }
  76. child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
  77. if (child_stderr == NULL)
  78. printf (_("Could not open stderr for %s\n"), command_line);
  79. /* scan stdout */
  80. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {
  81. if (verbose)
  82. printf ("%s", input_buffer);
  83. if (strstr (input_buffer, ".in-addr.arpa")) {
  84. if ((temp_buffer = strstr (input_buffer, "name = ")))
  85. address = strdup (temp_buffer + 7);
  86. else {
  87. output = strdup (_("Unknown error (plugin)"));
  88. result = STATE_WARNING;
  89. }
  90. }
  91. /* the server is responding, we just got the host name... */
  92. if (strstr (input_buffer, "Name:"))
  93. parse_address = TRUE;
  94. else if (parse_address == TRUE && (strstr (input_buffer, "Address:") ||
  95. strstr (input_buffer, "Addresses:"))) {
  96. temp_buffer = index (input_buffer, ':');
  97. temp_buffer++;
  98. /* Strip leading spaces */
  99. for (; *temp_buffer != '\0' && *temp_buffer == ' '; temp_buffer++)
  100. /* NOOP */;
  101. strip(temp_buffer);
  102. if (temp_buffer==NULL || strlen(temp_buffer)==0) {
  103. die (STATE_CRITICAL,
  104. _("DNS CRITICAL - '%s' returned empty host name string\n"),
  105. NSLOOKUP_COMMAND);
  106. }
  107. if (address == NULL)
  108. address = strdup (temp_buffer);
  109. else
  110. asprintf(&address, "%s,%s", address, temp_buffer);
  111. }
  112. else if (strstr (input_buffer, "Non-authoritative answer:")) {
  113. non_authoritative = TRUE;
  114. }
  115. result = error_scan (input_buffer);
  116. if (result != STATE_OK) {
  117. output = strdup (1 + index (input_buffer, ':'));
  118. strip (output);
  119. break;
  120. }
  121. }
  122. /* scan stderr */
  123. while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr)) {
  124. if (error_scan (input_buffer) != STATE_OK) {
  125. result = max_state (result, error_scan (input_buffer));
  126. output = strdup (1 + index (input_buffer, ':'));
  127. strip (output);
  128. }
  129. }
  130. /* close stderr */
  131. (void) fclose (child_stderr);
  132. /* close stdout */
  133. if (spclose (child_process)) {
  134. result = max_state (result, STATE_WARNING);
  135. if (!strcmp (output, ""))
  136. output = strdup (_("nslookup returned error status"));
  137. }
  138. /* If we got here, we should have an address string,
  139. and we can segfault if we do not */
  140. if (address==NULL || strlen(address)==0)
  141. die (STATE_CRITICAL,
  142. _("DNS CRITICAL - '%s' output parsing exited with no address\n"),
  143. NSLOOKUP_COMMAND);
  144. /* compare to expected address */
  145. if (result == STATE_OK && match_expected_address && strcmp(address, expected_address)) {
  146. result = STATE_CRITICAL;
  147. asprintf(&output, _("expected %s but got %s"), expected_address, address);
  148. }
  149. /* check if authoritative */
  150. if (result == STATE_OK && expect_authority && non_authoritative) {
  151. result = STATE_CRITICAL;
  152. asprintf(&output, _("server %s is not authoritative for %s"), dns_server, query_address);
  153. }
  154. microsec = deltime (tv);
  155. elapsed_time = (double)microsec / 1.0e6;
  156. if (result == STATE_OK) {
  157. if (strchr (address, ',') == NULL)
  158. multi_address = FALSE;
  159. else
  160. multi_address = TRUE;
  161. printf ("%s %s: ", _("DNS"), _("OK"));
  162. printf (ngettext("%.3f second response time, ", "%.3f seconds response time, ", elapsed_time), elapsed_time);
  163. printf (_("%s returns %s"), query_address, address);
  164. printf ("|%s\n", perfdata ("time", microsec, "us", FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
  165. }
  166. else if (result == STATE_WARNING)
  167. printf (_("DNS WARNING - %s\n"),
  168. !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
  169. else if (result == STATE_CRITICAL)
  170. printf (_("DNS CRITICAL - %s\n"),
  171. !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
  172. else
  173. printf (_("DNS problem - %s\n"),
  174. !strcmp (output, "") ? _(" Probably a non-existent host/domain") : output);
  175. return result;
  176. }
  177. int
  178. error_scan (char *input_buffer)
  179. {
  180. /* the DNS lookup timed out */
  181. if (strstr (input_buffer, "Note: nslookup is deprecated and may be removed from future releases.") ||
  182. strstr (input_buffer, "Consider using the `dig' or `host' programs instead. Run nslookup with") ||
  183. strstr (input_buffer, "the `-sil[ent]' option to prevent this message from appearing."))
  184. return STATE_OK;
  185. /* DNS server is not running... */
  186. else if (strstr (input_buffer, "No response from server"))
  187. die (STATE_CRITICAL, _("No response from name server %s\n"), dns_server);
  188. /* Host name is valid, but server doesn't have records... */
  189. else if (strstr (input_buffer, "No records"))
  190. die (STATE_CRITICAL, _("Name server %s has no records\n"), dns_server);
  191. /* Connection was refused */
  192. else if (strstr (input_buffer, "Connection refused") ||
  193. strstr (input_buffer, "Refused") ||
  194. (strstr (input_buffer, "** server can't find") &&
  195. strstr (input_buffer, ": REFUSED")))
  196. die (STATE_CRITICAL, _("Connection to name server %s was refused\n"), dns_server);
  197. /* Host or domain name does not exist */
  198. else if (strstr (input_buffer, "Non-existent") ||
  199. strstr (input_buffer, "** server can't find") ||
  200. strstr (input_buffer,"NXDOMAIN"))
  201. die (STATE_CRITICAL, _("Domain %s was not found by the server\n"), query_address);
  202. /* Network is unreachable */
  203. else if (strstr (input_buffer, "Network is unreachable"))
  204. die (STATE_CRITICAL, _("Network is unreachable\n"));
  205. /* Internal server failure */
  206. else if (strstr (input_buffer, "Server failure"))
  207. die (STATE_CRITICAL, _("Server failure for %s\n"), dns_server);
  208. /* Request error or the DNS lookup timed out */
  209. else if (strstr (input_buffer, "Format error") ||
  210. strstr (input_buffer, "Timed out"))
  211. return STATE_WARNING;
  212. return STATE_OK;
  213. }
  214. /* process command-line arguments */
  215. int
  216. process_arguments (int argc, char **argv)
  217. {
  218. int c;
  219. int opt_index = 0;
  220. static struct option long_opts[] = {
  221. {"help", no_argument, 0, 'h'},
  222. {"version", no_argument, 0, 'V'},
  223. {"verbose", no_argument, 0, 'v'},
  224. {"timeout", required_argument, 0, 't'},
  225. {"hostname", required_argument, 0, 'H'},
  226. {"server", required_argument, 0, 's'},
  227. {"reverse-server", required_argument, 0, 'r'},
  228. {"expected-address", required_argument, 0, 'a'},
  229. {"expect-authority", no_argument, 0, 'A'},
  230. {0, 0, 0, 0}
  231. };
  232. if (argc < 2)
  233. return ERROR;
  234. for (c = 1; c < argc; c++)
  235. if (strcmp ("-to", argv[c]) == 0)
  236. strcpy (argv[c], "-t");
  237. while (1) {
  238. c = getopt_long (argc, argv, "hVvAt:H:s:r:a:", long_opts, &opt_index);
  239. if (c == -1 || c == EOF)
  240. break;
  241. switch (c) {
  242. case '?': /* args not parsable */
  243. printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
  244. print_usage ();
  245. exit (STATE_UNKNOWN);
  246. case 'h': /* help */
  247. print_help ();
  248. exit (STATE_OK);
  249. case 'V': /* version */
  250. print_revision (progname, revision);
  251. exit (STATE_OK);
  252. case 'v': /* version */
  253. verbose = TRUE;
  254. break;
  255. case 't': /* timeout period */
  256. timeout_interval = atoi (optarg);
  257. break;
  258. case 'H': /* hostname */
  259. if (strlen (optarg) >= ADDRESS_LENGTH)
  260. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  261. strcpy (query_address, optarg);
  262. break;
  263. case 's': /* server name */
  264. /* TODO: this is_host check is probably unnecessary. */
  265. /* Better to confirm nslookup response matches */
  266. if (is_host (optarg) == FALSE) {
  267. printf (_("Invalid server name/address\n\n"));
  268. print_usage ();
  269. exit (STATE_UNKNOWN);
  270. }
  271. if (strlen (optarg) >= ADDRESS_LENGTH)
  272. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  273. strcpy (dns_server, optarg);
  274. break;
  275. case 'r': /* reverse server name */
  276. /* TODO: Is this is_host necessary? */
  277. if (is_host (optarg) == FALSE) {
  278. printf (_("Invalid host name/address\n\n"));
  279. print_usage ();
  280. exit (STATE_UNKNOWN);
  281. }
  282. if (strlen (optarg) >= ADDRESS_LENGTH)
  283. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  284. strcpy (ptr_server, optarg);
  285. break;
  286. case 'a': /* expected address */
  287. if (strlen (optarg) >= ADDRESS_LENGTH)
  288. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  289. strcpy (expected_address, optarg);
  290. match_expected_address = TRUE;
  291. break;
  292. case 'A': /* expect authority */
  293. expect_authority = TRUE;
  294. break;
  295. }
  296. }
  297. c = optind;
  298. if (strlen(query_address)==0 && c<argc) {
  299. if (strlen(argv[c])>=ADDRESS_LENGTH)
  300. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  301. strcpy (query_address, argv[c++]);
  302. }
  303. if (strlen(dns_server)==0 && c<argc) {
  304. /* TODO: See -s option */
  305. if (is_host(argv[c]) == FALSE) {
  306. printf (_("Invalid name/address: %s\n\n"), argv[c]);
  307. return ERROR;
  308. }
  309. if (strlen(argv[c]) >= ADDRESS_LENGTH)
  310. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  311. strcpy (dns_server, argv[c++]);
  312. }
  313. return validate_arguments ();
  314. }
  315. int
  316. validate_arguments ()
  317. {
  318. if (query_address[0] == 0)
  319. return ERROR;
  320. else
  321. return OK;
  322. }
  323. void
  324. print_help (void)
  325. {
  326. print_revision (progname, revision);
  327. printf (_("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"));
  328. printf (_(COPYRIGHT), copyright, email);
  329. print_usage ();
  330. printf (_(UT_HELP_VRSN));
  331. printf (_("\
  332. -H, --hostname=HOST\n\
  333. The name or address you want to query\n\
  334. -s, --server=HOST\n\
  335. Optional DNS server you want to use for the lookup\n\
  336. -a, --expected-address=IP-ADDRESS\n\
  337. Optional IP address you expect the DNS server to return\n\
  338. -A, --expect-authority\n\
  339. Optionally expect the DNS server to be authoritative for the lookup\n"));
  340. printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
  341. printf (_("\n\
  342. This plugin uses the nslookup program to obtain the IP address\n\
  343. for the given host/domain query. A optional DNS server to use may\n\
  344. be specified. If no DNS server is specified, the default server(s)\n\
  345. specified in /etc/resolv.conf will be used.\n"));
  346. printf (_(UT_SUPPORT));
  347. }
  348. void
  349. print_usage (void)
  350. {
  351. printf (_("\
  352. Usage: %s -H host [-s server] [-a expected-address] [-A] [-t timeout]\n\
  353. %s --help\n\
  354. %s --version\n"), progname, progname, progname);
  355. }