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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  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 "map.h"
  23. #include "dynamic_cfg.h"
  24. #include "utlist.h"
  25. #include "stat_api.h"
  26. #include "unix-std.h"
  27. #include <math.h>
  28. #define DEFAULT_SCORE 10.0
  29. #define DEFAULT_RLIMIT_NOFILE 2048
  30. #define DEFAULT_RLIMIT_MAXCORE 0
  31. #define DEFAULT_MAP_TIMEOUT 10
  32. #define DEFAULT_MIN_WORD 4
  33. #define DEFAULT_MAX_WORD 40
  34. #define DEFAULT_WORDS_DECAY 200
  35. struct rspamd_ucl_map_cbdata {
  36. struct rspamd_config *cfg;
  37. GString *buf;
  38. };
  39. static gchar * rspamd_ucl_read_cb (rspamd_mempool_t * pool,
  40. gchar * chunk,
  41. gint len,
  42. struct map_cb_data *data);
  43. static void rspamd_ucl_fin_cb (rspamd_mempool_t * pool,
  44. struct map_cb_data *data);
  45. gboolean
  46. rspamd_parse_bind_line (struct rspamd_config *cfg,
  47. struct rspamd_worker_conf *cf,
  48. const gchar *str)
  49. {
  50. struct rspamd_worker_bind_conf *cnf;
  51. gchar *err;
  52. gboolean ret = TRUE;
  53. if (str == NULL) {
  54. return FALSE;
  55. }
  56. cnf =
  57. rspamd_mempool_alloc0 (cfg->cfg_pool,
  58. sizeof (struct rspamd_worker_bind_conf));
  59. cnf->cnt = 1024;
  60. if (g_ascii_strncasecmp (str, "systemd:", sizeof ("systemd:") - 1) == 0) {
  61. /* The actual socket will be passed by systemd environment */
  62. cnf->is_systemd = TRUE;
  63. cnf->cnt = strtoul (str + sizeof ("systemd:") - 1, &err, 10);
  64. cnf->addrs = NULL;
  65. if (err == NULL || *err == '\0') {
  66. cnf->name = rspamd_mempool_strdup (cfg->cfg_pool, str);
  67. LL_PREPEND (cf->bind_conf, cnf);
  68. }
  69. else {
  70. msg_err_config ("cannot parse bind line: %s", str);
  71. ret = FALSE;
  72. }
  73. }
  74. else {
  75. if (!rspamd_parse_host_port_priority (str, &cnf->addrs,
  76. NULL, &cnf->name, DEFAULT_BIND_PORT, cfg->cfg_pool)) {
  77. msg_err_config ("cannot parse bind line: %s", str);
  78. ret = FALSE;
  79. }
  80. else {
  81. cnf->cnt = cnf->addrs->len;
  82. LL_PREPEND (cf->bind_conf, cnf);
  83. }
  84. }
  85. return ret;
  86. }
  87. struct rspamd_config *
  88. rspamd_config_new (void)
  89. {
  90. struct rspamd_config *cfg;
  91. cfg = g_slice_alloc0 (sizeof (*cfg));
  92. cfg->cfg_pool = rspamd_mempool_new (rspamd_mempool_suggest_size (), "cfg");
  93. cfg->dns_timeout = 1000;
  94. cfg->dns_retransmits = 5;
  95. /* After 20 errors do throttling for 10 seconds */
  96. cfg->dns_throttling_errors = 20;
  97. cfg->dns_throttling_time = 10000;
  98. /* 16 sockets per DNS server */
  99. cfg->dns_io_per_server = 16;
  100. /* 20 Kb */
  101. cfg->max_diff = 20480;
  102. cfg->metrics = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
  103. if (cfg->c_modules == NULL) {
  104. cfg->c_modules = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
  105. }
  106. cfg->composite_symbols =
  107. g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
  108. cfg->classifiers_symbols = g_hash_table_new (rspamd_str_hash,
  109. rspamd_str_equal);
  110. cfg->cfg_params = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
  111. cfg->metrics_symbols = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
  112. cfg->debug_modules = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
  113. cfg->explicit_modules = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
  114. cfg->wrk_parsers = g_hash_table_new (g_int_hash, g_int_equal);
  115. cfg->map_timeout = DEFAULT_MAP_TIMEOUT;
  116. cfg->log_level = G_LOG_LEVEL_WARNING;
  117. cfg->log_extended = TRUE;
  118. cfg->dns_max_requests = 64;
  119. cfg->history_rows = 200;
  120. /* Default log line */
  121. cfg->log_format_str = "id: <$mid>,$if_qid{ qid: <$>,}$if_ip{ ip: $,}"
  122. "$if_user{ user: $,}$if_smtp_from{ from: <$>,} (default: $is_spam "
  123. "($action): [$scores] [$symbols]), len: $len, time: $time_real real,"
  124. " $time_virtual virtual, dns req: $dns_req";
  125. /* Allow non-mime input by default */
  126. cfg->allow_raw_input = TRUE;
  127. /* Default maximum words processed */
  128. cfg->words_decay = DEFAULT_WORDS_DECAY;
  129. cfg->min_word_len = DEFAULT_MIN_WORD;
  130. cfg->max_word_len = DEFAULT_MAX_WORD;
  131. cfg->lua_state = rspamd_lua_init ();
  132. cfg->cache = rspamd_symbols_cache_new (cfg);
  133. cfg->ups_ctx = rspamd_upstreams_library_init ();
  134. cfg->re_cache = rspamd_re_cache_new ();
  135. cfg->doc_strings = ucl_object_typed_new (UCL_OBJECT);
  136. REF_INIT_RETAIN (cfg, rspamd_config_free);
  137. return cfg;
  138. }
  139. void
  140. rspamd_config_free (struct rspamd_config *cfg)
  141. {
  142. rspamd_map_remove_all (cfg);
  143. ucl_object_unref (cfg->rcl_obj);
  144. ucl_object_unref (cfg->doc_strings);
  145. g_hash_table_remove_all (cfg->metrics);
  146. g_hash_table_unref (cfg->metrics);
  147. g_hash_table_unref (cfg->c_modules);
  148. g_hash_table_remove_all (cfg->composite_symbols);
  149. g_hash_table_unref (cfg->composite_symbols);
  150. g_hash_table_remove_all (cfg->cfg_params);
  151. g_hash_table_unref (cfg->cfg_params);
  152. g_hash_table_destroy (cfg->metrics_symbols);
  153. g_hash_table_unref (cfg->classifiers_symbols);
  154. g_hash_table_unref (cfg->debug_modules);
  155. g_hash_table_unref (cfg->explicit_modules);
  156. g_hash_table_unref (cfg->wrk_parsers);
  157. if (cfg->checksum) {
  158. g_free (cfg->checksum);
  159. }
  160. g_list_free (cfg->classifiers);
  161. g_list_free (cfg->metrics_list);
  162. rspamd_symbols_cache_destroy (cfg->cache);
  163. REF_RELEASE (cfg->libs_ctx);
  164. rspamd_re_cache_unref (cfg->re_cache);
  165. rspamd_upstreams_library_unref (cfg->ups_ctx);
  166. rspamd_mempool_delete (cfg->cfg_pool);
  167. lua_close (cfg->lua_state);
  168. g_slice_free1 (sizeof (*cfg), cfg);
  169. }
  170. const ucl_object_t *
  171. rspamd_config_get_module_opt (struct rspamd_config *cfg,
  172. const gchar *module_name,
  173. const gchar *opt_name)
  174. {
  175. const ucl_object_t *res = NULL, *sec;
  176. sec = ucl_obj_get_key (cfg->rcl_obj, module_name);
  177. if (sec != NULL) {
  178. res = ucl_obj_get_key (sec, opt_name);
  179. }
  180. return res;
  181. }
  182. gchar
  183. rspamd_config_parse_flag (const gchar *str, guint len)
  184. {
  185. gchar c;
  186. if (!str || !*str) {
  187. return -1;
  188. }
  189. if (len == 0) {
  190. len = strlen (str);
  191. }
  192. switch (len) {
  193. case 1:
  194. c = g_ascii_tolower (*str);
  195. if (c == 'y' || c == '1') {
  196. return 1;
  197. }
  198. else if (c == 'n' || c == '0') {
  199. return 0;
  200. }
  201. break;
  202. case 2:
  203. if (g_ascii_strncasecmp (str, "no", len) == 0) {
  204. return 0;
  205. }
  206. else if (g_ascii_strncasecmp (str, "on", len) == 0) {
  207. return 1;
  208. }
  209. break;
  210. case 3:
  211. if (g_ascii_strncasecmp (str, "yes", len) == 0) {
  212. return 1;
  213. }
  214. else if (g_ascii_strncasecmp (str, "off", len) == 0) {
  215. return 0;
  216. }
  217. break;
  218. case 4:
  219. if (g_ascii_strncasecmp (str, "true", len) == 0) {
  220. return 1;
  221. }
  222. break;
  223. case 5:
  224. if (g_ascii_strncasecmp (str, "false", len) == 0) {
  225. return 0;
  226. }
  227. break;
  228. }
  229. return -1;
  230. }
  231. static gboolean
  232. rspamd_config_process_var (struct rspamd_config *cfg, const rspamd_ftok_t *var,
  233. const rspamd_ftok_t *content)
  234. {
  235. guint flags = RSPAMD_LOG_FLAG_DEFAULT;
  236. struct rspamd_log_format *lf;
  237. enum rspamd_log_format_type type;
  238. rspamd_ftok_t tok;
  239. gint id;
  240. g_assert (var != NULL);
  241. if (var->len > 3 && rspamd_lc_cmp (var->begin, "if_", 3) == 0) {
  242. flags |= RSPAMD_LOG_FLAG_CONDITION;
  243. tok.begin = var->begin + 3;
  244. tok.len = var->len - 3;
  245. }
  246. else {
  247. tok.begin = var->begin;
  248. tok.len = var->len;
  249. }
  250. /* Now compare variable and check what we have */
  251. if (rspamd_ftok_cstr_equal (&tok, "mid", TRUE)) {
  252. type = RSPAMD_LOG_MID;
  253. }
  254. else if (rspamd_ftok_cstr_equal (&tok, "qid", TRUE)) {
  255. type = RSPAMD_LOG_QID;
  256. }
  257. else if (rspamd_ftok_cstr_equal (&tok, "user", TRUE)) {
  258. type = RSPAMD_LOG_USER;
  259. }
  260. else if (rspamd_ftok_cstr_equal (&tok, "is_spam", TRUE)) {
  261. type = RSPAMD_LOG_ISSPAM;
  262. }
  263. else if (rspamd_ftok_cstr_equal (&tok, "action", TRUE)) {
  264. type = RSPAMD_LOG_ACTION;
  265. }
  266. else if (rspamd_ftok_cstr_equal (&tok, "scores", TRUE)) {
  267. type = RSPAMD_LOG_SCORES;
  268. }
  269. else if (rspamd_ftok_cstr_equal (&tok, "symbols", TRUE)) {
  270. type = RSPAMD_LOG_SYMBOLS;
  271. }
  272. else if (rspamd_ftok_cstr_equal (&tok, "symbols_scores", TRUE)) {
  273. type = RSPAMD_LOG_SYMBOLS;
  274. flags |= RSPAMD_LOG_FLAG_SYMBOLS_SCORES;
  275. }
  276. else if (rspamd_ftok_cstr_equal (&tok, "ip", TRUE)) {
  277. type = RSPAMD_LOG_IP;
  278. }
  279. else if (rspamd_ftok_cstr_equal (&tok, "len", TRUE)) {
  280. type = RSPAMD_LOG_LEN;
  281. }
  282. else if (rspamd_ftok_cstr_equal (&tok, "dns_req", TRUE)) {
  283. type = RSPAMD_LOG_DNS_REQ;
  284. }
  285. else if (rspamd_ftok_cstr_equal (&tok, "smtp_from", TRUE)) {
  286. type = RSPAMD_LOG_SMTP_FROM;
  287. }
  288. else if (rspamd_ftok_cstr_equal (&tok, "mime_from", TRUE)) {
  289. type = RSPAMD_LOG_MIME_FROM;
  290. }
  291. else if (rspamd_ftok_cstr_equal (&tok, "smtp_rcpt", TRUE)) {
  292. type = RSPAMD_LOG_SMTP_RCPT;
  293. }
  294. else if (rspamd_ftok_cstr_equal (&tok, "mime_rcpt", TRUE)) {
  295. type = RSPAMD_LOG_MIME_RCPT;
  296. }
  297. else if (rspamd_ftok_cstr_equal (&tok, "smtp_rcpts", TRUE)) {
  298. type = RSPAMD_LOG_SMTP_RCPTS;
  299. }
  300. else if (rspamd_ftok_cstr_equal (&tok, "mime_rcpts", TRUE)) {
  301. type = RSPAMD_LOG_MIME_RCPTS;
  302. }
  303. else if (rspamd_ftok_cstr_equal (&tok, "time_real", TRUE)) {
  304. type = RSPAMD_LOG_TIME_REAL;
  305. }
  306. else if (rspamd_ftok_cstr_equal (&tok, "time_virtual", TRUE)) {
  307. type = RSPAMD_LOG_TIME_VIRTUAL;
  308. }
  309. else if (rspamd_ftok_cstr_equal (&tok, "lua", TRUE)) {
  310. type = RSPAMD_LOG_LUA;
  311. }
  312. else {
  313. msg_err_config ("unknown log variable: %T", &tok);
  314. return FALSE;
  315. }
  316. lf = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*lf));
  317. lf->type = type;
  318. lf->flags = flags;
  319. if (type != RSPAMD_LOG_LUA) {
  320. if (content && content->len > 0) {
  321. lf->data = rspamd_mempool_alloc0 (cfg->cfg_pool,
  322. sizeof (rspamd_ftok_t));
  323. memcpy (lf->data, content, sizeof (*content));
  324. lf->len = sizeof (*content);
  325. }
  326. }
  327. else {
  328. /* Load lua code and ensure that we have function ref returned */
  329. if (!content || content->len == 0) {
  330. msg_err_config ("lua variable needs content: %T", &tok);
  331. return FALSE;
  332. }
  333. if (luaL_loadbuffer (cfg->lua_state, content->begin, content->len,
  334. "lua log variable") != 0) {
  335. msg_err_config ("error loading lua code: '%T': %s", content,
  336. lua_tostring (cfg->lua_state, -1));
  337. return FALSE;
  338. }
  339. if (lua_pcall (cfg->lua_state, 0, 1, 0) != 0) {
  340. msg_err_config ("error executing lua code: '%T': %s", content,
  341. lua_tostring (cfg->lua_state, -1));
  342. return FALSE;
  343. }
  344. if (lua_type (cfg->lua_state, -1) != LUA_TFUNCTION) {
  345. msg_err_config ("lua variable should return function: %T", content);
  346. lua_pop (cfg->lua_state, 1);
  347. return FALSE;
  348. }
  349. id = luaL_ref (cfg->lua_state, LUA_REGISTRYINDEX);
  350. lf->data = GINT_TO_POINTER (id);
  351. lf->len = 0;
  352. }
  353. DL_APPEND (cfg->log_format, lf);
  354. return TRUE;
  355. }
  356. static gboolean
  357. rspamd_config_parse_log_format (struct rspamd_config *cfg)
  358. {
  359. const gchar *p, *c, *end, *s;
  360. gchar *d;
  361. struct rspamd_log_format *lf = NULL;
  362. rspamd_ftok_t var, var_content;
  363. enum {
  364. parse_str,
  365. parse_dollar,
  366. parse_var_name,
  367. parse_var_content,
  368. } state = parse_str;
  369. gint braces = 0;
  370. g_assert (cfg != NULL);
  371. c = cfg->log_format_str;
  372. if (c == NULL) {
  373. return FALSE;
  374. }
  375. p = c;
  376. end = p + strlen (p);
  377. while (p < end) {
  378. switch (state) {
  379. case parse_str:
  380. if (*p == '$') {
  381. state = parse_dollar;
  382. }
  383. else {
  384. p ++;
  385. }
  386. break;
  387. case parse_dollar:
  388. if (p > c) {
  389. /* We have string element that we need to store */
  390. lf = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*lf));
  391. lf->type = RSPAMD_LOG_STRING;
  392. lf->data = rspamd_mempool_alloc (cfg->cfg_pool, p - c + 1);
  393. /* Filter \r\n from the destination */
  394. s = c;
  395. d = lf->data;
  396. while (s < p) {
  397. if (*s != '\r' && *s != '\n') {
  398. *d++ = *s++;
  399. }
  400. else {
  401. *d ++ = ' ';
  402. s++;
  403. }
  404. }
  405. *d = '\0';
  406. lf->len = d - (char *) lf->data;
  407. DL_APPEND (cfg->log_format, lf);
  408. lf = NULL;
  409. }
  410. p++;
  411. c = p;
  412. state = parse_var_name;
  413. break;
  414. case parse_var_name:
  415. if (*p == '{') {
  416. var.begin = c;
  417. var.len = p - c;
  418. p ++;
  419. c = p;
  420. state = parse_var_content;
  421. braces = 1;
  422. }
  423. else if (*p != '_' && *p != '-' && !g_ascii_isalnum (*p)) {
  424. /* Variable with no content */
  425. var.begin = c;
  426. var.len = p - c;
  427. c = p;
  428. if (!rspamd_config_process_var (cfg, &var, NULL)) {
  429. return FALSE;
  430. }
  431. state = parse_str;
  432. }
  433. else {
  434. p++;
  435. }
  436. break;
  437. case parse_var_content:
  438. if (*p == '}' && --braces == 0) {
  439. var_content.begin = c;
  440. var_content.len = p - c;
  441. p ++;
  442. c = p;
  443. if (!rspamd_config_process_var (cfg, &var, &var_content)) {
  444. return FALSE;
  445. }
  446. state = parse_str;
  447. }
  448. else if (*p == '{') {
  449. braces ++;
  450. p ++;
  451. }
  452. else {
  453. p++;
  454. }
  455. break;
  456. }
  457. }
  458. /* Last state */
  459. switch (state) {
  460. case parse_str:
  461. if (p > c) {
  462. /* We have string element that we need to store */
  463. lf = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*lf));
  464. lf->type = RSPAMD_LOG_STRING;
  465. lf->data = rspamd_mempool_alloc (cfg->cfg_pool, p - c + 1);
  466. /* Filter \r\n from the destination */
  467. s = c;
  468. d = lf->data;
  469. while (s < p) {
  470. if (*s != '\r' && *s != '\n') {
  471. *d++ = *s++;
  472. }
  473. else {
  474. *d++ = ' ';
  475. s++;
  476. }
  477. }
  478. *d = '\0';
  479. lf->len = d - (char *)lf->data;
  480. DL_APPEND (cfg->log_format, lf);
  481. lf = NULL;
  482. }
  483. break;
  484. case parse_var_name:
  485. var.begin = c;
  486. var.len = p - c;
  487. if (!rspamd_config_process_var (cfg, &var, NULL)) {
  488. return FALSE;
  489. }
  490. break;
  491. case parse_dollar:
  492. case parse_var_content:
  493. msg_err_config ("cannot parse log format %s: incomplete string",
  494. cfg->log_format_str);
  495. return FALSE;
  496. break;
  497. }
  498. return TRUE;
  499. }
  500. /*
  501. * Perform post load actions
  502. */
  503. gboolean
  504. rspamd_config_post_load (struct rspamd_config *cfg, gboolean validate_cache)
  505. {
  506. #ifdef HAVE_CLOCK_GETTIME
  507. struct timespec ts;
  508. #endif
  509. struct metric *def_metric;
  510. #ifdef HAVE_CLOCK_GETTIME
  511. #ifdef HAVE_CLOCK_PROCESS_CPUTIME_ID
  512. clock_getres (CLOCK_PROCESS_CPUTIME_ID, &ts);
  513. # elif defined(HAVE_CLOCK_VIRTUAL)
  514. clock_getres (CLOCK_VIRTUAL, &ts);
  515. # else
  516. clock_getres (CLOCK_REALTIME, &ts);
  517. # endif
  518. cfg->clock_res = log10 (1000000. / ts.tv_nsec);
  519. if (cfg->clock_res < 0) {
  520. cfg->clock_res = 0;
  521. }
  522. if (cfg->clock_res > 3) {
  523. cfg->clock_res = 3;
  524. }
  525. #else
  526. /* For gettimeofday */
  527. cfg->clock_res = 1;
  528. #endif
  529. rspamd_regexp_library_init ();
  530. if ((def_metric =
  531. g_hash_table_lookup (cfg->metrics, DEFAULT_METRIC)) == NULL) {
  532. def_metric = rspamd_config_new_metric (cfg, NULL, DEFAULT_METRIC);
  533. def_metric->actions[METRIC_ACTION_REJECT].score = DEFAULT_SCORE;
  534. }
  535. if (cfg->tld_file == NULL) {
  536. /* Try to guess tld file */
  537. GString *fpath = g_string_new (NULL);
  538. rspamd_printf_gstring (fpath, "%s%c%s", RSPAMD_PLUGINSDIR,
  539. G_DIR_SEPARATOR, "effective_tld_names.dat");
  540. if (access (fpath->str, R_OK)) {
  541. msg_warn_config ("url_tld option is not specified but %s is available,"
  542. " therefore this file is assumed as TLD file for URL"
  543. " extraction", fpath->str);
  544. cfg->tld_file = rspamd_mempool_strdup (cfg->cfg_pool, fpath->str);
  545. }
  546. else {
  547. msg_err_config ("no url_tld option has been specified, URL's detection "
  548. "will be awfully broken");
  549. }
  550. g_string_free (fpath, TRUE);
  551. }
  552. init_dynamic_config (cfg);
  553. rspamd_url_init (cfg->tld_file);
  554. /* Insert classifiers symbols */
  555. rspamd_config_insert_classify_symbols (cfg);
  556. /* Parse format string that we have */
  557. if (!rspamd_config_parse_log_format (cfg)) {
  558. msg_err_config ("cannot parse log format, task logging will not be available");
  559. }
  560. /* Init config cache */
  561. rspamd_symbols_cache_init (cfg->cache);
  562. /* Init re cache */
  563. rspamd_re_cache_init (cfg->re_cache, cfg);
  564. /* Config other libraries */
  565. rspamd_config_libs (cfg->libs_ctx, cfg);
  566. /* Validate cache */
  567. if (validate_cache) {
  568. return rspamd_symbols_cache_validate (cfg->cache, cfg, FALSE);
  569. }
  570. return TRUE;
  571. }
  572. #if 0
  573. void
  574. parse_err (const gchar *fmt, ...)
  575. {
  576. va_list aq;
  577. gchar logbuf[BUFSIZ], readbuf[32];
  578. gint r;
  579. va_start (aq, fmt);
  580. rspamd_strlcpy (readbuf, yytext, sizeof (readbuf));
  581. r = snprintf (logbuf,
  582. sizeof (logbuf),
  583. "config file parse error! line: %d, text: %s, reason: ",
  584. yylineno,
  585. readbuf);
  586. r += vsnprintf (logbuf + r, sizeof (logbuf) - r, fmt, aq);
  587. va_end (aq);
  588. g_critical ("%s", logbuf);
  589. }
  590. void
  591. parse_warn (const gchar *fmt, ...)
  592. {
  593. va_list aq;
  594. gchar logbuf[BUFSIZ], readbuf[32];
  595. gint r;
  596. va_start (aq, fmt);
  597. rspamd_strlcpy (readbuf, yytext, sizeof (readbuf));
  598. r = snprintf (logbuf,
  599. sizeof (logbuf),
  600. "config file parse warning! line: %d, text: %s, reason: ",
  601. yylineno,
  602. readbuf);
  603. r += vsnprintf (logbuf + r, sizeof (logbuf) - r, fmt, aq);
  604. va_end (aq);
  605. g_warning ("%s", logbuf);
  606. }
  607. #endif
  608. void
  609. rspamd_config_unescape_quotes (gchar *line)
  610. {
  611. gchar *c = line, *t;
  612. while (*c) {
  613. if (*c == '\\' && *(c + 1) == '"') {
  614. t = c;
  615. while (*t) {
  616. *t = *(t + 1);
  617. t++;
  618. }
  619. }
  620. c++;
  621. }
  622. }
  623. GList *
  624. rspamd_config_parse_comma_list (rspamd_mempool_t * pool, const gchar *line)
  625. {
  626. GList *res = NULL;
  627. const gchar *c, *p;
  628. gchar *str;
  629. c = line;
  630. p = c;
  631. while (*p) {
  632. if (*p == ',' && *c != *p) {
  633. str = rspamd_mempool_alloc (pool, p - c + 1);
  634. rspamd_strlcpy (str, c, p - c + 1);
  635. res = g_list_prepend (res, str);
  636. /* Skip spaces */
  637. while (g_ascii_isspace (*(++p))) ;
  638. c = p;
  639. continue;
  640. }
  641. p++;
  642. }
  643. if (res != NULL) {
  644. rspamd_mempool_add_destructor (pool,
  645. (rspamd_mempool_destruct_t) g_list_free,
  646. res);
  647. }
  648. return res;
  649. }
  650. struct rspamd_classifier_config *
  651. rspamd_config_new_classifier (struct rspamd_config *cfg,
  652. struct rspamd_classifier_config *c)
  653. {
  654. if (c == NULL) {
  655. c =
  656. rspamd_mempool_alloc0 (cfg->cfg_pool,
  657. sizeof (struct rspamd_classifier_config));
  658. }
  659. if (c->labels == NULL) {
  660. c->labels = g_hash_table_new_full (rspamd_str_hash,
  661. rspamd_str_equal,
  662. NULL,
  663. (GDestroyNotify)g_list_free);
  664. rspamd_mempool_add_destructor (cfg->cfg_pool,
  665. (rspamd_mempool_destruct_t) g_hash_table_destroy,
  666. c->labels);
  667. }
  668. return c;
  669. }
  670. struct rspamd_statfile_config *
  671. rspamd_config_new_statfile (struct rspamd_config *cfg,
  672. struct rspamd_statfile_config *c)
  673. {
  674. if (c == NULL) {
  675. c =
  676. rspamd_mempool_alloc0 (cfg->cfg_pool,
  677. sizeof (struct rspamd_statfile_config));
  678. }
  679. return c;
  680. }
  681. struct metric *
  682. rspamd_config_new_metric (struct rspamd_config *cfg, struct metric *c,
  683. const gchar *name)
  684. {
  685. int i;
  686. if (c == NULL) {
  687. c = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (struct metric));
  688. c->grow_factor = 1.0;
  689. c->symbols = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
  690. c->groups = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
  691. for (i = METRIC_ACTION_REJECT; i < METRIC_ACTION_MAX; i++) {
  692. c->actions[i].score = -1.0;
  693. c->actions[i].action = i;
  694. }
  695. c->subject = SPAM_SUBJECT;
  696. c->name = rspamd_mempool_strdup (cfg->cfg_pool, name);
  697. rspamd_mempool_add_destructor (cfg->cfg_pool,
  698. (rspamd_mempool_destruct_t) g_hash_table_unref,
  699. c->symbols);
  700. rspamd_mempool_add_destructor (cfg->cfg_pool,
  701. (rspamd_mempool_destruct_t) g_hash_table_unref,
  702. c->groups);
  703. g_hash_table_insert (cfg->metrics, (void *)c->name, c);
  704. cfg->metrics_list = g_list_prepend (cfg->metrics_list, c);
  705. if (strcmp (c->name, DEFAULT_METRIC) == 0) {
  706. cfg->default_metric = c;
  707. }
  708. }
  709. return c;
  710. }
  711. struct rspamd_symbols_group *
  712. rspamd_config_new_group (struct rspamd_config *cfg, struct metric *metric,
  713. const gchar *name)
  714. {
  715. struct rspamd_symbols_group *gr;
  716. gr = rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (*gr));
  717. gr->symbols = g_hash_table_new (rspamd_strcase_hash,
  718. rspamd_strcase_equal);
  719. rspamd_mempool_add_destructor (cfg->cfg_pool,
  720. (rspamd_mempool_destruct_t)g_hash_table_unref, gr->symbols);
  721. gr->name = rspamd_mempool_strdup (cfg->cfg_pool, name);
  722. g_hash_table_insert (metric->groups, gr->name, gr);
  723. return gr;
  724. }
  725. struct rspamd_worker_conf *
  726. rspamd_config_new_worker (struct rspamd_config *cfg,
  727. struct rspamd_worker_conf *c)
  728. {
  729. if (c == NULL) {
  730. c =
  731. rspamd_mempool_alloc0 (cfg->cfg_pool,
  732. sizeof (struct rspamd_worker_conf));
  733. c->params = g_hash_table_new (rspamd_str_hash, rspamd_str_equal);
  734. c->active_workers = g_queue_new ();
  735. rspamd_mempool_add_destructor (cfg->cfg_pool,
  736. (rspamd_mempool_destruct_t)g_hash_table_destroy,
  737. c->params);
  738. rspamd_mempool_add_destructor (cfg->cfg_pool,
  739. (rspamd_mempool_destruct_t)g_queue_free,
  740. c->active_workers);
  741. #ifdef HAVE_SC_NPROCESSORS_ONLN
  742. c->count = sysconf (_SC_NPROCESSORS_ONLN);
  743. #else
  744. c->count = DEFAULT_WORKERS_NUM;
  745. #endif
  746. c->rlimit_nofile = 0;
  747. c->rlimit_maxcore = 0;
  748. }
  749. return c;
  750. }
  751. static bool
  752. rspamd_include_map_handler (const guchar *data, gsize len,
  753. const ucl_object_t *args, void * ud)
  754. {
  755. struct rspamd_config *cfg = (struct rspamd_config *)ud;
  756. struct rspamd_ucl_map_cbdata *cbdata, **pcbdata;
  757. gchar *map_line;
  758. map_line = rspamd_mempool_alloc (cfg->cfg_pool, len + 1);
  759. rspamd_strlcpy (map_line, data, len + 1);
  760. cbdata = g_malloc (sizeof (struct rspamd_ucl_map_cbdata));
  761. pcbdata = g_malloc (sizeof (struct rspamd_ucl_map_cbdata *));
  762. cbdata->buf = NULL;
  763. cbdata->cfg = cfg;
  764. *pcbdata = cbdata;
  765. return rspamd_map_add (cfg,
  766. map_line,
  767. "ucl include",
  768. rspamd_ucl_read_cb,
  769. rspamd_ucl_fin_cb,
  770. (void **)pcbdata);
  771. }
  772. /*
  773. * Variables:
  774. * $CONFDIR - configuration directory
  775. * $RUNDIR - local states directory
  776. * $DBDIR - databases dir
  777. * $LOGDIR - logs dir
  778. * $PLUGINSDIR - pluggins dir
  779. * $PREFIX - installation prefix
  780. * $VERSION - rspamd version
  781. */
  782. #define RSPAMD_CONFDIR_MACRO "CONFDIR"
  783. #define RSPAMD_RUNDIR_MACRO "RUNDIR"
  784. #define RSPAMD_DBDIR_MACRO "DBDIR"
  785. #define RSPAMD_LOGDIR_MACRO "LOGDIR"
  786. #define RSPAMD_PLUGINSDIR_MACRO "PLUGINSDIR"
  787. #define RSPAMD_RULESDIR_MACRO "RULESDIR"
  788. #define RSPAMD_WWWDIR_MACRO "WWWDIR"
  789. #define RSPAMD_PREFIX_MACRO "PREFIX"
  790. #define RSPAMD_VERSION_MACRO "VERSION"
  791. void
  792. rspamd_ucl_add_conf_variables (struct ucl_parser *parser, GHashTable *vars)
  793. {
  794. GHashTableIter it;
  795. gpointer k, v;
  796. ucl_parser_register_variable (parser,
  797. RSPAMD_CONFDIR_MACRO,
  798. RSPAMD_CONFDIR);
  799. ucl_parser_register_variable (parser, RSPAMD_RUNDIR_MACRO,
  800. RSPAMD_RUNDIR);
  801. ucl_parser_register_variable (parser, RSPAMD_DBDIR_MACRO,
  802. RSPAMD_DBDIR);
  803. ucl_parser_register_variable (parser, RSPAMD_LOGDIR_MACRO,
  804. RSPAMD_LOGDIR);
  805. ucl_parser_register_variable (parser,
  806. RSPAMD_PLUGINSDIR_MACRO,
  807. RSPAMD_PLUGINSDIR);
  808. ucl_parser_register_variable (parser,
  809. RSPAMD_RULESDIR_MACRO,
  810. RSPAMD_RULESDIR);
  811. ucl_parser_register_variable (parser, RSPAMD_WWWDIR_MACRO,
  812. RSPAMD_WWWDIR);
  813. ucl_parser_register_variable (parser, RSPAMD_PREFIX_MACRO,
  814. RSPAMD_PREFIX);
  815. ucl_parser_register_variable (parser, RSPAMD_VERSION_MACRO, RVERSION);
  816. if (vars != NULL) {
  817. g_hash_table_iter_init (&it, vars);
  818. while (g_hash_table_iter_next (&it, &k, &v)) {
  819. ucl_parser_register_variable (parser, k, v);
  820. }
  821. }
  822. }
  823. void
  824. rspamd_ucl_add_conf_macros (struct ucl_parser *parser,
  825. struct rspamd_config *cfg)
  826. {
  827. ucl_parser_register_macro (parser,
  828. "include_map",
  829. rspamd_include_map_handler,
  830. cfg);
  831. }
  832. static void
  833. symbols_classifiers_callback (gpointer key, gpointer value, gpointer ud)
  834. {
  835. struct rspamd_config *cfg = ud;
  836. /* Actually, statistics should act like any ordinary symbol */
  837. rspamd_symbols_cache_add_symbol (cfg->cache, key, 0, NULL, NULL,
  838. SYMBOL_TYPE_CLASSIFIER, -1);
  839. }
  840. void
  841. rspamd_config_insert_classify_symbols (struct rspamd_config *cfg)
  842. {
  843. g_hash_table_foreach (cfg->classifiers_symbols,
  844. symbols_classifiers_callback,
  845. cfg);
  846. }
  847. struct rspamd_classifier_config *
  848. rspamd_config_find_classifier (struct rspamd_config *cfg, const gchar *name)
  849. {
  850. GList *cur;
  851. struct rspamd_classifier_config *cf;
  852. if (name == NULL) {
  853. return NULL;
  854. }
  855. cur = cfg->classifiers;
  856. while (cur) {
  857. cf = cur->data;
  858. if (g_ascii_strcasecmp (cf->name, name) == 0) {
  859. return cf;
  860. }
  861. cur = g_list_next (cur);
  862. }
  863. return NULL;
  864. }
  865. gboolean
  866. rspamd_config_check_statfiles (struct rspamd_classifier_config *cf)
  867. {
  868. struct rspamd_statfile_config *st;
  869. gboolean has_other = FALSE, res = FALSE, cur_class;
  870. GList *cur;
  871. /* First check classes directly */
  872. cur = cf->statfiles;
  873. while (cur) {
  874. st = cur->data;
  875. if (!has_other) {
  876. cur_class = st->is_spam;
  877. has_other = TRUE;
  878. }
  879. else {
  880. if (cur_class != st->is_spam) {
  881. return TRUE;
  882. }
  883. }
  884. cur = g_list_next (cur);
  885. }
  886. if (!has_other) {
  887. /* We have only one statfile */
  888. return FALSE;
  889. }
  890. /* We have not detected any statfile that has different class, so turn on euristic based on symbol's name */
  891. has_other = FALSE;
  892. cur = cf->statfiles;
  893. while (cur) {
  894. st = cur->data;
  895. if (rspamd_strncasestr (st->symbol, "spam", -1) != NULL) {
  896. st->is_spam = TRUE;
  897. }
  898. else if (rspamd_strncasestr (st->symbol, "ham", -1) != NULL) {
  899. st->is_spam = FALSE;
  900. }
  901. if (!has_other) {
  902. cur_class = st->is_spam;
  903. has_other = TRUE;
  904. }
  905. else {
  906. if (cur_class != st->is_spam) {
  907. res = TRUE;
  908. }
  909. }
  910. cur = g_list_next (cur);
  911. }
  912. return res;
  913. }
  914. static gchar *
  915. rspamd_ucl_read_cb (rspamd_mempool_t * pool,
  916. gchar * chunk,
  917. gint len,
  918. struct map_cb_data *data)
  919. {
  920. struct rspamd_ucl_map_cbdata *cbdata = data->cur_data, *prev;
  921. if (cbdata == NULL) {
  922. cbdata = g_malloc (sizeof (struct rspamd_ucl_map_cbdata));
  923. prev = data->prev_data;
  924. cbdata->buf = g_string_sized_new (BUFSIZ);
  925. cbdata->cfg = prev->cfg;
  926. data->cur_data = cbdata;
  927. }
  928. g_string_append_len (cbdata->buf, chunk, len);
  929. /* Say not to copy any part of this buffer */
  930. return NULL;
  931. }
  932. static void
  933. rspamd_ucl_fin_cb (rspamd_mempool_t * pool, struct map_cb_data *data)
  934. {
  935. struct rspamd_ucl_map_cbdata *cbdata = data->cur_data, *prev =
  936. data->prev_data;
  937. ucl_object_t *obj;
  938. struct ucl_parser *parser;
  939. guint32 checksum;
  940. ucl_object_iter_t it = NULL;
  941. const ucl_object_t *cur;
  942. struct rspamd_config *cfg = data->map->cfg;
  943. if (prev != NULL) {
  944. if (prev->buf != NULL) {
  945. g_string_free (prev->buf, TRUE);
  946. }
  947. g_free (prev);
  948. }
  949. if (cbdata == NULL) {
  950. msg_err_config ("map fin error: new data is NULL");
  951. return;
  952. }
  953. checksum = XXH64 (cbdata->buf->str, cbdata->buf->len, 0);
  954. if (data->map->checksum != checksum) {
  955. /* New data available */
  956. parser = ucl_parser_new (0);
  957. if (!ucl_parser_add_chunk (parser, cbdata->buf->str,
  958. cbdata->buf->len)) {
  959. msg_err_config ("cannot parse map %s: %s",
  960. data->map->uri,
  961. ucl_parser_get_error (parser));
  962. ucl_parser_free (parser);
  963. }
  964. else {
  965. obj = ucl_parser_get_object (parser);
  966. ucl_parser_free (parser);
  967. it = NULL;
  968. while ((cur = ucl_iterate_object (obj, &it, true))) {
  969. ucl_object_replace_key (cbdata->cfg->rcl_obj, (ucl_object_t *)cur,
  970. cur->key, cur->keylen, false);
  971. }
  972. ucl_object_unref (obj);
  973. data->map->checksum = checksum;
  974. }
  975. }
  976. else {
  977. msg_info_config ("do not reload map %s, checksum is the same: %d",
  978. data->map->uri,
  979. checksum);
  980. }
  981. }
  982. gboolean
  983. rspamd_init_filters (struct rspamd_config *cfg, bool reconfig)
  984. {
  985. GList *cur;
  986. module_t *mod, **pmod;
  987. struct module_ctx *mod_ctx;
  988. /* Init all compiled modules */
  989. if (!reconfig) {
  990. for (pmod = cfg->compiled_modules; pmod != NULL && *pmod != NULL; pmod ++) {
  991. mod = *pmod;
  992. mod_ctx = g_slice_alloc0 (sizeof (struct module_ctx));
  993. if (mod->module_init_func (cfg, &mod_ctx) == 0) {
  994. g_hash_table_insert (cfg->c_modules,
  995. (gpointer) mod->name,
  996. mod_ctx);
  997. mod_ctx->mod = mod;
  998. }
  999. }
  1000. }
  1001. cur = g_list_first (cfg->filters);
  1002. while (cur) {
  1003. /* Perform modules configuring */
  1004. mod_ctx = NULL;
  1005. mod_ctx = g_hash_table_lookup (cfg->c_modules, cur->data);
  1006. if (mod_ctx) {
  1007. mod = mod_ctx->mod;
  1008. mod_ctx->enabled = TRUE;
  1009. if (reconfig) {
  1010. (void)mod->module_reconfig_func (cfg);
  1011. msg_debug_config ("reconfig of %s", mod->name);
  1012. }
  1013. else {
  1014. (void)mod->module_config_func (cfg);
  1015. }
  1016. }
  1017. if (mod_ctx == NULL) {
  1018. msg_warn_config ("requested unknown module %s", cur->data);
  1019. }
  1020. cur = g_list_next (cur);
  1021. }
  1022. return rspamd_init_lua_filters (cfg);
  1023. }
  1024. gboolean
  1025. rspamd_config_add_metric_symbol (struct rspamd_config *cfg,
  1026. const gchar *metric_name, const gchar *symbol,
  1027. gdouble score, const gchar *description, const gchar *group,
  1028. gboolean one_shot, gboolean rewrite_existing)
  1029. {
  1030. struct rspamd_symbols_group *sym_group;
  1031. struct rspamd_symbol_def *sym_def;
  1032. GList *metric_list;
  1033. struct metric *metric;
  1034. gdouble *score_ptr;
  1035. g_assert (cfg != NULL);
  1036. g_assert (symbol != NULL);
  1037. if (metric_name == NULL) {
  1038. metric_name = DEFAULT_METRIC;
  1039. }
  1040. metric = g_hash_table_lookup (cfg->metrics, metric_name);
  1041. if (metric == NULL) {
  1042. msg_err_config ("metric %s has not been found", metric_name);
  1043. return FALSE;
  1044. }
  1045. if (g_hash_table_lookup (cfg->metrics_symbols, symbol) != NULL &&
  1046. !rewrite_existing) {
  1047. msg_debug_config ("symbol %s has been already registered, do not override",
  1048. symbol);
  1049. return FALSE;
  1050. }
  1051. sym_def =
  1052. rspamd_mempool_alloc0 (cfg->cfg_pool, sizeof (struct rspamd_symbol_def));
  1053. score_ptr = rspamd_mempool_alloc (cfg->cfg_pool, sizeof (gdouble));
  1054. *score_ptr = score;
  1055. sym_def->score = score;
  1056. sym_def->weight_ptr = score_ptr;
  1057. sym_def->name = rspamd_mempool_strdup (cfg->cfg_pool, symbol);
  1058. if (one_shot) {
  1059. sym_def->flags |= RSPAMD_SYMBOL_FLAG_ONESHOT;
  1060. }
  1061. if (description) {
  1062. sym_def->description = rspamd_mempool_strdup (cfg->cfg_pool, description);
  1063. }
  1064. msg_debug_config ("registered symbol %s with weight %.2f in metric %s and group %s",
  1065. sym_def->name, score, metric->name, group);
  1066. g_hash_table_insert (metric->symbols, sym_def->name, sym_def);
  1067. if ((metric_list =
  1068. g_hash_table_lookup (cfg->metrics_symbols, sym_def->name)) == NULL) {
  1069. metric_list = g_list_prepend (NULL, metric);
  1070. rspamd_mempool_add_destructor (cfg->cfg_pool,
  1071. (rspamd_mempool_destruct_t)g_list_free,
  1072. metric_list);
  1073. g_hash_table_insert (cfg->metrics_symbols, sym_def->name, metric_list);
  1074. }
  1075. else {
  1076. /* Slow but keep start element of list in safe */
  1077. if (!g_list_find (metric_list, metric)) {
  1078. metric_list = g_list_append (metric_list, metric);
  1079. }
  1080. }
  1081. /* Search for symbol group */
  1082. if (group == NULL) {
  1083. group = "ungrouped";
  1084. }
  1085. sym_group = g_hash_table_lookup (metric->groups, group);
  1086. if (sym_group == NULL) {
  1087. /* Create new group */
  1088. sym_group = rspamd_config_new_group (cfg, metric, group);
  1089. }
  1090. sym_def->gr = sym_group;
  1091. g_hash_table_insert (sym_group->symbols, sym_def->name, sym_def);
  1092. return TRUE;
  1093. }
  1094. gboolean
  1095. rspamd_config_is_module_enabled (struct rspamd_config *cfg,
  1096. const gchar *module_name)
  1097. {
  1098. gboolean is_c = FALSE;
  1099. struct metric *metric;
  1100. const ucl_object_t *conf, *enabled;
  1101. GList *cur;
  1102. struct rspamd_symbols_group *gr;
  1103. metric = cfg->default_metric;
  1104. if (g_hash_table_lookup (cfg->c_modules, module_name)) {
  1105. is_c = TRUE;
  1106. }
  1107. if (g_hash_table_lookup (cfg->explicit_modules, module_name) != NULL) {
  1108. /* Always load module */
  1109. return TRUE;
  1110. }
  1111. if (is_c) {
  1112. gboolean found = FALSE;
  1113. cur = g_list_first (cfg->filters);
  1114. while (cur) {
  1115. if (strcmp (cur->data, module_name) == 0) {
  1116. found = TRUE;
  1117. break;
  1118. }
  1119. cur = g_list_next (cur);
  1120. }
  1121. if (!found) {
  1122. msg_info_config ("internal module %s is disable in `filters` line",
  1123. module_name);
  1124. return FALSE;
  1125. }
  1126. }
  1127. conf = ucl_object_find_key (cfg->rcl_obj, module_name);
  1128. if (conf == NULL) {
  1129. msg_info_config ("%s module %s is enabled but has not been configured",
  1130. is_c ? "internal" : "lua", module_name);
  1131. if (!is_c) {
  1132. msg_info_config ("%s disabling unconfigured lua module", module_name);
  1133. return FALSE;
  1134. }
  1135. }
  1136. else {
  1137. enabled = ucl_object_find_key (conf, "enabled");
  1138. if (enabled && ucl_object_type (enabled) == UCL_BOOLEAN) {
  1139. if (!ucl_object_toboolean (enabled)) {
  1140. msg_info_config ("%s module %s is disabled in the configuration",
  1141. is_c ? "internal" : "lua", module_name);
  1142. return FALSE;
  1143. }
  1144. }
  1145. }
  1146. /* Now we check symbols group */
  1147. gr = g_hash_table_lookup (metric->groups, module_name);
  1148. if (gr) {
  1149. if (gr->disabled) {
  1150. msg_info_config ("%s module %s is disabled in the configuration as "
  1151. "its group has been disabled",
  1152. is_c ? "internal" : "lua", module_name);
  1153. return FALSE;
  1154. }
  1155. }
  1156. return TRUE;
  1157. }