RMPS: improve enumtostr() and confparser

This commit is contained in:
2019-01-17 14:31:50 +02:00
parent 85c0e8db3d
commit 0c5cfbe2cb
4 changed files with 50 additions and 36 deletions

View File

@@ -154,37 +154,41 @@ static void init_conf(void)
//conf.nfs = {0}; /* TODO */ //conf.nfs = {0}; /* TODO */
} }
static void log_and_free(char **msg, const char *config, char *mem)
{
log(ERROR, *msg, config);
if (*msg) /* in case enumtostr() fails */
free(*msg);
free(mem);
}
static bool test_conf_perms(char *config) static bool test_conf_perms(char *config)
{ {
struct stat s; struct stat s;
char confresult[128]; char *confresult;
char *config_copy = strdup(config); char *config_copy = strdup(config);
int err = stat(config, &s); int err = stat(config, &s);
if (err == -1) { if (err == -1) {
if (errno == ENOENT) { if (errno == ENOENT) {
enumtostr(confresult, CONF_MISSING); enumtostr(&confresult, CONF_MISSING);
log(ERROR, confresult, config); log_and_free(&confresult, config, config_copy);
free(config_copy);
return false; return false;
} }
} 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, config); log_and_free(&confresult, config, config_copy);
free(config_copy);
return false; return false;
} }
if (!(0400 & s.st_mode)) { if (!(0400 & s.st_mode)) {
enumtostr(confresult, CONF_PERM); enumtostr(&confresult, CONF_PERM);
log(ERROR, confresult, config); log_and_free(&confresult, config, config_copy);
free(config_copy);
return false; return false;
} }
if (access(config, R_OK) != 0) { if (access(config, R_OK) != 0) {
enumtostr(confresult, CONF_NOT_READABLE); enumtostr(&confresult, CONF_NOT_READABLE);
log(ERROR, confresult, config); log_and_free(&confresult, config, config_copy);
free(config_copy);
return false; return false;
} }
} }
@@ -193,23 +197,20 @@ static bool 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, config_copy); log_and_free(&confresult, config, config_copy);
free(config_copy);
return false; return false;
} }
} 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, config_copy); log_and_free(&confresult, config, config_copy);
free(config_copy);
return false; return false;
} }
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, config_copy); log_and_free(&confresult, config, config_copy);
free(config_copy);
return false; return false;
} }
} }

View File

@@ -19,35 +19,48 @@
* along with RMPS. If not, see <http://www.gnu.org/licenses/>. * along with RMPS. If not, see <http://www.gnu.org/licenses/>.
*/ */
#define _GNU_SOURCE
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <stdbool.h>
#include "enum_functions.h" #include "enum_functions.h"
#include "log.h" #include "log.h"
void enumtostr(char *scode, int code) void enumtostr(char **meaning, int code)
{ {
char line[128]; char *line = NULL, *val_ptr;
size_t len = 0;
ssize_t line_len;
FILE *fp; FILE *fp;
int bytes = 0; bool found = false;
snprintf(scode, 10, "%d", code); if (asprintf(meaning, "%d", code) == -1) {
log(ERROR, "asprintf() in enumtostr() OOM!");
free(*meaning);
return;
}
fp = fopen("/usr/lib/rmps/resources/enum_codes", "r"); fp = fopen("/usr/lib/rmps/resources/enum_codes", "r");
if (fp == NULL) { if (fp == NULL) {
log(ERROR, "Failed to fetch error enum code!"); log(ERROR, "Failed to fetch error enum code!");
return; return;
} }
while (fgets(line, sizeof(line), fp) != NULL) while ((line_len = getline(&line, &len, fp)) != -1) {
if (strstr(line, scode) != NULL) { if (line[line_len-2] == '"')
char *byte; line[line_len-2] = '\0';
if ((val_ptr = strstr(line, *meaning)) != NULL) {
bytes = sprintf(scode, "%s", line); found = true;
byte = memchr(scode, '\n', bytes); val_ptr = strchr(line, '"') + 1;
byte[-1] = '\0'; asprintf(meaning, "%s", val_ptr);
break; break;
} }
strncpy(scode, (char *)memchr(scode, '\"', bytes) + 1, bytes); }
if (!found)
asprintf(meaning, "Unknown enum_code: %d", code);
if (line)
free(line);
fclose(fp); fclose(fp);
} }

View File

@@ -41,6 +41,6 @@ enum WARN_CODES {
CONF_FILE_PERM_INSECURE, CONF_FILE_PERM_INSECURE,
}; };
extern void enumtostr(char *scode, int code); extern void enumtostr(char **meaning, int code);
#endif /* ENUM_FUNCTIONS_H */ #endif /* ENUM_FUNCTIONS_H */

View File

@@ -106,7 +106,7 @@ int main(int argc, char *argv[])
config = calloc(sizeof(char), path_size); config = calloc(sizeof(char), path_size);
if (config == NULL) { if (config == NULL) {
fprintf(stderr, "malloc() failed for config\n"); fprintf(stderr, "calloc() failed for config\n");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
memcpy(config, path, path_size); memcpy(config, path, path_size);