7 #include "auto_qmail.h"
27 substdio ssout = SUBSTDIO_FDBUF(write,1,ssoutbuf,sizeof(ssoutbuf));
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(); }
33 int timeoutread(fd,buf,n) int fd; char *buf; int n;
38 r = read(fd,buf,n); saveerrno = errno;
40 errno = saveerrno; return r;
44 substdio ssin = SUBSTDIO_FDBUF(timeoutread,0,ssinbuf,sizeof(ssinbuf));
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(); }
51 stralloc greeting = {0};
53 stralloc liphost = {0};
55 stralloc rcpthosts = {0};
56 struct constmap maprcpthosts;
60 stralloc relayhosts = {0};
61 struct constmap maprelayhosts;
62 struct constmap mapbmf;
63 int flagbarf; /* defined if seenmail */
65 stralloc helohost = {0};
66 stralloc mailfrom = {0};
67 stralloc rcptto = {0};
70 stralloc addr = {0}; /* will be 0-terminated, if addrparse returns 1 */
78 void dohelo(arg) char *arg;
80 if (!stralloc_copys(&helohost,arg)) outofmem();
81 if (!stralloc_0(&helohost)) outofmem();
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");
95 if (!relayclient && relayhostsok) {
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,
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.\
121 void blast(ssfrom,hops)
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 */
136 pos = 0; flagmaybex = flagmaybey = flagmaybez = 1;
139 if (substdio_get(ssfrom,&ch,1) <= 0) die();
144 if (ch != "delivered"[pos]) if (ch != "DELIVERED"[pos]) flagmaybez = 0;
145 if (flagmaybez) if (pos == 8) ++*hops;
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;
153 if (ch == '\n') { pos = 0; flagmaybex = flagmaybey = flagmaybez = 1; }
158 if (ch == '\n') straynewline();
159 if (ch == '\r') { state = 4; continue; }
162 if (ch == '\n') straynewline();
163 if (ch == '.') { state = 2; continue; }
164 if (ch == '\r') { state = 4; continue; }
167 case 2: /* \r\n + . */
168 if (ch == '\n') straynewline();
169 if (ch == '\r') { state = 3; continue; }
172 case 3: /* \r\n + .\r */
173 if (ch == '\n') return;
174 qmail_put(&qqt,".\r",2);
175 if (ch == '\r') { state = 4; continue; }
179 if (ch == '\n') { state = 1; break; }
180 if (ch != '\r') { qmail_put(&qqt,"\r",1); state = 0; }
182 qmail_put(&qqt,&ch,1);
191 struct ip_address ip;
195 arg += str_chr(arg,'<');
196 if (*arg != '<') return 0;
199 /* strip source route */
200 if (*arg == '@') while (*arg) if (*arg++ == ':') break;
203 if (!stralloc_copys(&addr,"")) outofmem();
206 for (i = 0;ch = arg[i];++i) /* copy arg to addr, stripping quotes */
209 { if (!stralloc_append(&addr,&ch)) outofmem(); flagesc = 0; }
212 if (!flagquoted && (ch == '>')) break;
215 case '\\': flagesc = 1; break;
216 case '"': flagquoted = !flagquoted; break;
217 default: if (!stralloc_append(&addr,&ch)) outofmem();
222 if (!stralloc_append(&addr,"")) outofmem();
226 if (!case_diffs(arg + i," BODY=8BITMIME")) i += 14;
227 else if (!case_diffs(arg + i," BODY=7BIT")) i += 10;
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)])
240 if (!stralloc_cat(&addr,&liphost)) outofmem();
241 if (!stralloc_0(&addr)) outofmem();
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;
266 if (constmap(&mapbmf,addr.s,addr.len - 1)) { flagbarf = 1; return; }
267 j = byte_rchr(addr.s,addr.len,'@');
269 if (constmap(&mapbmf,addr.s + j,addr.len - j - 1)) flagbarf = 1;
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");
290 dohelo(arg ? arg : ""); }
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; }
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; }
311 if (!stralloc_cats(&addr,relayclient)) outofmem();
312 if (!stralloc_0(&addr)) outofmem();
315 if (!addrallowed()) { err_nogateway(); return; }
317 if (!stralloc_cats(&rcptto,"T")) outofmem();
318 if (!stralloc_cats(&rcptto,addr.s)) outofmem();
319 if (!stralloc_0(&rcptto)) outofmem(); }
321 char accept_buf[FMT_ULONG];
322 void acceptmessage(qp) unsigned long qp;
327 accept_buf[fmt_ulong(accept_buf,(unsigned long) when)] = 0;
330 accept_buf[fmt_ulong(accept_buf,qp)] = 0;
336 int hops; int r; unsigned long qp;
337 if (!seenmail) { err_wantmail(); return; }
338 if (!rcptto.len) { err_wantrcpt(); return; }
340 if (qmail_open(&qqt) == -1) { err_qqt(); return; }
342 out("354 go ahead\r\n");
344 received(&qqt,"SMTP",local,remoteip,remotehost,remoteinfo,case_diffs(remotehost,helohost.s) ? helohost.s : 0);
346 hops = (hops >= MAXHOPS);
347 if (hops) qmail_fail(&qqt);
348 qmail_from(&qqt,mailfrom.s);
349 qmail_put(&qqt,rcptto.s,rcptto.len);
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; }
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;
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 }
391 for (i = 0;smtpcmd[i].fun;++i)
393 for (j = 0;ch = smtpcmd[i].text[j];++j)
394 if ((cmd[j] != ch) && (cmd[j] != ch - 32))
397 if (!cmd[j] || (cmd[j] == ' '))
399 while (cmd[j] == ' ') ++j;
401 smtpcmd[i].fun((char *) 0);
403 smtpcmd[i].fun(cmd + j);
404 if (smtpcmd[i].flagflush) flush();
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))
425 if (!constmap_init(&maprcpthosts,rcpthosts.s,rcpthosts.len,0)) die();
427 switch(control_readfile(&bmf,"control/badmailfrom",0))
432 if (!constmap_init(&mapbmf,bmf.s,bmf.len,0)) die();
434 switch (control_readfile(&relayhosts, "control/relayhosts", 0)) {
439 if (!constmap_init(&maprelayhosts, relayhosts.s, relayhosts.len, 1))
446 static stralloc cmd = {0};
449 sig_alarmcatch(sigalrm);
452 if (chdir(auto_qmail) == -1) die();
456 if (ipme_init() != 1) die();
463 /* XXX: recipient can contain quoted lf. aargh. */
464 if (getln(&ssin,&cmd,&match,'\n') == -1) 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;