aboutsummaryrefslogtreecommitdiffstats
path: root/src/client/rspamc.c
blob: ceb76169a0c2a37754e5aa458f93853da40cf559 (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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*
 * Copyright (c) 2011, Vsevolod Stakhov
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include "config.h"
#include "../../lib/librspamdclient.h"

#define PRINT_FUNC printf

#define DEFAULT_PORT 11333
#define DEFAULT_CONTROL_PORT 11334

static gchar                   *connect_str = "localhost";
static gchar                   *password;
static gchar                   *statfile;
static gchar                   *ip;
static gint                     weight = 1;
static gint                     flag;
static gboolean                 pass_all;
static gboolean                 tty = FALSE;
static gboolean                 verbose = FALSE;

static GOptionEntry entries[] =
{
		{ "connect", 'h', 0, G_OPTION_ARG_STRING, &connect_str, "Specify host and port", NULL },
		{ "password", 'P', 0, G_OPTION_ARG_STRING, &password, "Specify control password", NULL },
		{ "statfile", 's', 0, G_OPTION_ARG_STRING, &statfile, "Statfile to learn (symbol name)", NULL },
		{ "weight", 'w', 0, G_OPTION_ARG_INT, &weight, "Weight for fuzzy operations", NULL },
		{ "flag", 'f', 0, G_OPTION_ARG_INT, &flag, "Flag for fuzzy operations", NULL },
		{ "pass", 'p', 0, G_OPTION_ARG_NONE, &pass_all, "Pass all filters", NULL },
		{ "verbose", 'v', 0, G_OPTION_ARG_NONE, &verbose, "More verbose output", NULL },
		{ "ip", 'i', 0, G_OPTION_ARG_STRING, &ip, "Emulate that message was received from specified ip address", NULL },
		{ NULL, 0, 0, G_OPTION_ARG_NONE, NULL, NULL, NULL }
};

enum rspamc_command {
	RSPAMC_COMMAND_UNKNOWN = 0,
	RSPAMC_COMMAND_SYMBOLS,
	RSPAMC_COMMAND_LEARN,
	RSPAMC_COMMAND_FUZZY_ADD,
	RSPAMC_COMMAND_FUZZY_DEL,
	RSPAMC_COMMAND_STAT
};

/*
 * Parse command line
 */
static void
read_cmd_line (gint *argc, gchar ***argv)
{
	GError                         *error = NULL;
	GOptionContext                 *context;

	/* Prepare parser */
	context = g_option_context_new ("- run rspamc client");
	g_option_context_set_summary (context, "Summary:\n  Rspamd client version " RVERSION "\n  Release id: " RID);
	g_option_context_add_main_entries (context, entries, NULL);

	/* Parse options */
	if (!g_option_context_parse (context, argc, argv, &error)) {
		fprintf (stderr, "option parsing failed: %s\n", error->message);
		exit (EXIT_FAILURE);
	}

	/* Argc and argv are shifted after this function */
}

/*
 * Check rspamc command from string (used for arguments parsing)
 */
static enum rspamc_command
check_rspamc_command (const gchar *cmd)
{
	if (g_ascii_strcasecmp (cmd, "SYMBOLS") == 0 ||
		g_ascii_strcasecmp (cmd, "CHECK") == 0 ||
		g_ascii_strcasecmp (cmd, "REPORT") == 0) {
		/* These all are symbols, don't use other commands */
		return RSPAMC_COMMAND_SYMBOLS;
	}
	else if (g_ascii_strcasecmp (cmd, "LEARN") == 0) {
		return RSPAMC_COMMAND_LEARN;
	}
	else if (g_ascii_strcasecmp (cmd, "FUZZY_ADD") == 0) {
		return RSPAMC_COMMAND_FUZZY_ADD;
	}
	else if (g_ascii_strcasecmp (cmd, "FUZZY_DEL") == 0) {
		return RSPAMC_COMMAND_FUZZY_DEL;
	}
	else if (g_ascii_strcasecmp (cmd, "STAT") == 0) {
		return RSPAMC_COMMAND_STAT;
	}

	return RSPAMC_COMMAND_UNKNOWN;
}

/*
 * Parse connect_str and add server to librspamdclient
 */
static void
add_rspamd_server (gboolean is_control)
{
	gchar                         **vec, *err_str;
	guint16                         port;
	GError                         *err = NULL;

	if (connect_str == NULL) {
		fprintf (stderr, "cannot connect to rspamd server - empty string\n");
		exit (EXIT_FAILURE);
	}
	vec = g_strsplit_set (connect_str, ":", 2);
	if (vec == NULL || *vec == NULL) {
		fprintf (stderr, "cannot connect to rspamd server: %s\n", connect_str);
		exit (EXIT_FAILURE);
	}

	if (vec[1] == NULL) {
		port = is_control ? DEFAULT_CONTROL_PORT : DEFAULT_PORT;
	}
	else {
		port = strtoul (vec[1], &err_str, 10);
		if (*err_str != '\0') {
			fprintf (stderr, "cannot connect to rspamd server: %s, at pos %s\n", connect_str, err_str);
			exit (EXIT_FAILURE);
		}
	}

	if (! rspamd_add_server (vec[0], port, port, &err)) {
		fprintf (stderr, "cannot connect to rspamd server: %s, error: %s\n", connect_str, err->message);
		exit (EXIT_FAILURE);
	}
}

static void
show_metric_result (gpointer key, gpointer value, gpointer ud)
{
	struct rspamd_metric            *metric = value;
	struct rspamd_symbol            *s;
	GList                           *cur;
	GHashTableIter                   it;
	gpointer                         k, v;
	gboolean                         first;

	if (metric->is_skipped) {
		PRINT_FUNC ("\n%s: Skipped\n", (const gchar *)key);
	}
	else {
		if (tty) {
			PRINT_FUNC ("\n\033[1m%s:\033[0m %s [ %.2f / %.2f ]\n", (const gchar *)key,
						metric->score > metric->required_score ? "True" : "False",
						metric->score, metric->required_score);
		}
		else {
			PRINT_FUNC ("\n%s: %s [ %.2f / %.2f ]\n", (const gchar *)key,
						metric->score > metric->required_score ? "True" : "False",
						metric->score, metric->required_score);
		}
		if (tty) {
			if (metric->action) {
				PRINT_FUNC ("\033[1mAction:\033[0m %s\n", metric->action);
			}
			PRINT_FUNC ("\033[1mSymbols: \033[0m");
		}
		else {
			if (metric->action) {
				PRINT_FUNC ("Action: %s\n", metric->action);
			}
			else {
				PRINT_FUNC ("Symbols: ");
			}
		}
		if (metric->symbols) {
			first = TRUE;
			g_hash_table_iter_init (&it, metric->symbols);
			while (g_hash_table_iter_next (&it, &k, &v)) {
				s = v;
				if (verbose) {
					if (tty) {
						PRINT_FUNC ("\n\033[1mSymbol\033[0m - %s(%.2f)", s->name, s->weight);
					}
					else {
						PRINT_FUNC ("\nSymbol - %s(%.2f)", s->name, s->weight);
					}
					if (s->options) {
						PRINT_FUNC (": ");
						cur = g_list_first (s->options);
						while (cur) {
							if (cur->next) {
								PRINT_FUNC ("%s,", (const gchar *)cur->data);
							}
							else {
								PRINT_FUNC ("%s", (const gchar *)cur->data);
							}
							cur = g_list_next (cur);
						}
					}
					if (s->description) {
						PRINT_FUNC (" - \"%s\"", s->description);
					}
				}
				else {
					if (! first) {
						PRINT_FUNC (", ");
					}
					else {
						first = FALSE;
					}
					PRINT_FUNC ("%s(%.2f)", s->name, s->weight);

					if (s->options) {
						PRINT_FUNC ("(");
						cur = g_list_first (s->options);
						while (cur) {
							if (cur->next) {
								PRINT_FUNC ("%s,", (const gchar *)cur->data);
							}
							else {
								PRINT_FUNC ("%s)", (const gchar *)cur->data);
							}
							cur = g_list_next (cur);
						}
					}

				}
			}
		}
		PRINT_FUNC ("\n");
	}
}

static void
print_rspamd_result (struct rspamd_result *res)
{
	GHashTableIter                   it;
	gpointer                         k, v;

	g_assert (res != 0);

	if (tty) {
		printf ("\033[1m");
	}
	PRINT_FUNC ("Results for host: %s\n", connect_str);
	if (tty) {
		printf ("\033[0m");
	}
	g_hash_table_foreach (res->metrics, show_metric_result, NULL);
	/* Show other headers */
	g_hash_table_iter_init (&it, res->headers);
	PRINT_FUNC ("\n");
	while (g_hash_table_iter_next (&it, &k, &v)) {
		if (tty) {
			PRINT_FUNC ("\033[1m%s:\033[0m %s\n", (const gchar *)k, (const gchar *)v);
		}
		else {
			PRINT_FUNC ("%s: %s\n", (const gchar *)k, (const gchar *)v);
		}
	}
	PRINT_FUNC ("\n");
}

static void
add_options (GHashTable *opts)
{
	if (ip != NULL) {
		g_hash_table_insert (opts, "Ip", ip);
	}
	if (pass_all) {
		g_hash_table_insert (opts, "Pass", "all");
	}
}

/*
 * Scan STDIN
 */
static void
scan_rspamd_stdin ()
{
	gchar                           *in_buf;

	gint                             r = 0, len;
	GError                          *err = NULL;
	struct rspamd_result            *res;
	GHashTable                      *opts;

	/* Init options hash */
	opts = g_hash_table_new (g_str_hash, g_str_equal);
	add_options (opts);
	/* Add server */
	add_rspamd_server (FALSE);

	/* Allocate input buffer */
	len = BUFSIZ;
	in_buf = g_malloc (len);

	/* Read stdin */
	while (!feof (stdin)) {
		r += fread (in_buf + r, 1, len - r, stdin);
		if (len - r < len / 2) {
			/* Grow buffer */
			len *= 2;
			in_buf = g_realloc (in_buf, len);
		}
	}
	res = rspamd_scan_memory (in_buf, r, opts, &err);
	g_hash_table_destroy (opts);
	if (err != NULL) {
		fprintf (stderr, "cannot scan message: %s\n", err->message);
		exit (EXIT_FAILURE);
	}
	print_rspamd_result (res);
	rspamd_free_result (res);
}

static void
scan_rspamd_file (const gchar *file)
{
	GError                          *err = NULL;
	struct rspamd_result            *res;
	GHashTable                      *opts;

	/* Init options hash */
	opts = g_hash_table_new (g_str_hash, g_str_equal);
	add_options (opts);

	/* Add server */
	add_rspamd_server (FALSE);

	res = rspamd_scan_file (file, opts, &err);
	g_hash_table_destroy (opts);
	if (err != NULL) {
		fprintf (stderr, "cannot scan message: %s\n", err->message);
		return;
	}
	print_rspamd_result (res);
	if (res) {
		rspamd_free_result (res);
	}
}

static void
learn_rspamd_stdin ()
{
	gchar                           *in_buf;
	gint                             r = 0, len;
	GError                          *err = NULL;

	if (password == NULL || statfile == NULL) {
		fprintf (stderr, "cannot learn message without password and symbol name\n");
		exit (EXIT_FAILURE);
	}
	/* Add server */
	add_rspamd_server (TRUE);

	/* Allocate input buffer */
	len = BUFSIZ;
	in_buf = g_malloc (len);

	/* Read stdin */
	while (!feof (stdin)) {
		r += fread (in_buf + r, 1, len - r, stdin);
		if (len - r < len / 2) {
			/* Grow buffer */
			len *= 2;
			in_buf = g_realloc (in_buf, len);
		}
	}
	if (!rspamd_learn_memory (in_buf, r, statfile, password, &err)) {
		if (err != NULL) {
			fprintf (stderr, "cannot learn message: %s\n", err->message);
		}
		else {
			fprintf (stderr, "cannot learn message\n");
		}
		exit (EXIT_FAILURE);
	}
	else {
		if (tty) {
			printf ("\033[1m");
		}
		PRINT_FUNC ("Results for host: %s: learn ok\n", connect_str);
		if (tty) {
			printf ("\033[0m");
		}
	}
}

static void
learn_rspamd_file (const gchar *file)
{
	GError                          *err = NULL;

	if (password == NULL || statfile == NULL) {
		fprintf (stderr, "cannot learn message without password and symbol name\n");
		exit (EXIT_FAILURE);
	}
	/* Add server */
	add_rspamd_server (TRUE);

	if (!rspamd_learn_file (file, statfile, password, &err)) {
		if (err != NULL) {
			fprintf (stderr, "cannot learn message: %s\n", err->message);
		}
		else {
			fprintf (stderr, "cannot learn message\n");
		}
	}
	else {
		if (tty) {
			printf ("\033[1m");
		}
		PRINT_FUNC ("learn ok\n");
		if (tty) {
			printf ("\033[0m");
		}
	}
}

static void
fuzzy_rspamd_stdin (gboolean delete)
{
	gchar                           *in_buf;
	gint                             r = 0, len;
	GError                          *err = NULL;

	if (password == NULL) {
		fprintf (stderr, "cannot learn message without password\n");
		exit (EXIT_FAILURE);
	}
	/* Add server */
	add_rspamd_server (TRUE);

	/* Allocate input buffer */
	len = BUFSIZ;
	in_buf = g_malloc (len);

	/* Read stdin */
	while (!feof (stdin)) {
		r += fread (in_buf + r, 1, len - r, stdin);
		if (len - r < len / 2) {
			/* Grow buffer */
			len *= 2;
			in_buf = g_realloc (in_buf, len);
		}
	}
	if (!rspamd_fuzzy_memory (in_buf, r, password, weight, flag, delete, &err)) {
		if (err != NULL) {
			fprintf (stderr, "cannot learn message: %s\n", err->message);
		}
		else {
			fprintf (stderr, "cannot learn message\n");
		}
		exit (EXIT_FAILURE);
	}
	else {
		if (tty) {
			printf ("\033[1m");
		}
		PRINT_FUNC ("Results for host: %s: learn ok\n", connect_str);
		if (tty) {
			printf ("\033[0m");
		}
	}
}

static void
fuzzy_rspamd_file (const gchar *file, gboolean delete)
{
	GError                          *err = NULL;

	if (password == NULL) {
		fprintf (stderr, "cannot learn message without password\n");
		exit (EXIT_FAILURE);
	}
	/* Add server */
	add_rspamd_server (TRUE);

	if (!rspamd_fuzzy_file (file, password, weight, flag, delete, &err)) {
		if (err != NULL) {
			fprintf (stderr, "cannot learn message: %s\n", err->message);
		}
		else {
			fprintf (stderr, "cannot learn message\n");
		}
	}
	else {
		if (tty) {
			printf ("\033[1m");
		}
		PRINT_FUNC ("learn ok\n");
		if (tty) {
			printf ("\033[0m");
		}
	}
}

static void
rspamd_do_stat ()
{
	GError                          *err = NULL;
	GString                         *res;

	/* Add server */
	add_rspamd_server (TRUE);

	res = rspamd_get_stat (&err);
	if (res == NULL) {
		if (err != NULL) {
			fprintf (stderr, "cannot learn message: %s\n", err->message);
		}
		else {
			fprintf (stderr, "cannot learn message\n");
		}
		exit (EXIT_FAILURE);
	}
	if (tty) {
		printf ("\033[1m");
	}
	PRINT_FUNC ("Results for host: %s\n\n", connect_str);
	if (tty) {
		printf ("\033[0m");
	}
	res = g_string_append_c (res, '\0');
	printf ("%s\n", res->str);
}

gint
main (gint argc, gchar **argv, gchar **env)
{
	enum rspamc_command             cmd;
	gint                            i;

	rspamd_client_init ();

	read_cmd_line (&argc, &argv);
	tty = isatty (STDOUT_FILENO);
	/* Now read other args from argc and argv */
	if (argc == 1) {
		/* No args, just read stdin */
		scan_rspamd_stdin ();
	}
	else if (argc == 2) {
		/* One argument is whether command or filename */
		if ((cmd = check_rspamc_command (argv[1])) != RSPAMC_COMMAND_UNKNOWN) {
			/* In case of command read stdin */
			switch (cmd) {
			case RSPAMC_COMMAND_SYMBOLS:
				scan_rspamd_stdin ();
				break;
			case RSPAMC_COMMAND_LEARN:
				learn_rspamd_stdin ();
				break;
			case RSPAMC_COMMAND_FUZZY_ADD:
				fuzzy_rspamd_stdin (FALSE);
				break;
			case RSPAMC_COMMAND_FUZZY_DEL:
				fuzzy_rspamd_stdin (TRUE);
				break;
			case RSPAMC_COMMAND_STAT:
				rspamd_do_stat ();
				break;
			default:
				fprintf (stderr, "invalid arguments\n");
				exit (EXIT_FAILURE);
			}
		}
		else {
			scan_rspamd_file (argv[1]);
		}
	}
	else {
		if ((cmd = check_rspamc_command (argv[1])) != RSPAMC_COMMAND_UNKNOWN) {
			/* In case of command read arguments starting from 2 */
			for (i = 2; i < argc; i ++) {
				if (tty) {
					printf ("\033[1m");
				}
				PRINT_FUNC ("Results for file: %s\n\n", argv[i]);
				if (tty) {
					printf ("\033[0m");
				}
				switch (cmd) {
				case RSPAMC_COMMAND_SYMBOLS:
					scan_rspamd_file (argv[i]);
					break;
				case RSPAMC_COMMAND_LEARN:
					learn_rspamd_file (argv[i]);
					break;
				case RSPAMC_COMMAND_FUZZY_ADD:
					fuzzy_rspamd_file (argv[i], FALSE);
					break;
				case RSPAMC_COMMAND_FUZZY_DEL:
					fuzzy_rspamd_file (argv[i], TRUE);
					break;
				default:
					fprintf (stderr, "invalid arguments\n");
					exit (EXIT_FAILURE);
				}
			}
		}
		else {
			for (i = 1; i < argc; i ++) {
				scan_rspamd_file (argv[i]);
			}
		}
	}

	rspamd_client_close ();

	return 0;
}