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