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