msg.c 30 KB

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