summaryrefslogtreecommitdiff
path: root/common/src/tfe_scan.cpp
blob: b746a1b07c5428c61136169249fc52a17e8f258b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#include <MESA/Maat_rule.h>
#include <tfe_resource.h>
#include <tfe_scan.h>

int tfe_scan_subscribe_id(const struct tfe_stream *stream, struct Maat_rule_t *result, scan_status_t *scan_mid,
                          int hit_cnt, unsigned int thread_id, void *logger, const char *addr)
{
    int scan_ret = 0;
    int hit_cnt_ip = 0;
    uint16_t opt_out_size;
    char dest_subscribe_id[TFE_STRING_MAX] = {0};
    char source_subscribe_id[TFE_STRING_MAX] = {0};
    struct tfe_cmsg *cmsg = tfe_stream_get0_cmsg(stream);
    if (cmsg != NULL)
    {
        scan_ret = tfe_cmsg_get_value(cmsg, TFE_CMSG_SRC_SUB_ID, (unsigned char *)source_subscribe_id, sizeof(source_subscribe_id), &opt_out_size);
        if (scan_ret != 0)
        {
            TFE_LOG_ERROR(logger, "fetch src sub id from cmsg failed, ret: %d addr: %s", scan_ret, addr);
        }
        scan_ret = tfe_cmsg_get_value(cmsg, TFE_CMSG_DST_SUB_ID, (unsigned char *)dest_subscribe_id, sizeof(dest_subscribe_id), &opt_out_size);
        if (scan_ret != 0)
        {
            TFE_LOG_ERROR(logger, "fetch dst sub id from cmsg failed, ret: %d addr: %s", scan_ret, addr);
        }
    }
    TFE_LOG_DEBUG(logger, "fetch src sub id:%s dst sub id:%s addr: %s", source_subscribe_id, dest_subscribe_id, addr);

    if (strlen(source_subscribe_id))
    {
        scan_ret = Maat_full_scan_string(tfe_bussiness_resouce_get(STATIC_MAAT), tfe_bussiness_tableid_get(TABLE_OBJ_SUBSCRIBER_ID),
                                         CHARSET_UTF8, source_subscribe_id, strlen(source_subscribe_id),
                                         result + hit_cnt + hit_cnt_ip, NULL, MAX_SCAN_RESULT - hit_cnt - hit_cnt_ip,
                                         scan_mid, (int)thread_id);
        if (scan_ret > 0)
        {
            TFE_LOG_INFO(logger, "Scan src TSG_OBJ_SUBSCRIBER_ID, Hit subid: %s scan ret: %d policy_id: %d service: %d action: %d addr: %s",
                         source_subscribe_id, scan_ret, result[hit_cnt + hit_cnt_ip].config_id, result[hit_cnt + hit_cnt_ip].service_id, result[hit_cnt + hit_cnt_ip].action, addr);
            hit_cnt_ip += scan_ret;
        }
        else
        {
            TFE_LOG_INFO(logger, "Scan src TSG_OBJ_SUBSCRIBER_ID, NO hit subid: %s scan ret: %d addr: %s",
                         source_subscribe_id, scan_ret, addr);
        }
    }

    if (strlen(dest_subscribe_id))
    {
        scan_ret = Maat_full_scan_string(tfe_bussiness_resouce_get(STATIC_MAAT), tfe_bussiness_tableid_get(TABLE_OBJ_SUBSCRIBER_ID),
                                         CHARSET_UTF8, dest_subscribe_id, strlen(dest_subscribe_id),
                                         result + hit_cnt + hit_cnt_ip, NULL, MAX_SCAN_RESULT - hit_cnt - hit_cnt_ip,
                                         scan_mid, (int)thread_id);
        if (scan_ret > 0)
        {
            TFE_LOG_INFO(logger, "Scan dst TSG_OBJ_SUBSCRIBER_ID, Hit subid: %s scan ret: %d policy_id: %d service: %d action: %d addr: %s",
                         dest_subscribe_id, scan_ret, result[hit_cnt + hit_cnt_ip].config_id, result[hit_cnt + hit_cnt_ip].service_id, result[hit_cnt + hit_cnt_ip].action, addr);
            hit_cnt_ip += scan_ret;
        }
        else
        {
            TFE_LOG_INFO(logger, "Scan dst TSG_OBJ_SUBSCRIBER_ID, NO hit subid: %s scan ret: %d addr: %s",
                         dest_subscribe_id, scan_ret, addr);
        }
    }

    return hit_cnt_ip;
}

int tfe_scan_fqdn_cat(const struct tfe_stream *stream, struct Maat_rule_t *result, scan_status_t *scan_mid,
					  int hit_cnt, unsigned int thread_id, void *logger, int table_id)
{
	int scan_ret = 0, i, fqdn_len =0;
	uint16_t opt_out_size;
	int category_num = 0, hit_cnt_fqdn = 0;
	char category_id_num[24] = {0};
	char category_id_val[TFE_SYMBOL_MAX] = {0};

    struct tfe_cmsg *cmsg = tfe_stream_get0_cmsg(stream);
	if (cmsg != NULL)
    {
        scan_ret = tfe_cmsg_get_value(cmsg, TFE_CMSG_SRC_FQDN_ID, (unsigned char *)category_id_val, sizeof(category_id_val), &opt_out_size);
        if (scan_ret != 0)
        {
            TFE_LOG_ERROR(logger, "fetch fqdn cat id from cmsg failed, ret: %d", scan_ret);
        }
		scan_ret = tfe_cmsg_get_value(cmsg, TFE_CMSG_SRC_FQDN_NUM, (unsigned char *)category_id_num, sizeof(category_id_num), &opt_out_size);
        if (scan_ret != 0)
        {
            TFE_LOG_ERROR(logger, "fetch fqdn cat id from cmsg failed, ret: %d", scan_ret);
        }
		category_num = atoll(category_id_num);
    }

	for(i = 0; i< category_num && i < 8; i++)
	{
		int fqdn_id =0;
		char fqdn_val[16]={0};

		snprintf(fqdn_val, sizeof(int), "%s", category_id_val + fqdn_len);
		fqdn_id = atoi(fqdn_val);
		scan_ret=Maat_scan_intval(tfe_bussiness_resouce_get(STATIC_MAAT), table_id, fqdn_id, result + hit_cnt + hit_cnt_fqdn,
								  MAX_SCAN_RESULT-hit_cnt-hit_cnt_fqdn, scan_mid, (int) thread_id);
		if(scan_ret>0)
		{
			hit_cnt_fqdn+=scan_ret;
		}
		fqdn_len += sizeof(int);
	}
	return hit_cnt_fqdn;
}

int tfe_scan_ip_location(const struct tfe_stream *stream, struct Maat_rule_t *result, scan_status_t *scan_mid,
                         int hit_cnt, unsigned int thread_id, void *logger, const char *addr, char **location_server, char **location_client)
{
    int scan_ret = 0;
    int hit_cnt_ip = 0;
    uint16_t opt_out_size;
    char buff[TFE_STRING_MAX] = {0};
    char src_city[TFE_STRING_MAX] = {0};
    char dst_city[TFE_STRING_MAX] = {0};
    char src_provine[TFE_STRING_MAX] = {0};
    char dst_provine[TFE_STRING_MAX] = {0};
    char src_country[TFE_STRING_MAX] = {0};
    char dst_country[TFE_STRING_MAX] = {0};
    struct tfe_cmsg *cmsg = tfe_stream_get0_cmsg(stream);
    if (cmsg != NULL)
    {
        scan_ret = tfe_cmsg_get_value(cmsg, TFE_CMSG_SRC_IP_LOCATION_COUNTRY, (unsigned char *)src_country, sizeof(src_country), &opt_out_size);
        if (scan_ret != 0)
        {
            TFE_LOG_ERROR(logger, "fetch src country from cmsg failed, ret: %d addr: %s", scan_ret, addr);
        }
        scan_ret = tfe_cmsg_get_value(cmsg, TFE_CMSG_DST_IP_LOCATION_COUNTRY, (unsigned char *)dst_country, sizeof(dst_country), &opt_out_size);
        if (scan_ret != 0)
        {
            TFE_LOG_ERROR(logger, "fetch dst country from cmsg failed, ret: %d addr: %s", scan_ret, addr);
        }
        scan_ret = tfe_cmsg_get_value(cmsg, TFE_CMSG_SRC_IP_LOCATION_PROVINE, (unsigned char *)src_provine, sizeof(src_provine), &opt_out_size);
        if (scan_ret != 0)
        {
            TFE_LOG_ERROR(logger, "fetch src provine from cmsg failed, ret: %d addr: %s", scan_ret, addr);
        }
        scan_ret = tfe_cmsg_get_value(cmsg, TFE_CMSG_DST_IP_LOCATION_PROVINE, (unsigned char *)dst_provine, sizeof(dst_provine), &opt_out_size);
        if (scan_ret != 0)
        {
            TFE_LOG_ERROR(logger, "fetch dst provine from cmsg failed, ret: %d addr: %s", scan_ret, addr);
        }
        scan_ret = tfe_cmsg_get_value(cmsg, TFE_CMSG_SRC_IP_LOCATION_CITY, (unsigned char *)src_city, sizeof(src_city), &opt_out_size);
        if (scan_ret != 0)
        {
            TFE_LOG_ERROR(logger, "fetch src city from cmsg failed, ret: %d addr: %s", scan_ret, addr);
        }
        scan_ret = tfe_cmsg_get_value(cmsg, TFE_CMSG_DST_IP_LOCATION_CITY, (unsigned char *)dst_city, sizeof(dst_city), &opt_out_size);
        if (scan_ret != 0)
        {
            TFE_LOG_ERROR(logger, "fetch dst city from cmsg failed, ret: %d addr: %s", scan_ret, addr);
        }
    }
    TFE_LOG_DEBUG(logger, "fetch src country:%s provine:%s city:%s; dst country:%s provine:%s city:%s addr: %s", src_country, src_provine, src_city, dst_country, dst_provine, dst_city, addr);

    if (strlen(dst_country) || strlen(dst_city))
    {
        memset(buff, 0, sizeof(buff));
        snprintf(buff, sizeof(buff), "%s.%s.", dst_country, dst_city);
        scan_ret = Maat_full_scan_string(tfe_bussiness_resouce_get(STATIC_MAAT), tfe_bussiness_tableid_get(TABLE_SECURITY_DESTINATION_LOCATION),
                                         CHARSET_GBK, buff, strlen(buff),
                                         result + hit_cnt + hit_cnt_ip, NULL, MAX_SCAN_RESULT - hit_cnt - hit_cnt_ip,
                                         scan_mid, (int)thread_id);
        if (scan_ret > 0)
        {
            TFE_LOG_INFO(logger, "Scan TSG_SECURITY_DESTINATION_LOCATION, Hit location: %s scan ret: %d policy_id: %d service: %d action: %d addr: %s",
                         buff, scan_ret, result[hit_cnt + hit_cnt_ip].config_id, result[hit_cnt + hit_cnt_ip].service_id, result[hit_cnt + hit_cnt_ip].action, addr);
            hit_cnt_ip += scan_ret;
        }
        else
        {
            TFE_LOG_INFO(logger, "Scan TSG_SECURITY_DESTINATION_LOCATION, NO hit location: %s scan ret: %d addr: %s",
                         buff, scan_ret, addr);
        }
        memset(buff, 0, sizeof(buff));
        snprintf(buff, sizeof(buff), "%s,%s,%s", dst_city, dst_provine, dst_country);
        *location_server = tfe_strdup(buff);
    }
    if (strlen(src_country) || strlen(src_city))
    {
        memset(buff, 0, sizeof(buff));
        snprintf(buff, sizeof(buff), "%s.%s.", src_country, src_city);
        scan_ret = Maat_full_scan_string(tfe_bussiness_resouce_get(STATIC_MAAT), tfe_bussiness_tableid_get(TABLE_SECURITY_SOURCE_LOCATION),
                                         CHARSET_GBK, buff, strlen(buff),
                                         result + hit_cnt + hit_cnt_ip, NULL, MAX_SCAN_RESULT - hit_cnt - hit_cnt_ip,
                                         scan_mid, (int)thread_id);
        if (scan_ret > 0)
        {
            TFE_LOG_INFO(logger, "Scan TSG_SECURITY_SOURCE_LOCATION, Hit location: %s scan ret: %d policy_id: %d service: %d action: %d addr: %s",
                         buff, scan_ret, result[hit_cnt + hit_cnt_ip].config_id, result[hit_cnt + hit_cnt_ip].service_id, result[hit_cnt + hit_cnt_ip].action, addr);
            hit_cnt_ip += scan_ret;
        }
        else
        {
            TFE_LOG_INFO(logger, "Scan TSG_SECURITY_SOURCE_LOCATION, NO hit location: %s scan ret: %d addr: %s",
                         buff, scan_ret, addr);
        }

        memset(buff, 0, sizeof(buff));
        snprintf(buff, sizeof(buff), "%s,%s,%s", src_city, src_provine, src_country);
        *location_client = tfe_strdup(buff);
    }

    return hit_cnt_ip;
}

int tfe_scan_ip_asn(const struct tfe_stream *stream, struct Maat_rule_t *result, scan_status_t *scan_mid,
                    int hit_cnt, unsigned int thread_id, void *logger, const char *addr, char **asn_server, char **asn_client)
{
    int scan_ret = 0;
    int hit_cnt_ip = 0;
    uint16_t opt_out_size;
    char buff[TFE_STRING_MAX] = {0};
    char src_asn[TFE_STRING_MAX] = {0};
    char dst_asn[TFE_STRING_MAX] = {0};
    char src_org[TFE_STRING_MAX] = {0};
    char dst_org[TFE_STRING_MAX] = {0};
    struct tfe_cmsg *cmsg = tfe_stream_get0_cmsg(stream);
    if (cmsg != NULL)
    {
        scan_ret = tfe_cmsg_get_value(cmsg, TFE_CMSG_SRC_ASN, (unsigned char *)src_asn, sizeof(src_asn), &opt_out_size);
        if (scan_ret != 0)
        {
            TFE_LOG_ERROR(logger, "fetch src asn from cmsg failed, ret: %d addr: %s", scan_ret, addr);
        }
        scan_ret = tfe_cmsg_get_value(cmsg, TFE_CMSG_DST_ASN, (unsigned char *)dst_asn, sizeof(dst_asn), &opt_out_size);
        if (scan_ret != 0)
        {
            TFE_LOG_ERROR(logger, "fetch dst asn from cmsg failed, ret: %d addr: %s", scan_ret, addr);
        }
        scan_ret = tfe_cmsg_get_value(cmsg, TFE_CMSG_SRC_ORGANIZATION, (unsigned char *)src_org, sizeof(src_org), &opt_out_size);
        if (scan_ret != 0)
        {
            TFE_LOG_ERROR(logger, "fetch src org from cmsg failed, ret: %d addr: %s", scan_ret, addr);
        }
        scan_ret = tfe_cmsg_get_value(cmsg, TFE_CMSG_DST_ORGANIZATION, (unsigned char *)dst_org, sizeof(dst_org), &opt_out_size);
        if (scan_ret != 0)
        {
            TFE_LOG_ERROR(logger, "fetch dst org from cmsg failed, ret: %d addr: %s", scan_ret, addr);
        }
    }
    TFE_LOG_DEBUG(logger, "fetch src asn:%s org:%s; dst asn:%s org:%s addr: %s", src_asn, src_org, dst_asn, dst_org, addr);

    if (strlen(dst_asn))
    {
        scan_ret = Maat_full_scan_string(tfe_bussiness_resouce_get(STATIC_MAAT), tfe_bussiness_tableid_get(TABLE_SECURITY_DESTINATION_ASN),
                                         CHARSET_UTF8, dst_asn, strlen(dst_asn),
                                         result + hit_cnt + hit_cnt_ip, NULL, MAX_SCAN_RESULT - hit_cnt - hit_cnt_ip,
                                         scan_mid, (int)thread_id);
        if (scan_ret > 0)
        {
            TFE_LOG_INFO(logger, "Scan TSG_SECURITY_DESTINATION_ASN, Hit asn: %s scan ret: %d policy_id: %d service: %d action: %d addr: %s",
                         dst_asn, scan_ret, result[hit_cnt + hit_cnt_ip].config_id, result[hit_cnt + hit_cnt_ip].service_id, result[hit_cnt + hit_cnt_ip].action, addr);
            hit_cnt_ip += scan_ret;
        }
        else
        {
            TFE_LOG_INFO(logger, "Scan TSG_SECURITY_DESTINATION_ASN, NO hit asn: %s scan ret: %d addr: %s",
                         dst_asn, scan_ret, addr);
        }
        memset(buff, 0, sizeof(buff));
        snprintf(buff, sizeof(buff), "%s(%s)", dst_asn, dst_org);
        *asn_server = tfe_strdup(buff);
    }
    if (strlen(src_asn))
    {
        scan_ret = Maat_full_scan_string(tfe_bussiness_resouce_get(STATIC_MAAT), tfe_bussiness_tableid_get(TABLE_SECURITY_SOURCE_ASN),
                                         CHARSET_UTF8, src_asn, strlen(src_asn),
                                         result + hit_cnt + hit_cnt_ip, NULL, MAX_SCAN_RESULT - hit_cnt - hit_cnt_ip,
                                         scan_mid, (int)thread_id);
        if (scan_ret > 0)
        {
            TFE_LOG_INFO(logger, "Scan TSG_SECURITY_SOURCE_ASN, Hit asn: %s scan ret: %d policy_id: %d service: %d action: %d addr: %s",
                         src_asn, scan_ret, result[hit_cnt + hit_cnt_ip].config_id, result[hit_cnt + hit_cnt_ip].service_id, result[hit_cnt + hit_cnt_ip].action, addr);
            hit_cnt_ip += scan_ret;
        }
        else
        {
            TFE_LOG_INFO(logger, "Scan TSG_SECURITY_SOURCE_ASN, NO hit asn: %s scan ret: %d addr: %s",
                         src_asn, scan_ret, addr);
        }

        memset(buff, 0, sizeof(buff));
        snprintf(buff, sizeof(buff), "%s(%s)", src_asn, src_org);
        *asn_client = tfe_strdup(buff);
    }
    return hit_cnt_ip;
}