summaryrefslogtreecommitdiff
path: root/tools/greylist.c
diff options
context:
space:
mode:
authorJohn Denker <jsd@av8n.com>2012-07-20 00:24:43 (GMT)
committerJohn Denker <jsd@av8n.com>2012-07-20 00:24:43 (GMT)
commitd4ca9eda839b2c9d3bbbf8b48617977f483001b2 (patch)
tree0b8a5c2a93c48ee52a72a3c3d9d69fb563280059 /tools/greylist.c
parent82616449ee0c9f704e8c123d8b485c65fe428179 (diff)
bare beginnings of a greylisting system
Diffstat (limited to 'tools/greylist.c')
-rw-r--r--tools/greylist.c89
1 files changed, 88 insertions, 1 deletions
diff --git a/tools/greylist.c b/tools/greylist.c
index fa7d701..8adac05 100644
--- a/tools/greylist.c
+++ b/tools/greylist.c
@@ -1,5 +1,92 @@
+#include <stdlib.h> /* for exit(), getenv() */
+#include <iostream>
+#include <string>
+#include <sys/types.h> /* for stat() */
+#include <sys/stat.h> /* for stat() */
+#include <unistd.h> /* for stat() */
+#include <stdio.h> /* for perror */
+#include <errno.h> /* for ENOENT */
+#include <fstream> /* for ofstream() */
+#include <fcntl.h> /* for creat() */
+using namespace std;
-int main(){
+const int sa_good = 0;
+const int bug_bait_grey = 1;
+// qmail_queue and spamc have similar interpretations here:
+const int sa_syserr = 71;
+
+pid_t mypid;
+string progname;
+
+void dump(const string var){
+ char* str = getenv(var.c_str());
+ cerr << progname
+ << "[" << mypid << "] "
+ << var;
+ if (str) cerr << " is set to '" << str << "'" << endl;
+ 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);
+
+int main(int argc, char** argv){
+ mypid = getpid();
+ progname = argv[0];
+// dump("TCPREMOTEIP");
+// dump("TCPREMOTEHOST");
+
+ char* ipvar = getenv("TCPREMOTEIP");
+ if (!ipvar) {
+ cerr << progname << ": TCPREMOTEIP not set???" << endl;
+ exit(sa_syserr);
+ }
+ string ipbase = ipvar;
+
+// see if our directory exists:
+ struct stat dirstat;
+ int rslt = stat(dirname.c_str(), &dirstat);
+ if (rslt != 0){
+ if (errno != ENOENT) {
+ cerr << progname << ": stat failed for '"
+ << dirname << "' : ";
+ perror(0);
+ }
+ rslt = mkdir(dirname.c_str(), 0755);
+ if (rslt != 0) {
+ cerr << progname
+ << "uid " << getuid()
+ << ": mkdir failed for '"
+ << dirname << "' : ";
+ perror(0);
+ exit(sa_syserr);
+ }
+ }
+
+ string ipname = dirname + "/" + ipbase;
+ struct stat ipstat;
+ rslt = stat(ipname.c_str(), &ipstat);
+ if (rslt != 0){
+ if (errno != ENOENT) {
+ cerr << progname << ": stat failed for '"
+ << ipname << "' : ";
+ perror(0);
+ }
+ ofstream foo;
+ int fd = creat(ipname.c_str(), 0644);
+ if (fd < 0){
+ cerr << progname << ": create failed for '"
+ << ipname << "' : ";
+ perror(0);
+ }
+ close(fd);
+ return(bug_bait_grey);
+ } else {
+ cerr << "file exists: " << ipname << endl;
+ }
return 0;
}