annotate src/dnsbl.cpp @ 121:e2506138561b stable-5-13

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