summaryrefslogtreecommitdiff
path: root/src/qmail-qmqpd.c
blob: 86404a902252874672b17261ef76fe6daa0ede10 (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include <unistd.h>
#include "auto_qmail.h"
#include "qmail.h"
#include "received.h"
#include "sig.h"
#include "buffer.h"
#include "exit.h"
#include "now.h"
#include "fmt.h"
#include "env.h"
#include "case.h"
#include "byte.h"
#include "ip.h"
#include "str.h"

#define PORT_QMQP "628"

void resources() { _exit(111); }

ssize_t safewrite(int fd,char *buf,int len)
{
  int r;
  r = write(fd,buf,len);
  if (r <= 0) _exit(0);
  return r;
}

ssize_t saferead(int fd,char *buf,int len)
{
  int r;
  r = read(fd,buf,len);
  if (r <= 0) _exit(0);
  return r;
}

char inbuf[512];
buffer bi = BUFFER_INIT(saferead,0,inbuf,sizeof(inbuf));
char outbuf[256];
buffer bo = BUFFER_INIT(safewrite,1,outbuf,sizeof(outbuf));

unsigned long bytesleft = 100;

void getbyte(char *ch)
{
  if (!bytesleft--) _exit(100);
  buffer_get(&bi,ch,1);
}

unsigned long getlen()
{
  unsigned long len = 0;
  char ch;

  for (;;) {
    getbyte(&ch);
    if (ch == ':') return len;
    if (len > 200000000) resources();
    len = 10 * len + (ch - '0');
  }
}

void getcomma()
{
  char ch;
  getbyte(&ch);
  if (ch != ',') _exit(100);
}

struct qmail qq;

void identify()
{
  char *remotehost;
  char *remoteinfo;
  char *remoteip;
  char *local;
  char *localport;

  remotehost = env_get("TCP6REMOTEHOST");
  if (!remotehost) remotehost = env_get("TCPREMOTEHOST");
  if (!remotehost) remotehost = "unknown";
  remoteinfo = env_get("TCP6REMOTEINFO");
  if (!remoteinfo) remoteinfo = env_get("TCPREMOTEINFO");
  remoteip = env_get("TCP6REMOTEIP");
  if (remoteip && byte_equal(remoteip,7,V4MAPPREFIX)) remoteip=remoteip+7;
  if (!remoteip) remoteip = env_get("TCPREMOTEIP");
  if (!remoteip) remoteip = "unknown";
  local = env_get("TCP6LOCALHOST");
  if (!local) local = env_get("TCPLOCALHOST");
  if (!local) local = env_get("TCP6LOCALIP");
  if (!local) local = env_get("TCPLOCALIP");
  if (!local) local = "unknown";
  localport = env_get("TCP6LOCALPORT");
  if (!localport) localport  = env_get("TCPLOCALPORT");
  if (!localport) localport = "0";

  received(&qq,"QMQP",local,remoteip,remotehost,remoteinfo,(char *) 0,(char *) 0,(char *) 0);
}

char buf[1000];
char strnum[FMT_ULONG];

int getbuf()
{
  unsigned long len;
  int i;

  len = getlen();
  if (len >= 1000) {
    for (i = 0; i < len; ++i) getbyte(buf);
    getcomma();
    buf[0] = 0;
    return 0;
  }

  for (i = 0; i < len; ++i) getbyte(buf + i);
  getcomma();
  buf[len] = 0;
  return byte_chr(buf,len,'\0') == len;
}

int flagok = 1;

int main()
{
  char *result;
  unsigned long qp;
  unsigned long len;
  char ch;

  sig_pipeignore();
  sig_alarmcatch(resources);
  alarm(3600);

  bytesleft = getlen();

  len = getlen();

  if (chdir(auto_qmail) == -1) resources();
  if (qmail_open(&qq) == -1) resources();
  qp = qmail_qp(&qq);
  identify();

  while (len > 0) { /* XXX: could speed this up */
    getbyte(&ch);
    --len;
    qmail_put(&qq,&ch,1);
  }
  getcomma();

  if (getbuf())
    qmail_from(&qq,buf);
  else {
    qmail_from(&qq,"");
    qmail_fail(&qq);
    flagok = 0;
  }

  while (bytesleft)
    if (getbuf())
      qmail_to(&qq,buf);
    else {
      qmail_fail(&qq);
      flagok = 0;
    }

  bytesleft = 1;
  getcomma();

  result = qmail_close(&qq);

  if (!*result) {
    len = fmt_str(buf,"Kok ");
    len += fmt_ulong(buf + len,(unsigned long) now());
    len += fmt_str(buf + len," qp ");
    len += fmt_ulong(buf + len,qp);
    buf[len] = 0;
    result = buf;
  }

  if (!flagok)
    result = "Dsorry, I can't accept addresses like that (#5.1.3)";

  buffer_put(&bo,strnum,fmt_ulong(strnum,(unsigned long) str_len(result)));
  buffer_puts(&bo,":");
  buffer_puts(&bo,result);
  buffer_puts(&bo,",");
  buffer_flush(&bo);
  _exit(0);
}