smtpd.c 30 KB

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