* Bug #15213, #18635: The use of the insecure m_scratch() and
[mmh] / uip / mhbuild.c
index bacec10..fbe2b2c 100644 (file)
@@ -3,6 +3,10 @@
  * mhbuild.c -- expand/translate MIME composition files
  *
  * $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.
  */
 
 #include <h/mh.h>
 #include <h/md5.h>
 #include <errno.h>
 #include <signal.h>
-#include <zotnet/mts/mts.h>
-#include <zotnet/tws/tws.h>
+#include <h/mts.h>
+#include <h/tws.h>
 #include <h/mime.h>
 #include <h/mhparse.h>
 #include <h/mhcachesbr.h>
+#include <h/utils.h>
 
 #ifdef HAVE_SYS_WAIT_H
 # include <sys/wait.h>
@@ -54,20 +59,21 @@ static struct swit switches[] = {
     { "rcache policy", 0 },
 #define        WCACHESW               15
     { "wcache policy", 0 },
-#define VERSIONSW              16
+#define        CONTENTIDSW            16
+    { "contentid", 0 },
+#define        NCONTENTIDSW           17
+    { "nocontentid", 0 },
+#define VERSIONSW              18
     { "version", 0 },
-#define        HELPSW                 17
-    { "help", 4 },
-#define        DEBUGSW                18
+#define        HELPSW                 19
+    { "help", 0 },
+#define        DEBUGSW                20
     { "debug", -5 },
     { NULL, 0 }
 };
 
 
-extern int errno;
-
 /* mhbuildsbr.c */
-extern int checksw;
 extern char *tmp;      /* directory to place temp files */
 
 /* mhcachesbr.c */
@@ -82,6 +88,7 @@ int verbosw = 0;
 int ebcdicsw = 0;
 int listsw   = 0;
 int rfc934sw = 0;
+int contentidsw = 1;
 
 /*
  * Temporary files
@@ -92,10 +99,12 @@ static int unlink_infile  = 0;
 static char outfile[BUFSIZ];
 static int unlink_outfile = 0;
 
+static void unlink_done (int) NORETURN;
 
 /* mhbuildsbr.c */
 CT build_mime (char *);
 int output_message (CT, char *);
+int output_message_fp (CT, FILE *, char*);
 
 /* mhlistsbr.c */
 int list_all_messages (CT *, int, int, int, int);
@@ -116,7 +125,10 @@ main (int argc, char **argv)
     char buffer[BUFSIZ], *compfile = NULL;
     char **argp, **arguments;
     CT ct, cts[2];
-    FILE *fp;
+    FILE *fp = NULL;
+    FILE *fp_out = NULL;
+
+    done=unlink_done;
 
 #ifdef LOCALE
     setlocale(LC_ALL, "");
@@ -216,6 +228,13 @@ main (int argc, char **argv)
                sizesw = 0;
                continue;
 
+           case CONTENTIDSW:
+               contentidsw = 1;
+               continue;
+           case NCONTENTIDSW:
+               contentidsw = 0;
+               continue;
+
            case VERBSW: 
                verbosw++;
                continue;
@@ -288,11 +307,8 @@ main (int argc, char **argv)
      * Process the composition file from standard input.
      */
     if (compfile[0] == '-' && compfile[1] == '\0') {
-
        /* copy standard input to temporary file */
-       strncpy (infile, m_scratch ("", invo_name), sizeof(infile));
-       if ((fp = fopen (infile, "w")) == NULL)
-           adios (infile, "unable to open");
+       strncpy (infile, m_mktemp(invo_name, NULL, &fp), sizeof(infile));
        while (fgets (buffer, BUFSIZ, stdin))
            fputs (buffer, fp);
        fclose (fp);
@@ -304,11 +320,12 @@ main (int argc, char **argv)
        cts[1] = NULL;
 
        /* output MIME message to this temporary file */
-       strncpy (outfile, m_scratch ("", invo_name), sizeof(outfile));
+       strncpy (outfile, m_mktemp(invo_name, NULL, &fp_out), sizeof(outfile));
        unlink_outfile = 1;
 
        /* output the message */
-       output_message (ct, outfile);
+       output_message_fp (ct, fp_out, outfile);
+        fclose(fp_out);
 
        /* output the temp file to standard output */
        if ((fp = fopen (outfile, "r")) == NULL)
@@ -337,11 +354,13 @@ main (int argc, char **argv)
     cts[1] = NULL;
 
     /* output MIME message to this temporary file */
-    strncpy (outfile, m_scratch (compfile, invo_name), sizeof(outfile));
+    strncpy(outfile, m_mktemp2(compfile, invo_name, NULL, &fp_out),
+            sizeof(outfile));
     unlink_outfile = 1;
 
     /* output the message */
-    output_message (ct, outfile);
+    output_message_fp (ct, fp_out, outfile);
+    fclose(fp_out);
 
     /*
      * List the message info
@@ -351,12 +370,13 @@ main (int argc, char **argv)
 
     /* Rename composition draft */
     snprintf (buffer, sizeof(buffer), "%s.orig", m_backup (compfile));
-    if (rename (compfile, buffer) == NOTOK)
-       adios (compfile, "unable to rename %s to", buffer);
+    if (rename (compfile, buffer) == NOTOK) {
+       adios (compfile, "unable to rename comp draft %s to", buffer);
+    }
 
     /* Rename output file to take its place */
     if (rename (outfile, compfile) == NOTOK) {
-       advise (outfile, "unable to rename %s to", compfile);
+       advise (outfile, "unable to rename output %s to", compfile);
        rename (buffer, compfile);
        done (1);
     }
@@ -364,12 +384,12 @@ main (int argc, char **argv)
 
     free_content (ct);
     done (0);
-    /* NOT REACHED */
+    return 1;
 }
 
 
-void
-done (int status)
+static void
+unlink_done (int status)
 {
     /*
      * Check if we need to remove stray