/* process all config files in a specific config directory (with directory recursion) */ int read_config_dir(char *dirname){ char config_file[MAX_FILENAME_LENGTH]; DIR *dirp; struct dirent *dirfile; struct stat buf; int result=OK; int x; /* open the directory for reading */ dirp=opendir(dirname); if(dirp==NULL){ syslog(LOG_ERR,"Could not open config directory '%s' for reading.\n",dirname); return ERROR; } /* process all files in the directory... */ while((dirfile=readdir(dirp))!=NULL){ /* create the full path to the config file or subdirectory */ snprintf(config_file,sizeof(config_file)-1,"%s/%s",dirname,dirfile->d_name); config_file[sizeof(config_file)-1]='\x0'; stat(config_file, &buf); /* process this if it's a config file... */ x=strlen(dirfile->d_name); if(x>4 && !strcmp(dirfile->d_name+(x-4),".cfg")){ /* only process normal files */ if(!S_ISREG(buf.st_mode)) continue; /* process the config file */ result=read_config_file(config_file); /* break out if we encountered an error */ if(result==ERROR) break; } /* recurse into subdirectories... */ if(S_ISDIR(buf.st_mode)){ /* ignore current, parent and hidden directory entries */ if(dirfile->d_name[0]=='.') continue; /* process the config directory */ result=read_config_dir(config_file); /* break out if we encountered an error */ if(result==ERROR) break; } } closedir(dirp); return result; }
/* read in the configuration file */ int read_config_file(char *filename){ FILE *fp; char config_file[MAX_FILENAME_LENGTH]; char input_buffer[MAX_INPUT_BUFFER]; char *input_line; char *temp_buffer; char *varname; char *varvalue; int line=0; int len=0; int x=0; /* open the config file for reading */ fp=fopen(filename,"r"); /* exit if we couldn't open the config file */ if(fp==NULL){ syslog(LOG_ERR,"Unable to open config file '%s' for reading\n",filename); return ERROR; } while(fgets(input_buffer,MAX_INPUT_BUFFER-1,fp)){ line++; input_line=input_buffer; /* skip leading whitespace */ while(isspace(*input_line)) ++input_line; /* trim trailing whitespace */ len=strlen(input_line); for(x=len-1;x>=0;x--){ if(isspace(input_line[x])) input_line[x]='\x0'; else break; } /* skip comments and blank lines */ if(input_line[0]=='#') continue; if(input_line[0]=='\x0') continue; if(input_line[0]=='\n') continue; /* get the variable name */ varname=strtok(input_line,"="); if(varname==NULL){ syslog(LOG_ERR,"No variable name specified in config file '%s' - Line %d\n",filename,line); return ERROR; } /* get the variable value */ varvalue=strtok(NULL,"\n"); if(varvalue==NULL){ syslog(LOG_ERR,"No variable value specified in config file '%s' - Line %d\n",filename,line); return ERROR; } /* allow users to specify directories to recurse into for config files */ else if(!strcmp(varname,"include_dir")){ strncpy(config_file,varvalue,sizeof(config_file)-1); config_file[sizeof(config_file)-1]='\x0'; /* strip trailing / if necessary */ if(config_file[strlen(config_file)-1]=='/') config_file[strlen(config_file)-1]='\x0'; /* process the config directory... */ if(read_config_dir(config_file)==ERROR) syslog(LOG_ERR,"Continuing with errors..."); } /* allow users to specify individual config files to include */ else if(!strcmp(varname,"include") || !strcmp(varname,"include_file")){ /* process the config file... */ if(read_config_file(varvalue)==ERROR) syslog(LOG_ERR,"Continuing with errors..."); } else if(!strcmp(varname,"server_port")) server_port=strdup(varvalue); else if(!strcmp(varname,"command_prefix")) command_prefix=strdup(varvalue); else if(!strcmp(varname,"server_address")){ server_address=strdup(varvalue); } else if(!strcmp(varname,"allowed_hosts")) allowed_hosts=strdup(varvalue); else if(strstr(input_line,"command[")){ temp_buffer=strtok(varname,"["); temp_buffer=strtok(NULL,"]"); if(temp_buffer==NULL){ syslog(LOG_ERR,"Invalid command specified in config file '%s' - Line %d\n",filename,line); return ERROR; } add_command(temp_buffer,varvalue); } else if(strstr(input_buffer,"debug")){ debug=atoi(varvalue); if(debug>0) debug=TRUE; else debug=FALSE; } else if(!strcmp(varname,"nrpe_user")) nrpe_user=strdup(varvalue); else if(!strcmp(varname,"nrpe_group")) nrpe_group=strdup(varvalue); else if(!strcmp(varname,"dont_blame_nrpe")) allow_arguments=(atoi(varvalue)==1)?TRUE:FALSE; else if(!strcmp(varname,"command_timeout")){ command_timeout=atoi(varvalue); if(command_timeout<1){ syslog(LOG_ERR,"Invalid command_timeout specified in config file '%s' - Line %d\n",filename,line); return ERROR; } } else if(!strcmp(varname,"connection_timeout")){ connection_timeout=atoi(varvalue); if(connection_timeout<1){ syslog(LOG_ERR,"Invalid connection_timeout specified in config file '%s' - Line %d\n",filename,line); return ERROR; } } else if(!strcmp(varname,"allow_weak_random_seed")) allow_weak_random_seed=(atoi(varvalue)==1)?TRUE:FALSE; else if(!strcmp(varname,"pid_file")) pid_file=strdup(varvalue); else if(!strcmp(varname,"log_facility")){ if((get_log_facility(varvalue))==OK){ /* re-open log using new facility */ closelog(); openlog("nrpe",LOG_PID,log_facility); } else syslog(LOG_WARNING,"Invalid log_facility specified in config file '%s' - Line %d\n",filename,line); } else{ syslog(LOG_WARNING,"Unknown option specified in config file '%s' - Line %d\n",filename,line); continue; } } /* close the config file */ fclose(fp); return OK; }
/* read in the configuration file */ int read_config_file(char *filename){ FILE *fp; char config_file[MAX_FILENAME_LENGTH]; char input_buffer[MAX_INPUT_BUFFER]; char *temp_buffer; char *varname; char *varvalue; int line; /* open the config file for reading */ fp=fopen(filename,"r"); /* exit if we couldn't open the config file */ if(fp==NULL){ syslog(LOG_ERR,"Unable to open config file '%s' for reading\n",filename); return ERROR; } line=0; while(fgets(input_buffer,MAX_INPUT_BUFFER-1,fp)){ line++; /* skip comments and blank lines */ if(input_buffer[0]=='#') continue; if(input_buffer[0]=='\x0') continue; if(input_buffer[0]=='\n') continue; /* get the variable name */ varname=strtok(input_buffer,"="); if(varname==NULL){ syslog(LOG_ERR,"No variable name specified in config file '%s' - Line %d\n",filename,line); return ERROR; } /* get the variable value */ varvalue=strtok(NULL,"\n"); if(varvalue==NULL){ syslog(LOG_ERR,"No variable value specified in config file '%s' - Line %d\n",filename,line); return ERROR; } /* allow users to specify directories to recurse into for config files */ else if(!strcmp(varname,"include_dir")){ strncpy(config_file,varvalue,sizeof(config_file)-1); config_file[sizeof(config_file)-1]='\x0'; /* strip trailing / if necessary */ if(config_file[strlen(config_file)-1]=='/') config_file[strlen(config_file)-1]='\x0'; /* process the config directory... */ if(read_config_dir(config_file)==ERROR) break; } /* allow users to specify individual config files to include */ else if(!strcmp(varname,"include") || !strcmp(varname,"include_file")){ /* process the config file... */ if(read_config_file(varvalue)==ERROR) break; } else if(!strcmp(varname,"server_port")){ server_port=atoi(varvalue); if(server_port<1024){ syslog(LOG_ERR,"Invalid port number specified in config file '%s' - Line %d\n",filename,line); return ERROR; } } else if(!strcmp(varname,"server_address")){ strncpy(server_address,varvalue,sizeof(server_address) - 1); server_address[sizeof(server_address)-1]='\0'; } else if(!strcmp(varname,"allowed_hosts")){ if(strlen(input_buffer)>sizeof(allowed_hosts)){ syslog(LOG_ERR,"Allowed hosts list too long in config file '%s' - Line %d\n",filename,line); return ERROR; } strncpy(allowed_hosts,varvalue,sizeof(allowed_hosts)); allowed_hosts[sizeof(allowed_hosts)-1]='\x0'; } else if(strstr(input_buffer,"command[")){ temp_buffer=strtok(varname,"["); temp_buffer=strtok(NULL,"]"); if(temp_buffer==NULL){ syslog(LOG_ERR,"Invalid command specified in config file '%s' - Line %d\n",filename,line); return ERROR; } add_command(temp_buffer,varvalue); } else if(strstr(input_buffer,"debug")){ debug=atoi(varvalue); if(debug>0) debug=TRUE; else debug=FALSE; } else if(!strcmp(varname,"nrpe_user")) nrpe_user=strdup(varvalue); else if(!strcmp(varname,"nrpe_group")) nrpe_group=strdup(varvalue); else if(!strcmp(varname,"dont_blame_nrpe")) allow_arguments=(atoi(varvalue)==1)?TRUE:FALSE; else if(!strcmp(varname,"command_timeout")){ command_timeout=atoi(varvalue); if(command_timeout<1){ syslog(LOG_ERR,"Invalid command_timeout specified in config file '%s' - Line %d\n",filename,line); return ERROR; } } else{ syslog(LOG_ERR,"Unknown option specified in config file '%s' - Line %d\n",filename,line); return ERROR; } } /* close the config file */ fclose(fp); return OK; }