Code

Fix for regex input of '|', being output causing problems with Nagios' parsing of
[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: check_rbl.c 970 2004-12-02 00:30:32Z opensides $
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";
25 const char *revision = "$Revision: 970 $";
26 //const char *copyright = "2000-2003";
27 //const char *email = "nagiosplug-devel@lists.sourceforge.net";
29 int process_arguments(int, char **);
30 int call_getopt(int, char **);
31 int validate_arguments(void);
32 int check_disk(int usp,int free_disk);
33 void print_help(void);
34 void print_usage(void);
35 char *reverse_ipaddr(char *ipaddr);
37 char *query_address=NULL;
38 char *query_address_rev=NULL;
39 char *dns_server=NULL;
40 char *rbl_name=NULL;
41 int verbose=FALSE;
43 int main(int argc, char **argv){
44         char input_buffer[MAX_INPUT_BUFFER];
45         char *command_line=NULL;
46         char *output=NULL;
47         int result=STATE_OK;
49         /* Set signal handling and alarm */
50         if (signal(SIGALRM,popen_timeout_alarm_handler)==SIG_ERR)
51                 usage("Cannot catch SIGALRM\n");
53         if (process_arguments(argc,argv)!=OK)
54                 usage   (_("check_rbl: could not parse arguments\n"));
56         /* reverse the octets in the IP address */
57         query_address_rev = reverse_ipaddr(query_address);
59         /* build the command to run */
60         if (dns_server) {
61           command_line=ssprintf(command_line,"%s @%s %s.%s",
62                                 PATH_TO_DIG,dns_server,
63                                 query_address_rev, rbl_name);
64         } else {
65           command_line=ssprintf(command_line,"%s %s.%s",
66                                 PATH_TO_DIG,
67                                 query_address_rev, rbl_name);
68         }
69         alarm(timeout_interval);
70         time(&start_time);
72         if (verbose)
73                 printf("%s\n",command_line);
74         /* run the command */
75         child_process=spopen(command_line);
76         if (child_process==NULL) {
77                 printf("Could not open pipe: %s\n",command_line);
78                 return STATE_UNKNOWN;
79         }
81         child_stderr=fdopen(child_stderr_array[fileno(child_process)],"r");
82         if(child_stderr==NULL)
83                 printf("Could not open stderr for %s\n",command_line);
85         output=strscpy(output,"");
87         while (fgets(input_buffer,MAX_INPUT_BUFFER-1,child_process)) {
89                 /* the server is responding, we just got the host name... */
90                 if (strstr(input_buffer,";; ANSWER SECTION:")) {
92                         /* get the host address */
93                         if (!fgets(input_buffer,MAX_INPUT_BUFFER-1,child_process))
94                                 break;
96                         if (strpbrk(input_buffer,"\r\n"))
97                                 input_buffer[strcspn(input_buffer,"\r\n")] = '\0';
99                         if (strstr(input_buffer,query_address_rev)==input_buffer) {
100                                 output=strscpy(output,input_buffer);
101                                 /* we found it, which means it's listed! */
102                                 result=STATE_CRITICAL;
103                         } else {
104                                 strcpy(output,"Server not RBL listed.");
105                                 result=STATE_OK;
106                         }
108                         continue;
109                 }
111         }
112         
113         /*
114         if (result!=STATE_OK) {
115                 strcpy(output,"No ANSWER SECTION found");
116         }
117         */
119         while (fgets(input_buffer,MAX_INPUT_BUFFER-1,child_stderr)) {
120                 /* If we get anything on STDERR, at least set warning */
121                 result=error_set(result,STATE_WARNING);
122                 printf("%s",input_buffer);
123                 if (!strcmp(output,""))
124                         strcpy(output,1+index(input_buffer,':'));
125         }
127         (void)fclose(child_stderr);
129         /* close the pipe */
130         if (spclose(child_process)) {
131                 result=error_set(result,STATE_WARNING);
132                 if (!strcmp(output,""))
133                         strcpy(output,"nslookup returned an error status");
134         }
135         
136         (void)time(&end_time);
138         if (result==STATE_OK)
139                 printf("RBL check okay - not listed.\n");
140         else if (result==STATE_WARNING)
141                 printf("RBL WARNING - %s\n",!strcmp(output,"")?" Probably a non-existent host/domain":output);
142         else if (result==STATE_CRITICAL)
143                 printf("RBL CRITICAL - %s is listed on %s\n",query_address, rbl_name);
144         else
145                 printf("DNS problem - %s\n",!strcmp(output,"")?" Probably a non-existent host/domain":output);
147         return result;
152 /* reverse the ipaddr */
153 char *reverse_ipaddr(char *ipaddr)
155   static char revip[MAX_HOST_ADDRESS_LENGTH];
156   int a, b, c, d;
158   if (strlen(ipaddr) >= MAX_HOST_ADDRESS_LENGTH ||
159       sscanf(ipaddr, "%d.%d.%d.%d", &a, &b, &c, &d) != 4) {
160     usage("IP address invalid or too long");
161   }
162   sprintf(revip, "%d.%d.%d.%d", d, c, b, a);
164   return revip;
169 /* process command-line arguments */
170 int process_arguments(int argc, char **argv)
172   int c;
174   if(argc<2)
175     return ERROR;
178   c=0;
179   while((c+=(call_getopt(argc-c,&argv[c])))<argc){
181                 if (is_option(argv[c]))
182                         continue;
184     if (query_address==NULL) {
185                         if (is_host(argv[c])) {
186                                 query_address=argv[c];
187                         } else {
188                                 usage("Invalid host name");
189                         }
190                 }
191   }
193   return validate_arguments();
198 int call_getopt(int argc, char **argv)
200   int c,i=0;
202 #ifdef HAVE_GETOPT_H
203         int option_index = 0;
204         static struct option long_options[] =
205         { 
206                 {"hostname",       required_argument,0,'H'},
207                 {"server",         required_argument,0,'s'},
208                 {"rblname",        required_argument,0,'r'},
209                 {"verbose",        no_argument,      0,'v'},
210                 {"version",        no_argument,      0,'V'},
211                 {"help",           no_argument,      0,'h'},
212                 {0,0,0,0}
213         };
214 #endif
216   while (1){
217 #ifdef HAVE_GETOPT_H
218     c = getopt_long(argc,argv,"+hVvt:s:H:r:",long_options,&option_index);
219 #else
220     c = getopt(argc,argv,"+?hVvt:s:H:r:");
221 #endif
223     i++;
225     if(c==-1||c==EOF||c==1)
226       break;
228     switch (c)
229       {
230       case 't':
231       case 'l':
232       case 'H':
233                                 i++;
234       }
236     switch (c)
237       {
238       case 'H': /* hostname */
239                                 if (is_host(optarg)) {
240                                         query_address=optarg;
241                                 } else {
242                                         usage("Invalid host name (-H)\n");
243                                 }
244                                 break;
245       case 's': /* server */
246                                 if (is_host(optarg)) {
247                                         dns_server=optarg;
248                                 } else {
249                                         usage("Invalid host name (-s)\n");
250                                 }
251                                 break;
252       case 'r': /* rblname */
253                                 rbl_name=optarg;
254                                 break;
255       case 'v': /* verbose */
256                                 verbose=TRUE;
257                                 break;
258       case 't': /* timeout */
259                                 if (is_intnonneg(optarg)) {
260                                         timeout_interval=atoi(optarg);
261                                 } else {
262                                         usage("Time interval must be a nonnegative integer\n");
263                                 }
264                                 break;
265       case 'V': /* version */
266                                 print_revision(progname,"$Revision: 970 $");
267                                 exit(STATE_OK);
268       case 'h': /* help */
269                                 print_help();
270                                 exit(STATE_OK);
271       case '?': /* help */
272                         printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
273                         print_usage ();
274                         exit (STATE_UNKNOWN);
275                         }
276   }
277   return i;
282 int validate_arguments(void)
284   if (query_address == NULL || rbl_name == NULL)
285     return ERROR;
286   else
287     return OK;
292 void print_help(void)
294         print_revision(progname,"$Revision: 970 $");
295         printf
296                 ("Copyright (c) 2000 Karl DeBisschop\n\n"
297                  "This plugin uses dig to test whether the specified host is on any RBL lists.\n\n");
298         print_usage();
299         printf
300                 ("\nOptions:\n"
301                  " -H, --hostname=IPADDRESS\n"
302                  "   Check status of indicated host\n"
303                  " -s, --server=STRING or IPADDRESS\n"
304                  "   DNS server to use\n"
305                  " -r, --rblname=STRING\n"
306                  "   RBL domain name to use (e.g. relays.ordb.org)\n"
307                  " -t, --timeout=INTEGER\n"
308                  "   Seconds before connection attempt times out (default: %d)\n"
309                  " -v, --verbose\n"
310                  "   Print extra information (command-line use only)\n"
311                  " -h, --help\n"
312                  "   Print detailed help screen\n"
313                  " -V, --version\n"
314                  "   Print version information\n\n",
315                  DEFAULT_SOCKET_TIMEOUT);
316                 support();
321 void print_usage(void)
323         printf
324                 ("Usage: %s -H hostip -r rblname [-s server] [-t timeout] [-v]\n"
325                  "       %s --help\n"
326                  "       %s --version\n",
327                  progname, progname, progname);