check_dns.c 24 KB

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