Remove shm from config

This commit is contained in:
2022-12-15 10:43:43 +01:00
parent 1f20c70772
commit 3ce72975b8
8 changed files with 53 additions and 148 deletions

View File

@ -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"}; 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++) { 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 (hc->type == CONFIG_TYPE_UNSET) break;
if (strcmp(hc->name, host) == 0) return hc; if (strcmp(hc->name, host) == 0) return hc;
if (hc->name[0] == '*' && hc->name[1] == '.') { 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; err_msg[0] = 0;
msg_content[0] = 0; msg_content[0] = 0;
host_config *conf = NULL; host_config_t *conf = NULL;
FILE *file = NULL; FILE *file = NULL;
long content_length = 0; long content_length = 0;

View File

@ -25,7 +25,7 @@ typedef struct {
char _s_addr[INET6_ADDRSTRLEN + 1]; char _s_addr[INET6_ADDRSTRLEN + 1];
} client_ctx_t; } 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); int client_handler(sock *client, unsigned long client_num);

View File

@ -8,60 +8,15 @@
#include "../logger.h" #include "../logger.h"
#include "config.h" #include "config.h"
#include "utils.h"
#include <stdio.h> #include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <string.h> #include <string.h>
#include <errno.h>
#include <stdlib.h> #include <stdlib.h>
t_config *config; config_t config;
char geoip_dir[256], dns_server[256]; 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) { int config_load(const char *filename) {
FILE *file = fopen(filename, "r"); FILE *file = fopen(filename, "r");
if (file == NULL) { if (file == NULL) {
@ -69,30 +24,23 @@ int config_load(const char *filename) {
return -1; 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 i = 0;
int j = 0; int j = 0;
int line = 0; int line_num = 0;
int mode = 0; int mode = 0;
char section = 0; char section = 0;
char *ptr = NULL;
char *source, *target; char *source, *target;
while ((ptr = strsep(&conf, "\r\n")) != NULL) {
line++; char *line = NULL;
char *comment = strchr(ptr, '#'); 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; if (comment != NULL) comment[0] = 0;
len = strlen(ptr); unsigned long len = strlen(ptr);
char *end_ptr = ptr + len - 1; char *end_ptr = ptr + len - 1;
while (end_ptr[0] == ' ' || end_ptr[0] == '\t') { while (end_ptr[0] == ' ' || end_ptr[0] == '\t') {
end_ptr[0] = 0; 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[0] == ' ' || ptr[0] == '\t' || ptr[0] == ']') ptr++;
while (ptr[l] != ' ' && ptr[l] != '\t' && ptr[l] != ']') l++; while (ptr[l] != ' ' && ptr[l] != '\t' && ptr[l] != ']') l++;
if (l == 0) goto err; 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++; i++;
section = 'h'; section = 'h';
} else if (strncmp(ptr, "cert", 4) == 0 && (ptr[4] == ' ' || ptr[4] == '\t')) { } 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[0] == ' ' || ptr[0] == '\t' || ptr[0] == ']') ptr++;
while (ptr[l] != ' ' && ptr[l] != '\t' && ptr[l] != ']') l++; while (ptr[l] != ' ' && ptr[l] != '\t' && ptr[l] != ']') l++;
if (l == 0) goto err; 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++; j++;
section = 'c'; section = 'c';
} else { } else {
@ -136,7 +84,7 @@ int config_load(const char *filename) {
goto err; goto err;
} }
} else if (section == 'c') { } 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')) { if (len > 12 && strncmp(ptr, "certificate", 11) == 0 && (ptr[11] == ' ' || ptr[11] == '\t')) {
source = ptr + 11; source = ptr + 11;
target = cc->full_chain; target = cc->full_chain;
@ -147,7 +95,7 @@ int config_load(const char *filename) {
goto err; goto err;
} }
} else if (section == 'h') { } 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')) { if (len > 8 && strncmp(ptr, "webroot", 7) == 0 && (ptr[7] == ' ' || ptr[7] == '\t')) {
source = ptr + 7; source = ptr + 7;
target = hc->local.webroot; target = hc->local.webroot;
@ -211,8 +159,7 @@ int config_load(const char *filename) {
while (source[0] == ' ' || source[0] == '\t') source++; while (source[0] == ' ' || source[0] == '\t') source++;
if (strlen(source) == 0) { if (strlen(source) == 0) {
err: err:
free(tmp_config); critical("Unable to parse config file (line_num %i)", line_num);
critical("Unable to parse config file (line %i)", line);
return -2; return -2;
} }
@ -220,23 +167,25 @@ int config_load(const char *filename) {
strcpy(target, source); strcpy(target, source);
} else if (mode == 1) { } else if (mode == 1) {
if (strcmp(source, "forbidden") == 0) { 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) { } 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) { } 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 { } else {
goto err; goto err;
} }
} else if (mode == 2) { } 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++) { 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) { 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] == '/') { if (webroot[strlen(webroot) - 1] == '/') {
webroot[strlen(webroot) - 1] = 0; webroot[strlen(webroot) - 1] = 0;
} }
@ -244,7 +193,7 @@ int config_load(const char *filename) {
if (hc->cert_name[0] == 0) goto err2; if (hc->cert_name[0] == 0) goto err2;
int found = 0; int found = 0;
for (int m = 0; m < j; m++) { 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; hc->cert = m;
found = 1; found = 1;
break; break;
@ -252,27 +201,10 @@ int config_load(const char *filename) {
} }
if (!found) { if (!found) {
err2: err2:
free(tmp_config);
critical("Unable to parse config file"); critical("Unable to parse config file");
return -2; 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; return 0;
} }

View File

@ -11,7 +11,6 @@
#include "uri.h" #include "uri.h"
#define CONFIG_SHM_KEY 255642
#define CONFIG_MAX_HOST_CONFIG 64 #define CONFIG_MAX_HOST_CONFIG 64
#define CONFIG_MAX_CERT_CONFIG 64 #define CONFIG_MAX_CERT_CONFIG 64
@ -40,26 +39,22 @@ typedef struct {
unsigned char dir_mode:2; unsigned char dir_mode:2;
} local; } local;
}; };
} host_config; } host_config_t;
typedef struct { typedef struct {
char name[256]; char name[256];
char full_chain[256]; char full_chain[256];
char priv_key[256]; char priv_key[256];
} cert_config; } cert_config_t;
typedef struct { typedef struct {
host_config hosts[CONFIG_MAX_HOST_CONFIG]; host_config_t hosts[CONFIG_MAX_HOST_CONFIG];
cert_config certs[CONFIG_MAX_CERT_CONFIG]; cert_config_t certs[CONFIG_MAX_CERT_CONFIG];
} t_config; } config_t;
extern t_config *config; extern config_t config;
extern char geoip_dir[256], dns_server[256]; extern char geoip_dir[256], dns_server[256];
int config_init(void);
int config_load(const char *filename); int config_load(const char *filename);
int config_unload(void);
#endif //SESIMOS_CONFIG_H #endif //SESIMOS_CONFIG_H

View File

@ -128,7 +128,7 @@ int proxy_request_header(http_req *req, int enc, client_ctx_t *ctx) {
return 0; 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]; char buf1[256], buf2[256];
int p_len; int p_len;
@ -180,7 +180,7 @@ int proxy_response_header(http_req *req, http_res *res, host_config *conf) {
return 0; 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]; char buffer[CHUNK_SIZE];
const char *connection, *upgrade, *ws_version; const char *connection, *upgrade, *ws_version;
long ret; long ret;

View File

@ -28,9 +28,9 @@ int proxy_preload(void);
int proxy_request_header(http_req *req, int enc, client_ctx_t *ctx); 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); int proxy_send(sock *client, unsigned long len_to_send, int flags);

View File

@ -16,10 +16,14 @@
#define URI_DIR_MODE_LIST 2 #define URI_DIR_MODE_LIST 2
#define URI_DIR_MODE_INFO 3 #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 { typedef struct {
char etag[64]; char etag[URI_ETAG_SIZE];
char type[24]; char type[URI_TYPE_SIZE];
char charset[16]; char charset[URI_CHARSET_SIZE];
char filename_comp_gz[256]; char filename_comp_gz[256];
char filename_comp_br[256]; char filename_comp_br[256];
struct stat stat; struct stat stat;

View File

@ -43,7 +43,7 @@ SSL_CTX *contexts[CONFIG_MAX_CERT_CONFIG];
static int ssl_servername_cb(SSL *ssl, int *ad, void *arg) { static int ssl_servername_cb(SSL *ssl, int *ad, void *arg) {
const char *servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name); const char *servername = SSL_get_servername(ssl, TLSEXT_NAMETYPE_host_name);
if (servername != NULL) { 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]); if (conf != NULL) SSL_set_SSL_CTX(ssl, contexts[conf->cert]);
} }
return SSL_TLSEXT_ERR_OK; return SSL_TLSEXT_ERR_OK;
@ -76,7 +76,6 @@ void terminate_forcefully(int sig) {
notice("Killed %i child process(es)", kills); notice("Killed %i child process(es)", kills);
} }
cache_unload(); cache_unload();
config_unload();
geoip_free(); geoip_free();
exit(2); exit(2);
} }
@ -142,7 +141,6 @@ void terminate_gracefully(int sig) {
info("Goodbye"); info("Goodbye");
cache_unload(); cache_unload();
config_unload();
geoip_free(); geoip_free();
exit(0); exit(0);
} }
@ -173,11 +171,6 @@ int main(int argc, const char *argv[]) {
} }
printf("Sesimos web server " SERVER_VERSION "\n"); printf("Sesimos web server " SERVER_VERSION "\n");
ret = config_init();
if (ret != 0) {
return 1;
}
config_file = NULL; config_file = NULL;
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {
const char *arg = argv[i]; const char *arg = argv[i];
@ -187,51 +180,38 @@ int main(int argc, const char *argv[]) {
"Options:\n" "Options:\n"
" -c, --config <CONFIG-FILE> path to the config file. If not provided, default will be used\n" " -c, --config <CONFIG-FILE> path to the config file. If not provided, default will be used\n"
" -h, --help print this dialogue\n"); " -h, --help print this dialogue\n");
config_unload();
return 0; return 0;
} else if (strcmp(arg, "-c") == 0 || strcmp(arg, "--config") == 0) { } else if (strcmp(arg, "-c") == 0 || strcmp(arg, "--config") == 0) {
if (i == argc - 1) { if (i == argc - 1) {
critical("Unable to parse argument %s, usage: --config <CONFIG-FILE>", arg); critical("Unable to parse argument %s, usage: --config <CONFIG-FILE>", arg);
config_unload();
return 1; return 1;
} }
config_file = argv[++i]; config_file = argv[++i];
} else { } else {
critical("Unable to parse argument '%s'", arg); critical("Unable to parse argument '%s'", arg);
config_unload();
return 1; return 1;
} }
} }
ret = config_load(config_file == NULL ? DEFAULT_CONFIG_FILE : config_file); if (config_load(config_file == NULL ? DEFAULT_CONFIG_FILE : config_file) != 0)
if (ret != 0) {
config_unload();
return 1; return 1;
}
sockets[0] = socket(AF_INET6, SOCK_STREAM, 0); if ((sockets[0] = socket(AF_INET6, SOCK_STREAM, 0)) == - 1 || (sockets[1] = socket(AF_INET6, SOCK_STREAM, 0)) == -1) {
if (sockets[0] < 0) goto socket_err;
sockets[1] = socket(AF_INET6, SOCK_STREAM, 0);
if (sockets[1] < 0) {
socket_err:
critical("Unable to create socket"); critical("Unable to create socket");
config_unload();
return 1; return 1;
} }
for (int i = 0; i < NUM_SOCKETS; i++) { for (int i = 0; i < NUM_SOCKETS; i++) {
if (setsockopt(sockets[i], SOL_SOCKET, SO_REUSEADDR, &YES, sizeof(YES)) < 0) { if (setsockopt(sockets[i], SOL_SOCKET, SO_REUSEADDR, &YES, sizeof(YES)) < 0) {
critical("Unable to set options for socket %i", i); critical("Unable to set options for socket %i", i);
config_unload();
return 1; return 1;
} }
} }
if (bind(sockets[0], (struct sockaddr *) &addresses[0], sizeof(addresses[0])) < 0) goto bind_err; if (bind(sockets[0], (struct sockaddr *) &addresses[0], sizeof(addresses[0])) == -1 ||
if (bind(sockets[1], (struct sockaddr *) &addresses[1], sizeof(addresses[1])) < 0) { bind(sockets[1], (struct sockaddr *) &addresses[1], sizeof(addresses[1])) == -1)
bind_err: {
critical("Unable to bind socket to address"); critical("Unable to bind socket to address");
config_unload();
return 1; return 1;
} }
@ -242,13 +222,11 @@ int main(int argc, const char *argv[]) {
if (ret == -1) { if (ret == -1) {
critical("Unable to initialize geoip"); critical("Unable to initialize geoip");
} }
config_unload();
return 1; return 1;
} }
ret = cache_init(); ret = cache_init();
if (ret < 0) { if (ret < 0) {
config_unload();
geoip_free(); geoip_free();
return 1; return 1;
} else if (ret != 0) { } 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++) { 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; if (conf->name[0] == 0) break;
contexts[i] = SSL_CTX_new(TLS_server_method()); 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) { 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); critical("Unable to load certificate chain file: %s: %s", ERR_reason_error_string(ERR_get_error()), conf->full_chain);
config_unload();
cache_unload(); cache_unload();
geoip_free(); geoip_free();
return 1; return 1;
} }
if (SSL_CTX_use_PrivateKey_file(ctx, conf->priv_key, SSL_FILETYPE_PEM) != 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); critical("Unable to load private key file: %s: %s", ERR_reason_error_string(ERR_get_error()), conf->priv_key);
config_unload();
cache_unload(); cache_unload();
geoip_free(); geoip_free();
return 1; return 1;
@ -295,7 +271,6 @@ int main(int argc, const char *argv[]) {
for (int i = 0; i < NUM_SOCKETS; i++) { for (int i = 0; i < NUM_SOCKETS; i++) {
if (listen(sockets[i], LISTEN_BACKLOG) < 0) { if (listen(sockets[i], LISTEN_BACKLOG) < 0) {
critical("Unable to listen on socket %i", i); critical("Unable to listen on socket %i", i);
config_unload();
cache_unload(); cache_unload();
geoip_free(); geoip_free();
return 1; return 1;
@ -369,7 +344,6 @@ int main(int argc, const char *argv[]) {
} }
} }
config_unload();
cache_unload(); cache_unload();
geoip_free(); geoip_free();
return 0; return 0;