From accd26d2223f2ea24f957a8d4aa36a5556a38b09 Mon Sep 17 00:00:00 2001 From: Michael R Sweet Date: Wed, 7 Mar 2018 09:03:24 -0500 Subject: [PATCH] Temporary files are now placed in the correct directory for sandboxed applications on macOS (rdar://problem/37789645) --- CHANGES.md | 2 ++ cups/ppd-util.c | 32 +++++++++++++++++++++++++------- cups/tempfile.c | 32 +++++++++++++++++++++++++------- 3 files changed, 52 insertions(+), 14 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 7103a2363e..cb79ca7587 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -49,6 +49,8 @@ Changes in CUPS v2.2.7 of "none" (rdar://36566269) - TLS connections now properly timeout (rdar://34938533) - The IPP backend did not properly detect failed PDF prints (rdar://34055474) +- Temporary files are now placed in the correct directory for sandboxed + applications on macOS (rdar://problem/37789645) Changes in CUPS v2.2.6 diff --git a/cups/ppd-util.c b/cups/ppd-util.c index d0194c8034..5f81972af5 100644 --- a/cups/ppd-util.c +++ b/cups/ppd-util.c @@ -1,8 +1,8 @@ /* * PPD utilities for CUPS. * - * Copyright 2007-2015 by Apple Inc. - * Copyright 1997-2006 by Easy Software Products. + * Copyright © 2007-2018 by Apple Inc. + * Copyright © 1997-2006 by Easy Software Products. * * These coded instructions, statements, and computer programs are the * property of Apple Inc. and are protected by Federal copyright @@ -216,6 +216,27 @@ cupsGetPPD3(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAUL const char *tmpdir; /* TMPDIR environment variable */ struct timeval curtime; /* Current time */ +#ifdef __APPLE__ + /* + * On macOS and iOS, the TMPDIR environment variable is not always the + * best location to place temporary files due to sandboxing. Instead, + * the confstr function should be called to get the proper per-user, + * per-process TMPDIR value. + */ + + char tmppath[1024]; /* Temporary directory */ + + if ((tmpdir = getenv("TMPDIR")) != NULL && access(tmpdir, W_OK)) + tmpdir = NULL; + + if (!tmpdir) + { + if (confstr(_CS_DARWIN_USER_TEMP_DIR, tmppath, sizeof(tmppath))) + tmpdir = tmppath; + else + tmpdir = "/private/tmp"; /* This should never happen */ + } +#else /* * Previously we put root temporary files in the default CUPS temporary * directory under /var/spool/cups. However, since the scheduler cleans @@ -224,11 +245,8 @@ cupsGetPPD3(http_t *http, /* I - HTTP connection or @code CUPS_HTTP_DEFAUL */ if ((tmpdir = getenv("TMPDIR")) == NULL) -# ifdef __APPLE__ - tmpdir = "/private/tmp"; /* /tmp is a symlink to /private/tmp */ -# else - tmpdir = "/tmp"; -# endif /* __APPLE__ */ + tmpdir = "/tmp"; +#endif /* __APPLE__ */ /* * Make the temporary name using the specified directory... diff --git a/cups/tempfile.c b/cups/tempfile.c index da705a94ea..1e248694c8 100644 --- a/cups/tempfile.c +++ b/cups/tempfile.c @@ -1,8 +1,8 @@ /* * Temp file utilities for CUPS. * - * Copyright 2007-2014 by Apple Inc. - * Copyright 1997-2006 by Easy Software Products. + * Copyright © 2007-2018 by Apple Inc. + * Copyright © 1997-2006 by Easy Software Products. * * These coded instructions, statements, and computer programs are the * property of Apple Inc. and are protected by Federal copyright @@ -42,8 +42,10 @@ cupsTempFd(char *filename, /* I - Pointer to buffer */ int fd; /* File descriptor for temp file */ int tries; /* Number of tries */ const char *tmpdir; /* TMPDIR environment var */ +#if defined(__APPLE__) || defined(WIN32) + char tmppath[1024]; /* Temporary directory */ +#endif /* __APPLE__ || WIN32 */ #ifdef WIN32 - char tmppath[1024]; /* Windows temporary directory */ DWORD curtime; /* Current time */ #else struct timeval curtime; /* Current time */ @@ -60,6 +62,26 @@ cupsTempFd(char *filename, /* I - Pointer to buffer */ GetTempPath(sizeof(tmppath), tmppath); tmpdir = tmppath; } + +#elif defined(__APPLE__) + /* + * On macOS and iOS, the TMPDIR environment variable is not always the best + * location to place temporary files due to sandboxing. Instead, the confstr + * function should be called to get the proper per-user, per-process TMPDIR + * value. + */ + + if ((tmpdir = getenv("TMPDIR")) != NULL && access(tmpdir, W_OK)) + tmpdir = NULL; + + if (!tmpdir) + { + if (confstr(_CS_DARWIN_USER_TEMP_DIR, tmppath, sizeof(tmppath))) + tmpdir = tmppath; + else + tmpdir = "/private/tmp"; /* This should never happen */ + } + #else /* * Previously we put root temporary files in the default CUPS temporary @@ -69,11 +91,7 @@ cupsTempFd(char *filename, /* I - Pointer to buffer */ */ if ((tmpdir = getenv("TMPDIR")) == NULL) -# if defined(__APPLE__) && !TARGET_OS_IOS - tmpdir = "/private/tmp"; /* /tmp is a symlink to /private/tmp */ -# else tmpdir = "/tmp"; -# endif /* __APPLE__ && !TARGET_OS_IOS */ #endif /* WIN32 */ /* -- 2.47.3