Mercurial > dnsbl
annotate src/context.h @ 153:8d7c439bb6fa
add auto whitelisting
author | carl |
---|---|
date | Sat, 07 Jul 2007 16:10:39 -0700 |
parents | c7fc218686f5 |
children | a220bfb9211f |
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) | |
105 int get_days() {return days;}; | |
106 }; | |
107 | |
94 | 108 struct DNSBL { |
109 char *name; // nickname for this dns based list | |
110 char *suffix; // blacklist suffix like blackholes.five-ten-sg.com | |
111 char *message; // error message with one or two %s operators for the ip address replacement | |
112 DNSBL(char *n, char *s, char *m); | |
113 bool operator==(const DNSBL &rhs); | |
114 }; | |
115 | |
116 class CONTEXT { | |
117 CONTEXTP parent; | |
118 char * name; | |
119 context_map children; // map child context names to their contexts | |
120 string_set env_to; // this context applies to these envelope recipients | |
121 char * verify_host; // use this smtp host to verify email addresses | |
153 | 122 VERIFYP verifier; // pointer to the verifier structure |
123 char * autowhite_file; // file to use for automatic whitelisting | |
124 WHITELISTERP whitelister; // pointer to the auto whitelister structure | |
94 | 125 string_map env_from; // map senders to white/black/unknown |
126 context_map env_from_context; // map senders to a child context | |
127 char * env_from_default; // default value for senders that are not found in the map white/black/unknown/inherit | |
128 bool content_filtering; // | |
119 | 129 char * content_suffix; // for url body filtering based on ip addresses of hostnames in the body |
94 | 130 char * content_message; // "" |
119 | 131 char * uribl_suffix; // for uribl body filtering based on hostnames in the body |
132 char * uribl_message; // "" | |
94 | 133 string_set content_host_ignore;// hosts to ignore for content sbl checking |
134 string_set content_tlds; // | |
117 | 135 string_set content_cctlds; // |
94 | 136 string_set html_tags; // set of valid html tags |
137 int host_limit; // limit on host names | |
138 char * host_limit_message; // error message for excessive host names | |
139 bool host_random; // pick a random selection of host names rather than error for excessive hosts | |
140 int tag_limit; // limit on bad html tags | |
141 char * tag_limit_message; // error message for excessive bad html tags | |
142 dnsblp_map dnsbl_names; // name to dnsbl mapping for lists that are available in this context and children | |
143 dnsblp_list dnsbl_list; // list of dnsbls to be used in this context | |
140 | 144 int default_rcpt_rate; // if not specified per user |
136 | 145 rcpt_rates rcpt_per_hour; // per user limits on number of recipients per hour |
146 | |
94 | 147 |
148 public: | |
149 CONTEXT(CONTEXTP parent_, char *name_); | |
150 ~CONTEXT(); | |
151 CONTEXTP get_parent() {return parent;}; | |
152 bool is_parent(CONTEXTP p); // is p a parent of this? | |
153 char* get_full_name(char *buf, int size); | |
154 void add_context(CONTEXTP child) {children[child->name] = child;}; | |
155 bool allow_env_to(char *to) {return (parent) ? parent->cover_env_to(to) : true;}; | |
156 bool cover_env_to(char *to); | |
157 | |
153 | 158 void set_verifier(VERIFYP v) {verifier = v;}; |
94 | 159 void set_verify(char *host) {verify_host = host;}; |
160 char* get_verify() {return verify_host;}; | |
161 VERIFYP find_verify(char *to); | |
162 | |
153 | 163 void set_whitelister(WHITELISTERP v) {whitelister = v;}; |
164 void set_autowhite(char *fn) {autowhite_file = fn;}; | |
165 char* get_autowhite() {return autowhite_file;}; | |
166 WHITELISTERP find_autowhite(char *to); | |
167 | |
140 | 168 void set_default_rate(int limit) {default_rcpt_rate = limit;}; |
136 | 169 void add_rate(char *user, int limit) {rcpt_per_hour[user] = limit;}; |
170 int find_rate(char *user); | |
171 | |
94 | 172 void add_to(char *to) {env_to.insert(to);}; |
173 void add_from(char *from, char *status) {env_from[from] = status;}; | |
174 void add_from_context(char *from, CONTEXTP con) {env_from_context[from] = con;}; | |
175 void set_from_default(char *status) {env_from_default = status;}; | |
176 char* find_from(char *from); | |
177 CONTEXTP find_context(char *from); | |
178 CONTEXTP find_from_context_name(char *name); | |
179 | |
180 void set_content_filtering(bool filter) {content_filtering = filter;}; | |
181 void set_content_suffix(char *suffix) {content_suffix = suffix;}; | |
182 void set_content_message(char *message) {content_message = message;}; | |
119 | 183 void set_uribl_suffix(char *suffix) {uribl_suffix = suffix;}; |
184 void set_uribl_message(char *message) {uribl_message = message;}; | |
94 | 185 void add_ignore(char *host) {content_host_ignore.insert(host);}; |
186 void add_tld(char *tld) {content_tlds.insert(tld);}; | |
117 | 187 void add_cctld(char *cctld) {content_cctlds.insert(cctld);}; |
94 | 188 |
189 void set_host_limit(int limit) {host_limit = limit;}; | |
190 void set_host_message(char *message) {host_limit_message = message;}; | |
191 void set_host_random(bool random) {host_random = random;}; | |
192 void set_tag_limit(int limit) {tag_limit = limit;}; | |
193 void set_tag_message(char *message) {tag_limit_message = message;}; | |
194 void add_tag(char *tag) {html_tags.insert(tag);}; | |
195 | |
196 void add_dnsbl(char *name, DNSBLP dns) {dnsbl_names[name] = dns;}; | |
197 void add_dnsbl(DNSBLP dns) {dnsbl_list.push_back(dns);}; | |
198 DNSBLP find_dnsbl(char *name); | |
199 | |
200 bool get_content_filtering() {return content_filtering;}; | |
201 int get_host_limit() {return host_limit;}; | |
202 bool get_host_random() {return host_random;}; | |
203 char* get_content_suffix(); | |
204 char* get_content_message(); | |
119 | 205 char* get_uribl_suffix(); |
206 char* get_uribl_message(); | |
94 | 207 string_set& get_content_host_ignore(); |
208 string_set& get_content_tlds(); | |
117 | 209 string_set& get_content_cctlds(); |
94 | 210 string_set& get_html_tags(); |
211 dnsblp_list& get_dnsbl_list(); | |
212 | |
213 bool acceptable_content(recorder &memory, char *&msg); | |
214 bool ignore_host(char *host); | |
215 | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
216 void dump(bool isdefault, int level = 0); |
94 | 217 }; |
218 | |
219 | |
220 struct CONFIG { | |
221 // the only mutable stuff once it has been loaded from the config file | |
222 int reference_count; // protected by the global config_mutex | |
223 // all the rest is constant after loading from the config file | |
224 int generation; | |
225 time_t load_time; | |
226 string_set config_files; | |
227 context_list contexts; // owns all the contexts, not just top level contexts | |
228 context_map env_to; // map recipient to a filtering context | |
229 CONTEXTP default_context;// for env_to values that don't have their own specific filtering context | |
230 // the default context is also used for some of the content filtering values | |
231 | |
232 CONFIG(); | |
233 ~CONFIG(); | |
234 void add_context(CONTEXTP con); | |
235 void add_to(char *to, CONTEXTP con); | |
236 CONTEXTP find_context(char *to); | |
237 void dump(); | |
238 }; | |
239 | |
136 | 240 struct RATELIMIT { |
241 | |
242 }; | |
243 | |
153 | 244 extern char *token_autowhite; |
94 | 245 extern char *token_black; |
117 | 246 extern char *token_cctld; |
94 | 247 extern char *token_content; |
248 extern char *token_context; | |
249 extern char *token_dccfrom; | |
250 extern char *token_dccto; | |
251 extern char *token_default; | |
252 extern char *token_dnsbl; | |
253 extern char *token_dnsbll; | |
254 extern char *token_envfrom; | |
255 extern char *token_envto; | |
256 extern char *token_filter; | |
257 extern char *token_host_limit; | |
258 extern char *token_html_limit; | |
259 extern char *token_html_tags; | |
260 extern char *token_ignore; | |
261 extern char *token_include; | |
262 extern char *token_inherit; | |
263 extern char *token_lbrace; | |
264 extern char *token_mailhost; | |
265 extern char *token_many; | |
266 extern char *token_off; | |
117 | 267 extern char *token_ok2; |
94 | 268 extern char *token_ok; |
269 extern char *token_on; | |
136 | 270 extern char *token_rate; |
94 | 271 extern char *token_rbrace; |
272 extern char *token_semi; | |
273 extern char *token_soft; | |
274 extern char *token_substitute; | |
275 extern char *token_tld; | |
276 extern char *token_unknown; | |
119 | 277 extern char *token_uribl; |
94 | 278 extern char *token_white; |
279 | |
153 | 280 extern pthread_mutex_t verifier_mutex; // protect the verifier map |
281 extern pthread_mutex_t whitelister_mutex; // protect the | |
94 | 282 |
283 void discard(string_set &s); | |
284 char* register_string(string_set &s, char *name); | |
285 char* register_string(char *name); | |
286 CONFIG *parse_config(char *fn); | |
287 bool load_conf(CONFIG &dc, char *fn); | |
288 void* verify_closer(void *arg); | |
153 | 289 void* whitelister_writer(void *arg); |
94 | 290 void token_init(); |
291 | |
292 #endif |