1
0
mirror of https://git.sr.ht/~sircmpwn/gmnisrv synced 2024-12-04 14:46:42 -05:00

Free up resources throughout

This commit is contained in:
Drew DeVault 2020-09-26 16:10:10 -04:00
parent e9641dbf1e
commit 50d21c03f5
5 changed files with 38 additions and 14 deletions

View File

@ -3,8 +3,9 @@
struct gmnisrv_config; struct gmnisrv_config;
int gmnisrv_tls_init(struct gmnisrv_config *conf); int tls_init(struct gmnisrv_config *conf);
SSL *gmnisrv_tls_get_ssl(struct gmnisrv_config *conf, int fd); void tls_finish(struct gmnisrv_config *conf);
void gmnisrv_tls_set_host(SSL *ssl, struct gmnisrv_host *host); SSL *tls_get_ssl(struct gmnisrv_config *conf, int fd);
void tls_set_host(SSL *ssl, struct gmnisrv_host *host);
#endif #endif

View File

@ -236,6 +236,7 @@ config_finish(struct gmnisrv_config *conf)
struct gmnisrv_host *next = host->next; struct gmnisrv_host *next = host->next;
free(host->hostname); free(host->hostname);
free(host->root); free(host->root);
free(host->index);
free(host); free(host);
host = next; host = next;
} }

View File

@ -40,10 +40,10 @@ main(int argc, char **argv)
int r = load_config(&conf, confpath); int r = load_config(&conf, confpath);
if (r != 0) { if (r != 0) {
server_error("Config load failed"); server_error("Config load failed");
goto exit_conf; goto exit;
} }
r = gmnisrv_tls_init(&conf); r = tls_init(&conf);
if (r != 0) { if (r != 0) {
server_error("TLS initialization failed"); server_error("TLS initialization failed");
goto exit_conf; goto exit_conf;
@ -52,13 +52,15 @@ main(int argc, char **argv)
struct gmnisrv_server server = {0}; struct gmnisrv_server server = {0};
r = server_init(&server, &conf); r = server_init(&server, &conf);
if (r != 0) { if (r != 0) {
goto exit; goto exit_tls;
} }
server_run(&server); server_run(&server);
exit:
server_finish(&server); server_finish(&server);
exit_tls:
tls_finish(&conf);
exit_conf: exit_conf:
config_finish(&conf); config_finish(&conf);
exit:
return 0; return 0;
} }

View File

@ -210,7 +210,7 @@ disconnect_client(struct gmnisrv_server *server, struct gmnisrv_client *client)
static int static int
client_init_ssl(struct gmnisrv_server *server, struct gmnisrv_client *client) client_init_ssl(struct gmnisrv_server *server, struct gmnisrv_client *client)
{ {
client->ssl = gmnisrv_tls_get_ssl(server->conf, client->sockfd); client->ssl = tls_get_ssl(server->conf, client->sockfd);
if (!client->ssl) { if (!client->ssl) {
client_error(&client->addr, client_error(&client->addr,
"unable to initialize SSL, disconnecting"); "unable to initialize SSL, disconnecting");
@ -377,6 +377,10 @@ sni_callback(SSL *ssl, int *al, void *arg)
const char *hostname = SSL_get_servername(client->ssl, const char *hostname = SSL_get_servername(client->ssl,
SSL_get_servername_type(client->ssl)); SSL_get_servername_type(client->ssl));
if (!hostname) {
return SSL_TLSEXT_ERR_NOACK;
}
struct gmnisrv_host *host = gmnisrv_config_get_host( struct gmnisrv_host *host = gmnisrv_config_get_host(
server->conf, hostname); server->conf, hostname);
if (!host) { if (!host) {
@ -384,7 +388,7 @@ sni_callback(SSL *ssl, int *al, void *arg)
} }
client->host = host; client->host = host;
gmnisrv_tls_set_host(client->ssl, client->host); tls_set_host(client->ssl, client->host);
return SSL_TLSEXT_ERR_OK; return SSL_TLSEXT_ERR_OK;
} }
@ -461,6 +465,12 @@ server_run(struct gmnisrv_server *server)
void void
server_finish(struct gmnisrv_server *server) server_finish(struct gmnisrv_server *server)
{ {
// TODO while (server->nclients) {
(void)server; disconnect_client(server, &server->clients[0]);
}
for (size_t i = 0; i < server->nfds; ++i) {
close(server->fds[i].fd);
}
free(server->fds);
free(server->clients);
} }

View File

@ -156,7 +156,7 @@ generate:
} }
int int
gmnisrv_tls_init(struct gmnisrv_config *conf) tls_init(struct gmnisrv_config *conf)
{ {
SSL_load_error_strings(); SSL_load_error_strings();
ERR_load_crypto_strings(); ERR_load_crypto_strings();
@ -177,8 +177,18 @@ gmnisrv_tls_init(struct gmnisrv_config *conf)
return 0; return 0;
} }
void
tls_finish(struct gmnisrv_config *conf)
{
SSL_CTX_free(conf->tls.ssl_ctx);
for (struct gmnisrv_host *host = conf->hosts; host; host = host->next) {
X509_free(host->x509);
EVP_PKEY_free(host->pkey);
}
}
SSL * SSL *
gmnisrv_tls_get_ssl(struct gmnisrv_config *conf, int fd) tls_get_ssl(struct gmnisrv_config *conf, int fd)
{ {
SSL *ssl = SSL_new(conf->tls.ssl_ctx); SSL *ssl = SSL_new(conf->tls.ssl_ctx);
if (!ssl) { if (!ssl) {
@ -190,7 +200,7 @@ gmnisrv_tls_get_ssl(struct gmnisrv_config *conf, int fd)
} }
void void
gmnisrv_tls_set_host(SSL *ssl, struct gmnisrv_host *host) tls_set_host(SSL *ssl, struct gmnisrv_host *host)
{ {
SSL_use_certificate(ssl, host->x509); SSL_use_certificate(ssl, host->x509);
SSL_use_PrivateKey(ssl, host->pkey); SSL_use_PrivateKey(ssl, host->pkey);