Lots and lots of whitespace and code style fixes

This commit is contained in:
2017-05-17 17:54:37 +03:00
parent 2442ceaee2
commit ff8546bf66
7 changed files with 191 additions and 180 deletions

View File

@@ -8,8 +8,8 @@
#include "confparser.h" #include "confparser.h"
#include "enum_functions.h" #include "enum_functions.h"
static int test_conf_perms(); static int test_conf_perms(void);
static int test_conf_syntax(); static int test_conf_syntax(void);
struct conf_table conf = { struct conf_table conf = {
0, /* isvalid initial state */ 0, /* isvalid initial state */
@@ -39,7 +39,7 @@ struct conf_table conf = {
}, },
{ {
0 /* nfs -> TODO */ 0 /* nfs -> TODO */
} }
}; };
const char* conf_db_pass(void) const char* conf_db_pass(void)
@@ -54,7 +54,7 @@ const char* conf_db_hostname(void)
void confexport(void) void confexport(void)
{ {
printf( "db.type=%s\n" printf("db.type=%s\n"
"db.hostname=%s\n" "db.hostname=%s\n"
"db.port=%s\n" "db.port=%s\n"
"db.pass=%s\n" "db.pass=%s\n"
@@ -75,17 +75,17 @@ void confexport(void)
"rmps.client_tls_key=%s\n" "rmps.client_tls_key=%s\n"
"rmps.client_poolsize=%d\n", "rmps.client_poolsize=%d\n",
conf.db.type, conf.db.type,
conf.db.hostname, conf.db.hostname,
conf.db.port, conf.db.port,
conf.db.pass, conf.db.pass,
conf.rmps.agent_ip, conf.rmps.agent_ip,
conf.rmps.agent_port, conf.rmps.agent_port,
conf.rmps.client_ip, conf.rmps.client_ip,
conf.rmps.client_port, conf.rmps.client_port,
conf.rmps.logfile, conf.rmps.logfile,
conf.rmps.errlog, conf.rmps.errlog,
conf.rmps.loglevel, conf.rmps.loglevel,
conf.rmps.pidfile, conf.rmps.pidfile,
conf.rmps.agent_tls_crt, conf.rmps.agent_tls_crt,
conf.rmps.agent_tls_key, conf.rmps.agent_tls_key,
conf.rmps.cafile, conf.rmps.cafile,
@@ -97,33 +97,34 @@ void confexport(void)
); );
} }
static int fopen_and_mkdir(const char *dir) { static int fopen_and_mkdir(const char *dir)
{
char tmp[256]; char tmp[256];
char *p = NULL; char *p = NULL;
size_t len; size_t len;
FILE *fp; FILE *fp;
snprintf(tmp, sizeof(tmp),"%s",dir); snprintf(tmp, sizeof(tmp), "%s", dir);
len = strlen(tmp); len = strlen(tmp);
if(tmp[len - 1] == '/') if (tmp[len - 1] == '/')
tmp[len - 1] = 0; tmp[len - 1] = 0;
for (p = tmp + 1; *p; p++) for (p = tmp + 1; *p; p++)
if(*p == '/') { if (*p == '/') {
*p = 0; *p = 0;
if (mkdir(tmp, S_IRWXU) == -1 && errno != EEXIST) { if (mkdir(tmp, S_IRWXU) == -1 && errno != EEXIST) {
log( ERROR, log(ERROR,
"Permission denied to create directory: %s", "Permission denied to create directory: %s",
tmp ); tmp);
return 1; return 1;
} }
*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 1;
} }
fclose(fp); fclose(fp);
return 0; return 0;
} }
@@ -146,8 +147,8 @@ static int test_conf_perms(void)
log(ERROR, confresult); log(ERROR, confresult);
return 1; return 1;
} }
if ( !(S_IRUSR & s.st_mode) || if (!(S_IRUSR & s.st_mode) ||
!(S_IXUSR & s.st_mode) ) { !(S_IXUSR & s.st_mode)) {
enumtostr(confresult, CONF_DIR_PERM); enumtostr(confresult, CONF_DIR_PERM);
log(ERROR, confresult); log(ERROR, confresult);
return 1; return 1;
@@ -155,13 +156,11 @@ static int test_conf_perms(void)
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);
} } 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);
} } else if ((S_IROTH & s.st_mode) ||
else if ( (S_IROTH & s.st_mode) || (S_IWOTH & s.st_mode)) {
(S_IWOTH & s.st_mode) ) {
enumtostr(confresult, CONF_DIR_PERM_INSECURE); enumtostr(confresult, CONF_DIR_PERM_INSECURE);
log(WARNING, confresult); log(WARNING, confresult);
} }
@@ -189,16 +188,14 @@ static int test_conf_perms(void)
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);
} } 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);
} } else if ((S_IROTH & s.st_mode) ||
else if ( (S_IROTH & s.st_mode) || (S_IWOTH & s.st_mode)) {
(S_IWOTH & s.st_mode) ) {
enumtostr(confresult, CONF_FILE_PERM_INSECURE); enumtostr(confresult, CONF_FILE_PERM_INSECURE);
log(WARNING, confresult); log(WARNING, confresult);
} }
} }
return 0; /* conf is readable */ return 0; /* conf is readable */
} }
@@ -226,9 +223,9 @@ static int test_conf_syntax(void)
if ((tmp = strstr(buf, "="))) if ((tmp = strstr(buf, "=")))
*tmp = '\0'; *tmp = '\0';
else { else {
log( ERROR, log(ERROR,
"Bad entry in /etc/rmps/rmps.conf, line %d: %s", "Bad entry in /etc/rmps/rmps.conf, line %d: %s",
j, buf ); j, buf);
ok = 0; ok = 0;
failed = 1; failed = 1;
continue; continue;
@@ -238,9 +235,9 @@ static int test_conf_syntax(void)
if (tmp[i] == '\n') if (tmp[i] == '\n')
tmp[i] = '\0'; tmp[i] = '\0';
if (tmp[strspn(tmp + 1, " \t\v\r\n") + 1] == '\0') { if (tmp[strspn(tmp + 1, " \t\v\r\n") + 1] == '\0') {
log( ERROR, log(ERROR,
"Specified entry without value, line %d: %s", "Specified entry without value, line %d: %s",
j, buf ); j, buf);
failed = 1; failed = 1;
continue; continue;
} }
@@ -278,28 +275,30 @@ static int test_conf_syntax(void)
/* TODO */ /* TODO */
} else if (!strcmp(buf, "rmps.agent_port")) { } else if (!strcmp(buf, "rmps.agent_port")) {
if ((i = strlen(tmp + 1)) < 6) { if ((i = strlen(tmp + 1)) < 6) {
if ((signed int)strspn(tmp + 1, "1234567890") == i) { if ((signed int)strspn(tmp + 1, "1234567890") == i) {
i = atoi(tmp + 1); i = atoi(tmp + 1);
if (i > 0 && i < 65536) { if (i > 0 && i < 65536) {
strcpy(conf.rmps.agent_port, tmp + 1); strcpy(conf.rmps.agent_port,
tmp + 1);
continue; continue;
} }
} }
} }
ok = 0; ok = 0;
failed = 1; failed = 1;
} else if (!strcmp(buf, "rmps.client_ip")) { } else if (!strcmp(buf, "rmps.client_ip")) {
/* TODO */ /* TODO */
} else if (!strcmp(buf, "rmps.client_port")) { } else if (!strcmp(buf, "rmps.client_port")) {
if ((i = strlen(tmp + 1)) < 6) { if ((i = strlen(tmp + 1)) < 6) {
if ((signed int)strspn(tmp + 1, "1234567890") == i) { if ((signed int)strspn(tmp + 1, "1234567890") == i) {
i = atoi(tmp + 1); i = atoi(tmp + 1);
if (i > 0 && i < 65536) { if (i > 0 && i < 65536) {
strcpy(conf.rmps.client_port, tmp + 1); strcpy(conf.rmps.client_port,
tmp + 1);
continue; continue;
} }
} }
} }
ok = 0; ok = 0;
failed = 1; failed = 1;
} else if (!strcmp(buf, "rmps.logfile")) { } else if (!strcmp(buf, "rmps.logfile")) {
@@ -309,13 +308,15 @@ static int test_conf_syntax(void)
} else if (!strcmp(buf, "rmps.errlog")) { } else if (!strcmp(buf, "rmps.errlog")) {
strcpy(conf.rmps.errlog, tmp + 1); strcpy(conf.rmps.errlog, tmp + 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")) {
strcpy(conf.rmps.pidfile, tmp + 1); strcpy(conf.rmps.pidfile, tmp + 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 && (tmp[1] > '0' && tmp[1] < '5')) if (strlen(tmp + 1) == 1 &&
(tmp[1] > '0' && tmp[1] < '5'))
conf.rmps.loglevel = tmp[1] - '0'; conf.rmps.loglevel = tmp[1] - '0';
else else
failed = 1; failed = 1;
@@ -323,21 +324,18 @@ static int test_conf_syntax(void)
if (access(tmp + 1, F_OK) == -1) { if (access(tmp + 1, F_OK) == -1) {
log(ERROR, "%s is missing", tmp + 1); log(ERROR, "%s is missing", tmp + 1);
failed = 1; failed = 1;
} } else if (access(tmp + 1, R_OK) == -1) {
else if (access(tmp + 1, R_OK) == -1) {
log(ERROR, "%s is not readable", tmp + 1); log(ERROR, "%s is not readable", tmp + 1);
failed = 1; failed = 1;
} else } else
strncpy(conf.rmps.agent_tls_crt, strncpy(conf.rmps.agent_tls_crt,
tmp + 1, tmp + 1,
sizeof(conf.rmps.agent_tls_crt)); sizeof(conf.rmps.agent_tls_crt));
} } else if (!strcmp(buf, "rmps.agent_tls_key")) {
else if (!strcmp(buf, "rmps.agent_tls_key")) {
if (access(tmp + 1, F_OK) == -1) { if (access(tmp + 1, F_OK) == -1) {
log(ERROR, "%s is missing", tmp + 1); log(ERROR, "%s is missing", tmp + 1);
failed = 1; failed = 1;
} } else if (access(tmp + 1, R_OK) == -1) {
else if (access(tmp + 1, R_OK) == -1) {
log(ERROR, "%s is not readable", tmp + 1); log(ERROR, "%s is not readable", tmp + 1);
failed = 1; failed = 1;
} else } else
@@ -345,23 +343,23 @@ static int test_conf_syntax(void)
tmp + 1, tmp + 1,
sizeof(conf.rmps.agent_tls_key)); sizeof(conf.rmps.agent_tls_key));
} else if (!strcmp(buf, "rmps.cipherlist")) { } else if (!strcmp(buf, "rmps.cipherlist")) {
strncpy(conf.rmps.cipherlist, tmp + 1, sizeof(conf.rmps.cipherlist)); strncpy(conf.rmps.cipherlist,
tmp + 1, sizeof(conf.rmps.cipherlist));
} else if (!strcmp(buf, "rmps.cafile")) { } else if (!strcmp(buf, "rmps.cafile")) {
if (access(tmp + 1, F_OK) == -1) { if (access(tmp + 1, F_OK) == -1) {
log(ERROR, "%s is missing", tmp + 1); log(ERROR, "%s is missing", tmp + 1);
failed = 1; failed = 1;
} } else if (access(tmp + 1, R_OK) == -1) {
else if (access(tmp + 1, R_OK) == -1) {
log(ERROR, "%s is not readable\n", tmp + 1); log(ERROR, "%s is not readable\n", tmp + 1);
failed = 1; failed = 1;
} else } else
strncpy(conf.rmps.cafile, tmp + 1, sizeof(conf.rmps.cafile)); strncpy(conf.rmps.cafile,
tmp + 1, sizeof(conf.rmps.cafile));
} else if (!strcmp(buf, "rmps.client_tls_crt")) { } else if (!strcmp(buf, "rmps.client_tls_crt")) {
if (access(tmp + 1, F_OK) == -1) { if (access(tmp + 1, F_OK) == -1) {
log(ERROR, "%s is missing", tmp + 1); log(ERROR, "%s is missing", tmp + 1);
failed = 1; failed = 1;
} } else if (access(tmp + 1, R_OK) == -1) {
else if (access(tmp + 1, R_OK) == -1) {
log(ERROR, "%s is not readable", tmp + 1); log(ERROR, "%s is not readable", tmp + 1);
failed = 1; failed = 1;
} else } else
@@ -372,8 +370,7 @@ static int test_conf_syntax(void)
if (access(tmp + 1, F_OK) == -1) { if (access(tmp + 1, F_OK) == -1) {
log(ERROR, "%s is missing", tmp + 1); log(ERROR, "%s is missing", tmp + 1);
failed = 1; failed = 1;
} } else if (access(tmp + 1, R_OK) == -1) {
else if (access(tmp + 1, R_OK) == -1) {
log(ERROR, "%s is not readable", tmp + 1); log(ERROR, "%s is not readable", tmp + 1);
failed = 1; failed = 1;
} else } else
@@ -381,11 +378,12 @@ static int test_conf_syntax(void)
tmp + 1, tmp + 1,
sizeof(conf.rmps.client_tls_key)); sizeof(conf.rmps.client_tls_key));
} else } else
log(ERROR, "Unknown config entry on line %d: %s", j, buf); log(ERROR, "Unknown config entry on line %d: %s",
j, buf);
if (!ok) { if (!ok) {
log( ERROR, log(ERROR,
"Invalid value for \"%s\", line %d: \"%s\"", "Invalid value for \"%s\", line %d: \"%s\"",
buf, j, tmp + 1 ); buf, j, tmp + 1);
ok = !ok; ok = !ok;
} }
} }
@@ -404,7 +402,7 @@ int confparse(void)
result = test_conf_perms(); result = test_conf_perms();
if (result) if (result)
return 1; /* Bad conf perms */ return 1; /* Bad conf perms */
result = test_conf_syntax(); result = test_conf_syntax();
if (result != 0) if (result != 0)
return 1; /* Bad conf syntax */ return 1; /* Bad conf syntax */

View File

@@ -21,7 +21,7 @@ struct conf_rmps {
char client_port[6]; char client_port[6];
char logfile[MAXPATHSIZE]; char logfile[MAXPATHSIZE];
char errlog[MAXPATHSIZE]; char errlog[MAXPATHSIZE];
LOG_LEVEL loglevel; enum LOG_LEVEL loglevel;
char pidfile[MAXPATHSIZE]; char pidfile[MAXPATHSIZE];
char agent_tls_crt[MAXPATHSIZE]; char agent_tls_crt[MAXPATHSIZE];
char agent_tls_key[MAXPATHSIZE]; char agent_tls_key[MAXPATHSIZE];

32
log.c
View File

@@ -35,8 +35,8 @@ static void set_fpts(void)
fdout = stdout; fdout = stdout;
} }
void log(LOG_LEVEL lvl, char *fmt, ... ) void log(enum LOG_LEVEL lvl, char *fmt, ...)
{ {
char fmt_with_pfx[1024]; char fmt_with_pfx[1024];
pthread_once(&init_once, set_fpts); pthread_once(&init_once, set_fpts);
@@ -45,31 +45,33 @@ void log(LOG_LEVEL lvl, char *fmt, ... )
if (lvl <= conf.rmps.loglevel) { if (lvl <= conf.rmps.loglevel) {
va_list list; va_list list;
FILE *fp; FILE *fp;
static const char *prefixes[] = {
static const char * const prefixes[] = {
"ERROR", "WARNING", "INFO", "VERBOSE" "ERROR", "WARNING", "INFO", "VERBOSE"
}; };
time_t t = time(NULL); time_t t = time(NULL);
struct tm tm; struct tm tm;
localtime_r(&t, &tm); localtime_r(&t, &tm);
if (lvl == ERROR || lvl == WARNING) if (lvl == ERROR || lvl == WARNING)
fp = fderr; fp = fderr;
else else
fp = fdout; fp = fdout;
snprintf( fmt_with_pfx, snprintf(fmt_with_pfx,
sizeof(fmt_with_pfx), sizeof(fmt_with_pfx),
"[%d-%02d-%02d %02d:%02d:%02d] %s: %s\n", "[%d-%02d-%02d %02d:%02d:%02d] %s: %s\n",
tm.tm_year + 1900, tm.tm_year + 1900,
tm.tm_mon + 1, tm.tm_mon + 1,
tm.tm_mday, tm.tm_mday,
tm.tm_hour, tm.tm_hour,
tm.tm_min, tm.tm_min,
tm.tm_sec, tm.tm_sec,
prefixes[lvl-1], prefixes[lvl-1],
fmt ); fmt);
va_start(list, fmt); va_start(list, fmt);
vfprintf(fp, fmt_with_pfx, list); vfprintf(fp, fmt_with_pfx, list);
va_end(list); va_end(list);
} }
} }

9
log.h
View File

@@ -4,14 +4,15 @@
#undef log #undef log
#define log log_trace #define log log_trace
typedef enum { enum LOG_LEVEL {
ERROR = 1, /* Errors only */ ERROR = 1, /* Errors only */
WARNING, /* Errors & warnings */ WARNING, /* Errors & warnings */
INFO, /* Errors, warnings & events */ INFO, /* Errors, warnings & events */
VERBOSE, /* Errors, warnings, events & more? */ VERBOSE, /* Errors, warnings, events & more? */
} LOG_LEVEL; };
void log_ssl(void); void log_ssl(void);
void log(LOG_LEVEL lvl, char *fmt, ... ); void log(enum LOG_LEVEL lvl, char *fmt, ...);
#endif /* LOG_TRACE_H */ #endif /* LOG_TRACE_H */

30
main.c
View File

@@ -10,9 +10,8 @@
static void usage(char *argv) static void usage(char *argv)
{ {
log( ERROR, log(ERROR,
"Usage:\n%s start|stop|restart [--daemonize=yes|no]\n", "Usage:\n%s start|stop|restart [--daemonize=yes|no]\n", argv);
argv );
} }
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@@ -36,7 +35,8 @@ int main(int argc, char *argv[])
} }
if (argc == 3) { if (argc == 3) {
if (!strcmp("--daemonize=yes", argv[2])); if (!strcmp("--daemonize=yes", argv[2]))
fork_flag = 1;
else if (!strcmp("--daemonize=no", argv[2])) else if (!strcmp("--daemonize=no", argv[2]))
fork_flag = 0; fork_flag = 0;
else { else {
@@ -45,16 +45,17 @@ int main(int argc, char *argv[])
} }
} }
if (confparse() != 0) { if (confparse() != 0) {
log(ERROR, "Failed to parse the conf!"); log(ERROR, "Failed to parse the conf!");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
log(VERBOSE, "Conf parser finished successfully"); log(VERBOSE, "Conf parser finished successfully");
//confexport(); //confexport();
if (task == 2 || task == 3) { if (task == 2 || task == 3) {
char buf[10]; char buf[10];
int pid; int pid;
FILE *fp; FILE *fp;
if (task == 2) if (task == 2)
log(VERBOSE, "We got a stop signal!"); log(VERBOSE, "We got a stop signal!");
else if (task == 3) else if (task == 3)
@@ -65,7 +66,8 @@ int main(int argc, char *argv[])
switch (errno) { switch (errno) {
case EEXIST: case EEXIST:
if (!fgets(buf, 10, fp)) { if (!fgets(buf, 10, fp)) {
log(ERROR, "Failed to read %s!", conf.rmps.pidfile); log(ERROR, "Failed to read %s!",
conf.rmps.pidfile);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
pid = strtol(buf, NULL, 10); pid = strtol(buf, NULL, 10);
@@ -80,16 +82,14 @@ int main(int argc, char *argv[])
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
break; break;
default: default:
log( ERROR, log(ERROR,
"Unhandled errno while opening PID: %d. Exiting!", "Failed to open PID file (errno: %d. Exiting!",
errno errno);
);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
} }
if (task == 1 || task == 3) { if (task == 1 || task == 3)
launch_rmps(&conf, fork_flag); launch_rmps(&conf, fork_flag);
}
return 0; return 0;
} }

141
rmps.c
View File

@@ -23,7 +23,7 @@ static int open_listener(int port);
static void cleanup(void); static void cleanup(void);
static void signal_handler(int sig); static void signal_handler(int sig);
static void load_certificates(SSL_CTX *ctx, const char *certfile, static void load_certificates(SSL_CTX *ctx, const char *certfile,
const char *keyfile, const char *cafile); const char *keyfile, const char *cafile);
static SSL_CTX* init_server_ctx(const char *cipherlist, int mode); static SSL_CTX* init_server_ctx(const char *cipherlist, int mode);
static int pid_file_handle; static int pid_file_handle;
@@ -32,9 +32,9 @@ static void cleanup(void)
{ {
log(VERBOSE, "Deleting pidfile %s", conf.rmps.pidfile); log(VERBOSE, "Deleting pidfile %s", conf.rmps.pidfile);
if (unlink(conf.rmps.pidfile) != 0) if (unlink(conf.rmps.pidfile) != 0)
log( WARNING, log(WARNING,
"Failed to delete pidfile %s. Reason code: %d", "Failed to delete pidfile %s. Reason code: %d",
conf.rmps.pidfile, errno ); conf.rmps.pidfile, errno);
} }
static void signal_handler(int sig) static void signal_handler(int sig)
@@ -66,25 +66,25 @@ static void rmps_shutdown(void)
static void set_env(void) static void set_env(void)
{ {
struct sigaction new_sigaction; struct sigaction new_sigaction;
sigset_t new_sigset; sigset_t new_sigset;
/* Set signal mask - signals we want to block */ /* Set signal mask - signals we want to block */
sigemptyset(&new_sigset); sigemptyset(&new_sigset);
sigaddset(&new_sigset, SIGCHLD); /* ignore child - i.e. we don't need to wait for it */ sigaddset(&new_sigset, SIGCHLD); /* ignore child */
sigaddset(&new_sigset, SIGTSTP); /* ignore Tty stop signals */ sigaddset(&new_sigset, SIGTSTP); /* ignore Tty stop signals */
sigaddset(&new_sigset, SIGTTOU); /* ignore Tty background writes */ sigaddset(&new_sigset, SIGTTOU); /* ignore Tty background writes */
sigaddset(&new_sigset, SIGTTIN); /* ignore Tty background reads */ sigaddset(&new_sigset, SIGTTIN); /* ignore Tty background reads */
sigprocmask(SIG_BLOCK, &new_sigset, NULL); /* Block the above specified signals */ sigprocmask(SIG_BLOCK, &new_sigset, NULL); /* Block above signals */
/* Set up a signal handler */ /* Set up a signal handler */
new_sigaction.sa_handler = signal_handler; new_sigaction.sa_handler = signal_handler;
sigemptyset(&new_sigaction.sa_mask); sigemptyset(&new_sigaction.sa_mask);
new_sigaction.sa_flags = 0; new_sigaction.sa_flags = 0;
/* Signals to handle */ /* Signals to handle */
sigaction(SIGHUP, &new_sigaction, NULL); /* catch hangup signal */ sigaction(SIGHUP, &new_sigaction, NULL); /* catch hangup signal */
sigaction(SIGTERM, &new_sigaction, NULL); /* catch term signal */ sigaction(SIGTERM, &new_sigaction, NULL); /* catch term signal */
sigaction(SIGINT, &new_sigaction, NULL); /* catch interrupt signal */ sigaction(SIGINT, &new_sigaction, NULL); /* catch interrupt signal */
signal(SIGPIPE, SIG_IGN); /* prevent crashing from bad writes */ signal(SIGPIPE, SIG_IGN); /* prevent crashing from bad writes */
} }
@@ -121,13 +121,13 @@ static void daemonize(const char *rundir)
close(1); /* stdout */ close(1); /* stdout */
close(2); /* stderr */ close(2); /* stderr */
/* Route I/O connections */ /* Route I/O connections */
/* Open STDIN */ /* Open STDIN */
i = open("/dev/null", O_RDWR); i = open("/dev/null", O_RDWR);
/* STDOUT */ /* STDOUT */
dup(i); dup(i);
/* STDERR */ /* STDERR */
dup(i); dup(i);
chdir(rundir); /* change running directory */ chdir(rundir); /* change running directory */
} }
static void spawn_pidfile(const char *pidfile) static void spawn_pidfile(const char *pidfile)
@@ -140,14 +140,14 @@ static void spawn_pidfile(const char *pidfile)
log(ERROR, "Could not create PID file %s - Exiting!", pidfile); log(ERROR, "Could not create PID file %s - Exiting!", pidfile);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/* Try to lock file */ /* Try to lock file */
if (lockf(pid_file_handle, F_TLOCK, 0) == -1) { if (lockf(pid_file_handle, F_TLOCK, 0) == -1) {
/* Couldn't get lock on lock file */ /* Couldn't get lock on lock file */
log(ERROR, "Could not lock PID file %s - Exiting!", pidfile); log(ERROR, "Could not lock PID file %s - Exiting!", pidfile);
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/* Get and format PID */ /* Get and format PID */
sprintf(str, "%d\n", getpid()); sprintf(str, "%d\n", getpid());
/* write pid to lockfile */ /* write pid to lockfile */
@@ -157,37 +157,41 @@ static void spawn_pidfile(const char *pidfile)
static inline int set_reuse_addr(int sockfd) static inline int set_reuse_addr(int sockfd)
{ {
int yes = 1; int yes = 1;
return setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, return setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
&yes, sizeof(yes)); &yes, sizeof(yes));
} }
static int open_listener(int port) static int open_listener(int port)
{ {
int sd; int sd;
struct sockaddr_in addr; struct sockaddr_in addr;
bzero(&addr, sizeof(addr)); bzero(&addr, sizeof(addr));
addr.sin_family = AF_INET; addr.sin_family = AF_INET;
addr.sin_port = htons(port); addr.sin_port = htons(port);
addr.sin_addr.s_addr = INADDR_ANY; addr.sin_addr.s_addr = INADDR_ANY;
sd = socket(addr.sin_family, SOCK_STREAM, 0); sd = socket(addr.sin_family, SOCK_STREAM, 0);
if (sd < 0) { if (sd < 0) {
log(ERROR, "Failed to create socket"); log(ERROR, "Failed to create socket");
goto exit; goto exit;
} }
if (set_reuse_addr(sd) < 0) { if (set_reuse_addr(sd) < 0) {
log(ERROR, "Failed to set reuse on address - Aborting...", port); log(ERROR,
"Failed to set reuse on address - Aborting...", port);
goto exit; goto exit;
} }
if (bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0) { if (bind(sd, (struct sockaddr *)&addr, sizeof(addr)) != 0) {
log(ERROR, "Failed to bind on port: %d - Aborting...", port); log(ERROR, "Failed to bind on port: %d - Aborting...", port);
goto exit; goto exit;
} }
if (listen(sd, 10) != 0) { if (listen(sd, 10) != 0) {
log(ERROR, "Failed to start listener on port %d - Aborting...", port); log(ERROR,
"Failed to start listener on port %d - Aborting...",
port);
goto exit; goto exit;
} }
return sd; return sd;
exit: exit:
log(INFO, "RMPS failed to start, shutting down..."); log(INFO, "RMPS failed to start, shutting down...");
atexit(cleanup); atexit(cleanup);
@@ -198,20 +202,21 @@ static SSL_CTX* init_server_ctx(const char *cipherlist, int mode)
{ {
SSL_CTX *ctx; SSL_CTX *ctx;
char ciphers[1024]; char ciphers[1024];
// OpenSSL_add_all_algorithms(); /* load & register all cryptos, etc. */ // OpenSSL_add_all_algorithms(); /* load & register all cryptos, etc. */
// OpenSSL_add_all_ciphers(); /* load & register all cryptos, etc. */ // OpenSSL_add_all_ciphers(); /* load & register all cryptos, etc. */
SSL_load_error_strings(); /* load all error messages */ SSL_load_error_strings(); /* load all error messages */
SSL_library_init(); SSL_library_init();
ctx = SSL_CTX_new(TLSv1_2_method()); /* create new context from method */ /* create new context from method */
ctx = SSL_CTX_new(TLSv1_2_method());
if (ctx == NULL) { if (ctx == NULL) {
log(ERROR, "SSL_CTX_new() returned NULL - Aborting..."); log(ERROR, "SSL_CTX_new() returned NULL - Aborting...");
log(ERROR, "RMPS failed to start, shutting down..."); log(ERROR, "RMPS failed to start, shutting down...");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
SSL_CTX_set_verify(ctx, mode, NULL); SSL_CTX_set_verify(ctx, mode, NULL);
ciphers[0] = 0; ciphers[0] = 0;
strcat(ciphers, "-ALL"); /* Disable any ciphers we have by default */ strcat(ciphers, "-ALL"); /* Disable any ciphers we have by default */
strcat(ciphers, cipherlist); strcat(ciphers, cipherlist);
@@ -244,16 +249,17 @@ void load_certificates(SSL_CTX* ctx, const char *certfile,
} }
/* verify private key */ /* verify private key */
if (!SSL_CTX_check_private_key(ctx)) { if (!SSL_CTX_check_private_key(ctx)) {
log(ERROR, "Private key does not match the public certificate."); log(ERROR,
"Private key does not match the public certificate.");
log(INFO, "RMPS failed to start, shutting down..."); log(INFO, "RMPS failed to start, shutting down...");
atexit(cleanup); atexit(cleanup);
} }
if (cafile != NULL) { if (cafile != NULL) {
SSL_CTX_set_client_CA_list(ctx, SSL_load_client_CA_file(cafile)); SSL_CTX_set_client_CA_list(ctx,
SSL_load_client_CA_file(cafile));
SSL_CTX_load_verify_locations(ctx, cafile, NULL); SSL_CTX_load_verify_locations(ctx, cafile, NULL);
//SSL_CTX_set_verify_depth(ctx, 1); //SSL_CTX_set_verify_depth(ctx, 1);
} }
} }
void launch_rmps(struct conf_table *conf, int fork_flag) void launch_rmps(struct conf_table *conf, int fork_flag)
@@ -269,30 +275,33 @@ void launch_rmps(struct conf_table *conf, int 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", atoi(conf->rmps.agent_port)); log(VERBOSE, "Starting agent listener on port: %d",
pool_args[0].srv = open_listener(atoi(conf->rmps.agent_port)); 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, SSL_VERIFY_NONE); pool_args[1].ctx = init_server_ctx(conf->rmps.cipherlist,
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", atoi(conf->rmps.client_port)); log(VERBOSE, "Starting client listener on port: %d",
pool_args[1].srv = open_listener(atoi(conf->rmps.client_port)); 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]);

3
rmps.h
View File

@@ -5,7 +5,7 @@
#include "confparser.h" #include "confparser.h"
struct pool_data { struct pool_data {
int srv; int srv;
SSL_CTX *ctx; SSL_CTX *ctx;
int size; int size;
}; };
@@ -13,3 +13,4 @@ struct pool_data {
extern void launch_rmps(struct conf_table *conf, int fork_flag); extern void launch_rmps(struct conf_table *conf, int fork_flag);
#endif /* RMPS_H */ #endif /* RMPS_H */