2013-05-01 17:17:16 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2013 Joris Vink <joris@coders.se>
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <fcntl.h>
|
2013-06-26 11:18:32 +02:00
|
|
|
#include <pwd.h>
|
2013-05-01 17:17:16 +02:00
|
|
|
|
|
|
|
#include "kore.h"
|
|
|
|
|
2013-06-04 16:53:30 +02:00
|
|
|
static int configure_bind(char **);
|
|
|
|
static int configure_load(char **);
|
|
|
|
static int configure_onload(char **);
|
|
|
|
static int configure_handler(char **);
|
|
|
|
static int configure_domain(char **);
|
|
|
|
static int configure_chroot(char **);
|
|
|
|
static int configure_runas(char **);
|
|
|
|
static int configure_workers(char **);
|
|
|
|
static int configure_pidfile(char **);
|
2013-06-24 11:32:45 +02:00
|
|
|
static int configure_accesslog(char **);
|
2013-06-05 09:47:08 +02:00
|
|
|
static int configure_certfile(char **);
|
|
|
|
static int configure_certkey(char **);
|
2013-06-26 16:37:22 +02:00
|
|
|
static int configure_max_connections(char **);
|
2013-06-27 08:43:07 +02:00
|
|
|
static int configure_ssl_cipher(char **);
|
2013-06-24 11:32:45 +02:00
|
|
|
static void domain_sslstart(void);
|
2013-05-01 17:17:16 +02:00
|
|
|
|
|
|
|
static struct {
|
|
|
|
const char *name;
|
|
|
|
int (*configure)(char **);
|
|
|
|
} config_names[] = {
|
2013-06-26 16:37:22 +02:00
|
|
|
{ "bind", configure_bind },
|
|
|
|
{ "load", configure_load },
|
|
|
|
{ "onload", configure_onload },
|
|
|
|
{ "static", configure_handler },
|
|
|
|
{ "dynamic", configure_handler },
|
2013-06-27 08:43:07 +02:00
|
|
|
{ "ssl_cipher", configure_ssl_cipher },
|
2013-06-26 16:37:22 +02:00
|
|
|
{ "domain", configure_domain },
|
|
|
|
{ "chroot", configure_chroot },
|
|
|
|
{ "runas", configure_runas },
|
|
|
|
{ "workers", configure_workers },
|
|
|
|
{ "worker_max_connections", configure_max_connections },
|
|
|
|
{ "pidfile", configure_pidfile },
|
|
|
|
{ "accesslog", configure_accesslog },
|
|
|
|
{ "certfile", configure_certfile },
|
|
|
|
{ "certkey", configure_certkey },
|
|
|
|
{ NULL, NULL },
|
2013-05-01 17:17:16 +02:00
|
|
|
};
|
|
|
|
|
2013-06-26 11:18:32 +02:00
|
|
|
char *config_file = NULL;
|
2013-06-24 11:32:45 +02:00
|
|
|
static struct kore_domain *current_domain = NULL;
|
2013-05-02 13:30:13 +02:00
|
|
|
|
2013-05-01 17:17:16 +02:00
|
|
|
void
|
2013-06-26 11:18:32 +02:00
|
|
|
kore_parse_config(void)
|
2013-05-01 17:17:16 +02:00
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
int i, lineno;
|
2013-05-01 20:10:45 +02:00
|
|
|
char buf[BUFSIZ], *p, *t, *argv[5];
|
2013-05-01 17:17:16 +02:00
|
|
|
|
2013-06-26 11:18:32 +02:00
|
|
|
if (config_file == NULL)
|
|
|
|
fatal("specify a configuration file with -c");
|
|
|
|
|
|
|
|
if ((fp = fopen(config_file, "r")) == NULL)
|
|
|
|
fatal("configuration given cannot be opened: %s", config_file);
|
2013-05-01 17:17:16 +02:00
|
|
|
|
|
|
|
lineno = 1;
|
|
|
|
while (fgets(buf, sizeof(buf), fp) != NULL) {
|
|
|
|
p = buf;
|
|
|
|
buf[strcspn(buf, "\n")] = '\0';
|
|
|
|
|
|
|
|
while (isspace(*p))
|
|
|
|
p++;
|
|
|
|
if (p[0] == '#' || p[0] == '\0') {
|
|
|
|
lineno++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (t = p; *t != '\0'; t++) {
|
|
|
|
if (*t == '\t')
|
|
|
|
*t = ' ';
|
|
|
|
}
|
|
|
|
|
2013-06-24 11:32:45 +02:00
|
|
|
if (!strcmp(p, "}") && current_domain != NULL)
|
|
|
|
domain_sslstart();
|
|
|
|
|
2013-05-01 20:10:45 +02:00
|
|
|
kore_split_string(p, " ", argv, 5);
|
2013-05-01 17:17:16 +02:00
|
|
|
for (i = 0; config_names[i].name != NULL; i++) {
|
|
|
|
if (!strcmp(config_names[i].name, argv[0])) {
|
|
|
|
if (!config_names[i].configure(argv)) {
|
|
|
|
fatal("configuration error on line %d",
|
|
|
|
lineno);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lineno++;
|
|
|
|
}
|
2013-06-26 11:18:32 +02:00
|
|
|
|
|
|
|
if (!kore_module_loaded())
|
|
|
|
fatal("no site module was loaded");
|
|
|
|
|
|
|
|
if (server_ip == NULL || server_port == 0)
|
|
|
|
fatal("missing a correct bind directive in configuration");
|
|
|
|
if (chroot_path == NULL)
|
|
|
|
fatal("missing a chroot path");
|
|
|
|
if (runas_user == NULL)
|
|
|
|
fatal("missing a username to run as");
|
|
|
|
if ((pw = getpwnam(runas_user)) == NULL)
|
|
|
|
fatal("user '%s' does not exist", runas_user);
|
2013-05-01 17:17:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
configure_bind(char **argv)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (argv[1] == NULL || argv[2] == NULL)
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
if (server_ip != NULL || server_port != 0) {
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug("duplicate bind directive seen");
|
2013-05-01 17:17:16 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
server_ip = kore_strdup(argv[1]);
|
|
|
|
server_port = kore_strtonum(argv[2], 1, 65535, &err);
|
|
|
|
if (err != KORE_RESULT_OK) {
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug("%s is an invalid port number", argv[2]);
|
2013-05-01 17:17:16 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
configure_load(char **argv)
|
|
|
|
{
|
|
|
|
if (argv[1] == NULL)
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
|
|
|
kore_module_load(argv[1]);
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
2013-05-30 21:26:39 +02:00
|
|
|
static int
|
|
|
|
configure_onload(char **argv)
|
|
|
|
{
|
|
|
|
if (argv[1] == NULL)
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
|
|
|
if (kore_module_onload != NULL) {
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug("duplicate onload directive found");
|
2013-05-30 21:26:39 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
kore_module_onload = kore_strdup(argv[1]);
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
2013-06-27 08:43:07 +02:00
|
|
|
static int
|
|
|
|
configure_ssl_cipher(char **argv)
|
|
|
|
{
|
|
|
|
if (argv[1] == NULL)
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
|
|
|
if (strcmp(kore_ssl_cipher_list, KORE_DEFAULT_CIPHER_LIST)) {
|
|
|
|
kore_debug("duplicate ssl_cipher directive specified");
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
kore_ssl_cipher_list = kore_strdup(argv[1]);
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
2013-05-02 13:30:13 +02:00
|
|
|
static int
|
|
|
|
configure_domain(char **argv)
|
|
|
|
{
|
2013-06-24 11:32:45 +02:00
|
|
|
if (argv[2] == NULL)
|
2013-05-02 13:30:13 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
2013-06-24 11:32:45 +02:00
|
|
|
if (current_domain != NULL) {
|
|
|
|
kore_debug("previous domain configuration not closed");
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strcmp(argv[2], "{")) {
|
|
|
|
kore_debug("missing { for domain directive");
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!kore_domain_new(argv[1])) {
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug("could not create new domain %s", current_domain);
|
2013-05-29 14:29:46 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
2013-05-02 13:30:13 +02:00
|
|
|
|
2013-06-24 11:32:45 +02:00
|
|
|
current_domain = kore_domain_lookup(argv[1]);
|
2013-05-02 13:30:13 +02:00
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
2013-05-01 17:17:16 +02:00
|
|
|
static int
|
|
|
|
configure_handler(char **argv)
|
|
|
|
{
|
|
|
|
int type;
|
|
|
|
|
2013-05-02 13:30:13 +02:00
|
|
|
if (current_domain == NULL) {
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug("missing domain for page handler");
|
2013-05-02 13:30:13 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
2013-05-01 17:17:16 +02:00
|
|
|
if (argv[1] == NULL || argv[2] == NULL)
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
|
|
|
if (!strcmp(argv[0], "static"))
|
|
|
|
type = HANDLER_TYPE_STATIC;
|
|
|
|
else if (!strcmp(argv[0], "dynamic"))
|
|
|
|
type = HANDLER_TYPE_DYNAMIC;
|
|
|
|
else
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
2013-06-24 11:32:45 +02:00
|
|
|
if (!kore_module_handler_new(argv[1],
|
|
|
|
current_domain->domain, argv[2], type)) {
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug("cannot create handler for %s", argv[1]);
|
2013-05-01 17:17:16 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
2013-05-04 22:18:27 +02:00
|
|
|
|
|
|
|
static int
|
|
|
|
configure_chroot(char **argv)
|
|
|
|
{
|
|
|
|
if (chroot_path != NULL) {
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug("duplicate chroot path specified");
|
2013-05-04 22:18:27 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argv[1] == NULL)
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
|
|
|
chroot_path = kore_strdup(argv[1]);
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
configure_runas(char **argv)
|
|
|
|
{
|
|
|
|
if (runas_user != NULL) {
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug("duplicate runas user specified");
|
2013-05-04 22:18:27 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argv[1] == NULL)
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
|
|
|
runas_user = kore_strdup(argv[1]);
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
2013-05-30 19:36:42 +02:00
|
|
|
|
|
|
|
static int
|
|
|
|
configure_workers(char **argv)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (worker_count != 0) {
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug("duplicate worker directive specified");
|
2013-05-30 19:36:42 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (argv[1] == NULL)
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
|
|
|
worker_count = kore_strtonum(argv[1], 1, 255, &err);
|
|
|
|
if (err != KORE_RESULT_OK) {
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug("%s is not a correct worker number", argv[1]);
|
2013-05-30 19:36:42 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
2013-06-04 16:53:30 +02:00
|
|
|
|
|
|
|
static int
|
|
|
|
configure_pidfile(char **argv)
|
|
|
|
{
|
|
|
|
if (argv[1] == NULL)
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
|
|
|
if (strcmp(kore_pidfile, KORE_PIDFILE_DEFAULT)) {
|
|
|
|
kore_debug("duplicate pidfile directive specified");
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
kore_pidfile = kore_strdup(argv[1]);
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
2013-06-05 09:47:08 +02:00
|
|
|
|
|
|
|
static int
|
2013-06-24 11:32:45 +02:00
|
|
|
configure_accesslog(char **argv)
|
2013-06-05 09:47:08 +02:00
|
|
|
{
|
|
|
|
if (argv[1] == NULL)
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
2013-06-24 11:32:45 +02:00
|
|
|
if (current_domain == NULL) {
|
|
|
|
kore_debug("missing domain for accesslog");
|
2013-06-05 09:47:08 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
2013-06-24 11:32:45 +02:00
|
|
|
if (current_domain->accesslog != -1) {
|
|
|
|
kore_debug("domain %s already has an open accesslog",
|
|
|
|
current_domain->domain);
|
2013-06-05 09:47:08 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
2013-06-24 11:32:45 +02:00
|
|
|
}
|
2013-06-05 09:47:08 +02:00
|
|
|
|
2013-06-24 11:32:45 +02:00
|
|
|
current_domain->accesslog = open(argv[1],
|
|
|
|
O_CREAT | O_APPEND | O_WRONLY, 0755);
|
|
|
|
if (current_domain->accesslog == -1) {
|
|
|
|
kore_debug("open(%s): %s", argv[1], errno_s);
|
2013-06-05 09:47:08 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
2013-06-24 09:36:40 +02:00
|
|
|
static int
|
2013-06-24 11:32:45 +02:00
|
|
|
configure_certfile(char **argv)
|
2013-06-24 09:36:40 +02:00
|
|
|
{
|
|
|
|
if (argv[1] == NULL)
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
|
|
|
if (current_domain == NULL) {
|
2013-06-24 11:32:45 +02:00
|
|
|
kore_debug("missing domain for certfile");
|
2013-06-24 09:36:40 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
2013-06-24 11:32:45 +02:00
|
|
|
if (current_domain->certfile != NULL) {
|
|
|
|
kore_debug("domain already has a certfile set");
|
2013-06-24 09:36:40 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
2013-06-24 11:32:45 +02:00
|
|
|
current_domain->certfile = kore_strdup(argv[1]);
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
configure_certkey(char **argv)
|
|
|
|
{
|
|
|
|
if (argv[1] == NULL)
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
|
|
|
if (current_domain == NULL) {
|
|
|
|
kore_debug("missing domain for certkey");
|
2013-06-24 09:36:40 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
2013-06-24 11:32:45 +02:00
|
|
|
if (current_domain->certkey != NULL) {
|
|
|
|
kore_debug("domain already has a certkey set");
|
2013-06-24 09:36:40 +02:00
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
2013-06-24 11:32:45 +02:00
|
|
|
current_domain->certkey = kore_strdup(argv[1]);
|
2013-06-24 09:36:40 +02:00
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
2013-06-24 11:32:45 +02:00
|
|
|
|
2013-06-26 16:37:22 +02:00
|
|
|
static int
|
|
|
|
configure_max_connections(char **argv)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (argv[1] == NULL)
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
|
|
|
worker_max_connections = kore_strtonum(argv[1], 1, 65535, &err);
|
|
|
|
if (err != KORE_RESULT_OK)
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
2013-06-24 11:32:45 +02:00
|
|
|
static void
|
|
|
|
domain_sslstart(void)
|
|
|
|
{
|
|
|
|
kore_domain_sslstart(current_domain);
|
|
|
|
current_domain = NULL;
|
|
|
|
}
|