annotate src/dnsbl.cpp @ 188:edcefdb7ccc1

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