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