annotate src/dnsbl.cpp @ 91:ca46fafc6621

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