smtpd.c 29 KB

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