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