annotate src/dnsbl.cpp @ 5:793ac9cc114d stable-1-0

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