Merge branch 'mdw/relayhosts'
[qmail] / qmail-smtpd.c
1 #include "sig.h"
2 #include "readwrite.h"
3 #include "stralloc.h"
4 #include "substdio.h"
5 #include "alloc.h"
6 #include "auto_qmail.h"
7 #include "control.h"
8 #include "received.h"
9 #include "constmap.h"
10 #include "error.h"
11 #include "ipme.h"
12 #include "ip.h"
13 #include "qmail.h"
14 #include "str.h"
15 #include "fmt.h"
16 #include "scan.h"
17 #include "byte.h"
18 #include "case.h"
19 #include "env.h"
20 #include "now.h"
21 #include "exit.h"
22 #include "rcpthosts.h"
23 #include "timeoutread.h"
24 #include "timeoutwrite.h"
25 #include "commands.h"
26
27 #define MAXHOPS 100
28 unsigned int databytes = 0;
29 int timeout = 1200;
30
31 int safewrite(fd,buf,len) int fd; char *buf; int len;
32 {
33 int r;
34 r = timeoutwrite(timeout,fd,buf,len);
35 if (r <= 0) _exit(1);
36 return r;
37 }
38
39 char ssoutbuf[512];
40 substdio ssout = SUBSTDIO_FDBUF(safewrite,1,ssoutbuf,sizeof ssoutbuf);
41
42 void flush() { substdio_flush(&ssout); }
43 void out(s) char *s; { substdio_puts(&ssout,s); }
44
45 void die_read() { _exit(1); }
46 void die_alarm() { out("451 timeout (#4.4.2)\r\n"); flush(); _exit(1); }
47 void die_nomem() { out("421 out of memory (#4.3.0)\r\n"); flush(); _exit(1); }
48 void die_control() { out("421 unable to read controls (#4.3.0)\r\n"); flush(); _exit(1); }
49 void die_ipme() { out("421 unable to figure out my IP addresses (#4.3.0)\r\n"); flush(); _exit(1); }
50 void straynewline() { out("451 See http://pobox.com/~djb/docs/smtplf.html.\r\n"); flush(); _exit(1); }
51
52 void err_bmf() { out("553 sorry, your envelope sender is in my badmailfrom list (#5.7.1)\r\n"); }
53 void err_nogateway() { out("553 sorry, that domain isn't in my list of allowed rcpthosts (#5.7.1)\r\n"); }
54 void err_unimpl() { out("502 unimplemented (#5.5.1)\r\n"); }
55 void err_syntax() { out("555 syntax error (#5.5.4)\r\n"); }
56 void err_wantmail() { out("503 MAIL first (#5.5.1)\r\n"); }
57 void err_wantrcpt() { out("503 RCPT first (#5.5.1)\r\n"); }
58 void err_noop() { out("250 ok\r\n"); }
59 void err_vrfy() { out("252 send some mail, i'll try my best\r\n"); }
60 void err_qqt() { out("451 qqt failure (#4.3.0)\r\n"); }
61
62
63 stralloc greeting = {0};
64
65 void smtp_greet(code) char *code;
66 {
67 substdio_puts(&ssout,code);
68 substdio_put(&ssout,greeting.s,greeting.len);
69 }
70 void smtp_help()
71 {
72 out("214 qmail home page: http://pobox.com/~djb/qmail.html\r\n");
73 }
74 void smtp_quit()
75 {
76 smtp_greet("221 "); out("\r\n"); flush(); _exit(0);
77 }
78
79 char *remoteip;
80 char *remotehost;
81 char *remoteinfo;
82 char *local;
83 char *relayclient;
84
85 stralloc helohost = {0};
86 char *fakehelo; /* pointer into helohost, or 0 */
87
88 void dohelo(arg) char *arg; {
89 if (!stralloc_copys(&helohost,arg)) die_nomem();
90 if (!stralloc_0(&helohost)) die_nomem();
91 fakehelo = case_diffs(remotehost,helohost.s) ? helohost.s : 0;
92 }
93
94 int liphostok = 0;
95 stralloc liphost = {0};
96 int relayhostsok = 0;
97 stralloc relayhosts = {0};
98 struct constmap maprelayhosts;
99 int bmfok = 0;
100 stralloc bmf = {0};
101 struct constmap mapbmf;
102
103 void setup()
104 {
105 char *x;
106 unsigned long u;
107
108 if (control_init() == -1) die_control();
109 if (control_rldef(&greeting,"control/smtpgreeting",1,(char *) 0) != 1)
110 die_control();
111 liphostok = control_rldef(&liphost,"control/localiphost",1,(char *) 0);
112 if (liphostok == -1) die_control();
113 if (control_readint(&timeout,"control/timeoutsmtpd") == -1) die_control();
114 if (timeout <= 0) timeout = 1;
115
116 if (rcpthosts_init() == -1) die_control();
117
118 bmfok = control_readfile(&bmf,"control/badmailfrom",0);
119 if (bmfok == -1) die_control();
120 if (bmfok)
121 if (!constmap_init(&mapbmf,bmf.s,bmf.len,0)) die_nomem();
122
123 switch (control_readfile(&relayhosts, "control/relayhosts", 0)) {
124 case -1:
125 die_control();
126 case 1:
127 relayhostsok = 1;
128 if (!constmap_init(&maprelayhosts, relayhosts.s, relayhosts.len, 1))
129 die_nomem();
130 }
131
132
133 if (control_readint(&databytes,"control/databytes") == -1) die_control();
134 x = env_get("DATABYTES");
135 if (x) { scan_ulong(x,&u); databytes = u; }
136 if (!(databytes + 1)) --databytes;
137
138 remoteip = env_get("TCPREMOTEIP");
139 if (!remoteip) remoteip = "unknown";
140 local = env_get("TCPLOCALHOST");
141 if (!local) local = env_get("TCPLOCALIP");
142 if (!local) local = "unknown";
143 remotehost = env_get("TCPREMOTEHOST");
144 if (!remotehost) remotehost = "unknown";
145 remoteinfo = env_get("TCPREMOTEINFO");
146 relayclient = env_get("RELAYCLIENT");
147 if (!relayclient && relayhostsok) {
148 int j;
149 int l = str_len(remotehost);
150 relayclient = constmap(&maprelayhosts, remotehost, l);
151 if (!relayclient) for (j = 0; j < l; ++j) {
152 if (remotehost[j] == '.' &&
153 (relayclient = constmap(&maprelayhosts,
154 remotehost + j,
155 l - j)) != 0)
156 break;
157 }
158 }
159 dohelo(remotehost);
160 }
161
162
163 stralloc addr = {0}; /* will be 0-terminated, if addrparse returns 1 */
164
165 int addrparse(arg)
166 char *arg;
167 {
168 int i;
169 char ch;
170 char terminator;
171 struct ip_address ip;
172 int flagesc;
173 int flagquoted;
174
175 terminator = '>';
176 i = str_chr(arg,'<');
177 if (arg[i])
178 arg += i + 1;
179 else { /* partner should go read rfc 821 */
180 terminator = ' ';
181 arg += str_chr(arg,':');
182 if (*arg == ':') ++arg;
183 while (*arg == ' ') ++arg;
184 }
185
186 /* strip source route */
187 if (*arg == '@') while (*arg) if (*arg++ == ':') break;
188
189 if (!stralloc_copys(&addr,"")) die_nomem();
190 flagesc = 0;
191 flagquoted = 0;
192 for (i = 0;ch = arg[i];++i) { /* copy arg to addr, stripping quotes */
193 if (flagesc) {
194 if (!stralloc_append(&addr,&ch)) die_nomem();
195 flagesc = 0;
196 }
197 else {
198 if (!flagquoted && (ch == terminator)) break;
199 switch(ch) {
200 case '\\': flagesc = 1; break;
201 case '"': flagquoted = !flagquoted; break;
202 default: if (!stralloc_append(&addr,&ch)) die_nomem();
203 }
204 }
205 }
206 /* could check for termination failure here, but why bother? */
207 if (!stralloc_append(&addr,"")) die_nomem();
208
209 if (liphostok) {
210 i = byte_rchr(addr.s,addr.len,'@');
211 if (i < addr.len) /* if not, partner should go read rfc 821 */
212 if (addr.s[i + 1] == '[')
213 if (!addr.s[i + 1 + ip_scanbracket(addr.s + i + 1,&ip)])
214 if (ipme_is(&ip)) {
215 addr.len = i + 1;
216 if (!stralloc_cat(&addr,&liphost)) die_nomem();
217 if (!stralloc_0(&addr)) die_nomem();
218 }
219 }
220
221 if (addr.len > 900) return 0;
222 return 1;
223 }
224
225 int bmfcheck()
226 {
227 int j;
228 if (!bmfok) return 0;
229 if (constmap(&mapbmf,addr.s,addr.len - 1)) return 1;
230 j = byte_rchr(addr.s,addr.len,'@');
231 if (j < addr.len)
232 if (constmap(&mapbmf,addr.s + j,addr.len - j - 1)) return 1;
233 return 0;
234 }
235
236 int addrallowed()
237 {
238 int r;
239 r = rcpthosts(addr.s,str_len(addr.s));
240 if (r == -1) die_control();
241 return r;
242 }
243
244
245 int seenmail = 0;
246 int flagbarf; /* defined if seenmail */
247 stralloc mailfrom = {0};
248 stralloc rcptto = {0};
249
250 void smtp_helo(arg) char *arg;
251 {
252 smtp_greet("250 "); out("\r\n");
253 seenmail = 0; dohelo(arg);
254 }
255 void smtp_ehlo(arg) char *arg;
256 {
257 smtp_greet("250-"); out("\r\n250-PIPELINING\r\n250 8BITMIME\r\n");
258 seenmail = 0; dohelo(arg);
259 }
260 void smtp_rset()
261 {
262 seenmail = 0;
263 out("250 flushed\r\n");
264 }
265 void smtp_mail(arg) char *arg;
266 {
267 if (!addrparse(arg)) { err_syntax(); return; }
268 flagbarf = bmfcheck();
269 seenmail = 1;
270 if (!stralloc_copys(&rcptto,"")) die_nomem();
271 if (!stralloc_copys(&mailfrom,addr.s)) die_nomem();
272 if (!stralloc_0(&mailfrom)) die_nomem();
273 out("250 ok\r\n");
274 }
275 void smtp_rcpt(arg) char *arg; {
276 if (!seenmail) { err_wantmail(); return; }
277 if (!addrparse(arg)) { err_syntax(); return; }
278 if (flagbarf) { err_bmf(); return; }
279 if (relayclient) {
280 --addr.len;
281 if (!stralloc_cats(&addr,relayclient)) die_nomem();
282 if (!stralloc_0(&addr)) die_nomem();
283 }
284 else
285 if (!addrallowed()) { err_nogateway(); return; }
286 if (!stralloc_cats(&rcptto,"T")) die_nomem();
287 if (!stralloc_cats(&rcptto,addr.s)) die_nomem();
288 if (!stralloc_0(&rcptto)) die_nomem();
289 out("250 ok\r\n");
290 }
291
292
293 int saferead(fd,buf,len) int fd; char *buf; int len;
294 {
295 int r;
296 flush();
297 r = timeoutread(timeout,fd,buf,len);
298 if (r == -1) if (errno == error_timeout) die_alarm();
299 if (r <= 0) die_read();
300 return r;
301 }
302
303 char ssinbuf[1024];
304 substdio ssin = SUBSTDIO_FDBUF(saferead,0,ssinbuf,sizeof ssinbuf);
305
306 struct qmail qqt;
307 unsigned int bytestooverflow = 0;
308
309 void put(ch)
310 char *ch;
311 {
312 if (bytestooverflow)
313 if (!--bytestooverflow)
314 qmail_fail(&qqt);
315 qmail_put(&qqt,ch,1);
316 }
317
318 void blast(hops)
319 int *hops;
320 {
321 char ch;
322 int state;
323 int flaginheader;
324 int pos; /* number of bytes since most recent \n, if fih */
325 int flagmaybex; /* 1 if this line might match RECEIVED, if fih */
326 int flagmaybey; /* 1 if this line might match \r\n, if fih */
327 int flagmaybez; /* 1 if this line might match DELIVERED, if fih */
328
329 state = 1;
330 *hops = 0;
331 flaginheader = 1;
332 pos = 0; flagmaybex = flagmaybey = flagmaybez = 1;
333 for (;;) {
334 substdio_get(&ssin,&ch,1);
335 if (flaginheader) {
336 if (pos < 9) {
337 if (ch != "delivered"[pos]) if (ch != "DELIVERED"[pos]) flagmaybez = 0;
338 if (flagmaybez) if (pos == 8) ++*hops;
339 if (pos < 8)
340 if (ch != "received"[pos]) if (ch != "RECEIVED"[pos]) flagmaybex = 0;
341 if (flagmaybex) if (pos == 7) ++*hops;
342 if (pos < 2) if (ch != "\r\n"[pos]) flagmaybey = 0;
343 if (flagmaybey) if (pos == 1) flaginheader = 0;
344 }
345 ++pos;
346 if (ch == '\n') { pos = 0; flagmaybex = flagmaybey = flagmaybez = 1; }
347 }
348 switch(state) {
349 case 0:
350 if (ch == '\n') straynewline();
351 if (ch == '\r') { state = 4; continue; }
352 break;
353 case 1: /* \r\n */
354 if (ch == '\n') straynewline();
355 if (ch == '.') { state = 2; continue; }
356 if (ch == '\r') { state = 4; continue; }
357 state = 0;
358 break;
359 case 2: /* \r\n + . */
360 if (ch == '\n') straynewline();
361 if (ch == '\r') { state = 3; continue; }
362 state = 0;
363 break;
364 case 3: /* \r\n + .\r */
365 if (ch == '\n') return;
366 put(".");
367 put("\r");
368 if (ch == '\r') { state = 4; continue; }
369 state = 0;
370 break;
371 case 4: /* + \r */
372 if (ch == '\n') { state = 1; break; }
373 if (ch != '\r') { put("\r"); state = 0; }
374 }
375 put(&ch);
376 }
377 }
378
379 char accept_buf[FMT_ULONG];
380 void acceptmessage(qp) unsigned long qp;
381 {
382 datetime_sec when;
383 when = now();
384 out("250 ok ");
385 accept_buf[fmt_ulong(accept_buf,(unsigned long) when)] = 0;
386 out(accept_buf);
387 out(" qp ");
388 accept_buf[fmt_ulong(accept_buf,qp)] = 0;
389 out(accept_buf);
390 out("\r\n");
391 }
392
393 void smtp_data() {
394 int hops;
395 unsigned long qp;
396 char *qqx;
397
398 if (!seenmail) { err_wantmail(); return; }
399 if (!rcptto.len) { err_wantrcpt(); return; }
400 seenmail = 0;
401 if (databytes) bytestooverflow = databytes + 1;
402 if (qmail_open(&qqt) == -1) { err_qqt(); return; }
403 qp = qmail_qp(&qqt);
404 out("354 go ahead\r\n");
405
406 received(&qqt,"SMTP",local,remoteip,remotehost,remoteinfo,fakehelo);
407 blast(&hops);
408 hops = (hops >= MAXHOPS);
409 if (hops) qmail_fail(&qqt);
410 qmail_from(&qqt,mailfrom.s);
411 qmail_put(&qqt,rcptto.s,rcptto.len);
412
413 qqx = qmail_close(&qqt);
414 if (!*qqx) { acceptmessage(qp); return; }
415 if (hops) { out("554 too many hops, this message is looping (#5.4.6)\r\n"); return; }
416 if (databytes) if (!bytestooverflow) { out("552 sorry, that message size exceeds my databytes limit (#5.3.4)\r\n"); return; }
417 if (*qqx == 'D') out("554 "); else out("451 ");
418 out(qqx + 1);
419 out("\r\n");
420 }
421
422 struct commands smtpcommands[] = {
423 { "rcpt", smtp_rcpt, 0 }
424 , { "mail", smtp_mail, 0 }
425 , { "data", smtp_data, flush }
426 , { "quit", smtp_quit, flush }
427 , { "helo", smtp_helo, flush }
428 , { "ehlo", smtp_ehlo, flush }
429 , { "rset", smtp_rset, 0 }
430 , { "help", smtp_help, flush }
431 , { "noop", err_noop, flush }
432 , { "vrfy", err_vrfy, flush }
433 , { 0, err_unimpl, flush }
434 } ;
435
436 void main()
437 {
438 sig_pipeignore();
439 if (chdir(auto_qmail) == -1) die_control();
440 setup();
441 if (ipme_init() != 1) die_ipme();
442 smtp_greet("220 ");
443 out(" ESMTP\r\n");
444 if (commands(&ssin,&smtpcommands) == 0) die_read();
445 die_nomem();
446 }