smtpd.c 33 KB

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