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