From 1bad24582e4447c24eb90ac5ad1460082acda871 Mon Sep 17 00:00:00 2001 From: Bogomil Vasilev Date: Sun, 6 Jan 2019 19:30:35 +0200 Subject: [PATCH] Improve conf and conf testing --- docs/enum_codes | 25 +++++++++++++------------ src/confparser.c | 41 +++++++++++++++++++++++------------------ src/enum_functions.h | 1 + src/main.c | 18 +++++++++++------- 4 files changed, 48 insertions(+), 37 deletions(-) diff --git a/docs/enum_codes b/docs/enum_codes index 31ecdb8..c99574f 100644 --- a/docs/enum_codes +++ b/docs/enum_codes @@ -1,15 +1,16 @@ # Error codes -100:CONF_DIR_MISSING:"Config directory /etc/rmps is missing!" -101:CONF_DIR_PERM:"Config dir /etc/rmps cannot be accessed, check permissions!" -102:CONF_DIR_NOTDIR:"It appears that /etc/rmps is a file. Should be a directory!" -103:CONF_MISSING:"Config file /etc/rmps/rmps.conf is missing!" -104:CONF_PERM:"Config file /etc/rmps/rmps.conf cannot be accessed, check permissions!" -105:CONF_NOTFILE:"It appears that /etc/rmps/rmps.conf is not a regular file!" +100:CONF_DIR_MISSING:"Config directory %s is missing!" +101:CONF_DIR_PERM:"Config dir %s cannot be accessed, check permissions!" +102:CONF_DIR_NOTDIR:"It appears that %s is a file. Should be a directory!" +103:CONF_MISSING:"Config file %s is missing!" +104:CONF_PERM:"Config file %s cannot be accessed, check permissions!" +105:CONF_NOTFILE:"It appears that %s is not a regular file!" +106:CONF_NOT_READABLE:"Config file %s is not readable!" # Warning codes -200:CONF_DIR_GUI_INSECURE:"Insecure group for /etc/rmps. Should be 'rmps'!" -201:CONF_DIR_UID_INSECURE:"Insecure owner for /etc/rmps. Should be 'root'!" -202:CONF_DIR_PERM_INSECURE:"Insecure global permissions for /etc/rmps. Should be 0770!" -203:CONF_FILE_GID_INSECURE:"Insecure group for /etc/rmps/rmps.conf. Should be 'rmps'!" -204:CONF_FILE_UID_INSECURE:"Insecure owner for /etc/rmps/rmps.conf. Should be 'root'!" -205:CONF_FILE_PERM_INSECURE:"Insecure global permissions /etc/rmps/rmps.conf. Shold be 0660!" +200:CONF_DIR_GUI_INSECURE:"Insecure group for %s. Should be 'rmps'!" +201:CONF_DIR_UID_INSECURE:"Insecure owner for %s. Should be 'root'!" +202:CONF_DIR_PERM_INSECURE:"Insecure global permissions for %s. Should be 0770!" +203:CONF_FILE_GID_INSECURE:"Insecure group for %s. Should be 'rmps'!" +204:CONF_FILE_UID_INSECURE:"Insecure owner for %s. Should be 'root'!" +205:CONF_FILE_PERM_INSECURE:"Insecure global permissions %s. Should be 0660!" diff --git a/src/confparser.c b/src/confparser.c index 132a814..8dcbfd1 100644 --- a/src/confparser.c +++ b/src/confparser.c @@ -166,30 +166,35 @@ static int test_conf_perms(char *config) if (err == -1) { if (errno == ENOENT) { enumtostr(confresult, CONF_MISSING); - log(ERROR, confresult); + log(ERROR, confresult, config); return 1; } } else { if (!S_ISREG(s.st_mode)) { enumtostr(confresult, CONF_NOTFILE); - log(ERROR, confresult); + log(ERROR, confresult, config); return 1; } if (!(0400 & s.st_mode)) { enumtostr(confresult, CONF_PERM); - log(ERROR, confresult); + log(ERROR, confresult, config); + return 1; + } + if (access(config, R_OK) != 0) { + enumtostr(confresult, CONF_NOT_READABLE); + log(ERROR, confresult, config); return 1; } if (s.st_uid != 0) { enumtostr(confresult, CONF_FILE_UID_INSECURE); - log(WARNING, confresult); + log(WARNING, confresult, config); } else if (s.st_gid != 0) { enumtostr(confresult, CONF_FILE_GID_INSECURE); - log(WARNING, confresult); + log(WARNING, confresult, config); } else if ((0004 & s.st_mode) || (0002 & s.st_mode)) { enumtostr(confresult, CONF_FILE_PERM_INSECURE); - log(WARNING, confresult); + log(WARNING, confresult, config); } } @@ -198,31 +203,31 @@ static int test_conf_perms(char *config) if (err == -1) { if (errno == ENOENT) { enumtostr(confresult, CONF_DIR_MISSING); - log(ERROR, confresult); + log(ERROR, confresult, config_copy); return 1; } } else { if (!S_ISDIR(s.st_mode)) { enumtostr(confresult, CONF_DIR_NOTDIR); - log(ERROR, confresult); + log(ERROR, confresult, config_copy); return 1; } if (!(0400 & s.st_mode) || !(0100 & s.st_mode)) { enumtostr(confresult, CONF_DIR_PERM); - log(ERROR, confresult); + log(ERROR, confresult, config_copy); return 1; } if (s.st_uid != 0) { enumtostr(confresult, CONF_DIR_UID_INSECURE); - log(WARNING, confresult); + log(WARNING, confresult, config_copy); } else if (s.st_gid != 0) { enumtostr(confresult, CONF_DIR_GID_INSECURE); - log(WARNING, confresult); + log(WARNING, confresult, config_copy); } else if ((0004 & s.st_mode) || (0002 & s.st_mode)) { enumtostr(confresult, CONF_DIR_PERM_INSECURE); - log(WARNING, confresult); + log(WARNING, confresult, config_copy); } } @@ -347,18 +352,18 @@ static int test_conf_syntax(char *config) } else if (!strcmp(buf, "rmps.logfile")) { strncpy(conf.rmps.logfile, tmp + 1, sizeof(conf.rmps.logfile) - 1); - if (fopen_and_mkdir(conf.rmps.logfile) != 0) - failed = 1; + /*if (fopen_and_mkdir(conf.rmps.logfile) != 0) + failed = 1;*/ } else if (!strcmp(buf, "rmps.errlog")) { strncpy(conf.rmps.errlog, tmp + 1, sizeof(conf.rmps.errlog) - 1); - if (fopen_and_mkdir(conf.rmps.errlog) != 0) - failed = 1; + /*if (fopen_and_mkdir(conf.rmps.errlog) != 0) + failed = 1;*/ } else if (!strcmp(buf, "rmps.pidfile")) { strncpy(conf.rmps.pidfile, tmp + 1, sizeof(conf.rmps.pidfile) - 1); - if (fopen_and_mkdir(conf.rmps.pidfile) != 0) - failed = 1; + /*if (fopen_and_mkdir(conf.rmps.pidfile) != 0) + failed = 1;*/ } else if (!strcmp(buf, "rmps.loglevel")) { if (strlen(tmp + 1) == 1 && (tmp[1] > '0' && tmp[1] < '5')) diff --git a/src/enum_functions.h b/src/enum_functions.h index 1921119..2b62d95 100644 --- a/src/enum_functions.h +++ b/src/enum_functions.h @@ -29,6 +29,7 @@ enum ERROR_CODES { CONF_MISSING, /* 103 */ CONF_PERM, /* 104 */ CONF_NOTFILE, /* 105 */ + CONF_NOT_READABLE, /* 106 */ }; enum WARN_CODES { diff --git a/src/main.c b/src/main.c index a59fd41..48aa23d 100644 --- a/src/main.c +++ b/src/main.c @@ -34,9 +34,11 @@ static void usage(char *argv) { fprintf(stderr, "Usage:\n%s TASK [-c CONFIG] [-d]\n\n" "Tasks:\n" - "\t--start\t\tStart the RMPS server.\n" + "\t--start\t\tStart the RMPS server. This is by default. \n" "\t--stop\t\tStop the RMPS server.\n" - "\t--restart\tRestart the RMPS server.\n\n" + "\t--restart\tRestart the RMPS server.\n" + "\t-t, --test\tTest the configuration file and exit,\n" + "\t-T\t\tTest the configuration file, dump it to stdout and exit.\n" "Options:\n" "\t-c, --config\n" "\t\tSpecify the configuration file path. Default is set " @@ -53,10 +55,12 @@ int main(int argc, char *argv[]) enum tasks { START = 1, STOP, - RESTART + RESTART, + TEST, + TEST_AND_EXPORT }; - static int task; - const char opts_short[] = "dc:h"; + static int task = START; + const char opts_short[] = "dc:htT"; static struct option opts_long[] = { {"start", no_argument, &task, START}, @@ -112,14 +116,14 @@ int main(int argc, char *argv[]) log(VERBOSE, "Conf parser finished successfully"); //confexport(); - if (task == STOP|| task == RESTART) { + if (task == STOP || task == RESTART) { char buf[10]; int pid; FILE *fp; if (task == STOP) log(VERBOSE, "We got a stop signal!"); - else if (task == RESTART) + else /* RESTART */ log(VERBOSE, "We got a restart signal!"); fp = fopen(conf.rmps.pidfile, "r");