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