Improve conf and conf testing

This commit is contained in:
2019-01-06 19:30:35 +02:00
parent 31a9156add
commit 1bad24582e
4 changed files with 48 additions and 37 deletions

View File

@@ -1,15 +1,16 @@
# Error codes # Error codes
100:CONF_DIR_MISSING:"Config directory /etc/rmps is missing!" 100:CONF_DIR_MISSING:"Config directory %s is missing!"
101:CONF_DIR_PERM:"Config dir /etc/rmps cannot be accessed, check permissions!" 101:CONF_DIR_PERM:"Config dir %s cannot be accessed, check permissions!"
102:CONF_DIR_NOTDIR:"It appears that /etc/rmps is a file. Should be a directory!" 102:CONF_DIR_NOTDIR:"It appears that %s is a file. Should be a directory!"
103:CONF_MISSING:"Config file /etc/rmps/rmps.conf is missing!" 103:CONF_MISSING:"Config file %s is missing!"
104:CONF_PERM:"Config file /etc/rmps/rmps.conf cannot be accessed, check permissions!" 104:CONF_PERM:"Config file %s cannot be accessed, check permissions!"
105:CONF_NOTFILE:"It appears that /etc/rmps/rmps.conf is not a regular file!" 105:CONF_NOTFILE:"It appears that %s is not a regular file!"
106:CONF_NOT_READABLE:"Config file %s is not readable!"
# Warning codes # Warning codes
200:CONF_DIR_GUI_INSECURE:"Insecure group for /etc/rmps. Should be 'rmps'!" 200:CONF_DIR_GUI_INSECURE:"Insecure group for %s. Should be 'rmps'!"
201:CONF_DIR_UID_INSECURE:"Insecure owner for /etc/rmps. Should be 'root'!" 201:CONF_DIR_UID_INSECURE:"Insecure owner for %s. Should be 'root'!"
202:CONF_DIR_PERM_INSECURE:"Insecure global permissions for /etc/rmps. Should be 0770!" 202:CONF_DIR_PERM_INSECURE:"Insecure global permissions for %s. Should be 0770!"
203:CONF_FILE_GID_INSECURE:"Insecure group for /etc/rmps/rmps.conf. Should be 'rmps'!" 203:CONF_FILE_GID_INSECURE:"Insecure group for %s. Should be 'rmps'!"
204:CONF_FILE_UID_INSECURE:"Insecure owner for /etc/rmps/rmps.conf. Should be 'root'!" 204:CONF_FILE_UID_INSECURE:"Insecure owner for %s. Should be 'root'!"
205:CONF_FILE_PERM_INSECURE:"Insecure global permissions /etc/rmps/rmps.conf. Shold be 0660!" 205:CONF_FILE_PERM_INSECURE:"Insecure global permissions %s. Should be 0660!"

View File

@@ -166,30 +166,35 @@ static int test_conf_perms(char *config)
if (err == -1) { if (err == -1) {
if (errno == ENOENT) { if (errno == ENOENT) {
enumtostr(confresult, CONF_MISSING); enumtostr(confresult, CONF_MISSING);
log(ERROR, confresult); log(ERROR, confresult, config);
return 1; return 1;
} }
} else { } else {
if (!S_ISREG(s.st_mode)) { if (!S_ISREG(s.st_mode)) {
enumtostr(confresult, CONF_NOTFILE); enumtostr(confresult, CONF_NOTFILE);
log(ERROR, confresult); log(ERROR, confresult, config);
return 1; return 1;
} }
if (!(0400 & s.st_mode)) { if (!(0400 & s.st_mode)) {
enumtostr(confresult, CONF_PERM); 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; return 1;
} }
if (s.st_uid != 0) { if (s.st_uid != 0) {
enumtostr(confresult, CONF_FILE_UID_INSECURE); enumtostr(confresult, CONF_FILE_UID_INSECURE);
log(WARNING, confresult); log(WARNING, confresult, config);
} else if (s.st_gid != 0) { } else if (s.st_gid != 0) {
enumtostr(confresult, CONF_FILE_GID_INSECURE); enumtostr(confresult, CONF_FILE_GID_INSECURE);
log(WARNING, confresult); log(WARNING, confresult, config);
} else if ((0004 & s.st_mode) || } else if ((0004 & s.st_mode) ||
(0002 & s.st_mode)) { (0002 & s.st_mode)) {
enumtostr(confresult, CONF_FILE_PERM_INSECURE); 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 (err == -1) {
if (errno == ENOENT) { if (errno == ENOENT) {
enumtostr(confresult, CONF_DIR_MISSING); enumtostr(confresult, CONF_DIR_MISSING);
log(ERROR, confresult); log(ERROR, confresult, config_copy);
return 1; return 1;
} }
} else { } else {
if (!S_ISDIR(s.st_mode)) { if (!S_ISDIR(s.st_mode)) {
enumtostr(confresult, CONF_DIR_NOTDIR); enumtostr(confresult, CONF_DIR_NOTDIR);
log(ERROR, confresult); log(ERROR, confresult, config_copy);
return 1; return 1;
} }
if (!(0400 & s.st_mode) || if (!(0400 & s.st_mode) ||
!(0100 & s.st_mode)) { !(0100 & s.st_mode)) {
enumtostr(confresult, CONF_DIR_PERM); enumtostr(confresult, CONF_DIR_PERM);
log(ERROR, confresult); log(ERROR, confresult, config_copy);
return 1; return 1;
} }
if (s.st_uid != 0) { if (s.st_uid != 0) {
enumtostr(confresult, CONF_DIR_UID_INSECURE); enumtostr(confresult, CONF_DIR_UID_INSECURE);
log(WARNING, confresult); log(WARNING, confresult, config_copy);
} else if (s.st_gid != 0) { } else if (s.st_gid != 0) {
enumtostr(confresult, CONF_DIR_GID_INSECURE); enumtostr(confresult, CONF_DIR_GID_INSECURE);
log(WARNING, confresult); log(WARNING, confresult, config_copy);
} else if ((0004 & s.st_mode) || } else if ((0004 & s.st_mode) ||
(0002 & s.st_mode)) { (0002 & s.st_mode)) {
enumtostr(confresult, CONF_DIR_PERM_INSECURE); 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")) { } else if (!strcmp(buf, "rmps.logfile")) {
strncpy(conf.rmps.logfile, tmp + 1, strncpy(conf.rmps.logfile, tmp + 1,
sizeof(conf.rmps.logfile) - 1); sizeof(conf.rmps.logfile) - 1);
if (fopen_and_mkdir(conf.rmps.logfile) != 0) /*if (fopen_and_mkdir(conf.rmps.logfile) != 0)
failed = 1; failed = 1;*/
} else if (!strcmp(buf, "rmps.errlog")) { } else if (!strcmp(buf, "rmps.errlog")) {
strncpy(conf.rmps.errlog, tmp + 1, strncpy(conf.rmps.errlog, tmp + 1,
sizeof(conf.rmps.errlog) - 1); sizeof(conf.rmps.errlog) - 1);
if (fopen_and_mkdir(conf.rmps.errlog) != 0) /*if (fopen_and_mkdir(conf.rmps.errlog) != 0)
failed = 1; failed = 1;*/
} else if (!strcmp(buf, "rmps.pidfile")) { } else if (!strcmp(buf, "rmps.pidfile")) {
strncpy(conf.rmps.pidfile, tmp + 1, strncpy(conf.rmps.pidfile, tmp + 1,
sizeof(conf.rmps.pidfile) - 1); sizeof(conf.rmps.pidfile) - 1);
if (fopen_and_mkdir(conf.rmps.pidfile) != 0) /*if (fopen_and_mkdir(conf.rmps.pidfile) != 0)
failed = 1; failed = 1;*/
} else if (!strcmp(buf, "rmps.loglevel")) { } else if (!strcmp(buf, "rmps.loglevel")) {
if (strlen(tmp + 1) == 1 && if (strlen(tmp + 1) == 1 &&
(tmp[1] > '0' && tmp[1] < '5')) (tmp[1] > '0' && tmp[1] < '5'))

View File

@@ -29,6 +29,7 @@ enum ERROR_CODES {
CONF_MISSING, /* 103 */ CONF_MISSING, /* 103 */
CONF_PERM, /* 104 */ CONF_PERM, /* 104 */
CONF_NOTFILE, /* 105 */ CONF_NOTFILE, /* 105 */
CONF_NOT_READABLE, /* 106 */
}; };
enum WARN_CODES { enum WARN_CODES {

View File

@@ -34,9 +34,11 @@ static void usage(char *argv)
{ {
fprintf(stderr, "Usage:\n%s TASK [-c CONFIG] [-d]\n\n" fprintf(stderr, "Usage:\n%s TASK [-c CONFIG] [-d]\n\n"
"Tasks:\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--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" "Options:\n"
"\t-c, --config\n" "\t-c, --config\n"
"\t\tSpecify the configuration file path. Default is set " "\t\tSpecify the configuration file path. Default is set "
@@ -53,10 +55,12 @@ int main(int argc, char *argv[])
enum tasks { enum tasks {
START = 1, START = 1,
STOP, STOP,
RESTART RESTART,
TEST,
TEST_AND_EXPORT
}; };
static int task; static int task = START;
const char opts_short[] = "dc:h"; const char opts_short[] = "dc:htT";
static struct option opts_long[] = static struct option opts_long[] =
{ {
{"start", no_argument, &task, START}, {"start", no_argument, &task, START},
@@ -119,7 +123,7 @@ int main(int argc, char *argv[])
if (task == STOP) if (task == STOP)
log(VERBOSE, "We got a stop signal!"); log(VERBOSE, "We got a stop signal!");
else if (task == RESTART) else /* RESTART */
log(VERBOSE, "We got a restart signal!"); log(VERBOSE, "We got a restart signal!");
fp = fopen(conf.rmps.pidfile, "r"); fp = fopen(conf.rmps.pidfile, "r");