94
|
1 #ifndef context_include
|
|
2 #define context_include
|
|
3
|
|
4 #include "tokenizer.h"
|
|
5 #include <map>
|
|
6
|
|
7
|
|
8 enum status {oksofar, // not rejected yet
|
|
9 white, // whitelisted
|
|
10 black, // blacklisted
|
|
11 reject}; // rejected by a dns list
|
|
12
|
|
13 class DNSBL;
|
|
14 class CONTEXT;
|
|
15 class VERIFY;
|
|
16 class SMTP;
|
|
17 class recorder;
|
|
18
|
|
19 typedef map<char *, char *, ltstr> string_map;
|
|
20 typedef set<int> int_set;
|
|
21 typedef list<SMTP *> smtp_list;
|
|
22 typedef list<char *> string_list;
|
|
23 typedef DNSBL * DNSBLP;
|
|
24 typedef VERIFY * VERIFYP;
|
|
25 typedef list<DNSBLP> dnsblp_list;
|
|
26 typedef map<char *, DNSBLP, ltstr> dnsblp_map;
|
|
27 typedef CONTEXT * CONTEXTP;
|
|
28 typedef list<CONTEXTP> context_list;
|
|
29 typedef map<char *, CONTEXTP, ltstr> context_map;
|
|
30 typedef map<char *, int, ltstr> ns_mapper;
|
136
|
31 typedef map<char *, int, ltstr> rcpt_rates;
|
94
|
32 typedef map<char *, VERIFYP, ltstr> verify_map;
|
|
33
|
|
34 class SMTP {
|
|
35 static const int maxlen = 1000;
|
|
36 int fd;
|
|
37 bool error;
|
|
38 time_t stamp;
|
|
39 char efrom[maxlen]; // last envelope from sent on this socket
|
|
40 int pending; // unread bytes in buffer, not including the null terminator
|
|
41 char buffer[maxlen];
|
|
42 public:
|
99
|
43 SMTP(int f) {fd = f; error = false; now(); efrom[0] = '\0'; init();};
|
94
|
44 ~SMTP() {if (!error) quit(); closefd();};
|
|
45 void init() {pending = 0; buffer[0] = '\0';};
|
|
46 void append(char *c) {strncat(buffer, c, max(0, maxlen-1-(int)strlen(c)));};
|
|
47 bool err() {return error;};
|
|
48 void now() {stamp = time(NULL);};
|
|
49 time_t get_stamp() {return stamp;};
|
|
50 int get_fd() {return fd;};
|
|
51 int writer();
|
|
52 int reader();
|
|
53 int read_line();
|
|
54 int read_response();
|
97
|
55 void flush_line(int r);
|
94
|
56 int cmd(char *c);
|
|
57 int helo();
|
|
58 int rset();
|
|
59 int from(char *f);
|
|
60 int rcpt(char *t);
|
|
61 int quit();
|
|
62 void closefd();
|
|
63 #ifdef VERIFY_DEBUG
|
|
64 static void log(char *m, int v);
|
|
65 static void log(char *m, char *v);
|
|
66 #endif
|
|
67 };
|
|
68
|
|
69 class VERIFY {
|
|
70 char *host; // host to be used to verify recipient addresses
|
|
71 time_t last_err; // time of last socket error
|
|
72 pthread_mutex_t mutex; // protect the lists of sockets and timestamps
|
|
73 smtp_list connections;// open sockets, ready to be used
|
|
74 public:
|
|
75 VERIFY(char *h);
|
|
76 void closer(); // if the oldest socket is ancient, close it
|
|
77 SMTP *get_connection();
|
|
78 void put_connection(SMTP *conn);
|
|
79 bool ok(char *from, char *to);
|
|
80 };
|
|
81
|
|
82 struct DNSBL {
|
|
83 char *name; // nickname for this dns based list
|
|
84 char *suffix; // blacklist suffix like blackholes.five-ten-sg.com
|
|
85 char *message; // error message with one or two %s operators for the ip address replacement
|
|
86 DNSBL(char *n, char *s, char *m);
|
|
87 bool operator==(const DNSBL &rhs);
|
|
88 };
|
|
89
|
|
90 class CONTEXT {
|
|
91 CONTEXTP parent;
|
|
92 char * name;
|
|
93 context_map children; // map child context names to their contexts
|
|
94 string_set env_to; // this context applies to these envelope recipients
|
|
95 char * verify_host; // use this smtp host to verify email addresses
|
|
96 string_map env_from; // map senders to white/black/unknown
|
|
97 context_map env_from_context; // map senders to a child context
|
|
98 char * env_from_default; // default value for senders that are not found in the map white/black/unknown/inherit
|
|
99 bool content_filtering; //
|
119
|
100 char * content_suffix; // for url body filtering based on ip addresses of hostnames in the body
|
94
|
101 char * content_message; // ""
|
119
|
102 char * uribl_suffix; // for uribl body filtering based on hostnames in the body
|
|
103 char * uribl_message; // ""
|
94
|
104 string_set content_host_ignore;// hosts to ignore for content sbl checking
|
|
105 string_set content_tlds; //
|
117
|
106 string_set content_cctlds; //
|
94
|
107 string_set html_tags; // set of valid html tags
|
|
108 int host_limit; // limit on host names
|
|
109 char * host_limit_message; // error message for excessive host names
|
|
110 bool host_random; // pick a random selection of host names rather than error for excessive hosts
|
|
111 int tag_limit; // limit on bad html tags
|
|
112 char * tag_limit_message; // error message for excessive bad html tags
|
|
113 dnsblp_map dnsbl_names; // name to dnsbl mapping for lists that are available in this context and children
|
|
114 dnsblp_list dnsbl_list; // list of dnsbls to be used in this context
|
136
|
115 rcpt_rates rcpt_per_hour; // per user limits on number of recipients per hour
|
|
116
|
94
|
117
|
|
118 public:
|
|
119 CONTEXT(CONTEXTP parent_, char *name_);
|
|
120 ~CONTEXT();
|
|
121 CONTEXTP get_parent() {return parent;};
|
|
122 bool is_parent(CONTEXTP p); // is p a parent of this?
|
|
123 char* get_full_name(char *buf, int size);
|
|
124 void add_context(CONTEXTP child) {children[child->name] = child;};
|
|
125 bool allow_env_to(char *to) {return (parent) ? parent->cover_env_to(to) : true;};
|
|
126 bool cover_env_to(char *to);
|
|
127
|
|
128 void set_verify(char *host) {verify_host = host;};
|
|
129 char* get_verify() {return verify_host;};
|
|
130 VERIFYP find_verify(char *to);
|
|
131
|
136
|
132 void add_rate(char *user, int limit) {rcpt_per_hour[user] = limit;};
|
|
133 int find_rate(char *user);
|
|
134
|
94
|
135 void add_to(char *to) {env_to.insert(to);};
|
|
136 void add_from(char *from, char *status) {env_from[from] = status;};
|
|
137 void add_from_context(char *from, CONTEXTP con) {env_from_context[from] = con;};
|
|
138 void set_from_default(char *status) {env_from_default = status;};
|
|
139 char* find_from(char *from);
|
|
140 CONTEXTP find_context(char *from);
|
|
141 CONTEXTP find_from_context_name(char *name);
|
|
142
|
|
143 void set_content_filtering(bool filter) {content_filtering = filter;};
|
|
144 void set_content_suffix(char *suffix) {content_suffix = suffix;};
|
|
145 void set_content_message(char *message) {content_message = message;};
|
119
|
146 void set_uribl_suffix(char *suffix) {uribl_suffix = suffix;};
|
|
147 void set_uribl_message(char *message) {uribl_message = message;};
|
94
|
148 void add_ignore(char *host) {content_host_ignore.insert(host);};
|
|
149 void add_tld(char *tld) {content_tlds.insert(tld);};
|
117
|
150 void add_cctld(char *cctld) {content_cctlds.insert(cctld);};
|
94
|
151
|
|
152 void set_host_limit(int limit) {host_limit = limit;};
|
|
153 void set_host_message(char *message) {host_limit_message = message;};
|
|
154 void set_host_random(bool random) {host_random = random;};
|
|
155 void set_tag_limit(int limit) {tag_limit = limit;};
|
|
156 void set_tag_message(char *message) {tag_limit_message = message;};
|
|
157 void add_tag(char *tag) {html_tags.insert(tag);};
|
|
158
|
|
159 void add_dnsbl(char *name, DNSBLP dns) {dnsbl_names[name] = dns;};
|
|
160 void add_dnsbl(DNSBLP dns) {dnsbl_list.push_back(dns);};
|
|
161 DNSBLP find_dnsbl(char *name);
|
|
162
|
|
163 bool get_content_filtering() {return content_filtering;};
|
|
164 int get_host_limit() {return host_limit;};
|
|
165 bool get_host_random() {return host_random;};
|
|
166 char* get_content_suffix();
|
|
167 char* get_content_message();
|
119
|
168 char* get_uribl_suffix();
|
|
169 char* get_uribl_message();
|
94
|
170 string_set& get_content_host_ignore();
|
|
171 string_set& get_content_tlds();
|
117
|
172 string_set& get_content_cctlds();
|
94
|
173 string_set& get_html_tags();
|
|
174 dnsblp_list& get_dnsbl_list();
|
|
175
|
|
176 bool acceptable_content(recorder &memory, char *&msg);
|
|
177 bool ignore_host(char *host);
|
|
178
|
|
179 void dump(int level = 0);
|
|
180 };
|
|
181
|
|
182
|
|
183 struct CONFIG {
|
|
184 // the only mutable stuff once it has been loaded from the config file
|
|
185 int reference_count; // protected by the global config_mutex
|
|
186 // all the rest is constant after loading from the config file
|
|
187 int generation;
|
|
188 time_t load_time;
|
|
189 string_set config_files;
|
|
190 context_list contexts; // owns all the contexts, not just top level contexts
|
|
191 context_map env_to; // map recipient to a filtering context
|
|
192 CONTEXTP default_context;// for env_to values that don't have their own specific filtering context
|
|
193 // the default context is also used for some of the content filtering values
|
|
194
|
|
195 CONFIG();
|
|
196 ~CONFIG();
|
|
197 void add_context(CONTEXTP con);
|
|
198 void add_to(char *to, CONTEXTP con);
|
|
199 CONTEXTP find_context(char *to);
|
|
200 void dump();
|
|
201 };
|
|
202
|
136
|
203 struct RATELIMIT {
|
|
204
|
|
205 };
|
|
206
|
94
|
207 extern char *token_black;
|
117
|
208 extern char *token_cctld;
|
94
|
209 extern char *token_content;
|
|
210 extern char *token_context;
|
|
211 extern char *token_dccfrom;
|
|
212 extern char *token_dccto;
|
|
213 extern char *token_default;
|
|
214 extern char *token_dnsbl;
|
|
215 extern char *token_dnsbll;
|
|
216 extern char *token_envfrom;
|
|
217 extern char *token_envto;
|
|
218 extern char *token_filter;
|
|
219 extern char *token_host_limit;
|
|
220 extern char *token_html_limit;
|
|
221 extern char *token_html_tags;
|
|
222 extern char *token_ignore;
|
|
223 extern char *token_include;
|
|
224 extern char *token_inherit;
|
|
225 extern char *token_lbrace;
|
|
226 extern char *token_mailhost;
|
|
227 extern char *token_many;
|
|
228 extern char *token_off;
|
117
|
229 extern char *token_ok2;
|
94
|
230 extern char *token_ok;
|
|
231 extern char *token_on;
|
136
|
232 extern char *token_rate;
|
94
|
233 extern char *token_rbrace;
|
|
234 extern char *token_semi;
|
|
235 extern char *token_soft;
|
|
236 extern char *token_substitute;
|
|
237 extern char *token_tld;
|
|
238 extern char *token_unknown;
|
119
|
239 extern char *token_uribl;
|
94
|
240 extern char *token_white;
|
|
241
|
|
242 extern char *token_myhostname;
|
|
243
|
|
244 extern verify_map verifiers; // map of smtp hosts to verify structures, owns all the verify structures
|
|
245 extern string_set all_strings; // owns all the strings, only modified by the config loader thread
|
|
246
|
|
247 void discard(string_set &s);
|
|
248 char* register_string(string_set &s, char *name);
|
|
249 char* register_string(char *name);
|
|
250 CONFIG *parse_config(char *fn);
|
|
251 bool load_conf(CONFIG &dc, char *fn);
|
|
252 void add_verify_host(char *host);
|
|
253 void* verify_closer(void *arg);
|
|
254 void token_init();
|
|
255
|
|
256 #endif
|