ant-http/httpd.c

270 lines
6.9 KiB
C
Raw Normal View History

2015-10-22 11:39:11 +02:00
#include <dirent.h>
#include "http_server.h"
2018-02-09 13:13:11 +01:00
#include "libs/ini.h"
2015-10-22 11:39:11 +02:00
#define MATCH(s, n) strcmp(section, s) == 0 && strcmp(name, n) == 0
2018-03-02 19:04:00 +01:00
int server_sock = -1;
2018-02-10 11:22:41 +01:00
#ifdef USE_OPENSSL
2018-02-10 16:57:21 +01:00
static int ssl_session_ctx_id = 1;
2018-03-02 19:04:00 +01:00
SSL_CTX *ctx;
2018-02-10 11:22:41 +01:00
void init_openssl()
{
SSL_load_error_strings();
OpenSSL_add_ssl_algorithms();
}
void cleanup_openssl()
{
EVP_cleanup();
}
SSL_CTX *create_context()
{
const SSL_METHOD *method;
SSL_CTX *ctx;
method = SSLv23_server_method();
ctx = SSL_CTX_new(method);
if (!ctx) {
perror("Unable to create SSL context");
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
}
return ctx;
}
void configure_context(SSL_CTX *ctx)
{
SSL_CTX_set_ecdh_auto(ctx, 1);
2018-02-10 16:57:21 +01:00
/* Set some options and the session id.
* SSL_OP_NO_SSLv2: SSLv2 is insecure, disable it.
* SSL_OP_NO_TICKET: We don't want TLS tickets used because this is an SSL server caching example.
* It should be fine to use tickets in addition to server side caching.
*/
SSL_CTX_set_options(ctx, SSL_OP_NO_SSLv2|SSL_OP_NO_TICKET);
SSL_CTX_set_session_id_context(ctx, (void *)&ssl_session_ctx_id, sizeof(ssl_session_ctx_id));
2018-02-10 11:22:41 +01:00
/* Set the key and cert */
if (SSL_CTX_use_certificate_file(ctx, server_config.sslcert, SSL_FILETYPE_PEM) <= 0) {
ERR_print_errors_fp(stderr);
2018-02-10 16:57:21 +01:00
exit(EXIT_FAILURE);
2018-02-10 11:22:41 +01:00
}
if (SSL_CTX_use_PrivateKey_file(ctx, server_config.sslkey, SSL_FILETYPE_PEM) <= 0 ) {
ERR_print_errors_fp(stderr);
2018-02-10 16:57:21 +01:00
exit(EXIT_FAILURE);
}
if (!SSL_CTX_check_private_key(ctx)) {
LOG("Failed to validate cert \n");
ERR_print_errors_fp(stderr);
exit(EXIT_FAILURE);
2018-02-10 11:22:41 +01:00
}
}
#endif
2015-10-22 11:39:11 +02:00
static int config_handler(void* conf, const char* section, const char* name,
const char* value)
{
config_t* pconfig = (config_t*)conf;
2017-07-29 22:00:34 +02:00
//char * ppath = NULL;
2015-10-22 11:39:11 +02:00
if (MATCH("SERVER", "port")) {
pconfig->port = atoi(value);
} else if (MATCH("SERVER", "plugins")) {
pconfig->plugins_dir = strdup(value);
} else if (MATCH("SERVER", "plugins_ext")) {
pconfig->plugins_ext = strdup(value);
} else if(MATCH("SERVER", "database")) {
pconfig->db_path = strdup(value);
} else if(MATCH("SERVER", "htdocs")) {
pconfig->htdocs = strdup(value);
} else if(MATCH("SERVER", "tmpdir")) {
pconfig->tmpdir = strdup(value);
2018-03-08 11:39:44 +01:00
}
else if(MATCH("SERVER", "maxcon")) {
pconfig->maxcon = atoi(value);
2018-02-05 23:04:02 +01:00
}
2018-02-10 11:22:41 +01:00
else if(MATCH("SERVER", "backlog")) {
2018-02-05 23:04:02 +01:00
pconfig->backlog = atoi(value);
2018-02-03 18:50:07 +01:00
}
2018-02-10 11:22:41 +01:00
#ifdef USE_OPENSSL
else if(MATCH("SERVER", "ssl.enable")) {
pconfig->usessl = atoi(value);
}
else if(MATCH("SERVER", "ssl.cert")) {
pconfig->sslcert = strdup(value);
}
else if(MATCH("SERVER", "ssl.key")) {
pconfig->sslkey = strdup(value);
}
#endif
2018-02-03 18:50:07 +01:00
else if (strcmp(section, "RULES") == 0)
{
2018-03-02 19:04:00 +01:00
list_put_s(&pconfig->rules, name);
list_put_s(&pconfig->rules, value);
2018-02-20 19:02:31 +01:00
}
else if (strcmp(section, "FILEHANDLER") == 0)
{
2018-03-02 19:04:00 +01:00
dput( pconfig->handlers, name ,strdup(value));
2018-02-03 18:50:07 +01:00
}
else if(strcmp(section,"AUTOSTART")==0){
2016-03-04 11:38:08 +01:00
// The server section must be added before the autostart section
// auto start plugin
plugin_load(value);
} else {
2015-10-22 11:39:11 +02:00
return 0; /* unknown section/name, error */
}
return 1;
}
void init_file_system()
{
struct stat st;
if (stat(server_config.plugins_dir, &st) == -1)
mkdir(server_config.plugins_dir, 0755);
if (stat(server_config.db_path, &st) == -1)
mkdir(server_config.db_path, 0755);
if (stat(server_config.htdocs, &st) == -1)
mkdir(server_config.htdocs, 0755);
if (stat(server_config.tmpdir, &st) == -1)
mkdir(server_config.tmpdir, 0755);
else
{
removeAll(server_config.tmpdir,0);
}
}
void load_config(const char* file)
{
server_config.port = 8888;
server_config.plugins_dir = "plugins/";
server_config.plugins_ext = ".dylib";
server_config.db_path = "databases/";
server_config.htdocs = "htdocs";
server_config.tmpdir = "tmp";
2018-02-05 23:04:02 +01:00
server_config.backlog = 100;
2018-02-23 19:54:16 +01:00
server_config.rules = list_init();
2018-02-20 19:02:31 +01:00
server_config.handlers = dict();
2018-03-08 11:39:44 +01:00
server_config.maxcon = 1000;
server_config.connection = 0;
2018-02-10 11:22:41 +01:00
#ifdef USE_OPENSSL
server_config.usessl = 0;
server_config.sslcert = "cert.pem";
server_config.sslkey = "key.pem";
#endif
2015-10-22 11:39:11 +02:00
if (ini_parse(file, config_handler, &server_config) < 0) {
LOG("Can't load '%s'\n. Used defaut configuration", file);
}
else
{
LOG("Using configuration : %s\n", file);
2018-02-10 11:22:41 +01:00
#ifdef USE_OPENSSL
2018-02-10 12:24:01 +01:00
LOG("SSL enable %d\n", server_config.usessl);
LOG("SSL cert %s\n", server_config.sslcert);
LOG("SSL key %s\n", server_config.sslkey);
2018-02-10 11:22:41 +01:00
#endif
2015-10-22 11:39:11 +02:00
}
init_file_system();
}
void stop_serve(int dummy) {
2018-03-02 19:04:00 +01:00
list_free(&(server_config.rules));
freedict(server_config.handlers);
2018-03-08 11:39:44 +01:00
LOG("Unclosed connection: %d\n", server_config.connection);
unload_all_plugin();
2018-03-02 19:04:00 +01:00
#ifdef USE_OPENSSL
SSL_CTX_free(ctx);
#endif
close(server_sock);
}
2015-10-22 11:39:11 +02:00
int main(int argc, char* argv[])
{
// load the config first
if(argc==1)
load_config(CONFIG);
else
load_config(argv[1]);
unsigned port = server_config.port;
int client_sock = -1;
struct sockaddr_in client_name;
socklen_t client_name_len = sizeof(client_name);
pthread_t newthread;
2018-03-14 10:51:46 +01:00
char* client_ip = NULL;
2015-10-22 11:39:11 +02:00
// ignore the broken PIPE error when writing
//or reading to/from a closed socked connection
signal(SIGPIPE, SIG_IGN);
2015-10-22 13:31:35 +02:00
signal(SIGABRT, SIG_IGN);
signal(SIGINT, stop_serve);
2018-02-10 11:22:41 +01:00
#ifdef USE_OPENSSL
if( server_config.usessl == 1 )
{
init_openssl();
ctx = create_context();
configure_context(ctx);
}
#endif
2015-10-22 11:39:11 +02:00
server_sock = startup(&port);
LOG("httpd running on port %d\n", port);
while (1)
{
2018-03-08 11:39:44 +01:00
if( server_config.connection >= server_config.maxcon )
{
LOG("Too many unclosed connection (%d). Wait for it\n", server_config.connection);
continue;
}
2018-02-10 12:24:01 +01:00
antd_client_t* client = (antd_client_t*)malloc(sizeof(antd_client_t));
2015-10-22 11:39:11 +02:00
client_sock = accept(server_sock,(struct sockaddr *)&client_name,&client_name_len);
if (client_sock == -1)
{
perror("Cannot accept client request\n");
continue;
}
2018-03-14 10:51:46 +01:00
/*
get the remote IP
*/
if (client_name.sin_family == AF_INET)
{
client_ip = inet_ntoa(client_name.sin_addr);
LOG("Client IP: %s\n", client_ip);
}
//return &(((struct sockaddr_in6*)sa)->sin6_addr);
2015-10-22 11:39:11 +02:00
/* accept_request(client_sock); */
2018-03-08 12:38:53 +01:00
client->sock = client_sock;
2018-03-08 11:39:44 +01:00
server_config.connection++;
//LOG("Unclosed connection: %d\n", server_config.connection);
2018-02-10 11:22:41 +01:00
#ifdef USE_OPENSSL
2018-02-10 12:24:01 +01:00
client->ssl = NULL;
2018-02-10 11:22:41 +01:00
if(server_config.usessl == 1)
{
2018-02-10 12:24:01 +01:00
client->ssl = (void*)SSL_new(ctx);
SSL_set_fd((SSL*)client->ssl, client_sock);
2018-02-10 11:22:41 +01:00
2018-02-10 12:24:01 +01:00
if (SSL_accept((SSL*)client->ssl) <= 0) {
2018-02-10 11:22:41 +01:00
ERR_print_errors_fp(stderr);
2018-03-08 11:39:44 +01:00
antd_close(client);
2018-02-10 11:22:41 +01:00
continue;
}
}
#endif
2018-02-10 12:24:01 +01:00
if (pthread_create(&newthread , NULL,(void *(*)(void *))accept_request, (void *)client) != 0)
2018-03-08 11:39:44 +01:00
{
2015-10-22 11:39:11 +02:00
perror("pthread_create");
2018-03-08 11:39:44 +01:00
antd_close(client);
}
2015-10-22 11:39:11 +02:00
else
{
//reclaim the stack data when thread finish
pthread_detach(newthread) ;
}
2018-02-10 12:24:01 +01:00
//accept_request(&client);
2015-10-22 11:39:11 +02:00
}
2018-03-02 19:04:00 +01:00
2015-10-22 11:39:11 +02:00
close(server_sock);
return(0);
}