4
0

check_dns.c 26 KB

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