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