mirror of
https://github.com/lxsang/ant-http
synced 2024-11-18 01:08:21 +01:00
Allow sandboxing plugin instances
All checks were successful
gitea-sync/ant-http/pipeline/head This commit looks good
All checks were successful
gitea-sync/ant-http/pipeline/head This commit looks good
This commit is contained in:
parent
bd0663ddf7
commit
a1acea2441
@ -76,11 +76,11 @@ void destroy_config()
|
|||||||
freedict(server_config.mimes);
|
freedict(server_config.mimes);
|
||||||
if (server_config.stat_fifo_path)
|
if (server_config.stat_fifo_path)
|
||||||
free(server_config.stat_fifo_path);
|
free(server_config.stat_fifo_path);
|
||||||
if(server_config.plugins)
|
if (server_config.plugins)
|
||||||
{
|
{
|
||||||
for_each_assoc(it, server_config.plugins)
|
for_each_assoc(it, server_config.plugins)
|
||||||
{
|
{
|
||||||
freedict((dictionary_t) it->value);
|
freedict((dictionary_t)it->value);
|
||||||
}
|
}
|
||||||
freedict(server_config.plugins);
|
freedict(server_config.plugins);
|
||||||
}
|
}
|
||||||
@ -94,7 +94,7 @@ void destroy_config()
|
|||||||
{
|
{
|
||||||
if (cnf->htdocs != NULL)
|
if (cnf->htdocs != NULL)
|
||||||
free(cnf->htdocs);
|
free(cnf->htdocs);
|
||||||
if(cnf->plugins)
|
if (cnf->plugins)
|
||||||
free(cnf->plugins);
|
free(cnf->plugins);
|
||||||
if (cnf->sock > 0)
|
if (cnf->sock > 0)
|
||||||
{
|
{
|
||||||
@ -114,7 +114,7 @@ static int config_handler(void *conf, const char *section, const char *name,
|
|||||||
config_t *pconfig = (config_t *)conf;
|
config_t *pconfig = (config_t *)conf;
|
||||||
regmatch_t regex_matches[2];
|
regmatch_t regex_matches[2];
|
||||||
char buf[255];
|
char buf[255];
|
||||||
char * tmp;
|
char *tmp;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
// trim(section, ' ');
|
// trim(section, ' ');
|
||||||
// trim(value,' ');
|
// trim(value,' ');
|
||||||
@ -125,7 +125,7 @@ static int config_handler(void *conf, const char *section, const char *name,
|
|||||||
if (stat(value, &st) == -1)
|
if (stat(value, &st) == -1)
|
||||||
mkdirp(value, 0755);
|
mkdirp(value, 0755);
|
||||||
tmp = realpath(value, NULL);
|
tmp = realpath(value, NULL);
|
||||||
if(!tmp)
|
if (!tmp)
|
||||||
{
|
{
|
||||||
ERROR("Unable to query real path for %s: %s", value, strerror(errno));
|
ERROR("Unable to query real path for %s: %s", value, strerror(errno));
|
||||||
}
|
}
|
||||||
@ -148,7 +148,7 @@ static int config_handler(void *conf, const char *section, const char *name,
|
|||||||
if (stat(value, &st) == -1)
|
if (stat(value, &st) == -1)
|
||||||
mkdirp(value, 0700);
|
mkdirp(value, 0700);
|
||||||
tmp = realpath(value, NULL);
|
tmp = realpath(value, NULL);
|
||||||
if(!tmp)
|
if (!tmp)
|
||||||
{
|
{
|
||||||
ERROR("Unable to query real path for %s: %s", value, strerror(errno));
|
ERROR("Unable to query real path for %s: %s", value, strerror(errno));
|
||||||
}
|
}
|
||||||
@ -171,7 +171,7 @@ static int config_handler(void *conf, const char *section, const char *name,
|
|||||||
removeAll(value, 0);
|
removeAll(value, 0);
|
||||||
}
|
}
|
||||||
tmp = realpath(value, NULL);
|
tmp = realpath(value, NULL);
|
||||||
if(!tmp)
|
if (!tmp)
|
||||||
{
|
{
|
||||||
ERROR("Unable to query real path for %s: %s", value, strerror(errno));
|
ERROR("Unable to query real path for %s: %s", value, strerror(errno));
|
||||||
}
|
}
|
||||||
@ -270,7 +270,7 @@ static int config_handler(void *conf, const char *section, const char *name,
|
|||||||
mkdirp(value, 0755);
|
mkdirp(value, 0755);
|
||||||
}
|
}
|
||||||
p->htdocs = realpath(value, NULL);
|
p->htdocs = realpath(value, NULL);
|
||||||
if(!p->htdocs)
|
if (!p->htdocs)
|
||||||
{
|
{
|
||||||
ERROR("Unable to query real path for %s: %s", value, strerror(errno));
|
ERROR("Unable to query real path for %s: %s", value, strerror(errno));
|
||||||
p->htdocs = strdup(value);
|
p->htdocs = strdup(value);
|
||||||
@ -280,7 +280,7 @@ static int config_handler(void *conf, const char *section, const char *name,
|
|||||||
LOG("Server root is %s", p->htdocs);
|
LOG("Server root is %s", p->htdocs);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if(strcmp(name, "plugins") == 0)
|
else if (strcmp(name, "plugins") == 0)
|
||||||
{
|
{
|
||||||
p->plugins = strdup(value);
|
p->plugins = strdup(value);
|
||||||
}
|
}
|
||||||
@ -320,34 +320,34 @@ static void init_plugins()
|
|||||||
{
|
{
|
||||||
chain_t it, it2;
|
chain_t it, it2;
|
||||||
dictionary_t config;
|
dictionary_t config;
|
||||||
const char* value;
|
const char *value;
|
||||||
for_each_assoc(it, server_config.plugins)
|
for_each_assoc(it, server_config.plugins)
|
||||||
{
|
{
|
||||||
config = (dictionary_t) it -> value;
|
config = (dictionary_t)it->value;
|
||||||
if(config)
|
if (config)
|
||||||
{
|
{
|
||||||
for_each_assoc(it2, config)
|
for_each_assoc(it2, config)
|
||||||
{
|
{
|
||||||
LOG("Plugin %s: [%s] -> [%s]", it->key, it2->key, (char*) it2->value);
|
LOG("Plugin %s: [%s] -> [%s]", it->key, it2->key, (char *)it2->value);
|
||||||
if(strncmp(it2->key, "file_type", 9) == 0 && it2->value)
|
if (strncmp(it2->key, "file_type", 9) == 0 && it2->value)
|
||||||
{
|
{
|
||||||
char* file_type = strdup((char*) it2->value);
|
char *file_type = strdup((char *)it2->value);
|
||||||
char* token;
|
char *token;
|
||||||
char *stringp = file_type;
|
char *stringp = file_type;
|
||||||
while((token = strsep(&stringp,",")))
|
while ((token = strsep(&stringp, ",")))
|
||||||
{
|
{
|
||||||
trim(token, ' ');
|
trim(token, ' ');
|
||||||
if(strlen(token) > 0)
|
if (strlen(token) > 0)
|
||||||
{
|
{
|
||||||
dput(server_config.handlers,token, strdup((char*)it->key));
|
dput(server_config.handlers, token, strdup((char *)it->key));
|
||||||
LOG("Plugin %s: support %s file", it->key, token);
|
LOG("Plugin %s: support %s file", it->key, token);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(file_type);
|
free(file_type);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
value = (char*)dvalue(config,"autoload");
|
value = (char *)dvalue(config, "autoload");
|
||||||
if( value && (strncmp(value,"1", 1) == 0 || strncmp(value, "true", 3) == 0 ) )
|
if (value && (strncmp(value, "1", 1) == 0 || strncmp(value, "true", 3) == 0))
|
||||||
{
|
{
|
||||||
// load the plugin
|
// load the plugin
|
||||||
LOG("Plugin %s: autoloading...", it->key);
|
LOG("Plugin %s: autoloading...", it->key);
|
||||||
@ -532,8 +532,7 @@ void *resolve_request(void *data)
|
|||||||
char *rqp = NULL;
|
char *rqp = NULL;
|
||||||
char *oldrqp = NULL;
|
char *oldrqp = NULL;
|
||||||
rq->client->state = ANTD_CLIENT_RESOLVE_REQUEST;
|
rq->client->state = ANTD_CLIENT_RESOLVE_REQUEST;
|
||||||
htdocs(rq, path);
|
snprintf(path, sizeof(path), "%s/%s", (char *)dvalue(rq->request, "SERVER_WWW_ROOT"), url);
|
||||||
strcat(path, url);
|
|
||||||
LOG("URL is : %s", url);
|
LOG("URL is : %s", url);
|
||||||
LOG("Resource Path is : %s", path);
|
LOG("Resource Path is : %s", path);
|
||||||
// if (path[strlen(path) - 1] == '/')
|
// if (path[strlen(path) - 1] == '/')
|
||||||
@ -567,8 +566,7 @@ void *resolve_request(void *data)
|
|||||||
{
|
{
|
||||||
newurl = __s("%s/index.%s", url, it->key);
|
newurl = __s("%s/index.%s", url, it->key);
|
||||||
memset(path, 0, sizeof(path));
|
memset(path, 0, sizeof(path));
|
||||||
htdocs(rq, path);
|
snprintf(path, sizeof(path), "%s/%s", (char *)dvalue(rq->request, "SERVER_WWW_ROOT"), newurl);
|
||||||
strcat(path, newurl);
|
|
||||||
if (stat(path, &st) != 0)
|
if (stat(path, &st) != 0)
|
||||||
{
|
{
|
||||||
free(newurl);
|
free(newurl);
|
||||||
@ -669,7 +667,7 @@ int rule_check(const char *k, const char *v, const char *host, const char *_url,
|
|||||||
char *tmp, rep[10];
|
char *tmp, rep[10];
|
||||||
int idx = 0;
|
int idx = 0;
|
||||||
memset(rep, 0, 10);
|
memset(rep, 0, 10);
|
||||||
LOG("Verify %s=%s on %s or %s", k, v , url, host);
|
LOG("Verify %s=%s on %s or %s", k, v, url, host);
|
||||||
// 1 group
|
// 1 group
|
||||||
if (!host || !(ret = regex_match(k, host, 10, key_matches)))
|
if (!host || !(ret = regex_match(k, host, 10, key_matches)))
|
||||||
{
|
{
|
||||||
@ -863,11 +861,11 @@ char *apply_rules(dictionary_t rules, const char *host, char *url)
|
|||||||
for_each_assoc(it, rules)
|
for_each_assoc(it, rules)
|
||||||
{
|
{
|
||||||
k = it->key;
|
k = it->key;
|
||||||
if(it->value)
|
if (it->value)
|
||||||
{
|
{
|
||||||
v = (char *)it->value;
|
v = (char *)it->value;
|
||||||
// 1 group
|
// 1 group
|
||||||
if (regex_match("<break>$",v, 0, NULL))
|
if (regex_match("<break>$", v, 0, NULL))
|
||||||
{
|
{
|
||||||
should_break = 1;
|
should_break = 1;
|
||||||
}
|
}
|
||||||
@ -882,7 +880,7 @@ char *apply_rules(dictionary_t rules, const char *host, char *url)
|
|||||||
*query_string = '\0';
|
*query_string = '\0';
|
||||||
query_string++;
|
query_string++;
|
||||||
}
|
}
|
||||||
if(should_break)
|
if (should_break)
|
||||||
{
|
{
|
||||||
i = rules->cap;
|
i = rules->cap;
|
||||||
LOG("Break rule check as matched found at %s -> %s", k, v);
|
LOG("Break rule check as matched found at %s -> %s", k, v);
|
||||||
@ -1718,7 +1716,7 @@ void *execute_plugin(void *data, const char *pname)
|
|||||||
port_config_t *pcnf = (port_config_t *)dvalue(server_config.ports, port_s);
|
port_config_t *pcnf = (port_config_t *)dvalue(server_config.ports, port_s);
|
||||||
|
|
||||||
// check if plugin is enabled on this port
|
// check if plugin is enabled on this port
|
||||||
if(!pcnf->plugins || !regex_match(pattern, pcnf->plugins , 0,NULL))
|
if (!pcnf->plugins || !regex_match(pattern, pcnf->plugins, 0, NULL))
|
||||||
{
|
{
|
||||||
LOG("No plugin matched in [%s] using pattern [%s]", pcnf->plugins, pattern);
|
LOG("No plugin matched in [%s] using pattern [%s]", pcnf->plugins, pattern);
|
||||||
antd_error(rq->client, 403, "Access forbidden");
|
antd_error(rq->client, 403, "Access forbidden");
|
||||||
@ -1768,19 +1766,6 @@ dictionary_t mimes_list()
|
|||||||
return server_config.mimes;
|
return server_config.mimes;
|
||||||
}
|
}
|
||||||
|
|
||||||
void dbdir(char **dest)
|
|
||||||
{
|
|
||||||
*dest = server_config.db_path;
|
|
||||||
}
|
|
||||||
void tmpdir(char **dest)
|
|
||||||
{
|
|
||||||
*dest = server_config.tmpdir;
|
|
||||||
}
|
|
||||||
void plugindir(char **dest)
|
|
||||||
{
|
|
||||||
*dest =server_config.plugins_dir;
|
|
||||||
}
|
|
||||||
|
|
||||||
#ifdef USE_ZLIB
|
#ifdef USE_ZLIB
|
||||||
int compressable(char *ctype)
|
int compressable(char *ctype)
|
||||||
{
|
{
|
||||||
|
688
httpd.c
688
httpd.c
@ -18,8 +18,8 @@
|
|||||||
#include "lib/utils.h"
|
#include "lib/utils.h"
|
||||||
|
|
||||||
#define SEND_STAT(fd, buff, ret, ...) \
|
#define SEND_STAT(fd, buff, ret, ...) \
|
||||||
snprintf(buff, BUFFLEN, ##__VA_ARGS__); \
|
snprintf(buff, BUFFLEN, ##__VA_ARGS__); \
|
||||||
ret = write(fd, buff, strlen(buff));
|
ret = write(fd, buff, strlen(buff));
|
||||||
|
|
||||||
static antd_scheduler_t *scheduler;
|
static antd_scheduler_t *scheduler;
|
||||||
|
|
||||||
@ -33,104 +33,104 @@ static int ssl_session_ctx_id = 1;
|
|||||||
SSL_CTX *ctx;
|
SSL_CTX *ctx;
|
||||||
static void init_openssl()
|
static void init_openssl()
|
||||||
{
|
{
|
||||||
SSL_load_error_strings();
|
SSL_load_error_strings();
|
||||||
OpenSSL_add_ssl_algorithms();
|
OpenSSL_add_ssl_algorithms();
|
||||||
}
|
}
|
||||||
|
|
||||||
static SSL_CTX *create_context()
|
static SSL_CTX *create_context()
|
||||||
{
|
{
|
||||||
const SSL_METHOD *method;
|
const SSL_METHOD *method;
|
||||||
SSL_CTX *ctx;
|
SSL_CTX *ctx;
|
||||||
|
|
||||||
method = SSLv23_server_method();
|
method = SSLv23_server_method();
|
||||||
|
|
||||||
ctx = SSL_CTX_new(method);
|
ctx = SSL_CTX_new(method);
|
||||||
if (!ctx)
|
if (!ctx)
|
||||||
{
|
{
|
||||||
ERROR("Unable to create SSL context");
|
ERROR("Unable to create SSL context");
|
||||||
ERR_print_errors_fp(stderr);
|
ERR_print_errors_fp(stderr);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
return ctx;
|
return ctx;
|
||||||
}
|
}
|
||||||
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
||||||
static unsigned char antd_protocols[] = {
|
static unsigned char antd_protocols[] = {
|
||||||
//TODO: add support to HTTP/2 protocol: 2,'h', '2',
|
// TODO: add support to HTTP/2 protocol: 2,'h', '2',
|
||||||
8, 'h', 't', 't', 'p', '/', '1', '.', '1'};
|
8, 'h', 't', 't', 'p', '/', '1', '.', '1'};
|
||||||
static int alpn_advertise_protos_cb(SSL *ssl, const unsigned char **out, unsigned int *outlen, void *arg)
|
static int alpn_advertise_protos_cb(SSL *ssl, const unsigned char **out, unsigned int *outlen, void *arg)
|
||||||
{
|
{
|
||||||
UNUSED(ssl);
|
UNUSED(ssl);
|
||||||
UNUSED(arg);
|
UNUSED(arg);
|
||||||
*out = antd_protocols;
|
*out = antd_protocols;
|
||||||
*outlen = sizeof(antd_protocols);
|
*outlen = sizeof(antd_protocols);
|
||||||
return SSL_TLSEXT_ERR_OK;
|
return SSL_TLSEXT_ERR_OK;
|
||||||
}
|
}
|
||||||
static int alpn_select_cb(SSL *ssl, const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
|
static int alpn_select_cb(SSL *ssl, const unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
|
||||||
{
|
{
|
||||||
UNUSED(ssl);
|
UNUSED(ssl);
|
||||||
UNUSED(arg);
|
UNUSED(arg);
|
||||||
if (SSL_select_next_proto((unsigned char **)out, outlen, antd_protocols, sizeof(antd_protocols), in, inlen) == OPENSSL_NPN_NEGOTIATED)
|
if (SSL_select_next_proto((unsigned char **)out, outlen, antd_protocols, sizeof(antd_protocols), in, inlen) == OPENSSL_NPN_NEGOTIATED)
|
||||||
{
|
{
|
||||||
return SSL_TLSEXT_ERR_OK;
|
return SSL_TLSEXT_ERR_OK;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ERROR("No protocol support overlap found between client and server\n");
|
ERROR("No protocol support overlap found between client and server\n");
|
||||||
return SSL_TLSEXT_ERR_ALERT_FATAL;
|
return SSL_TLSEXT_ERR_ALERT_FATAL;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
static void configure_context(SSL_CTX *ctx)
|
static void configure_context(SSL_CTX *ctx)
|
||||||
{
|
{
|
||||||
#if defined(SSL_CTX_set_ecdh_auto)
|
#if defined(SSL_CTX_set_ecdh_auto)
|
||||||
SSL_CTX_set_ecdh_auto(ctx, 1);
|
SSL_CTX_set_ecdh_auto(ctx, 1);
|
||||||
#else
|
#else
|
||||||
SSL_CTX_set_tmp_ecdh(ctx, EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
|
SSL_CTX_set_tmp_ecdh(ctx, EC_KEY_new_by_curve_name(NID_X9_62_prime256v1));
|
||||||
#endif
|
#endif
|
||||||
//SSL_CTX_set_ecdh_auto(ctx, 1);
|
// SSL_CTX_set_ecdh_auto(ctx, 1);
|
||||||
/* Set some options and the session id.
|
/* Set some options and the session id.
|
||||||
* SSL_OP_NO_SSLv2: SSLv2 is insecure, disable it.
|
* 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.
|
* 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.
|
* It should be fine to use tickets in addition to server side caching.
|
||||||
*/
|
*/
|
||||||
SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1 | SSL_OP_NO_SSLv2 | SSL_OP_NO_TICKET);
|
SSL_CTX_set_options(ctx, SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1 | 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));
|
SSL_CTX_set_session_id_context(ctx, (void *)&ssl_session_ctx_id, sizeof(ssl_session_ctx_id));
|
||||||
// set the cipher suit
|
// set the cipher suit
|
||||||
config_t *cnf = config();
|
config_t *cnf = config();
|
||||||
const char *suit = cnf->ssl_cipher ? cnf->ssl_cipher : CIPHER_SUIT;
|
const char *suit = cnf->ssl_cipher ? cnf->ssl_cipher : CIPHER_SUIT;
|
||||||
LOG("Cirpher suit used: %s", suit);
|
LOG("Cirpher suit used: %s", suit);
|
||||||
if (SSL_CTX_set_cipher_list(ctx, suit) != 1)
|
if (SSL_CTX_set_cipher_list(ctx, suit) != 1)
|
||||||
{
|
{
|
||||||
ERROR("Fail to set ssl cirpher suit: %s", suit);
|
ERROR("Fail to set ssl cirpher suit: %s", suit);
|
||||||
ERR_print_errors_fp(stderr);
|
ERR_print_errors_fp(stderr);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
/* Set the key and cert */
|
/* Set the key and cert */
|
||||||
/* use the full chain bundle of certificate */
|
/* use the full chain bundle of certificate */
|
||||||
//if (SSL_CTX_use_certificate_file(ctx, server_config->sslcert, SSL_FILETYPE_PEM) <= 0) {
|
// if (SSL_CTX_use_certificate_file(ctx, server_config->sslcert, SSL_FILETYPE_PEM) <= 0) {
|
||||||
if (SSL_CTX_use_certificate_chain_file(ctx, cnf->sslcert) <= 0)
|
if (SSL_CTX_use_certificate_chain_file(ctx, cnf->sslcert) <= 0)
|
||||||
{
|
{
|
||||||
ERROR("Fail to read SSL certificate chain file: %s", cnf->sslcert);
|
ERROR("Fail to read SSL certificate chain file: %s", cnf->sslcert);
|
||||||
ERR_print_errors_fp(stderr);
|
ERR_print_errors_fp(stderr);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (SSL_CTX_use_PrivateKey_file(ctx, cnf->sslkey, SSL_FILETYPE_PEM) <= 0)
|
if (SSL_CTX_use_PrivateKey_file(ctx, cnf->sslkey, SSL_FILETYPE_PEM) <= 0)
|
||||||
{
|
{
|
||||||
ERROR("Fail to read SSL private file: %s", cnf->sslkey);
|
ERROR("Fail to read SSL private file: %s", cnf->sslkey);
|
||||||
ERR_print_errors_fp(stderr);
|
ERR_print_errors_fp(stderr);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
if (!SSL_CTX_check_private_key(ctx))
|
if (!SSL_CTX_check_private_key(ctx))
|
||||||
{
|
{
|
||||||
ERROR("Failed to validate SSL certificate");
|
ERROR("Failed to validate SSL certificate");
|
||||||
ERR_print_errors_fp(stderr);
|
ERR_print_errors_fp(stderr);
|
||||||
exit(EXIT_FAILURE);
|
exit(EXIT_FAILURE);
|
||||||
}
|
}
|
||||||
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
#if OPENSSL_VERSION_NUMBER >= 0x10002000L
|
||||||
SSL_CTX_set_alpn_select_cb(ctx, alpn_select_cb, NULL);
|
SSL_CTX_set_alpn_select_cb(ctx, alpn_select_cb, NULL);
|
||||||
SSL_CTX_set_next_protos_advertised_cb(ctx, alpn_advertise_protos_cb, NULL);
|
SSL_CTX_set_next_protos_advertised_cb(ctx, alpn_advertise_protos_cb, NULL);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,314 +138,314 @@ static void configure_context(SSL_CTX *ctx)
|
|||||||
|
|
||||||
static void stop_serve(int dummy)
|
static void stop_serve(int dummy)
|
||||||
{
|
{
|
||||||
UNUSED(dummy);
|
UNUSED(dummy);
|
||||||
// close log server
|
// close log server
|
||||||
closelog();
|
closelog();
|
||||||
sigset_t mask;
|
sigset_t mask;
|
||||||
sigemptyset(&mask);
|
sigemptyset(&mask);
|
||||||
//Blocks the SIG_IGN signal (by adding SIG_IGN to newMask)
|
// Blocks the SIG_IGN signal (by adding SIG_IGN to newMask)
|
||||||
sigaddset(&mask, SIGINT);
|
sigaddset(&mask, SIGINT);
|
||||||
sigaddset(&mask, SIGPIPE);
|
sigaddset(&mask, SIGPIPE);
|
||||||
sigaddset(&mask, SIGABRT);
|
sigaddset(&mask, SIGABRT);
|
||||||
sigprocmask(SIG_BLOCK, &mask, NULL);
|
sigprocmask(SIG_BLOCK, &mask, NULL);
|
||||||
antd_scheduler_destroy(scheduler);
|
antd_scheduler_destroy(scheduler);
|
||||||
unload_all_plugin();
|
unload_all_plugin();
|
||||||
#ifdef USE_OPENSSL
|
#ifdef USE_OPENSSL
|
||||||
FIPS_mode_set(0);
|
FIPS_mode_set(0);
|
||||||
SSL_CTX_free(ctx);
|
SSL_CTX_free(ctx);
|
||||||
FIPS_mode_set(0);
|
FIPS_mode_set(0);
|
||||||
// DEPRECATED: CONF_modules_unload(1);
|
// DEPRECATED: CONF_modules_unload(1);
|
||||||
EVP_cleanup();
|
EVP_cleanup();
|
||||||
EVP_PBE_cleanup();
|
EVP_PBE_cleanup();
|
||||||
// DEPRECATED:ENGINE_cleanup();
|
// DEPRECATED:ENGINE_cleanup();
|
||||||
CRYPTO_cleanup_all_ex_data();
|
CRYPTO_cleanup_all_ex_data();
|
||||||
// DEPRECATED: ERR_remove_state(0);
|
// DEPRECATED: ERR_remove_state(0);
|
||||||
ERR_free_strings();
|
ERR_free_strings();
|
||||||
#endif
|
#endif
|
||||||
destroy_config();
|
destroy_config();
|
||||||
sigprocmask(SIG_UNBLOCK, &mask, NULL);
|
sigprocmask(SIG_UNBLOCK, &mask, NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void antd_monitor(port_config_t *pcnf)
|
static void antd_monitor(port_config_t *pcnf)
|
||||||
{
|
{
|
||||||
antd_task_t *task = NULL;
|
antd_task_t *task = NULL;
|
||||||
int client_sock = -1;
|
int client_sock = -1;
|
||||||
struct sockaddr_in client_name;
|
struct sockaddr_in client_name;
|
||||||
socklen_t client_name_len = sizeof(client_name);
|
socklen_t client_name_len = sizeof(client_name);
|
||||||
char *client_ip = NULL;
|
char *client_ip = NULL;
|
||||||
config_t *conf = config();
|
config_t *conf = config();
|
||||||
if (pcnf->sock > 0)
|
if (pcnf->sock > 0)
|
||||||
{
|
{
|
||||||
client_sock = accept(pcnf->sock, (struct sockaddr *)&client_name, &client_name_len);
|
client_sock = accept(pcnf->sock, (struct sockaddr *)&client_name, &client_name_len);
|
||||||
if (client_sock > 0)
|
if (client_sock > 0)
|
||||||
{
|
{
|
||||||
// just dump the scheduler when we have a connection
|
// just dump the scheduler when we have a connection
|
||||||
antd_client_t *client = (antd_client_t *)malloc(sizeof(antd_client_t));
|
antd_client_t *client = (antd_client_t *)malloc(sizeof(antd_client_t));
|
||||||
antd_request_t *request = (antd_request_t *)malloc(sizeof(*request));
|
antd_request_t *request = (antd_request_t *)malloc(sizeof(*request));
|
||||||
request->client = client;
|
request->client = client;
|
||||||
request->request = dict();
|
request->request = dict();
|
||||||
client->zstream = NULL;
|
client->zstream = NULL;
|
||||||
client->z_level = ANTD_CNONE;
|
client->z_level = ANTD_CNONE;
|
||||||
|
|
||||||
dictionary_t xheader = dict();
|
dictionary_t xheader = dict();
|
||||||
dput(request->request, "REQUEST_HEADER", xheader);
|
dput(request->request, "REQUEST_HEADER", xheader);
|
||||||
dput(request->request, "REQUEST_DATA", dict());
|
dput(request->request, "REQUEST_DATA", dict());
|
||||||
dput(request->request, "SERVER_PORT", (void *)__s("%d", pcnf->port));
|
dput(request->request, "SERVER_PORT", (void *)__s("%d", pcnf->port));
|
||||||
dput(request->request, "SERVER_WWW_ROOT", (void *)strdup(pcnf->htdocs));
|
dput(request->request, "SERVER_WWW_ROOT", (void *)strdup(pcnf->htdocs));
|
||||||
/*
|
/*
|
||||||
get the remote IP
|
get the remote IP
|
||||||
*/
|
*/
|
||||||
if (client_name.sin_family == AF_INET)
|
if (client_name.sin_family == AF_INET)
|
||||||
{
|
{
|
||||||
client_ip = inet_ntoa(client_name.sin_addr);
|
client_ip = inet_ntoa(client_name.sin_addr);
|
||||||
LOG("Connect to client IP: %s on port:%d (%d)", client_ip, pcnf->port, client_sock);
|
LOG("Connect to client IP: %s on port:%d (%d)", client_ip, pcnf->port, client_sock);
|
||||||
// ip address
|
// ip address
|
||||||
dput(request->request, "REMOTE_ADDR", (void *)strdup(client_ip));
|
dput(request->request, "REMOTE_ADDR", (void *)strdup(client_ip));
|
||||||
//LOG("socket: %d\n", client_sock);
|
// LOG("socket: %d\n", client_sock);
|
||||||
}
|
}
|
||||||
|
|
||||||
// set timeout to socket
|
// set timeout to socket
|
||||||
set_nonblock(client_sock);
|
set_nonblock(client_sock);
|
||||||
|
|
||||||
client->sock = client_sock;
|
client->sock = client_sock;
|
||||||
time(&client->last_io);
|
time(&client->last_io);
|
||||||
client->ssl = NULL;
|
client->ssl = NULL;
|
||||||
client->state = ANTD_CLIENT_ACCEPT;
|
client->state = ANTD_CLIENT_ACCEPT;
|
||||||
client->z_status = 0;
|
client->z_status = 0;
|
||||||
#ifdef USE_OPENSSL
|
#ifdef USE_OPENSSL
|
||||||
if (pcnf->usessl == 1)
|
if (pcnf->usessl == 1)
|
||||||
{
|
{
|
||||||
client->ssl = (void *)SSL_new(ctx);
|
client->ssl = (void *)SSL_new(ctx);
|
||||||
if (!client->ssl)
|
if (!client->ssl)
|
||||||
{
|
{
|
||||||
finish_request(request);
|
finish_request(request);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
SSL_set_fd((SSL *)client->ssl, client->sock);
|
SSL_set_fd((SSL *)client->ssl, client->sock);
|
||||||
// this can be used in the protocol select callback to
|
// this can be used in the protocol select callback to
|
||||||
// set the protocol selected by the server
|
// set the protocol selected by the server
|
||||||
if (!SSL_set_ex_data((SSL *)client->ssl, client->sock, client))
|
if (!SSL_set_ex_data((SSL *)client->ssl, client->sock, client))
|
||||||
{
|
{
|
||||||
ERROR("Cannot set ex data to ssl client:%d", client->sock);
|
ERROR("Cannot set ex data to ssl client:%d", client->sock);
|
||||||
}
|
}
|
||||||
/*if (SSL_accept((SSL*)client->ssl) <= 0) {
|
/*if (SSL_accept((SSL*)client->ssl) <= 0) {
|
||||||
LOG("EROOR accept\n");
|
LOG("EROOR accept\n");
|
||||||
ERR_print_errors_fp(stderr);
|
ERR_print_errors_fp(stderr);
|
||||||
antd_close(client);
|
antd_close(client);
|
||||||
continue;
|
continue;
|
||||||
}*/
|
}*/
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
antd_scheduler_lock(scheduler);
|
antd_scheduler_lock(scheduler);
|
||||||
conf->connection++;
|
conf->connection++;
|
||||||
antd_scheduler_unlock(scheduler);
|
antd_scheduler_unlock(scheduler);
|
||||||
// create callback for the server
|
// create callback for the server
|
||||||
task = antd_create_task(accept_request, (void *)request, finish_request, client->last_io);
|
task = antd_create_task(accept_request, (void *)request, finish_request, client->last_io);
|
||||||
antd_task_bind_event(task, client->sock, 0, TASK_EVT_ON_WRITABLE | TASK_EVT_ON_READABLE);
|
antd_task_bind_event(task, client->sock, 0, TASK_EVT_ON_WRITABLE | TASK_EVT_ON_READABLE);
|
||||||
antd_scheduler_add_task(scheduler, task);
|
antd_scheduler_add_task(scheduler, task);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void antd_scheduler_ext_statistic(int fd, void *user_data)
|
void antd_scheduler_ext_statistic(int fd, void *user_data)
|
||||||
{
|
{
|
||||||
antd_request_t *request = (antd_request_t *)user_data;
|
antd_request_t *request = (antd_request_t *)user_data;
|
||||||
chain_t it, it1;
|
chain_t it, it1;
|
||||||
dictionary_t tmp;
|
dictionary_t tmp;
|
||||||
int ret;
|
int ret;
|
||||||
char buff[BUFFLEN];
|
char buff[BUFFLEN];
|
||||||
if (request == NULL)
|
if (request == NULL)
|
||||||
{
|
{
|
||||||
SEND_STAT(fd, buff, ret, "Data is null\n");
|
SEND_STAT(fd, buff, ret, "Data is null\n");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// send client general infomation
|
// send client general infomation
|
||||||
SEND_STAT(fd, buff, ret, "Client id: %d\n", request->client->sock);
|
SEND_STAT(fd, buff, ret, "Client id: %d\n", request->client->sock);
|
||||||
SEND_STAT(fd, buff, ret, "Last IO: %lu\n", (unsigned long)request->client->last_io);
|
SEND_STAT(fd, buff, ret, "Last IO: %lu\n", (unsigned long)request->client->last_io);
|
||||||
SEND_STAT(fd, buff, ret, "Current state: %d\n", request->client->state);
|
SEND_STAT(fd, buff, ret, "Current state: %d\n", request->client->state);
|
||||||
SEND_STAT(fd, buff, ret, "z_level: %d\n", request->client->z_level);
|
SEND_STAT(fd, buff, ret, "z_level: %d\n", request->client->z_level);
|
||||||
if (request->client->ssl)
|
if (request->client->ssl)
|
||||||
{
|
{
|
||||||
SEND_STAT(fd, buff, ret, "SSL is enabled\n");
|
SEND_STAT(fd, buff, ret, "SSL is enabled\n");
|
||||||
}
|
}
|
||||||
// send client request detail
|
// send client request detail
|
||||||
if (request->request)
|
if (request->request)
|
||||||
{
|
{
|
||||||
for_each_assoc(it, request->request)
|
for_each_assoc(it, request->request)
|
||||||
{
|
{
|
||||||
if (strcmp(it->key, "REQUEST_HEADER") == 0 ||
|
if (strcmp(it->key, "REQUEST_HEADER") == 0 ||
|
||||||
strcmp(it->key, "REQUEST_DATA") == 0 ||
|
strcmp(it->key, "REQUEST_DATA") == 0 ||
|
||||||
strcmp(it->key, "COOKIE") == 0)
|
strcmp(it->key, "COOKIE") == 0)
|
||||||
{
|
{
|
||||||
tmp = (dictionary_t)it->value;
|
tmp = (dictionary_t)it->value;
|
||||||
if (tmp)
|
if (tmp)
|
||||||
{
|
{
|
||||||
for_each_assoc(it1, tmp)
|
for_each_assoc(it1, tmp)
|
||||||
{
|
{
|
||||||
SEND_STAT(fd, buff, ret, "%s: %s\n", it1->key, (char *)it1->value);
|
SEND_STAT(fd, buff, ret, "%s: %s\n", it1->key, (char *)it1->value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
SEND_STAT(fd, buff, ret, "%s: %s\n", it->key, (char *)it->value);
|
SEND_STAT(fd, buff, ret, "%s: %s\n", it->key, (char *)it->value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
UNUSED(ret);
|
UNUSED(ret);
|
||||||
}
|
}
|
||||||
|
|
||||||
void antd_scheduler_destroy_data(void *data)
|
void antd_scheduler_destroy_data(void *data)
|
||||||
{
|
{
|
||||||
antd_request_t *rq = (antd_request_t *)data;
|
antd_request_t *rq = (antd_request_t *)data;
|
||||||
antd_client_t *proxy = (antd_client_t *)dvalue(rq->request, "PROXY_HANDLE");
|
antd_client_t *proxy = (antd_client_t *)dvalue(rq->request, "PROXY_HANDLE");
|
||||||
if (proxy)
|
if (proxy)
|
||||||
{
|
{
|
||||||
close(proxy->sock);
|
close(proxy->sock);
|
||||||
}
|
}
|
||||||
finish_request(data);
|
finish_request(data);
|
||||||
}
|
}
|
||||||
|
|
||||||
int antd_scheduler_validate_data(antd_task_t *task)
|
int antd_scheduler_validate_data(antd_task_t *task)
|
||||||
{
|
{
|
||||||
config_t *conf = config();
|
config_t *conf = config();
|
||||||
return !(difftime(time(NULL), task->access_time) > conf->scheduler_timeout);
|
return !(difftime(time(NULL), task->access_time) > conf->scheduler_timeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
int antd_task_data_id(void *data)
|
int antd_task_data_id(void *data)
|
||||||
{
|
{
|
||||||
antd_request_t *rq = (antd_request_t *)data;
|
antd_request_t *rq = (antd_request_t *)data;
|
||||||
if (!rq)
|
if (!rq)
|
||||||
return 0;
|
return 0;
|
||||||
return antd_scheduler_next_id(scheduler, rq->client->sock);
|
return antd_scheduler_next_id(scheduler, rq->client->sock);
|
||||||
/*UNUSED(data);
|
/*UNUSED(data);
|
||||||
return antd_scheduler_next_id(scheduler,0);*/
|
return antd_scheduler_next_id(scheduler,0);*/
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
pthread_t sched_th;
|
pthread_t sched_th;
|
||||||
// startup port
|
// startup port
|
||||||
chain_t it;
|
chain_t it;
|
||||||
struct timeval timeout;
|
struct timeval timeout;
|
||||||
port_config_t *pcnf;
|
port_config_t *pcnf;
|
||||||
fd_set master_set, working_set;
|
fd_set master_set, working_set;
|
||||||
int status, maxfd = 0;
|
int status, maxfd = 0;
|
||||||
int nlisten = 0;
|
int nlisten = 0;
|
||||||
// load the config first
|
// load the config first
|
||||||
if (argc == 1)
|
if (argc == 1)
|
||||||
load_config(CONFIG_FILE);
|
load_config(CONFIG_FILE);
|
||||||
else
|
else
|
||||||
load_config(argv[1]);
|
load_config(argv[1]);
|
||||||
// ignore the broken PIPE error when writing
|
// ignore the broken PIPE error when writing
|
||||||
//or reading to/from a closed socked connection
|
// or reading to/from a closed socked connection
|
||||||
signal(SIGPIPE, SIG_IGN);
|
signal(SIGPIPE, SIG_IGN);
|
||||||
signal(SIGABRT, SIG_IGN);
|
signal(SIGABRT, SIG_IGN);
|
||||||
signal(SIGINT, stop_serve);
|
signal(SIGINT, stop_serve);
|
||||||
|
|
||||||
config_t *conf = config();
|
config_t *conf = config();
|
||||||
// start syslog
|
// start syslog
|
||||||
if (conf->debug_enable == 1)
|
if (conf->debug_enable == 1)
|
||||||
{
|
{
|
||||||
setlogmask(LOG_UPTO(LOG_NOTICE));
|
setlogmask(LOG_UPTO(LOG_NOTICE));
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
setlogmask(LOG_UPTO(LOG_ERR));
|
setlogmask(LOG_UPTO(LOG_ERR));
|
||||||
}
|
}
|
||||||
openlog(SERVER_NAME, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_DAEMON);
|
openlog(SERVER_NAME, LOG_CONS | LOG_PID | LOG_NDELAY, LOG_DAEMON);
|
||||||
|
|
||||||
#ifdef USE_OPENSSL
|
#ifdef USE_OPENSSL
|
||||||
if (conf->enable_ssl == 1)
|
if (conf->enable_ssl == 1)
|
||||||
{
|
{
|
||||||
init_openssl();
|
init_openssl();
|
||||||
ctx = create_context();
|
ctx = create_context();
|
||||||
|
|
||||||
configure_context(ctx);
|
configure_context(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
// enable scheduler
|
// enable scheduler
|
||||||
// default to 4 workers
|
// default to 4 workers
|
||||||
scheduler = antd_scheduler_init(conf->n_workers, conf->stat_fifo_path);
|
scheduler = antd_scheduler_init(conf->n_workers, conf->stat_fifo_path);
|
||||||
if (scheduler == NULL)
|
if (scheduler == NULL)
|
||||||
{
|
{
|
||||||
ERROR("Unable to initialise scheduler. Exit");
|
ERROR("Unable to initialise scheduler. Exit");
|
||||||
stop_serve(0);
|
stop_serve(0);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
FD_ZERO(&master_set);
|
FD_ZERO(&master_set);
|
||||||
for_each_assoc(it, conf->ports)
|
for_each_assoc(it, conf->ports)
|
||||||
{
|
{
|
||||||
pcnf = (port_config_t *)it->value;
|
pcnf = (port_config_t *)it->value;
|
||||||
if (pcnf)
|
if (pcnf)
|
||||||
{
|
{
|
||||||
pcnf->sock = startup(&pcnf->port);
|
pcnf->sock = startup(&pcnf->port);
|
||||||
if (pcnf->sock > 0)
|
if (pcnf->sock > 0)
|
||||||
{
|
{
|
||||||
set_nonblock(pcnf->sock);
|
set_nonblock(pcnf->sock);
|
||||||
FD_SET(pcnf->sock, &master_set);
|
FD_SET(pcnf->sock, &master_set);
|
||||||
LOG("Listening on port %d", pcnf->port);
|
LOG("Listening on port %d", pcnf->port);
|
||||||
maxfd = pcnf->sock > maxfd ? pcnf->sock : maxfd;
|
maxfd = pcnf->sock > maxfd ? pcnf->sock : maxfd;
|
||||||
nlisten++;
|
nlisten++;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
ERROR("Port %d is disabled", pcnf->port);
|
ERROR("Port %d is disabled", pcnf->port);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (nlisten == 0)
|
if (nlisten == 0)
|
||||||
{
|
{
|
||||||
ERROR("No port is listenned, quit!!");
|
ERROR("No port is listenned, quit!!");
|
||||||
stop_serve(0);
|
stop_serve(0);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
// Start scheduler
|
// Start scheduler
|
||||||
if (pthread_create(&sched_th, NULL, (void *(*)(void *))antd_scheduler_wait, (void *)scheduler) != 0)
|
if (pthread_create(&sched_th, NULL, (void *(*)(void *))antd_scheduler_wait, (void *)scheduler) != 0)
|
||||||
{
|
{
|
||||||
ERROR("pthread_create: cannot start scheduler thread");
|
ERROR("pthread_create: cannot start scheduler thread");
|
||||||
stop_serve(0);
|
stop_serve(0);
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// reclaim data when exit
|
// reclaim data when exit
|
||||||
pthread_detach(sched_th);
|
pthread_detach(sched_th);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (antd_scheduler_ok(scheduler))
|
while (antd_scheduler_ok(scheduler))
|
||||||
{
|
{
|
||||||
if (conf->connection > conf->maxcon)
|
if (conf->connection > conf->maxcon)
|
||||||
{
|
{
|
||||||
//ERROR("Reach max connection %d", conf->connection);
|
// ERROR("Reach max connection %d", conf->connection);
|
||||||
timeout.tv_sec = 0;
|
timeout.tv_sec = 0;
|
||||||
timeout.tv_usec = 10000; // 5 ms
|
timeout.tv_usec = 10000; // 5 ms
|
||||||
select(0, NULL, NULL, NULL, &timeout);
|
select(0, NULL, NULL, NULL, &timeout);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
FD_ZERO(&working_set);
|
FD_ZERO(&working_set);
|
||||||
memcpy(&working_set, &master_set, sizeof(master_set));
|
memcpy(&working_set, &master_set, sizeof(master_set));
|
||||||
// blocking select
|
// blocking select
|
||||||
status = select(maxfd + 1, &working_set, NULL, NULL, NULL);
|
status = select(maxfd + 1, &working_set, NULL, NULL, NULL);
|
||||||
if (status < 0)
|
if (status < 0)
|
||||||
{
|
{
|
||||||
ERROR("select() error: %s", strerror(errno));
|
ERROR("select() error: %s", strerror(errno));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (status == 0)
|
if (status == 0)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
for_each_assoc(it, conf->ports)
|
for_each_assoc(it, conf->ports)
|
||||||
{
|
{
|
||||||
pcnf = (port_config_t *)it->value;
|
pcnf = (port_config_t *)it->value;
|
||||||
if (pcnf && pcnf->sock > 0 && FD_ISSET(pcnf->sock, &working_set))
|
if (pcnf && pcnf->sock > 0 && FD_ISSET(pcnf->sock, &working_set))
|
||||||
{
|
{
|
||||||
antd_monitor(pcnf);
|
antd_monitor(pcnf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stop_serve(0);
|
stop_serve(0);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
22
lib/handle.c
22
lib/handle.c
@ -106,28 +106,6 @@ int compressable(char *ctype)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void htdocs(antd_request_t *rq, char *dest)
|
|
||||||
{
|
|
||||||
//dictionary_t xheader = (dictionary_t)dvalue(rq->request, "REQUEST_HEADER");
|
|
||||||
char *www = (char *)dvalue(rq->request, "SERVER_WWW_ROOT");
|
|
||||||
if (www)
|
|
||||||
{
|
|
||||||
strcpy(dest, www);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
void dbdir(char **dest)
|
|
||||||
{
|
|
||||||
UNUSED(dest);
|
|
||||||
}
|
|
||||||
void tmpdir(char **dest)
|
|
||||||
{
|
|
||||||
UNUSED(dest);
|
|
||||||
}
|
|
||||||
void plugindir(char **dest)
|
|
||||||
{
|
|
||||||
UNUSED(dest);
|
|
||||||
}
|
|
||||||
|
|
||||||
const char *get_status_str(int stat)
|
const char *get_status_str(int stat)
|
||||||
{
|
{
|
||||||
switch (stat)
|
switch (stat)
|
||||||
|
18
lib/handle.h
18
lib/handle.h
@ -30,6 +30,8 @@
|
|||||||
|
|
||||||
#define ANTD_PLUGIN_READY 0x0
|
#define ANTD_PLUGIN_READY 0x0
|
||||||
#define ANTD_PLUGIN_PANNIC 0x1
|
#define ANTD_PLUGIN_PANNIC 0x1
|
||||||
|
#define ANTD_PLUGIN_INIT 0x2
|
||||||
|
#define MAX_PATH_LEN 256
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
@ -108,26 +110,22 @@ typedef struct
|
|||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
char name[128];
|
char name[MAX_PATH_LEN];
|
||||||
char* dbpath;
|
char dbpath[MAX_PATH_LEN];
|
||||||
char* tmpdir;
|
char tmpdir[MAX_PATH_LEN];
|
||||||
char* pdir;
|
char pdir[MAX_PATH_LEN];
|
||||||
dictionary_t config;
|
dictionary_t config;
|
||||||
int raw_body;
|
int raw_body;
|
||||||
int status;
|
int status;
|
||||||
} plugin_header_t;
|
} plugin_header_t;
|
||||||
|
|
||||||
int __attribute__((weak)) require_plugin(const char *);
|
|
||||||
void __attribute__((weak)) htdocs(antd_request_t *rq, char *dest);
|
|
||||||
void __attribute__((weak)) dbdir(char **dest);
|
|
||||||
void __attribute__((weak)) tmpdir(char **dest);
|
|
||||||
void __attribute__((weak)) plugindir(char **dest);
|
|
||||||
|
|
||||||
int __attribute__((weak)) compressable(char *ctype);
|
|
||||||
|
|
||||||
void set_nonblock(int socket);
|
void set_nonblock(int socket);
|
||||||
//void set_block(int socket);
|
//void set_block(int socket);
|
||||||
|
|
||||||
|
int __attribute__((weak)) compressable(char *ctype);
|
||||||
|
|
||||||
void antd_send_header(void *, antd_response_header_t *);
|
void antd_send_header(void *, antd_response_header_t *);
|
||||||
const char *get_status_str(int stat);
|
const char *get_status_str(int stat);
|
||||||
int __t(void *, const char *, ...);
|
int __t(void *, const char *, ...);
|
||||||
|
@ -40,13 +40,8 @@ STATIC PART, should be included in any plugin
|
|||||||
|
|
||||||
static plugin_header_t __plugin__;
|
static plugin_header_t __plugin__;
|
||||||
// private function
|
// private function
|
||||||
void __init_plugin__(const char* pl, dictionary_t* conf){
|
void __init_plugin__(plugin_header_t* pl, dictionary_t* conf){
|
||||||
strcpy(__plugin__.name,pl);
|
(void) memcpy(&__plugin__, pl, sizeof(plugin_header_t));
|
||||||
dbdir(&__plugin__.dbpath);
|
|
||||||
plugindir(&__plugin__.pdir);
|
|
||||||
tmpdir(&__plugin__.tmpdir);
|
|
||||||
__plugin__.config = conf;
|
|
||||||
__plugin__.raw_body = 0;
|
|
||||||
__plugin__.status = ANTD_PLUGIN_READY;
|
__plugin__.status = ANTD_PLUGIN_READY;
|
||||||
init();
|
init();
|
||||||
};
|
};
|
||||||
|
305
plugin_manager.c
305
plugin_manager.c
@ -1,12 +1,15 @@
|
|||||||
#include <dlfcn.h>
|
#include <dlfcn.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include <sys/sendfile.h>
|
||||||
|
#include <unistd.h>
|
||||||
#include "plugin_manager.h"
|
#include "plugin_manager.h"
|
||||||
#include "lib/utils.h"
|
#include "lib/utils.h"
|
||||||
#include "lib/handle.h"
|
#include "lib/handle.h"
|
||||||
#include "http_server.h"
|
#include "http_server.h"
|
||||||
|
static void unload_plugin_by_name(const char *);
|
||||||
static void unload_plugin_by_name(const char*);
|
static void *plugin_from_file(char *name, char *path, dictionary_t conf);
|
||||||
static void * plugin_from_file(char* name, dictionary_t conf);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Plugin table to store the loaded plugin
|
* Plugin table to store the loaded plugin
|
||||||
@ -23,14 +26,8 @@ struct plugin_entry *plugin_lookup(char *s)
|
|||||||
struct plugin_entry *np;
|
struct plugin_entry *np;
|
||||||
for (np = plugin_table[hash(s, HASHSIZE)]; np != NULL; np = np->next)
|
for (np = plugin_table[hash(s, HASHSIZE)]; np != NULL; np = np->next)
|
||||||
if (strcmp(s, np->pname) == 0)
|
if (strcmp(s, np->pname) == 0)
|
||||||
return np; /* found */
|
return np; /* found */
|
||||||
return NULL; /* not found */
|
return NULL; /* not found */
|
||||||
}
|
|
||||||
|
|
||||||
int require_plugin(const char* name)
|
|
||||||
{
|
|
||||||
struct plugin_entry* ptr = plugin_load((char*)name, NULL);
|
|
||||||
return ptr != NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -40,63 +37,110 @@ int require_plugin(const char* name)
|
|||||||
* @param config: plugin configuration
|
* @param config: plugin configuration
|
||||||
* @return pointer to the loaded plugin
|
* @return pointer to the loaded plugin
|
||||||
*/
|
*/
|
||||||
struct plugin_entry *plugin_load(char *name, dictionary_t config)
|
struct plugin_entry *plugin_load(char *name, dictionary_t pconf)
|
||||||
{
|
{
|
||||||
char* pname = NULL;
|
char *pname = NULL;
|
||||||
|
char path[BUFFLEN];
|
||||||
struct plugin_entry *np;
|
struct plugin_entry *np;
|
||||||
unsigned hashval;
|
unsigned hashval;
|
||||||
plugin_header_t *(*metafn)();
|
plugin_header_t *(*metafn)();
|
||||||
plugin_header_t *meta = NULL;
|
plugin_header_t *meta = NULL;
|
||||||
char* error;
|
config_t *sconf = config();
|
||||||
if(config)
|
int fromfd, tofd;
|
||||||
{
|
char *error;
|
||||||
pname = dvalue(config, "name");
|
struct stat st;
|
||||||
}
|
int is_tmp = 0;
|
||||||
if(!pname)
|
if (pconf)
|
||||||
{
|
{
|
||||||
pname = name;
|
pname = dvalue(pconf, "name");
|
||||||
}
|
}
|
||||||
if ((np = plugin_lookup(name)) == NULL) { /* not found */
|
if ((np = plugin_lookup(name)) == NULL)
|
||||||
LOG("Loading plugin: %s -> %s", name, pname);
|
{ /* not found */
|
||||||
np = (struct plugin_entry *) malloc(sizeof(*np));
|
LOG("Loading plugin: %s...", name);
|
||||||
|
np = (struct plugin_entry *)malloc(sizeof(*np));
|
||||||
if (np == NULL || name == NULL)
|
if (np == NULL || name == NULL)
|
||||||
{
|
{
|
||||||
if(np) free(np);
|
if (np)
|
||||||
return NULL;
|
free(np);
|
||||||
}
|
return NULL;
|
||||||
np->pname = strdup(name);
|
}
|
||||||
if ((np->handle = plugin_from_file(pname,config)) == NULL)
|
|
||||||
{
|
(void)snprintf(path, sizeof(path), "%s/%s%s", sconf->plugins_dir, name, sconf->plugins_ext);
|
||||||
if(np->pname) free(np->pname);
|
if (pname && strcmp(name, pname) != 0)
|
||||||
if(np) free(np);
|
{
|
||||||
return NULL;
|
// copy plugin file to tmpdir
|
||||||
}
|
(void)snprintf(path, sizeof(path), "%s/%s%s", sconf->plugins_dir, pname, sconf->plugins_ext);
|
||||||
hashval = hash(name,HASHSIZE);
|
LOG("Original plugin file: %s", path);
|
||||||
|
if ((fromfd = open(path, O_RDONLY)) < 0)
|
||||||
|
{
|
||||||
|
ERROR("Unable to open file for reading %s: %s", path, strerror(errno));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (stat(path, &st) != 0)
|
||||||
|
{
|
||||||
|
close(fromfd);
|
||||||
|
ERROR("Unable to get file stat %s: %s", path, strerror(errno));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
(void)snprintf(path, sizeof(path), "%s/%s%s", sconf->tmpdir, name, sconf->plugins_ext);
|
||||||
|
LOG("TMP plugin file: %s", path);
|
||||||
|
if ((tofd = open(path, O_WRONLY | O_CREAT, 0600)) < 0)
|
||||||
|
{
|
||||||
|
close(fromfd);
|
||||||
|
ERROR("Unable open file for reading %s: %s", path, strerror(errno));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
if (sendfile(tofd, fromfd, NULL, st.st_size) != st.st_size)
|
||||||
|
{
|
||||||
|
close(fromfd);
|
||||||
|
close(tofd);
|
||||||
|
ERROR("Unable to copy file: %s", strerror(errno));
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
is_tmp = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
np->pname = strdup(name);
|
||||||
|
np->handle = plugin_from_file(name, path, pconf);
|
||||||
|
if (is_tmp)
|
||||||
|
{
|
||||||
|
(void)remove(path);
|
||||||
|
}
|
||||||
|
if (np->handle == NULL)
|
||||||
|
{
|
||||||
|
if (np->pname)
|
||||||
|
free(np->pname);
|
||||||
|
if (np)
|
||||||
|
free(np);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
hashval = hash(name, HASHSIZE);
|
||||||
np->next = plugin_table[hashval];
|
np->next = plugin_table[hashval];
|
||||||
plugin_table[hashval] = np;
|
plugin_table[hashval] = np;
|
||||||
} else /* already there */
|
}
|
||||||
|
else /* already there */
|
||||||
{
|
{
|
||||||
LOG("The plugin %s id already loaded", name);
|
LOG("The plugin %s id already loaded", name);
|
||||||
}
|
}
|
||||||
|
|
||||||
// check if plugin is ready
|
// check if plugin is ready
|
||||||
metafn = (plugin_header_t * (*)()) dlsym(np->handle, "meta");
|
metafn = (plugin_header_t * (*)()) dlsym(np->handle, "meta");
|
||||||
if ((error = dlerror()) != NULL)
|
if ((error = dlerror()) != NULL)
|
||||||
{
|
{
|
||||||
ERROR("Unable to fetch plugin meta-data: [%s] %s", name, error);
|
ERROR("Unable to fetch plugin meta-data: [%s] %s", name, error);
|
||||||
unload_plugin_by_name(name);
|
unload_plugin_by_name(name);
|
||||||
free(np);
|
free(np);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
meta = metafn();
|
meta = metafn();
|
||||||
LOG("PLugin status: [%s] %d", name, meta->status);
|
LOG("PLugin status: [%s] %d", name, meta->status);
|
||||||
if(!meta || meta->status != ANTD_PLUGIN_READY)
|
if (!meta || meta->status != ANTD_PLUGIN_READY)
|
||||||
{
|
{
|
||||||
ERROR("Plugin is not ready or error: [%s].", name);
|
ERROR("Plugin is not ready or error: [%s].", name);
|
||||||
unload_plugin_by_name(name);
|
unload_plugin_by_name(name);
|
||||||
free(np);
|
free(np);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
return np;
|
return np;
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
@ -104,96 +148,103 @@ struct plugin_entry *plugin_load(char *name, dictionary_t config)
|
|||||||
* @param name Name of the plugin
|
* @param name Name of the plugin
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
static void * plugin_from_file(char* name, dictionary_t conf)
|
static void *plugin_from_file(char *name, char *path, dictionary_t conf)
|
||||||
{
|
{
|
||||||
void *lib_handle;
|
void *lib_handle;
|
||||||
char* error;
|
char *error;
|
||||||
char* path = __s("%s/%s%s",config()->plugins_dir,name,config()->plugins_ext);
|
config_t *cnf = config();
|
||||||
void (*fn)(const char*, dictionary_t);
|
void (*fn)(plugin_header_t *, dictionary_t);
|
||||||
lib_handle = dlopen(path, RTLD_LAZY);
|
lib_handle = dlopen(path, RTLD_LAZY | RTLD_LOCAL /*| RTLD_NODELETE*/);
|
||||||
if (!lib_handle)
|
if (!lib_handle)
|
||||||
{
|
{
|
||||||
ERROR("Cannot load plugin '%s' : '%s'",name,dlerror());
|
ERROR("Cannot load plugin '%s' : '%s'", name, dlerror());
|
||||||
if(path)
|
return NULL;
|
||||||
free(path);
|
}
|
||||||
return NULL;
|
// set database path
|
||||||
}
|
fn = (void (*)(plugin_header_t *, dictionary_t))dlsym(lib_handle, "__init_plugin__");
|
||||||
// set database path
|
if ((error = dlerror()) != NULL)
|
||||||
fn = (void (*)(const char*, dictionary_t))dlsym(lib_handle, "__init_plugin__");
|
ERROR("Problem when finding plugin init function for %s : %s", name, error);
|
||||||
if ((error = dlerror()) != NULL)
|
else
|
||||||
ERROR("Problem when finding plugin init function for %s : %s", name,error);
|
{
|
||||||
else
|
plugin_header_t header;
|
||||||
(*fn)(name, conf);
|
strncpy(header.name, name, MAX_PATH_LEN - 1);
|
||||||
if(path)
|
strncpy(header.dbpath, cnf->db_path, MAX_PATH_LEN - 1);
|
||||||
free(path);
|
strncpy(header.tmpdir, cnf->tmpdir, MAX_PATH_LEN - 1);
|
||||||
return lib_handle;
|
strncpy(header.pdir, cnf->plugins_dir, MAX_PATH_LEN - 1);
|
||||||
|
header.config = conf;
|
||||||
|
header.raw_body = 0;
|
||||||
|
header.status = ANTD_PLUGIN_INIT;
|
||||||
|
(*fn)(&header, conf);
|
||||||
|
}
|
||||||
|
// trick libc that we close this lib, but it is not realy deleted
|
||||||
|
return lib_handle;
|
||||||
}
|
}
|
||||||
|
|
||||||
void unload_plugin(struct plugin_entry* np)
|
void unload_plugin(struct plugin_entry *np)
|
||||||
{
|
{
|
||||||
char* error;
|
char *error;
|
||||||
void (*fn)() = NULL;
|
void (*fn)() = NULL;
|
||||||
// find and execute the exit function
|
// find and execute the exit function
|
||||||
fn = (void(*)()) dlsym(np->handle, "__release__");
|
fn = (void (*)())dlsym(np->handle, "__release__");
|
||||||
if ((error = dlerror()) != NULL)
|
if ((error = dlerror()) != NULL)
|
||||||
{
|
{
|
||||||
ERROR("Cant not release plugin %s : %s", np->pname,error);
|
ERROR("Cant not release plugin %s : %s", np->pname, error);
|
||||||
}
|
}
|
||||||
if(fn)
|
if (fn)
|
||||||
{
|
{
|
||||||
(*fn)();
|
(*fn)();
|
||||||
}
|
}
|
||||||
dlclose(np->handle);
|
dlclose(np->handle);
|
||||||
//free((void *) np->handle);
|
// free((void *) np->handle);
|
||||||
if(np->pname)
|
if (np->pname)
|
||||||
free((void *) np->pname);
|
free((void *)np->pname);
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
Unload a plugin by its name
|
Unload a plugin by its name
|
||||||
*/
|
*/
|
||||||
void unload_plugin_by_name(const char* name)
|
void unload_plugin_by_name(const char *name)
|
||||||
{
|
{
|
||||||
struct plugin_entry *np;
|
struct plugin_entry *np;
|
||||||
int hasval = hash(name, HASHSIZE);
|
int hasval = hash(name, HASHSIZE);
|
||||||
np = plugin_table[hasval];
|
np = plugin_table[hasval];
|
||||||
if(strcmp(np->pname,name) == 0)
|
if (strcmp(np->pname, name) == 0)
|
||||||
{
|
{
|
||||||
unload_plugin(np);
|
unload_plugin(np);
|
||||||
plugin_table[hasval] = np->next;
|
plugin_table[hasval] = np->next;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
for (np = plugin_table[hasval] ; np != NULL; np = np->next)
|
for (np = plugin_table[hasval]; np != NULL; np = np->next)
|
||||||
{
|
{
|
||||||
if (np->next != NULL && strcmp(name, np->next->pname) == 0)
|
if (np->next != NULL && strcmp(name, np->next->pname) == 0)
|
||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(np == NULL) return; // the plugin is is not loaded
|
if (np == NULL)
|
||||||
unload_plugin(np->next);
|
return; // the plugin is is not loaded
|
||||||
np->next = np->next->next;
|
unload_plugin(np->next);
|
||||||
}
|
np->next = np->next->next;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Unload all the plugin loaded on the plugin table
|
* Unload all the plugin loaded on the plugin table
|
||||||
*/
|
*/
|
||||||
void unload_all_plugin()
|
void unload_all_plugin()
|
||||||
{
|
{
|
||||||
LOG("Unload all plugins");
|
LOG("Unload all plugins");
|
||||||
for(int i=0;i<HASHSIZE;i++)
|
for (int i = 0; i < HASHSIZE; i++)
|
||||||
{
|
{
|
||||||
struct plugin_entry **np, *curr;
|
struct plugin_entry **np, *curr;
|
||||||
np = &plugin_table[i];
|
np = &plugin_table[i];
|
||||||
|
|
||||||
while((curr = *np) != NULL)
|
while ((curr = *np) != NULL)
|
||||||
{
|
{
|
||||||
(*np) = (*np)->next;
|
(*np) = (*np)->next;
|
||||||
unload_plugin(curr);
|
unload_plugin(curr);
|
||||||
free(curr);
|
free(curr);
|
||||||
}
|
}
|
||||||
plugin_table[i] = NULL;
|
plugin_table[i] = NULL;
|
||||||
}
|
}
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user