summaryrefslogtreecommitdiff
path: root/src/qmail-postgrey.c
blob: 942dee640180584a77997c1bb99906e61a9c1f88 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <netinet/in.h>
#include <sys/socket.h>
#include <unistd.h>

#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);
}