From 3ce72975b898953408616b426f3710ea5d3f7854 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Thu, 15 Dec 2022 10:43:43 +0100 Subject: [PATCH] Remove shm from config --- src/client.c | 6 +-- src/client.h | 2 +- src/lib/config.c | 118 ++++++++++------------------------------------- src/lib/config.h | 17 +++---- src/lib/proxy.c | 4 +- src/lib/proxy.h | 4 +- src/lib/uri.h | 10 ++-- src/server.c | 40 +++------------- 8 files changed, 53 insertions(+), 148 deletions(-) diff --git a/src/client.c b/src/client.c index 4cafa78..bb81b60 100644 --- a/src/client.c +++ b/src/client.c @@ -36,9 +36,9 @@ struct timeval client_timeout = {.tv_sec = CLIENT_TIMEOUT, .tv_usec = 0}; static const char *color_table[] = {"\x1B[31m", "\x1B[32m", "\x1B[33m", "\x1B[34m", "\x1B[35m", "\x1B[36m"}; -host_config *get_host_config(const char *host) { +host_config_t *get_host_config(const char *host) { for (int i = 0; i < CONFIG_MAX_HOST_CONFIG; i++) { - host_config *hc = &config->hosts[i]; + host_config_t *hc = &config.hosts[i]; if (hc->type == CONFIG_TYPE_UNSET) break; if (strcmp(hc->name, host) == 0) return hc; if (hc->name[0] == '*' && hc->name[1] == '.') { @@ -70,7 +70,7 @@ int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long clien err_msg[0] = 0; msg_content[0] = 0; - host_config *conf = NULL; + host_config_t *conf = NULL; FILE *file = NULL; long content_length = 0; diff --git a/src/client.h b/src/client.h index b585282..ba168c0 100644 --- a/src/client.h +++ b/src/client.h @@ -25,7 +25,7 @@ typedef struct { char _s_addr[INET6_ADDRSTRLEN + 1]; } client_ctx_t; -host_config *get_host_config(const char *host); +host_config_t *get_host_config(const char *host); int client_handler(sock *client, unsigned long client_num); diff --git a/src/lib/config.c b/src/lib/config.c index 18dbd5b..bfdde47 100644 --- a/src/lib/config.c +++ b/src/lib/config.c @@ -8,60 +8,15 @@ #include "../logger.h" #include "config.h" -#include "utils.h" #include -#include -#include #include -#include #include -t_config *config; +config_t config; char geoip_dir[256], dns_server[256]; -int config_init(void) { - int shm_id = shmget(CONFIG_SHM_KEY, sizeof(t_config), IPC_CREAT | IPC_EXCL | 0640); - if (shm_id < 0) { - critical("Unable to create config shared memory"); - return -1; - } - - void *shm = shmat(shm_id, NULL, SHM_RDONLY); - if (shm == (void *) -1) { - critical("Unable to attach config shared memory (ro)"); - return -2; - } - config = shm; - - void *shm_rw = shmat(shm_id, NULL, 0); - if (shm_rw == (void *) -1) { - critical("Unable to attach config shared memory (rw)"); - return -3; - } - config = shm_rw; - memset(config, 0, sizeof(t_config)); - shmdt(shm_rw); - config = shm; - return 0; -} - -int config_unload(void) { - int shm_id = shmget(CONFIG_SHM_KEY, 0, 0); - if (shm_id < 0) { - critical("Unable to get config shared memory id"); - shmdt(config); - return -1; - } else if (shmctl(shm_id, IPC_RMID, NULL) < 0) { - critical("Unable to configure config shared memory"); - shmdt(config); - return -1; - } - shmdt(config); - return 0; -} - int config_load(const char *filename) { FILE *file = fopen(filename, "r"); if (file == NULL) { @@ -69,30 +24,23 @@ int config_load(const char *filename) { return -1; } - fseek(file, 0, SEEK_END); - unsigned long len = ftell(file); - fseek(file, 0, SEEK_SET); - char *conf = alloca(len + 1); - fread(conf, 1, len, file); - conf[len] = 0; - fclose(file); - - t_config *tmp_config = malloc(sizeof(t_config)); - memset(tmp_config, 0, sizeof(t_config)); - int i = 0; int j = 0; - int line = 0; + int line_num = 0; int mode = 0; char section = 0; - char *ptr = NULL; char *source, *target; - while ((ptr = strsep(&conf, "\r\n")) != NULL) { - line++; - char *comment = strchr(ptr, '#'); + + char *line = NULL; + ssize_t read; + size_t line_len = 0; + while ((read = getline(&line, &line_len, file)) != -1) { + line_num++; + char *ptr = line; + char *comment = strpbrk(ptr, "#\r\n"); if (comment != NULL) comment[0] = 0; - len = strlen(ptr); + unsigned long len = strlen(ptr); char *end_ptr = ptr + len - 1; while (end_ptr[0] == ' ' || end_ptr[0] == '\t') { end_ptr[0] = 0; @@ -110,7 +58,7 @@ int config_load(const char *filename) { while (ptr[0] == ' ' || ptr[0] == '\t' || ptr[0] == ']') ptr++; while (ptr[l] != ' ' && ptr[l] != '\t' && ptr[l] != ']') l++; if (l == 0) goto err; - snprintf(tmp_config->hosts[i].name, sizeof(tmp_config->hosts[i].name), "%.*s", l, ptr); + snprintf(config.hosts[i].name, sizeof(config.hosts[i].name), "%.*s", l, ptr); i++; section = 'h'; } else if (strncmp(ptr, "cert", 4) == 0 && (ptr[4] == ' ' || ptr[4] == '\t')) { @@ -118,7 +66,7 @@ int config_load(const char *filename) { while (ptr[0] == ' ' || ptr[0] == '\t' || ptr[0] == ']') ptr++; while (ptr[l] != ' ' && ptr[l] != '\t' && ptr[l] != ']') l++; if (l == 0) goto err; - snprintf(tmp_config->certs[j].name, sizeof(tmp_config->certs[j].name), "%.*s", l, ptr); + snprintf(config.certs[j].name, sizeof(config.certs[j].name), "%.*s", l, ptr); j++; section = 'c'; } else { @@ -136,7 +84,7 @@ int config_load(const char *filename) { goto err; } } else if (section == 'c') { - cert_config *cc = &tmp_config->certs[j - 1]; + cert_config_t *cc = &config.certs[j - 1]; if (len > 12 && strncmp(ptr, "certificate", 11) == 0 && (ptr[11] == ' ' || ptr[11] == '\t')) { source = ptr + 11; target = cc->full_chain; @@ -147,7 +95,7 @@ int config_load(const char *filename) { goto err; } } else if (section == 'h') { - host_config *hc = &tmp_config->hosts[i - 1]; + host_config_t *hc = &config.hosts[i - 1]; if (len > 8 && strncmp(ptr, "webroot", 7) == 0 && (ptr[7] == ' ' || ptr[7] == '\t')) { source = ptr + 7; target = hc->local.webroot; @@ -211,8 +159,7 @@ int config_load(const char *filename) { while (source[0] == ' ' || source[0] == '\t') source++; if (strlen(source) == 0) { err: - free(tmp_config); - critical("Unable to parse config file (line %i)", line); + critical("Unable to parse config file (line_num %i)", line_num); return -2; } @@ -220,23 +167,25 @@ int config_load(const char *filename) { strcpy(target, source); } else if (mode == 1) { if (strcmp(source, "forbidden") == 0) { - tmp_config->hosts[i - 1].local.dir_mode = URI_DIR_MODE_FORBIDDEN; + config.hosts[i - 1].local.dir_mode = URI_DIR_MODE_FORBIDDEN; } else if (strcmp(source, "info") == 0) { - tmp_config->hosts[i - 1].local.dir_mode = URI_DIR_MODE_INFO; + config.hosts[i - 1].local.dir_mode = URI_DIR_MODE_INFO; } else if (strcmp(source, "list") == 0) { - tmp_config->hosts[i - 1].local.dir_mode = URI_DIR_MODE_LIST; + config.hosts[i - 1].local.dir_mode = URI_DIR_MODE_LIST; } else { goto err; } } else if (mode == 2) { - tmp_config->hosts[i - 1].proxy.port = (unsigned short) strtoul(source, NULL, 10); + config.hosts[i - 1].proxy.port = (unsigned short) strtoul(source, NULL, 10); } } + free(line); + for (int k = 0; k < i; k++) { - host_config *hc = &tmp_config->hosts[k]; + host_config_t *hc = &config.hosts[k]; if (hc->type == CONFIG_TYPE_LOCAL) { - char *webroot = tmp_config->hosts[k].local.webroot; + char *webroot = config.hosts[k].local.webroot; if (webroot[strlen(webroot) - 1] == '/') { webroot[strlen(webroot) - 1] = 0; } @@ -244,7 +193,7 @@ int config_load(const char *filename) { if (hc->cert_name[0] == 0) goto err2; int found = 0; for (int m = 0; m < j; m++) { - if (strcmp(tmp_config->certs[m].name, hc->cert_name) == 0) { + if (strcmp(config.certs[m].name, hc->cert_name) == 0) { hc->cert = m; found = 1; break; @@ -252,27 +201,10 @@ int config_load(const char *filename) { } if (!found) { err2: - free(tmp_config); critical("Unable to parse config file"); return -2; } } - int shm_id = shmget(CONFIG_SHM_KEY, 0, 0); - if (shm_id < 0) { - critical("Unable to get config shared memory id"); - shmdt(config); - return -3; - } - - void *shm_rw = shmat(shm_id, NULL, 0); - if (shm_rw == (void *) -1) { - free(tmp_config); - critical("Unable to attach config shared memory (rw)"); - return -4; - } - memcpy(shm_rw, tmp_config, sizeof(t_config)); - free(tmp_config); - shmdt(shm_rw); return 0; } diff --git a/src/lib/config.h b/src/lib/config.h index c3c84e0..e3aae9f 100644 --- a/src/lib/config.h +++ b/src/lib/config.h @@ -11,7 +11,6 @@ #include "uri.h" -#define CONFIG_SHM_KEY 255642 #define CONFIG_MAX_HOST_CONFIG 64 #define CONFIG_MAX_CERT_CONFIG 64 @@ -40,26 +39,22 @@ typedef struct { unsigned char dir_mode:2; } local; }; -} host_config; +} host_config_t; typedef struct { char name[256]; char full_chain[256]; char priv_key[256]; -} cert_config; +} cert_config_t; typedef struct { - host_config hosts[CONFIG_MAX_HOST_CONFIG]; - cert_config certs[CONFIG_MAX_CERT_CONFIG]; -} t_config; + host_config_t hosts[CONFIG_MAX_HOST_CONFIG]; + cert_config_t certs[CONFIG_MAX_CERT_CONFIG]; +} config_t; -extern t_config *config; +extern config_t config; extern char geoip_dir[256], dns_server[256]; -int config_init(void); - int config_load(const char *filename); -int config_unload(void); - #endif //SESIMOS_CONFIG_H diff --git a/src/lib/proxy.c b/src/lib/proxy.c index 69b0c63..b8e0582 100644 --- a/src/lib/proxy.c +++ b/src/lib/proxy.c @@ -128,7 +128,7 @@ int proxy_request_header(http_req *req, int enc, client_ctx_t *ctx) { return 0; } -int proxy_response_header(http_req *req, http_res *res, host_config *conf) { +int proxy_response_header(http_req *req, http_res *res, host_config_t *conf) { char buf1[256], buf2[256]; int p_len; @@ -180,7 +180,7 @@ int proxy_response_header(http_req *req, http_res *res, host_config *conf) { return 0; } -int proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config *conf, sock *client, client_ctx_t *cctx, http_status *custom_status, char *err_msg) { +int proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config_t *conf, sock *client, client_ctx_t *cctx, http_status *custom_status, char *err_msg) { char buffer[CHUNK_SIZE]; const char *connection, *upgrade, *ws_version; long ret; diff --git a/src/lib/proxy.h b/src/lib/proxy.h index 0dad53b..0009412 100644 --- a/src/lib/proxy.h +++ b/src/lib/proxy.h @@ -28,9 +28,9 @@ int proxy_preload(void); int proxy_request_header(http_req *req, int enc, client_ctx_t *ctx); -int proxy_response_header(http_req *req, http_res *res, host_config *conf); +int proxy_response_header(http_req *req, http_res *res, host_config_t *conf); -int proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config *conf, sock *client, client_ctx_t *cctx, http_status *custom_status, char *err_msg); +int proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config_t *conf, sock *client, client_ctx_t *cctx, http_status *custom_status, char *err_msg); int proxy_send(sock *client, unsigned long len_to_send, int flags); diff --git a/src/lib/uri.h b/src/lib/uri.h index da9b016..6cba1c3 100644 --- a/src/lib/uri.h +++ b/src/lib/uri.h @@ -16,10 +16,14 @@ #define URI_DIR_MODE_LIST 2 #define URI_DIR_MODE_INFO 3 +#define URI_ETAG_SIZE 64 // SHA256 hex len +#define URI_TYPE_SIZE 64 +#define URI_CHARSET_SIZE 16 + typedef struct { - char etag[64]; - char type[24]; - char charset[16]; + char etag[URI_ETAG_SIZE]; + char type[URI_TYPE_SIZE]; + char charset[URI_CHARSET_SIZE]; char filename_comp_gz[256]; char filename_comp_br[256]; struct stat stat; diff --git a/src/server.c b/src/server.c index ade1b4b..8600512 100644 --- a/src/server.c +++ b/src/server.c @@ -43,7 +43,7 @@ SSL_CTX *contexts[CONFIG_MAX_CERT_CONFIG]; static int ssl_servername_cb(SSL *ssl, int *ad, void *arg) { const char *servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); if (servername != NULL) { - const host_config *conf = get_host_config(servername); + const host_config_t *conf = get_host_config(servername); if (conf != NULL) SSL_set_SSL_CTX(ssl, contexts[conf->cert]); } return SSL_TLSEXT_ERR_OK; @@ -76,7 +76,6 @@ void terminate_forcefully(int sig) { notice("Killed %i child process(es)", kills); } cache_unload(); - config_unload(); geoip_free(); exit(2); } @@ -142,7 +141,6 @@ void terminate_gracefully(int sig) { info("Goodbye"); cache_unload(); - config_unload(); geoip_free(); exit(0); } @@ -173,11 +171,6 @@ int main(int argc, const char *argv[]) { } printf("Sesimos web server " SERVER_VERSION "\n"); - ret = config_init(); - if (ret != 0) { - return 1; - } - config_file = NULL; for (int i = 1; i < argc; i++) { const char *arg = argv[i]; @@ -187,51 +180,38 @@ int main(int argc, const char *argv[]) { "Options:\n" " -c, --config path to the config file. If not provided, default will be used\n" " -h, --help print this dialogue\n"); - config_unload(); return 0; } else if (strcmp(arg, "-c") == 0 || strcmp(arg, "--config") == 0) { if (i == argc - 1) { critical("Unable to parse argument %s, usage: --config ", arg); - config_unload(); return 1; } config_file = argv[++i]; } else { critical("Unable to parse argument '%s'", arg); - config_unload(); return 1; } } - ret = config_load(config_file == NULL ? DEFAULT_CONFIG_FILE : config_file); - if (ret != 0) { - config_unload(); + if (config_load(config_file == NULL ? DEFAULT_CONFIG_FILE : config_file) != 0) return 1; - } - sockets[0] = socket(AF_INET6, SOCK_STREAM, 0); - if (sockets[0] < 0) goto socket_err; - sockets[1] = socket(AF_INET6, SOCK_STREAM, 0); - if (sockets[1] < 0) { - socket_err: + if ((sockets[0] = socket(AF_INET6, SOCK_STREAM, 0)) == - 1 || (sockets[1] = socket(AF_INET6, SOCK_STREAM, 0)) == -1) { critical("Unable to create socket"); - config_unload(); return 1; } for (int i = 0; i < NUM_SOCKETS; i++) { if (setsockopt(sockets[i], SOL_SOCKET, SO_REUSEADDR, &YES, sizeof(YES)) < 0) { critical("Unable to set options for socket %i", i); - config_unload(); return 1; } } - if (bind(sockets[0], (struct sockaddr *) &addresses[0], sizeof(addresses[0])) < 0) goto bind_err; - if (bind(sockets[1], (struct sockaddr *) &addresses[1], sizeof(addresses[1])) < 0) { - bind_err: + if (bind(sockets[0], (struct sockaddr *) &addresses[0], sizeof(addresses[0])) == -1 || + bind(sockets[1], (struct sockaddr *) &addresses[1], sizeof(addresses[1])) == -1) + { critical("Unable to bind socket to address"); - config_unload(); return 1; } @@ -242,13 +222,11 @@ int main(int argc, const char *argv[]) { if (ret == -1) { critical("Unable to initialize geoip"); } - config_unload(); return 1; } ret = cache_init(); if (ret < 0) { - config_unload(); geoip_free(); return 1; } else if (ret != 0) { @@ -258,7 +236,7 @@ int main(int argc, const char *argv[]) { } for (int i = 0; i < CONFIG_MAX_CERT_CONFIG; i++) { - const cert_config *conf = &config->certs[i]; + const cert_config_t *conf = &config.certs[i]; if (conf->name[0] == 0) break; contexts[i] = SSL_CTX_new(TLS_server_method()); @@ -273,14 +251,12 @@ int main(int argc, const char *argv[]) { if (SSL_CTX_use_certificate_chain_file(ctx, conf->full_chain) != 1) { critical("Unable to load certificate chain file: %s: %s", ERR_reason_error_string(ERR_get_error()), conf->full_chain); - config_unload(); cache_unload(); geoip_free(); return 1; } if (SSL_CTX_use_PrivateKey_file(ctx, conf->priv_key, SSL_FILETYPE_PEM) != 1) { critical("Unable to load private key file: %s: %s", ERR_reason_error_string(ERR_get_error()), conf->priv_key); - config_unload(); cache_unload(); geoip_free(); return 1; @@ -295,7 +271,6 @@ int main(int argc, const char *argv[]) { for (int i = 0; i < NUM_SOCKETS; i++) { if (listen(sockets[i], LISTEN_BACKLOG) < 0) { critical("Unable to listen on socket %i", i); - config_unload(); cache_unload(); geoip_free(); return 1; @@ -369,7 +344,6 @@ int main(int argc, const char *argv[]) { } } - config_unload(); cache_unload(); geoip_free(); return 0;