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