Exemplo n.º 1
0
/* Terminate gracefully on SIGTERM */
void
sigterm_cb(int fd, short event, void * arg) {
    evbase_t     * evbase = (evbase_t *)arg;
    struct timeval tv     = { .tv_usec = 100000, .tv_sec = 0 }; /* 100 ms */

    event_base_loopexit(evbase, &tv);
}

void
init_thread_cb(evhtp_t * htp, evthr_t * thr, void * arg) {
    static int aux = 0;

    printf("Spinning up a thread: %d\n", ++aux);
    evthr_set_aux(thr, &aux);
}

int
main(int argc, char ** argv) {
    struct event *ev_sigterm;
    evbase_t    * evbase  = event_base_new();
    evhtp_t     * evhtp   = evhtp_new(evbase, NULL);

    evhtp_set_gencb(evhtp, frontend_cb, NULL);

#ifndef EVHTP_DISABLE_SSL
    evhtp_ssl_cfg_t scfg1 = { 0 };

    scfg1.pemfile  = "./server.pem";
    scfg1.privfile = "./server.pem";

    evhtp_ssl_init(evhtp, &scfg1);
#endif

    evhtp_use_threads(evhtp, init_thread_cb, 8, NULL);
#ifndef WIN32
    ev_sigterm = evsignal_new(evbase, SIGTERM, sigterm_cb, evbase);
    evsignal_add(ev_sigterm, NULL);
#endif
    evhtp_bind_socket(evhtp, "0.0.0.0", 8081, 1024);
    event_base_loop(evbase, 0);

    printf("Clean exit\n");
    return 0;
}
Exemplo n.º 2
0
int
main(int argc, char ** argv) {
    struct event     * ev_sigint;
    evbase_t         * evbase = NULL;
    evhtp_t          * htp    = NULL;
    evhtp_callback_t * cb_1   = NULL;
    evhtp_callback_t * cb_2   = NULL;
    evhtp_callback_t * cb_3   = NULL;
    evhtp_callback_t * cb_4   = NULL;
    evhtp_callback_t * cb_5   = NULL;

#ifndef EVHTP_DISABLE_REGEX
    evhtp_callback_t * cb_6   = NULL;
#endif
    evhtp_callback_t * cb_7   = NULL;
#ifndef EVHTP_DISABLE_REGEX
    evhtp_callback_t * cb_8   = NULL;
#endif
    evhtp_callback_t * cb_9   = NULL;
    evhtp_callback_t * cb_10  = NULL;
    evhtp_callback_t * cb_11  = NULL;
    evhtp_callback_t * cb_12  = NULL;

    if (parse_args(argc, argv) < 0) {
        exit(1);
    }

    srand((unsigned)time(NULL));

    evbase = event_base_new();
    htp    = evhtp_new(evbase, NULL);

    evhtp_set_parser_flags(htp, EVHTP_PARSE_QUERY_FLAG_LENIENT);
    evhtp_set_max_keepalive_requests(htp, max_keepalives);

    //htp->enable_nodelay = 1;
    //htp->enable_defer_accept = 1;
    htp->enable_reuseport = 1;

    cb_1   = evhtp_set_cb(htp, "/ref", test_default_cb, "fjdkls");
    cb_2   = evhtp_set_cb(htp, "/foo", test_foo_cb, "bar");
    cb_3   = evhtp_set_cb(htp, "/foo/", test_foo_cb, "bar");
    cb_4   = evhtp_set_cb(htp, "/bar", test_bar_cb, "baz");
    cb_5   = evhtp_set_cb(htp, "/500", test_500_cb, "500");
#ifndef EVHTP_DISABLE_REGEX
    cb_6   = evhtp_set_regex_cb(htp, "^(/anything/).*", test_regex, NULL);
#endif
    cb_7   = evhtp_set_cb(htp, "/pause", test_pause_cb, NULL);
#ifndef EVHTP_DISABLE_REGEX
    cb_8   = evhtp_set_regex_cb(htp, "^/create/(.*)", create_callback, NULL);
#endif
    cb_9   = evhtp_set_glob_cb(htp, "*/glob/*", test_glob_cb, NULL);
    cb_10  = evhtp_set_cb(htp, "/max_body_size", test_max_body, NULL);

    /* set a callback to test out chunking API */
    cb_11  = evhtp_set_cb(htp, "/chunkme", test_chunking, NULL);

    /* set a callback which takes ownership of the underlying bufferevent and
     * just starts echoing things
     */
    cb_12  = evhtp_set_cb(htp, "/ownme", test_ownership, NULL);

    /* set a callback to pause on each header for cb_7 */
    evhtp_set_hook(&cb_7->hooks, evhtp_hook_on_path, pause_init_cb, NULL);

    /* set a callback to set hooks specifically for the cb_6 callback */
#ifndef EVHTP_DISABLE_REGEX
    evhtp_set_hook(&cb_6->hooks, evhtp_hook_on_headers, test_regex_hdrs_cb, NULL);
#endif

    evhtp_set_hook(&cb_10->hooks, evhtp_hook_on_headers, set_max_body, NULL);

    /* set a default request handler */
    evhtp_set_gencb(htp, test_default_cb, "foobarbaz");

    /* set a callback invoked before a connection is accepted */
    //evhtp_set_pre_accept_cb(htp, test_pre_accept, &bind_port);

    /* set a callback to set per-connection hooks (via a post_accept cb) */
    evhtp_set_post_accept_cb(htp, set_my_connection_handlers, NULL);

#ifndef EVHTP_DISABLE_SSL
    if (ssl_pem != NULL) {
        evhtp_ssl_cfg_t scfg = {
            .pemfile            = ssl_pem,
            .privfile           = ssl_pem,
            .cafile             = ssl_ca,
            .capath             = ssl_capath,
            .ciphers            = "RC4+RSA:HIGH:+MEDIUM:+LOW",
            .ssl_opts           = SSL_OP_NO_SSLv2,
            .ssl_ctx_timeout    = 60 * 60 * 48,
            .verify_peer        = SSL_VERIFY_PEER,
            .verify_depth       = 42,
            .x509_verify_cb     = dummy_ssl_verify_callback,
            .x509_chk_issued_cb = dummy_check_issued_cb,
            .scache_type        = evhtp_ssl_scache_type_internal,
            .scache_size        = 1024,
            .scache_timeout     = 1024,
            .scache_init        = NULL,
            .scache_add         = NULL,
            .scache_get         = NULL,
            .scache_del         = NULL,
        };

        evhtp_ssl_init(htp, &scfg);
#ifndef EVHTP_DISABLE_EVTHR
        if (use_threads) {
            #define OPENSSL_THREAD_DEFINES
#include <openssl/opensslconf.h>
#if defined(OPENSSL_THREADS)
#else
            fprintf(stderr, "Your version of OpenSSL does not support threading!\n");
            exit(-1);
#endif
        }
#endif
    }
#endif

#ifndef EVHTP_DISABLE_EVTHR
    if (use_threads) {
        evhtp_use_threads(htp, NULL, num_threads, NULL);
    }
#endif

    if (evhtp_bind_socket(htp, bind_addr, bind_port, backlog) < 0) {
        fprintf(stderr, "Could not bind socket: %s\n", strerror(errno));
        exit(-1);
    }

    ev_sigint = evsignal_new(evbase, SIGINT, sigint, evbase);
    evsignal_add(ev_sigint, NULL);

    event_base_loop(evbase, 0);

    event_free(ev_sigint);
    evhtp_unbind_socket(htp);

    evhtp_free(htp);
    event_base_free(evbase);

    return 0;
} /* main */
Exemplo n.º 3
0
Arquivo: test.c Projeto: pibi/libevhtp
int
main(int argc, char ** argv) {
    evbase_t         * evbase = NULL;
    evhtp_t          * htp    = NULL;
    evhtp_callback_t * cb_1   = NULL;
    evhtp_callback_t * cb_2   = NULL;
    evhtp_callback_t * cb_3   = NULL;
    evhtp_callback_t * cb_4   = NULL;
    evhtp_callback_t * cb_5   = NULL;
    evhtp_callback_t * cb_6   = NULL;
    evhtp_callback_t * cb_7   = NULL;
    evhtp_callback_t * cb_8   = NULL;

    if (parse_args(argc, argv) < 0) {
        exit(1);
    }

    srand((unsigned)time(NULL));

    evbase = event_base_new();
    htp    = evhtp_new(evbase, NULL);

    cb_1   = evhtp_set_cb(htp, "/ref", test_default_cb, "fjdkls");
    cb_2   = evhtp_set_cb(htp, "/foo", test_foo_cb, "bar");
    cb_3   = evhtp_set_cb(htp, "/foo/", test_foo_cb, "bar");
    cb_4   = evhtp_set_cb(htp, "/bar", test_bar_cb, "baz");
    cb_5   = evhtp_set_cb(htp, "/500", test_500_cb, "500");
    cb_6   = evhtp_set_regex_cb(htp, "^(/anything/).*", test_regex, NULL);
    cb_7   = evhtp_set_cb(htp, "/pause", test_pause_cb, NULL);
    cb_8   = evhtp_set_regex_cb(htp, "^/create/(.*)", create_callback, NULL);

    /* set a callback to pause on each header for cb_7 */
    evhtp_set_hook(&cb_7->hooks, evhtp_hook_on_path, pause_init_cb, NULL);

    /* set a callback to set hooks specifically for the cb_6 callback */
    evhtp_set_hook(&cb_6->hooks, evhtp_hook_on_headers, test_regex_hdrs_cb, NULL);

    /* set a default request handler */
    evhtp_set_gencb(htp, test_default_cb, "foobarbaz");

    /* set a callback invoked before a connection is accepted */
    evhtp_set_pre_accept_cb(htp, test_pre_accept, &bind_port);

    /* set a callback to set per-connection hooks (via a post_accept cb) */
    evhtp_set_post_accept_cb(htp, set_my_connection_handlers, NULL);

#ifndef DISABLE_SSL
    if (ssl_pem != NULL) {
        evhtp_ssl_cfg_t scfg = {
            .pemfile            = ssl_pem,
            .privfile           = ssl_pem,
            .cafile             = ssl_ca,
            .capath             = ssl_capath,
            .ciphers            = "RC4+RSA:HIGH:+MEDIUM:+LOW",
            .ssl_opts           = SSL_OP_NO_SSLv2,
            .verify_peer        = SSL_VERIFY_PEER,
            .verify_depth       = 42,
            .x509_verify_cb     = dummy_ssl_verify_callback,
            .x509_chk_issued_cb = dummy_check_issued_cb,
            .scache_type        = evhtp_ssl_scache_type_internal,
            .scache_size        = 1024,
            .scache_timeout     = 1024,
            .scache_init        = NULL,
            .scache_add         = NULL,
            .scache_get         = NULL,
            .scache_del         = NULL,
        };

        evhtp_ssl_init(htp, &scfg);

        if (use_threads) {
            #define OPENSSL_THREAD_DEFINES
#include <openssl/opensslconf.h>
#if defined(OPENSSL_THREADS)
#else
            fprintf(stderr, "Your version of OpenSSL does not support threading!\n");
            exit(-1);
#endif
        }
    }
#endif

#ifndef DISABLE_EVTHR
    if (use_threads) {
        evhtp_use_threads(htp, NULL, num_threads, NULL);
    }
#endif

    if (evhtp_bind_socket(htp, bind_addr, bind_port, 128) < 0) {
        fprintf(stderr, "Could not bind socket: %s\n", strerror(errno));
        exit(-1);
    }

    signal(SIGINT, sigint);

    event_base_loop(evbase, 0);
    return 0;
} /* main */
Exemplo n.º 4
0
void wshtp_ssl_init(wshtp_server_t *server, evhtp_ssl_cfg_t *scfg) {
    evhtp_ssl_use_threads();
    evhtp_ssl_init(server->htp, scfg);
}
/* Functions */
int
genauthz_httprest_init(evbase_t * evbase, struct app_parent *app_p) {
    struct passwd *pwd = NULL;
    struct tq_listener_s *p_listener, *tmp_p_listener;
    struct tq_service_s *p_service, *tmp_p_service;

    if (evbase == NULL || app_p == NULL)
        return GA_BAD;

    /* Reset Application parent counter */
    app_p->total_call_count = 0;
    app_p->threads_online   = 0;

    for (p_listener = TAILQ_FIRST(&(app_p->listener_head));
         p_listener != NULL;
         tmp_p_listener = TAILQ_NEXT(p_listener, next),
                p_listener = tmp_p_listener) {

        p_listener->listener_call_count = 0; /* Reset counter */

        p_listener->evhtp = evhtp_new(evbase, NULL);
        if (p_listener->evhtp == NULL) {
            syslog(LOG_ERR, "Failed on evhtp_new()");
            goto cleanup;
        }

        /* Map the Application parent object to each listener */
        p_listener->app_parent = app_p;

        /* Setup security context */
        if (p_listener->scfg) {
            evhtp_ssl_init(p_listener->evhtp, p_listener->scfg);
        }

        /* Bind */
        if (evhtp_bind_socket(p_listener->evhtp, p_listener->bindip,
                              p_listener->port, p_listener->backlog) != 0) {
            syslog(LOG_ERR, "Failed to bind a listener to \"%s\" on port \'%d\'",
                            p_listener->bindip, p_listener->port);
            goto cleanup;
        }

        syslog(LOG_INFO, "Listening on \"%s\" on port \'%d\'%s",
                        p_listener->bindip, p_listener->port,
                        p_listener->scfg ? " with SSL." : "");

#if 0
        /* Register thread handler */
        evhtp_use_threads(p_listener->evhtp,
                          app_init_thread,
                          p_listener->thread_cnt,
                          p_listener);
#endif

        for (p_service = TAILQ_FIRST(&(p_listener->services_head));
             p_service != NULL;
             tmp_p_service = TAILQ_NEXT(p_service, next),
                    p_service = tmp_p_service) {

            syslog(LOG_INFO, "URI: \"%s\"", p_service->uri);

            /* Register thread handler */
            evhtp_use_threads(p_listener->evhtp,
                              app_init_thread,
                              p_service->thread_cnt,
                              p_service);

            /* Reset service counter */
            p_service->uri_call_count = 0;

            /* Service type switcher */
            switch(p_service->ltype) {
                case PAP:
                    if (evhtp_set_cb(p_listener->evhtp,
                                     p_service->uri,
                                     pap_cb,
                                     p_service) == NULL) {
                        syslog(LOG_ERR, "Failed to set the PAP callback for the URI \"%s\"", p_service->uri);
                        goto cleanup;
                    } else {
                        syslog(LOG_INFO, "Set the \"PAP\" callback on the URI \"%s\" with \'%d\' threads",
                                         p_service->uri, p_service->thread_cnt);
                    }
                    break;
                case PDP:
                    if (evhtp_set_cb(p_listener->evhtp,
                                     p_service->uri,
                                     pdp_cb,
                                     p_service) == NULL) {
                        syslog(LOG_ERR, "Failed to set the PDP callback for the URI \"%s\"", p_service->uri);
                        goto cleanup;
                    } else {
                        syslog(LOG_INFO, "Set the \"PDP\" callback on the URI \"%s\" with \'%d\' threads",
                                         p_service->uri, p_service->thread_cnt);
                    }
                    break;
                case CONTROL:
                    if (evhtp_set_cb(p_listener->evhtp,
                                     p_service->uri,
                                     control_cb,
                                     p_service) == NULL) {
                        syslog(LOG_ERR, "Failed to set the CONTROL callback for the URI \"%s\"", p_service->uri);
                        goto cleanup;
                    } else {
                        syslog(LOG_INFO, "Set the \"CONTROL\" callback on the URI \"%s\" with \'%d\' threads",
                                         p_service->uri, p_service->thread_cnt);
                    }
                    break;
                default:
                    if (evhtp_set_cb(p_listener->evhtp,
                                     p_service->uri,
                                     generic_http_cb,
                                     p_service) == NULL) {
                        syslog(LOG_ERR, "Failed to set the generic callback for the URI \"%s\"", p_service->uri);
                        goto cleanup;
                    } else {
                        syslog(LOG_INFO, "Set the \"generic_http_cb()\" callback on the URI \"%s\" with \'%d\' threads",
                                         p_service->uri, p_service->thread_cnt);
                    }
                    break;
            }
        }
    }

    /* Privilege downgrade */
    if (geteuid() == 0) {
        /* Test for effective seteuid()-only, like sudo, switch to caller ID */
        if (getuid() != 0) {
            if (getegid() == 0) {
                if (setegid(getgid()) < 0) {
                    return GA_BAD;
                }
            }
            if (seteuid(getuid()) < 0) {
                return GA_BAD;
            }
        } else {
            /* Lower privs after bind to 'okoeroo' or 'nobody' */
            pwd = getpwnam("nobody");
            if (pwd == NULL) {
                return GA_BAD;
            }
            if (getegid() == 0) {
                if (setegid(pwd->pw_gid) < 0)
                    return GA_BAD;
                if (setgid(pwd->pw_gid) < 0)
                    return GA_BAD;
            }
            if (setuid(pwd->pw_uid) < 0)
                return GA_BAD;
            if (seteuid(pwd->pw_uid) < 0)
                return GA_BAD;
        }
    }
    syslog(LOG_INFO, "Running as uid: %d, euid: %d, gid: %d, egid: %d", getuid(), geteuid(), getgid(), geteuid());

    return GA_GOOD;
cleanup:
    return GA_BAD;
}
Exemplo n.º 6
0
int
main(int argc, char *argv[])
{
    evbase_t *evbase = NULL;
    evhtp_t *htp = NULL;
    int daemon_mode = 1;
    int c;
    char *logfile = NULL;
    char *ccnet_debug_level_str = "info";
    char *http_debug_level_str = "debug";
    const char *debug_str = NULL;

    config_dir = DEFAULT_CONFIG_DIR;

    while ((c = getopt_long(argc, argv,
                short_opts, long_opts, NULL)) != EOF) {
        switch (c) {
        case 'h':
            usage();
            exit(0);
        case 'v':
            exit(-1);
            break;
        case 'c':
            config_dir = strdup(optarg);
            break;
        case 'd':
            seafile_dir = strdup(optarg);
            break;
        case 't':
            num_threads = atoi(optarg);
            break;
        case 'f':
            daemon_mode = 0;
            break;
        case 'l':
            logfile = g_strdup(optarg);
            break;
        case 'g':
            ccnet_debug_level_str = optarg;
            break;
        case 'G':
            http_debug_level_str = optarg;
            break;
        case 'D':
            debug_str = optarg;
            break;
        default:
            usage();
            exit(-1);
        }
    }

#ifndef WIN32
    if (daemon_mode)
        daemon(1, 0);
#endif

    g_type_init();

    ccnet_client = ccnet_client_new();
    if ((ccnet_client_load_confdir(ccnet_client, config_dir)) < 0) {
        g_warning ("Read config dir error\n");
        return -1;
    }

    if (seafile_dir == NULL)
        seafile_dir = g_build_filename (config_dir, "seafile-data", NULL);
    if (logfile == NULL)
        logfile = g_build_filename (seafile_dir, "http.log", NULL);

    seaf = seafile_session_new (seafile_dir, ccnet_client);
    if (!seaf) {
        g_warning ("Failed to create seafile session.\n");
        exit (1);
    }
    if (seafile_session_init(seaf) < 0)
        exit (1);

    seaf->client_pool = ccnet_client_pool_new (config_dir);

    if (!debug_str)
        debug_str = g_getenv("SEAFILE_DEBUG");
    seafile_debug_set_flags_string (debug_str);

    if (seafile_log_init (logfile, ccnet_debug_level_str,
                          http_debug_level_str) < 0) {
        g_warning ("Failed to init log.\n");
        exit (1);
    }

    load_httpserver_config (seaf);
    if (use_https) {
        seaf_message ("port = %d, https = true, pemfile = %s, privkey = %s\n",
                      bind_port, pemfile, privkey);
    } else {
        seaf_message ("port = %d, https = false\n", bind_port);
    }

    evbase = event_base_new();
    htp = evhtp_new(evbase, NULL);

    if (pemfile != NULL) {
        evhtp_ssl_cfg_t scfg;

        memset (&scfg, 0, sizeof(scfg));

        scfg.pemfile        = pemfile;
        scfg.privfile       = privkey;
        scfg.scache_type    = evhtp_ssl_scache_type_internal;
        scfg.scache_timeout = 5000;

        evhtp_ssl_init (htp, &scfg);
    }

    if (access_file_init (htp) < 0)
        exit (1);

    if (upload_file_init (htp) < 0)
        exit (1);

    evhtp_set_gencb(htp, default_cb, NULL);

    evhtp_use_threads(htp, NULL, num_threads, NULL);

    if (evhtp_bind_socket(htp, bind_addr, bind_port, 128) < 0) {
        g_warning ("Could not bind socket: %s\n", strerror(errno));
        exit(-1);
    }

    event_base_loop(evbase, 0);

    return 0;
}
Exemplo n.º 7
0
int main(int argc, char **argv) {

  init_config(argc, argv);

  open_authorizations("r");

  init_webfinger();

  /** OPEN MAGIC DATABASE **/

  magic_cookie = magic_open(MAGIC_MIME);
  if(magic_load(magic_cookie, RS_MAGIC_DATABASE) != 0) {
    log_error("Failed to load magic database: %s", magic_error(magic_cookie));
    exit(EXIT_FAILURE);
  }

  log_info("starting process: main");

  if(prctl(PR_SET_NAME, "rs-serve [main]", 0, 0, 0) != 0) {
    log_error("Failed to set process name: %s", strerror(errno));
  }

  /** SETUP EVENT BASE **/

  rs_event_base = event_base_new();
  ASSERT_NOT_NULL(rs_event_base, "event_base_new()");
  log_debug("libevent method: %s", event_base_get_method(rs_event_base));
  event_set_log_callback(log_event_base_message);

  // TODO: add error cb to base

  /** SETUP AUTH TOKENS HASH TABLE **/

  auth_sessions = sm_new(1024); // FIXME: this a hardcoded value.

  /** SETUP MAIN LISTENER **/

  struct sockaddr_in sin;
  memset(&sin, 0, sizeof(struct sockaddr_in));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = htonl(0);
  sin.sin_port = htons(RS_PORT);

  evhtp_t *server = evhtp_new(rs_event_base, NULL);

  if(RS_USE_SSL) {
    evhtp_ssl_cfg_t ssl_config = {
      .pemfile = RS_SSL_CERT_PATH,
      .privfile = RS_SSL_KEY_PATH,
      .cafile = RS_SSL_CA_PATH,
      // what's this???
      .capath = NULL,
      .ciphers = "RC4+RSA:HIGH:+MEDIUM:+LOW",
      .ssl_opts = SSL_OP_NO_SSLv2,
      .ssl_ctx_timeout = 60*60*48,
      .verify_peer = SSL_VERIFY_PEER,
      .verify_depth = 42,
      .x509_verify_cb = dummy_ssl_verify_callback,
      .x509_chk_issued_cb = dummy_check_issued_cb,
      .scache_type = evhtp_ssl_scache_type_internal,
      .scache_size = 1024,
      .scache_timeout = 1024,
      .scache_init = NULL,
      .scache_add = NULL,
      .scache_get = NULL,
      .scache_del = NULL
    };

    if(evhtp_ssl_init(server, &ssl_config) != 0) {
      log_error("evhtp_ssl_init() failed");
      exit(EXIT_FAILURE);
    }
  }

  /* WEBFINGER */

  evhtp_callback_cb webfinger_cb = (RS_WEBFINGER_ENABLED ?
                                    handle_webfinger : reject_webfinger);
  evhtp_set_cb(server, "/.well-known/webfinger", webfinger_cb, NULL);
  // support legacy webfinger clients (we don't support XRD though):
  evhtp_set_cb(server, "/.well-known/host-meta", webfinger_cb, NULL);
  evhtp_set_cb(server, "/.well-known/host-meta.json", webfinger_cb, NULL);

  /* REMOTESTORAGE */

  evhtp_callback_t *storage_cb = evhtp_set_regex_cb(server, "^/storage/([^/]+)/.*$", handle_storage, NULL);

  evhtp_set_hook(&storage_cb->hooks, evhtp_hook_on_request_fini, finish_request, NULL);

  if(evhtp_bind_sockaddr(server, (struct sockaddr*)&sin, sizeof(sin), 1024) != 0) {
    log_error("evhtp_bind_sockaddr() failed: %s", strerror(errno));
    exit(EXIT_FAILURE);
  }

  /** SETUP AUTH LISTENER **/

  memset(&sin, 0, sizeof(struct sockaddr_in));
  sin.sin_family = AF_INET;
  sin.sin_addr.s_addr = htonl(0);
  sin.sin_port = htons(RS_AUTH_PORT);

  evhtp_t *server_auth = evhtp_new(rs_event_base, NULL);

  if(RS_USE_SSL) {
    evhtp_ssl_cfg_t ssl_config = {
      .pemfile = RS_SSL_CERT_PATH,
      .privfile = RS_SSL_KEY_PATH,
      .cafile = RS_SSL_CA_PATH,
      // what's this???
      .capath = NULL,
      .ciphers = "RC4+RSA:HIGH:+MEDIUM:+LOW",
      .ssl_opts = SSL_OP_NO_SSLv2,
      .ssl_ctx_timeout = 60*60*48,
      .verify_peer = SSL_VERIFY_PEER,
      .verify_depth = 42,
      .x509_verify_cb = dummy_ssl_verify_callback,
      .x509_chk_issued_cb = dummy_check_issued_cb,
      .scache_type = evhtp_ssl_scache_type_internal,
      .scache_size = 1024,
      .scache_timeout = 1024,
      .scache_init = NULL,
      .scache_add = NULL,
      .scache_get = NULL,
      .scache_del = NULL
    };

    if(evhtp_ssl_init(server_auth, &ssl_config) != 0) {
      log_error("evhtp_ssl_init() failed");
      exit(EXIT_FAILURE);
    }
  }

  /* AUTH */

  evhtp_set_cb(server_auth, "/authenticate", handle_authenticate, NULL);
  evhtp_set_cb(server_auth, "/authorizations", handle_authorizations, NULL);

  evhtp_set_hook(&storage_cb->hooks, evhtp_hook_on_request_fini, finish_request, NULL);

  if(evhtp_bind_sockaddr(server_auth, (struct sockaddr*)&sin, sizeof(sin), 1024) != 0) {
    log_error("evhtp_bind_sockaddr() failed: %s", strerror(errno));
    exit(EXIT_FAILURE);
  }

  /** SETUP SIGNALS **/

  sigset_t sigmask;
  sigemptyset(&sigmask);
  sigaddset(&sigmask, SIGINT);
  sigaddset(&sigmask, SIGTERM);
  sigaddset(&sigmask, SIGCHLD);
  ASSERT_ZERO(sigprocmask(SIG_BLOCK, &sigmask, NULL), "sigprocmask()");
  int sfd = signalfd(-1, &sigmask, SFD_NONBLOCK);
  ASSERT_NOT_EQ(sfd, -1, "signalfd()");

  struct event *signal_event = event_new(rs_event_base, sfd, EV_READ | EV_PERSIST,
                                         handle_signal, NULL);
  event_add(signal_event, NULL);

  /** RUN EVENT LOOP **/

  if(RS_DETACH) {
    int pid = fork();
    if(pid == 0) {
      event_reinit(rs_event_base);

      if(RS_LOG_FILE == stdout) {
        log_warn("No --log-file option given. Future output will be lost.");
        freopen("/dev/null", "r", stdout);
        freopen("/dev/null", "r", stderr);
      }

      return event_base_dispatch(rs_event_base);
    } else {
      printf("rs-serve detached with pid %d\n", pid);
      if(RS_PID_FILE) {
        fprintf(RS_PID_FILE, "%d", pid);
        fflush(RS_PID_FILE);
      }
      _exit(EXIT_SUCCESS);
    }
  } else {
    if(RS_PID_FILE) {
      fprintf(RS_PID_FILE, "%d", getpid());
      fflush(RS_PID_FILE);
    }
    return event_base_dispatch(rs_event_base);
  }
}