Przeglądaj źródła

Added include and include_dir directives (Arie Grapa)

Ethan Galstad 23 lat temu
rodzic
commit
228a5cca8d
7 zmienionych plików z 127 dodań i 9 usunięć
  1. 5 1
      Changelog
  2. 2 1
      common/common.h
  3. 6 1
      common/config.h.in
  4. 1 1
      configure
  5. 1 1
      configure.in
  6. 17 1
      nrpe.cfg.in
  7. 95 3
      src/nrpe.c

+ 5 - 1
Changelog

@@ -8,7 +8,11 @@ NRPE Changelog
 - Added support for passing arguments to command
 - NRPE daemon can no longer be run as root user/group
 - Added getopt support
-- Added native SSL support
+- Added 'include' variable to config file to allow inclusion
+  of external config files
+- Added 'include_dir' variable to allow inclusion of external
+  config files in directories (with recursion)
+- Added native SSL support (still not working)
 
 
 1.8 - 01/16/2003

+ 2 - 1
common/common.h

@@ -2,7 +2,7 @@
  *
  * COMMON.H - NRPE Common Include File
  * Copyright (c) 1999-2003 Ethan Galstad (nagios@nagios.org)
- * Last Modified: 01-28-2003
+ * Last Modified: 03-05-2003
  *
  * License:
  *
@@ -41,6 +41,7 @@
 #define DEFAULT_SOCKET_TIMEOUT	10	/* timeout after 10 seconds */
 
 #define MAX_INPUT_BUFFER	2048	/* max size of most buffers we use */
+#define MAX_FILENAME_LENGTH     256
 
 #define MAX_HOST_ADDRESS_LENGTH	256	/* max size of a host address */
 

+ 6 - 1
common/config.h.in

@@ -2,7 +2,7 @@
  *
  * NRPE Common Header File
  * Copyright (c) 1999-2003 Ethan Galstad (nagios@nagios.org)
- * Last Modified: 02-04-2003
+ * Last Modified: 03-05-2003
  *
  * License:
  *
@@ -157,6 +157,11 @@
 #include <grp.h>
 #endif
 
+#undef HAVE_DIRENT_H
+#ifdef HAVE_DIRENT_H
+#include <dirent.h>
+#endif
+
 #undef HAVE_SSL
 #ifdef HAVE_SSL
 #include <rsa.h>

+ 1 - 1
configure

@@ -1134,7 +1134,7 @@ EOF
 
 fi
 
-for ac_hdr in ctype.h errno.h fcntl.h getopt.h grp.h netdb.h pwd.h signal.h strings.h string.h syslog.h unistd.h arpa/inet.h netinet/in.h sys/types.h sys/time.h sys/resource.h sys/wait.h sys/socket.h sys/stat.h
+for ac_hdr in ctype.h dirent.h errno.h fcntl.h getopt.h grp.h netdb.h pwd.h signal.h strings.h string.h syslog.h unistd.h arpa/inet.h netinet/in.h sys/types.h sys/time.h sys/resource.h sys/wait.h sys/socket.h sys/stat.h
 do
 ac_safe=`echo "$ac_hdr" | sed 'y%./+-%__p_%'`
 echo $ac_n "checking for $ac_hdr""... $ac_c" 1>&6

+ 1 - 1
configure.in

@@ -26,7 +26,7 @@ dnl Checks for header files.
 AC_HEADER_STDC
 AC_HEADER_TIME
 AC_HEADER_SYS_WAIT
-AC_CHECK_HEADERS(ctype.h errno.h fcntl.h getopt.h grp.h netdb.h pwd.h signal.h strings.h string.h syslog.h unistd.h arpa/inet.h netinet/in.h sys/types.h sys/time.h sys/resource.h sys/wait.h sys/socket.h sys/stat.h)
+AC_CHECK_HEADERS(ctype.h dirent.h errno.h fcntl.h getopt.h grp.h netdb.h pwd.h signal.h strings.h string.h syslog.h unistd.h arpa/inet.h netinet/in.h sys/types.h sys/time.h sys/resource.h sys/wait.h sys/socket.h sys/stat.h)
 
 dnl Checks for typedefs, structures, and compiler characteristics.
 AC_C_CONST

+ 17 - 1
nrpe.cfg.in

@@ -2,7 +2,7 @@
 # Sample NRPE Config File 
 # Written by: Ethan Galstad (nagios@nagios.org)
 # 
-# Last Modified: 01-30-2003
+# Last Modified: 03-05-2003
 #
 # NOTES:
 # This is a sample configuration file for the NRPE daemon.  It needs to be
@@ -98,6 +98,22 @@ command_timeout=60
 
 
 
+# INCLUDE CONFIG FILE
+# This directive allows you to include definitions from an external config file.
+
+#include=<somefile.cfg>
+
+
+
+# INCLUDE CONFIG DIRECTORY
+# This directive allows you to include definitions from config files (with a
+# .cfg extension) in one or more directories (with recursion).
+
+#include_dir=<somedirectory>
+#include_dir=<someotherdirectory>
+
+
+
 # COMMAND DEFINITIONS
 # Command definitions that this daemon will run.  Definitions
 # are in the following format:

+ 95 - 3
src/nrpe.c

@@ -4,7 +4,7 @@
  * Copyright (c) 1999-2003 Ethan Galstad (nagios@nagios.org)
  * License: GPL
  *
- * Last Modified: 03-03-2003
+ * Last Modified: 03-05-2003
  *
  * Command line: nrpe -c <config_file> [--inetd | --daemon]
  *
@@ -32,6 +32,7 @@ int process_arguments(int,char **);
 void wait_for_connections(void);
 void handle_connection(int);
 int read_config_file(char *);
+int read_config_dir(char *);
 int add_command(char *,char *);
 command *find_command(char *);
 void sighandler(int);
@@ -248,6 +249,7 @@ int main(int argc, char **argv){
 /* 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;
@@ -292,7 +294,30 @@ int read_config_file(char *filename){
 			return ERROR;
 		        }
 
-		if(!strcmp(varname,"server_port")){
+		/* 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);
@@ -300,7 +325,7 @@ int read_config_file(char *filename){
 			        }
 		        }
 
-               else if(!strcmp(varname,"server_address")){
+		else if(!strcmp(varname,"server_address")){
                         strncpy(server_address,varvalue,sizeof(server_address) - 1);
                         server_address[sizeof(server_address) - 1] = '\0';
                         }
@@ -369,6 +394,73 @@ int read_config_file(char *filename){
 	}
 
 
+/* 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;
+	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){
+
+		/* process this if it's a config file... */
+		x=strlen(dirfile->d_name);
+		if(x>4 && !strcmp(dirfile->d_name+(x-4),".cfg")){
+
+#ifdef _DIRENT_HAVE_D_TYPE
+			/* only process normal files */
+			if(dirfile->d_type!=DT_REG)
+				continue;
+#endif
+
+			/* create the full path to the config file */
+			snprintf(config_file,sizeof(config_file)-1,"%s/%s",dirname,dirfile->d_name);
+			config_file[sizeof(config_file)-1]='\x0';
+
+			/* process the config file */
+			result=read_config_file(config_file);
+
+			/* break out if we encountered an error */
+			if(result==ERROR)
+				break;
+		        }
+
+#ifdef _DIRENT_HAVE_D_TYPE
+		/* recurse into subdirectories... */
+		if(dirfile->d_type==DT_DIR){
+
+			/* ignore current, parent and hidden directory entries */
+			if(dirfile->d_name[0]=='.')
+				continue;
+
+			/* create the full path to the config directory */
+			snprintf(config_file,sizeof(config_file)-1,"%s/%s",dirname,dirfile->d_name);
+			config_file[sizeof(config_file)-1]='\x0';
+
+			/* process the config directory */
+			result=read_config_dir(config_file);
+
+			/* break out if we encountered an error */
+			if(result==ERROR)
+				break;
+		        }
+#endif
+		}
+
+	closedir(dirp);
+
+	return result;
+        }
+
 
 /* adds a new command definition from the config file to the list in memory */
 int add_command(char *command_name, char *command_line){