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