Back to index

nagios-plugins  1.4.16
check_rbl.c
Go to the documentation of this file.
00001 /******************************************************************************
00002 *
00003 * check_rbl.c
00004 *
00005 * Modified by Tim Bell <bhat@trinity.unimelb.edu.au> 2002-06-05
00006 * based on:
00007 *
00008 * * check_dig.c
00009 * *
00010 * * Program: dig plugin for NetSaint
00011 * * License: GPL
00012 * * Copyright (c) 2000
00013 * * 
00014 * * $Id: check_rbl.c 970 2004-12-02 00:30:32Z opensides $
00015 *
00016 *****************************************************************************/
00017 
00018 #include "config.h"
00019 #include "common.h"
00020 #include "utils.h"
00021 #include "popen.h"
00022 #include "string.h"
00023 
00024 const char progname = "check_rbl";
00025 const char *revision = "$Revision: 970 $";
00026 //const char *copyright = "2000-2003";
00027 //const char *email = "nagiosplug-devel@lists.sourceforge.net";
00028 
00029 int process_arguments(int, char **);
00030 int call_getopt(int, char **);
00031 int validate_arguments(void);
00032 int check_disk(int usp,int free_disk);
00033 void print_help(void);
00034 void print_usage(void);
00035 char *reverse_ipaddr(char *ipaddr);
00036 
00037 char *query_address=NULL;
00038 char *query_address_rev=NULL;
00039 char *dns_server=NULL;
00040 char *rbl_name=NULL;
00041 int verbose=FALSE;
00042 
00043 int main(int argc, char **argv){
00044        char input_buffer[MAX_INPUT_BUFFER];
00045        char *command_line=NULL;
00046        char *output=NULL;
00047        int result=STATE_OK;
00048 
00049        /* Set signal handling and alarm */
00050        if (signal(SIGALRM,popen_timeout_alarm_handler)==SIG_ERR)
00051               usage("Cannot catch SIGALRM\n");
00052 
00053        if (process_arguments(argc,argv)!=OK)
00054               usage  (_("check_rbl: could not parse arguments\n"));
00055 
00056        /* reverse the octets in the IP address */
00057        query_address_rev = reverse_ipaddr(query_address);
00058 
00059        /* build the command to run */
00060        if (dns_server) {
00061          command_line=ssprintf(command_line,"%s @%s %s.%s",
00062                             PATH_TO_DIG,dns_server,
00063                             query_address_rev, rbl_name);
00064        } else {
00065          command_line=ssprintf(command_line,"%s %s.%s",
00066                             PATH_TO_DIG,
00067                             query_address_rev, rbl_name);
00068        }
00069        alarm(timeout_interval);
00070        time(&start_time);
00071 
00072        if (verbose)
00073               printf("%s\n",command_line);
00074        /* run the command */
00075        child_process=spopen(command_line);
00076        if (child_process==NULL) {
00077               printf("Could not open pipe: %s\n",command_line);
00078               return STATE_UNKNOWN;
00079        }
00080 
00081        child_stderr=fdopen(child_stderr_array[fileno(child_process)],"r");
00082        if(child_stderr==NULL)
00083               printf("Could not open stderr for %s\n",command_line);
00084 
00085        output=strscpy(output,"");
00086 
00087        while (fgets(input_buffer,MAX_INPUT_BUFFER-1,child_process)) {
00088 
00089               /* the server is responding, we just got the host name... */
00090               if (strstr(input_buffer,";; ANSWER SECTION:")) {
00091 
00092                      /* get the host address */
00093                      if (!fgets(input_buffer,MAX_INPUT_BUFFER-1,child_process))
00094                             break;
00095 
00096                      if (strpbrk(input_buffer,"\r\n"))
00097                             input_buffer[strcspn(input_buffer,"\r\n")] = '\0';
00098 
00099                      if (strstr(input_buffer,query_address_rev)==input_buffer) {
00100                             output=strscpy(output,input_buffer);
00101                             /* we found it, which means it's listed! */
00102                             result=STATE_CRITICAL;
00103                      } else {
00104                             strcpy(output,"Server not RBL listed.");
00105                             result=STATE_OK;
00106                      }
00107 
00108                      continue;
00109               }
00110 
00111        }
00112        
00113        /*
00114        if (result!=STATE_OK) {
00115               strcpy(output,"No ANSWER SECTION found");
00116        }
00117        */
00118 
00119        while (fgets(input_buffer,MAX_INPUT_BUFFER-1,child_stderr)) {
00120               /* If we get anything on STDERR, at least set warning */
00121               result=error_set(result,STATE_WARNING);
00122               printf("%s",input_buffer);
00123               if (!strcmp(output,""))
00124                      strcpy(output,1+index(input_buffer,':'));
00125        }
00126 
00127        (void)fclose(child_stderr);
00128 
00129        /* close the pipe */
00130        if (spclose(child_process)) {
00131               result=error_set(result,STATE_WARNING);
00132               if (!strcmp(output,""))
00133                      strcpy(output,"nslookup returned an error status");
00134        }
00135        
00136        (void)time(&end_time);
00137 
00138        if (result==STATE_OK)
00139               printf("RBL check okay - not listed.\n");
00140        else if (result==STATE_WARNING)
00141               printf("RBL WARNING - %s\n",!strcmp(output,"")?" Probably a non-existent host/domain":output);
00142        else if (result==STATE_CRITICAL)
00143               printf("RBL CRITICAL - %s is listed on %s\n",query_address, rbl_name);
00144        else
00145               printf("DNS problem - %s\n",!strcmp(output,"")?" Probably a non-existent host/domain":output);
00146 
00147        return result;
00148 }
00149 
00150 
00151 
00152 /* reverse the ipaddr */
00153 char *reverse_ipaddr(char *ipaddr)
00154 {
00155   static char revip[MAX_HOST_ADDRESS_LENGTH];
00156   int a, b, c, d;
00157 
00158   if (strlen(ipaddr) >= MAX_HOST_ADDRESS_LENGTH ||
00159       sscanf(ipaddr, "%d.%d.%d.%d", &a, &b, &c, &d) != 4) {
00160     usage("IP address invalid or too long");
00161   }
00162   sprintf(revip, "%d.%d.%d.%d", d, c, b, a);
00163 
00164   return revip;
00165 }
00166 
00167 
00168 
00169 /* process command-line arguments */
00170 int process_arguments(int argc, char **argv)
00171 {
00172   int c;
00173 
00174   if(argc<2)
00175     return ERROR;
00176 
00177 
00178   c=0;
00179   while((c+=(call_getopt(argc-c,&argv[c])))<argc){
00180 
00181               if (is_option(argv[c]))
00182                      continue;
00183 
00184     if (query_address==NULL) {
00185                      if (is_host(argv[c])) {
00186                             query_address=argv[c];
00187                      } else {
00188                             usage("Invalid host name");
00189                      }
00190               }
00191   }
00192 
00193   return validate_arguments();
00194 }
00195 
00196 
00197 
00198 int call_getopt(int argc, char **argv)
00199 {
00200   int c,i=0;
00201 
00202 #ifdef HAVE_GETOPT_H
00203        int option_index = 0;
00204        static struct option long_options[] =
00205        { 
00206               {"hostname",       required_argument,0,'H'},
00207               {"server",         required_argument,0,'s'},
00208               {"rblname",        required_argument,0,'r'},
00209               {"verbose",        no_argument,      0,'v'},
00210               {"version",        no_argument,      0,'V'},
00211               {"help",           no_argument,      0,'h'},
00212               {0,0,0,0}
00213        };
00214 #endif
00215 
00216   while (1){
00217 #ifdef HAVE_GETOPT_H
00218     c = getopt_long(argc,argv,"+hVvt:s:H:r:",long_options,&option_index);
00219 #else
00220     c = getopt(argc,argv,"+?hVvt:s:H:r:");
00221 #endif
00222 
00223     i++;
00224 
00225     if(c==-1||c==EOF||c==1)
00226       break;
00227 
00228     switch (c)
00229       {
00230       case 't':
00231       case 'l':
00232       case 'H':
00233                             i++;
00234       }
00235 
00236     switch (c)
00237       {
00238       case 'H': /* hostname */
00239                             if (is_host(optarg)) {
00240                                    query_address=optarg;
00241                             } else {
00242                                    usage("Invalid host name (-H)\n");
00243                             }
00244                             break;
00245       case 's': /* server */
00246                             if (is_host(optarg)) {
00247                                    dns_server=optarg;
00248                             } else {
00249                                    usage("Invalid host name (-s)\n");
00250                             }
00251                             break;
00252       case 'r': /* rblname */
00253                             rbl_name=optarg;
00254                             break;
00255       case 'v': /* verbose */
00256                             verbose=TRUE;
00257                             break;
00258       case 't': /* timeout */
00259                             if (is_intnonneg(optarg)) {
00260                                    timeout_interval=atoi(optarg);
00261                             } else {
00262                                    usage("Time interval must be a nonnegative integer\n");
00263                             }
00264                             break;
00265       case 'V': /* version */
00266                             print_revision(progname,"$Revision: 970 $");
00267                             exit(STATE_OK);
00268       case 'h': /* help */
00269                             print_help();
00270                             exit(STATE_OK);
00271       case '?': /* help */
00272                      printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
00273                      print_usage ();
00274                      exit (STATE_UNKNOWN);
00275                      }
00276   }
00277   return i;
00278 }
00279 
00280 
00281 
00282 int validate_arguments(void)
00283 {
00284   if (query_address == NULL || rbl_name == NULL)
00285     return ERROR;
00286   else
00287     return OK;
00288 }
00289 
00290 
00291 
00292 void print_help(void)
00293 {
00294        print_revision(progname,"$Revision: 970 $");
00295        printf
00296               ("Copyright (c) 2000 Karl DeBisschop\n\n"
00297                "This plugin uses dig to test whether the specified host is on any RBL lists.\n\n");
00298        print_usage();
00299        printf
00300               ("\nOptions:\n"
00301                " -H, --hostname=IPADDRESS\n"
00302                "   Check status of indicated host\n"
00303                " -s, --server=STRING or IPADDRESS\n"
00304                "   DNS server to use\n"
00305                " -r, --rblname=STRING\n"
00306                "   RBL domain name to use (e.g. relays.ordb.org)\n"
00307                " -t, --timeout=INTEGER\n"
00308                "   Seconds before connection attempt times out (default: %d)\n"
00309                " -v, --verbose\n"
00310                "   Print extra information (command-line use only)\n"
00311                " -h, --help\n"
00312                "   Print detailed help screen\n"
00313                " -V, --version\n"
00314                "   Print version information\n\n",
00315                DEFAULT_SOCKET_TIMEOUT);
00316               support();
00317 }
00318 
00319 
00320 
00321 void print_usage(void)
00322 {
00323        printf
00324               ("Usage: %s -H hostip -r rblname [-s server] [-t timeout] [-v]\n"
00325                "       %s --help\n"
00326                "       %s --version\n",
00327                progname, progname, progname);
00328 }