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