示例#1
0
/** Create and return top-level cont with no transient data
 *  Sets up engine manager and kill-or-continue txn hook before launching
 *  potentially-slow mainconfiguration in separate thread.
 */
static ib_status_t tsib_pre_init(TSCont *contp)
{
    int rv;
    ib_status_t rc;
    TSCont cont;

    assert(contp != NULL);

    /* create a cont to fend off traffic while we read config */
    *contp = cont = TSContCreate(ironbee_plugin, NULL);
    if (cont == NULL) {
        TSError("[ironbee] failed to create initial continuation: disabled");
        return IB_EUNKNOWN;
    }
    if (module_data.allow_at_startup) {
        /* SSN_START doesn't use contdata; READ_REQUEST_HDR only needs non-null flag.
         * Using &module_data might let us clean up some tsib_api stuff in future.
         */
        TSContDataSet(cont, &module_data);
    }
    else {
        /* NULL contdata signals the READ_REQUEST_HDR hook to reject requests */
        TSContDataSet(cont, NULL);
    }
    TSHttpHookAdd(TS_HTTP_READ_REQUEST_HDR_HOOK, cont);

    if (!module_data.log_disable) {
        /* success is documented as TS_LOG_ERROR_NO_ERROR but that's undefined.
         * It's actually a TS_SUCCESS (proxy/InkAPI.cc line 6641).
         */
        printf("Logging to \"%s\"\n", module_data.log_file);
        rv = TSTextLogObjectCreate(module_data.log_file,
                                   TS_LOG_MODE_ADD_TIMESTAMP,
                                   &module_data.logger);
        if (rv != TS_SUCCESS) {
            TSError("[ironbee] Error creating log file.");
            return IB_EUNKNOWN;
        }
    }

    /* Initialize IronBee (including util) */
    rc = ib_initialize();
    if (rc != IB_OK) {
        TSError("[ironbee] Error initializing IronBee: %s",
                ib_status_to_string(rc));
        return rc;
    }

    /* Create the IronBee engine manager */
    TSDebug("ironbee", "Creating IronBee engine manager");
    rc = ib_manager_create(&(module_data.manager),   /* Engine Manager */
                           &ibplugin,                /* Server object */
                           module_data.max_engines); /* Default max */
    if (rc != IB_OK) {
        TSError("[ironbee] Error creating IronBee engine manager: %s",
                ib_status_to_string(rc));
    }
    return rc;
}
示例#2
0
/// @test Test ironbee library - ib_engine_create()
TEST(TestIronBee, test_engine_create_null_server)
{
    ib_engine_t *ib;
    ib_status_t rc;

    rc = ib_initialize();
    ASSERT_EQ(IB_OK, rc);

    rc = ib_engine_create(&ib, NULL);
    ASSERT_EQ(IB_EINVAL, rc);
    ASSERT_FALSE(ib);

    ib_shutdown();
}
示例#3
0
/**
 * Initializes and configures the ironbee engine.
 */
static int ironbee_post_config(apr_pool_t *p, apr_pool_t *plog, apr_pool_t *ptmp,
                             server_rec *s)
{
    ironbee_config_t *modcfg =
        (ironbee_config_t *)ap_get_module_config(s->module_config,
                                                 &ironbee_module);
    ib_cfgparser_t *cp;
    ib_provider_t *lpr;
    void *init = NULL;
    ib_status_t rc;


    /* Init IB library. */
    rc = ib_initialize();
    if (rc != IB_OK) {
        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
                     IB_PRODUCT_NAME ": Error initializing ib library");
        return OK;
    }

    ib_util_log_level(4);

    /* Detect first (validation) run vs real config run. */
    apr_pool_userdata_get(&init, "ironbee-init", s->process->pool);
    if (init == NULL) {
        ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
                     MODULE_NAME_FULL " loading.");

        apr_pool_userdata_set((const void *)1, "ironbee-init",
                              apr_pool_cleanup_null, s->process->pool);

        return OK;
    }

    /// @todo Tracefile needs removed
    //ib_trace_init("/tmp/ironbee.trace");
    ib_trace_init(NULL);

    /* Create the engine handle. */
    rc = ib_engine_create(&ironbee, &ibplugin);
    if (rc != IB_OK) {
        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
                     IB_PRODUCT_NAME ": Error creating engine: %d", rc);
        return OK;
    }

    /* Register the logger. */
    rc = ib_provider_register(ironbee, IB_PROVIDER_TYPE_LOGGER,
                              MODULE_NAME_STR, &lpr,
                              &ironbee_logger_iface,
                              NULL);
    if (rc != IB_OK) {
        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
                     IB_PRODUCT_NAME ": Error registering log provider: %d", rc);
        return OK;
    }
    ib_provider_data_set(lpr, (void *)s);

    /* Default logger */
    /// @todo Need to add a post set hook in core for this to work correctly
    ib_context_set_string(ib_context_engine(ironbee),
                          IB_PROVIDER_TYPE_LOGGER,
                          MODULE_NAME_STR);
    ib_context_set_num(ib_context_engine(ironbee),
                       IB_PROVIDER_TYPE_LOGGER ".log_level",
                       4);


    rc = ib_engine_init(ironbee);
    if (rc != IB_OK) {
        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
                     IB_PRODUCT_NAME ": Error initializing engine: %d", rc);
        return OK;
    }

    /* Register module cleanup. */
    apr_pool_cleanup_register(p, (void *)s, ironbee_module_cleanup,
                              apr_pool_cleanup_null);

    /* Register conn/tx init hooks. */
    ib_hook_register(ironbee, conn_opened_event,
                     (ib_void_fn_t)ironbee_conn_init, s);

    /* Configure the engine. */
    if (modcfg->config != NULL) {
        ib_context_t *ctx;

        /* Notify the engine that the config process has started. This
         * will also create a main configuration context.
         */
        ib_state_notify_cfg_started(ironbee);

        /* Get the main configuration context. */
        ctx = ib_context_main(ironbee);

        /* Set some defaults */
        ib_context_set_string(ctx, IB_PROVIDER_TYPE_LOGGER, MODULE_NAME_STR);
        ib_context_set_num(ctx, "logger.log_level", 4);

        /* Parse the config file. */
        rc = ib_cfgparser_create(&cp, ironbee);
        if ((rc == IB_OK) && (cp != NULL)) {
            ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
                         IB_PRODUCT_NAME ": Parsing config: %s",
                         modcfg->config);
            ib_cfgparser_parse(cp, modcfg->config);
            ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
                         IB_PRODUCT_NAME ": Destroying config parser");
            ib_cfgparser_destroy(cp);
        }

        /* Notify the engine that the config process has finished. This
         * will also close out the main configuration context.
         */
        ib_state_notify_cfg_finished(ironbee);
    }
    else {
        ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, s,
                     IB_PRODUCT_NAME ": No config specified with IronBeeConfig directive");
    }

    ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s,
                 MODULE_NAME_FULL " configured.");

    return OK;
}
示例#4
0
 TestIBUtilField()
 {
     ib_initialize();
 }
示例#5
0
/**
 * Ironbee initialisation function.  Sets up engine and logging,
 * and reads Ironbee config.
 *
 * @param[in]  cf     Configuration rec
 * @return     NGX_OK or error
 */
static ngx_int_t ironbee_init(ngx_conf_t *cf)
{
    ngx_log_t *prev_log;
    ib_context_t *ctx;
    ib_cfgparser_t *cp;
    ironbee_proc_t *proc;
    ib_status_t rc, rc1;

    prev_log = ngxib_log(cf->log);
    ngx_regex_malloc_init(cf->pool);

    ngx_log_error(NGX_LOG_NOTICE, cf->log, 0, "ironbee_init %d", getpid());

    proc = ngx_http_conf_get_module_main_conf(cf, ngx_ironbee_module);
    if (proc->loglevel == NGX_CONF_UNSET_UINT)
        proc->loglevel = 4; /* default */

    rc = ib_initialize();
    if (rc != IB_OK)
        cleanup_return(prev_log) IB2NG(rc);

    ib_util_log_level(proc->loglevel);

    rc = ib_engine_create(&ironbee, ngxib_server());
    if (rc != IB_OK)
        cleanup_return(prev_log) IB2NG(rc);

    if (proc->use_ngxib_logger)
        ib_log_set_logger_fn(ironbee, ngxib_logger, NULL);
    /* Using default log level function. */

    rc = ib_engine_init(ironbee);
    if (rc != IB_OK)
        cleanup_return(prev_log) IB2NG(rc);

    /* TODO: TS creates logfile at this point */

    ib_hook_conn_register(ironbee, conn_opened_event, ngxib_conn_init, NULL);

    rc = ib_cfgparser_create(&cp, ironbee);
    assert((cp != NULL) || (rc != IB_OK));
    if (rc != IB_OK)
        cleanup_return(prev_log) IB2NG(rc);

    rc = ib_engine_config_started(ironbee, cp);
    if (rc != IB_OK)
        cleanup_return(prev_log) IB2NG(rc);

    /* Get the main context, set some defaults */
    ctx = ib_context_main(ironbee);
    ib_context_set_num(ctx, "logger.log_level", proc->loglevel);

    /* FIXME - use the temp pool operation for this */
    char *buf = strndup((char*)proc->config_file.data, proc->config_file.len);
    rc = ib_cfgparser_parse(cp, buf);
    free(buf);
    rc1 = ib_engine_config_finished(ironbee);
    ib_cfgparser_destroy(cp);

    cleanup_return(prev_log) rc == IB_OK ? rc1 == IB_OK ? NGX_OK : IB2NG(rc1) : IB2NG(rc);
}
示例#6
0
文件: ironbee.c 项目: igalic/ironbee
static int ironbee_init(const char *configfile, const char *logfile)
{
  /* grab from httpd module's post-config */
  ib_status_t rc;
//  ib_provider_t *lpr;
  ib_cfgparser_t *cp;
  ib_context_t *ctx;
  int rv;

  rc = ib_initialize();
  if (rc != IB_OK) {
    return rc;
  }

  ib_util_log_level(9);

  ib_trace_init(TRACEFILE);

  rc = ib_engine_create(&ironbee, &ibplugin);
  if (rc != IB_OK) {
    return rc;
  }

  rc = ib_provider_register(ironbee, IB_PROVIDER_TYPE_LOGGER, "ironbee-ts",
                            NULL, &ironbee_logger_iface, NULL);
  if (rc != IB_OK) {
    return rc;
  }

  ib_context_set_string(ib_context_engine(ironbee),
                        IB_PROVIDER_TYPE_LOGGER, "ironbee-ts");
  ib_context_set_num(ib_context_engine(ironbee),
                     IB_PROVIDER_TYPE_LOGGER ".log_level", 4);

  rc = ib_engine_init(ironbee);
  if (rc != IB_OK) {
    return rc;
  }

  /* success is documented as TS_LOG_ERROR_NO_ERROR but that's undefined.
   * It's actually a TS_SUCCESS (proxy/InkAPI.cc line 6641).
   */
  rv = TSTextLogObjectCreate(logfile, TS_LOG_MODE_ADD_TIMESTAMP, &ironbee_log);
  if (rv != TS_SUCCESS) {
    return IB_OK + rv;
  }
   
  rc = atexit(ibexit);
  if (rc != 0) {
    return IB_OK + rv;
  }

  ib_hook_register(ironbee, conn_opened_event,
                   (ib_void_fn_t)ironbee_conn_init, NULL);


  ib_state_notify_cfg_started(ironbee);
  ctx = ib_context_main(ironbee);

  ib_context_set_string(ctx, IB_PROVIDER_TYPE_LOGGER, "ironbee-ts");
  ib_context_set_num(ctx, "logger.log_level", 4);

  rc = ib_cfgparser_create(&cp, ironbee);
  if (rc != IB_OK) {
    return rc;
  }
  if (cp != NULL) {  // huh?
    ib_cfgparser_parse(cp, configfile);
    ib_cfgparser_destroy(cp);
  }
  ib_state_notify_cfg_finished(ironbee);


  return IB_OK;
}
示例#7
0
/**
 * IronBee initialization function.  Sets up engine and logging,
 * and reads IronBee config.
 *
 * @param[in]  cf     Configuration rec
 * @return     NGX_OK or error
 */
static ngx_int_t ironbee_init(ngx_conf_t *cf)
{
    ironbee_proc_t *proc;
    ib_status_t rc;
    module_data_t *mod_data = &module_data;
    char *buf;

    /* We still use the global-log hack to initialise */
    ngx_regex_malloc_init(cf->pool);

    ngx_log_error(NGX_LOG_NOTICE, cf->log, 0, "ironbee_init %d", getpid());

    proc = ngx_http_conf_get_module_main_conf(cf, ngx_ironbee_module);
    if (proc->log_level == NGX_CONF_UNSET_UINT) {
        proc->log_level = IB_LOG_NOTICE;
    }
    if (proc->max_engines == NGX_CONF_UNSET_UINT) {
        proc->max_engines = IB_MANAGER_DEFAULT_MAX_ENGINES;
    }
    if (proc->use_ngxib_logger == NGX_CONF_UNSET) {
        proc->use_ngxib_logger =1;
    }

    /* initialise fields in mod_data */
    mod_data->ib_log_active = proc->use_ngxib_logger;
    mod_data->log = cf->log;
    mod_data->log_level = proc->log_level;

    rc = ib_initialize();
    if (rc != IB_OK) {
        cleanup_return IB2NG(rc);
    }

    /* Create the IronBee engine manager */
    rc = ib_manager_create(&(mod_data->manager),  /* Engine Manager */
                           ngxib_server(),        /* Server object */
                           proc->max_engines);    /* Max engines */
    if (rc != IB_OK) {
        cleanup_return IB2NG(rc);
    }

    rc = ib_manager_register_module_fn(
        mod_data->manager,
        ngxib_module,
        mod_data);
    if (rc != IB_OK) {
        cleanup_return IB2NG(rc);
    }

    /* Null manager here would be a bug, as per RNS-CR-143 comments */
    assert(mod_data->manager != NULL);

    /* FIXME - use the temp pool operation for this */
    buf = strndup((char*)proc->config_file.data, proc->config_file.len);

    /* Create the initial engine */
    rc = ib_manager_engine_create(mod_data->manager, buf);
    if (rc != IB_OK) {
        free(buf);
        cleanup_return IB2NG(rc);
    }
    free(buf);

    cleanup_return rc == IB_OK ? NGX_OK : IB2NG(rc);
}