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_file.h 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /*-
  2. * Copyright 2016-2017 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. #ifndef CFG_FILE_H
  17. #define CFG_FILE_H
  18. #include "config.h"
  19. #include "mem_pool.h"
  20. #include "upstream.h"
  21. #include "rspamd_symcache.h"
  22. #include "cfg_rcl.h"
  23. #include "ucl.h"
  24. #include "regexp.h"
  25. #include "libserver/re_cache.h"
  26. #include "ref.h"
  27. #include "libutil/radix.h"
  28. #include "monitored.h"
  29. #include "redis_pool.h"
  30. #define DEFAULT_BIND_PORT 11333
  31. #define DEFAULT_CONTROL_PORT 11334
  32. /* Default metric name */
  33. #define DEFAULT_METRIC "default"
  34. struct expression;
  35. struct tokenizer;
  36. struct rspamd_stat_classifier;
  37. struct module_s;
  38. struct worker_s;
  39. struct rspamd_external_libs_ctx;
  40. struct rspamd_cryptobox_pubkey;
  41. struct rspamd_dns_resolver;
  42. /**
  43. * Types of rspamd bind lines
  44. */
  45. enum rspamd_cred_type {
  46. CRED_NORMAL,
  47. CRED_CONTROL,
  48. CRED_LMTP,
  49. CRED_DELIVERY
  50. };
  51. /**
  52. * Logging type
  53. */
  54. enum rspamd_log_type {
  55. RSPAMD_LOG_CONSOLE,
  56. RSPAMD_LOG_SYSLOG,
  57. RSPAMD_LOG_FILE
  58. };
  59. enum rspamd_log_cfg_flags {
  60. RSPAMD_LOG_FLAG_DEFAULT = 0,
  61. RSPAMD_LOG_FLAG_SYSTEMD = (1 << 0),
  62. RSPAMD_LOG_FLAG_COLOR = (1 << 1),
  63. RSPAMD_LOG_FLAG_RE_CACHE = (1 << 2),
  64. RSPAMD_LOG_FLAG_USEC = (1 << 3),
  65. RSPAMD_LOG_FLAG_RSPAMADM = (1 << 4),
  66. };
  67. struct rspamd_worker_log_pipe {
  68. gint fd;
  69. gint type;
  70. struct rspamd_worker_log_pipe *prev, *next;
  71. };
  72. /**
  73. * script module list item
  74. */
  75. struct script_module {
  76. gchar *name; /**< name of module */
  77. gchar *path; /**< path to module */
  78. };
  79. /**
  80. * Type of lua variable
  81. */
  82. enum lua_var_type {
  83. LUA_VAR_NUM,
  84. LUA_VAR_BOOLEAN,
  85. LUA_VAR_STRING,
  86. LUA_VAR_FUNCTION,
  87. LUA_VAR_UNKNOWN
  88. };
  89. /**
  90. * Symbols group
  91. */
  92. struct rspamd_symbol;
  93. struct rspamd_symbols_group {
  94. gchar *name;
  95. GHashTable *symbols;
  96. gdouble max_score;
  97. gboolean disabled;
  98. gboolean one_shot;
  99. };
  100. #define RSPAMD_SYMBOL_FLAG_IGNORE (1 << 1)
  101. #define RSPAMD_SYMBOL_FLAG_ONEPARAM (1 << 2)
  102. #define RSPAMD_SYMBOL_FLAG_UNGROUPPED (1 << 3)
  103. /**
  104. * Symbol config definition
  105. */
  106. struct rspamd_symbol {
  107. gchar *name;
  108. gchar *description;
  109. gdouble *weight_ptr;
  110. gdouble score;
  111. guint priority;
  112. struct rspamd_symbols_group *gr; /* Main group */
  113. GPtrArray *groups; /* Other groups */
  114. guint flags;
  115. gint nshots;
  116. };
  117. /**
  118. * Statfile config definition
  119. */
  120. struct rspamd_statfile_config {
  121. gchar *symbol; /**< symbol of statfile */
  122. gchar *label; /**< label of this statfile */
  123. ucl_object_t *opts; /**< other options */
  124. gboolean is_spam; /**< spam flag */
  125. struct rspamd_classifier_config *clcf; /**< parent pointer of classifier configuration */
  126. gpointer data; /**< opaque data */
  127. };
  128. struct rspamd_tokenizer_config {
  129. const ucl_object_t *opts; /**< other options */
  130. const gchar *name; /**< name of tokenizer */
  131. };
  132. /* Classifier has all integer values (e.g. bayes) */
  133. #define RSPAMD_FLAG_CLASSIFIER_INTEGER (1 << 0)
  134. /*
  135. * Set if backend for a classifier is intended to increment and not set values
  136. * (e.g. redis)
  137. */
  138. #define RSPAMD_FLAG_CLASSIFIER_INCREMENTING_BACKEND (1 << 1)
  139. /*
  140. * No backend required for classifier
  141. */
  142. #define RSPAMD_FLAG_CLASSIFIER_NO_BACKEND (1 << 2)
  143. /**
  144. * Classifier config definition
  145. */
  146. struct rspamd_classifier_config {
  147. GList *statfiles; /**< statfiles list */
  148. GHashTable *labels; /**< statfiles with labels */
  149. gchar *metric; /**< metric of this classifier */
  150. gchar *classifier; /**< classifier interface */
  151. struct rspamd_tokenizer_config *tokenizer; /**< tokenizer used for classifier */
  152. const gchar *backend; /**< name of statfile's backend */
  153. ucl_object_t *opts; /**< other options */
  154. GList *learn_conditions; /**< list of learn condition callbacks */
  155. gchar *name; /**< unique name of classifier */
  156. guint32 min_tokens; /**< minimal number of tokens to process classifier */
  157. guint32 max_tokens; /**< maximum number of tokens */
  158. guint min_token_hits; /**< minimum number of hits for a token to be considered */
  159. gdouble min_prob_strength; /**< use only tokens with probability in [0.5 - MPS, 0.5 + MPS] */
  160. guint min_learns; /**< minimum number of learns for each statfile */
  161. guint flags;
  162. };
  163. struct rspamd_worker_bind_conf {
  164. GPtrArray *addrs;
  165. guint cnt;
  166. gchar *name;
  167. const gchar *bind_line;
  168. gboolean is_systemd;
  169. struct rspamd_worker_bind_conf *next;
  170. };
  171. struct rspamd_worker_lua_script {
  172. gint cbref;
  173. struct rspamd_worker_lua_script *prev, *next;
  174. };
  175. /**
  176. * Config params for rspamd worker
  177. */
  178. struct rspamd_worker_conf {
  179. struct worker_s *worker; /**< pointer to worker type */
  180. GQuark type; /**< type of worker */
  181. struct rspamd_worker_bind_conf *bind_conf; /**< bind configuration */
  182. gint16 count; /**< number of workers */
  183. GList *listen_socks; /**< listening sockets descriptors */
  184. guint32 rlimit_nofile; /**< max files limit */
  185. guint32 rlimit_maxcore; /**< maximum core file size */
  186. GHashTable *params; /**< params for worker */
  187. GQueue *active_workers; /**< linked list of spawned workers */
  188. gboolean has_socket; /**< whether we should make listening socket in main process */
  189. gpointer *ctx; /**< worker's context */
  190. ucl_object_t *options; /**< other worker's options */
  191. struct rspamd_worker_lua_script *scripts; /**< registered lua scripts */
  192. gboolean enabled;
  193. ref_entry_t ref;
  194. };
  195. enum rspamd_log_format_type {
  196. RSPAMD_LOG_STRING = 0,
  197. RSPAMD_LOG_MID,
  198. RSPAMD_LOG_QID,
  199. RSPAMD_LOG_USER,
  200. RSPAMD_LOG_ISSPAM,
  201. RSPAMD_LOG_ACTION,
  202. RSPAMD_LOG_SCORES,
  203. RSPAMD_LOG_SYMBOLS,
  204. RSPAMD_LOG_IP,
  205. RSPAMD_LOG_LEN,
  206. RSPAMD_LOG_DNS_REQ,
  207. RSPAMD_LOG_SMTP_FROM,
  208. RSPAMD_LOG_MIME_FROM,
  209. RSPAMD_LOG_SMTP_RCPT,
  210. RSPAMD_LOG_MIME_RCPT,
  211. RSPAMD_LOG_SMTP_RCPTS,
  212. RSPAMD_LOG_MIME_RCPTS,
  213. RSPAMD_LOG_TIME_REAL,
  214. RSPAMD_LOG_TIME_VIRTUAL,
  215. RSPAMD_LOG_LUA,
  216. RSPAMD_LOG_DIGEST,
  217. RSPAMD_LOG_FILENAME,
  218. RSPAMD_LOG_FORCED_ACTION,
  219. };
  220. enum rspamd_log_format_flags {
  221. RSPAMD_LOG_FMT_FLAG_DEFAULT = 0,
  222. RSPAMD_LOG_FMT_FLAG_OPTIONAL = (1 << 0),
  223. RSPAMD_LOG_FMT_FLAG_MIME_ALTERNATIVE = (1 << 1),
  224. RSPAMD_LOG_FMT_FLAG_CONDITION = (1 << 2),
  225. RSPAMD_LOG_FMT_FLAG_SYMBOLS_SCORES = (1 << 3),
  226. RSPAMD_LOG_FMT_FLAG_SYMBOLS_PARAMS = (1 << 4)
  227. };
  228. struct rspamd_log_format {
  229. enum rspamd_log_format_type type;
  230. guint flags;
  231. gsize len;
  232. gpointer data;
  233. struct rspamd_log_format *prev, *next;
  234. };
  235. /**
  236. * Standard actions
  237. */
  238. enum rspamd_action_type {
  239. METRIC_ACTION_REJECT = 0,
  240. METRIC_ACTION_SOFT_REJECT,
  241. METRIC_ACTION_REWRITE_SUBJECT,
  242. METRIC_ACTION_ADD_HEADER,
  243. METRIC_ACTION_GREYLIST,
  244. METRIC_ACTION_NOACTION,
  245. METRIC_ACTION_MAX,
  246. METRIC_ACTION_CUSTOM = 999,
  247. METRIC_ACTION_DISCARD,
  248. METRIC_ACTION_QUARANTINE
  249. };
  250. enum rspamd_action_flags {
  251. RSPAMD_ACTION_NORMAL = 0,
  252. RSPAMD_ACTION_NO_THRESHOLD = (1u << 0),
  253. RSPAMD_ACTION_THRESHOLD_ONLY = (1u << 1),
  254. RSPAMD_ACTION_HAM = (1u << 2),
  255. RSPAMD_ACTION_MILTER = (1u << 3),
  256. };
  257. struct rspamd_action;
  258. struct rspamd_config_post_load_script {
  259. gint cbref;
  260. struct rspamd_config_post_load_script *prev, *next;
  261. };
  262. struct rspamd_lang_detector;
  263. /**
  264. * Structure that stores all config data
  265. */
  266. struct rspamd_config {
  267. gchar *rspamd_user; /**< user to run as */
  268. gchar *rspamd_group; /**< group to run as */
  269. rspamd_mempool_t *cfg_pool; /**< memory pool for config */
  270. gchar *cfg_name; /**< name of config file */
  271. gchar *pid_file; /**< name of pid file */
  272. gchar *temp_dir; /**< dir for temp files */
  273. gchar *control_socket_path; /**< path to the control socket */
  274. const ucl_object_t *local_addrs; /**< tree of local addresses */
  275. #ifdef WITH_GPERF_TOOLS
  276. gchar *profile_path;
  277. #endif
  278. gdouble unknown_weight; /**< weight of unknown symbols */
  279. gdouble grow_factor; /**< grow factor for metric */
  280. GHashTable *symbols; /**< weights of symbols in metric */
  281. const gchar *subject; /**< subject rewrite string */
  282. GHashTable * groups; /**< groups of symbols */
  283. struct rspamd_action *actions; /**< all actions of the metric */
  284. gboolean raw_mode; /**< work in raw mode instead of utf one */
  285. gboolean one_shot_mode; /**< rules add only one symbol */
  286. gboolean check_text_attachements; /**< check text attachements as text */
  287. gboolean check_all_filters; /**< check all filters */
  288. gboolean allow_raw_input; /**< scan messages with invalid mime */
  289. gboolean disable_hyperscan; /**< disable hyperscan usage */
  290. gboolean vectorized_hyperscan; /**< use vectorized hyperscan matching */
  291. gboolean enable_shutdown_workaround; /**< enable workaround for legacy SA clients (exim) */
  292. gboolean ignore_received; /**< Ignore data from the first received header */
  293. gboolean enable_sessions_cache; /**< Enable session cache for debug */
  294. gboolean enable_experimental; /**< Enable experimental plugins */
  295. gboolean disable_pcre_jit; /**< Disable pcre JIT */
  296. gboolean disable_lua_squeeze; /**< Disable lua rules squeezing */
  297. gboolean own_lua_state; /**< True if we have created lua_state internally */
  298. gboolean soft_reject_on_timeout; /**< If true emit soft reject on task timeout (if not reject) */
  299. gsize max_cores_size; /**< maximum size occupied by rspamd core files */
  300. gsize max_cores_count; /**< maximum number of core files */
  301. gchar *cores_dir; /**< directory for core files */
  302. gsize max_message; /**< maximum size for messages */
  303. gsize max_pic_size; /**< maximum size for a picture to process */
  304. gsize images_cache_size; /**< size of LRU cache for DCT data from images */
  305. gdouble task_timeout; /**< maximum message processing time */
  306. gint default_max_shots; /**< default maximum count of symbols hits permitted (-1 for unlimited) */
  307. enum rspamd_log_type log_type; /**< log type */
  308. gint log_facility; /**< log facility in case of syslog */
  309. gint log_level; /**< log level trigger */
  310. gchar *log_file; /**< path to logfile in case of file logging */
  311. gboolean log_buffered; /**< whether logging is buffered */
  312. gboolean log_silent_workers; /**< silence info messages from workers */
  313. guint32 log_buf_size; /**< length of log buffer */
  314. const ucl_object_t *debug_ip_map; /**< turn on debugging for specified ip addresses */
  315. gboolean log_urls; /**< whether we should log URLs */
  316. GHashTable *debug_modules; /**< logging modules to debug */
  317. struct rspamd_cryptobox_pubkey *log_encryption_key; /**< encryption key for logs */
  318. guint log_flags; /**< logging flags */
  319. guint log_error_elts; /**< number of elements in error logbuf */
  320. guint log_error_elt_maxlen; /**< maximum size of error log element */
  321. struct rspamd_worker_log_pipe *log_pipes;
  322. gboolean compat_messages; /**< use old messages in the protocol (array) */
  323. GList *script_modules; /**< linked list of script modules to load */
  324. GHashTable *explicit_modules; /**< modules that should be always loaded */
  325. GList *filters; /**< linked list of all filters */
  326. GList *workers; /**< linked list of all workers params */
  327. GHashTable *wrk_parsers; /**< hash for worker config parsers, indexed by worker quarks */
  328. ucl_object_t *rcl_obj; /**< rcl object */
  329. ucl_object_t *config_comments; /**< comments saved from the config */
  330. ucl_object_t *doc_strings; /**< documentation strings for config options */
  331. GPtrArray *c_modules; /**< list of C modules */
  332. GHashTable * composite_symbols; /**< hash of composite symbols indexed by its name */
  333. GList *classifiers; /**< list of all classifiers defined */
  334. GList *statfiles; /**< list of all statfiles in config file order */
  335. GHashTable *classifiers_symbols; /**< hashtable indexed by symbol name of classifiers */
  336. GHashTable * cfg_params; /**< all cfg params indexed by its name in this structure */
  337. gchar *dynamic_conf; /**< path to dynamic configuration */
  338. ucl_object_t *current_dynamic_conf; /**< currently loaded dynamic configuration */
  339. GHashTable * domain_settings; /**< settings per-domains */
  340. GHashTable * user_settings; /**< settings per-user */
  341. gchar * domain_settings_str; /**< string representation of settings */
  342. gchar * user_settings_str;
  343. gint clock_res; /**< resolution of clock used */
  344. GList *maps; /**< maps active */
  345. gdouble map_timeout; /**< maps watch timeout */
  346. gdouble map_file_watch_multiplier; /**< multiplier for watch timeout when maps are files */
  347. gchar *maps_cache_dir; /**< where to save HTTP cached data */
  348. gdouble monitored_interval; /**< interval between monitored checks */
  349. gboolean disable_monitored; /**< disable monitoring completely */
  350. struct rspamd_symcache *cache; /**< symbols cache object */
  351. gchar *cache_filename; /**< filename of cache file */
  352. gdouble cache_reload_time; /**< how often cache reload should be performed */
  353. gchar * checksum; /**< real checksum of config file */
  354. gchar * dump_checksum; /**< dump checksum of config file */
  355. gpointer lua_state; /**< pointer to lua state */
  356. gpointer lua_thread_pool; /**< pointer to lua thread (coroutine) pool */
  357. gchar * rrd_file; /**< rrd file to store statistics */
  358. gchar * history_file; /**< file to save rolling history */
  359. gchar * tld_file; /**< file to load effective tld list from */
  360. gchar * hs_cache_dir; /**< directory to save hyperscan databases */
  361. gchar * magic_file; /**< file to initialize libmagic */
  362. gdouble dns_timeout; /**< timeout in milliseconds for waiting for dns reply */
  363. guint32 dns_retransmits; /**< maximum retransmits count */
  364. guint32 dns_throttling_errors; /**< maximum errors for starting resolver throttling */
  365. guint32 dns_throttling_time; /**< time in seconds for DNS throttling */
  366. guint32 dns_io_per_server; /**< number of sockets per DNS server */
  367. const ucl_object_t *nameservers; /**< list of nameservers or NULL to parse resolv.conf */
  368. guint32 dns_max_requests; /**< limit of DNS requests per task */
  369. gboolean enable_dnssec; /**< enable dnssec stub resolver */
  370. guint upstream_max_errors; /**< upstream max errors before shutting off */
  371. gdouble upstream_error_time; /**< rate of upstream errors */
  372. gdouble upstream_revive_time; /**< revive timeout for upstreams */
  373. struct upstream_ctx *ups_ctx; /**< upstream context */
  374. struct rspamd_dns_resolver *dns_resolver; /**< dns resolver if loaded */
  375. guint min_word_len; /**< minimum length of the word to be considered */
  376. guint max_word_len; /**< maximum length of the word to be considered */
  377. guint words_decay; /**< limit for words for starting adaptive ignoring */
  378. guint history_rows; /**< number of history rows stored */
  379. guint max_sessions_cache; /**< maximum number of sessions cache elts */
  380. GList *classify_headers; /**< list of headers using for statistics */
  381. struct module_s **compiled_modules; /**< list of compiled C modules */
  382. struct worker_s **compiled_workers; /**< list of compiled C modules */
  383. struct rspamd_config_post_load_script *finish_callbacks; /**< list of callbacks called on worker's termination */
  384. struct rspamd_log_format *log_format; /**< parsed log format */
  385. gchar *log_format_str; /**< raw log format string */
  386. struct rspamd_external_libs_ctx *libs_ctx; /**< context for external libraries */
  387. struct rspamd_monitored_ctx *monitored_ctx; /**< context for monitored resources */
  388. struct rspamd_redis_pool *redis_pool; /**< redis connectiosn pool */
  389. struct rspamd_re_cache *re_cache; /**< static regexp cache */
  390. GHashTable *trusted_keys; /**< list of trusted public keys */
  391. struct rspamd_config_post_load_script *on_load; /**< list of scripts executed on config load */
  392. gchar *ssl_ca_path; /**< path to CA certs */
  393. gchar *ssl_ciphers; /**< set of preferred ciphers */
  394. gchar *zstd_input_dictionary; /**< path to zstd input dictionary */
  395. gchar *zstd_output_dictionary; /**< path to zstd output dictionary */
  396. ucl_object_t *neighbours; /**< other servers in the cluster */
  397. struct rspamd_lang_detector *lang_det; /**< language detector */
  398. ref_entry_t ref; /**< reference counter */
  399. };
  400. /**
  401. * Parse bind credits
  402. * @param cf config file to use
  403. * @param str line that presents bind line
  404. * @param type type of credits
  405. * @return 1 if line was successfully parsed and 0 in case of error
  406. */
  407. gboolean rspamd_parse_bind_line (struct rspamd_config *cfg,
  408. struct rspamd_worker_conf *cf, const gchar *str);
  409. enum rspamd_config_init_flags {
  410. RSPAMD_CONFIG_INIT_DEFAULT = 0,
  411. RSPAMD_CONFIG_INIT_SKIP_LUA = (1 << 0)
  412. };
  413. /**
  414. * Init default values
  415. * @param cfg config file
  416. */
  417. struct rspamd_config *rspamd_config_new (enum rspamd_config_init_flags flags);
  418. /**
  419. * Free memory used by config structure
  420. * @param cfg config file
  421. */
  422. void rspamd_config_free (struct rspamd_config *cfg);
  423. /**
  424. * Gets module option with specified name
  425. * @param cfg config file
  426. * @param module_name name of module
  427. * @param opt_name name of option to get
  428. * @return module value or NULL if option does not defined
  429. */
  430. const ucl_object_t * rspamd_config_get_module_opt (struct rspamd_config *cfg,
  431. const gchar *module_name,
  432. const gchar *opt_name);
  433. /**
  434. * Parse flag
  435. * @param str string representation of flag (eg. 'on')
  436. * @return numeric value of flag (0 or 1)
  437. */
  438. gchar rspamd_config_parse_flag (const gchar *str, guint len);
  439. enum rspamd_post_load_options {
  440. RSPAMD_CONFIG_INIT_URL = 1 << 0,
  441. RSPAMD_CONFIG_INIT_LIBS = 1 << 1,
  442. RSPAMD_CONFIG_INIT_SYMCACHE = 1 << 2,
  443. RSPAMD_CONFIG_INIT_VALIDATE = 1 << 3,
  444. RSPAMD_CONFIG_INIT_NO_TLD = 1 << 4,
  445. RSPAMD_CONFIG_INIT_PRELOAD_MAPS = 1 << 5,
  446. };
  447. #define RSPAMD_CONFIG_LOAD_ALL (RSPAMD_CONFIG_INIT_URL| \
  448. RSPAMD_CONFIG_INIT_LIBS| \
  449. RSPAMD_CONFIG_INIT_SYMCACHE| \
  450. RSPAMD_CONFIG_INIT_VALIDATE| \
  451. RSPAMD_CONFIG_INIT_PRELOAD_MAPS)
  452. /**
  453. * Do post load actions for config
  454. * @param cfg config file
  455. */
  456. gboolean rspamd_config_post_load (struct rspamd_config *cfg,
  457. enum rspamd_post_load_options opts);
  458. /**
  459. * Calculate checksum for config file
  460. * @param cfg config file
  461. */
  462. gboolean rspamd_config_calculate_checksum (struct rspamd_config *cfg);
  463. /**
  464. * Replace all \" with a single " in given string
  465. * @param line input string
  466. */
  467. void rspamd_config_unescape_quotes (gchar *line);
  468. /*
  469. * Convert comma separated string to a list of strings
  470. */
  471. GList * rspamd_config_parse_comma_list (rspamd_mempool_t *pool,
  472. const gchar *line);
  473. /*
  474. * Return a new classifier_config structure, setting default and non-conflicting attributes
  475. */
  476. struct rspamd_classifier_config * rspamd_config_new_classifier (
  477. struct rspamd_config *cfg,
  478. struct rspamd_classifier_config *c);
  479. /*
  480. * Return a new worker_conf structure, setting default and non-conflicting attributes
  481. */
  482. struct rspamd_worker_conf * rspamd_config_new_worker (struct rspamd_config *cfg,
  483. struct rspamd_worker_conf *c);
  484. /*
  485. * Return a new metric structure, setting default and non-conflicting attributes
  486. */
  487. void rspamd_config_init_metric (struct rspamd_config *cfg);
  488. /*
  489. * Return new symbols group definition
  490. */
  491. struct rspamd_symbols_group * rspamd_config_new_group (
  492. struct rspamd_config *cfg,
  493. const gchar *name);
  494. /*
  495. * Return a new statfile structure, setting default and non-conflicting attributes
  496. */
  497. struct rspamd_statfile_config * rspamd_config_new_statfile (
  498. struct rspamd_config *cfg,
  499. struct rspamd_statfile_config *c);
  500. /*
  501. * Register symbols of classifiers inside metrics
  502. */
  503. void rspamd_config_insert_classify_symbols (struct rspamd_config *cfg);
  504. /*
  505. * Check statfiles inside a classifier
  506. */
  507. gboolean rspamd_config_check_statfiles (struct rspamd_classifier_config *cf);
  508. /*
  509. * Find classifier config by name
  510. */
  511. struct rspamd_classifier_config * rspamd_config_find_classifier (
  512. struct rspamd_config *cfg,
  513. const gchar *name);
  514. void rspamd_ucl_add_conf_macros (struct ucl_parser *parser,
  515. struct rspamd_config *cfg);
  516. void rspamd_ucl_add_conf_variables (struct ucl_parser *parser, GHashTable *vars);
  517. /**
  518. * Initialize rspamd filtering system (lua and C filters)
  519. * @param cfg
  520. * @param reconfig
  521. * @return
  522. */
  523. gboolean rspamd_init_filters (struct rspamd_config *cfg, bool reconfig);
  524. /**
  525. * Add new symbol to the metric
  526. * @param cfg
  527. * @param metric metric's name (or NULL for the default metric)
  528. * @param symbol symbol's name
  529. * @param score symbol's score
  530. * @param description optional description
  531. * @param group optional group name
  532. * @param one_shot TRUE if symbol can add its score once
  533. * @param rewrite_existing TRUE if we need to rewrite the existing symbol
  534. * @param priority use the following priority for a symbol
  535. * @param nshots means maximum number of hits for a symbol in metric (-1 for unlimited)
  536. * @return TRUE if symbol has been inserted or FALSE if symbol already exists with higher priority
  537. */
  538. gboolean rspamd_config_add_symbol (struct rspamd_config *cfg,
  539. const gchar *symbol,
  540. gdouble score,
  541. const gchar *description,
  542. const gchar *group,
  543. guint flags,
  544. guint priority,
  545. gint nshots);
  546. /**
  547. * Adds new group for a symbol
  548. * @param cfg
  549. * @param symbol
  550. * @param group
  551. * @return
  552. */
  553. gboolean rspamd_config_add_symbol_group (struct rspamd_config *cfg,
  554. const gchar *symbol,
  555. const gchar *group);
  556. /**
  557. * Sets action score for a specified metric with the specified priority
  558. * @param cfg config file
  559. * @param metric metric name (or NULL for default metric)
  560. * @param action_name symbolic name of action
  561. * @param obj data to set for action
  562. * @return TRUE if symbol has been inserted or FALSE if action already exists with higher priority
  563. */
  564. gboolean rspamd_config_set_action_score (struct rspamd_config *cfg,
  565. const gchar *action_name,
  566. const ucl_object_t *obj);
  567. /**
  568. * Check priority and maybe disable action completely
  569. * @param cfg
  570. * @param action_name
  571. * @param priority
  572. * @return
  573. */
  574. gboolean rspamd_config_maybe_disable_action (struct rspamd_config *cfg,
  575. const gchar *action_name,
  576. guint priority);
  577. /**
  578. * Checks if a specified C or lua module is enabled or disabled in the config.
  579. * The logic of check is the following:
  580. *
  581. * - For C modules, we check `filters` line and enable module only if it is found there
  582. * - For LUA modules we check the corresponding configuration section:
  583. * - if section exists, then we check `enabled` key and check its value
  584. * - if section is absent, we consider module as disabled
  585. * - For both C and LUA modules we check if the group with the module name is disabled in the default metric
  586. * @param cfg config file
  587. * @param module_name module name
  588. * @return TRUE if a module is enabled
  589. */
  590. gboolean rspamd_config_is_module_enabled (struct rspamd_config *cfg,
  591. const gchar *module_name);
  592. /*
  593. * Get action from a string
  594. */
  595. gboolean rspamd_action_from_str (const gchar *data, gint *result);
  596. /*
  597. * Return textual representation of action enumeration
  598. */
  599. const gchar * rspamd_action_to_str (enum rspamd_action_type action);
  600. const gchar * rspamd_action_to_str_alt (enum rspamd_action_type action);
  601. /*
  602. * Resort all actions (needed to operate with thresholds)
  603. */
  604. void rspamd_actions_sort (struct rspamd_config *cfg);
  605. /**
  606. * Parse radix tree or radix map from ucl object
  607. * @param cfg configuration object
  608. * @param obj ucl object with parameter
  609. * @param target target radix tree
  610. * @param err error pointer
  611. * @return
  612. */
  613. struct rspamd_radix_map_helper;
  614. gboolean rspamd_config_radix_from_ucl (struct rspamd_config *cfg,
  615. const ucl_object_t *obj,
  616. const gchar *description,
  617. struct rspamd_radix_map_helper **target,
  618. GError **err);
  619. /**
  620. * Returns action object by name
  621. * @param cfg
  622. * @param name
  623. * @return
  624. */
  625. struct rspamd_action * rspamd_config_get_action (struct rspamd_config *cfg,
  626. const gchar *name);
  627. struct rspamd_action * rspamd_config_get_action_by_type (struct rspamd_config *cfg,
  628. enum rspamd_action_type type);
  629. #define msg_err_config(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL, \
  630. cfg->cfg_pool->tag.tagname, cfg->checksum, \
  631. G_STRFUNC, \
  632. __VA_ARGS__)
  633. #define msg_err_config_forced(...) rspamd_default_log_function (G_LOG_LEVEL_CRITICAL|RSPAMD_LOG_FORCED, \
  634. cfg->cfg_pool->tag.tagname, cfg->checksum, \
  635. G_STRFUNC, \
  636. __VA_ARGS__)
  637. #define msg_warn_config(...) rspamd_default_log_function (G_LOG_LEVEL_WARNING, \
  638. cfg->cfg_pool->tag.tagname, cfg->checksum, \
  639. G_STRFUNC, \
  640. __VA_ARGS__)
  641. #define msg_info_config(...) rspamd_default_log_function (G_LOG_LEVEL_INFO, \
  642. cfg->cfg_pool->tag.tagname, cfg->checksum, \
  643. G_STRFUNC, \
  644. __VA_ARGS__)
  645. extern guint rspamd_config_log_id;
  646. #define msg_debug_config(...) rspamd_conditional_debug_fast (NULL, NULL, \
  647. rspamd_config_log_id, "config", cfg->checksum, \
  648. G_STRFUNC, \
  649. __VA_ARGS__)
  650. #endif /* ifdef CFG_FILE_H */