msg.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780
  1. #include <u.h>
  2. #include <libc.h>
  3. #include <bio.h>
  4. #include <libsec.h>
  5. #include <auth.h>
  6. #include <fcall.h>
  7. #include "imap4d.h"
  8. static void body64(int in, int out);
  9. static void bodystrip(int in, int out);
  10. static void cleanupHeader(Header *h);
  11. static char *domBang(char *s);
  12. static void freeMAddr(MAddr *a);
  13. static void freeMimeHdr(MimeHdr *mh);
  14. static char *headAddrSpec(char *e, char *w);
  15. static MAddr *headAddresses(void);
  16. static MAddr *headAddress(void);
  17. static char *headAtom(char *disallowed);
  18. static int headChar(int eat);
  19. static char *headDomain(char *e);
  20. static MAddr *headMAddr(MAddr *old);
  21. static char *headPhrase(char *e, char *w);
  22. static char *headQuoted(int start, int stop);
  23. static char *headSkipWhite(int);
  24. static void headSkip(void);
  25. static char *headSubDomain(void);
  26. static char *headText(void);
  27. static void headToEnd(void);
  28. static char *headWord(void);
  29. static void mimeDescription(Header *h);
  30. static void mimeDisposition(Header *h);
  31. static void mimeEncoding(Header *h);
  32. static void mimeId(Header *h);
  33. static void mimeLanguage(Header *h);
  34. static void mimeMd5(Header *h);
  35. static MimeHdr *mimeParams(void);
  36. static void mimeType(Header *h);
  37. static MimeHdr *mkMimeHdr(char *s, char *t, MimeHdr *next);
  38. static void msgAddDate(Msg *m);
  39. static void msgAddHead(Msg *m, char *head, char *body);
  40. static int msgBodySize(Msg *m);
  41. static int msgHeader(Msg *m, Header *h, char *file);
  42. static long msgReadFile(Msg *m, char *file, char **ss);
  43. static int msgUnix(Msg *m, int top);
  44. static void stripQuotes(char *q);
  45. static MAddr *unixFrom(char *s);
  46. static char bogusBody[] =
  47. "This message contains null characters, so it cannot be displayed correctly.\r\n"
  48. "Most likely you were sent a bogus message or a binary file.\r\n"
  49. "\r\n"
  50. "Each of the following attachments has a different version of the message.\r\n"
  51. "The first is inlined with all non-printable characters stripped.\r\n"
  52. "The second contains the message as it was stored in your mailbox.\r\n"
  53. "The third has the initial header stripped.\r\n";
  54. static char bogusMimeText[] =
  55. "Content-Disposition: inline\r\n"
  56. "Content-Type: text/plain; charset=\"US-ASCII\"\r\n"
  57. "Content-Transfer-Encoding: 7bit\r\n";
  58. static char bogusMimeBinary[] =
  59. "Content-Disposition: attachment\r\n"
  60. "Content-Type: application/octet-stream\r\n"
  61. "Content-Transfer-Encoding: base64\r\n";
  62. /*
  63. * stop list for header fields
  64. */
  65. static char *headFieldStop = ":";
  66. static char *mimeTokenStop = "()<>@,;:\\\"/[]?=";
  67. static char *headAtomStop = "()<>@,;:\\\".[]";
  68. static uchar *headStr;
  69. static uchar *lastWhite;
  70. long
  71. selectFields(char *dst, long n, char *hdr, SList *fields, int matches)
  72. {
  73. SList *f;
  74. uchar *start;
  75. char *s;
  76. long m, nf;
  77. headStr = (uchar*)hdr;
  78. m = 0;
  79. for(;;){
  80. start = headStr;
  81. s = headAtom(headFieldStop);
  82. if(s == nil)
  83. break;
  84. headSkip();
  85. for(f = fields; f != nil; f = f->next){
  86. if(cistrcmp(s, f->s) == !matches){
  87. nf = headStr - start;
  88. if(m + nf > n)
  89. return 0;
  90. memmove(&dst[m], start, nf);
  91. m += nf;
  92. }
  93. }
  94. free(s);
  95. }
  96. if(m + 3 > n)
  97. return 0;
  98. dst[m++] = '\r';
  99. dst[m++] = '\n';
  100. dst[m] = '\0';
  101. return m;
  102. }
  103. void
  104. freeMsg(Msg *m)
  105. {
  106. Msg *k, *last;
  107. free(m->iBuf);
  108. freeMAddr(m->to);
  109. if(m->replyTo != m->from)
  110. freeMAddr(m->replyTo);
  111. if(m->sender != m->from)
  112. freeMAddr(m->sender);
  113. if(m->from != m->unixFrom)
  114. freeMAddr(m->from);
  115. freeMAddr(m->unixFrom);
  116. freeMAddr(m->cc);
  117. freeMAddr(m->bcc);
  118. free(m->unixDate);
  119. cleanupHeader(&m->head);
  120. cleanupHeader(&m->mime);
  121. for(k = m->kids; k != nil; ){
  122. last = k;
  123. k = k->next;
  124. freeMsg(last);
  125. }
  126. free(m);
  127. }
  128. ulong
  129. msgSize(Msg *m)
  130. {
  131. return m->head.size + m->size;
  132. }
  133. int
  134. infoIsNil(char *s)
  135. {
  136. return s == nil || s[0] == '\0';
  137. }
  138. char*
  139. maddrStr(MAddr *a)
  140. {
  141. char *host, *addr;
  142. int n;
  143. host = a->host;
  144. if(host == nil)
  145. host = "";
  146. n = strlen(a->box) + strlen(host) + 2;
  147. if(a->personal != nil)
  148. n += strlen(a->personal) + 3;
  149. addr = emalloc(n);
  150. if(a->personal != nil)
  151. snprint(addr, n, "%s <%s@%s>", a->personal, a->box, host);
  152. else
  153. snprint(addr, n, "%s@%s", a->box, host);
  154. return addr;
  155. }
  156. /*
  157. * return actual name of f in m's fs directory
  158. * this is special cased when opening m/rawbody, m/mimeheader, or m/rawheader,
  159. * if the message was corrupted. in that case,
  160. * a temporary file is made to hold the base64 encoding of m/raw.
  161. */
  162. int
  163. msgFile(Msg *m, char *f)
  164. {
  165. Msg *parent, *p;
  166. Dir d;
  167. Tm tm;
  168. char buf[64], nbuf[2];
  169. uchar dbuf[64];
  170. int i, n, fd, fd1, fd2;
  171. if(!m->bogus
  172. || strcmp(f, "") != 0 && strcmp(f, "rawbody") != 0
  173. && strcmp(f, "rawheader") != 0 && strcmp(f, "mimeheader") != 0
  174. && strcmp(f, "info") != 0 && strcmp(f, "unixheader") != 0){
  175. if(strlen(f) > MsgNameLen)
  176. bye("internal error: msgFile name too long");
  177. strcpy(m->efs, f);
  178. return cdOpen(m->fsDir, m->fs, OREAD);
  179. }
  180. /*
  181. * walk up the stupid runt message parts for non-multipart messages
  182. */
  183. parent = m->parent;
  184. if(parent != nil && parent->parent != nil){
  185. m = parent;
  186. parent = m->parent;
  187. }
  188. p = m;
  189. if(parent != nil)
  190. p = parent;
  191. if(strcmp(f, "info") == 0 || strcmp(f, "unixheader") == 0){
  192. strcpy(p->efs, f);
  193. return cdOpen(p->fsDir, p->fs, OREAD);
  194. }
  195. fd = imapTmp();
  196. if(fd < 0)
  197. return -1;
  198. /*
  199. * craft the message parts for bogus messages
  200. */
  201. if(strcmp(f, "") == 0){
  202. /*
  203. * make a fake directory for each kid
  204. * all we care about is the name
  205. */
  206. if(parent == nil){
  207. nulldir(&d);
  208. d.mode = DMDIR|0600;
  209. d.qid.type = QTDIR;
  210. d.name = nbuf;
  211. nbuf[1] = '\0';
  212. for(i = '1'; i <= '4'; i++){
  213. nbuf[0] = i;
  214. n = convD2M(&d, dbuf, sizeof(dbuf));
  215. if(n <= BIT16SZ)
  216. fprint(2, "bad convD2M %d\n", n);
  217. write(fd, dbuf, n);
  218. }
  219. }
  220. }else if(strcmp(f, "mimeheader") == 0){
  221. if(parent != nil){
  222. switch(m->id){
  223. case 1:
  224. case 2:
  225. fprint(fd, "%s", bogusMimeText);
  226. break;
  227. case 3:
  228. case 4:
  229. fprint(fd, "%s", bogusMimeBinary);
  230. break;
  231. }
  232. }
  233. }else if(strcmp(f, "rawheader") == 0){
  234. if(parent == nil){
  235. date2tm(&tm, m->unixDate);
  236. rfc822date(buf, sizeof(buf), &tm);
  237. fprint(fd,
  238. "Date: %s\r\n"
  239. "From: imap4 daemon <%s@%s>\r\n"
  240. "To: <%s@%s>\r\n"
  241. "Subject: This message was illegal or corrupted\r\n"
  242. "MIME-Version: 1.0\r\n"
  243. "Content-Type: multipart/mixed;\r\n\tboundary=\"upas-%s\"\r\n",
  244. buf, username, site, username, site, m->info[IDigest]);
  245. }
  246. }else if(strcmp(f, "rawbody") == 0){
  247. fd1 = msgFile(p, "raw");
  248. strcpy(p->efs, "rawbody");
  249. fd2 = cdOpen(p->fsDir, p->fs, OREAD);
  250. if(fd1 < 0 || fd2 < 0){
  251. close(fd);
  252. close(fd1);
  253. close(fd2);
  254. return -1;
  255. }
  256. if(parent == nil){
  257. fprint(fd,
  258. "This is a multi-part message in MIME format.\r\n"
  259. "--upas-%s\r\n"
  260. "%s"
  261. "\r\n"
  262. "%s"
  263. "\r\n",
  264. m->info[IDigest], bogusMimeText, bogusBody);
  265. fprint(fd,
  266. "--upas-%s\r\n"
  267. "%s"
  268. "\r\n",
  269. m->info[IDigest], bogusMimeText);
  270. bodystrip(fd1, fd);
  271. fprint(fd,
  272. "--upas-%s\r\n"
  273. "%s"
  274. "\r\n",
  275. m->info[IDigest], bogusMimeBinary);
  276. seek(fd1, 0, 0);
  277. body64(fd1, fd);
  278. fprint(fd,
  279. "--upas-%s\r\n"
  280. "%s"
  281. "\r\n",
  282. m->info[IDigest], bogusMimeBinary);
  283. body64(fd2, fd);
  284. fprint(fd, "--upas-%s--\r\n", m->info[IDigest]);
  285. }else{
  286. switch(m->id){
  287. case 1:
  288. fprint(fd, "%s", bogusBody);
  289. break;
  290. case 2:
  291. bodystrip(fd1, fd);
  292. break;
  293. case 3:
  294. body64(fd1, fd);
  295. break;
  296. case 4:
  297. body64(fd2, fd);
  298. break;
  299. }
  300. }
  301. close(fd1);
  302. close(fd2);
  303. }
  304. seek(fd, 0, 0);
  305. return fd;
  306. }
  307. int
  308. msgIsMulti(Header *h)
  309. {
  310. return h->type != nil && cistrcmp("multipart", h->type->s) == 0;
  311. }
  312. int
  313. msgIsRfc822(Header *h)
  314. {
  315. return h->type != nil && cistrcmp("message", h->type->s) == 0 && cistrcmp("rfc822", h->type->t) == 0;
  316. }
  317. /*
  318. * check if a message has been deleted by someone else
  319. */
  320. void
  321. msgDead(Msg *m)
  322. {
  323. if(m->expunged)
  324. return;
  325. *m->efs = '\0';
  326. if(!cdExists(m->fsDir, m->fs))
  327. m->expunged = 1;
  328. }
  329. /*
  330. * make sure the message has valid associated info
  331. * used for ISubject, IDigest, IInReplyTo, IMessageId.
  332. */
  333. int
  334. msgInfo(Msg *m)
  335. {
  336. char *s;
  337. int i;
  338. if(m->info[0] != nil)
  339. return 1;
  340. i = msgReadFile(m, "info", &m->iBuf);
  341. if(i < 0)
  342. return 0;
  343. s = m->iBuf;
  344. for(i = 0; i < IMax; i++){
  345. m->info[i] = s;
  346. s = strchr(s, '\n');
  347. if(s == nil)
  348. break;
  349. *s++ = '\0';
  350. }
  351. for(; i < IMax; i++)
  352. m->info[i] = nil;
  353. for(i = 0; i < IMax; i++)
  354. if(infoIsNil(m->info[i]))
  355. m->info[i] = nil;
  356. return 1;
  357. }
  358. /*
  359. * make sure the message has valid mime structure
  360. * and sub-messages
  361. */
  362. int
  363. msgStruct(Msg *m, int top)
  364. {
  365. Msg *k, head, *last;
  366. Dir *d;
  367. char *s;
  368. ulong max, id;
  369. int i, nd, fd, ns;
  370. if(m->kids != nil)
  371. return 1;
  372. if(m->expunged
  373. || !msgInfo(m)
  374. || !msgUnix(m, top)
  375. || !msgBodySize(m)
  376. || !msgHeader(m, &m->mime, "mimeheader")
  377. || (top || msgIsRfc822(&m->mime) || msgIsMulti(&m->mime)) && !msgHeader(m, &m->head, "rawheader")){
  378. if(top && m->bogus && !(m->bogus & BogusTried)){
  379. m->bogus |= BogusTried;
  380. return msgStruct(m, top);
  381. }
  382. msgDead(m);
  383. return 0;
  384. }
  385. /*
  386. * if a message has no kids, it has a kid which is just the body of the real message
  387. */
  388. if(!msgIsMulti(&m->head) && !msgIsMulti(&m->mime) && !msgIsRfc822(&m->head) && !msgIsRfc822(&m->mime)){
  389. k = MKZ(Msg);
  390. k->id = 1;
  391. k->fsDir = m->fsDir;
  392. k->bogus = m->bogus;
  393. k->parent = m->parent;
  394. ns = m->efs - m->fs;
  395. k->fs = emalloc(ns + (MsgNameLen + 1));
  396. memmove(k->fs, m->fs, ns);
  397. k->efs = k->fs + ns;
  398. *k->efs = '\0';
  399. k->size = m->size;
  400. m->kids = k;
  401. return 1;
  402. }
  403. /*
  404. * read in all child messages messages
  405. */
  406. fd = msgFile(m, "");
  407. if(fd < 0){
  408. msgDead(m);
  409. return 0;
  410. }
  411. max = 0;
  412. head.next = nil;
  413. last = &head;
  414. while((nd = dirread(fd, &d)) > 0){
  415. for(i = 0; i < nd; i++){
  416. s = d[i].name;
  417. id = strtol(s, &s, 10);
  418. if(id <= max || *s != '\0'
  419. || (d[i].mode & DMDIR) != DMDIR)
  420. continue;
  421. max = id;
  422. k = MKZ(Msg);
  423. k->id = id;
  424. k->fsDir = m->fsDir;
  425. k->bogus = m->bogus;
  426. k->parent = m;
  427. ns = strlen(m->fs);
  428. k->fs = emalloc(ns + 2 * (MsgNameLen + 1));
  429. k->efs = seprint(k->fs, k->fs + ns + (MsgNameLen + 1), "%s%lud/", m->fs, id);
  430. k->prev = last;
  431. k->size = ~0UL;
  432. k->lines = ~0UL;
  433. last->next = k;
  434. last = k;
  435. }
  436. }
  437. close(fd);
  438. m->kids = head.next;
  439. /*
  440. * if kids fail, just whack them
  441. */
  442. top = top && (msgIsRfc822(&m->head) || msgIsMulti(&m->head));
  443. for(k = m->kids; k != nil; k = k->next){
  444. if(!msgStruct(k, top)){
  445. for(k = m->kids; k != nil; ){
  446. last = k;
  447. k = k->next;
  448. freeMsg(last);
  449. }
  450. m->kids = nil;
  451. break;
  452. }
  453. }
  454. return 1;
  455. }
  456. static long
  457. msgReadFile(Msg *m, char *file, char **ss)
  458. {
  459. Dir *d;
  460. char *s, buf[BufSize];
  461. vlong length;
  462. long n, nn;
  463. int fd;
  464. fd = msgFile(m, file);
  465. if(fd < 0){
  466. msgDead(m);
  467. return -1;
  468. }
  469. n = read(fd, buf, BufSize);
  470. if(n < BufSize){
  471. close(fd);
  472. if(n < 0){
  473. *ss = nil;
  474. return -1;
  475. }
  476. s = emalloc(n + 1);
  477. memmove(s, buf, n);
  478. s[n] = '\0';
  479. *ss = s;
  480. return n;
  481. }
  482. d = dirfstat(fd);
  483. if(d == nil){
  484. close(fd);
  485. return -1;
  486. }
  487. length = d->length;
  488. free(d);
  489. nn = length;
  490. s = emalloc(nn + 1);
  491. memmove(s, buf, n);
  492. if(nn > n)
  493. nn = readn(fd, s+n, nn-n) + n;
  494. close(fd);
  495. if(nn != length){
  496. free(s);
  497. return -1;
  498. }
  499. s[nn] = '\0';
  500. *ss = s;
  501. return nn;
  502. }
  503. static void
  504. freeMAddr(MAddr *a)
  505. {
  506. MAddr *p;
  507. while(a != nil){
  508. p = a;
  509. a = a->next;
  510. free(p->personal);
  511. free(p->box);
  512. free(p->host);
  513. free(p);
  514. }
  515. }
  516. /*
  517. * the message is corrupted or illegal.
  518. * reset message fields. msgStruct will reparse the message,
  519. * relying on msgFile to make up corrected body parts.
  520. */
  521. static int
  522. msgBogus(Msg *m, int flags)
  523. {
  524. if(!(m->bogus & flags))
  525. m->bogus |= flags;
  526. m->lines = ~0;
  527. free(m->head.buf);
  528. free(m->mime.buf);
  529. memset(&m->head, 0, sizeof(Header));
  530. memset(&m->mime, 0, sizeof(Header));
  531. return 0;
  532. }
  533. /*
  534. * stolen from upas/marshal; base64 encodes from one fd to another.
  535. *
  536. * the size of buf is very important to enc64. Anything other than
  537. * a multiple of 3 will cause enc64 to output a termination sequence.
  538. * To ensure that a full buf corresponds to a multiple of complete lines,
  539. * we make buf a multiple of 3*18 since that's how many enc64 sticks on
  540. * a single line. This avoids short lines in the output which is pleasing
  541. * but not necessary.
  542. */
  543. static int
  544. enc64x18(char *out, int lim, uchar *in, int n)
  545. {
  546. int m, mm, nn;
  547. nn = 0;
  548. for(; n > 0; n -= m){
  549. m = 18 * 3;
  550. if(m > n)
  551. m = n;
  552. mm = enc64(out, lim - nn, in, m);
  553. in += m;
  554. out += mm;
  555. *out++ = '\r';
  556. *out++ = '\n';
  557. nn += mm + 2;
  558. }
  559. return nn;
  560. }
  561. static void
  562. body64(int in, int out)
  563. {
  564. uchar buf[3*18*54];
  565. char obuf[3*18*54*2];
  566. int m, n;
  567. for(;;){
  568. n = read(in, buf, sizeof(buf));
  569. if(n < 0)
  570. return;
  571. if(n == 0)
  572. break;
  573. m = enc64x18(obuf, sizeof(obuf), buf, n);
  574. if(write(out, obuf, m) < 0)
  575. return;
  576. }
  577. }
  578. /*
  579. * strip all non-printable characters from a file
  580. */
  581. static void
  582. bodystrip(int in, int out)
  583. {
  584. uchar buf[3*18*54];
  585. int m, n, i, c;
  586. for(;;){
  587. n = read(in, buf, sizeof(buf));
  588. if(n < 0)
  589. return;
  590. if(n == 0)
  591. break;
  592. m = 0;
  593. for(i = 0; i < n; i++){
  594. c = buf[i];
  595. if(c > 0x1f && c < 0x7f /* normal characters */
  596. || c >= 0x9 && c <= 0xd) /* \t, \n, vertical tab, form feed, \r */
  597. buf[m++] = c;
  598. }
  599. if(m && write(out, buf, m) < 0)
  600. return;
  601. }
  602. }
  603. /*
  604. * read in the message body to count \n without a preceding \r
  605. */
  606. static int
  607. msgBodySize(Msg *m)
  608. {
  609. Dir *d;
  610. char buf[BufSize + 2], *s, *se;
  611. vlong length;
  612. ulong size, lines, bad;
  613. int n, fd, c;
  614. if(m->lines != ~0UL)
  615. return 1;
  616. fd = msgFile(m, "rawbody");
  617. if(fd < 0)
  618. return 0;
  619. d = dirfstat(fd);
  620. if(d == nil){
  621. close(fd);
  622. return 0;
  623. }
  624. length = d->length;
  625. free(d);
  626. size = 0;
  627. lines = 0;
  628. bad = 0;
  629. buf[0] = ' ';
  630. for(;;){
  631. n = read(fd, &buf[1], BufSize);
  632. if(n <= 0)
  633. break;
  634. size += n;
  635. se = &buf[n + 1];
  636. for(s = &buf[1]; s < se; s++){
  637. c = *s;
  638. if(c == '\0'){
  639. close(fd);
  640. return msgBogus(m, BogusBody);
  641. }
  642. if(c != '\n')
  643. continue;
  644. if(s[-1] != '\r')
  645. bad++;
  646. lines++;
  647. }
  648. buf[0] = buf[n];
  649. }
  650. if(size != length)
  651. bye("bad length reading rawbody");
  652. size += bad;
  653. m->size = size;
  654. m->lines = lines;
  655. close(fd);
  656. return 1;
  657. }
  658. /*
  659. * retrieve information from the unixheader file
  660. */
  661. static int
  662. msgUnix(Msg *m, int top)
  663. {
  664. Tm tm;
  665. char *s, *ss;
  666. if(m->unixDate != nil)
  667. return 1;
  668. if(!top){
  669. bogus:
  670. m->unixDate = estrdup("");
  671. m->unixFrom = unixFrom(nil);
  672. return 1;
  673. }
  674. if(msgReadFile(m, "unixheader", &ss) < 0)
  675. return 0;
  676. s = ss;
  677. s = strchr(s, ' ');
  678. if(s == nil){
  679. free(ss);
  680. goto bogus;
  681. }
  682. s++;
  683. m->unixFrom = unixFrom(s);
  684. s = (char*)headStr;
  685. if(date2tm(&tm, s) == nil)
  686. s = m->info[IUnixDate];
  687. if(s == nil){
  688. free(ss);
  689. goto bogus;
  690. }
  691. m->unixDate = estrdup(s);
  692. free(ss);
  693. return 1;
  694. }
  695. /*
  696. * parse the address in the unix header
  697. * last line of defence, so must return something
  698. */
  699. static MAddr *
  700. unixFrom(char *s)
  701. {
  702. MAddr *a;
  703. char *e, *t;
  704. if(s == nil)
  705. return nil;
  706. headStr = (uchar*)s;
  707. t = emalloc(strlen(s) + 2);
  708. e = headAddrSpec(t, nil);
  709. if(e == nil)
  710. a = nil;
  711. else{
  712. if(*e != '\0')
  713. *e++ = '\0';
  714. else
  715. e = site;
  716. a = MKZ(MAddr);
  717. a->box = estrdup(t);
  718. a->host = estrdup(e);
  719. }
  720. free(t);
  721. return a;
  722. }
  723. /*
  724. * read in the entire header,
  725. * and parse out any existing mime headers
  726. */
  727. static int
  728. msgHeader(Msg *m, Header *h, char *file)
  729. {
  730. char *s, *ss, *t, *te;
  731. ulong lines, n, nn;
  732. long ns;
  733. int dated, c;
  734. if(h->buf != nil)
  735. return 1;
  736. ns = msgReadFile(m, file, &ss);
  737. if(ns < 0)
  738. return 0;
  739. s = ss;
  740. n = ns;
  741. /*
  742. * count lines ending with \n and \r\n
  743. * add an extra line at the end, since upas/fs headers
  744. * don't have a terminating \r\n
  745. */
  746. lines = 1;
  747. te = s + ns;
  748. for(t = s; t < te; t++){
  749. c = *t;
  750. if(c == '\0')
  751. return msgBogus(m, BogusHeader);
  752. if(c != '\n')
  753. continue;
  754. if(t == s || t[-1] != '\r')
  755. n++;
  756. lines++;
  757. }
  758. if(t > s && t[-1] != '\n'){
  759. if(t[-1] != '\r')
  760. n++;
  761. n++;
  762. }
  763. n += 2;
  764. h->buf = emalloc(n + 1);
  765. h->size = n;
  766. h->lines = lines;
  767. /*
  768. * make sure all headers end in \r\n
  769. */
  770. nn = 0;
  771. for(t = s; t < te; t++){
  772. c = *t;
  773. if(c == '\n'){
  774. if(!nn || h->buf[nn - 1] != '\r')
  775. h->buf[nn++] = '\r';
  776. lines++;
  777. }
  778. h->buf[nn++] = c;
  779. }
  780. if(nn && h->buf[nn-1] != '\n'){
  781. if(h->buf[nn-1] != '\r')
  782. h->buf[nn++] = '\r';
  783. h->buf[nn++] = '\n';
  784. }
  785. h->buf[nn++] = '\r';
  786. h->buf[nn++] = '\n';
  787. h->buf[nn] = '\0';
  788. if(nn != n)
  789. bye("misconverted header %d %d", nn, n);
  790. free(s);
  791. /*
  792. * and parse some mime headers
  793. */
  794. headStr = (uchar*)h->buf;
  795. dated = 0;
  796. while(s = headAtom(headFieldStop)){
  797. if(cistrcmp(s, "content-type") == 0)
  798. mimeType(h);
  799. else if(cistrcmp(s, "content-transfer-encoding") == 0)
  800. mimeEncoding(h);
  801. else if(cistrcmp(s, "content-id") == 0)
  802. mimeId(h);
  803. else if(cistrcmp(s, "content-description") == 0)
  804. mimeDescription(h);
  805. else if(cistrcmp(s, "content-disposition") == 0)
  806. mimeDisposition(h);
  807. else if(cistrcmp(s, "content-md5") == 0)
  808. mimeMd5(h);
  809. else if(cistrcmp(s, "content-language") == 0)
  810. mimeLanguage(h);
  811. else if(h == &m->head && cistrcmp(s, "from") == 0)
  812. m->from = headMAddr(m->from);
  813. else if(h == &m->head && cistrcmp(s, "to") == 0)
  814. m->to = headMAddr(m->to);
  815. else if(h == &m->head && cistrcmp(s, "reply-to") == 0)
  816. m->replyTo = headMAddr(m->replyTo);
  817. else if(h == &m->head && cistrcmp(s, "sender") == 0)
  818. m->sender = headMAddr(m->sender);
  819. else if(h == &m->head && cistrcmp(s, "cc") == 0)
  820. m->cc = headMAddr(m->cc);
  821. else if(h == &m->head && cistrcmp(s, "bcc") == 0)
  822. m->bcc = headMAddr(m->bcc);
  823. else if(h == &m->head && cistrcmp(s, "date") == 0)
  824. dated = 1;
  825. headSkip();
  826. free(s);
  827. }
  828. if(h == &m->head){
  829. if(m->from == nil){
  830. m->from = m->unixFrom;
  831. if(m->from != nil){
  832. s = maddrStr(m->from);
  833. msgAddHead(m, "From", s);
  834. free(s);
  835. }
  836. }
  837. if(m->sender == nil)
  838. m->sender = m->from;
  839. if(m->replyTo == nil)
  840. m->replyTo = m->from;
  841. if(infoIsNil(m->info[IDate]))
  842. m->info[IDate] = m->unixDate;
  843. if(!dated && m->from != nil)
  844. msgAddDate(m);
  845. }
  846. return 1;
  847. }
  848. /*
  849. * prepend head: body to the cached header
  850. */
  851. static void
  852. msgAddHead(Msg *m, char *head, char *body)
  853. {
  854. char *s;
  855. long size, n;
  856. n = strlen(head) + strlen(body) + 4;
  857. size = m->head.size + n;
  858. s = emalloc(size + 1);
  859. snprint(s, size + 1, "%s: %s\r\n%s", head, body, m->head.buf);
  860. free(m->head.buf);
  861. m->head.buf = s;
  862. m->head.size = size;
  863. m->head.lines++;
  864. }
  865. static void
  866. msgAddDate(Msg *m)
  867. {
  868. Tm tm;
  869. char buf[64];
  870. /* don't bother if we don't have a date */
  871. if(infoIsNil(m->info[IDate]))
  872. return;
  873. date2tm(&tm, m->info[IDate]);
  874. rfc822date(buf, sizeof(buf), &tm);
  875. msgAddHead(m, "Date", buf);
  876. }
  877. static MimeHdr*
  878. mkMimeHdr(char *s, char *t, MimeHdr *next)
  879. {
  880. MimeHdr *mh;
  881. mh = MK(MimeHdr);
  882. mh->s = s;
  883. mh->t = t;
  884. mh->next = next;
  885. return mh;
  886. }
  887. static void
  888. freeMimeHdr(MimeHdr *mh)
  889. {
  890. MimeHdr *last;
  891. while(mh != nil){
  892. last = mh;
  893. mh = mh->next;
  894. free(last->s);
  895. free(last->t);
  896. free(last);
  897. }
  898. }
  899. static void
  900. cleanupHeader(Header *h)
  901. {
  902. freeMimeHdr(h->type);
  903. freeMimeHdr(h->id);
  904. freeMimeHdr(h->description);
  905. freeMimeHdr(h->encoding);
  906. freeMimeHdr(h->md5);
  907. freeMimeHdr(h->disposition);
  908. freeMimeHdr(h->language);
  909. }
  910. /*
  911. * parser for rfc822 & mime header fields
  912. */
  913. /*
  914. * type : 'content-type' ':' token '/' token params
  915. */
  916. static void
  917. mimeType(Header *h)
  918. {
  919. char *s, *t;
  920. if(headChar(1) != ':')
  921. return;
  922. s = headAtom(mimeTokenStop);
  923. if(s == nil || headChar(1) != '/'){
  924. free(s);
  925. return;
  926. }
  927. t = headAtom(mimeTokenStop);
  928. if(t == nil){
  929. free(s);
  930. return;
  931. }
  932. h->type = mkMimeHdr(s, t, mimeParams());
  933. }
  934. /*
  935. * params :
  936. * | params ';' token '=' token
  937. * | params ';' token '=' quoted-str
  938. */
  939. static MimeHdr*
  940. mimeParams(void)
  941. {
  942. MimeHdr head, *last;
  943. char *s, *t;
  944. head.next = nil;
  945. last = &head;
  946. for(;;){
  947. if(headChar(1) != ';')
  948. break;
  949. s = headAtom(mimeTokenStop);
  950. if(s == nil || headChar(1) != '='){
  951. free(s);
  952. break;
  953. }
  954. if(headChar(0) == '"'){
  955. t = headQuoted('"', '"');
  956. stripQuotes(t);
  957. }else
  958. t = headAtom(mimeTokenStop);
  959. if(t == nil){
  960. free(s);
  961. break;
  962. }
  963. last->next = mkMimeHdr(s, t, nil);
  964. last = last->next;
  965. }
  966. return head.next;
  967. }
  968. /*
  969. * encoding : 'content-transfer-encoding' ':' token
  970. */
  971. static void
  972. mimeEncoding(Header *h)
  973. {
  974. char *s;
  975. if(headChar(1) != ':')
  976. return;
  977. s = headAtom(mimeTokenStop);
  978. if(s == nil)
  979. return;
  980. h->encoding = mkMimeHdr(s, nil, nil);
  981. }
  982. /*
  983. * mailaddr : ':' addresses
  984. */
  985. static MAddr*
  986. headMAddr(MAddr *old)
  987. {
  988. MAddr *a;
  989. if(headChar(1) != ':')
  990. return old;
  991. if(headChar(0) == '\n')
  992. return old;
  993. a = headAddresses();
  994. if(a == nil)
  995. return old;
  996. freeMAddr(old);
  997. return a;
  998. }
  999. /*
  1000. * addresses : address | addresses ',' address
  1001. */
  1002. static MAddr*
  1003. headAddresses(void)
  1004. {
  1005. MAddr *addr, *tail, *a;
  1006. addr = headAddress();
  1007. if(addr == nil)
  1008. return nil;
  1009. tail = addr;
  1010. while(headChar(0) == ','){
  1011. headChar(1);
  1012. a = headAddress();
  1013. if(a == nil){
  1014. freeMAddr(addr);
  1015. return nil;
  1016. }
  1017. tail->next = a;
  1018. tail = a;
  1019. }
  1020. return addr;
  1021. }
  1022. /*
  1023. * address : mailbox | group
  1024. * group : phrase ':' mboxes ';' | phrase ':' ';'
  1025. * mailbox : addr-spec
  1026. * | optphrase '<' addr-spec '>'
  1027. * | optphrase '<' route ':' addr-spec '>'
  1028. * optphrase : | phrase
  1029. * route : '@' domain
  1030. * | route ',' '@' domain
  1031. * personal names are the phrase before '<',
  1032. * or a comment before or after a simple addr-spec
  1033. */
  1034. static MAddr*
  1035. headAddress(void)
  1036. {
  1037. MAddr *addr;
  1038. uchar *hs;
  1039. char *s, *e, *w, *personal;
  1040. int c;
  1041. s = emalloc(strlen((char*)headStr) + 2);
  1042. e = s;
  1043. personal = headSkipWhite(1);
  1044. c = headChar(0);
  1045. if(c == '<')
  1046. w = nil;
  1047. else{
  1048. w = headWord();
  1049. c = headChar(0);
  1050. }
  1051. if(c == '.' || c == '@' || c == ',' || c == '\n' || c == '\0'){
  1052. lastWhite = headStr;
  1053. e = headAddrSpec(s, w);
  1054. if(personal == nil){
  1055. hs = headStr;
  1056. headStr = lastWhite;
  1057. personal = headSkipWhite(1);
  1058. headStr = hs;
  1059. }
  1060. }else{
  1061. if(c != '<' || w != nil){
  1062. free(personal);
  1063. if(!headPhrase(e, w)){
  1064. free(s);
  1065. return nil;
  1066. }
  1067. /*
  1068. * ignore addresses with groups,
  1069. * so the only thing left if <
  1070. */
  1071. c = headChar(1);
  1072. if(c != '<'){
  1073. free(s);
  1074. return nil;
  1075. }
  1076. personal = estrdup(s);
  1077. }else
  1078. headChar(1);
  1079. /*
  1080. * after this point, we need to free personal before returning.
  1081. * set e to nil to everything afterwards fails.
  1082. *
  1083. * ignore routes, they are useless, and heavily discouraged in rfc1123.
  1084. * imap4 reports them up to, but not including, the terminating :
  1085. */
  1086. e = s;
  1087. c = headChar(0);
  1088. if(c == '@'){
  1089. for(;;){
  1090. c = headChar(1);
  1091. if(c != '@'){
  1092. e = nil;
  1093. break;
  1094. }
  1095. headDomain(e);
  1096. c = headChar(1);
  1097. if(c != ','){
  1098. e = s;
  1099. break;
  1100. }
  1101. }
  1102. if(c != ':')
  1103. e = nil;
  1104. }
  1105. if(e != nil)
  1106. e = headAddrSpec(s, nil);
  1107. if(headChar(1) != '>')
  1108. e = nil;
  1109. }
  1110. /*
  1111. * e points to @host, or nil if an error occured
  1112. */
  1113. if(e == nil){
  1114. free(personal);
  1115. addr = nil;
  1116. }else{
  1117. if(*e != '\0')
  1118. *e++ = '\0';
  1119. else
  1120. e = site;
  1121. addr = MKZ(MAddr);
  1122. addr->personal = personal;
  1123. addr->box = estrdup(s);
  1124. addr->host = estrdup(e);
  1125. }
  1126. free(s);
  1127. return addr;
  1128. }
  1129. /*
  1130. * phrase : word
  1131. * | phrase word
  1132. * w is the optional initial word of the phrase
  1133. * returns the end of the phrase, or nil if a failure occured
  1134. */
  1135. static char*
  1136. headPhrase(char *e, char *w)
  1137. {
  1138. int c;
  1139. for(;;){
  1140. if(w == nil){
  1141. w = headWord();
  1142. if(w == nil)
  1143. return nil;
  1144. }
  1145. if(w[0] == '"')
  1146. stripQuotes(w);
  1147. strcpy(e, w);
  1148. free(w);
  1149. w = nil;
  1150. e = strchr(e, '\0');
  1151. c = headChar(0);
  1152. if(c <= ' ' || strchr(headAtomStop, c) != nil && c != '"')
  1153. break;
  1154. *e++ = ' ';
  1155. *e = '\0';
  1156. }
  1157. return e;
  1158. }
  1159. /*
  1160. * addr-spec : local-part '@' domain
  1161. * | local-part extension to allow ! and local names
  1162. * local-part : word
  1163. * | local-part '.' word
  1164. *
  1165. * if no '@' is present, rewrite d!e!f!u as @d,@e:u@f,
  1166. * where d, e, f are valid domain components.
  1167. * the @d,@e: is ignored, since routes are ignored.
  1168. * perhaps they should be rewritten as e!f!u@d, but that is inconsistent with upas.
  1169. *
  1170. * returns a pointer to '@', the end if none, or nil if there was an error
  1171. */
  1172. static char*
  1173. headAddrSpec(char *e, char *w)
  1174. {
  1175. char *s, *at, *b, *bang, *dom;
  1176. int c;
  1177. s = e;
  1178. for(;;){
  1179. if(w == nil){
  1180. w = headWord();
  1181. if(w == nil)
  1182. return nil;
  1183. }
  1184. strcpy(e, w);
  1185. free(w);
  1186. w = nil;
  1187. e = strchr(e, '\0');
  1188. lastWhite = headStr;
  1189. c = headChar(0);
  1190. if(c != '.')
  1191. break;
  1192. headChar(1);
  1193. *e++ = '.';
  1194. *e = '\0';
  1195. }
  1196. if(c != '@'){
  1197. /*
  1198. * extenstion: allow name without domain
  1199. * check for domain!xxx
  1200. */
  1201. bang = domBang(s);
  1202. if(bang == nil)
  1203. return e;
  1204. /*
  1205. * if dom1!dom2!xxx, ignore dom1!
  1206. */
  1207. dom = s;
  1208. for(; b = domBang(bang + 1); bang = b)
  1209. dom = bang + 1;
  1210. /*
  1211. * convert dom!mbox into mbox@dom
  1212. */
  1213. *bang = '@';
  1214. strrev(dom, bang);
  1215. strrev(bang+1, e);
  1216. strrev(dom, e);
  1217. bang = &dom[e - bang - 1];
  1218. if(dom > s){
  1219. bang -= dom - s;
  1220. for(e = s; *e = *dom; e++)
  1221. dom++;
  1222. }
  1223. /*
  1224. * eliminate a trailing '.'
  1225. */
  1226. if(e[-1] == '.')
  1227. e[-1] = '\0';
  1228. return bang;
  1229. }
  1230. headChar(1);
  1231. at = e;
  1232. *e++ = '@';
  1233. *e = '\0';
  1234. if(!headDomain(e))
  1235. return nil;
  1236. return at;
  1237. }
  1238. /*
  1239. * find the ! in domain!rest, where domain must have at least
  1240. * one internal '.'
  1241. */
  1242. static char*
  1243. domBang(char *s)
  1244. {
  1245. int dot, c;
  1246. dot = 0;
  1247. for(; c = *s; s++){
  1248. if(c == '!'){
  1249. if(!dot || dot == 1 && s[-1] == '.' || s[1] == '\0')
  1250. return nil;
  1251. return s;
  1252. }
  1253. if(c == '"')
  1254. break;
  1255. if(c == '.')
  1256. dot++;
  1257. }
  1258. return nil;
  1259. }
  1260. /*
  1261. * domain : sub-domain
  1262. * | domain '.' sub-domain
  1263. * returns the end of the domain, or nil if a failure occured
  1264. */
  1265. static char*
  1266. headDomain(char *e)
  1267. {
  1268. char *w;
  1269. for(;;){
  1270. w = headSubDomain();
  1271. if(w == nil)
  1272. return nil;
  1273. strcpy(e, w);
  1274. free(w);
  1275. e = strchr(e, '\0');
  1276. lastWhite = headStr;
  1277. if(headChar(0) != '.')
  1278. break;
  1279. headChar(1);
  1280. *e++ = '.';
  1281. *e = '\0';
  1282. }
  1283. return e;
  1284. }
  1285. /*
  1286. * id : 'content-id' ':' msg-id
  1287. * msg-id : '<' addr-spec '>'
  1288. */
  1289. static void
  1290. mimeId(Header *h)
  1291. {
  1292. char *s, *e, *w;
  1293. if(headChar(1) != ':')
  1294. return;
  1295. if(headChar(1) != '<')
  1296. return;
  1297. s = emalloc(strlen((char*)headStr) + 3);
  1298. e = s;
  1299. *e++ = '<';
  1300. e = headAddrSpec(e, nil);
  1301. if(e == nil || headChar(1) != '>'){
  1302. free(s);
  1303. return;
  1304. }
  1305. e = strchr(e, '\0');
  1306. *e++ = '>';
  1307. e[0] = '\0';
  1308. w = strdup(s);
  1309. free(s);
  1310. h->id = mkMimeHdr(w, nil, nil);
  1311. }
  1312. /*
  1313. * description : 'content-description' ':' *text
  1314. */
  1315. static void
  1316. mimeDescription(Header *h)
  1317. {
  1318. if(headChar(1) != ':')
  1319. return;
  1320. headSkipWhite(0);
  1321. h->description = mkMimeHdr(headText(), nil, nil);
  1322. }
  1323. /*
  1324. * disposition : 'content-disposition' ':' token params
  1325. */
  1326. static void
  1327. mimeDisposition(Header *h)
  1328. {
  1329. char *s;
  1330. if(headChar(1) != ':')
  1331. return;
  1332. s = headAtom(mimeTokenStop);
  1333. if(s == nil)
  1334. return;
  1335. h->disposition = mkMimeHdr(s, nil, mimeParams());
  1336. }
  1337. /*
  1338. * md5 : 'content-md5' ':' token
  1339. */
  1340. static void
  1341. mimeMd5(Header *h)
  1342. {
  1343. char *s;
  1344. if(headChar(1) != ':')
  1345. return;
  1346. s = headAtom(mimeTokenStop);
  1347. if(s == nil)
  1348. return;
  1349. h->md5 = mkMimeHdr(s, nil, nil);
  1350. }
  1351. /*
  1352. * language : 'content-language' ':' langs
  1353. * langs : token
  1354. * | langs commas token
  1355. * commas : ','
  1356. * | commas ','
  1357. */
  1358. static void
  1359. mimeLanguage(Header *h)
  1360. {
  1361. MimeHdr head, *last;
  1362. char *s;
  1363. head.next = nil;
  1364. last = &head;
  1365. for(;;){
  1366. s = headAtom(mimeTokenStop);
  1367. if(s == nil)
  1368. break;
  1369. last->next = mkMimeHdr(s, nil, nil);
  1370. last = last->next;
  1371. while(headChar(0) != ',')
  1372. headChar(1);
  1373. }
  1374. h->language = head.next;
  1375. }
  1376. /*
  1377. * token : 1*<char 33-255, except "()<>@,;:\\\"/[]?=" aka mimeTokenStop>
  1378. * atom : 1*<chars 33-255, except "()<>@,;:\\\".[]" aka headAtomStop>
  1379. * note this allows 8 bit characters, which occur in utf.
  1380. */
  1381. static char*
  1382. headAtom(char *disallowed)
  1383. {
  1384. char *s;
  1385. int c, ns, as;
  1386. headSkipWhite(0);
  1387. s = emalloc(StrAlloc);
  1388. as = StrAlloc;
  1389. ns = 0;
  1390. for(;;){
  1391. c = *headStr++;
  1392. if(c <= ' ' || strchr(disallowed, c) != nil){
  1393. headStr--;
  1394. break;
  1395. }
  1396. s[ns++] = c;
  1397. if(ns >= as){
  1398. as += StrAlloc;
  1399. s = erealloc(s, as);
  1400. }
  1401. }
  1402. if(ns == 0){
  1403. free(s);
  1404. return 0;
  1405. }
  1406. s[ns] = '\0';
  1407. return s;
  1408. }
  1409. /*
  1410. * sub-domain : atom | domain-lit
  1411. */
  1412. static char *
  1413. headSubDomain(void)
  1414. {
  1415. if(headChar(0) == '[')
  1416. return headQuoted('[', ']');
  1417. return headAtom(headAtomStop);
  1418. }
  1419. /*
  1420. * word : atom | quoted-str
  1421. */
  1422. static char *
  1423. headWord(void)
  1424. {
  1425. if(headChar(0) == '"')
  1426. return headQuoted('"', '"');
  1427. return headAtom(headAtomStop);
  1428. }
  1429. /*
  1430. * q is a quoted string. remove enclosing " and and \ escapes
  1431. */
  1432. static void
  1433. stripQuotes(char *q)
  1434. {
  1435. char *s;
  1436. int c;
  1437. if(q == nil)
  1438. return;
  1439. s = q++;
  1440. while(c = *q++){
  1441. if(c == '\\'){
  1442. c = *q++;
  1443. if(!c)
  1444. return;
  1445. }
  1446. *s++ = c;
  1447. }
  1448. s[-1] = '\0';
  1449. }
  1450. /*
  1451. * quoted-str : '"' *(any char but '"\\\r', or '\' any char, or linear-white-space) '"'
  1452. * domain-lit : '[' *(any char but '[]\\\r', or '\' any char, or linear-white-space) ']'
  1453. */
  1454. static char *
  1455. headQuoted(int start, int stop)
  1456. {
  1457. char *s;
  1458. int c, ns, as;
  1459. if(headChar(1) != start)
  1460. return nil;
  1461. s = emalloc(StrAlloc);
  1462. as = StrAlloc;
  1463. ns = 0;
  1464. s[ns++] = start;
  1465. for(;;){
  1466. c = *headStr;
  1467. if(c == stop){
  1468. headStr++;
  1469. break;
  1470. }
  1471. if(c == '\0'){
  1472. free(s);
  1473. return nil;
  1474. }
  1475. if(c == '\r'){
  1476. headStr++;
  1477. continue;
  1478. }
  1479. if(c == '\n'){
  1480. headStr++;
  1481. while(*headStr == ' ' || *headStr == '\t' || *headStr == '\r' || *headStr == '\n')
  1482. headStr++;
  1483. c = ' ';
  1484. }else if(c == '\\'){
  1485. headStr++;
  1486. s[ns++] = c;
  1487. c = *headStr;
  1488. if(c == '\0'){
  1489. free(s);
  1490. return nil;
  1491. }
  1492. headStr++;
  1493. }else
  1494. headStr++;
  1495. s[ns++] = c;
  1496. if(ns + 1 >= as){ /* leave room for \c or "0 */
  1497. as += StrAlloc;
  1498. s = erealloc(s, as);
  1499. }
  1500. }
  1501. s[ns++] = stop;
  1502. s[ns] = '\0';
  1503. return s;
  1504. }
  1505. /*
  1506. * headText : contents of rest of header line
  1507. */
  1508. static char *
  1509. headText(void)
  1510. {
  1511. uchar *v;
  1512. char *s;
  1513. v = headStr;
  1514. headToEnd();
  1515. s = emalloc(headStr - v + 1);
  1516. memmove(s, v, headStr - v);
  1517. s[headStr - v] = '\0';
  1518. return s;
  1519. }
  1520. /*
  1521. * white space is ' ' '\t' or nested comments.
  1522. * skip white space.
  1523. * if com and a comment is seen,
  1524. * return it's contents and stop processing white space.
  1525. */
  1526. static char*
  1527. headSkipWhite(int com)
  1528. {
  1529. char *s;
  1530. int c, incom, as, ns;
  1531. s = nil;
  1532. as = StrAlloc;
  1533. ns = 0;
  1534. if(com)
  1535. s = emalloc(StrAlloc);
  1536. incom = 0;
  1537. for(; c = *headStr; headStr++){
  1538. switch(c){
  1539. case ' ':
  1540. case '\t':
  1541. case '\r':
  1542. c = ' ';
  1543. break;
  1544. case '\n':
  1545. c = headStr[1];
  1546. if(c != ' ' && c != '\t')
  1547. goto breakout;
  1548. c = ' ';
  1549. break;
  1550. case '\\':
  1551. if(com && incom)
  1552. s[ns++] = c;
  1553. c = headStr[1];
  1554. if(c == '\0')
  1555. goto breakout;
  1556. headStr++;
  1557. break;
  1558. case '(':
  1559. incom++;
  1560. if(incom == 1)
  1561. continue;
  1562. break;
  1563. case ')':
  1564. incom--;
  1565. if(com && !incom){
  1566. s[ns] = '\0';
  1567. return s;
  1568. }
  1569. break;
  1570. default:
  1571. if(!incom)
  1572. goto breakout;
  1573. break;
  1574. }
  1575. if(com && incom && (c != ' ' || ns > 0 && s[ns-1] != ' ')){
  1576. s[ns++] = c;
  1577. if(ns + 1 >= as){ /* leave room for \c or 0 */
  1578. as += StrAlloc;
  1579. s = erealloc(s, as);
  1580. }
  1581. }
  1582. }
  1583. breakout:;
  1584. free(s);
  1585. return nil;
  1586. }
  1587. /*
  1588. * return the next non-white character
  1589. */
  1590. static int
  1591. headChar(int eat)
  1592. {
  1593. int c;
  1594. headSkipWhite(0);
  1595. c = *headStr;
  1596. if(eat && c != '\0' && c != '\n')
  1597. headStr++;
  1598. return c;
  1599. }
  1600. static void
  1601. headToEnd(void)
  1602. {
  1603. uchar *s;
  1604. int c;
  1605. for(;;){
  1606. s = headStr;
  1607. c = *s++;
  1608. while(c == '\r')
  1609. c = *s++;
  1610. if(c == '\n'){
  1611. c = *s++;
  1612. if(c != ' ' && c != '\t')
  1613. return;
  1614. }
  1615. if(c == '\0')
  1616. return;
  1617. headStr = s;
  1618. }
  1619. }
  1620. static void
  1621. headSkip(void)
  1622. {
  1623. int c;
  1624. while(c = *headStr){
  1625. headStr++;
  1626. if(c == '\n'){
  1627. c = *headStr;
  1628. if(c == ' ' || c == '\t')
  1629. continue;
  1630. return;
  1631. }
  1632. }
  1633. }