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