annotate src/dnsbl.cpp @ 12:6ac6d6b822ce stable-2-0

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