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

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