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