check_rbl.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. /******************************************************************************
  2. *
  3. * check_rbl.c
  4. *
  5. * Modified by Tim Bell <bhat@trinity.unimelb.edu.au> 2002-06-05
  6. * based on:
  7. *
  8. * * check_dig.c
  9. * *
  10. * * Program: dig plugin for NetSaint
  11. * * License: GPL
  12. * * Copyright (c) 2000
  13. * *
  14. * * $Id: check_rbl.c 970 2004-12-02 00:30:32Z opensides $
  15. *
  16. *****************************************************************************/
  17. #include "config.h"
  18. #include "common.h"
  19. #include "utils.h"
  20. #include "popen.h"
  21. #include "string.h"
  22. const char progname = "check_rbl";
  23. const char *revision = "$Revision: 970 $";
  24. //const char *copyright = "2000-2003";
  25. //const char *email = "nagiosplug-devel@lists.sourceforge.net";
  26. int process_arguments(int, char **);
  27. int call_getopt(int, char **);
  28. int validate_arguments(void);
  29. int check_disk(int usp,int free_disk);
  30. void print_help(void);
  31. void print_usage(void);
  32. char *reverse_ipaddr(char *ipaddr);
  33. char *query_address=NULL;
  34. char *query_address_rev=NULL;
  35. char *dns_server=NULL;
  36. char *rbl_name=NULL;
  37. int verbose=FALSE;
  38. int main(int argc, char **argv){
  39. char input_buffer[MAX_INPUT_BUFFER];
  40. char *command_line=NULL;
  41. char *output=NULL;
  42. int result=STATE_OK;
  43. /* Set signal handling and alarm */
  44. if (signal(SIGALRM,popen_timeout_alarm_handler)==SIG_ERR)
  45. usage("Cannot catch SIGALRM\n");
  46. if (process_arguments(argc,argv)!=OK)
  47. usage (_("check_rbl: could not parse arguments\n"));
  48. /* reverse the octets in the IP address */
  49. query_address_rev = reverse_ipaddr(query_address);
  50. /* build the command to run */
  51. if (dns_server) {
  52. command_line=ssprintf(command_line,"%s @%s %s.%s",
  53. PATH_TO_DIG,dns_server,
  54. query_address_rev, rbl_name);
  55. } else {
  56. command_line=ssprintf(command_line,"%s %s.%s",
  57. PATH_TO_DIG,
  58. query_address_rev, rbl_name);
  59. }
  60. alarm(timeout_interval);
  61. time(&start_time);
  62. if (verbose)
  63. printf("%s\n",command_line);
  64. /* run the command */
  65. child_process=spopen(command_line);
  66. if (child_process==NULL) {
  67. printf("Could not open pipe: %s\n",command_line);
  68. return STATE_UNKNOWN;
  69. }
  70. child_stderr=fdopen(child_stderr_array[fileno(child_process)],"r");
  71. if(child_stderr==NULL)
  72. printf("Could not open stderr for %s\n",command_line);
  73. output=strscpy(output,"");
  74. while (fgets(input_buffer,MAX_INPUT_BUFFER-1,child_process)) {
  75. /* the server is responding, we just got the host name... */
  76. if (strstr(input_buffer,";; ANSWER SECTION:")) {
  77. /* get the host address */
  78. if (!fgets(input_buffer,MAX_INPUT_BUFFER-1,child_process))
  79. break;
  80. if (strpbrk(input_buffer,"\r\n"))
  81. input_buffer[strcspn(input_buffer,"\r\n")] = '\0';
  82. if (strstr(input_buffer,query_address_rev)==input_buffer) {
  83. output=strscpy(output,input_buffer);
  84. /* we found it, which means it's listed! */
  85. result=STATE_CRITICAL;
  86. } else {
  87. strcpy(output,"Server not RBL listed.");
  88. result=STATE_OK;
  89. }
  90. continue;
  91. }
  92. }
  93. /*
  94. if (result!=STATE_OK) {
  95. strcpy(output,"No ANSWER SECTION found");
  96. }
  97. */
  98. while (fgets(input_buffer,MAX_INPUT_BUFFER-1,child_stderr)) {
  99. /* If we get anything on STDERR, at least set warning */
  100. result=error_set(result,STATE_WARNING);
  101. printf("%s",input_buffer);
  102. if (!strcmp(output,""))
  103. strcpy(output,1+index(input_buffer,':'));
  104. }
  105. (void)fclose(child_stderr);
  106. /* close the pipe */
  107. if (spclose(child_process)) {
  108. result=error_set(result,STATE_WARNING);
  109. if (!strcmp(output,""))
  110. strcpy(output,"nslookup returned an error status");
  111. }
  112. (void)time(&end_time);
  113. if (result==STATE_OK)
  114. printf("RBL check okay - not listed.\n");
  115. else if (result==STATE_WARNING)
  116. printf("RBL WARNING - %s\n",!strcmp(output,"")?" Probably a non-existent host/domain":output);
  117. else if (result==STATE_CRITICAL)
  118. printf("RBL CRITICAL - %s is listed on %s\n",query_address, rbl_name);
  119. else
  120. printf("DNS problem - %s\n",!strcmp(output,"")?" Probably a non-existent host/domain":output);
  121. return result;
  122. }
  123. /* reverse the ipaddr */
  124. char *reverse_ipaddr(char *ipaddr)
  125. {
  126. static char revip[MAX_HOST_ADDRESS_LENGTH];
  127. int a, b, c, d;
  128. if (strlen(ipaddr) >= MAX_HOST_ADDRESS_LENGTH ||
  129. sscanf(ipaddr, "%d.%d.%d.%d", &a, &b, &c, &d) != 4) {
  130. usage("IP address invalid or too long");
  131. }
  132. sprintf(revip, "%d.%d.%d.%d", d, c, b, a);
  133. return revip;
  134. }
  135. /* process command-line arguments */
  136. int process_arguments(int argc, char **argv)
  137. {
  138. int c;
  139. if(argc<2)
  140. return ERROR;
  141. c=0;
  142. while((c+=(call_getopt(argc-c,&argv[c])))<argc){
  143. if (is_option(argv[c]))
  144. continue;
  145. if (query_address==NULL) {
  146. if (is_host(argv[c])) {
  147. query_address=argv[c];
  148. } else {
  149. usage("Invalid host name");
  150. }
  151. }
  152. }
  153. return validate_arguments();
  154. }
  155. int call_getopt(int argc, char **argv)
  156. {
  157. int c,i=0;
  158. #ifdef HAVE_GETOPT_H
  159. int option_index = 0;
  160. static struct option long_options[] =
  161. {
  162. {"hostname", required_argument,0,'H'},
  163. {"server", required_argument,0,'s'},
  164. {"rblname", required_argument,0,'r'},
  165. {"verbose", no_argument, 0,'v'},
  166. {"version", no_argument, 0,'V'},
  167. {"help", no_argument, 0,'h'},
  168. {0,0,0,0}
  169. };
  170. #endif
  171. while (1){
  172. #ifdef HAVE_GETOPT_H
  173. c = getopt_long(argc,argv,"+hVvt:s:H:r:",long_options,&option_index);
  174. #else
  175. c = getopt(argc,argv,"+?hVvt:s:H:r:");
  176. #endif
  177. i++;
  178. if(c==-1||c==EOF||c==1)
  179. break;
  180. switch (c)
  181. {
  182. case 't':
  183. case 'l':
  184. case 'H':
  185. i++;
  186. }
  187. switch (c)
  188. {
  189. case 'H': /* hostname */
  190. if (is_host(optarg)) {
  191. query_address=optarg;
  192. } else {
  193. usage("Invalid host name (-H)\n");
  194. }
  195. break;
  196. case 's': /* server */
  197. if (is_host(optarg)) {
  198. dns_server=optarg;
  199. } else {
  200. usage("Invalid host name (-s)\n");
  201. }
  202. break;
  203. case 'r': /* rblname */
  204. rbl_name=optarg;
  205. break;
  206. case 'v': /* verbose */
  207. verbose=TRUE;
  208. break;
  209. case 't': /* timeout */
  210. if (is_intnonneg(optarg)) {
  211. timeout_interval=atoi(optarg);
  212. } else {
  213. usage("Time interval must be a nonnegative integer\n");
  214. }
  215. break;
  216. case 'V': /* version */
  217. print_revision(progname,"$Revision: 970 $");
  218. exit(STATE_OK);
  219. case 'h': /* help */
  220. print_help();
  221. exit(STATE_OK);
  222. case '?': /* help */
  223. printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
  224. print_usage ();
  225. exit (STATE_UNKNOWN);
  226. }
  227. }
  228. return i;
  229. }
  230. int validate_arguments(void)
  231. {
  232. if (query_address == NULL || rbl_name == NULL)
  233. return ERROR;
  234. else
  235. return OK;
  236. }
  237. void print_help(void)
  238. {
  239. print_revision(progname,"$Revision: 970 $");
  240. printf
  241. ("Copyright (c) 2000 Karl DeBisschop\n\n"
  242. "This plugin uses dig to test whether the specified host is on any RBL lists.\n\n");
  243. print_usage();
  244. printf
  245. ("\nOptions:\n"
  246. " -H, --hostname=IPADDRESS\n"
  247. " Check status of indicated host\n"
  248. " -s, --server=STRING or IPADDRESS\n"
  249. " DNS server to use\n"
  250. " -r, --rblname=STRING\n"
  251. " RBL domain name to use (e.g. relays.ordb.org)\n"
  252. " -t, --timeout=INTEGER\n"
  253. " Seconds before connection attempt times out (default: %d)\n"
  254. " -v, --verbose\n"
  255. " Print extra information (command-line use only)\n"
  256. " -h, --help\n"
  257. " Print detailed help screen\n"
  258. " -V, --version\n"
  259. " Print version information\n\n",
  260. DEFAULT_SOCKET_TIMEOUT);
  261. support();
  262. }
  263. void print_usage(void)
  264. {
  265. printf
  266. ("Usage: %s -H hostip -r rblname [-s server] [-t timeout] [-v]\n"
  267. " %s --help\n"
  268. " %s --version\n",
  269. progname, progname, progname);
  270. }