2015-10-10 18:30:12 +08:00
|
|
|
|
|
|
|
|
#include <MESA/MESA_handle_logger.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <MESA/MESA_htable.h>
|
|
|
|
|
#include "cJSON.h"
|
2018-01-26 18:47:51 +08:00
|
|
|
#include "hiredis.h"
|
2015-10-10 18:30:12 +08:00
|
|
|
#include "map_str2int.h"
|
2019-07-25 14:49:11 +06:00
|
|
|
#include "Maat_table.h"
|
2015-10-10 18:30:12 +08:00
|
|
|
#include "Maat_rule_internal.h"
|
2018-09-24 18:49:18 +08:00
|
|
|
#include "Maat_utils.h"
|
|
|
|
|
|
2017-09-10 19:02:28 +08:00
|
|
|
#define maat_json (module_name_str("MAAT_JSON"))
|
|
|
|
|
|
2016-09-19 17:09:03 +08:00
|
|
|
const char* untitled_group_name="Untitled";
|
2015-10-10 18:30:12 +08:00
|
|
|
const int json_version=1;
|
|
|
|
|
#define MAX_PATH_LINE 256
|
|
|
|
|
#define MAX_COLUMN_NUM 32
|
|
|
|
|
struct group_info_t
|
|
|
|
|
{
|
|
|
|
|
int group_id;
|
2020-03-11 23:26:55 +08:00
|
|
|
char group_name[MAX_PATH_LINE];
|
2015-10-10 18:30:12 +08:00
|
|
|
};
|
|
|
|
|
struct iris_table_t
|
|
|
|
|
{
|
|
|
|
|
char table_name[MAX_PATH_LINE];
|
|
|
|
|
char table_path[MAX_PATH_LINE];
|
|
|
|
|
int line_count;
|
2020-01-22 18:25:01 +08:00
|
|
|
enum MAAT_TABLE_TYPE table_type;
|
|
|
|
|
void* buff;
|
|
|
|
|
size_t write_pos;
|
|
|
|
|
size_t buff_sz;
|
2015-10-10 18:30:12 +08:00
|
|
|
};
|
|
|
|
|
struct iris_description_t
|
|
|
|
|
{
|
|
|
|
|
int group_cnt;
|
|
|
|
|
int region_cnt;
|
|
|
|
|
|
|
|
|
|
char tmp_iris_dir[MAX_PATH_LINE];
|
|
|
|
|
char tmp_iris_index_dir[MAX_PATH_LINE];
|
|
|
|
|
char index_path[MAX_PATH_LINE];
|
|
|
|
|
|
2018-11-12 16:55:39 +08:00
|
|
|
struct iris_table_t* group_table;
|
2020-06-13 21:05:42 +08:00
|
|
|
struct iris_table_t* group2group_table;
|
|
|
|
|
struct iris_table_t* group2compile_table;
|
2018-11-12 16:55:39 +08:00
|
|
|
struct iris_table_t* compile_table;
|
2015-10-10 18:30:12 +08:00
|
|
|
MESA_htable_handle group_name_map;
|
|
|
|
|
MESA_htable_handle iris_table_map;
|
|
|
|
|
MESA_htable_handle str2int_map;
|
2018-01-26 18:47:51 +08:00
|
|
|
redisContext *redis_write_ctx;
|
2020-01-22 20:49:45 +08:00
|
|
|
char* encrypt_key;
|
|
|
|
|
char* encrypt_algo;
|
|
|
|
|
FILE* idx_fp;
|
2015-10-10 18:30:12 +08:00
|
|
|
};
|
|
|
|
|
struct traslate_command_t
|
|
|
|
|
{
|
|
|
|
|
const char* json_string;
|
|
|
|
|
char* json_value;
|
|
|
|
|
int json_type;
|
|
|
|
|
int str2int_flag;
|
|
|
|
|
int empty_allowed;
|
|
|
|
|
const char* default_string;
|
|
|
|
|
int default_int;
|
|
|
|
|
};
|
2020-01-22 18:25:01 +08:00
|
|
|
struct iris_table_t* query_table_info(iris_description_t* p_iris, const char* table_name, enum MAAT_TABLE_TYPE table_type)
|
2018-11-12 16:55:39 +08:00
|
|
|
{
|
|
|
|
|
struct iris_table_t* table_info=NULL;
|
|
|
|
|
table_info=(struct iris_table_t*)MESA_htable_search(p_iris->iris_table_map, (const unsigned char*)table_name,strlen(table_name));
|
|
|
|
|
if(table_info==NULL)
|
|
|
|
|
{
|
2020-01-22 18:25:01 +08:00
|
|
|
table_info=ALLOC(struct iris_table_t, 1);
|
2018-11-12 16:55:39 +08:00
|
|
|
table_info->line_count=0;
|
2020-01-22 18:25:01 +08:00
|
|
|
table_info->table_type=table_type;
|
|
|
|
|
memcpy(table_info->table_name, table_name, MIN(sizeof(table_info->table_name)-1, strlen(table_name)));
|
|
|
|
|
snprintf(table_info->table_path,sizeof(table_info->table_path), "%s/%s.local", p_iris->tmp_iris_dir, table_info->table_name);
|
|
|
|
|
MESA_htable_add(p_iris->iris_table_map, (const unsigned char*)table_info->table_name, strlen(table_info->table_name), table_info);
|
2018-11-12 16:55:39 +08:00
|
|
|
}
|
|
|
|
|
return table_info;
|
|
|
|
|
}
|
2020-01-22 18:25:01 +08:00
|
|
|
void free_iris_table_info(void* p)
|
|
|
|
|
{
|
|
|
|
|
struct iris_table_t* table=(struct iris_table_t*)p;
|
|
|
|
|
free(table->buff);
|
|
|
|
|
table->buff=NULL;
|
|
|
|
|
free(table);
|
|
|
|
|
}
|
2018-01-26 18:47:51 +08:00
|
|
|
static int get_group_seq(struct iris_description_t* iris_cfg)
|
|
|
|
|
{
|
|
|
|
|
redisReply* data_reply=NULL;
|
|
|
|
|
int sequence=0;
|
|
|
|
|
if(iris_cfg->redis_write_ctx==NULL)
|
|
|
|
|
{
|
|
|
|
|
sequence=iris_cfg->group_cnt;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-06-12 21:49:38 +08:00
|
|
|
data_reply=_wrap_redisCommand(iris_cfg->redis_write_ctx,"INCRBY %s 1", mr_group_id_var);
|
2018-01-26 18:47:51 +08:00
|
|
|
sequence=(int)data_reply->integer-1;
|
|
|
|
|
freeReplyObject(data_reply);
|
|
|
|
|
}
|
|
|
|
|
iris_cfg->group_cnt++;
|
|
|
|
|
return sequence;
|
|
|
|
|
}
|
|
|
|
|
static int get_region_seq(struct iris_description_t* iris_cfg)
|
|
|
|
|
{
|
|
|
|
|
redisReply* data_reply=NULL;
|
|
|
|
|
int sequence=0;
|
|
|
|
|
if(iris_cfg->redis_write_ctx==NULL)
|
|
|
|
|
{
|
|
|
|
|
sequence=iris_cfg->region_cnt;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2019-06-12 21:49:38 +08:00
|
|
|
data_reply=_wrap_redisCommand(iris_cfg->redis_write_ctx,"INCRBY %s 1", mr_region_id_var);
|
2018-01-26 18:47:51 +08:00
|
|
|
sequence=(int)data_reply->integer-1;
|
|
|
|
|
freeReplyObject(data_reply);
|
|
|
|
|
}
|
|
|
|
|
iris_cfg->region_cnt++;
|
|
|
|
|
return sequence;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-13 21:05:42 +08:00
|
|
|
int set_iris_descriptor(const char* json_file,cJSON *json, const char* encrypt_key, const char* encrypt_algo, const char*compile_tn, const char* group2compile_tn, const char* group2group_tn, redisContext *redis_write_ctx, struct iris_description_t *iris_cfg, void * logger)
|
2015-10-10 18:30:12 +08:00
|
|
|
{
|
|
|
|
|
memset(iris_cfg,0,sizeof(struct iris_description_t));
|
|
|
|
|
snprintf(iris_cfg->tmp_iris_dir,sizeof(iris_cfg->tmp_iris_dir),"%s_iris_tmp",json_file);
|
|
|
|
|
snprintf(iris_cfg->tmp_iris_index_dir,sizeof(iris_cfg->tmp_iris_index_dir),"%s_iris_tmp/index",json_file);
|
|
|
|
|
snprintf(iris_cfg->index_path,sizeof(iris_cfg->index_path),"%s/full_config_index.%010d",iris_cfg->tmp_iris_index_dir,json_version);
|
|
|
|
|
|
2018-01-26 18:47:51 +08:00
|
|
|
iris_cfg->redis_write_ctx=redis_write_ctx;
|
2015-10-10 18:30:12 +08:00
|
|
|
MESA_htable_create_args_t hargs;
|
|
|
|
|
memset(&hargs,0,sizeof(hargs));
|
|
|
|
|
hargs.thread_safe=1;
|
|
|
|
|
hargs.hash_slot_size = 1024;
|
|
|
|
|
hargs.max_elem_num = 0;
|
2016-04-03 12:29:41 +08:00
|
|
|
hargs.eliminate_type = HASH_ELIMINATE_ALGO_FIFO;
|
2015-10-10 18:30:12 +08:00
|
|
|
hargs.expire_time = 0;
|
|
|
|
|
hargs.key_comp = NULL;
|
|
|
|
|
hargs.key2index = NULL;
|
|
|
|
|
hargs.recursive = 0;
|
|
|
|
|
hargs.data_free = free;
|
|
|
|
|
hargs.data_expire_with_condition = NULL;
|
|
|
|
|
|
|
|
|
|
iris_cfg->group_name_map=MESA_htable_create(&hargs, sizeof(hargs));
|
|
|
|
|
MESA_htable_print_crtl(iris_cfg->group_name_map, 0);
|
2020-01-22 18:25:01 +08:00
|
|
|
|
|
|
|
|
hargs.data_free = free_iris_table_info;
|
2015-10-10 18:30:12 +08:00
|
|
|
iris_cfg->iris_table_map=MESA_htable_create(&hargs, sizeof(hargs));
|
|
|
|
|
MESA_htable_print_crtl(iris_cfg->iris_table_map, 0);
|
|
|
|
|
|
|
|
|
|
iris_cfg->str2int_map=map_create();
|
|
|
|
|
|
|
|
|
|
map_register(iris_cfg->str2int_map, "yes",1);
|
|
|
|
|
map_register(iris_cfg->str2int_map, "no",0);
|
|
|
|
|
|
|
|
|
|
map_register(iris_cfg->str2int_map, "ip",TABLE_TYPE_IP);
|
2019-05-23 18:29:59 +08:00
|
|
|
map_register(iris_cfg->str2int_map, "ip_plus",TABLE_TYPE_IP_PLUS);
|
2015-10-10 18:30:12 +08:00
|
|
|
map_register(iris_cfg->str2int_map, "string",TABLE_TYPE_EXPR);
|
2016-02-11 13:57:39 +08:00
|
|
|
map_register(iris_cfg->str2int_map, "expr",TABLE_TYPE_EXPR);
|
|
|
|
|
map_register(iris_cfg->str2int_map, "expr_plus",TABLE_TYPE_EXPR_PLUS);
|
2017-07-03 12:53:12 +08:00
|
|
|
map_register(iris_cfg->str2int_map, "intval",TABLE_TYPE_INTERVAL);
|
2020-08-22 19:13:13 +08:00
|
|
|
map_register(iris_cfg->str2int_map, "interval",TABLE_TYPE_INTERVAL);
|
2020-08-18 16:56:31 +08:00
|
|
|
map_register(iris_cfg->str2int_map, "intval_plus",TABLE_TYPE_INTERVAL_PLUS);
|
2020-08-22 19:13:13 +08:00
|
|
|
map_register(iris_cfg->str2int_map, "interval_plus",TABLE_TYPE_INTERVAL_PLUS);
|
2015-11-12 17:49:57 +08:00
|
|
|
map_register(iris_cfg->str2int_map, "digest",TABLE_TYPE_DIGEST);
|
2017-07-07 20:47:27 +08:00
|
|
|
map_register(iris_cfg->str2int_map, "similar",TABLE_TYPE_SIMILARITY);
|
2016-02-11 13:57:39 +08:00
|
|
|
|
2015-11-12 17:49:57 +08:00
|
|
|
|
2015-10-10 18:30:12 +08:00
|
|
|
map_register(iris_cfg->str2int_map, "ipv4",4);
|
|
|
|
|
map_register(iris_cfg->str2int_map, "ipv6",6);
|
|
|
|
|
|
|
|
|
|
map_register(iris_cfg->str2int_map, "double",0);
|
|
|
|
|
map_register(iris_cfg->str2int_map, "single",1);
|
|
|
|
|
|
|
|
|
|
map_register(iris_cfg->str2int_map, "none",0);
|
|
|
|
|
map_register(iris_cfg->str2int_map, "and",1);
|
|
|
|
|
map_register(iris_cfg->str2int_map, "regex",2);
|
|
|
|
|
map_register(iris_cfg->str2int_map, "offset",3);
|
|
|
|
|
|
|
|
|
|
map_register(iris_cfg->str2int_map, "sub",0);
|
|
|
|
|
map_register(iris_cfg->str2int_map, "right",1);
|
2019-04-03 17:40:42 +08:00
|
|
|
map_register(iris_cfg->str2int_map, "suffix",1);
|
2015-10-10 18:30:12 +08:00
|
|
|
map_register(iris_cfg->str2int_map, "left",2);
|
2019-04-03 17:40:42 +08:00
|
|
|
map_register(iris_cfg->str2int_map, "prefix",2);
|
2015-10-10 18:30:12 +08:00
|
|
|
map_register(iris_cfg->str2int_map, "complete",3);
|
2020-05-30 20:56:49 +08:00
|
|
|
map_register(iris_cfg->str2int_map, "exact",3);
|
2015-10-10 18:30:12 +08:00
|
|
|
|
|
|
|
|
map_register(iris_cfg->str2int_map, "uncase plain",0);
|
|
|
|
|
map_register(iris_cfg->str2int_map, "hexbin",1);
|
|
|
|
|
map_register(iris_cfg->str2int_map, "case plain",2);
|
|
|
|
|
|
2020-01-22 18:25:01 +08:00
|
|
|
iris_cfg->compile_table=query_table_info(iris_cfg, compile_tn, TABLE_TYPE_COMPILE);
|
2020-06-13 21:05:42 +08:00
|
|
|
iris_cfg->group2compile_table=query_table_info(iris_cfg, group2compile_tn, TABLE_TYPE_GROUP2COMPILE);
|
2020-06-14 20:52:14 +08:00
|
|
|
iris_cfg->group2group_table=query_table_info(iris_cfg, group2group_tn, TABLE_TYPE_GROUP2GROUP);
|
2020-01-22 20:49:45 +08:00
|
|
|
|
|
|
|
|
if(encrypt_key && encrypt_algo)
|
|
|
|
|
{
|
|
|
|
|
iris_cfg->encrypt_key=_maat_strdup(encrypt_key);
|
|
|
|
|
iris_cfg->encrypt_algo=_maat_strdup(encrypt_algo);
|
|
|
|
|
}
|
2018-11-12 16:55:39 +08:00
|
|
|
|
2015-10-10 18:30:12 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
void clear_iris_descriptor(struct iris_description_t *iris_cfg)
|
|
|
|
|
{
|
|
|
|
|
if(iris_cfg->group_name_map!=NULL)
|
|
|
|
|
{
|
|
|
|
|
MESA_htable_destroy(iris_cfg->group_name_map, NULL);
|
|
|
|
|
}
|
|
|
|
|
if(iris_cfg->iris_table_map!=NULL)
|
|
|
|
|
{
|
|
|
|
|
MESA_htable_destroy(iris_cfg->iris_table_map, NULL);
|
|
|
|
|
}
|
2018-11-12 16:55:39 +08:00
|
|
|
map_destroy(iris_cfg->str2int_map);
|
2020-01-22 20:49:45 +08:00
|
|
|
free(iris_cfg->encrypt_algo);
|
|
|
|
|
free(iris_cfg->encrypt_key);
|
2015-10-10 18:30:12 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int create_tmp_dir(struct iris_description_t *p)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if((access(p->tmp_iris_dir,F_OK))<0)
|
|
|
|
|
{
|
|
|
|
|
if((mkdir(p->tmp_iris_dir, 0777)) < 0)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if((access(p->tmp_iris_index_dir,F_OK))<0)
|
|
|
|
|
{
|
|
|
|
|
if((mkdir(p->tmp_iris_index_dir, 0777)) < 0)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
int set_file_rulenum(const char* path,int rulenum,void* logger)
|
|
|
|
|
{
|
|
|
|
|
FILE* fp=NULL;
|
|
|
|
|
if(rulenum==0)
|
|
|
|
|
{
|
|
|
|
|
fp=fopen(path,"w");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
fp=fopen(path,"r+");
|
|
|
|
|
}
|
|
|
|
|
if(fp==NULL)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
|
|
|
|
|
"fopen %s failed %s at set rule num.",path,strerror(errno));
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
fprintf(fp,"%010d\n",rulenum);
|
|
|
|
|
fclose(fp);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2020-01-22 18:25:01 +08:00
|
|
|
int direct_write_rule(cJSON* json, MESA_htable_handle str2int, struct traslate_command_t*cmd, int cmd_cnt, struct iris_table_t* table, void* logger)
|
2015-10-10 18:30:12 +08:00
|
|
|
{
|
|
|
|
|
int i=0,ret=-1;
|
|
|
|
|
cJSON* item=NULL;
|
|
|
|
|
cJSON dummy;
|
|
|
|
|
char *p=NULL;
|
|
|
|
|
int int_value=0;
|
|
|
|
|
for(i=0;i<cmd_cnt;i++)
|
|
|
|
|
{
|
|
|
|
|
item=cJSON_GetObjectItem(json,cmd[i].json_string);
|
|
|
|
|
if(item==NULL&&cmd[i].empty_allowed==1)
|
|
|
|
|
{
|
|
|
|
|
dummy.valuestring=(char*)cmd[i].default_string;
|
2017-09-13 11:40:56 +08:00
|
|
|
dummy.valueint=cmd[i].default_int;
|
|
|
|
|
dummy.valuedouble=cmd[i].default_int;
|
2015-10-10 18:30:12 +08:00
|
|
|
dummy.type=cmd[i].json_type;
|
|
|
|
|
|
|
|
|
|
item=&dummy;
|
|
|
|
|
}
|
|
|
|
|
if(item==NULL||item->type!=cmd[i].json_type)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
|
|
|
|
|
"%s not defined or wrong format.",cmd[i].json_string);
|
2020-01-22 18:25:01 +08:00
|
|
|
ret=-1;
|
2015-10-10 18:30:12 +08:00
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
if(cmd[i].str2int_flag==1)
|
|
|
|
|
{
|
|
|
|
|
p=item->valuestring;
|
|
|
|
|
ret=map_str2int(str2int, p, &int_value);
|
|
|
|
|
if(ret<0)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
|
|
|
|
|
"%s's value %s is not valid format.",cmd[i].json_string,p);
|
|
|
|
|
free(p);
|
2020-01-22 18:25:01 +08:00
|
|
|
ret=-1;
|
2015-10-10 18:30:12 +08:00
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
cmd[i].json_value=(char*)malloc(21);/* 2^64+1 can be represented in 21 chars. */
|
2020-01-22 18:25:01 +08:00
|
|
|
snprintf(cmd[i].json_value,21, "%d", int_value);
|
2015-10-10 18:30:12 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
switch(item->type)
|
|
|
|
|
{
|
|
|
|
|
case cJSON_Number:
|
|
|
|
|
cmd[i].json_value=cJSON_Print(item);
|
|
|
|
|
break;
|
|
|
|
|
case cJSON_String:
|
2020-01-22 18:25:01 +08:00
|
|
|
cmd[i].json_value=ALLOC(char, strlen(item->valuestring)+1);
|
|
|
|
|
memcpy(cmd[i].json_value, item->valuestring, strlen(item->valuestring));
|
2015-10-10 18:30:12 +08:00
|
|
|
break;
|
|
|
|
|
default://impossible ,already checked
|
|
|
|
|
assert(0);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for(i=0;i<cmd_cnt;i++)
|
|
|
|
|
{
|
2020-01-22 18:25:01 +08:00
|
|
|
table->write_pos+=memcat(&(table->buff), table->write_pos, &table->buff_sz, cmd[i].json_value, strlen(cmd[i].json_value));
|
|
|
|
|
table->write_pos+=memcat(&(table->buff), table->write_pos, &table->buff_sz, "\t", 1);
|
2015-10-10 18:30:12 +08:00
|
|
|
}
|
2020-01-22 18:25:01 +08:00
|
|
|
table->write_pos+=memcat(&(table->buff), table->write_pos, &table->buff_sz, "\n", 1);
|
|
|
|
|
table->line_count++;
|
|
|
|
|
ret=0;
|
2015-10-10 18:30:12 +08:00
|
|
|
|
2020-01-22 18:25:01 +08:00
|
|
|
error_out:
|
2015-10-10 18:30:12 +08:00
|
|
|
for(i=0;i<cmd_cnt;i++)
|
|
|
|
|
{
|
|
|
|
|
if(cmd[i].json_value!=NULL)
|
|
|
|
|
{
|
|
|
|
|
free(cmd[i].json_value);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-01-22 18:25:01 +08:00
|
|
|
return ret;
|
|
|
|
|
|
2015-10-10 18:30:12 +08:00
|
|
|
}
|
2020-01-22 18:25:01 +08:00
|
|
|
int write_ip_line(cJSON *region_json, struct iris_description_t *p_iris, struct iris_table_t* table, void * logger)
|
2015-10-10 18:30:12 +08:00
|
|
|
{
|
|
|
|
|
struct traslate_command_t json_cmd[MAX_COLUMN_NUM];
|
|
|
|
|
int cmd_cnt=0;
|
|
|
|
|
memset(json_cmd,0,sizeof(json_cmd));
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="region_id";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="group_id";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="addr_type";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].str2int_flag=1;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="src_ip";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_string="0.0.0.0";
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="mask_src_ip";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_string="255.255.255.255";
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="src_port";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_string="0";
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="mask_src_port";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_string="65535";
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="dst_ip";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_string="0.0.0.0";
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="mask_dst_ip";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_string="255.255.255.255";
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="dst_port";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_string="0";
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="mask_dst_port";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_string="65535";
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="protocol";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_int=0;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="direction";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].str2int_flag=1;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_string="double";
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="is_valid";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
2020-01-22 18:25:01 +08:00
|
|
|
return direct_write_rule(region_json, p_iris->str2int_map,json_cmd, cmd_cnt, table, logger);
|
2015-10-10 18:30:12 +08:00
|
|
|
}
|
2020-01-22 18:25:01 +08:00
|
|
|
int write_ip_plus_line(cJSON *region_json, struct iris_description_t *p_iris, struct iris_table_t* table, void * logger)
|
2019-05-23 18:29:59 +08:00
|
|
|
{
|
|
|
|
|
struct traslate_command_t json_cmd[MAX_COLUMN_NUM];
|
|
|
|
|
int cmd_cnt=0;
|
|
|
|
|
memset(json_cmd,0,sizeof(json_cmd));
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="region_id";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="group_id";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="addr_type";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].str2int_flag=1;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="saddr_format";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_string="mask";
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="src_ip1";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_string="0.0.0.0";
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="src_ip2";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_string="255.255.255.255";
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="sport_format";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_string="mask";
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="src_port1";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_string="0";
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="src_port2";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_string="65535";
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="daddr_format";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_string="mask";
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="dst_ip1";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_string="0.0.0.0";
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="dst_ip2";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_string="255.255.255.255";
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="dport_format";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_string="mask";
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="dst_port1";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_string="0";
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="dst_port2";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_string="65535";
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="protocol";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_int=0;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="direction";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].str2int_flag=1;
|
|
|
|
|
json_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
json_cmd[cmd_cnt].default_string="double";
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="is_valid";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
2020-01-22 18:25:01 +08:00
|
|
|
return direct_write_rule(region_json, p_iris->str2int_map,json_cmd, cmd_cnt, table, logger);
|
2019-05-23 18:29:59 +08:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-22 18:25:01 +08:00
|
|
|
int write_expr_line(cJSON *region_json, struct iris_description_t *p_iris, struct iris_table_t* table, void * logger)
|
2015-10-10 18:30:12 +08:00
|
|
|
{
|
|
|
|
|
struct traslate_command_t json_cmd[MAX_COLUMN_NUM];
|
|
|
|
|
int cmd_cnt=0;
|
|
|
|
|
memset(json_cmd,0,sizeof(json_cmd));
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="region_id";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="group_id";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
2020-01-22 18:25:01 +08:00
|
|
|
if(table->table_type==TABLE_TYPE_EXPR_PLUS)
|
2016-02-11 13:57:39 +08:00
|
|
|
{
|
|
|
|
|
json_cmd[cmd_cnt].json_string="district";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-10 18:30:12 +08:00
|
|
|
json_cmd[cmd_cnt].json_string="keywords";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="expr_type";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].str2int_flag=1;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="match_method";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].str2int_flag=1;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="format";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
json_cmd[cmd_cnt].str2int_flag=1;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="is_valid";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
2020-01-22 18:25:01 +08:00
|
|
|
return direct_write_rule(region_json, p_iris->str2int_map,json_cmd, cmd_cnt, table, logger);
|
2015-10-10 18:30:12 +08:00
|
|
|
|
|
|
|
|
}
|
2020-01-22 18:25:01 +08:00
|
|
|
int write_intval_line(cJSON *region_json, struct iris_description_t *p_iris, struct iris_table_t* table, void * logger)
|
2015-10-10 18:30:12 +08:00
|
|
|
{
|
|
|
|
|
struct traslate_command_t json_cmd[MAX_COLUMN_NUM];
|
|
|
|
|
int cmd_cnt=0;
|
|
|
|
|
memset(json_cmd,0,sizeof(json_cmd));
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="region_id";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="group_id";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
2020-08-18 16:56:31 +08:00
|
|
|
|
|
|
|
|
if(table->table_type==TABLE_TYPE_INTERVAL_PLUS)
|
|
|
|
|
{
|
|
|
|
|
json_cmd[cmd_cnt].json_string="district";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-10 18:30:12 +08:00
|
|
|
json_cmd[cmd_cnt].json_string="low_boundary";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="up_boundary";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="is_valid";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
2020-01-22 18:25:01 +08:00
|
|
|
return direct_write_rule(region_json, p_iris->str2int_map, json_cmd, cmd_cnt, table, logger);
|
2015-10-10 18:30:12 +08:00
|
|
|
|
2015-11-12 17:49:57 +08:00
|
|
|
}
|
2020-01-22 18:25:01 +08:00
|
|
|
int write_digest_line(cJSON *region_json, struct iris_description_t *p_iris, struct iris_table_t* table, void * logger)
|
2015-11-12 17:49:57 +08:00
|
|
|
{
|
|
|
|
|
struct traslate_command_t json_cmd[MAX_COLUMN_NUM];
|
|
|
|
|
int cmd_cnt=0;
|
|
|
|
|
memset(json_cmd,0,sizeof(json_cmd));
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="region_id";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="group_id";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="raw_len";
|
2015-11-13 18:08:55 +08:00
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
2015-11-12 17:49:57 +08:00
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="digest";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="cfds_level";
|
2015-11-13 18:08:55 +08:00
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
2015-11-12 17:49:57 +08:00
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="is_valid";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
2020-01-22 18:25:01 +08:00
|
|
|
return direct_write_rule(region_json, p_iris->str2int_map,json_cmd, cmd_cnt, table, logger);
|
2015-11-12 17:49:57 +08:00
|
|
|
|
2017-07-07 20:47:27 +08:00
|
|
|
}
|
2020-01-22 18:25:01 +08:00
|
|
|
int write_similar_line(cJSON *region_json, struct iris_description_t *p_iris, struct iris_table_t* table, void * logger)
|
2017-07-07 20:47:27 +08:00
|
|
|
{
|
|
|
|
|
struct traslate_command_t json_cmd[MAX_COLUMN_NUM];
|
|
|
|
|
int cmd_cnt=0;
|
|
|
|
|
memset(json_cmd,0,sizeof(json_cmd));
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="region_id";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="group_id";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="target";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="threshold";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
json_cmd[cmd_cnt].json_string="is_valid";
|
|
|
|
|
json_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
2020-01-22 18:25:01 +08:00
|
|
|
return direct_write_rule(region_json, p_iris->str2int_map,json_cmd, cmd_cnt, table, logger);
|
2017-07-07 20:47:27 +08:00
|
|
|
|
2015-10-10 18:30:12 +08:00
|
|
|
}
|
2018-11-12 16:55:39 +08:00
|
|
|
|
2020-01-22 18:25:01 +08:00
|
|
|
int write_plugin_line(cJSON* plug_table_json, int sequence, iris_description_t* p_iris, void* logger)
|
2015-10-10 18:30:12 +08:00
|
|
|
{
|
2020-01-22 18:25:01 +08:00
|
|
|
cJSON* item=NULL,*table_content=NULL, *each_line=NULL;
|
2015-10-10 18:30:12 +08:00
|
|
|
struct iris_table_t* table_info=NULL;
|
2020-01-22 18:25:01 +08:00
|
|
|
const char* table_name=NULL, *line_content=NULL;
|
|
|
|
|
int i=0, line_cnt=0;
|
2015-10-10 18:30:12 +08:00
|
|
|
|
2020-01-22 18:25:01 +08:00
|
|
|
item=cJSON_GetObjectItem(plug_table_json, "table_name");
|
2015-10-10 18:30:12 +08:00
|
|
|
if(item==NULL||item->type!=cJSON_String)
|
|
|
|
|
{
|
2020-01-22 18:25:01 +08:00
|
|
|
MESA_handle_runtime_log(logger, RLOG_LV_FATAL, maat_json,
|
|
|
|
|
"The %d plugin_table's table_name not defined or format error.", sequence);
|
2015-10-10 18:30:12 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2020-01-22 18:25:01 +08:00
|
|
|
table_name=item->valuestring;
|
|
|
|
|
table_info=query_table_info(p_iris, table_name, TABLE_TYPE_PLUGIN);
|
|
|
|
|
table_content=cJSON_GetObjectItem(plug_table_json, "table_content");
|
2015-10-10 18:30:12 +08:00
|
|
|
if(table_content==NULL||table_content->type!=cJSON_Array)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
|
|
|
|
|
"%d plugin_table's table_content not defined or format error."
|
|
|
|
|
,sequence);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
line_cnt=cJSON_GetArraySize(table_content);
|
|
|
|
|
|
|
|
|
|
for(i=0;i<line_cnt;i++)
|
|
|
|
|
{
|
|
|
|
|
each_line=cJSON_GetArrayItem(table_content,i);
|
|
|
|
|
if(each_line==NULL||each_line->type!=cJSON_String)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
|
|
|
|
|
"plugin_table %s's line %d format error.",table_info->table_name,i+1);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
line_content=each_line->valuestring;
|
2020-01-22 18:25:01 +08:00
|
|
|
table_info->write_pos+=memcat(&(table_info->buff), table_info->write_pos, &(table_info->buff_sz), line_content, strlen(line_content));
|
|
|
|
|
table_info->write_pos+=memcat(&(table_info->buff), table_info->write_pos, &(table_info->buff_sz), "\n", 1);
|
2015-10-10 18:30:12 +08:00
|
|
|
table_info->line_count++;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2020-01-22 18:25:01 +08:00
|
|
|
int write_region_rule(cJSON* region_json, int compile_id, int group_id, iris_description_t* p_iris, void* logger)
|
2015-10-10 18:30:12 +08:00
|
|
|
{
|
|
|
|
|
cJSON* item=NULL,*table_content=NULL;
|
|
|
|
|
int ret=0;
|
|
|
|
|
int region_id=0;
|
|
|
|
|
const char* table_name=NULL,*table_type_str=NULL;
|
|
|
|
|
enum MAAT_TABLE_TYPE table_type=TABLE_TYPE_EXPR;
|
|
|
|
|
struct iris_table_t* table_info=NULL;
|
|
|
|
|
|
2020-01-22 18:25:01 +08:00
|
|
|
|
2015-10-10 18:30:12 +08:00
|
|
|
item=cJSON_GetObjectItem(region_json,"table_type");
|
|
|
|
|
if(item==NULL||item->type!=cJSON_String)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
|
|
|
|
|
"compile rule %d table name %s's table_type not defined or format error."
|
|
|
|
|
,compile_id,table_name);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
table_type_str=item->valuestring;
|
2020-01-22 18:25:01 +08:00
|
|
|
ret=map_str2int(p_iris->str2int_map, table_type_str, (int*)&(table_type));
|
2015-10-10 18:30:12 +08:00
|
|
|
if(ret!=1)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
|
|
|
|
|
"compile rule %d table name %s's table_type %s invalid."
|
|
|
|
|
,compile_id,table_name,table_type_str);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2020-01-22 18:25:01 +08:00
|
|
|
item=cJSON_GetObjectItem(region_json,"table_name");
|
|
|
|
|
if(item==NULL||item->type!=cJSON_String)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
|
|
|
|
|
"compile rule %d's region table_name not defined or format error.",compile_id);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
table_name=item->valuestring;
|
|
|
|
|
table_info=query_table_info(p_iris, table_name, table_type);
|
|
|
|
|
|
2015-10-10 18:30:12 +08:00
|
|
|
table_content=cJSON_GetObjectItem(region_json,"table_content");
|
|
|
|
|
if(table_content==NULL||table_content->type!=cJSON_Object)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
|
|
|
|
|
"compile rule %d table name %s's table_content not defined or format error."
|
|
|
|
|
,compile_id,table_name);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2020-01-22 18:25:01 +08:00
|
|
|
|
2018-01-26 18:47:51 +08:00
|
|
|
region_id=get_region_seq(p_iris);
|
2015-10-10 18:30:12 +08:00
|
|
|
cJSON_AddNumberToObject(table_content, "region_id", region_id);
|
|
|
|
|
cJSON_AddNumberToObject(table_content, "group_id", group_id);
|
|
|
|
|
cJSON_AddNumberToObject(table_content, "is_valid", 1);
|
|
|
|
|
|
|
|
|
|
switch(table_type)
|
|
|
|
|
{
|
|
|
|
|
case TABLE_TYPE_EXPR:
|
2016-02-11 13:57:39 +08:00
|
|
|
case TABLE_TYPE_EXPR_PLUS:
|
2020-01-22 18:25:01 +08:00
|
|
|
ret=write_expr_line(table_content, p_iris, table_info, logger);
|
2015-10-10 18:30:12 +08:00
|
|
|
break;
|
|
|
|
|
case TABLE_TYPE_IP:
|
2020-01-22 18:25:01 +08:00
|
|
|
ret=write_ip_line(table_content, p_iris, table_info, logger);
|
2015-10-10 18:30:12 +08:00
|
|
|
break;
|
2019-05-23 18:29:59 +08:00
|
|
|
case TABLE_TYPE_IP_PLUS:
|
2020-01-22 18:25:01 +08:00
|
|
|
write_ip_plus_line(table_content, p_iris, table_info, logger);
|
2019-05-23 18:29:59 +08:00
|
|
|
break;
|
2017-07-03 12:53:12 +08:00
|
|
|
case TABLE_TYPE_INTERVAL:
|
2020-08-18 16:56:31 +08:00
|
|
|
case TABLE_TYPE_INTERVAL_PLUS:
|
2020-01-22 18:25:01 +08:00
|
|
|
ret=write_intval_line(table_content, p_iris, table_info, logger);
|
2015-10-10 18:30:12 +08:00
|
|
|
break;
|
2015-11-12 17:49:57 +08:00
|
|
|
case TABLE_TYPE_DIGEST:
|
2020-01-22 18:25:01 +08:00
|
|
|
ret=write_digest_line(table_content, p_iris, table_info, logger);
|
2017-07-07 20:47:27 +08:00
|
|
|
break;
|
|
|
|
|
case TABLE_TYPE_SIMILARITY:
|
2020-01-22 18:25:01 +08:00
|
|
|
ret=write_similar_line(table_content, p_iris, table_info, logger);
|
2017-07-07 20:47:27 +08:00
|
|
|
break;
|
2015-10-10 18:30:12 +08:00
|
|
|
default:
|
|
|
|
|
assert(0);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-04 20:37:31 +08:00
|
|
|
int write_compile_line(cJSON *compile, struct iris_description_t *p_iris, void * logger)
|
2015-10-10 18:30:12 +08:00
|
|
|
{
|
2020-06-16 22:19:02 +08:00
|
|
|
int compile_id=-1,cmd_cnt=0,ret=-1, clause_num=0, group_num=0, i=0, clause_index=0;
|
2015-10-10 18:30:12 +08:00
|
|
|
cJSON* item=NULL;
|
2018-11-09 16:40:32 +08:00
|
|
|
struct iris_table_t* table_info=NULL;
|
2020-06-13 21:05:42 +08:00
|
|
|
cJSON* group_array=NULL, *group_obj=NULL;
|
|
|
|
|
int* clause_ids=NULL;
|
2020-06-16 22:19:02 +08:00
|
|
|
|
|
|
|
|
item=cJSON_GetObjectItem(compile,"compile_id");
|
|
|
|
|
if(item->type!=cJSON_Number)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
|
|
|
|
|
"compile_id format not number.");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
compile_id=item->valueint;
|
|
|
|
|
|
2020-06-13 21:05:42 +08:00
|
|
|
group_array=cJSON_GetObjectItem(compile, "groups");
|
|
|
|
|
group_num=cJSON_GetArraySize(group_array);
|
|
|
|
|
clause_ids=ALLOC(int, group_num);
|
2020-06-16 22:19:02 +08:00
|
|
|
cJSON_ArrayForEach(group_obj, group_array)
|
2020-06-13 21:05:42 +08:00
|
|
|
{
|
2020-06-16 22:19:02 +08:00
|
|
|
item=cJSON_GetObjectItem(group_obj, "clause_index");
|
2020-06-13 21:05:42 +08:00
|
|
|
if(item)
|
|
|
|
|
{
|
2020-06-16 22:19:02 +08:00
|
|
|
clause_index=item->valueint;
|
2020-06-13 21:05:42 +08:00
|
|
|
for(i=0; i<clause_num; i++)
|
|
|
|
|
{
|
2020-06-16 22:19:02 +08:00
|
|
|
if(clause_ids[i]==clause_index)
|
2020-06-13 21:05:42 +08:00
|
|
|
{
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if(i==clause_num)
|
|
|
|
|
{
|
2020-06-16 22:19:02 +08:00
|
|
|
clause_ids[clause_num]=clause_index;
|
2020-06-13 21:05:42 +08:00
|
|
|
clause_num++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
free(clause_ids);
|
|
|
|
|
clause_ids=NULL;
|
|
|
|
|
if(clause_num==0)
|
|
|
|
|
{
|
2020-06-16 22:19:02 +08:00
|
|
|
clause_num=group_num;
|
2020-06-13 21:05:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cJSON_AddNumberToObject(compile, "clause_num", clause_num);
|
2020-02-04 11:00:57 +08:00
|
|
|
|
2015-10-10 18:30:12 +08:00
|
|
|
struct traslate_command_t compile_cmd[MAX_COLUMN_NUM];
|
|
|
|
|
memset(compile_cmd,0,sizeof(compile_cmd));
|
2018-11-09 16:40:32 +08:00
|
|
|
|
|
|
|
|
|
2015-10-10 18:30:12 +08:00
|
|
|
compile_cmd[cmd_cnt].json_string="compile_id";
|
|
|
|
|
compile_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
compile_cmd[cmd_cnt].json_string="service";
|
|
|
|
|
compile_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
compile_cmd[cmd_cnt].json_string="action";
|
|
|
|
|
compile_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
compile_cmd[cmd_cnt].json_string="do_blacklist";
|
|
|
|
|
compile_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
compile_cmd[cmd_cnt].json_string="do_log";
|
|
|
|
|
compile_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
2018-09-21 21:32:09 +08:00
|
|
|
|
|
|
|
|
compile_cmd[cmd_cnt].json_string="tags";
|
|
|
|
|
compile_cmd[cmd_cnt].json_type=cJSON_String;
|
2017-09-10 18:30:44 +08:00
|
|
|
compile_cmd[cmd_cnt].empty_allowed=1;
|
2018-09-21 21:32:09 +08:00
|
|
|
compile_cmd[cmd_cnt].default_string="0";
|
2015-10-10 18:30:12 +08:00
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
compile_cmd[cmd_cnt].json_string="user_region";
|
|
|
|
|
compile_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
|
|
|
|
compile_cmd[cmd_cnt].json_string="is_valid";
|
|
|
|
|
compile_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
compile_cmd[cmd_cnt].str2int_flag=1;
|
|
|
|
|
cmd_cnt++;
|
2020-02-04 11:00:57 +08:00
|
|
|
|
2020-06-13 21:05:42 +08:00
|
|
|
compile_cmd[cmd_cnt].json_string="clause_num";
|
2020-02-04 11:00:57 +08:00
|
|
|
compile_cmd[cmd_cnt].json_type=cJSON_Number;
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
2020-02-04 16:36:29 +08:00
|
|
|
compile_cmd[cmd_cnt].json_string="evaluation_order";
|
2020-02-04 11:00:57 +08:00
|
|
|
compile_cmd[cmd_cnt].json_type=cJSON_String;
|
|
|
|
|
compile_cmd[cmd_cnt].empty_allowed=1;
|
|
|
|
|
compile_cmd[cmd_cnt].default_string="0.0";
|
|
|
|
|
cmd_cnt++;
|
|
|
|
|
|
2015-10-10 18:30:12 +08:00
|
|
|
|
2018-11-09 16:40:32 +08:00
|
|
|
item=cJSON_GetObjectItem(compile,"table_name");
|
|
|
|
|
if(item==NULL||item->type!=cJSON_String)
|
|
|
|
|
{
|
2018-11-12 16:55:39 +08:00
|
|
|
table_info=p_iris->compile_table;
|
2018-11-09 16:40:32 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-01-22 18:25:01 +08:00
|
|
|
table_info=query_table_info(p_iris, item->valuestring, TABLE_TYPE_COMPILE);
|
2018-11-09 16:40:32 +08:00
|
|
|
}
|
2015-10-10 18:30:12 +08:00
|
|
|
|
2020-02-04 11:00:57 +08:00
|
|
|
ret=direct_write_rule(compile, p_iris->str2int_map, compile_cmd, cmd_cnt, table_info, logger);
|
2015-10-10 18:30:12 +08:00
|
|
|
if(ret<0)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return compile_id;
|
|
|
|
|
}
|
2020-06-16 22:19:02 +08:00
|
|
|
int write_group2compile_line(int group_id, int compile_id, int group_not_flag, int clause_index, const char* virtual_table, struct iris_description_t *p_iris, void * logger)
|
2020-06-13 21:05:42 +08:00
|
|
|
{
|
|
|
|
|
char buff[1024*4];
|
|
|
|
|
struct iris_table_t* table=p_iris->group2compile_table;
|
2020-06-16 22:19:02 +08:00
|
|
|
snprintf(buff, sizeof(buff), "%d\t%d\t1\t%d\t%s\t%d\n", group_id, compile_id, group_not_flag, virtual_table, clause_index);
|
2020-06-13 21:05:42 +08:00
|
|
|
table->write_pos+=memcat(&(table->buff), table->write_pos, &(table->buff_sz), buff, strlen(buff));
|
|
|
|
|
table->line_count++;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
int write_group2group_line(int group_id, int superior_gorup_id, struct iris_description_t *p_iris, void * logger)
|
2015-10-10 18:30:12 +08:00
|
|
|
{
|
2020-01-22 18:25:01 +08:00
|
|
|
char buff[1024*4];
|
2020-06-13 21:05:42 +08:00
|
|
|
struct iris_table_t* table=p_iris->group2group_table;
|
|
|
|
|
snprintf(buff, sizeof(buff), "%d\t%d\t1\n", group_id, superior_gorup_id);
|
2020-01-22 18:25:01 +08:00
|
|
|
table->write_pos+=memcat(&(table->buff), table->write_pos, &(table->buff_sz), buff, strlen(buff));
|
|
|
|
|
table->line_count++;
|
2015-10-10 18:30:12 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
2020-06-13 21:05:42 +08:00
|
|
|
|
2015-10-10 18:30:12 +08:00
|
|
|
void table_idx_write_cb(const uchar * key, uint size, void * data, void * user)
|
|
|
|
|
{
|
2020-01-22 20:49:45 +08:00
|
|
|
struct iris_description_t *p_iris=(struct iris_description_t *)user;
|
2020-01-22 18:25:01 +08:00
|
|
|
struct iris_table_t* table=(struct iris_table_t*)data;
|
2020-01-22 20:49:45 +08:00
|
|
|
FILE* table_fp=NULL;
|
|
|
|
|
char line_cnt_str[32], err_str[256];
|
|
|
|
|
snprintf(line_cnt_str, sizeof(line_cnt_str), "%010d\n", table->line_count);
|
2020-01-22 18:25:01 +08:00
|
|
|
|
2020-01-22 20:49:45 +08:00
|
|
|
int ret=0;
|
|
|
|
|
size_t table_file_sz=strlen(line_cnt_str)+table->write_pos;
|
|
|
|
|
unsigned char* buff=ALLOC(unsigned char, table_file_sz);
|
|
|
|
|
unsigned char* encrypt_buff=NULL;
|
|
|
|
|
size_t encrypt_buff_sz=0;
|
|
|
|
|
memcpy(buff, line_cnt_str, strlen(line_cnt_str));
|
|
|
|
|
memcpy(buff+strlen(line_cnt_str), table->buff, table->write_pos);
|
|
|
|
|
table_fp=fopen(table->table_path, "w");
|
|
|
|
|
if(p_iris->encrypt_key)
|
|
|
|
|
{
|
|
|
|
|
ret=crypt_memory(buff, table_file_sz, &encrypt_buff, &encrypt_buff_sz, p_iris->encrypt_key, p_iris->encrypt_algo, 1, err_str, sizeof(err_str));
|
|
|
|
|
assert(ret==0);
|
|
|
|
|
fwrite(encrypt_buff, encrypt_buff_sz, 1, table_fp);
|
|
|
|
|
fprintf(p_iris->idx_fp,"%s\t%d\t%s\t%s\n", table->table_name, table->line_count, table->table_path, p_iris->encrypt_algo);
|
2020-06-16 21:31:26 +08:00
|
|
|
free(encrypt_buff);
|
2020-01-22 20:49:45 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
fwrite(buff, table_file_sz, 1, table_fp);
|
|
|
|
|
fprintf(p_iris->idx_fp,"%s\t%d\t%s\n", table->table_name, table->line_count, table->table_path);
|
|
|
|
|
}
|
|
|
|
|
fclose(table_fp);
|
|
|
|
|
free(buff);
|
|
|
|
|
buff=NULL;
|
|
|
|
|
|
|
|
|
|
|
2015-10-10 18:30:12 +08:00
|
|
|
}
|
|
|
|
|
int write_index_file(struct iris_description_t *p_iris,void* logger)
|
|
|
|
|
{
|
2020-01-22 20:49:45 +08:00
|
|
|
p_iris->idx_fp=fopen(p_iris->index_path,"w");
|
|
|
|
|
if(p_iris->idx_fp==NULL)
|
2015-10-10 18:30:12 +08:00
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
|
2020-01-22 20:49:45 +08:00
|
|
|
"index file %s fopen error %s.",p_iris->index_path, strerror(errno));
|
2015-10-10 18:30:12 +08:00
|
|
|
return -1;
|
|
|
|
|
}
|
2020-01-22 20:49:45 +08:00
|
|
|
MESA_htable_iterate(p_iris->iris_table_map, table_idx_write_cb, p_iris);
|
|
|
|
|
fclose(p_iris->idx_fp);
|
|
|
|
|
p_iris->idx_fp=NULL;
|
2015-10-10 18:30:12 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
2020-05-30 20:56:49 +08:00
|
|
|
static struct group_info_t* group_info_read(MESA_htable_handle table, const char* group_name)
|
|
|
|
|
{
|
|
|
|
|
return (struct group_info_t*)MESA_htable_search(table, (const unsigned char*)group_name, strlen(group_name));
|
|
|
|
|
}
|
|
|
|
|
static struct group_info_t* group_info_add_unsafe(struct iris_description_t* p_iris, MESA_htable_handle table, const char* group_name)
|
|
|
|
|
{
|
|
|
|
|
static struct group_info_t untitled_group;
|
|
|
|
|
struct group_info_t *group_info=NULL;
|
|
|
|
|
if(0==strncasecmp(group_name, untitled_group_name, strlen(untitled_group_name)))
|
|
|
|
|
{
|
|
|
|
|
group_info=&untitled_group;
|
|
|
|
|
group_info->group_id=get_group_seq(p_iris);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
group_info=ALLOC(struct group_info_t, 1);
|
|
|
|
|
group_info->group_id=get_group_seq(p_iris);
|
|
|
|
|
strncpy(group_info->group_name, group_name, sizeof(group_info->group_name));
|
|
|
|
|
MESA_htable_add(p_iris->group_name_map, (const unsigned char*)group_name, strlen(group_name), group_info);
|
|
|
|
|
}
|
|
|
|
|
return group_info;
|
|
|
|
|
}
|
2020-06-13 21:05:42 +08:00
|
|
|
|
|
|
|
|
int write_group_rule(cJSON *group_json, int parent_id, int parent_type, int tracking_compile_id, int Nth_group, struct iris_description_t *p_iris, void* logger)
|
2015-10-10 18:30:12 +08:00
|
|
|
{
|
2019-05-04 20:37:31 +08:00
|
|
|
const char* _str_parent_type[2]={"compile", "group"};
|
2020-06-13 21:05:42 +08:00
|
|
|
int ret=0, i=0;
|
2020-06-16 22:19:02 +08:00
|
|
|
int group_not_flag=0, clause_index=0;
|
2019-05-04 20:37:31 +08:00
|
|
|
cJSON *region_json=NULL, *item=NULL;
|
|
|
|
|
cJSON *sub_groups=NULL, *region_rule=NULL;
|
2019-07-28 15:03:33 +06:00
|
|
|
const char* group_name=NULL, *virtual_table=NULL;
|
2015-10-10 18:30:12 +08:00
|
|
|
struct group_info_t *group_info=NULL;
|
2019-05-04 20:37:31 +08:00
|
|
|
|
|
|
|
|
item=cJSON_GetObjectItem(group_json, "group_name");
|
|
|
|
|
if(item==NULL||item->type!=cJSON_String)
|
|
|
|
|
{
|
|
|
|
|
group_name=untitled_group_name;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
group_name=item->valuestring;
|
|
|
|
|
}
|
2020-06-13 21:05:42 +08:00
|
|
|
if(parent_type==PARENT_TYPE_COMPILE)
|
2019-05-04 20:37:31 +08:00
|
|
|
{
|
2020-06-13 21:05:42 +08:00
|
|
|
item=cJSON_GetObjectItem(group_json, "virtual_table");
|
|
|
|
|
if(item==NULL||item->type!=cJSON_String)
|
|
|
|
|
{
|
|
|
|
|
virtual_table="null";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
virtual_table=item->valuestring;
|
|
|
|
|
}
|
|
|
|
|
item=cJSON_GetObjectItem(group_json,"not_flag");
|
|
|
|
|
if(item==NULL||item->type!=cJSON_Number)
|
|
|
|
|
{
|
|
|
|
|
group_not_flag=0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
group_not_flag=item->valueint;
|
|
|
|
|
}
|
2020-06-16 22:19:02 +08:00
|
|
|
item=cJSON_GetObjectItem(group_json,"clause_index");
|
2020-06-13 21:05:42 +08:00
|
|
|
if(item==NULL||item->type!=cJSON_Number)
|
|
|
|
|
{
|
2020-06-16 22:19:02 +08:00
|
|
|
clause_index=Nth_group;
|
2020-06-13 21:05:42 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2020-06-16 22:19:02 +08:00
|
|
|
clause_index=item->valueint;
|
2020-06-13 21:05:42 +08:00
|
|
|
}
|
2019-05-04 20:37:31 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
group_not_flag=0;
|
|
|
|
|
}
|
2020-05-30 20:56:49 +08:00
|
|
|
group_info=group_info_read(p_iris->group_name_map, group_name);
|
2020-03-11 23:26:55 +08:00
|
|
|
if(group_info==NULL)//exist group name, regions and sub groups will be ommit.
|
2019-05-04 20:37:31 +08:00
|
|
|
{
|
2020-05-30 20:56:49 +08:00
|
|
|
group_info=group_info_add_unsafe(p_iris, p_iris->group_name_map, group_name);
|
2020-03-11 23:26:55 +08:00
|
|
|
region_json=cJSON_GetObjectItem(group_json,"regions");
|
|
|
|
|
if(region_json!=NULL)
|
2019-05-04 20:37:31 +08:00
|
|
|
{
|
2020-03-11 23:26:55 +08:00
|
|
|
cJSON_ArrayForEach(region_rule, region_json)
|
2019-05-04 20:37:31 +08:00
|
|
|
{
|
2020-03-11 23:26:55 +08:00
|
|
|
ret=write_region_rule(region_rule, tracking_compile_id, group_info->group_id, p_iris, logger);
|
|
|
|
|
if(ret<0)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
|
|
|
|
|
"compile rule %d write region error.", tracking_compile_id);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2019-05-04 20:37:31 +08:00
|
|
|
}
|
|
|
|
|
}
|
2020-03-11 23:26:55 +08:00
|
|
|
sub_groups=cJSON_GetObjectItem(group_json,"sub_groups");
|
|
|
|
|
if(sub_groups!=NULL)
|
2019-05-04 20:37:31 +08:00
|
|
|
{
|
2020-03-11 23:26:55 +08:00
|
|
|
//recursively
|
2020-06-13 21:05:42 +08:00
|
|
|
i=0;
|
2020-03-11 23:26:55 +08:00
|
|
|
cJSON_ArrayForEach(item, sub_groups)
|
2019-05-04 20:37:31 +08:00
|
|
|
{
|
2020-06-13 21:05:42 +08:00
|
|
|
i++;
|
|
|
|
|
ret=write_group_rule(item, group_info->group_id, PARENT_TYPE_GROUP, tracking_compile_id, i, p_iris, logger);
|
2020-03-11 23:26:55 +08:00
|
|
|
if(ret<0)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2020-06-13 21:05:42 +08:00
|
|
|
|
2019-05-04 20:37:31 +08:00
|
|
|
}
|
|
|
|
|
}
|
2020-03-11 23:26:55 +08:00
|
|
|
if(region_json==NULL && sub_groups==NULL)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger, RLOG_LV_INFO, maat_json,
|
|
|
|
|
"A group of compile rule %d has neither regions, sub groups, nor refered another exisited group.", tracking_compile_id);
|
|
|
|
|
}
|
2019-05-04 20:37:31 +08:00
|
|
|
}
|
2020-06-13 21:05:42 +08:00
|
|
|
if(parent_type==PARENT_TYPE_COMPILE)
|
|
|
|
|
{
|
2020-06-16 22:19:02 +08:00
|
|
|
ret=write_group2compile_line(group_info->group_id, parent_id, group_not_flag, clause_index, virtual_table, p_iris, logger);
|
2020-06-13 21:05:42 +08:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
ret=write_group2group_line(group_info->group_id, parent_id, p_iris, logger);
|
|
|
|
|
}
|
2020-03-11 23:26:55 +08:00
|
|
|
if(ret<0)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
|
|
|
|
|
"%s rule %d write group error.", _str_parent_type[parent_type], parent_id);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-04 20:37:31 +08:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
int write_iris(cJSON *json, struct iris_description_t *p_iris, void* logger)
|
|
|
|
|
{
|
2019-08-16 14:32:26 +08:00
|
|
|
int i=0;
|
|
|
|
|
int compile_id=-1, compile_cnt=0, group_cnt=0;
|
2019-05-04 20:37:31 +08:00
|
|
|
int ret=0;
|
2020-06-13 21:05:42 +08:00
|
|
|
cJSON *compile_array=NULL, *group_array=NULL, *plug_tables=NULL;
|
|
|
|
|
cJSON *compile_obj=NULL, *group_obj=NULL, *each_plug_table=NULL, *item=NULL;
|
2020-05-30 20:56:49 +08:00
|
|
|
static struct group_info_t* parent_group=NULL;
|
|
|
|
|
const char* parent_group_name=NULL;
|
|
|
|
|
|
2015-10-10 18:30:12 +08:00
|
|
|
plug_tables=cJSON_GetObjectItem(json,"plugin_table");
|
|
|
|
|
if(NULL!=plug_tables)
|
|
|
|
|
{
|
2020-06-13 21:05:42 +08:00
|
|
|
i=0;
|
2019-08-16 14:32:26 +08:00
|
|
|
cJSON_ArrayForEach(each_plug_table, plug_tables)
|
2015-10-10 18:30:12 +08:00
|
|
|
{
|
2019-05-04 20:37:31 +08:00
|
|
|
write_plugin_line(each_plug_table, i, p_iris, logger);
|
2020-06-13 21:05:42 +08:00
|
|
|
i++;
|
2015-10-10 18:30:12 +08:00
|
|
|
}
|
|
|
|
|
}
|
2020-06-13 21:05:42 +08:00
|
|
|
group_array=cJSON_GetObjectItem(json, "groups");//sub-group to group
|
|
|
|
|
if(group_array!=NULL)
|
2020-05-30 20:56:49 +08:00
|
|
|
{
|
2020-06-13 21:05:42 +08:00
|
|
|
cJSON_ArrayForEach(group_obj, group_array)
|
2020-05-30 20:56:49 +08:00
|
|
|
{
|
2020-06-13 21:05:42 +08:00
|
|
|
item=cJSON_GetObjectItem(group_obj, "parent_group");
|
2020-05-30 20:56:49 +08:00
|
|
|
if(item==NULL || item->type!=cJSON_String)
|
|
|
|
|
{
|
|
|
|
|
parent_group_name=untitled_group_name;
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
parent_group_name=item->string;
|
|
|
|
|
}
|
|
|
|
|
parent_group=group_info_read(p_iris->group_name_map, parent_group_name);
|
|
|
|
|
if(parent_group==NULL)
|
|
|
|
|
{
|
2020-06-16 21:31:26 +08:00
|
|
|
parent_group=group_info_add_unsafe(p_iris, p_iris->group_name_map, parent_group_name);
|
2020-05-30 20:56:49 +08:00
|
|
|
}
|
2020-06-13 21:05:42 +08:00
|
|
|
ret=write_group_rule(group_obj, parent_group->group_id, PARENT_TYPE_GROUP, 0, 0, p_iris, logger);
|
2020-05-30 20:56:49 +08:00
|
|
|
if(ret<0)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
2020-06-13 21:05:42 +08:00
|
|
|
compile_array=cJSON_GetObjectItem(json,"rules");
|
|
|
|
|
if(compile_array==NULL)
|
2015-10-10 18:30:12 +08:00
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
|
|
|
|
|
"have no rules.");
|
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
|
|
}
|
2020-06-13 21:05:42 +08:00
|
|
|
compile_cnt=cJSON_GetArraySize(compile_array);
|
2015-10-10 18:30:12 +08:00
|
|
|
if(compile_cnt<=0)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
|
|
|
|
|
"have no rules.");
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2020-06-13 21:05:42 +08:00
|
|
|
cJSON_ArrayForEach(compile_obj, compile_array)
|
2015-10-10 18:30:12 +08:00
|
|
|
{
|
2020-06-13 21:05:42 +08:00
|
|
|
compile_id=write_compile_line(compile_obj,p_iris, logger);
|
2015-10-10 18:30:12 +08:00
|
|
|
if(compile_id<0)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
|
|
|
|
|
"In %d compile rule.",i);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2020-06-13 21:05:42 +08:00
|
|
|
group_array=cJSON_GetObjectItem(compile_obj, "groups");
|
|
|
|
|
if(group_array==NULL)
|
2015-10-10 18:30:12 +08:00
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
|
|
|
|
|
"compile rule %d have no group.",compile_id);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2020-06-13 21:05:42 +08:00
|
|
|
group_cnt=cJSON_GetArraySize(group_array);
|
2015-10-10 18:30:12 +08:00
|
|
|
if(group_cnt<=0)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,
|
|
|
|
|
"compile rule %d have no groups.",compile_id);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2020-06-18 21:59:44 +08:00
|
|
|
i=0;
|
2020-06-13 21:05:42 +08:00
|
|
|
cJSON_ArrayForEach(group_obj, group_array)
|
2015-10-10 18:30:12 +08:00
|
|
|
{
|
2020-06-14 20:52:14 +08:00
|
|
|
ret=write_group_rule(group_obj, compile_id, PARENT_TYPE_COMPILE, compile_id, i, p_iris, logger);
|
2015-10-10 18:30:12 +08:00
|
|
|
if(ret<0)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
2020-06-14 20:52:14 +08:00
|
|
|
i++;
|
2015-10-10 18:30:12 +08:00
|
|
|
}
|
|
|
|
|
}
|
2020-01-22 18:25:01 +08:00
|
|
|
ret=write_index_file(p_iris, logger);
|
2015-10-10 18:30:12 +08:00
|
|
|
if(ret<0)
|
|
|
|
|
{
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2019-06-12 21:49:38 +08:00
|
|
|
// redis_write_ctx is used by maat_redis_tool to write json to redis.
|
2020-06-13 21:05:42 +08:00
|
|
|
int json2iris(const char* json_buff, const char* json_filename, const char*compile_tn, const char* group2compile_tn, const char* group2group_tn, redisContext *redis_write_ctx, char* iris_dir_buf, int buf_len, char* encrypt_key, char* encrypt_algo, void* logger)
|
2015-10-10 18:30:12 +08:00
|
|
|
{
|
2018-01-26 18:47:51 +08:00
|
|
|
cJSON *json=NULL, *tmp_obj=NULL;
|
2015-10-10 18:30:12 +08:00
|
|
|
int ret=-1;
|
|
|
|
|
struct iris_description_t iris_cfg;
|
2020-01-22 20:49:45 +08:00
|
|
|
memset(&iris_cfg, 0, sizeof(iris_cfg));
|
2015-10-10 18:30:12 +08:00
|
|
|
json=cJSON_Parse(json_buff);
|
|
|
|
|
if (!json)
|
|
|
|
|
{
|
|
|
|
|
MESA_handle_runtime_log(logger,RLOG_LV_FATAL,maat_json,"Error before: %-200.200s",cJSON_GetErrorPtr());
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
2020-01-22 20:49:45 +08:00
|
|
|
tmp_obj=cJSON_GetObjectItem(json, "compile_table");
|
2018-11-09 16:40:32 +08:00
|
|
|
if(tmp_obj)
|
2018-01-26 18:47:51 +08:00
|
|
|
{
|
|
|
|
|
compile_tn=tmp_obj->valuestring;
|
|
|
|
|
}
|
2018-11-09 16:40:32 +08:00
|
|
|
|
2020-06-13 21:05:42 +08:00
|
|
|
tmp_obj=cJSON_GetObjectItem(json, "group2compile_table");
|
2018-11-09 16:40:32 +08:00
|
|
|
if(tmp_obj)
|
2018-01-26 18:47:51 +08:00
|
|
|
{
|
2020-06-13 21:05:42 +08:00
|
|
|
group2compile_tn=tmp_obj->valuestring;
|
2018-01-26 18:47:51 +08:00
|
|
|
}
|
2020-06-13 21:05:42 +08:00
|
|
|
tmp_obj=cJSON_GetObjectItem(json, "group2group_table");
|
|
|
|
|
if(tmp_obj)
|
|
|
|
|
{
|
|
|
|
|
group2group_tn=tmp_obj->valuestring;
|
|
|
|
|
}
|
|
|
|
|
ret=set_iris_descriptor(json_filename, json, encrypt_key, encrypt_algo, compile_tn, group2compile_tn, group2group_tn, redis_write_ctx, &iris_cfg, logger);
|
2015-10-10 18:30:12 +08:00
|
|
|
if(ret<0)
|
|
|
|
|
{
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
|
|
|
|
ret=create_tmp_dir(&iris_cfg);
|
|
|
|
|
if(ret<0)
|
|
|
|
|
{
|
2020-01-13 19:05:24 +08:00
|
|
|
MESA_handle_runtime_log(logger, RLOG_LV_FATAL, maat_json,
|
2020-01-22 20:49:45 +08:00
|
|
|
"create tmp folder %s error", iris_cfg.tmp_iris_dir);
|
2015-10-10 18:30:12 +08:00
|
|
|
goto error_out;
|
|
|
|
|
}
|
2020-01-22 20:49:45 +08:00
|
|
|
ret=write_iris(json, &iris_cfg, logger);
|
2015-10-10 18:30:12 +08:00
|
|
|
if(ret<0)
|
|
|
|
|
{
|
|
|
|
|
goto error_out;
|
|
|
|
|
}
|
2020-01-22 20:49:45 +08:00
|
|
|
memcpy(iris_dir_buf, iris_cfg.tmp_iris_index_dir, MIN(strlen(iris_cfg.tmp_iris_index_dir)+1, (unsigned int)buf_len));
|
2015-10-10 18:30:12 +08:00
|
|
|
|
|
|
|
|
cJSON_Delete(json);
|
|
|
|
|
clear_iris_descriptor(&iris_cfg);
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
error_out:
|
|
|
|
|
cJSON_Delete(json);
|
|
|
|
|
clear_iris_descriptor(&iris_cfg);
|
|
|
|
|
return -1;
|
|
|
|
|
}
|
|
|
|
|
|