check_dns.c 26 KB

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