/** * Configuration function to select ident provider * Implements IdentType directive * * @param cp IronBee configuration parser * @param name Unused * @param p1 Select ident provider * @param p2 Optional. If set to "any", ident will be checked by all * available providers if the configured provider doesn't * identify. Expected to be used in "Log" mode. * @param dummy Unused * @return status */ static ib_status_t ident_type(ib_cfgparser_t *cp, const char *name, const char *p1, const char *p2, void *dummy) { ident_cfg_t *cfg; ib_status_t rc; ib_module_t *m; char *p; rc = ib_engine_module_get(cp->ib, MODULE_NAME_STR, &m); assert((rc == IB_OK) && (m != NULL)); rc = ib_context_module_config(ib_context_main(cp->ib), m, &cfg); assert((rc == IB_OK) && (cfg != NULL)); cfg->type = p = ib_mm_strdup(cp->mm, p1); assert(p != NULL); do { if (isupper(*p)) { *p = tolower(*p); } } while (*++p); if (p2 && !strcasecmp(p2, "any")) { cfg->accept_any = 1; } else { cfg->accept_any = 0; } return rc; }
TEST_F(TestIBUtilField, AliasBytestr) { const char s1[] = "hello"; const char s2[] = "bye"; ib_field_t *f; const ib_bytestr_t *obs; ib_status_t rc; ib_bytestr_t *bs; uint8_t *copy; copy = (uint8_t *)ib_mm_strdup(MM(), "x"); rc = ib_field_create_bytestr_alias(&f, MM(), IB_S2SL("foo"), copy, 0); ASSERT_EQ(IB_OK, rc); rc = ib_bytestr_dup_nulstr(&bs, MM(), s1); ASSERT_EQ(IB_OK, rc); rc = ib_field_setv(f, bs); ASSERT_EQ(IB_OK, rc); rc = ib_field_value(f, ib_ftype_bytestr_out(&obs)); ASSERT_EQ(IB_OK, rc); ASSERT_EQ(strlen(s1), ib_bytestr_length(obs)); ASSERT_EQ(0, memcmp(s1, ib_bytestr_const_ptr(obs), ib_bytestr_length(obs)) ); rc = ib_bytestr_dup_nulstr(&bs, MM(), s2); ASSERT_EQ(IB_OK, rc); rc = ib_field_setv(f, bs); ASSERT_EQ(IB_OK, rc); rc = ib_field_value(f, ib_ftype_bytestr_out(&obs)); ASSERT_EQ(IB_OK, rc); ASSERT_EQ(strlen(s2), ib_bytestr_length(obs)); ASSERT_EQ(0, memcmp(s2, ib_bytestr_const_ptr(obs), ib_bytestr_length(obs)) ); }
ib_status_t ib_dso_sym_name_find( const char **fname, const char **sname, ib_mm_t mm, void *addr ) { /* If we believe dladdr is implemented on this architecture, do this. */ #ifdef HAVE_DLADDR assert(fname != NULL); assert(sname != NULL); assert(addr != NULL); Dl_info dl_info; int sysrc; sysrc = dladdr(addr, &dl_info); if (sysrc == 0) { return IB_EOTHER; } if (dl_info.dli_fname != NULL) { const char *fname_tmp = ib_mm_strdup(mm, dl_info.dli_fname); if (fname_tmp == NULL) { return IB_EALLOC; } *fname = fname_tmp; } if (dl_info.dli_sname != NULL) { const char *sname_tmp = ib_mm_strdup(mm, dl_info.dli_sname); if (sname_tmp == NULL) { return IB_EALLOC; } *sname = sname_tmp; } return IB_OK; /* If we do not think dladdr is implemented, return [not implemented]. */ #else return IB_ENOTIMPL; #endif }
/// @test Test util field library - ib_field_create() ib_field_create() TEST_F(TestIBUtilField, test_field_create) { ib_field_t *f; ib_status_t rc; const char *nulstrval = "TestValue"; ib_num_t numval = 5; ib_bytestr_t *bytestrval; const char *nulout; const char *nulcopy; nulcopy = ib_mm_strdup(MM(), nulstrval); ASSERT_STRNE(NULL, nulcopy); rc = ib_field_create(&f, MM(), IB_S2SL("test_nulstr"), IB_FTYPE_NULSTR, ib_ftype_nulstr_in(nulcopy)); ASSERT_EQ(IB_OK, rc); ASSERT_TRUE(f); ASSERT_EQ(11UL, f->nlen); ASSERT_EQ(0, memcmp("test_nulstr", f->name, 11)); rc = ib_field_value(f, ib_ftype_nulstr_out(&nulout)); ASSERT_EQ(IB_OK, rc); ASSERT_STREQ(nulstrval, nulout); rc = ib_field_create(&f, MM(), IB_S2SL("test_num"), IB_FTYPE_NUM, ib_ftype_num_in(&numval)); ASSERT_EQ(IB_OK, rc); ASSERT_TRUE(f); ASSERT_EQ(8UL, f->nlen); ASSERT_EQ(0, memcmp("test_num", f->name, 8)); rc = ib_bytestr_dup_mem(&bytestrval, MM(), (uint8_t *)nulstrval, strlen(nulstrval)); ASSERT_EQ(IB_OK, rc); ASSERT_TRUE(f); rc = ib_field_create(&f, MM(), IB_S2SL("test_bytestr"), IB_FTYPE_BYTESTR, ib_ftype_bytestr_in(bytestrval)); ASSERT_EQ(IB_OK, rc); ASSERT_TRUE(f); ASSERT_EQ(12UL, f->nlen); ASSERT_EQ(0, memcmp("test_bytestr", f->name, 12)); rc = ib_field_create(&f, MM(), IB_S2SL("test_nulstr_ex"), IB_FTYPE_NULSTR, ib_ftype_nulstr_in(nulstrval)); ASSERT_EQ(IB_OK, rc); ASSERT_TRUE(f); rc = ib_field_create(&f, MM(), IB_S2SL("test_num_ex"), IB_FTYPE_NUM, ib_ftype_num_in(&numval)); ASSERT_EQ(IB_OK, rc); ASSERT_TRUE(f); rc = ib_field_create(&f, MM(), IB_S2SL("test_bytestr_ex"), IB_FTYPE_BYTESTR, ib_ftype_bytestr_in(bytestrval)); ASSERT_EQ(IB_OK, rc); ASSERT_TRUE(f); }
ib_status_t ib_flags_oplist_parse( const ib_strval_t *map, ib_mm_t mm, const char *str, const char *sep, ib_list_t *oplist) { if ( (map == NULL) || (str == NULL) || (sep == NULL) || (oplist == NULL) ) { return IB_EINVAL; } char *copy; const char *tmp; /* Make a copy of the string that we can use for strtok */ copy = ib_mm_strdup(mm, str); if (copy == NULL) { return IB_EALLOC; } /* Clear the list */ ib_list_clear(oplist); /* Walk through the separated list, parser each operator, build the list */ tmp = strtok(copy, sep); do { ib_status_t rc; ib_flags_op_t op; ib_flags_t flags; ib_flags_operation_t *operation; rc = parse_single(map, tmp, &op, &flags); if (rc != IB_OK) { return rc; } operation = ib_mm_alloc(mm, sizeof(*operation)); if (operation == NULL) { return IB_EALLOC; } operation->op = op; operation->flags = flags; rc = ib_list_push(oplist, operation); if (rc != IB_OK) { return rc; } } while ( (tmp = strtok(NULL, sep)) != NULL); return IB_OK; }
ib_status_t ib_flags_strtok( const ib_strval_t *map, ib_mm_t mm, const char *str, const char *sep, ib_flags_t *pflags, ib_flags_t *pmask) { if ( (map == NULL) || (str == NULL) || (sep == NULL) || (pflags == NULL) || (pmask == NULL) ) { return IB_EINVAL; } int n = 0; ib_flags_t flags = 0; ib_flags_t mask = 0; char *copy; const char *tmp; /* Make a copy of the string that we can use for strtok */ copy = ib_mm_strdup(mm, str); if (copy == NULL) { return IB_EALLOC; } /* Walk through the separated list */ tmp = strtok(copy, sep); do { ib_status_t rc; rc = ib_flags_string(map, tmp, n++, &flags, &mask); if (rc != IB_OK) { return rc; } } while ( (tmp = strtok(NULL, sep)) != NULL); /* Done */ *pflags = flags; *pmask = mask; return IB_OK; }
ib_status_t ib_action_create( ib_action_t **action, ib_mm_t mm, const char *name, ib_action_create_fn_t create_fn, void *create_cbdata, ib_action_destroy_fn_t destroy_fn, void *destroy_cbdata, ib_action_execute_fn_t execute_fn, void *execute_cbdata ) { assert(action != NULL); assert(name != NULL); ib_action_t *local_action; local_action = (ib_action_t *)ib_mm_alloc(mm, sizeof(*local_action)); if (local_action == NULL) { return IB_EALLOC; } local_action->name = ib_mm_strdup(mm, name); if (local_action->name == NULL) { return IB_EALLOC; } local_action->create_fn = create_fn; local_action->create_cbdata = create_cbdata; local_action->destroy_fn = destroy_fn; local_action->destroy_cbdata = destroy_cbdata; local_action->execute_fn = execute_fn; local_action->execute_cbdata = execute_cbdata; *action = local_action; return IB_OK; }
TEST_F(TestIBUtilField, AliasConvert) { char *str; ib_field_t *f1; ib_field_t *f2; ib_status_t rc; ib_num_t num; ib_float_t flt; /* * Convert numeric string to number */ /* Copy a number into the string */ str = ib_mm_strdup(MM(), "1"); ASSERT_TRUE(str != NULL); /* Create the aliased field */ rc = ib_field_create_alias(&f1, MM(), "one", 3, IB_FTYPE_NULSTR, ib_ftype_nulstr_mutable_out(&str)); ASSERT_EQ(IB_OK, rc); /* Attempt a numeric conversion. */ rc = ib_field_convert(MM(), IB_FTYPE_NUM, f1, &f2); ASSERT_EQ(IB_OK, rc); /* Pull out param value for check. */ rc = ib_field_value(f2, ib_ftype_num_out(&num)); ASSERT_EQ(IB_OK, rc); ASSERT_EQ(1, num); /* * Convert floating-point string to float */ /* Copy a number into the string */ str = ib_mm_strdup(MM(), "1.1"); ASSERT_TRUE(str != NULL); /* Create the aliased field */ rc = ib_field_create_alias(&f1, MM(), "one", 3, IB_FTYPE_NULSTR, ib_ftype_nulstr_mutable_out(&str)); ASSERT_EQ(IB_OK, rc); /* Attempt a numeric conversion. */ rc = ib_field_convert(MM(), IB_FTYPE_FLOAT, f1, &f2); ASSERT_EQ(IB_OK, rc); /* Pull out param value for check. */ rc = ib_field_value(f2, ib_ftype_float_out(&flt)); ASSERT_EQ(IB_OK, rc); ASSERT_FLOAT_EQ(1.1, flt); /* * Convert non-numeric string to number */ /* Copy a number into the string */ str = ib_mm_strdup(MM(), "x1"); ASSERT_TRUE(str != NULL); /* Create the aliased field */ rc = ib_field_create_alias(&f1, MM(), "one", 3, IB_FTYPE_NULSTR, ib_ftype_nulstr_mutable_out(&str)); ASSERT_EQ(IB_OK, rc); /* Attempt a numeric conversion. */ rc = ib_field_convert(MM(), IB_FTYPE_NUM, f1, &f2); ASSERT_EQ(IB_EINVAL, rc); /* * Convert floating-point string to number */ /* Copy a number into the string */ str = ib_mm_strdup(MM(), "1.1"); ASSERT_TRUE(str != NULL); /* Create the aliased field */ rc = ib_field_create_alias(&f1, MM(), "one", 3, IB_FTYPE_NULSTR, ib_ftype_nulstr_mutable_out(&str)); ASSERT_EQ(IB_OK, rc); /* Attempt a numeric conversion. */ rc = ib_field_convert(MM(), IB_FTYPE_NUM, f1, &f2); ASSERT_EQ(IB_EINVAL, rc); /* * Convert non-numeric string to float */ /* Copy a number into the string */ str = ib_mm_strdup(MM(), "1.1"); ASSERT_TRUE(str != NULL); /* Create the aliased field */ rc = ib_field_create_alias(&f1, MM(), "one", 3, IB_FTYPE_NULSTR, ib_ftype_nulstr_mutable_out(&str)); ASSERT_EQ(IB_OK, rc); /* Attempt a numeric conversion. */ rc = ib_field_convert(MM(), IB_FTYPE_FLOAT, f1, &f2); ASSERT_EQ(IB_OK, rc); /* Pull out param value for check. */ rc = ib_field_value(f2, ib_ftype_float_out(&flt)); ASSERT_EQ(IB_OK, rc); ASSERT_FLOAT_EQ(1.1, flt); }
ib_status_t ib_action_inst_create( ib_action_inst_t **act_inst, ib_mm_t mm, ib_context_t *ctx, const char *action_name, const char *parameters ) { assert(action_name != NULL); assert(act_inst != NULL); assert(ctx != NULL); assert(ctx->ib != NULL); const ib_action_t *action; ib_action_inst_t *local_action_inst = NULL; ib_status_t rc; rc = ib_action_lookup(ctx->ib, action_name, strlen(action_name), &action); if (rc != IB_OK) { return rc; } local_action_inst = (ib_action_inst_t *)ib_mm_alloc(mm, sizeof(*local_action_inst)); if (local_action_inst == NULL) { return IB_EALLOC; } if (parameters != NULL) { local_action_inst->parameters = ib_mm_strdup(mm, parameters); if (local_action_inst->parameters == NULL) { return IB_EALLOC; } } else { local_action_inst->parameters = NULL; } local_action_inst->action = action; if (action->create_fn == NULL) { local_action_inst->instance_data = NULL; } else { rc = action->create_fn( mm, ctx, parameters, &(local_action_inst->instance_data), action->create_cbdata ); if (rc != IB_OK) { return rc; } } if (action->destroy_fn != NULL) { /* Register the destroy function. */ rc = ib_mm_register_cleanup(mm, cleanup_action, local_action_inst); if (rc != IB_OK) { return rc; } } *act_inst = local_action_inst; return IB_OK; }
static ib_status_t sqli_dir_fingerprint_set( ib_cfgparser_t *cp, const char *directive_name, const char *set_name, const char *set_path, void *cbdata ) { assert(cp != NULL); assert(directive_name != NULL); assert(set_name != NULL); assert(set_path != NULL); ib_status_t rc; ib_context_t *ctx = NULL; ib_module_t *m = NULL; sqli_module_config_t *cfg = NULL; sqli_fingerprint_set_t *ps = NULL; ib_mm_t mm; char *abs_set_path = NULL; rc = ib_cfgparser_context_current(cp, &ctx); assert(rc == IB_OK); assert(ctx != NULL); if (ctx != ib_context_main(cp->ib)) { ib_cfg_log_error(cp, "%s: Only valid at main context.", directive_name ); return IB_EINVAL; } if (strcmp("default", set_name) == 0) { ib_cfg_log_error(cp, "%s: default is a reserved set name.", directive_name ); return IB_EINVAL; } mm = ib_engine_mm_main_get(cp->ib); rc = ib_engine_module_get( ib_context_get_engine(ctx), MODULE_NAME_STR, &m ); assert(rc == IB_OK); rc = ib_context_module_config(ctx, m, &cfg); assert(rc == IB_OK); if (cfg->fingerprint_sets == NULL) { rc = ib_hash_create(&cfg->fingerprint_sets, mm); assert(rc == IB_OK); } assert(cfg->fingerprint_sets != NULL); rc = ib_hash_get(cfg->fingerprint_sets, NULL, set_name); if (rc == IB_OK) { ib_cfg_log_error(cp, "%s: Duplicate fingerprint set definition: %s", directive_name, set_name ); return IB_EINVAL; } assert(rc == IB_ENOENT); abs_set_path = ib_util_relative_file( ib_engine_mm_config_get(cp->ib), ib_cfgparser_curr_file(cp), set_path ); if (abs_set_path == NULL) { return IB_EALLOC; } rc = sqli_create_fingerprint_set_from_file(&ps, abs_set_path, mm); if (rc != IB_OK) { ib_cfg_log_error(cp, "%s: Failure to load fingerprint set from file: %s", directive_name, abs_set_path ); return IB_EINVAL; } assert(ps != NULL); rc = ib_hash_set(cfg->fingerprint_sets, ib_mm_strdup(mm, set_name), ps); assert(rc == IB_OK); return IB_OK; }
/********************************* * Helper Functions *********************************/ static ib_status_t sqli_create_fingerprint_set_from_file( sqli_fingerprint_set_t **out_ps, const char *path, ib_mm_t mm ) { assert(out_ps != NULL); assert(path != NULL); ib_status_t rc; FILE *fp = NULL; char *buffer = NULL; size_t buffer_size = 0; ib_list_t *items = NULL; ib_list_node_t *n = NULL; ib_mpool_lite_t *tmp = NULL; ib_mm_t tmp_mm; sqli_fingerprint_set_t *ps = NULL; size_t i = 0; /* Temporary memory pool for this function only. */ rc = ib_mpool_lite_create(&tmp); assert(rc == IB_OK); assert(tmp != NULL); tmp_mm = ib_mm_mpool_lite(tmp); fp = fopen(path, "r"); if (fp == NULL) { goto fail; } rc = ib_list_create(&items, tmp_mm); assert(rc == IB_OK); assert(items != NULL); for (;;) { char *buffer_copy; int read = getline(&buffer, &buffer_size, fp); char *space = NULL; ib_num_t confidence = 0; sqli_fingerprint_entry_t *entry = ib_mm_alloc(tmp_mm, sizeof(*entry)); if (read == -1) { if (! feof(fp)) { fclose(fp); goto fail; } else { break; } } while (buffer[read-1] == '\n' || buffer[read-1] == '\r') { buffer[read-1] = '\0'; --read; } space = strstr(buffer, " "); if (space != NULL) { rc = ib_type_atoi(space + 1, 10, &confidence); if (rc != IB_OK || confidence > 100) { return IB_EINVAL; } *space = '\0'; } buffer_copy = ib_mm_strdup(mm, buffer); assert(buffer_copy != NULL); entry->confidence = confidence; entry->fingerprint = buffer_copy; rc = ib_list_push(items, (void *)entry); assert(rc == IB_OK); } fclose(fp); ps = ib_mm_alloc(mm, sizeof(*ps)); assert(ps != NULL); ps->num_fingerprints = ib_list_elements(items); ps->fingerprints = ib_mm_alloc(mm, ps->num_fingerprints * sizeof(*ps->fingerprints)); assert(ps->fingerprints != NULL); i = 0; IB_LIST_LOOP(items, n) { const sqli_fingerprint_entry_t *entry = (const sqli_fingerprint_entry_t *)ib_list_node_data(n); ps->fingerprints[i] = *entry; ++i; } assert(i == ps->num_fingerprints); ib_mpool_lite_destroy(tmp); qsort( ps->fingerprints, ps->num_fingerprints, sizeof(*ps->fingerprints), &sqli_cmp ); *out_ps = ps; return IB_OK; fail: ib_mpool_lite_destroy(tmp); return IB_EINVAL; }