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 56KB

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