comparison src/dnsbl.cpp @ 41:d95af8129dfa

updates for 3.2, changing file layout, add queueid to messages
author carl
date Mon, 05 Jul 2004 10:52:02 -0700
parents dc3d8d1aa2d2
children afcf403709ef
comparison
equal deleted inserted replaced
40:dc3d8d1aa2d2 41:d95af8129dfa
177 177
178 static pthread_mutex_t config_mutex; 178 static pthread_mutex_t config_mutex;
179 static pthread_mutex_t syslog_mutex; 179 static pthread_mutex_t syslog_mutex;
180 static pthread_mutex_t resolve_mutex; 180 static pthread_mutex_t resolve_mutex;
181 181
182 struct mlfiPriv;
183
182 184
183 //////////////////////////////////////////////// 185 ////////////////////////////////////////////////
184 // helper to discard the strings and objects held by an ns_map 186 // helper to discard the strings and objects held by an ns_map
185 // 187 //
186 static void discard(ns_map &s); 188 static void discard(ns_map &s);
227 } 229 }
228 230
229 //////////////////////////////////////////////// 231 ////////////////////////////////////////////////
230 // syslog a message 232 // syslog a message
231 // 233 //
232 static void my_syslog(char *text); 234 static void my_syslog(mlfiPriv *priv, char *text);
233 static void my_syslog(char *text) {
234 pthread_mutex_lock(&syslog_mutex);
235 openlog("dnsbl", LOG_PID, LOG_MAIL);
236 syslog(LOG_NOTICE, "%s", text);
237 closelog();
238 pthread_mutex_unlock(&syslog_mutex);
239 }
240 235
241 236
242 // include the content scanner 237 // include the content scanner
243 #include "scanner.cpp" 238 #include "scanner.cpp"
244 239
252 CONFIG *pc; // global context with our maps 247 CONFIG *pc; // global context with our maps
253 int ip; // ip4 address of the smtp client 248 int ip; // ip4 address of the smtp client
254 map<DNSBLP, status> checked; // status from those lists 249 map<DNSBLP, status> checked; // status from those lists
255 // message specific data 250 // message specific data
256 char *mailaddr; // envelope from value 251 char *mailaddr; // envelope from value
252 char *queueid; // sendmail queue id
257 bool authenticated; // client authenticated? if so, suppress all dnsbl checks 253 bool authenticated; // client authenticated? if so, suppress all dnsbl checks
258 bool have_whites; // have at least one whitelisted recipient? need to accept content and remove all non-whitelisted recipients if it fails 254 bool have_whites; // have at least one whitelisted recipient? need to accept content and remove all non-whitelisted recipients if it fails
259 bool only_whites; // every recipient is whitelisted? 255 bool only_whites; // every recipient is whitelisted?
260 string_set non_whites; // remember the non-whitelisted recipients so we can remove them if need be 256 string_set non_whites; // remember the non-whitelisted recipients so we can remove them if need be
261 recorder *memory; // memory for the content scanner 257 recorder *memory; // memory for the content scanner
269 pc = config; 265 pc = config;
270 pc->reference_count++; 266 pc->reference_count++;
271 pthread_mutex_unlock(&config_mutex); 267 pthread_mutex_unlock(&config_mutex);
272 ip = 0; 268 ip = 0;
273 mailaddr = NULL; 269 mailaddr = NULL;
270 queueid = NULL;
274 authenticated = false; 271 authenticated = false;
275 have_whites = false; 272 have_whites = false;
276 only_whites = true; 273 only_whites = true;
277 memory = new recorder(&pc->html_tags, &pc->tlds); 274 memory = new recorder(this, &pc->html_tags, &pc->tlds);
278 scanner = new url_scanner(memory); 275 scanner = new url_scanner(memory);
279 } 276 }
280 mlfiPriv::~mlfiPriv() { 277 mlfiPriv::~mlfiPriv() {
281 pthread_mutex_lock(&config_mutex); 278 pthread_mutex_lock(&config_mutex);
282 pc->reference_count--; 279 pc->reference_count--;
283 pthread_mutex_unlock(&config_mutex); 280 pthread_mutex_unlock(&config_mutex);
284 reset(true); 281 reset(true);
285 } 282 }
286 void mlfiPriv::reset(bool final) { 283 void mlfiPriv::reset(bool final) {
287 if (mailaddr) free(mailaddr); 284 if (mailaddr) free(mailaddr);
285 if (queueid) free(queueid);
288 discard(non_whites); 286 discard(non_whites);
289 delete memory; 287 delete memory;
290 delete scanner; 288 delete scanner;
291 if (!final) { 289 if (!final) {
292 mailaddr = NULL; 290 mailaddr = NULL;
291 queueid = NULL;
293 authenticated = false; 292 authenticated = false;
294 have_whites = false; 293 have_whites = false;
295 only_whites = true; 294 only_whites = true;
296 memory = new recorder(&pc->html_tags, &pc->tlds); 295 memory = new recorder(this, &pc->html_tags, &pc->tlds);
297 scanner = new url_scanner(memory); 296 scanner = new url_scanner(memory);
298 } 297 }
299 } 298 }
300 299
301 #define MLFIPRIV ((struct mlfiPriv *) smfi_getpriv(ctx)) 300 #define MLFIPRIV ((struct mlfiPriv *) smfi_getpriv(ctx))
302 301
302
303 ////////////////////////////////////////////////
304 // syslog a message
305 //
306 static void my_syslog(mlfiPriv *priv, char *text) {
307 char buf[1000];
308 if (priv) {
309 snprintf(buf, sizeof(buf), "%s %s", priv->queueid, text);
310 text = buf;
311 }
312 pthread_mutex_lock(&syslog_mutex);
313 openlog("dnsbl", LOG_PID, LOG_MAIL);
314 syslog(LOG_NOTICE, "%s", text);
315 closelog();
316 pthread_mutex_unlock(&syslog_mutex);
317 }
318
319 static void my_syslog(char *text);
320 static void my_syslog(char *text) {
321 my_syslog(NULL, text);
322 }
303 323
304 //////////////////////////////////////////////// 324 ////////////////////////////////////////////////
305 // register a global string 325 // register a global string
306 // 326 //
307 static char* register_string(char *name); 327 static char* register_string(char *name);
582 snprintf(buf, sizeof(buf), "host %s found at %s", host, adr); 602 snprintf(buf, sizeof(buf), "host %s found at %s", host, adr);
583 } 603 }
584 else { 604 else {
585 snprintf(buf, sizeof(buf), "host %s not found", host); 605 snprintf(buf, sizeof(buf), "host %s not found", host);
586 } 606 }
587 my_syslog(buf); 607 my_syslog(&priv, buf);
588 } 608 }
589 if (ip) { 609 if (ip) {
590 status st = check_single(ip, dc.content_suffix); 610 status st = check_single(ip, dc.content_suffix);
591 if (st == reject) { 611 if (st == reject) {
592 discard(nameservers); 612 discard(nameservers);
613 snprintf(buf, sizeof(buf), "ns %s found at %s", host, adr); 633 snprintf(buf, sizeof(buf), "ns %s found at %s", host, adr);
614 } 634 }
615 else { 635 else {
616 snprintf(buf, sizeof(buf), "ns %s not found", host); 636 snprintf(buf, sizeof(buf), "ns %s not found", host);
617 } 637 }
618 my_syslog(buf); 638 my_syslog(&priv, buf);
619 } 639 }
620 if (ip) { 640 if (ip) {
621 status st = check_single(ip, dc.content_suffix); 641 status st = check_single(ip, dc.content_suffix);
622 if (st == reject) { 642 if (st == reject) {
623 host = register_string(priv.memory->hosts, host); // put a copy into priv.memory->hosts, and return that reference 643 host = register_string(priv.memory->hosts, host); // put a copy into priv.memory->hosts, and return that reference
629 discard(nameservers); 649 discard(nameservers);
630 host = NULL; 650 host = NULL;
631 int bin = priv.memory->binary_tags; 651 int bin = priv.memory->binary_tags;
632 int bad = priv.memory->bad_html_tags; 652 int bad = priv.memory->bad_html_tags;
633 lim = priv.pc->tag_limit; 653 lim = priv.pc->tag_limit;
634 if (bin > bad) return oksofar; // probably .zip or .tar.gz with random content 654 if (3*bin > bad) return oksofar; // probably .zip or .tar.gz with random content
635 if ((bad > lim) && (lim > 0)) return reject_tag; 655 if ((bad > lim) && (lim > 0)) return reject_tag;
636 return oksofar; 656 return oksofar;
637 } 657 }
638 658
639 659
667 { 687 {
668 DNSBLP rejectlist = NULL; // list that caused the reject 688 DNSBLP rejectlist = NULL; // list that caused the reject
669 status st = oksofar; 689 status st = oksofar;
670 mlfiPriv &priv = *MLFIPRIV; 690 mlfiPriv &priv = *MLFIPRIV;
671 CONFIG &dc = *priv.pc; 691 CONFIG &dc = *priv.pc;
692 if (!priv.queueid) priv.queueid = strdup(smfi_getsymval(ctx, "i");
672 char *rcptaddr = rcpt[0]; 693 char *rcptaddr = rcpt[0];
673 char *dnsname = lookup(rcptaddr, dc.env_to_dnsbll); 694 char *dnsname = lookup(rcptaddr, dc.env_to_dnsbll);
674 char *fromname = lookup(rcptaddr, dc.env_to_chkfrom); 695 char *fromname = lookup(rcptaddr, dc.env_to_chkfrom);
675 if ((strcmp(dnsname, BLACK) == 0) || 696 if ((strcmp(dnsname, BLACK) == 0) ||
676 (strcmp(fromname, BLACK) == 0)) { 697 (strcmp(fromname, BLACK) == 0)) {