diff options
author | Jannis Hoffmann <jannis@fehcom.de> | 2024-07-03 15:48:04 +0200 |
---|---|---|
committer | Jannis Hoffmann <jannis@fehcom.de> | 2024-07-03 15:48:04 +0200 |
commit | 89b7b67a13ebb7965cc7f13ad0595e2194a2d34c (patch) | |
tree | 25efd77a90ae87236e6730d8ea3846bbe0fd126f /src/qmail-postgrey.c |
add sqmail-4.2.29asqmail-4.2
Diffstat (limited to 'src/qmail-postgrey.c')
-rw-r--r-- | src/qmail-postgrey.c | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/src/qmail-postgrey.c b/src/qmail-postgrey.c new file mode 100644 index 0000000..bd88389 --- /dev/null +++ b/src/qmail-postgrey.c @@ -0,0 +1,105 @@ +#include <unistd.h> +#include <sys/socket.h> +#include <netinet/in.h> +#include "stralloc.h" +#include "logmsg.h" +#include "ip.h" +#include "case.h" +#include "str.h" +#include "exit.h" +#include "scan.h" +#include "timeout.h" +#include "timeoutconn.h" +#include "socket_if.h" + +#define WHO "qmail-postgrey" + +#define CT 10 /* Connect timeout */ +#define WT 10 /* Write timeout */ +#define RT 10 /* Read timeout */ + +unsigned int port = 60000; /* default port */ + +int main(int argc, char **argv) +{ + struct ip4_address ip4; + struct ip6_address ip6; + stralloc query = {0}; + char buf[64]; + char *remoteip = 0; + char *netif = 0; + uint32 ifidx = 0; + int pgfd; + int i, j, r; + + if (argc != 6) + logmsg(WHO,100,USAGE,"qmail-postgrey ip%ifidx;port sender recipient client_address client_name"); + + remoteip = argv[1]; + i = str_chr(remoteip,':'); + if (remoteip[i] == ':') { + j = str_chr(remoteip,'%'); /* IF index */ + if (remoteip[j] == '%') { + remoteip[j] = 0; + netif = &remoteip[j + 1]; + ifidx = socket_getifidx(netif); + } + if (!ip6_scan(remoteip,(char *)&ip6.d)) + logmsg(WHO,111,FATAL,B("No valid IPv6 address provided: ",remoteip)); + pgfd = socket(AF_INET6,SOCK_STREAM,0); + if (pgfd == -1) + logmsg(WHO,111,FATAL,"Can't bind to IPv6 socket."); + r = timeoutconn6(pgfd,(char *)&ip6.d,port,CT,ifidx); + } else { + if (!ip4_scan(remoteip,(char *)&ip4.d)) + logmsg(WHO,111,FATAL,B("No valid IPv6 address provided: ",remoteip)); + pgfd = socket(AF_INET,SOCK_STREAM,0); + if (pgfd == -1) + logmsg(WHO,111,FATAL,"Can't bind to IPv4 socket."); + r = timeoutconn4(pgfd,(char *)&ip4.d,port,CT); + } + if (r != 0) { + if (errno == ETIMEDOUT) + close(pgfd); + logmsg(WHO,111,FATAL,B("Can't communicate with postgrey server: ",remoteip)); + _exit(1); + } + + /* Provide SMTP connect vector to postgrey server */ + + if (!stralloc_copys(&query,"request=smtpd_access_policy\nclient_address=")) _exit(1); + if (!stralloc_cats(&query,argv[4])) _exit(1); + if (!stralloc_cats(&query,"\nclient_name=")) _exit(1); + if (!stralloc_cats(&query,argv[5])) _exit(1); + if (!stralloc_cats(&query,"\nsender=")) _exit(1); + if (!stralloc_cats(&query,argv[2])) _exit(1); + if (!stralloc_cats(&query,"\nrecipient=")) _exit(1); + if (!stralloc_cats(&query,argv[3])) _exit(1); + if (!stralloc_cats(&query,"\n\n")) _exit(1); + + do { + r = timeoutwrite(WT,pgfd,query.s,query.len); + } while (r == -1 && errno == EINTR); + + if (r != query.len) { close(pgfd); _exit(1); } + + /* Read response */ + + do { + r = timeoutread(RT,pgfd,buf,sizeof(buf)); + } while (r == -1 && errno == EINTR); + + if (r == -1) { close(pgfd); _exit(1); } + close(pgfd); + +// logmsg(WHO,0,INFO,buf); + + if (r >= 12) + if (!case_diffb(buf,12,"action=dunno")) _exit(0); + if (r >= 14) + if (!case_diffb(buf,14,"action=prepend")) _exit(0); + if (r >= 22) + if (!case_diffb(buf,22,"action=defer_if_permit")) _exit(10); + + exit(1); +} |