smtpd.c 30 KB

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