Mercurial > dnsbl
annotate src/context.h @ 164:5809bcdc325b stable-6-0-4
spamassassin changes
author | carl |
---|---|
date | Sun, 26 Aug 2007 19:43:35 -0700 |
parents | 97d7da45fe2a |
children | 9b129ed78d7d |
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 | |
163 | 145 int spamassassin_limit; // max score from spamassassin |
94 | 146 dnsblp_map dnsbl_names; // name to dnsbl mapping for lists that are available in this context and children |
147 dnsblp_list dnsbl_list; // list of dnsbls to be used in this context | |
140 | 148 int default_rcpt_rate; // if not specified per user |
136 | 149 rcpt_rates rcpt_per_hour; // per user limits on number of recipients per hour |
150 | |
94 | 151 |
152 public: | |
153 CONTEXT(CONTEXTP parent_, char *name_); | |
154 ~CONTEXT(); | |
155 CONTEXTP get_parent() {return parent;}; | |
156 bool is_parent(CONTEXTP p); // is p a parent of this? | |
157 char* get_full_name(char *buf, int size); | |
158 void add_context(CONTEXTP child) {children[child->name] = child;}; | |
159 bool allow_env_to(char *to) {return (parent) ? parent->cover_env_to(to) : true;}; | |
160 bool cover_env_to(char *to); | |
161 | |
153 | 162 void set_verifier(VERIFYP v) {verifier = v;}; |
94 | 163 void set_verify(char *host) {verify_host = host;}; |
164 char* get_verify() {return verify_host;}; | |
165 VERIFYP find_verify(char *to); | |
166 | |
153 | 167 void set_whitelister(WHITELISTERP v) {whitelister = v;}; |
168 void set_autowhite(char *fn) {autowhite_file = fn;}; | |
169 char* get_autowhite() {return autowhite_file;}; | |
162 | 170 WHITELISTERP find_autowhite(char *from, char *to); |
153 | 171 |
140 | 172 void set_default_rate(int limit) {default_rcpt_rate = limit;}; |
136 | 173 void add_rate(char *user, int limit) {rcpt_per_hour[user] = limit;}; |
174 int find_rate(char *user); | |
175 | |
94 | 176 void add_to(char *to) {env_to.insert(to);}; |
177 void add_from(char *from, char *status) {env_from[from] = status;}; | |
178 void add_from_context(char *from, CONTEXTP con) {env_from_context[from] = con;}; | |
179 void set_from_default(char *status) {env_from_default = status;}; | |
180 char* find_from(char *from); | |
181 CONTEXTP find_context(char *from); | |
182 CONTEXTP find_from_context_name(char *name); | |
183 | |
163 | 184 void set_content_filtering(bool filter) {content_filtering = filter; }; |
185 void set_content_suffix(char *suffix) {content_suffix = suffix; }; | |
186 void set_content_message(char *message) {content_message = message; }; | |
187 void set_uribl_suffix(char *suffix) {uribl_suffix = suffix; }; | |
188 void set_uribl_message(char *message) {uribl_message = message; }; | |
94 | 189 void add_ignore(char *host) {content_host_ignore.insert(host);}; |
163 | 190 void add_tld(char *tld) {content_tlds.insert(tld); }; |
191 void add_cctld(char *cctld) {content_cctlds.insert(cctld); }; | |
94 | 192 |
163 | 193 void set_host_limit(int limit) {host_limit = limit; }; |
194 void set_spamassassin_limit(int limit) {spamassassin_limit = limit; }; | |
94 | 195 void set_host_message(char *message) {host_limit_message = message;}; |
163 | 196 void set_host_random(bool random) {host_random = random; }; |
197 void set_tag_limit(int limit) {tag_limit = limit; }; | |
94 | 198 void set_tag_message(char *message) {tag_limit_message = message;}; |
163 | 199 void add_tag(char *tag) {html_tags.insert(tag); }; |
94 | 200 |
163 | 201 void add_dnsbl(char *name, DNSBLP dns) {dnsbl_names[name] = dns; }; |
94 | 202 void add_dnsbl(DNSBLP dns) {dnsbl_list.push_back(dns);}; |
203 DNSBLP find_dnsbl(char *name); | |
204 | |
163 | 205 bool get_content_filtering() {return content_filtering; }; |
206 int get_host_limit() {return host_limit; }; | |
207 int get_spamassassin_limit() {return spamassassin_limit;}; | |
208 bool get_host_random() {return host_random; }; | |
94 | 209 char* get_content_suffix(); |
210 char* get_content_message(); | |
119 | 211 char* get_uribl_suffix(); |
212 char* get_uribl_message(); | |
94 | 213 string_set& get_content_host_ignore(); |
214 string_set& get_content_tlds(); | |
117 | 215 string_set& get_content_cctlds(); |
94 | 216 string_set& get_html_tags(); |
217 dnsblp_list& get_dnsbl_list(); | |
218 | |
163 | 219 bool acceptable_content(recorder &memory, int score, char *&msg); |
94 | 220 bool ignore_host(char *host); |
221 | |
144
31ff00ea6bfb
allow parent/child to share a fully qualified env_to address
carl
parents:
143
diff
changeset
|
222 void dump(bool isdefault, int level = 0); |
94 | 223 }; |
224 | |
225 | |
226 struct CONFIG { | |
227 // the only mutable stuff once it has been loaded from the config file | |
228 int reference_count; // protected by the global config_mutex | |
229 // all the rest is constant after loading from the config file | |
230 int generation; | |
231 time_t load_time; | |
232 string_set config_files; | |
233 context_list contexts; // owns all the contexts, not just top level contexts | |
234 context_map env_to; // map recipient to a filtering context | |
235 CONTEXTP default_context;// for env_to values that don't have their own specific filtering context | |
236 // the default context is also used for some of the content filtering values | |
237 | |
238 CONFIG(); | |
239 ~CONFIG(); | |
240 void add_context(CONTEXTP con); | |
241 void add_to(char *to, CONTEXTP con); | |
242 CONTEXTP find_context(char *to); | |
243 void dump(); | |
244 }; | |
245 | |
136 | 246 struct RATELIMIT { |
247 | |
248 }; | |
249 | |
153 | 250 extern char *token_autowhite; |
94 | 251 extern char *token_black; |
117 | 252 extern char *token_cctld; |
94 | 253 extern char *token_content; |
254 extern char *token_context; | |
255 extern char *token_dccfrom; | |
256 extern char *token_dccto; | |
257 extern char *token_default; | |
258 extern char *token_dnsbl; | |
259 extern char *token_dnsbll; | |
260 extern char *token_envfrom; | |
261 extern char *token_envto; | |
262 extern char *token_filter; | |
263 extern char *token_host_limit; | |
264 extern char *token_html_limit; | |
265 extern char *token_html_tags; | |
266 extern char *token_ignore; | |
267 extern char *token_include; | |
268 extern char *token_inherit; | |
269 extern char *token_lbrace; | |
270 extern char *token_mailhost; | |
271 extern char *token_many; | |
272 extern char *token_off; | |
117 | 273 extern char *token_ok2; |
94 | 274 extern char *token_ok; |
275 extern char *token_on; | |
136 | 276 extern char *token_rate; |
94 | 277 extern char *token_rbrace; |
278 extern char *token_semi; | |
279 extern char *token_soft; | |
163 | 280 extern char *token_spamassassin; |
94 | 281 extern char *token_substitute; |
282 extern char *token_tld; | |
283 extern char *token_unknown; | |
119 | 284 extern char *token_uribl; |
94 | 285 extern char *token_white; |
286 | |
153 | 287 extern pthread_mutex_t verifier_mutex; // protect the verifier map |
288 extern pthread_mutex_t whitelister_mutex; // protect the | |
94 | 289 |
290 void discard(string_set &s); | |
291 char* register_string(string_set &s, char *name); | |
292 char* register_string(char *name); | |
164 | 293 void clear_strings(); |
94 | 294 CONFIG *parse_config(char *fn); |
295 bool load_conf(CONFIG &dc, char *fn); | |
296 void* verify_closer(void *arg); | |
153 | 297 void* whitelister_writer(void *arg); |
94 | 298 void token_init(); |
299 | |
300 #endif |