diff options
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/check_rbl.c | 329 |
1 files changed, 329 insertions, 0 deletions
diff --git a/contrib/check_rbl.c b/contrib/check_rbl.c new file mode 100644 index 00000000..5c661135 --- /dev/null +++ b/contrib/check_rbl.c | |||
@@ -0,0 +1,329 @@ | |||
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 | |||
18 | #include "config.h" | ||
19 | #include "common.h" | ||
20 | #include "utils.h" | ||
21 | #include "popen.h" | ||
22 | #include "string.h" | ||
23 | |||
24 | #define PROGNAME "check_rbl" | ||
25 | |||
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); | ||
33 | |||
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; | ||
39 | |||
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; | ||
45 | |||
46 | /* Set signal handling and alarm */ | ||
47 | if (signal(SIGALRM,popen_timeout_alarm_handler)==SIG_ERR) | ||
48 | usage("Cannot catch SIGALRM\n"); | ||
49 | |||
50 | if (process_arguments(argc,argv)!=OK) | ||
51 | usage("Could not parse arguments\n"); | ||
52 | |||
53 | /* reverse the octets in the IP address */ | ||
54 | query_address_rev = reverse_ipaddr(query_address); | ||
55 | |||
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); | ||
68 | |||
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 | } | ||
77 | |||
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); | ||
81 | |||
82 | output=strscpy(output,""); | ||
83 | |||
84 | while (fgets(input_buffer,MAX_INPUT_BUFFER-1,child_process)) { | ||
85 | |||
86 | /* the server is responding, we just got the host name... */ | ||
87 | if (strstr(input_buffer,";; ANSWER SECTION:")) { | ||
88 | |||
89 | /* get the host address */ | ||
90 | if (!fgets(input_buffer,MAX_INPUT_BUFFER-1,child_process)) | ||
91 | break; | ||
92 | |||
93 | if (strpbrk(input_buffer,"\r\n")) | ||
94 | input_buffer[strcspn(input_buffer,"\r\n")] = '\0'; | ||
95 | |||
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 | } | ||
104 | |||
105 | continue; | ||
106 | } | ||
107 | |||
108 | } | ||
109 | |||
110 | /* | ||
111 | if (result!=STATE_OK) { | ||
112 | strcpy(output,"No ANSWER SECTION found"); | ||
113 | } | ||
114 | */ | ||
115 | |||
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 | } | ||
123 | |||
124 | (void)fclose(child_stderr); | ||
125 | |||
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); | ||
134 | |||
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); | ||
143 | |||
144 | return result; | ||
145 | } | ||
146 | |||
147 | /* reverse the ipaddr */ | ||
148 | char *reverse_ipaddr(char *ipaddr) | ||
149 | { | ||
150 | static char revip[MAX_HOST_ADDRESS_LENGTH]; | ||
151 | int a, b, c, d; | ||
152 | |||
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); | ||
158 | |||
159 | return revip; | ||
160 | } | ||
161 | |||
162 | |||
163 | /* process command-line arguments */ | ||
164 | int process_arguments(int argc, char **argv) | ||
165 | { | ||
166 | int c; | ||
167 | |||
168 | if(argc<2) | ||
169 | return ERROR; | ||
170 | |||
171 | |||
172 | c=0; | ||
173 | while((c+=(call_getopt(argc-c,&argv[c])))<argc){ | ||
174 | |||
175 | if (is_option(argv[c])) | ||
176 | continue; | ||
177 | |||
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 | } | ||
186 | |||
187 | return validate_arguments(); | ||
188 | } | ||
189 | |||
190 | |||
191 | |||
192 | |||
193 | |||
194 | |||
195 | int call_getopt(int argc, char **argv) | ||
196 | { | ||
197 | int c,i=0; | ||
198 | |||
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 | ||
212 | |||
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 | ||
219 | |||
220 | i++; | ||
221 | |||
222 | if(c==-1||c==EOF||c==1) | ||
223 | break; | ||
224 | |||
225 | switch (c) | ||
226 | { | ||
227 | case 't': | ||
228 | case 'l': | ||
229 | case 'H': | ||
230 | i++; | ||
231 | } | ||
232 | |||
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; | ||
273 | } | ||
274 | |||
275 | |||
276 | |||
277 | |||
278 | |||
279 | int validate_arguments(void) | ||
280 | { | ||
281 | if (query_address == NULL || rbl_name == NULL) | ||
282 | return ERROR; | ||
283 | else | ||
284 | return OK; | ||
285 | } | ||
286 | |||
287 | |||
288 | |||
289 | |||
290 | |||
291 | void print_help(void) | ||
292 | { | ||
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(); | ||
316 | } | ||
317 | |||
318 | |||
319 | |||
320 | |||
321 | |||
322 | void print_usage(void) | ||
323 | { | ||
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); | ||
329 | } | ||