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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516
  1. /**
  2. * @file cfg_file.h
  3. * Config file parser and config routines API
  4. */
  5. #ifndef CFG_FILE_H
  6. #define CFG_FILE_H
  7. #include "config.h"
  8. #include "mem_pool.h"
  9. #include "upstream.h"
  10. #include "memcached.h"
  11. #include "symbols_cache.h"
  12. #include "cfg_rcl.h"
  13. #include "utlist.h"
  14. #include "ucl.h"
  15. #define DEFAULT_BIND_PORT 11333
  16. #define DEFAULT_CONTROL_PORT 11334
  17. #define MAX_MEMCACHED_SERVERS 4
  18. #define DEFAULT_MEMCACHED_PORT 11211
  19. /* Memcached timeouts */
  20. #define DEFAULT_MEMCACHED_CONNECT_TIMEOUT 1000
  21. /* Upstream timeouts */
  22. #define DEFAULT_UPSTREAM_ERROR_TIME 10
  23. #define DEFAULT_UPSTREAM_ERROR_TIME 10
  24. #define DEFAULT_UPSTREAM_DEAD_TIME 300
  25. #define DEFAULT_UPSTREAM_MAXERRORS 10
  26. struct expression;
  27. struct tokenizer;
  28. struct classifier;
  29. enum { VAL_UNDEF=0, VAL_TRUE, VAL_FALSE };
  30. /**
  31. * Type of time configuration parameter
  32. */
  33. enum time_type {
  34. TIME_SECONDS = 0,
  35. TIME_MILLISECONDS,
  36. TIME_MINUTES,
  37. TIME_HOURS
  38. };
  39. /**
  40. * Types of rspamd bind lines
  41. */
  42. enum rspamd_cred_type {
  43. CRED_NORMAL,
  44. CRED_CONTROL,
  45. CRED_LMTP,
  46. CRED_DELIVERY
  47. };
  48. /**
  49. * Regexp type: /H - header, /M - mime, /U - url /X - raw header
  50. */
  51. enum rspamd_regexp_type {
  52. REGEXP_NONE = 0,
  53. REGEXP_HEADER,
  54. REGEXP_MIME,
  55. REGEXP_MESSAGE,
  56. REGEXP_URL,
  57. REGEXP_RAW_HEADER
  58. };
  59. /**
  60. * Logging type
  61. */
  62. enum rspamd_log_type {
  63. RSPAMD_LOG_CONSOLE,
  64. RSPAMD_LOG_SYSLOG,
  65. RSPAMD_LOG_FILE
  66. };
  67. /**
  68. * Regexp structure
  69. */
  70. struct rspamd_regexp {
  71. enum rspamd_regexp_type type; /**< regexp type */
  72. gchar *regexp_text; /**< regexp text representation */
  73. GRegex *regexp; /**< glib regexp structure */
  74. GRegex *raw_regexp; /**< glib regexp structure for raw matching */
  75. gchar *header; /**< header name for header regexps */
  76. gboolean is_test; /**< true if this expression must be tested */
  77. gboolean is_raw; /**< true if this regexp is done by raw matching */
  78. gboolean is_strong; /**< true if headers search must be case sensitive */
  79. };
  80. /**
  81. * Memcached server object
  82. */
  83. struct memcached_server {
  84. struct upstream up; /**< common upstream base */
  85. struct in_addr addr; /**< address of server */
  86. guint16 port; /**< port to connect */
  87. short alive; /**< is this server alive */
  88. gint16 num; /**< number of servers in case of mirror */
  89. };
  90. /**
  91. * script module list item
  92. */
  93. struct script_module {
  94. gchar *name; /**< name of module */
  95. gchar *path; /**< path to module */
  96. };
  97. /**
  98. * Type of lua variable
  99. */
  100. enum lua_var_type {
  101. LUA_VAR_NUM,
  102. LUA_VAR_BOOLEAN,
  103. LUA_VAR_STRING,
  104. LUA_VAR_FUNCTION,
  105. LUA_VAR_UNKNOWN
  106. };
  107. /**
  108. * Module option
  109. */
  110. struct module_opt {
  111. gchar *param; /**< parameter name */
  112. gchar *value; /**< parameter value */
  113. gchar *description; /**< parameter description */
  114. gchar *group; /**< parameter group */
  115. gpointer actual_data; /**< parsed data */
  116. gboolean is_lua; /**< actually this is lua variable */
  117. enum lua_var_type lua_type; /**< type of lua variable */
  118. };
  119. struct module_meta_opt {
  120. gchar *name; /**< Name of meta option */
  121. GList *options; /**< List of struct module_opt */
  122. };
  123. /**
  124. * Symbol definition
  125. */
  126. struct symbol_def {
  127. gchar *name;
  128. gchar *description;
  129. gdouble *weight_ptr;
  130. };
  131. /**
  132. * Symbols group
  133. */
  134. struct symbols_group {
  135. gchar *name;
  136. GList *symbols;
  137. };
  138. /**
  139. * Statfile section definition
  140. */
  141. struct statfile_section {
  142. guint32 code; /**< section's code */
  143. guint64 size; /**< size of section */
  144. double weight; /**< weight coefficient for section */
  145. };
  146. /**
  147. * Statfile autolearn parameters
  148. */
  149. struct statfile_autolearn_params {
  150. const gchar *metric; /**< metric name for autolearn triggering */
  151. double threshold_min; /**< threshold mark */
  152. double threshold_max; /**< threshold mark */
  153. GList *symbols; /**< list of symbols */
  154. };
  155. /**
  156. * Sync affinity
  157. */
  158. enum sync_affinity {
  159. AFFINITY_NONE = 0,
  160. AFFINITY_MASTER,
  161. AFFINITY_SLAVE
  162. };
  163. /**
  164. * Binlog params
  165. */
  166. struct statfile_binlog_params {
  167. enum sync_affinity affinity;
  168. time_t rotate_time;
  169. gchar *master_addr;
  170. guint16 master_port;
  171. };
  172. typedef double (*statfile_normalize_func)(struct config_file *cfg, long double score, void *params);
  173. /**
  174. * Statfile config definition
  175. */
  176. struct statfile {
  177. gchar *symbol; /**< symbol of statfile */
  178. gchar *path; /**< filesystem pattern (with %r or %f) */
  179. gchar *label; /**< label of this statfile */
  180. gsize size; /**< size of statfile */
  181. GList *sections; /**< list of sections in statfile */
  182. struct statfile_autolearn_params *autolearn; /**< autolearn params */
  183. struct statfile_binlog_params *binlog; /**< binlog params */
  184. statfile_normalize_func normalizer; /**< function that is used as normaliser */
  185. void *normalizer_data; /**< normalizer function params */
  186. gchar *normalizer_str; /**< source string (for dump) */
  187. ucl_object_t *opts; /**< other options */
  188. gboolean is_spam; /**< spam flag */
  189. };
  190. /**
  191. * Classifier config definition
  192. */
  193. struct classifier_config {
  194. GList *statfiles; /**< statfiles list */
  195. GHashTable *labels; /**< statfiles with labels */
  196. gchar *metric; /**< metric of this classifier */
  197. struct classifier *classifier; /**< classifier interface */
  198. struct tokenizer *tokenizer; /**< tokenizer used for classifier */
  199. GHashTable *opts; /**< other options */
  200. GList *pre_callbacks; /**< list of callbacks that are called before classification */
  201. GList *post_callbacks; /**< list of callbacks that are called after classification */
  202. };
  203. struct rspamd_worker_bind_conf {
  204. gchar *bind_host;
  205. guint16 bind_port;
  206. gint ai;
  207. struct rspamd_worker_bind_conf *next;
  208. };
  209. struct rspamd_worker_param_parser {
  210. rspamd_rcl_handler_t handler; /**< handler function */
  211. struct rspamd_rcl_struct_parser parser; /**< parser attributes */
  212. const gchar *name; /**< parameter's name */
  213. UT_hash_handle hh; /**< hash by name */
  214. };
  215. struct rspamd_worker_cfg_parser {
  216. struct rspamd_worker_param_parser *parsers; /**< parsers hash */
  217. gint type; /**< workers quark */
  218. gboolean (*def_obj_parser)(ucl_object_t *obj, gpointer ud); /**< default object parser */
  219. gpointer def_ud;
  220. UT_hash_handle hh; /**< hash by type */
  221. };
  222. /**
  223. * Config params for rspamd worker
  224. */
  225. struct worker_conf {
  226. worker_t *worker; /**< pointer to worker type */
  227. GQuark type; /**< type of worker */
  228. struct rspamd_worker_bind_conf *bind_conf; /**< bind configuration */
  229. guint16 count; /**< number of workers */
  230. GList *listen_socks; /**< listening sockets desctiptors */
  231. guint32 rlimit_nofile; /**< max files limit */
  232. guint32 rlimit_maxcore; /**< maximum core file size */
  233. GHashTable *params; /**< params for worker */
  234. GQueue *active_workers; /**< linked list of spawned workers */
  235. gboolean has_socket; /**< whether we should make listening socket in main process */
  236. gpointer *ctx; /**< worker's context */
  237. ucl_object_t *options; /**< other worker's options */
  238. };
  239. /**
  240. * Structure that stores all config data
  241. */
  242. struct config_file {
  243. gchar *rspamd_user; /**< user to run as */
  244. gchar *rspamd_group; /**< group to run as */
  245. memory_pool_t *cfg_pool; /**< memory pool for config */
  246. gchar *cfg_name; /**< name of config file */
  247. gchar *pid_file; /**< name of pid file */
  248. gchar *temp_dir; /**< dir for temp files */
  249. #ifdef WITH_GPERF_TOOLS
  250. gchar *profile_path;
  251. #endif
  252. gboolean no_fork; /**< if 1 do not call daemon() */
  253. gboolean config_test; /**< if TRUE do only config file test */
  254. gboolean raw_mode; /**< work in raw mode instead of utf one */
  255. gboolean one_shot_mode; /**< rules add only one symbol */
  256. gboolean check_text_attachements; /**< check text attachements as text */
  257. gboolean convert_config; /**< convert config to XML format */
  258. gboolean strict_protocol_headers; /**< strictly check protocol headers */
  259. gsize max_diff; /**< maximum diff size for text parts */
  260. enum rspamd_log_type log_type; /**< log type */
  261. gint log_facility; /**< log facility in case of syslog */
  262. gint log_level; /**< log level trigger */
  263. gchar *log_file; /**< path to logfile in case of file logging */
  264. gboolean log_buffered; /**< whether logging is buffered */
  265. guint32 log_buf_size; /**< length of log buffer */
  266. gchar *debug_ip_map; /**< turn on debugging for specified ip addresses */
  267. gboolean log_urls; /**< whether we should log URLs */
  268. GList *debug_symbols; /**< symbols to debug */
  269. gboolean log_color; /**< output colors for console output */
  270. gboolean log_extended; /**< log extended information */
  271. guint32 statfile_sync_interval; /**< synchronization interval */
  272. guint32 statfile_sync_timeout; /**< synchronization timeout */
  273. gboolean mlock_statfile_pool; /**< use mlock (2) for locking statfiles */
  274. struct memcached_server memcached_servers[MAX_MEMCACHED_SERVERS]; /**< memcached servers */
  275. gsize memcached_servers_num; /**< number of memcached servers */
  276. memc_proto_t memcached_protocol; /**< memcached protocol */
  277. guint memcached_error_time; /**< memcached error time (see upstream documentation) */
  278. guint memcached_dead_time; /**< memcached dead time */
  279. guint memcached_maxerrors; /**< maximum number of errors */
  280. guint memcached_connect_timeout; /**< connection timeout */
  281. gboolean delivery_enable; /**< is delivery agent is enabled */
  282. gchar *deliver_host; /**< host for mail deliviring */
  283. struct in_addr deliver_addr; /**< its address */
  284. guint16 deliver_port; /**< port for deliviring */
  285. guint16 deliver_family; /**< socket family for delivirnig */
  286. gchar *deliver_agent_path; /**< deliver to pipe instead of socket */
  287. gboolean deliver_lmtp; /**< use LMTP instead of SMTP */
  288. GList *script_modules; /**< linked list of script modules to load */
  289. GList *filters; /**< linked list of all filters */
  290. GList *workers; /**< linked list of all workers params */
  291. struct rspamd_worker_cfg_parser *wrk_parsers; /**< hash for worker config parsers, indexed by worker quarks */
  292. gchar *filters_str; /**< string of filters */
  293. ucl_object_t *rcl_obj; /**< rcl object */
  294. GHashTable* metrics; /**< hash of metrics indexed by metric name */
  295. GList* symbols_groups; /**< groups of symbols */
  296. GList* metrics_list; /**< linked list of metrics */
  297. GHashTable* metrics_symbols; /**< hash table of metrics indexed by symbol */
  298. GHashTable* c_modules; /**< hash of c modules indexed by module name */
  299. GHashTable* composite_symbols; /**< hash of composite symbols indexed by its name */
  300. GList *classifiers; /**< list of all classifiers defined */
  301. GList *statfiles; /**< list of all statfiles in config file order */
  302. GHashTable *classifiers_symbols; /**< hashtable indexed by symbol name of classifiers */
  303. GHashTable* cfg_params; /**< all cfg params indexed by its name in this structure */
  304. GList *views; /**< views */
  305. GList *pre_filters; /**< list of pre-processing lua filters */
  306. GList *post_filters; /**< list of post-processing lua filters */
  307. gchar *dynamic_conf; /**< path to dynamic configuration */
  308. GList *current_dynamic_conf; /**< currently loaded dynamic configuration */
  309. GHashTable* domain_settings; /**< settings per-domains */
  310. GHashTable* user_settings; /**< settings per-user */
  311. gchar* domain_settings_str; /**< string representation of settings */
  312. gchar* user_settings_str;
  313. gint clock_res; /**< resolution of clock used */
  314. GList *maps; /**< maps active */
  315. memory_pool_t *map_pool; /**< static maps pool */
  316. gdouble map_timeout; /**< maps watch timeout */
  317. struct symbols_cache *cache; /**< symbols cache object */
  318. gchar *cache_filename; /**< filename of cache file */
  319. struct metric *default_metric; /**< default metric */
  320. gchar* checksum; /**< real checksum of config file */
  321. gchar* dump_checksum; /**< dump checksum of config file */
  322. gpointer lua_state; /**< pointer to lua state */
  323. gchar* rrd_file; /**< rrd file to store statistics */
  324. gchar* history_file; /**< file to save rolling history */
  325. guint32 dns_timeout; /**< timeout in milliseconds for waiting for dns reply */
  326. guint32 dns_retransmits; /**< maximum retransmits count */
  327. guint32 dns_throttling_errors; /**< maximum errors for starting resolver throttling */
  328. guint32 dns_throttling_time; /**< time in seconds for DNS throttling */
  329. guint32 dns_io_per_server; /**< number of sockets per DNS server */
  330. GList *nameservers; /**< list of nameservers or NULL to parse resolv.conf */
  331. };
  332. /**
  333. * Parse host[:port[:priority]] line
  334. * @param ina host address
  335. * @param port port
  336. * @param priority priority
  337. * @return TRUE if string was parsed
  338. */
  339. gboolean parse_host_port_priority (memory_pool_t *pool, const gchar *str, gchar **addr, guint16 *port, guint *priority);
  340. /**
  341. * Parse host:port line
  342. * @param ina host address
  343. * @param port port
  344. * @return TRUE if string was parsed
  345. */
  346. gboolean parse_host_port (memory_pool_t *pool, const gchar *str, gchar **addr, guint16 *port);
  347. /**
  348. * Parse host:priority line
  349. * @param ina host address
  350. * @param priority priority
  351. * @return TRUE if string was parsed
  352. */
  353. gboolean parse_host_priority (memory_pool_t *pool, const gchar *str, gchar **addr, guint *priority);
  354. /**
  355. * Parse bind credits
  356. * @param cf config file to use
  357. * @param str line that presents bind line
  358. * @param type type of credits
  359. * @return 1 if line was successfully parsed and 0 in case of error
  360. */
  361. gboolean parse_bind_line (struct config_file *cfg, struct worker_conf *cf, const gchar *str);
  362. /**
  363. * Init default values
  364. * @param cfg config file
  365. */
  366. void init_defaults (struct config_file *cfg);
  367. /**
  368. * Free memory used by config structure
  369. * @param cfg config file
  370. */
  371. void free_config (struct config_file *cfg);
  372. /**
  373. * Gets module option with specified name
  374. * @param cfg config file
  375. * @param module_name name of module
  376. * @param opt_name name of option to get
  377. * @return module value or NULL if option does not defined
  378. */
  379. ucl_object_t* get_module_opt (struct config_file *cfg, const gchar *module_name,
  380. const gchar *opt_name);
  381. /**
  382. * Parse limit
  383. * @param limit string representation of limit (eg. 1M)
  384. * @return numeric value of limit
  385. */
  386. guint64 parse_limit (const gchar *limit, guint len);
  387. /**
  388. * Parse flag
  389. * @param str string representation of flag (eg. 'on')
  390. * @return numeric value of flag (0 or 1)
  391. */
  392. gchar parse_flag (const gchar *str);
  393. /**
  394. * Do post load actions for config
  395. * @param cfg config file
  396. */
  397. void post_load_config (struct config_file *cfg);
  398. /**
  399. * Calculate checksum for config file
  400. * @param cfg config file
  401. */
  402. gboolean get_config_checksum (struct config_file *cfg);
  403. /**
  404. * Replace all \" with a single " in given string
  405. * @param line input string
  406. */
  407. void unescape_quotes (gchar *line);
  408. /*
  409. * Convert comma separated string to a list of strings
  410. */
  411. GList* parse_comma_list (memory_pool_t *pool, const gchar *line);
  412. /*
  413. * Return a new classifier_config structure, setting default and non-conflicting attributes
  414. */
  415. struct classifier_config* check_classifier_conf (struct config_file *cfg, struct classifier_config *c);
  416. /*
  417. * Return a new worker_conf structure, setting default and non-conflicting attributes
  418. */
  419. struct worker_conf* check_worker_conf (struct config_file *cfg, struct worker_conf *c);
  420. /*
  421. * Return a new metric structure, setting default and non-conflicting attributes
  422. */
  423. struct metric* check_metric_conf (struct config_file *cfg, struct metric *c);
  424. /*
  425. * Return a new statfile structure, setting default and non-conflicting attributes
  426. */
  427. struct statfile* check_statfile_conf (struct config_file *cfg, struct statfile *c);
  428. /*
  429. * XXX: Depreciated function, now it is used for
  430. */
  431. gboolean parse_normalizer (struct config_file *cfg, struct statfile *st, const gchar *line);
  432. /*
  433. * Read XML configuration file
  434. */
  435. gboolean read_rspamd_config (struct config_file *cfg,
  436. const gchar *filename, const gchar *convert_to,
  437. rspamd_rcl_section_fin_t logger_fin, gpointer logger_ud);
  438. /*
  439. * Register symbols of classifiers inside metrics
  440. */
  441. void insert_classifier_symbols (struct config_file *cfg);
  442. /*
  443. * Check statfiles inside a classifier
  444. */
  445. gboolean check_classifier_statfiles (struct classifier_config *cf);
  446. /*
  447. * Find classifier config by name
  448. */
  449. struct classifier_config* find_classifier_conf (struct config_file *cfg, const gchar *name);
  450. #endif /* ifdef CFG_FILE_H */
  451. /*
  452. * vi:ts=4
  453. */