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

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