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; //
|
|
103 string_set html_tags; // set of valid html tags
|
|
104 int host_limit; // limit on host names
|
|
105 char * host_limit_message; // error message for excessive host names
|
|
106 bool host_random; // pick a random selection of host names rather than error for excessive hosts
|
|
107 int tag_limit; // limit on bad html tags
|
|
108 char * tag_limit_message; // error message for excessive bad html tags
|
|
109 dnsblp_map dnsbl_names; // name to dnsbl mapping for lists that are available in this context and children
|
|
110 dnsblp_list dnsbl_list; // list of dnsbls to be used in this context
|
|
111
|
|
112 public:
|
|
113 CONTEXT(CONTEXTP parent_, char *name_);
|
|
114 ~CONTEXT();
|
|
115 CONTEXTP get_parent() {return parent;};
|
|
116 bool is_parent(CONTEXTP p); // is p a parent of this?
|
|
117 char* get_full_name(char *buf, int size);
|
|
118 void add_context(CONTEXTP child) {children[child->name] = child;};
|
|
119 bool allow_env_to(char *to) {return (parent) ? parent->cover_env_to(to) : true;};
|
|
120 bool cover_env_to(char *to);
|
|
121
|
|
122 void set_verify(char *host) {verify_host = host;};
|
|
123 char* get_verify() {return verify_host;};
|
|
124 VERIFYP find_verify(char *to);
|
|
125
|
|
126 void add_to(char *to) {env_to.insert(to);};
|
|
127 void add_from(char *from, char *status) {env_from[from] = status;};
|
|
128 void add_from_context(char *from, CONTEXTP con) {env_from_context[from] = con;};
|
|
129 void set_from_default(char *status) {env_from_default = status;};
|
|
130 char* find_from(char *from);
|
|
131 CONTEXTP find_context(char *from);
|
|
132 CONTEXTP find_from_context_name(char *name);
|
|
133
|
|
134 void set_content_filtering(bool filter) {content_filtering = filter;};
|
|
135 void set_content_suffix(char *suffix) {content_suffix = suffix;};
|
|
136 void set_content_message(char *message) {content_message = message;};
|
|
137 void add_ignore(char *host) {content_host_ignore.insert(host);};
|
|
138 void add_tld(char *tld) {content_tlds.insert(tld);};
|
|
139
|
|
140 void set_host_limit(int limit) {host_limit = limit;};
|
|
141 void set_host_message(char *message) {host_limit_message = message;};
|
|
142 void set_host_random(bool random) {host_random = random;};
|
|
143 void set_tag_limit(int limit) {tag_limit = limit;};
|
|
144 void set_tag_message(char *message) {tag_limit_message = message;};
|
|
145 void add_tag(char *tag) {html_tags.insert(tag);};
|
|
146
|
|
147 void add_dnsbl(char *name, DNSBLP dns) {dnsbl_names[name] = dns;};
|
|
148 void add_dnsbl(DNSBLP dns) {dnsbl_list.push_back(dns);};
|
|
149 DNSBLP find_dnsbl(char *name);
|
|
150
|
|
151 bool get_content_filtering() {return content_filtering;};
|
|
152 int get_host_limit() {return host_limit;};
|
|
153 bool get_host_random() {return host_random;};
|
|
154 char* get_content_suffix();
|
|
155 char* get_content_message();
|
|
156 string_set& get_content_host_ignore();
|
|
157 string_set& get_content_tlds();
|
|
158 string_set& get_html_tags();
|
|
159 dnsblp_list& get_dnsbl_list();
|
|
160
|
|
161 bool acceptable_content(recorder &memory, char *&msg);
|
|
162 bool ignore_host(char *host);
|
|
163
|
|
164 void dump(int level = 0);
|
|
165 };
|
|
166
|
|
167
|
|
168 struct CONFIG {
|
|
169 // the only mutable stuff once it has been loaded from the config file
|
|
170 int reference_count; // protected by the global config_mutex
|
|
171 // all the rest is constant after loading from the config file
|
|
172 int generation;
|
|
173 time_t load_time;
|
|
174 string_set config_files;
|
|
175 context_list contexts; // owns all the contexts, not just top level contexts
|
|
176 context_map env_to; // map recipient to a filtering context
|
|
177 CONTEXTP default_context;// for env_to values that don't have their own specific filtering context
|
|
178 // the default context is also used for some of the content filtering values
|
|
179
|
|
180 CONFIG();
|
|
181 ~CONFIG();
|
|
182 void add_context(CONTEXTP con);
|
|
183 void add_to(char *to, CONTEXTP con);
|
|
184 CONTEXTP find_context(char *to);
|
|
185 void dump();
|
|
186 };
|
|
187
|
|
188 extern char *token_black;
|
|
189 extern char *token_content;
|
|
190 extern char *token_context;
|
|
191 extern char *token_dccfrom;
|
|
192 extern char *token_dccto;
|
|
193 extern char *token_default;
|
|
194 extern char *token_dnsbl;
|
|
195 extern char *token_dnsbll;
|
|
196 extern char *token_envfrom;
|
|
197 extern char *token_envto;
|
|
198 extern char *token_filter;
|
|
199 extern char *token_host_limit;
|
|
200 extern char *token_html_limit;
|
|
201 extern char *token_html_tags;
|
|
202 extern char *token_ignore;
|
|
203 extern char *token_include;
|
|
204 extern char *token_inherit;
|
|
205 extern char *token_lbrace;
|
|
206 extern char *token_mailhost;
|
|
207 extern char *token_many;
|
|
208 extern char *token_off;
|
|
209 extern char *token_ok;
|
|
210 extern char *token_ok2;
|
|
211 extern char *token_on;
|
|
212 extern char *token_rbrace;
|
|
213 extern char *token_semi;
|
|
214 extern char *token_soft;
|
|
215 extern char *token_substitute;
|
|
216 extern char *token_tld;
|
|
217 extern char *token_unknown;
|
|
218 extern char *token_white;
|
|
219
|
|
220 extern char *token_myhostname;
|
|
221
|
|
222 extern verify_map verifiers; // map of smtp hosts to verify structures, owns all the verify structures
|
|
223 extern string_set all_strings; // owns all the strings, only modified by the config loader thread
|
|
224
|
|
225 void discard(string_set &s);
|
|
226 char* register_string(string_set &s, char *name);
|
|
227 char* register_string(char *name);
|
|
228 CONFIG *parse_config(char *fn);
|
|
229 bool load_conf(CONFIG &dc, char *fn);
|
|
230 void add_verify_host(char *host);
|
|
231 void* verify_closer(void *arg);
|
|
232 void token_init();
|
|
233
|
|
234 #endif
|