16 Commits

33 changed files with 363 additions and 240 deletions

4
.gitignore vendored
View File

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

View File

@@ -5,22 +5,71 @@ LIBS=-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 permit clean
all: prod
default: bin bin/lib bin/libsesimos.so bin/sesimos
prod: CFLAGS += -O3
prod: default
debug: default
debian: CFLAGS += $(DEBIAN_OPTS)
debian: prod
bin:
mkdir -p bin
bin/lib:
mkdir -p bin/lib
bin/%.o: src/%.c
$(CC) -c -o $@ $(CFLAGS) $<
bin/lib/%.o: src/lib/%.c
$(CC) -c -o $@ $(CFLAGS) -fPIC $<
bin/libsesimos.so: 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 $@ --shared -fPIC $(CFLAGS) $^ $(LIBS)
bin/sesimos: bin/server.o bin/client.o
$(CC) -o $@ $^ $(CFLAGS) -Lbin -lsesimos -Wl,-rpath=$(shell pwd)/bin $(LIBS)
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
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
bin/lib/cache.o: src/lib/cache.h src/lib/utils.h src/lib/uri.h src/lib/compress.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
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
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
bin/lib/rev_proxy.o: src/lib/rev_proxy.h src/defs.h src/server.h src/lib/compress.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
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

View File

@@ -1,8 +1,9 @@
/**
* 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"
@@ -29,7 +30,7 @@
#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;
@@ -48,7 +49,7 @@ host_config *get_host_config(const char *host) {
return NULL;
}
void client_terminate() {
void client_terminate(int _) {
server_keep_alive = 0;
}
@@ -85,7 +86,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);
@@ -200,12 +201,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 +248,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;
@@ -396,18 +391,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);
@@ -495,7 +495,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);
@@ -679,7 +678,7 @@ int client_request_handler(sock *client, unsigned long client_num, unsigned int
}
} 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);
@@ -720,12 +719,6 @@ 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;
}

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

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,8 +1,9 @@
/**
* 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 "cache.h"
@@ -24,7 +25,7 @@ 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));
@@ -37,11 +38,11 @@ int magic_init() {
return 0;
}
void cache_process_term() {
void cache_process_term(int _) {
cache_continue = 0;
}
int cache_process() {
int cache_process(void) {
signal(SIGINT, cache_process_term);
signal(SIGTERM, cache_process_term);
@@ -221,7 +222,7 @@ int cache_process() {
return 0;
}
int cache_init() {
int cache_init(void) {
if (magic_init() != 0) {
return -1;
}
@@ -267,7 +268,7 @@ int cache_init() {
}
}
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));

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,8 +1,9 @@
/**
* 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 "config.h"
@@ -19,7 +20,7 @@
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));
@@ -45,7 +46,7 @@ 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));

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,8 +1,9 @@
/**
* 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"
@@ -599,12 +600,6 @@ 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) {
@@ -612,7 +607,6 @@ int fastcgi_receive(fastcgi_conn *conn, sock *client, unsigned long len) {
return -1;
}
send:
rcv_len += ret;
header.contentLengthB1 = (ret >> 8) & 0xFF;
header.contentLengthB0 = ret & 0xFF;
@@ -625,3 +619,40 @@ int fastcgi_receive(fastcgi_conn *conn, sock *client, unsigned long len) {
}
return 0;
}
int fastcgi_receive_chunked(fastcgi_conn *conn, sock *client) {
long ret;
unsigned long next_len;
char tmp[16];
while (1) {
ret = sock_recv(client, tmp, sizeof(tmp), MSG_PEEK);
if (ret < 0) return -2;
else if (ret < 2) continue;
int len = 0;
for (int i = 0; i < ret; i++) {
char ch = tmp[i];
if (ch == '\r') {
continue;
} else if (ch == '\n') {
len = i + 1;
break;
} else if (!((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F'))) {
return -2;
}
}
if (len == 0) continue;
next_len = strtol(tmp, NULL, 16);
ret = sock_recv(client, tmp, len, 0);
if (ret < 0) return -2;
if (next_len <= 0) break;
ret = fastcgi_receive(conn, client, next_len);
if (ret < 0) return 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
@@ -54,4 +55,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,8 +1,9 @@
/**
* 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 "http.h"
@@ -144,7 +145,7 @@ 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));
return -1;
@@ -154,6 +155,8 @@ int http_receive_request(sock *client, http_req *req) {
if (header_len <= 0) {
print(ERR_STR "Unable to parse http header: End of header not found" CLR_STR);
return 5;
} else {
rcv_len = sock_recv(client, buf, header_len, 0);
}
for (int i = 0; i < header_len; i++) {
@@ -215,13 +218,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,8 +1,9 @@
/**
* 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"
@@ -23,10 +24,7 @@ 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;
}
@@ -323,46 +321,35 @@ int rev_proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host_conf
}
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;
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;
} 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;
}
ret = sock_recv(&rev_proxy, buffer, sizeof(buffer), MSG_PEEK);

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
@@ -22,7 +23,7 @@
extern sock rev_proxy;
int rev_proxy_preload();
int rev_proxy_preload(void);
int rev_proxy_request_header(http_req *req, int enc);

View File

@@ -1,8 +1,9 @@
/**
* 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"
@@ -102,6 +103,44 @@ 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;
char tmp[16];
while (1) {
ret = sock_recv(src, tmp, sizeof(tmp), MSG_PEEK);
if (ret < 0) return -2;
else if (ret < 2) continue;
int len = 0;
for (int i = 0; i < ret; i++) {
char ch = tmp[i];
if (ch == '\r') {
continue;
} else if (ch == '\n') {
len = i + 1;
break;
} else if (!((ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F'))) {
return -2;
}
}
if (len == 0) continue;
next_len = strtol(tmp, NULL, 16);
ret = sock_recv(src, tmp, len, 0);
if (ret < 0) return -2;
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 +159,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 +167,23 @@ 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);
}

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,16 @@ 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);
#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"

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

View File

@@ -1,8 +1,9 @@
/**
* 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"
@@ -15,9 +16,9 @@
#include <signal.h>
int terminate = 0;
volatile sig_atomic_t terminate = 0;
void ws_terminate() {
void ws_terminate(int _) {
terminate = 1;
}
@@ -145,33 +146,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) {
} else if (ret < 0) {
print(ERR_STR "Unable to poll sockets: %s" CLR_STR, strerror(errno));
return -1;
} else if (poll == 0) {
} else if (n_readable == 0) {
print(ERR_STR "Connection timed out" CLR_STR);
return -2;
} else if (n_error > 0) {
print(ERR_STR "Peer closed connection" CLR_STR);
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;
// print("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) {
@@ -188,10 +194,10 @@ int ws_handle_connection(sock *s1, sock *s2) {
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;
return -4;
} else if (ret != frame.len) {
print(ERR_STR "Unable to forward correct number of bytes in WebSocket" CLR_STR);
return -3;
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

View File

@@ -1,8 +1,9 @@
/**
* 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
@@ -35,14 +36,14 @@
#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() {
void openssl_init(void) {
SSL_library_init();
SSL_load_error_strings();
ERR_load_BIO_strings();
@@ -58,7 +59,7 @@ static int ssl_servername_cb(SSL *ssl, int *ad, void *arg) {
return SSL_TLSEXT_ERR_OK;
}
void destroy() {
void destroy(int _) {
fprintf(stderr, "\n" ERR_STR "Terminating forcefully!" CLR_STR "\n");
int status = 0;
int ret;
@@ -87,7 +88,7 @@ void destroy() {
exit(2);
}
void terminate() {
void terminate(int _) {
fprintf(stderr, "\nTerminating gracefully...\n");
active = 0;
@@ -290,10 +291,6 @@ int main(int argc, const char *argv[]) {
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;
@@ -347,7 +344,7 @@ int main(int argc, const char *argv[]) {
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();
terminate(0);
return 1;
}

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
@@ -25,7 +27,7 @@ extern int sockets[NUM_SOCKETS];
extern pid_t children[MAX_CHILDREN];
extern MMDB_s mmdbs[MAX_MMDB];
extern int server_keep_alive;
extern volatile sig_atomic_t 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 struct timeval client_timeout;