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