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