summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Denker <jsd@av8n.com>2012-07-19 20:58:24 -0700
committerJohn Denker <jsd@av8n.com>2012-07-29 15:32:34 -0700
commit945767f12154698fab3e7e370486a5e9b09276e9 (patch)
tree39f2cceeebf52a9c380e6dc3d48befd81ca24a8e
parent8a896e9b2bce51742d264f6a23c4cce544ec5af7 (diff)
add "Scan" function to greylist
-rw-r--r--tools/greylist.c89
-rw-r--r--tools/makefile3
2 files changed, 77 insertions, 15 deletions
diff --git a/tools/greylist.c b/tools/greylist.c
index d769ff4..3ba502d 100644
--- a/tools/greylist.c
+++ b/tools/greylist.c
@@ -1,5 +1,6 @@
#include <stdlib.h> /* for exit(), getenv() */
#include <iostream>
+#include <iomanip>
#include <string>
#include <sys/types.h> /* for stat() */
@@ -11,8 +12,16 @@
#include <fcntl.h> /* for creat() */
#include <sys/time.h> /* for gettimeofday() */
+// requires apt-get install libboost-filesystem-dev:
+#include <boost/filesystem.hpp>
+
using namespace std;
+const int minute(60);
+const int hour(60*minute);
+const int day(24*hour);
+
+const int minimum_age(5*minute);
const int sa_good = 0;
const int bug_bait_grey = 1;
// qmail_queue and spamc have similar interpretations here:
@@ -30,27 +39,26 @@ void dump(const string var){
else cerr << " is not set." << endl;
}
-const string dirname("/var/qmail/greylist");
// int stat(const char *path, struct stat *buf);
// int fstat(int fd, struct stat *buf);
// int lstat(const char *path, struct stat *buf);
-const int minute(60);
-const int hour(60*minute);
-const int day(24*hour);
class whatsit{
public:
+ string dirname;
string progname;
pid_t mypid;
timeval now;
+ string ipbase;
string ipname;
+ string hostname;
int mod_age;
int ac_age;
- whatsit(const string name)
- : progname(name), mypid(getpid())
+ whatsit(const string name, const string _dirname)
+ : dirname(_dirname), progname(name), mypid(getpid()), mod_age(0), ac_age(0)
{
gettimeofday(&now, NULL);
}
@@ -59,10 +67,49 @@ public:
void update(const string msg, const timeval new_mod, const timeval new_ac);
};
+void scan(const string p){
+ 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();
+ cout << setw(20) << 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 << progname << ": 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) << mod_age
+ << " " << setw(10) << ac_age;
+ if (mod_age < minimum_age) {
+ cout << " young";
+ } else if (mod_age == ac_age) {
+ cout << " never used";
+ }
+ }
+ cout << '\n';
+ }
+ }
+ else {
+ // starting point is not a directory:
+ cout << (exists(p) ? "Found: " : "Not found: ") << p << '\n';
+ }
+}
+
void whatsit::update(const string msg, const timeval new_mod, const timeval new_ac){
cerr << progname << ": "
- << msg << ": " << ipname
- << " mod_age: " << mod_age
+ << msg << ": " << ipbase;
+ if (hostname.length()) cerr << " " << hostname;
+ cerr << " mod_age: " << mod_age
<< " ac_age: " << ac_age
<< endl;
timeval upd[2] = {
@@ -73,12 +120,22 @@ void whatsit::update(const string msg, const timeval new_mod, const timeval new_
utimes(ipname.c_str(), upd);
}
-int main(int argc, char** argv){
-
-// dump("TCPREMOTEIP");
-// dump("TCPREMOTEHOST");
+int main(int _argc, char** _argv){
+ int argc(_argc);
+ char** argv(_argv);
+ const string dirname("/var/qmail/greylist");
+ whatsit foo(argv[0], dirname); argc--; argv++;
+ while (argc > 0) {
+ string arg = argv[0]; argc--; argv++;
+ if (arg == "-scan") {
+ scan(dirname);
+ return 0;
+ }
+ else {
+ cerr << "Unrecognized arg, ignored: " << arg << endl;
+ }
+ }
- whatsit foo(argv[0]);
return foo.doit();
}
@@ -88,7 +145,9 @@ int whatsit::doit(){
cerr << progname << ": TCPREMOTEIP not set???" << endl;
exit(sa_syserr);
}
- string ipbase = ipvar;
+ ipbase = ipvar;
+ char* hostvar = getenv("TCPREMOTEHOST");
+ if (hostvar) hostname = hostvar;
// see if our directory exists:
struct stat dirstat;
@@ -134,7 +193,7 @@ int whatsit::doit(){
mod_age = now.tv_sec - ipstat.st_mtime;
ac_age = now.tv_sec - ipstat.st_atime;
timeval mod_orig = {ipstat.st_mtime, 0};
- if (mod_age < 5*minute) {
+ if (mod_age < minimum_age) {
update("early bird", mod_orig, now);
return(bug_bait_grey);
}
diff --git a/tools/makefile b/tools/makefile
index 8837952..97c345c 100644
--- a/tools/makefile
+++ b/tools/makefile
@@ -14,6 +14,9 @@ progs = pido hi-q skrewt hi-test mail-scan greylist
all: $(progs)
+greylist: greylist.c
+ $(CC) $< -lboost_filesystem-mt -o $@
+
mail-scan: mail-scan.o
$(CC) $< -lboost_regex -o $@