s/qmail 4.2.29a
Next generation secure email transport
Loading...
Searching...
No Matches
qmail-postgrey.c
Go to the documentation of this file.
1#include <unistd.h>
2#include <sys/socket.h>
3#include <netinet/in.h>
4#include "stralloc.h"
5#include "logmsg.h"
6#include "ip.h"
7#include "case.h"
8#include "str.h"
9#include "exit.h"
10#include "scan.h"
11#include "timeout.h"
12#include "timeoutconn.h"
13#include "socket_if.h"
14
15#define WHO "qmail-postgrey"
16
17#define CT 10 /* Connect timeout */
18#define WT 10 /* Write timeout */
19#define RT 10 /* Read timeout */
20
21unsigned int port = 60000; /* default port */
22
23int main(int argc, char **argv)
24{
25 struct ip4_address ip4;
26 struct ip6_address ip6;
27 stralloc query = {0};
28 char buf[64];
29 char *remoteip = 0;
30 char *netif = 0;
31 uint32 ifidx = 0;
32 int pgfd;
33 int i, j, r;
34
35 if (argc != 6)
36 logmsg(WHO,100,USAGE,"qmail-postgrey ip%ifidx;port sender recipient client_address client_name");
37
38 remoteip = argv[1];
39 i = str_chr(remoteip,':');
40 if (remoteip[i] == ':') {
41 j = str_chr(remoteip,'%'); /* IF index */
42 if (remoteip[j] == '%') {
43 remoteip[j] = 0;
44 netif = &remoteip[j + 1];
45 ifidx = socket_getifidx(netif);
46 }
47 if (!ip6_scan(remoteip,(char *)&ip6.d))
48 logmsg(WHO,111,FATAL,B("No valid IPv6 address provided: ",remoteip));
49 pgfd = socket(AF_INET6,SOCK_STREAM,0);
50 if (pgfd == -1)
51 logmsg(WHO,111,FATAL,"Can't bind to IPv6 socket.");
52 r = timeoutconn6(pgfd,(char *)&ip6.d,port,CT,ifidx);
53 } else {
54 if (!ip4_scan(remoteip,(char *)&ip4.d))
55 logmsg(WHO,111,FATAL,B("No valid IPv6 address provided: ",remoteip));
56 pgfd = socket(AF_INET,SOCK_STREAM,0);
57 if (pgfd == -1)
58 logmsg(WHO,111,FATAL,"Can't bind to IPv4 socket.");
59 r = timeoutconn4(pgfd,(char *)&ip4.d,port,CT);
60 }
61 if (r != 0) {
62 if (errno == ETIMEDOUT)
63 close(pgfd);
64 logmsg(WHO,111,FATAL,B("Can't communicate with postgrey server: ",remoteip));
65 _exit(1);
66 }
67
68 /* Provide SMTP connect vector to postgrey server */
69
70 if (!stralloc_copys(&query,"request=smtpd_access_policy\nclient_address=")) _exit(1);
71 if (!stralloc_cats(&query,argv[4])) _exit(1);
72 if (!stralloc_cats(&query,"\nclient_name=")) _exit(1);
73 if (!stralloc_cats(&query,argv[5])) _exit(1);
74 if (!stralloc_cats(&query,"\nsender=")) _exit(1);
75 if (!stralloc_cats(&query,argv[2])) _exit(1);
76 if (!stralloc_cats(&query,"\nrecipient=")) _exit(1);
77 if (!stralloc_cats(&query,argv[3])) _exit(1);
78 if (!stralloc_cats(&query,"\n\n")) _exit(1);
79
80 do {
81 r = timeoutwrite(WT,pgfd,query.s,query.len);
82 } while (r == -1 && errno == EINTR);
83
84 if (r != query.len) { close(pgfd); _exit(1); }
85
86 /* Read response */
87
88 do {
89 r = timeoutread(RT,pgfd,buf,sizeof(buf));
90 } while (r == -1 && errno == EINTR);
91
92 if (r == -1) { close(pgfd); _exit(1); }
93 close(pgfd);
94
95// logmsg(WHO,0,INFO,buf);
96
97 if (r >= 12)
98 if (!case_diffb(buf,12,"action=dunno")) _exit(0);
99 if (r >= 14)
100 if (!case_diffb(buf,14,"action=prepend")) _exit(0);
101 if (r >= 22)
102 if (!case_diffb(buf,22,"action=defer_if_permit")) _exit(10);
103
104 exit(1);
105}
int main()
Definition: chkshsgr.c:6
int stralloc_copys(stralloc *, char const *)
char ip6[16]
Definition: dnsptr.c:15
char ip4[4]
Definition: dnsptr.c:14
void _exit()
char buf[100+FMT_ULONG]
Definition: hier.c:10
void exit(int fail)
Supported storage methods: (1) authuser:[=]plainpasswd, (2) authuser:hashpasswd, (3) authuser:?...
char * remoteip
Definition: qmail-popup.c:71
#define WT
#define CT
unsigned int port
#define RT
#define WHO
stralloc query
Definition: qmail-qmaint.c:35
uint32 ifidx
Definition: qmail-remote.c:90
int j
Definition: qmail-send.c:920