check_dns.c 12 KB

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