Mercurial > dnsbl
annotate src/dnsbl.cpp @ 225:8e1cbf3d96fc stable-6-0-20
simple dns lookup failure need not kill the resolver process
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Mon, 29 Dec 2008 18:12:22 -0800 |
parents | da9e7f1c8160 |
children | 3fee608becbc |
rev | line source |
---|---|
94 | 1 /* |
2 | |
152 | 3 Copyright (c) 2007 Carl Byington - 510 Software Group, released under |
4 the GPL version 3 or any later version at your choice available at | |
5 http://www.gnu.org/licenses/gpl-3.0.txt | |
94 | 6 |
7 Based on a sample milter Copyright (c) 2000-2003 Sendmail, Inc. and its | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
8 suppliers. Inspired by the DCC by Rhyolite Software |
94 | 9 |
177 | 10 -b port The port used to talk to the dcc interface daemon |
94 | 11 -r port The port used to talk to our internal dns resolver processes |
12 -p port The port through which the MTA will connect to this milter. | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
13 -t sec The timeout value. |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
14 -c Check the config, and print a copy to stdout. Don't start the |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
15 milter or do anything with the socket. |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
16 -s Stress test by loading and deleting the current config in a loop. |
163 | 17 -d level set the debug level |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
18 -e f|t Print the results of looking up from address f and to address |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
19 t in the current config |
94 | 20 |
21 */ | |
22 | |
23 | |
24 // from sendmail sample | |
25 #include <sys/types.h> | |
26 #include <sys/stat.h> | |
27 #include <errno.h> | |
28 #include <sysexits.h> | |
29 #include <unistd.h> | |
30 | |
31 // needed for socket io | |
32 #include <sys/ioctl.h> | |
33 #include <net/if.h> | |
34 #include <arpa/inet.h> | |
35 #include <netinet/in.h> | |
36 #include <netinet/tcp.h> | |
37 #include <netdb.h> | |
38 #include <sys/socket.h> | |
39 #include <sys/un.h> | |
40 | |
41 // needed for thread | |
42 #include <pthread.h> | |
43 | |
44 // needed for std c++ collections | |
45 #include <set> | |
46 #include <map> | |
47 #include <list> | |
48 | |
49 // for the dns resolver | |
50 #include <netinet/in.h> | |
51 #include <arpa/nameser.h> | |
52 #include <resolv.h> | |
53 | |
54 // misc stuff needed here | |
55 #include <ctype.h> | |
56 #include <syslog.h> | |
57 #include <pwd.h> | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
58 #include <sys/wait.h> /* header for waitpid() and various macros */ |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
59 #include <signal.h> /* header for signal functions */ |
94 | 60 |
61 #include "includes.h" | |
62 | |
211
4db1457cd11a
Extend auto-whitelisting when receiving mail even if the auto whitelist is specified in a parent context.
Carl Byington <carl@five-ten-sg.com>
parents:
203
diff
changeset
|
63 #ifndef HAVE_DAEMON |
4db1457cd11a
Extend auto-whitelisting when receiving mail even if the auto whitelist is specified in a parent context.
Carl Byington <carl@five-ten-sg.com>
parents:
203
diff
changeset
|
64 #include "daemon.h" |
4db1457cd11a
Extend auto-whitelisting when receiving mail even if the auto whitelist is specified in a parent context.
Carl Byington <carl@five-ten-sg.com>
parents:
203
diff
changeset
|
65 #include "daemon.c" |
4db1457cd11a
Extend auto-whitelisting when receiving mail even if the auto whitelist is specified in a parent context.
Carl Byington <carl@five-ten-sg.com>
parents:
203
diff
changeset
|
66 #endif |
94 | 67 |
68 extern "C" { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
69 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
70 sfsistat mlfi_helo(SMFICTX * ctx, char *helohost); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
71 sfsistat mlfi_envfrom(SMFICTX *ctx, char **argv); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
72 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **argv); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
73 sfsistat mlfi_header(SMFICTX* ctx, char* headerf, char* headerv); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
74 sfsistat mlfi_body(SMFICTX *ctx, u_char *data, size_t len); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
75 sfsistat mlfi_eom(SMFICTX *ctx); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
76 sfsistat mlfi_abort(SMFICTX *ctx); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
77 sfsistat mlfi_close(SMFICTX *ctx); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
78 void sig_chld(int signo); |
94 | 79 } |
80 | |
81 int debug_syslog = 0; | |
82 bool syslog_opened = false; | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
83 bool use_syslog = true; // false to printf |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
84 bool loader_run = true; // used to stop the config loader thread |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
85 CONFIG *config = NULL; // protected by the config_mutex |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
86 int generation = 0; // protected by the config_mutex |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
87 const int maxlen = 1000; // used for snprintf buffers |
94 | 88 |
89 pthread_mutex_t config_mutex; | |
90 pthread_mutex_t syslog_mutex; | |
91 pthread_mutex_t resolve_mutex; | |
92 pthread_mutex_t fd_pool_mutex; | |
136 | 93 pthread_mutex_t rate_mutex; |
94 | 94 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
95 std::set<int> fd_pool; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
96 int NULL_SOCKET = -1; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
97 const time_t ERROR_SOCKET_TIME = 60; // number of seconds between attempts to open a socket to the dns resolver process |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
98 const char *resolver_port = NULL; // unix domain socket to talk to the dns resolver process |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
99 int resolver_socket = NULL_SOCKET; // socket used to listen for resolver requests |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
100 const char *dccifd_port = NULL; // unix domain socket to talk to the dcc interface daemon |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
101 time_t last_error_time; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
102 int resolver_sock_count = 0; // protected with fd_pool_mutex |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
103 int resolver_pool_size = 0; // protected with fd_pool_mutex |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
104 rcpt_rates rcpt_counts; // protected with rate_mutex |
94 | 105 |
106 | |
107 struct ns_map { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
108 // all the strings are owned by the keys/values in the ns_host string map |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
109 string_map ns_host; // nameserver name -> host name that uses this name server |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
110 ns_mapper ns_ip; // nameserver name -> ip address of the name server |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
111 ~ns_map(); |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
112 void add(const char *name, const char *refer); |
94 | 113 }; |
114 | |
115 | |
116 ns_map::~ns_map() { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
117 for (string_map::iterator i=ns_host.begin(); i!=ns_host.end(); i++) { |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
118 const char *x = (*i).first; |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
119 const char *y = (*i).second; |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
120 free((void*)x); |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
121 free((void*)y); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
122 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
123 ns_ip.clear(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
124 ns_host.clear(); |
94 | 125 } |
126 | |
127 | |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
128 void ns_map::add(const char *name, const char *refer) { |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
129 string_map::iterator i = ns_host.find(name); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
130 if (i != ns_host.end()) return; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
131 char *x = strdup(name); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
132 char *y = strdup(refer); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
133 ns_ip[x] = 0; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
134 ns_host[x] = y; |
94 | 135 |
136 } | |
137 | |
138 // packed structure to allow a single socket write to dump the | |
139 // length and the following answer. The packing attribute is gcc specific. | |
140 struct glommer { | |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
141 size_t length; |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
142 #ifdef NS_PACKETSZ |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
143 u_char answer[NS_PACKETSZ*4]; // with a resolver, we return resolver answers |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
144 #else |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
145 int answer; // without a resolver, we return a single ip4 address, 0 == no answer |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
146 #endif |
94 | 147 } __attribute__ ((packed)); |
148 | |
149 | |
150 //////////////////////////////////////////////// | |
136 | 151 // helper to manipulate recipient counts |
152 // | |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
153 int incr_rcpt_count(const char *user); |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
154 int incr_rcpt_count(const char *user) { |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
155 pthread_mutex_lock(&rate_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
156 rcpt_rates::iterator i = rcpt_counts.find(user); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
157 int c = 1; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
158 if (i == rcpt_counts.end()) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
159 user = strdup(user); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
160 rcpt_counts[user] = c; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
161 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
162 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
163 c = ++((*i).second); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
164 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
165 pthread_mutex_unlock(&rate_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
166 return c; |
136 | 167 } |
168 | |
169 //////////////////////////////////////////////// | |
94 | 170 // helper to discard the strings held by a context_map |
171 // | |
172 void discard(context_map &cm); | |
173 void discard(context_map &cm) { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
174 for (context_map::iterator i=cm.begin(); i!=cm.end(); i++) { |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
175 const char *x = (*i).first; |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
176 free((void*)x); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
177 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
178 cm.clear(); |
94 | 179 } |
180 | |
181 | |
182 //////////////////////////////////////////////// | |
183 // helper to register a string in a context_map | |
184 // | |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
185 void register_string(context_map &cm, const char *name, CONTEXT *con); |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
186 void register_string(context_map &cm, const char *name, CONTEXT *con) { |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
187 context_map::iterator i = cm.find(name); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
188 if (i != cm.end()) return; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
189 char *x = strdup(name); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
190 cm[x] = con; |
94 | 191 } |
192 | |
193 | |
194 //////////////////////////////////////////////// | |
195 // disconnect the fd from the dns resolver process | |
196 // | |
197 void my_disconnect(int sock, bool decrement = true); | |
198 void my_disconnect(int sock, bool decrement) { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
199 if (sock != NULL_SOCKET) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
200 if (decrement) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
201 pthread_mutex_lock(&fd_pool_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
202 resolver_sock_count--; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
203 pthread_mutex_unlock(&fd_pool_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
204 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
205 shutdown(sock, SHUT_RDWR); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
206 close(sock); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
207 } |
94 | 208 } |
209 | |
210 | |
211 //////////////////////////////////////////////// | |
212 // return fd connected to the dns resolver process | |
213 // | |
214 int my_connect(); | |
215 int my_connect() { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
216 // if we have had recent errors, don't even try to open the socket |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
217 if ((time(NULL) - last_error_time) < ERROR_SOCKET_TIME) return NULL_SOCKET; |
94 | 218 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
219 // nothing recent, maybe this time it will work |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
220 int sock = NULL_SOCKET; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
221 sockaddr_un server; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
222 memset(&server, '\0', sizeof(server)); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
223 server.sun_family = AF_UNIX; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
224 strncpy(server.sun_path, resolver_port, sizeof(server.sun_path)-1); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
225 sock = socket(AF_UNIX, SOCK_STREAM, 0); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
226 if (sock != NULL_SOCKET) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
227 bool rc = (connect(sock, (sockaddr *)&server, sizeof(server)) == 0); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
228 if (!rc) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
229 my_disconnect(sock, false); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
230 sock = NULL_SOCKET; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
231 last_error_time = time(NULL); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
232 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
233 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
234 else last_error_time = time(NULL); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
235 if (sock != NULL_SOCKET) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
236 pthread_mutex_lock(&fd_pool_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
237 resolver_sock_count++; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
238 pthread_mutex_unlock(&fd_pool_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
239 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
240 return sock; |
94 | 241 } |
242 | |
243 | |
244 mlfiPriv::mlfiPriv() { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
245 pthread_mutex_lock(&config_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
246 pc = config; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
247 pc->reference_count++; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
248 pthread_mutex_unlock(&config_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
249 get_fd(); |
190
004b855c6c1f
fix null pointer dereference from missing HELO command
carl
parents:
188
diff
changeset
|
250 ctx = NULL; |
004b855c6c1f
fix null pointer dereference from missing HELO command
carl
parents:
188
diff
changeset
|
251 eom = false; |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
252 ip = 0; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
253 helo = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
254 mailaddr = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
255 queueid = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
256 authenticated = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
257 client_name = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
258 have_whites = false; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
259 only_whites = true; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
260 want_spamassassin = false; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
261 want_dccgrey = false; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
262 want_dccbulk = false; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
263 is_bulk_precedence = false; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
264 content_context = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
265 memory = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
266 scanner = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
267 content_suffix = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
268 content_message = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
269 uribl_suffix = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
270 uribl_message = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
271 content_host_ignore = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
272 assassin = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
273 dccifd = NULL; |
94 | 274 } |
275 | |
276 mlfiPriv::~mlfiPriv() { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
277 return_fd(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
278 pthread_mutex_lock(&config_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
279 pc->reference_count--; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
280 bool last = (!pc->reference_count) && (pc != config); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
281 pthread_mutex_unlock(&config_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
282 if (last) delete pc; // free this config, since we were the last reference to it |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
283 if (helo) free((void*)helo); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
284 reset(true); |
94 | 285 } |
286 | |
287 void mlfiPriv::reset(bool final) { | |
194
688ec12a3c0c
delay autowhitelisting to avoid out of office reply bots
carl
parents:
193
diff
changeset
|
288 while (!delayer.empty()) { |
193
3ea79ef741a0
delay autowhitelisting to avoid out of office reply bots
carl
parents:
192
diff
changeset
|
289 DELAYWHITEP dwp = delayer.front(); |
3ea79ef741a0
delay autowhitelisting to avoid out of office reply bots
carl
parents:
192
diff
changeset
|
290 delete dwp; |
3ea79ef741a0
delay autowhitelisting to avoid out of office reply bots
carl
parents:
192
diff
changeset
|
291 delayer.pop_front(); |
3ea79ef741a0
delay autowhitelisting to avoid out of office reply bots
carl
parents:
192
diff
changeset
|
292 } |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
293 if (mailaddr) free((void*)mailaddr); |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
294 if (queueid) free((void*)queueid); |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
295 if (authenticated) free((void*)authenticated); |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
296 if (client_name) free((void*)client_name); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
297 delayer.clear(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
298 discard(env_to); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
299 if (memory) delete memory; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
300 if (scanner) delete scanner; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
301 if (assassin) delete assassin; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
302 if (dccifd) delete dccifd; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
303 if (!final) { |
190
004b855c6c1f
fix null pointer dereference from missing HELO command
carl
parents:
188
diff
changeset
|
304 ctx = NULL; |
004b855c6c1f
fix null pointer dereference from missing HELO command
carl
parents:
188
diff
changeset
|
305 eom = false; |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
306 mailaddr = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
307 queueid = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
308 authenticated = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
309 client_name = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
310 have_whites = false; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
311 only_whites = true; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
312 want_spamassassin = false; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
313 want_dccgrey = false; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
314 want_dccbulk = false; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
315 is_bulk_precedence = false; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
316 content_context = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
317 memory = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
318 scanner = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
319 content_suffix = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
320 content_message = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
321 uribl_suffix = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
322 uribl_message = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
323 content_host_ignore = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
324 assassin = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
325 dccifd = NULL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
326 } |
94 | 327 } |
328 | |
329 void mlfiPriv::get_fd() { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
330 err = true; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
331 fd = NULL_SOCKET; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
332 int result = pthread_mutex_lock(&fd_pool_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
333 if (!result) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
334 std::set<int>::iterator i; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
335 i = fd_pool.begin(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
336 if (i != fd_pool.end()) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
337 // have at least one fd in the pool |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
338 err = false; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
339 fd = *i; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
340 fd_pool.erase(fd); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
341 resolver_pool_size--; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
342 pthread_mutex_unlock(&fd_pool_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
343 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
344 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
345 // pool is empty, get a new fd |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
346 pthread_mutex_unlock(&fd_pool_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
347 fd = my_connect(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
348 err = (fd == NULL_SOCKET); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
349 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
350 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
351 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
352 // cannot lock the pool, just get a new fd |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
353 fd = my_connect(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
354 err = (fd == NULL_SOCKET); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
355 } |
94 | 356 } |
357 | |
358 void mlfiPriv::return_fd() { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
359 if (err) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
360 // this fd got a socket error, so close it, rather than returning it to the pool |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
361 my_disconnect(fd); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
362 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
363 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
364 int result = pthread_mutex_lock(&fd_pool_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
365 if (!result) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
366 if ((resolver_sock_count > resolver_pool_size*5) || (resolver_pool_size < 5)) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
367 // return the fd to the pool |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
368 fd_pool.insert(fd); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
369 resolver_pool_size++; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
370 pthread_mutex_unlock(&fd_pool_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
371 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
372 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
373 // more than 20% of the open resolver sockets are in the pool, and the |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
374 // pool as at least 5 sockets. that is enough, so just close this one. |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
375 pthread_mutex_unlock(&fd_pool_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
376 my_disconnect(fd); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
377 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
378 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
379 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
380 // could not lock the pool, so just close the fd |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
381 my_disconnect(fd); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
382 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
383 } |
94 | 384 } |
385 | |
177 | 386 size_t mlfiPriv::my_write(const char *buf, size_t len) { |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
387 if (err) return 0; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
388 size_t rs = 0; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
389 while (len) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
390 size_t ws = write(fd, buf, len); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
391 if (ws > 0) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
392 rs += ws; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
393 len -= ws; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
394 buf += ws; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
395 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
396 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
397 // peer closed the socket! |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
398 rs = 0; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
399 err = true; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
400 break; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
401 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
402 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
403 return rs; |
94 | 404 } |
405 | |
177 | 406 size_t mlfiPriv::my_read(char *buf, size_t len) { |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
407 if (err) return 0; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
408 size_t rs = 0; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
409 while (len) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
410 size_t ws = read(fd, buf, len); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
411 if (ws > 0) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
412 rs += ws; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
413 len -= ws; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
414 buf += ws; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
415 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
416 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
417 // peer closed the socket! |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
418 rs = 0; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
419 err = true; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
420 break; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
421 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
422 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
423 return rs; |
94 | 424 } |
425 | |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
426 void mlfiPriv::need_content_filter(const char *rcpt, CONTEXT &con) { |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
427 register_string(env_to, rcpt, &con); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
428 if (!memory) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
429 // first recipient that needs content filtering sets |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
430 // some of the content filtering parameters |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
431 memory = new recorder(this, con.get_html_tags(), con.get_content_tlds(), con.get_content_cctlds()); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
432 scanner = new url_scanner(memory); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
433 content_suffix = con.get_content_suffix(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
434 content_message = con.get_content_message(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
435 uribl_suffix = con.get_uribl_suffix(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
436 uribl_message = con.get_uribl_message(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
437 content_host_ignore = &con.get_content_host_ignore(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
438 } |
94 | 439 } |
440 | |
186
2a80c9b5d2c9
fix null pointer dereference from missing HELO command
carl
parents:
185
diff
changeset
|
441 |
2a80c9b5d2c9
fix null pointer dereference from missing HELO command
carl
parents:
185
diff
changeset
|
442 mlfiPriv* fetch_priv_from_ctx(SMFICTX *ctx); |
2a80c9b5d2c9
fix null pointer dereference from missing HELO command
carl
parents:
185
diff
changeset
|
443 mlfiPriv* fetch_priv_from_ctx(SMFICTX *ctx) |
2a80c9b5d2c9
fix null pointer dereference from missing HELO command
carl
parents:
185
diff
changeset
|
444 { |
2a80c9b5d2c9
fix null pointer dereference from missing HELO command
carl
parents:
185
diff
changeset
|
445 mlfiPriv *priv = (struct mlfiPriv *)smfi_getpriv(ctx); |
187
f0eda59e8afd
fix null pointer dereference from missing HELO command
carl
parents:
186
diff
changeset
|
446 priv->ctx = ctx; |
186
2a80c9b5d2c9
fix null pointer dereference from missing HELO command
carl
parents:
185
diff
changeset
|
447 return priv; |
2a80c9b5d2c9
fix null pointer dereference from missing HELO command
carl
parents:
185
diff
changeset
|
448 } |
2a80c9b5d2c9
fix null pointer dereference from missing HELO command
carl
parents:
185
diff
changeset
|
449 #define MLFIPRIV fetch_priv_from_ctx(ctx) |
2a80c9b5d2c9
fix null pointer dereference from missing HELO command
carl
parents:
185
diff
changeset
|
450 |
94 | 451 |
452 | |
453 //////////////////////////////////////////////// | |
454 // syslog a message | |
455 // | |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
456 void my_syslog(mlfiPriv *priv, const char *text) { |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
457 char buf[maxlen]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
458 if (priv) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
459 snprintf(buf, sizeof(buf), "%s: %s", priv->queueid, text); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
460 text = buf; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
461 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
462 if (use_syslog) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
463 pthread_mutex_lock(&syslog_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
464 if (!syslog_opened) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
465 openlog("dnsbl", LOG_PID, LOG_MAIL); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
466 syslog_opened = true; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
467 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
468 syslog(LOG_NOTICE, "%s", text); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
469 pthread_mutex_unlock(&syslog_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
470 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
471 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
472 printf("%s \n", text); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
473 } |
94 | 474 } |
475 | |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
476 void my_syslog(mlfiPriv *priv, const string text) { |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
477 if (debug_syslog > 3) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
478 char buf[maxlen]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
479 strncpy(buf, text.c_str(), sizeof(buf)); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
480 buf[maxlen-1] = '\0'; // ensure null termination |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
481 my_syslog(priv, buf); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
482 } |
163 | 483 } |
484 | |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
485 void my_syslog(const char *text) { |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
486 my_syslog(NULL, text); |
94 | 487 } |
488 | |
489 | |
490 //////////////////////////////////////////////// | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
491 // read a resolver request from the socket, process it, and |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
492 // write the result back to the socket. |
94 | 493 |
494 void process_resolver_requests(int socket); | |
495 void process_resolver_requests(int socket) { | |
496 #ifdef NS_MAXDNAME | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
497 char question[NS_MAXDNAME]; |
94 | 498 #else |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
499 char question[1000]; |
94 | 500 #endif |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
501 glommer glom; |
94 | 502 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
503 int maxq = sizeof(question); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
504 while (true) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
505 // read a question |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
506 int rs = 0; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
507 while (rs < maxq) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
508 int ns = read(socket, question+rs, maxq-rs); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
509 if (ns > 0) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
510 rs += ns; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
511 if (question[rs-1] == '\0') { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
512 // last byte read was the null terminator, we are done |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
513 break; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
514 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
515 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
516 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
517 // peer closed the socket |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
518 #ifdef RESOLVER_DEBUG |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
519 my_syslog("process_resolver_requests() peer closed socket while reading question"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
520 #endif |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
521 shutdown(socket, SHUT_RDWR); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
522 close(socket); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
523 return; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
524 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
525 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
526 question[rs-1] = '\0'; // ensure null termination |
94 | 527 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
528 // find the answer |
94 | 529 #ifdef NS_PACKETSZ |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
530 #ifdef RESOLVER_DEBUG |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
531 char text[1000]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
532 snprintf(text, sizeof(text), "process_resolver_requests() has a question %s", question); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
533 my_syslog(text); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
534 #endif |
223
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
535 int res_result = res_search(question, ns_c_in, ns_t_a, glom.answer, sizeof(glom.answer)); |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
536 if (res_result < 0) glom.length = 0; // represent all errors as zero length answers |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
537 else glom.length = (size_t)res_result; |
94 | 538 #else |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
539 glom.length = sizeof(glom.answer); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
540 glom.answer = 0; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
541 struct hostent *host = gethostbyname(question); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
542 if (host && (host->h_addrtype == AF_INET)) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
543 memcpy(&glom.answer, host->h_addr, sizeof(glom.answer)); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
544 } |
94 | 545 #endif |
546 | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
547 // write the answer |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
548 char *buf = (char *)&glom; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
549 int len = glom.length + sizeof(glom.length); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
550 #ifdef RESOLVER_DEBUG |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
551 snprintf(text, sizeof(text), "process_resolver_requests() writing answer length %d for total %d", glom.length, len); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
552 my_syslog(text); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
553 #endif |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
554 int ws = 0; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
555 while (len > ws) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
556 int ns = write(socket, buf+ws, len-ws); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
557 if (ns > 0) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
558 ws += ns; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
559 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
560 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
561 // peer closed the socket! |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
562 #ifdef RESOLVER_DEBUG |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
563 my_syslog("process_resolver_requests() peer closed socket while writing answer"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
564 #endif |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
565 shutdown(socket, SHUT_RDWR); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
566 close(socket); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
567 return; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
568 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
569 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
570 } |
94 | 571 } |
572 | |
573 | |
574 //////////////////////////////////////////////// | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
575 // ask a dns question and get an A record answer - we don't try |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
576 // very hard, just using the default resolver retry settings. |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
577 // If we cannot get an answer, we just accept the mail. |
94 | 578 // |
579 // | |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
580 int dns_interface(mlfiPriv &priv, const char *question, bool maybe_ip, ns_map *nameservers); |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
581 int dns_interface(mlfiPriv &priv, const char *question, bool maybe_ip, ns_map *nameservers) { |
186
2a80c9b5d2c9
fix null pointer dereference from missing HELO command
carl
parents:
185
diff
changeset
|
582 // tell sendmail we are still working |
2a80c9b5d2c9
fix null pointer dereference from missing HELO command
carl
parents:
185
diff
changeset
|
583 #if _FFR_SMFI_PROGRESS |
190
004b855c6c1f
fix null pointer dereference from missing HELO command
carl
parents:
188
diff
changeset
|
584 if (priv.eom) smfi_progress(priv.ctx); |
186
2a80c9b5d2c9
fix null pointer dereference from missing HELO command
carl
parents:
185
diff
changeset
|
585 #endif |
2a80c9b5d2c9
fix null pointer dereference from missing HELO command
carl
parents:
185
diff
changeset
|
586 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
587 // this part can be done without locking the resolver mutex. Each |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
588 // milter thread is talking over its own socket to a separate resolver |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
589 // process, which does the actual dns resolution. |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
590 if (priv.err) return 0; // cannot ask more questions on this socket. |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
591 if (maybe_ip) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
592 // might be a bare ip address, try this first to avoid dns lookups that may not be needed |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
593 in_addr ip; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
594 if (inet_aton(question, &ip)) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
595 return (int)ip.s_addr; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
596 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
597 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
598 int n = strlen(question); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
599 if (question[n-1] == '.') { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
600 priv.my_write(question, n+1); // write the question including the null terminator |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
601 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
602 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
603 priv.my_write(question, n); // write the question |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
604 priv.my_write(".", 2); // and the fully qualified . terminator and null string terminator |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
605 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
606 glommer glom; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
607 char *buf = (char *)&glom; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
608 priv.my_read(buf, sizeof(glom.length)); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
609 buf += sizeof(glom.length); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
610 #ifdef RESOLVER_DEBUG |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
611 char text[1000]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
612 snprintf(text, sizeof(text), "dns_interface() wrote question %s and has answer length %d", question, glom.length); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
613 my_syslog(text); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
614 #endif |
225
8e1cbf3d96fc
simple dns lookup failure need not kill the resolver process
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
615 if (glom.length == 0) return 0; |
8e1cbf3d96fc
simple dns lookup failure need not kill the resolver process
Carl Byington <carl@five-ten-sg.com>
parents:
223
diff
changeset
|
616 if (glom.length > sizeof(glom.answer)) { |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
617 priv.err = true; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
618 return 0; // cannot process overlarge answers |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
619 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
620 priv.my_read(buf, glom.length); |
94 | 621 |
622 #ifdef NS_PACKETSZ | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
623 // now we need to lock the resolver mutex to keep the milter threads from |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
624 // stepping on each other while parsing the dns answer. |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
625 int ret_address = 0; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
626 pthread_mutex_lock(&resolve_mutex); |
223
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
627 // parse the answer |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
628 ns_msg handle; |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
629 ns_rr rr; |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
630 if (ns_initparse(glom.answer, glom.length, &handle) == 0) { |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
631 // look for ns names |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
632 if (nameservers) { |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
633 ns_map &ns = *nameservers; |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
634 int rrnum = 0; |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
635 while (ns_parserr(&handle, ns_s_ns, rrnum++, &rr) == 0) { |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
636 if (ns_rr_type(rr) == ns_t_ns) { |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
637 char nam[NS_MAXDNAME+1]; |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
638 char *n = nam; |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
639 const u_char *p = ns_rr_rdata(rr); |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
640 while (((n-nam) < NS_MAXDNAME) && ((size_t)(p-glom.answer) < glom.length) && *p) { |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
641 size_t s = *(p++); |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
642 if (s > 191) { |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
643 // compression pointer |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
644 s = (s-192)*256 + *(p++); |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
645 if (s >= glom.length) break; // pointer outside bounds of answer |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
646 p = glom.answer + s; |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
647 s = *(p++); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
648 } |
223
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
649 if (s > 0) { |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
650 if ((size_t)(n-nam) >= (NS_MAXDNAME-s)) break; // destination would overflow name buffer |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
651 if ((size_t)(p-glom.answer) >= (glom.length-s)) break; // source outside bounds of answer |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
652 memcpy(n, p, s); |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
653 n += s; |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
654 p += s; |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
655 *(n++) = '.'; |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
656 } |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
657 } |
223
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
658 if (n-nam) n--; // remove trailing . |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
659 *n = '\0'; // null terminate it |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
660 ns.add(nam, question); // ns host to lookup later |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
661 } |
223
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
662 } |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
663 rrnum = 0; |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
664 while (ns_parserr(&handle, ns_s_ar, rrnum++, &rr) == 0) { |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
665 if (ns_rr_type(rr) == ns_t_a) { |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
666 char* nam = (char*)ns_rr_name(rr); |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
667 ns_mapper::iterator i = ns.ns_ip.find(nam); |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
668 if (i != ns.ns_ip.end()) { |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
669 // we want this ip address |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
670 int address; |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
671 memcpy(&address, ns_rr_rdata(rr), sizeof(address)); |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
672 ns.ns_ip[nam] = address; |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
673 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
674 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
675 } |
223
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
676 } |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
677 int rrnum = 0; |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
678 while (ns_parserr(&handle, ns_s_an, rrnum++, &rr) == 0) { |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
679 if (ns_rr_type(rr) == ns_t_a) { |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
680 int address; |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
681 memcpy(&address, ns_rr_rdata(rr), sizeof(address)); |
da9e7f1c8160
fix unsigned signed compare, back to mixed -lresolv and libresolv.a with auto requires
Carl Byington <carl@five-ten-sg.com>
parents:
222
diff
changeset
|
682 ret_address = address; |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
683 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
684 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
685 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
686 pthread_mutex_unlock(&resolve_mutex); |
222
b2a7ca398712
add dns resolver debugging
Carl Byington <carl@five-ten-sg.com>
parents:
220
diff
changeset
|
687 #ifdef RESOLVER_DEBUG |
b2a7ca398712
add dns resolver debugging
Carl Byington <carl@five-ten-sg.com>
parents:
220
diff
changeset
|
688 snprintf(text, sizeof(text), "dns_interface() found ip %d", ret_address); |
b2a7ca398712
add dns resolver debugging
Carl Byington <carl@five-ten-sg.com>
parents:
220
diff
changeset
|
689 my_syslog(text); |
b2a7ca398712
add dns resolver debugging
Carl Byington <carl@five-ten-sg.com>
parents:
220
diff
changeset
|
690 #endif |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
691 return ret_address; |
94 | 692 #else |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
693 return glom.answer; |
94 | 694 #endif |
695 } | |
696 | |
697 | |
698 //////////////////////////////////////////////// | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
699 // check a single dnsbl |
94 | 700 // |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
701 bool check_single(mlfiPriv &priv, int ip, const char *suffix); |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
702 bool check_single(mlfiPriv &priv, int ip, const char *suffix) { |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
703 // make a dns question |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
704 const u_char *src = (const u_char *)&ip; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
705 if (src[0] == 127) return false; // don't do dns lookups on localhost |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
706 if (src[0] == 10) return false; // don't do dns lookups on rfc1918 space |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
707 if ((src[0] == 192) && (src[1] == 168)) return false; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
708 if ((src[0] == 172) && (16 <= src[1]) && (src[1] <= 31)) return false; |
94 | 709 #ifdef NS_MAXDNAME |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
710 char question[NS_MAXDNAME]; |
94 | 711 #else |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
712 char question[1000]; |
94 | 713 #endif |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
714 snprintf(question, sizeof(question), "%u.%u.%u.%u.%s.", src[3], src[2], src[1], src[0], suffix); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
715 // ask the question, if we get an A record it implies a blacklisted ip address |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
716 return dns_interface(priv, question, false, NULL); |
94 | 717 } |
718 | |
719 | |
720 //////////////////////////////////////////////// | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
721 // check a single dnsbl |
94 | 722 // |
723 bool check_single(mlfiPriv &priv, int ip, DNSBL &bl); | |
724 bool check_single(mlfiPriv &priv, int ip, DNSBL &bl) { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
725 return check_single(priv, ip, bl.suffix); |
94 | 726 } |
727 | |
728 | |
729 //////////////////////////////////////////////// | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
730 // check the dnsbls specified for this recipient |
94 | 731 // |
732 bool check_dnsbl(mlfiPriv &priv, dnsblp_list &dnsbll, DNSBLP &rejectlist); | |
733 bool check_dnsbl(mlfiPriv &priv, dnsblp_list &dnsbll, DNSBLP &rejectlist) { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
734 for (dnsblp_list::iterator i=dnsbll.begin(); i!=dnsbll.end(); i++) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
735 DNSBLP dp = *i; // non null by construction |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
736 bool st; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
737 map<DNSBLP, bool>::iterator f = priv.checked.find(dp); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
738 if (f == priv.checked.end()) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
739 // have not checked this list yet |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
740 st = check_single(priv, priv.ip, *dp); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
741 rejectlist = dp; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
742 priv.checked[dp] = st; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
743 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
744 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
745 st = (*f).second; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
746 rejectlist = (*f).first; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
747 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
748 if (st) return st; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
749 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
750 return false; |
94 | 751 } |
752 | |
753 | |
754 //////////////////////////////////////////////// | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
755 // lookup the domain name part of a hostname on the uribl |
117 | 756 // |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
757 // if we find part of the hostname on the uribl, return |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
758 // true and point found to the part of the hostname that we found |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
759 // as a string registered in hosts. |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
760 // otherwise, return false and preserve the value of found. |
124 | 761 // |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
762 bool uriblookup(mlfiPriv &priv, string_set &hosts, const char *hostname, const char *top, const char *&found) ; |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
763 bool uriblookup(mlfiPriv &priv, string_set &hosts, const char *hostname, const char *top, const char *&found) { |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
764 // top is pointer to '.' char at end of base domain, or null for ip address form |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
765 // so for hostname of www.fred.mydomain.co.uk |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
766 // top points to-----------------------^ |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
767 // and we end up looking at only mydomain.co.uk, ignoring the www.fred stuff |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
768 char buf[maxlen]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
769 if (top) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
770 // add one more component |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
771 const char *x = (const char *)memrchr(hostname, '.', top-hostname); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
772 if (x) hostname = x+1; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
773 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
774 snprintf(buf, sizeof(buf), "%s.%s.", hostname, priv.uribl_suffix); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
775 if (dns_interface(priv, buf, false, NULL)) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
776 if (debug_syslog > 2) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
777 char tmp[maxlen]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
778 snprintf(tmp, sizeof(tmp), "found %s on %s", hostname, priv.uribl_suffix); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
779 my_syslog(tmp); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
780 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
781 found = register_string(hosts, hostname); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
782 return true; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
783 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
784 return false; |
117 | 785 } |
786 | |
787 | |
788 //////////////////////////////////////////////// | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
789 // uribl checker |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
790 // ------------- |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
791 // hostname MUST not have a trailing dot |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
792 // If tld, two level lookup. |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
793 // Else, look up three level domain. |
124 | 794 // |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
795 // if we find part of the hostname on the uribl, return |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
796 // true and point found to the part of the hostname that we found |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
797 // as a string registered in hosts. |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
798 // otherwise, return false and preserve the value of found. |
124 | 799 // |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
800 bool check_uribl(mlfiPriv &priv, string_set &hosts, const char *hostname, const char *&found) ; |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
801 bool check_uribl(mlfiPriv &priv, string_set &hosts, const char *hostname, const char *&found) { |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
802 in_addr ip; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
803 if (inet_aton(hostname, &ip)) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
804 const u_char *src = (const u_char *)&ip.s_addr; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
805 if (src[0] == 127) return false; // don't do dns lookups on localhost |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
806 if (src[0] == 10) return false; // don't do dns lookups on rfc1918 space |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
807 if ((src[0] == 192) && (src[1] == 168)) return false; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
808 if ((src[0] == 172) && (16 <= src[1]) && (src[1] <= 31)) return false; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
809 char adr[sizeof "255.255.255.255 "]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
810 snprintf(adr, sizeof(adr), "%u.%u.%u.%u", src[3], src[2], src[1], src[0]); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
811 // cannot use inet_ntop here since we want the octets reversed. |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
812 return (uriblookup(priv, hosts, adr, NULL, found)); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
813 } |
117 | 814 |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
815 const char *top, *top2, *top3; |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
816 top = strrchr(hostname, '.'); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
817 if (top) { |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
818 top2 = (const char *)memrchr(hostname, '.', top-hostname); |
117 | 819 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
820 if (top2) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
821 string_set::iterator i = priv.memory->get_cctlds()->find(top2+1); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
822 string_set::iterator x = priv.memory->get_cctlds()->end(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
823 // if we have a 2-level-cctld, just look at top three levels of the name |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
824 if (i != x) return uriblookup(priv, hosts, hostname, top2, found); |
117 | 825 |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
826 // if we have more than 3 levels in the name, look at the top three levels of the name |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
827 top3 = (const char *)memrchr(hostname, '.', top2-hostname); |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
828 if (top3 && uriblookup(priv, hosts, hostname, top2, found)) return true; |
117 | 829 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
830 // if that was not found, fall thru to looking at the top two levels |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
831 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
832 // look at the top two levels of the name |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
833 return uriblookup(priv, hosts, hostname, top, found); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
834 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
835 return false; |
117 | 836 } |
837 | |
838 | |
839 //////////////////////////////////////////////// | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
840 // check the hosts from the body against the content filter and uribl dnsbls |
94 | 841 // |
124 | 842 // |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
843 bool check_hosts(mlfiPriv &priv, bool random, int limit, const char *&msg, const char *&host, int &ip, const char *&found); |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
844 bool check_hosts(mlfiPriv &priv, bool random, int limit, const char *&msg, const char *&host, int &ip, const char *&found) { |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
845 found = NULL; // normally ip address style |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
846 if (!priv.content_suffix && !priv.uribl_suffix) return false; // nothing to check |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
847 string_set &hosts = priv.memory->get_hosts(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
848 string_set &ignore = *priv.content_host_ignore; |
94 | 849 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
850 int count = 0; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
851 int cnt = hosts.size(); // number of hosts we could look at |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
852 int_set ips; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
853 ns_map nameservers; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
854 for (string_set::iterator i=hosts.begin(); i!=hosts.end(); i++) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
855 host = *i; // a reference into hosts, which will live until this smtp transaction is closed |
94 | 856 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
857 // don't bother looking up hosts on the ignore list |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
858 string_set::iterator j = ignore.find(host); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
859 if (j != ignore.end()) continue; |
94 | 860 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
861 // try to only look at limit/cnt fraction of the available cnt host names in random mode |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
862 if ((cnt > limit) && (limit > 0) && random) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
863 int r = rand() % cnt; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
864 if (r >= limit) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
865 if (debug_syslog > 2) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
866 char buf[maxlen]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
867 snprintf(buf, sizeof(buf), "host %s skipped", host); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
868 my_syslog(&priv, buf); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
869 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
870 continue; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
871 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
872 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
873 count++; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
874 ip = dns_interface(priv, host, true, &nameservers); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
875 if (debug_syslog > 2) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
876 char buf[maxlen]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
877 if (ip) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
878 char adr[sizeof "255.255.255.255 "]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
879 adr[0] = '\0'; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
880 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr)); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
881 snprintf(buf, sizeof(buf), "host %s found at %s", host, adr); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
882 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
883 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
884 snprintf(buf, sizeof(buf), "host %s not found", host); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
885 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
886 my_syslog(&priv, buf); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
887 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
888 if (ip) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
889 int_set::iterator i = ips.find(ip); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
890 if (i == ips.end()) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
891 // we haven't looked this up yet |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
892 ips.insert(ip); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
893 // check dnsbl style list |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
894 if (priv.content_suffix && check_single(priv, ip, priv.content_suffix)) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
895 msg = priv.content_message; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
896 return true; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
897 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
898 // Check uribl & surbl style list |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
899 if (priv.uribl_suffix && check_uribl(priv, hosts, host, found)) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
900 msg = priv.uribl_message; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
901 return true; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
902 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
903 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
904 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
905 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
906 limit *= 4; // allow average of 3 ns per host name |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
907 for (ns_mapper::iterator i=nameservers.ns_ip.begin(); i!=nameservers.ns_ip.end(); i++) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
908 count++; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
909 if ((count > limit) && (limit > 0)) return false; // too many name servers to check them all |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
910 host = (*i).first; // a transient reference that needs to be replaced before we return it |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
911 ip = (*i).second; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
912 if (!ip) ip = dns_interface(priv, host, false, NULL); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
913 if (debug_syslog > 2) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
914 char buf[maxlen]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
915 if (ip) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
916 char adr[sizeof "255.255.255.255 "]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
917 adr[0] = '\0'; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
918 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr)); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
919 snprintf(buf, sizeof(buf), "ns %s found at %s", host, adr); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
920 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
921 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
922 snprintf(buf, sizeof(buf), "ns %s not found", host); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
923 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
924 my_syslog(&priv, buf); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
925 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
926 if (ip) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
927 int_set::iterator i = ips.find(ip); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
928 if (i == ips.end()) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
929 ips.insert(ip); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
930 if (check_single(priv, ip, priv.content_suffix)) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
931 msg = priv.content_message; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
932 string_map::iterator j = nameservers.ns_host.find(host); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
933 if (j != nameservers.ns_host.end()) { |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
934 const char *refer = (*j).second; |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
935 char buf[maxlen]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
936 snprintf(buf, sizeof(buf), "%s with nameserver %s", refer, host); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
937 host = register_string(hosts, buf); // put a copy into hosts, and return that reference |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
938 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
939 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
940 host = register_string(hosts, host); // put a copy into hosts, and return that reference |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
941 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
942 return true; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
943 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
944 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
945 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
946 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
947 return false; |
94 | 948 } |
949 | |
127 | 950 |
94 | 951 //////////////////////////////////////////////// |
127 | 952 // |
953 // this email address is passed in from sendmail, and will normally be | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
954 // enclosed in <>. I think older versions of sendmail supplied the <> |
127 | 955 // wrapper if the mail client did not, but the current version does not do |
956 // that. So the <> wrapper is now optional. It may have mixed case, just | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
957 // as the mail client sent it. We dup the string and convert the duplicate |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
958 // to lower case. Some clients enclose the entire address in single quotes, |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
959 // so we strip those as well. |
94 | 960 // |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
961 const char *to_lower_string(const char *email); |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
962 const char *to_lower_string(const char *email) { |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
963 int n = strlen(email); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
964 if (email[0] == '<') { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
965 // assume it also ends with > |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
966 n -= 2; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
967 if (n < 1) return strdup(email); // return "<>" |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
968 email++; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
969 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
970 if ((email[0] == '\'') && (email[n-1] == '\'') && (n > 2)) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
971 n -= 2; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
972 email++; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
973 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
974 char *key = strdup(email); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
975 key[n] = '\0'; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
976 for (int i=0; i<n; i++) key[i] = tolower(key[i]); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
977 return key; |
94 | 978 } |
979 | |
980 | |
981 //////////////////////////////////////////////// | |
982 // start of sendmail milter interfaces | |
983 // | |
984 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr) | |
985 { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
986 // allocate some private memory |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
987 mlfiPriv *priv = new mlfiPriv; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
988 if (hostaddr->sa_family == AF_INET) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
989 priv->ip = ((struct sockaddr_in *)hostaddr)->sin_addr.s_addr; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
990 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
991 // save the private data |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
992 smfi_setpriv(ctx, (void*)priv); |
94 | 993 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
994 // continue processing |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
995 return SMFIS_CONTINUE; |
94 | 996 } |
997 | |
163 | 998 sfsistat mlfi_helo(SMFICTX * ctx, char *helohost) |
999 { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1000 mlfiPriv &priv = *MLFIPRIV; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1001 priv.helo = strdup(helohost); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1002 return SMFIS_CONTINUE; |
163 | 1003 } |
1004 | |
94 | 1005 sfsistat mlfi_envfrom(SMFICTX *ctx, char **from) |
1006 { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1007 mlfiPriv &priv = *MLFIPRIV; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1008 priv.mailaddr = to_lower_string(from[0]); |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1009 priv.queueid = strdup(smfi_getsymval(ctx, (char*)"i")); |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1010 priv.authenticated = smfi_getsymval(ctx, (char*)"{auth_authen}"); |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1011 priv.client_name = smfi_getsymval(ctx, (char*)"_"); |
191
2a67d31099c3
fix null pointer dereference from missing HELO command
carl
parents:
190
diff
changeset
|
1012 if (!priv.helo) priv.helo = strdup("unknown"); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1013 if (priv.authenticated) priv.authenticated = strdup(priv.authenticated); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1014 if (priv.client_name) priv.client_name = strdup(priv.client_name); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1015 if (spamc != spamc_empty) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1016 priv.assassin = new SpamAssassin(&priv, priv.ip, priv.helo, priv.mailaddr, priv.queueid); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1017 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1018 if (dccifd_port) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1019 priv.dccifd = new DccInterface(dccifd_port, &priv, priv.ip, priv.helo, priv.mailaddr); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1020 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1021 return SMFIS_CONTINUE; |
94 | 1022 } |
1023 | |
1024 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **rcpt) | |
1025 { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1026 DNSBLP rejectlist = NULL; // list that caused the reject |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1027 mlfiPriv &priv = *MLFIPRIV; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1028 CONFIG &dc = *priv.pc; |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1029 const char *rcptaddr = rcpt[0]; |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1030 const char *loto = to_lower_string(rcptaddr); |
216
784030ac71f1
Never whitelist self addressed mail. Changes for Fedora 10 and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
214
diff
changeset
|
1031 bool self = (strcmp(loto, priv.mailaddr) == 0); |
174 | 1032 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1033 // some version of sendmail allowed rcpt to:<> and passed it thru to the milters |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1034 if (strcmp(loto, "<>") == 0) { |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1035 smfi_setreply(ctx, (char*)"550", (char*)"5.7.1", (char*)"bogus recipient"); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1036 return SMFIS_REJECT; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1037 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1038 // priv.mailaddr sending original message to loto |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1039 CONTEXT &con = *(dc.find_context(loto)->find_context(priv.mailaddr)); |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1040 VERIFYP ver = con.find_verify(loto); |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1041 const char *fromvalue = con.find_from(priv.mailaddr, true); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1042 // tell spam assassin and dccifd about this recipient |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1043 if (priv.assassin) priv.assassin->mlfi_envrcpt(ctx, loto); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1044 if (priv.dccifd) priv.dccifd->mlfi_envrcpt(ctx, loto, con.get_grey() && !priv.authenticated); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1045 // loto sending a reply back to priv.mailaddr |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1046 CONTEXT &con2 = *(dc.find_context(priv.mailaddr)->find_context(loto)); |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1047 const char *replyvalue = con2.find_from(loto); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1048 if (debug_syslog > 1) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1049 char buf[maxlen]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1050 char msg[maxlen]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1051 snprintf(msg, sizeof(msg), "from <%s> to <%s> using context %s state %s reply state %s", priv.mailaddr, loto, con.get_full_name(buf,maxlen), fromvalue, replyvalue); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1052 my_syslog(&priv, msg); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1053 } |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1054 free((void*)loto); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1055 status st; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1056 if (replyvalue == token_black) { |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1057 smfi_setreply(ctx, (char*)"550", (char*)"5.7.1", (char*)"recipient can not reply due to blacklisting"); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1058 return SMFIS_REJECT; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1059 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1060 if (priv.authenticated) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1061 int c = incr_rcpt_count(priv.authenticated); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1062 int l = dc.default_context->find_rate(priv.authenticated); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1063 if (debug_syslog > 1) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1064 char msg[maxlen]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1065 snprintf(msg, sizeof(msg), "authenticated id %s (%d recipients, %d limit)", priv.authenticated, c, l); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1066 my_syslog(&priv, msg); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1067 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1068 if (c > l) { |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1069 smfi_setreply(ctx, (char*)"550", (char*)"5.7.1", (char*)"recipient rate limit exceeded"); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1070 return SMFIS_REJECT; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1071 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1072 st = white; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1073 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1074 else if (fromvalue == token_black) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1075 st = black; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1076 } |
216
784030ac71f1
Never whitelist self addressed mail. Changes for Fedora 10 and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
214
diff
changeset
|
1077 else if ((fromvalue == token_white) && !self) { |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1078 st = white; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1079 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1080 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1081 // check the dns based lists |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1082 st = (check_dnsbl(priv, con.get_dnsbl_list(), rejectlist)) ? reject : oksofar; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1083 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1084 if (st == reject) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1085 // reject the recipient based on some dnsbl |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1086 char adr[sizeof "255.255.255.255 "]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1087 adr[0] = '\0'; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1088 inet_ntop(AF_INET, (const u_char *)&priv.ip, adr, sizeof(adr)); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1089 char buf[maxlen]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1090 snprintf(buf, sizeof(buf), rejectlist->message, adr, adr); |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1091 smfi_setreply(ctx, (char*)"550", (char*)"5.7.1", buf); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1092 return SMFIS_REJECT; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1093 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1094 if (st == oksofar) { |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1095 const char *msg = con.generic_match(priv.client_name); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1096 if (msg) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1097 // reject the recipient based on generic reverse dns |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1098 char buf[maxlen]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1099 snprintf(buf, sizeof(buf), msg, priv.client_name); |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1100 smfi_setreply(ctx, (char*)"550", (char*)"5.7.1", buf); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1101 return SMFIS_REJECT; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1102 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1103 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1104 if (st == black) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1105 // reject the recipient based on blacklisting either from or to |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1106 smfi_setreply(ctx, (char*)"550", (char*)"5.7.1", (char*)"no such user"); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1107 return SMFIS_REJECT; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1108 } |
203
92a5c866bdfa
Verify from/to pairs even if they might be explicitly whitelisted.
Carl Byington <carl@five-ten-sg.com>
parents:
194
diff
changeset
|
1109 if (ver) { |
92a5c866bdfa
Verify from/to pairs even if they might be explicitly whitelisted.
Carl Byington <carl@five-ten-sg.com>
parents:
194
diff
changeset
|
1110 // try to verify this from/to pair of addresses even if it might be explicitly whitelisted |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1111 const char *loto = to_lower_string(rcptaddr); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1112 bool rc = ver->ok(priv.mailaddr, loto); |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1113 free((void*)loto); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1114 if (!rc) { |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1115 smfi_setreply(ctx, (char*)"550", (char*)"5.7.1", (char*)"no such user"); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1116 return SMFIS_REJECT; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1117 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1118 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1119 // we will accept the recipient, but add an auto-whitelist entry |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1120 // if needed to ensure we can accept replies |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1121 loto = to_lower_string(rcptaddr); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1122 WHITELISTERP w = con2.find_autowhite(loto, priv.mailaddr); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1123 // check if local part is too big |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1124 const int max_local_size = 30; |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1125 const char *p = strchr(loto, '@'); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1126 int len = (p) ? p-loto : max_local_size; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1127 if (len >= max_local_size) w = NULL; // too big, pretend we don't have a whitelister |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1128 // record it if we have a whitelister |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1129 if (w) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1130 DELAYWHITEP dwp = new DELAYWHITE(loto, w, &con2); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1131 priv.delayer.push_back(dwp); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1132 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1133 else { |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1134 free((void*)loto); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1135 } |
179 | 1136 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1137 // accept the recipient |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1138 if (!con.get_content_filtering()) st = white; |
179 | 1139 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1140 if (st == oksofar) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1141 // remember first content filtering context |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1142 if (con.get_content_filtering()) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1143 if (!priv.content_context) priv.content_context = &con; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1144 else if (con.get_require() && (priv.content_context != &con)) { |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1145 smfi_setreply(ctx, (char*)"452", (char*)"4.2.1", (char*)"incompatible filtering contexts"); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1146 return SMFIS_TEMPFAIL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1147 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1148 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1149 // remember the non-whites |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1150 priv.need_content_filter(rcptaddr, con); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1151 priv.only_whites = false; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1152 priv.want_spamassassin |= (priv.assassin) && // have spam assassin available and |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1153 (con.get_spamassassin_limit() != 0); // want to use it with a non-zero score |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1154 priv.want_dccgrey |= (priv.dccifd) && // have dcc interface and |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1155 (con.get_grey()); // want to use it for greylisting |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1156 priv.want_dccbulk |= (priv.dccifd) && // have dcc interface and |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1157 (con.get_bulk() != 0); // want to use it for bulk detection |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1158 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1159 if (st == white) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1160 priv.have_whites = true; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1161 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1162 return SMFIS_CONTINUE; |
94 | 1163 } |
1164 | |
163 | 1165 sfsistat mlfi_header(SMFICTX* ctx, char* headerf, char* headerv) |
1166 { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1167 mlfiPriv &priv = *MLFIPRIV; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1168 // detect precedence:bulk for avoiding autowhitelisting |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1169 if ((strcasecmp(headerf, "precedence") == 0) && |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1170 (strcasecmp(headerv, "bulk") == 0)) priv.is_bulk_precedence = true; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1171 // other headers are only needed for content filtering |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1172 if (priv.authenticated) return SMFIS_CONTINUE; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1173 if (priv.only_whites) return SMFIS_CONTINUE; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1174 if (priv.want_spamassassin) priv.assassin->mlfi_header(headerf, headerv); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1175 if (priv.want_dccgrey || priv.want_dccbulk) priv.dccifd->mlfi_header(ctx, headerf, headerv); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1176 return SMFIS_CONTINUE; |
163 | 1177 } |
1178 | |
1179 sfsistat mlfi_eoh(SMFICTX* ctx) | |
1180 { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1181 mlfiPriv &priv = *MLFIPRIV; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1182 // delayed autowhitelisting |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1183 while (!priv.delayer.empty()) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1184 DELAYWHITEP dwp = priv.delayer.front(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1185 if (!priv.is_bulk_precedence) { |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1186 const char *loto = dwp->get_loto(); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1187 WHITELISTERP w = dwp->get_w(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1188 CONTEXTP con2 = dwp->get_con(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1189 if (debug_syslog > 1) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1190 char buf[maxlen]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1191 char msg[maxlen]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1192 snprintf(msg, sizeof(msg), "whitelist reply from <%s> in context %s", loto, con2->get_full_name(buf,maxlen)); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1193 my_syslog(&priv, msg); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1194 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1195 w->sent(loto); // don't free it, the whitelister takes ownership of the string |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1196 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1197 delete dwp; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1198 priv.delayer.pop_front(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1199 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1200 // content filtering |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1201 if (priv.authenticated) return SMFIS_CONTINUE; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1202 if (priv.only_whites) return SMFIS_CONTINUE; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1203 if (priv.want_spamassassin) priv.assassin->mlfi_eoh(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1204 if (priv.want_dccgrey || priv.want_dccbulk) priv.dccifd->mlfi_eoh(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1205 return SMFIS_CONTINUE; |
163 | 1206 } |
1207 | |
94 | 1208 sfsistat mlfi_body(SMFICTX *ctx, u_char *data, size_t len) |
1209 { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1210 mlfiPriv &priv = *MLFIPRIV; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1211 if (priv.authenticated) return SMFIS_CONTINUE; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1212 if (priv.only_whites) return SMFIS_CONTINUE; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1213 if (priv.want_spamassassin) priv.assassin->mlfi_body(data, len); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1214 if (priv.want_dccgrey || priv.want_dccbulk) priv.dccifd->mlfi_body(data, len); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1215 priv.scanner->scan(data, len); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1216 return SMFIS_CONTINUE; |
94 | 1217 } |
1218 | |
1219 sfsistat mlfi_eom(SMFICTX *ctx) | |
1220 { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1221 sfsistat rc; |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1222 mlfiPriv &priv = *MLFIPRIV; |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1223 const char *host = NULL; |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1224 int ip; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1225 // process end of message |
190
004b855c6c1f
fix null pointer dereference from missing HELO command
carl
parents:
188
diff
changeset
|
1226 priv.eom = true; |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1227 if (priv.authenticated || priv.only_whites) rc = SMFIS_CONTINUE; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1228 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1229 // assert env_to not empty, it contains the |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1230 // non-whitelisted folks that want content filtering |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1231 int score = (priv.want_spamassassin) ? priv.assassin->mlfi_eom() : 0; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1232 bool grey = false; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1233 int bulk = 0; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1234 if (priv.want_dccgrey || priv.want_dccbulk) priv.dccifd->mlfi_eom(grey, bulk); |
178 | 1235 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1236 char buf[maxlen]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1237 string msg; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1238 string_set alive; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1239 bool random = false; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1240 int limit = 0; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1241 for (context_map::iterator i=priv.env_to.begin(); i!=priv.env_to.end(); i++) { |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1242 const char *rcpt = (*i).first; |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1243 CONTEXT &con = *((*i).second); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1244 if (!con.acceptable_content(*priv.memory, score, bulk, msg)) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1245 // bad html tags or excessive hosts or |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1246 // high spam assassin score or dcc bulk threshold exceedeed |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1247 smfi_delrcpt(ctx, (char*)rcpt); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1248 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1249 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1250 alive.insert(rcpt); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1251 random |= con.get_host_random(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1252 limit = max(limit, con.get_host_limit()); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1253 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1254 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1255 bool rejecting = alive.empty(); // if alive is empty, we must have set msg above in acceptable_content() |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1256 if (!rejecting) { |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1257 const char *fmt; |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1258 const char *found; |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1259 if (check_hosts(priv, random, limit, fmt, host, ip, found)) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1260 if (found) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1261 // uribl style |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1262 snprintf(buf, sizeof(buf), fmt, host, found); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1263 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1264 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1265 // dnsbl style |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1266 char adr[sizeof "255.255.255.255 "]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1267 adr[0] = '\0'; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1268 inet_ntop(AF_INET, (const u_char *)&ip, adr, sizeof(adr)); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1269 snprintf(buf, sizeof(buf), fmt, host, adr); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1270 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1271 msg = string(buf); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1272 rejecting = true; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1273 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1274 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1275 if (!rejecting) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1276 if (priv.want_dccgrey && grey) { |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1277 smfi_setreply(ctx, (char*)"452", (char*)"4.2.1", (char*)"temporary greylist embargoed"); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1278 rc = SMFIS_TEMPFAIL; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1279 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1280 else rc = SMFIS_CONTINUE; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1281 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1282 else if (!priv.have_whites) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1283 // can reject the entire message |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1284 snprintf(buf, sizeof(buf), "%s", msg.c_str()); |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1285 smfi_setreply(ctx, (char*)"550", (char*)"5.7.1", buf); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1286 rc = SMFIS_REJECT; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1287 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1288 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1289 // need to accept it but remove the recipients that don't want it |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1290 for (string_set::iterator i=alive.begin(); i!=alive.end(); i++) { |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1291 const char *rcpt = *i; |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1292 smfi_delrcpt(ctx, (char*)rcpt); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1293 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1294 rc = SMFIS_CONTINUE; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1295 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1296 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1297 // reset for a new message on the same connection |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1298 mlfi_abort(ctx); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1299 return rc; |
94 | 1300 } |
1301 | |
1302 sfsistat mlfi_abort(SMFICTX *ctx) | |
1303 { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1304 mlfiPriv &priv = *MLFIPRIV; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1305 priv.reset(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1306 return SMFIS_CONTINUE; |
94 | 1307 } |
1308 | |
1309 sfsistat mlfi_close(SMFICTX *ctx) | |
1310 { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1311 mlfiPriv *priv = MLFIPRIV; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1312 if (!priv) return SMFIS_CONTINUE; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1313 delete priv; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1314 smfi_setpriv(ctx, NULL); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1315 return SMFIS_CONTINUE; |
94 | 1316 } |
1317 | |
1318 struct smfiDesc smfilter = | |
1319 { | |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1320 (char*)"DNSBL", // filter name |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1321 SMFI_VERSION, // version code -- do not change |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1322 SMFIF_DELRCPT, // flags |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1323 mlfi_connect, // connection info filter |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1324 mlfi_helo, // SMTP HELO command filter |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1325 mlfi_envfrom, // envelope sender filter |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1326 mlfi_envrcpt, // envelope recipient filter |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1327 mlfi_header, // header filter |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1328 mlfi_eoh, // end of header |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1329 mlfi_body, // body block filter |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1330 mlfi_eom, // end of message |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1331 mlfi_abort, // message aborted |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1332 mlfi_close, // connection cleanup |
94 | 1333 }; |
1334 | |
1335 | |
1336 //////////////////////////////////////////////// | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1337 // reload the config |
94 | 1338 // |
1339 CONFIG* new_conf(); | |
1340 CONFIG* new_conf() { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1341 CONFIG *newc = new CONFIG; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1342 pthread_mutex_lock(&config_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1343 newc->generation = generation++; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1344 pthread_mutex_unlock(&config_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1345 if (debug_syslog) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1346 char buf[maxlen]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1347 snprintf(buf, sizeof(buf), "loading configuration generation %d", newc->generation); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1348 my_syslog(buf); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1349 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1350 if (load_conf(*newc, "dnsbl.conf")) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1351 newc->load_time = time(NULL); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1352 return newc; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1353 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1354 delete newc; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1355 return NULL; |
94 | 1356 } |
1357 | |
1358 | |
1359 //////////////////////////////////////////////// | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1360 // thread to watch the old config files for changes |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1361 // and reload when needed. |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1362 // we also clear the SMTP AUTH recipient counts hourly |
94 | 1363 // |
163 | 1364 extern "C" {void* config_loader(void *arg);} |
94 | 1365 void* config_loader(void *arg) { |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1366 int loop = 0; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1367 while (loader_run) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1368 sleep(180); // look for modifications every 3 minutes |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1369 if (!loader_run) break; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1370 loop++; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1371 if (loop == 20) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1372 // three minutes thru each loop, 20 loops per hour |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1373 // clear the recipient counts |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1374 pthread_mutex_lock(&rate_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1375 for (rcpt_rates::iterator i=rcpt_counts.begin(); i!=rcpt_counts.end(); i++) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1376 (*i).second = 0; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1377 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1378 pthread_mutex_unlock(&rate_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1379 loop = 0; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1380 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1381 CONFIG &dc = *config; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1382 time_t then = dc.load_time; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1383 struct stat st; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1384 bool reload = false; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1385 for (string_set::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) { |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1386 const char *fn = *i; |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1387 if (stat(fn, &st)) reload = true; // file disappeared |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1388 else if (st.st_mtime > then) reload = true; // file modified |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1389 if (reload) break; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1390 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1391 if (reload) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1392 CONFIG *newc = new_conf(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1393 if (newc) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1394 // replace the global config pointer |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1395 pthread_mutex_lock(&config_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1396 CONFIG *pc = config; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1397 bool last = pc && (!pc->reference_count); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1398 config = newc; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1399 pthread_mutex_unlock(&config_mutex); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1400 if (last) delete pc; // there were no references to this config |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1401 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1402 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1403 // failed to load new config |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1404 my_syslog("failed to load new configuration"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1405 system("echo 'failed to load new dnsbl configuration from /etc/dnsbl' | mail -s 'error in /etc/dnsbl configuration' root"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1406 // update the load time on the current config to prevent complaining every 3 minutes |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1407 dc.load_time = time(NULL); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1408 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1409 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1410 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1411 return NULL; |
94 | 1412 } |
1413 | |
1414 | |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1415 void usage(const char *prog); |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1416 void usage(const char *prog) |
94 | 1417 { |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1418 fprintf(stderr, "Usage: %s [-d [level]] [-c] [-s] [-e from|to] [-b dccifd-addr] -r port -p sm-sock-addr [-t timeout]\n", prog); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1419 fprintf(stderr, "where dccifd_addr is for the connection to dccifd\n"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1420 fprintf(stderr, " and should be local-domain-socket-file-name\n"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1421 fprintf(stderr, "where port is for the connection to our own dns resolver processes\n"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1422 fprintf(stderr, " and should be local-domain-socket-file-name\n"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1423 fprintf(stderr, "where sm-sock-addr is for the connection to sendmail\n"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1424 fprintf(stderr, " and should be one of\n"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1425 fprintf(stderr, " inet:port@ip-address\n"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1426 fprintf(stderr, " local:local-domain-socket-file-name\n"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1427 fprintf(stderr, "-c will load and dump the config to stdout\n"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1428 fprintf(stderr, "-s will stress test the config loading code by repeating the load/free cycle\n"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1429 fprintf(stderr, " in an infinte loop.\n"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1430 fprintf(stderr, "-d will set the syslog message level, currently 0 to 3\n"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1431 fprintf(stderr, "-e will print the results of looking up the from and to addresses in the\n"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1432 fprintf(stderr, " current config. The | character is used to separate the from and to\n"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1433 fprintf(stderr, " addresses in the argument to the -e switch\n"); |
94 | 1434 } |
1435 | |
1436 | |
1437 | |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1438 void setup_socket(const char *sock); |
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1439 void setup_socket(const char *sock) { |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1440 unlink(sock); |
94 | 1441 } |
1442 | |
1443 | |
1444 /* | |
1445 * The signal handler function -- only gets called when a SIGCHLD | |
1446 * is received, ie when a child terminates | |
1447 */ | |
1448 void sig_chld(int signo) | |
1449 { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1450 int status; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1451 /* Wait for any child without blocking */ |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1452 while (waitpid(-1, &status, WNOHANG) > 0) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1453 // ignore child exit status, we only do this to cleanup zombies |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1454 } |
94 | 1455 } |
1456 | |
1457 | |
1458 int main(int argc, char**argv) | |
1459 { | |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1460 token_init(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1461 bool check = false; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1462 bool stress = false; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1463 bool setconn = false; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1464 bool setreso = false; |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1465 const char *email = NULL; |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1466 int c; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1467 const char *args = "b:r:p:t:e:d:chs"; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1468 extern char *optarg; |
94 | 1469 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1470 // Process command line options |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1471 while ((c = getopt(argc, argv, args)) != -1) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1472 switch (c) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1473 case 'b': |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1474 if (optarg == NULL || *optarg == '\0') { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1475 fprintf(stderr, "Illegal dccifd socket: %s\n", optarg); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1476 exit(EX_USAGE); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1477 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1478 dccifd_port = strdup(optarg); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1479 break; |
177 | 1480 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1481 case 'r': |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1482 if (optarg == NULL || *optarg == '\0') { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1483 fprintf(stderr, "Illegal resolver socket: %s\n", optarg); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1484 exit(EX_USAGE); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1485 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1486 resolver_port = strdup(optarg); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1487 setup_socket(resolver_port); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1488 setreso = true; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1489 break; |
94 | 1490 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1491 case 'p': |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1492 if (optarg == NULL || *optarg == '\0') { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1493 fprintf(stderr, "Illegal sendmail socket: %s\n", optarg); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1494 exit(EX_USAGE); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1495 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1496 if (smfi_setconn(optarg) == MI_FAILURE) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1497 fprintf(stderr, "smfi_setconn failed\n"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1498 exit(EX_SOFTWARE); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1499 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1500 if (strncasecmp(optarg, "unix:", 5) == 0) setup_socket(optarg + 5); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1501 else if (strncasecmp(optarg, "local:", 6) == 0) setup_socket(optarg + 6); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1502 setconn = true; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1503 break; |
94 | 1504 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1505 case 't': |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1506 if (optarg == NULL || *optarg == '\0') { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1507 fprintf(stderr, "Illegal timeout: %s\n", optarg); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1508 exit(EX_USAGE); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1509 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1510 if (smfi_settimeout(atoi(optarg)) == MI_FAILURE) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1511 fprintf(stderr, "smfi_settimeout failed\n"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1512 exit(EX_SOFTWARE); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1513 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1514 break; |
94 | 1515 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1516 case 'e': |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1517 if (email) free((void*)email); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1518 email = strdup(optarg); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1519 break; |
94 | 1520 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1521 case 'c': |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1522 check = true; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1523 break; |
94 | 1524 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1525 case 's': |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1526 stress = true; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1527 break; |
94 | 1528 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1529 case 'd': |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1530 if (optarg == NULL || *optarg == '\0') debug_syslog = 1; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1531 else debug_syslog = atoi(optarg); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1532 break; |
94 | 1533 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1534 case 'h': |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1535 default: |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1536 usage(argv[0]); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1537 exit(EX_USAGE); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1538 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1539 } |
94 | 1540 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1541 if (check) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1542 use_syslog = false; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1543 debug_syslog = 10; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1544 CONFIG *conf = new_conf(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1545 if (conf) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1546 conf->dump(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1547 delete conf; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1548 clear_strings(); // for valgrind checking |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1549 return 0; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1550 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1551 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1552 return 1; // config failed to load |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1553 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1554 } |
94 | 1555 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1556 if (stress) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1557 fprintf(stdout, "stress testing\n"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1558 while (1) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1559 for (int i=0; i<10; i++) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1560 CONFIG *conf = new_conf(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1561 if (conf) delete conf; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1562 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1563 fprintf(stdout, "."); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1564 fflush(stdout); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1565 sleep(1); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1566 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1567 } |
94 | 1568 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1569 if (email) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1570 char *x = strchr(email, '|'); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1571 if (x) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1572 *x = '\0'; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1573 char *from = strdup(email); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1574 char *to = strdup(x+1); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1575 use_syslog = false; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1576 CONFIG *conf = new_conf(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1577 if (conf) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1578 CONTEXTP con = conf->find_context(to); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1579 char buf[maxlen]; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1580 fprintf(stdout, "envelope to <%s> finds context %s\n", to, con->get_full_name(buf,maxlen)); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1581 CONTEXTP fc = con->find_context(from); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1582 fprintf(stdout, "envelope from <%s> finds context %s\n", from, fc->get_full_name(buf,maxlen)); |
214
82886d4dd71f
Fixes to compile on Fedora 9 and for const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
211
diff
changeset
|
1583 const char *st = fc->find_from(from); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1584 fprintf(stdout, "envelope from <%s> finds status %s\n", from, st); |
216
784030ac71f1
Never whitelist self addressed mail. Changes for Fedora 10 and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
214
diff
changeset
|
1585 bool self = (strcmp(from, to) == 0); |
784030ac71f1
Never whitelist self addressed mail. Changes for Fedora 10 and const correctness.
Carl Byington <carl@five-ten-sg.com>
parents:
214
diff
changeset
|
1586 if ((st == token_white) && self) fprintf(stdout, "ignore self whitelisting\n"); |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1587 delete conf; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1588 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1589 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1590 return 0; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1591 } |
94 | 1592 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1593 if (!setconn) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1594 fprintf(stderr, "%s: Missing required -p argument\n", argv[0]); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1595 usage(argv[0]); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1596 exit(EX_USAGE); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1597 } |
94 | 1598 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1599 if (!setreso) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1600 fprintf(stderr, "%s: Missing required -r argument\n", argv[0]); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1601 usage(argv[0]); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1602 exit(EX_USAGE); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1603 } |
94 | 1604 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1605 if (smfi_register(smfilter) == MI_FAILURE) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1606 fprintf(stderr, "smfi_register failed\n"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1607 exit(EX_UNAVAILABLE); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1608 } |
94 | 1609 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1610 // switch to background mode |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1611 if (daemon(1,0) < 0) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1612 fprintf(stderr, "daemon() call failed\n"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1613 exit(EX_UNAVAILABLE); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1614 } |
94 | 1615 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1616 // write the pid |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1617 const char *pidpath = "/var/run/dnsbl.pid"; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1618 unlink(pidpath); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1619 FILE *f = fopen(pidpath, "w"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1620 if (f) { |
94 | 1621 #ifdef linux |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1622 // from a comment in the DCC source code: |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1623 // Linux threads are broken. Signals given the |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1624 // original process are delivered to only the |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1625 // thread that happens to have that PID. The |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1626 // sendmail libmilter thread that needs to hear |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1627 // SIGINT and other signals does not, and that breaks |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1628 // scripts that need to stop milters. |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1629 // However, signaling the process group works. |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1630 fprintf(f, "-%d\n", (u_int)getpgrp()); |
94 | 1631 #else |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1632 fprintf(f, "%d\n", (u_int)getpid()); |
94 | 1633 #endif |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1634 fclose(f); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1635 } |
94 | 1636 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1637 // initialize the thread sync objects |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1638 pthread_mutex_init(&config_mutex, 0); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1639 pthread_mutex_init(&syslog_mutex, 0); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1640 pthread_mutex_init(&resolve_mutex, 0); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1641 pthread_mutex_init(&fd_pool_mutex, 0); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1642 pthread_mutex_init(&verifier_mutex, 0); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1643 pthread_mutex_init(&whitelister_mutex, 0); |
94 | 1644 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1645 // drop root privs |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1646 struct passwd *pw = getpwnam("dnsbl"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1647 if (pw) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1648 if (setgid(pw->pw_gid) == -1) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1649 my_syslog("failed to switch to group dnsbl"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1650 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1651 if (setuid(pw->pw_uid) == -1) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1652 my_syslog("failed to switch to user dnsbl"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1653 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1654 } |
94 | 1655 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1656 // load the initial config |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1657 config = new_conf(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1658 if (!config) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1659 my_syslog("failed to load initial configuration, quitting"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1660 exit(1); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1661 } |
94 | 1662 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1663 // fork off the resolver listener process |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1664 pid_t child = fork(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1665 if (child < 0) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1666 my_syslog("failed to create resolver listener process"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1667 exit(0); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1668 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1669 if (child == 0) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1670 // we are the child - dns resolver listener process |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1671 resolver_socket = socket(AF_UNIX, SOCK_STREAM, 0); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1672 if (resolver_socket < 0) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1673 my_syslog("child failed to create resolver socket"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1674 exit(0); // failed |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1675 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1676 sockaddr_un server; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1677 memset(&server, '\0', sizeof(server)); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1678 server.sun_family = AF_UNIX; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1679 strncpy(server.sun_path, resolver_port, sizeof(server.sun_path)-1); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1680 //try to bind the address to the socket. |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1681 if (bind(resolver_socket, (sockaddr *)&server, sizeof(server)) < 0) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1682 // bind failed |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1683 shutdown(resolver_socket, SHUT_RDWR); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1684 close(resolver_socket); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1685 my_syslog("child failed to bind resolver socket"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1686 exit(0); // failed |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1687 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1688 //listen on the socket. |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1689 if (listen(resolver_socket, 10) < 0) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1690 // listen failed |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1691 shutdown(resolver_socket, SHUT_RDWR); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1692 close(resolver_socket); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1693 my_syslog("child failed to listen to resolver socket"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1694 exit(0); // failed |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1695 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1696 // setup sigchld handler to prevent zombies |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1697 struct sigaction act; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1698 act.sa_handler = sig_chld; // Assign sig_chld as our SIGCHLD handler |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1699 sigemptyset(&act.sa_mask); // We don't want to block any other signals in this example |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1700 act.sa_flags = SA_NOCLDSTOP; // only want children that have terminated |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1701 if (sigaction(SIGCHLD, &act, NULL) < 0) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1702 my_syslog("child failed to setup SIGCHLD handler"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1703 exit(0); // failed |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1704 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1705 while (true) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1706 sockaddr_un client; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1707 socklen_t clientlen = sizeof(client); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1708 int s = accept(resolver_socket, (sockaddr *)&client, &clientlen); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1709 if (s > 0) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1710 // accept worked, it did not get cancelled before we could accept it |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1711 // fork off a process to handle this connection |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1712 int newchild = fork(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1713 if (newchild == 0) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1714 // this is the worker process |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1715 // child does not need the listening socket |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1716 close(resolver_socket); |
220
495cfe5caead
try to allow mixed -lresolv and libresolv.a for ns_parserr
Carl Byington <carl@five-ten-sg.com>
parents:
216
diff
changeset
|
1717 #ifdef NS_PACKETSZ |
495cfe5caead
try to allow mixed -lresolv and libresolv.a for ns_parserr
Carl Byington <carl@five-ten-sg.com>
parents:
216
diff
changeset
|
1718 res_init(); |
495cfe5caead
try to allow mixed -lresolv and libresolv.a for ns_parserr
Carl Byington <carl@five-ten-sg.com>
parents:
216
diff
changeset
|
1719 _res.retry = 2; |
495cfe5caead
try to allow mixed -lresolv and libresolv.a for ns_parserr
Carl Byington <carl@five-ten-sg.com>
parents:
216
diff
changeset
|
1720 _res.retrans = RES_TIMEOUT; |
495cfe5caead
try to allow mixed -lresolv and libresolv.a for ns_parserr
Carl Byington <carl@five-ten-sg.com>
parents:
216
diff
changeset
|
1721 #endif |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1722 process_resolver_requests(s); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1723 exit(0); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1724 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1725 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1726 // this is the parent |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1727 // parent does not need the accepted socket |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1728 close(s); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1729 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1730 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1731 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1732 exit(0); // make sure we don't fall thru. |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1733 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1734 else { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1735 sleep(2); // allow child to get started |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1736 } |
94 | 1737 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1738 // only create threads after the fork() in daemon |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1739 pthread_t tid; |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1740 if (pthread_create(&tid, 0, config_loader, 0)) |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1741 my_syslog("failed to create config loader thread"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1742 if (pthread_detach(tid)) |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1743 my_syslog("failed to detach config loader thread"); |
153 | 1744 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1745 if (pthread_create(&tid, 0, verify_closer, 0)) |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1746 my_syslog("failed to create verify closer thread"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1747 if (pthread_detach(tid)) |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1748 my_syslog("failed to detach verify closer thread"); |
94 | 1749 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1750 if (pthread_create(&tid, 0, whitelister_writer, 0)) |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1751 my_syslog("failed to create autowhite writer thread"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1752 if (pthread_detach(tid)) |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1753 my_syslog("failed to detach autowhite writer thread"); |
153 | 1754 |
192
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1755 time_t starting = time(NULL); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1756 int rc = smfi_main(); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1757 if ((rc != MI_SUCCESS) && (time(NULL) > starting+5*60)) { |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1758 my_syslog("trying to restart after smfi_main()"); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1759 loader_run = false; // eventually the config loader thread will terminate |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1760 execvp(argv[0], argv); |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1761 } |
8f4a9a37d4d9
delay autowhitelisting to avoid out of office reply bots
carl
parents:
191
diff
changeset
|
1762 exit((rc == MI_SUCCESS) ? 0 : EX_UNAVAILABLE); |
94 | 1763 } |
1764 |