/** * Create an instance of the @c ee_match_any operator. * * Looks up the automata name and adds the automata to the operator instance. * * @param[in] ctx Current context. * @param[in] parameters Automata name. * @param[out] instance_data Instance data. * @param[in] cbdata Callback data. */ static ib_status_t ee_match_any_operator_create( ib_context_t *ctx, const char *parameters, void *instance_data, void *cbdata ) { assert(ctx != NULL); assert(parameters != NULL); assert(instance_data != NULL); ib_status_t rc; ia_eudoxus_t* eudoxus; ee_operator_data_t *operator_data; ib_module_t *module; ib_engine_t *ib = ib_context_get_engine(ctx); ib_mpool_t *pool = ib_context_get_mpool(ctx); const ee_config_t *config = ee_get_config(ib); const ib_hash_t *eudoxus_pattern_hash; assert(config != NULL); assert(config->eudoxus_pattern_hash != NULL); /* Get my module object */ rc = ib_engine_module_get(ib, MODULE_NAME_STR, &module); if (rc != IB_OK) { ib_log_error(ib, "Failed to get eudoxus operator module object: %s", ib_status_to_string(rc)); return rc; } /* Allocate a rule data object, populate it */ operator_data = ib_mpool_alloc(pool, sizeof(*operator_data)); if (operator_data == NULL) { return IB_EALLOC; } eudoxus_pattern_hash = config->eudoxus_pattern_hash; rc = ib_hash_get(eudoxus_pattern_hash, &eudoxus, parameters); if (rc == IB_ENOENT ) { ib_log_error(ib, MODULE_NAME_STR ": No eudoxus automata named %s found.", parameters); return rc; } else if (rc != IB_OK) { ib_log_error(ib, MODULE_NAME_STR ": Error setting up eudoxus automata operator."); return rc; } operator_data->eudoxus = eudoxus; *(ee_operator_data_t **)instance_data = operator_data; ib_log_debug(ib, "Found compiled eudoxus pattern \"%s\"", parameters); return IB_OK; }
/** * Release resources when the module is unloaded. * * All eudoxus engines created by the LoadEudoxus directive are destroyed. * * @param[in] ib Ironbee engine. * @param[in] m Module instance. * @param[in] cbdata Not used. */ static ib_status_t ee_module_finish(ib_engine_t *ib, ib_module_t *m, void *cbdata) { ib_status_t rc; ia_eudoxus_t *eudoxus; ib_mpool_lite_t *pool; const ee_config_t *config = ee_get_config(ib); ib_hash_t *eudoxus_pattern_hash; ib_hash_iterator_t *iterator; if ( config == NULL || config->eudoxus_pattern_hash == NULL ) { return IB_OK; } eudoxus_pattern_hash = config->eudoxus_pattern_hash; rc = ib_mpool_lite_create(&pool); if (rc != IB_OK) { return rc; } iterator = ib_hash_iterator_create(ib_mm_mpool_lite(pool)); if (iterator == NULL) { ib_mpool_lite_destroy(pool); return IB_EALLOC; } for ( ib_hash_iterator_first(iterator, eudoxus_pattern_hash); ! ib_hash_iterator_at_end(iterator); ib_hash_iterator_next(iterator) ) { ib_hash_iterator_fetch(NULL, NULL, &eudoxus, iterator); if (eudoxus != NULL) { ia_eudoxus_destroy(eudoxus); } } ib_hash_clear(eudoxus_pattern_hash); ib_mpool_lite_destroy(pool); return IB_OK; }
/** * Initialize the eudoxus operator module. * * Registers the operators and the hash for storing the eudoxus engine * instances created by the LoadEudoxus directive. * * @param[in] ib Ironbee engine. * @param[in] m Module instance. * @param[in] cbdata Not used. */ static ib_status_t ee_module_init(ib_engine_t *ib, ib_module_t *m, void *cbdata) { ib_status_t rc; ib_mm_t mm; ee_config_t *config; mm = ib_engine_mm_main_get(ib); config = ee_get_config(ib); assert(config != NULL); if (config->eudoxus_pattern_hash == NULL) { rc = ib_hash_create_nocase(&(config->eudoxus_pattern_hash), mm); if (rc != IB_OK ) { return rc; } } rc = ib_operator_create_and_register( NULL, ib, "ee", IB_OP_CAPABILITY_CAPTURE, &ee_operator_create, NULL, NULL, NULL, &ee_operator_execute, m ); if (rc != IB_OK) { ib_log_error( ib, "Error registering ee operator: %s", ib_status_to_string(rc)); return rc; } rc = ib_operator_stream_create_and_register( NULL, ib, "ee", IB_OP_CAPABILITY_CAPTURE, &ee_operator_create, NULL, NULL, NULL, &ee_operator_execute_stream, m ); if (rc != IB_OK) { ib_log_error( ib, "Error registering ee stream operator: %s", ib_status_to_string(rc)); return rc; } rc = ib_operator_create_and_register( NULL, ib, "ee_match", IB_OP_CAPABILITY_CAPTURE, &ee_operator_create, NULL, NULL, NULL, &ee_match_operator_execute, m ); if (rc != IB_OK) { ib_log_error( ib, "Error registering ee operator: %s", ib_status_to_string(rc)); return rc; } rc = ib_hook_tx_register(ib, tx_finished_state, ee_tx_finished_handler, m); if (rc != IB_OK) { ib_log_error( ib, "Error registering transaction finished state for ee operator: %s", ib_status_to_string(rc)); return rc; } return IB_OK; }
/** * Load a eudoxus pattern so it can be used in rules. * * The filename should point to a compiled automata. If a relative path is * given, it will be loaded relative to the current configuration file. * * @param[in] cp Configuration parser. * @param[in] name Directive name. * @param[in] pattern_name Name to associate with the pattern. * @param[in] filename Filename to load. * @param[in] cbdata Callback data (unused) * @return * - IB_OK on success. * - IB_EEXIST if the pattern has already been defined. * - IB_EINVAL if there was an error loading the automata. */ static ib_status_t load_eudoxus_pattern_param2(ib_cfgparser_t *cp, const char *name, const char *pattern_name, const char *filename, void *cbdata) { ib_engine_t *ib; ib_status_t rc; const char *automata_file; ia_eudoxus_result_t ia_rc; ib_hash_t *eudoxus_pattern_hash; ia_eudoxus_t *eudoxus; const ee_config_t* config; ib_mm_t mm_tmp; void *tmp; assert(cp != NULL); assert(cp->ib != NULL); assert(pattern_name != NULL); assert(filename != NULL); mm_tmp = ib_engine_mm_temp_get(cp->ib); ib = cp->ib; config = ee_get_config(ib); assert(config != NULL); eudoxus_pattern_hash = config->eudoxus_pattern_hash; assert(eudoxus_pattern_hash != NULL); /* Check if the pattern name is already in use */ rc = ib_hash_get(eudoxus_pattern_hash, &tmp, pattern_name); if (rc == IB_OK) { ib_log_error(cp->ib, MODULE_NAME_STR ": Pattern named \"%s\" already defined", pattern_name); return IB_EEXIST; } automata_file = ib_util_relative_file(mm_tmp, cp->curr->file, filename); if (access(automata_file, R_OK) != 0) { ib_log_error(cp->ib, MODULE_NAME_STR ": Error accessing eudoxus automata file: %s.", automata_file); return IB_EINVAL; } ia_rc = ia_eudoxus_create_from_path(&eudoxus, automata_file); if (ia_rc != IA_EUDOXUS_OK) { ib_log_error(cp->ib, MODULE_NAME_STR ": Error loading eudoxus automata file[%d]: %s.", ia_rc, automata_file); return IB_EINVAL; } rc = ib_hash_set(eudoxus_pattern_hash, pattern_name, eudoxus); if (rc != IB_OK) { ia_eudoxus_destroy(eudoxus); return rc; } return IB_OK; }
/** * Initialize the eudoxus operator module. * * Registers the operators and the hash for storing the eudoxus engine * instances created by the LoadEudoxus directive. * * @param[in] ib Ironbee engine. * @param[in] m Module instance. * @param[in] cbdata Not used. */ static ib_status_t ee_module_init(ib_engine_t *ib, ib_module_t *m, void *cbdata) { ib_status_t rc; ib_mpool_t *main_mp; ib_mpool_t *mod_mp; ee_config_t *config; main_mp = ib_engine_pool_main_get(ib); config = ee_get_config(ib); assert(config != NULL); rc = ib_mpool_create(&mod_mp, "ee_module", main_mp); if (rc != IB_OK ) { ib_log_error(ib, MODULE_NAME_STR ": Error allocating module mpool."); return rc; } if (config->eudoxus_pattern_hash == NULL) { rc = ib_hash_create_nocase(&(config->eudoxus_pattern_hash), mod_mp); if (rc != IB_OK ) { ib_log_error(ib, MODULE_NAME_STR ": Error initializing module."); return rc; } } rc = ib_operator_create_and_register( NULL, ib, "ee_match_any", ( IB_OP_CAPABILITY_NON_STREAM | IB_OP_CAPABILITY_STREAM | IB_OP_CAPABILITY_CAPTURE ), &ee_match_any_operator_create, NULL, NULL, NULL, &ee_match_any_operator_execute, m ); if (rc != IB_OK) { ib_log_error( ib, "Failed to register ee_match_any operator: %s", ib_status_to_string(rc)); return rc; } rc = ib_hook_tx_register(ib, tx_finished_event, ee_tx_finished_handler, m); if (rc != IB_OK) { ib_log_error( ib, "Failed to register transaction finished event for ee_match_any operator: %s", ib_status_to_string(rc)); return rc; } return IB_OK; }