9 static void body64(int in, int out);
10 static void bodystrip(int in, int out);
11 static void cleanupHeader(Header *h);
12 static char *domBang(char *s);
13 static void freeMAddr(MAddr *a);
14 static void freeMimeHdr(MimeHdr *mh);
15 static char *headAddrSpec(char *e, char *w);
16 static MAddr *headAddresses(void);
17 static MAddr *headAddress(void);
18 static char *headAtom(char *disallowed);
19 static int headChar(int eat);
20 static char *headDomain(char *e);
21 static MAddr *headMAddr(MAddr *old);
22 static char *headPhrase(char *e, char *w);
23 static char *headQuoted(int start, int stop);
24 static char *headSkipWhite(int);
25 static void headSkip(void);
26 static char *headSubDomain(void);
27 static char *headText(void);
28 static void headToEnd(void);
29 static char *headWord(void);
30 static void mimeDescription(Header *h);
31 static void mimeDisposition(Header *h);
32 static void mimeEncoding(Header *h);
33 static void mimeId(Header *h);
34 static void mimeLanguage(Header *h);
35 static void mimeMd5(Header *h);
36 static MimeHdr *mimeParams(void);
37 static void mimeType(Header *h);
38 static MimeHdr *mkMimeHdr(char *s, char *t, MimeHdr *next);
39 static void msgAddDate(Msg *m);
40 static void msgAddHead(Msg *m, char *head, char *body);
41 static int msgBodySize(Msg *m);
42 static int msgHeader(Msg *m, Header *h, char *file);
43 static long msgReadFile(Msg *m, char *file, char **ss);
44 static int msgUnix(Msg *m, int top);
45 static void stripQuotes(char *q);
46 static MAddr *unixFrom(char *s);
49 static char bogusBody[] =
50 "This message contains null characters, so it cannot be displayed correctly.\r\n"
51 "Most likely you were sent a bogus message or a binary file.\r\n"
53 "Each of the following attachments has a different version of the message.\r\n"
54 "The first is inlined with all non-printable characters stripped.\r\n"
55 "The second contains the message as it was stored in your mailbox.\r\n"
56 "The third has the initial header stripped.\r\n";
58 static char bogusMimeText[] =
59 "Content-Disposition: inline\r\n"
60 "Content-Type: text/plain; charset=\"US-ASCII\"\r\n"
61 "Content-Transfer-Encoding: 7bit\r\n";
63 static char bogusMimeBinary[] =
64 "Content-Disposition: attachment\r\n"
65 "Content-Type: application/octet-stream\r\n"
66 "Content-Transfer-Encoding: base64\r\n";
69 * stop list for header fields
71 static char *headFieldStop = ":";
72 static char *mimeTokenStop = "()<>@,;:\\\"/[]?=";
73 static char *headAtomStop = "()<>@,;:\\\".[]";
74 static uchar *headStr;
75 static uchar *lastWhite;
78 selectFields(char *dst, long n, char *hdr, SList *fields, int matches)
85 headStr = (uchar*)hdr;
89 s = headAtom(headFieldStop);
93 for(f = fields; f != nil; f = f->next){
94 if(cistrcmp(s, f->s) == !matches){
98 memmove(&dst[m], start, nf);
119 if(m->replyTo != m->from)
120 freeMAddr(m->replyTo);
121 if(m->sender != m->from)
122 freeMAddr(m->sender);
123 if(m->from != m->unixFrom)
125 freeMAddr(m->unixFrom);
129 cleanupHeader(&m->head);
130 cleanupHeader(&m->mime);
131 for(k = m->kids; k != nil; ){
143 return m->head.size + m->size;
149 return s == nil || s[0] == '\0';
161 n = strlen(a->box) + strlen(host) + 2;
162 if(a->personal != nil)
163 n += strlen(a->personal) + 3;
165 if(a->personal != nil)
166 snprint(addr, n, "%s <%s@%s>", a->personal, a->box, host);
168 snprint(addr, n, "%s@%s", a->box, host);
173 * return actual name of f in m's fs directory
174 * this is special cased when opening m/rawbody, m/mimeheader, or m/rawheader,
175 * if the message was corrupted. in that case,
176 * a temporary file is made to hold the base64 encoding of m/raw.
179 msgFile(Msg *m, char *f)
184 char buf[64], nbuf[2];
186 int i, n, fd, fd1, fd2;
189 || strcmp(f, "") != 0 && strcmp(f, "rawbody") != 0
190 && strcmp(f, "rawheader") != 0 && strcmp(f, "mimeheader") != 0
191 && strcmp(f, "info") != 0 && strcmp(f, "unixheader") != 0){
192 if(strlen(f) > MsgNameLen)
193 bye("internal error: msgFile name too long");
195 return cdOpen(m->fsDir, m->fs, OREAD);
199 * walk up the stupid runt message parts for non-multipart messages
202 if(parent != nil && parent->parent != nil){
210 if(strcmp(f, "info") == 0 || strcmp(f, "unixheader") == 0){
212 return cdOpen(p->fsDir, p->fs, OREAD);
220 * craft the message parts for bogus messages
222 if(strcmp(f, "") == 0){
224 * make a fake directory for each kid
225 * all we care about is the name
233 for(i = '1'; i <= '4'; i++){
235 n = convD2M(&d, dbuf, sizeof(dbuf));
237 fprint(2, "bad convD2M %d\n", n);
241 }else if(strcmp(f, "mimeheader") == 0){
246 fprint(fd, "%s", bogusMimeText);
250 fprint(fd, "%s", bogusMimeBinary);
254 }else if(strcmp(f, "rawheader") == 0){
256 date2tm(&tm, m->unixDate);
257 rfc822date(buf, sizeof(buf), &tm);
260 "From: imap4 daemon <%s@%s>\r\n"
262 "Subject: This message was illegal or corrupted\r\n"
263 "MIME-Version: 1.0\r\n"
264 "Content-Type: multipart/mixed;\r\n\tboundary=\"upas-%s\"\r\n",
265 buf, username, site, username, site, m->info[IDigest]);
267 }else if(strcmp(f, "rawbody") == 0){
268 fd1 = msgFile(p, "raw");
269 strcpy(p->efs, "rawbody");
270 fd2 = cdOpen(p->fsDir, p->fs, OREAD);
271 if(fd1 < 0 || fd2 < 0){
279 "This is a multi-part message in MIME format.\r\n"
285 m->info[IDigest], bogusMimeText, bogusBody);
291 m->info[IDigest], bogusMimeText);
298 m->info[IDigest], bogusMimeBinary);
306 m->info[IDigest], bogusMimeBinary);
309 fprint(fd, "--upas-%s--\r\n", m->info[IDigest]);
313 fprint(fd, "%s", bogusBody);
334 msgIsMulti(Header *h)
336 return h->type != nil && cistrcmp("multipart", h->type->s) == 0;
340 msgIsRfc822(Header *h)
342 return h->type != nil && cistrcmp("message", h->type->s) == 0 && cistrcmp("rfc822", h->type->t) == 0;
346 * check if a message has been deleted by someone else
354 if(!cdExists(m->fsDir, m->fs))
359 * make sure the message has valid associated info
360 * used for ISubject, IDigest, IInReplyTo, IMessageId.
368 if(m->info[0] != nil)
371 i = msgReadFile(m, "info", &m->iBuf);
376 for(i = 0; i < IMax; i++){
386 for(i = 0; i < IMax; i++)
387 if(infoIsNil(m->info[i]))
394 * make sure the message has valid mime structure
398 msgStruct(Msg *m, int top)
413 || !msgHeader(m, &m->mime, "mimeheader")
414 || (top || msgIsRfc822(&m->mime) || msgIsMulti(&m->mime)) && !msgHeader(m, &m->head, "rawheader")){
415 if(top && m->bogus && !(m->bogus & BogusTried)){
416 m->bogus |= BogusTried;
417 return msgStruct(m, top);
424 * if a message has no kids, it has a kid which is just the body of the real message
426 if(!msgIsMulti(&m->head) && !msgIsMulti(&m->mime) && !msgIsRfc822(&m->head) && !msgIsRfc822(&m->mime)){
431 k->parent = m->parent;
433 k->fs = emalloc(ns + (MsgNameLen + 1));
434 memmove(k->fs, m->fs, ns);
443 * read in all child messages messages
454 while((nd = dirread(fd, &d)) > 0){
455 for(i = 0; i < nd; i++){
457 id = strtol(s, &s, 10);
458 if(id <= max || *s != '\0'
459 || (d[i].mode & DMDIR) != DMDIR)
470 k->fs = emalloc(ns + 2 * (MsgNameLen + 1));
471 k->efs = seprint(k->fs, k->fs + ns + (MsgNameLen + 1), "%s%lud/", m->fs, id);
483 * if kids fail, just whack them
485 top = top && (msgIsRfc822(&m->head) || msgIsMulti(&m->head));
486 for(k = m->kids; k != nil; k = k->next){
487 if(!msgStruct(k, top)){
488 for(k = m->kids; k != nil; ){
501 msgReadFile(Msg *m, char *file, char **ss)
504 char *s, buf[BufSize];
509 fd = msgFile(m, file);
515 n = read(fd, buf, BufSize);
540 nn = readn(fd, s+n, nn-n) + n;
567 * the message is corrupted or illegal.
568 * reset message fields. msgStruct will reparse the message,
569 * relying on msgFile to make up corrected body parts.
572 msgBogus(Msg *m, int flags)
574 if(!(m->bogus & flags))
579 memset(&m->head, 0, sizeof(Header));
580 memset(&m->mime, 0, sizeof(Header));
585 * stolen from upas/marshal; base64 encodes from one fd to another.
587 * the size of buf is very important to enc64. Anything other than
588 * a multiple of 3 will cause enc64 to output a termination sequence.
589 * To ensure that a full buf corresponds to a multiple of complete lines,
590 * we make buf a multiple of 3*18 since that's how many enc64 sticks on
591 * a single line. This avoids short lines in the output which is pleasing
595 enc64x18(char *out, int lim, uchar *in, int n)
600 for(; n > 0; n -= m){
604 mm = enc64(out, lim - nn, in, m);
615 body64(int in, int out)
618 char obuf[3*18*54*2];
622 n = read(in, buf, sizeof(buf));
627 m = enc64x18(obuf, sizeof(obuf), buf, n);
628 if(write(out, obuf, m) < 0)
634 * strip all non-printable characters from a file
637 bodystrip(int in, int out)
643 n = read(in, buf, sizeof(buf));
649 for(i = 0; i < n; i++){
651 if(c > 0x1f && c < 0x7f /* normal characters */
652 || c >= 0x9 && c <= 0xd) /* \t, \n, vertical tab, form feed, \r */
656 if(m && write(out, buf, m) < 0)
662 * read in the message body to count \n without a preceding \r
668 char buf[BufSize + 2], *s, *se;
670 ulong size, lines, bad;
675 fd = msgFile(m, "rawbody");
691 n = read(fd, &buf[1], BufSize);
696 for(s = &buf[1]; s < se; s++){
700 return msgBogus(m, BogusBody);
711 bye("bad length reading rawbody");
720 * retrieve information from the unixheader file
723 msgUnix(Msg *m, int top)
728 if(m->unixDate != nil)
733 m->unixDate = estrdup("");
734 m->unixFrom = unixFrom(nil);
738 if(msgReadFile(m, "unixheader", &ss) < 0)
747 m->unixFrom = unixFrom(s);
749 if(date2tm(&tm, s) == nil)
750 s = m->info[IUnixDate];
755 m->unixDate = estrdup(s);
761 * parse the address in the unix header
762 * last line of defence, so must return something
773 t = emalloc(strlen(s) + 2);
774 e = headAddrSpec(t, nil);
785 a->host = estrdup(e);
792 * read in the entire header,
793 * and parse out any existing mime headers
796 msgHeader(Msg *m, Header *h, char *file)
798 char *s, *ss, *t, *te;
806 ns = msgReadFile(m, file, &ss);
813 * count lines ending with \n and \r\n
814 * add an extra line at the end, since upas/fs headers
815 * don't have a terminating \r\n
819 for(t = s; t < te; t++){
822 return msgBogus(m, BogusHeader);
825 if(t == s || t[-1] != '\r')
829 if(t > s && t[-1] != '\n'){
835 h->buf = emalloc(n + 1);
840 * make sure all headers end in \r\n
843 for(t = s; t < te; t++){
846 if(!nn || h->buf[nn - 1] != '\r')
852 if(nn && h->buf[nn-1] != '\n'){
853 if(h->buf[nn-1] != '\r')
861 bye("misconverted header %ld %ld", nn, n);
865 * and parse some mime headers
867 headStr = (uchar*)h->buf;
869 while(s = headAtom(headFieldStop)){
870 if(cistrcmp(s, "content-type") == 0)
872 else if(cistrcmp(s, "content-transfer-encoding") == 0)
874 else if(cistrcmp(s, "content-id") == 0)
876 else if(cistrcmp(s, "content-description") == 0)
878 else if(cistrcmp(s, "content-disposition") == 0)
880 else if(cistrcmp(s, "content-md5") == 0)
882 else if(cistrcmp(s, "content-language") == 0)
884 else if(h == &m->head && cistrcmp(s, "from") == 0)
885 m->from = headMAddr(m->from);
886 else if(h == &m->head && cistrcmp(s, "to") == 0)
887 m->to = headMAddr(m->to);
888 else if(h == &m->head && cistrcmp(s, "reply-to") == 0)
889 m->replyTo = headMAddr(m->replyTo);
890 else if(h == &m->head && cistrcmp(s, "sender") == 0)
891 m->sender = headMAddr(m->sender);
892 else if(h == &m->head && cistrcmp(s, "cc") == 0)
893 m->cc = headMAddr(m->cc);
894 else if(h == &m->head && cistrcmp(s, "bcc") == 0)
895 m->bcc = headMAddr(m->bcc);
896 else if(h == &m->head && cistrcmp(s, "date") == 0)
904 m->from = m->unixFrom;
906 s = maddrStr(m->from);
907 msgAddHead(m, "From", s);
913 if(m->replyTo == nil)
914 m->replyTo = m->from;
916 if(infoIsNil(m->info[IDate]))
917 m->info[IDate] = m->unixDate;
918 if(!dated && m->from != nil)
925 * prepend head: body to the cached header
928 msgAddHead(Msg *m, char *head, char *body)
933 n = strlen(head) + strlen(body) + 4;
934 size = m->head.size + n;
935 s = emalloc(size + 1);
936 snprint(s, size + 1, "%s: %s\r\n%s", head, body, m->head.buf);
949 /* don't bother if we don't have a date */
950 if(infoIsNil(m->info[IDate]))
953 date2tm(&tm, m->info[IDate]);
954 rfc822date(buf, sizeof(buf), &tm);
955 msgAddHead(m, "Date", buf);
959 mkMimeHdr(char *s, char *t, MimeHdr *next)
971 freeMimeHdr(MimeHdr *mh)
985 cleanupHeader(Header *h)
987 freeMimeHdr(h->type);
989 freeMimeHdr(h->description);
990 freeMimeHdr(h->encoding);
992 freeMimeHdr(h->disposition);
993 freeMimeHdr(h->language);
997 * parser for rfc822 & mime header fields
1001 * type : 'content-type' ':' token '/' token params
1008 if(headChar(1) != ':')
1010 s = headAtom(mimeTokenStop);
1011 if(s == nil || headChar(1) != '/'){
1015 t = headAtom(mimeTokenStop);
1020 h->type = mkMimeHdr(s, t, mimeParams());
1025 * | params ';' token '=' token
1026 * | params ';' token '=' quoted-str
1031 MimeHdr head, *last;
1037 if(headChar(1) != ';')
1039 s = headAtom(mimeTokenStop);
1040 if(s == nil || headChar(1) != '='){
1044 if(headChar(0) == '"'){
1045 t = headQuoted('"', '"');
1048 t = headAtom(mimeTokenStop);
1053 last->next = mkMimeHdr(s, t, nil);
1060 * encoding : 'content-transfer-encoding' ':' token
1063 mimeEncoding(Header *h)
1067 if(headChar(1) != ':')
1069 s = headAtom(mimeTokenStop);
1072 h->encoding = mkMimeHdr(s, nil, nil);
1076 * mailaddr : ':' addresses
1079 headMAddr(MAddr *old)
1083 if(headChar(1) != ':')
1086 if(headChar(0) == '\n')
1089 a = headAddresses();
1098 * addresses : address | addresses ',' address
1103 MAddr *addr, *tail, *a;
1105 addr = headAddress();
1109 while(headChar(0) == ','){
1123 * address : mailbox | group
1124 * group : phrase ':' mboxes ';' | phrase ':' ';'
1125 * mailbox : addr-spec
1126 * | optphrase '<' addr-spec '>'
1127 * | optphrase '<' route ':' addr-spec '>'
1128 * optphrase : | phrase
1129 * route : '@' domain
1130 * | route ',' '@' domain
1131 * personal names are the phrase before '<',
1132 * or a comment before or after a simple addr-spec
1139 char *s, *e, *w, *personal;
1142 s = emalloc(strlen((char*)headStr) + 2);
1144 personal = headSkipWhite(1);
1152 if(c == '.' || c == '@' || c == ',' || c == '\n' || c == '\0'){
1153 lastWhite = headStr;
1154 e = headAddrSpec(s, w);
1155 if(personal == nil){
1157 headStr = lastWhite;
1158 personal = headSkipWhite(1);
1162 if(c != '<' || w != nil){
1164 if(!headPhrase(e, w)){
1170 * ignore addresses with groups,
1171 * so the only thing left if <
1178 personal = estrdup(s);
1183 * after this point, we need to free personal before returning.
1184 * set e to nil to everything afterwards fails.
1186 * ignore routes, they are useless, and heavily discouraged in rfc1123.
1187 * imap4 reports them up to, but not including, the terminating :
1210 e = headAddrSpec(s, nil);
1211 if(headChar(1) != '>')
1216 * e points to @host, or nil if an error occured
1228 addr->personal = personal;
1229 addr->box = estrdup(s);
1230 addr->host = estrdup(e);
1239 * w is the optional initial word of the phrase
1240 * returns the end of the phrase, or nil if a failure occured
1243 headPhrase(char *e, char *w)
1258 e = strchr(e, '\0');
1260 if(c <= ' ' || strchr(headAtomStop, c) != nil && c != '"')
1269 * addr-spec : local-part '@' domain
1270 * | local-part extension to allow ! and local names
1272 * | local-part '.' word
1274 * if no '@' is present, rewrite d!e!f!u as @d,@e:u@f,
1275 * where d, e, f are valid domain components.
1276 * the @d,@e: is ignored, since routes are ignored.
1277 * perhaps they should be rewritten as e!f!u@d, but that is inconsistent with upas.
1279 * returns a pointer to '@', the end if none, or nil if there was an error
1282 headAddrSpec(char *e, char *w)
1284 char *s, *at, *b, *bang, *dom;
1297 e = strchr(e, '\0');
1298 lastWhite = headStr;
1309 * extenstion: allow name without domain
1310 * check for domain!xxx
1317 * if dom1!dom2!xxx, ignore dom1!
1320 for(; b = domBang(bang + 1); bang = b)
1324 * convert dom!mbox into mbox@dom
1330 bang = &dom[e - bang - 1];
1333 for(e = s; *e = *dom; e++)
1338 * eliminate a trailing '.'
1355 * find the ! in domain!rest, where domain must have at least
1366 if(!dot || dot == 1 && s[-1] == '.' || s[1] == '\0')
1379 * domain : sub-domain
1380 * | domain '.' sub-domain
1381 * returns the end of the domain, or nil if a failure occured
1389 w = headSubDomain();
1394 e = strchr(e, '\0');
1395 lastWhite = headStr;
1396 if(headChar(0) != '.')
1406 * id : 'content-id' ':' msg-id
1407 * msg-id : '<' addr-spec '>'
1414 if(headChar(1) != ':')
1416 if(headChar(1) != '<')
1419 s = emalloc(strlen((char*)headStr) + 3);
1422 e = headAddrSpec(e, nil);
1423 if(e == nil || headChar(1) != '>'){
1427 e = strchr(e, '\0');
1432 h->id = mkMimeHdr(w, nil, nil);
1436 * description : 'content-description' ':' *text
1439 mimeDescription(Header *h)
1441 if(headChar(1) != ':')
1444 h->description = mkMimeHdr(headText(), nil, nil);
1448 * disposition : 'content-disposition' ':' token params
1451 mimeDisposition(Header *h)
1455 if(headChar(1) != ':')
1457 s = headAtom(mimeTokenStop);
1460 h->disposition = mkMimeHdr(s, nil, mimeParams());
1464 * md5 : 'content-md5' ':' token
1471 if(headChar(1) != ':')
1473 s = headAtom(mimeTokenStop);
1476 h->md5 = mkMimeHdr(s, nil, nil);
1480 * language : 'content-language' ':' langs
1482 * | langs commas token
1487 mimeLanguage(Header *h)
1489 MimeHdr head, *last;
1495 s = headAtom(mimeTokenStop);
1498 last->next = mkMimeHdr(s, nil, nil);
1500 while(headChar(0) != ',')
1503 h->language = head.next;
1507 * token : 1*<char 33-255, except "()<>@,;:\\\"/[]?=" aka mimeTokenStop>
1508 * atom : 1*<chars 33-255, except "()<>@,;:\\\".[]" aka headAtomStop>
1509 * note this allows 8 bit characters, which occur in utf.
1512 headAtom(char *disallowed)
1519 s = emalloc(StrAlloc);
1524 if(c <= ' ' || strchr(disallowed, c) != nil){
1531 s = erealloc(s, as);
1543 * sub-domain : atom | domain-lit
1548 if(headChar(0) == '[')
1549 return headQuoted('[', ']');
1550 return headAtom(headAtomStop);
1554 * word : atom | quoted-str
1559 if(headChar(0) == '"')
1560 return headQuoted('"', '"');
1561 return headAtom(headAtomStop);
1565 * q is a quoted string. remove enclosing " and and \ escapes
1568 stripQuotes(char *q)
1588 * quoted-str : '"' *(any char but '"\\\r', or '\' any char, or linear-white-space) '"'
1589 * domain-lit : '[' *(any char but '[]\\\r', or '\' any char, or linear-white-space) ']'
1592 headQuoted(int start, int stop)
1597 if(headChar(1) != start)
1599 s = emalloc(StrAlloc);
1619 while(*headStr == ' ' || *headStr == '\t' || *headStr == '\r' || *headStr == '\n')
1622 }else if(c == '\\'){
1634 if(ns + 1 >= as){ /* leave room for \c or "0 */
1636 s = erealloc(s, as);
1645 * headText : contents of rest of header line
1655 s = emalloc(headStr - v + 1);
1656 memmove(s, v, headStr - v);
1657 s[headStr - v] = '\0';
1662 * white space is ' ' '\t' or nested comments.
1664 * if com and a comment is seen,
1665 * return it's contents and stop processing white space.
1668 headSkipWhite(int com)
1671 int c, incom, as, ns;
1677 s = emalloc(StrAlloc);
1679 for(; c = *headStr; headStr++){
1688 if(c != ' ' && c != '\t')
1717 if(com && incom && (c != ' ' || ns > 0 && s[ns-1] != ' ')){
1719 if(ns + 1 >= as){ /* leave room for \c or 0 */
1721 s = erealloc(s, as);
1731 * return the next non-white character
1740 if(eat && c != '\0' && c != '\n')
1758 if(c != ' ' && c != '\t')
1772 while(c = *headStr){
1776 if(c == ' ' || c == '\t')