X-Git-Url: http://git.marmaro.de/?a=blobdiff_plain;f=uip%2Fmhparse.c;h=91cc4116d27a2d1654606b995c71bf4ef8e89e96;hb=436e6b477fa6dabe40404c84334e148b49682708;hp=f5ff85c24437cf1a4b86240cc343eab37866f82c;hpb=636b3bab53e3fc2154bb86fb02aeecf433cc977a;p=mmh diff --git a/uip/mhparse.c b/uip/mhparse.c index f5ff85c..91cc411 100644 --- a/uip/mhparse.c +++ b/uip/mhparse.c @@ -2,8 +2,6 @@ /* * mhparse.c -- routines to parse the contents of MIME messages * - * $Id$ - * * This code is Copyright (c) 2002, by the authors of nmh. See the * COPYRIGHT file in the root directory of the nmh distribution for * complete copyright information. @@ -14,7 +12,6 @@ #include #include #include -#include #include #include #include @@ -22,10 +19,6 @@ #include #include -#ifdef HAVE_SYS_WAIT_H -# include -#endif - extern int debugsw; @@ -92,16 +85,12 @@ struct k2v SubApplication[] = { }; -/* ftpsbr.c */ -int ftp_get (char *, char *, char *, char *, char *, char *, int, int); - /* mhcachesbr.c */ int find_cache (CT, int, int *, char *, char *, int); /* mhmisc.c */ int part_ok (CT, int); int type_ok (CT, int); -int make_intermediates (char *); void content_error (char *, CT, char *, ...); /* mhfree.c */ @@ -204,12 +193,14 @@ parse_mime (char *file) * Check if file is actually standard input */ if ((is_stdin = !(strcmp (file, "-")))) { - file = add (m_tmpfil (invo_name), NULL); - if ((fp = fopen (file, "w+")) == NULL) { - advise (file, "unable to fopen for writing and reading"); - return NULL; - } + char *tfile = m_mktemp2(NULL, invo_name, NULL, &fp); + if (tfile == NULL) { + advise("mhparse", "unable to create temporary file"); + return NULL; + } + file = add (tfile, NULL); chmod (file, 0600); + while (fgets (buffer, sizeof(buffer), stdin)) fputs (buffer, fp); fflush (fp); @@ -1046,6 +1037,8 @@ invalid: static int InitGeneric (CT ct) { + NMH_UNUSED (ct); + return OK; /* not much to do here */ } @@ -1058,7 +1051,7 @@ static int InitText (CT ct) { char buffer[BUFSIZ]; - char *chset; + char *chset = NULL; char **ap, **ep, *cp; struct k2v *kv; struct text *t; @@ -1087,9 +1080,12 @@ InitText (CT ct) /* check if content specified a character set */ if (*ap) { /* match character set or set to CHARSET_UNKNOWN */ - for (kv = Charset; kv->kv_key; kv++) - if (!mh_strcasecmp (*ep, kv->kv_key)) + for (kv = Charset; kv->kv_key; kv++) { + if (!mh_strcasecmp (*ep, kv->kv_key)) { + chset = *ep; break; + } + } t->tx_charset = kv->kv_value; } else { t->tx_charset = CHARSET_UNSPECIFIED; @@ -1731,7 +1727,7 @@ static int openBase64 (CT ct, char **file) { int bitno, cc, digested; - int fd, len, skip; + int fd, len, skip, own_ct_fp = 0; unsigned long bits; unsigned char value, *b, *b1, *b2, *b3; unsigned char *cp, *ep; @@ -1741,10 +1737,15 @@ openBase64 (CT ct, char **file) CE ce; MD5_CTX mdContext; + /* the decoder works on the least-significant three bytes of the bits integer, + but their position in memory depend on both endian-ness and size of + long int... for little-endian architectures the size is irrelevant, for + big-endian archs it's crucial... ideally we'd adopt posix and use a64l instead + of this mess. */ b = (unsigned char *) &bits; - b1 = &b[endian > 0 ? 1 : 2]; - b2 = &b[endian > 0 ? 2 : 1]; - b3 = &b[endian > 0 ? 3 : 0]; + b1 = &b[endian > 0 ? sizeof(bits)==8?5:1 : 2]; + b2 = &b[endian > 0 ? sizeof(bits)==8?6:2 : 1]; + b3 = &b[endian > 0 ? sizeof(bits)==8?7:3 : 0]; ce = ct->c_cefile; if (ce->ce_fp) { @@ -1761,7 +1762,7 @@ openBase64 (CT ct, char **file) } if (*file == NULL) { - ce->ce_file = add (m_scratch ("", tmp), NULL); + ce->ce_file = add (m_mktemp(tmp, NULL, NULL), NULL); ce->ce_unlink = 1; } else { ce->ce_file = add (*file, NULL); @@ -1778,8 +1779,21 @@ openBase64 (CT ct, char **file) ci->ci_type); cp = context_find (buffer); } - if (cp != NULL && *cp != '\0') - ce->ce_file = add (cp, ce->ce_file); + if (cp != NULL && *cp != '\0') { + if (ce->ce_unlink) { + /* Temporary file already exists, so we rename to + version with extension. */ + char *file_org = strdup(ce->ce_file); + ce->ce_file = add (cp, ce->ce_file); + if (rename(file_org, ce->ce_file)) { + adios (ce->ce_file, "unable to rename %s to ", file_org); + } + free(file_org); + + } else { + ce->ce_file = add (cp, ce->ce_file); + } + } if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) { content_error (ce->ce_file, ct, "unable to fopen for reading/writing"); @@ -1789,9 +1803,12 @@ openBase64 (CT ct, char **file) if ((len = ct->c_end - ct->c_begin) < 0) adios (NULL, "internal error(1)"); - if (!ct->c_fp && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) { - content_error (ct->c_file, ct, "unable to open for reading"); - return NOTOK; + if (! ct->c_fp) { + if ((ct->c_fp = fopen (ct->c_file, "r")) == NULL) { + content_error (ct->c_file, ct, "unable to open for reading"); + return NOTOK; + } + own_ct_fp = 1; } if ((digested = ct->c_digested)) @@ -1903,9 +1920,17 @@ self_delimiting: ready_to_go: *file = ce->ce_file; + if (own_ct_fp) { + fclose (ct->c_fp); + ct->c_fp = NULL; + } return fileno (ce->ce_fp); clean_up: + if (own_ct_fp) { + fclose (ct->c_fp); + ct->c_fp = NULL; + } free_encoding (ct, 0); return NOTOK; } @@ -1945,7 +1970,7 @@ InitQuoted (CT ct) static int openQuoted (CT ct, char **file) { - int cc, digested, len, quoted; + int cc, digested, len, quoted, own_ct_fp = 0; unsigned char *cp, *ep; char buffer[BUFSIZ]; unsigned char mask; @@ -1969,7 +1994,7 @@ openQuoted (CT ct, char **file) } if (*file == NULL) { - ce->ce_file = add (m_scratch ("", tmp), NULL); + ce->ce_file = add (m_mktemp(tmp, NULL, NULL), NULL); ce->ce_unlink = 1; } else { ce->ce_file = add (*file, NULL); @@ -1986,12 +2011,20 @@ openQuoted (CT ct, char **file) ci->ci_type); cp = context_find (buffer); } - if (cp != NULL && *cp != '\0') - ce->ce_file = add (cp, ce->ce_file); - - if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) { - content_error (ce->ce_file, ct, "unable to fopen for reading/writing"); - return NOTOK; + if (cp != NULL && *cp != '\0') { + if (ce->ce_unlink) { + /* Temporary file already exists, so we rename to + version with extension. */ + char *file_org = strdup(ce->ce_file); + ce->ce_file = add (cp, ce->ce_file); + if (rename(file_org, ce->ce_file)) { + adios (ce->ce_file, "unable to rename %s to ", file_org); + } + free(file_org); + + } else { + ce->ce_file = add (cp, ce->ce_file); + } } if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) { @@ -2002,9 +2035,12 @@ openQuoted (CT ct, char **file) if ((len = ct->c_end - ct->c_begin) < 0) adios (NULL, "internal error(2)"); - if (!ct->c_fp && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) { - content_error (ct->c_file, ct, "unable to open for reading"); - return NOTOK; + if (! ct->c_fp) { + if ((ct->c_fp = fopen (ct->c_file, "r")) == NULL) { + content_error (ct->c_file, ct, "unable to open for reading"); + return NOTOK; + } + own_ct_fp = 1; } if ((digested = ct->c_digested)) @@ -2126,10 +2162,18 @@ openQuoted (CT ct, char **file) ready_to_go: *file = ce->ce_file; + if (own_ct_fp) { + fclose (ct->c_fp); + ct->c_fp = NULL; + } return fileno (ce->ce_fp); clean_up: free_encoding (ct, 0); + if (own_ct_fp) { + fclose (ct->c_fp); + ct->c_fp = NULL; + } return NOTOK; } @@ -2152,7 +2196,7 @@ Init7Bit (CT ct) int open7Bit (CT ct, char **file) { - int cc, fd, len; + int cc, fd, len, own_ct_fp = 0; char buffer[BUFSIZ]; /* sbeck -- handle suffixes */ char *cp; @@ -2174,7 +2218,7 @@ open7Bit (CT ct, char **file) } if (*file == NULL) { - ce->ce_file = add (m_scratch ("", tmp), NULL); + ce->ce_file = add (m_mktemp(tmp, NULL, NULL), NULL); ce->ce_unlink = 1; } else { ce->ce_file = add (*file, NULL); @@ -2191,8 +2235,21 @@ open7Bit (CT ct, char **file) ci->ci_type); cp = context_find (buffer); } - if (cp != NULL && *cp != '\0') - ce->ce_file = add (cp, ce->ce_file); + if (cp != NULL && *cp != '\0') { + if (ce->ce_unlink) { + /* Temporary file already exists, so we rename to + version with extension. */ + char *file_org = strdup(ce->ce_file); + ce->ce_file = add (cp, ce->ce_file); + if (rename(file_org, ce->ce_file)) { + adios (ce->ce_file, "unable to rename %s to ", file_org); + } + free(file_org); + + } else { + ce->ce_file = add (cp, ce->ce_file); + } + } if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) { content_error (ce->ce_file, ct, "unable to fopen for reading/writing"); @@ -2249,9 +2306,12 @@ open7Bit (CT ct, char **file) if ((len = ct->c_end - ct->c_begin) < 0) adios (NULL, "internal error(3)"); - if (!ct->c_fp && (ct->c_fp = fopen (ct->c_file, "r")) == NULL) { - content_error (ct->c_file, ct, "unable to open for reading"); - return NOTOK; + if (! ct->c_fp) { + if ((ct->c_fp = fopen (ct->c_file, "r")) == NULL) { + content_error (ct->c_file, ct, "unable to open for reading"); + return NOTOK; + } + own_ct_fp = 1; } lseek (fd = fileno (ct->c_fp), (off_t) ct->c_begin, SEEK_SET); @@ -2288,10 +2348,18 @@ open7Bit (CT ct, char **file) ready_to_go: *file = ce->ce_file; + if (own_ct_fp) { + fclose (ct->c_fp); + ct->c_fp = NULL; + } return fileno (ce->ce_fp); clean_up: free_encoding (ct, 0); + if (own_ct_fp) { + fclose (ct->c_fp); + ct->c_fp = NULL; + } return NOTOK; } @@ -2447,10 +2515,8 @@ openFTP (CT ct, char **file) if ((ftp = context_find (nmhaccessftp)) && !*ftp) ftp = NULL; -#ifndef BUILTIN_FTP if (!ftp) return NOTOK; -#endif switch (openExternal (e->eb_parent, e->eb_content, ce, file, &fd)) { case NOTOK: @@ -2517,7 +2583,8 @@ openFTP (CT ct, char **file) if (e->eb_flags) { user = "anonymous"; - snprintf (buffer, sizeof(buffer), "%s@%s", getusername (), LocalName ()); + snprintf (buffer, sizeof(buffer), "%s@%s", getusername (), + LocalName (1)); pass = buffer; } else { ruserpass (e->eb_site, &username, &password); @@ -2542,16 +2609,13 @@ openFTP (CT ct, char **file) else if (caching) ce->ce_file = add (cachefile, NULL); else - ce->ce_file = add (m_scratch ("", tmp), NULL); + ce->ce_file = add (m_mktemp(tmp, NULL, NULL), NULL); if ((ce->ce_fp = fopen (ce->ce_file, "w+")) == NULL) { content_error (ce->ce_file, ct, "unable to fopen for reading/writing"); return NOTOK; } -#ifdef BUILTIN_FTP - if (ftp) -#endif { int child_id, i, vecp; char *vec[9]; @@ -2570,7 +2634,7 @@ openFTP (CT ct, char **file) fflush (stdout); - for (i = 0; (child_id = vfork ()) == NOTOK && i < 5; i++) + for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++) sleep (5); switch (child_id) { case NOTOK: @@ -2587,9 +2651,6 @@ openFTP (CT ct, char **file) default: if (pidXwait (child_id, NULL)) { -#ifdef BUILTIN_FTP -losing_ftp: -#endif username = password = NULL; ce->ce_unlink = 1; return NOTOK; @@ -2597,14 +2658,6 @@ losing_ftp: break; } } -#ifdef BUILTIN_FTP - else - if (ftp_get (e->eb_site, user, pass, e->eb_dir, e->eb_name, - ce->ce_file, - e->eb_mode && !mh_strcasecmp (e->eb_mode, "ascii"), 0) - == NOTOK) - goto losing_ftp; -#endif if (cachefile[0]) { if (caching) @@ -2723,7 +2776,7 @@ openMail (CT ct, char **file) vec[vecp++] = e->eb_body; vec[vecp] = NULL; - for (i = 0; (child_id = vfork ()) == NOTOK && i < 5; i++) + for (i = 0; (child_id = vfork()) == NOTOK && i < 5; i++) sleep (5); switch (child_id) { case NOTOK: @@ -2744,7 +2797,7 @@ openMail (CT ct, char **file) } if (*file == NULL) { - ce->ce_file = add (m_scratch ("", tmp), NULL); + ce->ce_file = add (m_mktemp(tmp, NULL, NULL), NULL); ce->ce_unlink = 1; } else { ce->ce_file = add (*file, NULL); @@ -2777,10 +2830,16 @@ readDigest (CT ct, char *cp) unsigned char *dp, value, *ep; unsigned char *b, *b1, *b2, *b3; - b = (unsigned char *) &bits, - b1 = &b[endian > 0 ? 1 : 2], - b2 = &b[endian > 0 ? 2 : 1], - b3 = &b[endian > 0 ? 3 : 0]; + /* the decoder works on the least-significant three bytes of the bits integer, + but their position in memory depend on both endian-ness and size of + long int... for little-endian architectures the size is irrelevant, for + big-endian archs it's crucial... ideally we'd adopt posix and use a64l instead + of this mess. */ + b = (unsigned char *) &bits; + b1 = &b[endian > 0 ? sizeof(bits)==8?5:1 : 2]; + b2 = &b[endian > 0 ? sizeof(bits)==8?6:2 : 1]; + b3 = &b[endian > 0 ? sizeof(bits)==8?7:3 : 0]; + bitno = 18; bits = 0L; skip = 0;