Mercurial > dnsbl
annotate src/dnsbl.cpp @ 76:81f1e400e8ab
start coding on new config syntax
author | carl |
---|---|
date | Sat, 16 Jul 2005 13:47:19 -0700 |
parents | 1142e46be550 |
children | 8487650c98ee |
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. |
76 | 15 -s Stress test by loading and deleting the current config in a loop. |
16 | 16 -d Add debug syslog entries |
76 | 17 -e f|t Print the results of looking up from address f and to address |
18 t in the current config | |
16 | 19 |
0 | 20 |
13 | 21 TODO: |
22 | |
76 | 23 1) Add option for using smtp connections to verify addresses from backup |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
24 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
|
25 on the primary machine. |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
26 |
0 | 27 */ |
28 | |
29 | |
30 // from sendmail sample | |
31 #include <sys/types.h> | |
32 #include <sys/stat.h> | |
33 #include <errno.h> | |
34 #include <sysexits.h> | |
35 #include <unistd.h> | |
36 | |
37 // needed for socket io | |
38 #include <sys/ioctl.h> | |
39 #include <net/if.h> | |
40 #include <arpa/inet.h> | |
41 #include <netinet/in.h> | |
42 #include <netinet/tcp.h> | |
43 #include <netdb.h> | |
44 #include <sys/socket.h> | |
42 | 45 #include <sys/un.h> |
0 | 46 |
47 // needed for thread | |
48 #include <pthread.h> | |
49 | |
50 // needed for std c++ collections | |
51 #include <set> | |
52 #include <map> | |
53 #include <list> | |
54 | |
55 // for the dns resolver | |
56 #include <netinet/in.h> | |
57 #include <arpa/nameser.h> | |
58 #include <resolv.h> | |
59 | |
60 // misc stuff needed here | |
61 #include <ctype.h> | |
62 #include <syslog.h> | |
42 | 63 #include <pwd.h> |
65 | 64 #include <sys/wait.h> /* header for waitpid() and various macros */ |
65 #include <signal.h> /* header for signal functions */ | |
0 | 66 |
72 | 67 #include "includes.h" |
8 | 68 |
73 | 69 static char* dnsbl_version="$Id$"; |
1 | 70 |
0 | 71 |
72 extern "C" { | |
73 #include "libmilter/mfapi.h" | |
74 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr); | |
75 sfsistat mlfi_envfrom(SMFICTX *ctx, char **argv); | |
76 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **argv); | |
8 | 77 sfsistat mlfi_body(SMFICTX *ctx, u_char *data, size_t len); |
78 sfsistat mlfi_eom(SMFICTX *ctx); | |
79 sfsistat mlfi_abort(SMFICTX *ctx); | |
0 | 80 sfsistat mlfi_close(SMFICTX *ctx); |
65 | 81 void sig_chld(int signo); |
0 | 82 } |
83 | |
72 | 84 bool debug_syslog = false; |
85 bool syslog_opened = false; | |
75 | 86 bool use_syslog = true; // false to printf |
87 bool loader_run = true; // used to stop the config loader thread | |
76 | 88 CONFIG *config = NULL; // protected by the config_mutex |
89 int generation = 0; // protected by the config_mutex | |
90 const int maxlen = 1000; // used for snprintf buffers | |
0 | 91 |
72 | 92 pthread_mutex_t config_mutex; |
93 pthread_mutex_t syslog_mutex; | |
94 pthread_mutex_t resolve_mutex; | |
95 pthread_mutex_t fd_pool_mutex; | |
0 | 96 |
72 | 97 std::set<int> fd_pool; |
98 int NULL_SOCKET = -1; | |
99 char *resolver_port = NULL; // unix domain socket to talk to the dns resolver process | |
100 int resolver_socket = NULL_SOCKET; // socket used to listen for resolver requests | |
101 time_t ERROR_SOCKET_TIME = 60; // number of seconds between attempts to open the spam filter socket | |
102 time_t last_error_time; | |
103 int resolver_sock_count = 0; // protected with fd_pool_mutex | |
104 int resolver_pool_size = 0; // protected with fd_pool_mutex | |
105 | |
53
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
106 |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
107 struct ns_map { |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
108 // 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
|
109 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
|
110 ns_mapper ns_ip; // nameserver name -> ip address of the name server |
72 | 111 ~ns_map(); |
112 void add(char *name, char *refer); | |
53
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
113 }; |
0 | 114 |
72 | 115 |
74 | 116 ns_map::~ns_map() { |
72 | 117 for (string_map::iterator i=ns_host.begin(); i!=ns_host.end(); i++) { |
118 char *x = (*i).first; | |
119 char *y = (*i).second; | |
120 free(x); | |
121 free(y); | |
0 | 122 } |
72 | 123 ns_ip.clear(); |
124 ns_host.clear(); | |
0 | 125 } |
126 | |
127 | |
72 | 128 void ns_map::add(char *name, char *refer) { |
129 string_map::iterator i = ns_host.find(name); | |
130 if (i != ns_host.end()) return; | |
131 char *x = strdup(name); | |
132 char *y = strdup(refer); | |
133 ns_ip[x] = 0; | |
134 ns_host[x] = y; | |
62
7ad7d8b100bf
only keep 20% of the open resolver sockets in the ready pool.
carl
parents:
61
diff
changeset
|
135 |
72 | 136 } |
61
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
137 |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
138 // 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
|
139 // 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
|
140 struct glommer { |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
141 int length; |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
142 #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
|
143 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
|
144 #else |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
145 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
|
146 #endif |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
147 } __attribute__ ((packed)); |
0 | 148 |
72 | 149 |
150 //////////////////////////////////////////////// | |
151 // helper to discard the strings held by a context_map | |
152 // | |
153 void discard(context_map &cm); | |
154 void discard(context_map &cm) { | |
155 for (context_map::iterator i=cm.begin(); i!=cm.end(); i++) { | |
156 char *x = (*i).first; | |
157 free(x); | |
158 } | |
76 | 159 cm.clear(); |
72 | 160 } |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
161 |
0 | 162 |
163 //////////////////////////////////////////////// | |
72 | 164 // helper to register a string in a context_map |
34 | 165 // |
72 | 166 void register_string(context_map &cm, char *name, CONTEXT *con); |
167 void register_string(context_map &cm, char *name, CONTEXT *con) { | |
168 context_map::iterator i = cm.find(name); | |
169 if (i != cm.end()) return; | |
34 | 170 char *x = strdup(name); |
72 | 171 cm[x] = con; |
34 | 172 } |
173 | |
12 | 174 |
0 | 175 //////////////////////////////////////////////// |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
176 // disconnect the fd from the dns resolver process |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
177 // |
64
6add229288a8
only keep 20% of the open resolver sockets in the ready pool.
carl
parents:
63
diff
changeset
|
178 void my_disconnect(int sock, bool decrement = true); |
72 | 179 void my_disconnect(int sock, bool decrement) { |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
180 if (sock != NULL_SOCKET) { |
64
6add229288a8
only keep 20% of the open resolver sockets in the ready pool.
carl
parents:
63
diff
changeset
|
181 if (decrement) { |
6add229288a8
only keep 20% of the open resolver sockets in the ready pool.
carl
parents:
63
diff
changeset
|
182 pthread_mutex_lock(&fd_pool_mutex); |
6add229288a8
only keep 20% of the open resolver sockets in the ready pool.
carl
parents:
63
diff
changeset
|
183 resolver_sock_count--; |
6add229288a8
only keep 20% of the open resolver sockets in the ready pool.
carl
parents:
63
diff
changeset
|
184 pthread_mutex_unlock(&fd_pool_mutex); |
6add229288a8
only keep 20% of the open resolver sockets in the ready pool.
carl
parents:
63
diff
changeset
|
185 } |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
186 shutdown(sock, SHUT_RDWR); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
187 close(sock); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
188 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
189 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
190 |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
191 |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
192 //////////////////////////////////////////////// |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
193 // return fd connected to the dns resolver process |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
194 // |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
195 int my_connect(); |
72 | 196 int my_connect() { |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
197 // 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
|
198 time_t now = time(NULL); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
199 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
|
200 |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
201 // nothing recent, maybe this time it will work |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
202 int sock = NULL_SOCKET; |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
203 sockaddr_un server; |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
204 memset(&server, '\0', sizeof(server)); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
205 server.sun_family = AF_UNIX; |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
206 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
|
207 sock = socket(AF_UNIX, SOCK_STREAM, 0); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
208 if (sock != NULL_SOCKET) { |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
209 bool rc = (connect(sock, (sockaddr *)&server, sizeof(server)) == 0); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
210 if (!rc) { |
64
6add229288a8
only keep 20% of the open resolver sockets in the ready pool.
carl
parents:
63
diff
changeset
|
211 my_disconnect(sock, false); |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
212 sock = NULL_SOCKET; |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
213 last_error_time = now; |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
214 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
215 } |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
216 else last_error_time = now; |
62
7ad7d8b100bf
only keep 20% of the open resolver sockets in the ready pool.
carl
parents:
61
diff
changeset
|
217 if (sock != NULL_SOCKET) { |
7ad7d8b100bf
only keep 20% of the open resolver sockets in the ready pool.
carl
parents:
61
diff
changeset
|
218 pthread_mutex_lock(&fd_pool_mutex); |
7ad7d8b100bf
only keep 20% of the open resolver sockets in the ready pool.
carl
parents:
61
diff
changeset
|
219 resolver_sock_count++; |
7ad7d8b100bf
only keep 20% of the open resolver sockets in the ready pool.
carl
parents:
61
diff
changeset
|
220 pthread_mutex_unlock(&fd_pool_mutex); |
7ad7d8b100bf
only keep 20% of the open resolver sockets in the ready pool.
carl
parents:
61
diff
changeset
|
221 } |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
222 return sock; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
223 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
224 |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
225 |
0 | 226 mlfiPriv::mlfiPriv() { |
227 pthread_mutex_lock(&config_mutex); | |
228 pc = config; | |
229 pc->reference_count++; | |
230 pthread_mutex_unlock(&config_mutex); | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
231 get_fd(); |
76 | 232 ip = 0; |
233 mailaddr = NULL; | |
234 queueid = NULL; | |
235 authenticated = false; | |
236 have_whites = false; | |
237 only_whites = true; | |
238 memory = NULL; | |
239 scanner = NULL; | |
240 content_suffix = NULL; | |
241 content_message = NULL; | |
242 content_host_ignore = NULL; | |
0 | 243 } |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
244 |
0 | 245 mlfiPriv::~mlfiPriv() { |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
246 return_fd(); |
0 | 247 pthread_mutex_lock(&config_mutex); |
248 pc->reference_count--; | |
249 pthread_mutex_unlock(&config_mutex); | |
8 | 250 reset(true); |
251 } | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
252 |
8 | 253 void mlfiPriv::reset(bool final) { |
76 | 254 if (debug_syslog) my_syslog(this, "mlfiPriv::reset"); |
0 | 255 if (mailaddr) free(mailaddr); |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
256 if (queueid) free(queueid); |
72 | 257 discard(env_to); |
76 | 258 if (memory) delete memory; |
259 if (scanner) delete scanner; | |
8 | 260 if (!final) { |
76 | 261 mailaddr = NULL; |
262 queueid = NULL; | |
263 authenticated = false; | |
264 have_whites = false; | |
265 only_whites = true; | |
266 memory = NULL; | |
267 scanner = NULL; | |
268 content_suffix = NULL; | |
269 content_message = NULL; | |
270 content_host_ignore = NULL; | |
8 | 271 } |
76 | 272 if (debug_syslog) my_syslog("mlfiPriv::reset exit"); |
0 | 273 } |
274 | |
72 | 275 void mlfiPriv::get_fd() { |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
276 err = true; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
277 fd = NULL_SOCKET; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
278 int result = pthread_mutex_lock(&fd_pool_mutex); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
279 if (!result) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
280 std::set<int>::iterator i; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
281 i = fd_pool.begin(); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
282 if (i != fd_pool.end()) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
283 // have at least one fd in the pool |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
284 err = false; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
285 fd = *i; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
286 fd_pool.erase(fd); |
62
7ad7d8b100bf
only keep 20% of the open resolver sockets in the ready pool.
carl
parents:
61
diff
changeset
|
287 resolver_pool_size--; |
64
6add229288a8
only keep 20% of the open resolver sockets in the ready pool.
carl
parents:
63
diff
changeset
|
288 pthread_mutex_unlock(&fd_pool_mutex); |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
289 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
290 else { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
291 // 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
|
292 pthread_mutex_unlock(&fd_pool_mutex); |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
293 fd = my_connect(); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
294 err = (fd == NULL_SOCKET); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
295 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
296 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
297 else { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
298 // 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
|
299 fd = my_connect(); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
300 err = (fd == NULL_SOCKET); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
301 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
302 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
303 |
72 | 304 void mlfiPriv::return_fd() { |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
305 if (err) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
306 // 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
|
307 my_disconnect(fd); |
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 else { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
310 int result = pthread_mutex_lock(&fd_pool_mutex); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
311 if (!result) { |
63
579dc5955cbe
only keep 20% of the open resolver sockets in the ready pool.
carl
parents:
62
diff
changeset
|
312 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
|
313 // return the fd to the pool |
7ad7d8b100bf
only keep 20% of the open resolver sockets in the ready pool.
carl
parents:
61
diff
changeset
|
314 fd_pool.insert(fd); |
7ad7d8b100bf
only keep 20% of the open resolver sockets in the ready pool.
carl
parents:
61
diff
changeset
|
315 resolver_pool_size++; |
64
6add229288a8
only keep 20% of the open resolver sockets in the ready pool.
carl
parents:
63
diff
changeset
|
316 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
|
317 } |
7ad7d8b100bf
only keep 20% of the open resolver sockets in the ready pool.
carl
parents:
61
diff
changeset
|
318 else { |
63
579dc5955cbe
only keep 20% of the open resolver sockets in the ready pool.
carl
parents:
62
diff
changeset
|
319 // 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
|
320 // 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
|
321 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
|
322 my_disconnect(fd); |
7ad7d8b100bf
only keep 20% of the open resolver sockets in the ready pool.
carl
parents:
61
diff
changeset
|
323 } |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
324 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
325 else { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
326 // 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
|
327 my_disconnect(fd); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
328 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
329 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
330 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
331 |
72 | 332 int mlfiPriv::my_write(char *buf, int len) { |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
333 if (err) return 0; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
334 int rs = 0; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
335 while (len) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
336 int ws = write(fd, buf, len); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
337 if (ws > 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
338 rs += ws; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
339 len -= ws; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
340 buf += ws; |
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 else { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
343 // peer closed the socket! |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
344 rs = 0; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
345 err = true; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
346 break; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
347 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
348 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
349 return rs; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
350 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
351 |
72 | 352 int mlfiPriv::my_read(char *buf, int len) { |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
353 if (err) return 0; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
354 int rs = 0; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
355 while (len > 1) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
356 int ws = read(fd, buf, len); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
357 if (ws > 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
358 rs += ws; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
359 len -= ws; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
360 buf += ws; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
361 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
362 else { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
363 // peer closed the socket! |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
364 rs = 0; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
365 err = true; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
366 break; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
367 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
368 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
369 return rs; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
370 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
371 |
72 | 372 void mlfiPriv::need_content_filter(char *rcpt, CONTEXT &con) { |
76 | 373 if (debug_syslog) my_syslog(this, "need_content_filter"); |
72 | 374 register_string(env_to, rcpt, &con); |
76 | 375 if (!memory) { |
376 // first recipient that needs content filtering sets all | |
377 // the content filtering parameters | |
378 memory = new recorder(this, con.get_html_tags(), con.get_content_tlds()); | |
379 scanner = new url_scanner(memory); | |
380 content_suffix = con.get_content_suffix(); | |
381 content_message = con.get_content_message(); | |
382 content_host_ignore = &con.get_content_host_ignore(); | |
383 } | |
384 if (debug_syslog) my_syslog(this, "need_content_filter exit"); | |
72 | 385 } |
386 | |
0 | 387 #define MLFIPRIV ((struct mlfiPriv *) smfi_getpriv(ctx)) |
388 | |
389 | |
390 //////////////////////////////////////////////// | |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
391 // syslog a message |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
392 // |
72 | 393 void my_syslog(mlfiPriv *priv, char *text) { |
76 | 394 char buf[maxlen]; |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
395 if (priv) { |
42 | 396 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
|
397 text = buf; |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
398 } |
75 | 399 if (use_syslog) { |
400 pthread_mutex_lock(&syslog_mutex); | |
401 if (!syslog_opened) { | |
402 openlog("dnsbl", LOG_PID, LOG_MAIL); | |
403 syslog_opened = true; | |
404 } | |
405 syslog(LOG_NOTICE, "%s", text); | |
76 | 406 closelog(); |
407 syslog_opened = false; | |
75 | 408 pthread_mutex_unlock(&syslog_mutex); |
409 } | |
410 else { | |
411 printf("%s \n", text); | |
412 } | |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
413 } |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
414 |
72 | 415 void my_syslog(char *text) { |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
416 my_syslog(NULL, text); |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
417 } |
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
418 |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
419 |
0 | 420 //////////////////////////////////////////////// |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
421 // 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
|
422 // write the result back to the socket. |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
423 |
72 | 424 void process_resolver_requests(int socket); |
425 void process_resolver_requests(int socket) { | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
426 #ifdef NS_MAXDNAME |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
427 char question[NS_MAXDNAME]; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
428 #else |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
429 char question[1000]; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
430 #endif |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
431 glommer glom; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
432 |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
433 int maxq = sizeof(question); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
434 while (true) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
435 // read a question |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
436 int rs = 0; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
437 while (true) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
438 int ns = read(socket, question+rs, maxq-rs); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
439 if (ns > 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
440 rs += ns; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
441 if (question[rs-1] == '\0') { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
442 // 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
|
443 break; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
444 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
445 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
446 else { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
447 // peer closed the socket |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
448 //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
|
449 shutdown(socket, SHUT_RDWR); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
450 close(socket); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
451 return; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
452 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
453 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
454 |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
455 // 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
|
456 #ifdef NS_PACKETSZ |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
457 //char text[1000]; |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
458 //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
|
459 //my_syslog(text); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
460 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
|
461 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
|
462 #else |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
463 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
|
464 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
|
465 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
|
466 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
|
467 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
|
468 } |
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
469 #endif |
59
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 // write the answer |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
472 char *buf = (char *)&glom; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
473 int len = glom.length + sizeof(glom.length); |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
474 //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
|
475 //my_syslog(text); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
476 int ws = 0; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
477 while (len > ws) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
478 int ns = write(socket, buf+ws, len-ws); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
479 if (ns > 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
480 ws += ns; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
481 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
482 else { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
483 // peer closed the socket! |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
484 //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
|
485 shutdown(socket, SHUT_RDWR); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
486 close(socket); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
487 return; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
488 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
489 } |
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 } |
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 //////////////////////////////////////////////// |
8 | 495 // ask a dns question and get an A record answer - we don't try |
496 // 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
|
497 // If we cannot get an answer, we just accept the mail. |
8 | 498 // |
0 | 499 // |
72 | 500 int dns_interface(mlfiPriv &priv, char *question, bool maybe_ip, ns_map *nameservers); |
501 int dns_interface(mlfiPriv &priv, char *question, bool maybe_ip, ns_map *nameservers) { | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
502 // 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
|
503 // 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
|
504 // process, which does the actual dns resolution. |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
505 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
|
506 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
|
507 glommer glom; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
508 char *buf = (char *)&glom; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
509 priv.my_read(buf, sizeof(glom.length)); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
510 buf += sizeof(glom.length); |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
511 ///char text[1000]; |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
512 ///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
|
513 ///my_syslog(text); |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
514 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
|
515 priv.err = true; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
516 return 0; // cannot process overlarge answers |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
517 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
518 priv.my_read(buf, glom.length); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
519 |
61
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
520 #ifdef NS_PACKETSZ |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
521 // 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
|
522 // 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
|
523 int ret_address = 0; |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
524 pthread_mutex_lock(&resolve_mutex); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
525 if (glom.length > 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
526 // parse the answer |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
527 ns_msg handle; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
528 ns_rr rr; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
529 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
|
530 // look for ns names |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
531 if (nameservers) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
532 ns_map &ns = *nameservers; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
533 int rrnum = 0; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
534 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
|
535 if (ns_rr_type(rr) == ns_t_ns) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
536 char nam[NS_MAXDNAME+1]; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
537 char *n = nam; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
538 const u_char *p = ns_rr_rdata(rr); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
539 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
|
540 size_t s = *(p++); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
541 if (s > 191) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
542 // compression pointer |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
543 s = (s-192)*256 + *(p++); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
544 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
|
545 p = glom.answer + s; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
546 s = *(p++); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
547 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
548 if (s > 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
549 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
|
550 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
|
551 memcpy(n, p, s); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
552 n += s; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
553 p += s; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
554 *(n++) = '.'; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
555 } |
34 | 556 } |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
557 if (n-nam) n--; // remove trailing . |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
558 *n = '\0'; // null terminate it |
72 | 559 ns.add(nam, question); // ns host to lookup later |
34 | 560 } |
561 } | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
562 rrnum = 0; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
563 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
|
564 if (ns_rr_type(rr) == ns_t_a) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
565 char* nam = (char*)ns_rr_name(rr); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
566 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
|
567 if (i != ns.ns_ip.end()) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
568 // we want this ip address |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
569 int address; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
570 memcpy(&address, ns_rr_rdata(rr), sizeof(address)); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
571 ns.ns_ip[nam] = address; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
572 } |
34 | 573 } |
574 } | |
575 } | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
576 int rrnum = 0; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
577 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
|
578 if (ns_rr_type(rr) == ns_t_a) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
579 int address; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
580 memcpy(&address, ns_rr_rdata(rr), sizeof(address)); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
581 ret_address = address; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
582 } |
23
06de5ab6a232
add url decoding stage, allow http:/ single / in yahoo redirector, allow ip address hostnames
carl
parents:
22
diff
changeset
|
583 } |
06de5ab6a232
add url decoding stage, allow http:/ single / in yahoo redirector, allow ip address hostnames
carl
parents:
22
diff
changeset
|
584 } |
06de5ab6a232
add url decoding stage, allow http:/ single / in yahoo redirector, allow ip address hostnames
carl
parents:
22
diff
changeset
|
585 } |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
586 if (maybe_ip && !ret_address) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
587 // might be a bare ip address |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
588 in_addr ip; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
589 if (inet_aton(question, &ip)) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
590 ret_address = ip.s_addr; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
591 } |
8 | 592 } |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
593 pthread_mutex_unlock(&resolve_mutex); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
594 return ret_address; |
16 | 595 #else |
61
7f44a4974bf6
Use resolver processes even if we don't have the interfaces, and need to use gethostbyname.
carl
parents:
60
diff
changeset
|
596 return glom.answer; |
16 | 597 #endif |
8 | 598 } |
599 | |
600 | |
601 //////////////////////////////////////////////// | |
602 // check a single dnsbl | |
603 // | |
72 | 604 bool check_single(mlfiPriv &priv, int ip, char *suffix); |
605 bool check_single(mlfiPriv &priv, int ip, char *suffix) { | |
0 | 606 // make a dns question |
607 const u_char *src = (const u_char *)&ip; | |
75 | 608 if (src[0] == 127) return false; // don't do dns lookups on localhost |
16 | 609 #ifdef NS_MAXDNAME |
0 | 610 char question[NS_MAXDNAME]; |
16 | 611 #else |
612 char question[1000]; | |
613 #endif | |
8 | 614 snprintf(question, sizeof(question), "%u.%u.%u.%u.%s.", src[3], src[2], src[1], src[0], suffix); |
615 // ask the question, if we get an A record it implies a blacklisted ip address | |
72 | 616 return dns_interface(priv, question, false, NULL); |
8 | 617 } |
618 | |
619 | |
620 //////////////////////////////////////////////// | |
621 // check a single dnsbl | |
622 // | |
72 | 623 bool check_single(mlfiPriv &priv, int ip, DNSBL &bl); |
624 bool check_single(mlfiPriv &priv, int ip, DNSBL &bl) { | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
625 return check_single(priv, ip, bl.suffix); |
0 | 626 } |
627 | |
628 | |
629 //////////////////////////////////////////////// | |
630 // check the dnsbls specified for this recipient | |
631 // | |
72 | 632 bool check_dnsbl(mlfiPriv &priv, dnsblp_list &dnsbll, DNSBLP &rejectlist); |
633 bool check_dnsbl(mlfiPriv &priv, dnsblp_list &dnsbll, DNSBLP &rejectlist) { | |
75 | 634 if (priv.authenticated) return false; |
72 | 635 for (dnsblp_list::iterator i=dnsbll.begin(); i!=dnsbll.end(); i++) { |
0 | 636 DNSBLP dp = *i; // non null by construction |
72 | 637 bool st; |
638 map<DNSBLP, bool>::iterator f = priv.checked.find(dp); | |
0 | 639 if (f == priv.checked.end()) { |
640 // have not checked this list yet | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
641 st = check_single(priv, priv.ip, *dp); |
0 | 642 rejectlist = dp; |
643 priv.checked[dp] = st; | |
644 } | |
645 else { | |
646 st = (*f).second; | |
647 rejectlist = (*f).first; | |
648 } | |
72 | 649 if (st) return st; |
0 | 650 } |
72 | 651 return false; |
0 | 652 } |
653 | |
654 | |
655 //////////////////////////////////////////////// | |
55
44babba1a9b9
updates for 3.6, better documentation on removing content filtering, missing some files in cvs
carl
parents:
53
diff
changeset
|
656 // check the hosts from the body against the content dnsbl |
8 | 657 // |
72 | 658 bool check_hosts(mlfiPriv &priv, bool random, int limit, char *&host, int ip); |
74 | 659 bool check_hosts(mlfiPriv &priv, bool random, int limit, char *&host, int ip) { |
75 | 660 CONFIG &dc = *priv.pc; |
74 | 661 string_set &hosts = priv.memory->get_hosts(); |
76 | 662 string_set &ignore = *priv.content_host_ignore; |
663 char *suffix = priv.content_suffix; | |
72 | 664 |
8 | 665 int count = 0; |
72 | 666 int cnt = hosts.size(); // number of hosts we could look at |
667 int_set ips; | |
668 ns_map nameservers; | |
669 for (string_set::iterator i=hosts.begin(); i!=hosts.end(); i++) { | |
670 host = *i; // a reference into hosts, which will live until this smtp transaction is closed | |
58 | 671 |
672 // don't bother looking up hosts on the ignore list | |
72 | 673 string_set::iterator j = ignore.find(host); |
674 if (j != ignore.end()) continue; | |
58 | 675 |
72 | 676 // try to only look at limit/cnt fraction of the available cnt host names in random mode |
677 if ((cnt > limit) && (limit > 0) && random) { | |
44 | 678 int r = rand() % cnt; |
72 | 679 if (r >= limit) { |
76 | 680 char buf[maxlen]; |
44 | 681 snprintf(buf, sizeof(buf), "host %s skipped", host); |
682 my_syslog(&priv, buf); | |
683 continue; | |
684 } | |
685 } | |
8 | 686 count++; |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
687 ip = dns_interface(priv, host, true, &nameservers); |
16 | 688 if (debug_syslog) { |
76 | 689 char buf[maxlen]; |
29
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
690 if (ip) { |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
691 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
|
692 adr[0] = '\0'; |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
693 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
|
694 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
|
695 } |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
696 else { |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
697 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
|
698 } |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
699 my_syslog(&priv, buf); |
16 | 700 } |
8 | 701 if (ip) { |
44 | 702 int_set::iterator i = ips.find(ip); |
703 if (i == ips.end()) { | |
704 ips.insert(ip); | |
75 | 705 if (check_single(priv, ip, suffix)) { |
72 | 706 return true; |
44 | 707 } |
34 | 708 } |
8 | 709 } |
710 } | |
72 | 711 limit *= 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
|
712 for (ns_mapper::iterator i=nameservers.ns_ip.begin(); i!=nameservers.ns_ip.end(); i++) { |
34 | 713 count++; |
72 | 714 if ((count > limit) && (limit > 0)) { |
715 if (random) continue; // don't complain | |
716 return true; | |
34 | 717 } |
36 | 718 host = (*i).first; // a transient reference that needs to be replaced before we return it |
34 | 719 ip = (*i).second; |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
720 if (!ip) ip = dns_interface(priv, host, false, NULL); |
34 | 721 if (debug_syslog) { |
76 | 722 char buf[maxlen]; |
34 | 723 if (ip) { |
724 char adr[sizeof "255.255.255.255"]; | |
725 adr[0] = '\0'; | |
726 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr)); | |
727 snprintf(buf, sizeof(buf), "ns %s found at %s", host, adr); | |
728 } | |
729 else { | |
730 snprintf(buf, sizeof(buf), "ns %s not found", host); | |
731 } | |
41
d95af8129dfa
updates for 3.2, changing file layout, add queueid to messages
carl
parents:
40
diff
changeset
|
732 my_syslog(&priv, buf); |
34 | 733 } |
734 if (ip) { | |
44 | 735 int_set::iterator i = ips.find(ip); |
736 if (i == ips.end()) { | |
737 ips.insert(ip); | |
75 | 738 if (check_single(priv, ip, suffix)) { |
53
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
739 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
|
740 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
|
741 char *refer = (*j).second; |
76 | 742 char buf[maxlen]; |
53
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
743 snprintf(buf, sizeof(buf), "%s with nameserver %s", refer, host); |
72 | 744 host = register_string(hosts, buf); // put a copy into hosts, and return that reference |
53
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
745 } |
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
746 else { |
72 | 747 host = register_string(hosts, host); // put a copy into hosts, and return that reference |
53
c2371bb6cf84
3.5 - better error message when rejecting based on ns records on the sbl
carl
parents:
48
diff
changeset
|
748 } |
72 | 749 return true; |
44 | 750 } |
34 | 751 } |
752 } | |
753 } | |
72 | 754 return false; |
8 | 755 } |
756 | |
757 | |
758 //////////////////////////////////////////////// | |
75 | 759 // this email address is passed in from sendmail, and will |
760 // always be enclosed in <>. It may have mixed case, just | |
761 // as the mail client sent it. We dup the string and convert | |
762 // the duplicate to lower case. | |
763 // | |
764 char *to_lower_string(char *email); | |
765 char *to_lower_string(char *email) { | |
766 int n = strlen(email)-2; | |
767 if (n < 1) return strdup(email); | |
768 char *key = strdup(email+1); | |
769 key[n] = '\0'; | |
770 for (int i=0; i<n; i++) key[i] = tolower(key[i]); | |
771 return key; | |
772 } | |
773 | |
774 | |
775 //////////////////////////////////////////////// | |
0 | 776 // start of sendmail milter interfaces |
777 // | |
778 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr) | |
779 { | |
76 | 780 if (debug_syslog) my_syslog("mlfi_connect"); |
0 | 781 // allocate some private memory |
782 mlfiPriv *priv = new mlfiPriv; | |
783 if (hostaddr->sa_family == AF_INET) { | |
784 priv->ip = ((struct sockaddr_in *)hostaddr)->sin_addr.s_addr; | |
785 } | |
786 | |
787 // save the private data | |
788 smfi_setpriv(ctx, (void*)priv); | |
789 | |
790 // continue processing | |
791 return SMFIS_CONTINUE; | |
792 } | |
793 | |
794 sfsistat mlfi_envfrom(SMFICTX *ctx, char **from) | |
795 { | |
796 mlfiPriv &priv = *MLFIPRIV; | |
76 | 797 if (debug_syslog) my_syslog(&priv, "mlfi_envfrom"); |
75 | 798 priv.mailaddr = to_lower_string(from[0]); |
0 | 799 priv.authenticated = (smfi_getsymval(ctx, "{auth_authen}") != NULL); |
800 return SMFIS_CONTINUE; | |
801 } | |
802 | |
803 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **rcpt) | |
804 { | |
805 DNSBLP rejectlist = NULL; // list that caused the reject | |
806 mlfiPriv &priv = *MLFIPRIV; | |
76 | 807 if (debug_syslog) my_syslog(&priv, "mlfi_envrcpt"); |
0 | 808 CONFIG &dc = *priv.pc; |
42 | 809 if (!priv.queueid) priv.queueid = strdup(smfi_getsymval(ctx, "i")); |
75 | 810 char *rcptaddr = rcpt[0]; |
811 char *loto = to_lower_string(rcptaddr); | |
76 | 812 if (debug_syslog) my_syslog(&priv, "finding context"); |
813 CONTEXT &con = *(dc.find_context(loto)->find_context(priv.mailaddr)); | |
814 if (debug_syslog) { | |
815 char buf[maxlen]; | |
816 char msg[maxlen]; | |
817 snprintf(msg, sizeof(msg), "from <%s> to <%s> using context %s", priv.mailaddr, loto, con.get_full_name(buf,maxlen)); | |
818 my_syslog(&priv, msg); | |
819 } | |
820 if (debug_syslog) my_syslog(&priv, "finding from value"); | |
72 | 821 char *fromvalue = con.find_from(priv.mailaddr); |
75 | 822 free(loto); |
72 | 823 status st; |
824 if (fromvalue == token_black) { | |
825 st = black; | |
0 | 826 } |
72 | 827 else if (fromvalue == token_white) { |
0 | 828 st = white; |
829 } | |
830 else { | |
72 | 831 // check the dns based lists |
76 | 832 if (debug_syslog) my_syslog(&priv, "checking dns lists"); |
74 | 833 st = (check_dnsbl(priv, con.get_dnsbl_list(), rejectlist)) ? black : oksofar; |
0 | 834 } |
835 if (st == reject) { | |
836 // reject the recipient based on some dnsbl | |
837 char adr[sizeof "255.255.255.255"]; | |
838 adr[0] = '\0'; | |
8 | 839 inet_ntop(AF_INET, (const u_char *)&priv.ip, adr, sizeof(adr)); |
76 | 840 char buf[maxlen]; |
0 | 841 snprintf(buf, sizeof(buf), rejectlist->message, adr, adr); |
842 smfi_setreply(ctx, "550", "5.7.1", buf); | |
843 return SMFIS_REJECT; | |
844 } | |
845 else if (st == black) { | |
846 // reject the recipient based on blacklisting either from or to | |
847 smfi_setreply(ctx, "550", "5.7.1", "no such user"); | |
848 return SMFIS_REJECT; | |
849 } | |
850 else { | |
851 // accept the recipient | |
76 | 852 if (debug_syslog) my_syslog(&priv, "checking content filtering"); |
72 | 853 if (!con.get_content_filtering()) st = white; |
8 | 854 if (st == oksofar) { |
855 // but remember the non-whites | |
72 | 856 priv.need_content_filter(rcptaddr, con); |
8 | 857 priv.only_whites = false; |
858 } | |
859 if (st == white) { | |
860 priv.have_whites = true; | |
861 } | |
0 | 862 return SMFIS_CONTINUE; |
863 } | |
864 } | |
865 | |
8 | 866 sfsistat mlfi_body(SMFICTX *ctx, u_char *data, size_t len) |
0 | 867 { |
868 mlfiPriv &priv = *MLFIPRIV; | |
76 | 869 if (debug_syslog) my_syslog(&priv, "mlfi_body"); |
55
44babba1a9b9
updates for 3.6, better documentation on removing content filtering, missing some files in cvs
carl
parents:
53
diff
changeset
|
870 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
|
871 if (priv.only_whites) return SMFIS_CONTINUE; |
8 | 872 priv.scanner->scan(data, len); |
11 | 873 return SMFIS_CONTINUE; |
8 | 874 } |
875 | |
876 sfsistat mlfi_eom(SMFICTX *ctx) | |
877 { | |
76 | 878 if (debug_syslog) my_syslog("mlfi_eom"); |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
879 sfsistat rc; |
8 | 880 mlfiPriv &priv = *MLFIPRIV; |
76 | 881 if (debug_syslog) my_syslog(&priv, "mlfi_eom"); |
72 | 882 CONFIG &dc = *priv.pc; |
27
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
883 char *host = NULL; |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
884 int ip; |
43a4f6b3e668
add configurable host name limit and bad html tag limits.
carl
parents:
26
diff
changeset
|
885 status st; |
8 | 886 // process end of message |
72 | 887 if (priv.authenticated || priv.only_whites) rc = SMFIS_CONTINUE; |
8 | 888 else { |
76 | 889 // assert env_to not empty |
890 char buf[maxlen]; | |
72 | 891 char *msg = NULL; |
892 string_set alive; | |
893 bool random = false; | |
74 | 894 int limit = 0; |
895 for (context_map::iterator i=priv.env_to.begin(); i!=priv.env_to.end(); i++) { | |
72 | 896 char *rcpt = (*i).first; |
897 CONTEXT &con = *((*i).second); | |
74 | 898 if (!con.acceptable_content(*priv.memory, msg)) { |
72 | 899 // bad html tags or excessive hosts |
900 smfi_delrcpt(ctx, rcpt); | |
24 | 901 } |
902 else { | |
72 | 903 alive.insert(rcpt); |
904 random |= con.get_host_random(); | |
905 limit = max(limit, con.get_host_limit()); | |
906 } | |
907 } | |
76 | 908 bool rejecting = alive.empty(); // if alive is empty, we must have set msg above in acceptable_content() |
72 | 909 if (!rejecting) { |
76 | 910 if (check_hosts(priv, random, limit, host, ip)) { |
24 | 911 char adr[sizeof "255.255.255.255"]; |
912 adr[0] = '\0'; | |
913 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr)); | |
76 | 914 snprintf(buf, sizeof(buf), priv.content_message, host, adr); |
72 | 915 msg = buf; |
76 | 916 rejecting = true; |
24 | 917 } |
72 | 918 } |
919 if (!rejecting) { | |
920 rc = SMFIS_CONTINUE; | |
921 } | |
76 | 922 else if (!priv.have_whites) { |
72 | 923 // can reject the entire message |
924 smfi_setreply(ctx, "550", "5.7.1", msg); | |
8 | 925 rc = SMFIS_REJECT; |
926 } | |
927 else { | |
928 // need to accept it but remove the recipients that don't want it | |
72 | 929 for (string_set::iterator i=alive.begin(); i!=alive.end(); i++) { |
8 | 930 char *rcpt = *i; |
931 smfi_delrcpt(ctx, rcpt); | |
932 } | |
933 rc = SMFIS_CONTINUE; | |
934 } | |
0 | 935 } |
8 | 936 // reset for a new message on the same connection |
937 mlfi_abort(ctx); | |
938 return rc; | |
939 } | |
940 | |
941 sfsistat mlfi_abort(SMFICTX *ctx) | |
942 { | |
943 mlfiPriv &priv = *MLFIPRIV; | |
76 | 944 if (debug_syslog) my_syslog(&priv, "mlfi_abort"); |
8 | 945 priv.reset(); |
0 | 946 return SMFIS_CONTINUE; |
947 } | |
948 | |
949 sfsistat mlfi_close(SMFICTX *ctx) | |
950 { | |
951 mlfiPriv *priv = MLFIPRIV; | |
76 | 952 if (debug_syslog) my_syslog(priv, "mlfi_close"); |
0 | 953 if (!priv) return SMFIS_CONTINUE; |
954 delete priv; | |
955 smfi_setpriv(ctx, NULL); | |
956 return SMFIS_CONTINUE; | |
957 } | |
958 | |
959 struct smfiDesc smfilter = | |
960 { | |
961 "DNSBL", // filter name | |
962 SMFI_VERSION, // version code -- do not change | |
963 SMFIF_DELRCPT, // flags | |
964 mlfi_connect, // connection info filter | |
965 NULL, // SMTP HELO command filter | |
966 mlfi_envfrom, // envelope sender filter | |
967 mlfi_envrcpt, // envelope recipient filter | |
968 NULL, // header filter | |
969 NULL, // end of header | |
8 | 970 mlfi_body, // body block filter |
971 mlfi_eom, // end of message | |
972 mlfi_abort, // message aborted | |
0 | 973 mlfi_close, // connection cleanup |
974 }; | |
975 | |
976 | |
977 //////////////////////////////////////////////// | |
978 // reload the config | |
979 // | |
72 | 980 CONFIG* new_conf(); |
981 CONFIG* new_conf() { | |
0 | 982 CONFIG *newc = new CONFIG; |
29
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
983 pthread_mutex_lock(&config_mutex); |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
984 newc->generation = generation++; |
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
985 pthread_mutex_unlock(&config_mutex); |
76 | 986 char buf[maxlen]; |
29
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
987 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
|
988 my_syslog(buf); |
74 | 989 if (load_conf(*newc, "dnsbl.conf")) { |
72 | 990 newc->load_time = time(NULL); |
991 return newc; | |
992 } | |
993 delete newc; | |
994 return NULL; | |
0 | 995 } |
996 | |
997 | |
998 //////////////////////////////////////////////// | |
999 // thread to watch the old config files for changes | |
1000 // and reload when needed. we also cleanup old | |
1001 // configs whose reference count has gone to zero. | |
1002 // | |
72 | 1003 void* config_loader(void *arg); |
1004 void* config_loader(void *arg) { | |
0 | 1005 typedef set<CONFIG *> configp_set; |
1006 configp_set old_configs; | |
18 | 1007 while (loader_run) { |
0 | 1008 sleep(180); // look for modifications every 3 minutes |
18 | 1009 if (!loader_run) break; |
0 | 1010 CONFIG &dc = *config; |
1011 time_t then = dc.load_time; | |
1012 struct stat st; | |
1013 bool reload = false; | |
74 | 1014 for (string_set::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) { |
0 | 1015 char *fn = *i; |
1016 if (stat(fn, &st)) reload = true; // file disappeared | |
1017 else if (st.st_mtime > then) reload = true; // file modified | |
1018 if (reload) break; | |
1019 } | |
1020 if (reload) { | |
1021 CONFIG *newc = new_conf(); | |
1022 // replace the global config pointer | |
1023 pthread_mutex_lock(&config_mutex); | |
1024 CONFIG *old = config; | |
1025 config = newc; | |
1026 pthread_mutex_unlock(&config_mutex); | |
1027 if (old) old_configs.insert(old); | |
1028 } | |
1029 // now look for old configs with zero ref counts | |
1030 for (configp_set::iterator i=old_configs.begin(); i!=old_configs.end(); ) { | |
1031 CONFIG *old = *i; | |
1032 if (!old->reference_count) { | |
76 | 1033 char buf[maxlen]; |
29
4dfdf33f1db0
add syslog msg freeing memory, use bare tld names without leading period
carl
parents:
28
diff
changeset
|
1034 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
|
1035 my_syslog(buf); |
0 | 1036 delete old; // destructor does all the work |
1037 old_configs.erase(i++); | |
1038 } | |
1039 else i++; | |
1040 } | |
1041 } | |
18 | 1042 return NULL; |
0 | 1043 } |
1044 | |
1045 | |
72 | 1046 void usage(char *prog); |
1047 void usage(char *prog) | |
0 | 1048 { |
75 | 1049 fprintf(stderr, "Usage: %s [-d] [-c] [-s] [-e from|to] -r port -p sm-sock-addr [-t timeout]\n", prog); |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1050 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
|
1051 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
|
1052 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
|
1053 fprintf(stderr, " and should be one of\n"); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1054 fprintf(stderr, " inet:port@ip-address\n"); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1055 fprintf(stderr, " local:local-domain-socket-file-name\n"); |
9 | 1056 fprintf(stderr, "-c will load and dump the config to stdout\n"); |
75 | 1057 fprintf(stderr, "-s will stress test the config loading code by repeating the load/free cycle\n"); |
1058 fprintf(stderr, " in an infinte loop.\n"); | |
16 | 1059 fprintf(stderr, "-d will add some syslog debug messages\n"); |
75 | 1060 fprintf(stderr, "-e will print the results of looking up the from and to addresses in the\n"); |
1061 fprintf(stderr, " current config. The | character is used to separate the from and to\n"); | |
1062 fprintf(stderr, " addresses in the argument to the -e switch\n"); | |
0 | 1063 } |
1064 | |
1065 | |
42 | 1066 |
72 | 1067 void setup_socket(char *sock); |
1068 void setup_socket(char *sock) { | |
42 | 1069 unlink(sock); |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1070 // sockaddr_un addr; |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1071 // memset(&addr, '\0', sizeof addr); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1072 // addr.sun_family = AF_UNIX; |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1073 // 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
|
1074 // int s = socket(AF_UNIX, SOCK_STREAM, 0); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1075 // bind(s, (sockaddr*)&addr, sizeof(addr)); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1076 // close(s); |
42 | 1077 } |
1078 | |
1079 | |
65 | 1080 /* |
1081 * The signal handler function -- only gets called when a SIGCHLD | |
1082 * is received, ie when a child terminates | |
1083 */ | |
1084 void sig_chld(int signo) | |
1085 { | |
1086 int status; | |
1087 /* Wait for any child without blocking */ | |
1088 while (waitpid(-1, &status, WNOHANG) > 0) { | |
1089 // ignore child exit status, we only do this to cleanup zombies | |
1090 } | |
1091 } | |
1092 | |
1093 | |
0 | 1094 int main(int argc, char**argv) |
1095 { | |
72 | 1096 token_init(); |
3 | 1097 bool check = false; |
75 | 1098 bool stress = false; |
3 | 1099 bool setconn = false; |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1100 bool setreso = false; |
75 | 1101 char *email = NULL; |
0 | 1102 int c; |
75 | 1103 const char *args = "r:p:t:e:cdhs"; |
0 | 1104 extern char *optarg; |
1105 | |
1106 // Process command line options | |
1107 while ((c = getopt(argc, argv, args)) != -1) { | |
1108 switch (c) { | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1109 case 'r': |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1110 if (optarg == NULL || *optarg == '\0') { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1111 fprintf(stderr, "Illegal resolver socket: %s\n", optarg); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1112 exit(EX_USAGE); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1113 } |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1114 resolver_port = strdup(optarg); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1115 setup_socket(resolver_port); |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1116 setreso = true; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1117 break; |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1118 |
0 | 1119 case 'p': |
1120 if (optarg == NULL || *optarg == '\0') { | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1121 fprintf(stderr, "Illegal sendmail socket: %s\n", optarg); |
0 | 1122 exit(EX_USAGE); |
1123 } | |
1124 if (smfi_setconn(optarg) == MI_FAILURE) { | |
1125 fprintf(stderr, "smfi_setconn failed\n"); | |
1126 exit(EX_SOFTWARE); | |
1127 } | |
42 | 1128 if (strncasecmp(optarg, "unix:", 5) == 0) setup_socket(optarg + 5); |
1129 else if (strncasecmp(optarg, "local:", 6) == 0) setup_socket(optarg + 6); | |
3 | 1130 setconn = true; |
0 | 1131 break; |
1132 | |
1133 case 't': | |
1134 if (optarg == NULL || *optarg == '\0') { | |
1135 fprintf(stderr, "Illegal timeout: %s\n", optarg); | |
1136 exit(EX_USAGE); | |
1137 } | |
1138 if (smfi_settimeout(atoi(optarg)) == MI_FAILURE) { | |
1139 fprintf(stderr, "smfi_settimeout failed\n"); | |
1140 exit(EX_SOFTWARE); | |
1141 } | |
1142 break; | |
1143 | |
75 | 1144 case 'e': |
1145 if (email) free(email); | |
1146 email = strdup(optarg); | |
1147 break; | |
1148 | |
3 | 1149 case 'c': |
1150 check = true; | |
1151 break; | |
1152 | |
75 | 1153 case 's': |
1154 stress = true; | |
1155 break; | |
1156 | |
16 | 1157 case 'd': |
1158 debug_syslog = true; | |
1159 break; | |
1160 | |
0 | 1161 case 'h': |
1162 default: | |
1163 usage(argv[0]); | |
1164 exit(EX_USAGE); | |
1165 } | |
1166 } | |
5 | 1167 |
1168 if (check) { | |
76 | 1169 use_syslog = false; |
1170 debug_syslog = true; | |
72 | 1171 CONFIG *conf = new_conf(); |
1172 if (conf) { | |
1173 conf->dump(); | |
1174 delete conf; | |
1175 return 0; | |
1176 } | |
1177 else { | |
1178 return 1; // config failed to load | |
1179 } | |
5 | 1180 } |
1181 | |
75 | 1182 if (stress) { |
1183 fprintf(stdout, "stress testing\n"); | |
1184 while (1) { | |
1185 for (int i=0; i<10; i++) { | |
1186 CONFIG *conf = new_conf(); | |
1187 if (conf) delete conf; | |
1188 } | |
1189 fprintf(stdout, "."); | |
1190 fflush(stdout); | |
1191 sleep(1); | |
1192 } | |
1193 } | |
1194 | |
1195 if (email) { | |
1196 char *x = strchr(email, '|'); | |
1197 if (x) { | |
1198 *x = '\0'; | |
1199 char *from = strdup(email); | |
1200 char *to = strdup(x+1); | |
1201 use_syslog = false; | |
1202 CONFIG *conf = new_conf(); | |
1203 if (conf) { | |
1204 CONTEXTP con = conf->find_context(to); | |
1205 char buf[maxlen]; | |
1206 fprintf(stdout, "envelope to <%s> finds context %s\n", to, con->get_full_name(buf,maxlen)); | |
1207 CONTEXTP fc = con->find_context(from); | |
1208 fprintf(stdout, "envelope from <%s> finds context %s\n", from, fc->get_full_name(buf,maxlen)); | |
1209 char *st = fc->find_from(from); | |
1210 fprintf(stdout, "envelope from <%s> finds status %s\n", from, st); | |
1211 delete conf; | |
1212 } | |
1213 } | |
1214 return 0; | |
1215 } | |
1216 | |
0 | 1217 if (!setconn) { |
1218 fprintf(stderr, "%s: Missing required -p argument\n", argv[0]); | |
1219 usage(argv[0]); | |
1220 exit(EX_USAGE); | |
1221 } | |
5 | 1222 |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1223 if (!setreso) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1224 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
|
1225 usage(argv[0]); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1226 exit(EX_USAGE); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1227 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1228 |
0 | 1229 if (smfi_register(smfilter) == MI_FAILURE) { |
1230 fprintf(stderr, "smfi_register failed\n"); | |
1231 exit(EX_UNAVAILABLE); | |
1232 } | |
1233 | |
1234 // switch to background mode | |
1235 if (daemon(1,0) < 0) { | |
1236 fprintf(stderr, "daemon() call failed\n"); | |
1237 exit(EX_UNAVAILABLE); | |
1238 } | |
1239 | |
1240 // write the pid | |
1241 const char *pidpath = "/var/run/dnsbl.pid"; | |
1242 unlink(pidpath); | |
1243 FILE *f = fopen(pidpath, "w"); | |
1244 if (f) { | |
22 | 1245 #ifdef linux |
1246 // from a comment in the DCC source code: | |
1247 // Linux threads are broken. Signals given the | |
1248 // original process are delivered to only the | |
1249 // thread that happens to have that PID. The | |
1250 // sendmail libmilter thread that needs to hear | |
1251 // SIGINT and other signals does not, and that breaks | |
1252 // scripts that need to stop milters. | |
1253 // However, signaling the process group works. | |
0 | 1254 fprintf(f, "-%d\n", (u_int)getpgrp()); |
22 | 1255 #else |
1256 fprintf(f, "%d\n", (u_int)getpid()); | |
1257 #endif | |
0 | 1258 fclose(f); |
1259 } | |
1260 | |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1261 // initialize the thread sync objects |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1262 pthread_mutex_init(&config_mutex, 0); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1263 pthread_mutex_init(&syslog_mutex, 0); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1264 pthread_mutex_init(&resolve_mutex, 0); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1265 pthread_mutex_init(&fd_pool_mutex, 0); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1266 |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1267 // drop root privs |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1268 struct passwd *pw = getpwnam("dnsbl"); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1269 if (pw) { |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1270 if (setgid(pw->pw_gid) == -1) { |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1271 my_syslog("failed to switch to group dnsbl"); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1272 } |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1273 if (setuid(pw->pw_uid) == -1) { |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1274 my_syslog("failed to switch to user dnsbl"); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1275 } |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1276 } |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1277 |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1278 // fork off the resolver listener process |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1279 pid_t child = fork(); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1280 if (child < 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1281 my_syslog("failed to create resolver listener process"); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1282 exit(0); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1283 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1284 if (child == 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1285 // we are the child - dns resolver listener process |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1286 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
|
1287 if (resolver_socket < 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1288 my_syslog("child failed to create resolver socket"); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1289 exit(0); // failed |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1290 } |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1291 sockaddr_un server; |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1292 memset(&server, '\0', sizeof(server)); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1293 server.sun_family = AF_UNIX; |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1294 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
|
1295 //try to bind the address to the socket. |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1296 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
|
1297 // bind failed |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1298 shutdown(resolver_socket, SHUT_RDWR); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1299 close(resolver_socket); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1300 my_syslog("child failed to bind resolver socket"); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1301 exit(0); // failed |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1302 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1303 //listen on the socket. |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1304 if (listen(resolver_socket, 10) < 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1305 // listen failed |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1306 shutdown(resolver_socket, SHUT_RDWR); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1307 close(resolver_socket); |
65 | 1308 my_syslog("child failed to listen to resolver socket"); |
1309 exit(0); // failed | |
1310 } | |
1311 // setup sigchld handler to prevent zombies | |
1312 struct sigaction act; | |
1313 act.sa_handler = sig_chld; // Assign sig_chld as our SIGCHLD handler | |
1314 sigemptyset(&act.sa_mask); // We don't want to block any other signals in this example | |
1315 act.sa_flags = SA_NOCLDSTOP; // only want children that have terminated | |
1316 if (sigaction(SIGCHLD, &act, NULL) < 0) { | |
1317 my_syslog("child failed to setup SIGCHLD handler"); | |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1318 exit(0); // failed |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1319 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1320 while (true) { |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1321 sockaddr_un client; |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1322 socklen_t clientlen = sizeof(client); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1323 int s = accept(resolver_socket, (sockaddr *)&client, &clientlen); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1324 if (s > 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1325 // 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
|
1326 // fork off a process to handle this connection |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1327 int newchild = fork(); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1328 if (newchild == 0) { |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1329 // this is the worker process |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1330 // child does not need the listening socket |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1331 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
|
1332 //my_syslog("child forked a worker process"); |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1333 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
|
1334 //my_syslog("child terminated a worker process"); |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1335 exit(0); |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1336 } |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1337 else { |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1338 // this is the parent |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1339 // parent does not need the accepted socket |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1340 close(s); |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1341 } |
59
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1342 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1343 } |
510a511ad554
Add resolver processes to allow better performance on busy machines
carl
parents:
58
diff
changeset
|
1344 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
|
1345 } |
60
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1346 else { |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1347 sleep(2); // allow child to get started |
390ed250c5d2
use local unix domain socket for resolver process communication
carl
parents:
59
diff
changeset
|
1348 } |
48 | 1349 |
1350 // load the initial config | |
1351 config = new_conf(); | |
1352 | |
1353 // only create threads after the fork() in daemon | |
1354 pthread_t tid; | |
1355 if (pthread_create(&tid, 0, config_loader, 0)) | |
1356 my_syslog("failed to create config loader thread"); | |
1357 if (pthread_detach(tid)) | |
1358 my_syslog("failed to detach config loader thread"); | |
1359 | |
18 | 1360 time_t starting = time(NULL); |
1361 int rc = smfi_main(); | |
22 | 1362 if ((rc != MI_SUCCESS) && (time(NULL) > starting+5*60)) { |
18 | 1363 my_syslog("trying to restart after smfi_main()"); |
1364 loader_run = false; // eventually the config loader thread will terminate | |
1365 execvp(argv[0], argv); | |
1366 } | |
1367 exit((rc == MI_SUCCESS) ? 0 : EX_UNAVAILABLE); | |
0 | 1368 } |
8 | 1369 |