aboutsummaryrefslogtreecommitdiffstats
path: root/src/cfg_file.h
blob: fbf7309d3fb4a1f00ff074bd5fd49134e5ab5051 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/**
 * @file cfg_file.h
 * Config file parser and config routines API
 */

#ifndef CFG_FILE_H
#define CFG_FILE_H

#include "config.h"
#include "mem_pool.h"
#include "upstream.h"
#include "memcached.h"
#include "filter.h"

#define DEFAULT_BIND_PORT 768
#define DEFAULT_CONTROL_PORT 7608
#define DEFAULT_LMTP_PORT 7609
#define MAX_MEMCACHED_SERVERS 48
#define DEFAULT_MEMCACHED_PORT 11211
/* Memcached timeouts */
#define DEFAULT_MEMCACHED_CONNECT_TIMEOUT 1000
/* Upstream timeouts */
#define DEFAULT_UPSTREAM_ERROR_TIME 10
#define DEFAULT_UPSTREAM_ERROR_TIME 10
#define DEFAULT_UPSTREAM_DEAD_TIME 300
#define DEFAULT_UPSTREAM_MAXERRORS 10
/* Statfile pool size, 50Mb */
#define DEFAULT_STATFILE_SIZE 52428800L

/* 1 worker by default */
#define DEFAULT_WORKERS_NUM 1

#define yyerror parse_err
#define yywarn parse_warn

struct expression;
struct tokenizer;
struct classifier;

enum { VAL_UNDEF=0, VAL_TRUE, VAL_FALSE };

/**
 * Types of rspamd bind lines
 */
enum rspamd_cred_type {
	CRED_NORMAL,
	CRED_CONTROL,
	CRED_LMTP,
	CRED_DELIVERY,
};

/**
 * Regexp type: /H - header, /M - mime, /U - url /X - raw header
 */
enum rspamd_regexp_type {
	REGEXP_NONE = 0,
	REGEXP_HEADER,
	REGEXP_MIME,
	REGEXP_MESSAGE,
	REGEXP_URL,
	REGEXP_RAW_HEADER,
};

/**
 * Logging type
 */
enum rspamd_log_type {
	RSPAMD_LOG_CONSOLE,
	RSPAMD_LOG_SYSLOG,
	RSPAMD_LOG_FILE,
};

/**
 * Regexp structure
 */
struct rspamd_regexp {
	enum rspamd_regexp_type type;					/**< regexp type										*/
	char *regexp_text;								/**< regexp text representation							*/
	GRegex *regexp;									/**< glib regexp structure								*/
	char *header;									/**< header name for header regexps						*/
};

/**
 * Memcached server object
 */
struct memcached_server {
	struct upstream up;								/**< common upstream base								*/
	struct in_addr addr;							/**< address of server									*/
	uint16_t port;									/**< port to connect									*/
	short alive;									/**< is this server alive								*/
	short int num;									/**< number of servers in case of mirror				*/
};

/**
 * Perl module list item
 */
struct perl_module {
	char *path;										/**< path to module										*/
	LIST_ENTRY (perl_module) next;					/**< chain link											*/
};

/**
 * Module option
 */
struct module_opt {
	char *param;									/**< parameter name										*/
	char *value;									/**< paramater value									*/
	LIST_ENTRY (module_opt) next;					
};

/**
 * Statfile section definition
 */
struct statfile_section {
	uint32_t code;									/**< section's code										*/
	uint64_t size;									/**< size of section									*/
	double weight;									/**< weight coefficient for section						*/
};

/**
 * Statfile config definition
 */
struct statfile {
	char *alias;									/**< alias of statfile									*/
	char *pattern;									/**< filesystem pattern (with %r or %f)					*/
	double weight;									/**< weight scale										*/
	char *metric;									/**< metric name										*/
	size_t size;									/**< size of statfile									*/
	struct tokenizer *tokenizer;					/**< tokenizer used for statfile						*/
	GList *sections;								/**< list of sections in statfile						*/
};

/**
 * Config option for importing to perl module
 */
struct config_scalar {
    void *pointer;									/**< pointer to data									*/
    enum {
        SCALAR_TYPE_INT,
        SCALAR_TYPE_UINT,
        SCALAR_TYPE_STR,
        SCALAR_TYPE_SIZE,
    } type;											/**< type of data										*/
};

/**
 * Structure that stores all config data
 */
struct config_file {
	char *rspamd_user;								/**< user to run as										*/
	char *rspamd_group;								/**< group to run as									*/
	memory_pool_t *cfg_pool;						/**< memory pool for config								*/
	char *cfg_name;									/**< name of config file								*/
	char *pid_file;									/**< name of pid file									*/
	char *temp_dir;									/**< dir for temp files									*/

	char *bind_host;								/**< bind line											*/
	struct in_addr bind_addr;						/**< bind address in case of TCP socket					*/
	uint16_t bind_port;								/**< bind port in case of TCP socket					*/
	uint16_t bind_family;							/**< bind type (AF_UNIX or AF_INET)						*/

	char *control_host;								/**< bind line for controller							*/
	struct in_addr control_addr;					/**< bind address for controller						*/
	uint16_t control_port;							/**< bind port for controller							*/
	uint16_t control_family;						/**< bind family for controller							*/
	int controller_enabled;							/**< whether controller is enabled						*/
	char *control_password;							/**< controller password								*/

	gboolean no_fork;								/**< if 1 do not call daemon()							*/
	gboolean config_test;							/**< if TRUE do only config file test					*/
	unsigned int workers_number;					/**< number of workers									*/
	unsigned int lmtp_workers_number;				/**< number of lmtp workers								*/

	enum rspamd_log_type log_type;					/**< log type											*/
	int log_facility;								/**< log facility in case of syslog						*/
	int log_level;									/**< log level trigger									*/
	char *log_file;									/**< path to logfile in case of file logging			*/
	int log_fd;										/**< log descriptor in case of file logging				*/

	size_t max_statfile_size;						/**< maximum size for statfile							*/

	struct memcached_server memcached_servers[MAX_MEMCACHED_SERVERS];	/**< memcached servers				*/
	size_t memcached_servers_num;					/**< number of memcached servers						*/
	memc_proto_t memcached_protocol;				/**< memcached protocol									*/
	unsigned int memcached_error_time;				/**< memcached error time (see upstream documentation)	*/
	unsigned int memcached_dead_time;				/**< memcached dead time								*/
	unsigned int memcached_maxerrors;				/**< maximum number of errors							*/
	unsigned int memcached_connect_timeout;			/**< connection timeout									*/

	gboolean lmtp_enable;							/**< is lmtp agent is enabled							*/
	char *lmtp_host;								/**< host for lmtp agent								*/
	struct in_addr lmtp_addr;						/**< bind address for lmtp								*/
	uint16_t lmtp_port;								/**< bind port for lmtp agent							*/
	uint16_t lmtp_family;							/**< bind family for lmtp agent							*/
	char *lmtp_metric;								/**< metric to use in lmtp module						*/

	gboolean delivery_enable;						/**< is delivery agent is enabled						*/
	char *deliver_host;								/**< host for mail deliviring							*/
	struct in_addr deliver_addr;					/**< its address										*/
	uint16_t deliver_port;							/**< port for deliviring								*/
	uint16_t deliver_family;						/**< socket family for delivirnig						*/
	char *deliver_agent_path;						/**< deliver to pipe instead of socket					*/
	gboolean deliver_lmtp;							/**< use LMTP instead of SMTP							*/

	LIST_HEAD (modulesq, perl_module) perl_modules;	/**< linked list of perl modules to load				*/

	LIST_HEAD (headersq, filter) header_filters;	/**< linked list of all header's filters				*/
	LIST_HEAD (mimesq, filter) mime_filters;		/**< linked list of all mime filters					*/
	LIST_HEAD (messagesq, filter) message_filters;	/**< linked list of all message's filters				*/
	LIST_HEAD (urlsq, filter) url_filters;			/**< linked list of all url's filters					*/
	char *header_filters_str;						/**< string of header's filters							*/
	char *mime_filters_str;							/**< string of mime's filters							*/
	char *message_filters_str;						/**< string of message's filters						*/
	char *url_filters_str;							/**< string for url's filters							*/
	GHashTable* modules_opts;						/**< hash for module options indexed by module name		*/
	GHashTable* variables;							/**< hash of $variables defined in config, indexed by variable name */
	GHashTable* metrics;							/**< hash of metrics indexed by metric name				*/
	GHashTable* factors;							/**< hash of factors indexed by symbol name				*/
	GHashTable* c_modules;							/**< hash of c modules indexed by module name			*/
	GHashTable* composite_symbols;					/**< hash of composite symbols indexed by its name		*/
	GHashTable* statfiles;							/**< hash of defined statfiles indexed by alias			*/
    GHashTable* cfg_params;							/**< all cfg params indexed by its name in this structure */
	int clock_res;									/**< resolution of clock used							*/
};

/**
 * Add memcached server to config
 * @param cf config file to use
 * @param str line that describes server's credits
 * @return 1 if line was successfully parsed and 0 in case of error
 */
int add_memcached_server (struct config_file *cf, char *str);

/**
 * Parse bind credits
 * @param cf config file to use
 * @param str line that presents bind line
 * @param type type of credits
 * @return 1 if line was successfully parsed and 0 in case of error
 */
int parse_bind_line (struct config_file *cf, char *str, enum rspamd_cred_type type);

/**
 * Init default values
 * @param cfg config file
 */
void init_defaults (struct config_file *cfg);

/**
 * Free memory used by config structure
 * @param cfg config file
 */
void free_config (struct config_file *cfg);

/**
 * Gets module option with specified name
 * @param cfg config file
 * @param module_name name of module
 * @param opt_name name of option to get
 * @return module value or NULL if option does not defined
 */
char* get_module_opt (struct config_file *cfg, char *module_name, char *opt_name);

/**
 * Parse limit
 * @param limit string representation of limit (eg. 1M)
 * @return numeric value of limit
 */
size_t parse_limit (const char *limit);

/**
 * Parse seconds
 * @param t string representation of seconds (eg. 1D)
 * @return numeric value of string
 */
unsigned int parse_seconds (const char *t);

/**
 * Parse flag
 * @param str string representation of flag (eg. 'on')
 * @return numeric value of flag (0 or 1)
 */
char parse_flag (const char *str);

/**
 * Substitutes variable in specified string, may be recursive (eg. ${var1${var2}})
 * @param cfg config file
 * @param str incoming string
 * @param recursive whether do recursive scanning
 * @return new string with substituted variables (uses cfg memory pool for allocating)
 */
char* substitute_variable (struct config_file *cfg, char *str, u_char recursive);

/**
 * Do post load actions for config
 * @param cfg config file
 */
void post_load_config (struct config_file *cfg);


/**
 * Replace all \" with a single " in given string
 * @param line input string
 */
void unescape_quotes (char *line);

int yylex (void);
int yyparse (void);
void yyrestart (FILE *);
void parse_err (const char *fmt, ...);
void parse_warn (const char *fmt, ...);

#endif /* ifdef CFG_FILE_H */
/* 
 * vi:ts=4 
 */