/
controller.c
3951 lines (3333 loc) · 101 KB
/
controller.c
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* Copyright 2016 Vsevolod Stakhov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "config.h"
#include "libserver/dynamic_cfg.h"
#include "libserver/cfg_file_private.h"
#include "libutil/rrd.h"
#include "libutil/map.h"
#include "libutil/map_helpers.h"
#include "libutil/map_private.h"
#include "libutil/http_private.h"
#include "libutil/http_router.h"
#include "libstat/stat_api.h"
#include "rspamd.h"
#include "libserver/worker_util.h"
#include "worker_private.h"
#include "lua/lua_common.h"
#include "cryptobox.h"
#include "ottery.h"
#include "fuzzy_wire.h"
#include "unix-std.h"
#include "utlist.h"
#include "libmime/lang_detection.h"
#include <math.h>
/* 60 seconds for worker's IO */
#define DEFAULT_WORKER_IO_TIMEOUT 60000
#define DEFAULT_STATS_PATH RSPAMD_DBDIR "/stats.ucl"
/* HTTP paths */
#define PATH_AUTH "/auth"
#define PATH_SYMBOLS "/symbols"
#define PATH_ACTIONS "/actions"
#define PATH_MAPS "/maps"
#define PATH_GET_MAP "/getmap"
#define PATH_GRAPH "/graph"
#define PATH_PIE_CHART "/pie"
#define PATH_HISTORY "/history"
#define PATH_HISTORY_RESET "/historyreset"
#define PATH_LEARN_SPAM "/learnspam"
#define PATH_LEARN_HAM "/learnham"
#define PATH_SAVE_ACTIONS "/saveactions"
#define PATH_SAVE_SYMBOLS "/savesymbols"
#define PATH_SAVE_MAP "/savemap"
#define PATH_SCAN "/scan"
#define PATH_CHECK "/check"
#define PATH_CHECKV2 "/checkv2"
#define PATH_STAT "/stat"
#define PATH_STAT_RESET "/statreset"
#define PATH_COUNTERS "/counters"
#define PATH_ERRORS "/errors"
#define PATH_NEIGHBOURS "/neighbours"
#define PATH_PLUGINS "/plugins"
#define PATH_PING "/ping"
#define msg_err_session(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \
session->pool->tag.tagname, session->pool->tag.uid, \
G_STRFUNC, \
__VA_ARGS__)
#define msg_warn_session(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
session->pool->tag.tagname, session->pool->tag.uid, \
G_STRFUNC, \
__VA_ARGS__)
#define msg_info_session(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \
session->pool->tag.tagname, session->pool->tag.uid, \
G_STRFUNC, \
__VA_ARGS__)
#define msg_err_ctx(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \
"controller", ctx->cfg->cfg_pool->tag.uid, \
G_STRFUNC, \
__VA_ARGS__)
#define msg_warn_ctx(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
"controller", ctx->cfg->cfg_pool->tag.uid, \
G_STRFUNC, \
__VA_ARGS__)
#define msg_info_ctx(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \
"controller", ctx->cfg->cfg_pool->tag.uid, \
G_STRFUNC, \
__VA_ARGS__)
#define msg_debug_session(...) rspamd_conditional_debug_fast (NULL, session->from_addr, \
rspamd_controller_log_id, "controller", session->pool->tag.uid, \
G_STRFUNC, \
__VA_ARGS__)
INIT_LOG_MODULE(controller)
/* Graph colors */
#define COLOR_CLEAN "#58A458"
#define COLOR_PROBABLE_SPAM "#D67E7E"
#define COLOR_GREYLIST "#A0A0A0"
#define COLOR_REJECT "#CB4B4B"
#define COLOR_TOTAL "#9440ED"
const static ev_tstamp rrd_update_time = 1.0;
const static guint64 rspamd_controller_ctx_magic = 0xf72697805e6941faULL;
extern void fuzzy_stat_command (struct rspamd_task *task);
gpointer init_controller_worker (struct rspamd_config *cfg);
void start_controller_worker (struct rspamd_worker *worker);
worker_t controller_worker = {
"controller", /* Name */
init_controller_worker, /* Init function */
start_controller_worker, /* Start function */
RSPAMD_WORKER_HAS_SOCKET | RSPAMD_WORKER_KILLABLE |
RSPAMD_WORKER_SCANNER | RSPAMD_WORKER_CONTROLLER,
RSPAMD_WORKER_SOCKET_TCP, /* TCP socket */
RSPAMD_WORKER_VER /* Version info */
};
/*
* Worker's context
*/
struct rspamd_controller_worker_ctx {
guint64 magic;
/* Events base */
struct ev_loop *event_loop;
/* DNS resolver */
struct rspamd_dns_resolver *resolver;
/* Config */
struct rspamd_config *cfg;
/* END OF COMMON PART */
ev_tstamp timeout;
/* Whether we use ssl for this server */
gboolean use_ssl;
/* Webui password */
gchar *password;
/* Privilleged password */
gchar *enable_password;
/* Cached versions of the passwords */
rspamd_ftok_t cached_password;
rspamd_ftok_t cached_enable_password;
/* HTTP server */
struct rspamd_http_context *http_ctx;
struct rspamd_http_connection_router *http;
/* Server's start time */
ev_tstamp start_time;
/* Main server */
struct rspamd_main *srv;
/* SSL cert */
gchar *ssl_cert;
/* SSL private key */
gchar *ssl_key;
/* A map of secure IP */
const ucl_object_t *secure_ip;
struct rspamd_radix_map_helper *secure_map;
/* Static files dir */
gchar *static_files_dir;
/* Saved statistics path */
gchar *saved_stats_path;
/* Custom commands registered by plugins */
GHashTable *custom_commands;
/* Plugins registered from lua */
GHashTable *plugins;
/* Worker */
struct rspamd_worker *worker;
/* Local keypair */
gpointer key;
ev_timer rrd_event;
struct rspamd_rrd_file *rrd;
ev_timer save_stats_event;
struct rspamd_lang_detector *lang_det;
gdouble task_timeout;
};
struct rspamd_controller_plugin_cbdata {
lua_State *L;
struct rspamd_controller_worker_ctx *ctx;
gchar *plugin;
struct ucl_lua_funcdata *handler;
ucl_object_t *obj;
gboolean is_enable;
gboolean need_task;
guint version;
};
static gboolean
rspamd_is_encrypted_password (const gchar *password,
struct rspamd_controller_pbkdf const **pbkdf)
{
const gchar *start, *end;
gint64 id;
gsize size, i;
gboolean ret = FALSE;
const struct rspamd_controller_pbkdf *p;
if (password[0] == '$') {
/* Parse id */
start = password + 1;
end = start;
size = 0;
while (*end != '\0' && g_ascii_isdigit (*end)) {
size++;
end++;
}
if (size > 0) {
gchar *endptr;
id = strtoul (start, &endptr, 10);
if ((endptr == NULL || *endptr == *end)) {
for (i = 0; i < RSPAMD_PBKDF_ID_MAX - 1; i ++) {
p = &pbkdf_list[i];
if (p->id == id) {
ret = TRUE;
if (pbkdf != NULL) {
*pbkdf = &pbkdf_list[i];
}
break;
}
}
}
}
}
return ret;
}
static const gchar *
rspamd_encrypted_password_get_str (const gchar * password, gsize skip,
gsize * length)
{
const gchar *str, *start, *end;
gsize size;
start = password + skip;
end = start;
size = 0;
while (*end != '\0' && g_ascii_isalnum (*end)) {
size++;
end++;
}
if (size) {
str = start;
*length = size;
}
else {
str = NULL;
}
return str;
}
static gboolean
rspamd_check_encrypted_password (struct rspamd_controller_worker_ctx *ctx,
const rspamd_ftok_t * password, const gchar * check,
const struct rspamd_controller_pbkdf *pbkdf,
gboolean is_enable)
{
const gchar *salt, *hash;
gchar *salt_decoded, *key_decoded;
gsize salt_len = 0, key_len = 0;
gboolean ret = TRUE;
guchar *local_key;
rspamd_ftok_t *cache;
gpointer m;
/* First of all check cached versions to save resources */
if (is_enable && ctx->cached_enable_password.len != 0) {
if (password->len != ctx->cached_enable_password.len ||
!rspamd_constant_memcmp (password->begin,
ctx->cached_enable_password.begin, password->len)) {
msg_info_ctx ("incorrect or absent enable password has been specified");
return FALSE;
}
return TRUE;
}
else if (!is_enable && ctx->cached_password.len != 0) {
if (password->len != ctx->cached_password.len ||
!rspamd_constant_memcmp (password->begin,
ctx->cached_password.begin, password->len)) {
/* We still need to check enable password here */
if (ctx->cached_enable_password.len != 0) {
if (password->len != ctx->cached_enable_password.len ||
!rspamd_constant_memcmp (password->begin,
ctx->cached_enable_password.begin,
password->len)) {
msg_info_ctx (
"incorrect or absent password has been specified");
return FALSE;
}
else {
/* Cached matched */
return TRUE;
}
}
else {
/* We might want to check uncached version */
goto check_uncached;
}
}
else {
/* Cached matched */
return TRUE;
}
}
check_uncached:
g_assert (pbkdf != NULL);
/* get salt */
salt = rspamd_encrypted_password_get_str (check, 3, &salt_len);
/* get hash */
hash = rspamd_encrypted_password_get_str (check, 3 + salt_len + 1,
&key_len);
if (salt != NULL && hash != NULL) {
/* decode salt */
salt_decoded = rspamd_decode_base32 (salt, salt_len, &salt_len);
if (salt_decoded == NULL || salt_len != pbkdf->salt_len) {
/* We have some unknown salt here */
msg_info_ctx ("incorrect salt: %z, while %z expected",
salt_len, pbkdf->salt_len);
return FALSE;
}
key_decoded = rspamd_decode_base32 (hash, key_len, &key_len);
if (key_decoded == NULL || key_len != pbkdf->key_len) {
/* We have some unknown salt here */
msg_info_ctx ("incorrect key: %z, while %z expected",
key_len, pbkdf->key_len);
return FALSE;
}
local_key = g_alloca (pbkdf->key_len);
rspamd_cryptobox_pbkdf (password->begin, password->len,
salt_decoded, salt_len,
local_key, pbkdf->key_len, pbkdf->complexity,
pbkdf->type);
if (!rspamd_constant_memcmp (key_decoded, local_key, pbkdf->key_len)) {
msg_info_ctx ("incorrect or absent password has been specified");
ret = FALSE;
}
g_free (salt_decoded);
g_free (key_decoded);
}
if (ret) {
/* Save cached version */
cache = is_enable ? &ctx->cached_enable_password : &ctx->cached_password;
if (cache->len == 0) {
/* Mmap region */
#ifdef MAP_NOCORE
m = mmap (NULL, password->len, PROT_WRITE,
MAP_PRIVATE | MAP_ANON | MAP_NOCORE, -1, 0);
#else
m = mmap (NULL, password->len, PROT_WRITE,
MAP_PRIVATE | MAP_ANON, -1, 0);
#endif
if (m != MAP_FAILED) {
memcpy (m, password->begin, password->len);
(void)mprotect (m, password->len, PROT_READ);
(void)mlock (m, password->len);
cache->begin = m;
cache->len = password->len;
}
else {
msg_err_ctx ("cannot store cached password, mmap failed: %s",
strerror (errno));
}
}
}
return ret;
}
/**
* Checks for X-Forwarded-For header and update client's address if needed
*
* This function is intended to be called for a trusted client to ensure that
* a request is not proxied through it
* @return 0 if no forwarded found, 1 if forwarded found and it is yet trusted
* and -1 if forwarded is denied
*/
static gint
rspamd_controller_check_forwarded (struct rspamd_controller_session *session,
struct rspamd_http_message *msg,
struct rspamd_controller_worker_ctx *ctx)
{
const rspamd_ftok_t *hdr;
const gchar *comma;
const char *hdr_name = "X-Forwarded-For", *alt_hdr_name = "X-Real-IP";
char ip_buf[INET6_ADDRSTRLEN + 1];
rspamd_inet_addr_t *addr = NULL;
gint ret = 0;
hdr = rspamd_http_message_find_header (msg, hdr_name);
if (hdr) {
/*
* We need to parse and update the header
* X-Forwarded-For: client, proxy1, proxy2
*/
comma = rspamd_memrchr (hdr->begin, ',', hdr->len);
if (comma != NULL) {
while (comma < hdr->begin + hdr->len &&
(*comma == ',' || g_ascii_isspace (*comma))) {
comma ++;
}
}
else {
comma = hdr->begin;
}
if (rspamd_parse_inet_address (&addr, comma,
(hdr->begin + hdr->len) - comma)) {
/* We have addr now, so check if it is still trusted */
if (ctx->secure_map &&
rspamd_match_radix_map_addr (ctx->secure_map, addr) != NULL) {
/* rspamd_inet_address_to_string is not reentrant */
rspamd_strlcpy (ip_buf, rspamd_inet_address_to_string (addr),
sizeof (ip_buf));
msg_info_session ("allow unauthorized proxied connection "
"from a trusted IP %s via %s",
ip_buf,
rspamd_inet_address_to_string (session->from_addr));
ret = 1;
}
else {
ret = -1;
}
rspamd_inet_address_free (addr);
}
else {
msg_warn_session ("cannot parse forwarded IP: %T", hdr);
ret = -1;
}
}
else {
/* Try also X-Real-IP */
hdr = rspamd_http_message_find_header (msg, alt_hdr_name);
if (hdr) {
if (rspamd_parse_inet_address (&addr, hdr->begin, hdr->len)) {
/* We have addr now, so check if it is still trusted */
if (ctx->secure_map &&
rspamd_match_radix_map_addr (ctx->secure_map, addr) != NULL) {
/* rspamd_inet_address_to_string is not reentrant */
rspamd_strlcpy (ip_buf, rspamd_inet_address_to_string (addr),
sizeof (ip_buf));
msg_info_session ("allow unauthorized proxied connection "
"from a trusted IP %s via %s",
ip_buf,
rspamd_inet_address_to_string (session->from_addr));
ret = 1;
}
else {
ret = -1;
}
rspamd_inet_address_free (addr);
}
else {
msg_warn_session ("cannot parse real IP: %T", hdr);
ret = -1;
}
}
}
return ret;
}
/* Check for password if it is required by configuration */
static gboolean
rspamd_controller_check_password (struct rspamd_http_connection_entry *entry,
struct rspamd_controller_session *session,
struct rspamd_http_message *msg, gboolean is_enable)
{
const gchar *check;
const rspamd_ftok_t *password;
rspamd_ftok_t lookup;
GHashTable *query_args = NULL;
struct rspamd_controller_worker_ctx *ctx = session->ctx;
gboolean check_normal = TRUE, check_enable = TRUE, ret = TRUE,
use_enable = FALSE;
const struct rspamd_controller_pbkdf *pbkdf = NULL;
/* Access list logic */
if (rspamd_inet_address_get_af (session->from_addr) == AF_UNIX) {
ret = rspamd_controller_check_forwarded (session, msg, ctx);
if (ret == 1) {
session->is_enable = TRUE;
return TRUE;
}
else if (ret == 0) {
/* No forwarded found */
msg_info_session ("allow unauthorized connection from a unix socket");
session->is_enable = TRUE;
return TRUE;
}
}
else if (ctx->secure_map
&& rspamd_match_radix_map_addr (ctx->secure_map, session->from_addr)
!= NULL) {
ret = rspamd_controller_check_forwarded (session, msg, ctx);
if (ret == 1) {
session->is_enable = TRUE;
return TRUE;
}
else if (ret == 0) {
/* No forwarded found */
msg_info_session ("allow unauthorized connection from a trusted IP %s",
rspamd_inet_address_to_string (session->from_addr));
session->is_enable = TRUE;
return TRUE;
}
}
/* Password logic */
password = rspamd_http_message_find_header (msg, "Password");
if (password == NULL) {
/* Try to get password from query args */
query_args = rspamd_http_message_parse_query (msg);
lookup.begin = (gchar *)"password";
lookup.len = sizeof ("password") - 1;
password = g_hash_table_lookup (query_args, &lookup);
}
if (password == NULL) {
if (ctx->secure_map == NULL) {
if (ctx->password == NULL && !is_enable) {
return TRUE;
}
else if (is_enable && (ctx->password == NULL &&
ctx->enable_password == NULL)) {
session->is_enable = TRUE;
return TRUE;
}
}
msg_info_session ("absent password has been specified");
ret = FALSE;
}
else {
if (rspamd_ftok_cstr_equal (password, "q1", FALSE) ||
rspamd_ftok_cstr_equal (password, "q2", FALSE)) {
msg_info_session ("deny default password for remote access");
ret = FALSE;
goto end;
}
if (is_enable) {
/* For privileged commands we strictly require enable password */
if (ctx->enable_password != NULL) {
check = ctx->enable_password;
use_enable = TRUE;
}
else {
/* Use just a password (legacy mode) */
msg_info(
"using password as enable_password for a privileged command");
check = ctx->password;
}
if (check != NULL) {
if (!rspamd_is_encrypted_password (check, &pbkdf)) {
ret = FALSE;
if (strlen (check) == password->len) {
ret = rspamd_constant_memcmp (password->begin, check,
password->len);
}
}
else {
ret = rspamd_check_encrypted_password (ctx, password, check,
pbkdf, use_enable);
}
}
else {
msg_warn_session (
"no password to check while executing a privileged command");
ret = FALSE;
}
if (ret) {
session->is_enable = TRUE;
}
}
else {
/* Accept both normal and enable passwords */
if (ctx->password != NULL) {
check = ctx->password;
if (!rspamd_is_encrypted_password (check, &pbkdf)) {
check_normal = FALSE;
if (strlen (check) == password->len) {
check_normal = rspamd_constant_memcmp (password->begin,
check,
password->len);
}
}
else {
check_normal = rspamd_check_encrypted_password (ctx,
password,
check, pbkdf, FALSE);
}
}
else {
check_normal = FALSE;
}
if (ctx->enable_password != NULL) {
check = ctx->enable_password;
if (!rspamd_is_encrypted_password (check, &pbkdf)) {
check_enable = FALSE;
if (strlen (check) == password->len) {
check_enable = rspamd_constant_memcmp (password->begin,
check,
password->len);
}
}
else {
check_enable = rspamd_check_encrypted_password (ctx,
password,
check, pbkdf, TRUE);
}
if (check_enable) {
session->is_enable = TRUE;
}
}
else {
check_enable = FALSE;
if (check_normal) {
/*
* If no enable password is specified use normal password as
* enable password
*/
session->is_enable = TRUE;
}
}
}
}
if (check_normal == FALSE && check_enable == FALSE) {
msg_info ("absent or incorrect password has been specified");
ret = FALSE;
}
end:
if (query_args != NULL) {
g_hash_table_unref (query_args);
}
if (!ret) {
rspamd_controller_send_error (entry, 403, "Unauthorized");
}
return ret;
}
/* Command handlers */
/*
* Auth command handler:
* request: /auth
* headers: Password
* reply: json {"auth": "ok", "version": "0.5.2", "uptime": "some uptime", "error": "none"}
*/
static int
rspamd_controller_handle_auth (struct rspamd_http_connection_entry *conn_ent,
struct rspamd_http_message *msg)
{
struct rspamd_controller_session *session = conn_ent->ud;
struct rspamd_stat *st;
int64_t uptime;
gulong data[5];
ucl_object_t *obj;
if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) {
return 0;
}
obj = ucl_object_typed_new (UCL_OBJECT);
st = session->ctx->srv->stat;
data[0] = st->actions_stat[METRIC_ACTION_NOACTION];
data[1] = st->actions_stat[METRIC_ACTION_ADD_HEADER] +
st->actions_stat[METRIC_ACTION_REWRITE_SUBJECT];
data[2] = st->actions_stat[METRIC_ACTION_GREYLIST];
data[3] = st->actions_stat[METRIC_ACTION_REJECT];
data[4] = st->actions_stat[METRIC_ACTION_SOFT_REJECT];
/* Get uptime */
uptime = ev_time () - session->ctx->start_time;
ucl_object_insert_key (obj, ucl_object_fromstring (
RVERSION), "version", 0, false);
ucl_object_insert_key (obj, ucl_object_fromstring (
"ok"), "auth", 0, false);
ucl_object_insert_key (obj, ucl_object_fromint (
uptime), "uptime", 0, false);
ucl_object_insert_key (obj, ucl_object_fromint (
data[0]), "clean", 0, false);
ucl_object_insert_key (obj, ucl_object_fromint (
data[1]), "probable", 0, false);
ucl_object_insert_key (obj, ucl_object_fromint (
data[2]), "greylist", 0, false);
ucl_object_insert_key (obj, ucl_object_fromint (
data[3]), "reject", 0, false);
ucl_object_insert_key (obj, ucl_object_fromint (
data[4]), "soft_reject", 0, false);
ucl_object_insert_key (obj, ucl_object_fromint (
st->messages_scanned), "scanned", 0, false);
ucl_object_insert_key (obj, ucl_object_fromint (
st->messages_learned), "learned", 0, false);
ucl_object_insert_key (obj, ucl_object_frombool (!session->is_enable),
"read_only", 0, false);
ucl_object_insert_key (obj, ucl_object_fromstring (session->ctx->cfg->checksum),
"config_id", 0, false);
rspamd_controller_send_ucl (conn_ent, obj);
ucl_object_unref (obj);
return 0;
}
/*
* Symbols command handler:
* request: /symbols
* reply: json [{
* "name": "group_name",
* "symbols": [
* {
* "name": "name",
* "weight": 0.1,
* "description": "description of symbol"
* },
* {...}
* },
* {...}]
*/
static int
rspamd_controller_handle_symbols (struct rspamd_http_connection_entry *conn_ent,
struct rspamd_http_message *msg)
{
struct rspamd_controller_session *session = conn_ent->ud;
GHashTableIter it, sit;
struct rspamd_symbols_group *gr;
struct rspamd_symbol *sym;
ucl_object_t *obj, *top, *sym_obj, *group_symbols;
gpointer k, v;
if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) {
return 0;
}
top = ucl_object_typed_new (UCL_ARRAY);
/* Go through all symbols groups in the default metric */
g_hash_table_iter_init (&it, session->cfg->groups);
while (g_hash_table_iter_next (&it, &k, &v)) {
gr = v;
obj = ucl_object_typed_new (UCL_OBJECT);
ucl_object_insert_key (obj, ucl_object_fromstring (
gr->name), "group", 0, false);
/* Iterate through all symbols */
g_hash_table_iter_init (&sit, gr->symbols);
group_symbols = ucl_object_typed_new (UCL_ARRAY);
while (g_hash_table_iter_next (&sit, &k, &v)) {
gdouble tm = 0.0, freq = 0, freq_dev = 0;
sym = v;
sym_obj = ucl_object_typed_new (UCL_OBJECT);
ucl_object_insert_key (sym_obj, ucl_object_fromstring (sym->name),
"symbol", 0, false);
ucl_object_insert_key (sym_obj,
ucl_object_fromdouble (*sym->weight_ptr),
"weight", 0, false);
if (sym->description) {
ucl_object_insert_key (sym_obj,
ucl_object_fromstring (sym->description),
"description", 0, false);
}
if (rspamd_symcache_stat_symbol (session->ctx->cfg->cache,
sym->name, &freq, &freq_dev, &tm, NULL)) {
ucl_object_insert_key (sym_obj,
ucl_object_fromdouble (freq),
"frequency", 0, false);
ucl_object_insert_key (sym_obj,
ucl_object_fromdouble (freq_dev),
"frequency_stddev", 0, false);
ucl_object_insert_key (sym_obj,
ucl_object_fromdouble (tm),
"time", 0, false);
}
ucl_array_append (group_symbols, sym_obj);
}
ucl_object_insert_key (obj, group_symbols, "rules", 0, false);
ucl_array_append (top, obj);
}
rspamd_controller_send_ucl (conn_ent, top);
ucl_object_unref (top);
return 0;
}
/*
* Actions command handler:
* request: /actions
* reply: json [{
* "action": "no action",
* "value": 1.1
* },
* {...}]
*/
static int
rspamd_controller_handle_actions (struct rspamd_http_connection_entry *conn_ent,
struct rspamd_http_message *msg)
{
struct rspamd_controller_session *session = conn_ent->ud;
struct rspamd_action *act, *tmp;
ucl_object_t *obj, *top;
if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) {
return 0;
}
top = ucl_object_typed_new (UCL_ARRAY);
HASH_ITER (hh, session->cfg->actions, act, tmp) {
obj = ucl_object_typed_new (UCL_OBJECT);
ucl_object_insert_key (obj,
ucl_object_fromstring (act->name),
"action", 0, false);
ucl_object_insert_key (obj,
ucl_object_fromdouble (act->threshold),
"value", 0, false);
ucl_array_append (top, obj);
}
rspamd_controller_send_ucl (conn_ent, top);
ucl_object_unref (top);
return 0;
}
static gboolean
rspamd_controller_can_edit_map (struct rspamd_map_backend *bk)
{
gchar *fpath;
if (access (bk->uri, W_OK) == 0) {
return TRUE;
}
else if (access (bk->uri, R_OK) == -1 && errno == ENOENT) {
fpath = g_path_get_dirname (bk->uri);
if (fpath) {
if (access (fpath, W_OK) == 0) {
g_free (fpath);
return TRUE;
}
g_free (fpath);
}
}
return FALSE;
}
/*
* Maps command handler:
* request: /maps
* headers: Password
* reply: json [
* {
* "map": "name",
* "description": "description",
* "editable": true
* },
* {...}
* ]
*/
static int
rspamd_controller_handle_maps (struct rspamd_http_connection_entry *conn_ent,
struct rspamd_http_message *msg)
{
struct rspamd_controller_session *session = conn_ent->ud;
GList *cur;
struct rspamd_map *map;
struct rspamd_map_backend *bk;
guint i;
gboolean editable;
ucl_object_t *obj, *top;
if (!rspamd_controller_check_password (conn_ent, session, msg, FALSE)) {
return 0;
}
top = ucl_object_typed_new (UCL_ARRAY);
/* Iterate over all maps */
cur = session->ctx->cfg->maps;
while (cur) {
map = cur->data;
PTR_ARRAY_FOREACH (map->backends, i, bk) {
if (bk->protocol == MAP_PROTO_FILE) {
editable = rspamd_controller_can_edit_map (bk);
if (!editable && access (bk->uri, R_OK) == -1) {
/* Skip unreadable and non-existing maps */
continue;
}
obj = ucl_object_typed_new (UCL_OBJECT);
ucl_object_insert_key (obj, ucl_object_fromint (bk->id),
"map", 0, false);
if (map->description) {
ucl_object_insert_key (obj, ucl_object_fromstring (map->description),
"description", 0, false);
}
ucl_object_insert_key (obj, ucl_object_fromstring (bk->uri),
"uri", 0, false);
ucl_object_insert_key (obj, ucl_object_frombool (editable),
"editable", 0, false);
ucl_array_append (top, obj);
}
}
cur = g_list_next (cur);
}
rspamd_controller_send_ucl (conn_ent, top);
ucl_object_unref (top);
return 0;
}
/*
* Get map command handler:
* request: /getmap
* headers: Password, Map
* reply: plain-text
*/
static int
rspamd_controller_handle_get_map (struct rspamd_http_connection_entry *conn_ent,
struct rspamd_http_message *msg)
{
struct rspamd_controller_session *session = conn_ent->ud;
GList *cur;
struct rspamd_map *map;
struct rspamd_map_backend *bk = NULL;
const rspamd_ftok_t *idstr;
struct stat st;