Browse code

secfilter: machine-parsable data structure for RPC printing data

Jose Luis Verdeguer authored on 12/08/2022 11:13:04
Showing 1 changed files
... ...
@@ -161,22 +161,18 @@ void secf_rpc_reload(rpc_t *rpc, void *ctx)
161 161
 }
162 162
 
163 163
 
164
-/* Print str_list data */
165
-static void rpc_print_data(rpc_t *rpc, void *ctx, struct str_list *list)
166
-{
167
-	int i = 1;
168
-
169
-	while(list) {
170
-		rpc->rpl_printf(ctx, "    %04d -> %.*s", i, list->s.len, list->s.s);
171
-		list = list->next;
172
-		i++;
173
-	}
174
-}
175
-
176
-
177 164
 /* Print values */
178 165
 void secf_rpc_print(rpc_t *rpc, void *ctx)
179 166
 {
167
+	void *handle;
168
+	void *dsth, *dstbh;
169
+	void *uah, *uabh, *uawh;
170
+	void *ch, *cbh, *cwh;
171
+	void *dh, *dbh, *dwh;
172
+	void *iph, *ipbh, *ipwh;
173
+	void *usrh, *usrbh, *usrwh;
174
+	struct str_list *list;
175
+
180 176
 	str param = STR_NULL;
181 177
 	int showall = 0;
182 178
 
... ...
@@ -185,124 +181,291 @@ void secf_rpc_print(rpc_t *rpc, void *ctx)
185 181
 		
186 182
 	param.len = strlen(param.s);
187 183
 
188
-	if(!strncmp(param.s, "dst", param.len)) {
189
-		rpc->rpl_printf(ctx, "");
190
-		rpc->rpl_printf(ctx, "Destinations");
191
-		rpc->rpl_printf(ctx, "============");
192
-		rpc->rpl_printf(ctx, "[+] Blacklisted");
193
-		rpc->rpl_printf(ctx, "    -----------");
194
-		rpc_print_data(rpc, ctx, secf_data->bl.dst);
184
+	/* Create empty structure and obtain its handle */
185
+	if (rpc->add(ctx, "{", &handle) < 0) return;
186
+
187
+	if(showall == 1 || !strncmp(param.s, "dst", param.len)) {
188
+		if (rpc->struct_add(handle, "{", "Destinations", &dsth) < 0)
189
+		{
190
+			rpc->fault(ctx, 500, "Internal error creating inner struct");
191
+			return;
192
+		}
193
+
194
+		if (rpc->struct_add(dsth, "{", "Blacklisted", &dstbh) < 0)
195
+		{
196
+			rpc->fault(ctx, 500, "Internal error creating inner struct");
197
+			return;
198
+		}
199
+
200
+		list = secf_data->bl.dst;
201
+		while(list) {
202
+			if (rpc->struct_add(dstbh, "S", "Value", &list->s) < 0) {
203
+				rpc->fault(ctx, 500, "Internal error creating inner struct");
204
+				return;
205
+			}
206
+			list = list->next;
207
+		}
195 208
 	}
196 209
 
197 210
 	if(showall == 1 || !strncmp(param.s, "ua", param.len)) {
198
-		rpc->rpl_printf(ctx, "");
199
-		rpc->rpl_printf(ctx, "User-agent");
200
-		rpc->rpl_printf(ctx, "==========");
201
-		rpc->rpl_printf(ctx, "[+] Blacklisted");
202
-		rpc->rpl_printf(ctx, "    -----------");
203
-		rpc_print_data(rpc, ctx, secf_data->bl.ua);
204
-		rpc->rpl_printf(ctx, "");
205
-		rpc->rpl_printf(ctx, "[+] Whitelisted");
206
-		rpc->rpl_printf(ctx, "    -----------");
207
-		rpc_print_data(rpc, ctx, secf_data->wl.ua);
211
+		if (rpc->struct_add(handle, "{", "User-Agent", &uah) < 0)
212
+		{
213
+			rpc->fault(ctx, 500, "Internal error creating inner struct");
214
+			return;
215
+		}
216
+
217
+		if (rpc->struct_add(uah, "{", "Blacklisted", &uabh) < 0)
218
+		{
219
+			rpc->fault(ctx, 500, "Internal error creating inner struct");
220
+			return;
221
+		}
222
+
223
+		if (rpc->struct_add(uah, "{", "Whitelisted", &uawh) < 0)
224
+		{
225
+			rpc->fault(ctx, 500, "Internal error creating inner struct");
226
+			return;
227
+		}
228
+
229
+		list = secf_data->bl.ua;
230
+		while(list) {
231
+			if (rpc->struct_add(uabh, "S", "Value", &list->s.s) < 0) {
232
+				rpc->fault(ctx, 500, "Internal error creating inner struct");
233
+				return;
234
+			}
235
+			list = list->next;
236
+		}
237
+
238
+		list = secf_data->wl.ua;
239
+		while(list) {
240
+			if (rpc->struct_add(uawh, "S", "Value", &list->s) < 0) {
241
+				rpc->fault(ctx, 500, "Internal error creating inner struct");
242
+				return;
243
+			}
244
+			list = list->next;
245
+		}
208 246
 	}
209 247
 
210 248
 	if(showall == 1 || !strncmp(param.s, "country", param.len)) {
211
-		rpc->rpl_printf(ctx, "");
212
-		rpc->rpl_printf(ctx, "Country");
213
-		rpc->rpl_printf(ctx, "=======");
214
-		rpc->rpl_printf(ctx, "[+] Blacklisted");
215
-		rpc->rpl_printf(ctx, "    -----------");
216
-		rpc_print_data(rpc, ctx, secf_data->bl.country);
217
-		rpc->rpl_printf(ctx, "");
218
-		rpc->rpl_printf(ctx, "[+] Whitelisted");
219
-		rpc->rpl_printf(ctx, "    -----------");
220
-		rpc_print_data(rpc, ctx, secf_data->wl.country);
249
+		if (rpc->struct_add(handle, "{", "Country", &ch) < 0)
250
+		{
251
+			rpc->fault(ctx, 500, "Internal error creating inner struct");
252
+			return;
253
+		}
254
+
255
+		if (rpc->struct_add(ch, "{", "Blacklisted", &cbh) < 0)
256
+		{
257
+			rpc->fault(ctx, 500, "Internal error creating inner struct");
258
+			return;
259
+		}
260
+
261
+		if (rpc->struct_add(ch, "{", "Whitelisted", &cwh) < 0)
262
+		{
263
+			rpc->fault(ctx, 500, "Internal error creating inner struct");
264
+			return;
265
+		}
266
+
267
+		list = secf_data->bl.country;
268
+		while(list) {
269
+			if (rpc->struct_add(cbh, "S", "Value", &list->s) < 0) {
270
+				rpc->fault(ctx, 500, "Internal error creating inner struct");
271
+				return;
272
+			}
273
+			list = list->next;
274
+		}
275
+
276
+		list = secf_data->wl.country;
277
+		while(list) {
278
+			if (rpc->struct_add(cwh, "S", "Value", &list->s) < 0) {
279
+				rpc->fault(ctx, 500, "Internal error creating inner struct");
280
+				return;
281
+			}
282
+			list = list->next;
283
+		}
221 284
 	}
222 285
 
223 286
 	if(showall == 1 || !strncmp(param.s, "domain", param.len)) {
224
-		rpc->rpl_printf(ctx, "");
225
-		rpc->rpl_printf(ctx, "Domain");
226
-		rpc->rpl_printf(ctx, "======");
227
-		rpc->rpl_printf(ctx, "[+] Blacklisted");
228
-		rpc->rpl_printf(ctx, "    -----------");
229
-		rpc_print_data(rpc, ctx, secf_data->bl.domain);
230
-		rpc->rpl_printf(ctx, "");
231
-		rpc->rpl_printf(ctx, "[+] Whitelisted");
232
-		rpc->rpl_printf(ctx, "    -----------");
233
-		rpc_print_data(rpc, ctx, secf_data->wl.domain);
287
+		if (rpc->struct_add(handle, "{", "Domain", &dh) < 0)
288
+		{
289
+			rpc->fault(ctx, 500, "Internal error creating inner struct");
290
+			return;
291
+		}
292
+
293
+		if (rpc->struct_add(dh, "{", "Blacklisted", &dbh) < 0)
294
+		{
295
+			rpc->fault(ctx, 500, "Internal error creating inner struct");
296
+			return;
297
+		}
298
+
299
+		if (rpc->struct_add(dh, "{", "Whitelisted", &dwh) < 0)
300
+		{
301
+			rpc->fault(ctx, 500, "Internal error creating inner struct");
302
+			return;
303
+		}
304
+
305
+		list = secf_data->bl.domain;
306
+		while(list) {
307
+			if (rpc->struct_add(dbh, "S", "Value", &list->s) < 0) {
308
+				rpc->fault(ctx, 500, "Internal error creating inner struct");
309
+				return;
310
+			}
311
+			list = list->next;
312
+		}
313
+
314
+		list = secf_data->wl.domain;
315
+		while(list) {
316
+			if (rpc->struct_add(dwh, "S", "Value", &list->s) < 0) {
317
+				rpc->fault(ctx, 500, "Internal error creating inner struct");
318
+				return;
319
+			}
320
+			list = list->next;
321
+		}
234 322
 	}
235 323
 
236 324
 	if(showall == 1 || !strncmp(param.s, "ip", param.len)) {
237
-		rpc->rpl_printf(ctx, "");
238
-		rpc->rpl_printf(ctx, "IP Address");
239
-		rpc->rpl_printf(ctx, "==========");
240
-		rpc->rpl_printf(ctx, "[+] Blacklisted");
241
-		rpc->rpl_printf(ctx, "    -----------");
242
-		rpc_print_data(rpc, ctx, secf_data->bl.ip);
243
-		rpc->rpl_printf(ctx, "");
244
-		rpc->rpl_printf(ctx, "[+] Whitelisted");
245
-		rpc->rpl_printf(ctx, "    -----------");
246
-		rpc_print_data(rpc, ctx, secf_data->wl.ip);
325
+		if (rpc->struct_add(handle, "{", "IP-Address", &iph) < 0)
326
+		{
327
+			rpc->fault(ctx, 500, "Internal error creating inner struct");
328
+			return;
329
+		}
330
+
331
+		if (rpc->struct_add(iph, "{", "Blacklisted", &ipbh) < 0)
332
+		{
333
+			rpc->fault(ctx, 500, "Internal error creating inner struct");
334
+			return;
335
+		}
336
+
337
+		if (rpc->struct_add(iph, "{", "Whitelisted", &ipwh) < 0)
338
+		{
339
+			rpc->fault(ctx, 500, "Internal error creating inner struct");
340
+			return;
341
+		}
342
+
343
+		list = secf_data->bl.ip;
344
+		while(list) {
345
+			if (rpc->struct_add(ipbh, "S", "Value", &list->s) < 0) {
346
+				rpc->fault(ctx, 500, "Internal error creating inner struct");
347
+				return;
348
+			}
349
+			list = list->next;
350
+		}
351
+
352
+		list = secf_data->wl.ip;
353
+		while(list) {
354
+			if (rpc->struct_add(ipwh, "S", "Value", &list->s) < 0) {
355
+				rpc->fault(ctx, 500, "Internal error creating inner struct");
356
+				return;
357
+			}
358
+			list = list->next;
359
+		}
247 360
 	}
248 361
 
249 362
 	if(showall == 1 || !strncmp(param.s, "user", param.len)) {
250
-		rpc->rpl_printf(ctx, "");
251
-		rpc->rpl_printf(ctx, "User");
252
-		rpc->rpl_printf(ctx, "====");
253
-		rpc->rpl_printf(ctx, "[+] Blacklisted");
254
-		rpc->rpl_printf(ctx, "    -----------");
255
-		rpc_print_data(rpc, ctx, secf_data->bl.user);
256
-		rpc->rpl_printf(ctx, "");
257
-		rpc->rpl_printf(ctx, "[+] Whitelisted");
258
-		rpc->rpl_printf(ctx, "    -----------");
259
-		rpc_print_data(rpc, ctx, secf_data->wl.user);
363
+		if (rpc->struct_add(handle, "{", "Username", &usrh) < 0)
364
+		{
365
+			rpc->fault(ctx, 500, "Internal error creating inner struct");
366
+			return;
367
+		}
368
+
369
+		if (rpc->struct_add(usrh, "{", "Blacklisted", &usrbh) < 0)
370
+		{
371
+			rpc->fault(ctx, 500, "Internal error creating inner struct");
372
+			return;
373
+		}
374
+
375
+		if (rpc->struct_add(usrh, "{", "Whitelisted", &usrwh) < 0)
376
+		{
377
+			rpc->fault(ctx, 500, "Internal error creating inner struct");
378
+			return;
379
+		}
380
+
381
+		list = secf_data->bl.user;
382
+		while(list) {
383
+			if (rpc->struct_add(usrbh, "S", "Value", &list->s) < 0) {
384
+				rpc->fault(ctx, 500, "Internal error creating inner struct");
385
+				return;
386
+			}
387
+			list = list->next;
388
+		}
389
+
390
+		list = secf_data->wl.user;
391
+		while(list) {
392
+			if (rpc->struct_add(usrwh, "S", "Value", &list->s) < 0) {
393
+				rpc->fault(ctx, 500, "Internal error creating inner struct");
394
+				return;
395
+			}
396
+			list = list->next;
397
+		}
260 398
 	}
261
-
262
-	rpc->rpl_printf(ctx, "");
263 399
 }
264 400
 
265 401
 /* Print stats */
266 402
 void secf_rpc_stats(rpc_t *rpc, void *ctx)
267 403
 {
268
-	rpc->rpl_printf(ctx, "");
269
-	rpc->rpl_printf(ctx, "Blocked messages (blacklist)");
270
-	rpc->rpl_printf(ctx, "============================");
271
-	rpc->rpl_printf(ctx, "[+] By user-agent    : %d", secf_stats[BL_UA]);
272
-	rpc->rpl_printf(ctx, "[+] By country       : %d", secf_stats[BL_COUNTRY]);
273
-	rpc->rpl_printf(ctx, "[+] By from domain   : %d", secf_stats[BL_FDOMAIN]);
274
-	rpc->rpl_printf(ctx, "[+] By to domain     : %d", secf_stats[BL_TDOMAIN]);
275
-	rpc->rpl_printf(ctx, "[+] By contact domain: %d", secf_stats[BL_CDOMAIN]);
276
-	rpc->rpl_printf(ctx, "[+] By IP address    : %d", secf_stats[BL_IP]);
277
-	rpc->rpl_printf(ctx, "[+] By from name     : %d", secf_stats[BL_FNAME]);
278
-	rpc->rpl_printf(ctx, "[+] By to name       : %d", secf_stats[BL_TNAME]);
279
-	rpc->rpl_printf(ctx, "[+] By contact name  : %d", secf_stats[BL_CNAME]);
280
-	rpc->rpl_printf(ctx, "[+] By from user     : %d", secf_stats[BL_FUSER]);
281
-	rpc->rpl_printf(ctx, "[+] By to user       : %d", secf_stats[BL_TUSER]);
282
-	rpc->rpl_printf(ctx, "[+] By contact user  : %d", secf_stats[BL_CUSER]);
283
-
284
-	rpc->rpl_printf(ctx, "");
285
-	rpc->rpl_printf(ctx, "Allowed messages (whitelist)");
286
-	rpc->rpl_printf(ctx, "============================");
287
-	rpc->rpl_printf(ctx, "[+] By user-agent    : %d", secf_stats[WL_UA]);
288
-	rpc->rpl_printf(ctx, "[+] By country       : %d", secf_stats[WL_COUNTRY]);
289
-	rpc->rpl_printf(ctx, "[+] By from domain   : %d", secf_stats[WL_FDOMAIN]);
290
-	rpc->rpl_printf(ctx, "[+] By to domain     : %d", secf_stats[WL_TDOMAIN]);
291
-	rpc->rpl_printf(ctx, "[+] By contact domain: %d", secf_stats[WL_CDOMAIN]);
292
-	rpc->rpl_printf(ctx, "[+] By IP address    : %d", secf_stats[WL_IP]);
293
-	rpc->rpl_printf(ctx, "[+] By from name     : %d", secf_stats[WL_FNAME]);
294
-	rpc->rpl_printf(ctx, "[+] By to name       : %d", secf_stats[WL_TNAME]);
295
-	rpc->rpl_printf(ctx, "[+] By contact name  : %d", secf_stats[WL_CNAME]);
296
-	rpc->rpl_printf(ctx, "[+] By from user     : %d", secf_stats[WL_FUSER]);
297
-	rpc->rpl_printf(ctx, "[+] By to user       : %d", secf_stats[WL_TUSER]);
298
-	rpc->rpl_printf(ctx, "[+] By contact user  : %d", secf_stats[WL_CUSER]);
299
-
300
-	rpc->rpl_printf(ctx, "");
301
-	rpc->rpl_printf(ctx, "Other blocked messages");
302
-	rpc->rpl_printf(ctx, "======================");
303
-	rpc->rpl_printf(ctx, "[+] Destinations   : %d", secf_stats[BL_DST]);
304
-	rpc->rpl_printf(ctx, "[+] SQL injection  : %d", secf_stats[BL_SQL]);
305
-	rpc->rpl_printf(ctx, "");
404
+	void *handle;
405
+	void *bh;
406
+	void *wh;
407
+	void *oh;
408
+
409
+	/* Create empty structure and obtain its handle */
410
+	if (rpc->add(ctx, "{", &handle) < 0) return;
411
+
412
+	/* Create branchesempty structure and obtain its handle */
413
+	if (rpc->struct_add(handle, "{", "Blacklist", &bh) < 0)
414
+	{
415
+		rpc->fault(ctx, 500, "Internal error creating inner struct");
416
+		return;
417
+	}
418
+
419
+	if (rpc->struct_add(handle, "{", "Whitelist", &wh) < 0)
420
+	{
421
+		rpc->fault(ctx, 500, "Internal error creating inner struct");
422
+		return;
423
+	}
424
+
425
+	if (rpc->struct_add(handle, "{", "Other", &oh) < 0)
426
+	{
427
+		rpc->fault(ctx, 500, "Internal error creating inner struct");
428
+		return;
429
+	}
430
+
431
+	/* Fill-in the structure */
432
+	if (rpc->struct_add(bh, "dddddddddddd", "User-Agent", secf_stats[BL_UA],
433
+			"Country", secf_stats[BL_COUNTRY],
434
+			"From-Domain", secf_stats[BL_FDOMAIN],
435
+			"To-Domain", secf_stats[BL_TDOMAIN],
436
+			"Contact-Domain", secf_stats[BL_CDOMAIN],
437
+			"IP-Address", secf_stats[BL_IP],
438
+			"From-Name", secf_stats[BL_FNAME],
439
+			"To-Name", secf_stats[BL_TNAME],
440
+			"Contact-Name", secf_stats[BL_CNAME],
441
+			"From-User", secf_stats[BL_FUSER],
442
+			"To-User", secf_stats[BL_TUSER],
443
+			"Contact-User", secf_stats[BL_CUSER]) < 0) {
444
+		rpc->fault(ctx, 500, "Internal error creating inner struct");
445
+		return;
446
+	}
447
+
448
+	if (rpc->struct_add(wh, "dddddddddddd", "User-Agent", secf_stats[WL_UA],
449
+			"Country", secf_stats[WL_COUNTRY],
450
+			"From-Domain", secf_stats[WL_FDOMAIN],
451
+			"To-Domain", secf_stats[WL_TDOMAIN],
452
+			"Contact-Domain", secf_stats[WL_CDOMAIN],
453
+			"IP-Address", secf_stats[WL_IP],
454
+			"From-Name", secf_stats[WL_FNAME],
455
+			"To-Name", secf_stats[WL_TNAME],
456
+			"Contact-Name", secf_stats[WL_CNAME],
457
+			"From-User", secf_stats[WL_FUSER],
458
+			"To-User", secf_stats[WL_TUSER],
459
+			"Contact-User", secf_stats[WL_CUSER]) < 0) {
460
+		rpc->fault(ctx, 500, "Internal error creating inner struct");
461
+		return;
462
+	}
463
+
464
+	if (rpc->struct_add(oh, "dd", "Destination", secf_stats[BL_DST],
465
+			"SQL-Injection", secf_stats[BL_SQL]) < 0) {
466
+		rpc->fault(ctx, 500, "Internal error creating inner struct");
467
+		return;
468
+	}
306 469
 }
307 470
 
308 471
 /* Reset stats */