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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889
  1. /*
  2. * Copyright 2024 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 "libutil/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. #ifdef __cplusplus
  35. extern "C" {
  36. #endif
  37. struct expression;
  38. struct tokenizer;
  39. struct rspamd_stat_classifier;
  40. struct module_s;
  41. struct worker_s;
  42. struct rspamd_external_libs_ctx;
  43. struct rspamd_cryptobox_pubkey;
  44. struct rspamd_dns_resolver;
  45. /**
  46. * Logging type
  47. */
  48. enum rspamd_log_type {
  49. RSPAMD_LOG_CONSOLE,
  50. RSPAMD_LOG_SYSLOG,
  51. RSPAMD_LOG_FILE
  52. };
  53. enum rspamd_log_cfg_flags {
  54. RSPAMD_LOG_FLAG_DEFAULT = 0u,
  55. RSPAMD_LOG_FLAG_SYSTEMD = (1u << 0u),
  56. RSPAMD_LOG_FLAG_COLOR = (1u << 1u),
  57. RSPAMD_LOG_FLAG_RE_CACHE = (1u << 2u),
  58. RSPAMD_LOG_FLAG_USEC = (1u << 3u),
  59. RSPAMD_LOG_FLAG_RSPAMADM = (1u << 4u),
  60. RSPAMD_LOG_FLAG_ENFORCED = (1u << 5u),
  61. RSPAMD_LOG_FLAG_SEVERITY = (1u << 6u),
  62. RSPAMD_LOG_FLAG_JSON = (1u << 7u),
  63. };
  64. struct rspamd_worker_log_pipe {
  65. int fd;
  66. int type;
  67. struct rspamd_worker_log_pipe *prev, *next;
  68. };
  69. /**
  70. * script module list item
  71. */
  72. struct script_module {
  73. char *name; /**< name of module */
  74. char *path; /**< path to module */
  75. char *digest;
  76. };
  77. enum rspamd_symbol_group_flags {
  78. RSPAMD_SYMBOL_GROUP_NORMAL = 0u,
  79. RSPAMD_SYMBOL_GROUP_DISABLED = (1u << 0u),
  80. RSPAMD_SYMBOL_GROUP_ONE_SHOT = (1u << 1u),
  81. RSPAMD_SYMBOL_GROUP_UNGROUPED = (1u << 2u),
  82. RSPAMD_SYMBOL_GROUP_PUBLIC = (1u << 3u),
  83. };
  84. /**
  85. * Symbols group
  86. */
  87. struct rspamd_symbol;
  88. struct rspamd_symbols_group {
  89. char *name;
  90. char *description;
  91. GHashTable *symbols;
  92. double max_score;
  93. unsigned int flags;
  94. };
  95. enum rspamd_symbol_flags {
  96. RSPAMD_SYMBOL_FLAG_NORMAL = 0,
  97. RSPAMD_SYMBOL_FLAG_IGNORE_METRIC = (1 << 1),
  98. RSPAMD_SYMBOL_FLAG_ONEPARAM = (1 << 2),
  99. RSPAMD_SYMBOL_FLAG_UNGROUPED = (1 << 3),
  100. RSPAMD_SYMBOL_FLAG_DISABLED = (1 << 4),
  101. RSPAMD_SYMBOL_FLAG_UNSCORED = (1 << 5),
  102. };
  103. /**
  104. * Symbol config definition
  105. */
  106. struct rspamd_symbol {
  107. char *name;
  108. char *description;
  109. double *weight_ptr;
  110. double score;
  111. unsigned int priority;
  112. struct rspamd_symbols_group *gr; /* Main group */
  113. GPtrArray *groups; /* Other groups */
  114. unsigned int flags;
  115. void *cache_item;
  116. int nshots;
  117. };
  118. /**
  119. * Statfile config definition
  120. */
  121. struct rspamd_statfile_config {
  122. char *symbol; /**< symbol of statfile */
  123. char *label; /**< label of this statfile */
  124. ucl_object_t *opts; /**< other options */
  125. gboolean is_spam; /**< spam flag */
  126. struct rspamd_classifier_config *clcf; /**< parent pointer of classifier configuration */
  127. gpointer data; /**< opaque data */
  128. };
  129. struct rspamd_tokenizer_config {
  130. const ucl_object_t *opts; /**< other options */
  131. const char *name; /**< name of tokenizer */
  132. };
  133. /* Classifier has all integer values (e.g. bayes) */
  134. #define RSPAMD_FLAG_CLASSIFIER_INTEGER (1 << 0)
  135. /*
  136. * Set if backend for a classifier is intended to increment and not set values
  137. * (e.g. redis)
  138. */
  139. #define RSPAMD_FLAG_CLASSIFIER_INCREMENTING_BACKEND (1 << 1)
  140. /*
  141. * No backend required for classifier
  142. */
  143. #define RSPAMD_FLAG_CLASSIFIER_NO_BACKEND (1 << 2)
  144. /**
  145. * Classifier config definition
  146. */
  147. struct rspamd_classifier_config {
  148. GList *statfiles; /**< statfiles list */
  149. GHashTable *labels; /**< statfiles with labels */
  150. char *metric; /**< metric of this classifier */
  151. char *classifier; /**< classifier interface */
  152. struct rspamd_tokenizer_config *tokenizer; /**< tokenizer used for classifier */
  153. const char *backend; /**< name of statfile's backend */
  154. ucl_object_t *opts; /**< other options */
  155. GList *learn_conditions; /**< list of learn condition callbacks */
  156. GList *classify_conditions; /**< list of classify condition callbacks */
  157. char *name; /**< unique name of classifier */
  158. uint32_t min_tokens; /**< minimal number of tokens to process classifier */
  159. uint32_t max_tokens; /**< maximum number of tokens */
  160. unsigned int min_token_hits; /**< minimum number of hits for a token to be considered */
  161. double min_prob_strength; /**< use only tokens with probability in [0.5 - MPS, 0.5 + MPS] */
  162. unsigned int min_learns; /**< minimum number of learns for each statfile */
  163. unsigned int flags;
  164. };
  165. struct rspamd_worker_bind_conf {
  166. GPtrArray *addrs;
  167. unsigned int cnt;
  168. char *name;
  169. char *bind_line;
  170. gboolean is_systemd;
  171. struct rspamd_worker_bind_conf *next;
  172. };
  173. struct rspamd_worker_lua_script {
  174. int cbref;
  175. struct rspamd_worker_lua_script *prev, *next;
  176. };
  177. /**
  178. * Config params for rspamd worker
  179. */
  180. struct rspamd_worker_conf {
  181. struct worker_s *worker; /**< pointer to worker type */
  182. GQuark type; /**< type of worker */
  183. struct rspamd_worker_bind_conf *bind_conf; /**< bind configuration */
  184. int16_t count; /**< number of workers */
  185. GList *listen_socks; /**< listening sockets descriptors */
  186. uint64_t rlimit_nofile; /**< max files limit */
  187. uint64_t rlimit_maxcore; /**< maximum core file size */
  188. GHashTable *params; /**< params for worker */
  189. GQueue *active_workers; /**< linked list of spawned workers */
  190. gpointer ctx; /**< worker's context */
  191. ucl_object_t *options; /**< other worker's options */
  192. struct rspamd_worker_lua_script *scripts; /**< registered lua scripts */
  193. gboolean enabled;
  194. ref_entry_t ref;
  195. };
  196. enum rspamd_log_format_type {
  197. RSPAMD_LOG_STRING = 0,
  198. RSPAMD_LOG_MID,
  199. RSPAMD_LOG_QID,
  200. RSPAMD_LOG_USER,
  201. RSPAMD_LOG_ISSPAM,
  202. RSPAMD_LOG_ACTION,
  203. RSPAMD_LOG_SCORES,
  204. RSPAMD_LOG_SYMBOLS,
  205. RSPAMD_LOG_IP,
  206. RSPAMD_LOG_LEN,
  207. RSPAMD_LOG_DNS_REQ,
  208. RSPAMD_LOG_SMTP_FROM,
  209. RSPAMD_LOG_MIME_FROM,
  210. RSPAMD_LOG_SMTP_RCPT,
  211. RSPAMD_LOG_MIME_RCPT,
  212. RSPAMD_LOG_SMTP_RCPTS,
  213. RSPAMD_LOG_MIME_RCPTS,
  214. RSPAMD_LOG_TIME_REAL,
  215. RSPAMD_LOG_TIME_VIRTUAL,
  216. RSPAMD_LOG_LUA,
  217. RSPAMD_LOG_DIGEST,
  218. RSPAMD_LOG_FILENAME,
  219. RSPAMD_LOG_FORCED_ACTION,
  220. RSPAMD_LOG_SETTINGS_ID,
  221. RSPAMD_LOG_GROUPS,
  222. RSPAMD_LOG_PUBLIC_GROUPS,
  223. RSPAMD_LOG_MEMPOOL_SIZE,
  224. RSPAMD_LOG_MEMPOOL_WASTE,
  225. };
  226. enum rspamd_log_format_flags {
  227. RSPAMD_LOG_FMT_FLAG_DEFAULT = 0,
  228. RSPAMD_LOG_FMT_FLAG_OPTIONAL = (1 << 0),
  229. RSPAMD_LOG_FMT_FLAG_MIME_ALTERNATIVE = (1 << 1),
  230. RSPAMD_LOG_FMT_FLAG_CONDITION = (1 << 2),
  231. RSPAMD_LOG_FMT_FLAG_SYMBOLS_SCORES = (1 << 3),
  232. RSPAMD_LOG_FMT_FLAG_SYMBOLS_PARAMS = (1 << 4)
  233. };
  234. struct rspamd_log_format {
  235. enum rspamd_log_format_type type;
  236. unsigned int flags;
  237. gsize len;
  238. gpointer data;
  239. struct rspamd_log_format *prev, *next;
  240. };
  241. /**
  242. * Standard actions
  243. */
  244. enum rspamd_action_type {
  245. METRIC_ACTION_REJECT = 0,
  246. METRIC_ACTION_SOFT_REJECT,
  247. METRIC_ACTION_REWRITE_SUBJECT,
  248. METRIC_ACTION_ADD_HEADER,
  249. METRIC_ACTION_GREYLIST,
  250. METRIC_ACTION_NOACTION,
  251. METRIC_ACTION_MAX,
  252. METRIC_ACTION_CUSTOM = 999,
  253. METRIC_ACTION_DISCARD,
  254. METRIC_ACTION_QUARANTINE
  255. };
  256. enum rspamd_action_flags {
  257. RSPAMD_ACTION_NORMAL = 0u,
  258. RSPAMD_ACTION_NO_THRESHOLD = (1u << 0u),
  259. RSPAMD_ACTION_THRESHOLD_ONLY = (1u << 1u),
  260. RSPAMD_ACTION_HAM = (1u << 2u),
  261. RSPAMD_ACTION_MILTER = (1u << 3u),
  262. };
  263. struct rspamd_action;
  264. struct rspamd_config_cfg_lua_script {
  265. int cbref;
  266. int priority;
  267. char *lua_src_pos;
  268. struct rspamd_config_cfg_lua_script *prev, *next;
  269. };
  270. struct rspamd_config_post_init_script {
  271. int cbref;
  272. struct rspamd_config_post_init_script *prev, *next;
  273. };
  274. struct rspamd_lang_detector;
  275. struct rspamd_rcl_sections_map;
  276. enum rspamd_config_settings_policy {
  277. RSPAMD_SETTINGS_POLICY_DEFAULT = 0,
  278. RSPAMD_SETTINGS_POLICY_IMPLICIT_ALLOW = 1,
  279. RSPAMD_SETTINGS_POLICY_IMPLICIT_DENY = 2,
  280. };
  281. enum rspamd_gtube_patterns_policy {
  282. RSPAMD_GTUBE_DISABLED = 0, /* Disabled */
  283. RSPAMD_GTUBE_REJECT, /* Reject message with GTUBE pattern */
  284. RSPAMD_GTUBE_ALL /* Check all GTUBE like patterns */
  285. };
  286. struct rspamd_config_settings_elt {
  287. uint32_t id;
  288. enum rspamd_config_settings_policy policy;
  289. const char *name;
  290. ucl_object_t *symbols_enabled;
  291. ucl_object_t *symbols_disabled;
  292. struct rspamd_config_settings_elt *prev, *next;
  293. ref_entry_t ref;
  294. };
  295. /**
  296. * Structure that stores all config data
  297. */
  298. struct rspamd_config {
  299. char *rspamd_user; /**< user to run as */
  300. char *rspamd_group; /**< group to run as */
  301. rspamd_mempool_t *cfg_pool; /**< memory pool for config */
  302. char *cfg_name; /**< name of config file */
  303. char *pid_file; /**< name of pid file */
  304. char *temp_dir; /**< dir for temp files */
  305. char *control_socket_path; /**< path to the control socket */
  306. const ucl_object_t *local_addrs; /**< tree of local addresses */
  307. #ifdef WITH_GPERF_TOOLS
  308. char *profile_path;
  309. #endif
  310. double unknown_weight; /**< weight of unknown symbols */
  311. double grow_factor; /**< grow factor for metric */
  312. GHashTable *symbols; /**< weights of symbols in metric */
  313. const char *subject; /**< subject rewrite string */
  314. GHashTable *groups; /**< groups of symbols */
  315. void *actions; /**< all actions of the metric (opaque type) */
  316. gboolean one_shot_mode; /**< rules add only one symbol */
  317. gboolean check_text_attachements; /**< check text attachements as text */
  318. gboolean check_all_filters; /**< check all filters */
  319. gboolean allow_raw_input; /**< scan messages with invalid mime */
  320. gboolean disable_hyperscan; /**< disable hyperscan usage */
  321. gboolean vectorized_hyperscan; /**< use vectorized hyperscan matching */
  322. gboolean enable_shutdown_workaround; /**< enable workaround for legacy SA clients (exim) */
  323. gboolean ignore_received; /**< Ignore data from the first received header */
  324. gboolean enable_sessions_cache; /**< Enable session cache for debug */
  325. gboolean enable_experimental; /**< Enable experimental plugins */
  326. gboolean disable_pcre_jit; /**< Disable pcre JIT */
  327. gboolean own_lua_state; /**< True if we have created lua_state internally */
  328. gboolean soft_reject_on_timeout; /**< If true emit soft reject on task timeout (if not reject) */
  329. gboolean public_groups_only; /**< Output merely public groups everywhere */
  330. enum rspamd_gtube_patterns_policy gtube_patterns_policy; /**< Enable test patterns */
  331. gboolean enable_css_parser; /**< Enable css parsing in HTML */
  332. gsize max_cores_size; /**< maximum size occupied by rspamd core files */
  333. gsize max_cores_count; /**< maximum number of core files */
  334. char *cores_dir; /**< directory for core files */
  335. gsize max_message; /**< maximum size for messages */
  336. gsize max_pic_size; /**< maximum size for a picture to process */
  337. gsize images_cache_size; /**< size of LRU cache for DCT data from images */
  338. double task_timeout; /**< maximum message processing time */
  339. int default_max_shots; /**< default maximum count of symbols hits permitted (-1 for unlimited) */
  340. int32_t heartbeats_loss_max; /**< number of heartbeats lost to consider worker's termination */
  341. double heartbeat_interval; /**< interval for heartbeats for workers */
  342. enum rspamd_log_type log_type; /**< log type */
  343. int log_facility; /**< log facility in case of syslog */
  344. int log_level; /**< log level trigger */
  345. char *log_file; /**< path to logfile in case of file logging */
  346. gboolean log_buffered; /**< whether logging is buffered */
  347. gboolean log_silent_workers; /**< silence info messages from workers */
  348. uint32_t log_buf_size; /**< length of log buffer */
  349. const ucl_object_t *debug_ip_map; /**< turn on debugging for specified ip addresses */
  350. gboolean log_urls; /**< whether we should log URLs */
  351. GHashTable *debug_modules; /**< logging modules to debug */
  352. struct rspamd_cryptobox_pubkey *log_encryption_key; /**< encryption key for logs */
  353. unsigned int log_flags; /**< logging flags */
  354. unsigned int log_error_elts; /**< number of elements in error logbuf */
  355. unsigned int log_error_elt_maxlen; /**< maximum size of error log element */
  356. unsigned int log_task_max_elts; /**< maximum number of elements in task logging */
  357. struct rspamd_worker_log_pipe *log_pipes;
  358. gboolean compat_messages; /**< use old messages in the protocol (array) */
  359. GPtrArray *script_modules; /**< a list of script modules to load */
  360. GHashTable *explicit_modules; /**< modules that should be always loaded */
  361. GList *filters; /**< linked list of all filters */
  362. GList *workers; /**< linked list of all workers params */
  363. struct rspamd_rcl_sections_map *rcl_top_section; /**< top section for RCL config */
  364. ucl_object_t *cfg_ucl_obj; /**< ucl object */
  365. ucl_object_t *config_comments; /**< comments saved from the config */
  366. ucl_object_t *doc_strings; /**< documentation strings for config options */
  367. GPtrArray *c_modules; /**< list of C modules */
  368. void *composites_manager; /**< hash of composite symbols indexed by its name */
  369. GList *classifiers; /**< list of all classifiers defined */
  370. GList *statfiles; /**< list of all statfiles in config file order */
  371. GHashTable *classifiers_symbols; /**< hashtable indexed by symbol name of classifiers */
  372. GHashTable *cfg_params; /**< all cfg params indexed by its name in this structure */
  373. char *dynamic_conf; /**< path to dynamic configuration */
  374. ucl_object_t *current_dynamic_conf; /**< currently loaded dynamic configuration */
  375. int clock_res; /**< resolution of clock used */
  376. GList *maps; /**< maps active */
  377. double map_timeout; /**< maps watch timeout */
  378. double map_file_watch_multiplier; /**< multiplier for watch timeout when maps are files */
  379. char *maps_cache_dir; /**< where to save HTTP cached data */
  380. double monitored_interval; /**< interval between monitored checks */
  381. gboolean disable_monitored; /**< disable monitoring completely */
  382. gboolean fips_mode; /**< turn on fips mode for openssl */
  383. struct rspamd_symcache *cache; /**< symbols cache object */
  384. char *cache_filename; /**< filename of cache file */
  385. double cache_reload_time; /**< how often cache reload should be performed */
  386. char *checksum; /**< real checksum of config file */
  387. gpointer lua_state; /**< pointer to lua state */
  388. gpointer lua_thread_pool; /**< pointer to lua thread (coroutine) pool */
  389. char *rrd_file; /**< rrd file to store statistics */
  390. char *history_file; /**< file to save rolling history */
  391. char *stats_file; /**< file to save stats */
  392. char *tld_file; /**< file to load effective tld list from */
  393. char *hs_cache_dir; /**< directory to save hyperscan databases */
  394. char *events_backend; /**< string representation of the events backend used */
  395. double dns_timeout; /**< timeout in milliseconds for waiting for dns reply */
  396. uint32_t dns_retransmits; /**< maximum retransmits count */
  397. uint32_t dns_io_per_server; /**< number of sockets per DNS server */
  398. const ucl_object_t *nameservers; /**< list of nameservers or NULL to parse resolv.conf */
  399. uint32_t dns_max_requests; /**< limit of DNS requests per task */
  400. gboolean enable_dnssec; /**< enable dnssec stub resolver */
  401. unsigned int upstream_max_errors; /**< upstream max errors before shutting off */
  402. double upstream_error_time; /**< rate of upstream errors */
  403. double upstream_revive_time; /**< revive timeout for upstreams */
  404. double upstream_lazy_resolve_time; /**< lazy resolve time for upstreams */
  405. struct upstream_ctx *ups_ctx; /**< upstream context */
  406. struct rspamd_dns_resolver *dns_resolver; /**< dns resolver if loaded */
  407. unsigned int min_word_len; /**< minimum length of the word to be considered */
  408. unsigned int max_word_len; /**< maximum length of the word to be considered */
  409. unsigned int words_decay; /**< limit for words for starting adaptive ignoring */
  410. unsigned int history_rows; /**< number of history rows stored */
  411. unsigned int max_sessions_cache; /**< maximum number of sessions cache elts */
  412. unsigned int lua_gc_step; /**< lua gc step */
  413. unsigned int lua_gc_pause; /**< lua gc pause */
  414. unsigned int full_gc_iters; /**< iterations between full gc cycle */
  415. unsigned int max_lua_urls; /**< maximum number of urls to be passed to Lua */
  416. unsigned int max_urls; /**< maximum number of urls to be processed in general */
  417. int max_recipients; /**< maximum number of recipients to be processed */
  418. unsigned int max_blas_threads; /**< maximum threads for openblas when learning ANN */
  419. unsigned int max_opts_len; /**< maximum length for all options for a symbol */
  420. gsize max_html_len; /**< maximum length of HTML document */
  421. struct module_s **compiled_modules; /**< list of compiled C modules */
  422. struct worker_s **compiled_workers; /**< list of compiled C modules */
  423. struct rspamd_log_format *log_format; /**< parsed log format */
  424. char *log_format_str; /**< raw log format string */
  425. struct rspamd_external_libs_ctx *libs_ctx; /**< context for external libraries */
  426. struct rspamd_monitored_ctx *monitored_ctx; /**< context for monitored resources */
  427. void *redis_pool; /**< redis connection pool */
  428. struct rspamd_re_cache *re_cache; /**< static regexp cache */
  429. GHashTable *trusted_keys; /**< list of trusted public keys */
  430. struct rspamd_config_cfg_lua_script *on_load_scripts; /**< list of scripts executed on workers load */
  431. struct rspamd_config_cfg_lua_script *post_init_scripts; /**< list of scripts executed on config being fully loaded */
  432. struct rspamd_config_cfg_lua_script *on_term_scripts; /**< list of callbacks called on worker's termination */
  433. struct rspamd_config_cfg_lua_script *config_unload_scripts; /**< list of scripts executed on config unload */
  434. char *ssl_ca_path; /**< path to CA certs */
  435. char *ssl_ciphers; /**< set of preferred ciphers */
  436. char *zstd_input_dictionary; /**< path to zstd input dictionary */
  437. char *zstd_output_dictionary; /**< path to zstd output dictionary */
  438. ucl_object_t *neighbours; /**< other servers in the cluster */
  439. struct rspamd_config_settings_elt *setting_ids; /**< preprocessed settings ids */
  440. struct rspamd_lang_detector *lang_det; /**< language detector */
  441. struct rspamd_worker *cur_worker; /**< set dynamically by each worker */
  442. ref_entry_t ref; /**< reference counter */
  443. };
  444. /**
  445. * Parse bind credits
  446. * @param cf config file to use
  447. * @param str line that presents bind line
  448. * @param type type of credits
  449. * @return 1 if line was successfully parsed and 0 in case of error
  450. */
  451. gboolean rspamd_parse_bind_line(struct rspamd_config *cfg,
  452. struct rspamd_worker_conf *cf, const char *str);
  453. enum rspamd_config_init_flags {
  454. RSPAMD_CONFIG_INIT_DEFAULT = 0u,
  455. RSPAMD_CONFIG_INIT_SKIP_LUA = (1u << 0u),
  456. RSPAMD_CONFIG_INIT_WIPE_LUA_MEM = (1u << 1u),
  457. };
  458. /**
  459. * Init default values
  460. * @param cfg config file
  461. */
  462. struct rspamd_config *rspamd_config_new(enum rspamd_config_init_flags flags);
  463. /**
  464. * Free memory used by config structure
  465. * @param cfg config file
  466. */
  467. void rspamd_config_free(struct rspamd_config *cfg);
  468. /**
  469. * Gets module option with specified name
  470. * @param cfg config file
  471. * @param module_name name of module
  472. * @param opt_name name of option to get
  473. * @return module value or NULL if option does not defined
  474. */
  475. const ucl_object_t *rspamd_config_get_module_opt(struct rspamd_config *cfg,
  476. const char *module_name,
  477. const char *opt_name) G_GNUC_WARN_UNUSED_RESULT;
  478. /**
  479. * Parse flag
  480. * @param str string representation of flag (eg. 'on')
  481. * @return numeric value of flag (0 or 1)
  482. */
  483. int rspamd_config_parse_flag(const char *str, unsigned int len);
  484. enum rspamd_post_load_options {
  485. RSPAMD_CONFIG_INIT_URL = 1 << 0,
  486. RSPAMD_CONFIG_INIT_LIBS = 1 << 1,
  487. RSPAMD_CONFIG_INIT_SYMCACHE = 1 << 2,
  488. RSPAMD_CONFIG_INIT_VALIDATE = 1 << 3,
  489. RSPAMD_CONFIG_INIT_NO_TLD = 1 << 4,
  490. RSPAMD_CONFIG_INIT_PRELOAD_MAPS = 1 << 5,
  491. RSPAMD_CONFIG_INIT_POST_LOAD_LUA = 1 << 6,
  492. };
  493. #define RSPAMD_CONFIG_LOAD_ALL (RSPAMD_CONFIG_INIT_URL | \
  494. RSPAMD_CONFIG_INIT_LIBS | \
  495. RSPAMD_CONFIG_INIT_SYMCACHE | \
  496. RSPAMD_CONFIG_INIT_VALIDATE | \
  497. RSPAMD_CONFIG_INIT_PRELOAD_MAPS | \
  498. RSPAMD_CONFIG_INIT_POST_LOAD_LUA)
  499. /**
  500. * Do post load actions for config
  501. * @param cfg config file
  502. */
  503. gboolean rspamd_config_post_load(struct rspamd_config *cfg,
  504. enum rspamd_post_load_options opts);
  505. /*
  506. * Return a new classifier_config structure, setting default and non-conflicting attributes
  507. */
  508. struct rspamd_classifier_config *rspamd_config_new_classifier(
  509. struct rspamd_config *cfg,
  510. struct rspamd_classifier_config *c);
  511. /*
  512. * Return a new worker_conf structure, setting default and non-conflicting attributes
  513. */
  514. struct rspamd_worker_conf *rspamd_config_new_worker(struct rspamd_config *cfg,
  515. struct rspamd_worker_conf *c);
  516. /*
  517. * Return a new metric structure, setting default and non-conflicting attributes
  518. */
  519. void rspamd_config_init_metric(struct rspamd_config *cfg);
  520. /*
  521. * Return new symbols group definition
  522. */
  523. struct rspamd_symbols_group *rspamd_config_new_group(
  524. struct rspamd_config *cfg,
  525. const char *name);
  526. /*
  527. * Return a new statfile structure, setting default and non-conflicting attributes
  528. */
  529. struct rspamd_statfile_config *rspamd_config_new_statfile(
  530. struct rspamd_config *cfg,
  531. struct rspamd_statfile_config *c);
  532. /*
  533. * Register symbols of classifiers inside metrics
  534. */
  535. void rspamd_config_insert_classify_symbols(struct rspamd_config *cfg);
  536. /*
  537. * Check statfiles inside a classifier
  538. */
  539. gboolean rspamd_config_check_statfiles(struct rspamd_classifier_config *cf);
  540. /*
  541. * Find classifier config by name
  542. */
  543. struct rspamd_classifier_config *rspamd_config_find_classifier(
  544. struct rspamd_config *cfg,
  545. const char *name);
  546. void rspamd_ucl_add_conf_macros(struct ucl_parser *parser,
  547. struct rspamd_config *cfg);
  548. void rspamd_ucl_add_conf_variables(struct ucl_parser *parser, GHashTable *vars);
  549. /**
  550. * Initialize rspamd filtering system (lua and C filters)
  551. * @param cfg
  552. * @param reconfig
  553. * @return
  554. */
  555. gboolean rspamd_init_filters(struct rspamd_config *cfg, bool reconfig, bool strict);
  556. /**
  557. * Add new symbol to the metric
  558. * @param cfg
  559. * @param metric metric's name (or NULL for the default metric)
  560. * @param symbol symbol's name
  561. * @param score symbol's score
  562. * @param description optional description
  563. * @param group optional group name
  564. * @param one_shot TRUE if symbol can add its score once
  565. * @param rewrite_existing TRUE if we need to rewrite the existing symbol
  566. * @param priority use the following priority for a symbol
  567. * @param nshots means maximum number of hits for a symbol in metric (-1 for unlimited)
  568. * @return TRUE if symbol has been inserted or FALSE if symbol already exists with higher priority
  569. */
  570. gboolean rspamd_config_add_symbol(struct rspamd_config *cfg,
  571. const char *symbol,
  572. double score,
  573. const char *description,
  574. const char *group,
  575. unsigned int flags,
  576. unsigned int priority,
  577. int nshots);
  578. /**
  579. * Adds new group for a symbol
  580. * @param cfg
  581. * @param symbol
  582. * @param group
  583. * @return
  584. */
  585. gboolean rspamd_config_add_symbol_group(struct rspamd_config *cfg,
  586. const char *symbol,
  587. const char *group);
  588. /**
  589. * Sets action score for a specified metric with the specified priority
  590. * @param cfg config file
  591. * @param metric metric name (or NULL for default metric)
  592. * @param action_name symbolic name of action
  593. * @param obj data to set for action
  594. * @return TRUE if symbol has been inserted or FALSE if action already exists with higher priority
  595. */
  596. gboolean rspamd_config_set_action_score(struct rspamd_config *cfg,
  597. const char *action_name,
  598. const ucl_object_t *obj);
  599. /**
  600. * Check priority and maybe disable action completely
  601. * @param cfg
  602. * @param action_name
  603. * @param priority
  604. * @return
  605. */
  606. gboolean rspamd_config_maybe_disable_action(struct rspamd_config *cfg,
  607. const char *action_name,
  608. unsigned int priority);
  609. /**
  610. * Checks if a specified C or lua module is enabled or disabled in the config.
  611. * The logic of check is the following:
  612. *
  613. * - For C modules, we check `filters` line and enable module only if it is found there
  614. * - For LUA modules we check the corresponding configuration section:
  615. * - if section exists, then we check `enabled` key and check its value
  616. * - if section is absent, we consider module as disabled
  617. * - For both C and LUA modules we check if the group with the module name is disabled in the default metric
  618. * @param cfg config file
  619. * @param module_name module name
  620. * @return TRUE if a module is enabled
  621. */
  622. gboolean rspamd_config_is_module_enabled(struct rspamd_config *cfg,
  623. const char *module_name);
  624. /**
  625. * Verifies enabled/disabled combination in the specified object
  626. * @param obj
  627. * @return TRUE if there is no explicit disable in the object found
  628. */
  629. gboolean rspamd_config_is_enabled_from_ucl(rspamd_mempool_t *pool,
  630. const ucl_object_t *obj);
  631. /*
  632. * Get action from a string
  633. */
  634. gboolean rspamd_action_from_str(const char *data, enum rspamd_action_type *result);
  635. /*
  636. * Return textual representation of action enumeration
  637. */
  638. const char *rspamd_action_to_str(enum rspamd_action_type action);
  639. const char *rspamd_action_to_str_alt(enum rspamd_action_type action);
  640. /**
  641. * Parse radix tree or radix map from ucl object
  642. * @param cfg configuration object
  643. * @param obj ucl object with parameter
  644. * @param target target radix tree
  645. * @param err error pointer
  646. * @return
  647. */
  648. struct rspamd_radix_map_helper;
  649. gboolean rspamd_config_radix_from_ucl(struct rspamd_config *cfg, const ucl_object_t *obj, const char *description,
  650. struct rspamd_radix_map_helper **target, GError **err,
  651. struct rspamd_worker *worker, const char *map_name);
  652. /**
  653. * Adds new settings id to be preprocessed
  654. * @param cfg
  655. * @param name
  656. * @param symbols_enabled (ownership is transferred to callee)
  657. * @param symbols_disabled (ownership is transferred to callee)
  658. */
  659. void rspamd_config_register_settings_id(struct rspamd_config *cfg,
  660. const char *name,
  661. ucl_object_t *symbols_enabled,
  662. ucl_object_t *symbols_disabled,
  663. enum rspamd_config_settings_policy policy);
  664. /**
  665. * Convert settings name to settings id
  666. * @param name
  667. * @param namelen
  668. * @return
  669. */
  670. uint32_t rspamd_config_name_to_id(const char *name, gsize namelen);
  671. /**
  672. * Finds settings id element and obtain reference count (must be unrefed by caller)
  673. * @param cfg
  674. * @param id
  675. * @return
  676. */
  677. struct rspamd_config_settings_elt *rspamd_config_find_settings_id_ref(
  678. struct rspamd_config *cfg,
  679. uint32_t id);
  680. /**
  681. * Finds settings id element and obtain reference count (must be unrefed by callee)
  682. * @param cfg
  683. * @param id
  684. * @return
  685. */
  686. struct rspamd_config_settings_elt *rspamd_config_find_settings_name_ref(
  687. struct rspamd_config *cfg,
  688. const char *name, gsize namelen);
  689. /**
  690. * Returns action object by name
  691. * @param cfg
  692. * @param name
  693. * @return
  694. */
  695. struct rspamd_action *rspamd_config_get_action(struct rspamd_config *cfg,
  696. const char *name);
  697. struct rspamd_action *rspamd_config_get_action_by_type(struct rspamd_config *cfg,
  698. enum rspamd_action_type type);
  699. /**
  700. * Iterate over all actions
  701. * @param cfg
  702. * @param func
  703. * @param data
  704. */
  705. void rspamd_config_actions_foreach(struct rspamd_config *cfg,
  706. void (*func)(struct rspamd_action *act, void *d),
  707. void *data);
  708. /**
  709. * Iterate over all actions with index
  710. * @param cfg
  711. * @param func
  712. * @param data
  713. */
  714. void rspamd_config_actions_foreach_enumerate(struct rspamd_config *cfg,
  715. void (*func)(int idx, struct rspamd_action *act, void *d),
  716. void *data);
  717. /**
  718. * Returns number of actions defined in the config
  719. * @param cfg
  720. * @return
  721. */
  722. gsize rspamd_config_actions_size(struct rspamd_config *cfg);
  723. int rspamd_config_ev_backend_get(struct rspamd_config *cfg);
  724. const char *rspamd_config_ev_backend_to_string(int ev_backend, gboolean *effective);
  725. struct rspamd_external_libs_ctx;
  726. /**
  727. * Initialize rspamd libraries
  728. */
  729. struct rspamd_external_libs_ctx *rspamd_init_libs(void);
  730. /**
  731. * Reset and initialize decompressor
  732. * @param ctx
  733. */
  734. gboolean rspamd_libs_reset_decompression(struct rspamd_external_libs_ctx *ctx);
  735. /**
  736. * Reset and initialize compressor
  737. * @param ctx
  738. */
  739. gboolean rspamd_libs_reset_compression(struct rspamd_external_libs_ctx *ctx);
  740. /**
  741. * Destroy external libraries context
  742. */
  743. void rspamd_deinit_libs(struct rspamd_external_libs_ctx *ctx);
  744. /**
  745. * Returns TRUE if an address belongs to some local address
  746. */
  747. gboolean rspamd_ip_is_local_cfg(struct rspamd_config *cfg,
  748. const rspamd_inet_addr_t *addr);
  749. /**
  750. * Configure libraries
  751. */
  752. gboolean rspamd_config_libs(struct rspamd_external_libs_ctx *ctx,
  753. struct rspamd_config *cfg);
  754. #define msg_err_config(...) rspamd_default_log_function(G_LOG_LEVEL_CRITICAL, \
  755. cfg->cfg_pool->tag.tagname, cfg->checksum, \
  756. RSPAMD_LOG_FUNC, \
  757. __VA_ARGS__)
  758. #define msg_err_config_forced(...) rspamd_default_log_function((int) G_LOG_LEVEL_CRITICAL | (int) RSPAMD_LOG_FORCED, \
  759. cfg->cfg_pool->tag.tagname, cfg->checksum, \
  760. RSPAMD_LOG_FUNC, \
  761. __VA_ARGS__)
  762. #define msg_warn_config(...) rspamd_default_log_function(G_LOG_LEVEL_WARNING, \
  763. cfg->cfg_pool->tag.tagname, cfg->checksum, \
  764. RSPAMD_LOG_FUNC, \
  765. __VA_ARGS__)
  766. #define msg_info_config(...) rspamd_default_log_function(G_LOG_LEVEL_INFO, \
  767. cfg->cfg_pool->tag.tagname, cfg->checksum, \
  768. RSPAMD_LOG_FUNC, \
  769. __VA_ARGS__)
  770. extern unsigned int rspamd_config_log_id;
  771. #define msg_debug_config(...) rspamd_conditional_debug_fast(NULL, NULL, \
  772. rspamd_config_log_id, "config", cfg->checksum, \
  773. RSPAMD_LOG_FUNC, \
  774. __VA_ARGS__)
  775. #ifdef __cplusplus
  776. }
  777. #endif
  778. #endif /* ifdef CFG_FILE_H */