check_dns.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /*****************************************************************************
  2. *
  3. * Nagios check_dns plugin
  4. *
  5. * License: GPL
  6. * Copyright (c) 2000-2014 Nagios Plugins Development Team
  7. *
  8. * Description:
  9. *
  10. * This file contains the check_dns plugin
  11. *
  12. * LIMITATION: nslookup on Solaris 7 can return output over 2 lines, which
  13. * will not be picked up by this plugin
  14. *
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU General Public License as published by
  18. * the Free Software Foundation, either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. *
  29. *
  30. *****************************************************************************/
  31. const char *progname = "check_dns";
  32. const char *copyright = "2000-2014";
  33. const char *email = "devel@nagios-plugins.org";
  34. #include "common.h"
  35. #include "utils.h"
  36. #include "utils_base.h"
  37. #include "netutils.h"
  38. #include "runcmd.h"
  39. int process_arguments (int, char **);
  40. int validate_arguments (void);
  41. int error_scan (char *);
  42. void print_help (void);
  43. void print_usage (void);
  44. #define ADDRESS_LENGTH 384
  45. char query_address[ADDRESS_LENGTH] = "";
  46. char dns_server[ADDRESS_LENGTH] = "";
  47. char ptr_server[ADDRESS_LENGTH] = "";
  48. char query_type[16] = "";
  49. int query_set = FALSE;
  50. int verbose = FALSE;
  51. char **expected_address = NULL;
  52. int expected_address_cnt = 0;
  53. int expect_authority = FALSE;
  54. int accept_cname = FALSE;
  55. thresholds *time_thresholds = NULL;
  56. static int
  57. qstrcmp(const void *p1, const void *p2)
  58. {
  59. /* The actual arguments to this function are "pointers to
  60. pointers to char", but strcmp() arguments are "pointers
  61. to char", hence the following cast plus dereference */
  62. return strcmp(* (char * const *) p1, * (char * const *) p2);
  63. }
  64. char *
  65. check_new_address(char *temp_buffer)
  66. {
  67. temp_buffer++;
  68. /* Strip leading spaces */
  69. for (; *temp_buffer != '\0' && *temp_buffer == ' '; temp_buffer++)
  70. /* NOOP */;
  71. strip(temp_buffer);
  72. if (temp_buffer==NULL || strlen(temp_buffer)==0) {
  73. die (STATE_CRITICAL,
  74. _("DNS CRITICAL - '%s' returned empty host name string\n"),
  75. NSLOOKUP_COMMAND);
  76. }
  77. return temp_buffer;
  78. }
  79. int
  80. main (int argc, char **argv)
  81. {
  82. char *command_line = NULL;
  83. char input_buffer[MAX_INPUT_BUFFER];
  84. char *address = NULL; /* comma seperated str with addrs/ptrs (sorted) */
  85. char **addresses = NULL;
  86. int n_addresses = 0;
  87. char *msg = NULL;
  88. char query_found[16] = "";
  89. int query_size = 16;
  90. char *temp_buffer = NULL;
  91. int non_authoritative = TRUE;
  92. int result = STATE_UNKNOWN;
  93. double elapsed_time;
  94. long microsec;
  95. struct timeval tv;
  96. int multi_address;
  97. int parse_address = FALSE; /* This flag scans for Address: but only after Name: */
  98. output chld_out, chld_err;
  99. size_t i;
  100. setlocale (LC_ALL, "");
  101. bindtextdomain (PACKAGE, LOCALEDIR);
  102. textdomain (PACKAGE);
  103. /* Set signal handling and alarm */
  104. if (signal (SIGALRM, runcmd_timeout_alarm_handler) == SIG_ERR) {
  105. usage_va(_("Cannot catch SIGALRM"));
  106. }
  107. /* Parse extra opts if any */
  108. argv=np_extra_opts (&argc, argv, progname);
  109. if (process_arguments (argc, argv) == ERROR) {
  110. usage_va(_("Could not parse arguments"));
  111. }
  112. /* get the command to run */
  113. xasprintf (&command_line, "%s %s %s %s", NSLOOKUP_COMMAND, query_type, query_address, dns_server);
  114. alarm (timeout_interval);
  115. gettimeofday (&tv, NULL);
  116. if (verbose)
  117. printf ("%s\n", command_line);
  118. /* run the command */
  119. if((np_runcmd(command_line, &chld_out, &chld_err, 0)) != 0) {
  120. msg = (char *)_("nslookup returned an error status");
  121. result = STATE_WARNING;
  122. }
  123. /* scan stdout */
  124. for(i = 0; i < chld_out.lines; i++) {
  125. if (addresses == NULL)
  126. addresses = malloc(sizeof(*addresses)*10);
  127. else if (!(n_addresses % 10))
  128. addresses = realloc(addresses,sizeof(*addresses) * (n_addresses + 10));
  129. if (verbose)
  130. puts(chld_out.line[i]);
  131. /* bug ID: 2946553 - Older versions of bind will use all available dns
  132. + servers, we have to match the one specified */
  133. if (strlen(dns_server) > 0 && strstr(chld_out.line[i], "Server:")) {
  134. temp_buffer = strchr(chld_out.line[i], ':');
  135. temp_buffer++;
  136. /* Strip leading tabs */
  137. for (; *temp_buffer != '\0' && *temp_buffer == '\t'; temp_buffer++)
  138. /* NOOP */;
  139. strip(temp_buffer);
  140. if (temp_buffer==NULL || strlen(temp_buffer)==0)
  141. die (STATE_CRITICAL, "%s%s%s\n", _("DNS CRITICAL - '"), NSLOOKUP_COMMAND, _("' returned empty server string"));
  142. if (strcmp(temp_buffer, dns_server) != 0)
  143. die (STATE_CRITICAL, "%s %s\n", _("No response from DNS"), dns_server);
  144. }
  145. if (strstr (chld_out.line[i], "Authoritative answers can be found from:")) {
  146. non_authoritative = FALSE;
  147. break;
  148. }
  149. /* the server is responding, we just got the host name...*/
  150. if (strstr (chld_out.line[i], "Name:"))
  151. parse_address = TRUE;
  152. /* begin handling types of records */
  153. if (strstr (chld_out.line[i], "AAAA address") && (strncmp(query_type, "-querytype=AAAA", query_size) == 0 || strncmp(query_type, "-querytype=ANY", query_size) == 0)) {
  154. if (verbose) printf("Found AAAA record\n");
  155. temp_buffer = rindex (chld_out.line[i], ' ');
  156. addresses[n_addresses++] = check_new_address(temp_buffer);
  157. strncpy(query_found, "-querytype=AAAA", sizeof(query_found));
  158. }
  159. else if (strstr (chld_out.line[i], "exchanger =") && (strncmp(query_type, "-querytype=MX", query_size) == 0 || strncmp(query_type, "-querytype=ANY", query_size) == 0)) {
  160. if (verbose) printf("Found MX record\n");
  161. temp_buffer = index (chld_out.line[i], '=');
  162. addresses[n_addresses++] = check_new_address(temp_buffer);
  163. strncpy(query_found, "-querytype=MX", sizeof(query_found));
  164. }
  165. else if (strstr (chld_out.line[i], "service =") && (strncmp(query_type, "-querytype=SRV", query_size) == 0 || strncmp(query_type, "-querytype=ANY", query_size) == 0)) {
  166. if (verbose) printf("Found SRV record\n");
  167. temp_buffer = index (chld_out.line[i], '=');
  168. addresses[n_addresses++] = check_new_address(temp_buffer);
  169. strncpy(query_found, "-querytype=SRV", sizeof(query_found));
  170. }
  171. else if (accept_cname && strstr (chld_out.line[i], "canonical name =") && (strncmp(query_type, "-querytype=CNAME", query_size) == 0 || strncmp(query_type, "-querytype=ANY", query_size) == 0)) {
  172. if (verbose) printf("Found CNAME record\n");
  173. temp_buffer = index (chld_out.line[i], '=');
  174. addresses[n_addresses++] = check_new_address(temp_buffer);
  175. strncpy(query_found, "-querytype=CNAME", sizeof(query_found));
  176. }
  177. /* does not need strncmp as we want A at all times unless another record match */
  178. else if (parse_address == TRUE && (strstr (chld_out.line[i], "Address:") || strstr (chld_out.line[i], "Addresses:"))) {
  179. if (verbose) printf("Found A record\n");
  180. temp_buffer = index (chld_out.line[i], ':');
  181. addresses[n_addresses++] = check_new_address(temp_buffer);
  182. strncpy(query_found, "-querytype=A", sizeof(query_found));
  183. }
  184. else if (strstr (chld_out.line[i], "nameserver =") && (strncmp(query_type, "-querytype=NS", query_size) == 0 || strncmp(query_type, "-querytype=ANY", query_size) == 0)) {
  185. if (verbose) printf("Found NS record\n");
  186. temp_buffer = index (chld_out.line[i], '=');
  187. addresses[n_addresses++] = check_new_address(temp_buffer);
  188. strncpy(query_found, "-querytype=NS", sizeof(query_found));
  189. }
  190. else if (strstr (chld_out.line[i], "dname =") && (strncmp(query_type, "-querytype=DNAME", query_size) == 0 || strncmp(query_type, "-querytype=ANY", query_size) == 0)) {
  191. if (verbose) printf("Found DNAME record\n");
  192. temp_buffer = index (chld_out.line[i], '=');
  193. addresses[n_addresses++] = check_new_address(temp_buffer);
  194. strncpy(query_found, "-querytype=DNAME", sizeof(query_found));
  195. }
  196. /* must be after other records with "name" as an identifier, as ptr does not spefify */
  197. else if (strstr (chld_out.line[i], "name =") && (strncmp(query_type, "-querytype=PTR", query_size) == 0 || strncmp(query_type, "-querytype=ANY", query_size) == 0)) {
  198. if (verbose) printf("Found PTR record\n");
  199. temp_buffer = index (chld_out.line[i], '=');
  200. addresses[n_addresses++] = check_new_address(temp_buffer);
  201. strncpy(query_found, "-querytype=PTR", sizeof(query_found));
  202. }
  203. else if (strstr (chld_out.line[i], "protocol =") && (strncmp(query_type, "-querytype=WKS", query_size) == 0 || strncmp(query_type, "-querytype=ANY", query_size) == 0)) {
  204. if (verbose) printf("Found WKS record\n");
  205. temp_buffer = index (chld_out.line[i], '=');
  206. addresses[n_addresses++] = check_new_address(temp_buffer);
  207. strncpy(query_found, "-querytype=WKS", sizeof(query_found));
  208. }
  209. /* TODO: needs to be changed to handle txt output and max size of txt recrods */
  210. else if (strstr (chld_out.line[i], "text =") && (strncmp(query_type, "-querytype=TXT", query_size) == 0 || strncmp(query_type, "-querytype=ANY", query_size) == 0)) {
  211. if (verbose) printf("Found TXT record\n");
  212. temp_buffer = index (chld_out.line[i], '=');
  213. addresses[n_addresses++] = check_new_address(temp_buffer);
  214. strncpy(query_found, "-querytype=TXT", sizeof(query_found));
  215. }
  216. /* only matching for origin records, if requested other fields could be included at a later date */
  217. else if (strstr (chld_out.line[i], "origin =") && (strncmp(query_type, "-querytype=SOA", query_size) == 0 || strncmp(query_type, "-querytype=ANY", query_size) == 0)) {
  218. if (verbose) printf("Found SOA record\n");
  219. temp_buffer = index(chld_out.line[i], '=');
  220. addresses[n_addresses++] = check_new_address(temp_buffer);
  221. strncpy(query_found, "-querytype=SOA", sizeof(query_found));
  222. }
  223. /* needed for non-query ptr\reverse lookup checks */
  224. else if (strstr(chld_out.line[i], ".in-addr.arpa") && !query_set) {
  225. if ((temp_buffer = strstr(chld_out.line[i], "name = ")))
  226. addresses[n_addresses++] = strdup(temp_buffer + 7);
  227. else {
  228. msg = (char *)_("Warning plugin error");
  229. result = STATE_WARNING;
  230. }
  231. }
  232. if (strstr (chld_out.line[i], _("Non-authoritative answer:"))) {
  233. non_authoritative = TRUE;
  234. }
  235. result = error_scan (chld_out.line[i]);
  236. if (result != STATE_OK) {
  237. msg = strchr (chld_out.line[i], ':');
  238. if(msg) msg++;
  239. break;
  240. }
  241. }
  242. /* scan stderr */
  243. for(i = 0; i < chld_err.lines; i++) {
  244. if (verbose)
  245. puts(chld_err.line[i]);
  246. if (error_scan (chld_err.line[i]) != STATE_OK) {
  247. result = max_state (result, error_scan (chld_err.line[i]));
  248. msg = strchr(input_buffer, ':');
  249. if(msg) msg++;
  250. }
  251. }
  252. if (addresses) {
  253. int i,slen;
  254. char *adrp;
  255. qsort(addresses, n_addresses, sizeof(*addresses), qstrcmp);
  256. for(i=0, slen=1; i < n_addresses; i++) {
  257. slen += strlen(addresses[i])+1;
  258. }
  259. adrp = address = malloc(slen);
  260. for(i=0; i < n_addresses; i++) {
  261. if (i) *adrp++ = ',';
  262. strcpy(adrp, addresses[i]);
  263. adrp += strlen(addresses[i]);
  264. }
  265. *adrp = 0;
  266. } else
  267. die (STATE_CRITICAL,
  268. _("DNS CRITICAL - '%s' msg parsing exited with no address\n"),
  269. NSLOOKUP_COMMAND);
  270. /* compare to expected address */
  271. if (result == STATE_OK && expected_address_cnt > 0) {
  272. result = STATE_CRITICAL;
  273. temp_buffer = "";
  274. for (i=0; i<expected_address_cnt; i++) {
  275. /* check if we get a match and prepare an error string */
  276. if (strcmp(address, expected_address[i]) == 0) result = STATE_OK;
  277. xasprintf(&temp_buffer, "%s%s; ", temp_buffer, expected_address[i]);
  278. }
  279. if (result == STATE_CRITICAL) {
  280. /* Strip off last semicolon... */
  281. temp_buffer[strlen(temp_buffer)-2] = '\0';
  282. xasprintf(&msg, _("expected '%s' but got '%s'"), temp_buffer, address);
  283. }
  284. }
  285. /* check if authoritative */
  286. if (result == STATE_OK && expect_authority && !non_authoritative) {
  287. result = STATE_CRITICAL;
  288. if (strncmp(dns_server, "", 1))
  289. xasprintf(&msg, _("server %s is not authoritative for %s"), dns_server, query_address);
  290. else
  291. xasprintf(&msg, _("there is no authoritative server for %s"), query_address);
  292. }
  293. /* compare query type to query found, if query type is ANY we can skip as any record is accepted*/
  294. if (result == STATE_OK && strncmp(query_type, "", 1) && (strncmp(query_type, "-querytype=ANY", 15) != 0)) {
  295. if (strncmp(query_type, query_found, 16) != 0) {
  296. if (verbose)
  297. printf( "Failed query for %s only found %s, or nothing\n", query_type, query_found);
  298. result = STATE_CRITICAL;
  299. xasprintf(&msg, _("query type of %s was not found for %s"), query_type, query_address);
  300. }
  301. }
  302. microsec = deltime (tv);
  303. elapsed_time = (double)microsec / 1.0e6;
  304. if (result == STATE_OK) {
  305. if (strchr (address, ',') == NULL)
  306. multi_address = FALSE;
  307. else
  308. multi_address = TRUE;
  309. result = get_status(elapsed_time, time_thresholds);
  310. if (result == STATE_OK) {
  311. printf ("DNS %s: ", _("OK"));
  312. } else if (result == STATE_WARNING) {
  313. printf ("DNS %s: ", _("WARNING"));
  314. } else if (result == STATE_CRITICAL) {
  315. printf ("DNS %s: ", _("CRITICAL"));
  316. }
  317. printf (ngettext("%.3f second response time", "%.3f seconds response time", elapsed_time), elapsed_time);
  318. printf (_(". %s returns %s"), query_address, address);
  319. if ((time_thresholds->warning != NULL) && (time_thresholds->critical != NULL)) {
  320. printf ("|%s\n", fperfdata ("time", elapsed_time, "s",
  321. TRUE, time_thresholds->warning->end,
  322. TRUE, time_thresholds->critical->end,
  323. TRUE, 0, FALSE, 0));
  324. } else if ((time_thresholds->warning == NULL) && (time_thresholds->critical != NULL)) {
  325. printf ("|%s\n", fperfdata ("time", elapsed_time, "s",
  326. FALSE, 0,
  327. TRUE, time_thresholds->critical->end,
  328. TRUE, 0, FALSE, 0));
  329. } else if ((time_thresholds->warning != NULL) && (time_thresholds->critical == NULL)) {
  330. printf ("|%s\n", fperfdata ("time", elapsed_time, "s",
  331. TRUE, time_thresholds->warning->end,
  332. FALSE, 0,
  333. TRUE, 0, FALSE, 0));
  334. } else
  335. printf ("|%s\n", fperfdata ("time", elapsed_time, "s", FALSE, 0, FALSE, 0, TRUE, 0, FALSE, 0));
  336. }
  337. else if (result == STATE_WARNING)
  338. printf (_("DNS WARNING - %s\n"),
  339. !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
  340. else if (result == STATE_CRITICAL)
  341. printf (_("DNS CRITICAL - %s\n"),
  342. !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
  343. else
  344. printf (_("DNS UNKNOWN - %s\n"),
  345. !strcmp (msg, "") ? _(" Probably a non-existent host/domain") : msg);
  346. return result;
  347. }
  348. int
  349. error_scan (char *input_buffer)
  350. {
  351. /* the DNS lookup timed out */
  352. if (strstr (input_buffer, _("Note: nslookup is deprecated and may be removed from future releases.")) ||
  353. strstr (input_buffer, _("Consider using the `dig' or `host' programs instead. Run nslookup with")) ||
  354. strstr (input_buffer, _("the `-sil[ent]' option to prevent this message from appearing.")))
  355. return STATE_OK;
  356. /* DNS server is not running... */
  357. else if (strstr (input_buffer, "No response from server"))
  358. die (STATE_CRITICAL, _("No response from DNS %s\n"), dns_server);
  359. /* Host name is valid, but server doesn't have records... */
  360. else if (strstr (input_buffer, "No records") || strstr (input_buffer, "No answer"))
  361. die (STATE_CRITICAL, _("DNS %s has no records\n"), dns_server);
  362. /* Connection was refused */
  363. else if (strstr (input_buffer, "Connection refused") ||
  364. strstr (input_buffer, "Couldn't find server") ||
  365. strstr (input_buffer, "Refused") ||
  366. (strstr (input_buffer, "** server can't find") &&
  367. strstr (input_buffer, ": REFUSED")))
  368. die (STATE_CRITICAL, _("Connection to DNS %s was refused\n"), dns_server);
  369. /* Query refused (usually by an ACL in the namserver) */
  370. else if (strstr (input_buffer, "Query refused"))
  371. die (STATE_CRITICAL, _("Query was refused by DNS server at %s\n"), dns_server);
  372. /* No information (e.g. nameserver IP has two PTR records) */
  373. else if (strstr (input_buffer, "No information"))
  374. die (STATE_CRITICAL, _("No information returned by DNS server at %s\n"), dns_server);
  375. /* Host or domain name does not exist */
  376. else if (strstr (input_buffer, "Non-existent") ||
  377. strstr (input_buffer, "** server can't find") ||
  378. strstr (input_buffer,"NXDOMAIN"))
  379. die (STATE_CRITICAL, _("Domain %s was not found by the server\n"), query_address);
  380. /* Network is unreachable */
  381. else if (strstr (input_buffer, "Network is unreachable"))
  382. die (STATE_CRITICAL, _("Network is unreachable\n"));
  383. /* Internal server failure */
  384. else if (strstr (input_buffer, "Server failure"))
  385. die (STATE_CRITICAL, _("DNS failure for %s\n"), dns_server);
  386. /* Request error or the DNS lookup timed out */
  387. else if (strstr (input_buffer, "Format error") ||
  388. strstr (input_buffer, "Timed out"))
  389. return STATE_WARNING;
  390. return STATE_OK;
  391. }
  392. /* process command-line arguments */
  393. int
  394. process_arguments (int argc, char **argv)
  395. {
  396. int c;
  397. char *warning = NULL;
  398. char *critical = NULL;
  399. int opt_index = 0;
  400. static struct option long_opts[] = {
  401. {"help", no_argument, 0, 'h'},
  402. {"version", no_argument, 0, 'V'},
  403. {"verbose", no_argument, 0, 'v'},
  404. {"timeout", required_argument, 0, 't'},
  405. {"hostname", required_argument, 0, 'H'},
  406. {"server", required_argument, 0, 's'},
  407. {"reverse-server", required_argument, 0, 'r'},
  408. {"querytype", required_argument, 0, 'q'},
  409. {"expected-address", required_argument, 0, 'a'},
  410. {"expect-authority", no_argument, 0, 'A'},
  411. {"accept-cname", no_argument, 0, 'n'},
  412. {"warning", required_argument, 0, 'w'},
  413. {"critical", required_argument, 0, 'c'},
  414. {0, 0, 0, 0}
  415. };
  416. if (argc < 2)
  417. return ERROR;
  418. for (c = 1; c < argc; c++)
  419. if (strcmp ("-to", argv[c]) == 0)
  420. strcpy (argv[c], "-t");
  421. while (1) {
  422. c = getopt_long (argc, argv, "hVvAnt:H:s:r:a:q:w:c:", long_opts, &opt_index);
  423. if (c == -1 || c == EOF)
  424. break;
  425. switch (c) {
  426. case 'h': /* help */
  427. print_help ();
  428. exit (STATE_OK);
  429. case 'V': /* version */
  430. print_revision (progname, NP_VERSION);
  431. exit (STATE_OK);
  432. case 'v': /* version */
  433. verbose = TRUE;
  434. break;
  435. case 't': /* timeout period */
  436. timeout_interval = atoi (optarg);
  437. break;
  438. case 'H': /* hostname */
  439. if (strlen (optarg) >= ADDRESS_LENGTH)
  440. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  441. strcpy (query_address, optarg);
  442. break;
  443. case 's': /* server name */
  444. /* TODO: this host_or_die check is probably unnecessary.
  445. * Better to confirm nslookup response matches */
  446. host_or_die(optarg);
  447. if (strlen (optarg) >= ADDRESS_LENGTH)
  448. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  449. strcpy (dns_server, optarg);
  450. break;
  451. case 'r': /* reverse server name */
  452. /* TODO: Is this host_or_die necessary? */
  453. host_or_die(optarg);
  454. if (strlen (optarg) >= ADDRESS_LENGTH)
  455. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  456. strcpy (ptr_server, optarg);
  457. break;
  458. case 'a': /* expected address */
  459. if (strlen (optarg) >= ADDRESS_LENGTH)
  460. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  461. expected_address = (char **)realloc(expected_address, (expected_address_cnt+1) * sizeof(char**));
  462. expected_address[expected_address_cnt] = strdup(optarg);
  463. expected_address_cnt++;
  464. break;
  465. case 'q': /* querytype -- A or AAAA or ANY or SRV or TXT, etc. */
  466. if (strlen (optarg) < 1 || strlen (optarg) > 5)
  467. die (STATE_UNKNOWN, _("Missing valid querytype parameter. Try using 'A' or 'AAAA' or 'SRV'\n"));
  468. strntoupper(optarg, sizeof(optarg));
  469. strcpy(query_type, "-querytype=");
  470. strcat(query_type, optarg);
  471. query_set = TRUE;
  472. break;
  473. case 'A': /* expect authority */
  474. expect_authority = TRUE;
  475. break;
  476. case 'n': /* accept cname responses as a result */
  477. accept_cname = TRUE;
  478. break;
  479. case 'w':
  480. warning = optarg;
  481. break;
  482. case 'c':
  483. critical = optarg;
  484. break;
  485. default: /* args not parsable */
  486. usage5();
  487. }
  488. }
  489. c = optind;
  490. if (strlen(query_address)==0 && c<argc) {
  491. if (strlen(argv[c])>=ADDRESS_LENGTH)
  492. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  493. strcpy (query_address, argv[c++]);
  494. }
  495. if (strlen(dns_server)==0 && c<argc) {
  496. /* TODO: See -s option */
  497. host_or_die(argv[c]);
  498. if (strlen(argv[c]) >= ADDRESS_LENGTH)
  499. die (STATE_UNKNOWN, _("Input buffer overflow\n"));
  500. strcpy (dns_server, argv[c++]);
  501. }
  502. set_thresholds(&time_thresholds, warning, critical);
  503. return validate_arguments ();
  504. }
  505. int
  506. validate_arguments ()
  507. {
  508. if (query_address[0] == 0)
  509. return ERROR;
  510. return OK;
  511. }
  512. void
  513. print_help (void)
  514. {
  515. print_revision (progname, NP_VERSION);
  516. printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
  517. printf (COPYRIGHT, copyright, email);
  518. printf ("%s\n", _("This plugin uses the nslookup program to obtain the IP address for the given host/domain query."));
  519. printf ("%s\n", _("An optional DNS server to use may be specified."));
  520. printf ("%s\n", _("If no DNS server is specified, the default server(s) specified in /etc/resolv.conf will be used."));
  521. printf ("\n\n");
  522. print_usage ();
  523. printf (UT_HELP_VRSN);
  524. printf (UT_EXTRA_OPTS);
  525. printf (" -H, --hostname=HOST\n");
  526. printf (" %s\n", _("The name or address you want to query"));
  527. printf (" -s, --server=HOST\n");
  528. printf (" %s\n", _("Optional DNS server you want to use for the lookup"));
  529. printf (" -q, --querytype=TYPE\n");
  530. printf (" %s\n", _("Optional DNS record query type where TYPE =(A, AAAA, SRV, TXT, MX, ANY)"));
  531. printf (" %s\n", _("The default query type is 'A' (IPv4 host entry)"));
  532. printf (" -a, --expected-address=IP-ADDRESS|HOST\n");
  533. printf (" %s\n", _("Optional IP-ADDRESS you expect the DNS server to return. HOST must end with"));
  534. printf (" %s\n", _("a dot (.). This option can be repeated multiple times (Returns OK if any"));
  535. printf (" %s\n", _("value match). If multiple addresses are returned at once, you have to match"));
  536. printf (" %s\n", _("the whole string of addresses separated with commas (sorted alphabetically)."));
  537. printf (" %s\n", _("If you would like to test for the presence of a cname, combine with -n param."));
  538. printf (" -A, --expect-authority\n");
  539. printf (" %s\n", _("Optionally expect the DNS server to be authoritative for the lookup"));
  540. printf (" -n, --accept-cname\n");
  541. printf (" %s\n", _("Optionally accept cname responses as a valid result to a query"));
  542. printf (" %s\n", _("The default is to ignore cname responses as part of the result"));
  543. printf (" -w, --warning=seconds\n");
  544. printf (" %s\n", _("Return warning if elapsed time exceeds value. Default off"));
  545. printf (" -c, --critical=seconds\n");
  546. printf (" %s\n", _("Return critical if elapsed time exceeds value. Default off"));
  547. printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
  548. printf (UT_SUPPORT);
  549. }
  550. void
  551. print_usage (void)
  552. {
  553. printf ("%s\n", _("Usage:"));
  554. printf ("%s -H host [-s server] [-q type ] [-a expected-address] [-A] [-n] [-t timeout] [-w warn] [-c crit]\n", progname);
  555. }