d5844a1
diff -up cups-1.4.3/cups/tempfile.c.str3382 cups-1.4.3/cups/tempfile.c
d5844a1
--- cups-1.4.3/cups/tempfile.c.str3382	2010-01-18 19:47:12.000000000 +0100
d5844a1
+++ cups-1.4.3/cups/tempfile.c	2010-03-31 13:26:52.000000000 +0200
37e0a9a
@@ -35,6 +35,7 @@
37e0a9a
 #  include <io.h>
37e0a9a
 #else
37e0a9a
 #  include <unistd.h>
37e0a9a
+#  include <sys/types.h>
37e0a9a
 #endif /* WIN32 || __EMX__ */
37e0a9a
 
37e0a9a
 
37e0a9a
@@ -56,7 +57,7 @@ cupsTempFd(char *filename,		/* I - Point
37e0a9a
   char		tmppath[1024];		/* Windows temporary directory */
37e0a9a
   DWORD		curtime;		/* Current time */
37e0a9a
 #else
37e0a9a
-  struct timeval curtime;		/* Current time */
37e0a9a
+  mode_t	old_umask;		/* Old umask before using mkstemp() */
37e0a9a
 #endif /* WIN32 */
37e0a9a
 
37e0a9a
 
37e0a9a
@@ -107,33 +108,25 @@ cupsTempFd(char *filename,		/* I - Point
37e0a9a
 
37e0a9a
     snprintf(filename, len - 1, "%s/%05lx%08lx", tmpdir,
37e0a9a
              GetCurrentProcessId(), curtime);
37e0a9a
-#else
37e0a9a
-   /*
37e0a9a
-    * Get the current time of day...
37e0a9a
-    */
37e0a9a
-
37e0a9a
-    gettimeofday(&curtime, NULL);
37e0a9a
-
37e0a9a
-   /*
37e0a9a
-    * Format a string using the hex time values...
37e0a9a
-    */
37e0a9a
-
d5844a1
-    snprintf(filename, len - 1, "%s/%05x%08x", tmpdir, (unsigned)getpid(),
d5844a1
-             (unsigned)(curtime.tv_sec + curtime.tv_usec + tries));
37e0a9a
-#endif /* WIN32 */
37e0a9a
 
37e0a9a
    /*
37e0a9a
     * Open the file in "exclusive" mode, making sure that we don't
37e0a9a
     * stomp on an existing file or someone's symlink crack...
37e0a9a
     */
37e0a9a
 
37e0a9a
-#ifdef WIN32
37e0a9a
     fd = open(filename, _O_CREAT | _O_RDWR | _O_TRUNC | _O_BINARY,
37e0a9a
               _S_IREAD | _S_IWRITE);
37e0a9a
-#elif defined(O_NOFOLLOW)
37e0a9a
-    fd = open(filename, O_RDWR | O_CREAT | O_EXCL | O_NOFOLLOW, 0600);
37e0a9a
 #else
37e0a9a
-    fd = open(filename, O_RDWR | O_CREAT | O_EXCL, 0600);
37e0a9a
+
37e0a9a
+   /*
37e0a9a
+    * Use the standard mkstemp() call to make a temporary filename
37e0a9a
+    * securely.  -- andrew.wood@jdplc.com
37e0a9a
+    */
37e0a9a
+    snprintf(filename, len - 1, "%s/cupsXXXXXX", tmpdir);
37e0a9a
+
37e0a9a
+    old_umask = umask(0077);
37e0a9a
+    fd = mkstemp(filename);
37e0a9a
+    umask(old_umask);
37e0a9a
 #endif /* WIN32 */
37e0a9a
 
37e0a9a
     if (fd < 0 && errno != EEXIST)