msg.c 31 KB

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