Initial getopt in the project

This commit is contained in:
2019-01-06 01:42:05 +02:00
parent 0382ead79b
commit 31a9156add
3 changed files with 119 additions and 78 deletions

View File

@@ -25,12 +25,13 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include "log.h"
#include "confparser.h"
#include "enum_functions.h"
static int test_conf_perms(void);
static int test_conf_syntax(void);
static int test_conf_perms(char *config);
static int test_conf_syntax(char *config);
struct conf_table conf = {
0, /* isvalid initial state */
@@ -155,11 +156,44 @@ static int fopen_and_mkdir(const char *dir)
}
static int test_conf_perms(void)
static int test_conf_perms(char *config)
{
struct stat s;
char confresult[128];
int err = stat("/etc/rmps", &s);
char *config_copy = strdup(config);
int err = stat(config, &s);
if (err == -1) {
if (errno == ENOENT) {
enumtostr(confresult, CONF_MISSING);
log(ERROR, confresult);
return 1;
}
} else {
if (!S_ISREG(s.st_mode)) {
enumtostr(confresult, CONF_NOTFILE);
log(ERROR, confresult);
return 1;
}
if (!(0400 & s.st_mode)) {
enumtostr(confresult, CONF_PERM);
log(ERROR, confresult);
return 1;
}
if (s.st_uid != 0) {
enumtostr(confresult, CONF_FILE_UID_INSECURE);
log(WARNING, confresult);
} else if (s.st_gid != 0) {
enumtostr(confresult, CONF_FILE_GID_INSECURE);
log(WARNING, confresult);
} else if ((0004 & s.st_mode) ||
(0002 & s.st_mode)) {
enumtostr(confresult, CONF_FILE_PERM_INSECURE);
log(WARNING, confresult);
}
}
err = stat(dirname(config_copy), &s);
if (err == -1) {
if (errno == ENOENT) {
@@ -192,48 +226,17 @@ static int test_conf_perms(void)
}
}
err = stat("/etc/rmps/rmps.conf", &s);
if (err == -1) {
if (errno == ENOENT) {
enumtostr(confresult, CONF_MISSING);
log(ERROR, confresult);
return 1;
}
} else {
if (!S_ISREG(s.st_mode)) {
enumtostr(confresult, CONF_NOTFILE);
log(ERROR, confresult);
return 1;
}
if (!(0400 & s.st_mode)) {
enumtostr(confresult, CONF_PERM);
log(ERROR, confresult);
return 1;
}
if (s.st_uid != 0) {
enumtostr(confresult, CONF_FILE_UID_INSECURE);
log(WARNING, confresult);
} else if (s.st_gid != 0) {
enumtostr(confresult, CONF_FILE_GID_INSECURE);
log(WARNING, confresult);
} else if ((0004 & s.st_mode) ||
(0002 & s.st_mode)) {
enumtostr(confresult, CONF_FILE_PERM_INSECURE);
log(WARNING, confresult);
}
}
return 0; /* conf is readable */
}
static int test_conf_syntax(void)
static int test_conf_syntax(char *config)
{
int i, j = 0, ok = 1, failed = 0;
char buf[CFGLINESIZE], *tmp;
FILE *fp = fopen("/etc/rmps/rmps.conf", "r");
FILE *fp = fopen(config, "r");
if (fp == NULL) {
log(ERROR, "Failed to read /etc/rmps/rmps.conf");
log(ERROR, "Failed to read %s", config);
return 1;
}
@@ -252,8 +255,7 @@ static int test_conf_syntax(void)
*tmp = '\0';
else {
log(ERROR,
"Bad entry in /etc/rmps/rmps.conf, line %d: %s",
j, buf);
"Bad entry in %s, line %d: %s", config, j, buf);
ok = 0;
failed = 1;
continue;
@@ -438,15 +440,15 @@ static int test_conf_syntax(void)
return 0;
}
int confparse(void)
int confparse(char *config)
{
int result;
result = test_conf_perms();
result = test_conf_perms(config);
if (result)
return 1; /* Bad conf perms */
result = test_conf_syntax();
result = test_conf_syntax(config);
if (result != 0)
return 1; /* Bad conf syntax */
return 0; /* seems legit */