You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cfg_utils.c 50KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121
  1. /*-
  2. * Copyright 2016 Vsevolod Stakhov
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "config.h"
  17. #include "cfg_file.h"
  18. #include "rspamd.h"
  19. #include "uthash_strcase.h"
  20. #include "filter.h"
  21. #include "lua/lua_common.h"
  22. #include "lua/lua_thread_pool.h"
  23. #include "map.h"
  24. #include "map_helpers.h"
  25. #include "map_private.h"
  26. #include "dynamic_cfg.h"
  27. #include "utlist.h"
  28. #include "stat_api.h"
  29. #include "unix-std.h"
  30. #include "libutil/multipattern.h"
  31. #include "monitored.h"
  32. #include "ref.h"
  33. #include <math.h>
  34. #define DEFAULT_SCORE 10.0
  35. #define DEFAULT_RLIMIT_NOFILE 2048
  36. #define DEFAULT_RLIMIT_MAXCORE 0
  37. #define DEFAULT_MAP_TIMEOUT 60.0 * 5
  38. #define DEFAULT_MAP_FILE_WATCH_MULTIPLIER 1
  39. #define DEFAULT_MIN_WORD 0
  40. #define DEFAULT_MAX_WORD 40
  41. #define DEFAULT_WORDS_DECAY 600
  42. #define DEFAULT_MAX_MESSAGE (50 * 1024 * 1024)
  43. #define DEFAULT_MAX_PIC (1 * 1024 * 1024)
  44. #define DEFAULT_MAX_SHOTS 100
  45. #define DEFAULT_MAX_SESSIONS 100
  46. #define DEFAULT_MAX_WORKERS 4
  47. /* Timeout for task processing */
  48. #define DEFAULT_TASK_TIMEOUT 8.0
  49. struct rspamd_ucl_map_cbdata {
  50. struct rspamd_config *cfg;
  51. GString *buf;
  52. };
  53. static gchar * rspamd_ucl_read_cb (gchar * chunk,
  54. gint len,
  55. struct map_cb_data *data,
  56. gboolean final);
  57. static void rspamd_ucl_fin_cb (struct map_cb_data *data);
  58. static void rspamd_ucl_dtor_cb (struct map_cb_data *data);
  59. guint rspamd_config_log_id = (guint)-1;
  60. RSPAMD_CONSTRUCTOR(rspamd_config_log_init)
  61. {
  62. rspamd_config_log_id = rspamd_logger_add_debug_module("config");
  63. }
  64. gboolean
  65. rspamd_parse_bind_line (struct rspamd_config *cfg,
  66. struct rspamd_worker_conf *cf,
  67. const gchar *str)
  68. {
  69. struct rspamd_worker_bind_conf *cnf;
  70. gchar *err;
  71. gboolean ret = TRUE;
  72. if (str == NULL) {
  73. return FALSE;
  74. }
  75. cnf =
  76. rspamd_mempool_alloc0 (cfg->cfg_pool,
  77. sizeof (struct rspamd_worker_bind_conf));
  78. cnf->cnt = 1024;
  79. cnf->bind_line = str;
  80. if (g_ascii_strncasecmp (str, "systemd:", sizeof ("systemd:") - 1) == 0) {
  81. /* The actual socket will be passed by systemd environment */
  82. cnf->is_systemd = TRUE;
  83. cnf->cnt = strtoul (str + sizeof ("systemd:") - 1, &err, 10);
  84. cnf->addrs = NULL;
  85. if (err == NULL || *err == '\0') {
  86. cnf->name = rspamd_mempool_strdup (cfg->cfg_pool, str);
  87. LL_PREPEND (cf->bind_conf, cnf);
  88. }
  89. else {
  90. msg_err_config ("cannot parse bind line: %s", str);
  91. ret = FALSE;
  92. }
  93. }
  94. else {
  95. if (!rspamd_parse_host_port_priority (str, &cnf->addrs,
  96. NULL, &cnf->name, DEFAULT_BIND_PORT, cfg->cfg_pool)) {
  97. msg_err_config ("cannot parse bind line: %s", str);
  98. ret = FALSE;
  99. }
  100. else {
  101. cnf->cnt = cnf->addrs->len;
  102. LL_PREPEND (cf->bind_conf, cnf);
  103. }
  104. }
  105. return ret;
  106. }
  107. struct rspamd_config *
  108. rspamd_config_new (enum rspamd_config_init_flags flags)
  109. {
  110. struct rspamd_config *cfg;
  111. cfg = g_malloc0 (sizeof (*cfg));
  112. /* Allocate larger pool for cfg */
  113. cfg->cfg_pool = rspamd_mempool_new (8 * 1024 * 1024, "cfg");
  114. cfg->dns_timeout = 1000;
  115. cfg->dns_retransmits = 5;
  116. /* After 20 errors do throttling for 10 seconds */
  117. cfg->dns_throttling_errors = 20;
  118. cfg->dns_throttling_time = 10000;
  119. /* 16 sockets per DNS server */
  120. cfg->dns_io_per_server = 16;
  121. /* Disable timeout */
  122. cfg->task_timeout = DEFAULT_TASK_TIMEOUT;
  123. rspamd_config_init_metric (cfg);
  124. cfg->composite_symbols =
  125. g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
  126. cfg->classifiers_symbols = g_hash_table_new (rspamd_str_hash,
  127. rspamd_str_equal);
  128. cfg->cfg_params = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
  129. cfg->debug_modules = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
  130. cfg->explicit_modules = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
  131. cfg->wrk_parsers = g_hash_table_new (g_int_hash, g_int_equal);
  132. cfg->trusted_keys = g_hash_table_new (rspamd_str_hash,
  133. rspamd_str_equal);
  134. cfg->map_timeout = DEFAULT_MAP_TIMEOUT;
  135. cfg->map_file_watch_multiplier = DEFAULT_MAP_FILE_WATCH_MULTIPLIER;
  136. cfg->log_level = G_LOG_LEVEL_WARNING;
  137. cfg->log_flags = RSPAMD_LOG_FLAG_DEFAULT;
  138. cfg->check_text_attachements = TRUE;
  139. cfg->dns_max_requests = 64;
  140. cfg->history_rows = 200;
  141. cfg->log_error_elts = 10;
  142. cfg->log_error_elt_maxlen = 1000;
  143. cfg->cache_reload_time = 30.0;
  144. /* Default log line */
  145. cfg->log_format_str = "id: <$mid>,$if_qid{ qid: <$>,}$if_ip{ ip: $,}"
  146. "$if_user{ user: $,}$if_smtp_from{ from: <$>,} (default: $is_spam "
  147. "($action): [$scores] [$symbols_scores_params]), len: $len, time: $time_real real,"
  148. " $time_virtual virtual, dns req: $dns_req, digest: <$digest>"
  149. "$if_smtp_rcpts{ rcpts: <$>, }$if_mime_rcpt{ mime_rcpt: <$>, }";
  150. /* Allow non-mime input by default */
  151. cfg->allow_raw_input = TRUE;
  152. /* Default maximum words processed */
  153. cfg->words_decay = DEFAULT_WORDS_DECAY;
  154. cfg->min_word_len = DEFAULT_MIN_WORD;
  155. cfg->max_word_len = DEFAULT_MAX_WORD;
  156. if (!(flags & RSPAMD_CONFIG_INIT_SKIP_LUA)) {
  157. cfg->lua_state = rspamd_lua_init ();
  158. cfg->own_lua_state = TRUE;
  159. cfg->lua_thread_pool = lua_thread_pool_new (cfg->lua_state);
  160. }
  161. cfg->cache = rspamd_symcache_new (cfg);
  162. cfg->ups_ctx = rspamd_upstreams_library_init ();
  163. cfg->re_cache = rspamd_re_cache_new ();
  164. cfg->doc_strings = ucl_object_typed_new (UCL_OBJECT);
  165. /*
  166. * Unless exim is fixed
  167. */
  168. cfg->enable_shutdown_workaround = TRUE;
  169. cfg->ssl_ciphers = "HIGH:!aNULL:!kRSA:!PSK:!SRP:!MD5:!RC4";
  170. cfg->max_message = DEFAULT_MAX_MESSAGE;
  171. cfg->max_pic_size = DEFAULT_MAX_PIC;
  172. cfg->images_cache_size = 256;
  173. cfg->monitored_ctx = rspamd_monitored_ctx_init ();
  174. cfg->neighbours = ucl_object_typed_new (UCL_OBJECT);
  175. #ifdef WITH_HIREDIS
  176. cfg->redis_pool = rspamd_redis_pool_init ();
  177. #endif
  178. cfg->default_max_shots = DEFAULT_MAX_SHOTS;
  179. cfg->max_sessions_cache = DEFAULT_MAX_SESSIONS;
  180. cfg->maps_cache_dir = rspamd_mempool_strdup (cfg->cfg_pool, RSPAMD_DBDIR);
  181. cfg->c_modules = g_ptr_array_new ();
  182. REF_INIT_RETAIN (cfg, rspamd_config_free);
  183. return cfg;
  184. }
  185. void
  186. rspamd_config_free (struct rspamd_config *cfg)
  187. {
  188. struct rspamd_config_post_load_script *sc, *sctmp;
  189. struct rspamd_worker_log_pipe *lp, *ltmp;
  190. DL_FOREACH_SAFE (cfg->finish_callbacks, sc, sctmp) {
  191. luaL_unref (cfg->lua_state, LUA_REGISTRYINDEX, sc->cbref);
  192. g_free (sc);
  193. }
  194. DL_FOREACH_SAFE (cfg->on_load, sc, sctmp) {
  195. luaL_unref (cfg->lua_state, LUA_REGISTRYINDEX, sc->cbref);
  196. g_free (sc);
  197. }
  198. rspamd_map_remove_all (cfg);
  199. rspamd_mempool_destructors_enforce (cfg->cfg_pool);
  200. g_list_free (cfg->classifiers);
  201. g_list_free (cfg->workers);
  202. rspamd_symcache_destroy (cfg->cache);
  203. ucl_object_unref (cfg->rcl_obj);
  204. ucl_object_unref (cfg->config_comments);
  205. ucl_object_unref (cfg->doc_strings);
  206. ucl_object_unref (cfg->neighbours);
  207. g_hash_table_remove_all (cfg->composite_symbols);
  208. g_hash_table_unref (cfg->composite_symbols);
  209. g_hash_table_remove_all (cfg->cfg_params);
  210. g_hash_table_unref (cfg->cfg_params);
  211. g_hash_table_unref (cfg->classifiers_symbols);
  212. g_hash_table_unref (cfg->debug_modules);
  213. g_hash_table_unref (cfg->explicit_modules);
  214. g_hash_table_unref (cfg->wrk_parsers);
  215. g_hash_table_unref (cfg->trusted_keys);
  216. rspamd_re_cache_unref (cfg->re_cache);
  217. rspamd_upstreams_library_unref (cfg->ups_ctx);
  218. g_ptr_array_free (cfg->c_modules, TRUE);
  219. if (cfg->lua_state && cfg->own_lua_state) {
  220. lua_thread_pool_free (cfg->lua_thread_pool);
  221. lua_close (cfg->lua_state);
  222. }
  223. #ifdef WITH_HIREDIS
  224. if (cfg->redis_pool) {
  225. rspamd_redis_pool_destroy (cfg->redis_pool);
  226. }
  227. #endif
  228. if (cfg->monitored_ctx) {
  229. rspamd_monitored_ctx_destroy (cfg->monitored_ctx);
  230. }
  231. rspamd_mempool_delete (cfg->cfg_pool);
  232. if (cfg->checksum) {
  233. g_free (cfg->checksum);
  234. }
  235. REF_RELEASE (cfg->libs_ctx);
  236. DL_FOREACH_SAFE (cfg->log_pipes, lp, ltmp) {
  237. close (lp->fd);
  238. g_free (lp);
  239. }
  240. g_free (cfg);
  241. }
  242. const ucl_object_t *
  243. rspamd_config_get_module_opt (struct rspamd_config *cfg,
  244. const gchar *module_name,
  245. const gchar *opt_name)
  246. {
  247. const ucl_object_t *res = NULL, *sec;
  248. sec = ucl_obj_get_key (cfg->rcl_obj, module_name);
  249. if (sec != NULL) {
  250. res = ucl_obj_get_key (sec, opt_name);
  251. }
  252. return res;
  253. }
  254. gchar
  255. rspamd_config_parse_flag (const gchar *str, guint len)
  256. {
  257. gchar c;
  258. if (!str || !*str) {
  259. return -1;
  260. }
  261. if (len == 0) {
  262. len = strlen (str);
  263. }
  264. switch (len) {
  265. case 1:
  266. c = g_ascii_tolower (*str);
  267. if (c == 'y' || c == '1') {
  268. return 1;
  269. }
  270. else if (c == 'n' || c == '0') {
  271. return 0;
  272. }
  273. break;
  274. case 2:
  275. if (g_ascii_strncasecmp (str, "no", len) == 0) {
  276. return 0;
  277. }
  278. else if (g_ascii_strncasecmp (str, "on", len) == 0) {
  279. return 1;
  280. }
  281. break;
  282. case 3:
  283. if (g_ascii_strncasecmp (str, "yes", len) == 0) {
  284. return 1;
  285. }
  286. else if (g_ascii_strncasecmp (str, "off", len) == 0) {
  287. return 0;
  288. }
  289. break;
  290. case 4:
  291. if (g_ascii_strncasecmp (str, "true", len) == 0) {
  292. return 1;
  293. }
  294. break;
  295. case 5:
  296. if (g_ascii_strncasecmp (str, "false", len) == 0) {
  297. return 0;
  298. }
  299. break;
  300. }
  301. return -1;
  302. }
  303. static gboolean
  304. rspamd_config_process_var (struct rspamd_config *cfg, const rspamd_ftok_t *var,
  305. const rspamd_ftok_t *content)
  306. {
  307. guint flags = RSPAMD_LOG_FLAG_DEFAULT;
  308. struct rspamd_log_format *lf;
  309. enum rspamd_log_format_type type;
  310. rspamd_ftok_t tok;
  311. gint id;
  312. g_assert (var != NULL);
  313. if (var->len > 3 && rspamd_lc_cmp (var->begin, "if_", 3) == 0) {
  314. flags |= RSPAMD_LOG_FMT_FLAG_CONDITION;
  315. tok.begin = var->begin + 3;
  316. tok.len = var->len - 3;
  317. }
  318. else {
  319. tok.begin = var->begin;
  320. tok.len = var->len;
  321. }
  322. /* Now compare variable and check what we have */
  323. if (rspamd_ftok_cstr_equal (&tok, "mid", TRUE)) {
  324. type = RSPAMD_LOG_MID;
  325. }
  326. else if (rspamd_ftok_cstr_equal (&tok, "qid", TRUE)) {
  327. type = RSPAMD_LOG_QID;
  328. }
  329. else if (rspamd_ftok_cstr_equal (&tok, "user", TRUE)) {
  330. type = RSPAMD_LOG_USER;
  331. }
  332. else if (rspamd_ftok_cstr_equal (&tok, "is_spam", TRUE)) {
  333. type = RSPAMD_LOG_ISSPAM;
  334. }
  335. else if (rspamd_ftok_cstr_equal (&tok, "action", TRUE)) {
  336. type = RSPAMD_LOG_ACTION;
  337. }
  338. else if (rspamd_ftok_cstr_equal (&tok, "scores", TRUE)) {
  339. type = RSPAMD_LOG_SCORES;
  340. }
  341. else if (rspamd_ftok_cstr_equal (&tok, "symbols", TRUE)) {
  342. type = RSPAMD_LOG_SYMBOLS;
  343. }
  344. else if (rspamd_ftok_cstr_equal (&tok, "symbols_scores", TRUE)) {
  345. type = RSPAMD_LOG_SYMBOLS;
  346. flags |= RSPAMD_LOG_FMT_FLAG_SYMBOLS_SCORES;
  347. }
  348. else if (rspamd_ftok_cstr_equal (&tok, "symbols_params", TRUE)) {
  349. type = RSPAMD_LOG_SYMBOLS;
  350. flags |= RSPAMD_LOG_FMT_FLAG_SYMBOLS_PARAMS;
  351. }
  352. else if (rspamd_ftok_cstr_equal (&tok, "symbols_scores_params", TRUE)) {
  353. type = RSPAMD_LOG_SYMBOLS;
  354. flags |= RSPAMD_LOG_FMT_FLAG_SYMBOLS_PARAMS|RSPAMD_LOG_FMT_FLAG_SYMBOLS_SCORES;
  355. }
  356. else if (rspamd_ftok_cstr_equal (&tok, "ip", TRUE)) {
  357. type = RSPAMD_LOG_IP;
  358. }
  359. else if (rspamd_ftok_cstr_equal (&tok, "len", TRUE)) {
  360. type = RSPAMD_LOG_LEN;
  361. }
  362. else if (rspamd_ftok_cstr_equal (&tok, "dns_req", TRUE)) {
  363. type = RSPAMD_LOG_DNS_REQ;
  364. }
  365. else if (rspamd_ftok_cstr_equal (&tok, "smtp_from", TRUE)) {
  366. type = RSPAMD_LOG_SMTP_FROM;
  367. }
  368. else if (rspamd_ftok_cstr_equal (&tok, "mime_from", TRUE)) {
  369. type = RSPAMD_LOG_MIME_FROM;
  370. }
  371. else if (rspamd_ftok_cstr_equal (&tok, "smtp_rcpt", TRUE)) {
  372. type = RSPAMD_LOG_SMTP_RCPT;
  373. }
  374. else if (rspamd_ftok_cstr_equal (&tok, "mime_rcpt", TRUE)) {
  375. type = RSPAMD_LOG_MIME_RCPT;
  376. }
  377. else if (rspamd_ftok_cstr_equal (&tok, "smtp_rcpts", TRUE)) {
  378. type = RSPAMD_LOG_SMTP_RCPTS;
  379. }
  380. else if (rspamd_ftok_cstr_equal (&tok, "mime_rcpts", TRUE)) {
  381. type = RSPAMD_LOG_MIME_RCPTS;
  382. }
  383. else if (rspamd_ftok_cstr_equal (&tok, "time_real", TRUE)) {
  384. type = RSPAMD_LOG_TIME_REAL;
  385. }
  386. else if (rspamd_ftok_cstr_equal (&tok, "time_virtual", TRUE)) {
  387. type = RSPAMD_LOG_TIME_VIRTUAL;
  388. }
  389. else if (rspamd_ftok_cstr_equal (&tok, "lua", TRUE)) {
  390. type = RSPAMD_LOG_LUA;
  391. }
  392. else if (rspamd_ftok_cstr_equal (&tok, "digest", TRUE) ||
  393. rspamd_ftok_cstr_equal (&tok, "checksum", TRUE)) {
  394. type = RSPAMD_LOG_DIGEST;
  395. }
  396. else if (rspamd_ftok_cstr_equal (&tok, "filename", TRUE)) {
  397. type = RSPAMD_LOG_FILENAME;
  398. }
  399. else if (rspamd_ftok_cstr_equal (&tok, "forced_action", TRUE)) {
  400. type = RSPAMD_LOG_FORCED_ACTION;
  401. }
  402. else {
  403. msg_err_config ("unknown log variable: %T", &tok);
  404. return FALSE;
  405. }
  406. lf = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*lf));
  407. lf->type = type;
  408. lf->flags = flags;
  409. if (type != RSPAMD_LOG_LUA) {
  410. if (content && content->len > 0) {
  411. lf->data = rspamd_mempool_alloc0 (cfg->cfg_pool,
  412. sizeof (rspamd_ftok_t));
  413. memcpy (lf->data, content, sizeof (*content));
  414. lf->len = sizeof (*content);
  415. }
  416. }
  417. else {
  418. /* Load lua code and ensure that we have function ref returned */
  419. if (!content || content->len == 0) {
  420. msg_err_config ("lua variable needs content: %T", &tok);
  421. return FALSE;
  422. }
  423. if (luaL_loadbuffer (cfg->lua_state, content->begin, content->len,
  424. "lua log variable") != 0) {
  425. msg_err_config ("error loading lua code: '%T': %s", content,
  426. lua_tostring (cfg->lua_state, -1));
  427. return FALSE;
  428. }
  429. if (lua_pcall (cfg->lua_state, 0, 1, 0) != 0) {
  430. msg_err_config ("error executing lua code: '%T': %s", content,
  431. lua_tostring (cfg->lua_state, -1));
  432. lua_pop (cfg->lua_state, 1);
  433. return FALSE;
  434. }
  435. if (lua_type (cfg->lua_state, -1) != LUA_TFUNCTION) {
  436. msg_err_config ("lua variable should return function: %T", content);
  437. lua_pop (cfg->lua_state, 1);
  438. return FALSE;
  439. }
  440. id = luaL_ref (cfg->lua_state, LUA_REGISTRYINDEX);
  441. lf->data = GINT_TO_POINTER (id);
  442. lf->len = 0;
  443. }
  444. DL_APPEND (cfg->log_format, lf);
  445. return TRUE;
  446. }
  447. static gboolean
  448. rspamd_config_parse_log_format (struct rspamd_config *cfg)
  449. {
  450. const gchar *p, *c, *end, *s;
  451. gchar *d;
  452. struct rspamd_log_format *lf = NULL;
  453. rspamd_ftok_t var, var_content;
  454. enum {
  455. parse_str,
  456. parse_dollar,
  457. parse_var_name,
  458. parse_var_content,
  459. } state = parse_str;
  460. gint braces = 0;
  461. g_assert (cfg != NULL);
  462. c = cfg->log_format_str;
  463. if (c == NULL) {
  464. return FALSE;
  465. }
  466. p = c;
  467. end = p + strlen (p);
  468. while (p < end) {
  469. switch (state) {
  470. case parse_str:
  471. if (*p == '$') {
  472. state = parse_dollar;
  473. }
  474. else {
  475. p ++;
  476. }
  477. break;
  478. case parse_dollar:
  479. if (p > c) {
  480. /* We have string element that we need to store */
  481. lf = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*lf));
  482. lf->type = RSPAMD_LOG_STRING;
  483. lf->data = rspamd_mempool_alloc (cfg->cfg_pool, p - c + 1);
  484. /* Filter \r\n from the destination */
  485. s = c;
  486. d = lf->data;
  487. while (s < p) {
  488. if (*s != '\r' && *s != '\n') {
  489. *d++ = *s++;
  490. }
  491. else {
  492. *d ++ = ' ';
  493. s++;
  494. }
  495. }
  496. *d = '\0';
  497. lf->len = d - (char *) lf->data;
  498. DL_APPEND (cfg->log_format, lf);
  499. lf = NULL;
  500. }
  501. p++;
  502. c = p;
  503. state = parse_var_name;
  504. break;
  505. case parse_var_name:
  506. if (*p == '{') {
  507. var.begin = c;
  508. var.len = p - c;
  509. p ++;
  510. c = p;
  511. state = parse_var_content;
  512. braces = 1;
  513. }
  514. else if (*p != '_' && *p != '-' && !g_ascii_isalnum (*p)) {
  515. /* Variable with no content */
  516. var.begin = c;
  517. var.len = p - c;
  518. c = p;
  519. if (!rspamd_config_process_var (cfg, &var, NULL)) {
  520. return FALSE;
  521. }
  522. state = parse_str;
  523. }
  524. else {
  525. p++;
  526. }
  527. break;
  528. case parse_var_content:
  529. if (*p == '}' && --braces == 0) {
  530. var_content.begin = c;
  531. var_content.len = p - c;
  532. p ++;
  533. c = p;
  534. if (!rspamd_config_process_var (cfg, &var, &var_content)) {
  535. return FALSE;
  536. }
  537. state = parse_str;
  538. }
  539. else if (*p == '{') {
  540. braces ++;
  541. p ++;
  542. }
  543. else {
  544. p++;
  545. }
  546. break;
  547. }
  548. }
  549. /* Last state */
  550. switch (state) {
  551. case parse_str:
  552. if (p > c) {
  553. /* We have string element that we need to store */
  554. lf = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*lf));
  555. lf->type = RSPAMD_LOG_STRING;
  556. lf->data = rspamd_mempool_alloc (cfg->cfg_pool, p - c + 1);
  557. /* Filter \r\n from the destination */
  558. s = c;
  559. d = lf->data;
  560. while (s < p) {
  561. if (*s != '\r' && *s != '\n') {
  562. *d++ = *s++;
  563. }
  564. else {
  565. *d++ = ' ';
  566. s++;
  567. }
  568. }
  569. *d = '\0';
  570. lf->len = d - (char *)lf->data;
  571. DL_APPEND (cfg->log_format, lf);
  572. lf = NULL;
  573. }
  574. break;
  575. case parse_var_name:
  576. var.begin = c;
  577. var.len = p - c;
  578. if (!rspamd_config_process_var (cfg, &var, NULL)) {
  579. return FALSE;
  580. }
  581. break;
  582. case parse_dollar:
  583. case parse_var_content:
  584. msg_err_config ("cannot parse log format %s: incomplete string",
  585. cfg->log_format_str);
  586. return FALSE;
  587. break;
  588. }
  589. return TRUE;
  590. }
  591. static void
  592. rspamd_urls_config_dtor (gpointer _unused)
  593. {
  594. rspamd_url_deinit ();
  595. }
  596. /*
  597. * Perform post load actions
  598. */
  599. gboolean
  600. rspamd_config_post_load (struct rspamd_config *cfg,
  601. enum rspamd_post_load_options opts)
  602. {
  603. #ifdef HAVE_CLOCK_GETTIME
  604. struct timespec ts;
  605. #endif
  606. gboolean ret = TRUE;
  607. #ifdef HAVE_CLOCK_GETTIME
  608. #ifdef HAVE_CLOCK_PROCESS_CPUTIME_ID
  609. clock_getres (CLOCK_PROCESS_CPUTIME_ID, &ts);
  610. # elif defined(HAVE_CLOCK_VIRTUAL)
  611. clock_getres (CLOCK_VIRTUAL, &ts);
  612. # else
  613. clock_getres (CLOCK_REALTIME, &ts);
  614. # endif
  615. rspamd_logger_configure_modules (cfg->debug_modules);
  616. cfg->clock_res = log10 (1000000. / ts.tv_nsec);
  617. if (cfg->clock_res < 0) {
  618. cfg->clock_res = 0;
  619. }
  620. if (cfg->clock_res > 3) {
  621. cfg->clock_res = 3;
  622. }
  623. #else
  624. /* For gettimeofday */
  625. cfg->clock_res = 1;
  626. #endif
  627. if (cfg->one_shot_mode) {
  628. msg_info_config ("enabling one shot mode (was %d max shots)",
  629. cfg->default_max_shots);
  630. cfg->default_max_shots = 1;
  631. }
  632. rspamd_regexp_library_init (cfg);
  633. rspamd_multipattern_library_init (cfg->hs_cache_dir);
  634. #ifdef WITH_HYPERSCAN
  635. if (!cfg->disable_hyperscan) {
  636. if (!(cfg->libs_ctx->crypto_ctx->cpu_config & CPUID_SSSE3)) {
  637. msg_warn_config ("CPU doesn't have SSSE3 instructions set "
  638. "required for hyperscan, disable it");
  639. cfg->disable_hyperscan = TRUE;
  640. }
  641. }
  642. #endif
  643. if (opts & RSPAMD_CONFIG_INIT_URL) {
  644. if (cfg->tld_file == NULL) {
  645. /* Try to guess tld file */
  646. GString *fpath = g_string_new (NULL);
  647. rspamd_printf_gstring (fpath, "%s%c%s", RSPAMD_SHAREDIR,
  648. G_DIR_SEPARATOR, "effective_tld_names.dat");
  649. if (access (fpath->str, R_OK) != -1) {
  650. msg_debug_config ("url_tld option is not specified but %s is available,"
  651. " therefore this file is assumed as TLD file for URL"
  652. " extraction", fpath->str);
  653. cfg->tld_file = rspamd_mempool_strdup (cfg->cfg_pool, fpath->str);
  654. }
  655. else {
  656. if (opts & RSPAMD_CONFIG_INIT_VALIDATE) {
  657. msg_err_config ("no url_tld option has been specified");
  658. ret = FALSE;
  659. }
  660. }
  661. g_string_free (fpath, TRUE);
  662. }
  663. else {
  664. if (access (cfg->tld_file, R_OK) == -1) {
  665. if (opts & RSPAMD_CONFIG_INIT_VALIDATE) {
  666. ret = FALSE;
  667. msg_err_config ("cannot access tld file %s: %s", cfg->tld_file,
  668. strerror (errno));
  669. }
  670. else {
  671. msg_debug_config ("cannot access tld file %s: %s", cfg->tld_file,
  672. strerror (errno));
  673. cfg->tld_file = NULL;
  674. }
  675. }
  676. }
  677. if (opts & RSPAMD_CONFIG_INIT_NO_TLD) {
  678. rspamd_url_init (NULL);
  679. }
  680. else {
  681. rspamd_url_init (cfg->tld_file);
  682. }
  683. rspamd_mempool_add_destructor (cfg->cfg_pool, rspamd_urls_config_dtor,
  684. NULL);
  685. }
  686. init_dynamic_config (cfg);
  687. /* Insert classifiers symbols */
  688. rspamd_config_insert_classify_symbols (cfg);
  689. /* Parse format string that we have */
  690. if (!rspamd_config_parse_log_format (cfg)) {
  691. msg_err_config ("cannot parse log format, task logging will not be available");
  692. }
  693. if (opts & RSPAMD_CONFIG_INIT_SYMCACHE) {
  694. lua_State *L = cfg->lua_state;
  695. int err_idx;
  696. /* Process squeezed Lua rules */
  697. lua_pushcfunction (L, &rspamd_lua_traceback);
  698. err_idx = lua_gettop (L);
  699. if (rspamd_lua_require_function (cfg->lua_state, "lua_squeeze_rules",
  700. "squeeze_init")) {
  701. if (lua_pcall (L, 0, 0, err_idx) != 0) {
  702. GString *tb = lua_touserdata (L, -1);
  703. msg_err_config ("call to squeeze_init script failed: %v", tb);
  704. if (tb) {
  705. g_string_free (tb, TRUE);
  706. }
  707. }
  708. }
  709. lua_settop (L, err_idx - 1);
  710. /* Init config cache */
  711. rspamd_symcache_init (cfg->cache);
  712. /* Init re cache */
  713. rspamd_re_cache_init (cfg->re_cache, cfg);
  714. }
  715. if (opts & RSPAMD_CONFIG_INIT_LIBS) {
  716. /* Config other libraries */
  717. rspamd_config_libs (cfg->libs_ctx, cfg);
  718. }
  719. /* Validate cache */
  720. if (opts & RSPAMD_CONFIG_INIT_VALIDATE) {
  721. /* Check for actions sanity */
  722. int i, prev_act = 0;
  723. gdouble prev_score = NAN;
  724. gboolean seen_controller = FALSE;
  725. GList *cur;
  726. struct rspamd_worker_conf *wcf;
  727. for (i = METRIC_ACTION_REJECT; i < METRIC_ACTION_MAX; i ++) {
  728. if (!isnan (prev_score) && !isnan (cfg->actions[i].score)) {
  729. if (prev_score <= isnan (cfg->actions[i].score)) {
  730. msg_warn_config ("incorrect metrics scores: action %s"
  731. " has lower score: %.2f than action %s: %.2f",
  732. rspamd_action_to_str (prev_act), prev_score,
  733. rspamd_action_to_str (i), cfg->actions[i].score);
  734. ret = FALSE;
  735. }
  736. }
  737. if (!isnan (cfg->actions[i].score)) {
  738. prev_score = cfg->actions[i].score;
  739. prev_act = i;
  740. }
  741. }
  742. cur = cfg->workers;
  743. while (cur) {
  744. wcf = cur->data;
  745. if (wcf->type == g_quark_from_static_string ("controller")) {
  746. seen_controller = TRUE;
  747. break;
  748. }
  749. cur = g_list_next (cur);
  750. }
  751. if (!seen_controller) {
  752. msg_warn_config ("controller worker is unconfigured: learning,"
  753. " periodic scripts, maps watching and many other"
  754. " Rspamd features will be broken");
  755. ret = FALSE;
  756. }
  757. ret = rspamd_symcache_validate (cfg->cache, cfg, FALSE) && ret;
  758. }
  759. if (opts & RSPAMD_CONFIG_INIT_PRELOAD_MAPS) {
  760. rspamd_map_preload (cfg);
  761. }
  762. return ret;
  763. }
  764. #if 0
  765. void
  766. parse_err (const gchar *fmt, ...)
  767. {
  768. va_list aq;
  769. gchar logbuf[BUFSIZ], readbuf[32];
  770. gint r;
  771. va_start (aq, fmt);
  772. rspamd_strlcpy (readbuf, yytext, sizeof (readbuf));
  773. r = snprintf (logbuf,
  774. sizeof (logbuf),
  775. "config file parse error! line: %d, text: %s, reason: ",
  776. yylineno,
  777. readbuf);
  778. r += vsnprintf (logbuf + r, sizeof (logbuf) - r, fmt, aq);
  779. va_end (aq);
  780. g_critical ("%s", logbuf);
  781. }
  782. void
  783. parse_warn (const gchar *fmt, ...)
  784. {
  785. va_list aq;
  786. gchar logbuf[BUFSIZ], readbuf[32];
  787. gint r;
  788. va_start (aq, fmt);
  789. rspamd_strlcpy (readbuf, yytext, sizeof (readbuf));
  790. r = snprintf (logbuf,
  791. sizeof (logbuf),
  792. "config file parse warning! line: %d, text: %s, reason: ",
  793. yylineno,
  794. readbuf);
  795. r += vsnprintf (logbuf + r, sizeof (logbuf) - r, fmt, aq);
  796. va_end (aq);
  797. g_warning ("%s", logbuf);
  798. }
  799. #endif
  800. void
  801. rspamd_config_unescape_quotes (gchar *line)
  802. {
  803. gchar *c = line, *t;
  804. while (*c) {
  805. if (*c == '\\' && *(c + 1) == '"') {
  806. t = c;
  807. while (*t) {
  808. *t = *(t + 1);
  809. t++;
  810. }
  811. }
  812. c++;
  813. }
  814. }
  815. GList *
  816. rspamd_config_parse_comma_list (rspamd_mempool_t * pool, const gchar *line)
  817. {
  818. GList *res = NULL;
  819. const gchar *c, *p;
  820. gchar *str;
  821. c = line;
  822. p = c;
  823. while (*p) {
  824. if (*p == ',' && *c != *p) {
  825. str = rspamd_mempool_alloc (pool, p - c + 1);
  826. rspamd_strlcpy (str, c, p - c + 1);
  827. res = g_list_prepend (res, str);
  828. /* Skip spaces */
  829. while (g_ascii_isspace (*(++p))) ;
  830. c = p;
  831. continue;
  832. }
  833. p++;
  834. }
  835. if (res != NULL) {
  836. rspamd_mempool_add_destructor (pool,
  837. (rspamd_mempool_destruct_t) g_list_free,
  838. res);
  839. }
  840. return res;
  841. }
  842. struct rspamd_classifier_config *
  843. rspamd_config_new_classifier (struct rspamd_config *cfg,
  844. struct rspamd_classifier_config *c)
  845. {
  846. if (c == NULL) {
  847. c =
  848. rspamd_mempool_alloc0 (cfg->cfg_pool,
  849. sizeof (struct rspamd_classifier_config));
  850. c->min_prob_strength = 0.05;
  851. c->min_token_hits = 2;
  852. }
  853. if (c->labels == NULL) {
  854. c->labels = g_hash_table_new_full (rspamd_str_hash,
  855. rspamd_str_equal,
  856. NULL,
  857. (GDestroyNotify)g_list_free);
  858. rspamd_mempool_add_destructor (cfg->cfg_pool,
  859. (rspamd_mempool_destruct_t) g_hash_table_destroy,
  860. c->labels);
  861. }
  862. return c;
  863. }
  864. struct rspamd_statfile_config *
  865. rspamd_config_new_statfile (struct rspamd_config *cfg,
  866. struct rspamd_statfile_config *c)
  867. {
  868. if (c == NULL) {
  869. c =
  870. rspamd_mempool_alloc0 (cfg->cfg_pool,
  871. sizeof (struct rspamd_statfile_config));
  872. }
  873. return c;
  874. }
  875. void
  876. rspamd_config_init_metric (struct rspamd_config *cfg)
  877. {
  878. int i;
  879. cfg->grow_factor = 1.0;
  880. cfg->symbols = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
  881. cfg->groups = g_hash_table_new (rspamd_strcase_hash, rspamd_strcase_equal);
  882. for (i = METRIC_ACTION_REJECT; i < METRIC_ACTION_MAX; i++) {
  883. cfg->actions[i].score = NAN;
  884. cfg->actions[i].action = i;
  885. cfg->actions[i].priority = 0;
  886. }
  887. cfg->subject = SPAM_SUBJECT;
  888. rspamd_mempool_add_destructor (cfg->cfg_pool,
  889. (rspamd_mempool_destruct_t) g_hash_table_unref,
  890. cfg->symbols);
  891. rspamd_mempool_add_destructor (cfg->cfg_pool,
  892. (rspamd_mempool_destruct_t) g_hash_table_unref,
  893. cfg->groups);
  894. }
  895. struct rspamd_symbols_group *
  896. rspamd_config_new_group (struct rspamd_config *cfg, const gchar *name)
  897. {
  898. struct rspamd_symbols_group *gr;
  899. gr = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*gr));
  900. gr->symbols = g_hash_table_new (rspamd_strcase_hash,
  901. rspamd_strcase_equal);
  902. rspamd_mempool_add_destructor (cfg->cfg_pool,
  903. (rspamd_mempool_destruct_t)g_hash_table_unref, gr->symbols);
  904. gr->name = rspamd_mempool_strdup (cfg->cfg_pool, name);
  905. g_hash_table_insert (cfg->groups, gr->name, gr);
  906. return gr;
  907. }
  908. static void
  909. rspamd_worker_conf_dtor (struct rspamd_worker_conf *wcf)
  910. {
  911. if (wcf) {
  912. ucl_object_unref (wcf->options);
  913. g_queue_free (wcf->active_workers);
  914. g_hash_table_unref (wcf->params);
  915. g_free (wcf);
  916. }
  917. }
  918. static void
  919. rspamd_worker_conf_cfg_fin (gpointer d)
  920. {
  921. struct rspamd_worker_conf *wcf = d;
  922. REF_RELEASE (wcf);
  923. }
  924. struct rspamd_worker_conf *
  925. rspamd_config_new_worker (struct rspamd_config *cfg,
  926. struct rspamd_worker_conf *c)
  927. {
  928. if (c == NULL) {
  929. c = g_malloc0 (sizeof (struct rspamd_worker_conf));
  930. c->params = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
  931. c->active_workers = g_queue_new ();
  932. #ifdef HAVE_SC_NPROCESSORS_ONLN
  933. c->count = MIN (DEFAULT_MAX_WORKERS,
  934. MAX (1, sysconf (_SC_NPROCESSORS_ONLN) - 2));
  935. #else
  936. c->count = DEFAULT_MAX_WORKERS;
  937. #endif
  938. c->rlimit_nofile = 0;
  939. c->rlimit_maxcore = 0;
  940. c->enabled = TRUE;
  941. REF_INIT_RETAIN (c, rspamd_worker_conf_dtor);
  942. rspamd_mempool_add_destructor (cfg->cfg_pool,
  943. rspamd_worker_conf_cfg_fin, c);
  944. }
  945. return c;
  946. }
  947. static bool
  948. rspamd_include_map_handler (const guchar *data, gsize len,
  949. const ucl_object_t *args, void * ud)
  950. {
  951. struct rspamd_config *cfg = (struct rspamd_config *)ud;
  952. struct rspamd_ucl_map_cbdata *cbdata, **pcbdata;
  953. gchar *map_line;
  954. map_line = rspamd_mempool_alloc (cfg->cfg_pool, len + 1);
  955. rspamd_strlcpy (map_line, data, len + 1);
  956. cbdata = g_malloc (sizeof (struct rspamd_ucl_map_cbdata));
  957. pcbdata = g_malloc (sizeof (struct rspamd_ucl_map_cbdata *));
  958. cbdata->buf = NULL;
  959. cbdata->cfg = cfg;
  960. *pcbdata = cbdata;
  961. return rspamd_map_add (cfg,
  962. map_line,
  963. "ucl include",
  964. rspamd_ucl_read_cb,
  965. rspamd_ucl_fin_cb,
  966. rspamd_ucl_dtor_cb,
  967. (void **)pcbdata) != NULL;
  968. }
  969. /*
  970. * Variables:
  971. * $CONFDIR - configuration directory
  972. * $LOCAL_CONFDIR - local configuration directory
  973. * $RUNDIR - local states directory
  974. * $DBDIR - databases dir
  975. * $LOGDIR - logs dir
  976. * $PLUGINSDIR - pluggins dir
  977. * $PREFIX - installation prefix
  978. * $VERSION - rspamd version
  979. */
  980. #define RSPAMD_CONFDIR_MACRO "CONFDIR"
  981. #define RSPAMD_LOCAL_CONFDIR_MACRO "LOCAL_CONFDIR"
  982. #define RSPAMD_RUNDIR_MACRO "RUNDIR"
  983. #define RSPAMD_DBDIR_MACRO "DBDIR"
  984. #define RSPAMD_LOGDIR_MACRO "LOGDIR"
  985. #define RSPAMD_PLUGINSDIR_MACRO "PLUGINSDIR"
  986. #define RSPAMD_SHAREDIR_MACRO "SHAREDIR"
  987. #define RSPAMD_RULESDIR_MACRO "RULESDIR"
  988. #define RSPAMD_WWWDIR_MACRO "WWWDIR"
  989. #define RSPAMD_PREFIX_MACRO "PREFIX"
  990. #define RSPAMD_VERSION_MACRO "VERSION"
  991. #define RSPAMD_VERSION_MAJOR_MACRO "VERSION_MAJOR"
  992. #define RSPAMD_VERSION_MINOR_MACRO "VERSION_MINOR"
  993. #define RSPAMD_VERSION_PATCH_MACRO "VERSION_PATCH"
  994. #define RSPAMD_BRANCH_VERSION_MACRO "BRANCH_VERSION"
  995. #define RSPAMD_HOSTNAME_MACRO "HOSTNAME"
  996. void
  997. rspamd_ucl_add_conf_variables (struct ucl_parser *parser, GHashTable *vars)
  998. {
  999. GHashTableIter it;
  1000. gpointer k, v;
  1001. gchar *hostbuf;
  1002. gsize hostlen;
  1003. ucl_parser_register_variable (parser,
  1004. RSPAMD_CONFDIR_MACRO,
  1005. RSPAMD_CONFDIR);
  1006. ucl_parser_register_variable (parser,
  1007. RSPAMD_LOCAL_CONFDIR_MACRO,
  1008. RSPAMD_LOCAL_CONFDIR);
  1009. ucl_parser_register_variable (parser, RSPAMD_RUNDIR_MACRO,
  1010. RSPAMD_RUNDIR);
  1011. ucl_parser_register_variable (parser, RSPAMD_DBDIR_MACRO,
  1012. RSPAMD_DBDIR);
  1013. ucl_parser_register_variable (parser, RSPAMD_LOGDIR_MACRO,
  1014. RSPAMD_LOGDIR);
  1015. ucl_parser_register_variable (parser,
  1016. RSPAMD_PLUGINSDIR_MACRO,
  1017. RSPAMD_PLUGINSDIR);
  1018. ucl_parser_register_variable (parser,
  1019. RSPAMD_SHAREDIR_MACRO,
  1020. RSPAMD_SHAREDIR);
  1021. ucl_parser_register_variable (parser,
  1022. RSPAMD_RULESDIR_MACRO,
  1023. RSPAMD_RULESDIR);
  1024. ucl_parser_register_variable (parser, RSPAMD_WWWDIR_MACRO,
  1025. RSPAMD_WWWDIR);
  1026. ucl_parser_register_variable (parser, RSPAMD_PREFIX_MACRO,
  1027. RSPAMD_PREFIX);
  1028. ucl_parser_register_variable (parser, RSPAMD_VERSION_MACRO, RVERSION);
  1029. ucl_parser_register_variable (parser, RSPAMD_VERSION_MAJOR_MACRO,
  1030. RSPAMD_VERSION_MAJOR);
  1031. ucl_parser_register_variable (parser, RSPAMD_VERSION_MINOR_MACRO,
  1032. RSPAMD_VERSION_MINOR);
  1033. ucl_parser_register_variable (parser, RSPAMD_VERSION_PATCH_MACRO,
  1034. RSPAMD_VERSION_PATCH);
  1035. ucl_parser_register_variable (parser, RSPAMD_BRANCH_VERSION_MACRO,
  1036. RSPAMD_VERSION_BRANCH);
  1037. #if defined(WITH_TORCH) && defined(WITH_LUAJIT) && defined(__x86_64__)
  1038. ucl_parser_register_variable (parser, "HAS_TORCH",
  1039. "yes");
  1040. #else
  1041. ucl_parser_register_variable (parser, "HAS_TORCH",
  1042. "no");
  1043. #endif
  1044. hostlen = sysconf (_SC_HOST_NAME_MAX);
  1045. if (hostlen <= 0) {
  1046. hostlen = 256;
  1047. }
  1048. else {
  1049. hostlen ++;
  1050. }
  1051. hostbuf = g_alloca (hostlen);
  1052. memset (hostbuf, 0, hostlen);
  1053. gethostname (hostbuf, hostlen - 1);
  1054. /* UCL copies variables, so it is safe to pass an ephemeral buffer here */
  1055. ucl_parser_register_variable (parser, RSPAMD_HOSTNAME_MACRO,
  1056. hostbuf);
  1057. if (vars != NULL) {
  1058. g_hash_table_iter_init (&it, vars);
  1059. while (g_hash_table_iter_next (&it, &k, &v)) {
  1060. ucl_parser_register_variable (parser, k, v);
  1061. }
  1062. }
  1063. }
  1064. void
  1065. rspamd_ucl_add_conf_macros (struct ucl_parser *parser,
  1066. struct rspamd_config *cfg)
  1067. {
  1068. ucl_parser_register_macro (parser,
  1069. "include_map",
  1070. rspamd_include_map_handler,
  1071. cfg);
  1072. }
  1073. static void
  1074. symbols_classifiers_callback (gpointer key, gpointer value, gpointer ud)
  1075. {
  1076. struct rspamd_config *cfg = ud;
  1077. /* Actually, statistics should act like any ordinary symbol */
  1078. rspamd_symcache_add_symbol (cfg->cache, key, 0, NULL, NULL,
  1079. SYMBOL_TYPE_CLASSIFIER | SYMBOL_TYPE_NOSTAT, -1);
  1080. }
  1081. void
  1082. rspamd_config_insert_classify_symbols (struct rspamd_config *cfg)
  1083. {
  1084. g_hash_table_foreach (cfg->classifiers_symbols,
  1085. symbols_classifiers_callback,
  1086. cfg);
  1087. }
  1088. struct rspamd_classifier_config *
  1089. rspamd_config_find_classifier (struct rspamd_config *cfg, const gchar *name)
  1090. {
  1091. GList *cur;
  1092. struct rspamd_classifier_config *cf;
  1093. if (name == NULL) {
  1094. return NULL;
  1095. }
  1096. cur = cfg->classifiers;
  1097. while (cur) {
  1098. cf = cur->data;
  1099. if (g_ascii_strcasecmp (cf->name, name) == 0) {
  1100. return cf;
  1101. }
  1102. cur = g_list_next (cur);
  1103. }
  1104. return NULL;
  1105. }
  1106. gboolean
  1107. rspamd_config_check_statfiles (struct rspamd_classifier_config *cf)
  1108. {
  1109. struct rspamd_statfile_config *st;
  1110. gboolean has_other = FALSE, res = FALSE, cur_class;
  1111. GList *cur;
  1112. /* First check classes directly */
  1113. cur = cf->statfiles;
  1114. while (cur) {
  1115. st = cur->data;
  1116. if (!has_other) {
  1117. cur_class = st->is_spam;
  1118. has_other = TRUE;
  1119. }
  1120. else {
  1121. if (cur_class != st->is_spam) {
  1122. return TRUE;
  1123. }
  1124. }
  1125. cur = g_list_next (cur);
  1126. }
  1127. if (!has_other) {
  1128. /* We have only one statfile */
  1129. return FALSE;
  1130. }
  1131. /* We have not detected any statfile that has different class, so turn on euristic based on symbol's name */
  1132. has_other = FALSE;
  1133. cur = cf->statfiles;
  1134. while (cur) {
  1135. st = cur->data;
  1136. if (rspamd_substring_search_caseless (st->symbol,
  1137. strlen (st->symbol),"spam", 4) != -1) {
  1138. st->is_spam = TRUE;
  1139. }
  1140. else if (rspamd_substring_search_caseless (st->symbol,
  1141. strlen (st->symbol),"ham", 3) != -1) {
  1142. st->is_spam = FALSE;
  1143. }
  1144. if (!has_other) {
  1145. cur_class = st->is_spam;
  1146. has_other = TRUE;
  1147. }
  1148. else {
  1149. if (cur_class != st->is_spam) {
  1150. res = TRUE;
  1151. }
  1152. }
  1153. cur = g_list_next (cur);
  1154. }
  1155. return res;
  1156. }
  1157. static gchar *
  1158. rspamd_ucl_read_cb (gchar * chunk,
  1159. gint len,
  1160. struct map_cb_data *data,
  1161. gboolean final)
  1162. {
  1163. struct rspamd_ucl_map_cbdata *cbdata = data->cur_data, *prev;
  1164. if (cbdata == NULL) {
  1165. cbdata = g_malloc (sizeof (struct rspamd_ucl_map_cbdata));
  1166. prev = data->prev_data;
  1167. cbdata->buf = g_string_sized_new (BUFSIZ);
  1168. cbdata->cfg = prev->cfg;
  1169. data->cur_data = cbdata;
  1170. }
  1171. g_string_append_len (cbdata->buf, chunk, len);
  1172. /* Say not to copy any part of this buffer */
  1173. return NULL;
  1174. }
  1175. static void
  1176. rspamd_ucl_fin_cb (struct map_cb_data *data)
  1177. {
  1178. struct rspamd_ucl_map_cbdata *cbdata = data->cur_data, *prev =
  1179. data->prev_data;
  1180. ucl_object_t *obj;
  1181. struct ucl_parser *parser;
  1182. ucl_object_iter_t it = NULL;
  1183. const ucl_object_t *cur;
  1184. struct rspamd_config *cfg = data->map->cfg;
  1185. if (prev != NULL) {
  1186. if (prev->buf != NULL) {
  1187. g_string_free (prev->buf, TRUE);
  1188. }
  1189. g_free (prev);
  1190. }
  1191. if (cbdata == NULL) {
  1192. msg_err_config ("map fin error: new data is NULL");
  1193. return;
  1194. }
  1195. /* New data available */
  1196. parser = ucl_parser_new (0);
  1197. if (!ucl_parser_add_chunk (parser, cbdata->buf->str,
  1198. cbdata->buf->len)) {
  1199. msg_err_config ("cannot parse map %s: %s",
  1200. data->map->name,
  1201. ucl_parser_get_error (parser));
  1202. ucl_parser_free (parser);
  1203. }
  1204. else {
  1205. obj = ucl_parser_get_object (parser);
  1206. ucl_parser_free (parser);
  1207. it = NULL;
  1208. while ((cur = ucl_object_iterate (obj, &it, true))) {
  1209. ucl_object_replace_key (cbdata->cfg->rcl_obj, (ucl_object_t *)cur,
  1210. cur->key, cur->keylen, false);
  1211. }
  1212. ucl_object_unref (obj);
  1213. }
  1214. }
  1215. static void
  1216. rspamd_ucl_dtor_cb (struct map_cb_data *data)
  1217. {
  1218. struct rspamd_ucl_map_cbdata *cbdata = data->cur_data;
  1219. if (cbdata != NULL) {
  1220. if (cbdata->buf != NULL) {
  1221. g_string_free (cbdata->buf, TRUE);
  1222. }
  1223. g_free (cbdata);
  1224. }
  1225. }
  1226. gboolean
  1227. rspamd_check_module (struct rspamd_config *cfg, module_t *mod)
  1228. {
  1229. gboolean ret = TRUE;
  1230. if (mod != NULL) {
  1231. if (mod->module_version != RSPAMD_CUR_MODULE_VERSION) {
  1232. msg_err_config ("module %s has incorrect version %xd (%xd expected)",
  1233. mod->name, (gint)mod->module_version, RSPAMD_CUR_MODULE_VERSION);
  1234. ret = FALSE;
  1235. }
  1236. if (ret && mod->rspamd_version != RSPAMD_VERSION_NUM) {
  1237. msg_err_config ("module %s has incorrect rspamd version %xL (%xL expected)",
  1238. mod->name, mod->rspamd_version, RSPAMD_VERSION_NUM);
  1239. ret = FALSE;
  1240. }
  1241. if (ret && strcmp (mod->rspamd_features, RSPAMD_FEATURES) != 0) {
  1242. msg_err_config ("module %s has incorrect rspamd features '%s' ('%s' expected)",
  1243. mod->name, mod->rspamd_features, RSPAMD_FEATURES);
  1244. ret = FALSE;
  1245. }
  1246. }
  1247. else {
  1248. ret = FALSE;
  1249. }
  1250. return ret;
  1251. }
  1252. gboolean
  1253. rspamd_check_worker (struct rspamd_config *cfg, worker_t *wrk)
  1254. {
  1255. gboolean ret = TRUE;
  1256. if (wrk != NULL) {
  1257. if (wrk->worker_version != RSPAMD_CUR_WORKER_VERSION) {
  1258. msg_err_config ("worker %s has incorrect version %xd (%xd expected)",
  1259. wrk->name, wrk->worker_version, RSPAMD_CUR_WORKER_VERSION);
  1260. ret = FALSE;
  1261. }
  1262. if (ret && wrk->rspamd_version != RSPAMD_VERSION_NUM) {
  1263. msg_err_config ("worker %s has incorrect rspamd version %xL (%xL expected)",
  1264. wrk->name, wrk->rspamd_version, RSPAMD_VERSION_NUM);
  1265. ret = FALSE;
  1266. }
  1267. if (ret && strcmp (wrk->rspamd_features, RSPAMD_FEATURES) != 0) {
  1268. msg_err_config ("worker %s has incorrect rspamd features '%s' ('%s' expected)",
  1269. wrk->name, wrk->rspamd_features, RSPAMD_FEATURES);
  1270. ret = FALSE;
  1271. }
  1272. }
  1273. else {
  1274. ret = FALSE;
  1275. }
  1276. return ret;
  1277. }
  1278. gboolean
  1279. rspamd_init_filters (struct rspamd_config *cfg, bool reconfig)
  1280. {
  1281. GList *cur;
  1282. module_t *mod, **pmod;
  1283. guint i = 0;
  1284. struct module_ctx *mod_ctx, *cur_ctx;
  1285. gboolean ret = TRUE;
  1286. /* Init all compiled modules */
  1287. for (pmod = cfg->compiled_modules; pmod != NULL && *pmod != NULL; pmod ++) {
  1288. mod = *pmod;
  1289. if (rspamd_check_module (cfg, mod)) {
  1290. if (mod->module_init_func (cfg, &mod_ctx) == 0) {
  1291. g_assert (mod_ctx != NULL);
  1292. g_ptr_array_add (cfg->c_modules, mod_ctx);
  1293. mod_ctx->mod = mod;
  1294. mod->ctx_offset = i ++;
  1295. }
  1296. }
  1297. }
  1298. /* Now check what's enabled */
  1299. cur = g_list_first (cfg->filters);
  1300. while (cur) {
  1301. /* Perform modules configuring */
  1302. mod_ctx = NULL;
  1303. PTR_ARRAY_FOREACH (cfg->c_modules, i, cur_ctx) {
  1304. if (g_ascii_strcasecmp (cur_ctx->mod->name,
  1305. (const gchar *)cur->data) == 0) {
  1306. mod_ctx = cur_ctx;
  1307. break;
  1308. }
  1309. }
  1310. if (mod_ctx) {
  1311. mod = mod_ctx->mod;
  1312. mod_ctx->enabled = rspamd_config_is_module_enabled (cfg, mod->name);
  1313. if (reconfig) {
  1314. if (!mod->module_reconfig_func (cfg)) {
  1315. msg_err_config ("reconfig of %s failed!", mod->name);
  1316. }
  1317. else {
  1318. msg_info_config ("reconfig of %s", mod->name);
  1319. }
  1320. }
  1321. else {
  1322. if (!mod->module_config_func (cfg)) {
  1323. msg_info_config ("config of %s failed!", mod->name);
  1324. ret = FALSE;
  1325. }
  1326. }
  1327. }
  1328. if (mod_ctx == NULL) {
  1329. msg_warn_config ("requested unknown module %s", cur->data);
  1330. }
  1331. cur = g_list_next (cur);
  1332. }
  1333. ret = rspamd_init_lua_filters (cfg, 0) && ret;
  1334. return ret;
  1335. }
  1336. static void
  1337. rspamd_config_new_symbol (struct rspamd_config *cfg, const gchar *symbol,
  1338. gdouble score, const gchar *description, const gchar *group,
  1339. guint flags, guint priority, gint nshots)
  1340. {
  1341. struct rspamd_symbols_group *sym_group;
  1342. struct rspamd_symbol *sym_def;
  1343. gdouble *score_ptr;
  1344. sym_def =
  1345. rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (struct rspamd_symbol));
  1346. score_ptr = rspamd_mempool_alloc (cfg->cfg_pool, sizeof (gdouble));
  1347. *score_ptr = score;
  1348. sym_def->score = score;
  1349. sym_def->weight_ptr = score_ptr;
  1350. sym_def->name = rspamd_mempool_strdup (cfg->cfg_pool, symbol);
  1351. sym_def->priority = priority;
  1352. sym_def->flags = flags;
  1353. sym_def->nshots = nshots;
  1354. sym_def->groups = g_ptr_array_sized_new (1);
  1355. rspamd_mempool_add_destructor (cfg->cfg_pool, rspamd_ptr_array_free_hard,
  1356. sym_def->groups);
  1357. if (description) {
  1358. sym_def->description = rspamd_mempool_strdup (cfg->cfg_pool, description);
  1359. }
  1360. msg_debug_config ("registered symbol %s with weight %.2f in and group %s",
  1361. sym_def->name, score, group);
  1362. g_hash_table_insert (cfg->symbols, sym_def->name, sym_def);
  1363. /* Search for symbol group */
  1364. if (group == NULL) {
  1365. group = "ungrouped";
  1366. sym_def->flags |= RSPAMD_SYMBOL_FLAG_UNGROUPPED;
  1367. }
  1368. else {
  1369. if (strcmp (group, "ungrouped") == 0) {
  1370. sym_def->flags |= RSPAMD_SYMBOL_FLAG_UNGROUPPED;
  1371. }
  1372. }
  1373. sym_group = g_hash_table_lookup (cfg->groups, group);
  1374. if (sym_group == NULL) {
  1375. /* Create new group */
  1376. sym_group = rspamd_config_new_group (cfg, group);
  1377. }
  1378. sym_def->gr = sym_group;
  1379. g_hash_table_insert (sym_group->symbols, sym_def->name, sym_def);
  1380. if (!(sym_def->flags & RSPAMD_SYMBOL_FLAG_UNGROUPPED)) {
  1381. g_ptr_array_add (sym_def->groups, sym_group);
  1382. }
  1383. }
  1384. gboolean
  1385. rspamd_config_add_symbol (struct rspamd_config *cfg,
  1386. const gchar *symbol,
  1387. gdouble score, const gchar *description,
  1388. const gchar *group,
  1389. guint flags, guint priority, gint nshots)
  1390. {
  1391. struct rspamd_symbol *sym_def;
  1392. struct rspamd_symbols_group *sym_group;
  1393. guint i;
  1394. g_assert (cfg != NULL);
  1395. g_assert (symbol != NULL);
  1396. sym_def = g_hash_table_lookup (cfg->symbols, symbol);
  1397. if (sym_def != NULL) {
  1398. if (group != NULL) {
  1399. gboolean has_group = FALSE;
  1400. PTR_ARRAY_FOREACH (sym_def->groups, i, sym_group) {
  1401. if (g_ascii_strcasecmp (sym_group->name, group) == 0) {
  1402. /* Group is already here */
  1403. has_group = TRUE;
  1404. break;
  1405. }
  1406. }
  1407. if (!has_group) {
  1408. /* Non-empty group has a priority over non-groupped one */
  1409. sym_group = g_hash_table_lookup (cfg->groups, group);
  1410. if (sym_group == NULL) {
  1411. /* Create new group */
  1412. sym_group = rspamd_config_new_group (cfg, group);
  1413. }
  1414. if (!sym_def->gr) {
  1415. sym_def->gr = sym_group;
  1416. }
  1417. g_hash_table_insert (sym_group->symbols, sym_def->name, sym_def);
  1418. sym_def->flags &= ~(RSPAMD_SYMBOL_FLAG_UNGROUPPED);
  1419. g_ptr_array_add (sym_def->groups, sym_group);
  1420. }
  1421. }
  1422. if (sym_def->priority > priority) {
  1423. msg_debug_config ("symbol %s has been already registered with "
  1424. "priority %ud, do not override (new priority: %ud)",
  1425. symbol,
  1426. sym_def->priority,
  1427. priority);
  1428. /* But we can still add description */
  1429. if (!sym_def->description && description) {
  1430. sym_def->description = rspamd_mempool_strdup (cfg->cfg_pool,
  1431. description);
  1432. }
  1433. return FALSE;
  1434. }
  1435. else {
  1436. msg_debug_config ("symbol %s has been already registered with "
  1437. "priority %ud, override it with new priority: %ud, "
  1438. "old score: %.2f, new score: %.2f",
  1439. symbol,
  1440. sym_def->priority,
  1441. priority,
  1442. sym_def->score,
  1443. score);
  1444. *sym_def->weight_ptr = score;
  1445. sym_def->score = score;
  1446. sym_def->flags = flags;
  1447. sym_def->nshots = nshots;
  1448. if (description) {
  1449. sym_def->description = rspamd_mempool_strdup (cfg->cfg_pool,
  1450. description);
  1451. }
  1452. sym_def->priority = priority;
  1453. /* We also check group information in this case */
  1454. if (group != NULL && sym_def->gr != NULL &&
  1455. strcmp (group, sym_def->gr->name) != 0) {
  1456. msg_debug_config ("move symbol %s from group %s to %s",
  1457. sym_def->gr->name, group);
  1458. g_hash_table_remove (sym_def->gr->symbols, sym_def->name);
  1459. sym_group = g_hash_table_lookup (cfg->groups, group);
  1460. if (sym_group == NULL) {
  1461. /* Create new group */
  1462. sym_group = rspamd_config_new_group (cfg, group);
  1463. }
  1464. sym_def->gr = sym_group;
  1465. g_hash_table_insert (sym_group->symbols, sym_def->name, sym_def);
  1466. }
  1467. return TRUE;
  1468. }
  1469. }
  1470. rspamd_config_new_symbol (cfg, symbol, score, description,
  1471. group, flags, priority, nshots);
  1472. return TRUE;
  1473. }
  1474. gboolean
  1475. rspamd_config_add_symbol_group (struct rspamd_config *cfg,
  1476. const gchar *symbol,
  1477. const gchar *group)
  1478. {
  1479. struct rspamd_symbol *sym_def;
  1480. struct rspamd_symbols_group *sym_group;
  1481. guint i;
  1482. g_assert (cfg != NULL);
  1483. g_assert (symbol != NULL);
  1484. g_assert (group != NULL);
  1485. sym_def = g_hash_table_lookup (cfg->symbols, symbol);
  1486. if (sym_def != NULL) {
  1487. gboolean has_group = FALSE;
  1488. PTR_ARRAY_FOREACH (sym_def->groups, i, sym_group) {
  1489. if (g_ascii_strcasecmp (sym_group->name, group) == 0) {
  1490. /* Group is already here */
  1491. has_group = TRUE;
  1492. break;
  1493. }
  1494. }
  1495. if (!has_group) {
  1496. /* Non-empty group has a priority over non-groupped one */
  1497. sym_group = g_hash_table_lookup (cfg->groups, group);
  1498. if (sym_group == NULL) {
  1499. /* Create new group */
  1500. sym_group = rspamd_config_new_group (cfg, group);
  1501. }
  1502. if (!sym_def->gr) {
  1503. sym_def->gr = sym_group;
  1504. }
  1505. g_hash_table_insert (sym_group->symbols, sym_def->name, sym_def);
  1506. sym_def->flags &= ~(RSPAMD_SYMBOL_FLAG_UNGROUPPED);
  1507. g_ptr_array_add (sym_def->groups, sym_group);
  1508. return TRUE;
  1509. }
  1510. }
  1511. return FALSE;
  1512. }
  1513. gboolean
  1514. rspamd_config_is_module_enabled (struct rspamd_config *cfg,
  1515. const gchar *module_name)
  1516. {
  1517. gboolean is_c = FALSE;
  1518. const ucl_object_t *conf, *enabled;
  1519. GList *cur;
  1520. struct rspamd_symbols_group *gr;
  1521. lua_State *L = cfg->lua_state;
  1522. struct module_ctx *cur_ctx;
  1523. guint i;
  1524. PTR_ARRAY_FOREACH (cfg->c_modules, i, cur_ctx) {
  1525. if (g_ascii_strcasecmp (cur_ctx->mod->name, module_name) == 0) {
  1526. is_c = TRUE;
  1527. break;
  1528. }
  1529. }
  1530. if (g_hash_table_lookup (cfg->explicit_modules, module_name) != NULL) {
  1531. /* Always load module */
  1532. rspamd_plugins_table_push_elt (L, "enabled", module_name);
  1533. return TRUE;
  1534. }
  1535. if (is_c) {
  1536. gboolean found = FALSE;
  1537. cur = g_list_first (cfg->filters);
  1538. while (cur) {
  1539. if (strcmp (cur->data, module_name) == 0) {
  1540. found = TRUE;
  1541. break;
  1542. }
  1543. cur = g_list_next (cur);
  1544. }
  1545. if (!found) {
  1546. msg_info_config ("internal module %s is disable in `filters` line",
  1547. module_name);
  1548. rspamd_plugins_table_push_elt (L,
  1549. "disabled_explicitly", module_name);
  1550. return FALSE;
  1551. }
  1552. }
  1553. conf = ucl_object_lookup (cfg->rcl_obj, module_name);
  1554. if (conf == NULL) {
  1555. rspamd_plugins_table_push_elt (L, "disabled_unconfigured", module_name);
  1556. msg_info_config ("%s module %s is enabled but has not been configured",
  1557. is_c ? "internal" : "lua", module_name);
  1558. if (!is_c) {
  1559. msg_info_config ("%s disabling unconfigured lua module", module_name);
  1560. return FALSE;
  1561. }
  1562. }
  1563. else {
  1564. enabled = ucl_object_lookup (conf, "enabled");
  1565. if (enabled) {
  1566. if (ucl_object_type (enabled) == UCL_BOOLEAN) {
  1567. if (!ucl_object_toboolean (enabled)) {
  1568. rspamd_plugins_table_push_elt (L,
  1569. "disabled_explicitly", module_name);
  1570. msg_info_config (
  1571. "%s module %s is disabled in the configuration",
  1572. is_c ? "internal" : "lua", module_name);
  1573. return FALSE;
  1574. }
  1575. }
  1576. else if (ucl_object_type (enabled) == UCL_STRING) {
  1577. gint ret;
  1578. ret = rspamd_config_parse_flag (ucl_object_tostring (enabled), 0);
  1579. if (ret == 0) {
  1580. rspamd_plugins_table_push_elt (L,
  1581. "disabled_explicitly", module_name);
  1582. msg_info_config (
  1583. "%s module %s is disabled in the configuration",
  1584. is_c ? "internal" : "lua", module_name);
  1585. return FALSE;
  1586. }
  1587. else if (ret == -1) {
  1588. rspamd_plugins_table_push_elt (L,
  1589. "disabled_failed", module_name);
  1590. msg_info_config (
  1591. "%s module %s has wrong enabled flag (%s) in the configuration",
  1592. is_c ? "internal" : "lua", module_name,
  1593. ucl_object_tostring (enabled));
  1594. return FALSE;
  1595. }
  1596. }
  1597. }
  1598. }
  1599. /* Now we check symbols group */
  1600. gr = g_hash_table_lookup (cfg->groups, module_name);
  1601. if (gr) {
  1602. if (gr->disabled) {
  1603. rspamd_plugins_table_push_elt (L,
  1604. "disabled_explicitly", module_name);
  1605. msg_info_config ("%s module %s is disabled in the configuration as "
  1606. "its group has been disabled",
  1607. is_c ? "internal" : "lua", module_name);
  1608. return FALSE;
  1609. }
  1610. }
  1611. rspamd_plugins_table_push_elt (L, "enabled", module_name);
  1612. return TRUE;
  1613. }
  1614. gboolean
  1615. rspamd_config_set_action_score (struct rspamd_config *cfg,
  1616. const gchar *action_name,
  1617. gdouble score,
  1618. guint priority)
  1619. {
  1620. struct rspamd_action *act;
  1621. gint act_num;
  1622. g_assert (cfg != NULL);
  1623. g_assert (action_name != NULL);
  1624. if (!rspamd_action_from_str (action_name, &act_num)) {
  1625. msg_err_config ("invalid action name: %s", action_name);
  1626. return FALSE;
  1627. }
  1628. g_assert (act_num >= METRIC_ACTION_REJECT && act_num < METRIC_ACTION_MAX);
  1629. act = &cfg->actions[act_num];
  1630. if (isnan (act->score)) {
  1631. act->score = score;
  1632. act->priority = priority;
  1633. }
  1634. else {
  1635. if (act->priority > priority) {
  1636. msg_info_config ("action %s has been already registered with "
  1637. "priority %ud, do not override (new priority: %ud)",
  1638. action_name,
  1639. act->priority,
  1640. priority);
  1641. return FALSE;
  1642. }
  1643. else {
  1644. msg_info_config ("action %s has been already registered with "
  1645. "priority %ud, override it with new priority: %ud, "
  1646. "old score: %.2f, new score: %.2f",
  1647. action_name,
  1648. act->priority,
  1649. priority,
  1650. act->score,
  1651. score);
  1652. act->score = score;
  1653. act->priority = priority;
  1654. }
  1655. }
  1656. return TRUE;
  1657. }
  1658. gboolean
  1659. rspamd_config_radix_from_ucl (struct rspamd_config *cfg,
  1660. const ucl_object_t *obj,
  1661. const gchar *description,
  1662. struct rspamd_radix_map_helper **target,
  1663. GError **err)
  1664. {
  1665. ucl_type_t type;
  1666. ucl_object_iter_t it = NULL;
  1667. const ucl_object_t *cur, *cur_elt;
  1668. const gchar *str;
  1669. /* Cleanup */
  1670. *target = NULL;
  1671. LL_FOREACH (obj, cur_elt) {
  1672. type = ucl_object_type (cur_elt);
  1673. switch (type) {
  1674. case UCL_STRING:
  1675. /* Either map or a list of IPs */
  1676. str = ucl_object_tostring (cur_elt);
  1677. if (rspamd_map_is_map (str)) {
  1678. if (rspamd_map_add_from_ucl (cfg, cur_elt,
  1679. description,
  1680. rspamd_radix_read,
  1681. rspamd_radix_fin,
  1682. rspamd_radix_dtor,
  1683. (void **)target) == NULL) {
  1684. g_set_error (err, g_quark_from_static_string ("rspamd-config"),
  1685. EINVAL, "bad map definition %s for %s", str,
  1686. ucl_object_key (obj));
  1687. return FALSE;
  1688. }
  1689. return TRUE;
  1690. }
  1691. else {
  1692. /* Just a list */
  1693. if (!*target) {
  1694. *target = rspamd_map_helper_new_radix (NULL);
  1695. }
  1696. rspamd_map_helper_insert_radix_resolve (*target, str, "");
  1697. }
  1698. break;
  1699. case UCL_OBJECT:
  1700. /* Should be a map description */
  1701. if (rspamd_map_add_from_ucl (cfg, cur_elt,
  1702. description,
  1703. rspamd_radix_read,
  1704. rspamd_radix_fin,
  1705. rspamd_radix_dtor,
  1706. (void **)target) == NULL) {
  1707. g_set_error (err, g_quark_from_static_string ("rspamd-config"),
  1708. EINVAL, "bad map object for %s", ucl_object_key (obj));
  1709. return FALSE;
  1710. }
  1711. return TRUE;
  1712. break;
  1713. case UCL_ARRAY:
  1714. /* List of IP addresses */
  1715. it = ucl_object_iterate_new (cur_elt);
  1716. while ((cur = ucl_object_iterate_safe (it, true)) != NULL) {
  1717. str = ucl_object_tostring (cur);
  1718. if (!*target) {
  1719. *target = rspamd_map_helper_new_radix (NULL);
  1720. }
  1721. rspamd_map_helper_insert_radix_resolve (*target, str, "");
  1722. }
  1723. ucl_object_iterate_free (it);
  1724. break;
  1725. default:
  1726. g_set_error (err, g_quark_from_static_string ("rspamd-config"),
  1727. EINVAL, "bad map type %s for %s",
  1728. ucl_object_type_to_string (type),
  1729. ucl_object_key (obj));
  1730. return FALSE;
  1731. }
  1732. }
  1733. /* Destroy on cfg cleanup */
  1734. rspamd_mempool_add_destructor (cfg->cfg_pool,
  1735. (rspamd_mempool_destruct_t)rspamd_map_helper_destroy_radix,
  1736. *target);
  1737. return TRUE;
  1738. }
  1739. gboolean
  1740. rspamd_action_from_str (const gchar *data, gint *result)
  1741. {
  1742. guint64 h;
  1743. h = rspamd_cryptobox_fast_hash_specific (RSPAMD_CRYPTOBOX_XXHASH64,
  1744. data, strlen (data), 0xdeadbabe);
  1745. switch (h) {
  1746. case 0x9917BFDB46332B8CULL: /* reject */
  1747. *result = METRIC_ACTION_REJECT;
  1748. break;
  1749. case 0x7130EE37D07B3715ULL: /* greylist */
  1750. *result = METRIC_ACTION_GREYLIST;
  1751. break;
  1752. case 0xCA6087E05480C60CULL: /* add_header */
  1753. case 0x87A3D27783B16241ULL: /* add header */
  1754. *result = METRIC_ACTION_ADD_HEADER;
  1755. break;
  1756. case 0x4963374ED8B90449ULL: /* rewrite_subject */
  1757. case 0x5C9FC4679C025948ULL: /* rewrite subject */
  1758. *result = METRIC_ACTION_REWRITE_SUBJECT;
  1759. break;
  1760. case 0xFC7D6502EE71FDD9ULL: /* soft reject */
  1761. case 0x73576567C262A82DULL: /* soft_reject */
  1762. *result = METRIC_ACTION_SOFT_REJECT;
  1763. break;
  1764. case 0x207091B927D1EC0DULL: /* no action */
  1765. case 0xB7D92D002CD46325ULL: /* no_action */
  1766. case 0x167C0DF4BAA9BCECULL: /* accept */
  1767. *result = METRIC_ACTION_NOACTION;
  1768. break;
  1769. default:
  1770. return FALSE;
  1771. }
  1772. return TRUE;
  1773. }
  1774. const gchar *
  1775. rspamd_action_to_str (enum rspamd_action_type action)
  1776. {
  1777. switch (action) {
  1778. case METRIC_ACTION_REJECT:
  1779. return "reject";
  1780. case METRIC_ACTION_SOFT_REJECT:
  1781. return "soft reject";
  1782. case METRIC_ACTION_REWRITE_SUBJECT:
  1783. return "rewrite subject";
  1784. case METRIC_ACTION_ADD_HEADER:
  1785. return "add header";
  1786. case METRIC_ACTION_GREYLIST:
  1787. return "greylist";
  1788. case METRIC_ACTION_NOACTION:
  1789. return "no action";
  1790. case METRIC_ACTION_MAX:
  1791. return "invalid max action";
  1792. }
  1793. return "unknown action";
  1794. }
  1795. const gchar *
  1796. rspamd_action_to_str_alt (enum rspamd_action_type action)
  1797. {
  1798. switch (action) {
  1799. case METRIC_ACTION_REJECT:
  1800. return "reject";
  1801. case METRIC_ACTION_SOFT_REJECT:
  1802. return "soft_reject";
  1803. case METRIC_ACTION_REWRITE_SUBJECT:
  1804. return "rewrite_subject";
  1805. case METRIC_ACTION_ADD_HEADER:
  1806. return "add_header";
  1807. case METRIC_ACTION_GREYLIST:
  1808. return "greylist";
  1809. case METRIC_ACTION_NOACTION:
  1810. return "no action";
  1811. case METRIC_ACTION_MAX:
  1812. return "invalid max action";
  1813. }
  1814. return "unknown action";
  1815. }