annotate src/dnsbl.cpp @ 78:b6beb282fd83

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