Code

convert PROGANE from a define to a const char
[nagiosplug.git] / contrib / check_rbl.c
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 *****************************************************************************/
18 #include "config.h"
19 #include "common.h"
20 #include "utils.h"
21 #include "popen.h"
22 #include "string.h"
24 const char progname = "check_rbl";
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);
34 char *query_address=NULL;
35 char *query_address_rev=NULL;
36 char *dns_server=NULL;
37 char *rbl_name=NULL;
38 int verbose=FALSE;
40 int main(int argc, char **argv){
41         char input_buffer[MAX_INPUT_BUFFER];
42         char *command_line=NULL;
43         char *output=NULL;
44         int result=STATE_OK;
46         /* Set signal handling and alarm */
47         if (signal(SIGALRM,popen_timeout_alarm_handler)==SIG_ERR)
48                 usage("Cannot catch SIGALRM\n");
50         if (process_arguments(argc,argv)!=OK)
51                 usage("Could not parse arguments\n");
53         /* reverse the octets in the IP address */
54         query_address_rev = reverse_ipaddr(query_address);
56         /* build the command to run */
57         if (dns_server) {
58           command_line=ssprintf(command_line,"%s @%s %s.%s",
59                                 PATH_TO_DIG,dns_server,
60                                 query_address_rev, rbl_name);
61         } else {
62           command_line=ssprintf(command_line,"%s %s.%s",
63                                 PATH_TO_DIG,
64                                 query_address_rev, rbl_name);
65         }
66         alarm(timeout_interval);
67         time(&start_time);
69         if (verbose)
70                 printf("%s\n",command_line);
71         /* run the command */
72         child_process=spopen(command_line);
73         if (child_process==NULL) {
74                 printf("Could not open pipe: %s\n",command_line);
75                 return STATE_UNKNOWN;
76         }
78         child_stderr=fdopen(child_stderr_array[fileno(child_process)],"r");
79         if(child_stderr==NULL)
80                 printf("Could not open stderr for %s\n",command_line);
82         output=strscpy(output,"");
84         while (fgets(input_buffer,MAX_INPUT_BUFFER-1,child_process)) {
86                 /* the server is responding, we just got the host name... */
87                 if (strstr(input_buffer,";; ANSWER SECTION:")) {
89                         /* get the host address */
90                         if (!fgets(input_buffer,MAX_INPUT_BUFFER-1,child_process))
91                                 break;
93                         if (strpbrk(input_buffer,"\r\n"))
94                                 input_buffer[strcspn(input_buffer,"\r\n")] = '\0';
96                         if (strstr(input_buffer,query_address_rev)==input_buffer) {
97                                 output=strscpy(output,input_buffer);
98                                 /* we found it, which means it's listed! */
99                                 result=STATE_CRITICAL;
100                         } else {
101                                 strcpy(output,"Server not RBL listed.");
102                                 result=STATE_OK;
103                         }
105                         continue;
106                 }
108         }
109         
110         /*
111         if (result!=STATE_OK) {
112                 strcpy(output,"No ANSWER SECTION found");
113         }
114         */
116         while (fgets(input_buffer,MAX_INPUT_BUFFER-1,child_stderr)) {
117                 /* If we get anything on STDERR, at least set warning */
118                 result=error_set(result,STATE_WARNING);
119                 printf("%s",input_buffer);
120                 if (!strcmp(output,""))
121                         strcpy(output,1+index(input_buffer,':'));
122         }
124         (void)fclose(child_stderr);
126         /* close the pipe */
127         if (spclose(child_process)) {
128                 result=error_set(result,STATE_WARNING);
129                 if (!strcmp(output,""))
130                         strcpy(output,"nslookup returned error status");
131         }
132         
133         (void)time(&end_time);
135         if (result==STATE_OK)
136                 printf("RBL check okay - not listed.\n");
137         else if (result==STATE_WARNING)
138                 printf("RBL WARNING - %s\n",!strcmp(output,"")?" Probably a non-existent host/domain":output);
139         else if (result==STATE_CRITICAL)
140                 printf("RBL CRITICAL - %s is listed on %s\n",query_address, rbl_name);
141         else
142                 printf("DNS problem - %s\n",!strcmp(output,"")?" Probably a non-existent host/domain":output);
144         return result;
147 /* reverse the ipaddr */
148 char *reverse_ipaddr(char *ipaddr)
150   static char revip[MAX_HOST_ADDRESS_LENGTH];
151   int a, b, c, d;
153   if (strlen(ipaddr) >= MAX_HOST_ADDRESS_LENGTH ||
154       sscanf(ipaddr, "%d.%d.%d.%d", &a, &b, &c, &d) != 4) {
155     usage("IP address invalid or too long");
156   }
157   sprintf(revip, "%d.%d.%d.%d", d, c, b, a);
159   return revip;
163 /* process command-line arguments */
164 int process_arguments(int argc, char **argv)
166   int c;
168   if(argc<2)
169     return ERROR;
172   c=0;
173   while((c+=(call_getopt(argc-c,&argv[c])))<argc){
175                 if (is_option(argv[c]))
176                         continue;
178     if (query_address==NULL) {
179                         if (is_host(argv[c])) {
180                                 query_address=argv[c];
181                         } else {
182                                 usage("Invalid host name");
183                         }
184                 }
185   }
187   return validate_arguments();
195 int call_getopt(int argc, char **argv)
197   int c,i=0;
199 #ifdef HAVE_GETOPT_H
200         int option_index = 0;
201         static struct option long_options[] =
202         { 
203                 {"hostname",       required_argument,0,'H'},
204                 {"server",         required_argument,0,'s'},
205                 {"rblname",        required_argument,0,'r'},
206                 {"verbose",        no_argument,      0,'v'},
207                 {"version",        no_argument,      0,'V'},
208                 {"help",           no_argument,      0,'h'},
209                 {0,0,0,0}
210         };
211 #endif
213   while (1){
214 #ifdef HAVE_GETOPT_H
215     c = getopt_long(argc,argv,"+hVvt:s:H:r:",long_options,&option_index);
216 #else
217     c = getopt(argc,argv,"+?hVvt:s:H:r:");
218 #endif
220     i++;
222     if(c==-1||c==EOF||c==1)
223       break;
225     switch (c)
226       {
227       case 't':
228       case 'l':
229       case 'H':
230                                 i++;
231       }
233     switch (c)
234       {
235       case 'H': /* hostname */
236                                 if (is_host(optarg)) {
237                                         query_address=optarg;
238                                 } else {
239                                         usage("Invalid host name (-H)\n");
240                                 }
241                                 break;
242       case 's': /* server */
243                                 if (is_host(optarg)) {
244                                         dns_server=optarg;
245                                 } else {
246                                         usage("Invalid host name (-s)\n");
247                                 }
248                                 break;
249       case 'r': /* rblname */
250                                 rbl_name=optarg;
251                                 break;
252       case 'v': /* verbose */
253                                 verbose=TRUE;
254                                 break;
255       case 't': /* timeout */
256                                 if (is_intnonneg(optarg)) {
257                                         timeout_interval=atoi(optarg);
258                                 } else {
259                                         usage("Time interval must be a nonnegative integer\n");
260                                 }
261                                 break;
262       case 'V': /* version */
263                                 print_revision(progname,"$Revision$");
264                                 exit(STATE_OK);
265       case 'h': /* help */
266                                 print_help();
267                                 exit(STATE_OK);
268       case '?': /* help */
269                                 usage("Invalid argument\n");
270                         }
271   }
272   return i;
279 int validate_arguments(void)
281   if (query_address == NULL || rbl_name == NULL)
282     return ERROR;
283   else
284     return OK;
291 void print_help(void)
293         print_revision(progname,"$Revision$");
294         printf
295                 ("Copyright (c) 2000 Karl DeBisschop\n\n"
296                  "This plugin uses dig to test whether the specified host is on any RBL lists.\n\n");
297         print_usage();
298         printf
299                 ("\nOptions:\n"
300                  " -H, --hostname=IPADDRESS\n"
301                  "   Check status of indicated host\n"
302                  " -s, --server=STRING or IPADDRESS\n"
303                  "   DNS server to use\n"
304                  " -r, --rblname=STRING\n"
305                  "   RBL domain name to use (e.g. relays.ordb.org)\n"
306                  " -t, --timeout=INTEGER\n"
307                  "   Seconds before connection attempt times out (default: %d)\n"
308                  " -v, --verbose\n"
309                  "   Print extra information (command-line use only)\n"
310                  " -h, --help\n"
311                  "   Print detailed help screen\n"
312                  " -V, --version\n"
313                  "   Print version information\n\n",
314                  DEFAULT_SOCKET_TIMEOUT);
315                 support();
322 void print_usage(void)
324         printf
325                 ("Usage: %s -H hostip -r rblname [-s server] [-t timeout] [-v]\n"
326                  "       %s --help\n"
327                  "       %s --version\n",
328                  progname, progname, progname);