smtpd.c 31 KB

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