check_rbl.c 7.0 KB

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