annotate src/dnsbl.cpp @ 3:7e1eb343a825

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