diff --git a/inc/context.h b/inc/context.h index d918c46..6042c06 100644 --- a/inc/context.h +++ b/inc/context.h @@ -3,11 +3,48 @@ #include #include /* for offsetof() macro */ #include +#include #include +#define HTTP_OPTIONS 1 +#define LOGLEVEL_OPTIONS 2 +#define TSA_OPTIONS 3 +#define MAIN_CONF_SECTION "main" + +#define RFC3161_OPTIONS_LEN \ + sizeof(rfc3161_options) / sizeof(struct rfc3161_option) + typedef struct { uint64_t query_counter; bool stdout_dbg; TS_RESP_CTX *resp_ctx; int loglevel; + const char *http_options[40]; } rfc3161_context; + +struct rfc3161_option { + const char *name; + int type; + const char *default_value; +}; + +static struct rfc3161_option rfc3161_options[] = { + {"num_threads", HTTP_OPTIONS, "50"}, + {"run_as_user", HTTP_OPTIONS, NULL}, + {"throttle", HTTP_OPTIONS, NULL}, + {"enable_keep_alive", HTTP_OPTIONS, "no"}, + {"listening_ports", HTTP_OPTIONS, "8080"}, + {"request_timeout_ms", HTTP_OPTIONS, "30000"}, + {"ssl_certificate", HTTP_OPTIONS, NULL}, + {"ssl_verify_peer", HTTP_OPTIONS, "yes"}, + {"ssl_ca_path", HTTP_OPTIONS, NULL}, + {"ssl_ca_file", HTTP_OPTIONS, NULL}, + {"ssl_verify_depth", HTTP_OPTIONS, "9"}, + {"ssl_default_verify_paths", HTTP_OPTIONS, "yes"}, + {"ssl_cipher_list", HTTP_OPTIONS, NULL}, + {"ssl_protocol_version", HTTP_OPTIONS, "0"}, + {"ssl_short_trust", HTTP_OPTIONS, "no"}, + {"access_control_allow_origin", HTTP_OPTIONS, "*"}, + {"tcp_nodelay", HTTP_OPTIONS, "0"}, + {"log_level", LOGLEVEL_OPTIONS, "info"}, +}; diff --git a/inc/utils.h b/inc/utils.h index 2ea144a..3b00f8f 100644 --- a/inc/utils.h +++ b/inc/utils.h @@ -2,3 +2,4 @@ void skeleton_daemon(); void logger(rfc3161_context *ct, int priority, char *fmt, ...); +int set_params(rfc3161_context *ct, char *conf_file); diff --git a/src/lib/http.c b/src/lib/http.c index 4ad7a76..0b6b4b3 100644 --- a/src/lib/http.c +++ b/src/lib/http.c @@ -104,19 +104,17 @@ int http_server_start(char *conffile, bool stdout_dbg) { struct mg_context *ctx; struct mg_callbacks callbacks; - // List of options. Last element must be NULL. - const char *options[] = {"listening_ports", "8080", NULL}; + rfc3161_context *ct = (rfc3161_context *)calloc(1, sizeof(rfc3161_context)); + ct->stdout_dbg = stdout_dbg; + ct->loglevel = 8; + set_params(ct, conffile); // Prepare callbacks structure. We have only one callback, the rest are // NULL. memset(&callbacks, 0, sizeof(callbacks)); - // callbacks.begin_request = begin_request_handler; // Start the web server. - ctx = mg_start(&callbacks, NULL, options); - rfc3161_context *ct = (rfc3161_context *)calloc(1, sizeof(rfc3161_context)); - ct->stdout_dbg = stdout_dbg; - ct->loglevel = 8; + ctx = mg_start(&callbacks, NULL, ct->http_options); mg_set_request_handler(ctx, "/", rfc3161_handler, (void *)ct); // Wait until user hits "enter". Server is running in separate thread. diff --git a/src/lib/utils.c b/src/lib/utils.c index b250694..7e932d2 100644 --- a/src/lib/utils.c +++ b/src/lib/utils.c @@ -82,28 +82,35 @@ void logger(rfc3161_context *ct, int priority, char *fmt, ...) { if (ct->stdout_dbg) { switch (priority) { case LOG_EMERG: - printf("LOG_EMER : %s", out); - ; + printf("LOG_EMER : %s\n", out); + break; case LOG_ALERT: - printf("LOG_ALERT : %s", out); + printf("LOG_ALERT : %s\n", out); + break; ; case LOG_CRIT: - printf("LOG_CRIT : %s", out); + printf("LOG_CRIT : %s\n", out); + break; ; case LOG_ERR: - printf("LOG_ERR : %s", out); + printf("LOG_ERR : %s\n", out); + break; ; case LOG_WARNING: - printf("LOG_WARNING: %s", out); + printf("LOG_WARNING: %s\n", out); + break; ; case LOG_NOTICE: - printf("LOG_NOTICE : %s", out); + printf("LOG_NOTICE : %s\n", out); + break; ; case LOG_INFO: - printf("LOG_INFO : %s", out); + printf("LOG_INFO : %s\n", out); + break; ; case LOG_DEBUG: - printf("LOG_DEBUG : %s", out); + printf("LOG_DEBUG : %s\n", out); + break; ; } } @@ -136,6 +143,10 @@ static CONF *load_config_file(rfc3161_context *ct, const char *filename) { CONF *conf; int i; ct->loglevel = LOG_INFO; + if (filename == NULL) { + logger(ct, LOG_WARNING, "no configuration file passed"); + return NULL; + } in = bio_open_default(ct, filename, 'r'); if (in == NULL) { logger(ct, LOG_CRIT, "Can't load config file \"%s\"", filename); @@ -161,6 +172,38 @@ int set_params(rfc3161_context *ct, char *conf_file) { int ret = 0; CONF *conf = load_config_file(ct, conf_file); ret = 1; + int http_counter = 0; + for (int i = 0; i < RFC3161_OPTIONS_LEN; i++) { + int type = rfc3161_options[i].type; + const char *name = rfc3161_options[i].name; + const char *default_value = rfc3161_options[i].default_value; + const char *value = NCONF_get_string(conf, MAIN_CONF_SECTION, name); + if (value == NULL) { + logger(ct, LOG_NOTICE, + "configuration param['%s'] not set, using default: '%s'", + name, default_value); + value = default_value; + } + logger(ct, LOG_DEBUG, "configuration param['%s'] = '%s'", name, value); + switch (type) { + case HTTP_OPTIONS: + if (value != NULL) { + ct->http_options[http_counter] = name; + http_counter++; + ct->http_options[http_counter] = value; + http_counter++; + } + break; + ; + case LOGLEVEL_OPTIONS: + break; + ; + case TSA_OPTIONS: + break; + ; + } + ct->http_options[http_counter] = NULL; + } // device = NCONF_get_string(conf, section, ENV_CRYPTO_DEVICE); return ret;