167545Seric /* 267545Seric * Copyright (c) 1994 Eric P. Allman 367545Seric * Copyright (c) 1994 467545Seric * The Regents of the University of California. All rights reserved. 567545Seric * 667545Seric * %sccs.include.redist.c% 767545Seric */ 867545Seric 967545Seric # include "sendmail.h" 1067545Seric # include <string.h> 1167545Seric 1267545Seric #ifndef lint 13*68860Seric static char sccsid[] = "@(#)mime.c 8.16 (Berkeley) 04/23/95"; 1467545Seric #endif /* not lint */ 1567545Seric 1667545Seric /* 1767545Seric ** MIME support. 1867545Seric ** 1967545Seric ** I am indebted to John Beck of Hewlett-Packard, who contributed 2067545Seric ** his code to me for inclusion. As it turns out, I did not use 2167545Seric ** his code since he used a "minimum change" approach that used 2267545Seric ** several temp files, and I wanted a "minimum impact" approach 2367545Seric ** that would avoid copying. However, looking over his code 2467545Seric ** helped me cement my understanding of the problem. 2567545Seric ** 2667545Seric ** I also looked at, but did not directly use, Nathaniel 2767545Seric ** Borenstein's "code.c" module. Again, it functioned as 2867545Seric ** a file-to-file translator, which did not fit within my 2967545Seric ** design bounds, but it was a useful base for understanding 3067545Seric ** the problem. 3167545Seric */ 3267545Seric 3367545Seric 3467545Seric /* character set for hex and base64 encoding */ 3567545Seric char Base16Code[] = "0123456789ABCDEF"; 3667545Seric char Base64Code[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 3767545Seric 3867545Seric /* types of MIME boundaries */ 3967545Seric #define MBT_SYNTAX 0 /* syntax error */ 4067545Seric #define MBT_NOTSEP 1 /* not a boundary */ 4167545Seric #define MBT_INTERMED 2 /* intermediate boundary (no trailing --) */ 4267545Seric #define MBT_FINAL 3 /* final boundary (trailing -- included) */ 4367547Seric 4468717Seric static char *MimeBoundaryNames[] = 4568717Seric { 4668717Seric "SYNTAX", "NOTSEP", "INTERMED", "FINAL" 4768717Seric }; 4867545Seric /* 4967545Seric ** MIME8TO7 -- output 8 bit body in 7 bit format 5067545Seric ** 5167545Seric ** The header has already been output -- this has to do the 5267545Seric ** 8 to 7 bit conversion. It would be easy if we didn't have 5367545Seric ** to deal with nested formats (multipart/xxx and message/rfc822). 5467545Seric ** 5567545Seric ** We won't be called if we don't have to do a conversion, and 5667545Seric ** appropriate MIME-Version: and Content-Type: fields have been 5767545Seric ** output. Any Content-Transfer-Encoding: field has not been 5867545Seric ** output, and we can add it here. 5967545Seric ** 6067545Seric ** Parameters: 6167545Seric ** mci -- mailer connection information. 6267545Seric ** header -- the header for this body part. 6367545Seric ** e -- envelope. 6468517Seric ** boundaries -- the currently pending message boundaries. 6568517Seric ** NULL if we are processing the outer portion. 6668517Seric ** flags -- to tweak processing. 6767545Seric ** 6867545Seric ** Returns: 6967545Seric ** An indicator of what terminated the message part: 7067545Seric ** MBT_FINAL -- the final boundary 7167545Seric ** MBT_INTERMED -- an intermediate boundary 7267545Seric ** MBT_NOTSEP -- an end of file 7367545Seric */ 7467545Seric 7568517Seric struct args 7668517Seric { 7768517Seric char *field; /* name of field */ 7868517Seric char *value; /* value of that field */ 7968517Seric }; 8068517Seric 8167545Seric int 8268517Seric mime8to7(mci, header, e, boundaries, flags) 8367545Seric register MCI *mci; 8468717Seric HDR *header; 8568717Seric register ENVELOPE *e; 8668517Seric char **boundaries; 8768517Seric int flags; 8867545Seric { 8967545Seric register char *p; 9067545Seric int linelen; 9167545Seric int bt; 9267545Seric off_t offset; 9367545Seric size_t sectionsize, sectionhighbits; 9468517Seric int i; 9568517Seric char *type; 9668517Seric char *subtype; 9768847Seric char *cte; 9868517Seric char **pvp; 9968517Seric int argc = 0; 10068517Seric struct args argv[MAXMIMEARGS]; 10167545Seric char bbuf[128]; 10267545Seric char buf[MAXLINE]; 10368517Seric char pvpbuf[MAXLINE]; 10468711Seric extern char MimeTokenTab[256]; 10567545Seric 10667545Seric if (tTd(43, 1)) 10767545Seric { 10868717Seric printf("mime8to7: flags = %x, boundaries =", flags); 10968717Seric if (boundaries[0] == NULL) 11068717Seric printf(" <none>"); 11168717Seric else 11268717Seric { 11368717Seric for (i = 0; boundaries[i] != NULL; i++) 11468717Seric printf(" %s", boundaries[i]); 11568717Seric } 11668717Seric printf("\n"); 11767545Seric } 11868847Seric type = subtype = NULL; 11967545Seric p = hvalue("Content-Type", header); 12068847Seric if (p == NULL) 12168847Seric { 12268847Seric if (bitset(M87F_DIGEST, flags)) 12368847Seric p = "message/rfc822"; 12468847Seric else 12568847Seric p = "text/plain"; 12668847Seric } 12768517Seric if (p != NULL && 12868711Seric (pvp = prescan(p, '\0', pvpbuf, sizeof pvpbuf, NULL, 12968711Seric MimeTokenTab)) != NULL && 13068517Seric pvp[0] != NULL) 13167545Seric { 13268717Seric if (tTd(43, 40)) 13368717Seric { 13468717Seric for (i = 0; pvp[i] != NULL; i++) 13568717Seric printf("pvp[%d] = \"%s\"\n", i, pvp[i]); 13668717Seric } 13768517Seric type = *pvp++; 13868517Seric if (*pvp != NULL && strcmp(*pvp, "/") == 0 && 13968517Seric *++pvp != NULL) 14068517Seric { 14168517Seric subtype = *pvp++; 14268517Seric } 14368517Seric 14468517Seric /* break out parameters */ 14568517Seric while (*pvp != NULL && argc < MAXMIMEARGS) 14668517Seric { 14768517Seric /* skip to semicolon separator */ 14868517Seric while (*pvp != NULL && strcmp(*pvp, ";") != 0) 14968517Seric pvp++; 15068517Seric if (*pvp++ == NULL || *pvp == NULL) 15168517Seric break; 15268517Seric 15368517Seric /* extract field name */ 15468517Seric argv[argc].field = *pvp++; 15568517Seric 15668517Seric /* see if there is a value */ 15768517Seric if (*pvp != NULL && strcmp(*pvp, "=") == 0 && 15868517Seric (*++pvp == NULL || strcmp(*pvp, ";") != 0)) 15968517Seric { 16068517Seric argv[argc].value = *pvp; 16168517Seric argc++; 16268517Seric } 16368517Seric } 16468517Seric } 16568717Seric 16668847Seric /* check for disaster cases */ 16768847Seric if (type == NULL) 16868847Seric type = "-none-"; 16968847Seric if (subtype == NULL) 17068847Seric subtype = "-none-"; 17168847Seric 17268847Seric /* 17368847Seric ** Check for cases that can not be encoded. 17468847Seric ** 17568847Seric ** For example, you can't encode certain kinds of types 17668847Seric ** or already-encoded messages. If we find this case, 17768847Seric ** just copy it through. 17868847Seric */ 17968847Seric 18068847Seric cte = hvalue("content-transfer-encoding", header); 18168717Seric sprintf(buf, "%s/%s", type, subtype); 18268847Seric if (wordinclass(buf, 'n') || (cte != NULL && !wordinclass(cte, 'e'))) 18368717Seric flags |= M87F_NO8BIT; 18468717Seric 18568717Seric /* 18668717Seric ** Multipart requires special processing. 18768717Seric ** 18868717Seric ** Do a recursive descent into the message. 18968717Seric */ 19068717Seric 19168517Seric if (strcasecmp(type, "multipart") == 0) 19268517Seric { 19367545Seric register char *q; 19467545Seric 19568847Seric if (strcasecmp(subtype, "digest") == 0) 19668847Seric flags |= M87F_DIGEST; 19768847Seric 19868517Seric for (i = 0; i < argc; i++) 19967545Seric { 20068517Seric if (strcasecmp(argv[i].field, "boundary") == 0) 20168517Seric break; 20268517Seric } 20368517Seric if (i >= argc) 20468517Seric { 20567545Seric syserr("mime8to7: Content-Type: %s missing boundary", p); 20667545Seric p = "---"; 20767545Seric } 20867545Seric else 20968517Seric p = argv[i].value; 21067545Seric if (*p == '"') 21168711Seric q = strchr(++p, '"'); 21267545Seric else 21367545Seric q = p + strlen(p); 21467545Seric if (q - p > sizeof bbuf - 1) 21567545Seric { 21667545Seric syserr("mime8to7: multipart boundary \"%.*s\" too long", 21767545Seric q - p, p); 21867545Seric q = p + sizeof bbuf - 1; 21967545Seric } 22067545Seric strncpy(bbuf, p, q - p); 22167545Seric bbuf[q - p] = '\0'; 22267545Seric if (tTd(43, 1)) 22367545Seric printf("mime8to7: multipart boundary \"%s\"\n", bbuf); 22468517Seric for (i = 0; i < MAXMIMENESTING; i++) 22568517Seric if (boundaries[i] == NULL) 22668517Seric break; 22768517Seric if (i >= MAXMIMENESTING) 22868517Seric syserr("mime8to7: multipart nesting boundary too deep"); 22968517Seric else 23068517Seric { 23168517Seric boundaries[i] = bbuf; 23268517Seric boundaries[i + 1] = NULL; 23368517Seric } 23467545Seric 23567545Seric /* skip the early "comment" prologue */ 23668717Seric putline("", mci); 23767545Seric while (fgets(buf, sizeof buf, e->e_dfp) != NULL) 23867545Seric { 23968517Seric bt = mimeboundary(buf, boundaries); 24067545Seric if (bt != MBT_NOTSEP) 24167545Seric break; 24268847Seric putxline(buf, mci, PXLF_MAPFROM|PXLF_STRIP8BIT); 24368717Seric if (tTd(43, 99)) 24468717Seric printf(" ...%s", buf); 24567545Seric } 24668717Seric if (feof(e->e_dfp)) 24768717Seric bt = MBT_FINAL; 24867545Seric while (bt != MBT_FINAL) 24967545Seric { 25067545Seric auto HDR *hdr = NULL; 25167545Seric 25267545Seric sprintf(buf, "--%s", bbuf); 25367545Seric putline(buf, mci); 25468717Seric if (tTd(43, 35)) 25568717Seric printf(" ...%s\n", buf); 25667545Seric collect(e->e_dfp, FALSE, FALSE, &hdr, e); 25768717Seric if (tTd(43, 101)) 25868717Seric putline("+++after collect", mci); 25967936Seric putheader(mci, hdr, e, 0); 26068717Seric if (tTd(43, 101)) 26168717Seric putline("+++after putheader", mci); 26268517Seric bt = mime8to7(mci, hdr, e, boundaries, flags); 26367545Seric } 26467545Seric sprintf(buf, "--%s--", bbuf); 26567545Seric putline(buf, mci); 26668717Seric if (tTd(43, 35)) 26768717Seric printf(" ...%s\n", buf); 26868717Seric boundaries[i] = NULL; 26967545Seric 27067545Seric /* skip the late "comment" epilogue */ 27167545Seric while (fgets(buf, sizeof buf, e->e_dfp) != NULL) 27267545Seric { 27368517Seric bt = mimeboundary(buf, boundaries); 27467545Seric if (bt != MBT_NOTSEP) 27567545Seric break; 27668847Seric putxline(buf, mci, PXLF_MAPFROM|PXLF_STRIP8BIT); 27768717Seric if (tTd(43, 99)) 27868717Seric printf(" ...%s", buf); 27967545Seric } 28068717Seric if (feof(e->e_dfp)) 28168717Seric bt = MBT_FINAL; 28268717Seric if (tTd(43, 3)) 28368717Seric printf("\t\t\tmime8to7=>%s (multipart)\n", 28468717Seric MimeBoundaryNames[bt]); 28567545Seric return bt; 28667545Seric } 28767545Seric 28867545Seric /* 28968847Seric ** Message/* types -- recurse exactly once. 29068847Seric */ 29168847Seric 29268847Seric if (strcasecmp(type, "message") == 0) 29368847Seric { 29468847Seric register char *q; 29568847Seric auto HDR *hdr = NULL; 29668847Seric 29768847Seric putline("", mci); 29868847Seric 29968847Seric collect(e->e_dfp, FALSE, FALSE, &hdr, e); 30068847Seric if (tTd(43, 101)) 30168847Seric putline("+++after collect", mci); 30268847Seric putheader(mci, hdr, e, 0); 30368847Seric if (tTd(43, 101)) 30468847Seric putline("+++after putheader", mci); 30568847Seric bt = mime8to7(mci, hdr, e, boundaries, flags); 30668847Seric return bt; 30768847Seric } 30868847Seric 30968847Seric /* 31067545Seric ** Non-compound body type 31167545Seric ** 31267545Seric ** Compute the ratio of seven to eight bit characters; 31367545Seric ** use that as a heuristic to decide how to do the 31467545Seric ** encoding. 31567545Seric */ 31667545Seric 31767545Seric sectionsize = sectionhighbits = 0; 31868517Seric if (!bitset(M87F_NO8BIT, flags)) 31967545Seric { 32068515Seric /* remember where we were */ 32168515Seric offset = ftell(e->e_dfp); 32268515Seric if (offset == -1) 32368564Seric syserr("mime8to7: cannot ftell on df%s", e->e_id); 32468515Seric 32568515Seric /* do a scan of this body type to count character types */ 32668515Seric while (fgets(buf, sizeof buf, e->e_dfp) != NULL) 32767545Seric { 32868717Seric if (mimeboundary(buf, boundaries) != MBT_NOTSEP) 32968515Seric break; 33068515Seric for (p = buf; *p != '\0'; p++) 33168515Seric { 33268515Seric /* count bytes with the high bit set */ 33368515Seric sectionsize++; 33468515Seric if (bitset(0200, *p)) 33568515Seric sectionhighbits++; 33668515Seric } 33768515Seric 33868515Seric /* 33968515Seric ** Heuristic: if 1/4 of the first 4K bytes are 8-bit, 34068515Seric ** assume base64. This heuristic avoids double-reading 34168515Seric ** large graphics or video files. 34268515Seric */ 34368515Seric 34468515Seric if (sectionsize >= 4096 && 34568515Seric sectionhighbits > sectionsize / 4) 34668515Seric break; 34767545Seric } 34867547Seric 34968515Seric /* return to the original offset for processing */ 35068515Seric /* XXX use relative seeks to handle >31 bit file sizes? */ 35168515Seric if (fseek(e->e_dfp, offset, SEEK_SET) < 0) 35268564Seric syserr("mime8to7: cannot fseek on df%s", e->e_id); 35368717Seric else 35468717Seric clearerr(e->e_dfp); 35567545Seric } 35667545Seric 35767547Seric /* 35867547Seric ** Heuristically determine encoding method. 35967547Seric ** If more than 1/8 of the total characters have the 36067547Seric ** eighth bit set, use base64; else use quoted-printable. 361*68860Seric ** However, only encode binary encoded data as base64, 362*68860Seric ** since otherwise the NL=>CRLF mapping will be a problem. 36367547Seric */ 36467547Seric 36567545Seric if (tTd(43, 8)) 36667545Seric { 367*68860Seric printf("mime8to7: %ld high bit(s) in %ld byte(s), cte=%s\n", 368*68860Seric sectionhighbits, sectionsize, 369*68860Seric cte == NULL ? "[none]" : cte); 37067545Seric } 371*68860Seric if (cte != NULL && strcasecmp(cte, "binary") == 0) 372*68860Seric sectionsize = sectionhighbits; 37368717Seric linelen = 0; 37467554Seric if (sectionhighbits == 0) 37567545Seric { 37667554Seric /* no encoding necessary */ 37768847Seric if (cte != NULL) 37867695Seric { 37968847Seric sprintf(buf, "Content-Transfer-Encoding: %s", cte); 38067695Seric putline(buf, mci); 38168717Seric if (tTd(43, 36)) 38268717Seric printf(" ...%s\n", buf); 38367695Seric } 38467554Seric putline("", mci); 38567554Seric mci->mci_flags &= ~MCIF_INHEADER; 38667554Seric while (fgets(buf, sizeof buf, e->e_dfp) != NULL) 38767554Seric { 38868517Seric bt = mimeboundary(buf, boundaries); 38967554Seric if (bt != MBT_NOTSEP) 39067554Seric break; 39167554Seric putline(buf, mci); 39267554Seric } 39368717Seric if (feof(e->e_dfp)) 39468717Seric bt = MBT_FINAL; 39567554Seric } 39667554Seric else if (sectionsize / 8 < sectionhighbits) 39767554Seric { 39867545Seric /* use base64 encoding */ 39967545Seric int c1, c2; 40068847Seric int (*getcharf) __P((FILE *, char **, int *)); 40168847Seric extern int mime_getchar __P((FILE *, char **, int *)); 40268847Seric extern int mime_getchar_crlf __P((FILE *, char **, int *)); 40367545Seric 404*68860Seric if (cte != NULL && strcasecmp(cte, "binary") == 0) 405*68860Seric getcharf = mime_getchar; 406*68860Seric else 40768847Seric getcharf = mime_getchar_crlf; 40867545Seric putline("Content-Transfer-Encoding: base64", mci); 40968717Seric if (tTd(43, 36)) 41068717Seric printf(" ...Content-Transfer-Encoding: base64\n"); 41167545Seric putline("", mci); 41267545Seric mci->mci_flags &= ~MCIF_INHEADER; 41368847Seric while ((c1 = (*getcharf)(e->e_dfp, boundaries, &bt)) != EOF) 41467545Seric { 41567545Seric if (linelen > 71) 41667545Seric { 41767545Seric fputs(mci->mci_mailer->m_eol, mci->mci_out); 41867545Seric linelen = 0; 41967545Seric } 42067545Seric linelen += 4; 42168847Seric fputc(Base64Code[(c1 >> 2) & 0x3f], mci->mci_out); 42267545Seric c1 = (c1 & 0x03) << 4; 42368847Seric c2 = (*getcharf)(e->e_dfp, boundaries, &bt); 42467545Seric if (c2 == EOF) 42567545Seric { 42668847Seric fputc(Base64Code[c1 & 0x3f], mci->mci_out); 42767545Seric fputc('=', mci->mci_out); 42867545Seric fputc('=', mci->mci_out); 42967545Seric break; 43067545Seric } 43167545Seric c1 |= (c2 >> 4) & 0x0f; 43268847Seric fputc(Base64Code[c1 & 0x3f], mci->mci_out); 43367545Seric c1 = (c2 & 0x0f) << 2; 43468847Seric c2 = (*getcharf)(e->e_dfp, boundaries, &bt); 43567545Seric if (c2 == EOF) 43667545Seric { 43768847Seric fputc(Base64Code[c1 & 0x3f], mci->mci_out); 43867545Seric fputc('=', mci->mci_out); 43967545Seric break; 44067545Seric } 44167545Seric c1 |= (c2 >> 6) & 0x03; 44268847Seric fputc(Base64Code[c1 & 0x3f], mci->mci_out); 44367545Seric fputc(Base64Code[c2 & 0x3f], mci->mci_out); 44467545Seric } 44567545Seric } 44667545Seric else 44767545Seric { 44867545Seric /* use quoted-printable encoding */ 44967545Seric int c1, c2; 45068515Seric int fromstate; 45167545Seric 45267545Seric putline("Content-Transfer-Encoding: quoted-printable", mci); 45368717Seric if (tTd(43, 36)) 45468717Seric printf(" ...Content-Transfer-Encoding: quoted-printable\n"); 45567545Seric putline("", mci); 45667545Seric mci->mci_flags &= ~MCIF_INHEADER; 45768717Seric fromstate = 0; 45867554Seric c2 = '\n'; 45968717Seric while ((c1 = mime_getchar(e->e_dfp, boundaries, &bt)) != EOF) 46067545Seric { 46167545Seric if (c1 == '\n') 46267545Seric { 46367545Seric if (c2 == ' ' || c2 == '\t') 46467545Seric { 46567545Seric fputc('=', mci->mci_out); 46667840Seric fputc(Base16Code[(c2 >> 4) & 0x0f], 46767840Seric mci->mci_out); 46867840Seric fputc(Base16Code[c2 & 0x0f], 46967840Seric mci->mci_out); 47067840Seric fputs(mci->mci_mailer->m_eol, 47167840Seric mci->mci_out); 47267545Seric } 47367545Seric fputs(mci->mci_mailer->m_eol, mci->mci_out); 47468515Seric linelen = fromstate = 0; 47567545Seric c2 = c1; 47667545Seric continue; 47767545Seric } 47868515Seric if (c2 == ' ' && linelen == 4 && fromstate == 4 && 47968515Seric bitnset(M_ESCFROM, mci->mci_mailer->m_flags)) 48067840Seric { 48168515Seric fputs("=20", mci->mci_out); 48268515Seric linelen += 3; 48368515Seric } 48468515Seric else if (c2 == ' ' || c2 == '\t') 48568515Seric { 48667840Seric fputc(c2, mci->mci_out); 48767840Seric linelen++; 48867840Seric } 48967545Seric if (linelen > 72) 49067545Seric { 49167545Seric fputc('=', mci->mci_out); 49267545Seric fputs(mci->mci_mailer->m_eol, mci->mci_out); 49368515Seric linelen = fromstate = 0; 49467554Seric c2 = '\n'; 49567545Seric } 49667761Seric if (c2 == '\n' && c1 == '.' && 49767761Seric bitnset(M_XDOT, mci->mci_mailer->m_flags)) 49867761Seric { 49967761Seric fputc('.', mci->mci_out); 50067761Seric linelen++; 50167761Seric } 50267547Seric if ((c1 < 0x20 && c1 != '\t') || c1 >= 0x7f || c1 == '=') 50367545Seric { 50467545Seric fputc('=', mci->mci_out); 50567545Seric fputc(Base16Code[(c1 >> 4) & 0x0f], mci->mci_out); 50667545Seric fputc(Base16Code[c1 & 0x0f], mci->mci_out); 50767545Seric linelen += 3; 50867545Seric } 50967840Seric else if (c1 != ' ' && c1 != '\t') 51067545Seric { 51168515Seric if (linelen < 4 && c1 == "From"[linelen]) 51268515Seric fromstate++; 51367545Seric fputc(c1, mci->mci_out); 51467545Seric linelen++; 51567545Seric } 51667545Seric c2 = c1; 51767545Seric } 51867840Seric 51967840Seric /* output any saved character */ 52067840Seric if (c2 == ' ' || c2 == '\t') 52167840Seric { 52268515Seric fputc('=', mci->mci_out); 52368515Seric fputc(Base16Code[(c2 >> 4) & 0x0f], mci->mci_out); 52468515Seric fputc(Base16Code[c2 & 0x0f], mci->mci_out); 52568515Seric linelen += 3; 52667840Seric } 52767545Seric } 52867545Seric if (linelen > 0) 52967545Seric fputs(mci->mci_mailer->m_eol, mci->mci_out); 53068717Seric if (tTd(43, 3)) 53168717Seric printf("\t\t\tmime8to7=>%s (basic)\n", MimeBoundaryNames[bt]); 53268717Seric return bt; 53367545Seric } 53468515Seric /* 53568515Seric ** MIME_GETCHAR -- get a character for MIME processing 53668515Seric ** 53768515Seric ** Treats boundaries as EOF. 53868515Seric ** 53968515Seric ** Parameters: 54068515Seric ** fp -- the input file. 54168517Seric ** boundaries -- the current MIME boundaries. 54268717Seric ** btp -- if the return value is EOF, *btp is set to 54368717Seric ** the type of the boundary. 54468515Seric ** 54568515Seric ** Returns: 54668515Seric ** The next character in the input stream. 54768515Seric */ 54867545Seric 54967545Seric int 55068717Seric mime_getchar(fp, boundaries, btp) 55167545Seric register FILE *fp; 55268517Seric char **boundaries; 55368717Seric int *btp; 55467545Seric { 55567545Seric int c; 55667545Seric static char *bp = NULL; 55767545Seric static int buflen = 0; 55867545Seric static bool atbol = TRUE; /* at beginning of line */ 55968717Seric static int bt = MBT_SYNTAX; /* boundary type of next EOF */ 56067545Seric static char buf[128]; /* need not be a full line */ 56167545Seric 56267545Seric if (buflen > 0) 56367545Seric { 56467545Seric buflen--; 56567545Seric return *bp++; 56667545Seric } 56768515Seric bp = buf; 56868515Seric buflen = 0; 56967545Seric c = fgetc(fp); 57068515Seric if (c == '\n') 57168515Seric { 57268515Seric /* might be part of a MIME boundary */ 57368515Seric *bp++ = c; 57468515Seric atbol = TRUE; 57568515Seric c = fgetc(fp); 57668515Seric } 57768515Seric if (c != EOF) 57868515Seric *bp++ = c; 57968717Seric else 58068717Seric bt = MBT_FINAL; 58168517Seric if (atbol && c == '-') 58267545Seric { 58367545Seric /* check for a message boundary */ 58467545Seric c = fgetc(fp); 58567545Seric if (c != '-') 58667545Seric { 58767545Seric if (c != EOF) 58868515Seric *bp++ = c; 58968717Seric else 59068717Seric bt = MBT_FINAL; 59168515Seric buflen = bp - buf - 1; 59268515Seric bp = buf; 59368515Seric return *bp++; 59467545Seric } 59567545Seric 59667545Seric /* got "--", now check for rest of separator */ 59767545Seric *bp++ = '-'; 59868847Seric while (bp < &buf[sizeof buf - 2] && 59967545Seric (c = fgetc(fp)) != EOF && c != '\n') 60067545Seric { 60167545Seric *bp++ = c; 60267545Seric } 60367545Seric *bp = '\0'; 60468717Seric bt = mimeboundary(&buf[1], boundaries); 60568717Seric switch (bt) 60667545Seric { 60767545Seric case MBT_FINAL: 60867545Seric case MBT_INTERMED: 60967545Seric /* we have a message boundary */ 61067545Seric buflen = 0; 61168717Seric *btp = bt; 61267545Seric return EOF; 61367545Seric } 61467545Seric 61567545Seric atbol = c == '\n'; 61667545Seric if (c != EOF) 61767545Seric *bp++ = c; 61867545Seric } 61967545Seric 62068515Seric buflen = bp - buf - 1; 62168515Seric if (buflen < 0) 62268717Seric { 62368717Seric *btp = bt; 62468515Seric return EOF; 62568717Seric } 62668515Seric bp = buf; 62768515Seric return *bp++; 62867545Seric } 62967545Seric /* 63068847Seric ** MIME_GETCHAR_CRLF -- do mime_getchar, but translate NL => CRLF 63168847Seric ** 63268847Seric ** Parameters: 63368847Seric ** fp -- the input file. 63468847Seric ** boundaries -- the current MIME boundaries. 63568847Seric ** btp -- if the return value is EOF, *btp is set to 63668847Seric ** the type of the boundary. 63768847Seric ** 63868847Seric ** Returns: 63968847Seric ** The next character in the input stream. 64068847Seric */ 64168847Seric 64268847Seric int 64368847Seric mime_getchar_crlf(fp, boundaries, btp) 64468847Seric register FILE *fp; 64568847Seric char **boundaries; 64668847Seric int *btp; 64768847Seric { 64868847Seric static bool sendlf = FALSE; 64968847Seric int c; 65068847Seric 65168847Seric if (sendlf) 65268847Seric { 65368847Seric sendlf = FALSE; 65468847Seric return '\n'; 65568847Seric } 65668847Seric c = mime_getchar(fp, boundaries, btp); 65768847Seric if (c == '\n') 65868847Seric { 65968847Seric sendlf = TRUE; 66068847Seric return '\r'; 66168847Seric } 66268847Seric return c; 66368847Seric } 66468847Seric /* 66567545Seric ** MIMEBOUNDARY -- determine if this line is a MIME boundary & its type 66667545Seric ** 66767545Seric ** Parameters: 66867545Seric ** line -- the input line. 66968517Seric ** boundaries -- the set of currently pending boundaries. 67067545Seric ** 67167545Seric ** Returns: 67267545Seric ** MBT_NOTSEP -- if this is not a separator line 67367545Seric ** MBT_INTERMED -- if this is an intermediate separator 67467545Seric ** MBT_FINAL -- if this is a final boundary 67567545Seric ** MBT_SYNTAX -- if this is a boundary for the wrong 67667545Seric ** enclosure -- i.e., a syntax error. 67767545Seric */ 67867545Seric 67967545Seric int 68068517Seric mimeboundary(line, boundaries) 68167545Seric register char *line; 68268517Seric char **boundaries; 68367545Seric { 68467545Seric int type; 68567545Seric int i; 68668517Seric int savec; 68767545Seric 68868517Seric if (line[0] != '-' || line[1] != '-' || boundaries == NULL) 68967545Seric return MBT_NOTSEP; 69067545Seric i = strlen(line); 69167545Seric if (line[i - 1] == '\n') 69267545Seric i--; 69368717Seric if (tTd(43, 5)) 69468717Seric printf("mimeboundary: line=\"%.*s\"... ", i, line); 69568515Seric while (line[i - 1] == ' ' || line[i - 1] == '\t') 69668515Seric i--; 69767545Seric if (i > 2 && strncmp(&line[i - 2], "--", 2) == 0) 69867545Seric { 69967545Seric type = MBT_FINAL; 70067545Seric i -= 2; 70167545Seric } 70267545Seric else 70367545Seric type = MBT_INTERMED; 70467545Seric 70568517Seric savec = line[i]; 70668517Seric line[i] = '\0'; 70767545Seric /* XXX should check for improper nesting here */ 70868517Seric if (isboundary(&line[2], boundaries) < 0) 70967545Seric type = MBT_NOTSEP; 71068517Seric line[i] = savec; 71167545Seric if (tTd(43, 5)) 71268717Seric printf("%s\n", MimeBoundaryNames[type]); 71367545Seric return type; 71467545Seric } 71567896Seric /* 71667896Seric ** DEFCHARSET -- return default character set for message 71767896Seric ** 71867896Seric ** The first choice for character set is for the mailer 71967896Seric ** corresponding to the envelope sender. If neither that 72067896Seric ** nor the global configuration file has a default character 72167896Seric ** set defined, return "unknown-8bit" as recommended by 72267896Seric ** RFC 1428 section 3. 72367896Seric ** 72467896Seric ** Parameters: 72567896Seric ** e -- the envelope for this message. 72667896Seric ** 72767896Seric ** Returns: 72867896Seric ** The default character set for that mailer. 72967896Seric */ 73067896Seric 73167896Seric char * 73267896Seric defcharset(e) 73367896Seric register ENVELOPE *e; 73467896Seric { 73567896Seric if (e != NULL && e->e_from.q_mailer != NULL && 73667896Seric e->e_from.q_mailer->m_defcharset != NULL) 73767896Seric return e->e_from.q_mailer->m_defcharset; 73867896Seric if (DefaultCharSet != NULL) 73967896Seric return DefaultCharSet; 74067896Seric return "unknown-8bit"; 74167896Seric } 74268517Seric /* 74368517Seric ** ISBOUNDARY -- is a given string a currently valid boundary? 74468517Seric ** 74568517Seric ** Parameters: 74668517Seric ** line -- the current input line. 74768517Seric ** boundaries -- the list of valid boundaries. 74868517Seric ** 74968517Seric ** Returns: 75068517Seric ** The index number in boundaries if the line is found. 75168517Seric ** -1 -- otherwise. 75268517Seric ** 75368517Seric */ 75468517Seric 75568517Seric int 75668517Seric isboundary(line, boundaries) 75768517Seric char *line; 75868517Seric char **boundaries; 75968517Seric { 76068517Seric register int i; 76168517Seric 76268711Seric for (i = 0; boundaries[i] != NULL; i++) 76368517Seric { 76468517Seric if (strcmp(line, boundaries[i]) == 0) 76568517Seric return i; 76668517Seric } 76768517Seric return -1; 76868517Seric } 769