/** * Handle on/off directives. * * @param[in] cp Config parser * @param[in] name Directive name * @param[in] onoff on/off flag * @param[in] cbdata Callback data (ignored) * * @returns Status code */ static ib_status_t handle_directive_onoff(ib_cfgparser_t *cp, const char *name, int onoff, void *cbdata) { assert(cp != NULL); assert(name != NULL); assert(cp->ib != NULL); ib_engine_t *ib = cp->ib; ib_status_t rc; ib_module_t *module = NULL; modpcre_cfg_t *config = NULL; ib_context_t *ctx = cp->cur_ctx ? cp->cur_ctx : ib_context_main(ib); const char *pname; /* Get my module object */ rc = ib_engine_module_get(cp->ib, MODULE_NAME_STR, &module); if (rc != IB_OK) { ib_cfg_log_error(cp, "Failed to get %s module object: %s", MODULE_NAME_STR, ib_status_to_string(rc)); return rc; } /* Get my module configuration */ rc = ib_context_module_config(ctx, module, (void *)&config); if (rc != IB_OK) { ib_cfg_log_error(cp, "Failed to get %s module configuration: %s", MODULE_NAME_STR, ib_status_to_string(rc)); return rc; } if (strcasecmp("PcreStudy", name) == 0) { pname = MODULE_NAME_STR ".study"; } else if (strcasecmp("PcreUseJit", name) == 0) { pname = MODULE_NAME_STR ".use_jit"; } else { ib_cfg_log_error(cp, "Unhandled directive \"%s\"", name); return IB_EINVAL; } rc = ib_context_set_num(ctx, pname, onoff); if (rc != IB_OK) { ib_cfg_log_error(cp, "Failed to set \"%s\" to %s for \"%s\": %s", pname, onoff ? "true" : "false", name, ib_status_to_string(rc)); } return IB_OK; }
/** * @internal * Handle a PocSigTrace directive. * * @param cp Config parser * @param name Directive name * @param p1 First parameter * @param cbdata Callback data (from directive registration) * * @returns Status code */ static ib_status_t pocsig_dir_trace(ib_cfgparser_t *cp, const char *name, const char *p1, void *cbdata) { IB_FTRACE_INIT(pocsig_dir_trace); ib_engine_t *ib = cp->ib; ib_context_t *ctx = cp->cur_ctx ? cp->cur_ctx : ib_context_main(ib); ib_status_t rc; ib_log_debug(ib, 7, "%s: \"%s\" ctx=%p", name, p1, ctx); if (strcasecmp("On", p1) == 0) { rc = ib_context_set_num(ctx, MODULE_NAME_STR ".trace", 1); IB_FTRACE_RET_STATUS(rc); } else if (strcasecmp("Off", p1) == 0) { rc = ib_context_set_num(ctx, MODULE_NAME_STR ".trace", 0); IB_FTRACE_RET_STATUS(rc); } ib_log_error(ib, 1, "Failed to parse directive: %s \"%s\"", name, p1); IB_FTRACE_RET_STATUS(IB_EINVAL); }
/** * 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; }
/** * Handle single parameter directives. * * @param cp Config parser * @param name Directive name * @param p1 First parameter * @param cbdata Callback data (from directive registration) * * @returns Status code */ static ib_status_t handle_directive_param(ib_cfgparser_t *cp, const char *name, const char *p1, void *cbdata) { assert(cp != NULL); assert(name != NULL); assert(p1 != NULL); assert(cp->ib != NULL); ib_engine_t *ib = cp->ib; ib_status_t rc; ib_module_t *module = NULL; modpcre_cfg_t *config = NULL; ib_context_t *ctx = cp->cur_ctx ? cp->cur_ctx : ib_context_main(ib); const char *pname; ib_num_t value; /* Get my module object */ rc = ib_engine_module_get(cp->ib, MODULE_NAME_STR, &module); if (rc != IB_OK) { ib_cfg_log_error(cp, "Failed to get %s module object: %s", MODULE_NAME_STR, ib_status_to_string(rc)); return rc; } /* Get my module configuration */ rc = ib_context_module_config(ctx, module, (void *)&config); if (rc != IB_OK) { ib_cfg_log_error(cp, "Failed to get %s module configuration: %s", MODULE_NAME_STR, ib_status_to_string(rc)); return rc; } /* p1 should be a number */ rc = ib_string_to_num(p1, 0, &value); if (rc != IB_OK) { ib_cfg_log_error(cp, "Failed to convert \"%s\" to a number for \"%s\": %s", p1, name, ib_status_to_string(rc)); return rc; } if (strcasecmp("PcreMatchLimit", name) == 0) { pname = "pcre.match_limit"; } else if (strcasecmp("PcreMatchLimitRecursion", name) == 0) { pname = "pcre.match_limit_recursion"; } else if (strcasecmp("PcreJitStackStart", name) == 0) { pname = "pcre.jit_stack_start"; } else if (strcasecmp("PcreJitStackMax", name) == 0) { pname = "pcre.jit_stack_max"; } else if (strcasecmp("PcreDfaWorkspaceSize", name) == 0) { pname = "pcre.dfa_workspace_size"; } else { ib_cfg_log_error(cp, "Unhandled directive \"%s\"", name); return IB_EINVAL; } rc = ib_context_set_num(ctx, pname, value); if (rc != IB_OK) { ib_cfg_log_error(cp, "Failed to set \"%s\" to %ld for \"%s\": %s", pname, (long int)value, name, ib_status_to_string(rc)); } return IB_OK; }
/** * 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); }
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; }