More whitespace and shut some warnings up

This commit is contained in:
2017-05-17 18:27:08 +03:00
parent ff8546bf66
commit 6d0a190c2f
7 changed files with 91 additions and 74 deletions

View File

@@ -3,6 +3,7 @@
#include "protocol.h" #include "protocol.h"
#include <pthread.h> #include <pthread.h>
#include <unistd.h> #include <unistd.h>
#include <string.h>
#define MAXJOBS 10 #define MAXJOBS 10
@@ -14,7 +15,7 @@ struct agent_args {
}; };
static void show_certs(SSL *ssl); static void show_certs(SSL *ssl);
static void* servlet(void *args); static void *servlet(void *args);
static void send_reject_msg(SSL *ssl); static void send_reject_msg(SSL *ssl);
static void show_certs(SSL *ssl) static void show_certs(SSL *ssl)
@@ -22,8 +23,9 @@ static void show_certs(SSL *ssl)
X509 *cert; X509 *cert;
char *line; char *line;
cert = SSL_get_peer_certificate(ssl); /* Get certificates (if available) */ /* Get certificates (if available) */
if (SSL_get_verify_result(ssl)==X509_V_OK) cert = SSL_get_peer_certificate(ssl);
if (SSL_get_verify_result(ssl) == X509_V_OK)
log(VERBOSE, "get_verify_result == ok"); log(VERBOSE, "get_verify_result == ok");
if (cert != NULL) { if (cert != NULL) {
log(VERBOSE, "Server certificates:"); log(VERBOSE, "Server certificates:");
@@ -38,12 +40,12 @@ static void show_certs(SSL *ssl)
log(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 */
{ {
struct msg_t buf; struct msg_t buf;
int bytes, ret; int bytes, ret;
//unsigned short job[MAXJOBS] = { 0 }; //unsigned short job[MAXJOBS] = { 0 };
struct agent_args *agent = (struct agent_args*)args; struct agent_args *agent = (struct agent_args *)args;
SSL_load_error_strings(); SSL_load_error_strings();
ret = SSL_accept(agent->ssl); ret = SSL_accept(agent->ssl);
@@ -60,23 +62,28 @@ 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_t)) { if (bytes != sizeof(struct msg_t)) {
log( WARNING, log(WARNING,
"Agent [%s] sent non-standard data!", "Agent [%s] sent non-standard data!",
agent->ip ); agent->ip);
continue; continue;
} }
log(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(VERBOSE, "SSL_RECEIVED_SHUTDOWN from agent [%s]", agent->ip); log(VERBOSE,
"SSL_RECEIVED_SHUTDOWN from agent [%s]",
agent->ip);
else { else {
log(VERBOSE, "Client didn't send data! SSL error below:"); log(VERBOSE,
//log_ssl(); /* We actually don't have anything to log from SSL */ "Client didn't send data! SSL error below:");
sprintf((char*)buf.chunk.data, "%s", "Where's the data, m8?"); /* I think logging is NOT needed here */
//log_ssl();
sprintf((char *)buf.chunk.data, "%s",
"Where's the data, m8?");
SSL_write(agent->ssl, &buf, sizeof(struct msg_t)); SSL_write(agent->ssl, &buf, sizeof(struct msg_t));
} }
log(INFO, "Agent [%s] disconnected.", agent->ip); log(INFO, "Agent [%s] disconnected.", agent->ip);
@@ -92,17 +99,19 @@ static void* servlet(void *args) /* Serve the connection -- threadable */
static void send_reject_msg(SSL *ssl) static void send_reject_msg(SSL *ssl)
{ {
char *reply = "FAILURE - The connection queue is full!\n"; char *reply = "FAILURE - The connection queue is full!\n";
SSL_write(ssl, reply, strlen(reply)); SSL_write(ssl, reply, strlen(reply));
} }
void* agent_pool(void *args) void *agent_pool(void *args)
{ {
struct pool_data *pool = args; struct pool_data *pool = args;
pthread_mutex_t mutex; pthread_mutex_t mutex;
pthread_attr_t attr; pthread_attr_t attr;
pthread_t *agent_thread = (pthread_t*)malloc(pool->size * sizeof(pthread_t)); pthread_t *agent_thread =
(pthread_t *)malloc(pool->size * sizeof(pthread_t));
struct agent_args *agent_struct = struct agent_args *agent_struct =
(struct agent_args*)malloc(pool->size * sizeof(struct agent_args)); (struct agent_args *)malloc(pool->size * sizeof(struct agent_args));
int i; int i;
memset(agent_thread, 0, sizeof(pthread_t) * pool->size); memset(agent_thread, 0, sizeof(pthread_t) * pool->size);
@@ -117,10 +126,12 @@ void* agent_pool(void *args)
char address[INET6_ADDRSTRLEN]; char address[INET6_ADDRSTRLEN];
socklen_t len = sizeof(addr); socklen_t len = sizeof(addr);
SSL *ssl; SSL *ssl;
int agent = accept(pool->srv, (struct sockaddr*)&addr, &len); int agent = accept(pool->srv, (struct sockaddr *)&addr, &len);
log(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));
for (i = 0; i < pool->size; i++) { for (i = 0; i < pool->size; i++) {
@@ -128,22 +139,24 @@ void* agent_pool(void *args)
agent_struct[i].busy = 1; agent_struct[i].busy = 1;
agent_struct[i].ssl = SSL_new(pool->ctx); agent_struct[i].ssl = SSL_new(pool->ctx);
agent_struct[i].sd = agent; agent_struct[i].sd = agent;
memcpy( agent_struct[i].ip, memcpy(agent_struct[i].ip,
inet_ntop(AF_INET, &addr.sin_addr, address, sizeof(address)), inet_ntop(AF_INET, &addr.sin_addr,
sizeof(agent_struct[i].ip) ); address, sizeof(address)),
SSL_set_fd(agent_struct[i].ssl, agent_struct[i].sd); sizeof(agent_struct[i].ip));
pthread_create( &agent_thread[i], SSL_set_fd(agent_struct[i].ssl,
agent_struct[i].sd);
pthread_create(&agent_thread[i],
&attr, &attr,
servlet, servlet,
&agent_struct[i] ); &agent_struct[i]);
break; break;
} }
} }
if (i == pool->size) { if (i == pool->size) {
log( 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)));
ssl = SSL_new(pool->ctx); ssl = SSL_new(pool->ctx);
SSL_set_fd(ssl, agent); SSL_set_fd(ssl, agent);
if (SSL_accept(ssl) == FAIL) { if (SSL_accept(ssl) == FAIL) {

View File

@@ -4,6 +4,7 @@
#include "protocol.h" #include "protocol.h"
#include <pthread.h> #include <pthread.h>
#include <unistd.h> #include <unistd.h>
#include <string.h>
#define MAXJOBS 10 #define MAXJOBS 10

View File

@@ -7,7 +7,7 @@ enum ERROR_CODES {
CONF_DIR_NOTDIR, /* 102 */ CONF_DIR_NOTDIR, /* 102 */
CONF_MISSING, /* 103 */ CONF_MISSING, /* 103 */
CONF_PERM, /* 104 */ CONF_PERM, /* 104 */
CONF_NOTFILE /* 105 */ CONF_NOTFILE, /* 105 */
}; };
enum WARN_CODES { enum WARN_CODES {
@@ -16,7 +16,7 @@ enum WARN_CODES {
CONF_DIR_PERM_INSECURE, CONF_DIR_PERM_INSECURE,
CONF_FILE_GID_INSECURE, CONF_FILE_GID_INSECURE,
CONF_FILE_UID_INSECURE, CONF_FILE_UID_INSECURE,
CONF_FILE_PERM_INSECURE CONF_FILE_PERM_INSECURE,
}; };
extern void enumtostr(char *scode, int code); extern void enumtostr(char *scode, int code);

View File

@@ -21,8 +21,8 @@ enum msg_types {
struct msg_meta_t { struct msg_meta_t {
unsigned short id; /* Agent job ID */ unsigned short id; /* Agent job ID */
unsigned short type; /* Data type */ unsigned short type; /* Data type */
unsigned len; /* Data size to expect in buffer */ unsigned int len; /* Data size to expect in buffer */
unsigned chunks; unsigned int chunks;
short is_recv; short is_recv;
short locking; short locking;
short isjob; short isjob;

3
rmps.c
View File

@@ -5,6 +5,7 @@
#include "job_queue.h" #include "job_queue.h"
#include "rmps.h" #include "rmps.h"
#include <pthread.h> #include <pthread.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
@@ -209,7 +210,7 @@ static SSL_CTX* init_server_ctx(const char *cipherlist, int mode)
SSL_library_init(); SSL_library_init();
/* create new context from method */ /* create new context from method */
ctx = SSL_CTX_new(TLSv1_2_method()); ctx = SSL_CTX_new(TLS_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...");

8
sql.c
View File

@@ -5,10 +5,11 @@
#include "sql.h" #include "sql.h"
#include "confparser.h" #include "confparser.h"
int add_user() int add_user(void)
{ {
MYSQL *con = mysql_init(NULL); MYSQL *con = mysql_init(NULL);
char sql[200]; char sql[200];
if (con == NULL) { if (con == NULL) {
log(ERROR, "Failed to add user: %s", mysql_error(con)); log(ERROR, "Failed to add user: %s", mysql_error(con));
return -1; return -1;
@@ -28,6 +29,7 @@ int add_user()
exit(1); exit(1);
} }
MYSQL_RES *result = mysql_store_result(con); MYSQL_RES *result = mysql_store_result(con);
if (result == NULL) { if (result == NULL) {
log(ERROR, "Failed to add user: %s", mysql_error(con)); log(ERROR, "Failed to add user: %s", mysql_error(con));
return -1; return -1;
@@ -39,11 +41,11 @@ int add_user()
while ((row = mysql_fetch_row(result))) { while ((row = mysql_fetch_row(result))) {
int i; int i;
for (i = 0; i < num_fields; i++) { for (i = 0; i < num_fields; i++) {
if (i == 0) { if (i == 0) {
while(field = mysql_fetch_field(result)) { while ((field = mysql_fetch_field(result)))
printf("| %s ", field->name); printf("| %s ", field->name);
}
printf("\n"); printf("\n");
} }
printf("| %s ", row[i] ? row[i] : "NULL"); printf("| %s ", row[i] ? row[i] : "NULL");

2
sql.h
View File

@@ -1,7 +1,7 @@
#ifndef SQL_H #ifndef SQL_H
#define SQL_H #define SQL_H
int add_user(); int add_user(void);
#endif /* SQL_H */ #endif /* SQL_H */