#include #include #include #include "case.h" #include "exit.h" #include "ip.h" #include "logmsg.h" #include "scan.h" #include "socket_if.h" #include "str.h" #include "stralloc.h" #include "timeout.h" #include "timeoutconn.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); }