#include "str.h" #include "scan.h" #include "env.h" #include "exit.h" #include "sgetopt.h" #include "strerr.h" #include "stralloc.h" #include "pathexec.h" #include "dns.h" #define FATAL "rblqmtpd: fatal: " void nomem(void) { strerr_die2x(111,FATAL,"out of memory"); } void usage(void) { strerr_die1x(100,"rblqmtpd: usage: rblqmtpd [ -r base ] [ -a base ] qmtpd [ arg ... ]"); } char *ip_env; static stralloc ip_reverse; void ip_init(void) { unsigned int i; unsigned int j; ip_env = env_get("TCPREMOTEIP"); if (!ip_env) ip_env = ""; if (!stralloc_copys(&ip_reverse,"")) nomem(); i = str_len(ip_env); while (i) { for (j = i;j > 0;--j) if (ip_env[j - 1] == '.') break; if (!stralloc_catb(&ip_reverse,ip_env + j,i - j)) nomem(); if (!stralloc_cats(&ip_reverse,".")) nomem(); if (!j) break; i = j - 1; } } int decision = 0; /* 0 undecided, 1 accept, 2 reject */ static stralloc text; /* defined if decision is 2 */ static stralloc tmp; void rbl(char *base) { if (decision) return; if (!stralloc_copy(&tmp,&ip_reverse)) nomem(); if (!stralloc_cats(&tmp,base)) nomem(); if (dns_txt(&text,&tmp) == -1) { decision = 2; return; } if (text.len) decision = 2; } void antirbl(char *base) { if (decision) return; if (!stralloc_copy(&tmp,&ip_reverse)) nomem(); if (!stralloc_cats(&tmp,base)) nomem(); if (dns_ip4(&text,&tmp) == -1) { decision = 1; return; } if (text.len) decision = 1; } void rblqmtpd(void) { _exit(0); /* drastic times call for drastic measures */ /* optionally, you could strerr_die here */ } main(int argc,char **argv,char **envp) { int flagwantdefaultrbl = 1; char *x; int opt; ip_init(); x = env_get("RBLQMTPD"); if (x) { if (!*x) decision = 1; else if (*x == '-') decision = 3; else decision = 2; } while ((opt = getopt(argc,argv,"r:a:")) != opteof) switch(opt) { case 'r': rbl(optarg); flagwantdefaultrbl = 0; break; case 'a': antirbl(optarg); break; default: usage(); } argv += optind; if (!*argv) usage(); if (flagwantdefaultrbl) rbl("rbl.maps.vix.com"); if (decision >= 2) rblqmtpd(); pathexec_run(*argv,argv,envp); strerr_die4sys(111,FATAL,"unable to run ",*argv,": "); }