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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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 "filter.h"
  12. #define DEFAULT_BIND_PORT 768
  13. #define DEFAULT_CONTROL_PORT 7608
  14. #define DEFAULT_LMTP_PORT 7609
  15. #define MAX_MEMCACHED_SERVERS 48
  16. #define DEFAULT_MEMCACHED_PORT 11211
  17. /* Memcached timeouts */
  18. #define DEFAULT_MEMCACHED_CONNECT_TIMEOUT 1000
  19. /* Upstream timeouts */
  20. #define DEFAULT_UPSTREAM_ERROR_TIME 10
  21. #define DEFAULT_UPSTREAM_ERROR_TIME 10
  22. #define DEFAULT_UPSTREAM_DEAD_TIME 300
  23. #define DEFAULT_UPSTREAM_MAXERRORS 10
  24. /* Statfile pool size, 50Mb */
  25. #define DEFAULT_STATFILE_SIZE 52428800L
  26. /* 1 worker by default */
  27. #define DEFAULT_WORKERS_NUM 1
  28. #define yyerror parse_err
  29. #define yywarn parse_warn
  30. struct expression;
  31. struct tokenizer;
  32. struct classifier;
  33. enum { VAL_UNDEF=0, VAL_TRUE, VAL_FALSE };
  34. /**
  35. * Types of rspamd bind lines
  36. */
  37. enum rspamd_cred_type {
  38. CRED_NORMAL,
  39. CRED_CONTROL,
  40. CRED_LMTP,
  41. CRED_DELIVERY,
  42. };
  43. /**
  44. * Regexp type: /H - header, /M - mime, /U - url
  45. */
  46. enum rspamd_regexp_type {
  47. REGEXP_NONE = 0,
  48. REGEXP_HEADER,
  49. REGEXP_MIME,
  50. REGEXP_MESSAGE,
  51. REGEXP_URL,
  52. };
  53. /**
  54. * Logging type
  55. */
  56. enum rspamd_log_type {
  57. RSPAMD_LOG_CONSOLE,
  58. RSPAMD_LOG_SYSLOG,
  59. RSPAMD_LOG_FILE,
  60. };
  61. /**
  62. * Regexp structure
  63. */
  64. struct rspamd_regexp {
  65. enum rspamd_regexp_type type; /**< regexp type */
  66. char *regexp_text; /**< regexp text representation */
  67. GRegex *regexp; /**< glib regexp structure */
  68. char *header; /**< header name for header regexps */
  69. };
  70. /**
  71. * Memcached server object
  72. */
  73. struct memcached_server {
  74. struct upstream up; /**< common upstream base */
  75. struct in_addr addr; /**< address of server */
  76. uint16_t port; /**< port to connect */
  77. short alive; /**< is this server alive */
  78. short int num; /**< number of servers in case of mirror */
  79. };
  80. /**
  81. * Perl module list item
  82. */
  83. struct perl_module {
  84. char *path; /**< path to module */
  85. LIST_ENTRY (perl_module) next; /**< chain link */
  86. };
  87. /**
  88. * Module option
  89. */
  90. struct module_opt {
  91. char *param; /**< parameter name */
  92. char *value; /**< paramater value */
  93. LIST_ENTRY (module_opt) next;
  94. };
  95. /**
  96. * Statfile config definition
  97. */
  98. struct statfile {
  99. char *alias; /**< alias of statfile */
  100. char *pattern; /**< filesystem pattern (with %r or %f) */
  101. double weight; /**< weight scale */
  102. char *metric; /**< metric name */
  103. size_t size; /**< size of statfile */
  104. struct tokenizer *tokenizer; /**< tokenizer used for statfile */
  105. };
  106. /**
  107. * Config option for importing to perl module
  108. */
  109. struct config_scalar {
  110. void *pointer; /**< pointer to data */
  111. enum {
  112. SCALAR_TYPE_INT,
  113. SCALAR_TYPE_UINT,
  114. SCALAR_TYPE_STR,
  115. SCALAR_TYPE_SIZE,
  116. } type; /**< type of data */
  117. };
  118. /**
  119. * Structure that stores all config data
  120. */
  121. struct config_file {
  122. memory_pool_t *cfg_pool; /**< memory pool for config */
  123. char *cfg_name; /**< name of config file */
  124. char *pid_file; /**< name of pid file */
  125. char *temp_dir; /**< dir for temp files */
  126. char *bind_host; /**< bind line */
  127. struct in_addr bind_addr; /**< bind address in case of TCP socket */
  128. uint16_t bind_port; /**< bind port in case of TCP socket */
  129. uint16_t bind_family; /**< bind type (AF_UNIX or AF_INET) */
  130. char *control_host; /**< bind line for controller */
  131. struct in_addr control_addr; /**< bind address for controller */
  132. uint16_t control_port; /**< bind port for controller */
  133. uint16_t control_family; /**< bind family for controller */
  134. int controller_enabled; /**< whether controller is enabled */
  135. char *control_password; /**< controller password */
  136. int no_fork; /**< if 1 do not call daemon() */
  137. unsigned int workers_number; /**< number of workers */
  138. enum rspamd_log_type log_type; /**< log type */
  139. int log_facility; /**< log facility in case of syslog */
  140. int log_level; /**< log level trigger */
  141. char *log_file; /**< path to logfile in case of file logging */
  142. int log_fd; /**< log descriptor in case of file logging */
  143. size_t max_statfile_size; /**< maximum size for statfile */
  144. struct memcached_server memcached_servers[MAX_MEMCACHED_SERVERS]; /**< memcached servers */
  145. size_t memcached_servers_num; /**< number of memcached servers */
  146. memc_proto_t memcached_protocol; /**< memcached protocol */
  147. unsigned int memcached_error_time; /**< memcached error time (see upstream documentation) */
  148. unsigned int memcached_dead_time; /**< memcached dead time */
  149. unsigned int memcached_maxerrors; /**< maximum number of errors */
  150. unsigned int memcached_connect_timeout; /**< connection timeout */
  151. gboolean lmtp_enable; /**< is lmtp agent is enabled */
  152. char *lmtp_host; /**< host for lmtp agent */
  153. struct in_addr lmtp_addr; /**< bind address for lmtp */
  154. uint16_t lmtp_port; /**< bind port for lmtp agent */
  155. uint16_t lmtp_family; /**< bind family for lmtp agent */
  156. char *lmtp_metric; /**< metric to use in lmtp module */
  157. gboolean delivery_enable; /**< is delivery agent is enabled */
  158. char *deliver_host; /**< host for mail deliviring */
  159. struct in_addr deliver_addr; /**< its address */
  160. uint16_t deliver_port; /**< port for deliviring */
  161. uint16_t deliver_family; /**< socket family for delivirnig */
  162. char *deliver_agent_path; /**< deliver to pipe instead of socket */
  163. gboolean deliver_lmtp; /**< use LMTP instead of SMTP */
  164. LIST_HEAD (modulesq, perl_module) perl_modules; /**< linked list of perl modules to load */
  165. LIST_HEAD (headersq, filter) header_filters; /**< linked list of all header's filters */
  166. LIST_HEAD (mimesq, filter) mime_filters; /**< linked list of all mime filters */
  167. LIST_HEAD (messagesq, filter) message_filters; /**< linked list of all message's filters */
  168. LIST_HEAD (urlsq, filter) url_filters; /**< linked list of all url's filters */
  169. char *header_filters_str; /**< string of header's filters */
  170. char *mime_filters_str; /**< string of mime's filters */
  171. char *message_filters_str; /**< string of message's filters */
  172. char *url_filters_str; /**< string for url's filters */
  173. GHashTable* modules_opts; /**< hash for module options indexed by module name */
  174. GHashTable* variables; /**< hash of $variables defined in config, indexed by variable name */
  175. GHashTable* metrics; /**< hash of metrics indexed by metric name */
  176. GHashTable* factors; /**< hash of factors indexed by symbol name */
  177. GHashTable* c_modules; /**< hash of c modules indexed by module name */
  178. GHashTable* composite_symbols; /**< hash of composite symbols indexed by its name */
  179. GHashTable* statfiles; /**< hash of defined statfiles indexed by alias */
  180. GHashTable* cfg_params; /**< all cfg params indexed by its name in this structure */
  181. };
  182. /**
  183. * Add memcached server to config
  184. * @param cf config file to use
  185. * @param str line that describes server's credits
  186. * @return 1 if line was successfully parsed and 0 in case of error
  187. */
  188. int add_memcached_server (struct config_file *cf, char *str);
  189. /**
  190. * Parse bind credits
  191. * @param cf config file to use
  192. * @param str line that presents bind line
  193. * @param type type of credits
  194. * @return 1 if line was successfully parsed and 0 in case of error
  195. */
  196. int parse_bind_line (struct config_file *cf, char *str, enum rspamd_cred_type type);
  197. /**
  198. * Init default values
  199. * @param cfg config file
  200. */
  201. void init_defaults (struct config_file *cfg);
  202. /**
  203. * Free memory used by config structure
  204. * @param cfg config file
  205. */
  206. void free_config (struct config_file *cfg);
  207. /**
  208. * Gets module option with specified name
  209. * @param cfg config file
  210. * @param module_name name of module
  211. * @param opt_name name of option to get
  212. * @return module value or NULL if option does not defined
  213. */
  214. char* get_module_opt (struct config_file *cfg, char *module_name, char *opt_name);
  215. /**
  216. * Parse limit
  217. * @param limit string representation of limit (eg. 1M)
  218. * @return numeric value of limit
  219. */
  220. size_t parse_limit (const char *limit);
  221. /**
  222. * Parse seconds
  223. * @param t string representation of seconds (eg. 1D)
  224. * @return numeric value of string
  225. */
  226. unsigned int parse_seconds (const char *t);
  227. /**
  228. * Parse flag
  229. * @param str string representation of flag (eg. 'on')
  230. * @return numeric value of flag (0 or 1)
  231. */
  232. char parse_flag (const char *str);
  233. /**
  234. * Substitutes variable in specified string, may be recursive (eg. ${var1${var2}})
  235. * @param cfg config file
  236. * @param str incoming string
  237. * @param recursive whether do recursive scanning
  238. * @return new string with substituted variables (uses cfg memory pool for allocating)
  239. */
  240. char* substitute_variable (struct config_file *cfg, char *str, u_char recursive);
  241. /**
  242. * Do post load actions for config
  243. * @param cfg config file
  244. */
  245. void post_load_config (struct config_file *cfg);
  246. /**
  247. * Parse regexp line to regexp structure
  248. * @param pool memory pool to use
  249. * @param line incoming line
  250. * @return regexp structure or NULL in case of error
  251. */
  252. struct rspamd_regexp* parse_regexp (memory_pool_t *pool, char *line);
  253. /**
  254. * Parse composites line to composites structure (eg. "SYMBOL1&SYMBOL2|!SYMBOL3")
  255. * @param pool memory pool to use
  256. * @param line incoming line
  257. * @return expression structure or NULL in case of error
  258. */
  259. struct expression* parse_expression (memory_pool_t *pool, char *line);
  260. int yylex (void);
  261. int yyparse (void);
  262. void yyrestart (FILE *);
  263. void parse_err (const char *fmt, ...);
  264. void parse_warn (const char *fmt, ...);
  265. #endif /* ifdef CFG_FILE_H */
  266. /*
  267. * vi:ts=4
  268. */