Renamed log_trace() to log()

This commit is contained in:
2016-08-10 20:21:54 +03:00
parent 65b923cba8
commit dcd7a46d0a
10 changed files with 100 additions and 97 deletions

View File

@@ -14,7 +14,7 @@ SOURCES = main.c \
confparser.c \ confparser.c \
rmps.c \ rmps.c \
enum_functions.c \ enum_functions.c \
log_trace.c \ log.c \
thread_pool.c thread_pool.c
OBJECTS = $(SOURCES:.c=.o) OBJECTS = $(SOURCES:.c=.o)

View File

@@ -146,7 +146,7 @@ void* get_memory(void *args)
job->buf.meta.len = sprintf( (char*)job->buf.chunk.data, job->buf.meta.len = sprintf( (char*)job->buf.chunk.data,
"%ld / %ld (MB)", "%ld / %ld (MB)",
((pages - freepages) * pagesize) / 1048576 /* 1024*1024 */, ((pages - freepages) * pagesize) / 1048576 /* 1024*1024 */,
(pages * pagesize) / 1048576 ); (pages * pagesize) / 1048576 );
SSL_write(job->ssl, &job->buf, sizeof(struct msg)); SSL_write(job->ssl, &job->buf, sizeof(struct msg));
job->slot = FREE; job->slot = FREE;
//return 0; //return 0;

View File

@@ -4,7 +4,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include "log_trace.h" #include "log.h"
#include "confparser.h" #include "confparser.h"
#include "enum_functions.h" #include "enum_functions.h"
@@ -36,7 +36,7 @@ struct conf_table conf = {
} }
}; };
void confexport() void confexport(void)
{ {
printf( "db.type=%s\n" printf( "db.type=%s\n"
"db.hostname=%s\n" "db.hostname=%s\n"
@@ -83,7 +83,7 @@ static int fopen_and_mkdir(const char *dir) {
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_trace( ERROR, log( ERROR,
"Permission denied to create directory: %s", "Permission denied to create directory: %s",
tmp ); tmp );
return 1; return 1;
@@ -92,7 +92,7 @@ static int fopen_and_mkdir(const char *dir) {
} }
fp = fopen(dir, "a"); fp = fopen(dir, "a");
if (!fp) { if (!fp) {
log_trace(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);
@@ -100,7 +100,7 @@ static int fopen_and_mkdir(const char *dir) {
} }
static int test_conf_perms() static int test_conf_perms(void)
{ {
struct stat s; struct stat s;
char confresult[128]; char confresult[128];
@@ -109,33 +109,33 @@ static int test_conf_perms()
if (err == -1) { if (err == -1) {
if (errno == ENOENT) { if (errno == ENOENT) {
enumtostr(confresult, CONF_DIR_MISSING); enumtostr(confresult, CONF_DIR_MISSING);
log_trace(ERROR, confresult); log(ERROR, confresult);
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_trace(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_trace(ERROR, confresult); log(ERROR, confresult);
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_trace(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_trace(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_trace(WARNING, confresult); log(WARNING, confresult);
} }
} }
@@ -144,45 +144,45 @@ static int test_conf_perms()
if (err == -1) { if (err == -1) {
if (errno == ENOENT) { if (errno == ENOENT) {
enumtostr(confresult, CONF_MISSING); enumtostr(confresult, CONF_MISSING);
log_trace(ERROR, confresult); log(ERROR, confresult);
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_trace(ERROR, confresult); log(ERROR, confresult);
return 1; return 1;
} }
if (!(S_IRUSR & s.st_mode)) { if (!(S_IRUSR & s.st_mode)) {
enumtostr(confresult, CONF_PERM); enumtostr(confresult, CONF_PERM);
log_trace(ERROR, confresult); log(ERROR, confresult);
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_trace(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_trace(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_trace(WARNING, confresult); log(WARNING, confresult);
} }
} }
return 0; /* conf is readable */ return 0; /* conf is readable */
} }
static int test_conf_syntax() static int test_conf_syntax(void)
{ {
int i, j = 0, ok = 1, failed = 0; int i, j = 0, ok = 1, failed = 0;
char buf[CFGLINESIZE], *tmp; char buf[CFGLINESIZE], *tmp;
FILE *fp = fopen("/etc/rmps/rmps.conf", "r"); FILE *fp = fopen("/etc/rmps/rmps.conf", "r");
if (fp == NULL) { if (fp == NULL) {
log_trace(ERROR, "Failed to read /etc/rmps/rmps.conf"); log(ERROR, "Failed to read /etc/rmps/rmps.conf");
return 1; return 1;
} }
@@ -198,7 +198,7 @@ static int test_conf_syntax()
if ((tmp = strstr(buf, "="))) if ((tmp = strstr(buf, "=")))
*tmp = '\0'; *tmp = '\0';
else { else {
log_trace( 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;
@@ -210,7 +210,7 @@ static int test_conf_syntax()
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_trace( 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;
@@ -278,22 +278,22 @@ static int test_conf_syntax()
failed = 1; failed = 1;
} else if (!strcmp(buf, "rmps.certfile")) { } else if (!strcmp(buf, "rmps.certfile")) {
if (access(tmp + 1, F_OK) == -1) { if (access(tmp + 1, F_OK) == -1) {
log_trace(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_trace(ERROR, "%s is not readable", tmp + 1); log(ERROR, "%s is not readable", tmp + 1);
failed = 1; failed = 1;
} else } else
strncpy(conf.rmps.certfile, tmp + 1, sizeof(conf.rmps.certfile)); strncpy(conf.rmps.certfile, tmp + 1, sizeof(conf.rmps.certfile));
} }
else if (!strcmp(buf, "rmps.keyfile")) { else if (!strcmp(buf, "rmps.keyfile")) {
if (access(tmp + 1, F_OK) == -1) { if (access(tmp + 1, F_OK) == -1) {
log_trace(ERROR, "%s is missing", conf.rmps.keyfile); log(ERROR, "%s is missing", conf.rmps.keyfile);
failed = 1; failed = 1;
} }
else if (access(tmp + 1, R_OK) == -1) { else if (access(tmp + 1, R_OK) == -1) {
log_trace(ERROR, "%s is not readable", tmp + 1); log(ERROR, "%s is not readable", tmp + 1);
failed = 1; failed = 1;
} else } else
strncpy(conf.rmps.keyfile, tmp + 1, sizeof(conf.rmps.keyfile)); strncpy(conf.rmps.keyfile, tmp + 1, sizeof(conf.rmps.keyfile));
@@ -301,18 +301,18 @@ static int test_conf_syntax()
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_trace(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_trace(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 } else
log_trace(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_trace( 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;
@@ -326,7 +326,7 @@ static int test_conf_syntax()
return 0; return 0;
} }
int confparse() int confparse(void)
{ {
int result; int result;

View File

@@ -1,7 +1,7 @@
#ifndef CONFPARSER_H #ifndef CONFPARSER_H
#define CONFPARSER_H #define CONFPARSER_H
#include "log_trace.h" #include "log.h"
#define MAXPATHSIZE 256 #define MAXPATHSIZE 256
#define HOSTNAMESIZE 128 #define HOSTNAMESIZE 128
@@ -39,8 +39,8 @@ struct conf_table {
}; };
extern struct conf_table conf; extern struct conf_table conf;
extern int confparse(); extern int confparse(void);
extern void confexport(); extern void confexport(void);
#endif /* CONFPARSER_H */ #endif /* CONFPARSER_H */

View File

@@ -2,7 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include "enum_functions.h" #include "enum_functions.h"
#include "log_trace.h" #include "log.h"
void enumtostr(char *scode, int code) void enumtostr(char *scode, int code)
{ {
@@ -14,7 +14,7 @@ void enumtostr(char *scode, int code)
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_trace(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 (fgets(line, sizeof(line), fp) != NULL)

View File

@@ -3,7 +3,7 @@
#include <stdarg.h> #include <stdarg.h>
#include <pthread.h> #include <pthread.h>
#include <openssl/err.h> #include <openssl/err.h>
#include "log_trace.h" #include "log.h"
#include "confparser.h" #include "confparser.h"
static FILE *fderr; static FILE *fderr;
@@ -35,7 +35,7 @@ static void set_fpts(void)
fdout = stdout; fdout = stdout;
} }
void log_trace(LOG_LEVEL lvl, char *fmt, ... ) void log(LOG_LEVEL lvl, char *fmt, ... )
{ {
char fmt_with_pfx[1024]; char fmt_with_pfx[1024];

View File

@@ -1,6 +1,9 @@
#ifndef LOG_TRACE_H #ifndef LOG_TRACE_H
#define LOG_TRACE_H #define LOG_TRACE_H
#undef log
#define log log_trace
typedef enum { typedef enum {
ERROR = 1, /* Errors only */ ERROR = 1, /* Errors only */
WARNING, /* Errors & warnings */ WARNING, /* Errors & warnings */
@@ -9,6 +12,6 @@ typedef enum {
} LOG_LEVEL; } LOG_LEVEL;
void log_ssl(void); void log_ssl(void);
void log_trace(LOG_LEVEL lvl, char *fmt, ... ); void log(LOG_LEVEL lvl, char *fmt, ... );
#endif /* LOG_TRACE_H */ #endif /* LOG_TRACE_H */

18
main.c
View File

@@ -5,12 +5,12 @@
#include <signal.h> #include <signal.h>
#include <unistd.h> #include <unistd.h>
#include "confparser.h" #include "confparser.h"
#include "log_trace.h" #include "log.h"
#include "rmps.h" #include "rmps.h"
static void usage(char *argv) static void usage(char *argv)
{ {
log_trace( 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 );
} }
@@ -45,10 +45,10 @@ int main(int argc, char *argv[])
} }
} }
if (confparse() != 0) { if (confparse() != 0) {
log_trace(ERROR, "Failed to parse the conf!"); log(ERROR, "Failed to parse the conf!");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
log_trace(VERBOSE, "Conf parser finished successfully"); log(VERBOSE, "Conf parser finished successfully");
//confexport(); //confexport();
if (task == 2 || task == 3) { if (task == 2 || task == 3) {
@@ -56,16 +56,16 @@ int main(int argc, char *argv[])
int pid; int pid;
FILE *fp; FILE *fp;
if (task == 2) if (task == 2)
log_trace(VERBOSE, "We got a stop signal!"); log(VERBOSE, "We got a stop signal!");
else if (task == 3) else if (task == 3)
log_trace(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");
switch (errno) { switch (errno) {
case EEXIST: case EEXIST:
if (!fgets(buf, 10, fp)) { if (!fgets(buf, 10, fp)) {
log_trace(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);
@@ -73,14 +73,14 @@ int main(int argc, char *argv[])
//waitpid(TODO); //waitpid(TODO);
break; break;
case EACCES: case EACCES:
log_trace(ERROR, "Permission denied to read PID. Exiting!"); log(ERROR, "Permission denied to read PID. Exiting!");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
case ENOENT: case ENOENT:
if (task == 2) if (task == 2)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
break; break;
default: default:
log_trace( ERROR, log( ERROR,
"Unhandled errno while opening PID: %d. Exiting!", "Unhandled errno while opening PID: %d. Exiting!",
errno errno
); );

68
rmps.c
View File

@@ -1,4 +1,4 @@
#include "log_trace.h" #include "log.h"
#include "confparser.h" #include "confparser.h"
#include "thread_pool.h" #include "thread_pool.h"
#include "rmps.h" #include "rmps.h"
@@ -27,9 +27,9 @@ static int pid_file_handle;
static void cleanup(void) static void cleanup(void)
{ {
log_trace(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_trace( 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 );
} }
@@ -38,18 +38,18 @@ static void signal_handler(int sig)
{ {
switch (sig) { switch (sig) {
case SIGHUP: case SIGHUP:
log_trace(WARNING, "Received SIGHUP signal. Ignoring..."); log(WARNING, "Received SIGHUP signal. Ignoring...");
break; break;
case SIGINT: case SIGINT:
case SIGTERM: case SIGTERM:
log_trace(INFO, "Received SIGTERM signal."); log(INFO, "Received SIGTERM signal.");
log_trace(INFO, "RMPS is shutting down..."); log(INFO, "RMPS is shutting down...");
rmps_shutdown(); rmps_shutdown();
log_trace(INFO, "RMPS has been stopped properly."); log(INFO, "RMPS has been stopped properly.");
_exit(EXIT_SUCCESS); _exit(EXIT_SUCCESS);
break; break;
default: default:
log_trace(WARNING, "Unhandled signal %s", strsignal(sig)); log(WARNING, "Unhandled signal %s", strsignal(sig));
break; break;
} }
} }
@@ -97,12 +97,12 @@ static void daemonize(const char *rundir)
pid = fork(); pid = fork();
if (pid < 0) { if (pid < 0) {
/* Could not fork */ /* Could not fork */
log_trace(ERROR, "Failed to fork the parent process. Exiting!"); log(ERROR, "Failed to fork the parent process. Exiting!");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
if (pid > 0) { if (pid > 0) {
/* Child created ok, so exit parent process */ /* Child created ok, so exit parent process */
log_trace(INFO, "Child process created: %d", pid); log(INFO, "Child process created: %d", pid);
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }
/* Child continues */ /* Child continues */
@@ -110,7 +110,7 @@ static void daemonize(const char *rundir)
/* Get a new process group */ /* Get a new process group */
sid = setsid(); sid = setsid();
if (sid < 0) { if (sid < 0) {
log_trace(ERROR, "Failed to create a process group. Exiting!"); log(ERROR, "Failed to create a process group. Exiting!");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/* Close all file descriptors because we fork */ /* Close all file descriptors because we fork */
@@ -134,14 +134,14 @@ static void spawn_pidfile(const char *pidfile)
pid_file_handle = open(pidfile, O_RDWR|O_CREAT, 0600); pid_file_handle = open(pidfile, O_RDWR|O_CREAT, 0600);
if (pid_file_handle == -1) { if (pid_file_handle == -1) {
/* Couldn't open lock file */ /* Couldn't open lock file */
log_trace(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_trace(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);
} }
@@ -169,23 +169,23 @@ static int open_listener(int 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_trace(ERROR, "Failed to create socket"); log(ERROR, "Failed to create socket");
log_trace(INFO, "RMPS failed to start, shutting down..."); log(INFO, "RMPS failed to start, shutting down...");
atexit(cleanup); atexit(cleanup);
} }
if (set_reuse_addr(sd) < 0) { if (set_reuse_addr(sd) < 0) {
log_trace(ERROR, "Failed to set reuse on address - Aborting...", port); log(ERROR, "Failed to set reuse on address - Aborting...", port);
log_trace(INFO, "RMPS failed to start, shutting down..."); log(INFO, "RMPS failed to start, shutting down...");
atexit(cleanup); atexit(cleanup);
} }
if (bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0) { if (bind(sd, (struct sockaddr*)&addr, sizeof(addr)) != 0) {
log_trace(ERROR, "Failed to bind on port: %d - Aborting...", port); log(ERROR, "Failed to bind on port: %d - Aborting...", port);
log_trace(INFO, "RMPS failed to start, shutting down..."); log(INFO, "RMPS failed to start, shutting down...");
atexit(cleanup); atexit(cleanup);
} }
if (listen(sd, 10) != 0) { if (listen(sd, 10) != 0) {
log_trace(ERROR, "Failed to start listener on port %d - Aborting...", port); log(ERROR, "Failed to start listener on port %d - Aborting...", port);
log_trace(INFO, "RMPS failed to start, shutting down..."); log(INFO, "RMPS failed to start, shutting down...");
atexit(cleanup); atexit(cleanup);
} }
return sd; return sd;
@@ -204,8 +204,8 @@ static SSL_CTX* init_server_ctx(const char *cipherlist)
ctx = SSL_CTX_new(TLSv1_2_method()); /* create new context from method */ ctx = SSL_CTX_new(TLSv1_2_method()); /* create new context from method */
if (ctx == NULL) { if (ctx == NULL) {
log_trace(ERROR, "SSL_CTX_new() returned NULL - Aborting..."); log(ERROR, "SSL_CTX_new() returned NULL - Aborting...");
log_trace(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, SSL_VERIFY_PEER | SSL_CTX_set_verify( ctx, SSL_VERIFY_PEER |
@@ -218,7 +218,7 @@ static SSL_CTX* init_server_ctx(const char *cipherlist)
strcat(ciphers, cipherlist); strcat(ciphers, cipherlist);
/* This is very delicate, try to understand the ciphers */ /* This is very delicate, try to understand the ciphers */
SSL_CTX_set_cipher_list(ctx, cipherlist); SSL_CTX_set_cipher_list(ctx, cipherlist);
log_trace(VERBOSE, "cipherlist = %s", cipherlist); log(VERBOSE, "cipherlist = %s", cipherlist);
return ctx; return ctx;
} }
@@ -231,22 +231,22 @@ void load_certificates(SSL_CTX* ctx, const char *certfile,
{ {
/* set the local certificate from certfile */ /* set the local certificate from certfile */
if (SSL_CTX_use_certificate_file(ctx, certfile, SSL_FILETYPE_PEM) <= 0) { if (SSL_CTX_use_certificate_file(ctx, certfile, SSL_FILETYPE_PEM) <= 0) {
log_trace(ERROR, "Failed to load certfile! SSL error below:"); log(ERROR, "Failed to load certfile! SSL error below:");
log_ssl(); log_ssl();
log_trace(INFO, "RMPS failed to start, shutting down..."); log(INFO, "RMPS failed to start, shutting down...");
atexit(cleanup); atexit(cleanup);
} }
/* set the private key from KeyFile (may be the same as CertFile) */ /* set the private key from KeyFile (may be the same as CertFile) */
if (SSL_CTX_use_PrivateKey_file(ctx, keyfile, SSL_FILETYPE_PEM) <= 0) { if (SSL_CTX_use_PrivateKey_file(ctx, keyfile, SSL_FILETYPE_PEM) <= 0) {
log_trace(ERROR, "Failed to load keyfile! SSL error below:"); log(ERROR, "Failed to load keyfile! SSL error below:");
log_ssl(); log_ssl();
log_trace(INFO, "RMPS failed to start, shutting down..."); log(INFO, "RMPS failed to start, shutting down...");
atexit(cleanup); atexit(cleanup);
} }
/* verify private key */ /* verify private key */
if (!SSL_CTX_check_private_key(ctx)) { if (!SSL_CTX_check_private_key(ctx)) {
log_trace(ERROR, "Private key does not match the public certificate."); log(ERROR, "Private key does not match the public certificate.");
log_trace(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) {
@@ -260,7 +260,7 @@ void load_certificates(SSL_CTX* ctx, const char *certfile,
int launch_rmps(struct conf_table *conf, int fork_flag) int launch_rmps(struct conf_table *conf, int fork_flag)
{ {
int server; int server;
log_trace(INFO, "Starting up RMPS..."); log(INFO, "Starting up RMPS...");
/* Set signal handling */ /* Set signal handling */
set_env(); set_env();
@@ -276,13 +276,13 @@ int launch_rmps(struct conf_table *conf, int fork_flag)
* -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
*/ */
log_trace(VERBOSE, "Loading crypto certs and keys."); log(VERBOSE, "Loading crypto certs and keys.");
load_certificates(ctx, conf->rmps.certfile, conf->rmps.keyfile, conf->rmps.cafile); load_certificates(ctx, conf->rmps.certfile, conf->rmps.keyfile, conf->rmps.cafile);
log_trace(VERBOSE, "Starting listener on port: %d", atoi(conf->rmps.bind_on_port)); log(VERBOSE, "Starting listener on port: %d", atoi(conf->rmps.bind_on_port));
server = open_listener(atoi(conf->rmps.bind_on_port)); server = open_listener(atoi(conf->rmps.bind_on_port));
log_trace(VERBOSE, "Creating mutex for thread pool."); log(VERBOSE, "Creating mutex for thread pool.");
ssl_pt_mutex(server, ctx, conf->rmps.threadpoolsize); ssl_pt_mutex(server, ctx, conf->rmps.threadpoolsize);
return 0; return 0;

View File

@@ -1,5 +1,5 @@
#include "thread_pool.h" #include "thread_pool.h"
#include "log_trace.h" #include "log.h"
#include "protocol.h" #include "protocol.h"
#include <pthread.h> #include <pthread.h>
#include <unistd.h> #include <unistd.h>
@@ -24,18 +24,18 @@ static void show_certs(SSL *ssl)
cert = SSL_get_peer_certificate(ssl); /* Get certificates (if available) */ cert = SSL_get_peer_certificate(ssl); /* Get certificates (if available) */
if (SSL_get_verify_result(ssl)==X509_V_OK) if (SSL_get_verify_result(ssl)==X509_V_OK)
log_trace(VERBOSE, "get_verify_result == ok"); log(VERBOSE, "get_verify_result == ok");
if (cert != NULL) { if (cert != NULL) {
log_trace(VERBOSE, "Server certificates:"); log(VERBOSE, "Server certificates:");
line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0); line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0);
log_trace(VERBOSE, "Subject: %s", line); log(VERBOSE, "Subject: %s", line);
free(line); free(line);
line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0); line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0);
log_trace(VERBOSE, "Issuer: %s", line); log(VERBOSE, "Issuer: %s", line);
free(line); free(line);
X509_free(cert); X509_free(cert);
} else } else
log_trace(VERBOSE, "No certificates from peer"); log(VERBOSE, "No certificates from peer");
} }
static void* servlet(void *args) /* Serve the connection -- threadable */ static void* servlet(void *args) /* Serve the connection -- threadable */
@@ -49,7 +49,7 @@ static void* servlet(void *args) /* Serve the connection -- threadable */
ret = SSL_accept(agent->ssl); ret = SSL_accept(agent->ssl);
/* We check for unclean (ret < 0) and clean (ret == 0) failures */ /* We check for unclean (ret < 0) and clean (ret == 0) failures */
if (ret <= 0) { if (ret <= 0) {
log_trace(WARNING, "SSL_accept() failed. Reason below:"); log(WARNING, "SSL_accept() failed. Reason below:");
log_ssl(); log_ssl();
} else { } else {
show_certs(agent->ssl); show_certs(agent->ssl);
@@ -60,26 +60,26 @@ static void* servlet(void *args) /* Serve the connection -- threadable */
bytes = SSL_read(agent->ssl, &buf, sizeof(buf)); bytes = SSL_read(agent->ssl, &buf, sizeof(buf));
if (bytes > 0) { if (bytes > 0) {
if (bytes != sizeof(struct msg)) { if (bytes != sizeof(struct msg)) {
log_trace( WARNING, log( WARNING,
"Agent [%s] sent non-standard data!", "Agent [%s] sent non-standard data!",
agent->ip ); agent->ip );
continue; continue;
} }
log_trace(VERBOSE, "Client msg: \"%s\"", buf.chunk.data); log(VERBOSE, "Client msg: \"%s\"", buf.chunk.data);
/* TODO: Insert msg handler here */ /* TODO: Insert msg handler here */
continue; continue;
} }
if (SSL_get_shutdown(agent->ssl) == SSL_RECEIVED_SHUTDOWN) if (SSL_get_shutdown(agent->ssl) == SSL_RECEIVED_SHUTDOWN)
log_trace(VERBOSE, "SSL_RECEIVED_SHUTDOWN from agent [%s]", agent->ip); log(VERBOSE, "SSL_RECEIVED_SHUTDOWN from agent [%s]", agent->ip);
else { else {
log_trace(VERBOSE, "Client didn't send data! SSL error below:"); log(VERBOSE, "Client didn't send data! SSL error below:");
//log_ssl(); /* We actually don't have anything to log from SSL */ //log_ssl(); /* We actually don't have anything to log from SSL */
sprintf((char*)buf.chunk.data, "%s", "Where's the data, m8?"); sprintf((char*)buf.chunk.data, "%s", "Where's the data, m8?");
SSL_write(agent->ssl, &buf, sizeof(struct msg)); SSL_write(agent->ssl, &buf, sizeof(struct msg));
} }
log_trace(INFO, "Agent [%s] disconnected.", agent->ip); log(INFO, "Agent [%s] disconnected.", agent->ip);
} while (bytes); } while (bytes);
} }
SSL_free(agent->ssl); /* release SSL state */ SSL_free(agent->ssl); /* release SSL state */
@@ -116,7 +116,7 @@ void ssl_pt_mutex(int srv, SSL_CTX *ctx, int poolsize)
socklen_t len = sizeof(addr); socklen_t len = sizeof(addr);
SSL *ssl; SSL *ssl;
int agent = accept(srv, (struct sockaddr*)&addr, &len); int agent = accept(srv, (struct sockaddr*)&addr, &len);
log_trace( INFO, log( INFO,
"Connection: %s:%d", "Connection: %s:%d",
inet_ntop(AF_INET, &addr.sin_addr, address, sizeof(address)), inet_ntop(AF_INET, &addr.sin_addr, address, sizeof(address)),
ntohs(addr.sin_port) ntohs(addr.sin_port)
@@ -139,7 +139,7 @@ void ssl_pt_mutex(int srv, SSL_CTX *ctx, int poolsize)
} }
} }
if (i == poolsize) { if (i == poolsize) {
log_trace( WARNING, log( WARNING,
"Agent [%s] dropped. Poolsize limit reached.", "Agent [%s] dropped. Poolsize limit reached.",
inet_ntop(AF_INET, &addr.sin_addr, address, sizeof(address)) inet_ntop(AF_INET, &addr.sin_addr, address, sizeof(address))
); );