Mercurial > dnsbl
comparison src/context.cpp @ 160:b3ed72ee6564
allow manual updates to auto whitelist files
author | carl |
---|---|
date | Tue, 10 Jul 2007 11:20:23 -0700 |
parents | a220bfb9211f |
children | c4bce911c276 |
comparison
equal
deleted
inserted
replaced
159:ea7c57a4a2d1 | 160:b3ed72ee6564 |
---|---|
6 | 6 |
7 */ | 7 */ |
8 | 8 |
9 #include "includes.h" | 9 #include "includes.h" |
10 | 10 |
11 // needed for socket io | 11 #include <arpa/inet.h> |
12 #include <unistd.h> | |
13 #include <sys/ioctl.h> | |
14 #include <net/if.h> | 12 #include <net/if.h> |
15 #include <arpa/inet.h> | 13 #include <netdb.h> |
16 #include <netinet/in.h> | 14 #include <netinet/in.h> |
17 #include <netinet/tcp.h> | 15 #include <netinet/tcp.h> |
18 #include <netdb.h> | 16 #include <sys/ioctl.h> |
19 #include <sys/socket.h> | 17 #include <sys/socket.h> |
18 #include <sys/stat.h> | |
20 #include <sys/un.h> | 19 #include <sys/un.h> |
20 #include <unistd.h> | |
21 | 21 |
22 static char* context_version="$Id$"; | 22 static char* context_version="$Id$"; |
23 | 23 |
24 char *token_autowhite; | 24 char *token_autowhite; |
25 char *token_black; | 25 char *token_black; |
423 WHITELISTER::WHITELISTER(char *f, int d) { | 423 WHITELISTER::WHITELISTER(char *f, int d) { |
424 fn = f; | 424 fn = f; |
425 days = d; | 425 days = d; |
426 pthread_mutex_init(&mutex, 0); | 426 pthread_mutex_init(&mutex, 0); |
427 need = false; | 427 need = false; |
428 loaded = time(NULL); | |
429 merge(); | |
430 } | |
431 | |
432 | |
433 void WHITELISTER::merge() { | |
434 time_t now = time(NULL); | |
428 ifstream ifs; | 435 ifstream ifs; |
429 ifs.open(fn); | 436 ifs.open(fn); |
430 if (!ifs.fail()) { | 437 if (!ifs.fail()) { |
431 const int maxlen = 1000; | 438 const int maxlen = 1000; |
432 char buf[maxlen]; | 439 char buf[maxlen]; |
433 while (ifs.getline(buf, maxlen)) { | 440 while (ifs.getline(buf, maxlen)) { |
434 char *p = strchr(buf, ' '); | 441 char *p = strchr(buf, ' '); |
435 if (p) { | 442 if (p) { |
436 *p = '\0'; | 443 *p = '\0'; |
437 char *who = strdup(buf); | 444 char *who = strdup(buf); |
438 int when = atoi(p+1); | 445 time_t when = atoi(p+1); |
439 rcpts[who] = when; | 446 if ((when == 0) || (when > now)) when = now; |
447 autowhite_sent::iterator i = rcpts.find(who); | |
448 if (i != rcpts.end()) { | |
449 time_t wh = (*i).second; | |
450 if (when > wh) rcpts[who] = when; | |
451 } | |
452 else { | |
453 rcpts[who] = when; | |
454 } | |
440 } | 455 } |
441 } | 456 } |
442 } | 457 } |
443 ifs.close(); | 458 ifs.close(); |
444 } | 459 } |
445 | 460 |
446 | 461 |
447 void WHITELISTER::writer() { | 462 void WHITELISTER::writer() { |
448 pthread_mutex_lock(&mutex); | 463 pthread_mutex_lock(&mutex); |
449 time_t limit = time(NULL) - days*86400; | 464 time_t limit = time(NULL) - days*86400; |
465 | |
466 // check for manually modified autowhitelist file | |
467 struct stat st; | |
468 if (stat(fn, &st)) need = true; // file has disappeared | |
469 else if (st.st_mtime > loaded) { | |
470 // file has been manually updated, merge new entries | |
471 merge(); | |
472 need = true; | |
473 } | |
474 | |
475 // purge old entries | |
450 for (autowhite_sent::iterator i=rcpts.begin(); i!=rcpts.end();) { | 476 for (autowhite_sent::iterator i=rcpts.begin(); i!=rcpts.end();) { |
451 time_t when = (*i).second; | 477 time_t when = (*i).second; |
452 if (when < limit) { | 478 if (when < limit) { |
453 char *who = (*i).first; | 479 char *who = (*i).first; |
454 free(who); | 480 free(who); |
458 i = j; | 484 i = j; |
459 need = true; | 485 need = true; |
460 } | 486 } |
461 else i++; | 487 else i++; |
462 } | 488 } |
489 | |
463 if (need) { | 490 if (need) { |
464 // dump the file | 491 // dump the file |
465 ofstream ofs; | 492 ofstream ofs; |
466 ofs.open(fn); | 493 ofs.open(fn); |
467 if (!ofs.fail()) { | 494 if (!ofs.fail()) { |
471 ofs << who << " " << when << endl; | 498 ofs << who << " " << when << endl; |
472 } | 499 } |
473 } | 500 } |
474 ofs.close(); | 501 ofs.close(); |
475 need = false; | 502 need = false; |
503 loaded = time(NULL); // update load time | |
476 } | 504 } |
477 pthread_mutex_unlock(&mutex); | 505 pthread_mutex_unlock(&mutex); |
478 } | 506 } |
479 | 507 |
480 | 508 |