2019-11-19 17:16:05 +01:00
|
|
|
#define PLUGIN_IMPLEMENT 1
|
2018-10-08 19:29:53 +02:00
|
|
|
#include <sys/wait.h>
|
2019-11-19 17:16:05 +01:00
|
|
|
#include <antd/plugin.h>
|
2019-11-13 16:59:44 +01:00
|
|
|
#include "antd/ini.h"
|
2019-12-15 12:06:09 +01:00
|
|
|
dictionary_t cgi_bin = NULL;
|
2018-10-07 23:43:38 +02:00
|
|
|
|
2018-10-08 19:29:53 +02:00
|
|
|
static int ini_handle(void *user_data, const char *section, const char *name,
|
2018-10-08 22:30:13 +02:00
|
|
|
const char *value)
|
2018-10-07 23:43:38 +02:00
|
|
|
{
|
2018-10-08 19:29:53 +02:00
|
|
|
UNUSED(user_data);
|
2018-10-08 22:30:13 +02:00
|
|
|
if (EQU(section, "CGI"))
|
|
|
|
{
|
|
|
|
dput(cgi_bin, name, strdup(value));
|
2018-10-08 19:29:53 +02:00
|
|
|
LOG("put %s for %s\n", value, name);
|
2018-10-08 22:30:13 +02:00
|
|
|
}
|
2018-10-08 19:29:53 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
2018-10-07 23:43:38 +02:00
|
|
|
|
2018-10-08 19:29:53 +02:00
|
|
|
void init()
|
|
|
|
{
|
|
|
|
use_raw_body();
|
|
|
|
cgi_bin = dict();
|
2018-10-08 22:30:13 +02:00
|
|
|
char *cnf = config_dir();
|
|
|
|
char *file = __s("%s/cgi.ini", cnf);
|
2018-10-08 19:29:53 +02:00
|
|
|
// read ini file
|
|
|
|
if (ini_parse(file, ini_handle, NULL) < 0)
|
2018-10-08 22:30:13 +02:00
|
|
|
{
|
|
|
|
LOG("Can't load '%s'\n", file);
|
|
|
|
}
|
2019-11-13 16:59:44 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
LOG("CGI config loaded\n");
|
|
|
|
}
|
2018-10-08 19:29:53 +02:00
|
|
|
free(cnf);
|
|
|
|
free(file);
|
2018-10-07 23:43:38 +02:00
|
|
|
}
|
|
|
|
void destroy()
|
|
|
|
{
|
2018-10-08 22:30:13 +02:00
|
|
|
if (cgi_bin)
|
2018-10-08 19:29:53 +02:00
|
|
|
freedict(cgi_bin);
|
2018-10-07 23:43:38 +02:00
|
|
|
}
|
|
|
|
|
2019-12-15 12:06:09 +01:00
|
|
|
static void add_vars(list_t *l, char *k, char *v)
|
2018-10-07 23:43:38 +02:00
|
|
|
{
|
2018-10-08 22:30:13 +02:00
|
|
|
if (!v || !l || !k)
|
|
|
|
return;
|
|
|
|
char *data = __s("%s=%s", k, v);
|
2019-12-22 17:28:16 +01:00
|
|
|
list_put_ptr(l, data);
|
2018-10-07 23:43:38 +02:00
|
|
|
}
|
|
|
|
|
2018-10-08 22:30:13 +02:00
|
|
|
static void write_request_body(antd_request_t *rq, int fd)
|
2018-10-07 23:43:38 +02:00
|
|
|
{
|
2018-10-08 22:30:13 +02:00
|
|
|
char *tmp = (char *)dvalue(rq->request, "METHOD");
|
|
|
|
if (!tmp || EQU(tmp, "GET") || EQU(tmp, "HEAD"))
|
|
|
|
return;
|
2018-10-08 19:29:53 +02:00
|
|
|
int clen = -1;
|
2019-12-15 12:06:09 +01:00
|
|
|
dictionary_t header = (dictionary_t)dvalue(rq->request, "REQUEST_HEADER");
|
2018-10-08 22:30:13 +02:00
|
|
|
tmp = (char *)dvalue(header, "Content-Length");
|
|
|
|
if (tmp)
|
|
|
|
clen = atoi(tmp);
|
|
|
|
if (clen == -1)
|
|
|
|
return;
|
2018-10-08 19:29:53 +02:00
|
|
|
// read data and write to the fd
|
|
|
|
char buf[BUFFLEN];
|
2018-10-08 22:30:13 +02:00
|
|
|
int readlen = clen > BUFFLEN ? BUFFLEN : clen;
|
|
|
|
int read = 0, stat = 1;
|
|
|
|
while (readlen > 0 && stat > 0)
|
|
|
|
{
|
|
|
|
stat = antd_recv(rq->client, buf, readlen);
|
|
|
|
if (stat > 0)
|
|
|
|
{
|
|
|
|
read += stat;
|
|
|
|
readlen = (clen - read) > BUFFLEN ? BUFFLEN : (clen - read);
|
2019-12-20 19:33:36 +01:00
|
|
|
UNUSED(write(fd, buf, stat));
|
2018-10-08 22:30:13 +02:00
|
|
|
}
|
|
|
|
}
|
2018-10-08 19:29:53 +02:00
|
|
|
}
|
2018-10-08 22:30:13 +02:00
|
|
|
static char *get_cgi_bin(antd_request_t *rq)
|
2018-10-08 19:29:53 +02:00
|
|
|
{
|
2018-10-08 22:30:13 +02:00
|
|
|
char *tmp = (char *)dvalue(rq->request, "RESOURCE_PATH");
|
|
|
|
if (!tmp)
|
|
|
|
return NULL;
|
2018-10-08 19:29:53 +02:00
|
|
|
tmp = ext(tmp);
|
2018-10-08 22:30:13 +02:00
|
|
|
if (!tmp)
|
|
|
|
return NULL;
|
|
|
|
char *bin = (char *)dvalue(cgi_bin, tmp);
|
2019-07-31 15:14:51 +02:00
|
|
|
LOG("CGI CMD: %s\n", bin);
|
2018-10-08 19:29:53 +02:00
|
|
|
free(tmp);
|
|
|
|
return bin;
|
|
|
|
}
|
2019-12-15 12:06:09 +01:00
|
|
|
static list_t get_env_vars(antd_request_t *rq)
|
2018-10-08 19:29:53 +02:00
|
|
|
{
|
2018-10-08 22:30:13 +02:00
|
|
|
char *tmp = NULL;
|
|
|
|
char *sub = NULL;
|
2019-12-15 12:06:09 +01:00
|
|
|
dictionary_t request = (dictionary_t)rq->request;
|
|
|
|
dictionary_t header = (dictionary_t)dvalue(rq->request, "REQUEST_HEADER");
|
|
|
|
list_t env_vars = list_init();
|
2018-10-07 23:43:38 +02:00
|
|
|
add_vars(&env_vars, "GATEWAY_INTERFACE", "CGI/1.1");
|
2018-10-08 22:30:13 +02:00
|
|
|
add_vars(&env_vars, "SERVER_SOFTWARE", SERVER_NAME);
|
|
|
|
tmp = (char *)dvalue(request, "REQUEST_QUERY");
|
|
|
|
if (!tmp)
|
2018-10-07 23:43:38 +02:00
|
|
|
add_vars(&env_vars, "QUERY_STRING", "");
|
|
|
|
else
|
|
|
|
{
|
2018-10-08 22:30:13 +02:00
|
|
|
sub = strchr(tmp, '?');
|
|
|
|
if (sub)
|
2018-10-07 23:43:38 +02:00
|
|
|
{
|
|
|
|
sub++;
|
|
|
|
add_vars(&env_vars, "QUERY_STRING", sub);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
add_vars(&env_vars, "QUERY_STRING", "");
|
|
|
|
}
|
2018-10-08 22:30:13 +02:00
|
|
|
tmp = (char *)dvalue(request, "METHOD");
|
|
|
|
if (tmp)
|
2018-10-07 23:43:38 +02:00
|
|
|
add_vars(&env_vars, "REQUEST_METHOD", tmp);
|
2018-10-08 22:30:13 +02:00
|
|
|
tmp = (char *)dvalue(header, "Content-Type");
|
|
|
|
if (tmp)
|
2018-10-07 23:43:38 +02:00
|
|
|
add_vars(&env_vars, "CONTENT_TYPE", tmp);
|
|
|
|
else
|
|
|
|
add_vars(&env_vars, "CONTENT_TYPE", "");
|
2018-10-08 22:30:13 +02:00
|
|
|
tmp = (char *)dvalue(header, "Content-Length");
|
|
|
|
if (tmp)
|
2018-10-07 23:43:38 +02:00
|
|
|
add_vars(&env_vars, "CONTENT_LENGTH", tmp);
|
|
|
|
else
|
|
|
|
add_vars(&env_vars, "CONTENT_LENGTH", "");
|
2019-12-20 19:33:36 +01:00
|
|
|
add_vars(&env_vars, "DOCUMENT_ROOT", rq->client->port_config->htdocs);
|
2018-10-08 22:30:13 +02:00
|
|
|
tmp = (char *)dvalue(request, "REQUEST_PATH");
|
|
|
|
if (tmp)
|
2018-10-07 23:43:38 +02:00
|
|
|
add_vars(&env_vars, "PATH_INFO", tmp);
|
|
|
|
else
|
|
|
|
add_vars(&env_vars, "PATH_INFO", "");
|
2018-10-08 22:30:13 +02:00
|
|
|
tmp = (char *)dvalue(header, "REMOTE_ADDR");
|
2018-10-07 23:43:38 +02:00
|
|
|
add_vars(&env_vars, "REMOTE_ADDR", tmp);
|
|
|
|
add_vars(&env_vars, "REMOTE_HOST", tmp);
|
|
|
|
add_vars(&env_vars, "SERVER_NAME", SERVER_NAME);
|
2018-10-08 22:30:13 +02:00
|
|
|
add_vars(&env_vars, "SERVER_PORT", (char *)dvalue(header, "SERVER_PORT"));
|
2018-10-07 23:43:38 +02:00
|
|
|
add_vars(&env_vars, "SERVER_PROTOCOL", "HTTP/1.1");
|
|
|
|
// add remaining header to the vars
|
2019-12-15 12:06:09 +01:00
|
|
|
chain_t it;
|
2018-10-07 23:43:38 +02:00
|
|
|
for_each_assoc(it, header)
|
|
|
|
{
|
|
|
|
tmp = __s("HTTP_%s", it->key);
|
|
|
|
char *s = tmp;
|
2018-10-08 22:30:13 +02:00
|
|
|
while (*s)
|
|
|
|
{
|
|
|
|
if (*s == '-')
|
2018-10-07 23:43:38 +02:00
|
|
|
*s = '_';
|
2018-10-08 22:30:13 +02:00
|
|
|
else if (*s != '_')
|
|
|
|
*s = toupper((char)*s);
|
2018-10-07 23:43:38 +02:00
|
|
|
s++;
|
|
|
|
}
|
2018-10-08 22:30:13 +02:00
|
|
|
add_vars(&env_vars, tmp, (char *)it->value);
|
2018-10-07 23:43:38 +02:00
|
|
|
free(tmp);
|
|
|
|
}
|
2018-10-08 22:30:13 +02:00
|
|
|
tmp = (char *)dvalue(request, "RESOURCE_PATH");
|
|
|
|
if (tmp)
|
2018-10-07 23:43:38 +02:00
|
|
|
{
|
|
|
|
add_vars(&env_vars, "SCRIPT_NAME", tmp);
|
2019-12-20 19:33:36 +01:00
|
|
|
tmp = __s("%s/%s", rq->client->port_config->htdocs, tmp);
|
2018-10-07 23:43:38 +02:00
|
|
|
add_vars(&env_vars, "SCRIPT_FILENAME", tmp);
|
|
|
|
add_vars(&env_vars, "PATH_TRANSLATED", tmp);
|
|
|
|
free(tmp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
add_vars(&env_vars, "SCRIPT_FILENAME", "");
|
|
|
|
add_vars(&env_vars, "PATH_TRANSLATED", "");
|
|
|
|
add_vars(&env_vars, "SCRIPT_NAME", "");
|
|
|
|
}
|
2018-10-08 19:29:53 +02:00
|
|
|
// redirect status for php
|
2018-10-08 22:30:13 +02:00
|
|
|
add_vars(&env_vars, "REDIRECT_STATUS", "200");
|
2018-10-08 19:29:53 +02:00
|
|
|
return env_vars;
|
|
|
|
}
|
|
|
|
|
2018-10-08 22:30:13 +02:00
|
|
|
void *handle(void *data)
|
2018-10-08 19:29:53 +02:00
|
|
|
{
|
|
|
|
antd_request_t *rq = (antd_request_t *)data;
|
2018-10-08 22:30:13 +02:00
|
|
|
void *cl = (void *)rq->client;
|
|
|
|
pid_t pid = 0;
|
2018-10-08 19:29:53 +02:00
|
|
|
int inpipefd[2];
|
|
|
|
int outpipefd[2];
|
|
|
|
char buf[BUFFLEN];
|
|
|
|
int status;
|
2018-10-08 22:30:13 +02:00
|
|
|
antd_task_t *task = NULL;
|
2019-12-15 12:06:09 +01:00
|
|
|
list_t env_vars = NULL;
|
2018-10-08 22:30:13 +02:00
|
|
|
char *bin = get_cgi_bin(rq);
|
|
|
|
if (!bin)
|
2018-10-08 19:29:53 +02:00
|
|
|
{
|
|
|
|
LOG("No cgi bin found\n");
|
2019-12-15 12:06:09 +01:00
|
|
|
antd_error(cl,503, "Service unavailable");
|
2019-11-13 16:59:44 +01:00
|
|
|
task = antd_create_task(NULL, data, NULL,rq->client->last_io);
|
|
|
|
task->priority++;
|
2018-10-08 19:29:53 +02:00
|
|
|
return task;
|
|
|
|
}
|
|
|
|
env_vars = get_env_vars(rq);
|
2018-10-07 23:43:38 +02:00
|
|
|
// now exec the cgi bin
|
2019-11-13 16:59:44 +01:00
|
|
|
LOG("Execute the cgi bin\n");
|
2019-12-15 12:06:09 +01:00
|
|
|
item_t np = env_vars;
|
2018-10-07 23:43:38 +02:00
|
|
|
int size = list_size(env_vars);
|
2018-10-08 22:30:13 +02:00
|
|
|
char **envs = (char **)malloc((size + 1) * sizeof(*envs));
|
2018-10-07 23:43:38 +02:00
|
|
|
envs[size] = NULL;
|
|
|
|
int i = 0;
|
2018-10-08 22:30:13 +02:00
|
|
|
while (np)
|
|
|
|
{
|
2019-12-22 17:28:16 +01:00
|
|
|
envs[i] = (char*)np->value.ptr;
|
2018-10-07 23:43:38 +02:00
|
|
|
np = np->next;
|
|
|
|
i++;
|
2018-10-08 22:30:13 +02:00
|
|
|
}
|
2018-10-08 19:29:53 +02:00
|
|
|
// PIPE
|
2019-12-20 19:33:36 +01:00
|
|
|
UNUSED(pipe(inpipefd));
|
|
|
|
UNUSED(pipe(outpipefd));
|
2018-10-07 23:43:38 +02:00
|
|
|
pid = fork();
|
|
|
|
if (pid == 0)
|
|
|
|
{
|
|
|
|
// Child
|
|
|
|
dup2(outpipefd[0], STDIN_FILENO);
|
|
|
|
dup2(inpipefd[1], STDOUT_FILENO);
|
|
|
|
dup2(inpipefd[1], STDERR_FILENO);
|
|
|
|
|
|
|
|
//ask kernel to deliver SIGTERM in case the parent dies
|
|
|
|
//prctl(PR_SET_PDEATHSIG, SIGTERM);
|
2018-10-08 22:30:13 +02:00
|
|
|
char *argv[] = {bin, 0};
|
2018-10-08 19:29:53 +02:00
|
|
|
execve(argv[0], &argv[0], envs);
|
2018-10-08 22:30:13 +02:00
|
|
|
// Nothing below this line should be executed by child process. If so,
|
2018-10-07 23:43:38 +02:00
|
|
|
// it means that the execl function wasn't successfull, so lets exit:
|
|
|
|
_exit(1);
|
|
|
|
}
|
2018-10-08 19:29:53 +02:00
|
|
|
// The code below will be executed only by parent.
|
2018-10-07 23:43:38 +02:00
|
|
|
|
|
|
|
//close unused pipe ends
|
|
|
|
close(outpipefd[0]);
|
|
|
|
close(inpipefd[1]);
|
|
|
|
|
2018-10-08 19:29:53 +02:00
|
|
|
// Now, we can write to outpipefd[1] and read from inpipefd[0] :
|
2018-10-08 22:30:13 +02:00
|
|
|
write_request_body(rq, outpipefd[1]);
|
|
|
|
|
2019-12-15 12:06:09 +01:00
|
|
|
const char* stat_str = get_status_str(200);
|
|
|
|
__t(cl, "HTTP/1.1 %d %s", 200, stat_str);
|
|
|
|
//set_status(cl, 200, "OK");
|
2018-10-08 22:30:13 +02:00
|
|
|
//wpid = 0;
|
|
|
|
//waitpid(pid, &status, 0); // wait for the child finish
|
|
|
|
// WNOHANG
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
ssize_t count = read(inpipefd[0], buf, BUFFLEN);
|
|
|
|
if (count == -1)
|
|
|
|
{
|
|
|
|
if (errno == EINTR)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (count == 0)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
antd_send(cl, buf, count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*
|
2018-10-08 19:29:53 +02:00
|
|
|
do {
|
|
|
|
memset(buf, 0, sizeof(buf));
|
|
|
|
int r = read(inpipefd[0], buf, BUFFLEN-1);
|
2018-10-07 23:43:38 +02:00
|
|
|
if(r > 0)
|
|
|
|
{
|
2018-10-08 19:29:53 +02:00
|
|
|
__t(cl, buf);
|
2018-10-07 23:43:38 +02:00
|
|
|
}
|
2018-10-08 19:29:53 +02:00
|
|
|
} while(wpid == 0);
|
2018-10-08 22:30:13 +02:00
|
|
|
*/
|
|
|
|
kill(pid, SIGKILL);
|
|
|
|
waitpid(pid, &status, 0);
|
2018-10-07 23:43:38 +02:00
|
|
|
free(envs);
|
|
|
|
list_free(&env_vars);
|
2019-11-13 16:59:44 +01:00
|
|
|
task = antd_create_task(NULL, data, NULL,rq->client->last_io);
|
|
|
|
task->priority++;
|
2018-10-07 23:43:38 +02:00
|
|
|
return task;
|
2019-11-13 16:59:44 +01:00
|
|
|
}
|