RMPS: rework confparser, much much better!
This commit is contained in:
421
src/confparser.c
421
src/confparser.c
@@ -18,7 +18,7 @@
|
|||||||
* You should have received a copy of the GNU General Public License
|
* You should have received a copy of the GNU General Public License
|
||||||
* 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 <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -30,39 +30,11 @@
|
|||||||
#include "confparser.h"
|
#include "confparser.h"
|
||||||
#include "enum_functions.h"
|
#include "enum_functions.h"
|
||||||
|
|
||||||
static int test_conf_perms(char *config);
|
static bool test_conf_perms(char *config);
|
||||||
static int test_conf_syntax(char *config);
|
static bool test_conf_syntax(char *config);
|
||||||
|
static bool test_conf_required(void);
|
||||||
|
|
||||||
struct conf_table conf = {
|
struct conf_table conf;
|
||||||
0, /* isvalid initial state */
|
|
||||||
{
|
|
||||||
"", /* db.type */
|
|
||||||
"", /* db.hostname */
|
|
||||||
"", /* db.port */
|
|
||||||
"" /* db.pass */
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"127.0.0.1", /* rmps.agent_ip */
|
|
||||||
"7000", /* rmps.agent_port */
|
|
||||||
"127.0.0.1", /* rmps.client_ip */
|
|
||||||
"7001", /* rmps.client_port */
|
|
||||||
"/var/log/rmps/rmpsd.log",
|
|
||||||
"/var/log/rmps/rmpsd.err",
|
|
||||||
'2', /* rmps.loglevel */
|
|
||||||
"/run/rmps/rmpsd.pid",
|
|
||||||
"/etc/rmps/agent.crt",
|
|
||||||
"/etc/rmps/agent.key",
|
|
||||||
"/etc/rmps/ca.crt",
|
|
||||||
"", /* rmps.cipherlist */
|
|
||||||
2, /* rmps.agent_poolsize */
|
|
||||||
"/etc/rmps/client.crt",
|
|
||||||
"/etc/rmps/client.key",
|
|
||||||
2 /* rmps.client_poolsize */
|
|
||||||
},
|
|
||||||
{
|
|
||||||
0 /* nfs -> TODO */
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const char *conf_db_pass(void)
|
const char *conf_db_pass(void)
|
||||||
{
|
{
|
||||||
@@ -142,21 +114,47 @@ static int fopen_and_mkdir(const char *dir)
|
|||||||
log(ERROR,
|
log(ERROR,
|
||||||
"Permission denied to create dir: %s",
|
"Permission denied to create dir: %s",
|
||||||
tmp);
|
tmp);
|
||||||
return 1;
|
return false;
|
||||||
}
|
}
|
||||||
*p = '/';
|
*p = '/';
|
||||||
}
|
}
|
||||||
fp = fopen(dir, "a");
|
fp = fopen(dir, "a");
|
||||||
if (!fp) {
|
if (!fp) {
|
||||||
log(ERROR, "Permission denied to write into: %s", dir);
|
log(ERROR, "Permission denied to write into: %s", dir);
|
||||||
return 1;
|
return false;
|
||||||
}
|
}
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
return 0;
|
return true;
|
||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static int test_conf_perms(char *config)
|
static void init_conf(void)
|
||||||
|
{
|
||||||
|
conf.isvalid = 0; /* initial state */
|
||||||
|
asprintf(&conf.db.type, "mysql");
|
||||||
|
conf.db.hostname[0] = '\0';
|
||||||
|
conf.db.port = NULL;
|
||||||
|
conf.db.pass = NULL;
|
||||||
|
asprintf(&conf.rmps.agent_ip, "any");
|
||||||
|
asprintf(&conf.rmps.agent_port, "7000");
|
||||||
|
asprintf(&conf.rmps.client_ip, "any");
|
||||||
|
asprintf(&conf.rmps.client_port, "7001");
|
||||||
|
asprintf(&conf.rmps.logfile, "/var/log/rmps/rmpsd.log");
|
||||||
|
asprintf(&conf.rmps.errlog, "/var/log/rmps/rmpsd.err");
|
||||||
|
conf.rmps.loglevel = 2; /* WARNING == 2 */
|
||||||
|
asprintf(&conf.rmps.pidfile, "/run/rmps/rmpsd.pid");
|
||||||
|
asprintf(&conf.rmps.agent_tls_crt, "/etc/rmps/certs/server.crt");
|
||||||
|
asprintf(&conf.rmps.agent_tls_key, "/etc/rmps/certs/server.key");
|
||||||
|
asprintf(&conf.rmps.cafile, "/etc/rmps/certs/ca.crt");
|
||||||
|
conf.rmps.cipherlist = NULL;
|
||||||
|
conf.rmps.agent_poolsize = 2;
|
||||||
|
asprintf(&conf.rmps.client_tls_crt, "/etc/rmps/certs/server.crt");
|
||||||
|
asprintf(&conf.rmps.client_tls_key, "/etc/rmps/certs/server.key");
|
||||||
|
conf.rmps.client_poolsize = 2;
|
||||||
|
//conf.nfs = {0}; /* TODO */
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool test_conf_perms(char *config)
|
||||||
{
|
{
|
||||||
struct stat s;
|
struct stat s;
|
||||||
char confresult[128];
|
char confresult[128];
|
||||||
@@ -168,26 +166,26 @@ static int test_conf_perms(char *config)
|
|||||||
enumtostr(confresult, CONF_MISSING);
|
enumtostr(confresult, CONF_MISSING);
|
||||||
log(ERROR, confresult, config);
|
log(ERROR, confresult, config);
|
||||||
free(config_copy);
|
free(config_copy);
|
||||||
return 1;
|
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(ERROR, confresult, config);
|
||||||
free(config_copy);
|
free(config_copy);
|
||||||
return 1;
|
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(ERROR, confresult, config);
|
||||||
free(config_copy);
|
free(config_copy);
|
||||||
return 1;
|
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(ERROR, confresult, config);
|
||||||
free(config_copy);
|
free(config_copy);
|
||||||
return 1;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -198,257 +196,266 @@ static int test_conf_perms(char *config)
|
|||||||
enumtostr(confresult, CONF_DIR_MISSING);
|
enumtostr(confresult, CONF_DIR_MISSING);
|
||||||
log(ERROR, confresult, config_copy);
|
log(ERROR, confresult, config_copy);
|
||||||
free(config_copy);
|
free(config_copy);
|
||||||
return 1;
|
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(ERROR, confresult, config_copy);
|
||||||
free(config_copy);
|
free(config_copy);
|
||||||
return 1;
|
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(ERROR, confresult, config_copy);
|
||||||
free(config_copy);
|
free(config_copy);
|
||||||
return 1;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(config_copy);
|
free(config_copy);
|
||||||
|
return true; /* conf is readable */
|
||||||
return 0; /* conf is readable */
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static int test_conf_syntax(char *config)
|
/* Import the config and test the syntax */
|
||||||
|
static bool test_conf_syntax(char *config)
|
||||||
{
|
{
|
||||||
int i, j = 0, ok = 1, failed = 0;
|
int i, line_itr = 0;
|
||||||
char buf[CFGLINESIZE], *tmp;
|
bool failed = false, val_ok = true;
|
||||||
|
char *line = NULL, *val_ptr;
|
||||||
|
size_t len = 0;
|
||||||
FILE *fp = fopen(config, "r");
|
FILE *fp = fopen(config, "r");
|
||||||
|
|
||||||
if (fp == NULL) {
|
if (fp == NULL) {
|
||||||
log(ERROR, "Failed to read %s", config);
|
log(ERROR, "Failed to read %s", config);
|
||||||
return 1;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (fgets(buf, CFGLINESIZE, fp) != NULL) {
|
while (getline(&line, &len, fp) != -1) {
|
||||||
j++;
|
line_itr++;
|
||||||
/* kill comments and ignore BLANK lines */
|
/* kill comments and ignore BLANK lines */
|
||||||
tmp = strstr(buf, "#");
|
val_ptr = strstr(line, "#");
|
||||||
if (tmp)
|
if (val_ptr)
|
||||||
*tmp = '\0';
|
*val_ptr = '\0';
|
||||||
if (buf[strspn(buf, " \t\v\r\n")] == '\0')
|
if (line[strspn(line, " \t\v\r\n")] == '\0')
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
/* If we have "=", it's a possible var */
|
/* If we have "=", it's a possible var */
|
||||||
tmp = strstr(buf, "=");
|
val_ptr = strstr(line, "=");
|
||||||
if (tmp)
|
if (val_ptr)
|
||||||
*tmp = '\0';
|
*val_ptr = '\0';
|
||||||
else {
|
else {
|
||||||
log(ERROR,
|
log(ERROR,
|
||||||
"Bad entry in %s, line %d: %s", config, j, buf);
|
"Bad entry in %s, line %d: %s", config, line_itr, line);
|
||||||
ok = 0;
|
val_ok = false;
|
||||||
failed = 1;
|
failed = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
/* Check if there actually is a value after '=' */
|
/* Check if there actually is a value after '=' */
|
||||||
i = strlen(tmp + 1);
|
i = strlen(val_ptr + 1);
|
||||||
if (tmp[i] == '\n')
|
if (val_ptr[i] == '\n') {
|
||||||
tmp[i] = '\0';
|
val_ptr[i] = '\0';
|
||||||
if (tmp[strspn(tmp + 1, " \t\v\r\n") + 1] == '\0') {
|
/* move ptr to beginning of the value str */
|
||||||
|
val_ptr = val_ptr + 1;
|
||||||
|
}
|
||||||
|
if (val_ptr[strspn(val_ptr, " \t\v\r\n")] == '\0') {
|
||||||
log(ERROR,
|
log(ERROR,
|
||||||
"Specified entry without value, line %d: %s",
|
"Specified entry without value, line %d: %s",
|
||||||
j, buf);
|
line_itr, line);
|
||||||
failed = 1;
|
failed = true;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Here we check every single conf entry manually */
|
/* Here we check every single conf entry manually */
|
||||||
if (!strcmp(buf, "db.type")) {
|
if (!strcmp(line, "db.type")) {
|
||||||
if (!strcmp(tmp + 1, "mysql")) {
|
if (!strcmp(val_ptr, "mysql")) {
|
||||||
/* || !strcmp(tmp[1], "postgresql") */
|
/* || !strcmp(val_ptr[1], "postgresql") */
|
||||||
/* || !strcmp(tmp[1], "oracle") */
|
/* || !strcmp(val_ptr[1], "oracle") */
|
||||||
strncpy(conf.db.type, tmp + 1,
|
asprintf(&conf.db.type, "%s", val_ptr);
|
||||||
sizeof(conf.db.type) - 1);
|
if (!conf.db.port)
|
||||||
if (conf.db.port[0] == '\0')
|
asprintf(&conf.db.port, "%s", val_ptr);
|
||||||
strncpy(conf.db.port, "3306",
|
|
||||||
sizeof(conf.db.port) - 1);
|
|
||||||
} else {
|
} else {
|
||||||
ok = 0;
|
val_ok = false;
|
||||||
log(ERROR, "Invalid db.type: %s", tmp + 1);
|
log(ERROR, "Invalid db.type: %s", "%s", val_ptr);
|
||||||
failed = 1;
|
failed = true;
|
||||||
}
|
}
|
||||||
} else if (!strcmp(buf, "db.hostname"))
|
} else if (!strcmp(line, "db.hostname"))
|
||||||
/* Just save it, launch_rmps will check it */
|
/* Just save it, launch_rmps will check it */
|
||||||
strncpy(conf.db.hostname, tmp + 1,
|
strncpy(conf.db.hostname, val_ptr, HOSTNAMESIZE - 1);
|
||||||
sizeof(conf.db.hostname) - 1);
|
else if (!strcmp(line, "db.port")) {
|
||||||
else if (!strcmp(buf, "db.port")) {
|
i = strlen(val_ptr);
|
||||||
i = strlen(tmp + 1);
|
|
||||||
if (i < 6) { /* max 5 digits for network port */
|
if (i < 6) { /* max 5 digits for network port */
|
||||||
if ((signed int)strspn(tmp + 1,
|
if ((signed int)strspn(val_ptr, "1234567890") == i) {
|
||||||
"1234567890") == i) {
|
i = atoi(val_ptr);
|
||||||
i = atoi(tmp + 1);
|
|
||||||
if (i > 0 && i < 65536) {
|
if (i > 0 && i < 65536) {
|
||||||
strncpy(conf.db.port, tmp + 1,
|
asprintf(&conf.db.port, "%s", val_ptr);
|
||||||
sizeof(conf.db.port)-1);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log(ERROR, "Invalid db.port value: %s", tmp + 1);
|
log(ERROR, "Invalid db.port value: %s", val_ptr);
|
||||||
ok = 0;
|
val_ok = false;
|
||||||
failed = 1;
|
failed = true;
|
||||||
} else if (!strcmp(buf, "db.pass"))
|
} else if (!strcmp(line, "db.pass"))
|
||||||
strncpy(conf.db.pass, tmp + 1,
|
asprintf(&conf.db.pass, "%s", val_ptr);
|
||||||
sizeof(conf.db.pass) - 1);
|
else if (!strcmp(line, "rmps.agent_ip")) {
|
||||||
else if (!strcmp(buf, "rmps.agent_ip")) {
|
|
||||||
/* TODO */
|
/* TODO */
|
||||||
} else if (!strcmp(buf, "rmps.agent_port")) {
|
} else if (!strcmp(line, "rmps.agent_port")) {
|
||||||
i = strlen(tmp + 1);
|
i = strlen(val_ptr);
|
||||||
if (i < 6) { /* max 5 digits for network port */
|
if (i < 6) { /* max 5 digits for network port */
|
||||||
if ((signed int)strspn(tmp + 1,
|
if ((signed int)strspn(val_ptr,
|
||||||
"1234567890") == i) {
|
"1234567890") == i) {
|
||||||
i = atoi(tmp + 1);
|
i = atoi(val_ptr);
|
||||||
if (i > 0 && i < 65536) {
|
if (i > 0 && i < 65536) {
|
||||||
strncpy(conf.rmps.agent_port,
|
asprintf(&conf.rmps.agent_port, "%s", val_ptr);
|
||||||
tmp + 1,
|
|
||||||
sizeof(conf.rmps.agent_port) - 1);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log(ERROR, "Invalid rmps.agent_port value: %s", tmp + 1);
|
log(ERROR, "Invalid rmps.agent_port value: %s", val_ptr);
|
||||||
ok = 0;
|
val_ok = false;
|
||||||
failed = 1;
|
failed = true;
|
||||||
} else if (!strcmp(buf, "rmps.client_ip")) {
|
} else if (!strcmp(line, "rmps.client_ip")) {
|
||||||
/* TODO */
|
/* TODO */
|
||||||
} else if (!strcmp(buf, "rmps.client_port")) {
|
} else if (!strcmp(line, "rmps.client_port")) {
|
||||||
i = strlen(tmp + 1);
|
i = strlen(val_ptr);
|
||||||
if (i < 6) { /* max 5 digits for network port */
|
if (i < 6) { /* max 5 digits for network port */
|
||||||
if ((signed int)strspn(tmp + 1,
|
if ((signed int)strspn(val_ptr, "1234567890") == i) {
|
||||||
"1234567890") == i) {
|
i = atoi(val_ptr);
|
||||||
i = atoi(tmp + 1);
|
|
||||||
if (i > 0 && i < 65536) {
|
if (i > 0 && i < 65536) {
|
||||||
strncpy(conf.rmps.client_port,
|
asprintf(&conf.rmps.client_port, "%s", val_ptr);
|
||||||
tmp + 1,
|
|
||||||
sizeof(conf.rmps.client_port) - 1);
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log(ERROR, "Invalid rmps.client_port value: %s", tmp + 1);
|
log(ERROR, "Invalid rmps.client_port value: %s", val_ptr);
|
||||||
ok = 0;
|
val_ok = false;
|
||||||
failed = 1;
|
failed = true;
|
||||||
} else if (!strcmp(buf, "rmps.logfile")) {
|
} else if (!strcmp(line, "rmps.logfile")) {
|
||||||
strncpy(conf.rmps.logfile, tmp + 1,
|
asprintf(&conf.rmps.logfile, "%s", val_ptr);
|
||||||
sizeof(conf.rmps.logfile) - 1);
|
|
||||||
/*if (fopen_and_mkdir(conf.rmps.logfile) != 0)
|
/*if (fopen_and_mkdir(conf.rmps.logfile) != 0)
|
||||||
failed = 1;*/
|
failed = true;*/
|
||||||
} else if (!strcmp(buf, "rmps.errlog")) {
|
} else if (!strcmp(line, "rmps.errlog")) {
|
||||||
strncpy(conf.rmps.errlog, tmp + 1,
|
asprintf(&conf.rmps.errlog, "%s", val_ptr);
|
||||||
sizeof(conf.rmps.errlog) - 1);
|
|
||||||
/*if (fopen_and_mkdir(conf.rmps.errlog) != 0)
|
/*if (fopen_and_mkdir(conf.rmps.errlog) != 0)
|
||||||
failed = 1;*/
|
failed = true;*/
|
||||||
} else if (!strcmp(buf, "rmps.pidfile")) {
|
} else if (!strcmp(line, "rmps.pidfile")) {
|
||||||
strncpy(conf.rmps.pidfile, tmp + 1,
|
asprintf(&conf.rmps.pidfile, "%s", val_ptr);
|
||||||
sizeof(conf.rmps.pidfile) - 1);
|
|
||||||
/*if (fopen_and_mkdir(conf.rmps.pidfile) != 0)
|
/*if (fopen_and_mkdir(conf.rmps.pidfile) != 0)
|
||||||
failed = 1;*/
|
failed = true;*/
|
||||||
} else if (!strcmp(buf, "rmps.loglevel")) {
|
} else if (!strcmp(line, "rmps.loglevel")) {
|
||||||
if (strlen(tmp + 1) == 1 &&
|
if (strlen(val_ptr) == 1 &&
|
||||||
(tmp[1] > '0' && tmp[1] < '5'))
|
(val_ptr[0] > '0' && val_ptr[0] < '5'))
|
||||||
conf.rmps.loglevel = tmp[1] - '0';
|
conf.rmps.loglevel = val_ptr[0] - '0';
|
||||||
else {
|
else {
|
||||||
log(ERROR, "Invalid loglevel: %s", tmp + 1);
|
log(ERROR, "Invalid rmps.loglevel: %s", val_ptr);
|
||||||
failed = 1;
|
failed = true;
|
||||||
}
|
}
|
||||||
} else if (!strcmp(buf, "rmps.agent_tls_crt")) {
|
} else if (!strcmp(line, "rmps.agent_tls_crt")) {
|
||||||
if (access(tmp + 1, F_OK) == -1) {
|
if (access(val_ptr, F_OK) == -1) {
|
||||||
log(ERROR, "%s is missing", tmp + 1);
|
log(ERROR, "%s is missing", val_ptr);
|
||||||
failed = 1;
|
failed = true;
|
||||||
} else if (access(tmp + 1, R_OK) == -1) {
|
} else if (access(val_ptr, R_OK) == -1) {
|
||||||
log(ERROR, "%s is not readable", tmp + 1);
|
log(ERROR, "%s is not readable", val_ptr);
|
||||||
failed = 1;
|
failed = true;
|
||||||
} else
|
} else
|
||||||
strncpy(conf.rmps.agent_tls_crt,
|
asprintf(&conf.rmps.agent_tls_crt, "%s", val_ptr);
|
||||||
tmp + 1,
|
} else if (!strcmp(line, "rmps.agent_tls_key")) {
|
||||||
sizeof(conf.rmps.agent_tls_crt) - 1);
|
if (access(val_ptr, F_OK) == -1) {
|
||||||
} else if (!strcmp(buf, "rmps.agent_tls_key")) {
|
log(ERROR, "%s is missing", val_ptr);
|
||||||
if (access(tmp + 1, F_OK) == -1) {
|
failed = true;
|
||||||
log(ERROR, "%s is missing", tmp + 1);
|
} else if (access(val_ptr, R_OK) == -1) {
|
||||||
failed = 1;
|
log(ERROR, "%s is not readable", val_ptr);
|
||||||
} else if (access(tmp + 1, R_OK) == -1) {
|
failed = true;
|
||||||
log(ERROR, "%s is not readable", tmp + 1);
|
|
||||||
failed = 1;
|
|
||||||
} else
|
} else
|
||||||
strncpy(conf.rmps.agent_tls_key,
|
asprintf(&conf.rmps.agent_tls_key, "%s", val_ptr);
|
||||||
tmp + 1,
|
} else if (!strcmp(line, "rmps.cipherlist")) {
|
||||||
sizeof(conf.rmps.agent_tls_key) - 1);
|
asprintf(&conf.rmps.cipherlist, "%s", val_ptr);
|
||||||
} else if (!strcmp(buf, "rmps.cipherlist")) {
|
} else if (!strcmp(line, "rmps.cafile")) {
|
||||||
strncpy(conf.rmps.cipherlist,
|
if (access(val_ptr, F_OK) == -1) {
|
||||||
tmp + 1, sizeof(conf.rmps.cipherlist) - 1);
|
log(ERROR, "%s is missing", val_ptr);
|
||||||
} else if (!strcmp(buf, "rmps.cafile")) {
|
failed = true;
|
||||||
if (access(tmp + 1, F_OK) == -1) {
|
} else if (access(val_ptr, R_OK) == -1) {
|
||||||
log(ERROR, "%s is missing", tmp + 1);
|
log(ERROR, "%s is not readable\n", val_ptr);
|
||||||
failed = 1;
|
failed = true;
|
||||||
} else if (access(tmp + 1, R_OK) == -1) {
|
|
||||||
log(ERROR, "%s is not readable\n", tmp + 1);
|
|
||||||
failed = 1;
|
|
||||||
} else
|
} else
|
||||||
strncpy(conf.rmps.cafile,
|
asprintf(&conf.rmps.cafile, "%s", val_ptr);
|
||||||
tmp + 1, sizeof(conf.rmps.cafile) - 1);
|
} else if (!strcmp(line, "rmps.client_tls_crt")) {
|
||||||
} else if (!strcmp(buf, "rmps.client_tls_crt")) {
|
if (access(val_ptr, F_OK) == -1) {
|
||||||
if (access(tmp + 1, F_OK) == -1) {
|
log(ERROR, "%s is missing", val_ptr);
|
||||||
log(ERROR, "%s is missing", tmp + 1);
|
failed = true;
|
||||||
failed = 1;
|
} else if (access(val_ptr, R_OK) == -1) {
|
||||||
} else if (access(tmp + 1, R_OK) == -1) {
|
log(ERROR, "%s is not readable", val_ptr);
|
||||||
log(ERROR, "%s is not readable", tmp + 1);
|
failed = true;
|
||||||
failed = 1;
|
|
||||||
} else
|
} else
|
||||||
strncpy(conf.rmps.client_tls_crt,
|
asprintf(&conf.rmps.client_tls_crt, "%s", val_ptr);
|
||||||
tmp + 1,
|
} else if (!strcmp(line, "rmps.client_tls_key")) {
|
||||||
sizeof(conf.rmps.client_tls_crt) - 1);
|
if (access(val_ptr, F_OK) == -1) {
|
||||||
} else if (!strcmp(buf, "rmps.client_tls_key")) {
|
log(ERROR, "%s is missing", val_ptr);
|
||||||
if (access(tmp + 1, F_OK) == -1) {
|
failed = true;
|
||||||
log(ERROR, "%s is missing", tmp + 1);
|
} else if (access(val_ptr, R_OK) == -1) {
|
||||||
failed = 1;
|
log(ERROR, "%s is not readable", val_ptr);
|
||||||
} else if (access(tmp + 1, R_OK) == -1) {
|
failed = true;
|
||||||
log(ERROR, "%s is not readable", tmp + 1);
|
|
||||||
failed = 1;
|
|
||||||
} else
|
} else
|
||||||
strncpy(conf.rmps.client_tls_key,
|
asprintf(&conf.rmps.client_tls_key, "%s", val_ptr);
|
||||||
tmp + 1,
|
|
||||||
sizeof(conf.rmps.client_tls_key) - 1);
|
|
||||||
} else {
|
} else {
|
||||||
log(ERROR, "Unknown config entry on line %d: %s",
|
log(ERROR, "Unknown config entry on line %d: %s",
|
||||||
j, buf);
|
line_itr, line);
|
||||||
failed = 1;
|
failed = true;
|
||||||
}
|
}
|
||||||
if (!ok) {
|
if (!val_ok) {
|
||||||
log(ERROR,
|
log(ERROR,
|
||||||
"Invalid value for \"%s\", line %d: \"%s\"",
|
"Invalid value for \"%s\", line %d: \"%s\"",
|
||||||
buf, j, tmp + 1);
|
line, line_itr, val_ptr);
|
||||||
ok = !ok;
|
val_ok = !val_ok;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
|
if (errno)
|
||||||
|
log(ERROR, "confparse - getline() - %s", strerror(errno));
|
||||||
|
free(line);
|
||||||
|
|
||||||
if (failed)
|
if (failed)
|
||||||
return 1;
|
return false;
|
||||||
conf.isvalid = 1;
|
return true;
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int confparse(char *config)
|
static bool test_conf_required(void)
|
||||||
{
|
{
|
||||||
int result;
|
bool failed = false;
|
||||||
|
|
||||||
result = test_conf_perms(config);
|
if (conf.db.hostname[0] == '\0') {
|
||||||
if (result)
|
log(ERROR, "cond.db.hostname - not set!");
|
||||||
return 1; /* Bad conf perms */
|
failed = true;
|
||||||
|
}
|
||||||
result = test_conf_syntax(config);
|
if (!conf.db.type) {
|
||||||
if (result != 0)
|
log(ERROR, "cond.db.type - not set!");
|
||||||
return 1; /* Bad conf syntax */
|
failed = true;
|
||||||
return 0; /* seems legit */
|
}
|
||||||
|
if (!conf.db.pass) {
|
||||||
|
log(ERROR, "cond.db.pass - not set!");
|
||||||
|
failed = true;
|
||||||
|
}
|
||||||
|
if (!conf.db.port) {
|
||||||
|
log(ERROR, "cond.db.port - not set!");
|
||||||
|
failed = true;
|
||||||
|
}
|
||||||
|
if (!conf.rmps.cipherlist) {
|
||||||
|
log(ERROR, "conf.rmps.cipherlist - not set!");
|
||||||
|
failed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (failed)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool confparse(char *config)
|
||||||
|
{
|
||||||
|
init_conf();
|
||||||
|
if (test_conf_perms(config) &&
|
||||||
|
test_conf_syntax(config) &&
|
||||||
|
test_conf_required())
|
||||||
|
conf.isvalid = true;
|
||||||
|
return conf.isvalid; /* seems legit */
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,34 +23,34 @@
|
|||||||
#define CONFPARSER_H
|
#define CONFPARSER_H
|
||||||
|
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
#include <stdbool.h>
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
#define HOSTNAMESIZE 128
|
#define HOSTNAMESIZE 256 /* according to RFC 1035 */
|
||||||
#define CFGLINESIZE (PATH_MAX + NAME_MAX + 30) /* 30 is pretty random */
|
|
||||||
|
|
||||||
struct conf_db {
|
struct conf_db {
|
||||||
char type[15];
|
char *type;
|
||||||
char hostname[HOSTNAMESIZE];
|
char hostname[HOSTNAMESIZE];
|
||||||
char port[6];
|
char *port;
|
||||||
char pass[60]; /* random decision */
|
char *pass; /* random decision */
|
||||||
};
|
};
|
||||||
|
|
||||||
struct conf_rmps {
|
struct conf_rmps {
|
||||||
char agent_ip[13];
|
char *agent_ip;
|
||||||
char agent_port[6];
|
char *agent_port;
|
||||||
char client_ip[13];
|
char *client_ip;
|
||||||
char client_port[6];
|
char *client_port;
|
||||||
char logfile[PATH_MAX+NAME_MAX];
|
char *logfile;
|
||||||
char errlog[PATH_MAX+NAME_MAX];
|
char *errlog;
|
||||||
enum LOG_LEVEL loglevel;
|
enum LOG_LEVEL loglevel;
|
||||||
char pidfile[PATH_MAX+NAME_MAX];
|
char *pidfile;
|
||||||
char agent_tls_crt[PATH_MAX+NAME_MAX];
|
char *agent_tls_crt;
|
||||||
char agent_tls_key[PATH_MAX+NAME_MAX];
|
char *agent_tls_key;
|
||||||
char cafile[PATH_MAX+NAME_MAX];
|
char *cafile;
|
||||||
char cipherlist[1024];
|
char *cipherlist;
|
||||||
int agent_poolsize;
|
int agent_poolsize;
|
||||||
char client_tls_crt[PATH_MAX+NAME_MAX];
|
char *client_tls_crt;
|
||||||
char client_tls_key[PATH_MAX+NAME_MAX];
|
char *client_tls_key;
|
||||||
int client_poolsize;
|
int client_poolsize;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -59,14 +59,14 @@ struct conf_nfs {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct conf_table {
|
struct conf_table {
|
||||||
int isvalid;
|
bool isvalid;
|
||||||
struct conf_db db;
|
struct conf_db db;
|
||||||
struct conf_rmps rmps;
|
struct conf_rmps rmps;
|
||||||
struct conf_nfs nfs;
|
struct conf_nfs nfs;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern struct conf_table conf;
|
extern struct conf_table conf;
|
||||||
extern int confparse(char *config);
|
extern bool confparse(char *config);
|
||||||
extern void confexport(void);
|
extern void confexport(void);
|
||||||
extern const char *conf_db_pass(void);
|
extern const char *conf_db_pass(void);
|
||||||
extern const char *conf_db_hostname(void);
|
extern const char *conf_db_hostname(void);
|
||||||
|
|||||||
@@ -112,7 +112,7 @@ int main(int argc, char *argv[])
|
|||||||
memcpy(config, path, path_size);
|
memcpy(config, path, path_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (confparse(config) != 0) {
|
if (!confparse(config)) {
|
||||||
fprintf(stderr, "Failed to parse the conf!\n");
|
fprintf(stderr, "Failed to parse the conf!\n");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
@@ -131,7 +131,7 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (task == START || task == RESTART)
|
if (task == START || task == RESTART)
|
||||||
rmps_launch(&conf, fork_flag);
|
rmps_launch(fork_flag);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
30
src/rmps.c
30
src/rmps.c
@@ -331,7 +331,7 @@ int rmps_die(void)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void rmps_launch(struct conf_table *conf, int fork_flag)
|
void rmps_launch(int fork_flag)
|
||||||
{
|
{
|
||||||
pthread_t pool[2];
|
pthread_t pool[2];
|
||||||
struct pool_data pool_args[2];
|
struct pool_data pool_args[2];
|
||||||
@@ -343,38 +343,38 @@ void rmps_launch(struct conf_table *conf, int fork_flag)
|
|||||||
if (fork_flag)
|
if (fork_flag)
|
||||||
daemonize("/tmp/");
|
daemonize("/tmp/");
|
||||||
/* Spawn & lock pidfile */
|
/* Spawn & lock pidfile */
|
||||||
spawn_pidfile(conf->rmps.pidfile);
|
spawn_pidfile(conf.rmps.pidfile);
|
||||||
|
|
||||||
/* openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days XXX -nodes
|
/* openssl req -x509 -newkey rsa:2048 -keyout key.pem -out cert.pem -days XXX -nodes
|
||||||
* -nodes is for not protecing with a passphrase
|
* -nodes is for not protecing with a passphrase
|
||||||
* http://stackoverflow.com/questions/10175812/how-to-create-a-self-signed-certificate-with-openssl
|
* http://stackoverflow.com/questions/10175812/how-to-create-a-self-signed-certificate-with-openssl
|
||||||
*/
|
*/
|
||||||
pool_args[0].ctx = init_server_ctx(conf->rmps.cipherlist,
|
pool_args[0].ctx = init_server_ctx(conf.rmps.cipherlist,
|
||||||
SSL_VERIFY_PEER |
|
SSL_VERIFY_PEER |
|
||||||
SSL_VERIFY_CLIENT_ONCE |
|
SSL_VERIFY_CLIENT_ONCE |
|
||||||
SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
|
SSL_VERIFY_FAIL_IF_NO_PEER_CERT);
|
||||||
log(VERBOSE, "Loading agent certs and keys.");
|
log(VERBOSE, "Loading agent certs and keys.");
|
||||||
load_certificates(pool_args[0].ctx, conf->rmps.agent_tls_crt,
|
load_certificates(pool_args[0].ctx, conf.rmps.agent_tls_crt,
|
||||||
conf->rmps.agent_tls_key, conf->rmps.cafile);
|
conf.rmps.agent_tls_key, conf.rmps.cafile);
|
||||||
log(VERBOSE, "Starting agent listener on port: %d",
|
log(VERBOSE, "Starting agent listener on port: %d",
|
||||||
atoi(conf->rmps.agent_port));
|
atoi(conf.rmps.agent_port));
|
||||||
pool_args[0].srv = open_listener(atoi(conf->rmps.agent_port));
|
pool_args[0].srv = open_listener(atoi(conf.rmps.agent_port));
|
||||||
pool_args[0].size = conf->rmps.agent_poolsize;
|
pool_args[0].size = conf.rmps.agent_poolsize;
|
||||||
log(VERBOSE, "Creating agent thread pool (mutex).");
|
log(VERBOSE, "Creating agent thread pool (mutex).");
|
||||||
pthread_create(&pool[0], NULL, agent_pool, &pool_args[0]);
|
pthread_create(&pool[0], NULL, agent_pool, &pool_args[0]);
|
||||||
|
|
||||||
pool_args[1].ctx = init_server_ctx(conf->rmps.cipherlist,
|
pool_args[1].ctx = init_server_ctx(conf.rmps.cipherlist,
|
||||||
SSL_VERIFY_NONE);
|
SSL_VERIFY_NONE);
|
||||||
log(VERBOSE, "Loading client certs and keys.");
|
log(VERBOSE, "Loading client certs and keys.");
|
||||||
load_certificates(pool_args[1].ctx, conf->rmps.client_tls_crt,
|
load_certificates(pool_args[1].ctx, conf.rmps.client_tls_crt,
|
||||||
conf->rmps.client_tls_key, conf->rmps.cafile);
|
conf.rmps.client_tls_key, conf.rmps.cafile);
|
||||||
log(VERBOSE, "Starting client listener on port: %d",
|
log(VERBOSE, "Starting client listener on port: %d",
|
||||||
atoi(conf->rmps.client_port));
|
atoi(conf.rmps.client_port));
|
||||||
pool_args[1].srv = open_listener(atoi(conf->rmps.client_port));
|
pool_args[1].srv = open_listener(atoi(conf.rmps.client_port));
|
||||||
pool_args[1].size = conf->rmps.client_poolsize;
|
pool_args[1].size = conf.rmps.client_poolsize;
|
||||||
log(VERBOSE, "Creating client thread pool (mutex).");
|
log(VERBOSE, "Creating client thread pool (mutex).");
|
||||||
pthread_create(&pool[1], NULL, client_pool, &pool_args[1]);
|
pthread_create(&pool[1], NULL, client_pool, &pool_args[1]);
|
||||||
if (start_job_queue(conf->rmps.agent_poolsize) == FAIL) {
|
if (start_job_queue(conf.rmps.agent_poolsize) == FAIL) {
|
||||||
log(ERROR,
|
log(ERROR,
|
||||||
"On start_job_queue(), RMPS failed to start, shutting down...");
|
"On start_job_queue(), RMPS failed to start, shutting down...");
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ struct pool_data {
|
|||||||
int size;
|
int size;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern void rmps_launch(struct conf_table *conf, int fork_flag);
|
extern void rmps_launch(int fork_flag);
|
||||||
extern int rmps_die(void);
|
extern int rmps_die(void);
|
||||||
|
|
||||||
#endif /* RMPS_H */
|
#endif /* RMPS_H */
|
||||||
|
|||||||
Reference in New Issue
Block a user