4 Commits

30 changed files with 203 additions and 128 deletions

View File

@@ -5,22 +5,69 @@ 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
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 $@ $^ $(LIBS) -Lbin -lsesimos -Wl,-rpath=$(shell pwd)/bin
bin/server.o: src/server.c 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.c 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.c src/lib/cache.h src/lib/utils.h src/lib/uri.h src/lib/compress.h
bin/lib/compress.o: src/lib/compress.c src/lib/compress.h
bin/lib/config.o: src/lib/config.c src/lib/config.h src/lib/utils.h src/lib/uri.h
bin/lib/fastcgi.o: src/lib/fastcgi.c 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.c src/lib/geoip.h
bin/lib/http.o: src/lib/http.c 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.c src/lib/rev_proxy.h src/defs.h src/server.h src/lib/compress.h
bin/lib/sock.o: src/lib/sock.c src/lib/sock.h
bin/lib/uri.o: src/lib/uri.c src/lib/uri.h src/lib/utils.h
bin/lib/utils.o: src/lib/utils.c src/lib/utils.h
bin/lib/websocket.o: src/lib/websocket.c 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 sesimos bin/*

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;
}

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"

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

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"

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,7 +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() {
int rev_proxy_preload(void) {
rev_proxy.buf = NULL;
rev_proxy.buf_len = 0;
rev_proxy.buf_off = 0;

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"

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

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;
}

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;
@@ -347,7 +348,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;