TEST_F(XRulesTest, SetFlag) { ib_field_t *field; ib_num_t num; ib_var_target_t *target; const ib_list_t *list; std::string config = std::string( "LogLevel DEBUG\n" "LoadModule \"ibmod_persistence_framework.so\"\n" "LoadModule \"ibmod_init_collection.so\"\n" "LoadModule \"ibmod_xrules.so\"\n" "InitCollection GeoIP vars: country_code=US\n" "SensorId B9C1B52B-C24A-4309-B9F9-0EF4CD577A3E\n" "SensorName UnitTesting\n" "SensorHostname unit-testing.sensor.tld\n" /* Note that both rules should fire and result in a single entry. */ "XRulePath / EnableRequestParamInspection priority=1\n" "XRuleGeo US EnableRequestParamInspection priority=1\n" "<Site test-site>\n" " SiteId AAAABBBB-1111-2222-3333-000000000000\n" " Hostname somesite.com\n" "</Site>\n" ); configureIronBeeByString(config.c_str()); performTx(); ASSERT_TRUE(ib_tx); ASSERT_TRUE(ib_tx->flags & IB_TX_FINSPECT_REQPARAMS); ASSERT_EQ( IB_OK, ib_var_target_acquire_from_string( &target, ib_tx->mp, ib_var_store_config(ib_tx->var_store), "FLAGS:inspectRequestParams", strlen("FLAGS:inspectRequestParams"), NULL, NULL) ); ASSERT_EQ( IB_OK, ib_var_target_get_const( target, &list, ib_tx->mp, ib_tx->var_store) ); ASSERT_EQ(1U, ib_list_elements(list)); field = (ib_field_t *)ib_list_node_data_const(ib_list_first_const(list)); ASSERT_EQ(IB_FTYPE_NUM, field->type); ASSERT_EQ( IB_OK, ib_field_value(field, ib_ftype_num_out(&num)) ); ASSERT_EQ(1, num); }
//! Increment iterator. void increment() { if (! m_past_the_end) { if (m_before_the_beginning) { m_node = ib_list_first_const(m_list); m_before_the_beginning = false; } else if (m_node->next) { m_node = m_node->next; } else { m_node = NULL; m_past_the_end = true; } } }
/** * Join a field with strings before and after it * * @param[in] mp Memory pool * @param[in] f Field to join * @param[in] iptr Pointer to initial string * @param[in] ilen Length of @a iptr * @param[in] fptr Pointer to final string * @param[in] flen Length of @a fptr * @param[in] nul true if NUL byte should be tacked on, false if not * @param[out] out Pointer to output block * @param[out] olen Length of the output block * * @returns status code */ static ib_status_t join_parts(ib_mpool_t *mp, const ib_field_t *f, const char *iptr, size_t ilen, const char *fptr, size_t flen, bool nul, char **out, size_t *olen) { IB_FTRACE_INIT(); ib_status_t rc; char numbuf[NUM_BUF_LEN+1]; /* Buffer used to convert number to str */ assert(NUM_BUF_LEN <= 256); switch(f->type) { case IB_FTYPE_NULSTR: { /* Field is a NUL-terminated string */ const char *s; rc = ib_field_value(f, ib_ftype_nulstr_out(&s)); if (rc != IB_OK) { IB_FTRACE_RET_STATUS(rc); } size_t slen = strlen(s); rc = join3(mp, iptr, ilen, s, slen, fptr, flen, nul, out, olen); break; } case IB_FTYPE_BYTESTR: { /* Field is a byte string */ const ib_bytestr_t *bs; rc = ib_field_value(f, ib_ftype_bytestr_out(&bs)); if (rc != IB_OK) { IB_FTRACE_RET_STATUS(rc); } rc = join3(mp, iptr, ilen, (const char *)ib_bytestr_const_ptr(bs), ib_bytestr_length(bs), fptr, flen, nul, out, olen); break; } case IB_FTYPE_NUM: { /* Field is a number; convert it to a string */ ib_num_t n; rc = ib_field_value(f, ib_ftype_num_out(&n)); if (rc != IB_OK) { IB_FTRACE_RET_STATUS(rc); } snprintf(numbuf, NUM_BUF_LEN, "%"PRId64, n); rc = join3(mp, iptr, ilen, numbuf, strlen(numbuf), fptr, flen, nul, out, olen); break; } case IB_FTYPE_UNUM: { /* Field is an unsigned number; convert it to a string */ ib_unum_t n; rc = ib_field_value(f, ib_ftype_unum_out(&n)); if (rc != IB_OK) { IB_FTRACE_RET_STATUS(rc); } snprintf(numbuf, NUM_BUF_LEN, "%"PRIu64, n); rc = join3(mp, iptr, ilen, numbuf, strlen(numbuf), fptr, flen, nul, out, olen); break; } case IB_FTYPE_LIST: { /* Field is a list: use the first element in the list */ const ib_list_t *list; const ib_list_node_t *node; const ib_field_t *element; rc = ib_field_value(f, ib_ftype_list_out(&list)); if (rc != IB_OK) { IB_FTRACE_RET_STATUS(rc); } node = ib_list_first_const(list); if (node == NULL) { rc = join2(mp, iptr, ilen, fptr, flen, nul, out, olen); break; } element = (const ib_field_t *)ib_list_node_data_const(node); rc = join_parts(mp, element, iptr, ilen, fptr, flen, nul, out, olen); break; } default: /* Something else: replace with "" */ rc = join2(mp, iptr, ilen, fptr, flen, nul, out, olen); break; } IB_FTRACE_RET_STATUS(rc); }
TEST_F(TestIBUtilLogformat, test_parse_default) { ib_status_t rc; ib_logformat_t *lf = NULL; const ib_list_node_t *node; const ib_logformat_item_t *item; static char linebuf[buflen + 1]; static const char *formatted = \ TIME_STAMP " " HOST_NAME " " REMOTE_IP " " SENSOR_ID " " SITE_ID " " \ TX_ID " " LOG_FILE; size_t len; rc = ib_logformat_create(MM(), &lf); ASSERT_EQ(IB_OK, rc); rc = ib_logformat_parse(lf, IB_LOGFORMAT_DEFAULT); ASSERT_EQ(IB_OK, rc); ASSERT_STREQ(IB_LOGFORMAT_DEFAULT, lf->format); ASSERT_EQ(13U, ib_list_elements(lf->items)); node = ib_list_first_const(lf->items); ASSERT_TRUE(node); item = (const ib_logformat_item_t *)node->data; ASSERT_EQ(item_type_format, item->itype); ASSERT_EQ('T', item->item.field.fchar); node = ib_list_node_next_const(node); ASSERT_TRUE(node); item = (const ib_logformat_item_t *)node->data; ASSERT_EQ(item_type_literal, item->itype); ASSERT_STREQ(" ", item->item.literal.buf.short_str); node = ib_list_node_next_const(node); ASSERT_TRUE(node); item = (const ib_logformat_item_t *)node->data; ASSERT_EQ(item_type_format, item->itype); ASSERT_EQ('h', item->item.field.fchar); node = ib_list_node_next_const(node); ASSERT_TRUE(node); item = (const ib_logformat_item_t *)node->data; ASSERT_EQ(item_type_literal, item->itype); ASSERT_STREQ(" ", item->item.literal.buf.short_str); node = ib_list_node_next_const(node); ASSERT_TRUE(node); item = (const ib_logformat_item_t *)node->data; ASSERT_EQ(item_type_format, item->itype); ASSERT_EQ('a', item->item.field.fchar); node = ib_list_node_next_const(node); ASSERT_TRUE(node); item = (const ib_logformat_item_t *)node->data; ASSERT_EQ(item_type_literal, item->itype); ASSERT_STREQ(" ", item->item.literal.buf.short_str); node = ib_list_node_next_const(node); ASSERT_TRUE(node); item = (const ib_logformat_item_t *)node->data; ASSERT_EQ(item_type_format, item->itype); ASSERT_EQ('S', item->item.field.fchar); node = ib_list_node_next_const(node); ASSERT_TRUE(node); item = (const ib_logformat_item_t *)node->data; ASSERT_EQ(item_type_literal, item->itype); ASSERT_STREQ(" ", item->item.literal.buf.short_str); node = ib_list_node_next_const(node); ASSERT_TRUE(node); item = (const ib_logformat_item_t *)node->data; ASSERT_EQ(item_type_format, item->itype); ASSERT_EQ('s', item->item.field.fchar); node = ib_list_node_next_const(node); ASSERT_TRUE(node); item = (const ib_logformat_item_t *)node->data; ASSERT_EQ(item_type_literal, item->itype); ASSERT_STREQ(" ", item->item.literal.buf.short_str); node = ib_list_node_next_const(node); ASSERT_TRUE(node); item = (const ib_logformat_item_t *)node->data; ASSERT_EQ(item_type_format, item->itype); ASSERT_EQ('t', item->item.field.fchar); node = ib_list_node_next_const(node); ASSERT_TRUE(node); item = (const ib_logformat_item_t *)node->data; ASSERT_EQ(item_type_literal, item->itype); ASSERT_STREQ(" ", item->item.literal.buf.short_str); node = ib_list_node_next_const(node); ASSERT_TRUE(node); item = (const ib_logformat_item_t *)node->data; ASSERT_EQ(item_type_format, item->itype); ASSERT_EQ('f', item->item.field.fchar); rc = ib_logformat_format(lf, linebuf, buflen, &len, format_field, NULL); ASSERT_EQ(IB_OK, rc); ASSERT_STREQ(formatted, linebuf); /* Verify that truncation is handled correctly */ rc = ib_logformat_format(lf, linebuf, trunclen, &len, format_field, NULL); ASSERT_EQ(IB_ETRUNC, rc); ASSERT_EQ(len, trunclen-1); char trunc_buf[trunclen]; strncpy(trunc_buf, formatted, trunclen-1); trunc_buf[trunclen-1] = '\0'; ASSERT_STREQ(trunc_buf, linebuf); }
/** * Handle managed collection: register for JSON file * * Examines the incoming parameters; if if it looks like a JSON file, * take it; otherwise do nothing (decline) * * @param[in] ib Engine * @param[in] module Collection manager's module object * @param[in] mp Memory pool to use for allocations * @param[in] collection_name Name of the collection * @param[in] uri Full collection URI * @param[in] uri_scheme URI scheme (unused) * @param[in] uri_data Hierarchical/data part of the URI (typically a path) * @param[in] params List of parameter strings * @param[in] register_data Selection callback data * @param[out] pmanager_inst_data Pointer to manager specific data * * @returns Status code: * - IB_DECLINED Parameters not recognized * - IB_OK All OK, parameters recognized * - IB_Exxx Other error */ static ib_status_t core_managed_collection_jsonfile_register_fn( const ib_engine_t *ib, const ib_module_t *module, const ib_collection_manager_t *manager, ib_mpool_t *mp, const char *collection_name, const char *uri, const char *uri_scheme, const char *uri_data, const ib_list_t *params, void *register_data, void **pmanager_inst_data) { assert(ib != NULL); assert(module != NULL); assert(mp != NULL); assert(collection_name != NULL); assert(params != NULL); assert(pmanager_inst_data != NULL); const ib_list_node_t *node; const char *path; const char *param; const char *path_start = uri_data; bool persist = false; core_json_file_t *json_file; /* Get the first element in the list */ if (ib_list_elements(params) > 1) { return IB_EINVAL; } /* Look at the first param (if it exists) */ node = ib_list_first_const(params); if (node != NULL) { param = (const char *)node->data; if (strcasecmp(param, "persist") == 0) { persist = true; } else { ib_log_warning(ib, "JSON file: \"%s\"; unknown parameter \"%s\"", uri, param); return IB_EINVAL; } } /* Try to stat the file */ if (!persist) { struct stat sbuf; if (stat(path_start, &sbuf) < 0) { ib_log_warning(ib, "JSON file: Declining \"%s\"; " "stat(\"%s\") failed: %s", uri, path_start, strerror(errno)); return IB_DECLINED; } if (! S_ISREG(sbuf.st_mode)) { ib_log_warning(ib, "JSON file: Declining \"%s\"; \"%s\" is not a file", uri, path_start); return IB_DECLINED; } } /* Happy now, copy the file name, be done */ path = ib_mpool_strdup(mp, path_start); if (path == NULL) { return IB_EALLOC; } json_file = ib_mpool_alloc(mp, sizeof(*json_file)); if (json_file == NULL) { return IB_EALLOC; } json_file->path = path; json_file->persist = persist; /* Store the file object as the manager specific collection data */ *pmanager_inst_data = json_file; return IB_OK; }