annotate src/dnsbl.cpp @ 0:96a9758165cd original

Initial revision
author carl
date Tue, 20 Apr 2004 20:02:29 -0700
parents
children bd0b1a153f67
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
96a9758165cd Initial revision
carl
parents:
diff changeset
1 /*
96a9758165cd Initial revision
carl
parents:
diff changeset
2
96a9758165cd Initial revision
carl
parents:
diff changeset
3 Copyright (c) 2004 Carl Byington - 510 Software Group, released under
96a9758165cd Initial revision
carl
parents:
diff changeset
4 the GPL version 2 or any later version at your choice available at
96a9758165cd Initial revision
carl
parents:
diff changeset
5 http://www.fsf.org/licenses/gpl.txt
96a9758165cd Initial revision
carl
parents:
diff changeset
6
96a9758165cd Initial revision
carl
parents:
diff changeset
7 Based on a sample milter Copyright (c) 2000-2003 Sendmail, Inc. and its
96a9758165cd Initial revision
carl
parents:
diff changeset
8 suppliers. Inspired by the DCC by Rhyolite Software
96a9758165cd Initial revision
carl
parents:
diff changeset
9
96a9758165cd Initial revision
carl
parents:
diff changeset
10 -p port The port through which the MTA will connect to this milter.
96a9758165cd Initial revision
carl
parents:
diff changeset
11 -t sec The timeout value.
96a9758165cd Initial revision
carl
parents:
diff changeset
12
96a9758165cd Initial revision
carl
parents:
diff changeset
13 TODO:
96a9758165cd Initial revision
carl
parents:
diff changeset
14
96a9758165cd Initial revision
carl
parents:
diff changeset
15 1) Add "include-dcc NAME fn" to read a dcc whiteclnt file looking
96a9758165cd Initial revision
carl
parents:
diff changeset
16 for many substitute mail-host domain, and add the equivalent "env_from
96a9758165cd Initial revision
carl
parents:
diff changeset
17 domain black" into the NAME mapping. That allows clients to use just the
96a9758165cd Initial revision
carl
parents:
diff changeset
18 DCC for white/blacklisting, but the backup mx machines can use dnsbl
96a9758165cd Initial revision
carl
parents:
diff changeset
19 and get the same effect.
96a9758165cd Initial revision
carl
parents:
diff changeset
20
96a9758165cd Initial revision
carl
parents:
diff changeset
21 */
96a9758165cd Initial revision
carl
parents:
diff changeset
22
96a9758165cd Initial revision
carl
parents:
diff changeset
23
96a9758165cd Initial revision
carl
parents:
diff changeset
24 // from sendmail sample
96a9758165cd Initial revision
carl
parents:
diff changeset
25 #include <sys/types.h>
96a9758165cd Initial revision
carl
parents:
diff changeset
26 #include <sys/stat.h>
96a9758165cd Initial revision
carl
parents:
diff changeset
27 #include <errno.h>
96a9758165cd Initial revision
carl
parents:
diff changeset
28 #include <stdio.h>
96a9758165cd Initial revision
carl
parents:
diff changeset
29 #include <stdlib.h>
96a9758165cd Initial revision
carl
parents:
diff changeset
30 #include <string.h>
96a9758165cd Initial revision
carl
parents:
diff changeset
31 #include <sysexits.h>
96a9758165cd Initial revision
carl
parents:
diff changeset
32 #include <unistd.h>
96a9758165cd Initial revision
carl
parents:
diff changeset
33
96a9758165cd Initial revision
carl
parents:
diff changeset
34 // needed for socket io
96a9758165cd Initial revision
carl
parents:
diff changeset
35 #include <sys/ioctl.h>
96a9758165cd Initial revision
carl
parents:
diff changeset
36 #include <net/if.h>
96a9758165cd Initial revision
carl
parents:
diff changeset
37 #include <arpa/inet.h>
96a9758165cd Initial revision
carl
parents:
diff changeset
38 #include <netinet/in.h>
96a9758165cd Initial revision
carl
parents:
diff changeset
39 #include <netinet/tcp.h>
96a9758165cd Initial revision
carl
parents:
diff changeset
40 #include <netdb.h>
96a9758165cd Initial revision
carl
parents:
diff changeset
41 #include <sys/socket.h>
96a9758165cd Initial revision
carl
parents:
diff changeset
42
96a9758165cd Initial revision
carl
parents:
diff changeset
43 // needed for thread
96a9758165cd Initial revision
carl
parents:
diff changeset
44 #include <pthread.h>
96a9758165cd Initial revision
carl
parents:
diff changeset
45
96a9758165cd Initial revision
carl
parents:
diff changeset
46 // needed for std c++ collections
96a9758165cd Initial revision
carl
parents:
diff changeset
47 #include <set>
96a9758165cd Initial revision
carl
parents:
diff changeset
48 #include <map>
96a9758165cd Initial revision
carl
parents:
diff changeset
49 #include <list>
96a9758165cd Initial revision
carl
parents:
diff changeset
50
96a9758165cd Initial revision
carl
parents:
diff changeset
51 // for the dns resolver
96a9758165cd Initial revision
carl
parents:
diff changeset
52 #include <netinet/in.h>
96a9758165cd Initial revision
carl
parents:
diff changeset
53 #include <arpa/nameser.h>
96a9758165cd Initial revision
carl
parents:
diff changeset
54 #include <resolv.h>
96a9758165cd Initial revision
carl
parents:
diff changeset
55
96a9758165cd Initial revision
carl
parents:
diff changeset
56 // misc stuff needed here
96a9758165cd Initial revision
carl
parents:
diff changeset
57 #include <ctype.h>
96a9758165cd Initial revision
carl
parents:
diff changeset
58 #include <fstream>
96a9758165cd Initial revision
carl
parents:
diff changeset
59 #include <syslog.h>
96a9758165cd Initial revision
carl
parents:
diff changeset
60
96a9758165cd Initial revision
carl
parents:
diff changeset
61
96a9758165cd Initial revision
carl
parents:
diff changeset
62 using namespace std;
96a9758165cd Initial revision
carl
parents:
diff changeset
63
96a9758165cd Initial revision
carl
parents:
diff changeset
64
96a9758165cd Initial revision
carl
parents:
diff changeset
65 extern "C" {
96a9758165cd Initial revision
carl
parents:
diff changeset
66 #include "libmilter/mfapi.h"
96a9758165cd Initial revision
carl
parents:
diff changeset
67 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr);
96a9758165cd Initial revision
carl
parents:
diff changeset
68 sfsistat mlfi_envfrom(SMFICTX *ctx, char **argv);
96a9758165cd Initial revision
carl
parents:
diff changeset
69 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **argv);
96a9758165cd Initial revision
carl
parents:
diff changeset
70 sfsistat mlfi_eom_or_abort(SMFICTX *ctx);
96a9758165cd Initial revision
carl
parents:
diff changeset
71 sfsistat mlfi_close(SMFICTX *ctx);
96a9758165cd Initial revision
carl
parents:
diff changeset
72 }
96a9758165cd Initial revision
carl
parents:
diff changeset
73
96a9758165cd Initial revision
carl
parents:
diff changeset
74 #ifndef bool
96a9758165cd Initial revision
carl
parents:
diff changeset
75 # define bool int
96a9758165cd Initial revision
carl
parents:
diff changeset
76 # define TRUE 1
96a9758165cd Initial revision
carl
parents:
diff changeset
77 # define FALSE 0
96a9758165cd Initial revision
carl
parents:
diff changeset
78 #endif /* ! bool */
96a9758165cd Initial revision
carl
parents:
diff changeset
79
96a9758165cd Initial revision
carl
parents:
diff changeset
80 struct ltstr {
96a9758165cd Initial revision
carl
parents:
diff changeset
81 bool operator()(char* s1, char* s2) const {
96a9758165cd Initial revision
carl
parents:
diff changeset
82 return strcmp(s1, s2) < 0;
96a9758165cd Initial revision
carl
parents:
diff changeset
83 }
96a9758165cd Initial revision
carl
parents:
diff changeset
84 };
96a9758165cd Initial revision
carl
parents:
diff changeset
85
96a9758165cd Initial revision
carl
parents:
diff changeset
86 struct DNSBL {
96a9758165cd Initial revision
carl
parents:
diff changeset
87 char *suffix; // blacklist suffix like blackholes.five-ten-sg.com
96a9758165cd Initial revision
carl
parents:
diff changeset
88 char *message; // error message with one or two %s operators for the ip address replacement
96a9758165cd Initial revision
carl
parents:
diff changeset
89 DNSBL(char *s, char *m);
96a9758165cd Initial revision
carl
parents:
diff changeset
90 };
96a9758165cd Initial revision
carl
parents:
diff changeset
91 DNSBL::DNSBL(char *s, char *m) {
96a9758165cd Initial revision
carl
parents:
diff changeset
92 suffix = s;
96a9758165cd Initial revision
carl
parents:
diff changeset
93 message = m;
96a9758165cd Initial revision
carl
parents:
diff changeset
94 }
96a9758165cd Initial revision
carl
parents:
diff changeset
95
96a9758165cd Initial revision
carl
parents:
diff changeset
96 typedef DNSBL * DNSBLP;
96a9758165cd Initial revision
carl
parents:
diff changeset
97 typedef list<DNSBLP> DNSBLL;
96a9758165cd Initial revision
carl
parents:
diff changeset
98 typedef DNSBLL * DNSBLLP;
96a9758165cd Initial revision
carl
parents:
diff changeset
99 typedef map<char *, char *, ltstr> string_map;
96a9758165cd Initial revision
carl
parents:
diff changeset
100 typedef map<char *, string_map *, ltstr> from_map;
96a9758165cd Initial revision
carl
parents:
diff changeset
101 typedef map<char *, DNSBLP, ltstr> dnsblp_map;
96a9758165cd Initial revision
carl
parents:
diff changeset
102 typedef map<char *, DNSBLLP, ltstr> dnsbllp_map;
96a9758165cd Initial revision
carl
parents:
diff changeset
103 typedef set<char *, ltstr> string_set;
96a9758165cd Initial revision
carl
parents:
diff changeset
104 typedef list<char *> string_list;
96a9758165cd Initial revision
carl
parents:
diff changeset
105
96a9758165cd Initial revision
carl
parents:
diff changeset
106 struct CONFIG {
96a9758165cd Initial revision
carl
parents:
diff changeset
107 // the only mutable stuff once it has been loaded from the config file
96a9758165cd Initial revision
carl
parents:
diff changeset
108 int reference_count; // protected by the global config_mutex
96a9758165cd Initial revision
carl
parents:
diff changeset
109 // all the rest is constant after loading from the config file
96a9758165cd Initial revision
carl
parents:
diff changeset
110 time_t load_time;
96a9758165cd Initial revision
carl
parents:
diff changeset
111 string_list config_files;
96a9758165cd Initial revision
carl
parents:
diff changeset
112 dnsblp_map dnsbls;
96a9758165cd Initial revision
carl
parents:
diff changeset
113 dnsbllp_map dnsblls;
96a9758165cd Initial revision
carl
parents:
diff changeset
114 from_map env_from;
96a9758165cd Initial revision
carl
parents:
diff changeset
115 string_map env_to_dnsbll; // map recipient to a named dnsbll
96a9758165cd Initial revision
carl
parents:
diff changeset
116 string_map env_to_chkfrom; // map recipient to a named from map
96a9758165cd Initial revision
carl
parents:
diff changeset
117 CONFIG();
96a9758165cd Initial revision
carl
parents:
diff changeset
118 ~CONFIG();
96a9758165cd Initial revision
carl
parents:
diff changeset
119 };
96a9758165cd Initial revision
carl
parents:
diff changeset
120 CONFIG::CONFIG() {
96a9758165cd Initial revision
carl
parents:
diff changeset
121 reference_count = 0;
96a9758165cd Initial revision
carl
parents:
diff changeset
122 load_time = 0;
96a9758165cd Initial revision
carl
parents:
diff changeset
123 }
96a9758165cd Initial revision
carl
parents:
diff changeset
124 CONFIG::~CONFIG() {
96a9758165cd Initial revision
carl
parents:
diff changeset
125 for (dnsblp_map::iterator i=dnsbls.begin(); i!=dnsbls.end(); i++) {
96a9758165cd Initial revision
carl
parents:
diff changeset
126 DNSBLP d = (*i).second;
96a9758165cd Initial revision
carl
parents:
diff changeset
127 delete d;
96a9758165cd Initial revision
carl
parents:
diff changeset
128 }
96a9758165cd Initial revision
carl
parents:
diff changeset
129 for (dnsbllp_map::iterator i=dnsblls.begin(); i!=dnsblls.end(); i++) {
96a9758165cd Initial revision
carl
parents:
diff changeset
130 DNSBLLP d = (*i).second;
96a9758165cd Initial revision
carl
parents:
diff changeset
131 delete d;
96a9758165cd Initial revision
carl
parents:
diff changeset
132 }
96a9758165cd Initial revision
carl
parents:
diff changeset
133 for (from_map::iterator i=env_from.begin(); i!=env_from.end(); i++) {
96a9758165cd Initial revision
carl
parents:
diff changeset
134 string_map *d = (*i).second;
96a9758165cd Initial revision
carl
parents:
diff changeset
135 delete d;
96a9758165cd Initial revision
carl
parents:
diff changeset
136 }
96a9758165cd Initial revision
carl
parents:
diff changeset
137 }
96a9758165cd Initial revision
carl
parents:
diff changeset
138
96a9758165cd Initial revision
carl
parents:
diff changeset
139 static string_set all_strings; // owns all the strings, only modified by the config loader thread
96a9758165cd Initial revision
carl
parents:
diff changeset
140 static CONFIG * config = NULL; // protected by the config_mutex
96a9758165cd Initial revision
carl
parents:
diff changeset
141
96a9758165cd Initial revision
carl
parents:
diff changeset
142 static pthread_mutex_t config_mutex;
96a9758165cd Initial revision
carl
parents:
diff changeset
143 static pthread_mutex_t syslog_mutex;
96a9758165cd Initial revision
carl
parents:
diff changeset
144 static pthread_mutex_t resolve_mutex;
96a9758165cd Initial revision
carl
parents:
diff changeset
145
96a9758165cd Initial revision
carl
parents:
diff changeset
146
96a9758165cd Initial revision
carl
parents:
diff changeset
147
96a9758165cd Initial revision
carl
parents:
diff changeset
148 ////////////////////////////////////////////////
96a9758165cd Initial revision
carl
parents:
diff changeset
149 // predefined names
96a9758165cd Initial revision
carl
parents:
diff changeset
150 //
96a9758165cd Initial revision
carl
parents:
diff changeset
151 #define DEFAULT "default"
96a9758165cd Initial revision
carl
parents:
diff changeset
152 #define WHITE "white"
96a9758165cd Initial revision
carl
parents:
diff changeset
153 #define BLACK "black"
96a9758165cd Initial revision
carl
parents:
diff changeset
154
96a9758165cd Initial revision
carl
parents:
diff changeset
155
96a9758165cd Initial revision
carl
parents:
diff changeset
156 ////////////////////////////////////////////////
96a9758165cd Initial revision
carl
parents:
diff changeset
157 // mail filter private data, held for us by sendmail
96a9758165cd Initial revision
carl
parents:
diff changeset
158 //
96a9758165cd Initial revision
carl
parents:
diff changeset
159 enum status {oksofar, // not rejected yet
96a9758165cd Initial revision
carl
parents:
diff changeset
160 white, // whitelisted by envelope from
96a9758165cd Initial revision
carl
parents:
diff changeset
161 black, // blacklisted by envelope from or to
96a9758165cd Initial revision
carl
parents:
diff changeset
162 reject}; // rejected by a dns list
96a9758165cd Initial revision
carl
parents:
diff changeset
163 struct mlfiPriv
96a9758165cd Initial revision
carl
parents:
diff changeset
164 {
96a9758165cd Initial revision
carl
parents:
diff changeset
165 CONFIG *pc; // global context with our maps
96a9758165cd Initial revision
carl
parents:
diff changeset
166 int ip; // ip4 address of the smtp client
96a9758165cd Initial revision
carl
parents:
diff changeset
167 char *mailaddr; // envelope from value
96a9758165cd Initial revision
carl
parents:
diff changeset
168 bool authenticated; // client authenticated? if so, suppress all dnsbl checks
96a9758165cd Initial revision
carl
parents:
diff changeset
169 map<DNSBLP, status> checked; // status from those lists
96a9758165cd Initial revision
carl
parents:
diff changeset
170 mlfiPriv();
96a9758165cd Initial revision
carl
parents:
diff changeset
171 ~mlfiPriv();
96a9758165cd Initial revision
carl
parents:
diff changeset
172 };
96a9758165cd Initial revision
carl
parents:
diff changeset
173 mlfiPriv::mlfiPriv() {
96a9758165cd Initial revision
carl
parents:
diff changeset
174 pthread_mutex_lock(&config_mutex);
96a9758165cd Initial revision
carl
parents:
diff changeset
175 pc = config;
96a9758165cd Initial revision
carl
parents:
diff changeset
176 pc->reference_count++;
96a9758165cd Initial revision
carl
parents:
diff changeset
177 pthread_mutex_unlock(&config_mutex);
96a9758165cd Initial revision
carl
parents:
diff changeset
178 ip = 0;
96a9758165cd Initial revision
carl
parents:
diff changeset
179 mailaddr = NULL;
96a9758165cd Initial revision
carl
parents:
diff changeset
180 }
96a9758165cd Initial revision
carl
parents:
diff changeset
181 mlfiPriv::~mlfiPriv() {
96a9758165cd Initial revision
carl
parents:
diff changeset
182 pthread_mutex_lock(&config_mutex);
96a9758165cd Initial revision
carl
parents:
diff changeset
183 pc->reference_count--;
96a9758165cd Initial revision
carl
parents:
diff changeset
184 pthread_mutex_unlock(&config_mutex);
96a9758165cd Initial revision
carl
parents:
diff changeset
185 if (mailaddr) free(mailaddr);
96a9758165cd Initial revision
carl
parents:
diff changeset
186 }
96a9758165cd Initial revision
carl
parents:
diff changeset
187
96a9758165cd Initial revision
carl
parents:
diff changeset
188 #define MLFIPRIV ((struct mlfiPriv *) smfi_getpriv(ctx))
96a9758165cd Initial revision
carl
parents:
diff changeset
189
96a9758165cd Initial revision
carl
parents:
diff changeset
190
96a9758165cd Initial revision
carl
parents:
diff changeset
191 ////////////////////////////////////////////////
96a9758165cd Initial revision
carl
parents:
diff changeset
192 // syslog a message
96a9758165cd Initial revision
carl
parents:
diff changeset
193 //
96a9758165cd Initial revision
carl
parents:
diff changeset
194 static void my_syslog(char *text);
96a9758165cd Initial revision
carl
parents:
diff changeset
195 static void my_syslog(char *text) {
96a9758165cd Initial revision
carl
parents:
diff changeset
196 pthread_mutex_lock(&syslog_mutex);
96a9758165cd Initial revision
carl
parents:
diff changeset
197 openlog("dnsbl", LOG_PID, LOG_MAIL);
96a9758165cd Initial revision
carl
parents:
diff changeset
198 syslog(LOG_NOTICE, "%s", text);
96a9758165cd Initial revision
carl
parents:
diff changeset
199 closelog();
96a9758165cd Initial revision
carl
parents:
diff changeset
200 pthread_mutex_unlock(&syslog_mutex);
96a9758165cd Initial revision
carl
parents:
diff changeset
201 }
96a9758165cd Initial revision
carl
parents:
diff changeset
202
96a9758165cd Initial revision
carl
parents:
diff changeset
203
96a9758165cd Initial revision
carl
parents:
diff changeset
204 ////////////////////////////////////////////////
96a9758165cd Initial revision
carl
parents:
diff changeset
205 // register a global string
96a9758165cd Initial revision
carl
parents:
diff changeset
206 //
96a9758165cd Initial revision
carl
parents:
diff changeset
207 static char* register_string(char *name);
96a9758165cd Initial revision
carl
parents:
diff changeset
208 static char* register_string(char *name) {
96a9758165cd Initial revision
carl
parents:
diff changeset
209 string_set::iterator i = all_strings.find(name);
96a9758165cd Initial revision
carl
parents:
diff changeset
210 if (i != all_strings.end()) return *i;
96a9758165cd Initial revision
carl
parents:
diff changeset
211 char *x = strdup(name);
96a9758165cd Initial revision
carl
parents:
diff changeset
212 all_strings.insert(x);
96a9758165cd Initial revision
carl
parents:
diff changeset
213 return x;
96a9758165cd Initial revision
carl
parents:
diff changeset
214 }
96a9758165cd Initial revision
carl
parents:
diff changeset
215
96a9758165cd Initial revision
carl
parents:
diff changeset
216
96a9758165cd Initial revision
carl
parents:
diff changeset
217 static char* next_token(char *delim);
96a9758165cd Initial revision
carl
parents:
diff changeset
218 static char* next_token(char *delim) {
96a9758165cd Initial revision
carl
parents:
diff changeset
219 char *name = strtok(NULL, delim);
96a9758165cd Initial revision
carl
parents:
diff changeset
220 if (!name) return name;
96a9758165cd Initial revision
carl
parents:
diff changeset
221 return register_string(name);
96a9758165cd Initial revision
carl
parents:
diff changeset
222 }
96a9758165cd Initial revision
carl
parents:
diff changeset
223
96a9758165cd Initial revision
carl
parents:
diff changeset
224
96a9758165cd Initial revision
carl
parents:
diff changeset
225 ////////////////////////////////////////////////
96a9758165cd Initial revision
carl
parents:
diff changeset
226 // lookup an email address in the env_from or env_to maps
96a9758165cd Initial revision
carl
parents:
diff changeset
227 //
96a9758165cd Initial revision
carl
parents:
diff changeset
228 static char* lookup1(char *email, string_map map);
96a9758165cd Initial revision
carl
parents:
diff changeset
229 static char* lookup1(char *email, string_map map) {
96a9758165cd Initial revision
carl
parents:
diff changeset
230 string_map::iterator i = map.find(email);
96a9758165cd Initial revision
carl
parents:
diff changeset
231 if (i != map.end()) return (*i).second;
96a9758165cd Initial revision
carl
parents:
diff changeset
232 char *x = strchr(email, '@');
96a9758165cd Initial revision
carl
parents:
diff changeset
233 if (!x) return DEFAULT;
96a9758165cd Initial revision
carl
parents:
diff changeset
234 x++;
96a9758165cd Initial revision
carl
parents:
diff changeset
235 i = map.find(x);
96a9758165cd Initial revision
carl
parents:
diff changeset
236 if (i != map.end()) return (*i).second;
96a9758165cd Initial revision
carl
parents:
diff changeset
237 return DEFAULT;
96a9758165cd Initial revision
carl
parents:
diff changeset
238 }
96a9758165cd Initial revision
carl
parents:
diff changeset
239
96a9758165cd Initial revision
carl
parents:
diff changeset
240
96a9758165cd Initial revision
carl
parents:
diff changeset
241 ////////////////////////////////////////////////
96a9758165cd Initial revision
carl
parents:
diff changeset
242 // lookup an email address in the env_from or env_to maps
96a9758165cd Initial revision
carl
parents:
diff changeset
243 // this email address is passed in from sendmail, and will
96a9758165cd Initial revision
carl
parents:
diff changeset
244 // always be enclosed in <>. It may have mixed case, just
96a9758165cd Initial revision
carl
parents:
diff changeset
245 // as the mail client sent it.
96a9758165cd Initial revision
carl
parents:
diff changeset
246 //
96a9758165cd Initial revision
carl
parents:
diff changeset
247 static char* lookup(char* email, string_map map);
96a9758165cd Initial revision
carl
parents:
diff changeset
248 static char* lookup(char* email, string_map map) {
96a9758165cd Initial revision
carl
parents:
diff changeset
249 int n = strlen(email)-2;
96a9758165cd Initial revision
carl
parents:
diff changeset
250 if (n < 1) return DEFAULT; // malformed
96a9758165cd Initial revision
carl
parents:
diff changeset
251 char *key = strdup(email+1);
96a9758165cd Initial revision
carl
parents:
diff changeset
252 key[n] = '\0';
96a9758165cd Initial revision
carl
parents:
diff changeset
253 for (int i=0; i<n; i++) key[i] = tolower(key[i]);
96a9758165cd Initial revision
carl
parents:
diff changeset
254 char *rc = lookup1(key, map);
96a9758165cd Initial revision
carl
parents:
diff changeset
255 free(key);
96a9758165cd Initial revision
carl
parents:
diff changeset
256 return rc;
96a9758165cd Initial revision
carl
parents:
diff changeset
257 }
96a9758165cd Initial revision
carl
parents:
diff changeset
258
96a9758165cd Initial revision
carl
parents:
diff changeset
259
96a9758165cd Initial revision
carl
parents:
diff changeset
260 ////////////////////////////////////////////////
96a9758165cd Initial revision
carl
parents:
diff changeset
261 // find the dnsbl with a specific name
96a9758165cd Initial revision
carl
parents:
diff changeset
262 //
96a9758165cd Initial revision
carl
parents:
diff changeset
263 static DNSBLP find_dnsbl(CONFIG &dc, char *name);
96a9758165cd Initial revision
carl
parents:
diff changeset
264 static DNSBLP find_dnsbl(CONFIG &dc, char *name) {
96a9758165cd Initial revision
carl
parents:
diff changeset
265 dnsblp_map::iterator i = dc.dnsbls.find(name);
96a9758165cd Initial revision
carl
parents:
diff changeset
266 if (i == dc.dnsbls.end()) return NULL;
96a9758165cd Initial revision
carl
parents:
diff changeset
267 return (*i).second;
96a9758165cd Initial revision
carl
parents:
diff changeset
268 }
96a9758165cd Initial revision
carl
parents:
diff changeset
269
96a9758165cd Initial revision
carl
parents:
diff changeset
270
96a9758165cd Initial revision
carl
parents:
diff changeset
271 ////////////////////////////////////////////////
96a9758165cd Initial revision
carl
parents:
diff changeset
272 // find the dnsbll with a specific name
96a9758165cd Initial revision
carl
parents:
diff changeset
273 //
96a9758165cd Initial revision
carl
parents:
diff changeset
274 static DNSBLLP find_dnsbll(CONFIG &dc, char *name);
96a9758165cd Initial revision
carl
parents:
diff changeset
275 static DNSBLLP find_dnsbll(CONFIG &dc, char *name) {
96a9758165cd Initial revision
carl
parents:
diff changeset
276 dnsbllp_map::iterator i = dc.dnsblls.find(name);
96a9758165cd Initial revision
carl
parents:
diff changeset
277 if (i == dc.dnsblls.end()) return NULL;
96a9758165cd Initial revision
carl
parents:
diff changeset
278 return (*i).second;
96a9758165cd Initial revision
carl
parents:
diff changeset
279 }
96a9758165cd Initial revision
carl
parents:
diff changeset
280
96a9758165cd Initial revision
carl
parents:
diff changeset
281
96a9758165cd Initial revision
carl
parents:
diff changeset
282 ////////////////////////////////////////////////
96a9758165cd Initial revision
carl
parents:
diff changeset
283 // find the envfrom map with a specific name
96a9758165cd Initial revision
carl
parents:
diff changeset
284 //
96a9758165cd Initial revision
carl
parents:
diff changeset
285 static string_map* find_from_map(CONFIG &dc, char *name);
96a9758165cd Initial revision
carl
parents:
diff changeset
286 static string_map* find_from_map(CONFIG &dc, char *name) {
96a9758165cd Initial revision
carl
parents:
diff changeset
287 from_map::iterator i = dc.env_from.find(name);
96a9758165cd Initial revision
carl
parents:
diff changeset
288 if (i == dc.env_from.end()) return NULL;
96a9758165cd Initial revision
carl
parents:
diff changeset
289 return (*i).second;
96a9758165cd Initial revision
carl
parents:
diff changeset
290 }
96a9758165cd Initial revision
carl
parents:
diff changeset
291
96a9758165cd Initial revision
carl
parents:
diff changeset
292
96a9758165cd Initial revision
carl
parents:
diff changeset
293 static string_map& really_find_from_map(CONFIG &dc, char *name);
96a9758165cd Initial revision
carl
parents:
diff changeset
294 static string_map& really_find_from_map(CONFIG &dc, char *name) {
96a9758165cd Initial revision
carl
parents:
diff changeset
295 string_map *sm = find_from_map(dc, name);
96a9758165cd Initial revision
carl
parents:
diff changeset
296 if (!sm) {
96a9758165cd Initial revision
carl
parents:
diff changeset
297 sm = new string_map;
96a9758165cd Initial revision
carl
parents:
diff changeset
298 dc.env_from[name] = sm;
96a9758165cd Initial revision
carl
parents:
diff changeset
299 }
96a9758165cd Initial revision
carl
parents:
diff changeset
300 return *sm;
96a9758165cd Initial revision
carl
parents:
diff changeset
301 }
96a9758165cd Initial revision
carl
parents:
diff changeset
302
96a9758165cd Initial revision
carl
parents:
diff changeset
303
96a9758165cd Initial revision
carl
parents:
diff changeset
304 ////////////////////////////////////////////////
96a9758165cd Initial revision
carl
parents:
diff changeset
305 // check a single dnsbl - we don't try very hard, just
96a9758165cd Initial revision
carl
parents:
diff changeset
306 // using the default resolver retry settings. If we cannot
96a9758165cd Initial revision
carl
parents:
diff changeset
307 // get an answer, we just accept the mail. The caller
96a9758165cd Initial revision
carl
parents:
diff changeset
308 // must ensure thread safety.
96a9758165cd Initial revision
carl
parents:
diff changeset
309 //
96a9758165cd Initial revision
carl
parents:
diff changeset
310 static status check_single(int ip, DNSBL &bl);
96a9758165cd Initial revision
carl
parents:
diff changeset
311 static status check_single(int ip, DNSBL &bl) {
96a9758165cd Initial revision
carl
parents:
diff changeset
312 // make a dns question
96a9758165cd Initial revision
carl
parents:
diff changeset
313 const u_char *src = (const u_char *)&ip;
96a9758165cd Initial revision
carl
parents:
diff changeset
314 if (src[0] == 127) return oksofar; // don't do dns lookups on localhost
96a9758165cd Initial revision
carl
parents:
diff changeset
315 char question[NS_MAXDNAME];
96a9758165cd Initial revision
carl
parents:
diff changeset
316 snprintf(question, sizeof(question), "%u.%u.%u.%u.%s.", src[3], src[2], src[1], src[0], bl.suffix);
96a9758165cd Initial revision
carl
parents:
diff changeset
317 // ask the question
96a9758165cd Initial revision
carl
parents:
diff changeset
318 u_char answer[NS_PACKETSZ];
96a9758165cd Initial revision
carl
parents:
diff changeset
319 int length = res_search(question, ns_c_in, ns_t_a, answer, sizeof(answer));
96a9758165cd Initial revision
carl
parents:
diff changeset
320 if (length < 0) return oksofar; // error in getting answer
96a9758165cd Initial revision
carl
parents:
diff changeset
321 // parse the answer
96a9758165cd Initial revision
carl
parents:
diff changeset
322 ns_msg handle;
96a9758165cd Initial revision
carl
parents:
diff changeset
323 ns_rr rr;
96a9758165cd Initial revision
carl
parents:
diff changeset
324 if (ns_initparse(answer, length, &handle) != 0) return oksofar;
96a9758165cd Initial revision
carl
parents:
diff changeset
325 int rrnum = 0;
96a9758165cd Initial revision
carl
parents:
diff changeset
326 while (ns_parserr(&handle, ns_s_an, rrnum++, &rr) == 0) {
96a9758165cd Initial revision
carl
parents:
diff changeset
327 if (ns_rr_type(rr) == ns_t_a) {
96a9758165cd Initial revision
carl
parents:
diff changeset
328 // we see an A record, implies blacklisted ip address
96a9758165cd Initial revision
carl
parents:
diff changeset
329 return reject;
96a9758165cd Initial revision
carl
parents:
diff changeset
330 }
96a9758165cd Initial revision
carl
parents:
diff changeset
331 }
96a9758165cd Initial revision
carl
parents:
diff changeset
332 return oksofar;
96a9758165cd Initial revision
carl
parents:
diff changeset
333 }
96a9758165cd Initial revision
carl
parents:
diff changeset
334
96a9758165cd Initial revision
carl
parents:
diff changeset
335
96a9758165cd Initial revision
carl
parents:
diff changeset
336 ////////////////////////////////////////////////
96a9758165cd Initial revision
carl
parents:
diff changeset
337 // check the dnsbls specified for this recipient
96a9758165cd Initial revision
carl
parents:
diff changeset
338 //
96a9758165cd Initial revision
carl
parents:
diff changeset
339 static status check_dnsbl(mlfiPriv &priv, DNSBLLP dnsbllp, DNSBLP &rejectlist);
96a9758165cd Initial revision
carl
parents:
diff changeset
340 static status check_dnsbl(mlfiPriv &priv, DNSBLLP dnsbllp, DNSBLP &rejectlist) {
96a9758165cd Initial revision
carl
parents:
diff changeset
341 if (priv.authenticated) return oksofar;
96a9758165cd Initial revision
carl
parents:
diff changeset
342 if (!dnsbllp) return oksofar;
96a9758165cd Initial revision
carl
parents:
diff changeset
343 DNSBLL &dnsbll = *dnsbllp;
96a9758165cd Initial revision
carl
parents:
diff changeset
344 for (DNSBLL::iterator i=dnsbll.begin(); i!=dnsbll.end(); i++) {
96a9758165cd Initial revision
carl
parents:
diff changeset
345 DNSBLP dp = *i; // non null by construction
96a9758165cd Initial revision
carl
parents:
diff changeset
346 status st;
96a9758165cd Initial revision
carl
parents:
diff changeset
347 map<DNSBLP, status>::iterator f = priv.checked.find(dp);
96a9758165cd Initial revision
carl
parents:
diff changeset
348 if (f == priv.checked.end()) {
96a9758165cd Initial revision
carl
parents:
diff changeset
349 // have not checked this list yet
96a9758165cd Initial revision
carl
parents:
diff changeset
350 pthread_mutex_lock(&resolve_mutex);
96a9758165cd Initial revision
carl
parents:
diff changeset
351 st = check_single(priv.ip, *dp);
96a9758165cd Initial revision
carl
parents:
diff changeset
352 pthread_mutex_unlock(&resolve_mutex);
96a9758165cd Initial revision
carl
parents:
diff changeset
353 rejectlist = dp;
96a9758165cd Initial revision
carl
parents:
diff changeset
354 priv.checked[dp] = st;
96a9758165cd Initial revision
carl
parents:
diff changeset
355 }
96a9758165cd Initial revision
carl
parents:
diff changeset
356 else {
96a9758165cd Initial revision
carl
parents:
diff changeset
357 st = (*f).second;
96a9758165cd Initial revision
carl
parents:
diff changeset
358 rejectlist = (*f).first;
96a9758165cd Initial revision
carl
parents:
diff changeset
359 }
96a9758165cd Initial revision
carl
parents:
diff changeset
360 if (st == reject) return st;
96a9758165cd Initial revision
carl
parents:
diff changeset
361 }
96a9758165cd Initial revision
carl
parents:
diff changeset
362 return oksofar;
96a9758165cd Initial revision
carl
parents:
diff changeset
363 }
96a9758165cd Initial revision
carl
parents:
diff changeset
364
96a9758165cd Initial revision
carl
parents:
diff changeset
365
96a9758165cd Initial revision
carl
parents:
diff changeset
366 ////////////////////////////////////////////////
96a9758165cd Initial revision
carl
parents:
diff changeset
367 // start of sendmail milter interfaces
96a9758165cd Initial revision
carl
parents:
diff changeset
368 //
96a9758165cd Initial revision
carl
parents:
diff changeset
369 sfsistat mlfi_connect(SMFICTX *ctx, char *hostname, _SOCK_ADDR *hostaddr)
96a9758165cd Initial revision
carl
parents:
diff changeset
370 {
96a9758165cd Initial revision
carl
parents:
diff changeset
371 // allocate some private memory
96a9758165cd Initial revision
carl
parents:
diff changeset
372 mlfiPriv *priv = new mlfiPriv;
96a9758165cd Initial revision
carl
parents:
diff changeset
373 if (hostaddr->sa_family == AF_INET) {
96a9758165cd Initial revision
carl
parents:
diff changeset
374 priv->ip = ((struct sockaddr_in *)hostaddr)->sin_addr.s_addr;
96a9758165cd Initial revision
carl
parents:
diff changeset
375 }
96a9758165cd Initial revision
carl
parents:
diff changeset
376
96a9758165cd Initial revision
carl
parents:
diff changeset
377 // save the private data
96a9758165cd Initial revision
carl
parents:
diff changeset
378 smfi_setpriv(ctx, (void*)priv);
96a9758165cd Initial revision
carl
parents:
diff changeset
379
96a9758165cd Initial revision
carl
parents:
diff changeset
380 // continue processing
96a9758165cd Initial revision
carl
parents:
diff changeset
381 return SMFIS_CONTINUE;
96a9758165cd Initial revision
carl
parents:
diff changeset
382 }
96a9758165cd Initial revision
carl
parents:
diff changeset
383
96a9758165cd Initial revision
carl
parents:
diff changeset
384 sfsistat mlfi_envfrom(SMFICTX *ctx, char **from)
96a9758165cd Initial revision
carl
parents:
diff changeset
385 {
96a9758165cd Initial revision
carl
parents:
diff changeset
386 mlfiPriv &priv = *MLFIPRIV;
96a9758165cd Initial revision
carl
parents:
diff changeset
387 priv.mailaddr = strdup(from[0]);
96a9758165cd Initial revision
carl
parents:
diff changeset
388 priv.authenticated = (smfi_getsymval(ctx, "{auth_authen}") != NULL);
96a9758165cd Initial revision
carl
parents:
diff changeset
389 return SMFIS_CONTINUE;
96a9758165cd Initial revision
carl
parents:
diff changeset
390 }
96a9758165cd Initial revision
carl
parents:
diff changeset
391
96a9758165cd Initial revision
carl
parents:
diff changeset
392 sfsistat mlfi_envrcpt(SMFICTX *ctx, char **rcpt)
96a9758165cd Initial revision
carl
parents:
diff changeset
393 {
96a9758165cd Initial revision
carl
parents:
diff changeset
394 DNSBLP rejectlist = NULL; // list that caused the reject
96a9758165cd Initial revision
carl
parents:
diff changeset
395 status st = oksofar;
96a9758165cd Initial revision
carl
parents:
diff changeset
396 mlfiPriv &priv = *MLFIPRIV;
96a9758165cd Initial revision
carl
parents:
diff changeset
397 CONFIG &dc = *priv.pc;
96a9758165cd Initial revision
carl
parents:
diff changeset
398 char *rcptaddr = rcpt[0];
96a9758165cd Initial revision
carl
parents:
diff changeset
399 char *dnsname = lookup(rcptaddr, dc.env_to_dnsbll);
96a9758165cd Initial revision
carl
parents:
diff changeset
400 char *fromname = lookup(rcptaddr, dc.env_to_chkfrom);
96a9758165cd Initial revision
carl
parents:
diff changeset
401 if ((strcmp(dnsname, BLACK) == 0) ||
96a9758165cd Initial revision
carl
parents:
diff changeset
402 (strcmp(fromname, BLACK) == 0)) {
96a9758165cd Initial revision
carl
parents:
diff changeset
403 st = black; // two options to blacklist this recipient
96a9758165cd Initial revision
carl
parents:
diff changeset
404 }
96a9758165cd Initial revision
carl
parents:
diff changeset
405 else if (strcmp(fromname, WHITE) == 0) {
96a9758165cd Initial revision
carl
parents:
diff changeset
406 st = white;
96a9758165cd Initial revision
carl
parents:
diff changeset
407 }
96a9758165cd Initial revision
carl
parents:
diff changeset
408 else {
96a9758165cd Initial revision
carl
parents:
diff changeset
409 // check an env_from map
96a9758165cd Initial revision
carl
parents:
diff changeset
410 string_map *sm = find_from_map(dc, fromname);
96a9758165cd Initial revision
carl
parents:
diff changeset
411 if (sm != NULL) {
96a9758165cd Initial revision
carl
parents:
diff changeset
412 fromname = lookup(priv.mailaddr, *sm); // returns default if name not in map
96a9758165cd Initial revision
carl
parents:
diff changeset
413 if (strcmp(fromname, BLACK) == 0) {
96a9758165cd Initial revision
carl
parents:
diff changeset
414 st = black; // blacklist this envelope from value
96a9758165cd Initial revision
carl
parents:
diff changeset
415 }
96a9758165cd Initial revision
carl
parents:
diff changeset
416 if (strcmp(fromname, WHITE) == 0) {
96a9758165cd Initial revision
carl
parents:
diff changeset
417 st = white; // blacklist this envelope from value
96a9758165cd Initial revision
carl
parents:
diff changeset
418 }
96a9758165cd Initial revision
carl
parents:
diff changeset
419 }
96a9758165cd Initial revision
carl
parents:
diff changeset
420 }
96a9758165cd Initial revision
carl
parents:
diff changeset
421 if ((st == oksofar) && (strcmp(dnsname, WHITE) != 0)) {
96a9758165cd Initial revision
carl
parents:
diff changeset
422 // check dns lists
96a9758165cd Initial revision
carl
parents:
diff changeset
423 st = check_dnsbl(priv, find_dnsbll(dc, dnsname), rejectlist);
96a9758165cd Initial revision
carl
parents:
diff changeset
424 }
96a9758165cd Initial revision
carl
parents:
diff changeset
425
96a9758165cd Initial revision
carl
parents:
diff changeset
426 if (st == reject) {
96a9758165cd Initial revision
carl
parents:
diff changeset
427 // reject the recipient based on some dnsbl
96a9758165cd Initial revision
carl
parents:
diff changeset
428 char adr[sizeof "255.255.255.255"];
96a9758165cd Initial revision
carl
parents:
diff changeset
429 adr[0] = '\0';
96a9758165cd Initial revision
carl
parents:
diff changeset
430 const char *rc = inet_ntop(AF_INET, (const u_char *)&priv.ip, adr, sizeof(adr));
96a9758165cd Initial revision
carl
parents:
diff changeset
431 char buf[2000];
96a9758165cd Initial revision
carl
parents:
diff changeset
432 snprintf(buf, sizeof(buf), rejectlist->message, adr, adr);
96a9758165cd Initial revision
carl
parents:
diff changeset
433 smfi_setreply(ctx, "550", "5.7.1", buf);
96a9758165cd Initial revision
carl
parents:
diff changeset
434 return SMFIS_REJECT;
96a9758165cd Initial revision
carl
parents:
diff changeset
435 }
96a9758165cd Initial revision
carl
parents:
diff changeset
436 else if (st == black) {
96a9758165cd Initial revision
carl
parents:
diff changeset
437 // reject the recipient based on blacklisting either from or to
96a9758165cd Initial revision
carl
parents:
diff changeset
438 smfi_setreply(ctx, "550", "5.7.1", "no such user");
96a9758165cd Initial revision
carl
parents:
diff changeset
439 return SMFIS_REJECT;
96a9758165cd Initial revision
carl
parents:
diff changeset
440 }
96a9758165cd Initial revision
carl
parents:
diff changeset
441 else {
96a9758165cd Initial revision
carl
parents:
diff changeset
442 // accept the recipient
96a9758165cd Initial revision
carl
parents:
diff changeset
443 return SMFIS_CONTINUE;
96a9758165cd Initial revision
carl
parents:
diff changeset
444 }
96a9758165cd Initial revision
carl
parents:
diff changeset
445 }
96a9758165cd Initial revision
carl
parents:
diff changeset
446
96a9758165cd Initial revision
carl
parents:
diff changeset
447 sfsistat mlfi_eom_or_abort(SMFICTX *ctx)
96a9758165cd Initial revision
carl
parents:
diff changeset
448 {
96a9758165cd Initial revision
carl
parents:
diff changeset
449 mlfiPriv &priv = *MLFIPRIV;
96a9758165cd Initial revision
carl
parents:
diff changeset
450 if (priv.mailaddr) {
96a9758165cd Initial revision
carl
parents:
diff changeset
451 free(priv.mailaddr);
96a9758165cd Initial revision
carl
parents:
diff changeset
452 priv.mailaddr = NULL;
96a9758165cd Initial revision
carl
parents:
diff changeset
453 }
96a9758165cd Initial revision
carl
parents:
diff changeset
454 return SMFIS_CONTINUE;
96a9758165cd Initial revision
carl
parents:
diff changeset
455 }
96a9758165cd Initial revision
carl
parents:
diff changeset
456
96a9758165cd Initial revision
carl
parents:
diff changeset
457 sfsistat mlfi_close(SMFICTX *ctx)
96a9758165cd Initial revision
carl
parents:
diff changeset
458 {
96a9758165cd Initial revision
carl
parents:
diff changeset
459 mlfiPriv *priv = MLFIPRIV;
96a9758165cd Initial revision
carl
parents:
diff changeset
460 if (!priv) return SMFIS_CONTINUE;
96a9758165cd Initial revision
carl
parents:
diff changeset
461 delete priv;
96a9758165cd Initial revision
carl
parents:
diff changeset
462 smfi_setpriv(ctx, NULL);
96a9758165cd Initial revision
carl
parents:
diff changeset
463 return SMFIS_CONTINUE;
96a9758165cd Initial revision
carl
parents:
diff changeset
464 }
96a9758165cd Initial revision
carl
parents:
diff changeset
465
96a9758165cd Initial revision
carl
parents:
diff changeset
466 struct smfiDesc smfilter =
96a9758165cd Initial revision
carl
parents:
diff changeset
467 {
96a9758165cd Initial revision
carl
parents:
diff changeset
468 "DNSBL", // filter name
96a9758165cd Initial revision
carl
parents:
diff changeset
469 SMFI_VERSION, // version code -- do not change
96a9758165cd Initial revision
carl
parents:
diff changeset
470 SMFIF_DELRCPT, // flags
96a9758165cd Initial revision
carl
parents:
diff changeset
471 mlfi_connect, // connection info filter
96a9758165cd Initial revision
carl
parents:
diff changeset
472 NULL, // SMTP HELO command filter
96a9758165cd Initial revision
carl
parents:
diff changeset
473 mlfi_envfrom, // envelope sender filter
96a9758165cd Initial revision
carl
parents:
diff changeset
474 mlfi_envrcpt, // envelope recipient filter
96a9758165cd Initial revision
carl
parents:
diff changeset
475 NULL, // header filter
96a9758165cd Initial revision
carl
parents:
diff changeset
476 NULL, // end of header
96a9758165cd Initial revision
carl
parents:
diff changeset
477 NULL, // body block filter
96a9758165cd Initial revision
carl
parents:
diff changeset
478 mlfi_eom_or_abort, // end of message
96a9758165cd Initial revision
carl
parents:
diff changeset
479 mlfi_eom_or_abort, // message aborted
96a9758165cd Initial revision
carl
parents:
diff changeset
480 mlfi_close, // connection cleanup
96a9758165cd Initial revision
carl
parents:
diff changeset
481 };
96a9758165cd Initial revision
carl
parents:
diff changeset
482
96a9758165cd Initial revision
carl
parents:
diff changeset
483
96a9758165cd Initial revision
carl
parents:
diff changeset
484 static void dumpit(char *name, string_map map);
96a9758165cd Initial revision
carl
parents:
diff changeset
485 static void dumpit(char *name, string_map map) {
96a9758165cd Initial revision
carl
parents:
diff changeset
486 fprintf(stderr, "\n");
96a9758165cd Initial revision
carl
parents:
diff changeset
487 for (string_map::iterator i=map.begin(); i!=map.end(); i++) {
96a9758165cd Initial revision
carl
parents:
diff changeset
488 fprintf(stderr, "%s %s->%s\n", name, (*i).first, (*i).second);
96a9758165cd Initial revision
carl
parents:
diff changeset
489 }
96a9758165cd Initial revision
carl
parents:
diff changeset
490 }
96a9758165cd Initial revision
carl
parents:
diff changeset
491
96a9758165cd Initial revision
carl
parents:
diff changeset
492
96a9758165cd Initial revision
carl
parents:
diff changeset
493 static void dumpit(from_map map);
96a9758165cd Initial revision
carl
parents:
diff changeset
494 static void dumpit(from_map map) {
96a9758165cd Initial revision
carl
parents:
diff changeset
495 fprintf(stderr, "\n");
96a9758165cd Initial revision
carl
parents:
diff changeset
496 for (from_map::iterator i=map.begin(); i!=map.end(); i++) {
96a9758165cd Initial revision
carl
parents:
diff changeset
497 fprintf(stderr, "envfrom map %s\n", (*i).first);
96a9758165cd Initial revision
carl
parents:
diff changeset
498 string_map *sm = (*i).second;
96a9758165cd Initial revision
carl
parents:
diff changeset
499 dumpit("envelope from", *sm);
96a9758165cd Initial revision
carl
parents:
diff changeset
500 }
96a9758165cd Initial revision
carl
parents:
diff changeset
501 }
96a9758165cd Initial revision
carl
parents:
diff changeset
502
96a9758165cd Initial revision
carl
parents:
diff changeset
503
96a9758165cd Initial revision
carl
parents:
diff changeset
504 static void dumpit();
96a9758165cd Initial revision
carl
parents:
diff changeset
505 static void dumpit() {
96a9758165cd Initial revision
carl
parents:
diff changeset
506 CONFIG &dc = *config;
96a9758165cd Initial revision
carl
parents:
diff changeset
507 fprintf(stderr, "dnsbls\n");
96a9758165cd Initial revision
carl
parents:
diff changeset
508 for (dnsblp_map::iterator i=dc.dnsbls.begin(); i!=dc.dnsbls.end(); i++) {
96a9758165cd Initial revision
carl
parents:
diff changeset
509 fprintf(stderr, "%s %s %s\n", (*i).first, (*i).second->suffix, (*i).second->message);
96a9758165cd Initial revision
carl
parents:
diff changeset
510 }
96a9758165cd Initial revision
carl
parents:
diff changeset
511 fprintf(stderr, "dnsbl_lists\n");
96a9758165cd Initial revision
carl
parents:
diff changeset
512 for (dnsbllp_map::iterator i=dc.dnsblls.begin(); i!=dc.dnsblls.end(); i++) {
96a9758165cd Initial revision
carl
parents:
diff changeset
513 char *name = (*i).first;
96a9758165cd Initial revision
carl
parents:
diff changeset
514 DNSBLL &dl = *((*i).second);
96a9758165cd Initial revision
carl
parents:
diff changeset
515 fprintf(stderr, "%s", name);
96a9758165cd Initial revision
carl
parents:
diff changeset
516 for (DNSBLL::iterator j=dl.begin(); j!=dl.end(); j++) {
96a9758165cd Initial revision
carl
parents:
diff changeset
517 DNSBL &d = **j;
96a9758165cd Initial revision
carl
parents:
diff changeset
518 fprintf(stderr, " %s", d.suffix);
96a9758165cd Initial revision
carl
parents:
diff changeset
519 }
96a9758165cd Initial revision
carl
parents:
diff changeset
520 fprintf(stderr, "\n");
96a9758165cd Initial revision
carl
parents:
diff changeset
521 }
96a9758165cd Initial revision
carl
parents:
diff changeset
522 }
96a9758165cd Initial revision
carl
parents:
diff changeset
523
96a9758165cd Initial revision
carl
parents:
diff changeset
524
96a9758165cd Initial revision
carl
parents:
diff changeset
525 ////////////////////////////////////////////////
96a9758165cd Initial revision
carl
parents:
diff changeset
526 // load a single config file
96a9758165cd Initial revision
carl
parents:
diff changeset
527 //
96a9758165cd Initial revision
carl
parents:
diff changeset
528 static void load_conf(CONFIG &dc, char *fn);
96a9758165cd Initial revision
carl
parents:
diff changeset
529 static void load_conf(CONFIG &dc, char *fn) {
96a9758165cd Initial revision
carl
parents:
diff changeset
530 dc.config_files.push_back(fn);
96a9758165cd Initial revision
carl
parents:
diff changeset
531 map<char*, int, ltstr> commands;
96a9758165cd Initial revision
carl
parents:
diff changeset
532 enum {dummy, dnsbl, dnsbll, envfrom, envto, include};
96a9758165cd Initial revision
carl
parents:
diff changeset
533 commands["dnsbl" ] = dnsbl;
96a9758165cd Initial revision
carl
parents:
diff changeset
534 commands["dnsbl_list"] = dnsbll;
96a9758165cd Initial revision
carl
parents:
diff changeset
535 commands["env_from" ] = envfrom;
96a9758165cd Initial revision
carl
parents:
diff changeset
536 commands["env_to" ] = envto;
96a9758165cd Initial revision
carl
parents:
diff changeset
537 commands["include" ] = include;
96a9758165cd Initial revision
carl
parents:
diff changeset
538 const int LINE_SIZE = 2000;
96a9758165cd Initial revision
carl
parents:
diff changeset
539 ifstream is(fn);
96a9758165cd Initial revision
carl
parents:
diff changeset
540 if (is.fail()) return;
96a9758165cd Initial revision
carl
parents:
diff changeset
541 char line[LINE_SIZE];
96a9758165cd Initial revision
carl
parents:
diff changeset
542 char orig[LINE_SIZE];
96a9758165cd Initial revision
carl
parents:
diff changeset
543 char *delim = " \t";
96a9758165cd Initial revision
carl
parents:
diff changeset
544 int curline = 0;
96a9758165cd Initial revision
carl
parents:
diff changeset
545 while (!is.eof()) {
96a9758165cd Initial revision
carl
parents:
diff changeset
546 is.getline(line, LINE_SIZE);
96a9758165cd Initial revision
carl
parents:
diff changeset
547 snprintf(orig, sizeof(orig), "%s", line);
96a9758165cd Initial revision
carl
parents:
diff changeset
548 curline++;
96a9758165cd Initial revision
carl
parents:
diff changeset
549 int n = strlen(line);
96a9758165cd Initial revision
carl
parents:
diff changeset
550 for (int i=0; i<n; i++) line[i] = tolower(line[i]);
96a9758165cd Initial revision
carl
parents:
diff changeset
551 char *cmd = strtok(line, delim);
96a9758165cd Initial revision
carl
parents:
diff changeset
552 if (cmd && (cmd[0] != '#') && (cmd[0] != '\0')) {
96a9758165cd Initial revision
carl
parents:
diff changeset
553 // have a decent command
96a9758165cd Initial revision
carl
parents:
diff changeset
554 bool processed = false;
96a9758165cd Initial revision
carl
parents:
diff changeset
555 switch (commands[cmd]) {
96a9758165cd Initial revision
carl
parents:
diff changeset
556 case dnsbl: {
96a9758165cd Initial revision
carl
parents:
diff changeset
557 // have a new dnsbl to use
96a9758165cd Initial revision
carl
parents:
diff changeset
558 char *name = next_token(delim);
96a9758165cd Initial revision
carl
parents:
diff changeset
559 if (!name) break; // no name name
96a9758165cd Initial revision
carl
parents:
diff changeset
560 if (find_dnsbl(dc, name)) break; // duplicate entry
96a9758165cd Initial revision
carl
parents:
diff changeset
561 char *suff = strtok(NULL, delim);
96a9758165cd Initial revision
carl
parents:
diff changeset
562 if (!suff) break; // no dns suffic
96a9758165cd Initial revision
carl
parents:
diff changeset
563 char *msg = suff + strlen(suff);
96a9758165cd Initial revision
carl
parents:
diff changeset
564 if ((msg - line) >= strlen(orig)) break; // line ended with the dns suffix
96a9758165cd Initial revision
carl
parents:
diff changeset
565 msg = strchr(msg+1, '\'');
96a9758165cd Initial revision
carl
parents:
diff changeset
566 if (!msg) break; // no reply message template
96a9758165cd Initial revision
carl
parents:
diff changeset
567 msg++; // move over the leading '
96a9758165cd Initial revision
carl
parents:
diff changeset
568 if ((msg - line) >= strlen(orig)) break; // line ended with the leading quote
96a9758165cd Initial revision
carl
parents:
diff changeset
569 char *last = strchr(msg, '\'');
96a9758165cd Initial revision
carl
parents:
diff changeset
570 if (!last) break; // no trailing quote
96a9758165cd Initial revision
carl
parents:
diff changeset
571 *last = '\0'; // make it a null terminator
96a9758165cd Initial revision
carl
parents:
diff changeset
572 dc.dnsbls[name] = new DNSBL(register_string(suff), register_string(msg));
96a9758165cd Initial revision
carl
parents:
diff changeset
573 processed = true;
96a9758165cd Initial revision
carl
parents:
diff changeset
574 } break;
96a9758165cd Initial revision
carl
parents:
diff changeset
575
96a9758165cd Initial revision
carl
parents:
diff changeset
576 case dnsbll: {
96a9758165cd Initial revision
carl
parents:
diff changeset
577 // define a new combination of dnsbls
96a9758165cd Initial revision
carl
parents:
diff changeset
578 char *name = next_token(delim);
96a9758165cd Initial revision
carl
parents:
diff changeset
579 if (!name) break;
96a9758165cd Initial revision
carl
parents:
diff changeset
580 if (find_dnsbll(dc, name)) break; // duplicate entry
96a9758165cd Initial revision
carl
parents:
diff changeset
581 char *list = next_token(delim);
96a9758165cd Initial revision
carl
parents:
diff changeset
582 if (!list || (*list == '\0') || (*list == '#')) break;
96a9758165cd Initial revision
carl
parents:
diff changeset
583 DNSBLLP d = new DNSBLL;
96a9758165cd Initial revision
carl
parents:
diff changeset
584 DNSBLP p = find_dnsbl(dc, list);
96a9758165cd Initial revision
carl
parents:
diff changeset
585 if (p) d->push_back(p);
96a9758165cd Initial revision
carl
parents:
diff changeset
586 while (true) {
96a9758165cd Initial revision
carl
parents:
diff changeset
587 list = next_token(delim);
96a9758165cd Initial revision
carl
parents:
diff changeset
588 if (!list || (*list == '\0') || (*list == '#')) break;
96a9758165cd Initial revision
carl
parents:
diff changeset
589 DNSBLP p = find_dnsbl(dc, list);
96a9758165cd Initial revision
carl
parents:
diff changeset
590 if (p) d->push_back(p);
96a9758165cd Initial revision
carl
parents:
diff changeset
591 }
96a9758165cd Initial revision
carl
parents:
diff changeset
592 dc.dnsblls[name] = d;
96a9758165cd Initial revision
carl
parents:
diff changeset
593 processed = true;
96a9758165cd Initial revision
carl
parents:
diff changeset
594 } break;
96a9758165cd Initial revision
carl
parents:
diff changeset
595
96a9758165cd Initial revision
carl
parents:
diff changeset
596 case envfrom: {
96a9758165cd Initial revision
carl
parents:
diff changeset
597 // add an entry into the named string_map
96a9758165cd Initial revision
carl
parents:
diff changeset
598 char *name = next_token(delim);
96a9758165cd Initial revision
carl
parents:
diff changeset
599 if (!name) break;
96a9758165cd Initial revision
carl
parents:
diff changeset
600 char *from = next_token(delim);
96a9758165cd Initial revision
carl
parents:
diff changeset
601 if (!from) break;
96a9758165cd Initial revision
carl
parents:
diff changeset
602 char *list = next_token(delim);
96a9758165cd Initial revision
carl
parents:
diff changeset
603 if (!list) break;
96a9758165cd Initial revision
carl
parents:
diff changeset
604 if ((strcmp(list, WHITE) == 0) ||
96a9758165cd Initial revision
carl
parents:
diff changeset
605 (strcmp(list, BLACK) == 0)) {
96a9758165cd Initial revision
carl
parents:
diff changeset
606 string_map &fm = really_find_from_map(dc, name);
96a9758165cd Initial revision
carl
parents:
diff changeset
607 fm[from] = list;
96a9758165cd Initial revision
carl
parents:
diff changeset
608 processed = true;
96a9758165cd Initial revision
carl
parents:
diff changeset
609 }
96a9758165cd Initial revision
carl
parents:
diff changeset
610 else {
96a9758165cd Initial revision
carl
parents:
diff changeset
611 // list may be the name of a previously defined from_map
96a9758165cd Initial revision
carl
parents:
diff changeset
612 string_map *m = find_from_map(dc, list);
96a9758165cd Initial revision
carl
parents:
diff changeset
613 if (m && (strcmp(list,name) != 0)) {
96a9758165cd Initial revision
carl
parents:
diff changeset
614 string_map &pm = *m;
96a9758165cd Initial revision
carl
parents:
diff changeset
615 string_map &fm = really_find_from_map(dc, name);
96a9758165cd Initial revision
carl
parents:
diff changeset
616 fm.insert(pm.begin(), pm.end());
96a9758165cd Initial revision
carl
parents:
diff changeset
617 processed = true;
96a9758165cd Initial revision
carl
parents:
diff changeset
618 }
96a9758165cd Initial revision
carl
parents:
diff changeset
619 }
96a9758165cd Initial revision
carl
parents:
diff changeset
620 } break;
96a9758165cd Initial revision
carl
parents:
diff changeset
621
96a9758165cd Initial revision
carl
parents:
diff changeset
622 case envto: {
96a9758165cd Initial revision
carl
parents:
diff changeset
623 // define the dnsbl_list and env_from maps to use for this recipient
96a9758165cd Initial revision
carl
parents:
diff changeset
624 char *to = next_token(delim);
96a9758165cd Initial revision
carl
parents:
diff changeset
625 if (!to) break;
96a9758165cd Initial revision
carl
parents:
diff changeset
626 char *list = next_token(delim);
96a9758165cd Initial revision
carl
parents:
diff changeset
627 if (!list) break;
96a9758165cd Initial revision
carl
parents:
diff changeset
628 char *from = next_token(delim);
96a9758165cd Initial revision
carl
parents:
diff changeset
629 if (!from) break;
96a9758165cd Initial revision
carl
parents:
diff changeset
630 dc.env_to_dnsbll[to] = list;
96a9758165cd Initial revision
carl
parents:
diff changeset
631 dc.env_to_chkfrom[to] = from;
96a9758165cd Initial revision
carl
parents:
diff changeset
632 processed = true;
96a9758165cd Initial revision
carl
parents:
diff changeset
633 } break;
96a9758165cd Initial revision
carl
parents:
diff changeset
634
96a9758165cd Initial revision
carl
parents:
diff changeset
635 case include: {
96a9758165cd Initial revision
carl
parents:
diff changeset
636 char *fn = next_token(delim);
96a9758165cd Initial revision
carl
parents:
diff changeset
637 if (fn) {
96a9758165cd Initial revision
carl
parents:
diff changeset
638 bool ok = true;
96a9758165cd Initial revision
carl
parents:
diff changeset
639 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) {
96a9758165cd Initial revision
carl
parents:
diff changeset
640 char *f = *i;
96a9758165cd Initial revision
carl
parents:
diff changeset
641 if (strcmp(f, fn) == 0) {
96a9758165cd Initial revision
carl
parents:
diff changeset
642 my_syslog("recursive include file detected");
96a9758165cd Initial revision
carl
parents:
diff changeset
643 ok = false;
96a9758165cd Initial revision
carl
parents:
diff changeset
644 break;
96a9758165cd Initial revision
carl
parents:
diff changeset
645 }
96a9758165cd Initial revision
carl
parents:
diff changeset
646 }
96a9758165cd Initial revision
carl
parents:
diff changeset
647 if (ok) {
96a9758165cd Initial revision
carl
parents:
diff changeset
648 load_conf(dc, fn);
96a9758165cd Initial revision
carl
parents:
diff changeset
649 processed = true;
96a9758165cd Initial revision
carl
parents:
diff changeset
650 }
96a9758165cd Initial revision
carl
parents:
diff changeset
651 }
96a9758165cd Initial revision
carl
parents:
diff changeset
652 } break;
96a9758165cd Initial revision
carl
parents:
diff changeset
653
96a9758165cd Initial revision
carl
parents:
diff changeset
654 default: {
96a9758165cd Initial revision
carl
parents:
diff changeset
655 } break;
96a9758165cd Initial revision
carl
parents:
diff changeset
656 }
96a9758165cd Initial revision
carl
parents:
diff changeset
657 if (!processed) {
96a9758165cd Initial revision
carl
parents:
diff changeset
658 pthread_mutex_lock(&syslog_mutex);
96a9758165cd Initial revision
carl
parents:
diff changeset
659 openlog("dnsbl", LOG_PID, LOG_MAIL);
96a9758165cd Initial revision
carl
parents:
diff changeset
660 syslog(LOG_ERR, "ignoring file %s line %d : %s\n", fn, curline, orig);
96a9758165cd Initial revision
carl
parents:
diff changeset
661 closelog();
96a9758165cd Initial revision
carl
parents:
diff changeset
662 pthread_mutex_unlock(&syslog_mutex);
96a9758165cd Initial revision
carl
parents:
diff changeset
663 }
96a9758165cd Initial revision
carl
parents:
diff changeset
664 }
96a9758165cd Initial revision
carl
parents:
diff changeset
665 }
96a9758165cd Initial revision
carl
parents:
diff changeset
666 is.close();
96a9758165cd Initial revision
carl
parents:
diff changeset
667 }
96a9758165cd Initial revision
carl
parents:
diff changeset
668
96a9758165cd Initial revision
carl
parents:
diff changeset
669
96a9758165cd Initial revision
carl
parents:
diff changeset
670 ////////////////////////////////////////////////
96a9758165cd Initial revision
carl
parents:
diff changeset
671 // reload the config
96a9758165cd Initial revision
carl
parents:
diff changeset
672 //
96a9758165cd Initial revision
carl
parents:
diff changeset
673 static CONFIG* new_conf();
96a9758165cd Initial revision
carl
parents:
diff changeset
674 static CONFIG* new_conf() {
96a9758165cd Initial revision
carl
parents:
diff changeset
675 my_syslog("loading new configuration");
96a9758165cd Initial revision
carl
parents:
diff changeset
676 CONFIG *newc = new CONFIG;
96a9758165cd Initial revision
carl
parents:
diff changeset
677 load_conf(*newc, "dnsbl.conf");
96a9758165cd Initial revision
carl
parents:
diff changeset
678 newc->load_time = time(NULL);
96a9758165cd Initial revision
carl
parents:
diff changeset
679 return newc;
96a9758165cd Initial revision
carl
parents:
diff changeset
680 }
96a9758165cd Initial revision
carl
parents:
diff changeset
681
96a9758165cd Initial revision
carl
parents:
diff changeset
682
96a9758165cd Initial revision
carl
parents:
diff changeset
683 ////////////////////////////////////////////////
96a9758165cd Initial revision
carl
parents:
diff changeset
684 // thread to watch the old config files for changes
96a9758165cd Initial revision
carl
parents:
diff changeset
685 // and reload when needed. we also cleanup old
96a9758165cd Initial revision
carl
parents:
diff changeset
686 // configs whose reference count has gone to zero.
96a9758165cd Initial revision
carl
parents:
diff changeset
687 //
96a9758165cd Initial revision
carl
parents:
diff changeset
688 static void* config_loader(void *arg);
96a9758165cd Initial revision
carl
parents:
diff changeset
689 static void* config_loader(void *arg) {
96a9758165cd Initial revision
carl
parents:
diff changeset
690 typedef set<CONFIG *> configp_set;
96a9758165cd Initial revision
carl
parents:
diff changeset
691 configp_set old_configs;
96a9758165cd Initial revision
carl
parents:
diff changeset
692 while (true) {
96a9758165cd Initial revision
carl
parents:
diff changeset
693 sleep(180); // look for modifications every 3 minutes
96a9758165cd Initial revision
carl
parents:
diff changeset
694 CONFIG &dc = *config;
96a9758165cd Initial revision
carl
parents:
diff changeset
695 time_t then = dc.load_time;
96a9758165cd Initial revision
carl
parents:
diff changeset
696 struct stat st;
96a9758165cd Initial revision
carl
parents:
diff changeset
697 bool reload = false;
96a9758165cd Initial revision
carl
parents:
diff changeset
698 for (string_list::iterator i=dc.config_files.begin(); i!=dc.config_files.end(); i++) {
96a9758165cd Initial revision
carl
parents:
diff changeset
699 char *fn = *i;
96a9758165cd Initial revision
carl
parents:
diff changeset
700 if (stat(fn, &st)) reload = true; // file disappeared
96a9758165cd Initial revision
carl
parents:
diff changeset
701 else if (st.st_mtime > then) reload = true; // file modified
96a9758165cd Initial revision
carl
parents:
diff changeset
702 if (reload) break;
96a9758165cd Initial revision
carl
parents:
diff changeset
703 }
96a9758165cd Initial revision
carl
parents:
diff changeset
704 if (reload) {
96a9758165cd Initial revision
carl
parents:
diff changeset
705 CONFIG *newc = new_conf();
96a9758165cd Initial revision
carl
parents:
diff changeset
706 // replace the global config pointer
96a9758165cd Initial revision
carl
parents:
diff changeset
707 pthread_mutex_lock(&config_mutex);
96a9758165cd Initial revision
carl
parents:
diff changeset
708 CONFIG *old = config;
96a9758165cd Initial revision
carl
parents:
diff changeset
709 config = newc;
96a9758165cd Initial revision
carl
parents:
diff changeset
710 pthread_mutex_unlock(&config_mutex);
96a9758165cd Initial revision
carl
parents:
diff changeset
711 // dumpit(env_from);
96a9758165cd Initial revision
carl
parents:
diff changeset
712 // dumpit("envelope to dnsbl", env_to_dnsbl);
96a9758165cd Initial revision
carl
parents:
diff changeset
713 // dumpit("envelope to check from", env_to_chkfrom);
96a9758165cd Initial revision
carl
parents:
diff changeset
714 // dumpit();
96a9758165cd Initial revision
carl
parents:
diff changeset
715 if (old) old_configs.insert(old);
96a9758165cd Initial revision
carl
parents:
diff changeset
716 }
96a9758165cd Initial revision
carl
parents:
diff changeset
717 // now look for old configs with zero ref counts
96a9758165cd Initial revision
carl
parents:
diff changeset
718 for (configp_set::iterator i=old_configs.begin(); i!=old_configs.end(); ) {
96a9758165cd Initial revision
carl
parents:
diff changeset
719 CONFIG *old = *i;
96a9758165cd Initial revision
carl
parents:
diff changeset
720 if (!old->reference_count) {
96a9758165cd Initial revision
carl
parents:
diff changeset
721 delete old; // destructor does all the work
96a9758165cd Initial revision
carl
parents:
diff changeset
722 old_configs.erase(i++);
96a9758165cd Initial revision
carl
parents:
diff changeset
723 }
96a9758165cd Initial revision
carl
parents:
diff changeset
724 else i++;
96a9758165cd Initial revision
carl
parents:
diff changeset
725 }
96a9758165cd Initial revision
carl
parents:
diff changeset
726 }
96a9758165cd Initial revision
carl
parents:
diff changeset
727 }
96a9758165cd Initial revision
carl
parents:
diff changeset
728
96a9758165cd Initial revision
carl
parents:
diff changeset
729
96a9758165cd Initial revision
carl
parents:
diff changeset
730 static void usage(char *prog);
96a9758165cd Initial revision
carl
parents:
diff changeset
731 static void usage(char *prog)
96a9758165cd Initial revision
carl
parents:
diff changeset
732 {
96a9758165cd Initial revision
carl
parents:
diff changeset
733 fprintf(stderr, "Usage: %s -p socket-addr [-t timeout]\n", prog);
96a9758165cd Initial revision
carl
parents:
diff changeset
734 fprintf(stderr, "where socket-addr is for the connection to sendmail and should be one of\n");
96a9758165cd Initial revision
carl
parents:
diff changeset
735 fprintf(stderr, " inet:port@local-ip-address\n");
96a9758165cd Initial revision
carl
parents:
diff changeset
736 fprintf(stderr, " local:local-domain-socket-file-name\n");
96a9758165cd Initial revision
carl
parents:
diff changeset
737 }
96a9758165cd Initial revision
carl
parents:
diff changeset
738
96a9758165cd Initial revision
carl
parents:
diff changeset
739
96a9758165cd Initial revision
carl
parents:
diff changeset
740 int main(int argc, char**argv)
96a9758165cd Initial revision
carl
parents:
diff changeset
741 {
96a9758165cd Initial revision
carl
parents:
diff changeset
742 bool setconn = FALSE;
96a9758165cd Initial revision
carl
parents:
diff changeset
743 int c;
96a9758165cd Initial revision
carl
parents:
diff changeset
744 const char *args = "p:s:h";
96a9758165cd Initial revision
carl
parents:
diff changeset
745 extern char *optarg;
96a9758165cd Initial revision
carl
parents:
diff changeset
746
96a9758165cd Initial revision
carl
parents:
diff changeset
747 // Process command line options
96a9758165cd Initial revision
carl
parents:
diff changeset
748 while ((c = getopt(argc, argv, args)) != -1) {
96a9758165cd Initial revision
carl
parents:
diff changeset
749 switch (c) {
96a9758165cd Initial revision
carl
parents:
diff changeset
750 case 'p':
96a9758165cd Initial revision
carl
parents:
diff changeset
751 if (optarg == NULL || *optarg == '\0') {
96a9758165cd Initial revision
carl
parents:
diff changeset
752 fprintf(stderr, "Illegal conn: %s\n", optarg);
96a9758165cd Initial revision
carl
parents:
diff changeset
753 exit(EX_USAGE);
96a9758165cd Initial revision
carl
parents:
diff changeset
754 }
96a9758165cd Initial revision
carl
parents:
diff changeset
755 if (smfi_setconn(optarg) == MI_FAILURE) {
96a9758165cd Initial revision
carl
parents:
diff changeset
756 fprintf(stderr, "smfi_setconn failed\n");
96a9758165cd Initial revision
carl
parents:
diff changeset
757 exit(EX_SOFTWARE);
96a9758165cd Initial revision
carl
parents:
diff changeset
758 }
96a9758165cd Initial revision
carl
parents:
diff changeset
759
96a9758165cd Initial revision
carl
parents:
diff changeset
760 if (strncasecmp(optarg, "unix:", 5) == 0) unlink(optarg + 5);
96a9758165cd Initial revision
carl
parents:
diff changeset
761 else if (strncasecmp(optarg, "local:", 6) == 0) unlink(optarg + 6);
96a9758165cd Initial revision
carl
parents:
diff changeset
762 setconn = TRUE;
96a9758165cd Initial revision
carl
parents:
diff changeset
763 break;
96a9758165cd Initial revision
carl
parents:
diff changeset
764
96a9758165cd Initial revision
carl
parents:
diff changeset
765 case 't':
96a9758165cd Initial revision
carl
parents:
diff changeset
766 if (optarg == NULL || *optarg == '\0') {
96a9758165cd Initial revision
carl
parents:
diff changeset
767 fprintf(stderr, "Illegal timeout: %s\n", optarg);
96a9758165cd Initial revision
carl
parents:
diff changeset
768 exit(EX_USAGE);
96a9758165cd Initial revision
carl
parents:
diff changeset
769 }
96a9758165cd Initial revision
carl
parents:
diff changeset
770 if (smfi_settimeout(atoi(optarg)) == MI_FAILURE) {
96a9758165cd Initial revision
carl
parents:
diff changeset
771 fprintf(stderr, "smfi_settimeout failed\n");
96a9758165cd Initial revision
carl
parents:
diff changeset
772 exit(EX_SOFTWARE);
96a9758165cd Initial revision
carl
parents:
diff changeset
773 }
96a9758165cd Initial revision
carl
parents:
diff changeset
774 break;
96a9758165cd Initial revision
carl
parents:
diff changeset
775
96a9758165cd Initial revision
carl
parents:
diff changeset
776 case 'h':
96a9758165cd Initial revision
carl
parents:
diff changeset
777 default:
96a9758165cd Initial revision
carl
parents:
diff changeset
778 usage(argv[0]);
96a9758165cd Initial revision
carl
parents:
diff changeset
779 exit(EX_USAGE);
96a9758165cd Initial revision
carl
parents:
diff changeset
780 }
96a9758165cd Initial revision
carl
parents:
diff changeset
781 }
96a9758165cd Initial revision
carl
parents:
diff changeset
782 if (!setconn) {
96a9758165cd Initial revision
carl
parents:
diff changeset
783 fprintf(stderr, "%s: Missing required -p argument\n", argv[0]);
96a9758165cd Initial revision
carl
parents:
diff changeset
784 usage(argv[0]);
96a9758165cd Initial revision
carl
parents:
diff changeset
785 exit(EX_USAGE);
96a9758165cd Initial revision
carl
parents:
diff changeset
786 }
96a9758165cd Initial revision
carl
parents:
diff changeset
787 if (smfi_register(smfilter) == MI_FAILURE) {
96a9758165cd Initial revision
carl
parents:
diff changeset
788 fprintf(stderr, "smfi_register failed\n");
96a9758165cd Initial revision
carl
parents:
diff changeset
789 exit(EX_UNAVAILABLE);
96a9758165cd Initial revision
carl
parents:
diff changeset
790 }
96a9758165cd Initial revision
carl
parents:
diff changeset
791
96a9758165cd Initial revision
carl
parents:
diff changeset
792 // switch to background mode
96a9758165cd Initial revision
carl
parents:
diff changeset
793 if (daemon(1,0) < 0) {
96a9758165cd Initial revision
carl
parents:
diff changeset
794 fprintf(stderr, "daemon() call failed\n");
96a9758165cd Initial revision
carl
parents:
diff changeset
795 exit(EX_UNAVAILABLE);
96a9758165cd Initial revision
carl
parents:
diff changeset
796 }
96a9758165cd Initial revision
carl
parents:
diff changeset
797
96a9758165cd Initial revision
carl
parents:
diff changeset
798 // initialize the thread sync objects
96a9758165cd Initial revision
carl
parents:
diff changeset
799 pthread_mutex_init(&config_mutex, 0);
96a9758165cd Initial revision
carl
parents:
diff changeset
800 pthread_mutex_init(&syslog_mutex, 0);
96a9758165cd Initial revision
carl
parents:
diff changeset
801 pthread_mutex_init(&resolve_mutex, 0);
96a9758165cd Initial revision
carl
parents:
diff changeset
802
96a9758165cd Initial revision
carl
parents:
diff changeset
803 // load the initial config
96a9758165cd Initial revision
carl
parents:
diff changeset
804 config = new_conf();
96a9758165cd Initial revision
carl
parents:
diff changeset
805
96a9758165cd Initial revision
carl
parents:
diff changeset
806 // only create threads after the fork() in daemon
96a9758165cd Initial revision
carl
parents:
diff changeset
807 pthread_t tid;
96a9758165cd Initial revision
carl
parents:
diff changeset
808 if (pthread_create(&tid, 0, config_loader, 0))
96a9758165cd Initial revision
carl
parents:
diff changeset
809 my_syslog("failed to create config loader thread");
96a9758165cd Initial revision
carl
parents:
diff changeset
810 if (pthread_detach(tid))
96a9758165cd Initial revision
carl
parents:
diff changeset
811 my_syslog("failed to detach config loader thread");
96a9758165cd Initial revision
carl
parents:
diff changeset
812
96a9758165cd Initial revision
carl
parents:
diff changeset
813 // write the pid
96a9758165cd Initial revision
carl
parents:
diff changeset
814 const char *pidpath = "/var/run/dnsbl.pid";
96a9758165cd Initial revision
carl
parents:
diff changeset
815 unlink(pidpath);
96a9758165cd Initial revision
carl
parents:
diff changeset
816 FILE *f = fopen(pidpath, "w");
96a9758165cd Initial revision
carl
parents:
diff changeset
817 if (f) {
96a9758165cd Initial revision
carl
parents:
diff changeset
818 fprintf(f, "-%d\n", (u_int)getpgrp());
96a9758165cd Initial revision
carl
parents:
diff changeset
819 fclose(f);
96a9758165cd Initial revision
carl
parents:
diff changeset
820 }
96a9758165cd Initial revision
carl
parents:
diff changeset
821
96a9758165cd Initial revision
carl
parents:
diff changeset
822 int rc = smfi_main();
96a9758165cd Initial revision
carl
parents:
diff changeset
823 }