smtpd.c 30 KB

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