marshal.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828
  1. #include "common.h"
  2. #include <ctype.h>
  3. typedef struct Attach Attach;
  4. typedef struct Alias Alias;
  5. typedef struct Addr Addr;
  6. typedef struct Ctype Ctype;
  7. struct Attach {
  8. Attach *next;
  9. char *path;
  10. char *type;
  11. int inline;
  12. Ctype *ctype;
  13. };
  14. struct Alias
  15. {
  16. Alias *next;
  17. int n;
  18. Addr *addr;
  19. };
  20. struct Addr
  21. {
  22. Addr *next;
  23. char *v;
  24. };
  25. enum {
  26. Hfrom,
  27. Hto,
  28. Hcc,
  29. Hbcc,
  30. Hsender,
  31. Hreplyto,
  32. Hinreplyto,
  33. Hdate,
  34. Hsubject,
  35. Hmime,
  36. Hpriority,
  37. Hmsgid,
  38. Hcontent,
  39. Hx,
  40. Nhdr,
  41. };
  42. enum {
  43. PGPsign = 1,
  44. PGPencrypt = 2,
  45. };
  46. char *hdrs[Nhdr] = {
  47. [Hfrom] "from:",
  48. [Hto] "to:",
  49. [Hcc] "cc:",
  50. [Hbcc] "bcc:",
  51. [Hreplyto] "reply-to:",
  52. [Hinreplyto] "in-reply-to:",
  53. [Hsender] "sender:",
  54. [Hdate] "date:",
  55. [Hsubject] "subject:",
  56. [Hpriority] "priority:",
  57. [Hmsgid] "message-id:",
  58. [Hmime] "mime-",
  59. [Hcontent] "content-",
  60. [Hx] "x-",
  61. };
  62. struct Ctype {
  63. char *type;
  64. char *ext;
  65. int display;
  66. };
  67. Ctype ctype[] = {
  68. { "text/plain", "txt", 1, },
  69. { "text/html", "html", 1, },
  70. { "text/html", "htm", 1, },
  71. { "text/tab-separated-values", "tsv", 1, },
  72. { "text/richtext", "rtx", 1, },
  73. { "message/rfc822", "txt", 1, },
  74. { "", 0, 0, },
  75. };
  76. Ctype *mimetypes;
  77. int pid = -1;
  78. int pgppid = -1;
  79. Attach* mkattach(char*, char*, int);
  80. int readheaders(Biobuf*, int*, String**, Addr**);
  81. void body(Biobuf*, Biobuf*, int);
  82. char* mkboundary(void);
  83. int printdate(Biobuf*);
  84. int printfrom(Biobuf*);
  85. int printto(Biobuf*, Addr*);
  86. int printcc(Biobuf*, Addr*);
  87. int printsubject(Biobuf*, char*);
  88. int printinreplyto(Biobuf*, char*);
  89. int sendmail(Addr*, Addr*, int*, char*);
  90. void attachment(Attach*, Biobuf*);
  91. int cistrncmp(char*, char*, int);
  92. int cistrcmp(char*, char*);
  93. char* waitforsubprocs(void);
  94. int enc64(char*, int, uchar*, int);
  95. Addr* expand(int, char**);
  96. Alias* readaliases(void);
  97. Addr* expandline(String**, Addr*);
  98. void Bdrain(Biobuf*);
  99. void freeaddr(Addr *);
  100. int pgpopts(char*);
  101. int pgpfilter(int*, int, int);
  102. void readmimetypes(void);
  103. char* estrdup(char*);
  104. void* emalloc(int);
  105. void* erealloc(void*, int);
  106. void freeaddr(Addr*);
  107. void freeaddrs(Addr*);
  108. void freealias(Alias*);
  109. void freealiases(Alias*);
  110. int doublequote(Fmt*);
  111. int rflag, lbflag, xflag, holding, nflag, Fflag, eightflag;
  112. int pgpflag = 0;
  113. char *user;
  114. char *login;
  115. Alias *aliases;
  116. int rfc822syntaxerror;
  117. char lastchar;
  118. char *replymsg;
  119. enum
  120. {
  121. Ok = 0,
  122. Nomessage = 1,
  123. Nobody = 2,
  124. Error = -1,
  125. };
  126. #pragma varargck type "Z" char*
  127. void
  128. usage(void)
  129. {
  130. fprint(2, "usage: %s [-Fr#xn] [-s subject] [-c ccrecipient] [-t type] [-aA attachment] [-p[es]] [-R replymsg] -8 | recipient-list\n",
  131. argv0);
  132. exits("usage");
  133. }
  134. void
  135. fatal(char *fmt, ...)
  136. {
  137. char buf[1024];
  138. va_list arg;
  139. if(pid >= 0)
  140. postnote(PNPROC, pid, "die");
  141. if(pgppid >= 0)
  142. postnote(PNPROC, pgppid, "die");
  143. va_start(arg, fmt);
  144. vseprint(buf, buf+sizeof(buf), fmt, arg);
  145. va_end(arg);
  146. fprint(2, "%s: %s\n", argv0, buf);
  147. holdoff(holding);
  148. exits(buf);
  149. }
  150. void
  151. main(int argc, char **argv)
  152. {
  153. Attach *first, **l, *a;
  154. char *subject, *type, *boundary;
  155. int flags, fd;
  156. Biobuf in, out, *b;
  157. Addr *to;
  158. Addr *cc;
  159. String *file, *hdrstring;
  160. int noinput, headersrv;
  161. int ccargc;
  162. char *ccargv[32];
  163. noinput = 0;
  164. subject = nil;
  165. first = nil;
  166. l = &first;
  167. type = nil;
  168. hdrstring = nil;
  169. ccargc = 0;
  170. quotefmtinstall();
  171. fmtinstall('Z', doublequote);
  172. ARGBEGIN{
  173. case 't':
  174. type = ARGF();
  175. if(type == nil)
  176. usage();
  177. break;
  178. case 'a':
  179. flags = 0;
  180. goto aflag;
  181. case 'A':
  182. flags = 1;
  183. aflag:
  184. a = mkattach(ARGF(), type, flags);
  185. if(a == nil)
  186. exits("bad args");
  187. type = nil;
  188. *l = a;
  189. l = &a->next;
  190. break;
  191. case 'C':
  192. if(ccargc >= nelem(ccargv)-1)
  193. sysfatal("too many cc's");
  194. ccargv[ccargc] = ARGF();
  195. if(ccargv[ccargc] == nil)
  196. usage();
  197. ccargc++;
  198. break;
  199. case 'R':
  200. replymsg = ARGF();
  201. break;
  202. case 's':
  203. subject = ARGF();
  204. break;
  205. case 'F':
  206. Fflag = 1; // file message
  207. break;
  208. case 'r':
  209. rflag = 1; // for sendmail
  210. break;
  211. case '#':
  212. lbflag = 1; // for sendmail
  213. break;
  214. case 'x':
  215. xflag = 1; // for sendmail
  216. break;
  217. case 'n': // no standard input
  218. nflag = 1;
  219. break;
  220. case '8': // read recipients from rfc822 header
  221. eightflag = 1;
  222. break;
  223. case 'p': // pgp flag: encrypt, sign, or both
  224. if(pgpopts(ARGF()) < 0)
  225. sysfatal("bad pgp options");
  226. break;
  227. default:
  228. usage();
  229. break;
  230. }ARGEND;
  231. login = getlog();
  232. user = getenv("upasname");
  233. if(user == nil || *user == 0)
  234. user = login;
  235. if(user == nil || *user == 0)
  236. sysfatal("can't read user name");
  237. if(Binit(&in, 0, OREAD) < 0)
  238. sysfatal("can't Binit 0: %r");
  239. if(nflag && eightflag)
  240. sysfatal("can't use both -n and -8");
  241. if(eightflag && argc >= 1)
  242. usage();
  243. else if(!eightflag && argc < 1)
  244. usage();
  245. aliases = readaliases();
  246. if(!eightflag){
  247. to = expand(argc, argv);
  248. cc = expand(ccargc, ccargv);
  249. } else {
  250. to = nil;
  251. cc = nil;
  252. }
  253. flags = 0;
  254. headersrv = Nomessage;
  255. if(!nflag && !xflag && !lbflag) {
  256. // pass through headers, keeping track of which we've seen,
  257. // perhaps building to list.
  258. holding = holdon();
  259. headersrv = readheaders(&in, &flags, &hdrstring, eightflag ? &to : nil);
  260. if(rfc822syntaxerror){
  261. Bdrain(&in);
  262. fatal("rfc822 syntax error, message not sent");
  263. }
  264. if(to == nil){
  265. Bdrain(&in);
  266. fatal("no addresses found, message not sent");
  267. }
  268. switch(headersrv){
  269. case Error: // error
  270. fatal("reading");
  271. break;
  272. case Nomessage: // no message, just exit mimicking old behavior
  273. noinput = 1;
  274. if(first == nil)
  275. exits(0);
  276. break;
  277. }
  278. }
  279. fd = sendmail(to, cc, &pid, Fflag ? argv[0] : nil);
  280. if(fd < 0)
  281. sysfatal("execing sendmail: %r\n:");
  282. if(xflag || lbflag){
  283. close(fd);
  284. exits(waitforsubprocs());
  285. }
  286. if(Binit(&out, fd, OWRITE) < 0)
  287. fatal("can't Binit 1: %r");
  288. if(!nflag){
  289. if(Bwrite(&out, s_to_c(hdrstring), s_len(hdrstring)) != s_len(hdrstring))
  290. fatal("write error");
  291. s_free(hdrstring);
  292. hdrstring = nil;
  293. // read user's standard headers
  294. file = s_new();
  295. mboxpath("headers", user, file, 0);
  296. b = Bopen(s_to_c(file), OREAD);
  297. if(b != nil){
  298. switch(readheaders(b, &flags, &hdrstring, nil)){
  299. case Error: // error
  300. fatal("reading");
  301. }
  302. Bterm(b);
  303. if(Bwrite(&out, s_to_c(hdrstring), s_len(hdrstring)) != s_len(hdrstring))
  304. fatal("write error");
  305. s_free(hdrstring);
  306. hdrstring = nil;
  307. }
  308. }
  309. // add any headers we need
  310. if((flags & (1<<Hdate)) == 0)
  311. if(printdate(&out) < 0)
  312. fatal("writing");
  313. if((flags & (1<<Hfrom)) == 0)
  314. if(printfrom(&out) < 0)
  315. fatal("writing");
  316. if((flags & (1<<Hto)) == 0)
  317. if(printto(&out, to) < 0)
  318. fatal("writing");
  319. if((flags & (1<<Hcc)) == 0)
  320. if(printcc(&out, cc) < 0)
  321. fatal("writing");
  322. if((flags & (1<<Hsubject)) == 0 && subject != nil)
  323. if(printsubject(&out, subject) < 0)
  324. fatal("writing");
  325. if(replymsg != nil)
  326. if(printinreplyto(&out, replymsg) < 0)
  327. fatal("writing");
  328. Bprint(&out, "MIME-Version: 1.0\n");
  329. if(pgpflag){ // interpose pgp process between us and sendmail to handle body
  330. Bflush(&out);
  331. Bterm(&out);
  332. fd = pgpfilter(&pgppid, fd, pgpflag);
  333. if(Binit(&out, fd, OWRITE) < 0)
  334. fatal("can't Binit 1: %r");
  335. }
  336. // if attachments, stick in multipart headers
  337. boundary = nil;
  338. if(first != nil){
  339. boundary = mkboundary();
  340. Bprint(&out, "Content-Type: multipart/mixed;\n");
  341. Bprint(&out, "\tboundary=\"%s\"\n\n", boundary);
  342. Bprint(&out, "This is a multi-part message in MIME format.\n");
  343. Bprint(&out, "--%s\n", boundary);
  344. Bprint(&out, "Content-Disposition: inline\n");
  345. }
  346. if(!nflag){
  347. if(!noinput && headersrv == Ok){
  348. body(&in, &out, 1);
  349. }
  350. } else
  351. Bprint(&out, "\n");
  352. holdoff(holding);
  353. Bflush(&out);
  354. for(a = first; a != nil; a = a->next){
  355. if(lastchar != '\n')
  356. Bprint(&out, "\n");
  357. Bprint(&out, "--%s\n", boundary);
  358. attachment(a, &out);
  359. }
  360. if(first != nil)
  361. Bprint(&out, "--%s--\n", boundary);
  362. Bterm(&out);
  363. close(fd);
  364. exits(waitforsubprocs());
  365. }
  366. // evaluate pgp option string
  367. int
  368. pgpopts(char *s)
  369. {
  370. if(s == nil || s[0] == '\0')
  371. return -1;
  372. while(*s){
  373. switch(*s++){
  374. case 's': case 'S':
  375. pgpflag |= PGPsign;
  376. break;
  377. case 'e': case 'E':
  378. pgpflag |= PGPencrypt;
  379. break;
  380. default:
  381. return -1;
  382. }
  383. }
  384. return 0;
  385. }
  386. // read headers from stdin into a String, expanding local aliases,
  387. // keep track of which headers are there, which addresses we have
  388. // remove Bcc: line.
  389. int
  390. readheaders(Biobuf *in, int *fp, String **sp, Addr **top)
  391. {
  392. Addr *to;
  393. String *s, *sline;
  394. char *p;
  395. int i, seen, hdrtype;
  396. s = s_new();
  397. sline = nil;
  398. to = nil;
  399. hdrtype = -1;
  400. seen = 0;
  401. for(;;) {
  402. if((p = Brdline(in, '\n')) != nil) {
  403. seen = 1;
  404. p[Blinelen(in)-1] = 0;
  405. // coalesce multiline headers
  406. if((*p == ' ' || *p == '\t') && sline){
  407. s_append(sline, "\n");
  408. s_append(sline, p);
  409. p[Blinelen(in)-1] = '\n';
  410. continue;
  411. }
  412. }
  413. // process the current header, it's all been read
  414. if(sline) {
  415. assert(hdrtype != -1);
  416. if(top){
  417. switch(hdrtype){
  418. case Hto:
  419. case Hcc:
  420. case Hbcc:
  421. to = expandline(&sline, to);
  422. break;
  423. }
  424. }
  425. if(top==nil || hdrtype!=Hbcc){
  426. s_append(s, s_to_c(sline));
  427. s_append(s, "\n");
  428. }
  429. s_free(sline);
  430. sline = nil;
  431. }
  432. if(p == nil)
  433. break;
  434. // if no :, it's not a header, seek back and break
  435. if(strchr(p, ':') == nil){
  436. p[Blinelen(in)-1] = '\n';
  437. Bseek(in, -Blinelen(in), 1);
  438. break;
  439. }
  440. sline = s_copy(p);
  441. // classify the header. If we don't recognize it, break. This is
  442. // to take care of user's that start messages with lines that contain
  443. // ':'s but that aren't headers. This is a bit hokey. Since I decided
  444. // to let users type headers, I need some way to distinguish. Therefore,
  445. // marshal tries to know all likely headers and will indeed screw up if
  446. // the user types an unlikely one. -- presotto
  447. hdrtype = -1;
  448. for(i = 0; i < nelem(hdrs); i++){
  449. if(cistrncmp(hdrs[i], p, strlen(hdrs[i])) == 0){
  450. *fp |= 1<<i;
  451. hdrtype = i;
  452. break;
  453. }
  454. }
  455. if(hdrtype == -1){
  456. p[Blinelen(in)-1] = '\n';
  457. Bseek(in, -Blinelen(in), 1);
  458. break;
  459. }
  460. p[Blinelen(in)-1] = '\n';
  461. }
  462. *sp = s;
  463. if(top)
  464. *top = to;
  465. if(seen == 0){
  466. if(Blinelen(in) == 0)
  467. return Nomessage;
  468. else
  469. return Ok;
  470. }
  471. if(p == nil)
  472. return Nobody;
  473. return Ok;
  474. }
  475. // pass the body to sendmail, make sure body starts with a newline
  476. void
  477. body(Biobuf *in, Biobuf *out, int docontenttype)
  478. {
  479. char *buf, *p;
  480. int i, n, len;
  481. n = 0;
  482. len = 16*1024;
  483. buf = emalloc(len);
  484. // first char must be newline
  485. i = Bgetc(in);
  486. if(i > 0){
  487. if(i != '\n')
  488. buf[n++] = '\n';
  489. buf[n++] = i;
  490. } else {
  491. buf[n++] = '\n';
  492. }
  493. // read into memory
  494. if(docontenttype){
  495. while(docontenttype){
  496. if(n == len){
  497. len += len>>2;
  498. buf = realloc(buf, len);
  499. if(buf == nil)
  500. sysfatal("%r");
  501. }
  502. p = buf+n;
  503. i = Bread(in, p, len - n);
  504. if(i < 0)
  505. fatal("input error2");
  506. if(i == 0)
  507. break;
  508. n += i;
  509. for(; i > 0; i--)
  510. if(*p++ & 0x80 && docontenttype){
  511. Bprint(out, "Content-Type: text/plain; charset=\"UTF-8\"\n");
  512. Bprint(out, "Content-Transfer-Encoding: 8bit\n");
  513. docontenttype = 0;
  514. break;
  515. }
  516. }
  517. if(docontenttype){
  518. Bprint(out, "Content-Type: text/plain; charset=\"US-ASCII\"\n");
  519. Bprint(out, "Content-Transfer-Encoding: 7bit\n");
  520. }
  521. }
  522. // write what we already read
  523. if(Bwrite(out, buf, n) < 0)
  524. fatal("output error");
  525. if(n > 0)
  526. lastchar = buf[n-1];
  527. else
  528. lastchar = '\n';
  529. // pass the rest
  530. for(;;){
  531. n = Bread(in, buf, len);
  532. if(n < 0)
  533. fatal("input error2");
  534. if(n == 0)
  535. break;
  536. if(Bwrite(out, buf, n) < 0)
  537. fatal("output error");
  538. lastchar = buf[n-1];
  539. }
  540. }
  541. // pass the body to sendmail encoding with base64
  542. //
  543. // the size of buf is very important to enc64. Anything other than
  544. // a multiple of 3 will cause enc64 to output a termination sequence.
  545. // To ensure that a full buf corresponds to a multiple of complete lines,
  546. // we make buf a multiple of 3*18 since that's how many enc64 sticks on
  547. // a single line. This avoids short lines in the output which is pleasing
  548. // but not necessary.
  549. //
  550. void
  551. body64(Biobuf *in, Biobuf *out)
  552. {
  553. uchar buf[3*18*54];
  554. char obuf[3*18*54*2];
  555. int m, n;
  556. Bprint(out, "\n");
  557. for(;;){
  558. n = Bread(in, buf, sizeof(buf));
  559. if(n < 0)
  560. fatal("input error");
  561. if(n == 0)
  562. break;
  563. m = enc64(obuf, sizeof(obuf), buf, n);
  564. if(Bwrite(out, obuf, m) < 0)
  565. fatal("output error");
  566. }
  567. lastchar = '\n';
  568. }
  569. // pass message to sendmail, make sure body starts with a newline
  570. void
  571. copy(Biobuf *in, Biobuf *out)
  572. {
  573. char buf[4*1024];
  574. int n;
  575. for(;;){
  576. n = Bread(in, buf, sizeof(buf));
  577. if(n < 0)
  578. fatal("input error");
  579. if(n == 0)
  580. break;
  581. if(Bwrite(out, buf, n) < 0)
  582. fatal("output error");
  583. }
  584. }
  585. void
  586. attachment(Attach *a, Biobuf *out)
  587. {
  588. Biobuf *f;
  589. char *p;
  590. // if it's already mime encoded, just copy
  591. if(strcmp(a->type, "mime") == 0){
  592. f = Bopen(a->path, OREAD);
  593. if(f == nil){
  594. /* hack: give marshal time to stdin, before we kill it (for dead.letter) */
  595. sleep(500);
  596. postnote(PNPROC, pid, "interrupt");
  597. sysfatal("opening %s: %r", a->path);
  598. }
  599. copy(f, out);
  600. Bterm(f);
  601. }
  602. // if it's not already mime encoded ...
  603. if(strcmp(a->type, "text/plain") != 0)
  604. Bprint(out, "Content-Type: %s\n", a->type);
  605. if(a->inline){
  606. Bprint(out, "Content-Disposition: inline\n");
  607. } else {
  608. p = strrchr(a->path, '/');
  609. if(p == nil)
  610. p = a->path;
  611. else
  612. p++;
  613. Bprint(out, "Content-Disposition: attachment; filename=%Z\n", p);
  614. }
  615. f = Bopen(a->path, OREAD);
  616. if(f == nil){
  617. /* hack: give marshal time to stdin, before we kill it (for dead.letter) */
  618. sleep(500);
  619. postnote(PNPROC, pid, "interrupt");
  620. sysfatal("opening %s: %r", a->path);
  621. }
  622. /* dump our local 'From ' line when passing along mail messages */
  623. if(strcmp(a->type, "message/rfc822") == 0){
  624. p = Brdline(f, '\n');
  625. if(strncmp(p, "From ", 5) != 0)
  626. Bseek(f, 0, 0);
  627. }
  628. if(a->ctype->display){
  629. body(f, out, strcmp(a->type, "text/plain") == 0);
  630. } else {
  631. Bprint(out, "Content-Transfer-Encoding: base64\n");
  632. body64(f, out);
  633. }
  634. Bterm(f);
  635. }
  636. char *ascwday[] =
  637. {
  638. "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  639. };
  640. char *ascmon[] =
  641. {
  642. "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  643. "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  644. };
  645. int
  646. printdate(Biobuf *b)
  647. {
  648. Tm *tm;
  649. int tz;
  650. tm = localtime(time(0));
  651. tz = (tm->tzoff/3600)*100 + ((tm->tzoff/60)%60);
  652. return Bprint(b, "Date: %s, %d %s %d %2.2d:%2.2d:%2.2d %s%.4d\n",
  653. ascwday[tm->wday], tm->mday, ascmon[tm->mon], 1900+tm->year,
  654. tm->hour, tm->min, tm->sec, tz>0?"+":"", tz);
  655. }
  656. int
  657. printfrom(Biobuf *b)
  658. {
  659. return Bprint(b, "From: %s\n", user);
  660. }
  661. int
  662. printto(Biobuf *b, Addr *a)
  663. {
  664. if(Bprint(b, "To: %s", a->v) < 0)
  665. return -1;
  666. for(a = a->next; a != nil; a = a->next)
  667. if(Bprint(b, ", %s", a->v) < 0)
  668. return -1;
  669. if(Bprint(b, "\n") < 0)
  670. return -1;
  671. return 0;
  672. }
  673. int
  674. printcc(Biobuf *b, Addr *a)
  675. {
  676. if(a == nil)
  677. return 0;
  678. if(Bprint(b, "CC: %s", a->v) < 0)
  679. return -1;
  680. for(a = a->next; a != nil; a = a->next)
  681. if(Bprint(b, ", %s", a->v) < 0)
  682. return -1;
  683. if(Bprint(b, "\n") < 0)
  684. return -1;
  685. return 0;
  686. }
  687. int
  688. printsubject(Biobuf *b, char *subject)
  689. {
  690. return Bprint(b, "Subject: %s\n", subject);
  691. }
  692. int
  693. printinreplyto(Biobuf *out, char *dir)
  694. {
  695. String *s = s_copy(dir);
  696. char buf[256];
  697. int fd;
  698. int n;
  699. s_append(s, "/messageid");
  700. fd = open(s_to_c(s), OREAD);
  701. s_free(s);
  702. if(fd < 0)
  703. return 0;
  704. n = read(fd, buf, sizeof(buf)-1);
  705. close(fd);
  706. if(n <= 0)
  707. return 0;
  708. buf[n] = 0;
  709. return Bprint(out, "In-Reply-To: %s\n", buf);
  710. }
  711. Attach*
  712. mkattach(char *file, char *type, int inline)
  713. {
  714. Ctype *c;
  715. Attach *a;
  716. char ftype[64];
  717. char *p;
  718. int n, pfd[2];
  719. if(file == nil)
  720. return nil;
  721. a = emalloc(sizeof(*a));
  722. a->path = file;
  723. a->next = nil;
  724. a->type = type;
  725. a->inline = inline;
  726. a->ctype = nil;
  727. if(type != nil){
  728. for(c = ctype; ; c++)
  729. if(strncmp(type, c->type, strlen(c->type)) == 0){
  730. a->ctype = c;
  731. break;
  732. }
  733. return a;
  734. }
  735. // pick a type depending on extension
  736. p = strchr(file, '.');
  737. if(p != nil)
  738. p++;
  739. // check the builtin extensions
  740. if(p != nil){
  741. for(c = ctype; c->ext != nil; c++)
  742. if(strcmp(p, c->ext) == 0){
  743. a->type = c->type;
  744. a->ctype = c;
  745. return a;
  746. }
  747. }
  748. // try the mime types file
  749. if(p != nil){
  750. if(mimetypes == nil)
  751. readmimetypes();
  752. for(c = mimetypes; c != nil && c->ext != nil; c++)
  753. if(strcmp(p, c->ext) == 0){
  754. a->type = c->type;
  755. a->ctype = c;
  756. return a;
  757. }
  758. }
  759. // run file to figure out the type
  760. a->type = "application/octet-stream"; // safest default
  761. if(pipe(pfd) < 0)
  762. return a;
  763. switch(fork()){
  764. case -1:
  765. break;
  766. case 0:
  767. close(pfd[1]);
  768. close(0);
  769. dup(pfd[0], 0);
  770. close(1);
  771. dup(pfd[0], 1);
  772. execl("/bin/file", "file", "-m", file, 0);
  773. exits(0);
  774. default:
  775. close(pfd[0]);
  776. n = read(pfd[1], ftype, sizeof(ftype));
  777. if(n > 0){
  778. ftype[n-1] = 0;
  779. a->type = estrdup(ftype);
  780. }
  781. close(pfd[1]);
  782. waitpid();
  783. break;
  784. }
  785. for(c = ctype; ; c++)
  786. if(strncmp(a->type, c->type, strlen(c->type)) == 0){
  787. a->ctype = c;
  788. break;
  789. }
  790. return a;
  791. }
  792. char*
  793. mkboundary(void)
  794. {
  795. char buf[32];
  796. int i;
  797. srand((time(0)<<16)|getpid());
  798. strcpy(buf, "upas-");
  799. for(i = 5; i < sizeof(buf)-1; i++)
  800. buf[i] = 'a' + nrand(26);
  801. buf[i] = 0;
  802. return estrdup(buf);
  803. }
  804. // copy types to two fd's
  805. static void
  806. tee(int in, int out1, int out2)
  807. {
  808. char buf[8*1024];
  809. int n;
  810. for(;;){
  811. n = read(in, buf, sizeof(buf));
  812. if(n <= 0)
  813. break;
  814. if(write(out1, buf, n) < 0)
  815. break;
  816. if(write(out2, buf, n) < 0)
  817. break;
  818. }
  819. }
  820. // print the unix from line
  821. int
  822. printunixfrom(int fd)
  823. {
  824. Tm *tm;
  825. int tz;
  826. tm = localtime(time(0));
  827. tz = (tm->tzoff/3600)*100 + ((tm->tzoff/60)%60);
  828. return fprint(fd, "From %s %s %s %d %2.2d:%2.2d:%2.2d %s%.4d %d\n",
  829. user,
  830. ascwday[tm->wday], ascmon[tm->mon], tm->mday,
  831. tm->hour, tm->min, tm->sec, tz>0?"+":"", tz, 1900+tm->year);
  832. }
  833. char *specialfile[] =
  834. {
  835. "pipeto",
  836. "pipefrom",
  837. "L.mbox",
  838. "forward",
  839. "names"
  840. };
  841. // return 1 if this is a special file
  842. static int
  843. special(String *s)
  844. {
  845. char *p;
  846. int i;
  847. p = strrchr(s_to_c(s), '/');
  848. if(p == nil)
  849. p = s_to_c(s);
  850. else
  851. p++;
  852. for(i = 0; i < nelem(specialfile); i++)
  853. if(strcmp(p, specialfile[i]) == 0)
  854. return 1;
  855. return 0;
  856. }
  857. // open the folder using the recipients account name
  858. static int
  859. openfolder(char *rcvr)
  860. {
  861. char *p;
  862. int c;
  863. String *file;
  864. Dir *d;
  865. int fd;
  866. int scarey;
  867. file = s_new();
  868. mboxpath("f", user, file, 0);
  869. // if $mail/f exists, store there, otherwise in $mail
  870. d = dirstat(s_to_c(file));
  871. if(d == nil || d->qid.type != QTDIR){
  872. scarey = 1;
  873. file->ptr -= 1;
  874. } else {
  875. s_putc(file, '/');
  876. scarey = 0;
  877. }
  878. free(d);
  879. p = strrchr(rcvr, '!');
  880. if(p != nil)
  881. rcvr = p+1;
  882. while(*rcvr && *rcvr != '@'){
  883. c = *rcvr++;
  884. if(c == '/')
  885. c = '_';
  886. s_putc(file, c);
  887. }
  888. s_terminate(file);
  889. if(scarey && special(file)){
  890. fprint(2, "%s: won't overwrite %s\n", argv0, s_to_c(file));
  891. s_free(file);
  892. return -1;
  893. }
  894. fd = open(s_to_c(file), OWRITE);
  895. if(fd < 0)
  896. fd = create(s_to_c(file), OWRITE, 0660);
  897. s_free(file);
  898. return fd;
  899. }
  900. // start up sendmail and return an fd to talk to it with
  901. int
  902. sendmail(Addr *to, Addr *cc, int *pid, char *rcvr)
  903. {
  904. char **av, **v;
  905. int ac, fd;
  906. int pfd[2];
  907. String *cmd;
  908. Addr *a;
  909. fd = -1;
  910. if(rcvr != nil)
  911. fd = openfolder(rcvr);
  912. ac = 0;
  913. for(a = to; a != nil; a = a->next)
  914. ac++;
  915. for(a = cc; a != nil; a = a->next)
  916. ac++;
  917. v = av = emalloc(sizeof(char*)*(ac+8));
  918. ac = 0;
  919. v[ac++] = "sendmail";
  920. if(xflag)
  921. v[ac++] = "-x";
  922. if(rflag)
  923. v[ac++] = "-r";
  924. if(lbflag)
  925. v[ac++] = "-#";
  926. for(a = to; a != nil; a = a->next)
  927. v[ac++] = a->v;
  928. for(a = cc; a != nil; a = a->next)
  929. v[ac++] = a->v;
  930. v[ac] = 0;
  931. if(pipe(pfd) < 0)
  932. fatal("%r");
  933. switch(*pid = rfork(RFFDG|RFREND|RFPROC|RFENVG)){
  934. case -1:
  935. fatal("%r");
  936. break;
  937. case 0:
  938. if(holding)
  939. close(holding);
  940. close(pfd[1]);
  941. dup(pfd[0], 0);
  942. close(pfd[0]);
  943. if(rcvr != nil){
  944. if(pipe(pfd) < 0)
  945. fatal("%r");
  946. switch(fork()){
  947. case -1:
  948. fatal("%r");
  949. break;
  950. case 0:
  951. close(pfd[0]);
  952. seek(fd, 0, 2);
  953. printunixfrom(fd);
  954. tee(0, pfd[1], fd);
  955. write(fd, "\n", 1);
  956. exits(0);
  957. default:
  958. close(fd);
  959. close(pfd[1]);
  960. dup(pfd[0], 0);
  961. break;
  962. }
  963. }
  964. if(replymsg != nil)
  965. putenv("replymsg", replymsg);
  966. cmd = mboxpath("pipefrom", user, s_new(), 0);
  967. exec(s_to_c(cmd), av);
  968. exec("/bin/myupassend", av);
  969. exec("/bin/upas/send", av);
  970. fatal("execing: %r");
  971. break;
  972. default:
  973. if(rcvr != nil)
  974. close(fd);
  975. close(pfd[0]);
  976. break;
  977. }
  978. return pfd[1];
  979. }
  980. // start up pgp process and return an fd to talk to it with.
  981. // its standard output will be the original fd, which goes to sendmail.
  982. int
  983. pgpfilter(int *pid, int fd, int pgpflag)
  984. {
  985. char **av, **v;
  986. int ac;
  987. int pfd[2];
  988. v = av = emalloc(sizeof(char*)*8);
  989. ac = 0;
  990. v[ac++] = "pgp";
  991. if(pgpflag & PGPsign)
  992. v[ac++] = "-s";
  993. if(pgpflag & PGPencrypt)
  994. v[ac++] = "-e";
  995. v[ac] = 0;
  996. if(pipe(pfd) < 0)
  997. fatal("%r");
  998. switch(*pid = fork()){
  999. case -1:
  1000. fatal("%r");
  1001. break;
  1002. case 0:
  1003. close(pfd[1]);
  1004. dup(pfd[0], 0);
  1005. close(pfd[0]);
  1006. dup(fd, 1);
  1007. close(fd);
  1008. exec("/bin/upas/pgp", av);
  1009. fatal("execing: %r");
  1010. break;
  1011. default:
  1012. close(pfd[0]);
  1013. break;
  1014. }
  1015. close(fd);
  1016. return pfd[1];
  1017. }
  1018. // wait for sendmail and pgp to exit; exit here if either failed
  1019. char*
  1020. waitforsubprocs(void)
  1021. {
  1022. Waitmsg *w;
  1023. char *err;
  1024. err = nil;
  1025. while((w = wait()) != nil){
  1026. if(w->pid == pid || w->pid == pgppid){
  1027. if(w->msg[0] != 0)
  1028. err = estrdup(w->msg);
  1029. }
  1030. free(w);
  1031. }
  1032. if(err)
  1033. exits(err);
  1034. return nil;
  1035. }
  1036. int
  1037. cistrncmp(char *a, char *b, int n)
  1038. {
  1039. while(n-- > 0){
  1040. if(tolower(*a++) != tolower(*b++))
  1041. return -1;
  1042. }
  1043. return 0;
  1044. }
  1045. int
  1046. cistrcmp(char *a, char *b)
  1047. {
  1048. for(;;){
  1049. if(tolower(*a) != tolower(*b++))
  1050. return -1;
  1051. if(*a++ == 0)
  1052. break;
  1053. }
  1054. return 0;
  1055. }
  1056. static uchar t64d[256];
  1057. static char t64e[64];
  1058. static void
  1059. init64(void)
  1060. {
  1061. int c, i;
  1062. memset(t64d, 255, 256);
  1063. memset(t64e, '=', 64);
  1064. i = 0;
  1065. for(c = 'A'; c <= 'Z'; c++){
  1066. t64e[i] = c;
  1067. t64d[c] = i++;
  1068. }
  1069. for(c = 'a'; c <= 'z'; c++){
  1070. t64e[i] = c;
  1071. t64d[c] = i++;
  1072. }
  1073. for(c = '0'; c <= '9'; c++){
  1074. t64e[i] = c;
  1075. t64d[c] = i++;
  1076. }
  1077. t64e[i] = '+';
  1078. t64d['+'] = i++;
  1079. t64e[i] = '/';
  1080. t64d['/'] = i;
  1081. }
  1082. int
  1083. enc64(char *out, int lim, uchar *in, int n)
  1084. {
  1085. int i;
  1086. ulong b24;
  1087. char *start = out;
  1088. char *e = out + lim;
  1089. if(t64e[0] == 0)
  1090. init64();
  1091. for(i = 0; i < n/3; i++){
  1092. b24 = (*in++)<<16;
  1093. b24 |= (*in++)<<8;
  1094. b24 |= *in++;
  1095. if(out + 5 >= e)
  1096. goto exhausted;
  1097. *out++ = t64e[(b24>>18)];
  1098. *out++ = t64e[(b24>>12)&0x3f];
  1099. *out++ = t64e[(b24>>6)&0x3f];
  1100. *out++ = t64e[(b24)&0x3f];
  1101. if((i%18) == 17)
  1102. *out++ = '\n';
  1103. }
  1104. switch(n%3){
  1105. case 2:
  1106. b24 = (*in++)<<16;
  1107. b24 |= (*in)<<8;
  1108. if(out + 4 >= e)
  1109. goto exhausted;
  1110. *out++ = t64e[(b24>>18)];
  1111. *out++ = t64e[(b24>>12)&0x3f];
  1112. *out++ = t64e[(b24>>6)&0x3f];
  1113. break;
  1114. case 1:
  1115. b24 = (*in)<<16;
  1116. if(out + 4 >= e)
  1117. goto exhausted;
  1118. *out++ = t64e[(b24>>18)];
  1119. *out++ = t64e[(b24>>12)&0x3f];
  1120. *out++ = '=';
  1121. break;
  1122. case 0:
  1123. if((i%18) != 0)
  1124. *out++ = '\n';
  1125. *out = 0;
  1126. return out - start;
  1127. }
  1128. exhausted:
  1129. *out++ = '=';
  1130. *out++ = '\n';
  1131. *out = 0;
  1132. return out - start;
  1133. }
  1134. void
  1135. freealias(Alias *a)
  1136. {
  1137. freeaddrs(a->addr);
  1138. free(a);
  1139. }
  1140. void
  1141. freealiases(Alias *a)
  1142. {
  1143. Alias *next;
  1144. while(a != nil){
  1145. next = a->next;
  1146. freealias(a);
  1147. a = next;
  1148. }
  1149. }
  1150. //
  1151. // read alias file
  1152. //
  1153. Alias*
  1154. readaliases(void)
  1155. {
  1156. Alias *a, **l, *first;
  1157. Addr *addr, **al;
  1158. String *file, *line, *token;
  1159. Biobuf *fp;
  1160. static int already;
  1161. first = nil;
  1162. file = s_new();
  1163. line = s_new();
  1164. token = s_new();
  1165. // open and get length
  1166. mboxpath("names", login, file, 0);
  1167. fp = Bopen(s_to_c(file), OREAD);
  1168. if(fp == nil)
  1169. goto out;
  1170. l = &first;
  1171. // read a line at a time.
  1172. while(s_getline(fp, s_restart(line))!=0) {
  1173. s_restart(line);
  1174. a = emalloc(sizeof(Alias));
  1175. al = &a->addr;
  1176. for(;;){
  1177. if(s_parse(line, s_restart(token))==0)
  1178. break;
  1179. addr = emalloc(sizeof(Addr));
  1180. addr->v = strdup(s_to_c(token));
  1181. addr->next = 0;
  1182. *al = addr;
  1183. al = &addr->next;
  1184. }
  1185. if(a->addr == nil || a->addr->next == nil){
  1186. freealias(a);
  1187. continue;
  1188. }
  1189. a->next = nil;
  1190. *l = a;
  1191. l = &a->next;
  1192. }
  1193. out:
  1194. s_free(file);
  1195. s_free(line);
  1196. s_free(token);
  1197. return first;
  1198. }
  1199. Addr*
  1200. newaddr(char *name)
  1201. {
  1202. Addr *a;
  1203. a = emalloc(sizeof(*a));
  1204. a->next = nil;
  1205. a->v = estrdup(name);
  1206. if(a->v == nil)
  1207. sysfatal("%r");
  1208. return a;
  1209. }
  1210. //
  1211. // expand personal aliases since the names are meaningless in
  1212. // other contexts
  1213. //
  1214. Addr*
  1215. _expand(Addr *old, int *changedp)
  1216. {
  1217. Alias *al;
  1218. Addr *first, *next, **l, *a;
  1219. *changedp = 0;
  1220. first = nil;
  1221. l = &first;
  1222. for(;old != nil; old = next){
  1223. next = old->next;
  1224. for(al = aliases; al != nil; al = al->next){
  1225. if(strcmp(al->addr->v, old->v) == 0){
  1226. for(a = al->addr->next; a != nil; a = a->next){
  1227. *l = newaddr(a->v);
  1228. if(*l == nil)
  1229. sysfatal("%r");
  1230. l = &(*l)->next;
  1231. *changedp = 1;
  1232. }
  1233. break;
  1234. }
  1235. }
  1236. if(al != nil){
  1237. freeaddr(old);
  1238. continue;
  1239. }
  1240. *l = old;
  1241. old->next = nil;
  1242. l = &(*l)->next;
  1243. }
  1244. return first;
  1245. }
  1246. Addr*
  1247. rexpand(Addr *old)
  1248. {
  1249. int i, changed;
  1250. changed = 0;
  1251. for(i=0; i<32; i++){
  1252. old = _expand(old, &changed);
  1253. if(changed == 0)
  1254. break;
  1255. }
  1256. return old;
  1257. }
  1258. Addr*
  1259. unique(Addr *first)
  1260. {
  1261. Addr *a, **l, *x;
  1262. for(a = first; a != nil; a = a->next){
  1263. for(l = &a->next; *l != nil;){
  1264. if(strcmp(a->v, (*l)->v) == 0){
  1265. x = *l;
  1266. *l = x->next;
  1267. freeaddr(x);
  1268. } else
  1269. l = &(*l)->next;
  1270. }
  1271. }
  1272. return first;
  1273. }
  1274. Addr*
  1275. expand(int ac, char **av)
  1276. {
  1277. Addr *first, **l;
  1278. int i;
  1279. first = nil;
  1280. // make a list of the starting addresses
  1281. l = &first;
  1282. for(i = 0; i < ac; i++){
  1283. *l = newaddr(av[i]);
  1284. if(*l == nil)
  1285. sysfatal("%r");
  1286. l = &(*l)->next;
  1287. }
  1288. // recurse till we don't change any more
  1289. return unique(rexpand(first));
  1290. }
  1291. Addr*
  1292. concataddr(Addr *a, Addr *b)
  1293. {
  1294. Addr *oa;
  1295. if(a == nil)
  1296. return b;
  1297. oa = a;
  1298. for(; a->next; a=a->next)
  1299. ;
  1300. a->next = b;
  1301. return oa;
  1302. }
  1303. void
  1304. freeaddr(Addr *ap)
  1305. {
  1306. free(ap->v);
  1307. free(ap);
  1308. }
  1309. void
  1310. freeaddrs(Addr *ap)
  1311. {
  1312. Addr *next;
  1313. for(; ap; ap=next) {
  1314. next = ap->next;
  1315. freeaddr(ap);
  1316. }
  1317. }
  1318. String*
  1319. s_copyn(char *s, int n)
  1320. {
  1321. return s_nappend(s_reset(nil), s, n);
  1322. }
  1323. // fetch the next token from an RFC822 address string
  1324. // we assume the header is RFC822-conformant in that
  1325. // we recognize escaping anywhere even though it is only
  1326. // supposed to be in quoted-strings, domain-literals, and comments.
  1327. //
  1328. // i'd use yylex or yyparse here, but we need to preserve
  1329. // things like comments, which i think it tosses away.
  1330. //
  1331. // we're not strictly RFC822 compliant. we misparse such nonsense as
  1332. //
  1333. // To: gre @ (Grace) plan9 . (Emlin) bell-labs.com
  1334. //
  1335. // make sure there's no whitespace in your addresses and
  1336. // you'll be fine.
  1337. //
  1338. enum {
  1339. Twhite,
  1340. Tcomment,
  1341. Twords,
  1342. Tcomma,
  1343. Tleftangle,
  1344. Trightangle,
  1345. Terror,
  1346. Tend,
  1347. };
  1348. //char *ty82[] = {"white", "comment", "words", "comma", "<", ">", "err", "end"};
  1349. #define ISWHITE(p) ((p)==' ' || (p)=='\t' || (p)=='\n' || (p)=='\r')
  1350. int
  1351. get822token(String **tok, char *p, char **pp)
  1352. {
  1353. char *op;
  1354. int type;
  1355. int quoting;
  1356. op = p;
  1357. switch(*p){
  1358. case '\0':
  1359. *tok = nil;
  1360. *pp = nil;
  1361. return Tend;
  1362. case ' ': // get whitespace
  1363. case '\t':
  1364. case '\n':
  1365. case '\r':
  1366. type = Twhite;
  1367. while(ISWHITE(*p))
  1368. p++;
  1369. break;
  1370. case '(': // get comment
  1371. type = Tcomment;
  1372. for(p++; *p && *p != ')'; p++)
  1373. if(*p == '\\') {
  1374. if(*(p+1) == '\0') {
  1375. *tok = nil;
  1376. return Terror;
  1377. }
  1378. p++;
  1379. }
  1380. if(*p != ')') {
  1381. *tok = nil;
  1382. return Terror;
  1383. }
  1384. p++;
  1385. break;
  1386. case ',':
  1387. type = Tcomma;
  1388. p++;
  1389. break;
  1390. case '<':
  1391. type = Tleftangle;
  1392. p++;
  1393. break;
  1394. case '>':
  1395. type = Trightangle;
  1396. p++;
  1397. break;
  1398. default: // bunch of letters, perhaps quoted strings tossed in
  1399. type = Twords;
  1400. quoting = 0;
  1401. for(; *p && (quoting || (!ISWHITE(*p) && *p != '>' && *p != '<' && *p != ',')); p++) {
  1402. if(*p == '"')
  1403. quoting = !quoting;
  1404. if(*p == '\\') {
  1405. if(*(p+1) == '\0') {
  1406. *tok = nil;
  1407. return Terror;
  1408. }
  1409. p++;
  1410. }
  1411. }
  1412. break;
  1413. }
  1414. if(pp)
  1415. *pp = p;
  1416. *tok = s_copyn(op, p-op);
  1417. return type;
  1418. }
  1419. // expand local aliases in an RFC822 mail line
  1420. // add list of expanded addresses to to.
  1421. Addr*
  1422. expandline(String **s, Addr *to)
  1423. {
  1424. Addr *na, *nto, *ap;
  1425. char *p;
  1426. int tok, inangle, hadangle, nword;
  1427. String *os, *ns, *stok, *lastword, *sinceword;
  1428. os = s_copy(s_to_c(*s));
  1429. p = strchr(s_to_c(*s), ':');
  1430. assert(p != nil);
  1431. p++;
  1432. ns = s_copyn(s_to_c(*s), p-s_to_c(*s));
  1433. stok = nil;
  1434. nto = nil;
  1435. //
  1436. // the only valid mailbox namings are word
  1437. // and word* < addr >
  1438. // without comments this would be simple.
  1439. // we keep the following:
  1440. // lastword - current guess at the address
  1441. // sinceword - whitespace and comment seen since lastword
  1442. //
  1443. lastword = s_new();
  1444. sinceword = s_new();
  1445. inangle = 0;
  1446. nword = 0;
  1447. hadangle = 0;
  1448. for(;;) {
  1449. stok = nil;
  1450. switch(tok = get822token(&stok, p, &p)){
  1451. default:
  1452. abort();
  1453. case Tcomma:
  1454. case Tend:
  1455. if(inangle)
  1456. goto Error;
  1457. if(nword != 1)
  1458. goto Error;
  1459. na = rexpand(newaddr(s_to_c(lastword)));
  1460. s_append(ns, na->v);
  1461. s_append(ns, s_to_c(sinceword));
  1462. for(ap=na->next; ap; ap=ap->next) {
  1463. s_append(ns, ", ");
  1464. s_append(ns, ap->v);
  1465. }
  1466. nto = concataddr(na, nto);
  1467. if(tok == Tcomma){
  1468. s_append(ns, ",");
  1469. s_free(stok);
  1470. }
  1471. if(tok == Tend)
  1472. goto Break2;
  1473. inangle = 0;
  1474. nword = 0;
  1475. hadangle = 0;
  1476. s_reset(sinceword);
  1477. s_reset(lastword);
  1478. break;
  1479. case Twhite:
  1480. case Tcomment:
  1481. s_append(sinceword, s_to_c(stok));
  1482. s_free(stok);
  1483. break;
  1484. case Trightangle:
  1485. if(!inangle)
  1486. goto Error;
  1487. inangle = 0;
  1488. hadangle = 1;
  1489. s_append(sinceword, s_to_c(stok));
  1490. s_free(stok);
  1491. break;
  1492. case Twords:
  1493. case Tleftangle:
  1494. if(hadangle)
  1495. goto Error;
  1496. if(tok != Tleftangle && inangle && s_len(lastword))
  1497. goto Error;
  1498. if(tok == Tleftangle) {
  1499. inangle = 1;
  1500. nword = 1;
  1501. }
  1502. s_append(ns, s_to_c(lastword));
  1503. s_append(ns, s_to_c(sinceword));
  1504. s_reset(sinceword);
  1505. if(tok == Tleftangle) {
  1506. s_append(ns, "<");
  1507. s_reset(lastword);
  1508. } else {
  1509. s_free(lastword);
  1510. lastword = stok;
  1511. }
  1512. if(!inangle)
  1513. nword++;
  1514. break;
  1515. case Terror: // give up, use old string, addrs
  1516. Error:
  1517. ns = os;
  1518. os = nil;
  1519. freeaddrs(nto);
  1520. nto = nil;
  1521. werrstr("rfc822 syntax error");
  1522. rfc822syntaxerror = 1;
  1523. goto Break2;
  1524. }
  1525. }
  1526. Break2:
  1527. s_free(*s);
  1528. s_free(os);
  1529. *s = ns;
  1530. nto = concataddr(nto, to);
  1531. return nto;
  1532. }
  1533. void
  1534. Bdrain(Biobuf *b)
  1535. {
  1536. char buf[8192];
  1537. while(Bread(b, buf, sizeof buf) > 0)
  1538. ;
  1539. }
  1540. void
  1541. readmimetypes(void)
  1542. {
  1543. Biobuf *b;
  1544. char *p;
  1545. char *f[6];
  1546. char type[256];
  1547. static int alloced, inuse;
  1548. if(mimetypes == 0){
  1549. alloced = 256;
  1550. mimetypes = emalloc(alloced*sizeof(Ctype));
  1551. mimetypes[0].ext = "";
  1552. }
  1553. b = Bopen("/sys/lib/mimetype", OREAD);
  1554. if(b == nil)
  1555. return;
  1556. for(;;){
  1557. p = Brdline(b, '\n');
  1558. if(p == nil)
  1559. break;
  1560. p[Blinelen(b)-1] = 0;
  1561. if(tokenize(p, f, 6) < 4)
  1562. continue;
  1563. if(strcmp(f[0], "-") == 0 || strcmp(f[1], "-") == 0 || strcmp(f[2], "-") == 0)
  1564. continue;
  1565. if(inuse + 1 >= alloced){
  1566. alloced += 256;
  1567. mimetypes = erealloc(mimetypes, alloced*sizeof(Ctype));
  1568. }
  1569. snprint(type, sizeof(type), "%s/%s", f[1], f[2]);
  1570. mimetypes[inuse].type = estrdup(type);
  1571. mimetypes[inuse].ext = estrdup(f[0]+1);
  1572. mimetypes[inuse].display = !strcmp(type, "text/plain");
  1573. inuse++;
  1574. // always make sure there's a terminator
  1575. mimetypes[inuse].ext = 0;
  1576. }
  1577. Bterm(b);
  1578. }
  1579. char*
  1580. estrdup(char *x)
  1581. {
  1582. x = strdup(x);
  1583. if(x == nil)
  1584. fatal("memory");
  1585. return x;
  1586. }
  1587. void*
  1588. emalloc(int n)
  1589. {
  1590. void *x;
  1591. x = malloc(n);
  1592. if(x == nil)
  1593. fatal("%r");
  1594. return x;
  1595. }
  1596. void*
  1597. erealloc(void *x, int n)
  1598. {
  1599. x = realloc(x, n);
  1600. if(x == nil)
  1601. fatal("%r");
  1602. return x;
  1603. }
  1604. //
  1605. // Formatter for %"
  1606. // Use double quotes to protect white space, frogs, \ and "
  1607. //
  1608. enum
  1609. {
  1610. Qok = 0,
  1611. Qquote,
  1612. Qbackslash,
  1613. };
  1614. static int
  1615. needtoquote(Rune r)
  1616. {
  1617. if(r >= Runeself)
  1618. return Qquote;
  1619. if(r <= ' ')
  1620. return Qquote;
  1621. if(r=='\\' || r=='"')
  1622. return Qbackslash;
  1623. return Qok;
  1624. }
  1625. int
  1626. doublequote(Fmt *f)
  1627. {
  1628. char *s, *t;
  1629. int w, quotes;
  1630. Rune r;
  1631. s = va_arg(f->args, char*);
  1632. if(s == nil || *s == '\0')
  1633. return fmtstrcpy(f, "\"\"");
  1634. quotes = 0;
  1635. for(t=s; *t; t+=w){
  1636. w = chartorune(&r, t);
  1637. quotes |= needtoquote(r);
  1638. }
  1639. if(quotes == 0)
  1640. return fmtstrcpy(f, s);
  1641. fmtrune(f, '"');
  1642. for(t=s; *t; t+=w){
  1643. w = chartorune(&r, t);
  1644. if(needtoquote(r) == Qbackslash)
  1645. fmtrune(f, '\\');
  1646. fmtrune(f, r);
  1647. }
  1648. return fmtrune(f, '"');
  1649. }