smtpd.c 31 KB

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