annotate src/dnsbl.cpp @ 1:bd0b1a153f67

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