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