msg.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776
  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. m->unixDate = estrdup(s);
  688. free(ss);
  689. return 1;
  690. }
  691. /*
  692. * parse the address in the unix header
  693. * last line of defence, so must return something
  694. */
  695. static MAddr *
  696. unixFrom(char *s)
  697. {
  698. MAddr *a;
  699. char *e, *t;
  700. if(s == nil)
  701. return nil;
  702. headStr = (uchar*)s;
  703. t = emalloc(strlen(s) + 2);
  704. e = headAddrSpec(t, nil);
  705. if(e == nil)
  706. a = nil;
  707. else{
  708. if(*e != '\0')
  709. *e++ = '\0';
  710. else
  711. e = site;
  712. a = MKZ(MAddr);
  713. a->box = estrdup(t);
  714. a->host = estrdup(e);
  715. }
  716. free(t);
  717. return a;
  718. }
  719. /*
  720. * read in the entire header,
  721. * and parse out any existing mime headers
  722. */
  723. static int
  724. msgHeader(Msg *m, Header *h, char *file)
  725. {
  726. char *s, *ss, *t, *te;
  727. ulong lines, n, nn;
  728. long ns;
  729. int dated, c;
  730. if(h->buf != nil)
  731. return 1;
  732. ns = msgReadFile(m, file, &ss);
  733. if(ns < 0)
  734. return 0;
  735. s = ss;
  736. n = ns;
  737. /*
  738. * count lines ending with \n and \r\n
  739. * add an extra line at the end, since upas/fs headers
  740. * don't have a terminating \r\n
  741. */
  742. lines = 1;
  743. te = s + ns;
  744. for(t = s; t < te; t++){
  745. c = *t;
  746. if(c == '\0')
  747. return msgBogus(m, BogusHeader);
  748. if(c != '\n')
  749. continue;
  750. if(t == s || t[-1] != '\r')
  751. n++;
  752. lines++;
  753. }
  754. if(t > s && t[-1] != '\n'){
  755. if(t[-1] != '\r')
  756. n++;
  757. n++;
  758. }
  759. n += 2;
  760. h->buf = emalloc(n + 1);
  761. h->size = n;
  762. h->lines = lines;
  763. /*
  764. * make sure all headers end in \r\n
  765. */
  766. nn = 0;
  767. for(t = s; t < te; t++){
  768. c = *t;
  769. if(c == '\n'){
  770. if(!nn || h->buf[nn - 1] != '\r')
  771. h->buf[nn++] = '\r';
  772. lines++;
  773. }
  774. h->buf[nn++] = c;
  775. }
  776. if(nn && h->buf[nn-1] != '\n'){
  777. if(h->buf[nn-1] != '\r')
  778. h->buf[nn++] = '\r';
  779. h->buf[nn++] = '\n';
  780. }
  781. h->buf[nn++] = '\r';
  782. h->buf[nn++] = '\n';
  783. h->buf[nn] = '\0';
  784. if(nn != n)
  785. bye("misconverted header %d %d", nn, n);
  786. free(s);
  787. /*
  788. * and parse some mime headers
  789. */
  790. headStr = (uchar*)h->buf;
  791. dated = 0;
  792. while(s = headAtom(headFieldStop)){
  793. if(cistrcmp(s, "content-type") == 0)
  794. mimeType(h);
  795. else if(cistrcmp(s, "content-transfer-encoding") == 0)
  796. mimeEncoding(h);
  797. else if(cistrcmp(s, "content-id") == 0)
  798. mimeId(h);
  799. else if(cistrcmp(s, "content-description") == 0)
  800. mimeDescription(h);
  801. else if(cistrcmp(s, "content-disposition") == 0)
  802. mimeDisposition(h);
  803. else if(cistrcmp(s, "content-md5") == 0)
  804. mimeMd5(h);
  805. else if(cistrcmp(s, "content-language") == 0)
  806. mimeLanguage(h);
  807. else if(h == &m->head && cistrcmp(s, "from") == 0)
  808. m->from = headMAddr(m->from);
  809. else if(h == &m->head && cistrcmp(s, "to") == 0)
  810. m->to = headMAddr(m->to);
  811. else if(h == &m->head && cistrcmp(s, "reply-to") == 0)
  812. m->replyTo = headMAddr(m->replyTo);
  813. else if(h == &m->head && cistrcmp(s, "sender") == 0)
  814. m->sender = headMAddr(m->sender);
  815. else if(h == &m->head && cistrcmp(s, "cc") == 0)
  816. m->cc = headMAddr(m->cc);
  817. else if(h == &m->head && cistrcmp(s, "bcc") == 0)
  818. m->bcc = headMAddr(m->bcc);
  819. else if(h == &m->head && cistrcmp(s, "date") == 0)
  820. dated = 1;
  821. headSkip();
  822. free(s);
  823. }
  824. if(h == &m->head){
  825. if(m->from == nil){
  826. m->from = m->unixFrom;
  827. if(m->from != nil){
  828. s = maddrStr(m->from);
  829. msgAddHead(m, "From", s);
  830. free(s);
  831. }
  832. }
  833. if(m->sender == nil)
  834. m->sender = m->from;
  835. if(m->replyTo == nil)
  836. m->replyTo = m->from;
  837. if(infoIsNil(m->info[IDate]))
  838. m->info[IDate] = m->unixDate;
  839. if(!dated && m->from != nil)
  840. msgAddDate(m);
  841. }
  842. return 1;
  843. }
  844. /*
  845. * prepend head: body to the cached header
  846. */
  847. static void
  848. msgAddHead(Msg *m, char *head, char *body)
  849. {
  850. char *s;
  851. long size, n;
  852. n = strlen(head) + strlen(body) + 4;
  853. size = m->head.size + n;
  854. s = emalloc(size + 1);
  855. snprint(s, size + 1, "%s: %s\r\n%s", head, body, m->head.buf);
  856. free(m->head.buf);
  857. m->head.buf = s;
  858. m->head.size = size;
  859. m->head.lines++;
  860. }
  861. static void
  862. msgAddDate(Msg *m)
  863. {
  864. Tm tm;
  865. char buf[64];
  866. /* don't bother if we don't have a date */
  867. if(infoIsNil(m->info[IDate]))
  868. return;
  869. date2tm(&tm, m->info[IDate]);
  870. rfc822date(buf, sizeof(buf), &tm);
  871. msgAddHead(m, "Date", buf);
  872. }
  873. static MimeHdr*
  874. mkMimeHdr(char *s, char *t, MimeHdr *next)
  875. {
  876. MimeHdr *mh;
  877. mh = MK(MimeHdr);
  878. mh->s = s;
  879. mh->t = t;
  880. mh->next = next;
  881. return mh;
  882. }
  883. static void
  884. freeMimeHdr(MimeHdr *mh)
  885. {
  886. MimeHdr *last;
  887. while(mh != nil){
  888. last = mh;
  889. mh = mh->next;
  890. free(last->s);
  891. free(last->t);
  892. free(last);
  893. }
  894. }
  895. static void
  896. cleanupHeader(Header *h)
  897. {
  898. freeMimeHdr(h->type);
  899. freeMimeHdr(h->id);
  900. freeMimeHdr(h->description);
  901. freeMimeHdr(h->encoding);
  902. freeMimeHdr(h->md5);
  903. freeMimeHdr(h->disposition);
  904. freeMimeHdr(h->language);
  905. }
  906. /*
  907. * parser for rfc822 & mime header fields
  908. */
  909. /*
  910. * type : 'content-type' ':' token '/' token params
  911. */
  912. static void
  913. mimeType(Header *h)
  914. {
  915. char *s, *t;
  916. if(headChar(1) != ':')
  917. return;
  918. s = headAtom(mimeTokenStop);
  919. if(s == nil || headChar(1) != '/'){
  920. free(s);
  921. return;
  922. }
  923. t = headAtom(mimeTokenStop);
  924. if(t == nil){
  925. free(s);
  926. return;
  927. }
  928. h->type = mkMimeHdr(s, t, mimeParams());
  929. }
  930. /*
  931. * params :
  932. * | params ';' token '=' token
  933. * | params ';' token '=' quoted-str
  934. */
  935. static MimeHdr*
  936. mimeParams(void)
  937. {
  938. MimeHdr head, *last;
  939. char *s, *t;
  940. head.next = nil;
  941. last = &head;
  942. for(;;){
  943. if(headChar(1) != ';')
  944. break;
  945. s = headAtom(mimeTokenStop);
  946. if(s == nil || headChar(1) != '='){
  947. free(s);
  948. break;
  949. }
  950. if(headChar(0) == '"'){
  951. t = headQuoted('"', '"');
  952. stripQuotes(t);
  953. }else
  954. t = headAtom(mimeTokenStop);
  955. if(t == nil){
  956. free(s);
  957. break;
  958. }
  959. last->next = mkMimeHdr(s, t, nil);
  960. last = last->next;
  961. }
  962. return head.next;
  963. }
  964. /*
  965. * encoding : 'content-transfer-encoding' ':' token
  966. */
  967. static void
  968. mimeEncoding(Header *h)
  969. {
  970. char *s;
  971. if(headChar(1) != ':')
  972. return;
  973. s = headAtom(mimeTokenStop);
  974. if(s == nil)
  975. return;
  976. h->encoding = mkMimeHdr(s, nil, nil);
  977. }
  978. /*
  979. * mailaddr : ':' addresses
  980. */
  981. static MAddr*
  982. headMAddr(MAddr *old)
  983. {
  984. MAddr *a;
  985. if(headChar(1) != ':')
  986. return old;
  987. if(headChar(0) == '\n')
  988. return old;
  989. a = headAddresses();
  990. if(a == nil)
  991. return old;
  992. freeMAddr(old);
  993. return a;
  994. }
  995. /*
  996. * addresses : address | addresses ',' address
  997. */
  998. static MAddr*
  999. headAddresses(void)
  1000. {
  1001. MAddr *addr, *tail, *a;
  1002. addr = headAddress();
  1003. if(addr == nil)
  1004. return nil;
  1005. tail = addr;
  1006. while(headChar(0) == ','){
  1007. headChar(1);
  1008. a = headAddress();
  1009. if(a == nil){
  1010. freeMAddr(addr);
  1011. return nil;
  1012. }
  1013. tail->next = a;
  1014. tail = a;
  1015. }
  1016. return addr;
  1017. }
  1018. /*
  1019. * address : mailbox | group
  1020. * group : phrase ':' mboxes ';' | phrase ':' ';'
  1021. * mailbox : addr-spec
  1022. * | optphrase '<' addr-spec '>'
  1023. * | optphrase '<' route ':' addr-spec '>'
  1024. * optphrase : | phrase
  1025. * route : '@' domain
  1026. * | route ',' '@' domain
  1027. * personal names are the phrase before '<',
  1028. * or a comment before or after a simple addr-spec
  1029. */
  1030. static MAddr*
  1031. headAddress(void)
  1032. {
  1033. MAddr *addr;
  1034. uchar *hs;
  1035. char *s, *e, *w, *personal;
  1036. int c;
  1037. s = emalloc(strlen((char*)headStr) + 2);
  1038. e = s;
  1039. personal = headSkipWhite(1);
  1040. c = headChar(0);
  1041. if(c == '<')
  1042. w = nil;
  1043. else{
  1044. w = headWord();
  1045. c = headChar(0);
  1046. }
  1047. if(c == '.' || c == '@' || c == ',' || c == '\n' || c == '\0'){
  1048. lastWhite = headStr;
  1049. e = headAddrSpec(s, w);
  1050. if(personal == nil){
  1051. hs = headStr;
  1052. headStr = lastWhite;
  1053. personal = headSkipWhite(1);
  1054. headStr = hs;
  1055. }
  1056. }else{
  1057. if(c != '<' || w != nil){
  1058. free(personal);
  1059. if(!headPhrase(e, w)){
  1060. free(s);
  1061. return nil;
  1062. }
  1063. /*
  1064. * ignore addresses with groups,
  1065. * so the only thing left if <
  1066. */
  1067. c = headChar(1);
  1068. if(c != '<'){
  1069. free(s);
  1070. return nil;
  1071. }
  1072. personal = estrdup(s);
  1073. }else
  1074. headChar(1);
  1075. /*
  1076. * after this point, we need to free personal before returning.
  1077. * set e to nil to everything afterwards fails.
  1078. *
  1079. * ignore routes, they are useless, and heavily discouraged in rfc1123.
  1080. * imap4 reports them up to, but not including, the terminating :
  1081. */
  1082. e = s;
  1083. c = headChar(0);
  1084. if(c == '@'){
  1085. for(;;){
  1086. c = headChar(1);
  1087. if(c != '@'){
  1088. e = nil;
  1089. break;
  1090. }
  1091. headDomain(e);
  1092. c = headChar(1);
  1093. if(c != ','){
  1094. e = s;
  1095. break;
  1096. }
  1097. }
  1098. if(c != ':')
  1099. e = nil;
  1100. }
  1101. if(e != nil)
  1102. e = headAddrSpec(s, nil);
  1103. if(headChar(1) != '>')
  1104. e = nil;
  1105. }
  1106. /*
  1107. * e points to @host, or nil if an error occured
  1108. */
  1109. if(e == nil){
  1110. free(personal);
  1111. addr = nil;
  1112. }else{
  1113. if(*e != '\0')
  1114. *e++ = '\0';
  1115. else
  1116. e = site;
  1117. addr = MKZ(MAddr);
  1118. addr->personal = personal;
  1119. addr->box = estrdup(s);
  1120. addr->host = estrdup(e);
  1121. }
  1122. free(s);
  1123. return addr;
  1124. }
  1125. /*
  1126. * phrase : word
  1127. * | phrase word
  1128. * w is the optional initial word of the phrase
  1129. * returns the end of the phrase, or nil if a failure occured
  1130. */
  1131. static char*
  1132. headPhrase(char *e, char *w)
  1133. {
  1134. int c;
  1135. for(;;){
  1136. if(w == nil){
  1137. w = headWord();
  1138. if(w == nil)
  1139. return nil;
  1140. }
  1141. if(w[0] == '"')
  1142. stripQuotes(w);
  1143. strcpy(e, w);
  1144. free(w);
  1145. w = nil;
  1146. e = strchr(e, '\0');
  1147. c = headChar(0);
  1148. if(c <= ' ' || strchr(headAtomStop, c) != nil && c != '"')
  1149. break;
  1150. *e++ = ' ';
  1151. *e = '\0';
  1152. }
  1153. return e;
  1154. }
  1155. /*
  1156. * addr-spec : local-part '@' domain
  1157. * | local-part extension to allow ! and local names
  1158. * local-part : word
  1159. * | local-part '.' word
  1160. *
  1161. * if no '@' is present, rewrite d!e!f!u as @d,@e:u@f,
  1162. * where d, e, f are valid domain components.
  1163. * the @d,@e: is ignored, since routes are ignored.
  1164. * perhaps they should be rewritten as e!f!u@d, but that is inconsistent with upas.
  1165. *
  1166. * returns a pointer to '@', the end if none, or nil if there was an error
  1167. */
  1168. static char*
  1169. headAddrSpec(char *e, char *w)
  1170. {
  1171. char *s, *at, *b, *bang, *dom;
  1172. int c;
  1173. s = e;
  1174. for(;;){
  1175. if(w == nil){
  1176. w = headWord();
  1177. if(w == nil)
  1178. return nil;
  1179. }
  1180. strcpy(e, w);
  1181. free(w);
  1182. w = nil;
  1183. e = strchr(e, '\0');
  1184. lastWhite = headStr;
  1185. c = headChar(0);
  1186. if(c != '.')
  1187. break;
  1188. headChar(1);
  1189. *e++ = '.';
  1190. *e = '\0';
  1191. }
  1192. if(c != '@'){
  1193. /*
  1194. * extenstion: allow name without domain
  1195. * check for domain!xxx
  1196. */
  1197. bang = domBang(s);
  1198. if(bang == nil)
  1199. return e;
  1200. /*
  1201. * if dom1!dom2!xxx, ignore dom1!
  1202. */
  1203. dom = s;
  1204. for(; b = domBang(bang + 1); bang = b)
  1205. dom = bang + 1;
  1206. /*
  1207. * convert dom!mbox into mbox@dom
  1208. */
  1209. *bang = '@';
  1210. strrev(dom, bang);
  1211. strrev(bang+1, e);
  1212. strrev(dom, e);
  1213. bang = &dom[e - bang - 1];
  1214. if(dom > s){
  1215. bang -= dom - s;
  1216. for(e = s; *e = *dom; e++)
  1217. dom++;
  1218. }
  1219. /*
  1220. * eliminate a trailing '.'
  1221. */
  1222. if(e[-1] == '.')
  1223. e[-1] = '\0';
  1224. return bang;
  1225. }
  1226. headChar(1);
  1227. at = e;
  1228. *e++ = '@';
  1229. *e = '\0';
  1230. if(!headDomain(e))
  1231. return nil;
  1232. return at;
  1233. }
  1234. /*
  1235. * find the ! in domain!rest, where domain must have at least
  1236. * one internal '.'
  1237. */
  1238. static char*
  1239. domBang(char *s)
  1240. {
  1241. int dot, c;
  1242. dot = 0;
  1243. for(; c = *s; s++){
  1244. if(c == '!'){
  1245. if(!dot || dot == 1 && s[-1] == '.' || s[1] == '\0')
  1246. return nil;
  1247. return s;
  1248. }
  1249. if(c == '"')
  1250. break;
  1251. if(c == '.')
  1252. dot++;
  1253. }
  1254. return nil;
  1255. }
  1256. /*
  1257. * domain : sub-domain
  1258. * | domain '.' sub-domain
  1259. * returns the end of the domain, or nil if a failure occured
  1260. */
  1261. static char*
  1262. headDomain(char *e)
  1263. {
  1264. char *w;
  1265. for(;;){
  1266. w = headSubDomain();
  1267. if(w == nil)
  1268. return nil;
  1269. strcpy(e, w);
  1270. free(w);
  1271. e = strchr(e, '\0');
  1272. lastWhite = headStr;
  1273. if(headChar(0) != '.')
  1274. break;
  1275. headChar(1);
  1276. *e++ = '.';
  1277. *e = '\0';
  1278. }
  1279. return e;
  1280. }
  1281. /*
  1282. * id : 'content-id' ':' msg-id
  1283. * msg-id : '<' addr-spec '>'
  1284. */
  1285. static void
  1286. mimeId(Header *h)
  1287. {
  1288. char *s, *e, *w;
  1289. if(headChar(1) != ':')
  1290. return;
  1291. if(headChar(1) != '<')
  1292. return;
  1293. s = emalloc(strlen((char*)headStr) + 3);
  1294. e = s;
  1295. *e++ = '<';
  1296. e = headAddrSpec(e, nil);
  1297. if(e == nil || headChar(1) != '>'){
  1298. free(s);
  1299. return;
  1300. }
  1301. e = strchr(e, '\0');
  1302. *e++ = '>';
  1303. e[0] = '\0';
  1304. w = strdup(s);
  1305. free(s);
  1306. h->id = mkMimeHdr(w, nil, nil);
  1307. }
  1308. /*
  1309. * description : 'content-description' ':' *text
  1310. */
  1311. static void
  1312. mimeDescription(Header *h)
  1313. {
  1314. if(headChar(1) != ':')
  1315. return;
  1316. headSkipWhite(0);
  1317. h->description = mkMimeHdr(headText(), nil, nil);
  1318. }
  1319. /*
  1320. * disposition : 'content-disposition' ':' token params
  1321. */
  1322. static void
  1323. mimeDisposition(Header *h)
  1324. {
  1325. char *s;
  1326. if(headChar(1) != ':')
  1327. return;
  1328. s = headAtom(mimeTokenStop);
  1329. if(s == nil)
  1330. return;
  1331. h->disposition = mkMimeHdr(s, nil, mimeParams());
  1332. }
  1333. /*
  1334. * md5 : 'content-md5' ':' token
  1335. */
  1336. static void
  1337. mimeMd5(Header *h)
  1338. {
  1339. char *s;
  1340. if(headChar(1) != ':')
  1341. return;
  1342. s = headAtom(mimeTokenStop);
  1343. if(s == nil)
  1344. return;
  1345. h->md5 = mkMimeHdr(s, nil, nil);
  1346. }
  1347. /*
  1348. * language : 'content-language' ':' langs
  1349. * langs : token
  1350. * | langs commas token
  1351. * commas : ','
  1352. * | commas ','
  1353. */
  1354. static void
  1355. mimeLanguage(Header *h)
  1356. {
  1357. MimeHdr head, *last;
  1358. char *s;
  1359. head.next = nil;
  1360. last = &head;
  1361. for(;;){
  1362. s = headAtom(mimeTokenStop);
  1363. if(s == nil)
  1364. break;
  1365. last->next = mkMimeHdr(s, nil, nil);
  1366. last = last->next;
  1367. while(headChar(0) != ',')
  1368. headChar(1);
  1369. }
  1370. h->language = head.next;
  1371. }
  1372. /*
  1373. * token : 1*<char 33-255, except "()<>@,;:\\\"/[]?=" aka mimeTokenStop>
  1374. * atom : 1*<chars 33-255, except "()<>@,;:\\\".[]" aka headAtomStop>
  1375. * note this allows 8 bit characters, which occur in utf.
  1376. */
  1377. static char*
  1378. headAtom(char *disallowed)
  1379. {
  1380. char *s;
  1381. int c, ns, as;
  1382. headSkipWhite(0);
  1383. s = emalloc(StrAlloc);
  1384. as = StrAlloc;
  1385. ns = 0;
  1386. for(;;){
  1387. c = *headStr++;
  1388. if(c <= ' ' || strchr(disallowed, c) != nil){
  1389. headStr--;
  1390. break;
  1391. }
  1392. s[ns++] = c;
  1393. if(ns >= as){
  1394. as += StrAlloc;
  1395. s = erealloc(s, as);
  1396. }
  1397. }
  1398. if(ns == 0){
  1399. free(s);
  1400. return 0;
  1401. }
  1402. s[ns] = '\0';
  1403. return s;
  1404. }
  1405. /*
  1406. * sub-domain : atom | domain-lit
  1407. */
  1408. static char *
  1409. headSubDomain(void)
  1410. {
  1411. if(headChar(0) == '[')
  1412. return headQuoted('[', ']');
  1413. return headAtom(headAtomStop);
  1414. }
  1415. /*
  1416. * word : atom | quoted-str
  1417. */
  1418. static char *
  1419. headWord(void)
  1420. {
  1421. if(headChar(0) == '"')
  1422. return headQuoted('"', '"');
  1423. return headAtom(headAtomStop);
  1424. }
  1425. /*
  1426. * q is a quoted string. remove enclosing " and and \ escapes
  1427. */
  1428. static void
  1429. stripQuotes(char *q)
  1430. {
  1431. char *s;
  1432. int c;
  1433. if(q == nil)
  1434. return;
  1435. s = q++;
  1436. while(c = *q++){
  1437. if(c == '\\'){
  1438. c = *q++;
  1439. if(!c)
  1440. return;
  1441. }
  1442. *s++ = c;
  1443. }
  1444. s[-1] = '\0';
  1445. }
  1446. /*
  1447. * quoted-str : '"' *(any char but '"\\\r', or '\' any char, or linear-white-space) '"'
  1448. * domain-lit : '[' *(any char but '[]\\\r', or '\' any char, or linear-white-space) ']'
  1449. */
  1450. static char *
  1451. headQuoted(int start, int stop)
  1452. {
  1453. char *s;
  1454. int c, ns, as;
  1455. if(headChar(1) != start)
  1456. return nil;
  1457. s = emalloc(StrAlloc);
  1458. as = StrAlloc;
  1459. ns = 0;
  1460. s[ns++] = start;
  1461. for(;;){
  1462. c = *headStr;
  1463. if(c == stop){
  1464. headStr++;
  1465. break;
  1466. }
  1467. if(c == '\0'){
  1468. free(s);
  1469. return nil;
  1470. }
  1471. if(c == '\r'){
  1472. headStr++;
  1473. continue;
  1474. }
  1475. if(c == '\n'){
  1476. headStr++;
  1477. while(*headStr == ' ' || *headStr == '\t' || *headStr == '\r' || *headStr == '\n')
  1478. headStr++;
  1479. c = ' ';
  1480. }else if(c == '\\'){
  1481. headStr++;
  1482. s[ns++] = c;
  1483. c = *headStr;
  1484. if(c == '\0'){
  1485. free(s);
  1486. return nil;
  1487. }
  1488. headStr++;
  1489. }else
  1490. headStr++;
  1491. s[ns++] = c;
  1492. if(ns + 1 >= as){ /* leave room for \c or "0 */
  1493. as += StrAlloc;
  1494. s = erealloc(s, as);
  1495. }
  1496. }
  1497. s[ns++] = stop;
  1498. s[ns] = '\0';
  1499. return s;
  1500. }
  1501. /*
  1502. * headText : contents of rest of header line
  1503. */
  1504. static char *
  1505. headText(void)
  1506. {
  1507. uchar *v;
  1508. char *s;
  1509. v = headStr;
  1510. headToEnd();
  1511. s = emalloc(headStr - v + 1);
  1512. memmove(s, v, headStr - v);
  1513. s[headStr - v] = '\0';
  1514. return s;
  1515. }
  1516. /*
  1517. * white space is ' ' '\t' or nested comments.
  1518. * skip white space.
  1519. * if com and a comment is seen,
  1520. * return it's contents and stop processing white space.
  1521. */
  1522. static char*
  1523. headSkipWhite(int com)
  1524. {
  1525. char *s;
  1526. int c, incom, as, ns;
  1527. s = nil;
  1528. as = StrAlloc;
  1529. ns = 0;
  1530. if(com)
  1531. s = emalloc(StrAlloc);
  1532. incom = 0;
  1533. for(; c = *headStr; headStr++){
  1534. switch(c){
  1535. case ' ':
  1536. case '\t':
  1537. case '\r':
  1538. c = ' ';
  1539. break;
  1540. case '\n':
  1541. c = headStr[1];
  1542. if(c != ' ' && c != '\t')
  1543. goto breakout;
  1544. c = ' ';
  1545. break;
  1546. case '\\':
  1547. if(com && incom)
  1548. s[ns++] = c;
  1549. c = headStr[1];
  1550. if(c == '\0')
  1551. goto breakout;
  1552. headStr++;
  1553. break;
  1554. case '(':
  1555. incom++;
  1556. if(incom == 1)
  1557. continue;
  1558. break;
  1559. case ')':
  1560. incom--;
  1561. if(com && !incom){
  1562. s[ns] = '\0';
  1563. return s;
  1564. }
  1565. break;
  1566. default:
  1567. if(!incom)
  1568. goto breakout;
  1569. break;
  1570. }
  1571. if(com && incom && (c != ' ' || ns > 0 && s[ns-1] != ' ')){
  1572. s[ns++] = c;
  1573. if(ns + 1 >= as){ /* leave room for \c or 0 */
  1574. as += StrAlloc;
  1575. s = erealloc(s, as);
  1576. }
  1577. }
  1578. }
  1579. breakout:;
  1580. free(s);
  1581. return nil;
  1582. }
  1583. /*
  1584. * return the next non-white character
  1585. */
  1586. static int
  1587. headChar(int eat)
  1588. {
  1589. int c;
  1590. headSkipWhite(0);
  1591. c = *headStr;
  1592. if(eat && c != '\0' && c != '\n')
  1593. headStr++;
  1594. return c;
  1595. }
  1596. static void
  1597. headToEnd(void)
  1598. {
  1599. uchar *s;
  1600. int c;
  1601. for(;;){
  1602. s = headStr;
  1603. c = *s++;
  1604. while(c == '\r')
  1605. c = *s++;
  1606. if(c == '\n'){
  1607. c = *s++;
  1608. if(c != ' ' && c != '\t')
  1609. return;
  1610. }
  1611. if(c == '\0')
  1612. return;
  1613. headStr = s;
  1614. }
  1615. }
  1616. static void
  1617. headSkip(void)
  1618. {
  1619. int c;
  1620. while(c = *headStr){
  1621. headStr++;
  1622. if(c == '\n'){
  1623. c = *headStr;
  1624. if(c == ' ' || c == '\t')
  1625. continue;
  1626. return;
  1627. }
  1628. }
  1629. }