Mercurial > dnsbl
annotate src/dnsbl.cpp @ 61:7f44a4974bf6 stable-4-2
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
author | carl |
---|---|
date | Sat, 08 Jan 2005 12:26:30 -0800 |
parents | 390ed250c5d2 |
children | 7ad7d8b100bf |
rev | line source |
---|---|
0 | 1 /* |
2 | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
3 Copyright (c) 2004, 2005 Carl Byington - 510 Software Group, released |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
4 under the GPL version 2 or any later version at your choice available at |
0 | 5 http://www.fsf.org/licenses/gpl.txt |
6 | |
7 Based on a sample milter Copyright (c) 2000-2003 Sendmail, Inc. and its | |
8 suppliers. Inspired by the DCC by Rhyolite Software | |
9 | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
10 -r port The port used to talk to our internal dns resolver processes |
0 | 11 -p port The port through which the MTA will connect to this milter. |
12 -t sec The timeout value. | |
9 | 13 -c Check the config, and print a copy to stdout. Don't start the |
4 | 14 milter or do anything with the socket. |
16 | 15 -d Add debug syslog entries |
16 | |
0 | 17 |
13 | 18 TODO: |
19 1) Add config for max_recipients for each mail domain. Recipients in | |
20 excess of that limit will be rejected, and the entire data will be | |
21 rejected if it is sent. | |
22 | |
23 2) Add config for poison addresses. If any recipient is poison, all | |
24 recipients are rejected even if they would be whitelisted, and the | |
25 data is rejected if sent. | |
26 | |
34 | 27 3) Add option to only allow one recipient if the return path is empty. |
28 | |
53
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
29 4) Check if the envelope from domain name primary MX points 127.0.0.0/8 |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
30 |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
31 5) Add option for using smtp connections to verify addresses from backup |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
32 mx machines. This allows the backup mx to learn the valid addresses |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
33 on the primary machine. |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
34 |
0 | 35 */ |
36 | |
37 | |
38 // from sendmail sample | |
39 #include <sys/types.h> | |
40 #include <sys/stat.h> | |
41 #include <errno.h> | |
42 #include <sysexits.h> | |
43 #include <unistd.h> | |
44 | |
45 // needed for socket io | |
46 #include <sys/ioctl.h> | |
47 #include <net/if.h> | |
48 #include <arpa/inet.h> | |
49 #include <netinet/in.h> | |
50 #include <netinet/tcp.h> | |
51 #include <netdb.h> | |
52 #include <sys/socket.h> | |
42 | 53 #include <sys/un.h> |
0 | 54 |
55 // needed for thread | |
56 #include <pthread.h> | |
57 | |
58 // needed for std c++ collections | |
59 #include <set> | |
60 #include <map> | |
61 #include <list> | |
62 | |
63 // for the dns resolver | |
64 #include <netinet/in.h> | |
65 #include <arpa/nameser.h> | |
66 #include <resolv.h> | |
67 | |
68 // misc stuff needed here | |
69 #include <ctype.h> | |
70 #include <fstream> | |
71 #include <syslog.h> | |
42 | 72 #include <pwd.h> |
0 | 73 |
8 | 74 static char* dnsbl_version="$Id$"; |
0 | 75 |
8 | 76 #define DEFAULT "default" |
77 #define WHITE "white" | |
78 #define BLACK "black" | |
79 #define OK "ok" | |
80 #define MANY "many" | |
81 | |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
82 enum status {oksofar, // not rejected yet |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
83 white, // whitelisted by envelope from |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
84 black, // blacklisted by envelope from or to |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
85 reject, // rejected by a dns list |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
86 reject_tag, // too many bad html tags |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
87 reject_host}; // too many hosts/urls in body |
1 | 88 |
0 | 89 using namespace std; |
90 | |
91 extern "C" { | |
92 #include "libmilter/mfapi.h" | |
93 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr); | |
94 sfsistat mlfi_envfrom(SMFICTX *ctx, char **argv); | |
95 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **argv); | |
8 | 96 sfsistat mlfi_body(SMFICTX *ctx, u_char *data, size_t len); |
97 sfsistat mlfi_eom(SMFICTX *ctx); | |
98 sfsistat mlfi_abort(SMFICTX *ctx); | |
0 | 99 sfsistat mlfi_close(SMFICTX *ctx); |
100 } | |
101 | |
102 struct ltstr { | |
103 bool operator()(char* s1, char* s2) const { | |
104 return strcmp(s1, s2) < 0; | |
105 } | |
106 }; | |
107 | |
108 struct DNSBL { | |
109 char *suffix; // blacklist suffix like blackholes.five-ten-sg.com | |
110 char *message; // error message with one or two %s operators for the ip address replacement | |
111 DNSBL(char *s, char *m); | |
112 }; | |
113 DNSBL::DNSBL(char *s, char *m) { | |
114 suffix = s; | |
115 message = m; | |
116 } | |
117 | |
118 typedef DNSBL * DNSBLP; | |
119 typedef list<DNSBLP> DNSBLL; | |
120 typedef DNSBLL * DNSBLLP; | |
121 typedef map<char *, char *, ltstr> string_map; | |
122 typedef map<char *, string_map *, ltstr> from_map; | |
123 typedef map<char *, DNSBLP, ltstr> dnsblp_map; | |
124 typedef map<char *, DNSBLLP, ltstr> dnsbllp_map; | |
125 typedef set<char *, ltstr> string_set; | |
44 | 126 typedef set<int> int_set; |
0 | 127 typedef list<char *> string_list; |
53
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
128 typedef map<char *, int, ltstr> ns_mapper; |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
129 |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
130 struct ns_map { |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
131 // all the strings are owned by the keys/values in the ns_host string map |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
132 string_map ns_host; // nameserver name -> host name that uses this name server |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
133 ns_mapper ns_ip; // nameserver name -> ip address of the name server |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
134 }; |
0 | 135 |
136 struct CONFIG { | |
137 // the only mutable stuff once it has been loaded from the config file | |
138 int reference_count; // protected by the global config_mutex | |
139 // all the rest is constant after loading from the config file | |
29
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
140 int generation; |
0 | 141 time_t load_time; |
142 string_list config_files; | |
143 dnsblp_map dnsbls; | |
144 dnsbllp_map dnsblls; | |
145 from_map env_from; | |
146 string_map env_to_dnsbll; // map recipient to a named dnsbll | |
147 string_map env_to_chkfrom; // map recipient to a named from map | |
8 | 148 char * content_suffix; // for sbl url body filtering |
9 | 149 char * content_message; // "" |
57 | 150 string_set content_host_ignore;// hosts to ignore for content sbl checking |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
151 char * host_limit_message; // error message for excessive host names |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
152 int host_limit; // limit on host names |
44 | 153 bool host_random; // pick a random selection of host names rather than error for excessive hosts |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
154 char * tag_limit_message; // error message for excessive bad html tags |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
155 int tag_limit; // limit on bad html tags |
24 | 156 string_set html_tags; // set of valid html tags |
28 | 157 string_set tlds; // set of valid tld components |
0 | 158 CONFIG(); |
159 ~CONFIG(); | |
160 }; | |
161 CONFIG::CONFIG() { | |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
162 reference_count = 0; |
29
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
163 generation = 0; |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
164 load_time = 0; |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
165 content_suffix = NULL; |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
166 content_message = NULL; |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
167 host_limit_message = NULL; |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
168 host_limit = 0; |
44 | 169 host_random = false; |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
170 tag_limit_message = NULL; |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
171 tag_limit = 0; |
0 | 172 } |
173 CONFIG::~CONFIG() { | |
174 for (dnsblp_map::iterator i=dnsbls.begin(); i!=dnsbls.end(); i++) { | |
175 DNSBLP d = (*i).second; | |
24 | 176 // delete the underlying DNSBL objects. |
0 | 177 delete d; |
178 } | |
179 for (dnsbllp_map::iterator i=dnsblls.begin(); i!=dnsblls.end(); i++) { | |
180 DNSBLLP d = (*i).second; | |
24 | 181 // *d is a list of pointers to DNSBL objects, but |
182 // the underlying objects have already been deleted above. | |
0 | 183 delete d; |
184 } | |
185 for (from_map::iterator i=env_from.begin(); i!=env_from.end(); i++) { | |
186 string_map *d = (*i).second; | |
187 delete d; | |
188 } | |
189 } | |
190 | |
16 | 191 static bool debug_syslog = false; |
18 | 192 static bool loader_run = true; // used to stop the config loader thread |
0 | 193 static string_set all_strings; // owns all the strings, only modified by the config loader thread |
194 static CONFIG * config = NULL; // protected by the config_mutex | |
29
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
195 static int generation = 0; // protected by the config_mutex |
0 | 196 |
197 static pthread_mutex_t config_mutex; | |
198 static pthread_mutex_t syslog_mutex; | |
199 static pthread_mutex_t resolve_mutex; | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
200 static pthread_mutex_t fd_pool_mutex; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
201 static std::set<int> fd_pool; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
202 |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
203 static int NULL_SOCKET = -1; |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
204 static char *resolver_port = NULL; // unix domain socket to talk to the dns resolver process |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
205 static int resolver_socket = NULL_SOCKET; // socket used to listen for resolver requests |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
206 static time_t ERROR_SOCKET_TIME = 60; // number of seconds between attempts to open the spam filter socket |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
207 static time_t last_error_time; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
208 |
61
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
209 |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
210 // packed structure to allow a single socket write to dump the |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
211 // length and the following answer. The packing attribute is gcc specific. |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
212 struct glommer { |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
213 int length; |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
214 #ifdef NS_PACKETSZ |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
215 u_char answer[NS_PACKETSZ]; // with a resolver, we return resolver answers |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
216 #else |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
217 int answer; // without a resolver, we return a single ip4 address, 0 == no answer |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
218 #endif |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
219 } __attribute__ ((packed)); |
0 | 220 |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
221 struct mlfiPriv; |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
222 |
0 | 223 |
224 //////////////////////////////////////////////// | |
34 | 225 // helper to discard the strings and objects held by an ns_map |
226 // | |
227 static void discard(ns_map &s); | |
228 static void discard(ns_map &s) { | |
53
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
229 for (string_map::iterator i=s.ns_host.begin(); i!=s.ns_host.end(); i++) { |
34 | 230 char *x = (*i).first; |
53
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
231 char *y = (*i).second; |
34 | 232 free(x); |
53
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
233 free(y); |
34 | 234 } |
53
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
235 s.ns_ip.clear(); |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
236 s.ns_host.clear(); |
34 | 237 } |
238 | |
239 //////////////////////////////////////////////// | |
240 // helper to register a string in an ns_map | |
241 // | |
53
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
242 static void register_string(ns_map &s, char *name, char *refer); |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
243 static void register_string(ns_map &s, char *name, char *refer) { |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
244 string_map::iterator i = s.ns_host.find(name); |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
245 if (i != s.ns_host.end()) return; |
34 | 246 char *x = strdup(name); |
53
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
247 char *y = strdup(refer); |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
248 s.ns_ip[x] = 0; |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
249 s.ns_host[x] = y; |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
250 |
34 | 251 } |
252 | |
253 //////////////////////////////////////////////// | |
8 | 254 // helper to discard the strings held by a string_set |
0 | 255 // |
9 | 256 static void discard(string_set &s); |
257 static void discard(string_set &s) { | |
8 | 258 for (string_set::iterator i=s.begin(); i!=s.end(); i++) { |
259 free(*i); | |
260 } | |
9 | 261 s.clear(); |
8 | 262 } |
0 | 263 |
12 | 264 //////////////////////////////////////////////// |
265 // helper to register a string in a string set | |
266 // | |
267 static char* register_string(string_set &s, char *name); | |
268 static char* register_string(string_set &s, char *name) { | |
269 string_set::iterator i = s.find(name); | |
270 if (i != s.end()) return *i; | |
271 char *x = strdup(name); | |
272 s.insert(x); | |
273 return x; | |
274 } | |
275 | |
16 | 276 //////////////////////////////////////////////// |
277 // syslog a message | |
278 // | |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
279 static void my_syslog(mlfiPriv *priv, char *text); |
16 | 280 |
281 | |
12 | 282 // include the content scanner |
283 #include "scanner.cpp" | |
284 | |
285 | |
0 | 286 //////////////////////////////////////////////// |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
287 // disconnect the fd from the dns resolver process |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
288 // |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
289 void my_disconnect(int sock); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
290 void my_disconnect(int sock) |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
291 { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
292 if (sock != NULL_SOCKET) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
293 shutdown(sock, SHUT_RDWR); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
294 close(sock); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
295 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
296 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
297 |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
298 |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
299 //////////////////////////////////////////////// |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
300 // return fd connected to the dns resolver process |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
301 // |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
302 int my_connect(); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
303 int my_connect() |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
304 { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
305 // if we have had recent errors, don't even try to open the socket |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
306 time_t now = time(NULL); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
307 if ((now - last_error_time) < ERROR_SOCKET_TIME) return NULL_SOCKET; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
308 |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
309 // nothing recent, maybe this time it will work |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
310 int sock = NULL_SOCKET; |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
311 sockaddr_un server; |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
312 memset(&server, '\0', sizeof(server)); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
313 server.sun_family = AF_UNIX; |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
314 strncpy(server.sun_path, resolver_port, sizeof(server.sun_path)-1); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
315 sock = socket(AF_UNIX, SOCK_STREAM, 0); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
316 if (sock != NULL_SOCKET) { |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
317 bool rc = (connect(sock, (sockaddr *)&server, sizeof(server)) == 0); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
318 if (!rc) { |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
319 int er = errno; |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
320 my_disconnect(sock); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
321 sock = NULL_SOCKET; |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
322 last_error_time = now; |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
323 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
324 } |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
325 else last_error_time = now; |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
326 return sock; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
327 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
328 |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
329 |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
330 //////////////////////////////////////////////// |
0 | 331 // mail filter private data, held for us by sendmail |
332 // | |
333 struct mlfiPriv | |
334 { | |
8 | 335 // connection specific data |
336 CONFIG *pc; // global context with our maps | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
337 int fd; // to talk to dns resolvers process |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
338 bool err; // did we get any errors on the resolver socket? |
8 | 339 int ip; // ip4 address of the smtp client |
340 map<DNSBLP, status> checked; // status from those lists | |
341 // message specific data | |
0 | 342 char *mailaddr; // envelope from value |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
343 char *queueid; // sendmail queue id |
0 | 344 bool authenticated; // client authenticated? if so, suppress all dnsbl checks |
8 | 345 bool have_whites; // have at least one whitelisted recipient? need to accept content and remove all non-whitelisted recipients if it fails |
346 bool only_whites; // every recipient is whitelisted? | |
24 | 347 string_set non_whites; // remember the non-whitelisted recipients so we can remove them if need be |
348 recorder *memory; // memory for the content scanner | |
8 | 349 url_scanner *scanner; // object to handle body scanning |
0 | 350 mlfiPriv(); |
351 ~mlfiPriv(); | |
8 | 352 void reset(bool final = false); // for a new message |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
353 void get_fd(); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
354 void return_fd(); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
355 int my_read(char *buf, int len); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
356 int my_write(char *buf, int len); |
0 | 357 }; |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
358 |
0 | 359 mlfiPriv::mlfiPriv() { |
360 pthread_mutex_lock(&config_mutex); | |
361 pc = config; | |
362 pc->reference_count++; | |
363 pthread_mutex_unlock(&config_mutex); | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
364 get_fd(); |
8 | 365 ip = 0; |
366 mailaddr = NULL; | |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
367 queueid = NULL; |
8 | 368 authenticated = false; |
369 have_whites = false; | |
370 only_whites = true; | |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
371 memory = new recorder(this, &pc->html_tags, &pc->tlds); |
24 | 372 scanner = new url_scanner(memory); |
0 | 373 } |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
374 |
0 | 375 mlfiPriv::~mlfiPriv() { |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
376 return_fd(); |
0 | 377 pthread_mutex_lock(&config_mutex); |
378 pc->reference_count--; | |
379 pthread_mutex_unlock(&config_mutex); | |
8 | 380 reset(true); |
381 } | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
382 |
8 | 383 void mlfiPriv::reset(bool final) { |
0 | 384 if (mailaddr) free(mailaddr); |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
385 if (queueid) free(queueid); |
24 | 386 discard(non_whites); |
387 delete memory; | |
8 | 388 delete scanner; |
389 if (!final) { | |
390 mailaddr = NULL; | |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
391 queueid = NULL; |
8 | 392 authenticated = false; |
393 have_whites = false; | |
394 only_whites = true; | |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
395 memory = new recorder(this, &pc->html_tags, &pc->tlds); |
24 | 396 scanner = new url_scanner(memory); |
8 | 397 } |
0 | 398 } |
399 | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
400 void mlfiPriv::get_fd() |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
401 { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
402 err = true; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
403 fd = NULL_SOCKET; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
404 int result = pthread_mutex_lock(&fd_pool_mutex); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
405 if (!result) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
406 std::set<int>::iterator i; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
407 i = fd_pool.begin(); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
408 if (i != fd_pool.end()) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
409 // have at least one fd in the pool |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
410 err = false; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
411 fd = *i; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
412 fd_pool.erase(fd); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
413 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
414 else { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
415 // pool is empty, get a new fd |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
416 fd = my_connect(); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
417 err = (fd == NULL_SOCKET); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
418 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
419 pthread_mutex_unlock(&fd_pool_mutex); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
420 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
421 else { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
422 // cannot lock the pool, just get a new fd |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
423 fd = my_connect(); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
424 err = (fd == NULL_SOCKET); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
425 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
426 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
427 |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
428 void mlfiPriv::return_fd() |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
429 { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
430 if (err) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
431 // this fd got a socket error, so close it, rather than returning it to the pool |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
432 my_disconnect(fd); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
433 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
434 else { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
435 int result = pthread_mutex_lock(&fd_pool_mutex); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
436 if (!result) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
437 // return the fd to the pool |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
438 fd_pool.insert(fd); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
439 pthread_mutex_unlock(&fd_pool_mutex); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
440 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
441 else { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
442 // could not lock the pool, so just close the fd |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
443 my_disconnect(fd); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
444 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
445 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
446 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
447 |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
448 int mlfiPriv::my_write(char *buf, int len) |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
449 { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
450 if (err) return 0; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
451 int rs = 0; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
452 while (len) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
453 int ws = write(fd, buf, len); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
454 if (ws > 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
455 rs += ws; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
456 len -= ws; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
457 buf += ws; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
458 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
459 else { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
460 // peer closed the socket! |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
461 rs = 0; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
462 err = true; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
463 break; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
464 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
465 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
466 return rs; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
467 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
468 |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
469 int mlfiPriv::my_read(char *buf, int len) |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
470 { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
471 if (err) return 0; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
472 int rs = 0; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
473 while (len > 1) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
474 int ws = read(fd, buf, len); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
475 if (ws > 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
476 rs += ws; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
477 len -= ws; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
478 buf += ws; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
479 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
480 else { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
481 // peer closed the socket! |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
482 rs = 0; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
483 err = true; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
484 break; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
485 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
486 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
487 return rs; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
488 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
489 |
0 | 490 #define MLFIPRIV ((struct mlfiPriv *) smfi_getpriv(ctx)) |
491 | |
492 | |
493 //////////////////////////////////////////////// | |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
494 // syslog a message |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
495 // |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
496 static void my_syslog(mlfiPriv *priv, char *text) { |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
497 char buf[1000]; |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
498 if (priv) { |
42 | 499 snprintf(buf, sizeof(buf), "%s: %s", priv->queueid, text); |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
500 text = buf; |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
501 } |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
502 pthread_mutex_lock(&syslog_mutex); |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
503 openlog("dnsbl", LOG_PID, LOG_MAIL); |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
504 syslog(LOG_NOTICE, "%s", text); |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
505 closelog(); |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
506 pthread_mutex_unlock(&syslog_mutex); |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
507 } |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
508 |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
509 static void my_syslog(char *text); |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
510 static void my_syslog(char *text) { |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
511 my_syslog(NULL, text); |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
512 } |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
513 |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
514 //////////////////////////////////////////////// |
0 | 515 // register a global string |
516 // | |
517 static char* register_string(char *name); | |
518 static char* register_string(char *name) { | |
12 | 519 return register_string(all_strings, name); |
0 | 520 } |
521 | |
522 | |
523 static char* next_token(char *delim); | |
524 static char* next_token(char *delim) { | |
525 char *name = strtok(NULL, delim); | |
526 if (!name) return name; | |
527 return register_string(name); | |
528 } | |
529 | |
530 | |
531 //////////////////////////////////////////////// | |
532 // lookup an email address in the env_from or env_to maps | |
533 // | |
534 static char* lookup1(char *email, string_map map); | |
535 static char* lookup1(char *email, string_map map) { | |
536 string_map::iterator i = map.find(email); | |
537 if (i != map.end()) return (*i).second; | |
538 char *x = strchr(email, '@'); | |
539 if (!x) return DEFAULT; | |
540 x++; | |
541 i = map.find(x); | |
542 if (i != map.end()) return (*i).second; | |
543 return DEFAULT; | |
544 } | |
545 | |
546 | |
547 //////////////////////////////////////////////// | |
548 // lookup an email address in the env_from or env_to maps | |
549 // this email address is passed in from sendmail, and will | |
550 // always be enclosed in <>. It may have mixed case, just | |
551 // as the mail client sent it. | |
552 // | |
553 static char* lookup(char* email, string_map map); | |
554 static char* lookup(char* email, string_map map) { | |
555 int n = strlen(email)-2; | |
556 if (n < 1) return DEFAULT; // malformed | |
557 char *key = strdup(email+1); | |
558 key[n] = '\0'; | |
559 for (int i=0; i<n; i++) key[i] = tolower(key[i]); | |
560 char *rc = lookup1(key, map); | |
561 free(key); | |
562 return rc; | |
563 } | |
564 | |
565 | |
566 //////////////////////////////////////////////// | |
567 // find the dnsbl with a specific name | |
568 // | |
569 static DNSBLP find_dnsbl(CONFIG &dc, char *name); | |
570 static DNSBLP find_dnsbl(CONFIG &dc, char *name) { | |
571 dnsblp_map::iterator i = dc.dnsbls.find(name); | |
572 if (i == dc.dnsbls.end()) return NULL; | |
573 return (*i).second; | |
574 } | |
575 | |
576 | |
577 //////////////////////////////////////////////// | |
578 // find the dnsbll with a specific name | |
579 // | |
580 static DNSBLLP find_dnsbll(CONFIG &dc, char *name); | |
581 static DNSBLLP find_dnsbll(CONFIG &dc, char *name) { | |
582 dnsbllp_map::iterator i = dc.dnsblls.find(name); | |
583 if (i == dc.dnsblls.end()) return NULL; | |
584 return (*i).second; | |
585 } | |
586 | |
587 | |
588 //////////////////////////////////////////////// | |
589 // find the envfrom map with a specific name | |
590 // | |
591 static string_map* find_from_map(CONFIG &dc, char *name); | |
592 static string_map* find_from_map(CONFIG &dc, char *name) { | |
593 from_map::iterator i = dc.env_from.find(name); | |
594 if (i == dc.env_from.end()) return NULL; | |
595 return (*i).second; | |
596 } | |
597 | |
598 | |
599 static string_map& really_find_from_map(CONFIG &dc, char *name); | |
600 static string_map& really_find_from_map(CONFIG &dc, char *name) { | |
601 string_map *sm = find_from_map(dc, name); | |
602 if (!sm) { | |
603 sm = new string_map; | |
604 dc.env_from[name] = sm; | |
605 } | |
606 return *sm; | |
607 } | |
608 | |
609 | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
610 |
0 | 611 //////////////////////////////////////////////// |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
612 // read a resolver request from the socket, process it, and |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
613 // write the result back to the socket. |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
614 |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
615 static void process_resolver_requests(int socket); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
616 static void process_resolver_requests(int socket) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
617 #ifdef NS_MAXDNAME |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
618 char question[NS_MAXDNAME]; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
619 #else |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
620 char question[1000]; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
621 #endif |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
622 glommer glom; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
623 |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
624 int maxq = sizeof(question); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
625 while (true) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
626 // read a question |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
627 int rs = 0; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
628 while (true) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
629 int ns = read(socket, question+rs, maxq-rs); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
630 if (ns > 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
631 rs += ns; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
632 if (question[rs-1] == '\0') { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
633 // last byte read was the null terminator, we are done |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
634 break; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
635 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
636 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
637 else { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
638 // peer closed the socket |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
639 //my_syslog("!!child worker process, peer closed socket while reading question"); |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
640 shutdown(socket, SHUT_RDWR); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
641 close(socket); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
642 return; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
643 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
644 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
645 |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
646 // find the answer |
61
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
647 #ifdef NS_PACKETSZ |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
648 //char text[1000]; |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
649 //snprintf(text, sizeof(text), "!!child worker process has a question %s", question); |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
650 //my_syslog(text); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
651 glom.length = res_search(question, ns_c_in, ns_t_a, glom.answer, sizeof(glom.answer)); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
652 if (glom.length < 0) glom.length = 0; // represent all errors as zero length answers |
61
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
653 #else |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
654 glom.length = sizeof(glom.answer); |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
655 glom.answer = 0; |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
656 struct hostent *host = gethostbyname(question); |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
657 if (host && (host->h_addrtype == AF_INET)) { |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
658 memcpy(&glom.answer, host->h_addr, sizeof(glom.answer)); |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
659 } |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
660 #endif |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
661 |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
662 // write the answer |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
663 char *buf = (char *)&glom; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
664 int len = glom.length + sizeof(glom.length); |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
665 //snprintf(text, sizeof(text), "!!child worker process writing answer length %d for total %d", glom.length, len); |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
666 //my_syslog(text); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
667 int ws = 0; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
668 while (len > ws) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
669 int ns = write(socket, buf+ws, len-ws); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
670 if (ns > 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
671 ws += ns; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
672 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
673 else { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
674 // peer closed the socket! |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
675 //my_syslog("!!child worker process, peer closed socket while writing answer"); |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
676 shutdown(socket, SHUT_RDWR); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
677 close(socket); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
678 return; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
679 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
680 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
681 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
682 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
683 |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
684 |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
685 //////////////////////////////////////////////// |
8 | 686 // ask a dns question and get an A record answer - we don't try |
687 // very hard, just using the default resolver retry settings. | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
688 // If we cannot get an answer, we just accept the mail. |
8 | 689 // |
0 | 690 // |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
691 static int dns_interface(mlfiPriv &priv, char *question, bool maybe_ip, ns_map *nameservers); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
692 static int dns_interface(mlfiPriv &priv, char *question, bool maybe_ip, ns_map *nameservers) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
693 // this part can be done without locking the resolver mutex. Each |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
694 // milter thread is talking over its own socket to a separate resolver |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
695 // process, which does the actual dns resolution. |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
696 if (priv.err) return 0; // cannot ask more questions on this socket. |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
697 priv.my_write(question, strlen(question)+1); // write the question including the null terminator |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
698 glommer glom; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
699 char *buf = (char *)&glom; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
700 priv.my_read(buf, sizeof(glom.length)); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
701 buf += sizeof(glom.length); |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
702 ///char text[1000]; |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
703 ///snprintf(text, sizeof(text), "!!milter thread wrote question %s and has answer length %d", question, glom.length); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
704 ///my_syslog(text); |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
705 if ((glom.length < 0) || (glom.length > sizeof(glom.answer))) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
706 priv.err = true; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
707 return 0; // cannot process overlarge answers |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
708 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
709 priv.my_read(buf, glom.length); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
710 |
61
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
711 #ifdef NS_PACKETSZ |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
712 // now we need to lock the resolver mutex to keep the milter threads from |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
713 // stepping on each other while parsing the dns answer. |
61
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
714 int ret_address = 0; |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
715 pthread_mutex_lock(&resolve_mutex); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
716 if (glom.length > 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
717 // parse the answer |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
718 ns_msg handle; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
719 ns_rr rr; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
720 if (ns_initparse(glom.answer, glom.length, &handle) == 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
721 // look for ns names |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
722 if (nameservers) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
723 ns_map &ns = *nameservers; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
724 int rrnum = 0; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
725 while (ns_parserr(&handle, ns_s_ns, rrnum++, &rr) == 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
726 if (ns_rr_type(rr) == ns_t_ns) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
727 char nam[NS_MAXDNAME+1]; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
728 char *n = nam; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
729 const u_char *p = ns_rr_rdata(rr); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
730 while (((n-nam) < NS_MAXDNAME) && ((p-glom.answer) < glom.length) && *p) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
731 size_t s = *(p++); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
732 if (s > 191) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
733 // compression pointer |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
734 s = (s-192)*256 + *(p++); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
735 if (s >= glom.length) break; // pointer outside bounds of answer |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
736 p = glom.answer + s; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
737 s = *(p++); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
738 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
739 if (s > 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
740 if ((n-nam) >= (NS_MAXDNAME-s)) break; // destination would overflow name buffer |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
741 if ((p-glom.answer) >= (glom.length-s)) break; // source outside bounds of answer |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
742 memcpy(n, p, s); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
743 n += s; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
744 p += s; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
745 *(n++) = '.'; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
746 } |
34 | 747 } |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
748 if (n-nam) n--; // remove trailing . |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
749 *n = '\0'; // null terminate it |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
750 register_string(ns, nam, question); // ns host to lookup later |
34 | 751 } |
752 } | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
753 rrnum = 0; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
754 while (ns_parserr(&handle, ns_s_ar, rrnum++, &rr) == 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
755 if (ns_rr_type(rr) == ns_t_a) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
756 char* nam = (char*)ns_rr_name(rr); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
757 ns_mapper::iterator i = ns.ns_ip.find(nam); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
758 if (i != ns.ns_ip.end()) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
759 // we want this ip address |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
760 int address; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
761 memcpy(&address, ns_rr_rdata(rr), sizeof(address)); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
762 ns.ns_ip[nam] = address; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
763 } |
34 | 764 } |
765 } | |
766 } | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
767 int rrnum = 0; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
768 while (ns_parserr(&handle, ns_s_an, rrnum++, &rr) == 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
769 if (ns_rr_type(rr) == ns_t_a) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
770 int address; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
771 memcpy(&address, ns_rr_rdata(rr), sizeof(address)); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
772 ret_address = address; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
773 } |
23
06de5ab6a232
add url decoding stage, allow http:/ single / in yahoo redirector, allow ip address hostnames
carl
parents:
22
diff
changeset
|
774 } |
06de5ab6a232
add url decoding stage, allow http:/ single / in yahoo redirector, allow ip address hostnames
carl
parents:
22
diff
changeset
|
775 } |
06de5ab6a232
add url decoding stage, allow http:/ single / in yahoo redirector, allow ip address hostnames
carl
parents:
22
diff
changeset
|
776 } |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
777 if (maybe_ip && !ret_address) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
778 // might be a bare ip address |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
779 in_addr ip; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
780 if (inet_aton(question, &ip)) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
781 ret_address = ip.s_addr; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
782 } |
8 | 783 } |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
784 pthread_mutex_unlock(&resolve_mutex); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
785 return ret_address; |
16 | 786 #else |
61
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
787 return glom.answer; |
16 | 788 #endif |
8 | 789 } |
790 | |
791 | |
792 //////////////////////////////////////////////// | |
793 // check a single dnsbl | |
794 // | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
795 static status check_single(mlfiPriv &priv, int ip, char *suffix); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
796 static status check_single(mlfiPriv &priv, int ip, char *suffix) { |
0 | 797 // make a dns question |
798 const u_char *src = (const u_char *)&ip; | |
799 if (src[0] == 127) return oksofar; // don't do dns lookups on localhost | |
16 | 800 #ifdef NS_MAXDNAME |
0 | 801 char question[NS_MAXDNAME]; |
16 | 802 #else |
803 char question[1000]; | |
804 #endif | |
8 | 805 snprintf(question, sizeof(question), "%u.%u.%u.%u.%s.", src[3], src[2], src[1], src[0], suffix); |
806 // ask the question, if we get an A record it implies a blacklisted ip address | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
807 return (dns_interface(priv, question, false, NULL)) ? reject : oksofar; |
8 | 808 } |
809 | |
810 | |
811 //////////////////////////////////////////////// | |
812 // check a single dnsbl | |
813 // | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
814 static status check_single(mlfiPriv &priv, int ip, DNSBL &bl); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
815 static status check_single(mlfiPriv &priv, int ip, DNSBL &bl) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
816 return check_single(priv, ip, bl.suffix); |
0 | 817 } |
818 | |
819 | |
820 //////////////////////////////////////////////// | |
821 // check the dnsbls specified for this recipient | |
822 // | |
823 static status check_dnsbl(mlfiPriv &priv, DNSBLLP dnsbllp, DNSBLP &rejectlist); | |
824 static status check_dnsbl(mlfiPriv &priv, DNSBLLP dnsbllp, DNSBLP &rejectlist) { | |
825 if (priv.authenticated) return oksofar; | |
826 if (!dnsbllp) return oksofar; | |
827 DNSBLL &dnsbll = *dnsbllp; | |
828 for (DNSBLL::iterator i=dnsbll.begin(); i!=dnsbll.end(); i++) { | |
829 DNSBLP dp = *i; // non null by construction | |
830 status st; | |
831 map<DNSBLP, status>::iterator f = priv.checked.find(dp); | |
832 if (f == priv.checked.end()) { | |
833 // have not checked this list yet | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
834 st = check_single(priv, priv.ip, *dp); |
0 | 835 rejectlist = dp; |
836 priv.checked[dp] = st; | |
837 } | |
838 else { | |
839 st = (*f).second; | |
840 rejectlist = (*f).first; | |
841 } | |
842 if (st == reject) return st; | |
843 } | |
844 return oksofar; | |
845 } | |
846 | |
847 | |
848 //////////////////////////////////////////////// | |
55
44babba1a9b9
updates for 3.6, better documentation on removing content filtering, missing some files in cvs
carl
parents:
53
diff
changeset
|
849 // check the hosts from the body against the content dnsbl |
8 | 850 // |
16 | 851 static status check_hosts(mlfiPriv &priv, char *&host, int &ip); |
852 static status check_hosts(mlfiPriv &priv, char *&host, int &ip) { | |
55
44babba1a9b9
updates for 3.6, better documentation on removing content filtering, missing some files in cvs
carl
parents:
53
diff
changeset
|
853 CONFIG &dc = *priv.pc; |
8 | 854 int count = 0; |
34 | 855 ns_map nameservers; |
44 | 856 bool ran = priv.pc->host_random; |
857 int lim = priv.pc->host_limit; // we should not look at more than this many hosts | |
858 int cnt = priv.memory->hosts.size(); // number of hosts we could look at | |
859 int_set ips; // remove duplicate ip addresses | |
24 | 860 for (string_set::iterator i=priv.memory->hosts.begin(); i!=priv.memory->hosts.end(); i++) { |
44 | 861 host = *i; // a reference into priv.memory->hosts, which will live until this smtp transaction is closed |
58 | 862 |
863 // don't bother looking up hosts on the ignore list | |
57 | 864 string_set::iterator j = priv.pc->content_host_ignore.find(host); |
58 | 865 if (j != priv.pc->content_host_ignore.end()) continue; |
866 | |
867 // try to only look at lim/cnt fraction of the available cnt host names in random mode | |
44 | 868 if ((cnt > lim) && (lim > 0) && ran) { |
869 int r = rand() % cnt; | |
870 if (r >= lim) { | |
871 char buf[1000]; | |
872 snprintf(buf, sizeof(buf), "host %s skipped", host); | |
873 my_syslog(&priv, buf); | |
874 continue; | |
875 } | |
876 } | |
8 | 877 count++; |
44 | 878 if ((count > lim) && (lim > 0) && (!ran)) { |
34 | 879 discard(nameservers); |
880 return reject_host; | |
881 } | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
882 ip = dns_interface(priv, host, true, &nameservers); |
16 | 883 if (debug_syslog) { |
44 | 884 char buf[1000]; |
29
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
885 if (ip) { |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
886 char adr[sizeof "255.255.255.255"]; |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
887 adr[0] = '\0'; |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
888 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr)); |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
889 snprintf(buf, sizeof(buf), "host %s found at %s", host, adr); |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
890 } |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
891 else { |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
892 snprintf(buf, sizeof(buf), "host %s not found", host); |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
893 } |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
894 my_syslog(&priv, buf); |
16 | 895 } |
8 | 896 if (ip) { |
44 | 897 int_set::iterator i = ips.find(ip); |
898 if (i == ips.end()) { | |
899 ips.insert(ip); | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
900 status st = check_single(priv, ip, dc.content_suffix); |
44 | 901 if (st == reject) { |
902 discard(nameservers); | |
903 return st; | |
904 } | |
34 | 905 } |
8 | 906 } |
907 } | |
34 | 908 lim *= 4; // allow average of 3 ns per host name |
53
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
909 for (ns_mapper::iterator i=nameservers.ns_ip.begin(); i!=nameservers.ns_ip.end(); i++) { |
34 | 910 count++; |
911 if ((count > lim) && (lim > 0)) { | |
44 | 912 if (ran) continue; // don't complain |
34 | 913 discard(nameservers); |
914 return reject_host; | |
915 } | |
36 | 916 host = (*i).first; // a transient reference that needs to be replaced before we return it |
34 | 917 ip = (*i).second; |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
918 if (!ip) ip = dns_interface(priv, host, false, NULL); |
34 | 919 if (debug_syslog) { |
920 char buf[200]; | |
921 if (ip) { | |
922 char adr[sizeof "255.255.255.255"]; | |
923 adr[0] = '\0'; | |
924 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr)); | |
925 snprintf(buf, sizeof(buf), "ns %s found at %s", host, adr); | |
926 } | |
927 else { | |
928 snprintf(buf, sizeof(buf), "ns %s not found", host); | |
929 } | |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
930 my_syslog(&priv, buf); |
34 | 931 } |
932 if (ip) { | |
44 | 933 int_set::iterator i = ips.find(ip); |
934 if (i == ips.end()) { | |
935 ips.insert(ip); | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
936 status st = check_single(priv, ip, dc.content_suffix); |
44 | 937 if (st == reject) { |
53
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
938 string_map::iterator j = nameservers.ns_host.find(host); |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
939 if (j != nameservers.ns_host.end()) { |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
940 char *refer = (*j).second; |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
941 char buf[1000]; |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
942 snprintf(buf, sizeof(buf), "%s with nameserver %s", refer, host); |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
943 host = register_string(priv.memory->hosts, buf); // put a copy into priv.memory->hosts, and return that reference |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
944 } |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
945 else { |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
946 host = register_string(priv.memory->hosts, host); // put a copy into priv.memory->hosts, and return that reference |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
947 } |
44 | 948 discard(nameservers); |
949 return st; | |
950 } | |
34 | 951 } |
952 } | |
953 } | |
954 discard(nameservers); | |
24 | 955 host = NULL; |
26 | 956 int bin = priv.memory->binary_tags; |
24 | 957 int bad = priv.memory->bad_html_tags; |
34 | 958 lim = priv.pc->tag_limit; |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
959 if (3*bin > bad) return oksofar; // probably .zip or .tar.gz with random content |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
960 if ((bad > lim) && (lim > 0)) return reject_tag; |
9 | 961 return oksofar; |
8 | 962 } |
963 | |
964 | |
965 //////////////////////////////////////////////// | |
0 | 966 // start of sendmail milter interfaces |
967 // | |
968 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr) | |
969 { | |
970 // allocate some private memory | |
971 mlfiPriv *priv = new mlfiPriv; | |
972 if (hostaddr->sa_family == AF_INET) { | |
973 priv->ip = ((struct sockaddr_in *)hostaddr)->sin_addr.s_addr; | |
974 } | |
975 | |
976 // save the private data | |
977 smfi_setpriv(ctx, (void*)priv); | |
978 | |
979 // continue processing | |
980 return SMFIS_CONTINUE; | |
981 } | |
982 | |
983 sfsistat mlfi_envfrom(SMFICTX *ctx, char **from) | |
984 { | |
985 mlfiPriv &priv = *MLFIPRIV; | |
986 priv.mailaddr = strdup(from[0]); | |
987 priv.authenticated = (smfi_getsymval(ctx, "{auth_authen}") != NULL); | |
988 return SMFIS_CONTINUE; | |
989 } | |
990 | |
991 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **rcpt) | |
992 { | |
993 DNSBLP rejectlist = NULL; // list that caused the reject | |
994 status st = oksofar; | |
995 mlfiPriv &priv = *MLFIPRIV; | |
996 CONFIG &dc = *priv.pc; | |
42 | 997 if (!priv.queueid) priv.queueid = strdup(smfi_getsymval(ctx, "i")); |
0 | 998 char *rcptaddr = rcpt[0]; |
999 char *dnsname = lookup(rcptaddr, dc.env_to_dnsbll); | |
1000 char *fromname = lookup(rcptaddr, dc.env_to_chkfrom); | |
1001 if ((strcmp(dnsname, BLACK) == 0) || | |
1002 (strcmp(fromname, BLACK) == 0)) { | |
1003 st = black; // two options to blacklist this recipient | |
1004 } | |
1005 else if (strcmp(fromname, WHITE) == 0) { | |
1006 st = white; | |
1007 } | |
1008 else { | |
1009 // check an env_from map | |
1010 string_map *sm = find_from_map(dc, fromname); | |
1011 if (sm != NULL) { | |
1012 fromname = lookup(priv.mailaddr, *sm); // returns default if name not in map | |
1013 if (strcmp(fromname, BLACK) == 0) { | |
1014 st = black; // blacklist this envelope from value | |
1015 } | |
1016 if (strcmp(fromname, WHITE) == 0) { | |
1017 st = white; // blacklist this envelope from value | |
1018 } | |
1019 } | |
1020 } | |
1021 if ((st == oksofar) && (strcmp(dnsname, WHITE) != 0)) { | |
1022 // check dns lists | |
1023 st = check_dnsbl(priv, find_dnsbll(dc, dnsname), rejectlist); | |
1024 } | |
1025 | |
1026 if (st == reject) { | |
1027 // reject the recipient based on some dnsbl | |
1028 char adr[sizeof "255.255.255.255"]; | |
1029 adr[0] = '\0'; | |
8 | 1030 inet_ntop(AF_INET, (const u_char *)&priv.ip, adr, sizeof(adr)); |
0 | 1031 char buf[2000]; |
1032 snprintf(buf, sizeof(buf), rejectlist->message, adr, adr); | |
1033 smfi_setreply(ctx, "550", "5.7.1", buf); | |
1034 return SMFIS_REJECT; | |
1035 } | |
1036 else if (st == black) { | |
1037 // reject the recipient based on blacklisting either from or to | |
1038 smfi_setreply(ctx, "550", "5.7.1", "no such user"); | |
1039 return SMFIS_REJECT; | |
1040 } | |
1041 else { | |
1042 // accept the recipient | |
8 | 1043 if (st == oksofar) { |
1044 // but remember the non-whites | |
12 | 1045 register_string(priv.non_whites, rcptaddr); |
8 | 1046 priv.only_whites = false; |
1047 } | |
1048 if (st == white) { | |
1049 priv.have_whites = true; | |
1050 } | |
0 | 1051 return SMFIS_CONTINUE; |
1052 } | |
1053 } | |
1054 | |
8 | 1055 sfsistat mlfi_body(SMFICTX *ctx, u_char *data, size_t len) |
0 | 1056 { |
1057 mlfiPriv &priv = *MLFIPRIV; | |
55
44babba1a9b9
updates for 3.6, better documentation on removing content filtering, missing some files in cvs
carl
parents:
53
diff
changeset
|
1058 if (priv.authenticated) return SMFIS_CONTINUE; |
44babba1a9b9
updates for 3.6, better documentation on removing content filtering, missing some files in cvs
carl
parents:
53
diff
changeset
|
1059 if (priv.only_whites) return SMFIS_CONTINUE; |
44babba1a9b9
updates for 3.6, better documentation on removing content filtering, missing some files in cvs
carl
parents:
53
diff
changeset
|
1060 if (!priv.pc->content_suffix) return SMFIS_CONTINUE; |
8 | 1061 priv.scanner->scan(data, len); |
11 | 1062 return SMFIS_CONTINUE; |
8 | 1063 } |
1064 | |
1065 sfsistat mlfi_eom(SMFICTX *ctx) | |
1066 { | |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1067 sfsistat rc; |
8 | 1068 mlfiPriv &priv = *MLFIPRIV; |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1069 char *host = NULL; |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1070 int ip; |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1071 status st; |
8 | 1072 // process end of message |
55
44babba1a9b9
updates for 3.6, better documentation on removing content filtering, missing some files in cvs
carl
parents:
53
diff
changeset
|
1073 if (priv.authenticated || |
44babba1a9b9
updates for 3.6, better documentation on removing content filtering, missing some files in cvs
carl
parents:
53
diff
changeset
|
1074 priv.only_whites || |
44babba1a9b9
updates for 3.6, better documentation on removing content filtering, missing some files in cvs
carl
parents:
53
diff
changeset
|
1075 (!priv.pc->content_suffix) || |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1076 ((st=check_hosts(priv, host, ip)) == oksofar)) rc = SMFIS_CONTINUE; |
8 | 1077 else { |
1078 if (!priv.have_whites) { | |
1079 // can reject the entire message | |
1080 char buf[2000]; | |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1081 if (st == reject_tag) { |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1082 // rejected due to excessive bad html tags |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1083 snprintf(buf, sizeof(buf), priv.pc->tag_limit_message); |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1084 } |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1085 else if (st == reject_host) { |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1086 // rejected due to excessive unique host/urls |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1087 snprintf(buf, sizeof(buf), priv.pc->host_limit_message); |
24 | 1088 } |
1089 else { | |
1090 char adr[sizeof "255.255.255.255"]; | |
1091 adr[0] = '\0'; | |
1092 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr)); | |
1093 snprintf(buf, sizeof(buf), priv.pc->content_message, host, adr); | |
1094 } | |
8 | 1095 smfi_setreply(ctx, "550", "5.7.1", buf); |
1096 rc = SMFIS_REJECT; | |
1097 } | |
1098 else { | |
1099 // need to accept it but remove the recipients that don't want it | |
1100 for (string_set::iterator i=priv.non_whites.begin(); i!=priv.non_whites.end(); i++) { | |
1101 char *rcpt = *i; | |
1102 smfi_delrcpt(ctx, rcpt); | |
1103 } | |
1104 rc = SMFIS_CONTINUE; | |
1105 } | |
0 | 1106 } |
8 | 1107 // reset for a new message on the same connection |
1108 mlfi_abort(ctx); | |
1109 return rc; | |
1110 } | |
1111 | |
1112 sfsistat mlfi_abort(SMFICTX *ctx) | |
1113 { | |
1114 mlfiPriv &priv = *MLFIPRIV; | |
1115 priv.reset(); | |
0 | 1116 return SMFIS_CONTINUE; |
1117 } | |
1118 | |
1119 sfsistat mlfi_close(SMFICTX *ctx) | |
1120 { | |
1121 mlfiPriv *priv = MLFIPRIV; | |
1122 if (!priv) return SMFIS_CONTINUE; | |
1123 delete priv; | |
1124 smfi_setpriv(ctx, NULL); | |
1125 return SMFIS_CONTINUE; | |
1126 } | |
1127 | |
1128 struct smfiDesc smfilter = | |
1129 { | |
1130 "DNSBL", // filter name | |
1131 SMFI_VERSION, // version code -- do not change | |
1132 SMFIF_DELRCPT, // flags | |
1133 mlfi_connect, // connection info filter | |
1134 NULL, // SMTP HELO command filter | |
1135 mlfi_envfrom, // envelope sender filter | |
1136 mlfi_envrcpt, // envelope recipient filter | |
1137 NULL, // header filter | |
1138 NULL, // end of header | |
8 | 1139 mlfi_body, // body block filter |
1140 mlfi_eom, // end of message | |
1141 mlfi_abort, // message aborted | |
0 | 1142 mlfi_close, // connection cleanup |
1143 }; | |
1144 | |
1145 | |
1146 static void dumpit(char *name, string_map map); | |
1147 static void dumpit(char *name, string_map map) { | |
9 | 1148 fprintf(stdout, "\n"); |
0 | 1149 for (string_map::iterator i=map.begin(); i!=map.end(); i++) { |
9 | 1150 fprintf(stdout, "%s %s->%s\n", name, (*i).first, (*i).second); |
0 | 1151 } |
1152 } | |
1153 | |
1154 | |
1155 static void dumpit(from_map map); | |
1156 static void dumpit(from_map map) { | |
1157 for (from_map::iterator i=map.begin(); i!=map.end(); i++) { | |
3 | 1158 char buf[2000]; |
1159 snprintf(buf, sizeof(buf), "envelope from map for %s", (*i).first); | |
0 | 1160 string_map *sm = (*i).second; |
3 | 1161 dumpit(buf, *sm); |
0 | 1162 } |
1163 } | |
1164 | |
1165 | |
3 | 1166 static void dumpit(CONFIG &dc); |
1167 static void dumpit(CONFIG &dc) { | |
5 | 1168 dumpit(dc.env_from); |
1169 dumpit("envelope to (dnsbl list)", dc.env_to_dnsbll); | |
1170 dumpit("envelope to (from map)", dc.env_to_chkfrom); | |
9 | 1171 fprintf(stdout, "\ndnsbls\n"); |
0 | 1172 for (dnsblp_map::iterator i=dc.dnsbls.begin(); i!=dc.dnsbls.end(); i++) { |
9 | 1173 fprintf(stdout, "%s %s %s\n", (*i).first, (*i).second->suffix, (*i).second->message); |
0 | 1174 } |
9 | 1175 fprintf(stdout, "\ndnsbl_lists\n"); |
0 | 1176 for (dnsbllp_map::iterator i=dc.dnsblls.begin(); i!=dc.dnsblls.end(); i++) { |
1177 char *name = (*i).first; | |
1178 DNSBLL &dl = *((*i).second); | |
9 | 1179 fprintf(stdout, "%s", name); |
0 | 1180 for (DNSBLL::iterator j=dl.begin(); j!=dl.end(); j++) { |
1181 DNSBL &d = **j; | |
9 | 1182 fprintf(stdout, " %s", d.suffix); |
0 | 1183 } |
9 | 1184 fprintf(stdout, "\n"); |
0 | 1185 } |
9 | 1186 if (dc.content_suffix) { |
1187 fprintf(stdout, "\ncontent filtering enabled with %s %s\n", dc.content_suffix, dc.content_message); | |
1188 } | |
57 | 1189 for (string_set::iterator i=dc.content_host_ignore.begin(); i!=dc.content_host_ignore.end(); i++) { |
1190 fprintf(stdout, "ignore %s\n", (*i)); | |
1191 } | |
46 | 1192 if (dc.host_limit && !dc.host_random) { |
1193 fprintf(stdout, "\ncontent filtering for host names hard limit %d %s\n", dc.host_limit, dc.host_limit_message); | |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1194 } |
46 | 1195 if (dc.host_limit && dc.host_random) { |
1196 fprintf(stdout, "\ncontent filtering for host names soft limit %d\n", dc.host_limit); | |
44 | 1197 } |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1198 if (dc.tag_limit) { |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1199 fprintf(stdout, "\ncontent filtering for excessive html tags enabled with limit %d %s\n", dc.tag_limit, dc.tag_limit_message); |
24 | 1200 } |
9 | 1201 fprintf(stdout, "\nfiles\n"); |
3 | 1202 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) { |
1203 char *f = *i; | |
9 | 1204 fprintf(stdout, "config includes %s\n", f); |
3 | 1205 } |
1206 } | |
1207 | |
1208 | |
1209 //////////////////////////////////////////////// | |
1210 // check for redundant or recursive include files | |
1211 // | |
1212 static bool ok_to_include(CONFIG &dc, char *fn); | |
1213 static bool ok_to_include(CONFIG &dc, char *fn) { | |
1214 if (!fn) return false; | |
1215 bool ok = true; | |
1216 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) { | |
1217 char *f = *i; | |
1218 if (strcmp(f, fn) == 0) { | |
1219 my_syslog("redundant or recursive include file detected"); | |
1220 ok = false; | |
1221 break; | |
1222 } | |
1223 } | |
1224 return ok; | |
0 | 1225 } |
1226 | |
1227 | |
1228 //////////////////////////////////////////////// | |
1229 // load a single config file | |
1230 // | |
3 | 1231 static void load_conf_dcc(CONFIG &dc, char *name, char *fn); |
1232 static void load_conf_dcc(CONFIG &dc, char *name, char *fn) { | |
1233 ifstream is(fn); | |
40 | 1234 if (is.fail()) { |
1235 char buf[1000]; | |
1236 snprintf(buf, sizeof(buf), "include file %s not found", fn); | |
1237 my_syslog(buf); | |
1238 return; | |
1239 } | |
39 | 1240 dc.config_files.push_back(fn); |
1241 const int LINE_SIZE = 2000; | |
3 | 1242 char line[LINE_SIZE]; |
39 | 1243 char *list = BLACK; |
3 | 1244 char *delim = " \t"; |
1245 int curline = 0; | |
1246 while (!is.eof()) { | |
1247 is.getline(line, LINE_SIZE); | |
1248 curline++; | |
1249 int n = strlen(line); | |
1250 if (!n) continue; | |
1251 for (int i=0; i<n; i++) line[i] = tolower(line[i]); | |
1252 if (line[0] == '#') continue; | |
1253 char *head = line; | |
1254 if (strspn(line, delim) == 0) { | |
1255 // have a leading ok/many tag to fetch | |
1256 char *cmd = strtok(line, delim); | |
1257 if (strcmp(cmd, MANY) == 0) list = BLACK; | |
1258 else if (strcmp(cmd, OK) == 0) list = WHITE; | |
1259 head = cmd + strlen(cmd) + 1; | |
1260 } | |
1261 char *cmd = strtok(head, delim); | |
1262 if (!cmd) continue; | |
1263 if (strcmp(cmd, "env_from") == 0) { | |
1264 char *from = next_token(delim); | |
1265 if (from) { | |
1266 string_map &fm = really_find_from_map(dc, name); | |
1267 fm[from] = list; | |
1268 } | |
1269 } | |
1270 else if (strcmp(cmd, "env_to") == 0) { | |
1271 char *to = next_token(delim); | |
1272 if (to) { | |
1273 dc.env_to_dnsbll[to] = list; | |
1274 dc.env_to_chkfrom[to] = list; | |
1275 } | |
1276 } | |
1277 else if (strcmp(cmd, "substitute") == 0) { | |
1278 char *tag = next_token(delim); | |
1279 if (tag && (strcmp(tag, "mail_host") == 0)) { | |
1280 char *from = next_token(delim); | |
1281 if (from) { | |
1282 string_map &fm = really_find_from_map(dc, name); | |
1283 fm[from] = list; | |
1284 } | |
1285 } | |
1286 } | |
1287 else if (strcmp(cmd, "include") == 0) { | |
1288 char *fn = next_token(delim); | |
1289 if (ok_to_include(dc, fn)) { | |
1290 load_conf_dcc(dc, name, fn); | |
1291 } | |
1292 } | |
1293 | |
1294 } | |
1295 is.close(); | |
1296 } | |
1297 | |
1298 | |
0 | 1299 static void load_conf(CONFIG &dc, char *fn); |
1300 static void load_conf(CONFIG &dc, char *fn) { | |
39 | 1301 ifstream is(fn); |
40 | 1302 if (is.fail()) { |
1303 char buf[1000]; | |
1304 snprintf(buf, sizeof(buf), "include file %s not found", fn); | |
1305 my_syslog(buf); | |
1306 return; | |
1307 } | |
0 | 1308 dc.config_files.push_back(fn); |
1309 map<char*, int, ltstr> commands; | |
57 | 1310 enum {dummy, tld, content, ignore, hostlimit, hostslimit, htmllimit, htmltag, dnsbl, dnsbll, envfrom, envto, include, includedcc}; |
46 | 1311 commands["tld" ] = tld; |
1312 commands["content" ] = content; | |
57 | 1313 commands["ignore" ] = ignore; |
46 | 1314 commands["host_limit" ] = hostlimit; |
1315 commands["host_soft_limit"] = hostslimit; | |
1316 commands["html_limit" ] = htmllimit; | |
1317 commands["html_tag" ] = htmltag; | |
1318 commands["dnsbl" ] = dnsbl; | |
1319 commands["dnsbl_list" ] = dnsbll; | |
1320 commands["env_from" ] = envfrom; | |
1321 commands["env_to" ] = envto; | |
1322 commands["include" ] = include; | |
1323 commands["include_dcc" ] = includedcc; | |
0 | 1324 const int LINE_SIZE = 2000; |
1325 char line[LINE_SIZE]; | |
1326 char orig[LINE_SIZE]; | |
1327 char *delim = " \t"; | |
1328 int curline = 0; | |
1329 while (!is.eof()) { | |
1330 is.getline(line, LINE_SIZE); | |
1331 snprintf(orig, sizeof(orig), "%s", line); | |
1332 curline++; | |
1333 int n = strlen(line); | |
1334 for (int i=0; i<n; i++) line[i] = tolower(line[i]); | |
1335 char *cmd = strtok(line, delim); | |
1336 if (cmd && (cmd[0] != '#') && (cmd[0] != '\0')) { | |
1337 // have a decent command | |
1338 bool processed = false; | |
1339 switch (commands[cmd]) { | |
28 | 1340 case tld: { |
29
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1341 char *tld = next_token(delim); |
28 | 1342 if (!tld) break; // no tld value |
29
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1343 dc.tlds.insert(tld); |
28 | 1344 processed = true; |
1345 } break; | |
1346 | |
8 | 1347 case content: { |
1348 char *suff = strtok(NULL, delim); | |
24 | 1349 if (!suff) break; // no dns suffix |
8 | 1350 char *msg = suff + strlen(suff); |
1351 if ((msg - line) >= strlen(orig)) break; // line ended with the dns suffix | |
1352 msg = strchr(msg+1, '\''); | |
1353 if (!msg) break; // no reply message template | |
1354 msg++; // move over the leading ' | |
1355 if ((msg - line) >= strlen(orig)) break; // line ended with the leading quote | |
1356 char *last = strchr(msg, '\''); | |
1357 if (!last) break; // no trailing quote | |
1358 *last = '\0'; // make it a null terminator | |
1359 dc.content_suffix = register_string(suff); | |
1360 dc.content_message = register_string(msg); | |
1361 processed = true; | |
1362 } break; | |
1363 | |
57 | 1364 case ignore: { |
1365 char *host = next_token(delim); | |
1366 if (!host) break; | |
1367 dc.content_host_ignore.insert(host); | |
1368 processed = true; | |
1369 } break; | |
1370 | |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1371 case hostlimit: { |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1372 char *limit = strtok(NULL, delim); |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1373 if (!limit) break; // no integer limit |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1374 char *msg = limit + strlen(limit); |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1375 if ((msg - line) >= strlen(orig)) break; // line ended with the limit |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1376 msg = strchr(msg+1, '\''); |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1377 if (!msg) break; // no reply message template |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1378 msg++; // move over the leading ' |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1379 if ((msg - line) >= strlen(orig)) break; // line ended with the leading quote |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1380 char *last = strchr(msg, '\''); |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1381 if (!last) break; // no trailing quote |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1382 *last = '\0'; // make it a null terminator |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1383 dc.host_limit = atoi(limit); |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1384 dc.host_limit_message = register_string(msg); |
46 | 1385 dc.host_random = false; |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1386 processed = true; |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1387 } break; |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1388 |
46 | 1389 case hostslimit: { |
1390 char *limit = next_token(delim); | |
1391 if (!limit) break; // no integer limit | |
1392 dc.host_limit = atoi(limit); | |
44 | 1393 dc.host_random = true; |
1394 processed = true; | |
1395 } break; | |
1396 | |
24 | 1397 case htmllimit: { |
1398 char *limit = strtok(NULL, delim); | |
1399 if (!limit) break; // no integer limit | |
1400 char *msg = limit + strlen(limit); | |
1401 if ((msg - line) >= strlen(orig)) break; // line ended with the limit | |
1402 msg = strchr(msg+1, '\''); | |
1403 if (!msg) break; // no reply message template | |
1404 msg++; // move over the leading ' | |
1405 if ((msg - line) >= strlen(orig)) break; // line ended with the leading quote | |
1406 char *last = strchr(msg, '\''); | |
1407 if (!last) break; // no trailing quote | |
1408 *last = '\0'; // make it a null terminator | |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1409 dc.tag_limit = atoi(limit); |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1410 dc.tag_limit_message = register_string(msg); |
24 | 1411 processed = true; |
1412 } break; | |
1413 | |
1414 case htmltag: { | |
1415 char *tag = next_token(delim); | |
1416 if (!tag) break; // no html tag value | |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1417 dc.html_tags.insert(tag); // base version |
26 | 1418 char buf[200]; |
1419 snprintf(buf, sizeof(buf), "/%s", tag); | |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1420 dc.html_tags.insert(register_string(buf)); // leading / |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1421 snprintf(buf, sizeof(buf), "%s/", tag); |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
1422 dc.html_tags.insert(register_string(buf)); // trailing / |
24 | 1423 processed = true; |
1424 } break; | |
1425 | |
0 | 1426 case dnsbl: { |
1427 // have a new dnsbl to use | |
1428 char *name = next_token(delim); | |
1429 if (!name) break; // no name name | |
1430 if (find_dnsbl(dc, name)) break; // duplicate entry | |
1431 char *suff = strtok(NULL, delim); | |
1432 if (!suff) break; // no dns suffic | |
1433 char *msg = suff + strlen(suff); | |
1434 if ((msg - line) >= strlen(orig)) break; // line ended with the dns suffix | |
1435 msg = strchr(msg+1, '\''); | |
1436 if (!msg) break; // no reply message template | |
1437 msg++; // move over the leading ' | |
1438 if ((msg - line) >= strlen(orig)) break; // line ended with the leading quote | |
1439 char *last = strchr(msg, '\''); | |
1440 if (!last) break; // no trailing quote | |
1441 *last = '\0'; // make it a null terminator | |
1442 dc.dnsbls[name] = new DNSBL(register_string(suff), register_string(msg)); | |
1443 processed = true; | |
1444 } break; | |
1445 | |
1446 case dnsbll: { | |
1447 // define a new combination of dnsbls | |
1448 char *name = next_token(delim); | |
1449 if (!name) break; | |
1450 if (find_dnsbll(dc, name)) break; // duplicate entry | |
1451 char *list = next_token(delim); | |
1452 if (!list || (*list == '\0') || (*list == '#')) break; | |
1453 DNSBLLP d = new DNSBLL; | |
1454 DNSBLP p = find_dnsbl(dc, list); | |
1455 if (p) d->push_back(p); | |
1456 while (true) { | |
1457 list = next_token(delim); | |
1458 if (!list || (*list == '\0') || (*list == '#')) break; | |
1459 DNSBLP p = find_dnsbl(dc, list); | |
1460 if (p) d->push_back(p); | |
1461 } | |
1462 dc.dnsblls[name] = d; | |
1463 processed = true; | |
1464 } break; | |
1465 | |
1466 case envfrom: { | |
1467 // add an entry into the named string_map | |
1468 char *name = next_token(delim); | |
1469 if (!name) break; | |
1470 char *from = next_token(delim); | |
1471 if (!from) break; | |
1472 char *list = next_token(delim); | |
1473 if (!list) break; | |
1474 if ((strcmp(list, WHITE) == 0) || | |
1475 (strcmp(list, BLACK) == 0)) { | |
1476 string_map &fm = really_find_from_map(dc, name); | |
1477 fm[from] = list; | |
1478 processed = true; | |
1479 } | |
1480 else { | |
1481 // list may be the name of a previously defined from_map | |
1482 string_map *m = find_from_map(dc, list); | |
1483 if (m && (strcmp(list,name) != 0)) { | |
1484 string_map &pm = *m; | |
1485 string_map &fm = really_find_from_map(dc, name); | |
1486 fm.insert(pm.begin(), pm.end()); | |
1487 processed = true; | |
1488 } | |
1489 } | |
1490 } break; | |
1491 | |
1492 case envto: { | |
1493 // define the dnsbl_list and env_from maps to use for this recipient | |
1494 char *to = next_token(delim); | |
1495 if (!to) break; | |
1496 char *list = next_token(delim); | |
1497 if (!list) break; | |
1498 char *from = next_token(delim); | |
1499 if (!from) break; | |
1500 dc.env_to_dnsbll[to] = list; | |
1501 dc.env_to_chkfrom[to] = from; | |
1502 processed = true; | |
1503 } break; | |
1504 | |
1505 case include: { | |
1506 char *fn = next_token(delim); | |
3 | 1507 if (ok_to_include(dc, fn)) { |
1508 load_conf(dc, fn); | |
1509 processed = true; | |
1510 } | |
1511 } break; | |
1512 | |
1513 case includedcc: { | |
1514 char *name = next_token(delim); | |
1515 if (!name) break; | |
1516 char *fn = next_token(delim); | |
1517 if (ok_to_include(dc, fn)) { | |
1518 load_conf_dcc(dc, name, fn); | |
1519 processed = true; | |
0 | 1520 } |
1521 } break; | |
1522 | |
1523 default: { | |
1524 } break; | |
1525 } | |
1526 if (!processed) { | |
1527 pthread_mutex_lock(&syslog_mutex); | |
1528 openlog("dnsbl", LOG_PID, LOG_MAIL); | |
1529 syslog(LOG_ERR, "ignoring file %s line %d : %s\n", fn, curline, orig); | |
1530 closelog(); | |
1531 pthread_mutex_unlock(&syslog_mutex); | |
1532 } | |
1533 } | |
1534 } | |
1535 is.close(); | |
1536 } | |
1537 | |
1538 | |
1539 //////////////////////////////////////////////// | |
1540 // reload the config | |
1541 // | |
1542 static CONFIG* new_conf(); | |
1543 static CONFIG* new_conf() { | |
1544 CONFIG *newc = new CONFIG; | |
29
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1545 pthread_mutex_lock(&config_mutex); |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1546 newc->generation = generation++; |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1547 pthread_mutex_unlock(&config_mutex); |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1548 char buf[200]; |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1549 snprintf(buf, sizeof(buf), "loading configuration generation %d", newc->generation); |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1550 my_syslog(buf); |
0 | 1551 load_conf(*newc, "dnsbl.conf"); |
1552 newc->load_time = time(NULL); | |
1553 return newc; | |
1554 } | |
1555 | |
1556 | |
1557 //////////////////////////////////////////////// | |
1558 // thread to watch the old config files for changes | |
1559 // and reload when needed. we also cleanup old | |
1560 // configs whose reference count has gone to zero. | |
1561 // | |
1562 static void* config_loader(void *arg); | |
1563 static void* config_loader(void *arg) { | |
1564 typedef set<CONFIG *> configp_set; | |
1565 configp_set old_configs; | |
18 | 1566 while (loader_run) { |
0 | 1567 sleep(180); // look for modifications every 3 minutes |
18 | 1568 if (!loader_run) break; |
0 | 1569 CONFIG &dc = *config; |
1570 time_t then = dc.load_time; | |
1571 struct stat st; | |
1572 bool reload = false; | |
1573 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) { | |
1574 char *fn = *i; | |
1575 if (stat(fn, &st)) reload = true; // file disappeared | |
1576 else if (st.st_mtime > then) reload = true; // file modified | |
1577 if (reload) break; | |
1578 } | |
1579 if (reload) { | |
1580 CONFIG *newc = new_conf(); | |
1581 // replace the global config pointer | |
1582 pthread_mutex_lock(&config_mutex); | |
1583 CONFIG *old = config; | |
1584 config = newc; | |
1585 pthread_mutex_unlock(&config_mutex); | |
1586 if (old) old_configs.insert(old); | |
1587 } | |
1588 // now look for old configs with zero ref counts | |
1589 for (configp_set::iterator i=old_configs.begin(); i!=old_configs.end(); ) { | |
1590 CONFIG *old = *i; | |
1591 if (!old->reference_count) { | |
29
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1592 char buf[200]; |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1593 snprintf(buf, sizeof(buf), "freeing memory for old configuration generation %d", old->generation); |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1594 my_syslog(buf); |
0 | 1595 delete old; // destructor does all the work |
1596 old_configs.erase(i++); | |
1597 } | |
1598 else i++; | |
1599 } | |
1600 } | |
18 | 1601 return NULL; |
0 | 1602 } |
1603 | |
1604 | |
1605 static void usage(char *prog); | |
1606 static void usage(char *prog) | |
1607 { | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1608 fprintf(stderr, "Usage: %s [-d] [-c] -r port -p sm-sock-addr [-t timeout]\n", prog); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1609 fprintf(stderr, "where port is for the connection to our own dns resolver processes\n"); |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1610 fprintf(stderr, " and should be local-domain-socket-file-name\n"); |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1611 fprintf(stderr, "where sm-sock-addr is for the connection to sendmail\n"); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1612 fprintf(stderr, " and should be one of\n"); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1613 fprintf(stderr, " inet:port@ip-address\n"); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1614 fprintf(stderr, " local:local-domain-socket-file-name\n"); |
9 | 1615 fprintf(stderr, "-c will load and dump the config to stdout\n"); |
16 | 1616 fprintf(stderr, "-d will add some syslog debug messages\n"); |
0 | 1617 } |
1618 | |
1619 | |
42 | 1620 |
1621 static void setup_socket(char *sock); | |
1622 static void setup_socket(char *sock) { | |
1623 unlink(sock); | |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1624 // sockaddr_un addr; |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1625 // memset(&addr, '\0', sizeof addr); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1626 // addr.sun_family = AF_UNIX; |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1627 // strncpy(addr.sun_path, sock, sizeof(addr.sun_path)-1); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1628 // int s = socket(AF_UNIX, SOCK_STREAM, 0); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1629 // bind(s, (sockaddr*)&addr, sizeof(addr)); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1630 // close(s); |
42 | 1631 } |
1632 | |
1633 | |
0 | 1634 int main(int argc, char**argv) |
1635 { | |
3 | 1636 bool check = false; |
1637 bool setconn = false; | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1638 bool setreso = false; |
0 | 1639 int c; |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1640 const char *args = "r:p:t:hcd"; |
0 | 1641 extern char *optarg; |
1642 | |
1643 // Process command line options | |
1644 while ((c = getopt(argc, argv, args)) != -1) { | |
1645 switch (c) { | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1646 case 'r': |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1647 if (optarg == NULL || *optarg == '\0') { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1648 fprintf(stderr, "Illegal resolver socket: %s\n", optarg); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1649 exit(EX_USAGE); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1650 } |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1651 resolver_port = strdup(optarg); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1652 setup_socket(resolver_port); |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1653 setreso = true; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1654 break; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1655 |
0 | 1656 case 'p': |
1657 if (optarg == NULL || *optarg == '\0') { | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1658 fprintf(stderr, "Illegal sendmail socket: %s\n", optarg); |
0 | 1659 exit(EX_USAGE); |
1660 } | |
1661 if (smfi_setconn(optarg) == MI_FAILURE) { | |
1662 fprintf(stderr, "smfi_setconn failed\n"); | |
1663 exit(EX_SOFTWARE); | |
1664 } | |
42 | 1665 if (strncasecmp(optarg, "unix:", 5) == 0) setup_socket(optarg + 5); |
1666 else if (strncasecmp(optarg, "local:", 6) == 0) setup_socket(optarg + 6); | |
3 | 1667 setconn = true; |
0 | 1668 break; |
1669 | |
1670 case 't': | |
1671 if (optarg == NULL || *optarg == '\0') { | |
1672 fprintf(stderr, "Illegal timeout: %s\n", optarg); | |
1673 exit(EX_USAGE); | |
1674 } | |
1675 if (smfi_settimeout(atoi(optarg)) == MI_FAILURE) { | |
1676 fprintf(stderr, "smfi_settimeout failed\n"); | |
1677 exit(EX_SOFTWARE); | |
1678 } | |
1679 break; | |
1680 | |
3 | 1681 case 'c': |
1682 check = true; | |
1683 break; | |
1684 | |
16 | 1685 case 'd': |
1686 debug_syslog = true; | |
1687 break; | |
1688 | |
0 | 1689 case 'h': |
1690 default: | |
1691 usage(argv[0]); | |
1692 exit(EX_USAGE); | |
1693 } | |
1694 } | |
5 | 1695 |
1696 if (check) { | |
1697 CONFIG &dc = *new_conf(); | |
1698 dumpit(dc); | |
1699 return 0; | |
1700 } | |
1701 | |
0 | 1702 if (!setconn) { |
1703 fprintf(stderr, "%s: Missing required -p argument\n", argv[0]); | |
1704 usage(argv[0]); | |
1705 exit(EX_USAGE); | |
1706 } | |
5 | 1707 |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1708 if (!setreso) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1709 fprintf(stderr, "%s: Missing required -r argument\n", argv[0]); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1710 usage(argv[0]); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1711 exit(EX_USAGE); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1712 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1713 |
0 | 1714 if (smfi_register(smfilter) == MI_FAILURE) { |
1715 fprintf(stderr, "smfi_register failed\n"); | |
1716 exit(EX_UNAVAILABLE); | |
1717 } | |
1718 | |
1719 // switch to background mode | |
1720 if (daemon(1,0) < 0) { | |
1721 fprintf(stderr, "daemon() call failed\n"); | |
1722 exit(EX_UNAVAILABLE); | |
1723 } | |
1724 | |
1725 // write the pid | |
1726 const char *pidpath = "/var/run/dnsbl.pid"; | |
1727 unlink(pidpath); | |
1728 FILE *f = fopen(pidpath, "w"); | |
1729 if (f) { | |
22 | 1730 #ifdef linux |
1731 // from a comment in the DCC source code: | |
1732 // Linux threads are broken. Signals given the | |
1733 // original process are delivered to only the | |
1734 // thread that happens to have that PID. The | |
1735 // sendmail libmilter thread that needs to hear | |
1736 // SIGINT and other signals does not, and that breaks | |
1737 // scripts that need to stop milters. | |
1738 // However, signaling the process group works. | |
0 | 1739 fprintf(f, "-%d\n", (u_int)getpgrp()); |
22 | 1740 #else |
1741 fprintf(f, "%d\n", (u_int)getpid()); | |
1742 #endif | |
0 | 1743 fclose(f); |
1744 } | |
1745 | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1746 // initialize the thread sync objects |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1747 pthread_mutex_init(&config_mutex, 0); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1748 pthread_mutex_init(&syslog_mutex, 0); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1749 pthread_mutex_init(&resolve_mutex, 0); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1750 pthread_mutex_init(&fd_pool_mutex, 0); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1751 |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1752 // drop root privs |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1753 struct passwd *pw = getpwnam("dnsbl"); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1754 if (pw) { |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1755 if (setgid(pw->pw_gid) == -1) { |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1756 my_syslog("failed to switch to group dnsbl"); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1757 } |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1758 if (setuid(pw->pw_uid) == -1) { |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1759 my_syslog("failed to switch to user dnsbl"); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1760 } |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1761 } |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1762 |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1763 // fork off the resolver listener process |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1764 pid_t child = fork(); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1765 if (child < 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1766 my_syslog("failed to create resolver listener process"); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1767 exit(0); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1768 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1769 if (child == 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1770 // we are the child - dns resolver listener process |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1771 resolver_socket = socket(AF_UNIX, SOCK_STREAM, 0); |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1772 if (resolver_socket < 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1773 my_syslog("child failed to create resolver socket"); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1774 exit(0); // failed |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1775 } |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1776 sockaddr_un server; |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1777 memset(&server, '\0', sizeof(server)); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1778 server.sun_family = AF_UNIX; |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1779 strncpy(server.sun_path, resolver_port, sizeof(server.sun_path)-1); |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1780 // set the socket options |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1781 ///int reuse_addr = 1; |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1782 ///linger linger_opt; |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1783 ///linger_opt.l_onoff = 0; // off |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1784 ///linger_opt.l_linger = 0; |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1785 ///setsockopt(resolver_socket, SOL_SOCKET, SO_REUSEADDR, reinterpret_cast<char*>(&reuse_addr), sizeof(reuse_addr)); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1786 ///setsockopt(resolver_socket, SOL_SOCKET, SO_LINGER, reinterpret_cast<char*>(&linger_opt), sizeof(linger_opt)); |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1787 ///// set nonblocking mode |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1788 ///int dummy = 0; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1789 ///int flags = fcntl(resolver_socket, F_GETFL, dummy); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1790 ///if (flags >= 0) fcntl(resolver_socket, F_SETFL, flags | O_NONBLOCK); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1791 //try to bind the address to the socket. |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1792 if (bind(resolver_socket, (sockaddr *)&server, sizeof(server)) < 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1793 // bind failed |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1794 shutdown(resolver_socket, SHUT_RDWR); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1795 close(resolver_socket); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1796 my_syslog("child failed to bind resolver socket"); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1797 exit(0); // failed |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1798 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1799 |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1800 //listen on the socket. |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1801 if (listen(resolver_socket, 10) < 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1802 // listen failed |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1803 shutdown(resolver_socket, SHUT_RDWR); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1804 close(resolver_socket); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1805 my_syslog("child failed to listen to the resolver socket"); |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1806 exit(0); // failed |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1807 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1808 while (true) { |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1809 sockaddr_un client; |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1810 socklen_t clientlen = sizeof(client); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1811 int s = accept(resolver_socket, (sockaddr *)&client, &clientlen); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1812 if (s > 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1813 // accept worked, it did not get cancelled before we could accept it |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1814 // fork off a process to handle this connection |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1815 int newchild = fork(); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1816 if (newchild == 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1817 // this is the worker process |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1818 // child does not need the listening socket |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1819 close(resolver_socket); |
61
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
1820 //my_syslog("child forked a worker process"); |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1821 process_resolver_requests(s); |
61
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
1822 //my_syslog("child terminated a worker process"); |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1823 exit(0); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1824 } |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1825 else { |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1826 // this is the parent |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1827 // parent does not need the accepted socket |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1828 close(s); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1829 } |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1830 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1831 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1832 exit(0); // make sure we don't fall thru. |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1833 } |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1834 else { |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1835 sleep(2); // allow child to get started |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1836 } |
48 | 1837 |
1838 // load the initial config | |
1839 config = new_conf(); | |
1840 | |
1841 // only create threads after the fork() in daemon | |
1842 pthread_t tid; | |
1843 if (pthread_create(&tid, 0, config_loader, 0)) | |
1844 my_syslog("failed to create config loader thread"); | |
1845 if (pthread_detach(tid)) | |
1846 my_syslog("failed to detach config loader thread"); | |
1847 | |
18 | 1848 time_t starting = time(NULL); |
1849 int rc = smfi_main(); | |
22 | 1850 if ((rc != MI_SUCCESS) && (time(NULL) > starting+5*60)) { |
18 | 1851 my_syslog("trying to restart after smfi_main()"); |
1852 loader_run = false; // eventually the config loader thread will terminate | |
1853 execvp(argv[0], argv); | |
1854 } | |
1855 exit((rc == MI_SUCCESS) ? 0 : EX_UNAVAILABLE); | |
0 | 1856 } |
8 | 1857 |