1 /******************************************************************************
5 * Modified by Tim Bell <bhat@trinity.unimelb.edu.au> 2002-06-05
10 * * Program: dig plugin for NetSaint
12 * * Copyright (c) 2000
14 * * $Id: check_rbl.c 970 2004-12-02 00:30:32Z opensides $
16 *****************************************************************************/
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
;
43 int main(int argc
, char **argv
){
44 char input_buffer
[MAX_INPUT_BUFFER
];
45 char *command_line
=NULL
;
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 */
61 command_line
=ssprintf(command_line
,"%s @%s %s.%s",
62 PATH_TO_DIG
,dns_server
,
63 query_address_rev
, rbl_name
);
65 command_line
=ssprintf(command_line
,"%s %s.%s",
67 query_address_rev
, rbl_name
);
69 alarm(timeout_interval
);
73 printf("%s\n",command_line
);
75 child_process
=spopen(command_line
);
76 if (child_process
==NULL
) {
77 printf("Could not open pipe: %s\n",command_line
);
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
))
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
;
104 strcpy(output
,"Server not RBL listed.");
114 if (result!=STATE_OK) {
115 strcpy(output,"No ANSWER SECTION found");
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
,':'));
127 (void)fclose(child_stderr
);
130 if (spclose(child_process
)) {
131 result
=error_set(result
,STATE_WARNING
);
132 if (!strcmp(output
,""))
133 strcpy(output
,"nslookup returned an error status");
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
);
145 printf("DNS problem - %s\n",!strcmp(output
,"")?" Probably a non-existent host/domain":output
);
152 /* reverse the ipaddr */
153 char *reverse_ipaddr(char *ipaddr
)
155 static char revip
[MAX_HOST_ADDRESS_LENGTH
];
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");
162 sprintf(revip
, "%d.%d.%d.%d", d
, c
, b
, a
);
169 /* process command-line arguments */
170 int process_arguments(int argc
, char **argv
)
179 while((c
+=(call_getopt(argc
-c
,&argv
[c
])))<argc
){
181 if (is_option(argv
[c
]))
184 if (query_address
==NULL
) {
185 if (is_host(argv
[c
])) {
186 query_address
=argv
[c
];
188 usage("Invalid host name");
193 return validate_arguments();
198 int call_getopt(int argc
, char **argv
)
203 int option_index
= 0;
204 static struct option long_options
[] =
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'},
218 c
= getopt_long(argc
,argv
,"+hVvt:s:H:r:",long_options
,&option_index
);
220 c
= getopt(argc
,argv
,"+?hVvt:s:H:r:");
225 if(c
==-1||c
==EOF
||c
==1)
238 case 'H': /* hostname */
239 if (is_host(optarg
)) {
240 query_address
=optarg
;
242 usage("Invalid host name (-H)\n");
245 case 's': /* server */
246 if (is_host(optarg
)) {
249 usage("Invalid host name (-s)\n");
252 case 'r': /* rblname */
255 case 'v': /* verbose */
258 case 't': /* timeout */
259 if (is_intnonneg(optarg
)) {
260 timeout_interval
=atoi(optarg
);
262 usage("Time interval must be a nonnegative integer\n");
265 case 'V': /* version */
266 print_revision(progname
,"$Revision: 970 $");
272 printf (_("%s: Unknown argument: %s\n\n"), progname
, optarg
);
274 exit (STATE_UNKNOWN
);
282 int validate_arguments(void)
284 if (query_address
== NULL
|| rbl_name
== NULL
)
292 void print_help(void)
294 print_revision(progname
,"$Revision: 970 $");
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");
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"
310 " Print extra information (command-line use only)\n"
312 " Print detailed help screen\n"
314 " Print version information\n\n",
315 DEFAULT_SOCKET_TIMEOUT
);
321 void print_usage(void)
324 ("Usage: %s -H hostip -r rblname [-s server] [-t timeout] [-v]\n"
327 progname
, progname
, progname
);