annotate src/dnsbl.cpp @ 89:946fc1bcfb2c stable-5-3

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