Esempio n. 1
0
NOEXPORT int daemonize(int fd) { /* go to background */
    if(global_options.option.foreground)
        return 0;
    dup2(fd, 0);
    dup2(fd, 1);
    dup2(fd, 2);
#if defined(HAVE_DAEMON) && !defined(__BEOS__)
    /* set noclose option when calling daemon() function,
     * so it does not require /dev/null device in the chrooted directory */
    if(daemon(0, 1)==-1) {
        ioerror("daemon");
        return 1;
    }
#else
    chdir("/");
    switch(fork()) {
    case -1:    /* fork failed */
        ioerror("fork");
        return 1;
    case 0:     /* child */
        break;
    default:    /* parent */
        exit(0);
    }
#endif
    tls_alloc(NULL, ui_tls, "main"); /* reuse thread-local storage */
#ifdef HAVE_SETSID
    setsid(); /* ignore the error */
#endif
    return 0;
}
Esempio n. 2
0
/**
 * Allocate an HTTP client instance
 *
 * @param clip      Pointer to allocated HTTP client
 * @param dnsc      DNS Client
 *
 * @return 0 if success, otherwise errorcode
 */
int http_client_alloc(struct http_cli **clip, struct dnsc *dnsc)
{
	struct http_cli *cli;
	int err;

	if (!clip || !dnsc)
		return EINVAL;

	cli = mem_zalloc(sizeof(*cli), cli_destructor);
	if (!cli)
		return ENOMEM;

#ifdef USE_TLS
	err = tls_alloc(&cli->tls, TLS_METHOD_SSLV23, NULL, NULL);
#else
	err = 0;
#endif
	if (err)
		goto out;

	cli->dnsc = mem_ref(dnsc);

 out:
	if (err)
		mem_deref(cli);
	else
		*clip = cli;

	return err;
}
Esempio n. 3
0
/* this has to be the first function called from ui_*.c */
void tls_init() {
    tls_platform_init();
    tls_initialized=1;
    ui_tls=tls_alloc(NULL, NULL, "ui");
    CRYPTO_set_mem_ex_functions(str_alloc_detached_debug,
        str_realloc_debug, free_function);
}
Esempio n. 4
0
void *str_alloc_debug(size_t size, const char *file, int line) {
    TLS_DATA *tls_data;
    ALLOC_LIST *alloc_list;

    if(!tls_initialized)
        fatal_debug("str not initialized", file, line);
    tls_data=tls_get();
    if(!tls_data) {
        tls_data=tls_alloc(NULL, NULL, "alloc");
        s_log(LOG_ERR, "INTERNAL ERROR: Uninitialized TLS at %s, line %d",
            file, line);
    }

    alloc_list=(ALLOC_LIST *)str_alloc_detached_debug(size, file, line)-1;
    alloc_list->prev=NULL;
    alloc_list->next=tls_data->alloc_head;
    alloc_list->tls=tls_data;
    if(tls_data->alloc_head)
        tls_data->alloc_head->prev=alloc_list;
    tls_data->alloc_head=alloc_list;
    tls_data->alloc_bytes+=size;
    tls_data->alloc_blocks++;

    return alloc_list+1;
}
Esempio n. 5
0
NOEXPORT void cron_thread(void *arg) {
    (void)arg; /* skip warning about unused parameter */
    tls_alloc(NULL, NULL, "cron");
    if(!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_LOWEST))
        ioerror("SetThreadPriority");
    cron_worker();
    _endthread(); /* it should never be executed */
}
Esempio n. 6
0
/* this has to be the first function called from ui_*.c */
void tls_init() {
    tls_platform_init();
    tls_initialized=1;
    ui_tls=tls_alloc(NULL, NULL, "ui");
#if OPENSSL_VERSION_NUMBER>=0x10100000L
    CRYPTO_set_mem_functions(str_alloc_detached_debug,
        str_realloc_detached_debug, str_free_debug);
#else
    CRYPTO_set_mem_ex_functions(str_alloc_detached_debug,
        str_realloc_detached_debug, free_function);
#endif
}
Esempio n. 7
0
int sthreads_init(void) {
    /* create the first (listening) context and put it in the running queue */
    if(!new_context()) {
        s_log(LOG_ERR, "Cannot create the listening context");
        return 1;
    }
    /* update tls for newly allocated ready_head */
    ui_tls=tls_alloc(NULL, ui_tls, "ui");
    /* no need to initialize ucontext_t structure here
       it will be initialied with swapcontext() call */
    return 0;
}
Esempio n. 8
0
NOEXPORT void *cron_thread(void *arg) {
#ifdef SCHED_BATCH
    struct sched_param param;
#endif

    (void)arg; /* skip warning about unused parameter */
    tls_alloc(NULL, NULL, "cron");
#ifdef SCHED_BATCH
    param.sched_priority=0;
    if(pthread_setschedparam(pthread_self(), SCHED_BATCH, &param))
        ioerror("pthread_getschedparam");
#endif
    cron_worker();
    return NULL; /* it should never be executed */
}
Esempio n. 9
0
NOEXPORT void WINAPI service_main(DWORD argc, LPTSTR* argv) {
    (void)argc; /* squash the unused parameter warning */
    (void)argv; /* squash the unused parameter warning */

    tls_alloc(NULL, NULL, "service"); /* new thread-local storage */

    /* initialise service status */
    serviceStatus.dwServiceType=SERVICE_WIN32;
    serviceStatus.dwCurrentState=SERVICE_STOPPED;
    serviceStatus.dwControlsAccepted=0;
    serviceStatus.dwWin32ExitCode=NO_ERROR;
    serviceStatus.dwServiceSpecificExitCode=NO_ERROR;
    serviceStatus.dwCheckPoint=0;
    serviceStatus.dwWaitHint=0;

    serviceStatusHandle=
        RegisterServiceCtrlHandler(SERVICE_NAME, control_handler);

    if(serviceStatusHandle) {
        /* service is starting */
        serviceStatus.dwCurrentState=SERVICE_START_PENDING;
        SetServiceStatus(serviceStatusHandle, &serviceStatus);

        /* running */
        serviceStatus.dwControlsAccepted|=
            (SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN);
        serviceStatus.dwCurrentState=SERVICE_RUNNING;
        SetServiceStatus(serviceStatusHandle, &serviceStatus);

        gui_loop();

        /* service was stopped */
        serviceStatus.dwCurrentState=SERVICE_STOP_PENDING;
        SetServiceStatus(serviceStatusHandle, &serviceStatus);

        /* service is now stopped */
        serviceStatus.dwControlsAccepted&=
            (DWORD)~(SERVICE_ACCEPT_STOP|SERVICE_ACCEPT_SHUTDOWN);
        serviceStatus.dwCurrentState=SERVICE_STOPPED;
        SetServiceStatus(serviceStatusHandle, &serviceStatus);
    }
}
Esempio n. 10
0
static int sipstack_fixture(struct sip **sipp)
{
    struct sa laddr, laddrs;
    struct sip *sip = NULL;
    struct tls *tls = NULL;
    int err;

    (void)sa_set_str(&laddr, "127.0.0.1", LOCAL_PORT);
    (void)sa_set_str(&laddrs, "127.0.0.1", LOCAL_SECURE_PORT);

    err = sip_alloc(&sip, NULL, 32, 32, 32, "retest", exit_handler, NULL);
    if (err)
        goto out;

    err |= sip_transp_add(sip, SIP_TRANSP_UDP, &laddr);
    err |= sip_transp_add(sip, SIP_TRANSP_TCP, &laddr);
    if (err)
        goto out;

#ifdef USE_TLS
    /* TLS-context for client -- no certificate needed */
    err = tls_alloc(&tls, TLS_METHOD_SSLV23, NULL, NULL);
    if (err)
        goto out;

    err |= sip_transp_add(sip, SIP_TRANSP_TLS, &laddrs, tls);
    if (err)
        goto out;
#endif

out:
    mem_deref(tls);
    if (err)
        mem_deref(sip);
    else
        *sipp = sip;

    return err;
}
Esempio n. 11
0
NOEXPORT void daemon_thread(void *arg) {
    (void)arg; /* squash the unused parameter warning */

    tls_alloc(NULL, NULL, "main"); /* new thread-local storage */
    main_init();
    SetEvent(main_initialized); /* unlock the GUI thread */
    /* get a valid configuration */
    while(main_configure(cmdline.config_file, NULL)) {
        if(cmdline.config_file && *cmdline.config_file=='-')
            cmdline.config_file=NULL; /* don't retry commandline switches */
        unbind_ports(); /* in case initialization failed after bind_ports() */
        log_flush(LOG_MODE_ERROR); /* otherwise logs are buffered */
        PostMessage(hwnd, WM_INVALID_CONFIG, 0, 0); /* display error */
        WaitForSingleObject(config_ready, INFINITE);
        log_close(); /* prevent main_configure() from logging in error mode */
    }
    PostMessage(hwnd, WM_VALID_CONFIG, 0, 0);

    /* start the main loop */
    daemon_loop();
    main_cleanup();
    _endthread(); /* SIGNAL_TERMINATE received */
}
Esempio n. 12
0
int main(int argc, char *argv[])
{
	const char *cert = NULL;
	int err = 0;

	tlsperf.num = 1;
	tlsperf.proto = IPPROTO_TCP;

	for (;;) {

		const int c = getopt(argc, argv, "a:dc:e:p:n:s:hv");
		if (0 > c)
			break;

		switch (c) {

		case 'c':
			cert = optarg;
			break;

		case 'd':
			tlsperf.proto = IPPROTO_UDP;
			break;

		case 'n':
			tlsperf.num = atoi(optarg);
			break;

		case 'v':
			tlsperf.verbose = true;
			break;

		case '?':
			err = EINVAL;
			/*@fallthrough@*/
		case 'h':
			usage();
			return err;
		}
	}

	err = libre_init();
	if (err)
		goto out;

	re_printf("tlsperf -- TLS performance testing program\n");
	re_printf("build:         %H\n", sys_build_get, 0);
	re_printf("compiler:      %s\n", __VERSION__);
	re_printf("libre:         %s\n", sys_libre_version_get());
	re_printf("os:            %s\n", sys_os_get());
	re_printf("arch:          %s\n", sys_arch_get());

#ifdef USE_OPENSSL
	re_printf("openssl info:  %s\n%s\n",
		  SSLeay_version(SSLEAY_VERSION),
		  SSLeay_version(SSLEAY_CFLAGS));
#endif

	re_printf("protocol:      %s\n",
		  tlsperf.proto == IPPROTO_TCP ? "TLS" : "DTLS");

	err = tls_alloc(&tlsperf.tls, TLS_METHOD_SSLV23, cert, 0);
	if (err)
		goto out;

	if (cert) {
		re_printf("certificate:   %s\n", cert);
	}
	else {
		re_printf("certificate:   selfsigned RSA-1024\n");
		err = tls_set_selfsigned(tlsperf.tls, "a@b");
		if (err)
			goto out;
	}

	re_printf("starting tests now. (num=%u)\n", tlsperf.num);

	/*
	 * Start timing now
	 */

	tlsperf.ts_start = tmr_jiffies();

	err = start_test();
	if (err)
		goto out;

	re_main(0);

 out:
	mem_deref(tlsperf.ep_srv);
	mem_deref(tlsperf.ep_cli);

	mem_deref(tlsperf.tls);

	libre_close();

	/* check for memory leaks */
	mem_debug();
	tmr_debug();

	return err ? err : tlsperf.err;
}
Esempio n. 13
0
static int add_transp_af(const struct sa *laddr)
{
	struct sa local;
	int err = 0;

	if (str_isset(uag.cfg->local)) {
		err = sa_decode(&local, uag.cfg->local,
				str_len(uag.cfg->local));
		if (err) {
			err = sa_set_str(&local, uag.cfg->local, 0);
			if (err) {
				warning("ua: decode failed: '%s'\n",
					uag.cfg->local);
				return err;
			}
		}

		if (!sa_isset(&local, SA_ADDR)) {
			uint16_t port = sa_port(&local);
			(void)sa_set_sa(&local, &laddr->u.sa);
			sa_set_port(&local, port);
		}

		if (sa_af(laddr) != sa_af(&local))
			return 0;
	}
	else {
		sa_cpy(&local, laddr);
		sa_set_port(&local, 0);
	}

	if (uag.use_udp)
		err |= sip_transp_add(uag.sip, SIP_TRANSP_UDP, &local);
	if (uag.use_tcp)
		err |= sip_transp_add(uag.sip, SIP_TRANSP_TCP, &local);
	if (err) {
		warning("ua: SIP Transport failed: %m\n", err);
		return err;
	}

#ifdef USE_TLS
	if (uag.use_tls) {
		/* Build our SSL context*/
		if (!uag.tls) {
			const char *cert = NULL;

			if (str_isset(uag.cfg->cert)) {
				cert = uag.cfg->cert;
				info("SIP Certificate: %s\n", cert);
			}

			err = tls_alloc(&uag.tls, TLS_METHOD_SSLV23,
					cert, NULL);
			if (err) {
				warning("ua: tls_alloc() failed: %m\n", err);
				return err;
			}
		}

		if (sa_isset(&local, SA_PORT))
			sa_set_port(&local, sa_port(&local) + 1);

		err = sip_transp_add(uag.sip, SIP_TRANSP_TLS, &local, uag.tls);
		if (err) {
			warning("ua: SIP/TLS transport failed: %m\n", err);
			return err;
		}
	}
#endif

	return err;
}
Esempio n. 14
0
void s_log(int level, const char *format, ...) {
    va_list ap;
    char *text, *stamp, *id;
    struct LIST *tmp;
#ifdef USE_WIN32
    DWORD libc_error;
#else
    int libc_error;
#endif
    int socket_error;
    time_t gmt;
    struct tm *timeptr;
#if defined(HAVE_LOCALTIME_R) && defined(_REENTRANT)
    struct tm timestruct;
#endif
    TLS_DATA *tls_data;

    tls_data=tls_get();
    if(!tls_data) {
        tls_data=tls_alloc(NULL, NULL, "log");
        s_log(LOG_ERR, "INTERNAL ERROR: Uninitialized TLS at %s, line %d",
            __FILE__, __LINE__);
    }

    /* performance optimization: skip the trivial case early */
    if(log_mode==LOG_MODE_CONFIGURED && level>tls_data->opt->log_level)
        return;

    libc_error=get_last_error();
    socket_error=get_last_socket_error();

    /* format the id to be logged */
    time(&gmt);
#if defined(HAVE_LOCALTIME_R) && defined(_REENTRANT)
    timeptr=localtime_r(&gmt, &timestruct);
#else
    timeptr=localtime(&gmt);
#endif
    stamp=str_printf("%04d.%02d.%02d %02d:%02d:%02d",
        timeptr->tm_year+1900, timeptr->tm_mon+1, timeptr->tm_mday,
        timeptr->tm_hour, timeptr->tm_min, timeptr->tm_sec);
    id=str_printf("LOG%d[%s]", level, tls_data->id);

    /* format the text to be logged */
    va_start(ap, format);
    text=str_vprintf(format, ap);
    va_end(ap);
    safestring(text);

    CRYPTO_THREAD_read_lock(stunnel_locks[LOCK_LOG_MODE]);
    if(log_mode==LOG_MODE_BUFFER) { /* save the text to log it later */
        CRYPTO_THREAD_write_lock(stunnel_locks[LOCK_LOG_BUFFER]);
        tmp=str_alloc_detached(sizeof(struct LIST));
        tmp->next=NULL;
        tmp->opt=tls_data->opt;
        tmp->level=level;
        tmp->stamp=stamp;
        str_detach(tmp->stamp);
        tmp->id=id;
        str_detach(tmp->id);
        tmp->text=text;
        str_detach(tmp->text);
        if(tail)
            tail->next=tmp;
        else
            head=tmp;
        tail=tmp;
        CRYPTO_THREAD_write_unlock(stunnel_locks[LOCK_LOG_BUFFER]);
    } else { /* ready log the text directly */
        log_raw(tls_data->opt, level, stamp, id, text);
        str_free(stamp);
        str_free(id);
        str_free(text);
    }
    CRYPTO_THREAD_read_unlock(stunnel_locks[LOCK_LOG_MODE]);

    set_last_error(libc_error);
    set_last_socket_error(socket_error);
}