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