30 Commits

Author SHA1 Message Date
7653c3117e Add architecture.md 2022-12-10 23:39:36 +01:00
483b386100 Remove some global variables 2022-12-10 22:44:26 +01:00
bfa9cf4fcd Add client_ctx_t 2022-12-10 22:33:30 +01:00
9ac67dbfd3 Clean up logger 2022-12-10 21:56:25 +01:00
2efe65fc74 Cleanup code 2022-12-10 21:40:41 +01:00
2937bdaded Add logger 2022-12-10 20:48:47 +01:00
782c7440b0 Websocket uuid as char array 2022-12-10 02:21:02 +01:00
88346fe722 Do not use shard object file anymore 2022-12-10 01:49:16 +01:00
0f75aeea7a Add tests for some utils 2022-12-10 01:33:16 +01:00
933aac0f09 Use OpenSSL EVP api for hashing 2022-12-09 14:27:06 +01:00
7f1299feb4 Fix rev proxy ret bug 2022-11-11 03:05:33 +01:00
8435048150 Handle fastcgit chunk return 2022-11-11 02:23:12 +01:00
206ae3264d Hopefully fix bug 2022-11-11 02:18:01 +01:00
5e050512ad Fix sock_recv return handling 2022-11-10 23:51:57 +01:00
db053121f2 Refactor Makefile 2022-11-10 20:42:24 +01:00
89a9d4b9d6 Fix chunked splicing 2022-11-10 20:21:25 +01:00
bc7c3591a2 Renamed docs/ to doc/ 2022-11-01 11:10:29 +01:00
1859c432c8 Fix chunked 2022-10-21 00:01:11 +02:00
33ec943e8b Fix Makefile 2022-10-20 23:51:06 +02:00
b30f9fa56d Implement Transfer-Encoding chunked for requests 2022-10-20 23:10:07 +02:00
90e324cf87 Remove client buffer 2022-10-20 23:00:11 +02:00
e7e1e7b18f Add debian target to Makefile 2022-10-20 22:07:51 +02:00
63b1ca5d6a Update file header comments 2022-10-20 22:03:46 +02:00
54313551fc Use (void) instead of void 2022-10-20 22:03:46 +02:00
92779e5dba Use volatile sig_atomic_t instead of int 2022-10-20 22:03:46 +02:00
100eb1597d Update Makefile to be a real Makefile 2022-10-20 22:03:46 +02:00
f4c3345445 Fix spacing 2022-10-20 22:03:16 +02:00
893316ebfa Remove debug message 2022-10-20 21:32:50 +02:00
6a511732af Add debug print for websocket 2022-08-27 17:37:54 +02:00
bb895c5bca Fix WebSocket connection close handling 2022-08-27 14:01:33 +02:00
41 changed files with 1244 additions and 638 deletions

8
.gitignore vendored
View File

@@ -1,8 +1,10 @@
*
!src
!src/**
!docs
!docs/**
!doc
!doc/**
!test
!test/**
!Makefile
!.gitignore
!README.md
!*.md

View File

@@ -1,26 +1,87 @@
CC=gcc
CFLAGS=-std=gnu11 -Wall -Wno-unused-but-set-variable
LIBS=-lssl -lcrypto -lmagic -lz -lmaxminddb -lbrotlienc
CFLAGS=-std=gnu11 -Wno-unused-but-set-variable -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_SVID_SOURCE -D_POSIX_C_SOURCE=200809L
LDFLAGS=-lssl -lcrypto -lmagic -lz -lmaxminddb -lbrotlienc
DEBIAN_OPTS=-D CACHE_MAGIC_FILE="\"/usr/share/file/magic.mgc\"" -D PHP_FPM_SOCKET="\"/var/run/php/php7.4-fpm.sock\""
packages:
@echo "Installing packages..."
sudo apt install gcc php-fpm libmagic-dev libssl-dev libmaxminddb-dev
@echo "Finished downloading!"
.PHONY: all prod debug default debian permit clean test
all: prod
default: bin bin/lib bin/sesimos
prod: CFLAGS += -O3
prod: default
debug: CFLAGS += -Wall -pedantic
debug: default
debian: CFLAGS += $(DEBIAN_OPTS)
debian: prod
test: CFLAGS += -include test/mock_*.h
test: bin bin/test
bin/test
bin:
mkdir -p bin
bin/lib:
mkdir -p bin/lib
bin/test: test/mock_*.c test/test_*.c src/lib/utils.c src/lib/sock.c
$(CC) -o $@ $(CFLAGS) $^ -lcriterion
bin/%.o: src/%.c
$(CC) -c -o $@ $(CFLAGS) $<
bin/lib/%.o: src/lib/%.c
$(CC) -c -o $@ $(CFLAGS) $<
bin/sesimos: bin/server.o bin/client.o bin/logger.o \
bin/lib/cache.o bin/lib/compress.o bin/lib/config.o bin/lib/fastcgi.o bin/lib/geoip.o \
bin/lib/http.o bin/lib/http_static.o bin/lib/rev_proxy.o bin/lib/sock.o bin/lib/uri.o \
bin/lib/utils.o bin/lib/websocket.o
$(CC) -o $@ $^ $(CFLAGS) $(LDFLAGS)
bin/server.o: src/server.h src/defs.h src/client.h src/lib/cache.h src/lib/config.h src/lib/sock.h \
src/lib/rev_proxy.h src/lib/geoip.h src/lib/utils.h src/logger.h
bin/client.o: src/client.h src/defs.h src/server.h src/lib/utils.h src/lib/config.h src/lib/sock.h \
src/lib/http.h src/lib/rev_proxy.h src/lib/fastcgi.h src/lib/cache.h src/lib/geoip.h src/lib/compress.h \
src/lib/websocket.h src/logger.h
bin/logger.o: src/logger.h
bin/lib/cache.o: src/lib/cache.h src/lib/utils.h src/lib/uri.h src/lib/compress.h src/logger.h
bin/lib/compress.o: src/lib/compress.h
bin/lib/config.o: src/lib/config.h src/lib/utils.h src/lib/uri.h src/logger.h
bin/lib/fastcgi.o: src/lib/fastcgi.h src/server.h src/lib/utils.h src/lib/compress.h src/lib/http.h \
src/lib/uri.h src/lib/include/fastcgi.h src/logger.h
bin/lib/geoip.o: src/lib/geoip.h
bin/lib/http.o: src/lib/http.h src/lib/utils.h src/lib/compress.h src/lib/sock.h src/logger.h
bin/lib/rev_proxy.o: src/lib/rev_proxy.h src/defs.h src/server.h src/lib/compress.h src/logger.h
bin/lib/sock.o: src/lib/sock.h
bin/lib/uri.o: src/lib/uri.h src/lib/utils.h
bin/lib/utils.o: src/lib/utils.h
bin/lib/websocket.o: src/lib/websocket.h src/defs.h src/lib/utils.h src/lib/sock.h src/logger.h
permit:
sudo setcap 'cap_net_bind_service=+ep' "$(shell pwd)/bin/sesimos"
compile:
@mkdir -p bin
$(CC) src/lib/*.c -o bin/libsesimos.so --shared -fPIC $(CFLAGS) $(LIBS)
$(CC) src/server.c src/client.c -o bin/sesimos $(CFLAGS) $(LIBS) \
-Lbin -lsesimos -Wl,-rpath=$(shell pwd)/bin
compile-prod:
@mkdir -p bin
$(CC) src/lib/*.c -o bin/libsesimos.so --shared -fPIC $(CFLAGS) $(LIBS) $(DEBIAN_OPTS) -O3
$(CC) src/server.c src/client.c -o bin/sesimos $(CFLAGS) $(LIBS) $(DEBIAN_OPTS) -O3 \
-Lbin -lsesimos -Wl,-rpath=$(shell pwd)/bin
clean:
rm -rf bin/*

View File

@@ -21,7 +21,7 @@ Sesimos Secure, simple, modern web server
## Configuration
See [docs/example.conf](docs/example.conf) for more details.
See [doc/example.conf](doc/example.conf) for more details.
### Global directives

22
architecture.md Normal file
View File

@@ -0,0 +1,22 @@
# Architecture
* logger (1)
* listener (2) - 80, 443
* cache_handler (1)
* connection_initializer
* request_handler
* local_handler
* rev_proxy_handler
* ws_handler
* fastcgi_handler
* -> logger
* main -> listener
* listener -> connection_handler
* connection_initializer -> request_handler
* request_handler -> local_handler -> request_handler
* local_handler -> fastcgi_handler -> request_handler
* request_handler -> rp_handler -> request_handler
* rev_proxy_handler -> ws_handler -> request_handler

View File

@@ -1,13 +1,15 @@
/**
* sesimos - secure, simple, modern web server
* Client connection and request handlers
* src/client.c
* Lorenz Stechauner, 2020-12-03
* @brief Client connection and request handlers
* @file src/client.c
* @author Lorenz Stechauner
* @date 2020-12-03
*/
#include "defs.h"
#include "client.h"
#include "server.h"
#include "logger.h"
#include "lib/utils.h"
#include "lib/config.h"
@@ -29,12 +31,9 @@
#include <arpa/inet.h>
int server_keep_alive = 1;
volatile sig_atomic_t server_keep_alive = 1;
struct timeval client_timeout = {.tv_sec = CLIENT_TIMEOUT, .tv_usec = 0};
char *log_client_prefix, *log_conn_prefix, *log_req_prefix, *client_geoip;
char *client_addr_str, *client_addr_str_ptr, *server_addr_str, *server_addr_str_ptr, *client_host_str;
host_config *get_host_config(const char *host) {
for (int i = 0; i < CONFIG_MAX_HOST_CONFIG; i++) {
host_config *hc = &config->hosts[i];
@@ -48,11 +47,11 @@ host_config *get_host_config(const char *host) {
return NULL;
}
void client_terminate() {
void client_terminate(int _) {
server_keep_alive = 0;
}
int client_request_handler(sock *client, unsigned long client_num, unsigned int req_num) {
int client_request_handler(client_ctx_t *cctx, sock *client, unsigned long client_num, unsigned int req_num, const char *restrict log_client_prefix) {
struct timespec begin, end;
long ret;
int client_keep_alive;
@@ -63,6 +62,7 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
char buffer[CHUNK_SIZE];
char host[256];
const char *host_ptr, *hdr_connection;
char log_req_prefix[512];
msg_buf[0] = 0;
err_msg[0] = 0;
@@ -77,7 +77,7 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
int use_rev_proxy = 0;
int p_len;
fastcgi_conn fcgi_conn = {.socket = 0, .req_id = 0};
fastcgi_conn fcgi_conn = {.socket = 0, .req_id = 0, .ctx = cctx};
http_status custom_status;
http_res res = {.version = "1.1", .status = http_get_status(501), .hdr.field_num = 0, .hdr.last_field_num = -1};
@@ -85,7 +85,7 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
clock_gettime(CLOCK_MONOTONIC, &begin);
ret = sock_poll_read(&client, NULL, 1, CLIENT_TIMEOUT * 1000);
ret = sock_poll_read(&client, NULL, NULL, 1, NULL, NULL, CLIENT_TIMEOUT * 1000);
http_add_header_field(&res.hdr, "Date", http_get_date(buf0, sizeof(buf0)));
http_add_header_field(&res.hdr, "Server", SERVER_STR);
@@ -128,10 +128,10 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
sprintf(err_msg, "Host header field is too long.");
goto respond;
} else if (host_ptr == NULL || strchr(host_ptr, '/') != NULL) {
if (strchr(client_addr_str, ':') == NULL) {
strcpy(host, client_addr_str);
if (strchr(cctx->addr, ':') == NULL) {
strcpy(host, cctx->addr);
} else {
sprintf(host, "[%s]", client_addr_str);
sprintf(host, "[%s]", cctx->addr);
}
res.status = http_get_status(400);
sprintf(err_msg, "The client provided no or an invalid Host header field.");
@@ -140,13 +140,13 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
strcpy(host, host_ptr);
}
sprintf(log_req_prefix, "[%6i][%s%*s%s]%s ", getpid(), BLD_STR, INET6_ADDRSTRLEN, host, CLR_STR, log_client_prefix);
log_prefix = log_req_prefix;
print(BLD_STR "%s %s" CLR_STR, req.method, req.uri);
sprintf(log_req_prefix, "[%6i][%s%*s%s]%s", getpid(), BLD_STR, INET6_ADDRSTRLEN, host, CLR_STR, log_client_prefix);
logger_set_prefix(log_req_prefix);
info(BLD_STR "%s %s", req.method, req.uri);
conf = get_host_config(host);
if (conf == NULL) {
print("Unknown host, redirecting to default");
info("Unknown host, redirecting to default");
res.status = http_get_status(307);
sprintf(buf0, "https://%s%s", DEFAULT_HOST, req.uri);
http_add_header_field(&res.hdr, "Location", buf0);
@@ -178,13 +178,12 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
int change_proto = strncmp(uri.uri, "/.well-known/", 13) != 0 && !client->enc;
if (strcmp(uri.uri, buf0) != 0 || change_proto) {
res.status = http_get_status(308);
size = sizeof(buf0);
url_encode(uri.uri, buf0, &size);
size = url_encode(uri.uri, strlen(uri.uri), buf0, sizeof(buf0));
if (change_proto) {
p_len = snprintf(buf1, sizeof(buf1), "https://%s%s", host, buf0);
if (p_len < 0 || p_len >= sizeof(buf1)) {
res.status = http_get_status(500);
print(ERR_STR "Header field 'Location' too long" CLR_STR);
error("Header field 'Location' too long");
goto respond;
}
http_add_header_field(&res.hdr, "Location", buf1);
@@ -200,12 +199,6 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
goto respond;
}
if (http_get_header_field(&req.hdr, "Transfer-Encoding") != NULL) {
sprintf(err_msg, "This server is unable to process requests with the Transfer-Encoding header field.");
res.status = http_get_status(501);
goto respond;
}
if (conf->type == CONFIG_TYPE_LOCAL) {
if (strcmp(req.method, "TRACE") == 0) {
res.status = http_get_status(200);
@@ -253,7 +246,7 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
goto respond;
}
if (http_get_header_field(&req.hdr, "Content-Length") != NULL) {
if (http_get_header_field(&req.hdr, "Content-Length") != NULL || http_get_header_field(&req.hdr, "Transfer-Encoding") != NULL) {
res.status = http_get_status(400);
sprintf(err_msg, "A GET request must not contain a payload");
goto respond;
@@ -379,7 +372,7 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
mode = FASTCGI_PHP;
} else {
res.status = http_get_status(500);
print(ERR_STR "Invalid FastCGI extension: %s" CLR_STR, uri.filename);
error("Invalid FastCGI extension: %s", uri.filename);
goto respond;
}
@@ -396,18 +389,23 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
}
const char *client_content_length = http_get_header_field(&req.hdr, "Content-Length");
const char *client_transfer_encoding = http_get_header_field(&req.hdr, "Transfer-Encoding");
if (client_content_length != NULL) {
unsigned long client_content_len = strtoul(client_content_length, NULL, 10);
ret = fastcgi_receive(&fcgi_conn, client, client_content_len);
if (ret != 0) {
if (ret < 0) {
goto abort;
} else {
sprintf(err_msg, "Unable to communicate with FastCGI socket.");
}
res.status = http_get_status(502);
goto respond;
} else if (client_transfer_encoding != NULL && strstr(client_transfer_encoding, "chunked") != NULL) {
ret = fastcgi_receive_chunked(&fcgi_conn, client);
} else {
ret = 0;
}
if (ret != 0) {
if (ret < 0) {
goto abort;
} else {
sprintf(err_msg, "Unable to communicate with FastCGI socket.");
}
res.status = http_get_status(502);
goto respond;
}
fastcgi_close_stdin(&fcgi_conn);
@@ -476,11 +474,11 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
}
}
} else if (conf->type == CONFIG_TYPE_REVERSE_PROXY) {
print("Reverse proxy for " BLD_STR "%s:%i" CLR_STR, conf->rev_proxy.hostname, conf->rev_proxy.port);
info("Reverse proxy for " BLD_STR "%s:%i" CLR_STR, conf->rev_proxy.hostname, conf->rev_proxy.port);
http_remove_header_field(&res.hdr, "Date", HTTP_REMOVE_ALL);
http_remove_header_field(&res.hdr, "Server", HTTP_REMOVE_ALL);
ret = rev_proxy_init(&req, &res, &ctx, conf, client, &custom_status, err_msg);
ret = rev_proxy_init(&req, &res, &ctx, conf, client, cctx, &custom_status, err_msg);
use_rev_proxy = (ret == 0);
if (res.status->code == 101) {
@@ -495,7 +493,6 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
use_rev_proxy = (strcmp(buf0, ws_accept) == 0) ? 2 : 1;
}
} else {
print("Fail Test1");
ctx.status = 101;
ctx.origin = INTERNAL;
res.status = http_get_status(501);
@@ -544,7 +541,7 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
}
*/
} else {
print(ERR_STR "Unknown host type: %i" CLR_STR, conf->type);
error("Unknown host type: %i", conf->type);
res.status = http_get_status(501);
}
@@ -575,7 +572,7 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
if (res.status->code >= 300 && res.status->code < 400) {
const char *location = http_get_header_field(&res.hdr, "Location");
if (location != NULL) {
snprintf(msg_content, sizeof(msg_content), "<ul>\n\t<li><a href=\"%1$s\">%1$s</a></li>\n</ul>\n", location);
snprintf(msg_content, sizeof(msg_content), "<ul>\n\t<li><a href=\"%s\">%s</a></li>\n</ul>\n", location, location);
}
}
} else if (strncmp(msg_content, "<!DOCTYPE html>", 15) == 0 || strncmp(msg_content, "<html", 5) == 0) {
@@ -639,21 +636,21 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
clock_gettime(CLOCK_MONOTONIC, &end);
const char *location = http_get_header_field(&res.hdr, "Location");
unsigned long micros = (end.tv_nsec - begin.tv_nsec) / 1000 + (end.tv_sec - begin.tv_sec) * 1000000;
print("%s%s%03i %s%s%s (%s)%s", http_get_status_color(res.status), use_rev_proxy ? "-> " : "", res.status->code,
res.status->msg, location != NULL ? " -> " : "", location != NULL ? location : "",
format_duration(micros, buf0), CLR_STR);
info("%s%s%03i %s%s%s (%s)%s", http_get_status_color(res.status), use_rev_proxy ? "-> " : "", res.status->code,
res.status->msg, location != NULL ? " -> " : "", location != NULL ? location : "",
format_duration(micros, buf0), CLR_STR);
// TODO access/error log file
if (use_rev_proxy == 2) {
// WebSocket
print("Upgrading connection to WebSocket connection");
info("Upgrading connection to WebSocket connection");
ret = ws_handle_connection(client, &rev_proxy);
if (ret != 0) {
client_keep_alive = 0;
close_proxy = 1;
}
print("WebSocket connection closed");
info("WebSocket connection closed");
} else if (strcmp(req.method, "HEAD") != 0) {
// default response
unsigned long snd_len = 0;
@@ -661,7 +658,7 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
if (msg_buf[0] != 0) {
ret = sock_send(client, msg_buf, content_length, 0);
if (ret <= 0) {
print(ERR_STR "Unable to send: %s" CLR_STR, sock_strerror(client));
error("Unable to send: %s", sock_strerror(client));
}
snd_len += ret;
} else if (file != NULL) {
@@ -672,14 +669,14 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
}
ret = sock_send(client, buffer, len, feof(file) ? 0 : MSG_MORE);
if (ret <= 0) {
print(ERR_STR "Unable to send: %s" CLR_STR, sock_strerror(client));
error("Unable to send: %s", sock_strerror(client));
break;
}
snd_len += ret;
}
} else if (use_fastcgi) {
const char *transfer_encoding = http_get_header_field(&res.hdr, "Transfer-Encoding");
int chunked = (transfer_encoding != NULL && strcmp(transfer_encoding, "chunked") == 0);
int chunked = (transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL);
int flags = (chunked ? FASTCGI_CHUNKED : 0) | (use_fastcgi & (FASTCGI_COMPRESS | FASTCGI_COMPRESS_HOLD));
ret = fastcgi_send(&fcgi_conn, client, flags);
@@ -703,13 +700,13 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
}
if (close_proxy && rev_proxy.socket != 0) {
print(BLUE_STR "Closing proxy connection" CLR_STR);
info(BLUE_STR "Closing proxy connection");
sock_close(&rev_proxy);
}
clock_gettime(CLOCK_MONOTONIC, &end);
micros = (end.tv_nsec - begin.tv_nsec) / 1000 + (end.tv_sec - begin.tv_sec) * 1000000;
print("Transfer complete: %s", format_duration(micros, buf0));
info("Transfer complete: %s", format_duration(micros, buf0));
uri_free(&uri);
abort:
@@ -720,16 +717,10 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
}
http_free_req(&req);
http_free_res(&res);
if (client->buf != NULL) {
free(client->buf);
client->buf = NULL;
client->buf_off = 0;
client->buf_len = 0;
}
return !client_keep_alive;
}
int client_connection_handler(sock *client, unsigned long client_num) {
int client_connection_handler(client_ctx_t *ctx, sock *client, unsigned long client_num, const char *restrict log_conn_prefix, const char *restrict log_client_prefix) {
struct timespec begin, end;
int ret, req_num;
char buf[1024];
@@ -737,16 +728,16 @@ int client_connection_handler(sock *client, unsigned long client_num) {
clock_gettime(CLOCK_MONOTONIC, &begin);
if (dns_server[0] != 0) {
sprintf(buf, "dig @%s +short +time=1 -x %s", dns_server, client_addr_str);
sprintf(buf, "dig @%s +short +time=1 -x %s", dns_server, ctx->addr);
FILE *dig = popen(buf, "r");
if (dig == NULL) {
print(ERR_STR "Unable to start dig: %s" CLR_STR "\n", strerror(errno));
error("Unable to start dig: %s", strerror(errno));
goto dig_err;
}
unsigned long read = fread(buf, 1, sizeof(buf), dig);
ret = pclose(dig);
if (ret != 0) {
print(ERR_STR "Dig terminated with exit code %i" CLR_STR "\n", ret);
error("Dig terminated with exit code %i", ret);
goto dig_err;
}
char *ptr = memchr(buf, '\n', read);
@@ -754,23 +745,21 @@ int client_connection_handler(sock *client, unsigned long client_num) {
goto dig_err;
}
ptr[-1] = 0;
client_host_str = malloc(strlen(buf) + 1);
strcpy(client_host_str, buf);
strncpy(ctx->host, buf, sizeof(ctx->host));
} else {
dig_err:
client_host_str = NULL;
ctx->host[0] = 0;
}
client_geoip = malloc(GEOIP_MAX_SIZE);
long str_off = 0;
for (int i = 0; i < MAX_MMDB && mmdbs[i].filename != NULL; i++) {
int gai_error, mmdb_res;
MMDB_lookup_result_s result = MMDB_lookup_string(&mmdbs[i], client_addr_str, &gai_error, &mmdb_res);
MMDB_lookup_result_s result = MMDB_lookup_string(&mmdbs[i], ctx->addr, &gai_error, &mmdb_res);
if (mmdb_res != MMDB_SUCCESS) {
print(ERR_STR "Unable to lookup geoip info: %s" CLR_STR "\n", MMDB_strerror(mmdb_res));
error("Unable to lookup geoip info: %s", MMDB_strerror(mmdb_res));
continue;
} else if (gai_error != 0) {
print(ERR_STR "Unable to lookup geoip info" CLR_STR "\n");
error("Unable to lookup geoip info");
continue;
} else if (!result.found_entry) {
continue;
@@ -779,7 +768,7 @@ int client_connection_handler(sock *client, unsigned long client_num) {
MMDB_entry_data_list_s *list;
mmdb_res = MMDB_get_entry_data_list(&result.entry, &list);
if (mmdb_res != MMDB_SUCCESS) {
print(ERR_STR "Unable to lookup geoip info: %s" CLR_STR "\n", MMDB_strerror(mmdb_res));
error("Unable to lookup geoip info: %s", MMDB_strerror(mmdb_res));
continue;
}
@@ -787,32 +776,30 @@ int client_connection_handler(sock *client, unsigned long client_num) {
if (str_off != 0) {
str_off--;
}
mmdb_json(list, client_geoip, &str_off, GEOIP_MAX_SIZE);
mmdb_json(list, ctx->geoip, &str_off, GEOIP_MAX_SIZE);
if (prev != 0) {
client_geoip[prev - 1] = ',';
ctx->geoip[prev - 1] = ',';
}
MMDB_free_entry_data_list(list);
}
char client_cc[3];
client_cc[0] = 0;
ctx->cc[0] = 0;
if (str_off == 0) {
free(client_geoip);
client_geoip = NULL;
ctx->geoip[0] = 0;
} else {
char *pos = client_geoip;
const char *pos = ctx->geoip;
pos = strstr(pos, "\"country\":");
if (pos != NULL) {
pos = strstr(pos, "\"iso_code\":");
pos += 12;
snprintf(client_cc, sizeof(client_cc), "%s", pos);
snprintf(ctx->cc, sizeof(ctx->cc), "%s", pos);
}
}
print("Connection accepted from %s %s%s%s[%s]", client_addr_str, client_host_str != NULL ? "(" : "",
client_host_str != NULL ? client_host_str : "", client_host_str != NULL ? ") " : "",
client_cc[0] != 0 ? client_cc : "N/A");
info("Connection accepted from %s %s%s%s[%s]", ctx->addr, ctx->host[0] != 0 ? "(" : "",
ctx->host[0] != 0 ? ctx->host : "", ctx->host[0] != 0 ? ") " : "",
ctx->cc[0] != 0 ? ctx->cc : "N/A");
client_timeout.tv_sec = CLIENT_TIMEOUT;
client_timeout.tv_usec = 0;
@@ -820,7 +807,7 @@ int client_connection_handler(sock *client, unsigned long client_num) {
goto set_timeout_err;
if (setsockopt(client->socket, SOL_SOCKET, SO_SNDTIMEO, &client_timeout, sizeof(client_timeout)) < 0) {
set_timeout_err:
print(ERR_STR "Unable to set timeout for socket: %s" CLR_STR, strerror(errno));
error("Unable to set timeout for socket");
return 1;
}
@@ -834,7 +821,7 @@ int client_connection_handler(sock *client, unsigned long client_num) {
client->_errno = errno;
client->_ssl_error = ERR_get_error();
if (ret <= 0) {
print(ERR_STR "Unable to perform handshake: %s" CLR_STR, sock_strerror(client));
error("Unable to perform handshake: %s", sock_strerror(client));
ret = -1;
goto close;
}
@@ -843,82 +830,62 @@ int client_connection_handler(sock *client, unsigned long client_num) {
req_num = 0;
ret = 0;
while (ret == 0 && server_keep_alive && req_num < REQ_PER_CONNECTION) {
ret = client_request_handler(client, client_num, req_num++);
log_prefix = log_conn_prefix;
ret = client_request_handler(ctx, client, client_num, req_num++, log_client_prefix);
logger_set_prefix(log_conn_prefix);
}
close:
sock_close(client);
if (rev_proxy.socket != 0) {
print(BLUE_STR "Closing proxy connection" CLR_STR);
info(BLUE_STR "Closing proxy connection");
sock_close(&rev_proxy);
}
clock_gettime(CLOCK_MONOTONIC, &end);
unsigned long micros = (end.tv_nsec - begin.tv_nsec) / 1000 + (end.tv_sec - begin.tv_sec) * 1000000;
print("Connection closed (%s)", format_duration(micros, buf));
info("Connection closed (%s)", format_duration(micros, buf));
return 0;
}
int client_handler(sock *client, unsigned long client_num, struct sockaddr_in6 *client_addr) {
int ret;
struct sockaddr_in6 *server_addr;
struct sockaddr_storage server_addr_storage;
client_ctx_t ctx;
char log_client_prefix[256], log_conn_prefix[512];
char *color_table[] = {"\x1B[31m", "\x1B[32m", "\x1B[33m", "\x1B[34m", "\x1B[35m", "\x1B[36m"};
signal(SIGINT, client_terminate);
signal(SIGTERM, client_terminate);
client_addr_str_ptr = malloc(INET6_ADDRSTRLEN);
inet_ntop(client_addr->sin6_family, (void *) &client_addr->sin6_addr, client_addr_str_ptr, INET6_ADDRSTRLEN);
if (strncmp(client_addr_str_ptr, "::ffff:", 7) == 0) {
client_addr_str = client_addr_str_ptr + 7;
inet_ntop(client_addr->sin6_family, (void *) &client_addr->sin6_addr, ctx._c_addr, sizeof(ctx._c_addr));
if (strncmp(ctx._c_addr, "::ffff:", 7) == 0) {
ctx.addr = ctx._c_addr + 7;
} else {
client_addr_str = client_addr_str_ptr;
ctx.addr = ctx._c_addr;
}
socklen_t len = sizeof(server_addr_storage);
getsockname(client->socket, (struct sockaddr *) &server_addr_storage, &len);
server_addr = (struct sockaddr_in6 *) &server_addr_storage;
server_addr_str_ptr = malloc(INET6_ADDRSTRLEN);
inet_ntop(server_addr->sin6_family, (void *) &server_addr->sin6_addr, server_addr_str_ptr, INET6_ADDRSTRLEN);
if (strncmp(server_addr_str_ptr, "::ffff:", 7) == 0) {
server_addr_str = server_addr_str_ptr + 7;
inet_ntop(server_addr->sin6_family, (void *) &server_addr->sin6_addr, ctx._s_addr, sizeof(ctx._s_addr));
if (strncmp(ctx._s_addr, "::ffff:", 7) == 0) {
ctx.s_addr = ctx._s_addr + 7;
} else {
server_addr_str = server_addr_str_ptr;
ctx.s_addr = ctx._s_addr;
}
log_req_prefix = malloc(256);
log_client_prefix = malloc(256);
sprintf(log_client_prefix, "[%s%4i%s]%s[%*s][%5i]%s", (int) client->enc ? HTTPS_STR : HTTP_STR,
ntohs(server_addr->sin6_port), CLR_STR, color_table[client_num % 6], INET6_ADDRSTRLEN, client_addr_str,
ntohs(server_addr->sin6_port), CLR_STR, color_table[client_num % 6], INET6_ADDRSTRLEN, ctx.addr,
ntohs(client_addr->sin6_port), CLR_STR);
log_conn_prefix = malloc(256);
sprintf(log_conn_prefix, "[%6i][%*s]%s ", getpid(), INET6_ADDRSTRLEN, server_addr_str, log_client_prefix);
log_prefix = log_conn_prefix;
sprintf(log_conn_prefix, "[%6i][%*s]%s", getpid(), INET6_ADDRSTRLEN, ctx.s_addr, log_client_prefix);
logger_set_prefix(log_conn_prefix);
print("Started child process with PID %i", getpid());
info("Started child process with PID %i", getpid());
ret = client_connection_handler(client, client_num);
free(client_addr_str_ptr);
client_addr_str_ptr = NULL;
free(server_addr_str_ptr);
server_addr_str_ptr = NULL;
if (client_host_str != NULL) {
free(client_host_str);
client_host_str = NULL;
}
free(log_conn_prefix);
log_conn_prefix = NULL;
free(log_req_prefix);
log_req_prefix = NULL;
free(log_client_prefix);
log_client_prefix = NULL;
return ret;
return client_connection_handler(&ctx, client, client_num, log_conn_prefix, log_client_prefix);
}

View File

@@ -1,8 +1,9 @@
/**
* sesimos - secure, simple, modern web server
* Client connection and request handlers (header file)
* src/client.h
* Lorenz Stechauner, 2022-08-16
* @brief Client connection and request handlers (header file)
* @file src/client.h
* @author Lorenz Stechauner
* @date 2022-08-16
*/
#ifndef SESIMOS_CLIENT_H
@@ -10,9 +11,20 @@
#include "lib/config.h"
#include "lib/sock.h"
#include "lib/geoip.h"
#include <arpa/inet.h>
typedef struct {
char *addr;
char *s_addr;
char cc[3];
char host[256];
char geoip[GEOIP_MAX_SIZE + 1];
char _c_addr[INET6_ADDRSTRLEN + 1];
char _s_addr[INET6_ADDRSTRLEN + 1];
} client_ctx_t;
host_config *get_host_config(const char *host);
int client_handler(sock *client, unsigned long client_num, struct sockaddr_in6 *client_addr);

View File

@@ -1,8 +1,9 @@
/**
* sesimos - secure, simple, modern web server
* Definitions
* src/defs.h
* Lorenz Stechauner, 2021-05-04
* @brief Definitions
* @file src/defs.h
* @author Lorenz Stechauner
* @date 2021-05-04
*/
#ifndef SESIMOS_DEF_H

View File

@@ -1,10 +1,12 @@
/**
* sesimos - secure, simple, modern web server
* File cache implementation
* src/lib/cache.c
* Lorenz Stechauner, 2020-12-19
* @brief File cache implementation
* @file src/lib/cache.c
* @author Lorenz Stechauner
* @date 2020-12-19
*/
#include "../logger.h"
#include "cache.h"
#include "utils.h"
#include "compress.h"
@@ -16,56 +18,59 @@
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <openssl/sha.h>
#include <malloc.h>
#include <openssl/evp.h>
int cache_continue = 1;
magic_t magic;
cache_entry *cache;
int magic_init() {
int magic_init(void) {
magic = magic_open(MAGIC_MIME);
if (magic == NULL) {
fprintf(stderr, ERR_STR "Unable to open magic cookie: %s" CLR_STR "\n", strerror(errno));
critical("Unable to open magic cookie");
return -1;
}
if (magic_load(magic, CACHE_MAGIC_FILE) != 0) {
fprintf(stderr, ERR_STR "Unable to load magic cookie: %s" CLR_STR "\n", magic_error(magic));
critical("Unable to load magic cookie: %s", magic_error(magic));
return -2;
}
return 0;
}
void cache_process_term() {
void cache_process_term(int _) {
cache_continue = 0;
}
int cache_process() {
int cache_process(void) {
errno = 0;
signal(SIGINT, cache_process_term);
signal(SIGTERM, cache_process_term);
logger_set_name("cache");
int shm_id = shmget(CACHE_SHM_KEY, CACHE_ENTRIES * sizeof(cache_entry), 0);
if (shm_id < 0) {
fprintf(stderr, ERR_STR "Unable to create cache shared memory: %s" CLR_STR "\n", strerror(errno));
critical("Unable to create cache shared memory");
return -1;
}
shmdt(cache);
errno = 0;
void *shm_rw = shmat(shm_id, NULL, 0);
if (shm_rw == (void *) -1) {
fprintf(stderr, ERR_STR "Unable to attach cache shared memory (rw): %s" CLR_STR "\n", strerror(errno));
critical("Unable to attach cache shared memory (rw)");
return -2;
}
cache = shm_rw;
if (mkdir("/var/sesimos/", 0755) < 0 && errno != EEXIST) {
fprintf(stderr, ERR_STR "Unable to create directory '/var/sesimos/': %s" CLR_STR "\n", strerror(errno));
critical("Unable to create directory '/var/sesimos/'");
return -3;
}
if (mkdir("/var/sesimos/server/", 0755) < 0 && errno != EEXIST) {
fprintf(stderr, ERR_STR "Unable to create directory '/var/sesimos/server/': %s" CLR_STR "\n", strerror(errno));
critical("Unable to create directory '/var/sesimos/server/'");
return -3;
}
@@ -75,19 +80,19 @@ int cache_process() {
fclose(cache_file);
}
errno = 0;
for (int i = 0; i < CACHE_ENTRIES; i++) {
cache[i].is_updating = 0;
}
FILE *file;
char *buf = malloc(CACHE_BUF_SIZE);
char *comp_buf = malloc(CACHE_BUF_SIZE);
char filename_comp_gz[256];
char filename_comp_br[256];
char buf[CACHE_BUF_SIZE], comp_buf[CACHE_BUF_SIZE], filename_comp_gz[256], filename_comp_br[256];
unsigned long read;
int compress;
SHA_CTX ctx;
unsigned char hash[SHA_DIGEST_LENGTH];
EVP_MD_CTX *ctx;
unsigned char hash[EVP_MAX_MD_SIZE];
unsigned int md_len;
int cache_changed = 0;
int p_len_gz, p_len_br;
int ret;
@@ -95,8 +100,10 @@ int cache_process() {
for (int i = 0; i < CACHE_ENTRIES; i++) {
if (cache[i].filename[0] != 0 && cache[i].meta.etag[0] == 0 && !cache[i].is_updating) {
cache[i].is_updating = 1;
fprintf(stdout, "[cache] Hashing file %s\n", cache[i].filename);
SHA1_Init(&ctx);
info("Hashing file %s", cache[i].filename);
ctx = EVP_MD_CTX_new();
EVP_DigestInit(ctx, EVP_sha1());
file = fopen(cache[i].filename, "rb");
compress = mime_is_compressible(cache[i].meta.type);
@@ -106,15 +113,16 @@ int cache_process() {
if (compress) {
sprintf(buf, "%.*s/.sesimos", cache[i].webroot_len, cache[i].filename);
if (mkdir(buf, 0755) != 0 && errno != EEXIST) {
fprintf(stderr, ERR_STR "Unable to create directory %s: %s" CLR_STR "\n", buf, strerror(errno));
error("Unable to create directory %s", buf);
goto comp_err;
}
sprintf(buf, "%.*s/.sesimos/cache", cache[i].webroot_len, cache[i].filename);
if (mkdir(buf, 0700) != 0 && errno != EEXIST) {
fprintf(stderr, ERR_STR "Unable to create directory %s: %s" CLR_STR "\n", buf, strerror(errno));
error("Unable to create directory %s", buf);
goto comp_err;
}
errno = 0;
char *rel_path = cache[i].filename + cache[i].webroot_len + 1;
for (int j = 0; j < strlen(rel_path); j++) {
@@ -130,26 +138,23 @@ int cache_process() {
p_len_br = snprintf(filename_comp_br, sizeof(filename_comp_br),
"%.*s/.sesimos/cache/%s.br",
cache[i].webroot_len, cache[i].filename, buf);
if (p_len_gz < 0 || p_len_gz >= sizeof(filename_comp_gz) ||
p_len_br < 0 || p_len_br >= sizeof(filename_comp_br))
{
fprintf(stderr, ERR_STR "Unable to open cached file: "
"File name for compressed file too long" CLR_STR "\n");
if (p_len_gz < 0 || p_len_gz >= sizeof(filename_comp_gz) || p_len_br < 0 || p_len_br >= sizeof(filename_comp_br)) {
error("Unable to open cached file: File name for compressed file too long");
goto comp_err;
}
fprintf(stdout, "[cache] Compressing file %s\n", cache[i].filename);
info("Compressing file %s", cache[i].filename);
comp_file_gz = fopen(filename_comp_gz, "wb");
comp_file_br = fopen(filename_comp_br, "wb");
if (comp_file_gz == NULL || comp_file_br == NULL) {
fprintf(stderr, ERR_STR "Unable to open cached file: %s" CLR_STR "\n", strerror(errno));
error("Unable to open cached file");
comp_err:
compress = 0;
} else {
ret = compress_init(&comp_ctx, COMPRESS_GZ | COMPRESS_BR);
if (ret != 0) {
fprintf(stderr, ERR_STR "Unable to init compression: %s" CLR_STR "\n", strerror(errno));
error("Unable to init compression");
compress = 0;
fclose(comp_file_gz);
fclose(comp_file_br);
@@ -158,21 +163,19 @@ int cache_process() {
}
while ((read = fread(buf, 1, CACHE_BUF_SIZE, file)) > 0) {
SHA1_Update(&ctx, buf, read);
EVP_DigestUpdate(ctx, buf, read);
if (compress) {
unsigned long avail_in, avail_out;
avail_in = read;
do {
avail_out = CACHE_BUF_SIZE;
compress_compress_mode(&comp_ctx, COMPRESS_GZ,buf + read - avail_in, &avail_in,
comp_buf, &avail_out, feof(file));
compress_compress_mode(&comp_ctx, COMPRESS_GZ,buf + read - avail_in, &avail_in, comp_buf, &avail_out, feof(file));
fwrite(comp_buf, 1, CACHE_BUF_SIZE - avail_out, comp_file_gz);
} while (avail_in != 0 || avail_out != CACHE_BUF_SIZE);
avail_in = read;
do {
avail_out = CACHE_BUF_SIZE;
compress_compress_mode(&comp_ctx, COMPRESS_BR, buf + read - avail_in, &avail_in,
comp_buf, &avail_out, feof(file));
compress_compress_mode(&comp_ctx, COMPRESS_BR, buf + read - avail_in, &avail_in, comp_buf, &avail_out, feof(file));
fwrite(comp_buf, 1, CACHE_BUF_SIZE - avail_out, comp_file_br);
} while (avail_in != 0 || avail_out != CACHE_BUF_SIZE);
}
@@ -182,20 +185,23 @@ int cache_process() {
compress_free(&comp_ctx);
fclose(comp_file_gz);
fclose(comp_file_br);
fprintf(stdout, "[cache] Finished compressing file %s\n", cache[i].filename);
info("Finished compressing file %s", cache[i].filename);
strcpy(cache[i].meta.filename_comp_gz, filename_comp_gz);
strcpy(cache[i].meta.filename_comp_br, filename_comp_br);
} else {
memset(cache[i].meta.filename_comp_gz, 0, sizeof(cache[i].meta.filename_comp_gz));
memset(cache[i].meta.filename_comp_br, 0, sizeof(cache[i].meta.filename_comp_br));
}
SHA1_Final(hash, &ctx);
EVP_DigestFinal(ctx, hash, &md_len);
EVP_MD_CTX_free(ctx);
memset(cache[i].meta.etag, 0, sizeof(cache[i].meta.etag));
for (int j = 0; j < SHA_DIGEST_LENGTH; j++) {
for (int j = 0; j < md_len; j++) {
sprintf(cache[i].meta.etag + j * 2, "%02x", hash[j]);
}
fclose(file);
fprintf(stdout, "[cache] Finished hashing file %s\n", cache[i].filename);
info("Finished hashing file %s", cache[i].filename);
cache[i].is_updating = 0;
cache_changed = 1;
}
@@ -205,9 +211,7 @@ int cache_process() {
cache_changed = 0;
cache_file = fopen("/var/sesimos/server/cache", "wb");
if (cache_file == NULL) {
fprintf(stderr, ERR_STR "Unable to open cache file: %s" CLR_STR "\n", strerror(errno));
free(buf);
free(comp_buf);
critical("Unable to open cache file");
return -1;
}
fwrite(cache, sizeof(cache_entry), CACHE_ENTRIES, cache_file);
@@ -216,38 +220,39 @@ int cache_process() {
sleep(1);
}
}
free(buf);
free(comp_buf);
return 0;
}
int cache_init() {
int cache_init(void) {
errno = 0;
if (magic_init() != 0) {
return -1;
}
int shm_id = shmget(CACHE_SHM_KEY, CACHE_ENTRIES * sizeof(cache_entry), IPC_CREAT | IPC_EXCL | 0600);
if (shm_id < 0) {
fprintf(stderr, ERR_STR "Unable to create cache shared memory: %s" CLR_STR "\n", strerror(errno));
critical("Unable to create cache shared memory");
return -2;
}
void *shm = shmat(shm_id, NULL, SHM_RDONLY);
if (shm == (void *) -1) {
fprintf(stderr, ERR_STR "Unable to attach cache shared memory (ro): %s" CLR_STR "\n", strerror(errno));
critical("Unable to attach cache shared memory (ro)");
return -3;
}
cache = shm;
void *shm_rw = shmat(shm_id, NULL, 0);
if (shm_rw == (void *) -1) {
fprintf(stderr, ERR_STR "Unable to attach cache shared memory (rw): %s" CLR_STR "\n", strerror(errno));
critical("Unable to attach cache shared memory (rw)");
return -4;
}
cache = shm_rw;
memset(cache, 0, CACHE_ENTRIES * sizeof(cache_entry));
shmdt(shm_rw);
cache = shm;
errno = 0;
pid_t pid = fork();
if (pid == 0) {
@@ -259,26 +264,27 @@ int cache_init() {
}
} else if (pid > 0) {
// parent
fprintf(stderr, "Started child process with PID %i as cache-updater\n", pid);
info("Started child process with PID %i as cache-updater", pid);
return pid;
} else {
fprintf(stderr, ERR_STR "Unable to create child process: %s" CLR_STR "\n", strerror(errno));
critical("Unable to create child process");
return -5;
}
}
int cache_unload() {
int cache_unload(void) {
int shm_id = shmget(CACHE_SHM_KEY, 0, 0);
if (shm_id < 0) {
fprintf(stderr, ERR_STR "Unable to get cache shared memory id: %s" CLR_STR "\n", strerror(errno));
critical("Unable to get cache shared memory id");
shmdt(cache);
return -1;
} else if (shmctl(shm_id, IPC_RMID, NULL) < 0) {
fprintf(stderr, ERR_STR "Unable to configure cache shared memory: %s" CLR_STR "\n", strerror(errno));
critical("Unable to configure cache shared memory");
shmdt(cache);
return -1;
}
shmdt(cache);
errno = 0;
return 0;
}
@@ -287,7 +293,7 @@ int cache_update_entry(int entry_num, const char *filename, const char *webroot)
int shm_id = shmget(CACHE_SHM_KEY, 0, 0);
void *shm_rw = shmat(shm_id, NULL, 0);
if (shm_rw == (void *) -1) {
print(ERR_STR "Unable to attach cache shared memory (rw): %s" CLR_STR, strerror(errno));
error("Unable to attach cache shared memory (rw)");
return -1;
}
cache = shm_rw;
@@ -322,6 +328,7 @@ int cache_update_entry(int entry_num, const char *filename, const char *webroot)
shmdt(shm_rw);
cache = cache_ro;
errno = 0;
return 0;
}
@@ -330,7 +337,7 @@ int cache_filename_comp_invalid(const char *filename) {
int shm_id = shmget(CACHE_SHM_KEY, 0, 0);
void *shm_rw = shmat(shm_id, NULL, 0);
if (shm_rw == (void *) -1) {
print(ERR_STR "Unable to attach cache shared memory (rw): %s" CLR_STR, strerror(errno));
error("Unable to attach cache shared memory (rw)");
return -1;
}
cache = shm_rw;

View File

@@ -1,8 +1,9 @@
/**
* sesimos - secure, simple, modern web server
* File cache implementation (header file)
* src/lib/cache.h
* Lorenz Stechauner, 2020-12-19
* @brief File cache implementation (header file)
* @file src/lib/cache.h
* @author Lorenz Stechauner
* @date 2020-12-19
*/
#ifndef SESIMOS_CACHE_H
@@ -30,15 +31,15 @@ extern cache_entry *cache;
extern int cache_continue;
int magic_init();
int magic_init(void);
void cache_process_term();
void cache_process_term(int _);
int cache_process();
int cache_process(void);
int cache_init();
int cache_init(void);
int cache_unload();
int cache_unload(void);
int cache_update_entry(int entry_num, const char *filename, const char *webroot);

View File

@@ -1,8 +1,9 @@
/**
* sesimos - secure, simple, modern web server
* Compression interface
* src/lib/compress.c
* Lorenz Stechauner, 2021-05-05
* @brief Compression interface
* @file src/lib/compress.c
* @author Lorenz Stechauner
* @date 2021-05-05
*/
#include "compress.h"

View File

@@ -1,8 +1,9 @@
/**
* sesimos - secure, simple, modern web server
* Compression interface (header file)
* src/lib/compress.h
* Lorenz Stechauner, 2021-05-05
* @brief Compression interface (header file)
* @file src/lib/compress.h
* @author Lorenz Stechauner
* @date 2021-05-05
*/
#ifndef SESIMOS_COMPRESS_H

View File

@@ -1,10 +1,12 @@
/**
* sesimos - secure, simple, modern web server
* Configuration file loader
* src/lib/config.c
* Lorenz Stechauner, 2021-01-05
* @brief Configuration file loader
* @file src/lib/config.c
* @author Lorenz Stechauner
* @date 2021-01-05
*/
#include "../logger.h"
#include "config.h"
#include "utils.h"
@@ -19,23 +21,23 @@
t_config *config;
char geoip_dir[256], dns_server[256];
int config_init() {
int config_init(void) {
int shm_id = shmget(CONFIG_SHM_KEY, sizeof(t_config), IPC_CREAT | IPC_EXCL | 0640);
if (shm_id < 0) {
fprintf(stderr, ERR_STR "Unable to create config shared memory: %s" CLR_STR "\n", strerror(errno));
critical("Unable to create config shared memory");
return -1;
}
void *shm = shmat(shm_id, NULL, SHM_RDONLY);
if (shm == (void *) -1) {
fprintf(stderr, ERR_STR "Unable to attach config shared memory (ro): %s" CLR_STR "\n", strerror(errno));
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) {
fprintf(stderr, ERR_STR "Unable to attach config shared memory (rw): %s" CLR_STR "\n", strerror(errno));
critical("Unable to attach config shared memory (rw)");
return -3;
}
config = shm_rw;
@@ -45,14 +47,14 @@ int config_init() {
return 0;
}
int config_unload() {
int config_unload(void) {
int shm_id = shmget(CONFIG_SHM_KEY, 0, 0);
if (shm_id < 0) {
fprintf(stderr, ERR_STR "Unable to get config shared memory id: %s" CLR_STR "\n", strerror(errno));
critical("Unable to get config shared memory id");
shmdt(config);
return -1;
} else if (shmctl(shm_id, IPC_RMID, NULL) < 0) {
fprintf(stderr, ERR_STR "Unable to configure config shared memory: %s" CLR_STR "\n", strerror(errno));
critical("Unable to configure config shared memory");
shmdt(config);
return -1;
}
@@ -63,7 +65,7 @@ int config_unload() {
int config_load(const char *filename) {
FILE *file = fopen(filename, "r");
if (file == NULL) {
fprintf(stderr, ERR_STR "Unable to open config file: %s" CLR_STR "\n", strerror(errno));
critical("Unable to open config file");
return -1;
}
@@ -210,7 +212,7 @@ int config_load(const char *filename) {
if (strlen(source) == 0) {
err:
free(tmp_config);
fprintf(stderr, ERR_STR "Unable to parse config file (line %i)" CLR_STR "\n", line);
critical("Unable to parse config file (line %i)", line);
return -2;
}
@@ -251,14 +253,14 @@ int config_load(const char *filename) {
if (!found) {
err2:
free(tmp_config);
fprintf(stderr, ERR_STR "Unable to parse config file" CLR_STR "\n");
critical("Unable to parse config file");
return -2;
}
}
int shm_id = shmget(CONFIG_SHM_KEY, 0, 0);
if (shm_id < 0) {
fprintf(stderr, ERR_STR "Unable to get config shared memory id: %s" CLR_STR "\n", strerror(errno));
critical("Unable to get config shared memory id");
shmdt(config);
return -3;
}
@@ -266,7 +268,7 @@ int config_load(const char *filename) {
void *shm_rw = shmat(shm_id, NULL, 0);
if (shm_rw == (void *) -1) {
free(tmp_config);
fprintf(stderr, ERR_STR "Unable to attach config shared memory (rw): %s" CLR_STR "\n", strerror(errno));
critical("Unable to attach config shared memory (rw)");
return -4;
}
memcpy(shm_rw, tmp_config, sizeof(t_config));

View File

@@ -1,8 +1,9 @@
/**
* sesimos - secure, simple, modern web server
* Configuration file loader (header file)
* src/lib/config.h
* Lorenz Stechauner, 2021-01-05
* @brief Configuration file loader (header file)
* @file src/lib/config.h
* @author Lorenz Stechauner
* @date 2021-01-05
*/
#ifndef SESIMOS_CONFIG_H
@@ -55,10 +56,10 @@ typedef struct {
extern t_config *config;
extern char geoip_dir[256], dns_server[256];
int config_init();
int config_init(void);
int config_load(const char *filename);
int config_unload();
int config_unload(void);
#endif //SESIMOS_CONFIG_H

View File

@@ -1,14 +1,16 @@
/**
* sesimos - secure, simple, modern web server
* FastCGI interface implementation
* src/lib/fastcgi.c
* Lorenz Stechauner, 2020-12-26
* @brief FastCGI interface implementation
* @file src/lib/fastcgi.c
* @author Lorenz Stechauner
* @date 2020-12-26
*/
#include "fastcgi.h"
#include "utils.h"
#include "compress.h"
#include "../server.h"
#include "../logger.h"
#include <sys/un.h>
#include <sys/socket.h>
@@ -51,8 +53,7 @@ char *fastcgi_add_param(char *buf, const char *key, const char *value) {
return ptr;
}
int fastcgi_init(fastcgi_conn *conn, int mode, unsigned int client_num, unsigned int req_num, const sock *client,
const http_req *req, const http_uri *uri) {
int fastcgi_init(fastcgi_conn *conn, int mode, unsigned int client_num, unsigned int req_num, const sock *client, const http_req *req, const http_uri *uri) {
unsigned short req_id = (client_num & 0xFFF) << 4;
if (client_num == 0) {
req_id |= (req_num + 1) & 0xF;
@@ -67,7 +68,7 @@ int fastcgi_init(fastcgi_conn *conn, int mode, unsigned int client_num, unsigned
int fcgi_sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (fcgi_sock < 0) {
print(ERR_STR "Unable to create unix socket: %s" CLR_STR, strerror(errno));
error("Unable to create unix socket");
return -1;
}
conn->socket = fcgi_sock;
@@ -80,7 +81,7 @@ int fastcgi_init(fastcgi_conn *conn, int mode, unsigned int client_num, unsigned
}
if (connect(conn->socket, (struct sockaddr *) &sock_addr, sizeof(sock_addr)) < 0) {
print(ERR_STR "Unable to connect to unix socket of FastCGI socket: %s" CLR_STR, strerror(errno));
error("Unable to connect to unix socket of FastCGI socket");
return -1;
}
@@ -100,7 +101,7 @@ int fastcgi_init(fastcgi_conn *conn, int mode, unsigned int client_num, unsigned
{.roleB1 = (FCGI_RESPONDER >> 8) & 0xFF, .roleB0 = FCGI_RESPONDER & 0xFF, .flags = 0}
};
if (send(conn->socket, &begin, sizeof(begin), 0) != sizeof(begin)) {
print(ERR_STR "Unable to send to FastCGI socket: %s" CLR_STR, strerror(errno));
error("Unable to send to FastCGI socket");
return -2;
}
@@ -131,8 +132,8 @@ int fastcgi_init(fastcgi_conn *conn, int mode, unsigned int client_num, unsigned
addr = (struct sockaddr_in6 *) &addr_storage;
sprintf(buf0, "%i", addr->sin6_port);
param_ptr = fastcgi_add_param(param_ptr, "REMOTE_PORT", buf0);
param_ptr = fastcgi_add_param(param_ptr, "REMOTE_ADDR", client_addr_str);
param_ptr = fastcgi_add_param(param_ptr, "REMOTE_HOST", client_host_str != NULL ? client_host_str : client_addr_str);
param_ptr = fastcgi_add_param(param_ptr, "REMOTE_ADDR", conn->ctx->addr);
param_ptr = fastcgi_add_param(param_ptr, "REMOTE_HOST", conn->ctx->host[0] != 0 ? conn->ctx->host : conn->ctx->addr);
//param_ptr = fastcgi_add_param(param_ptr, "REMOTE_IDENT", "");
//param_ptr = fastcgi_add_param(param_ptr, "REMOTE_USER", "");
@@ -155,8 +156,8 @@ int fastcgi_init(fastcgi_conn *conn, int mode, unsigned int client_num, unsigned
param_ptr = fastcgi_add_param(param_ptr, "CONTENT_LENGTH", content_length != NULL ? content_length : "");
const char *content_type = http_get_header_field(&req->hdr, "Content-Type");
param_ptr = fastcgi_add_param(param_ptr, "CONTENT_TYPE", content_type != NULL ? content_type : "");
if (client_geoip != NULL) {
param_ptr = fastcgi_add_param(param_ptr, "REMOTE_INFO", client_geoip);
if (conn->ctx->geoip[0] != 0) {
param_ptr = fastcgi_add_param(param_ptr, "REMOTE_INFO", conn->ctx->geoip);
}
for (int i = 0; i < req->hdr.field_num; i++) {
@@ -185,7 +186,7 @@ int fastcgi_init(fastcgi_conn *conn, int mode, unsigned int client_num, unsigned
header.contentLengthB0 = param_len & 0xFF;
memcpy(param_buf, &header, sizeof(header));
if (send(conn->socket, param_buf, param_len + sizeof(header), 0) != param_len + sizeof(header)) {
print(ERR_STR "Unable to send to FastCGI socket: %s" CLR_STR, strerror(errno));
error("Unable to send to FastCGI socket");
return -2;
}
@@ -193,7 +194,7 @@ int fastcgi_init(fastcgi_conn *conn, int mode, unsigned int client_num, unsigned
header.contentLengthB1 = 0;
header.contentLengthB0 = 0;
if (send(conn->socket, &header, sizeof(header), 0) != sizeof(header)) {
print(ERR_STR "Unable to send to FastCGI socket: %s" CLR_STR, strerror(errno));
error("Unable to send to FastCGI socket");
return -2;
}
@@ -213,7 +214,7 @@ int fastcgi_close_stdin(fastcgi_conn *conn) {
};
if (send(conn->socket, &header, sizeof(header), 0) != sizeof(header)) {
print(ERR_STR "Unable to send to FastCGI socket: %s" CLR_STR, strerror(errno));
error("Unable to send to FastCGI socket");
return -2;
}
@@ -230,7 +231,7 @@ int fastcgi_php_error(const fastcgi_conn *conn, const char *msg, int msg_len, ch
int err = 0;
// FIXME *msg is part of a stream, handle fragmented lines
while (1) {
int msg_type = 0;
log_lvl_t msg_type = LOG_INFO;
int msg_pre_len = 0;
ptr1 = strstr(ptr0, "PHP message: ");
if (ptr1 == NULL) {
@@ -244,16 +245,16 @@ int fastcgi_php_error(const fastcgi_conn *conn, const char *msg, int msg_len, ch
}
if (len >= 14 && strncmp(ptr0, "PHP Warning: ", 14) == 0) {
msg_type = 1;
msg_type = LOG_WARNING;
msg_pre_len = 14;
} else if (len >= 18 && strncmp(ptr0, "PHP Fatal error: ", 18) == 0) {
msg_type = 2;
msg_type = LOG_ERROR;
msg_pre_len = 18;
} else if (len >= 18 && strncmp(ptr0, "PHP Parse error: ", 18) == 0) {
msg_type = 2;
msg_type = LOG_ERROR;
msg_pre_len = 18;
} else if (len >= 18 && strncmp(ptr0, "PHP Notice: ", 13) == 0) {
msg_type = 1;
msg_type = LOG_NOTICE;
msg_pre_len = 13;
}
@@ -266,7 +267,7 @@ int fastcgi_php_error(const fastcgi_conn *conn, const char *msg, int msg_len, ch
if (ptr3 != NULL && (ptr3 - ptr2) < len2) {
len2 = (int) (ptr3 - ptr2);
}
print("%s%.*s%s", msg_type == 1 ? WRN_STR : msg_type == 2 ? ERR_STR : "", len2, ptr2, CLR_STR);
logmsgf(msg_type, "%.*s", len2, ptr2);
if (msg_type == 2 && ptr2 == ptr0) {
strcpy_rem_webroot(err_msg, ptr2, len2, conn->webroot);
err = 1;
@@ -299,12 +300,12 @@ int fastcgi_header(fastcgi_conn *conn, http_res *res, char *err_msg) {
if (ret < 0) {
res->status = http_get_status(500);
sprintf(err_msg, "Unable to communicate with FastCGI socket.");
print(ERR_STR "Unable to receive from FastCGI socket: %s" CLR_STR, strerror(errno));
error("Unable to receive from FastCGI socket");
return 1;
} else if (ret != sizeof(header)) {
res->status = http_get_status(500);
sprintf(err_msg, "Unable to communicate with FastCGI socket.");
print(ERR_STR "Unable to receive from FastCGI socket" CLR_STR);
error("Unable to receive from FastCGI socket");
return 1;
}
req_id = (header.requestIdB1 << 8) | header.requestIdB0;
@@ -314,13 +315,13 @@ int fastcgi_header(fastcgi_conn *conn, http_res *res, char *err_msg) {
if (ret < 0) {
res->status = http_get_status(500);
sprintf(err_msg, "Unable to communicate with FastCGI socket.");
print(ERR_STR "Unable to receive from FastCGI socket: %s" CLR_STR, strerror(errno));
error("Unable to receive from FastCGI socket");
free(content);
return 1;
} else if (ret != (content_len + header.paddingLength)) {
res->status = http_get_status(500);
sprintf(err_msg, "Unable to communicate with FastCGI socket.");
print(ERR_STR "Unable to receive from FastCGI socket" CLR_STR);
error("Unable to receive from FastCGI socket");
free(content);
return 1;
}
@@ -334,10 +335,10 @@ int fastcgi_header(fastcgi_conn *conn, http_res *res, char *err_msg) {
int app_status = (body->appStatusB3 << 24) | (body->appStatusB2 << 16) | (body->appStatusB1 << 8) |
body->appStatusB0;
if (body->protocolStatus != FCGI_REQUEST_COMPLETE) {
print(ERR_STR "FastCGI protocol error: %i" CLR_STR, body->protocolStatus);
error("FastCGI protocol error: %i", body->protocolStatus);
}
if (app_status != 0) {
print(ERR_STR "FastCGI app terminated with exit code %i" CLR_STR, app_status);
error("FastCGI app terminated with exit code %i", app_status);
}
close(conn->socket);
conn->socket = 0;
@@ -351,7 +352,7 @@ int fastcgi_header(fastcgi_conn *conn, http_res *res, char *err_msg) {
} else if (header.type == FCGI_STDOUT) {
break;
} else {
print(ERR_STR "Unknown FastCGI type: %i" CLR_STR, header.type);
error("Unknown FastCGI type: %i", header.type);
}
free(content);
@@ -368,13 +369,13 @@ int fastcgi_header(fastcgi_conn *conn, http_res *res, char *err_msg) {
char *buf = content;
unsigned short header_len = conn->out_off;
if (header_len <= 0) {
print(ERR_STR "Unable to parse header: End of header not found" CLR_STR);
error("Unable to parse header: End of header not found");
return 1;
}
for (int i = 0; i < header_len; i++) {
if ((buf[i] >= 0x00 && buf[i] <= 0x1F && buf[i] != '\r' && buf[i] != '\n') || buf[i] == 0x7F) {
print(ERR_STR "Unable to parse header: Header contains illegal characters" CLR_STR);
error("Unable to parse header: Header contains illegal characters");
return 2;
}
}
@@ -383,7 +384,7 @@ int fastcgi_header(fastcgi_conn *conn, http_res *res, char *err_msg) {
while (header_len != (ptr - buf)) {
char *pos0 = strstr(ptr, "\r\n");
if (pos0 == NULL) {
print(ERR_STR "Unable to parse header: Invalid header format" CLR_STR);
error("Unable to parse header: Invalid header format");
return 1;
}
@@ -412,13 +413,13 @@ int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) {
if (flags & FASTCGI_COMPRESS_BR) {
flags &= ~FASTCGI_COMPRESS_GZ;
if (compress_init(&comp_ctx, COMPRESS_BR) != 0) {
print(ERR_STR "Unable to init brotli: %s" CLR_STR, strerror(errno));
error("Unable to init brotli");
flags &= ~FASTCGI_COMPRESS_BR;
}
} else if (flags & FASTCGI_COMPRESS_GZ) {
flags &= ~FASTCGI_COMPRESS_BR;
if (compress_init(&comp_ctx, COMPRESS_GZ) != 0) {
print(ERR_STR "Unable to init gzip: %s" CLR_STR, strerror(errno));
error("Unable to init gzip");
flags &= ~FASTCGI_COMPRESS_GZ;
}
}
@@ -433,11 +434,10 @@ int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) {
while (1) {
ret = recv(conn->socket, &header, sizeof(header), 0);
if (ret < 0) {
print(ERR_STR "Unable to receive from FastCGI socket: %s" CLR_STR, strerror(errno));
error("Unable to receive from FastCGI socket");
return -1;
} else if (ret != sizeof(header)) {
print(ERR_STR "Unable to receive from FastCGI socket: received len (%li) != header len (%li)" CLR_STR,
ret, sizeof(header));
error("Unable to receive from FastCGI socket: received len (%li) != header len (%li)", ret, sizeof(header));
return -1;
}
@@ -450,7 +450,7 @@ int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) {
while (rcv_len < content_len + header.paddingLength) {
ret = recv(conn->socket, content + rcv_len, content_len + header.paddingLength - rcv_len, 0);
if (ret < 0) {
print(ERR_STR "Unable to receive from FastCGI socket: %s" CLR_STR, strerror(errno));
error("Unable to receive from FastCGI socket");
free(content);
return -1;
}
@@ -462,10 +462,10 @@ int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) {
int app_status = (body->appStatusB3 << 24) | (body->appStatusB2 << 16) | (body->appStatusB1 << 8) |
body->appStatusB0;
if (body->protocolStatus != FCGI_REQUEST_COMPLETE) {
print(ERR_STR "FastCGI protocol error: %i" CLR_STR, body->protocolStatus);
error("FastCGI protocol error: %i", body->protocolStatus);
}
if (app_status != 0) {
print(ERR_STR "FastCGI app terminated with exit code %i" CLR_STR, app_status);
error("FastCGI app terminated with exit code %i", app_status);
}
close(conn->socket);
conn->socket = 0;
@@ -493,13 +493,12 @@ int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) {
unsigned long avail_in, avail_out;
out:
avail_in = content_len;
void *next_in = ptr;
char *next_in = ptr;
do {
int buf_len = content_len;
if (flags & FASTCGI_COMPRESS) {
avail_out = sizeof(comp_out);
compress_compress(&comp_ctx, next_in + content_len - avail_in, &avail_in, comp_out, &avail_out,
finish_comp);
compress_compress(&comp_ctx, next_in + content_len - avail_in, &avail_in, comp_out, &avail_out, finish_comp);
ptr = comp_out;
buf_len = (int) (sizeof(comp_out) - avail_out);
}
@@ -512,7 +511,7 @@ int fastcgi_send(fastcgi_conn *conn, sock *client, int flags) {
} while ((flags & FASTCGI_COMPRESS) && (avail_in != 0 || avail_out != sizeof(comp_out)));
if (finish_comp) goto finish;
} else {
print(ERR_STR "Unknown FastCGI type: %i" CLR_STR, header.type);
error("Unknown FastCGI type: %i", header.type);
}
free(content);
}
@@ -532,11 +531,10 @@ int fastcgi_dump(fastcgi_conn *conn, char *buf, long len) {
while (1) {
ret = recv(conn->socket, &header, sizeof(header), 0);
if (ret < 0) {
print(ERR_STR "Unable to receive from FastCGI socket: %s" CLR_STR, strerror(errno));
error("Unable to receive from FastCGI socket");
return -1;
} else if (ret != sizeof(header)) {
print(ERR_STR "Unable to receive from FastCGI socket: received len (%li) != header len (%li)" CLR_STR,
ret, sizeof(header));
error("Unable to receive from FastCGI socket: received len (%li) != header len (%li)", ret, sizeof(header));
return -1;
}
@@ -548,7 +546,7 @@ int fastcgi_dump(fastcgi_conn *conn, char *buf, long len) {
while (rcv_len < content_len + header.paddingLength) {
ret = recv(conn->socket, content + rcv_len, content_len + header.paddingLength - rcv_len, 0);
if (ret < 0) {
print(ERR_STR "Unable to receive from FastCGI socket: %s" CLR_STR, strerror(errno));
error("Unable to receive from FastCGI socket");
free(content);
return -1;
}
@@ -560,10 +558,10 @@ int fastcgi_dump(fastcgi_conn *conn, char *buf, long len) {
int app_status = (body->appStatusB3 << 24) | (body->appStatusB2 << 16) | (body->appStatusB1 << 8) |
body->appStatusB0;
if (body->protocolStatus != FCGI_REQUEST_COMPLETE) {
print(ERR_STR "FastCGI protocol error: %i" CLR_STR, body->protocolStatus);
error("FastCGI protocol error: %i", body->protocolStatus);
}
if (app_status != 0) {
print(ERR_STR "FastCGI app terminated with exit code %i" CLR_STR, app_status);
error("FastCGI app terminated with exit code %i", app_status);
}
close(conn->socket);
conn->socket = 0;
@@ -578,7 +576,7 @@ int fastcgi_dump(fastcgi_conn *conn, char *buf, long len) {
} else if (header.type == FCGI_STDOUT) {
ptr += snprintf(ptr, len - (ptr - buf), "%.*s", content_len, content);
} else {
print(ERR_STR "Unknown FastCGI type: %i" CLR_STR, header.type);
error("Unknown FastCGI type: %i", header.type);
}
free(content);
}
@@ -599,29 +597,40 @@ int fastcgi_receive(fastcgi_conn *conn, sock *client, unsigned long len) {
.reserved = 0
};
if (client->buf != NULL && client->buf_len - client->buf_off > 0) {
ret = (int) (client->buf_len - client->buf_off);
memcpy(buf, client->buf + client->buf_off, ret);
goto send;
}
while (rcv_len < len) {
ret = sock_recv(client, buf, sizeof(buf), 0);
if (ret <= 0) {
print(ERR_STR "Unable to receive: %s" CLR_STR, sock_strerror(client));
error("Unable to receive: %s", sock_strerror(client));
return -1;
}
send:
rcv_len += ret;
header.contentLengthB1 = (ret >> 8) & 0xFF;
header.contentLengthB0 = ret & 0xFF;
if (send(conn->socket, &header, sizeof(header), 0) != sizeof(header)) goto err;
if (send(conn->socket, buf, ret, 0) != ret) {
err:
print(ERR_STR "Unable to send to FastCGI socket: %s" CLR_STR, strerror(errno));
error("Unable to send to FastCGI socket");
return -2;
}
}
return 0;
}
int fastcgi_receive_chunked(fastcgi_conn *conn, sock *client) {
long ret;
unsigned long next_len;
while (1) {
ret = sock_get_chunk_header(client);
if (ret < 0) return (int) ret;
next_len = ret;
if (next_len <= 0) break;
ret = fastcgi_receive(conn, client, next_len);
if (ret < 0) return (int) ret;
}
return 0;
}

View File

@@ -1,8 +1,9 @@
/**
* sesimos - secure, simple, modern web server
* FastCGI interface implementation (header file)
* src/lib/fastcgi.h
* Lorenz Stechauner, 2020-12-26
* @brief FastCGI interface implementation (header file)
* @file src/lib/fastcgi.h
* @author Lorenz Stechauner
* @date 2020-12-26
*/
#ifndef SESIMOS_FASTCGI_H
@@ -11,6 +12,7 @@
#include "include/fastcgi.h"
#include "http.h"
#include "uri.h"
#include "../client.h"
#define FASTCGI_CHUNKED 1
#define FASTCGI_COMPRESS_GZ 2
@@ -35,6 +37,7 @@ typedef struct {
const char *webroot;
unsigned short out_len;
unsigned short out_off;
client_ctx_t *ctx;
} fastcgi_conn;
char *fastcgi_add_param(char *buf, const char *key, const char *value);
@@ -54,4 +57,6 @@ int fastcgi_dump(fastcgi_conn *conn, char *buf, long len);
int fastcgi_receive(fastcgi_conn *conn, sock *client, unsigned long len);
int fastcgi_receive_chunked(fastcgi_conn *conn, sock *client);
#endif //SESIMOS_FASTCGI_H

View File

@@ -1,8 +1,9 @@
/**
* sesimos - secure, simple, modern web server
* MaxMind GeoIP Database interface
* src/lib/geoip.c
* Lorenz Stechauner, 2021-05-04
* @brief MaxMind GeoIP Database interface
* @file src/lib/geoip.c
* @author Lorenz Stechauner
* @date 2021-05-04
*/
#include "geoip.h"

View File

@@ -1,8 +1,9 @@
/**
* sesimos - secure, simple, modern web server
* MaxMind GeoIP Database interface (header file)
* src/lib/geoip.h
* Lorenz Stechauner, 2021-05-04
* @brief MaxMind GeoIP Database interface (header file)
* @file src/lib/geoip.h
* @author Lorenz Stechauner
* @date 2021-05-04
*/

View File

@@ -1,10 +1,12 @@
/**
* sesimos - secure, simple, modern web server
* HTTP implementation
* src/lib/http.c
* Lorenz Stechauner, 2020-12-09
* @brief HTTP implementation
* @file src/lib/http.c
* @author Lorenz Stechauner
* @date 2020-12-09
*/
#include "../logger.h"
#include "http.h"
#include "utils.h"
#include "compress.h"
@@ -86,14 +88,14 @@ void http_free_res(http_res *res) {
int http_parse_header_field(http_hdr *hdr, const char *buf, const char *end_ptr, int flags) {
if (hdr->last_field_num > hdr->field_num) {
print(ERR_STR "Unable to parse header: Invalid state" CLR_STR);
error("Unable to parse header: Invalid state");
return 3;
}
char *pos1 = (char *) buf, *pos2 = (char *) end_ptr;
if (buf[0] == ' ' || buf[0] == '\t') {
if (hdr->last_field_num == -1) {
print(ERR_STR "Unable to parse header" CLR_STR);
error("Unable to parse header");
return 3;
}
http_field *f = &hdr->fields[(int) hdr->last_field_num];
@@ -106,7 +108,7 @@ int http_parse_header_field(http_hdr *hdr, const char *buf, const char *end_ptr,
pos1 = memchr(buf, ':', end_ptr - buf);
if (pos1 == NULL) {
print(ERR_STR "Unable to parse header" CLR_STR);
error("Unable to parse header");
return 3;
}
long len1 = pos1 - buf;
@@ -119,7 +121,7 @@ int http_parse_header_field(http_hdr *hdr, const char *buf, const char *end_ptr,
int found = http_get_header_field_num_len(hdr, buf, len1);
if (!(flags & HTTP_MERGE_FIELDS) || found == -1) {
if (http_add_header_field_len(hdr, buf, len1, pos1, len2 < 0 ? 0 : len2) != 0) {
print(ERR_STR "Unable to parse header: Too many header fields" CLR_STR);
error("Unable to parse header: Too many header fields");
return 3;
}
} else {
@@ -144,21 +146,23 @@ int http_receive_request(sock *client, http_req *req) {
req->hdr.last_field_num = -1;
while (1) {
rcv_len = sock_recv(client, buf, CLIENT_MAX_HEADER_SIZE, 0);
rcv_len = sock_recv(client, buf, CLIENT_MAX_HEADER_SIZE, MSG_PEEK);
if (rcv_len <= 0) {
print("Unable to receive http header: %s", sock_strerror(client));
error("Unable to receive http header: %s", sock_strerror(client));
return -1;
}
unsigned long header_len = strstr(buf, "\r\n\r\n") - buf + 4;
if (header_len <= 0) {
print(ERR_STR "Unable to parse http header: End of header not found" CLR_STR);
error("Unable to parse http header: End of header not found");
return 5;
} else {
rcv_len = sock_recv(client, buf, header_len, 0);
}
for (int i = 0; i < header_len; i++) {
if ((buf[i] >= 0x00 && buf[i] <= 0x1F && buf[i] != '\r' && buf[i] != '\n') || buf[i] == 0x7F) {
print(ERR_STR "Unable to parse http header: Header contains illegal characters" CLR_STR);
error("Unable to parse http header: Header contains illegal characters");
return 4;
}
}
@@ -167,36 +171,36 @@ int http_receive_request(sock *client, http_req *req) {
while (header_len > (ptr - buf + 2)) {
pos0 = strstr(ptr, "\r\n");
if (pos0 == NULL) {
print(ERR_STR "Unable to parse http header: Invalid header format" CLR_STR);
error("Unable to parse http header: Invalid header format");
return 1;
}
if (req->version[0] == 0) {
pos1 = memchr(ptr, ' ', rcv_len - (ptr - buf)) + 1;
pos1 = (char *) memchr(ptr, ' ', rcv_len - (ptr - buf)) + 1;
if (pos1 == NULL) goto err_hdr_fmt;
if (pos1 - ptr - 1 >= sizeof(req->method)) {
print(ERR_STR "Unable to parse http header: Method name too long" CLR_STR);
error("Unable to parse http header: Method name too long");
return 2;
}
for (int i = 0; i < (pos1 - ptr - 1); i++) {
if (ptr[i] < 'A' || ptr[i] > 'Z') {
print(ERR_STR "Unable to parse http header: Invalid method" CLR_STR);
error("Unable to parse http header: Invalid method");
return 2;
}
}
snprintf(req->method, sizeof(req->method), "%.*s", (int) (pos1 - ptr - 1), ptr);
pos2 = memchr(pos1, ' ', rcv_len - (pos1 - buf)) + 1;
pos2 = (char *) memchr(pos1, ' ', rcv_len - (pos1 - buf)) + 1;
if (pos2 == NULL) {
err_hdr_fmt:
print(ERR_STR "Unable to parse http header: Invalid header format" CLR_STR);
error("Unable to parse http header: Invalid header format");
return 1;
}
if (memcmp(pos2, "HTTP/", 5) != 0 || memcmp(pos2 + 8, "\r\n", 2) != 0) {
print(ERR_STR "Unable to parse http header: Invalid version" CLR_STR);
error("Unable to parse http header: Invalid version");
return 3;
}
@@ -215,13 +219,6 @@ int http_receive_request(sock *client, http_req *req) {
}
}
client->buf_len = rcv_len - (pos0 - buf + 4);
if (client->buf_len > 0) {
client->buf = malloc(client->buf_len);
client->buf_off = 0;
memcpy(client->buf, pos0 + 4, client->buf_len);
}
return 0;
}

View File

@@ -1,8 +1,9 @@
/**
* sesimos - secure, simple, modern web server
* HTTP implementation (header file)
* src/lib/http.h
* Lorenz Stechauner, 2020-12-09
* @brief HTTP implementation (header file)
* @file src/lib/http.h
* @author Lorenz Stechauner
* @date 2020-12-09
*/
#ifndef SESIMOS_HTTP_H

View File

@@ -1,8 +1,9 @@
/**
* sesimos - secure, simple, modern web server
* HTTP static implementation
* src/lib/http_static.c
* Lorenz Stechauner, 2021-05-03
* @brief HTTP static implementation
* @file src/lib/http_static.c
* @author Lorenz Stechauner
* @date 2021-05-03
*/
#include "../defs.h"

View File

@@ -1,8 +1,7 @@
/**
* sesimos - secure, simple, modern web server
* FastCGI header file
* src/lib/include/fastcgi.h
* Lorenz Stechauner, 2021-05-03
* @brief FastCGI header file
* @file src/lib/include/fastcgi.h
*/
#ifndef SESIMOS_EXTERN_FASTCGI_H

View File

@@ -1,12 +1,14 @@
/**
* sesimos - secure, simple, modern web server
* Reverse proxy
* src/lib/rev_proxy.c
* Lorenz Stechauner, 2021-01-07
* @brief Reverse proxy
* @file src/lib/rev_proxy.c
* @author Lorenz Stechauner
* @date 2021-01-07
*/
#include "../defs.h"
#include "../server.h"
#include "../logger.h"
#include "rev_proxy.h"
#include "utils.h"
#include "compress.h"
@@ -23,15 +25,12 @@ sock rev_proxy;
char *rev_proxy_host = NULL;
struct timeval server_timeout = {.tv_sec = SERVER_TIMEOUT, .tv_usec = 0};
int rev_proxy_preload() {
rev_proxy.buf = NULL;
rev_proxy.buf_len = 0;
rev_proxy.buf_off = 0;
int rev_proxy_preload(void) {
rev_proxy.ctx = SSL_CTX_new(TLS_client_method());
return 0;
}
int rev_proxy_request_header(http_req *req, int enc) {
int rev_proxy_request_header(http_req *req, int enc, client_ctx_t *ctx) {
char buf1[256], buf2[256];
int p_len;
@@ -42,7 +41,7 @@ int rev_proxy_request_header(http_req *req, int enc) {
} else {
p_len = snprintf(buf2, sizeof(buf2), "%s, %s", via, buf1);
if (p_len < 0 || p_len >= sizeof(buf2)) {
print(ERR_STR "Header field 'Via' too long" CLR_STR);
error("Header field 'Via' too long");
return -1;
}
http_remove_header_field(&req->hdr, "Via", HTTP_REMOVE_ALL);
@@ -51,15 +50,15 @@ int rev_proxy_request_header(http_req *req, int enc) {
const char *host = http_get_header_field(&req->hdr, "Host");
const char *forwarded = http_get_header_field(&req->hdr, "Forwarded");
int client_ipv6 = strchr(client_addr_str, ':') != NULL;
int server_ipv6 = strchr(server_addr_str, ':') != NULL;
int client_ipv6 = strchr(ctx->addr, ':') != NULL;
int server_ipv6 = strchr(ctx->s_addr, ':') != NULL;
p_len = snprintf(buf1, sizeof(buf1), "by=%s%s%s;for=%s%s%s;host=%s;proto=%s",
server_ipv6 ? "\"[" : "", server_addr_str, server_ipv6 ? "]\"" : "",
client_ipv6 ? "\"[" : "", client_addr_str, client_ipv6 ? "]\"" : "",
server_ipv6 ? "\"[" : "", ctx->s_addr, server_ipv6 ? "]\"" : "",
client_ipv6 ? "\"[" : "", ctx->addr, client_ipv6 ? "]\"" : "",
host, enc ? "https" : "http");
if (p_len < 0 || p_len >= sizeof(buf1)) {
print(ERR_STR "Appended part of header field 'Forwarded' too long" CLR_STR);
error("Appended part of header field 'Forwarded' too long");
return -1;
}
@@ -68,7 +67,7 @@ int rev_proxy_request_header(http_req *req, int enc) {
} else {
p_len = snprintf(buf2, sizeof(buf2), "%s, %s", forwarded, buf1);
if (p_len < 0 || p_len >= sizeof(buf2)) {
print(ERR_STR "Header field 'Forwarded' too long" CLR_STR);
error("Header field 'Forwarded' too long");
return -1;
}
http_remove_header_field(&req->hdr, "Forwarded", HTTP_REMOVE_ALL);
@@ -77,9 +76,9 @@ int rev_proxy_request_header(http_req *req, int enc) {
const char *xff = http_get_header_field(&req->hdr, "X-Forwarded-For");
if (xff == NULL) {
http_add_header_field(&req->hdr, "X-Forwarded-For", client_addr_str);
http_add_header_field(&req->hdr, "X-Forwarded-For", ctx->addr);
} else {
sprintf(buf1, "%s, %s", xff, client_addr_str);
sprintf(buf1, "%s, %s", xff, ctx->addr);
http_remove_header_field(&req->hdr, "X-Forwarded-For", HTTP_REMOVE_ALL);
http_add_header_field(&req->hdr, "X-Forwarded-For", buf1);
}
@@ -136,7 +135,7 @@ int rev_proxy_response_header(http_req *req, http_res *res, host_config *conf) {
const char *via = http_get_header_field(&res->hdr, "Via");
p_len = snprintf(buf1, sizeof(buf1), "HTTP/%s %s", req->version, SERVER_NAME);
if (p_len < 0 || p_len >= sizeof(buf1)) {
print(ERR_STR "Appended part of header field 'Via' too long" CLR_STR);
error("Appended part of header field 'Via' too long");
return -1;
}
if (via == NULL) {
@@ -144,7 +143,7 @@ int rev_proxy_response_header(http_req *req, http_res *res, host_config *conf) {
} else {
p_len = snprintf(buf2, sizeof(buf2), "%s, %s", via, buf1);
if (p_len < 0 || p_len >= sizeof(buf2)) {
print(ERR_STR "Header field 'Via' too long" CLR_STR);
error("Header field 'Via' too long");
return -1;
}
http_remove_header_field(&res->hdr, "Via", HTTP_REMOVE_ALL);
@@ -181,7 +180,7 @@ int rev_proxy_response_header(http_req *req, http_res *res, host_config *conf) {
return 0;
}
int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config *conf, sock *client, http_status *custom_status, char *err_msg) {
int rev_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) {
char buffer[CHUNK_SIZE];
const char *connection, *upgrade, *ws_version;
long ret;
@@ -192,7 +191,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
retry:
if (rev_proxy.socket != 0) {
print(BLUE_STR "Closing proxy connection" CLR_STR);
info(BLUE_STR "Closing proxy connection");
sock_close(&rev_proxy);
}
retry = 0;
@@ -200,7 +199,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
rev_proxy.socket = socket(AF_INET6, SOCK_STREAM, 0);
if (rev_proxy.socket < 0) {
print(ERR_STR "Unable to create socket: %s" CLR_STR, strerror(errno));
error("Unable to create socket");
res->status = http_get_status(500);
ctx->origin = INTERNAL;
return -1;
@@ -219,7 +218,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
if (host_ent == NULL) {
res->status = http_get_status(503);
ctx->origin = SERVER_REQ;
print(ERR_STR "Unable to connect to server: Name or service not known" CLR_STR);
error("Unable to connect to server: Name or service not known");
sprintf(err_msg, "Unable to connect to server: Name or service not known.");
goto proxy_err;
}
@@ -236,7 +235,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
inet_ntop(address.sin6_family, (void *) &address.sin6_addr, buffer, sizeof(buffer));
print(BLUE_STR "Connecting to " BLD_STR "[%s]:%i" CLR_STR BLUE_STR "..." CLR_STR, buffer, conf->rev_proxy.port);
info(BLUE_STR "Connecting to " BLD_STR "[%s]:%i" CLR_STR BLUE_STR "...", buffer, conf->rev_proxy.port);
if (connect(rev_proxy.socket, (struct sockaddr *) &address, sizeof(address)) < 0) {
if (errno == ETIMEDOUT || errno == EINPROGRESS) {
res->status = http_get_status(504);
@@ -248,7 +247,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
res->status = http_get_status(500);
ctx->origin = INTERNAL;
}
print(ERR_STR "Unable to connect to [%s]:%i: %s" CLR_STR, buffer, conf->rev_proxy.port, strerror(errno));
error("Unable to connect to [%s]:%i: %s", buffer, conf->rev_proxy.port, strerror(errno));
sprintf(err_msg, "Unable to connect to server: %s.", strerror(errno));
goto proxy_err;
}
@@ -261,7 +260,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
rev_proxy_timeout_err:
res->status = http_get_status(500);
ctx->origin = INTERNAL;
print(ERR_STR "Unable to set timeout for reverse proxy socket: %s" CLR_STR, strerror(errno));
error("Unable to set timeout for reverse proxy socket");
sprintf(err_msg, "Unable to set timeout for reverse proxy socket: %s", strerror(errno));
goto proxy_err;
}
@@ -279,14 +278,14 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
if (ret < 0) {
res->status = http_get_status(502);
ctx->origin = SERVER_REQ;
print(ERR_STR "Unable to perform handshake: %s" CLR_STR, sock_strerror(&rev_proxy));
error("Unable to perform handshake: %s", sock_strerror(&rev_proxy));
sprintf(err_msg, "Unable to perform handshake: %s.", sock_strerror(&rev_proxy));
goto proxy_err;
}
}
rev_proxy_host = conf->name;
print(BLUE_STR "Established new connection with " BLD_STR "[%s]:%i" CLR_STR, buffer, conf->rev_proxy.port);
info(BLUE_STR "Established new connection with " BLD_STR "[%s]:%i", buffer, conf->rev_proxy.port);
rev_proxy:
connection = http_get_header_field(&req->hdr, "Connection");
@@ -305,7 +304,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
http_add_header_field(&req->hdr, "Connection", "keep-alive");
}
ret = rev_proxy_request_header(req, (int) client->enc);
ret = rev_proxy_request_header(req, (int) client->enc, cctx);
if (ret != 0) {
res->status = http_get_status(500);
ctx->origin = INTERNAL;
@@ -316,53 +315,42 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
if (ret < 0) {
res->status = http_get_status(502);
ctx->origin = SERVER_REQ;
print(ERR_STR "Unable to send request to server (1): %s" CLR_STR, sock_strerror(&rev_proxy));
error("Unable to send request to server (1): %s", sock_strerror(&rev_proxy));
sprintf(err_msg, "Unable to send request to server: %s.", sock_strerror(&rev_proxy));
retry = tries < 4;
goto proxy_err;
}
const char *content_length = http_get_header_field(&req->hdr, "Content-Length");
if (content_length != NULL) {
unsigned long content_len = strtoul(content_length, NULL, 10);
if (client->buf_len - client->buf_off > 0) {
unsigned long len = client->buf_len - client->buf_off;
if (len > content_len) {
len = content_len;
}
ret = sock_send(&rev_proxy, client->buf, len, 0);
if (ret <= 0) {
res->status = http_get_status(502);
ctx->origin = SERVER_REQ;
print(ERR_STR "Unable to send request to server (2): %s" CLR_STR, sock_strerror(&rev_proxy));
sprintf(err_msg, "Unable to send request to server: %s.", sock_strerror(&rev_proxy));
retry = tries < 4;
goto proxy_err;
}
content_len -= len;
}
if (content_len > 0) {
ret = sock_splice(&rev_proxy, client, buffer, sizeof(buffer), content_len);
if (ret <= 0) {
if (ret == -1) {
res->status = http_get_status(502);
ctx->origin = SERVER_REQ;
print(ERR_STR "Unable to send request to server (3): %s" CLR_STR, sock_strerror(&rev_proxy));
sprintf(err_msg, "Unable to send request to server: %s.", sock_strerror(&rev_proxy));
goto proxy_err;
} else if (ret == -2) {
res->status = http_get_status(400);
ctx->origin = CLIENT_REQ;
print(ERR_STR "Unable to receive request from client: %s" CLR_STR, sock_strerror(client));
sprintf(err_msg, "Unable to receive request from client: %s.", sock_strerror(client));
return -1;
}
res->status = http_get_status(500);
ctx->origin = INTERNAL;
print(ERR_STR "Unknown Error" CLR_STR);
return -1;
}
unsigned long content_len = content_length != NULL ? strtoul(content_length, NULL, 10) : 0;
const char *transfer_encoding = http_get_header_field(&req->hdr, "Transfer-Encoding");
ret = 0;
if (content_len > 0) {
ret = sock_splice(&rev_proxy, client, buffer, sizeof(buffer), content_len);
} else if (transfer_encoding != NULL && strstr(transfer_encoding, "chunked") != NULL) {
ret = sock_splice_chunked(&rev_proxy, client, buffer, sizeof(buffer));
}
if (ret < 0 || (content_len != 0 && ret != content_len)) {
if (ret == -1) {
res->status = http_get_status(502);
ctx->origin = SERVER_REQ;
error("Unable to send request to server (2): %s", sock_strerror(&rev_proxy));
sprintf(err_msg, "Unable to send request to server: %s.", sock_strerror(&rev_proxy));
retry = tries < 4;
goto proxy_err;
} else if (ret == -2) {
res->status = http_get_status(400);
ctx->origin = CLIENT_REQ;
error("Unable to receive request from client: %s", sock_strerror(client));
sprintf(err_msg, "Unable to receive request from client: %s.", sock_strerror(client));
return -1;
}
res->status = http_get_status(500);
ctx->origin = INTERNAL;
error("Unknown Error");
return -1;
}
ret = sock_recv(&rev_proxy, buffer, sizeof(buffer), MSG_PEEK);
@@ -377,7 +365,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
res->status = http_get_status(502);
ctx->origin = SERVER_RES;
}
print(ERR_STR "Unable to receive response from server: %s" CLR_STR, sock_strerror(&rev_proxy));
error("Unable to receive response from server: %s", sock_strerror(&rev_proxy));
sprintf(err_msg, "Unable to receive response from server: %s.", sock_strerror(&rev_proxy));
retry = tries < 4;
goto proxy_err;
@@ -389,7 +377,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
if (header_len <= 0) {
res->status = http_get_status(502);
ctx->origin = SERVER_RES;
print(ERR_STR "Unable to parse header: End of header not found" CLR_STR);
error("Unable to parse header: End of header not found");
sprintf(err_msg, "Unable to parser header: End of header not found.");
goto proxy_err;
}
@@ -398,7 +386,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
if ((buf[i] >= 0x00 && buf[i] <= 0x1F && buf[i] != '\r' && buf[i] != '\n') || buf[i] == 0x7F) {
res->status = http_get_status(502);
ctx->origin = SERVER_RES;
print(ERR_STR "Unable to parse header: Header contains illegal characters" CLR_STR);
error("Unable to parse header: Header contains illegal characters");
sprintf(err_msg, "Unable to parse header: Header contains illegal characters.");
goto proxy_err;
}
@@ -410,7 +398,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
if (pos0 == NULL) {
res->status = http_get_status(502);
ctx->origin = SERVER_RES;
print(ERR_STR "Unable to parse header: Invalid header format" CLR_STR);
error("Unable to parse header: Invalid header format");
sprintf(err_msg, "Unable to parse header: Invalid header format.");
goto proxy_err;
}
@@ -418,7 +406,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
if (strncmp(ptr, "HTTP/", 5) != 0) {
res->status = http_get_status(502);
ctx->origin = SERVER_RES;
print(ERR_STR "Unable to parse header: Invalid header format" CLR_STR);
error("Unable to parse header: Invalid header format");
sprintf(err_msg, "Unable to parse header: Invalid header format.");
goto proxy_err;
}
@@ -433,7 +421,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
} else if (res->status == NULL) {
res->status = http_get_status(502);
ctx->origin = SERVER_RES;
print(ERR_STR "Unable to parse header: Invalid or unknown status code" CLR_STR);
error("Unable to parse header: Invalid or unknown status code");
sprintf(err_msg, "Unable to parse header: Invalid or unknown status code.");
goto proxy_err;
}
@@ -442,7 +430,7 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
if (ret != 0) {
res->status = http_get_status(502);
ctx->origin = SERVER_RES;
print(ERR_STR "Unable to parse header" CLR_STR);
error("Unable to parse header");
sprintf(err_msg, "Unable to parse header.");
goto proxy_err;
}
@@ -477,13 +465,13 @@ int rev_proxy_send(sock *client, unsigned long len_to_send, int flags) {
if (flags & REV_PROXY_COMPRESS_BR) {
flags &= ~REV_PROXY_COMPRESS_GZ;
if (compress_init(&comp_ctx, COMPRESS_BR) != 0) {
print(ERR_STR "Unable to init brotli: %s" CLR_STR, strerror(errno));
error("Unable to init brotli");
flags &= ~REV_PROXY_COMPRESS_BR;
}
} else if (flags & REV_PROXY_COMPRESS_GZ) {
flags &= ~REV_PROXY_COMPRESS_BR;
if (compress_init(&comp_ctx, COMPRESS_GZ) != 0) {
print(ERR_STR "Unable to init gzip: %s" CLR_STR, strerror(errno));
error("Unable to init gzip");
flags &= ~REV_PROXY_COMPRESS_GZ;
}
}
@@ -491,20 +479,18 @@ int rev_proxy_send(sock *client, unsigned long len_to_send, int flags) {
do {
snd_len = 0;
if (flags & REV_PROXY_CHUNKED) {
char *pos;
ret = sock_recv(&rev_proxy, buffer, 16, MSG_PEEK);
if (ret <= 0) goto err0;
len_to_send = strtol(buffer, NULL, 16);
pos = strstr(buffer, "\r\n");
len = pos - buffer + 2;
sock_recv(&rev_proxy, buffer, len, 0);
if (ret <= 0) {
err0:
print("Unable to receive from server: %s", sock_strerror(&rev_proxy));
ret = sock_get_chunk_header(&rev_proxy);
if (ret < 0) {
if (ret == -1) {
error("Unable to receive from server: Malformed chunk header");
} else {
error("Unable to receive from server: %s", sock_strerror(&rev_proxy));
}
break;
}
len_to_send = ret;
ret = 1;
if (len_to_send == 0 && (flags & REV_PROXY_COMPRESS)) {
finish_comp = 1;
len = 0;
@@ -518,14 +504,14 @@ int rev_proxy_send(sock *client, unsigned long len_to_send, int flags) {
unsigned long avail_in, avail_out;
ret = sock_recv(&rev_proxy, buffer, CHUNK_SIZE < (len_to_send - snd_len) ? CHUNK_SIZE : len_to_send - snd_len, 0);
if (ret <= 0) {
print("Unable to receive from server: %s", sock_strerror(&rev_proxy));
error("Unable to receive from server: %s", sock_strerror(&rev_proxy));
break;
}
len = ret;
ptr = buffer;
out:
avail_in = len;
void *next_in = ptr;
char *next_in = ptr;
do {
long buf_len = len;
if (flags & REV_PROXY_COMPRESS) {
@@ -538,15 +524,18 @@ int rev_proxy_send(sock *client, unsigned long len_to_send, int flags) {
if (buf_len != 0) {
len = sprintf(buf, "%lX\r\n", buf_len);
ret = 1;
if (flags & REV_PROXY_CHUNKED) ret = sock_send(client, buf, len, 0);
if (ret <= 0) goto err;
ret = sock_send(client, ptr, buf_len, 0);
if (ret <= 0) goto err;
if (!(flags & REV_PROXY_COMPRESS)) snd_len += ret;
if (flags & REV_PROXY_CHUNKED) ret = sock_send(client, "\r\n", 2, 0);
if (ret <= 0) {
err:
print(ERR_STR "Unable to send: %s" CLR_STR, sock_strerror(client));
error("Unable to send: %s", sock_strerror(client));
break;
}
}
@@ -558,12 +547,12 @@ int rev_proxy_send(sock *client, unsigned long len_to_send, int flags) {
if (flags & REV_PROXY_CHUNKED) sock_recv(&rev_proxy, buffer, 2, 0);
} while ((flags & REV_PROXY_CHUNKED) && len_to_send > 0);
if (ret <= 0) return (int) -1;
if (ret <= 0) return -1;
if (flags & REV_PROXY_CHUNKED) {
ret = sock_send(client, "0\r\n\r\n", 5, 0);
if (ret <= 0) {
print(ERR_STR "Unable to send: %s" CLR_STR, sock_strerror(client));
error("Unable to send: %s", sock_strerror(client));
return -1;
}
}

View File

@@ -1,8 +1,9 @@
/**
* sesimos - secure, simple, modern web server
* Reverse proxy (header file)
* src/lib/rev_proxy.h
* Lorenz Stechauner, 2021-01-07
* @brief Reverse proxy (header file)
* @file src/lib/rev_proxy.h
* @author Lorenz Stechauner
* @date 2021-01-07
*/
#ifndef SESIMOS_REV_PROXY_H
@@ -19,17 +20,17 @@
#include "http.h"
#include "config.h"
#include "../client.h"
extern sock rev_proxy;
int rev_proxy_preload();
int rev_proxy_preload(void);
int rev_proxy_request_header(http_req *req, int enc);
int rev_proxy_request_header(http_req *req, int enc, client_ctx_t *ctx);
int rev_proxy_response_header(http_req *req, http_res *res, host_config *conf);
int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_config *conf, sock *client,
http_status *custom_status, char *err_msg);
int rev_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 rev_proxy_send(sock *client, unsigned long len_to_send, int flags);

View File

@@ -1,11 +1,13 @@
/**
* sesimos - secure, simple, modern web server
* Basic TCP and TLS socket
* src/lib/sock.c
* Lorenz Stechauner, 2021-01-07
* @brief Basic TCP and TLS socket
* @file src/lib/sock.c
* @author Lorenz Stechauner
* @date 2021-01-07
*/
#include "sock.h"
#include "utils.h"
#include <openssl/err.h>
#include <openssl/ssl.h>
@@ -71,11 +73,8 @@ long sock_send(sock *s, void *buf, unsigned long len, int flags) {
long sock_recv(sock *s, void *buf, unsigned long len, int flags) {
long ret;
if (s->enc) {
if (flags & MSG_PEEK) {
ret = SSL_peek(s->ssl, buf, (int) len);
} else {
ret = SSL_read(s->ssl, buf, (int) len);
}
int (*func)(SSL*, void*, int) = (flags & MSG_PEEK) ? SSL_peek : SSL_read;
ret = func(s->ssl, buf, (int) len);
s->_ssl_error = ERR_get_error();
} else {
ret = recv(s->socket, buf, len, flags);
@@ -92,7 +91,7 @@ long sock_splice(sock *dst, sock *src, void *buf, unsigned long buf_len, unsigne
while (send_len < len) {
next_len = (buf_len < (len - send_len)) ? buf_len : (len - send_len);
ret = sock_recv(src, buf, next_len, 0);
if (ret < 0) return -2;
if (ret <= 0) return -2;
next_len = ret;
ret = sock_send(dst, buf, next_len, send_len + next_len < len ? MSG_MORE : 0);
if (ret < 0) return -1;
@@ -102,6 +101,25 @@ long sock_splice(sock *dst, sock *src, void *buf, unsigned long buf_len, unsigne
return (long) send_len;
}
long sock_splice_chunked(sock *dst, sock *src, void *buf, unsigned long buf_len) {
long ret;
unsigned long send_len = 0;
unsigned long next_len;
while (1) {
ret = sock_get_chunk_header(src);
if (ret < 0) return -2;
next_len = ret;
if (next_len <= 0) break;
ret = sock_splice(dst, src, buf, buf_len, next_len);
if (ret < 0) return ret;
}
return (long) send_len;
}
int sock_close(sock *s) {
if ((int) s->enc && s->ssl != NULL) {
if (s->_last_ret >= 0) SSL_shutdown(s->ssl);
@@ -120,7 +138,7 @@ int sock_check(sock *s) {
return recv(s->socket, &buf, 1, MSG_PEEK | MSG_DONTWAIT) == 1;
}
int sock_poll(sock *sockets[], sock *ready[], short events, int n_sock, int timeout_ms) {
int sock_poll(sock *sockets[], sock *ready[], sock *error[], int n_sock, int *n_ready, int *n_error, short events, int timeout_ms) {
struct pollfd fds[n_sock];
for (int i = 0; i < n_sock; i++) {
fds[i].fd = sockets[i]->socket;
@@ -128,20 +146,58 @@ int sock_poll(sock *sockets[], sock *ready[], short events, int n_sock, int time
}
int ret = poll(fds, n_sock, timeout_ms);
if (ret < 0 || ready == NULL) return ret;
if (ret < 0 || ready == NULL || error == NULL) return ret;
int j = 0;
*n_ready = 0, *n_error = 0;
for (int i = 0; i < n_sock; i++) {
if (fds[i].revents & events)
ready[j++] = sockets[i];
ready[(*n_ready)++] = sockets[i];
if (fds[i].revents & (POLLERR | POLLHUP | POLLNVAL))
error[(*n_error)++] = sockets[i];
}
return j;
return ret;
}
int sock_poll_read(sock *sockets[], sock *readable[], int n_sock, int timeout_ms) {
return sock_poll(sockets, readable, POLLIN, n_sock, timeout_ms);
int sock_poll_read(sock *sockets[], sock *readable[], sock *error[], int n_sock, int *n_readable, int *n_error, int timeout_ms) {
return sock_poll(sockets, readable, error, n_sock, n_readable, n_error, POLLIN, timeout_ms);
}
int sock_poll_write(sock *sockets[], sock *writable[], int n_sock, int timeout_ms) {
return sock_poll(sockets, writable, POLLOUT, n_sock, timeout_ms);
int sock_poll_write(sock *sockets[], sock *writable[], sock *error[], int n_sock, int *n_writable, int *n_error, int timeout_ms) {
return sock_poll(sockets, writable, error, n_sock, n_writable, n_error, POLLOUT, timeout_ms);
}
long sock_parse_chunk_header(const char *buf, long len, long *ret_len) {
for (int i = 0; i < len; i++) {
char ch = buf[i];
if (ch == '\r') {
continue;
} else if (ch == '\n') {
if (ret_len != NULL) *ret_len = i + 1;
return strtol(buf, NULL, 16);
} else if (!((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F'))) {
return -2;
}
}
return -1;
}
long sock_get_chunk_header(sock *s) {
long ret, len;
char buf[16];
do {
ret = sock_recv(s, buf, sizeof(buf), MSG_PEEK);
if (ret <= 0) return -2;
else if (ret < 2) continue;
ret = sock_parse_chunk_header(buf, ret, &len);
if (ret == -2) return -1;
} while (ret < 0);
if (sock_recv(s, buf, len, 0) != len)
return -2;
return ret;
}

View File

@@ -1,8 +1,9 @@
/**
* sesimos - secure, simple, modern web server
* Basic TCP and TLS socket (header file)
* src/lib/sock.h
* Lorenz Stechauner, 2021-01-07
* @brief Basic TCP and TLS socket (header file)
* @file src/lib/sock.h
* @author Lorenz Stechauner
* @date 2021-01-07
*/
#ifndef SESIMOS_SOCK_H
@@ -16,9 +17,6 @@ typedef struct {
int socket;
SSL_CTX *ctx;
SSL *ssl;
char *buf;
unsigned long buf_len;
unsigned long buf_off;
long _last_ret;
int _errno;
unsigned long _ssl_error;
@@ -34,14 +32,20 @@ long sock_recv(sock *s, void *buf, unsigned long len, int flags);
long sock_splice(sock *dst, sock *src, void *buf, unsigned long buf_len, unsigned long len);
long sock_splice_chunked(sock *dst, sock *src, void *buf, unsigned long buf_len);
int sock_close(sock *s);
int sock_check(sock *s);
int sock_poll(sock *sockets[], sock *readable[], short events, int n_sock, int timeout_ms);
int sock_poll(sock *sockets[], sock *ready[], sock *error[], int n_sock, int *n_ready, int *n_error, short events, int timeout_ms);
int sock_poll_read(sock *sockets[], sock *readable[], int n_sock, int timeout_ms);
int sock_poll_read(sock *sockets[], sock *readable[], sock *error[], int n_sock, int *n_readable, int *n_error, int timeout_ms);
int sock_poll_write(sock *sockets[], sock *writable[], int n_sock, int timeout_ms);
int sock_poll_write(sock *sockets[], sock *writable[], sock *error[], int n_sock, int *n_writable, int *n_error, int timeout_ms);
long sock_parse_chunk_header(const char *buf, long len, long *ret_len);
long sock_get_chunk_header(sock *s);
#endif //SESIMOS_SOCK_H

View File

@@ -1,8 +1,9 @@
/**
* sesimos - secure, simple, modern web server
* URI and path handlers
* src/lib/uri.c
* Lorenz Stechauner, 2020-12-13
* @brief URI and path handlers
* @file src/lib/uri.c
* @author Lorenz Stechauner
* @date 2020-12-13
*/
#include "uri.h"

View File

@@ -1,8 +1,9 @@
/**
* sesimos - secure, simple, modern web server
* URI and path handlers (header file)
* src/lib/uri.h
* Lorenz Stechauner, 2020-12-13
* @brief URI and path handlers (header file)
* @file src/lib/uri.h
* @author Lorenz Stechauner
* @date 2020-12-13
*/
#ifndef SESIMOS_URI_H

View File

@@ -1,8 +1,9 @@
/**
* sesimos - secure, simple, modern web server
* Utilities
* src/lib/utils.c
* Lorenz Stechauner, 2020-12-03
* @brief Utilities
* @file src/lib/utils.c
* @author Lorenz Stechauner
* @date 2020-12-03
*/
#include "utils.h"
@@ -12,71 +13,77 @@
#include <stdlib.h>
char *log_prefix;
static const char base64_encode_table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const int base64_mod_table[3] = {0, 2, 1};
char *format_duration(unsigned long micros, char *buf) {
if (micros < 10000) {
sprintf(buf, "%.1f ms", (double) micros / 1000);
} else if (micros < 1000000) {
sprintf(buf, "%li ms", micros / 1000);
} else if (micros < 60000000) {
} else if (micros < 1000000 - 1000) {
sprintf(buf, "%.0f ms", (double) micros / 1000);
} else if (micros < 60000000 - 1000000) {
sprintf(buf, "%.1f s", (double) micros / 1000000);
} else if (micros < 6000000000) {
sprintf(buf, "%.1f min", (double) micros / 1000000 / 60);
} else {
sprintf(buf, "%li min", micros / 1000000 / 60);
sprintf(buf, "%.0f min", (double) micros / 1000000 / 60);
}
return buf;
}
int url_encode_component(const char *str, char *enc, long *size) {
char *ptr = enc;
char ch;
memset(enc, 0, *size);
for (int i = 0; i < strlen(str); i++, ptr++) {
if ((ptr - enc) >= *size) {
return -1;
int url_encode_component(const void *in, size_t size_in, char *out, size_t size_out) {
int size = 0;
// Encode control characters
for (int i = 0; i < size_in; i++) {
unsigned char ch = ((unsigned char *) in)[i];
if (ch == ' ') {
ch = '+';
} else if (
ch <= 0x20 || ch >= 0x7F ||
!((ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z') || (ch >= '0' && ch <= '9') ||
ch == '-' || ch == '_' || ch == '.' || ch == '!' || ch == '~' || ch == '*' || ch == '\'' ||
ch == '(' || ch == ')')
) {
size += 3;
if (size < size_out) sprintf(out + size - 3, "%%%02X", ch);
ch = 0;
}
ch = str[i];
if (ch == ':' || ch == '/' || ch == '?' || ch == '#' || ch == '[' || ch == ']' || ch == '@' || ch == '!' ||
ch == '$' || ch == '&' || ch == '\'' || ch == '(' || ch == ')' || ch == '*' || ch == '+' || ch == ',' ||
ch == ';' || ch == '=' || ch < ' ' || ch > '~') {
if ((ptr - enc + 2) >= *size) {
return -1;
}
sprintf(ptr, "%%%02X", ch);
ptr += 2;
} else if (ch == ' ') {
ptr[0] = '+';
} else {
ptr[0] = ch;
if (ch != 0) {
size++;
if (size < size_out) out[size - 1] = (char) ch;
}
}
*size = ptr - enc;
return 0;
// Set terminating null byte
if (size_out > 0) out[size < size_out ? size : size_out - 1] = 0;
// Return theoretical size
return size;
}
int url_encode(const char *str, char *enc, long *size) {
char *ptr = enc;
unsigned char ch;
memset(enc, 0, *size);
for (int i = 0; i < strlen(str); i++, ptr++) {
if ((ptr - enc) >= *size) {
return -1;
}
ch = str[i];
if (ch > 0x7F || ch == ' ') {
if ((ptr - enc + 2) >= *size) {
return -1;
}
sprintf(ptr, "%%%02X", ch);
ptr += 2;
int url_encode(const void *in, size_t size_in, char *out, size_t size_out) {
int size = 0;
// Encode control characters
for (int i = 0; i < size_in; i++) {
unsigned char ch = ((unsigned char *) in)[i];
if (ch <= 0x20 || ch >= 0x7F) {
size += 3;
if (size < size_out) sprintf(out + size - 3, "%%%02X", ch);
} else {
ptr[0] = (char) ch;
size++;
if (size < size_out) out[size - 1] = (char) ch;
}
}
*size = ptr - enc;
return 0;
// Set terminating null byte
if (size_out > 0) out[size < size_out ? size : size_out - 1] = 0;
// Return theoretical size
return size;
}
int url_decode(const char *str, char *dec, long *size) {

View File

@@ -1,8 +1,9 @@
/**
* sesimos - secure, simple, modern web server
* Utilities (header file)
* src/lib/utils.h
* Lorenz Stechauner, 2020-12-03
* @brief Utilities (header file)
* @file src/lib/utils.h
* @author Lorenz Stechauner
* @date 2020-12-03
*/
#ifndef SESIMOS_UTILS_H
@@ -18,27 +19,11 @@
#define HTTP_STR "\x1B[1;31m"
#define HTTPS_STR "\x1B[1;32m"
extern char *log_prefix;
static const char base64_encode_table[64] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static const int base64_mod_table[3] = {0, 2, 1};
#define out_1(fmt) fprintf(stdout, "%s" fmt "\n", log_prefix)
#define out_2(fmt, args...) fprintf(stdout, "%s" fmt "\n", log_prefix, args)
#define out_x(x, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8, arg9, FUNC, ...) FUNC
#define print(...) out_x(, ##__VA_ARGS__, out_2(__VA_ARGS__), out_2(__VA_ARGS__), out_2(__VA_ARGS__), \
out_2(__VA_ARGS__), out_2(__VA_ARGS__), out_2(__VA_ARGS__), out_2(__VA_ARGS__), \
out_2(__VA_ARGS__), out_1(__VA_ARGS__))
char *format_duration(unsigned long micros, char *buf);
int url_encode_component(const char *str, char *enc, long *size);
int url_encode_component(const void *in, size_t size_in, char *out, size_t size_out);
int url_encode(const char *str, char *enc, long *size);
int url_encode(const void *in, size_t size_in, char *out, size_t size_out);
int url_decode(const char *str, char *dec, long *size);

View File

@@ -1,11 +1,13 @@
/**
* sesimos - secure, simple, modern web server
* WebSocket reverse proxy
* src/lib/websocket.c
* Lorenz Stechauner, 2022-08-16
* @brief WebSocket reverse proxy
* @file src/lib/websocket.c
* @author Lorenz Stechauner
* @date 2022-08-16
*/
#include "../defs.h"
#include "../logger.h"
#include "websocket.h"
#include "utils.h"
@@ -14,10 +16,10 @@
#include <errno.h>
#include <signal.h>
static const char ws_key_uuid[] = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
static volatile sig_atomic_t terminate = 0;
int terminate = 0;
void ws_terminate() {
void ws_terminate(int _) {
terminate = 1;
}
@@ -44,10 +46,10 @@ int ws_recv_frame_header(sock *s, ws_frame *frame) {
long ret = sock_recv(s, buf, 2, 0);
if (ret < 0) {
print(ERR_STR "Unable to receive from socket: %s" CLR_STR, strerror(errno));
error("Unable to receive from socket");
return -1;
} else if (ret != 2) {
print(ERR_STR "Unable to receive 2 bytes from socket" CLR_STR);
error("Unable to receive 2 bytes from socket");
return -2;
}
@@ -69,10 +71,10 @@ int ws_recv_frame_header(sock *s, ws_frame *frame) {
ret = sock_recv(s, buf, remaining, 0);
if (ret < 0) {
print(ERR_STR "Unable to receive from socket: %s" CLR_STR, strerror(errno));
error("Unable to receive from socket");
return -1;
} else if (ret != remaining) {
print(ERR_STR "Unable to receive correct number of bytes from socket" CLR_STR);
error("Unable to receive correct number of bytes from socket");
return -2;
}
@@ -133,10 +135,10 @@ int ws_send_frame_header(sock *s, ws_frame *frame) {
long ret = sock_send(s, buf, ptr - buf, frame->len != 0 ? MSG_MORE : 0);
if (ret < 0) {
print(ERR_STR "Unable to send to socket: %s" CLR_STR, strerror(errno));
error("Unable to send to socket");
return -1;
} else if (ret != ptr - buf) {
print(ERR_STR "Unable to send to socket" CLR_STR);
error("Unable to send to socket");
return -2;
}
@@ -145,33 +147,38 @@ int ws_send_frame_header(sock *s, ws_frame *frame) {
int ws_handle_connection(sock *s1, sock *s2) {
sock *poll_socks[2] = {s1, s2};
sock *readable[2];
int n_sock = 2;
sock *readable[2], *error[2];
int n_sock = 2, n_readable = 0, n_error = 0;
ws_frame frame;
char buf[CHUNK_SIZE];
int poll, closes = 0;
int closes = 0;
long ret;
signal(SIGINT, ws_terminate);
signal(SIGTERM, ws_terminate);
while (!terminate && closes != 3) {
poll = sock_poll_read(poll_socks, readable, n_sock, WS_TIMEOUT * 1000);
ret = sock_poll_read(poll_socks, readable, error, n_sock, &n_readable, &n_error, WS_TIMEOUT * 1000);
if (terminate) {
break;
} else if (poll < 0) {
print(ERR_STR "Unable to poll sockets: %s" CLR_STR, strerror(errno));
} else if (ret < 0) {
error("Unable to poll sockets");
return -1;
} else if (poll == 0) {
print(ERR_STR "Connection timed out" CLR_STR);
} else if (n_readable == 0) {
error("Connection timed out");
return -2;
} else if (n_error > 0) {
error("Peer closed connection");
return -3;
}
for (int i = 0; i < poll; i++) {
for (int i = 0; i < n_readable; i++) {
sock *s = readable[i];
sock *o = (s == s1) ? s2 : s1;
if (ws_recv_frame_header(s, &frame) != 0) return -3;
// debug("WebSocket: Peer %s, Opcode=0x%X, Len=%li", (s == s1) ? "1" : "2", frame.opcode, frame.len);
if (frame.opcode == 0x8) {
n_sock--;
if (s == s1) {
@@ -187,11 +194,11 @@ int ws_handle_connection(sock *s1, sock *s2) {
if (frame.len > 0) {
ret = sock_splice(o, s, buf, sizeof(buf), frame.len);
if (ret < 0) {
print(ERR_STR "Unable to forward data in WebSocket: %s" CLR_STR, strerror(errno));
return -3;
error("Unable to forward data in WebSocket");
return -4;
} else if (ret != frame.len) {
print(ERR_STR "Unable to forward correct number of bytes in WebSocket" CLR_STR);
return -3;
error("Unable to forward correct number of bytes in WebSocket");
return -4;
}
}
}

View File

@@ -1,8 +1,9 @@
/**
* sesimos - secure, simple, modern web server
* WebSocket reverse proxy (header file)
* src/lib/websocket.h
* Lorenz Stechauner, 2022-08-16
* @brief WebSocket reverse proxy (header file)
* @file src/lib/websocket.h
* @author Lorenz Stechauner
* @date 2022-08-16
*/
#ifndef SESIMOS_WEBSOCKET_H
@@ -12,8 +13,6 @@
#define WS_TIMEOUT 3600
const char *ws_key_uuid = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
typedef struct {
unsigned char f_fin:1;
unsigned char f_rsv1:1;

253
src/logger.c Normal file
View File

@@ -0,0 +1,253 @@
/**
* sesimos - secure, simple, modern web server
* @brief Logger
* @file src/logger.h
* @author Lorenz Stechauner
* @date 2022-12-10
*/
#include "logger.h"
#include "lib/utils.h"
#include <stdio.h>
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>
#include <string.h>
#include <stdarg.h>
#include <signal.h>
#include <malloc.h>
#define LOG_MAX_MSG_SIZE 2048
#define LOG_BUF_SIZE 16
#define LOG_NAME_LEN 8
#define LOG_PREFIX_LEN 256
typedef struct {
log_lvl_t lvl;
char name[LOG_NAME_LEN];
char prefix[LOG_PREFIX_LEN];
char txt[LOG_MAX_MSG_SIZE];
} log_msg_t;
typedef struct {
int rd;
int wr;
log_msg_t msgs[LOG_BUF_SIZE];
} buf_t;
static volatile sig_atomic_t logger_alive = 0;
static sem_t sem_buf, sem_buf_free, sem_buf_used;
static buf_t buffer;
static pthread_key_t key_name = -1, key_prefix = -1;
static char global_name[LOG_NAME_LEN] = "", global_prefix[LOG_PREFIX_LEN] = "";
static const char *level_keywords[] = {
"EMERG",
"ALERT",
"CRIT",
"ERROR",
"WARN",
"NOTICE",
"INFO",
"DEBUG"
};
static void err(const char *restrict msg) {
char err_buf[64];
strerror_r(errno, err_buf, sizeof(err_buf));
fprintf(stderr, ERR_STR "[%6s][logger] %s: %s" CLR_STR "\n", level_keywords[LOG_CRITICAL], msg, err_buf);
}
void logmsgf(log_lvl_t level, const char *restrict format, ...) {
char buf[256];
char err_buf[64];
va_list args;
va_start(args, format);
const char *color = (level <= LOG_ERROR) ? ERR_STR : ((level <= LOG_WARNING) ? WRN_STR : "");
if (errno != 0) {
strerror_r(errno, err_buf, sizeof(err_buf));
snprintf(buf, sizeof(buf), "%s%s: %s" CLR_STR, color, format, err_buf);
} else {
snprintf(buf, sizeof(buf), "%s%s" CLR_STR, color, format);
}
void *name = pthread_getspecific(key_name);
if (name == NULL && global_name[0] != 0) name = global_name;
void *prefix = pthread_getspecific(key_prefix);
if (prefix == NULL && global_prefix[0] != 0) prefix = global_prefix;
if (!logger_alive) {
// no logger thread running
// simply write to stdout without synchronization
printf("%s[%-6s][%-6s]%s%s ", color, level_keywords[level], (name != NULL) ? (char *) name : "", CLR_STR, (prefix != NULL) ? (char *) prefix : "");
vprintf(buf, args);
printf("\n");
} else {
// wait for free slot in buffer
try_again_free:
if (sem_wait(&sem_buf_free) != 0) {
if (errno == EINTR) {
goto try_again_free;
} else {
err("Unable to lock semaphore");
}
// cleanup
va_end(args);
return;
}
// try to lock buffer
try_again_buf:
if (sem_wait(&sem_buf) != 0) {
if (errno == EINTR) {
goto try_again_buf;
} else {
err("Unable to lock semaphore");
}
// cleanup
sem_post(&sem_buf_free);
va_end(args);
return;
}
// write message to buffer
log_msg_t *msg = &buffer.msgs[buffer.rd];
buffer.rd = (buffer.rd + 1) % LOG_BUF_SIZE;
vsnprintf(msg->txt, sizeof(msg->txt), buf, args);
msg->lvl = level;
if (name != NULL) {
snprintf(msg->name, sizeof(msg->name), "%s", (char *) name);
} else {
msg->name[0] = 0;
}
if (prefix != NULL) {
snprintf(msg->prefix, sizeof(msg->prefix), "%s", (char *) prefix);
} else {
msg->prefix[0] = 0;
}
// unlock buffer
sem_post(&sem_buf);
// unlock slot in buffer for logger
sem_post(&sem_buf_used);
}
// cleanup
va_end(args);
}
static void logger_destroy(void) {
sem_destroy(&sem_buf);
sem_destroy(&sem_buf_free);
sem_destroy(&sem_buf_used);
}
static int logger_init(void) {
int ret;
// try to initialize all three semaphores
if (sem_init(&sem_buf, 0, 1) != 0 || sem_init(&sem_buf_free, 0, 1) != 0 || sem_init(&sem_buf_used, 0, 0) != 0) {
err("Unable to initialize semaphore");
logger_destroy();
return -1;
}
// initialize read/write heads
buffer.rd = 0;
buffer.wr = 0;
// initialize thread specific values (keys)
if ((ret = pthread_key_create(&key_name, free)) != 0 || (ret = pthread_key_create(&key_prefix, free)) != 0) {
errno = ret;
err("Unable to initialize thread specific values");
logger_destroy();
return -1;
}
return 0;
}
static int logger_remaining(void) {
int val = 0;
sem_getvalue(&sem_buf_used, &val);
return val;
}
void logger_set_name(const char *restrict name) {
if (key_name == -1) {
// not initialized
strncpy(global_name, name, sizeof(global_name));
} else {
int ret;
void *ptr = pthread_getspecific(key_name);
if (!ptr) {
ptr = malloc(LOG_NAME_LEN);
if ((ret = pthread_setspecific(key_name, ptr)) != 0) {
errno = ret;
err("Unable to set thread specific values");
return;
}
}
strncpy(ptr, name, LOG_NAME_LEN);
}
}
void logger_set_prefix(const char *restrict prefix) {
if (key_name == -1) {
strncpy(global_prefix, prefix, sizeof(global_prefix));
} else {
int ret;
void *ptr = pthread_getspecific(key_name);
if (!ptr) {
ptr = malloc(LOG_PREFIX_LEN);
pthread_setspecific(key_prefix, ptr);
if ((ret = pthread_setspecific(key_prefix, ptr)) != 0) {
errno = ret;
err("Unable to set thread specific values");
return;
}
}
strncpy(ptr, prefix, LOG_PREFIX_LEN);
}
}
void logger_stop(void) {
logger_alive = 0;
}
void logger(void) {
if (logger_init() != 0)
return;
logger_set_name("logger");
logger_alive = 1;
while (logger_alive || logger_remaining() > 0) {
// wait for buffer to be filled
if (sem_wait(&sem_buf_used) != 0) {
if (errno == EINTR) {
continue;
} else {
err("Unable to lock semaphore");
break;
}
}
log_msg_t *msg = &buffer.msgs[buffer.wr];
buffer.wr = (buffer.wr + 1) % LOG_BUF_SIZE;
printf("[%s]%s %s\n", msg->name, (msg->prefix[0] != 0) ? msg->prefix : "", msg->txt);
// unlock slot in buffer
sem_post(&sem_buf_free);
}
logger_destroy();
}

40
src/logger.h Normal file
View File

@@ -0,0 +1,40 @@
/**
* sesimos - secure, simple, modern web server
* @brief Logger (header file)
* @file src/logger.h
* @author Lorenz Stechauner
* @date 2022-12-10
*/
#ifndef SESIMOS_LOGGER_H
#define SESIMOS_LOGGER_H
#define LOG_DEBUG 7
#define LOG_INFO 6
#define LOG_NOTICE 5
#define LOG_WARNING 4
#define LOG_ERROR 3
#define LOG_CRITICAL 2
#define LOG_ALERT 1
typedef unsigned char log_lvl_t;
#define debug(...) logmsgf(LOG_DEBUG, __VA_ARGS__)
#define info(...) logmsgf(LOG_INFO, __VA_ARGS__)
#define notice(...) logmsgf(LOG_NOTICE, __VA_ARGS__)
#define warning(...) logmsgf(LOG_WARNING, __VA_ARGS__)
#define error(...) logmsgf(LOG_ERROR, __VA_ARGS__)
#define critical(...) logmsgf(LOG_CRITICAL, __VA_ARGS__)
#define alert(...) logmsgf(LOG_ALERT, __VA_ARGS__)
void logmsgf(log_lvl_t level, const char *restrict format, ...);
void logger_set_name(const char *restrict name);
void logger_set_prefix(const char *restrict prefix);
void logger(void);
void logger_stop(void);
#endif //SESIMOS_LOGGER_H

View File

@@ -1,15 +1,15 @@
/**
* Sesimos - secure, simple, modern web server
* Main executable
* src/server.c
* Lorenz Stechauner, 2020-12-03
* @brief Main executable
* @file src/server.c
* @author Lorenz Stechauner
* @date 2020-12-03
*/
#define _POSIX_C_SOURCE 199309L
#include "defs.h"
#include "server.h"
#include "client.h"
#include "logger.h"
#include "lib/cache.h"
#include "lib/config.h"
@@ -35,20 +35,13 @@
#include <dirent.h>
int active = 1;
volatile sig_atomic_t active = 1;
const char *config_file;
int sockets[NUM_SOCKETS];
pid_t children[MAX_CHILDREN];
MMDB_s mmdbs[MAX_MMDB];
SSL_CTX *contexts[CONFIG_MAX_CERT_CONFIG];
void openssl_init() {
SSL_library_init();
SSL_load_error_strings();
ERR_load_BIO_strings();
OpenSSL_add_all_algorithms();
}
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) {
@@ -58,8 +51,8 @@ static int ssl_servername_cb(SSL *ssl, int *ad, void *arg) {
return SSL_TLSEXT_ERR_OK;
}
void destroy() {
fprintf(stderr, "\n" ERR_STR "Terminating forcefully!" CLR_STR "\n");
void destroy(int sig) {
critical("Terminating forcefully!");
int status = 0;
int ret;
int kills = 0;
@@ -67,11 +60,11 @@ void destroy() {
if (children[i] != 0) {
ret = waitpid(children[i], &status, WNOHANG);
if (ret < 0) {
fprintf(stderr, ERR_STR "Unable to wait for child process (PID %i): %s" CLR_STR "\n", children[i], strerror(errno));
error("Unable to wait for child process (PID %i)", children[i]);
} else if (ret == children[i]) {
children[i] = 0;
if (status != 0) {
fprintf(stderr, ERR_STR "Child process with PID %i terminated with exit code %i" CLR_STR "\n", ret, status);
error("Child process with PID %i terminated with exit code %i", ret, status);
}
} else {
kill(children[i], SIGKILL);
@@ -80,15 +73,16 @@ void destroy() {
}
}
if (kills > 0) {
fprintf(stderr, ERR_STR "Killed %i child process(es)" CLR_STR "\n", kills);
critical("Killed %i child process(es)", kills);
}
cache_unload();
config_unload();
exit(2);
}
void terminate() {
fprintf(stderr, "\nTerminating gracefully...\n");
void terminate(int sig) {
fprintf(stderr, "\n");
notice("Terminating gracefully...");
active = 0;
signal(SIGINT, destroy);
@@ -106,11 +100,11 @@ void terminate() {
if (children[i] != 0) {
ret = waitpid(children[i], &status, WNOHANG);
if (ret < 0) {
fprintf(stderr, ERR_STR "Unable to wait for child process (PID %i): %s" CLR_STR "\n", children[i], strerror(errno));
critical("Unable to wait for child process (PID %i)", children[i]);
} else if (ret == children[i]) {
children[i] = 0;
if (status != 0) {
fprintf(stderr, ERR_STR "Child process with PID %i terminated with exit code %i" CLR_STR "\n", ret, status);
critical("Child process with PID %i terminated with exit code %i", ret, status);
}
} else {
kill(children[i], SIGTERM);
@@ -120,18 +114,18 @@ void terminate() {
}
if (wait_num > 0) {
fprintf(stderr, "Waiting for %i child process(es)...\n", wait_num);
notice("Waiting for %i child process(es)...", wait_num);
}
for (int i = 0; i < MAX_CHILDREN; i++) {
if (children[i] != 0) {
ret = waitpid(children[i], &status, 0);
if (ret < 0) {
fprintf(stderr, ERR_STR "Unable to wait for child process (PID %i): %s" CLR_STR "\n", children[i], strerror(errno));
critical("Unable to wait for child process (PID %i)", children[i]);
} else if (ret == children[i]) {
children[i] = 0;
if (status != 0) {
fprintf(stderr, ERR_STR "Child process with PID %i terminated with exit code %i" CLR_STR "\n", ret, status);
critical("Child process with PID %i terminated with exit code %i", ret, status);
}
}
}
@@ -143,10 +137,9 @@ void terminate() {
signal(SIGTERM, SIG_IGN);
struct timespec ts = {.tv_sec = 0, .tv_nsec = 50000000};
nanosleep(&ts, &ts);
fprintf(stderr, "\nGoodbye\n");
} else {
fprintf(stderr, "Goodbye\n");
}
info("Goodbye");
cache_unload();
config_unload();
exit(0);
@@ -174,8 +167,10 @@ int main(int argc, const char *argv[]) {
{.sin6_family = AF_INET6, .sin6_addr = IN6ADDR_ANY_INIT, .sin6_port = htons(443)}
};
if (setvbuf(stdout, NULL, _IOLBF, 0) != 0) {
fprintf(stderr, ERR_STR "Unable to set stdout to line-buffered mode: %s" CLR_STR, strerror(errno));
logger_set_name("server");
if (setvbuf(stdout, NULL, _IOLBF, 0) != 0 || setvbuf(stderr, NULL, _IOLBF, 0) != 0) {
critical("Unable to set stdout/stderr to line-buffered mode");
return 1;
}
printf("Sesimos web server " SERVER_VERSION "\n");
@@ -198,13 +193,13 @@ int main(int argc, const char *argv[]) {
return 0;
} else if (strcmp(arg, "-c") == 0 || strcmp(arg, "--config") == 0) {
if (i == argc - 1) {
fprintf(stderr, ERR_STR "Unable to parse argument %s, usage: --config <CONFIG-FILE>" CLR_STR "\n", arg);
critical("Unable to parse argument %s, usage: --config <CONFIG-FILE>", arg);
config_unload();
return 1;
}
config_file = argv[++i];
} else {
fprintf(stderr, ERR_STR "Unable to parse argument '%s'" CLR_STR "\n", arg);
critical("Unable to parse argument '%s'", arg);
config_unload();
return 1;
}
@@ -221,14 +216,14 @@ int main(int argc, const char *argv[]) {
sockets[1] = socket(AF_INET6, SOCK_STREAM, 0);
if (sockets[1] < 0) {
socket_err:
fprintf(stderr, ERR_STR "Unable to create socket: %s" CLR_STR "\n", strerror(errno));
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) {
fprintf(stderr, ERR_STR "Unable to set options for socket %i: %s" CLR_STR "\n", i, strerror(errno));
critical("Unable to set options for socket %i", i);
config_unload();
return 1;
}
@@ -237,7 +232,7 @@ int main(int argc, const char *argv[]) {
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:
fprintf(stderr, ERR_STR "Unable to bind socket to address: %s" CLR_STR "\n", strerror(errno));
critical("Unable to bind socket to address");
config_unload();
return 1;
}
@@ -248,7 +243,7 @@ int main(int argc, const char *argv[]) {
if (geoip_dir[0] != 0) {
DIR *geoip = opendir(geoip_dir);
if (geoip == NULL) {
fprintf(stderr, ERR_STR "Unable to open GeoIP dir: %s" CLR_STR "\n", strerror(errno));
critical("Unable to open GeoIP dir");
config_unload();
return 1;
}
@@ -257,21 +252,21 @@ int main(int argc, const char *argv[]) {
while ((dir = readdir(geoip)) != NULL) {
if (strcmp(dir->d_name + strlen(dir->d_name) - 5, ".mmdb") != 0) continue;
if (i >= MAX_MMDB) {
fprintf(stderr, ERR_STR "Too many .mmdb files" CLR_STR "\n");
critical("Too many .mmdb files");
config_unload();
return 1;
}
sprintf(buf, "%s/%s", geoip_dir, dir->d_name);
ret = MMDB_open(buf, 0, &mmdbs[i]);
if (ret != MMDB_SUCCESS) {
fprintf(stderr, ERR_STR "Unable to open .mmdb file: %s" CLR_STR "\n", MMDB_strerror(ret));
critical("Unable to open .mmdb file: %s", MMDB_strerror(ret));
config_unload();
return 1;
}
i++;
}
if (i == 0) {
fprintf(stderr, ERR_STR "No .mmdb files found in %s" CLR_STR "\n", geoip_dir);
critical("No .mmdb files found in %s", geoip_dir);
config_unload();
return 1;
}
@@ -288,12 +283,6 @@ int main(int argc, const char *argv[]) {
return 0;
}
openssl_init();
client.buf = NULL;
client.buf_len = 0;
client.buf_off = 0;
for (int i = 0; i < CONFIG_MAX_CERT_CONFIG; i++) {
const cert_config *conf = &config->certs[i];
if (conf->name[0] == 0) break;
@@ -309,13 +298,13 @@ int main(int argc, const char *argv[]) {
SSL_CTX_set_tlsext_servername_callback(ctx, ssl_servername_cb);
if (SSL_CTX_use_certificate_chain_file(ctx, conf->full_chain) != 1) {
fprintf(stderr, ERR_STR "Unable to load certificate chain file: %s: %s" CLR_STR "\n", 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();
return 1;
}
if (SSL_CTX_use_PrivateKey_file(ctx, conf->priv_key, SSL_FILETYPE_PEM) != 1) {
fprintf(stderr, ERR_STR "Unable to load private key file: %s: %s" CLR_STR "\n", 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();
return 1;
@@ -329,7 +318,7 @@ int main(int argc, const char *argv[]) {
for (int i = 0; i < NUM_SOCKETS; i++) {
if (listen(sockets[i], LISTEN_BACKLOG) < 0) {
fprintf(stderr, ERR_STR "Unable to listen on socket %i: %s" CLR_STR "\n", i, strerror(errno));
critical("Unable to listen on socket %i", i);
config_unload();
cache_unload();
return 1;
@@ -341,13 +330,14 @@ int main(int argc, const char *argv[]) {
poll_fds[i].events = POLLIN;
}
fprintf(stderr, "Ready to accept connections\n");
errno = 0;
notice("Ready to accept connections");
while (active) {
ready_sockets_num = poll(poll_fds, NUM_SOCKETS, 1000);
if (ready_sockets_num < 0) {
fprintf(stderr, ERR_STR "Unable to poll sockets: %s" CLR_STR "\n", strerror(errno));
terminate();
critical("Unable to poll sockets");
terminate(0);
return 1;
}
@@ -355,7 +345,7 @@ int main(int argc, const char *argv[]) {
if (poll_fds[i].revents & POLLIN) {
client_fd = accept(sockets[i], (struct sockaddr *) &client_addr, &client_addr_len);
if (client_fd < 0) {
fprintf(stderr, ERR_STR "Unable to accept connection: %s" CLR_STR "\n", strerror(errno));
critical("Unable to accept connection");
continue;
}
@@ -379,7 +369,7 @@ int main(int argc, const char *argv[]) {
}
}
} else {
fprintf(stderr, ERR_STR "Unable to create child process: %s" CLR_STR "\n", strerror(errno));
critical("Unable to create child process");
}
}
}
@@ -390,11 +380,11 @@ int main(int argc, const char *argv[]) {
if (children[i] != 0) {
ret = waitpid(children[i], &status, WNOHANG);
if (ret < 0) {
fprintf(stderr, ERR_STR "Unable to wait for child process (PID %i): %s" CLR_STR "\n", children[i], strerror(errno));
critical("Unable to wait for child process (PID %i)", children[i]);
} else if (ret == children[i]) {
children[i] = 0;
if (status != 0) {
fprintf(stderr, ERR_STR "Child process with PID %i terminated with exit code %i" CLR_STR "\n", ret, status);
critical("Child process with PID %i terminated with exit code %i", ret, status);
}
}
}

View File

@@ -1,8 +1,9 @@
/**
* sesimos - secure, simple, modern web server
* Main executable (header file)
* src/server.h
* Lorenz Stechauner, 2020-12-03
* @brief Main executable (header file)
* @file src/server.h
* @author Lorenz Stechauner
* @date 2020-12-03
*/
#ifndef SESIMOS_SERVER_H
@@ -10,6 +11,7 @@
#include <sys/time.h>
#include <maxminddb.h>
#include <signal.h>
#define NUM_SOCKETS 2
#define MAX_CHILDREN 1024
@@ -20,14 +22,11 @@
#define SERVER_TIMEOUT_INIT 4
#define SERVER_TIMEOUT 3600
extern int sockets[NUM_SOCKETS];
extern pid_t children[MAX_CHILDREN];
extern MMDB_s mmdbs[MAX_MMDB];
extern int server_keep_alive;
extern char *log_client_prefix, *log_conn_prefix, *log_req_prefix, *client_geoip;
extern char *client_addr_str, *client_addr_str_ptr, *server_addr_str, *server_addr_str_ptr, *client_host_str;
extern volatile sig_atomic_t server_keep_alive;
extern struct timeval client_timeout;
#endif //SESIMOS_SERVER_H

28
test/mock_socket.c Normal file
View File

@@ -0,0 +1,28 @@
#include <stdio.h>
#include <errno.h>
#include "mock_socket.h"
int mock_socket_send_mode;
static int sockets[256] = {0};
static int n_sockets = 0;
int mock_socket(int domain, int type, int protocol) {
printf("SOCKET\n");
return (n_sockets++) + 100;
}
ssize_t mock_send(int fd, const void *buf, size_t n, int flags) {
printf("SEND\n");
if (mock_socket_send_mode == MOCK_SOCKET_MODE_EINTR) {
errno = EINTR;
return rand() % ((ssize_t) n) - 1;
} else if (mock_socket_send_mode == MOCK_SOCKET_MODE_CLOSED) {
errno = 0; // TODO
return -1;
}
return (ssize_t) n;
}

20
test/mock_socket.h Normal file
View File

@@ -0,0 +1,20 @@
#ifndef SESIMOS_MOCK_SOCKET_H
#define SESIMOS_MOCK_SOCKET_H
#include <stdlib.h>
#define MOCK_SOCKET_MODE_SUCCESS 0
#define MOCK_SOCKET_MODE_EINTR 1
#define MOCK_SOCKET_MODE_CLOSED 2
#define socket(args...) mock_socket(args)
#define send(args...) mock_send(args)
extern int mock_socket_send_mode;
int mock_socket(int domain, int type, int protocol);
ssize_t mock_send(int fd, const void *buf, size_t n, int flags);
#endif //SESIMOS_MOCK_SOCKET_H

33
test/mock_ssl.c Normal file
View File

@@ -0,0 +1,33 @@
#include <openssl/crypto.h>
int SSL_write(SSL *ssl, const void *buf, int num) {
return num;
}
int SSL_read(SSL *ssl, void *buf, int num) {
return num;
}
int SSL_peek(SSL *ssl, void *buf, int num) {
return num;
}
int SSL_get_error(const SSL *s, int ret_code) {
return 0;
}
const char *ERR_reason_error_string(unsigned long e) {
return "";
}
int SSL_shutdown(SSL *s) {
return 0;
}
void SSL_free(SSL *ssl) {}
unsigned long ERR_get_error(void) {
return 0;
}

15
test/test_sock.c Normal file
View File

@@ -0,0 +1,15 @@
#include <criterion/criterion.h>
#include "mock_socket.h"
#include "../src/lib/sock.h"
Test(sock, sock_send_1) {
int fd = socket(AF_INET6, SOCK_STREAM, 0);
sock s;
s.enc = 0;
s.socket = fd;
long ret = sock_send(&s, "Hello", 5, 0);
cr_assert_eq(ret, 5);
}

86
test/test_utils.c Normal file
View File

@@ -0,0 +1,86 @@
#include <criterion/criterion.h>
#include <criterion/parameterized.h>
#include "../src/lib/utils.h"
struct url_encode_t {
long in_size;
char in[256];
long exp_size;
char exp[256];
};
struct format_duration_t {
unsigned long micros;
char exp[16];
};
ParameterizedTestParameters(utils, url_encode) {
static struct url_encode_t params[] = {
{0, "", 0, ""},
{9, "Test Text", 11, "Test%20Text"},
{21, "Text\0with\0null\0bytes\0", 29, "Text%00with%00null%00bytes%00"},
{59, "Text&with+some/strange_symbols-or#something?I%don't|know...", 59, "Text&with+some/strange_symbols-or#something?I%don't|know..."},
{33, "Data\x12With\x13Some" "\xFF" "Control" "\xFE" "Characters", 41, "Data%12With%13Some%FFControl%FECharacters"}
};
return cr_make_param_array(struct url_encode_t, params, sizeof(params) / sizeof(struct url_encode_t));
}
ParameterizedTest(struct url_encode_t *param, utils, url_encode) {
char out[256];
cr_assert_eq(url_encode(param->in, param->in_size, out, sizeof(out)), param->exp_size);
cr_assert_arr_eq(out, param->exp, param->exp_size + 1);
}
Test(utils, url_encode_bytes) {
char out[4];
char exp[4];
for (int i = 0; i < 256; i++) {
unsigned char ch = i;
if (ch <= 0x20 || ch >= 0x7F) {
cr_assert_eq(url_encode(&ch, 1, out, sizeof(out)), 3);
sprintf(exp, "%%%02X", ch);
cr_assert_str_eq(out, exp);
} else {
cr_assert_eq(url_encode(&ch, 1, out, sizeof(out)), 1);
sprintf(exp, "%c", ch);
cr_assert_str_eq(out, exp);
}
}
}
Test(utils, url_encode_invalid) {
cr_assert_eq(url_encode("Hello", 5, NULL, 0), 5);
}
ParameterizedTestParameters(utils, format_duration) {
static struct format_duration_t params[] = {
{0, "0.0 ms"},
{1, "0.0 ms"},
{90, "0.1 ms"},
{100, "0.1 ms"},
{110, "0.1 ms"},
{900, "0.9 ms"},
{1000, "1.0 ms"},
{9000, "9.0 ms"},
{9899, "9.9 ms"},
{9999, "10.0 ms"},
{10000, "10 ms"},
{11999, "12 ms"},
{999999, "1.0 s"},
{1000000, "1.0 s"},
{3000000, "3.0 s"},
{1000000 * 60, "1.0 min"},
{1000000 * 60 * 30L - 30000000, "29.5 min"},
{1000000 * 60 * 60L, "60.0 min"},
{1000000 * 60 * 120L, "120 min"},
};
return cr_make_param_array(struct format_duration_t, params, sizeof(params) / sizeof(struct format_duration_t));
}
ParameterizedTest(struct format_duration_t *param, utils, format_duration) {
char buf[16];
cr_assert_str_eq(format_duration(param->micros, buf), param->exp);
}