Beispiel #1
0
int botan_rng_init(botan_rng_t* rng_out, const char* rng_type)
   {
   try
      {
      BOTAN_ASSERT_ARG_NON_NULL(rng_out);

      if(rng_type == nullptr || *rng_type == 0)
         rng_type = "system";

      const std::string rng_type_s(rng_type);

      std::unique_ptr<Botan::RandomNumberGenerator> rng;

      if(rng_type_s == "system")
         rng.reset(new Botan::System_RNG);
      else if(rng_type_s == "user")
         rng.reset(new Botan::AutoSeeded_RNG);

      if(rng)
         {
         *rng_out = new botan_rng_struct(rng.release());
         return 0;
         }
      }
   catch(std::exception& e)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, e.what());
      }
   catch(...)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, "unknown");
      }

   return -1;
   }
Beispiel #2
0
static nx_logdata_t *nx_patterndb_pattern_exec(nx_module_t *module,
					       nx_logdata_t *logdata,
					       nx_pattern_t *pattern)
{
    nx_expr_eval_ctx_t eval_ctx;
    nx_exception_t e;

    nx_expr_eval_ctx_init(&eval_ctx, logdata, module, NULL);
    try
    {
	nx_expr_statement_list_execute(&eval_ctx, pattern->exec);
    }
    catch(e)
    {
	log_exception(e);
    }
    if ( eval_ctx.logdata == NULL )
    { // dropped
	nx_module_logqueue_pop(module, logdata);
	nx_logdata_free(logdata);
    }
    logdata = eval_ctx.logdata;
    nx_expr_eval_ctx_destroy(&eval_ctx);

    return ( logdata );
}
Beispiel #3
0
static void im_ssl_write(nx_module_t *module, nx_event_t *event)
{
    int rv;
    SSL *ssl;
    apr_socket_t *sock;
    nx_module_input_t *input = NULL;
    int errcode;
    nx_exception_t e;

    sock = (apr_socket_t *) event->data;

    ASSERT(module != NULL);
    
    CHECKERR_MSG(apr_socket_data_get((void **) &input, "input", sock),
		 "couldn't get input data from socket");
    ASSERT(input != NULL);
    ssl = (SSL *) nx_module_input_data_get(input, "ssl");
    ASSERT(ssl != NULL);

    if ( !SSL_is_init_finished(ssl) )
    {
	log_debug("doing handshake");
	try
	{
	    if ( (rv = SSL_do_handshake(ssl)) <= 0 )
	    {
		switch ( (errcode = nx_ssl_check_io_error(ssl, rv)) )
		{
		    case SSL_ERROR_ZERO_RETURN: // disconnected
			throw_msg("im_ssl got disconnected during handshake");
			break;
		    case SSL_ERROR_WANT_WRITE:
			log_debug("im_ssl WANT_WRITE");
			nx_module_pollset_add_socket(module, input->desc.s, APR_POLLOUT | APR_POLLHUP);
			break;
		    case SSL_ERROR_WANT_READ:
			log_debug("im_ssl WANT_READ");
			nx_module_pollset_add_socket(module, input->desc.s, APR_POLLIN | APR_POLLHUP);
			break;
		    default:
			throw_msg("im_ssl couldn't write handshake data (error code: %d)", errcode);
		}
	    }
	}
	catch(e)
	{
	    log_exception(e);
	    im_ssl_disconnect(input);
	    return;
	}
    }
    else
    {
	log_warn("SSL socket should not be sending anything after the handshake");
	nx_module_pollset_add_socket(module, input->desc.s, APR_POLLIN | APR_POLLHUP);
    }
}
Beispiel #4
0
void StarbookDriver::TimerHit() {
    try {
        Telescope::TimerHit();
    }
    catch (std::exception &e) {
        log_exception(getDeviceName(), e.what());
        throw e;
    }

}
Beispiel #5
0
void ISNewSwitch(const char* dev, const char* name, ISState* states, char* names[], int n)
{
    try {
        starbook_driver->ISNewSwitch(dev, name, states, names, n);
    }
    catch (std::exception &e) {
        log_exception(starbook_driver->getDeviceName(), e.what());
        throw e;
    }
}
Beispiel #6
0
void ISNewText(const char* dev, const char* name, char* texts[], char* names[], int n)
{
    try {
        starbook_driver->ISNewText(dev, name, texts, names, n);
    }
    catch (std::exception &e) {
        log_exception(starbook_driver->getDeviceName(), e.what());
        throw e;
    }
}
Beispiel #7
0
void ISNewNumber(const char* dev, const char* name, double values[], char* names[], int n)
{
    try {
        starbook_driver->ISNewNumber(dev, name, values, names, n);
    }
    catch (std::exception &e) {
        log_exception(starbook_driver->getDeviceName(), e.what());
        throw e;
    }
}
Beispiel #8
0
void ISGetProperties(const char* dev)
{
    try {
        starbook_driver->ISGetProperties(dev);
    }
    catch (std::exception &e) {
        log_exception(starbook_driver->getDeviceName(), e.what());
        throw e;
    }
}
Beispiel #9
0
int
csl_compile(char *str, char *name, char **codeptr, size_t *sizeptr)
{
	PyObject *pCode, *pStr;
	node *n;
	size_t size;

	// compile into a code object
	n = PyParser_SimpleParseString(str, Py_file_input);
	if (!n) {
		log_exception();
		return CSL_BADCODE;
	}
	pCode = (PyObject *) PyNode_Compile(n, name);
	PyNode_Free(n);
	if (!pCode) {
		log_exception();
		return CSL_BADCODE;
	}

	// serialize code object
#if PY_MINOR_VERSION == 3
	pStr = PyMarshal_WriteObjectToString(pCode);
#else
	pStr = PyMarshal_WriteObjectToString(pCode, 0);
#endif
	Py_DECREF(pCode);
	if (!pStr) {
		return CSL_NOMEM;
	}
	size = PyString_Size(pStr);
	*codeptr = malloc(size);
	if (!*codeptr) {
		Py_DECREF(pStr);
		return CSL_NOMEM;
	}
	memcpy(*codeptr, PyString_AsString(pStr), size);
	*sizeptr = size;
	Py_DECREF(pStr);

	return 0;
}
Beispiel #10
0
int cb_cpu_restore_state(CPUState *env, TranslationBlock *tb){
    printf("EXCEPTION - logging\n");
    DynValBuffer *dynval_buffer = PIFP->PIV->getDynvalBuffer();
    log_exception(dynval_buffer);

    // Then execute taint ops up until the exception occurs.  Execution of taint
    // ops will stop at the point of the exception.
    rewind_dynval_buffer(dynval_buffer);
    execute_taint_ops(PTFP->ttb, shadow, dynval_buffer);

    // Make sure there's nothing left in the buffer
    assert(dynval_buffer->ptr - dynval_buffer->start == dynval_buffer->cur_size);
    return 0;
}
Beispiel #11
0
int botan_hex_encode(const uint8_t* in, size_t len, char* out, uint32_t flags)
   {
   try
      {
      const bool uppercase = (flags & BOTAN_FFI_HEX_LOWER_CASE) == 0;
      Botan::hex_encode(out, in, len, uppercase);
      return 0;
      }
   catch(std::exception& e)
      {
      log_exception(BOTAN_CURRENT_FUNCTION, e.what());
      }

   return 1;
   }
Beispiel #12
0
static void im_exec_event(nx_module_t *module, nx_event_t *event)
{
    nx_im_exec_conf_t *imconf;
    nx_exception_t e;

    ASSERT(event != NULL);

    imconf = (nx_im_exec_conf_t *) module->config;

    switch ( event->type )
    {
	case NX_EVENT_DISCONNECT:
	    // FIXME: restart if imconf->restart == TRUE
	    log_warn("im_exec process %s exited", imconf->cmd);
	    imconf->running = FALSE;
	    im_exec_add_restart_event(module);
	    break;
	case NX_EVENT_READ:
	    try
	    {
		im_exec_read(module);
	    }
	    catch(e)
	    {
		log_exception(e);
		im_exec_add_restart_event(module);
	    }
	    break;
	case NX_EVENT_POLL:
#ifdef WIN32
	    nx_panic("pollset based im_exec implementation doesn't work on windows");
#else
	    if ( nx_module_get_status(module) == NX_MODULE_STATUS_RUNNING )
	    {
		nx_module_pollset_poll(module, TRUE);
	    }
	    break;
#endif
	default:
	    nx_panic("invalid event type: %d", event->type);
    }
}
void nx_expr_proc__xm_fileop_file_remove(nx_expr_eval_ctx_t *eval_ctx,
					 nx_module_t *module,
					 nx_expr_arg_list_t *args)
{
    nx_expr_arg_t *arg;
    nx_value_t file;
    apr_status_t rv;
    apr_pool_t *pool = NULL;
    apr_dir_t *dir;
    nx_exception_t e;
    nx_string_t *dirname = NULL;
    nx_string_t *fname = NULL;
    char *filename;
    char *idx;
    int flags = 0;
    apr_finfo_t finfo;
    nx_expr_arg_t *older;
    nx_value_t olderval;
    apr_time_t older_time = 0LL;

    ASSERT(module != NULL);

    ASSERT(args != NULL);
    arg = NX_DLIST_FIRST(args);
    ASSERT(arg != NULL);
    ASSERT(arg->expr != NULL);

    nx_expr_evaluate(eval_ctx, &file, arg->expr);

    if ( file.defined != TRUE )
    {
	throw_msg("'file' is undef");
    }

    try
    {
	if ( file.type != NX_VALUE_TYPE_STRING )
	{
	    throw_msg("string type required for 'file'");
	}

	older = NX_DLIST_NEXT(arg, link);
	if ( older != NULL )
	{
	    ASSERT(older->expr != NULL);
	    nx_expr_evaluate(eval_ctx, &olderval, older->expr);
	    if ( olderval.type != NX_VALUE_TYPE_DATETIME )
	    {
		nx_value_kill(&olderval);
		throw_msg("datetime type required for 'older'");
	    }
	    if ( olderval.defined == TRUE )
	    {
		older_time = olderval.datetime;
	    }
	}

	if ( apr_fnmatch_test(file.string->buf) != 0 )
	{ // we have wildcards, expand it
	    pool = nx_pool_create_core();

	    filename = file.string->buf;
	    idx = strrchr(filename, '/');
#ifndef WIN32
	    flags = APR_FNM_CASE_BLIND;
	    if ( idx == NULL ) 
	    {
		idx = strrchr(filename, '\\');
	    }
#endif
	    if ( idx == NULL )
	    {
		dirname = nx_string_create("."NX_DIR_SEPARATOR, -1);
	    }
	    else
	    {
		dirname = nx_string_create(filename, (int) (idx + 1 - filename));
		filename = idx + 1;
	    }

	    CHECKERR_MSG(apr_dir_open(&dir, dirname->buf, pool),
			 "failed to open directory: %s", dirname->buf);
	    fname = nx_string_new();

	    while ( apr_dir_read(&finfo, APR_FINFO_NAME | APR_FINFO_TYPE | APR_FINFO_CTIME,
				 dir) == APR_SUCCESS )
	    {
		if ( finfo.filetype == APR_REG )
		{
		    log_debug("checking '%s' against wildcard '%s':", finfo.name, filename);
		    if ( apr_fnmatch(filename, finfo.name, flags) == APR_SUCCESS )
		    {
			nx_string_sprintf(fname, "%s%s", dirname->buf, finfo.name);

			if ( (older_time == 0) ||
			     ((older_time != 0) && (finfo.ctime < older_time)) )
			{
			    log_debug("'%s' matches wildcard '%s' and is 'older', removing",
				      fname->buf, file.string->buf);
			    log_info("removing file %s", fname->buf);
			    rv = apr_file_remove(fname->buf, NULL);
			    if ( APR_STATUS_IS_ENOENT(rv) )
			    {
			    }
			    else if ( rv == APR_SUCCESS )
			    {
			    }
			    else
			    {
				log_aprerror(rv, "failed to remove file '%s'", fname->buf);
			    }
			    _reopen_logfile(fname->buf);
			}
		    }
		}
	    }
	    nx_string_free(fname);
	    apr_pool_destroy(pool);
	}
	else
	{
	    CHECKERR_MSG(apr_file_remove(file.string->buf, NULL), 
			 "failed to remove file '%s'", file.string->buf);
	}
    }
    catch(e)
    {
	nx_value_kill(&file);
	if ( pool != NULL )
	{
	    apr_pool_destroy(pool);
	}
	if ( dirname != NULL )
	{
	    nx_string_free(dirname);
	}
	log_exception(e);
    }
}
void nx_expr_proc__xm_fileop_file_cycle(nx_expr_eval_ctx_t *eval_ctx,
					nx_module_t *module,
					nx_expr_arg_list_t *args)
{
    nx_expr_arg_t *arg;
    nx_value_t file;
    nx_expr_arg_t *val;
    nx_value_t value;
    apr_status_t rv;
    apr_pool_t *pool = NULL;
    nx_exception_t e;
    volatile int64_t max = 0;
    int32_t i, last;
    nx_string_t *tmpstr = NULL;
    nx_string_t *tmpstr2 = NULL;

    ASSERT(module != NULL);

    ASSERT(args != NULL);
    arg = NX_DLIST_FIRST(args);
    ASSERT(arg != NULL);
    ASSERT(arg->expr != NULL);

    nx_expr_evaluate(eval_ctx, &file, arg->expr);

    if ( file.defined != TRUE )
    {
	throw_msg("'file' is undef");
    }
    if ( file.type != NX_VALUE_TYPE_STRING )
    {
	nx_value_kill(&file);
	throw_msg("string type required for 'file'");
    }


    val = NX_DLIST_NEXT(arg, link);
    if ( val != NULL )
    {
	ASSERT(val->expr != NULL);
	nx_expr_evaluate(eval_ctx, &value, val->expr);
	if ( value.type != NX_VALUE_TYPE_INTEGER )
	{
	    nx_value_kill(&file);
	    nx_value_kill(&value);
	    throw_msg("integer type required for 'max'");
	}
	if ( value.defined == TRUE )
	{
	    if ( value.integer <= 0 )
	    {
		nx_value_kill(&file);
		nx_value_kill(&value);
		throw_msg("'max' must be a positive integer");
	    }
	    max = value.integer;
	}
    }

    try
    {
	pool = nx_pool_create_core();
	
	if ( _file_exists(file.string->buf, pool) == TRUE )
	{  // check if the file we need to cycle exists
	    tmpstr = nx_string_new();
	    tmpstr2 = nx_string_new();
	    last = 0;
	    for ( i = 1; i < 2147483647 /*APR_INT32_MAX*/; i++ )
	    {
		nx_string_sprintf(tmpstr, "%s.%d", file.string->buf, i);
		if ( _file_exists(tmpstr->buf, pool) == FALSE )
		{
		    break;
		}
		if ( (max > 0) && (i >= max) )
		{
		    log_info("removing file %s", tmpstr->buf);
		    if ( (rv = apr_file_remove(tmpstr->buf, NULL)) != APR_SUCCESS )
		    {
			log_aprerror(rv, "failed to remove file '%s'", tmpstr->buf);
		    }
		}
		else
		{
		    last = i;
		}
	    }
	    if ( last > 0 )
	    { // now starting from the last existing file, cycle them
		for ( i = last; i > 0; i-- )
		{
		    nx_string_sprintf(tmpstr, "%s.%d", file.string->buf, i);
		    nx_string_sprintf(tmpstr2, "%s.%d", file.string->buf, i + 1);
		    log_debug("cycling %s to %s", tmpstr->buf, tmpstr2->buf);
		    if ( (rv = apr_file_rename(tmpstr->buf, tmpstr2->buf, NULL)) != APR_SUCCESS )
		    {
			log_aprerror(rv, "failed to rename file from '%s' to '%s'", 
				     tmpstr->buf, tmpstr2->buf);
		    }
		}
	    }
	    // finally rename file to file.1
	    nx_string_sprintf(tmpstr, "%s.%d", file.string->buf, 1);
	    if ( (rv = apr_file_rename(file.string->buf, tmpstr->buf, NULL)) != APR_SUCCESS )
	    {
		log_aprerror(rv, "failed to rename file from '%s' to '%s'", 
			     file.string->buf, tmpstr->buf);
	    }
	    _reopen_logfile(file.string->buf);

	    nx_string_free(tmpstr);
	    nx_string_free(tmpstr2);
	}
	apr_pool_destroy(pool);
	nx_value_kill(&file);
    }
    catch(e)
    {
	nx_value_kill(&file);
	if ( pool != NULL )
	{
	    apr_pool_destroy(pool);
	}
	if ( tmpstr != NULL )
	{
	    nx_string_free(tmpstr);
	}
	if ( tmpstr2 != NULL )
	{
	    nx_string_free(tmpstr2);
	}
	log_exception(e);
    }
}
Beispiel #15
0
// return TRUE if there was an error and we need to clean up the input
static boolean im_ssl_fill_buffer(nx_module_t *module, nx_module_input_t *input)
{
    volatile int retval;
    SSL *ssl;
    int nbytes;
    nx_exception_t e;

    ASSERT(input != NULL);
    ASSERT(input->desc_type == APR_POLL_SOCKET);
    ASSERT(input->desc.s != NULL);
    ASSERT(input->buf != NULL);

    ssl = (SSL *) nx_module_input_data_get(input, "ssl");
    ASSERT(ssl != NULL);

    if ( input->bufstart == input->bufsize )
    {
	input->bufstart = 0;
	input->buflen = 0;
    }
    if ( input->buflen == 0 )
    {
	input->bufstart = 0;
    }

    nbytes = (int) (input->bufsize - (input->buflen + input->bufstart));

    if ( nbytes > 0 )
    {
	try
	{
	    retval = nx_ssl_read(ssl, input->buf + input->bufstart + input->buflen, &nbytes);
	}
	catch(e)
	{
	    log_exception(e);
	    return ( TRUE );
	}
	ASSERT(nbytes <= (int) (input->bufsize - (input->buflen + input->bufstart)));
	input->buflen += nbytes;
	switch ( retval )
	{
	    case SSL_ERROR_NONE:
		log_debug("Module %s read %u bytes", input->module->name, (unsigned int) input->buflen);
		nx_module_pollset_add_socket(module, input->desc.s, APR_POLLIN | APR_POLLHUP);
		break;
	    case SSL_ERROR_ZERO_RETURN: // disconnected
		log_debug("remote ssl connection closed");
		return ( TRUE );
	    case SSL_ERROR_WANT_WRITE:
		log_debug("im_ssl WANT_WRITE");
		nx_module_pollset_add_socket(module, input->desc.s, APR_POLLOUT | APR_POLLHUP);
		break;
	    case SSL_ERROR_WANT_READ:
		log_debug("im_ssl WANT_READ");
		nx_module_pollset_add_socket(module, input->desc.s, APR_POLLIN | APR_POLLHUP);
		break;
	    default:
		log_error("im_ssl couldn't read, disconnecting (error code: %d)", retval);
		return ( TRUE );
	}
    }
    else
    {
	log_debug("im_ssl_fill_buffer called with full buffer");
    }

    return ( FALSE );
}
Beispiel #16
0
static void WINAPI nx_win32_svc_main(DWORD argc, LPTSTR *argv)
{
    nx_context_t thread_context;
    nx_exception_t e;

    if ( _nxlog_initializer == 0 )
    {   // running from service manager
        ASSERT(nx_init(&argc, &argv, NULL) == TRUE);

        nxlog_init(&nxlog);
        nx_logger_disable_foreground();
    }
    else if ( _nxlog_initializer != apr_os_thread_current() )
    {
        // service dispatcher runs in a new thread, we need
        // to initialize the exception context.
        _nxlog_initializer = apr_os_thread_current();
        memset(&thread_context, 0, sizeof(nx_context_t));
        init_exception_context(&thread_context.exception_context);
        apr_threadkey_private_set(&thread_context, nx_get_context_key());
    }

    log_debug("nx_win32_svc_main");

    try
    {
        // read config cache
        nx_config_cache_read();
        log_debug("nxlog cache read");

        // load DSO and read and verify module config
        nx_ctx_config_modules(nxlog.ctx);
        log_debug("nxlog config OK");

        // initialize modules
        nx_ctx_init_modules(nxlog.ctx);

        // initialize log routes
        nx_ctx_init_routes(nxlog.ctx);
        nx_ctx_init_jobs(nxlog.ctx);

        nx_ctx_restore_queues(nxlog.ctx);

        // setup threadpool
        nxlog_create_threads(&nxlog);

        // start modules
        nx_ctx_start_modules(nxlog.ctx);

        if ( nxlog.foreground != TRUE )
        {
            // register to service manager
            svc_status_handle = RegisterServiceCtrlHandler("nxlog", nx_win32_svc_change);
            if ( svc_status_handle == 0 )
            {
                nx_win32_error("RegisterServiceCtrlHandler() failed, couldn't register the service control handler");
            }

            // Signal to svc manager that we are running
            svc_status.dwWin32ExitCode = 0;
            svc_status.dwServiceSpecificExitCode = 0;
            svc_status.dwCheckPoint = 0;
            svc_status.dwWaitHint = 0;
            svc_status.dwServiceType = SERVICE_WIN32;
            svc_status.dwCurrentState = SERVICE_RUNNING;
            svc_status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
            if ( SetServiceStatus(svc_status_handle, &svc_status) == FALSE )
            {
                nx_win32_error("Cannot send start service status update");
            }
        }

        log_info(PACKAGE"-"VERSION_STRING" started");
    }
    catch(e)
    {
        log_exception(e);
        log_error("exiting...");
        svc_status.dwCurrentState = SERVICE_STOPPED;
        SetServiceStatus(svc_status_handle, &svc_status);
        exit(e.code);
    }

    // mainloop
    nxlog_mainloop(&nxlog, FALSE);

    nxlog_shutdown(&nxlog);

    if ( nxlog.foreground != TRUE )
    {
        // Signal back that we are stopped
        svc_status.dwCurrentState = SERVICE_STOPPED;
        SetServiceStatus(svc_status_handle, &svc_status);
        log_debug("service stopped");
    }

    nxlog_exit_function();
}
int cb_cpu_restore_state(CPUState *env, TranslationBlock *tb){
    printf("EXCEPTION - logging\n");
    DynValBuffer *dynval_buffer = PIFP->PIV->getDynvalBuffer();
    log_exception(dynval_buffer);
    return 0;
}
Beispiel #18
0
int main(int argc, const char * const *argv, const char * const *env)
{
    DWORD dispmode;
    nx_exception_t e;

    ASSERT(nx_init(&argc, &argv, &env) == TRUE);

    nxlog_init(&nxlog);
    _nxlog_initializer = apr_os_thread_current();

    try
    {
        // read cmd line
        parse_cmd_line(argc, argv);
        /*
        	// reload switch?
        	if ( nxlog.do_restart == TRUE )
        	{
        	    //FIXME restart service; use custom control code?
        	}
        */
        // stop switch?
        if ( nxlog.do_stop == TRUE )
        {
            nx_win32_svc_stop();
            exit(0);
        }

        if ( do_install == TRUE )
        {
            nx_win32_svc_install();
            exit(0);
        }

        if ( do_uninstall == TRUE )
        {
            nx_win32_svc_uninstall();
            exit(0);
        }

        // load and parse config
        nx_ctx_parse_cfg(nxlog.ctx, nxlog.cfgfile);

        if ( nxlog.ctx->spooldir != NULL )
        {
            CHECKERR_MSG(apr_filepath_set(nxlog.ctx->spooldir, nxlog.pool),
                         "Couldn't change to SpoolDir '%s'", nxlog.ctx->spooldir);
        }

        nx_ctx_init_logging(nxlog.ctx);

        if ( nxlog.ctx->rootdir != NULL )
        {
            throw_msg("RootDir not supported on windows");
        }

        if ( nxlog.verify_conf == TRUE )
        {
            // load DSO and read and verify module config
            nx_ctx_config_modules(nxlog.ctx);
            nx_ctx_init_routes(nxlog.ctx);
            log_info("configuration OK");
            exit(0);
        }

        if ( nxlog.foreground == TRUE )
        {
            nx_win32_svc_main(argc, argv);
        }
        else
        {
            // detect wheter we are invoked by SVM or from the console
            if ( GetConsoleDisplayMode(&dispmode) == 0 )
            {
                nx_win32_svc_dispatch();
            }
            else
            {
                nx_win32_svc_start(argc, argv);
            }
        }
    }
    catch(e)
    {
        apr_file_t *f = NULL;
        apr_pool_t *pool;
        HKEY regkey;
        uint32_t regtype = 0;
        char regvalbuf[1024];
        uint32_t regvalbufsize = 1024;
        const char *logfile = "c:\\nxlog-service-error.txt";

        pool = nx_pool_create_core();

        // TODO: log to eventlog in addition?

        if ( RegOpenKey(HKEY_LOCAL_MACHINE, "SOFTWARE\\nxlog", &regkey) == ERROR_SUCCESS )
        {
            if ( RegQueryValueEx(regkey, "installdir", 0, &regtype,
                                 (unsigned char *) regvalbuf, &regvalbufsize) == ERROR_SUCCESS )
            {
                if ( regtype == REG_SZ )
                {
                    logfile = apr_psprintf(nxlog.pool, "%sdata\\nxlog.log", regvalbuf);
                }
            }
            RegCloseKey(regkey);
        }
        apr_file_open(&f, logfile, APR_READ | APR_WRITE | APR_CREATE | APR_APPEND,
                      APR_OS_DEFAULT, pool);
        // FXIME: check status code
        if ( f != NULL )
        {
            nx_string_t *tmpstr;
            int i;
            char errmsg[1024];

            tmpstr = nx_string_new();

            for ( i = ((int) e.num_throw) - 1; i >= 0; i-- )
            {
                if ( tmpstr->len > 0 )
                {
                    nx_string_append(tmpstr, NX_LINEFEED, -1);
                }
                nx_string_append(tmpstr, e.msgbuf + e.throwlist[i].msgoffs, -1);
            }
            nx_string_append(tmpstr, NX_LINEFEED, -1);
            if ( e.code != APR_SUCCESS )
            {
                apr_strerror(e.code, errmsg, sizeof(errmsg));
                nx_string_append(tmpstr, errmsg, -1);
                nx_string_append(tmpstr, NX_LINEFEED, -1);
            }
            apr_file_printf(f, "nxlog failed to start: %s\r\n", tmpstr->buf);
            apr_file_close(f);
            nx_string_free(tmpstr);
        }
        apr_pool_destroy(pool);
        log_exception(e);
        exit(e.code);
    }

    return ( 0 );
}
Beispiel #19
0
static void xm_multiline_config(nx_module_t *module)
{
    nx_xm_multiline_conf_t *modconf;
    const nx_directive_t * volatile curr;
    nx_exception_t e;

    modconf = apr_pcalloc(module->pool, sizeof(nx_xm_multiline_conf_t));
    module->config = modconf;

    curr = module->directives;

    while ( curr != NULL )
    {
	if ( nx_module_common_keyword(curr->directive) == TRUE )
	{
	}
	else if ( strcasecmp(curr->directive, "headerline") == 0 )
	{
	    if ( modconf->headerline != NULL )
	    {
		nx_conf_error(curr, "HeaderLine is already defined");
	    }

	    try
	    {
		if ( (strlen(curr->args) > 1) &&
		     (curr->args[0] == '/') &&
		     (curr->args[strlen(curr->args) - 1] == '/') )
		{
		    // TODO: this is a hack because regexp and division are not correctly
		    // handled by the lexer/parser. expr-tokens.l needs to be fixed and
		    // needs to be stateful.
		    curr->args[strlen(curr->args) - 1] = '\0';
		    modconf->headerline = nx_value_new_regexp(curr->args + 1);
		}
		else
		{
		    modconf->headerline_expr = nx_expr_parse(module, curr->args, module->pool, curr->filename,
							     curr->line_num, curr->argsstart);
		    if ( !((modconf->headerline_expr->rettype == NX_VALUE_TYPE_STRING) ||
			   (modconf->headerline_expr->rettype == NX_VALUE_TYPE_REGEXP)) )
		    {
			throw_msg("string or regexp type required in expression, found '%s'",
				  nx_value_type_to_string(modconf->headerline_expr->rettype));
		    }
		    if ( modconf->headerline_expr->type == NX_EXPR_TYPE_VALUE )
		    {
			ASSERT(modconf->headerline_expr->value.defined == TRUE);
			modconf->headerline = &(modconf->headerline_expr->value);
		    }
		}
	    }
	    catch(e)
	    {
		log_exception(e);
		nx_conf_error(curr, "invalid expression in 'HeaderLine'");
	    }
	}
	else if ( strcasecmp(curr->directive, "endline") == 0 )
	{
	    if ( modconf->endline != NULL )
	    {
		nx_conf_error(curr, "EndLine is already defined");
	    }

	    try
	    {
		if ( (strlen(curr->args) > 1) &&
		     (curr->args[0] == '/') &&
		     (curr->args[strlen(curr->args) - 1] == '/') )
		{
		    // TODO: this is a hack because regexp and division are not correctly
		    // handled by the lexer/parser. expr-tokens.l needs to be fixed and
		    // needs to be stateful.
		    curr->args[strlen(curr->args) - 1] = '\0';
		    modconf->endline = nx_value_new_regexp(curr->args + 1);
		}
		else
		{
		    modconf->endline_expr = nx_expr_parse(module, curr->args, module->pool, curr->filename,
							     curr->line_num, curr->argsstart);
		    if ( !((modconf->endline_expr->rettype == NX_VALUE_TYPE_STRING) ||
			   (modconf->endline_expr->rettype == NX_VALUE_TYPE_REGEXP)) )
		    {
			throw_msg("string or regexp type required in expression, found '%s'",
				  nx_value_type_to_string(modconf->endline_expr->rettype));
		    }
		    if ( modconf->endline_expr->type == NX_EXPR_TYPE_VALUE )
		    {
			ASSERT(modconf->endline_expr->value.defined == TRUE);
			modconf->endline = &(modconf->endline_expr->value);
		    }
		}
	    }
	    catch(e)
	    {
		log_exception(e);
		nx_conf_error(curr, "invalid expression in 'Endline'");
	    }

	}
	else if ( strcasecmp(curr->directive, "fixedlinecount") == 0 )
	{
	    if ( modconf->fixedlinecount != 0 )
	    {
		nx_conf_error(curr, "FixedLineCount is already defined");
	    }
	    if ( sscanf(curr->args, "%u", &(modconf->fixedlinecount)) != 1 )
	    {
		nx_conf_error(curr, "invalid number: %s", curr->args);
	    }
	}
	else
	{
	    nx_conf_error(curr, "invalid keyword: %s", curr->directive);
	}
	curr = curr->next;
    }

    if ( (modconf->headerline == NULL) && 
	 (modconf->endline == NULL) &&
	 (modconf->fixedlinecount == 0) )
    {
	nx_conf_error(module->directives, "At least one of HeaderLine, EndLine or FixedLineCount is required");
    }

    if ( (modconf->headerline != NULL) &&
	 (modconf->endline != NULL) &&
	 (nx_value_eq(modconf->headerline, modconf->endline) == TRUE) )
    {
	nx_conf_error(module->directives, "HeaderLine and EndLine cannot be the same");
    }

    if ( nx_module_input_func_lookup(module->name) == NULL )
    {
	nx_module_input_func_register(NULL, module->name, &xm_multiline_input_func, module);
	log_debug("Inputreader '%s' registered", module->name);
    }
}
Beispiel #20
0
static nx_logdata_t *xm_multiline_input_func(nx_module_input_t *input,
					     void *data)
{
    volatile int i;
    nx_logdata_t * volatile retval = NULL;
    boolean foundcr = FALSE;
    nx_xm_multiline_ctx_t *ctx;
    nx_xm_multiline_conf_t *modconf;
    nx_module_t *module;
    boolean appendline;
    boolean gotline;
    volatile boolean done = FALSE;

    ASSERT(input != NULL);
    ASSERT(input->buflen >= 0);
    ASSERT(data != NULL);

    if ( input->buflen == 0 )
    {
	return ( NULL );
    }

    if ( input->ctx == NULL )
    {
	input->ctx = apr_pcalloc(input->pool, sizeof(nx_xm_multiline_ctx_t));
    }
    ctx = input->ctx;
    module = (nx_module_t *) data;
    modconf = (nx_xm_multiline_conf_t *) module->config;

    do
    {
	foundcr = FALSE;
	appendline = TRUE;
	gotline = FALSE;
	for ( i = 0; (i < input->buflen) && (gotline == FALSE) ; i++ )
	{
	    switch ( input->buf[input->bufstart + i] )
	    {
		case APR_ASCII_CR:
		    if ( foundcr == TRUE )
		    {
			gotline = TRUE;
			i--;
		    }
		    else
		    {
			foundcr = TRUE;
		    }
		    break;
		case APR_ASCII_LF:
		    if ( foundcr == TRUE )
		    {
			gotline = TRUE;
			i--;
		    }
		    else
		    {
			foundcr = TRUE;
		    }
		    break;
		default:
		    if ( foundcr == TRUE )
		    {
			gotline = TRUE;
			i--;
		    }
		    break;
	    }
	}

	if ( ctx->tmpline == NULL )
	{
	    ctx->tmpline = nx_logdata_new_logline(input->buf + input->bufstart, i);
	}
	else
	{
	    nx_logdata_append_logline(ctx->tmpline, input->buf + input->bufstart, i);
	}

	//log_info("tmpline: [%s]", ctx->tmpline->raw_event->buf);

	if ( foundcr == TRUE )
	{ // got a complete line
	    if ( module->exec != NULL )
	    {
		nx_expr_eval_ctx_t eval_ctx;
		nx_exception_t e;

		nx_expr_eval_ctx_init(&eval_ctx, ctx->tmpline, module, input);
		try
		{
		    nx_expr_statement_list_execute(&eval_ctx, module->exec);
		}
		catch(e)
		{
		    log_exception(e);
		}
		if ( eval_ctx.logdata == NULL )
		{ // dropped
		    //log_info("dropped");
		    appendline = FALSE;
		    ctx->tmpline = NULL;
		}
		else
		{
		    // TODO: merge fields
		}
		nx_expr_eval_ctx_destroy(&eval_ctx);
	    }

	    if ( appendline == TRUE )
	    { // not dropped
		size_t len;
		boolean gotheaderline = FALSE;
		boolean gotendline = FALSE;

		//log_info("appendline");
		// ignore trailing new line
		for ( len = ctx->tmpline->raw_event->len;
		      (len > 0) && ((ctx->tmpline->raw_event->buf[len - 1] == APR_ASCII_CR) ||
				    (ctx->tmpline->raw_event->buf[len - 1] == APR_ASCII_LF)); len--);
		// Check headerline
		if ( modconf->headerline != NULL )
		{
		    gotheaderline = match_line(modconf->headerline, 
					       ctx->tmpline->raw_event->buf, len);
		}

		// Check endline
		if ( modconf->endline != NULL )
		{
		    gotendline = match_line(modconf->endline,
					    ctx->tmpline->raw_event->buf, len);
		}

//		log_debug("gotheader: %d gotend: %d for [%s]", gotheaderline, gotendline,
//			  ctx->tmpline->raw_event->buf);

		if ( (gotendline == TRUE) && (gotheaderline == TRUE) )
		{
		    log_error("HeaderLine and Endline both match");
		}

		//log_info("gotheaderline: %d string: [%s]", gotheaderline, ctx->tmpline->raw_event->buf);
		if ( ctx->logdata == NULL )
		{
		    if ( (modconf->fixedlinecount <= 0) && (gotheaderline == FALSE) ) 
		    { // return if no header is found and there is no fixed linecount
			//log_info("return logdata: no header, no fixed linecount");
			retval = ctx->tmpline;
			ctx->tmpline = NULL;
			ctx->linecount = 0;
			done = TRUE;
		    }
		    else
		    {
			ctx->logdata = ctx->tmpline;
			ctx->tmpline = NULL;
			(ctx->linecount)++;
		    }
		}
		else
		{ // there is saved data
		    if ( gotendline == TRUE )
		    {
			nx_string_append(ctx->logdata->raw_event,
					 ctx->tmpline->raw_event->buf,
					 (int) ctx->tmpline->raw_event->len);
			nx_logdata_free(ctx->tmpline);
			ctx->tmpline = NULL;
			retval = ctx->logdata;
			ctx->logdata = NULL;
			done = TRUE;
		    }
		    else if ( gotheaderline == TRUE )
		    {
			//log_info("return logdata %lu: header ok", ctx->logdata);
			retval = ctx->logdata;
			ctx->logdata = ctx->tmpline;
			ctx->tmpline = NULL;
			(ctx->linecount)++;
			done = TRUE;
		    }
		    else
		    {
			nx_string_append(ctx->logdata->raw_event,
					 ctx->tmpline->raw_event->buf,
					 (int) ctx->tmpline->raw_event->len);
			nx_logdata_free(ctx->tmpline);
			ctx->tmpline = NULL;
			(ctx->linecount)++;
		    }
		}

		// Check FixedLineCount
		if ( modconf->fixedlinecount > 0 ) 
		{
		    if ( ctx->linecount >= modconf->fixedlinecount )
		    {
			//log_info("return logdata: fixed linecount");
			retval = ctx->logdata;
			ctx->logdata = NULL;
			ctx->linecount = 0;
			done = TRUE;
		    }
		}
	    }
	}
	input->buflen -= i;
	input->bufstart += i;
	//log_info("buflen: %d, foundcr: %d, i: %d, linecount: %d", input->buflen, foundcr, i, ctx->linecount);
	if ( input->buflen <= 0 )
	{
	    done = TRUE;
	}
    }
    while ( done != TRUE );

    if ( retval != NULL )
    {
	nx_string_strip_crlf(retval->raw_event);
    }

    return ( retval );
}
Beispiel #21
0
int
csl_execute(char *code, size_t size, const char *func_name, struct pack *pak, char **resptr, int *reslen)
{
	PyObject *pCode, *pModule, *pDict, *pFunc, *pValue, *pStr;
	PyObject *pArgs, *pkArgs;
	PyMethodDef *meth;
	node *n;

	pModule = PyImport_AddModule("__builtin__");
	pDict = PyModule_GetDict(pModule);
	for (meth = methods; meth->ml_name; meth++) {
		pCode = PyCFunction_New(meth, NULL);
		PyDict_SetItemString(pDict, meth->ml_name, pCode);
	}

	if (size == 0) {
		n = PyParser_SimpleParseString(code, Py_file_input);
		if (!n) {
			log_exception();
			return CSL_BADCODE;
		}
		pCode = (PyObject *) PyNode_Compile(n, "lala");
		PyNode_Free(n);
		if (!pCode) {
			log_exception();
			return CSL_BADCODE;
		}
	} else {
		pCode = PyMarshal_ReadObjectFromString(code, size);
		if (!pCode) {
			log_exception();
			return CSL_BADCODE;
		}
	}
	pModule = PyImport_ExecCodeModule("csl", pCode);
	Py_DECREF(pCode);

	if (!pModule || !PyModule_Check(pModule)) {
		return CSL_BADCODE;
	}

	pDict = PyModule_GetDict(pModule);
	if (!pDict) {
		Py_DECREF(pModule);
		return CSL_BADCODE;
	}

	pFunc = PyDict_GetItemString(pDict, func_name);
	if (!pFunc || !PyCallable_Check(pFunc)) {
		Py_DECREF(pModule);
		return CSL_NOFUNC;
	}

	pArgs = NULL;
	pkArgs = PyDict_New();
	while (pak) {
		PyObject *p;
		char *t, *t2;
		size_t sz;
		if (pack_get(pak, &t, &sz) == 0) break;
		if (pack_get(pak, &t2, &sz) == 0) {
			pArgs = PyTuple_New(1);
			PyTuple_SetItem(pArgs, 0, PyString_FromString(t));
			Py_DECREF(pkArgs);
			break;
		}
		p = PyString_FromStringAndSize(t2, sz);
		PyDict_SetItemString(pkArgs, t, p);
	}
	if (!pArgs) pArgs = PyTuple_New(0);

	pValue = PyObject_Call(pFunc, pArgs, pkArgs);
	if (!pValue) {
		log_exception();
		Py_DECREF(pModule);
		return CSL_FUNCERR;
	}

	pStr = PyObject_Str(pValue);

	Py_DECREF(pValue);
	Py_DECREF(pModule);

	// is return value asked?
	if (resptr == NULL) return 0;

	*reslen = PyString_Size(pStr);
	*resptr = malloc((*reslen) + 1);
	if (!*resptr) {
		Py_DECREF(pStr);
		return CSL_NOMEM;
	}
	memcpy(*resptr, PyString_AsString(pStr), *reslen);
	(*resptr)[*reslen] = '\0';

	return 0;
}