From d2564d25e802d1ee3230cf045c4940e836b5c6a2 Mon Sep 17 00:00:00 2001 From: John Denker Date: Sun, 29 Jul 2012 16:50:11 -0700 Subject: split ltgrey (and libltgrey) off from greylist; put some utility functions into their own file. --- tools/ltgrey.c | 153 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 153 insertions(+) create mode 100644 tools/ltgrey.c (limited to 'tools/ltgrey.c') diff --git a/tools/ltgrey.c b/tools/ltgrey.c new file mode 100644 index 0000000..afdb4c1 --- /dev/null +++ b/tools/ltgrey.c @@ -0,0 +1,153 @@ +#include +#include /* for exit(), atoi() */ + +#include "libltgrey.h" +#include "utils.h" +#include "qq_exit_codes.h" + +using namespace std; +pid_t mypid; +string progname; + +#define exeunt exit + +// forward reference: +void scan(const string progid, const string p, const int copies=1); + +int main(int _argc, char** _argv){ + mypid = getpid(); + int argc(_argc); + char** argv(_argv); + const string dirname("/var/qmail/greylist"); + whatsit foo(argv[0], dirname); argc--; argv++; + int scanmode(0); + int copies(1); + int penalty(0); + int stain(0); + int check(0); + while (argc > 0) { + string arg = argv[0]; argc--; argv++; + if (prefix(arg, "-scan")) { + scanmode++; + } else if (prefix(arg, "-copy")) { + copies++; + } else if (prefix(arg, "-verbose")) { + foo.verbosity++; + } else if (prefix(arg, "-check")) { + check++; + } else if (prefix(arg, "-penalize") + || prefix(arg, "-penalty")) { + if (!argc){ + cerr << "Option '" << arg << "' requires an argument" << endl; + exeunt(ex_syserr); + } + penalty = atoi(*argv++); argc--; + } else if (prefix(arg, "-stain")) { + if (!argc){ + cerr << "Option '" << arg << "' requires an argument" << endl; + exeunt(ex_syserr); + } + stain = atoi(*argv++); argc--; + } else if (prefix(arg, "-suffix")) { + if (!argc){ + cerr << "Option '" << arg << "' requires an argument" << endl; + exeunt(ex_syserr); + } + foo.suffix += *argv++; argc--; + } else { + cerr << "Unrecognized arg: " << arg << endl; + exeunt(ex_syserr); + } + } + if (foo.setup()) return ex_syserr; + + if (scanmode) { + scan(foo.progid, dirname, copies); + return 0; + } + + int sts = foo.doit(penalty, stain); + if (sts == ex_syserr) return sts; + if (!check) return ex_good; + +// check mode ... perform some extra checks. +// Probably a better design would be to +// (a) make more thorough DNS checks, and +// (b) move all the DNS checking to a separate module + + int dns = foo.check_dns(); + if (dns == ex_syserr || dns == ex_spam) return dns; + exeunt(sts); +} + +////////////////////////////////////////////////////////////////////// +// requires apt-get install libboost-filesystem-dev: +#include +#include +#include /* for stat(), getaddrinfo() */ +#include /* for stat() */ +#include /* for stat() */ +#include /* for perror */ +#include + +const int minute(60); +const int hour(60*minute); +const int day(24*hour); + +const int minimum_age(15*minute); +const int maximum_age(32*day); +const int probation(4*hour); + +void scan(const string progid, const string p, const int copies){ + timeval now; + gettimeofday(&now, NULL); + using namespace boost::filesystem; + + if (is_directory(p)) { + for (directory_iterator itr(p); itr!=directory_iterator(); ++itr) { + string basename = itr->path().filename(); + for (int ii = 0; ii < copies; ii++) + cout << setw(20) << left << basename << ' '; // display filename only + if (is_regular_file(itr->status())) { +// cout << " [" << file_size(itr->path()) << ']'; + struct stat mystat; + string fn = p + "/" + basename; + int rslt = stat(fn.c_str(), &mystat); + if (rslt != 0){ + cerr << progid << ": stat failed for '" + << fn << "' : "; + perror(0); + } + int mod_age = now.tv_sec - mystat.st_mtime; + int ac_age = now.tv_sec - mystat.st_atime; + cout << setw(10) << time_out(mod_age) + << " " << setw(10) << time_out(ac_age); + if (0) { + + } else if (mod_age < 0) { + cout << " penalty"; + } else if (mod_age < ac_age) { + cout << " parole"; + } else if (mod_age - ac_age < minimum_age // early bird, or completely unused + && mod_age > probation) { // did not diligently resubmit + cout << " disprobation"; + if (mod_age != ac_age) cout << "!"; + } else if (mod_age < minimum_age) { + cout << " young"; + if (mod_age != ac_age) cout << "!"; + } else if (mod_age == ac_age) { + cout << " unused"; + } else if (mod_age > maximum_age) { + cout << " expired"; + } else { + cout << " OK"; + } + } + cout << '\n'; + } + } + else { + // starting point is not a directory: + cout << (exists(p) ? "Found: " : "Not found: ") << p << '\n'; + } +} -- cgit v1.2.3 From 292a76b35fd16cf11613f79ea38693449e3317f6 Mon Sep 17 00:00:00 2001 From: John Denker Date: Sun, 29 Jul 2012 21:07:01 -0700 Subject: separate dnscheck functionality from other features; clean up a little --- tools/ltgrey.c | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) (limited to 'tools/ltgrey.c') diff --git a/tools/ltgrey.c b/tools/ltgrey.c index afdb4c1..c9f384f 100644 --- a/tools/ltgrey.c +++ b/tools/ltgrey.c @@ -14,17 +14,24 @@ string progname; // forward reference: void scan(const string progid, const string p, const int copies=1); + int main(int _argc, char** _argv){ + char* namevar; + std::string hostname; + char* ipvar; + std::string ipbase; + std::string ipname; + mypid = getpid(); int argc(_argc); char** argv(_argv); - const string dirname("/var/qmail/greylist"); - whatsit foo(argv[0], dirname); argc--; argv++; + const string parent_dir("/var/qmail/ltgrey"); + whatsit foo(argv[0], parent_dir); argc--; argv++; int scanmode(0); int copies(1); int penalty(0); int stain(0); - int check(0); + int dnscheck(0); while (argc > 0) { string arg = argv[0]; argc--; argv++; if (prefix(arg, "-scan")) { @@ -33,8 +40,8 @@ int main(int _argc, char** _argv){ copies++; } else if (prefix(arg, "-verbose")) { foo.verbosity++; - } else if (prefix(arg, "-check")) { - check++; + } else if (prefix(arg, "-dnscheck")) { + dnscheck++; } else if (prefix(arg, "-penalize") || prefix(arg, "-penalty")) { if (!argc){ @@ -61,23 +68,29 @@ int main(int _argc, char** _argv){ } if (foo.setup()) return ex_syserr; +// dnscheck mode ... +// Probably a better design would be to +// (a) make more thorough DNS checks, and +// (b) move all the DNS checking to a separate module + + ipvar = getenv("TCPREMOTEIP"); + if (ipvar) ipbase = ipvar; + namevar = getenv("TCPREMOTEHOST"); + if (namevar) hostname = namevar; + + if (dnscheck) { + exeunt(foo.check_dns(ipvar, namevar)); + } + if (scanmode) { + string dirname = parent_dir + "/quarante"; scan(foo.progid, dirname, copies); return 0; } int sts = foo.doit(penalty, stain); - if (sts == ex_syserr) return sts; - if (!check) return ex_good; - -// check mode ... perform some extra checks. -// Probably a better design would be to -// (a) make more thorough DNS checks, and -// (b) move all the DNS checking to a separate module + return sts; - int dns = foo.check_dns(); - if (dns == ex_syserr || dns == ex_spam) return dns; - exeunt(sts); } ////////////////////////////////////////////////////////////////////// -- cgit v1.2.3 From 67eb4c6c804c728db329a7f4a77d5a3cbd1b993c Mon Sep 17 00:00:00 2001 From: John Denker Date: Sun, 29 Jul 2012 21:55:41 -0700 Subject: get40 seems to be working --- tools/ltgrey.c | 58 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 32 insertions(+), 26 deletions(-) (limited to 'tools/ltgrey.c') diff --git a/tools/ltgrey.c b/tools/ltgrey.c index c9f384f..7a63b04 100644 --- a/tools/ltgrey.c +++ b/tools/ltgrey.c @@ -16,10 +16,7 @@ void scan(const string progid, const string p, const int copies=1); int main(int _argc, char** _argv){ - char* namevar; std::string hostname; - char* ipvar; - std::string ipbase; std::string ipname; mypid = getpid(); @@ -29,27 +26,35 @@ int main(int _argc, char** _argv){ whatsit foo(argv[0], parent_dir); argc--; argv++; int scanmode(0); int copies(1); - int penalty(0); + int shift(0); int stain(0); - int dnscheck(0); + int dns_mode(0); + string get40_mid; while (argc > 0) { string arg = argv[0]; argc--; argv++; if (prefix(arg, "-scan")) { scanmode++; - } else if (prefix(arg, "-copy")) { + } else if (prefix(arg, "-copy")) { copies++; - } else if (prefix(arg, "-verbose")) { + } else if (prefix(arg, "-verbose")) { foo.verbosity++; - } else if (prefix(arg, "-dnscheck")) { - dnscheck++; - } else if (prefix(arg, "-penalize") - || prefix(arg, "-penalty")) { + } else if (prefix(arg, "-dns_mode")) { + dns_mode++; + } else if (prefix(arg, "-get40")) { if (!argc){ cerr << "Option '" << arg << "' requires an argument" << endl; exeunt(ex_syserr); } - penalty = atoi(*argv++); argc--; - } else if (prefix(arg, "-stain")) { + get40_mid = *argv++; argc--; + + } else if (prefix(arg, "-shift") + || prefix(arg, "-shift")) { + if (!argc){ + cerr << "Option '" << arg << "' requires an argument" << endl; + exeunt(ex_syserr); + } + shift = atoi(*argv++); argc--; + } else if (prefix(arg, "-stain")) { if (!argc){ cerr << "Option '" << arg << "' requires an argument" << endl; exeunt(ex_syserr); @@ -68,27 +73,28 @@ int main(int _argc, char** _argv){ } if (foo.setup()) return ex_syserr; -// dnscheck mode ... -// Probably a better design would be to -// (a) make more thorough DNS checks, and -// (b) move all the DNS checking to a separate module - - ipvar = getenv("TCPREMOTEIP"); - if (ipvar) ipbase = ipvar; - namevar = getenv("TCPREMOTEHOST"); - if (namevar) hostname = namevar; - - if (dnscheck) { +// dns_mode mode ... +// Probably it would be better to make more thorough DNS checks. +// + if (dns_mode) { + char* ipvar = getenv("TCPREMOTEIP"); + char* namevar = getenv("TCPREMOTEHOST"); exeunt(foo.check_dns(ipvar, namevar)); } + if (get40_mid.length()){ + state_40 rslt = foo.get40(get40_mid); + cerr << foo.decode_40[rslt] << endl; + return 0; + } + if (scanmode) { string dirname = parent_dir + "/quarante"; scan(foo.progid, dirname, copies); return 0; } - int sts = foo.doit(penalty, stain); + int sts = foo.doit(shift, stain); return sts; } @@ -116,7 +122,7 @@ void scan(const string progid, const string p, const int copies){ gettimeofday(&now, NULL); using namespace boost::filesystem; - if (is_directory(p)) { + if (is_directory(p)) { for (directory_iterator itr(p); itr!=directory_iterator(); ++itr) { string basename = itr->path().filename(); for (int ii = 0; ii < copies; ii++) -- cgit v1.2.3 From 2f6fd23b841c1f4e3c18d1cbfd228784aa8c3298 Mon Sep 17 00:00:00 2001 From: John Denker Date: Sun, 29 Jul 2012 22:31:03 -0700 Subject: set40 appears to be working --- tools/ltgrey.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'tools/ltgrey.c') diff --git a/tools/ltgrey.c b/tools/ltgrey.c index 7a63b04..bb43b80 100644 --- a/tools/ltgrey.c +++ b/tools/ltgrey.c @@ -30,6 +30,7 @@ int main(int _argc, char** _argv){ int stain(0); int dns_mode(0); string get40_mid; + string set40_mid; while (argc > 0) { string arg = argv[0]; argc--; argv++; if (prefix(arg, "-scan")) { @@ -46,7 +47,12 @@ int main(int _argc, char** _argv){ exeunt(ex_syserr); } get40_mid = *argv++; argc--; - + } else if (prefix(arg, "-set40")) { + if (!argc){ + cerr << "Option '" << arg << "' requires an argument" << endl; + exeunt(ex_syserr); + } + set40_mid = *argv++; argc--; } else if (prefix(arg, "-shift") || prefix(arg, "-shift")) { if (!argc){ @@ -88,6 +94,10 @@ int main(int _argc, char** _argv){ return 0; } + if (set40_mid.length()){ + return foo.set40(set40_mid, shift); + } + if (scanmode) { string dirname = parent_dir + "/quarante"; scan(foo.progid, dirname, copies); -- cgit v1.2.3 From f8be4baf5a2318363b42f8883f66ed8a976dfc79 Mon Sep 17 00:00:00 2001 From: John Denker Date: Sun, 29 Jul 2012 23:26:28 -0700 Subject: scan40 appears to be working, much cleaner than last week's version --- tools/ltgrey.c | 77 ++-------------------------------------------------------- 1 file changed, 2 insertions(+), 75 deletions(-) (limited to 'tools/ltgrey.c') diff --git a/tools/ltgrey.c b/tools/ltgrey.c index bb43b80..1494338 100644 --- a/tools/ltgrey.c +++ b/tools/ltgrey.c @@ -33,7 +33,7 @@ int main(int _argc, char** _argv){ string set40_mid; while (argc > 0) { string arg = argv[0]; argc--; argv++; - if (prefix(arg, "-scan")) { + if (prefix(arg, "-scan40")) { scanmode++; } else if (prefix(arg, "-copy")) { copies++; @@ -99,8 +99,7 @@ int main(int _argc, char** _argv){ } if (scanmode) { - string dirname = parent_dir + "/quarante"; - scan(foo.progid, dirname, copies); + foo.scan40(copies); return 0; } @@ -108,75 +107,3 @@ int main(int _argc, char** _argv){ return sts; } - -////////////////////////////////////////////////////////////////////// -// requires apt-get install libboost-filesystem-dev: -#include -#include -#include /* for stat(), getaddrinfo() */ -#include /* for stat() */ -#include /* for stat() */ -#include /* for perror */ -#include - -const int minute(60); -const int hour(60*minute); -const int day(24*hour); - -const int minimum_age(15*minute); -const int maximum_age(32*day); -const int probation(4*hour); - -void scan(const string progid, const string p, const int copies){ - timeval now; - gettimeofday(&now, NULL); - using namespace boost::filesystem; - - if (is_directory(p)) { - for (directory_iterator itr(p); itr!=directory_iterator(); ++itr) { - string basename = itr->path().filename(); - for (int ii = 0; ii < copies; ii++) - cout << setw(20) << left << basename << ' '; // display filename only - if (is_regular_file(itr->status())) { -// cout << " [" << file_size(itr->path()) << ']'; - struct stat mystat; - string fn = p + "/" + basename; - int rslt = stat(fn.c_str(), &mystat); - if (rslt != 0){ - cerr << progid << ": stat failed for '" - << fn << "' : "; - perror(0); - } - int mod_age = now.tv_sec - mystat.st_mtime; - int ac_age = now.tv_sec - mystat.st_atime; - cout << setw(10) << time_out(mod_age) - << " " << setw(10) << time_out(ac_age); - if (0) { - - } else if (mod_age < 0) { - cout << " penalty"; - } else if (mod_age < ac_age) { - cout << " parole"; - } else if (mod_age - ac_age < minimum_age // early bird, or completely unused - && mod_age > probation) { // did not diligently resubmit - cout << " disprobation"; - if (mod_age != ac_age) cout << "!"; - } else if (mod_age < minimum_age) { - cout << " young"; - if (mod_age != ac_age) cout << "!"; - } else if (mod_age == ac_age) { - cout << " unused"; - } else if (mod_age > maximum_age) { - cout << " expired"; - } else { - cout << " OK"; - } - } - cout << '\n'; - } - } - else { - // starting point is not a directory: - cout << (exists(p) ? "Found: " : "Not found: ") << p << '\n'; - } -} -- cgit v1.2.3