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,v 1.2 2003/01/13 12:21:51 kdebisschop Exp $
16
*****************************************************************************/
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;
40
int main(int argc, char **argv){
41
char input_buffer[MAX_INPUT_BUFFER];
42
char *command_line=NULL;
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 */
58
command_line=ssprintf(command_line,"%s @%s %s.%s",
59
PATH_TO_DIG,dns_server,
60
query_address_rev, rbl_name);
62
command_line=ssprintf(command_line,"%s %s.%s",
64
query_address_rev, rbl_name);
66
alarm(timeout_interval);
70
printf("%s\n",command_line);
72
child_process=spopen(command_line);
73
if (child_process==NULL) {
74
printf("Could not open pipe: %s\n",command_line);
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))
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;
101
strcpy(output,"Server not RBL listed.");
111
if (result!=STATE_OK) {
112
strcpy(output,"No ANSWER SECTION found");
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,':'));
124
(void)fclose(child_stderr);
127
if (spclose(child_process)) {
128
result=error_set(result,STATE_WARNING);
129
if (!strcmp(output,""))
130
strcpy(output,"nslookup returned error status");
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);
142
printf("DNS problem - %s\n",!strcmp(output,"")?" Probably a non-existent host/domain":output);
147
/* reverse the ipaddr */
148
char *reverse_ipaddr(char *ipaddr)
150
static char revip[MAX_HOST_ADDRESS_LENGTH];
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");
157
sprintf(revip, "%d.%d.%d.%d", d, c, b, a);
163
/* process command-line arguments */
164
int process_arguments(int argc, char **argv)
173
while((c+=(call_getopt(argc-c,&argv[c])))<argc){
175
if (is_option(argv[c]))
178
if (query_address==NULL) {
179
if (is_host(argv[c])) {
180
query_address=argv[c];
182
usage("Invalid host name");
187
return validate_arguments();
195
int call_getopt(int argc, char **argv)
200
int option_index = 0;
201
static struct option long_options[] =
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'},
215
c = getopt_long(argc,argv,"+hVvt:s:H:r:",long_options,&option_index);
217
c = getopt(argc,argv,"+?hVvt:s:H:r:");
222
if(c==-1||c==EOF||c==1)
235
case 'H': /* hostname */
236
if (is_host(optarg)) {
237
query_address=optarg;
239
usage("Invalid host name (-H)\n");
242
case 's': /* server */
243
if (is_host(optarg)) {
246
usage("Invalid host name (-s)\n");
249
case 'r': /* rblname */
252
case 'v': /* verbose */
255
case 't': /* timeout */
256
if (is_intnonneg(optarg)) {
257
timeout_interval=atoi(optarg);
259
usage("Time interval must be a nonnegative integer\n");
262
case 'V': /* version */
263
print_revision(progname,"$Revision: 1.2 $");
269
usage("Invalid argument\n");
279
int validate_arguments(void)
281
if (query_address == NULL || rbl_name == NULL)
291
void print_help(void)
293
print_revision(progname,"$Revision: 1.2 $");
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");
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"
309
" Print extra information (command-line use only)\n"
311
" Print detailed help screen\n"
313
" Print version information\n\n",
314
DEFAULT_SOCKET_TIMEOUT);
322
void print_usage(void)
325
("Usage: %s -H hostip -r rblname [-s server] [-t timeout] [-v]\n"
328
progname, progname, progname);