diff --git a/agent_pool.c b/agent_pool.c index 87b3191..b3e8533 100644 --- a/agent_pool.c +++ b/agent_pool.c @@ -3,6 +3,7 @@ #include "protocol.h" #include #include +#include #define MAXJOBS 10 @@ -14,42 +15,43 @@ struct agent_args { }; 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 show_certs(SSL *ssl) { - X509 *cert; - char *line; + X509 *cert; + char *line; - cert = SSL_get_peer_certificate(ssl); /* Get certificates (if available) */ - if (SSL_get_verify_result(ssl)==X509_V_OK) + /* Get certificates (if available) */ + cert = SSL_get_peer_certificate(ssl); + if (SSL_get_verify_result(ssl) == X509_V_OK) log(VERBOSE, "get_verify_result == ok"); - if (cert != NULL) { - log(VERBOSE, "Server certificates:"); - line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0); - log(VERBOSE, "Subject: %s", line); - free(line); - line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0); - log(VERBOSE, "Issuer: %s", line); - free(line); - X509_free(cert); - } else - log(VERBOSE, "No certificates from peer"); + if (cert != NULL) { + log(VERBOSE, "Server certificates:"); + line = X509_NAME_oneline(X509_get_subject_name(cert), 0, 0); + log(VERBOSE, "Subject: %s", line); + free(line); + line = X509_NAME_oneline(X509_get_issuer_name(cert), 0, 0); + log(VERBOSE, "Issuer: %s", line); + free(line); + X509_free(cert); + } else + 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; - int bytes, ret; + struct msg_t buf; + int bytes, ret; //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(); ret = SSL_accept(agent->ssl); /* We check for unclean (ret < 0) and clean (ret == 0) failures */ if (ret <= 0) { - log(WARNING, "SSL_accept() failed. Reason below:"); + log(WARNING, "SSL_accept() failed. Reason below:"); log_ssl(); } else { show_certs(agent->ssl); @@ -60,23 +62,28 @@ static void* servlet(void *args) /* Serve the connection -- threadable */ bytes = SSL_read(agent->ssl, &buf, sizeof(buf)); if (bytes > 0) { if (bytes != sizeof(struct msg_t)) { - log( WARNING, + log(WARNING, "Agent [%s] sent non-standard data!", - agent->ip ); + agent->ip); continue; } - - log(VERBOSE, "Client msg: \"%s\"", buf.chunk.data); - /* TODO: Insert msg handler here */ + log(VERBOSE, "Client msg: \"%s\"", + buf.chunk.data); + /* TODO: Insert msg handler here */ continue; } 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 { - log(VERBOSE, "Client didn't send data! SSL error below:"); - //log_ssl(); /* We actually don't have anything to log from SSL */ - sprintf((char*)buf.chunk.data, "%s", "Where's the data, m8?"); + log(VERBOSE, + "Client didn't send data! SSL error below:"); + /* 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)); } log(INFO, "Agent [%s] disconnected.", agent->ip); @@ -92,58 +99,64 @@ static void* servlet(void *args) /* Serve the connection -- threadable */ static void send_reject_msg(SSL *ssl) { char *reply = "FAILURE - The connection queue is full!\n"; + SSL_write(ssl, reply, strlen(reply)); } -void* agent_pool(void *args) +void *agent_pool(void *args) { struct pool_data *pool = args; pthread_mutex_t mutex; 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*)malloc(pool->size * sizeof(struct agent_args)); + (struct agent_args *)malloc(pool->size * sizeof(struct agent_args)); int i; - + memset(agent_thread, 0, sizeof(pthread_t) * pool->size); memset(agent_struct, 0, sizeof(struct agent_args) * pool->size); - + pthread_mutex_init(&mutex, NULL); pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); while (1) { struct sockaddr_in addr; - char address[INET6_ADDRSTRLEN]; - socklen_t len = sizeof(addr); - SSL *ssl; - int agent = accept(pool->srv, (struct sockaddr*)&addr, &len); - log(INFO, + char address[INET6_ADDRSTRLEN]; + socklen_t len = sizeof(addr); + SSL *ssl; + int agent = accept(pool->srv, (struct sockaddr *)&addr, &len); + + log(INFO, "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)); - + for (i = 0; i < pool->size; i++) { if (!agent_struct[i].busy) { agent_struct[i].busy = 1; agent_struct[i].ssl = SSL_new(pool->ctx); agent_struct[i].sd = agent; - memcpy( agent_struct[i].ip, - inet_ntop(AF_INET, &addr.sin_addr, address, sizeof(address)), - sizeof(agent_struct[i].ip) ); - SSL_set_fd(agent_struct[i].ssl, agent_struct[i].sd); - pthread_create( &agent_thread[i], + memcpy(agent_struct[i].ip, + inet_ntop(AF_INET, &addr.sin_addr, + address, sizeof(address)), + sizeof(agent_struct[i].ip)); + SSL_set_fd(agent_struct[i].ssl, + agent_struct[i].sd); + pthread_create(&agent_thread[i], &attr, servlet, - &agent_struct[i] ); + &agent_struct[i]); break; } } if (i == pool->size) { - log( WARNING, - "Agent [%s] dropped. Poolsize limit reached.", - inet_ntop(AF_INET, &addr.sin_addr, address, sizeof(address)) - ); + log(WARNING, + "Agent [%s] dropped. Poolsize limit reached.", + inet_ntop(AF_INET, &addr.sin_addr, + address, sizeof(address))); ssl = SSL_new(pool->ctx); SSL_set_fd(ssl, agent); if (SSL_accept(ssl) == FAIL) { diff --git a/client_pool.c b/client_pool.c index f084e27..db2a253 100644 --- a/client_pool.c +++ b/client_pool.c @@ -4,6 +4,7 @@ #include "protocol.h" #include #include +#include #define MAXJOBS 10 diff --git a/enum_functions.h b/enum_functions.h index 485b891..b38916e 100644 --- a/enum_functions.h +++ b/enum_functions.h @@ -3,11 +3,11 @@ enum ERROR_CODES { CONF_DIR_MISSING = 100, - CONF_DIR_PERM, /* 101 */ + CONF_DIR_PERM, /* 101 */ CONF_DIR_NOTDIR, /* 102 */ CONF_MISSING, /* 103 */ CONF_PERM, /* 104 */ - CONF_NOTFILE /* 105 */ + CONF_NOTFILE, /* 105 */ }; enum WARN_CODES { @@ -16,7 +16,7 @@ enum WARN_CODES { CONF_DIR_PERM_INSECURE, CONF_FILE_GID_INSECURE, CONF_FILE_UID_INSECURE, - CONF_FILE_PERM_INSECURE + CONF_FILE_PERM_INSECURE, }; extern void enumtostr(char *scode, int code); diff --git a/protocol.h b/protocol.h index 91370b3..43c7806 100644 --- a/protocol.h +++ b/protocol.h @@ -21,8 +21,8 @@ enum msg_types { struct msg_meta_t { unsigned short id; /* Agent job ID */ unsigned short type; /* Data type */ - unsigned len; /* Data size to expect in buffer */ - unsigned chunks; + unsigned int len; /* Data size to expect in buffer */ + unsigned int chunks; short is_recv; short locking; short isjob; diff --git a/rmps.c b/rmps.c index bb6ec41..26725fd 100644 --- a/rmps.c +++ b/rmps.c @@ -5,6 +5,7 @@ #include "job_queue.h" #include "rmps.h" #include +#include #include #include #include @@ -209,7 +210,7 @@ static SSL_CTX* init_server_ctx(const char *cipherlist, int mode) SSL_library_init(); /* create new context from method */ - ctx = SSL_CTX_new(TLSv1_2_method()); + ctx = SSL_CTX_new(TLS_method()); if (ctx == NULL) { log(ERROR, "SSL_CTX_new() returned NULL - Aborting..."); log(ERROR, "RMPS failed to start, shutting down..."); diff --git a/sql.c b/sql.c index c7759e3..27f2a70 100644 --- a/sql.c +++ b/sql.c @@ -5,29 +5,31 @@ #include "sql.h" #include "confparser.h" -int add_user() +int add_user(void) { MYSQL *con = mysql_init(NULL); char sql[200]; + if (con == NULL) { - log(ERROR, "Failed to add user: %s", mysql_error(con)); - return -1; + log(ERROR, "Failed to add user: %s", mysql_error(con)); + return -1; } - if (mysql_real_connect(con, conf_db_hostname(), "rmps", conf_db_pass(), + if (mysql_real_connect(con, conf_db_hostname(), "rmps", conf_db_pass(), NULL, 0, NULL, 0) == NULL) { - log(ERROR, "Failed to add user: %s", mysql_error(con)); - mysql_close(con); - return -1; + log(ERROR, "Failed to add user: %s", mysql_error(con)); + mysql_close(con); + return -1; } sprintf(sql, "call addUser(`%s`, `%s`, `%s`, `%s`, `%s`, `%s`)", "user", "noob", "asd@asd.asd", "asdsadasdassda", "salt", "more"); if (mysql_query(con, sql)) { - //fprintf(stderr, "%s\n", mysql_error(con)); - - mysql_close(con); - exit(1); + //fprintf(stderr, "%s\n", mysql_error(con)); + + mysql_close(con); + exit(1); } MYSQL_RES *result = mysql_store_result(con); + if (result == NULL) { log(ERROR, "Failed to add user: %s", mysql_error(con)); return -1; @@ -39,12 +41,12 @@ int add_user() while ((row = mysql_fetch_row(result))) { int i; + for (i = 0; i < num_fields; i++) { if (i == 0) { - while(field = mysql_fetch_field(result)) { + while ((field = mysql_fetch_field(result))) printf("| %s ", field->name); - } - printf("\n"); + printf("\n"); } printf("| %s ", row[i] ? row[i] : "NULL"); } diff --git a/sql.h b/sql.h index 4d892c0..bf35419 100644 --- a/sql.h +++ b/sql.h @@ -1,7 +1,7 @@ #ifndef SQL_H #define SQL_H -int add_user(); +int add_user(void); #endif /* SQL_H */