smtpd.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  1. #include "common.h"
  2. #include "smtpd.h"
  3. #include "smtp.h"
  4. #include <ctype.h>
  5. #include <ip.h>
  6. #include <ndb.h>
  7. #include <mp.h>
  8. #include <libsec.h>
  9. #include <auth.h>
  10. #include "../smtp/y.tab.h"
  11. char *me;
  12. char *him="";
  13. char *dom;
  14. process *pp;
  15. String *mailer;
  16. NetConnInfo *nci;
  17. int filterstate = ACCEPT;
  18. int trusted;
  19. int logged;
  20. int rejectcount;
  21. int hardreject;
  22. Biobuf bin;
  23. int debug;
  24. int Dflag;
  25. int fflag;
  26. int gflag;
  27. int rflag;
  28. int sflag;
  29. int authenticate;
  30. int authenticated;
  31. int passwordinclear;
  32. char *tlscert;
  33. List senders;
  34. List rcvers;
  35. int pipemsg(int*);
  36. String* startcmd(void);
  37. int rejectcheck(void);
  38. String* mailerpath(char*);
  39. static int
  40. catchalarm(void *a, char *msg)
  41. {
  42. int rv = 1;
  43. USED(a);
  44. /* log alarms but continue */
  45. if(strstr(msg, "alarm")){
  46. if(senders.first && rcvers.first)
  47. syslog(0, "smtpd", "note: %s->%s: %s\n", s_to_c(senders.first->p),
  48. s_to_c(rcvers.first->p), msg);
  49. else
  50. syslog(0, "smtpd", "note: %s\n", msg);
  51. rv = 0;
  52. }
  53. /* kill the children if there are any */
  54. if(pp)
  55. syskillpg(pp->pid);
  56. return rv;
  57. }
  58. /* override string error functions to do something reasonable */
  59. void
  60. s_error(char *f, char *status)
  61. {
  62. char errbuf[Errlen];
  63. errbuf[0] = 0;
  64. rerrstr(errbuf, sizeof(errbuf));
  65. if(f && *f)
  66. reply("452 out of memory %s: %s\r\n", f, errbuf);
  67. else
  68. reply("452 out of memory %s\r\n", errbuf);
  69. syslog(0, "smtpd", "++Malloc failure %s [%s]", him, nci->rsys);
  70. exits(status);
  71. }
  72. void
  73. main(int argc, char **argv)
  74. {
  75. char *p, buf[1024];
  76. char *netdir;
  77. netdir = nil;
  78. ARGBEGIN{
  79. case 'D':
  80. Dflag++;
  81. break;
  82. case 'd':
  83. debug++;
  84. break;
  85. case 'n': /* log peer ip address */
  86. netdir = ARGF();
  87. break;
  88. case 'f': /* disallow relaying */
  89. fflag = 1;
  90. break;
  91. case 'g':
  92. gflag = 1;
  93. break;
  94. case 'h': /* default domain name */
  95. dom = ARGF();
  96. break;
  97. case 'k': /* prohibited ip address */
  98. p = ARGF();
  99. if (p)
  100. addbadguy(p);
  101. break;
  102. case 'm': /* set mail command */
  103. p = ARGF();
  104. if(p)
  105. mailer = mailerpath(p);
  106. break;
  107. case 'r':
  108. rflag = 1; /* verify sender's domain */
  109. break;
  110. case 's': /* save blocked messages */
  111. sflag = 1;
  112. break;
  113. case 'a':
  114. authenticate = 1;
  115. break;
  116. case 'p':
  117. passwordinclear = 1;
  118. break;
  119. case 'c':
  120. tlscert = ARGF();
  121. break;
  122. case 't':
  123. fprint(2, "%s: the -t option is no longer supported, see -c\n", argv0);
  124. tlscert = "/sys/lib/ssl/smtpd-cert.pem";
  125. break;
  126. default:
  127. fprint(2, "usage: smtpd [-dfhrs] [-n net] [-c cert]\n");
  128. exits("usage");
  129. }ARGEND;
  130. nci = getnetconninfo(netdir, 0);
  131. if(nci == nil)
  132. sysfatal("can't get remote system's address");
  133. if(mailer == nil)
  134. mailer = mailerpath("send");
  135. if(debug){
  136. close(2);
  137. snprint(buf, sizeof(buf), "%s/smtpd", UPASLOG);
  138. if (open(buf, OWRITE) >= 0) {
  139. seek(2, 0, 2);
  140. fprint(2, "%d smtpd %s\n", getpid(), thedate());
  141. } else
  142. debug = 0;
  143. }
  144. getconf();
  145. Binit(&bin, 0, OREAD);
  146. chdir(UPASLOG);
  147. me = sysname_read();
  148. if(dom == 0 || dom[0] == 0)
  149. dom = domainname_read();
  150. if(dom == 0 || dom[0] == 0)
  151. dom = me;
  152. sayhi();
  153. parseinit();
  154. /* allow 45 minutes to parse the header */
  155. atnotify(catchalarm, 1);
  156. alarm(45*60*1000);
  157. zzparse();
  158. exits(0);
  159. }
  160. void
  161. listfree(List *l)
  162. {
  163. Link *lp;
  164. Link *next;
  165. for(lp = l->first; lp; lp = next){
  166. next = lp->next;
  167. s_free(lp->p);
  168. free(lp);
  169. }
  170. l->first = l->last = 0;
  171. }
  172. void
  173. listadd(List *l, String *path)
  174. {
  175. Link *lp;
  176. lp = (Link *)malloc(sizeof(Link));
  177. lp->p = path;
  178. lp->next = 0;
  179. if(l->last)
  180. l->last->next = lp;
  181. else
  182. l->first = lp;
  183. l->last = lp;
  184. }
  185. #define SIZE 4096
  186. int
  187. reply(char *fmt, ...)
  188. {
  189. char buf[SIZE], *out;
  190. va_list arg;
  191. int n;
  192. va_start(arg, fmt);
  193. out = vseprint(buf, buf+SIZE, fmt, arg);
  194. va_end(arg);
  195. n = (long)(out-buf);
  196. if(debug) {
  197. seek(2, 0, 2);
  198. write(2, buf, n);
  199. }
  200. write(1, buf, n);
  201. return n;
  202. }
  203. void
  204. reset(void)
  205. {
  206. if(rejectcheck())
  207. return;
  208. listfree(&rcvers);
  209. listfree(&senders);
  210. if(filterstate != DIALUP){
  211. logged = 0;
  212. filterstate = ACCEPT;
  213. }
  214. reply("250 ok\r\n");
  215. }
  216. void
  217. sayhi(void)
  218. {
  219. reply("220 %s SMTP\r\n", dom);
  220. }
  221. void
  222. hello(String *himp, int extended)
  223. {
  224. if(rejectcheck())
  225. return;
  226. him = s_to_c(himp);
  227. if(strchr(him, '.') == 0 && nci != nil && strchr(nci->rsys, '.') != nil)
  228. him = nci->rsys;
  229. if(Dflag)
  230. sleep(15*1000);
  231. reply("250%c%s you are %s\r\n", extended ? '-' : ' ', dom, him);
  232. if (extended) {
  233. if(tlscert != nil)
  234. reply("250-STARTTLS\r\n");
  235. if (passwordinclear)
  236. reply("250 AUTH CRAM-MD5 PLAIN LOGIN\r\n");
  237. else
  238. reply("250 AUTH CRAM-MD5\r\n");
  239. }
  240. }
  241. void
  242. sender(String *path)
  243. {
  244. String *s;
  245. char *cp;
  246. static char *lastsender;
  247. if(rejectcheck())
  248. return;
  249. if (authenticate && !authenticated) {
  250. rejectcount++;
  251. reply("530 Authentication required\r\n");
  252. return;
  253. }
  254. if(him == 0 || *him == 0){
  255. rejectcount++;
  256. reply("503 Start by saying HELO, please.\r\n", s_to_c(path));
  257. return;
  258. }
  259. /* don't add the domain onto black holes or we will loop */
  260. if(strchr(s_to_c(path), '!') == 0 && strcmp(s_to_c(path), "/dev/null") != 0){
  261. s = s_new();
  262. s_append(s, him);
  263. s_append(s, "!");
  264. s_append(s, s_to_c(path));
  265. s_terminate(s);
  266. s_free(path);
  267. path = s;
  268. }
  269. if(shellchars(s_to_c(path))){
  270. rejectcount++;
  271. reply("503 Bad character in sender address %s.\r\n", s_to_c(path));
  272. return;
  273. }
  274. /*
  275. * if the last sender address resulted in a rejection because the sending
  276. * domain didn't exist and this sender has the same domain, reject immediately.
  277. */
  278. if(lastsender){
  279. if (strncmp(lastsender, s_to_c(path), strlen(lastsender)) == 0){
  280. filterstate = REFUSED;
  281. rejectcount++;
  282. reply("554 Sender domain must exist: %s\r\n", s_to_c(path));
  283. return;
  284. }
  285. free(lastsender); /* different sender domain */
  286. lastsender = 0;
  287. }
  288. /*
  289. * see if this ip address, domain name, user name or account is blocked
  290. */
  291. filterstate = blocked(path);
  292. /*
  293. * perform DNS lookup to see if sending domain exists
  294. */
  295. if(filterstate == ACCEPT && rflag && returnable(s_to_c(path))){
  296. if(rmtdns(nci->root, s_to_c(path)) < 0){
  297. filterstate = REFUSED;
  298. lastsender = strdup(s_to_c(path));
  299. cp = strrchr(lastsender, '!');
  300. if(cp)
  301. *cp = 0;
  302. }
  303. }
  304. logged = 0;
  305. listadd(&senders, path);
  306. reply("250 sender is %s\r\n", s_to_c(path));
  307. }
  308. enum { Rcpt, Domain, Ntoks };
  309. typedef struct Sender Sender;
  310. struct Sender {
  311. Sender *next;
  312. char *rcpt;
  313. char *domain;
  314. };
  315. static Sender *sendlist, *sendlast;
  316. static uchar rsysip[IPaddrlen];
  317. static int
  318. rdsenders(void)
  319. {
  320. int lnlen, nf, ok = 1;
  321. char *line, *senderfile;
  322. char *toks[Ntoks];
  323. Biobuf *sf;
  324. Sender *snd;
  325. static int beenhere = 0;
  326. if (beenhere)
  327. return 1;
  328. beenhere = 1;
  329. fmtinstall('I', eipfmt);
  330. parseip(rsysip, nci->rsys);
  331. /*
  332. * we're sticking with a system-wide sender list because
  333. * per-user lists would require fully resolving recipient
  334. * addresses to determine which users they correspond to
  335. * (barring syntactic conventions).
  336. */
  337. senderfile = smprint("%s/senders", UPASLIB);
  338. sf = Bopen(senderfile, OREAD);
  339. free(senderfile);
  340. if (sf == nil)
  341. return 1;
  342. while ((line = Brdline(sf, '\n')) != nil) {
  343. if (line[0] == '#' || line[0] == '\n')
  344. continue;
  345. lnlen = Blinelen(sf);
  346. line[lnlen-1] = '\0'; /* clobber newline */
  347. nf = tokenize(line, toks, nelem(toks));
  348. if (nf != nelem(toks))
  349. continue; /* malformed line */
  350. snd = malloc(sizeof *snd);
  351. if (snd == nil)
  352. sysfatal("out of memory: %r");
  353. memset(snd, 0, sizeof *snd);
  354. snd->next = nil;
  355. if (sendlast == nil)
  356. sendlist = snd;
  357. else
  358. sendlast->next = snd;
  359. sendlast = snd;
  360. snd->rcpt = strdup(toks[Rcpt]);
  361. snd->domain = strdup(toks[Domain]);
  362. }
  363. Bterm(sf);
  364. return ok;
  365. }
  366. /*
  367. * read (recipient, sender's DNS) pairs from /mail/lib/senders.
  368. * Only allow mail to recipient from any of sender's IPs.
  369. * A recipient not mentioned in the file is always permitted.
  370. */
  371. static int
  372. senderok(char *rcpt)
  373. {
  374. int mentioned = 0, matched = 0;
  375. uchar dnsip[IPaddrlen];
  376. Sender *snd;
  377. Ndbtuple *nt, *next, *first;
  378. rdsenders();
  379. for (snd = sendlist; snd != nil; snd = snd->next) {
  380. if (strcmp(rcpt, snd->rcpt) != 0)
  381. continue;
  382. /*
  383. * see if this domain's ips match nci->rsys.
  384. * if not, perhaps a later entry's domain will.
  385. */
  386. mentioned = 1;
  387. if (parseip(dnsip, snd->domain) != -1 &&
  388. memcmp(rsysip, dnsip, IPaddrlen) == 0)
  389. return 1;
  390. /*
  391. * NB: nt->line links form a circular list(!).
  392. * we need to make one complete pass over it to free it all.
  393. */
  394. first = nt = dnsquery(nci->root, snd->domain, "ip");
  395. if (first == nil)
  396. continue;
  397. do {
  398. if (strcmp(nt->attr, "ip") == 0 &&
  399. parseip(dnsip, nt->val) != -1 &&
  400. memcmp(rsysip, dnsip, IPaddrlen) == 0)
  401. matched = 1;
  402. next = nt->line;
  403. free(nt);
  404. nt = next;
  405. } while (nt != first);
  406. }
  407. if (matched)
  408. return 1;
  409. else
  410. return !mentioned;
  411. }
  412. void
  413. receiver(String *path)
  414. {
  415. char *sender, *rcpt;
  416. if(rejectcheck())
  417. return;
  418. if(him == 0 || *him == 0){
  419. rejectcount++;
  420. reply("503 Start by saying HELO, please\r\n");
  421. return;
  422. }
  423. if(senders.last)
  424. sender = s_to_c(senders.last->p);
  425. else
  426. sender = "<unknown>";
  427. if(!recipok(s_to_c(path))){
  428. rejectcount++;
  429. syslog(0, "smtpd", "Disallowed %s (%s/%s) to blocked name %s",
  430. sender, him, nci->rsys, s_to_c(path));
  431. reply("550 %s ... user unknown\r\n", s_to_c(path));
  432. return;
  433. }
  434. rcpt = s_to_c(path);
  435. if (!senderok(rcpt)) {
  436. rejectcount++;
  437. syslog(0, "smtpd", "Disallowed sending IP of %s (%s/%s) to %s",
  438. sender, him, nci->rsys, rcpt);
  439. reply("550 %s ... sending system not allowed\r\n", rcpt);
  440. return;
  441. }
  442. logged = 0;
  443. /* forwarding() can modify 'path' on loopback request */
  444. if(filterstate == ACCEPT && (fflag && !authenticated) && forwarding(path)) {
  445. syslog(0, "smtpd", "Bad Forward %s (%s/%s) (%s)",
  446. s_to_c(senders.last->p), him, nci->rsys, s_to_c(path));
  447. rejectcount++;
  448. reply("550 we don't relay. send to your-path@[] for loopback.\r\n");
  449. return;
  450. }
  451. listadd(&rcvers, path);
  452. reply("250 receiver is %s\r\n", s_to_c(path));
  453. }
  454. void
  455. quit(void)
  456. {
  457. reply("221 Successful termination\r\n");
  458. close(0);
  459. exits(0);
  460. }
  461. void
  462. turn(void)
  463. {
  464. if(rejectcheck())
  465. return;
  466. reply("502 TURN unimplemented\r\n");
  467. }
  468. void
  469. noop(void)
  470. {
  471. if(rejectcheck())
  472. return;
  473. reply("250 Stop wasting my time!\r\n");
  474. }
  475. void
  476. help(String *cmd)
  477. {
  478. if(rejectcheck())
  479. return;
  480. if(cmd)
  481. s_free(cmd);
  482. reply("250 Read rfc821 and stop wasting my time\r\n");
  483. }
  484. void
  485. verify(String *path)
  486. {
  487. char *p, *q;
  488. char *av[4];
  489. if(rejectcheck())
  490. return;
  491. if(shellchars(s_to_c(path))){
  492. reply("503 Bad character in address %s.\r\n", s_to_c(path));
  493. return;
  494. }
  495. av[0] = s_to_c(mailer);
  496. av[1] = "-x";
  497. av[2] = s_to_c(path);
  498. av[3] = 0;
  499. pp = noshell_proc_start(av, (stream *)0, outstream(), (stream *)0, 1, 0);
  500. if (pp == 0) {
  501. reply("450 We're busy right now, try later\r\n");
  502. return;
  503. }
  504. p = Brdline(pp->std[1]->fp, '\n');
  505. if(p == 0){
  506. reply("550 String does not match anything.\r\n");
  507. } else {
  508. p[Blinelen(pp->std[1]->fp)-1] = 0;
  509. if(strchr(p, ':'))
  510. reply("550 String does not match anything.\r\n");
  511. else{
  512. q = strrchr(p, '!');
  513. if(q)
  514. p = q+1;
  515. reply("250 %s <%s@%s>\r\n", s_to_c(path), p, dom);
  516. }
  517. }
  518. proc_wait(pp);
  519. proc_free(pp);
  520. pp = 0;
  521. }
  522. /*
  523. * get a line that ends in crnl or cr, turn terminating crnl into a nl
  524. *
  525. * return 0 on EOF
  526. */
  527. static int
  528. getcrnl(String *s, Biobuf *fp)
  529. {
  530. int c;
  531. for(;;){
  532. c = Bgetc(fp);
  533. if(debug) {
  534. seek(2, 0, 2);
  535. fprint(2, "%c", c);
  536. }
  537. switch(c){
  538. case -1:
  539. goto out;
  540. case '\r':
  541. c = Bgetc(fp);
  542. if(c == '\n'){
  543. if(debug) {
  544. seek(2, 0, 2);
  545. fprint(2, "%c", c);
  546. }
  547. s_putc(s, '\n');
  548. goto out;
  549. }
  550. Bungetc(fp);
  551. s_putc(s, '\r');
  552. break;
  553. case '\n':
  554. s_putc(s, c);
  555. goto out;
  556. default:
  557. s_putc(s, c);
  558. break;
  559. }
  560. }
  561. out:
  562. s_terminate(s);
  563. return s_len(s);
  564. }
  565. void
  566. logcall(int nbytes)
  567. {
  568. Link *l;
  569. String *to, *from;
  570. to = s_new();
  571. from = s_new();
  572. for(l = senders.first; l; l = l->next){
  573. if(l != senders.first)
  574. s_append(from, ", ");
  575. s_append(from, s_to_c(l->p));
  576. }
  577. for(l = rcvers.first; l; l = l->next){
  578. if(l != rcvers.first)
  579. s_append(to, ", ");
  580. s_append(to, s_to_c(l->p));
  581. }
  582. syslog(0, "smtpd", "[%s/%s] %s sent %d bytes to %s", him, nci->rsys,
  583. s_to_c(from), nbytes, s_to_c(to));
  584. s_free(to);
  585. s_free(from);
  586. }
  587. static void
  588. logmsg(char *action)
  589. {
  590. Link *l;
  591. if(logged)
  592. return;
  593. logged = 1;
  594. for(l = rcvers.first; l; l = l->next)
  595. syslog(0, "smtpd", "%s %s (%s/%s) (%s)", action,
  596. s_to_c(senders.last->p), him, nci->rsys, s_to_c(l->p));
  597. }
  598. static int
  599. optoutall(int filterstate)
  600. {
  601. Link *l;
  602. switch(filterstate){
  603. case ACCEPT:
  604. case TRUSTED:
  605. return filterstate;
  606. }
  607. for(l = rcvers.first; l; l = l->next)
  608. if(!optoutofspamfilter(s_to_c(l->p)))
  609. return filterstate;
  610. return ACCEPT;
  611. }
  612. String*
  613. startcmd(void)
  614. {
  615. int n;
  616. Link *l;
  617. char **av;
  618. String *cmd;
  619. char *filename;
  620. /*
  621. * ignore the filterstate if the all the receivers prefer it.
  622. */
  623. filterstate = optoutall(filterstate);
  624. switch (filterstate){
  625. case BLOCKED:
  626. case DELAY:
  627. rejectcount++;
  628. logmsg("Blocked");
  629. filename = dumpfile(s_to_c(senders.last->p));
  630. cmd = s_new();
  631. s_append(cmd, "cat > ");
  632. s_append(cmd, filename);
  633. pp = proc_start(s_to_c(cmd), instream(), 0, outstream(), 0, 0);
  634. break;
  635. case DIALUP:
  636. logmsg("Dialup");
  637. rejectcount++;
  638. reply("554 We don't accept mail from dial-up ports.\r\n");
  639. /*
  640. * we could exit here, because we're never going to accept mail from this
  641. * ip address, but it's unclear that RFC821 allows that. Instead we set
  642. * the hardreject flag and go stupid.
  643. */
  644. hardreject = 1;
  645. return 0;
  646. case DENIED:
  647. logmsg("Denied");
  648. rejectcount++;
  649. reply("554-We don't accept mail from %s.\r\n", s_to_c(senders.last->p));
  650. reply("554 Contact postmaster@%s for more information.\r\n", dom);
  651. return 0;
  652. case REFUSED:
  653. logmsg("Refused");
  654. rejectcount++;
  655. reply("554 Sender domain must exist: %s\r\n", s_to_c(senders.last->p));
  656. return 0;
  657. default:
  658. case NONE:
  659. logmsg("Confused");
  660. rejectcount++;
  661. reply("554-We have had an internal mailer error classifying your message.\r\n");
  662. reply("554-Filterstate is %d\r\n", filterstate);
  663. reply("554 Contact postmaster@%s for more information.\r\n", dom);
  664. return 0;
  665. case ACCEPT:
  666. case TRUSTED:
  667. /*
  668. * now that all other filters have been passed,
  669. * do grey-list processing.
  670. */
  671. if(gflag)
  672. vfysenderhostok();
  673. /*
  674. * set up mail command
  675. */
  676. cmd = s_clone(mailer);
  677. n = 3;
  678. for(l = rcvers.first; l; l = l->next)
  679. n++;
  680. av = malloc(n*sizeof(char*));
  681. if(av == nil){
  682. reply("450 We're busy right now, try later\n");
  683. s_free(cmd);
  684. return 0;
  685. }
  686. n = 0;
  687. av[n++] = s_to_c(cmd);
  688. av[n++] = "-r";
  689. for(l = rcvers.first; l; l = l->next)
  690. av[n++] = s_to_c(l->p);
  691. av[n] = 0;
  692. /*
  693. * start mail process
  694. */
  695. pp = noshell_proc_start(av, instream(), outstream(), outstream(), 0, 0);
  696. free(av);
  697. break;
  698. }
  699. if(pp == 0) {
  700. reply("450 We're busy right now, try later\n");
  701. s_free(cmd);
  702. return 0;
  703. }
  704. return cmd;
  705. }
  706. /*
  707. * print out a header line, expanding any domainless addresses into
  708. * address@him
  709. */
  710. char*
  711. bprintnode(Biobuf *b, Node *p)
  712. {
  713. if(p->s){
  714. if(p->addr && strchr(s_to_c(p->s), '@') == nil){
  715. if(Bprint(b, "%s@%s", s_to_c(p->s), him) < 0)
  716. return nil;
  717. } else {
  718. if(Bwrite(b, s_to_c(p->s), s_len(p->s)) < 0)
  719. return nil;
  720. }
  721. }else{
  722. if(Bputc(b, p->c) < 0)
  723. return nil;
  724. }
  725. if(p->white)
  726. if(Bwrite(b, s_to_c(p->white), s_len(p->white)) < 0)
  727. return nil;
  728. return p->end+1;
  729. }
  730. static String*
  731. getaddr(Node *p)
  732. {
  733. for(; p; p = p->next)
  734. if(p->s && p->addr)
  735. return p->s;
  736. return nil;
  737. }
  738. /*
  739. * add waring headers of the form
  740. * X-warning: <reason>
  741. * for any headers that looked like they might be forged.
  742. *
  743. * return byte count of new headers
  744. */
  745. static int
  746. forgedheaderwarnings(void)
  747. {
  748. int nbytes;
  749. Field *f;
  750. nbytes = 0;
  751. /* warn about envelope sender */
  752. if(strcmp(s_to_c(senders.last->p), "/dev/null") != 0 && masquerade(senders.last->p, nil))
  753. nbytes += Bprint(pp->std[0]->fp, "X-warning: suspect envelope domain\n");
  754. /*
  755. * check Sender: field. If it's OK, ignore the others because this is an
  756. * exploded mailing list.
  757. */
  758. for(f = firstfield; f; f = f->next){
  759. if(f->node->c == SENDER){
  760. if(masquerade(getaddr(f->node), him))
  761. nbytes += Bprint(pp->std[0]->fp, "X-warning: suspect Sender: domain\n");
  762. else
  763. return nbytes;
  764. }
  765. }
  766. /* check From: */
  767. for(f = firstfield; f; f = f->next){
  768. if(f->node->c == FROM && masquerade(getaddr(f->node), him))
  769. nbytes += Bprint(pp->std[0]->fp, "X-warning: suspect From: domain\n");
  770. }
  771. return nbytes;
  772. }
  773. /*
  774. * pipe message to mailer with the following transformations:
  775. * - change \r\n into \n.
  776. * - add sender's domain to any addrs with no domain
  777. * - add a From: if none of From:, Sender:, or Replyto: exists
  778. * - add a Received: line
  779. */
  780. int
  781. pipemsg(int *byteswritten)
  782. {
  783. int status;
  784. char *cp;
  785. String *line;
  786. String *hdr;
  787. int n, nbytes;
  788. int sawdot;
  789. Field *f;
  790. Node *p;
  791. Link *l;
  792. pipesig(&status); /* set status to 1 on write to closed pipe */
  793. sawdot = 0;
  794. status = 0;
  795. /*
  796. * add a 'From ' line as envelope
  797. */
  798. nbytes = 0;
  799. nbytes += Bprint(pp->std[0]->fp, "From %s %s remote from \n",
  800. s_to_c(senders.first->p), thedate());
  801. /*
  802. * add our own Received: stamp
  803. */
  804. nbytes += Bprint(pp->std[0]->fp, "Received: from %s ", him);
  805. if(nci->rsys)
  806. nbytes += Bprint(pp->std[0]->fp, "([%s]) ", nci->rsys);
  807. nbytes += Bprint(pp->std[0]->fp, "by %s; %s\n", me, thedate());
  808. /*
  809. * read first 16k obeying '.' escape. we're assuming
  810. * the header will all be there.
  811. */
  812. line = s_new();
  813. hdr = s_new();
  814. while(sawdot == 0 && s_len(hdr) < 16*1024){
  815. n = getcrnl(s_reset(line), &bin);
  816. /* eof or error ends the message */
  817. if(n <= 0)
  818. break;
  819. /* a line with only a '.' ends the message */
  820. cp = s_to_c(line);
  821. if(n == 2 && *cp == '.' && *(cp+1) == '\n'){
  822. sawdot = 1;
  823. break;
  824. }
  825. s_append(hdr, *cp == '.' ? cp+1 : cp);
  826. }
  827. /*
  828. * parse header
  829. */
  830. yyinit(s_to_c(hdr), s_len(hdr));
  831. yyparse();
  832. /*
  833. * Llook for masquerades. Let Sender: trump From: to allow mailing list
  834. * forwarded messages.
  835. */
  836. if(fflag)
  837. nbytes += forgedheaderwarnings();
  838. /*
  839. * add an orginator and/or destination if either is missing
  840. */
  841. if(originator == 0){
  842. if(senders.last == nil)
  843. Bprint(pp->std[0]->fp, "From: /dev/null@%s\n", him);
  844. else
  845. Bprint(pp->std[0]->fp, "From: %s\n", s_to_c(senders.last->p));
  846. }
  847. if(destination == 0){
  848. Bprint(pp->std[0]->fp, "To: ");
  849. for(l = rcvers.first; l; l = l->next){
  850. if(l != rcvers.first)
  851. Bprint(pp->std[0]->fp, ", ");
  852. Bprint(pp->std[0]->fp, "%s", s_to_c(l->p));
  853. }
  854. Bprint(pp->std[0]->fp, "\n");
  855. }
  856. /*
  857. * add sender's domain to any domainless addresses
  858. * (to avoid forging local addresses)
  859. */
  860. cp = s_to_c(hdr);
  861. for(f = firstfield; cp != nil && f; f = f->next){
  862. for(p = f->node; cp != 0 && p; p = p->next)
  863. cp = bprintnode(pp->std[0]->fp, p);
  864. if(status == 0 && Bprint(pp->std[0]->fp, "\n") < 0)
  865. status = 1;
  866. }
  867. if(cp == nil)
  868. status = 1;
  869. /* write anything we read following the header */
  870. if(status == 0 && Bwrite(pp->std[0]->fp, cp, s_to_c(hdr) + s_len(hdr) - cp) < 0)
  871. status = 1;
  872. s_free(hdr);
  873. /*
  874. * pass rest of message to mailer. take care of '.'
  875. * escapes.
  876. */
  877. while(sawdot == 0){
  878. n = getcrnl(s_reset(line), &bin);
  879. /* eof or error ends the message */
  880. if(n <= 0)
  881. break;
  882. /* a line with only a '.' ends the message */
  883. cp = s_to_c(line);
  884. if(n == 2 && *cp == '.' && *(cp+1) == '\n'){
  885. sawdot = 1;
  886. break;
  887. }
  888. nbytes += n;
  889. if(status == 0 && Bwrite(pp->std[0]->fp, *cp == '.' ? cp+1 : cp, n) < 0){
  890. status = 1;
  891. }
  892. }
  893. s_free(line);
  894. if(sawdot == 0){
  895. /* message did not terminate normally */
  896. syskillpg(pp->pid);
  897. status = 1;
  898. }
  899. if(status == 0 && Bflush(pp->std[0]->fp) < 0)
  900. status = 1;
  901. stream_free(pp->std[0]);
  902. pp->std[0] = 0;
  903. *byteswritten = nbytes;
  904. pipesigoff();
  905. return status;
  906. }
  907. char*
  908. firstline(char *x)
  909. {
  910. static char buf[128];
  911. char *p;
  912. strncpy(buf, x, sizeof(buf));
  913. buf[sizeof(buf)-1] = 0;
  914. p = strchr(buf, '\n');
  915. if(p)
  916. *p = 0;
  917. return buf;
  918. }
  919. void
  920. data(void)
  921. {
  922. String *cmd;
  923. String *err;
  924. int status, nbytes;
  925. char *cp, *ep;
  926. if(rejectcheck())
  927. return;
  928. if(senders.last == 0){
  929. reply("503 Data without MAIL FROM:\r\n");
  930. rejectcount++;
  931. return;
  932. }
  933. if(rcvers.last == 0){
  934. reply("503 Data without RCPT TO:\r\n");
  935. rejectcount++;
  936. return;
  937. }
  938. cmd = startcmd();
  939. if(cmd == 0)
  940. return;
  941. reply("354 Input message; end with <CRLF>.<CRLF>\r\n");
  942. /*
  943. * allow 145 more minutes to move the data
  944. */
  945. alarm(145*60*1000);
  946. status = pipemsg(&nbytes);
  947. /*
  948. * read any error messages
  949. */
  950. err = s_new();
  951. while(s_read_line(pp->std[2]->fp, err))
  952. ;
  953. alarm(0);
  954. atnotify(catchalarm, 0);
  955. status |= proc_wait(pp);
  956. if(debug){
  957. seek(2, 0, 2);
  958. fprint(2, "%d status %ux\n", getpid(), status);
  959. if(*s_to_c(err))
  960. fprint(2, "%d error %s\n", getpid(), s_to_c(err));
  961. }
  962. proc_free(pp);
  963. pp = 0;
  964. /*
  965. * if process terminated abnormally, send back error message
  966. */
  967. if(status){
  968. int code;
  969. if(strstr(s_to_c(err), "mail refused")){
  970. syslog(0, "smtpd", "++[%s/%s] %s %s refused: %s", him, nci->rsys,
  971. s_to_c(senders.first->p), s_to_c(cmd), firstline(s_to_c(err)));
  972. code = 554;
  973. } else {
  974. syslog(0, "smtpd", "++[%s/%s] %s %s returned %s", him, nci->rsys,
  975. s_to_c(senders.first->p), s_to_c(cmd), firstline(s_to_c(err)));
  976. code = 450;
  977. }
  978. for(cp = s_to_c(err); ep = strchr(cp, '\n'); cp = ep){
  979. *ep++ = 0;
  980. reply("%d-%s\r\n", code, cp);
  981. }
  982. reply("%d mail process terminated abnormally\r\n", code);
  983. } else {
  984. if(filterstate == BLOCKED)
  985. reply("554 we believe this is spam. we don't accept it.\r\n");
  986. else
  987. if(filterstate == DELAY)
  988. reply("554 There will be a delay in delivery of this message.\r\n");
  989. else {
  990. reply("250 sent\r\n");
  991. logcall(nbytes);
  992. }
  993. }
  994. s_free(cmd);
  995. s_free(err);
  996. listfree(&senders);
  997. listfree(&rcvers);
  998. }
  999. /*
  1000. * when we have blocked a transaction based on IP address, there is nothing
  1001. * that the sender can do to convince us to take the message. after the
  1002. * first rejection, some spammers continually RSET and give a new MAIL FROM:
  1003. * filling our logs with rejections. rejectcheck() limits the retries and
  1004. * swiftly rejects all further commands after the first 500-series message
  1005. * is issued.
  1006. */
  1007. int
  1008. rejectcheck(void)
  1009. {
  1010. if(rejectcount > MAXREJECTS){
  1011. syslog(0, "smtpd", "Rejected (%s/%s)", him, nci->rsys);
  1012. reply("554 too many errors. transaction failed.\r\n");
  1013. exits("errcount");
  1014. }
  1015. if(hardreject){
  1016. rejectcount++;
  1017. reply("554 We don't accept mail from dial-up ports.\r\n");
  1018. }
  1019. return hardreject;
  1020. }
  1021. /*
  1022. * create abs path of the mailer
  1023. */
  1024. String*
  1025. mailerpath(char *p)
  1026. {
  1027. String *s;
  1028. if(p == nil)
  1029. return nil;
  1030. if(*p == '/')
  1031. return s_copy(p);
  1032. s = s_new();
  1033. s_append(s, UPASBIN);
  1034. s_append(s, "/");
  1035. s_append(s, p);
  1036. return s;
  1037. }
  1038. String *
  1039. s_dec64(String *sin)
  1040. {
  1041. String *sout;
  1042. int lin, lout;
  1043. lin = s_len(sin);
  1044. /*
  1045. * if the string is coming from smtpd.y, it will have no nl.
  1046. * if it is coming from getcrnl below, it will have an nl.
  1047. */
  1048. if (*(s_to_c(sin)+lin-1) == '\n')
  1049. lin--;
  1050. sout = s_newalloc(lin+1);
  1051. lout = dec64((uchar *)s_to_c(sout), lin, s_to_c(sin), lin);
  1052. if (lout < 0) {
  1053. s_free(sout);
  1054. return nil;
  1055. }
  1056. sout->ptr = sout->base + lout;
  1057. s_terminate(sout);
  1058. return sout;
  1059. }
  1060. void
  1061. starttls(void)
  1062. {
  1063. uchar *cert;
  1064. int certlen, fd;
  1065. TLSconn *conn;
  1066. conn = mallocz(sizeof *conn, 1);
  1067. cert = readcert(tlscert, &certlen);
  1068. if (conn == nil || cert == nil) {
  1069. if (conn != nil)
  1070. free(conn);
  1071. reply("454 TLS not available\r\n");
  1072. return;
  1073. }
  1074. reply("220 Go ahead make my day\r\n");
  1075. conn->cert = cert;
  1076. conn->certlen = certlen;
  1077. fd = tlsServer(Bfildes(&bin), conn);
  1078. if (fd < 0) {
  1079. free(cert);
  1080. free(conn);
  1081. syslog(0, "smtpd", "TLS start-up failed with %s", him);
  1082. /* force the client to hang up */
  1083. close(Bfildes(&bin)); /* probably fd 0 */
  1084. close(1);
  1085. exits("tls failed");
  1086. }
  1087. Bterm(&bin);
  1088. Binit(&bin, fd, OREAD);
  1089. if (dup(fd, 1) < 0)
  1090. fprint(2, "dup of %d failed: %r\n", fd);
  1091. passwordinclear = 1;
  1092. syslog(0, "smtpd", "started TLS with %s", him);
  1093. }
  1094. void
  1095. auth(String *mech, String *resp)
  1096. {
  1097. Chalstate *chs = nil;
  1098. AuthInfo *ai = nil;
  1099. String *s_resp1_64 = nil;
  1100. String *s_resp2_64 = nil;
  1101. String *s_resp1 = nil;
  1102. String *s_resp2 = nil;
  1103. char *scratch = nil;
  1104. char *user, *pass;
  1105. if (rejectcheck())
  1106. goto bomb_out;
  1107. syslog(0, "smtpd", "auth(%s, %s) from %s\n", s_to_c(mech),
  1108. resp==nil?"nil":s_to_c(resp), him);
  1109. if (authenticated) {
  1110. bad_sequence:
  1111. rejectcount++;
  1112. reply("503 Bad sequence of commands\r\n");
  1113. goto bomb_out;
  1114. }
  1115. if (cistrcmp(s_to_c(mech), "plain") == 0) {
  1116. if (!passwordinclear) {
  1117. rejectcount++;
  1118. reply("538 Encryption required for requested authentication mechanism\r\n");
  1119. goto bomb_out;
  1120. }
  1121. s_resp1_64 = resp;
  1122. if (s_resp1_64 == nil) {
  1123. reply("334 \r\n");
  1124. s_resp1_64 = s_new();
  1125. if (getcrnl(s_resp1_64, &bin) <= 0) {
  1126. goto bad_sequence;
  1127. }
  1128. }
  1129. s_resp1 = s_dec64(s_resp1_64);
  1130. if (s_resp1 == nil) {
  1131. rejectcount++;
  1132. reply("501 Cannot decode base64\r\n");
  1133. goto bomb_out;
  1134. }
  1135. memset(s_to_c(s_resp1_64), 'X', s_len(s_resp1_64));
  1136. user = (s_to_c(s_resp1) + strlen(s_to_c(s_resp1)) + 1);
  1137. pass = user + (strlen(user) + 1);
  1138. ai = auth_userpasswd(user, pass);
  1139. authenticated = ai != nil;
  1140. memset(pass, 'X', strlen(pass));
  1141. goto windup;
  1142. }
  1143. else if (cistrcmp(s_to_c(mech), "login") == 0) {
  1144. if (!passwordinclear) {
  1145. rejectcount++;
  1146. reply("538 Encryption required for requested authentication mechanism\r\n");
  1147. goto bomb_out;
  1148. }
  1149. if (resp == nil) {
  1150. reply("334 VXNlcm5hbWU6\r\n");
  1151. s_resp1_64 = s_new();
  1152. if (getcrnl(s_resp1_64, &bin) <= 0)
  1153. goto bad_sequence;
  1154. }
  1155. reply("334 UGFzc3dvcmQ6\r\n");
  1156. s_resp2_64 = s_new();
  1157. if (getcrnl(s_resp2_64, &bin) <= 0)
  1158. goto bad_sequence;
  1159. s_resp1 = s_dec64(s_resp1_64);
  1160. s_resp2 = s_dec64(s_resp2_64);
  1161. memset(s_to_c(s_resp2_64), 'X', s_len(s_resp2_64));
  1162. if (s_resp1 == nil || s_resp2 == nil) {
  1163. rejectcount++;
  1164. reply("501 Cannot decode base64\r\n");
  1165. goto bomb_out;
  1166. }
  1167. ai = auth_userpasswd(s_to_c(s_resp1), s_to_c(s_resp2));
  1168. authenticated = ai != nil;
  1169. memset(s_to_c(s_resp2), 'X', s_len(s_resp2));
  1170. windup:
  1171. if (authenticated)
  1172. reply("235 Authentication successful\r\n");
  1173. else {
  1174. rejectcount++;
  1175. reply("535 Authentication failed\r\n");
  1176. }
  1177. goto bomb_out;
  1178. }
  1179. else if (cistrcmp(s_to_c(mech), "cram-md5") == 0) {
  1180. char *resp;
  1181. int chal64n;
  1182. char *t;
  1183. chs = auth_challenge("proto=cram role=server");
  1184. if (chs == nil) {
  1185. rejectcount++;
  1186. reply("501 Couldn't get CRAM-MD5 challenge\r\n");
  1187. goto bomb_out;
  1188. }
  1189. scratch = malloc(chs->nchal * 2 + 1);
  1190. chal64n = enc64(scratch, chs->nchal * 2, (uchar *)chs->chal, chs->nchal);
  1191. scratch[chal64n] = 0;
  1192. reply("334 %s\r\n", scratch);
  1193. s_resp1_64 = s_new();
  1194. if (getcrnl(s_resp1_64, &bin) <= 0)
  1195. goto bad_sequence;
  1196. s_resp1 = s_dec64(s_resp1_64);
  1197. if (s_resp1 == nil) {
  1198. rejectcount++;
  1199. reply("501 Cannot decode base64\r\n");
  1200. goto bomb_out;
  1201. }
  1202. /* should be of form <user><space><response> */
  1203. resp = s_to_c(s_resp1);
  1204. t = strchr(resp, ' ');
  1205. if (t == nil) {
  1206. rejectcount++;
  1207. reply("501 Poorly formed CRAM-MD5 response\r\n");
  1208. goto bomb_out;
  1209. }
  1210. *t++ = 0;
  1211. chs->user = resp;
  1212. chs->resp = t;
  1213. chs->nresp = strlen(t);
  1214. ai = auth_response(chs);
  1215. authenticated = ai != nil;
  1216. goto windup;
  1217. }
  1218. rejectcount++;
  1219. reply("501 Unrecognised authentication type %s\r\n", s_to_c(mech));
  1220. bomb_out:
  1221. if (ai)
  1222. auth_freeAI(ai);
  1223. if (chs)
  1224. auth_freechal(chs);
  1225. if (scratch)
  1226. free(scratch);
  1227. if (s_resp1)
  1228. s_free(s_resp1);
  1229. if (s_resp2)
  1230. s_free(s_resp2);
  1231. if (s_resp1_64)
  1232. s_free(s_resp1_64);
  1233. if (s_resp2_64)
  1234. s_free(s_resp2_64);
  1235. }