aboutsummaryrefslogtreecommitdiff
path: root/src/mailfrom.h
blob: 90d8460ea4b9fe2b9627d23a2652680336b9544d (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
/* This file is part of mailfrom filter.
   Copyright (C) 2005, 2006 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301 USA */

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <regex.h>
#include <ctype.h>
#include <mailutils/mailutils.h>
#include <libmilter/mfapi.h>

#ifndef MAXPACKET
# define MAXPACKET 8192	/* max packet size used internally by BIND */
#endif 

typedef enum {
	mf_success,
	mf_not_found,
	mf_failure,
	mf_temp_failure,
} mf_status;

#define mf_resolved(c) ((c) == mf_success || (c) == mf_not_found)

#define MAXMXCOUNT 32
typedef char *mxbuf_t[MAXMXCOUNT];

#define DEFAULT_PIDFILE MAILFROMSTATEDIR "/mailfromd.pid"
#define DEFAULT_DATABASE MAILFROMSTATEDIR "/mailfromd"
#define DEFAULT_RATE_DATABASE MAILFROMSTATEDIR "/rates"
#define DEFAULT_CONFIG_FILE SYSCONFDIR "/mailfromd.rc"

#define __DBG(lev) if (debug_level >= lev)

#define debug(lev,text) \
 __DBG(lev) debug_log("%s:%lu:%s: %s", __FILE__, __LINE__, __FUNCTION__, text)
#define debug1(lev,fmt,v) \
 __DBG(lev) debug_log("%s:%lu:%s: " fmt, __FILE__, __LINE__, __FUNCTION__, v)
#define debug2(lev,fmt,a,b) \
 __DBG(lev) debug_log("%s:%lu:%s: " fmt, \
   __FILE__, __LINE__, __FUNCTION__, a, b)
#define debug3(lev,fmt,a,b,c) \
 __DBG(lev) debug_log("%s:%lu:%s: " fmt, \
   __FILE__, __LINE__, __FUNCTION__, a, b, c)
#define debug4(lev,fmt,a,b,c,d) \
 __DBG(lev) debug_log("%s:%lu:%s: " fmt, \
   __FILE__, __LINE__, __FUNCTION__, a, b, c, d)
#define debug5(lev,fmt,a,b,c,d,e) \
 __DBG(lev) debug_log("%s:%lu:%s: " fmt, \
   __FILE__, __LINE__, __FUNCTION__, a, b, c, d, e)
#define debug6(lev,fmt,a,b,c,d,e,f) \
 __DBG(lev) debug_log("%s:%lu:%s: " fmt, \
   __FILE__, __LINE__, __FUNCTION__, a, b, c, d, e, f)

extern int debug_level;
extern int parse_error_count;
extern time_t positive_expire_interval;
extern time_t negative_expire_interval;
extern time_t rates_expire_interval;
extern int predict_next_option;
extern double predict_rate;
extern char *smtp_domain;
extern char *postmaster_email;


/* Configuration file support */

typedef struct node NODE;
typedef struct function FUNCTION;
typedef struct locus LOCUS;

struct locus {
	char *file;
	size_t line;
};

struct function {
	char *name;
	NODE *node;
	LOCUS locus;
};

struct if_node {
	NODE *cond;
	NODE *if_true;
	NODE *if_false;
};

struct poll_action {
	struct poll_action *next;
	struct locus locus;
	mf_status status;
	NODE *node;
};

struct poll_node {
	NODE *email;
	NODE *client_addr;
	NODE *ehlo;
	NODE *mailfrom;
	struct poll_action *actions;
};

enum bin_opcode {
	bin_and,
	bin_or,
	bin_eq,
	bin_ne,
	bin_regex,
	bin_match,
	bin_fnmatch
};

struct bin_node {
	enum bin_opcode opcode;
	NODE *arg[2];
};

struct regex_node { /* A special form of bin_node */
	enum bin_opcode opcode; /* always == bin_regex */
	NODE *arg;
	regex_t regex;
};

enum un_opcode {
	unary_not
};

struct un_node {
	enum un_opcode opcode;
	NODE *arg;
};

struct return_node {
	sfsistat stat;
	char *code;
	char *xcode;
	char *message;
};

enum header_opcode {
	header_add,
	header_replace,
	header_delete
};

struct header_node {
	enum header_opcode opcode;
	char *name;
	char *value;
};

struct rate_node {
	NODE *email;
	double limit;
};

enum node_type {
	node_type_string,
	node_type_symbol,
	node_type_if,
	node_type_poll,
	node_type_bin,
	node_type_un,
	node_type_next,
	node_type_return,
	node_type_header,
	node_type_hostname,
	node_type_relayed_domain,
	node_type_rate,
	node_type_listens,
	node_type_funcall,
	node_type_validuser
};

struct node {
	NODE *next;
	enum node_type type;
	struct locus locus;
	union {
		char *string;
		struct if_node cond;
		struct poll_node poll;
		struct bin_node bin;
		struct un_node un;
		struct return_node ret;
		struct header_node hdr;
		struct regex_node re;
		struct rate_node rate;
		NODE *node;
	} v;
};

void debug_log(char *fmt, ...);
void parse_error(char *fmt, ...);
void parse_error_locus(LOCUS *loc, char *fmt, ...);
int yyparse();
int source(char *, int);
int parse_config(char *name, int ydebug, int ldebug);
const struct locus *get_locus(void);
void trace(const char *fmt, ...);
int convert_rate(const char *arg, double *rate);

const char *header_command_str(enum header_opcode opcode);
const char *sfsistat_str(sfsistat stat);
int priv_store_header_command(SMFICTX *ctx, struct header_node *node);

mf_status getmx(char *ipstr, mxbuf_t mxbuf);

void dict_init(mu_list_t *dict);
char *dict_install(mu_list_t dict, char *name, char *value);
void dict_destroy(mu_list_t *dict);
char *dict_getsym(void *data, char *str);
int set_option(char *name, char *value, int override);
sfsistat run_program(SMFICTX *ctx);
sfsistat test_program(mu_list_t dict);
void print_config(void);

mf_status listens_on (const char *client_addr, int port);
mf_status method_standard(SMFICTX *ctx, char *email, char *ehlo, char *mailfrom);
mf_status method_strict(SMFICTX *ctx, char *email, char *client_addr,
			char *ehlo, char *mailfrom);



/* DBM support */
#include "mu_dbm.h"

typedef void (*db_item_printer_t)(const char *key, size_t size,
				 const void *content);
typedef int (*db_expire_t)(const void *content);

#ifdef USE_DBM
# define cache_get _mailfrom_cache_get
# define cache_insert _mailfrom_cache_insert
# define cache_get2 _mailfrom_cache_get2
# define cache_insert2 _mailfrom_cache_insert2
# define cache_delete _mailfrom_cache_delete
# define cache_list_item _mailfrom_cache_list_item
# define cache_list_db _mailfrom_cache_list_db
# define cache_expire_db _mailfrom_cache_expire_db
# define get_rate _get_rate
# define rate_list_item _rate_list_item
# define rate_list_db _rate_list_db
# define rate_expire_db _rate_expire_db
# define rate_delete _rate_delete

mf_status cache_get(char *email);
void cache_insert(char *email, mf_status rc);
void cache_delete(char *email);
mf_status cache_get2(char *email, char *client_addr);
void cache_insert2(char *email, char *client_addr, mf_status rc);
void cache_list_item(char *email);
void cache_list_db(void);
void cache_expire_db(void);
mf_status _get_rate(char *email, double *ret);
void _rate_list_item(char *email);
void _rate_list_db(void);
void _rate_expire_db(void);
void _rate_delete(char *email);

void db_list_item(char *dbname, char *key, db_item_printer_t fun);
void db_list(char *dbname, db_item_printer_t fun);
void db_expire(char *dbname, db_expire_t fun);
void db_delete(char *dbname, char *id);

#else
# define cache_get(email) mf_failure
# define cache_insert(email, rc)
# define cache_get2(email, client_addr) mf_failure
# define cache_insert2(email, client_addr, rc)
# define cache_delete(email)
# define cache_list_item(email)
# define cache_list_db()
# define cache_expire_db()
# define get_rate(email, ret) mf_failure
# define rate_list_item(e) 
# define rate_list_db()
# define rate_expire_db()
# define rate_delete(email)
#endif

NODE *function_lookup(char *name);
void function_install(char *name, NODE *node, LOCUS *locus);



Return to:

Send suggestions and report system problems to the System administrator.