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