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