]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/ppd-util.c
cupsGetPPD* still used the old TMPDIR code.
[thirdparty/cups.git] / cups / ppd-util.c
index 939e63288a25d2d4daaf439abc4dc4a1ba49a5a5..fd1c0b02f28ee3af4330a4c0d5e41b536eb03b37 100644 (file)
@@ -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.
  *
  * Licensed under Apache License v2.0.  See the file "LICENSE" for more
  * information.
@@ -150,18 +150,21 @@ cupsGetPPD3(http_t     *http,             /* I  - HTTP connection or @code CUPS_HTTP_DEFAUL
 
   if (!name)
   {
+    DEBUG_puts("2cupsGetPPD3: No printer name, returning NULL.");
     _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No printer name"), 1);
     return (HTTP_STATUS_NOT_ACCEPTABLE);
   }
 
   if (!modtime)
   {
+    DEBUG_puts("2cupsGetPPD3: No modtime, returning NULL.");
     _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("No modification time"), 1);
     return (HTTP_STATUS_NOT_ACCEPTABLE);
   }
 
   if (!buffer || bufsize <= 1)
   {
+    DEBUG_puts("2cupsGetPPD3: No filename buffer, returning NULL.");
     _cupsSetError(IPP_STATUS_ERROR_INTERNAL, _("Bad filename buffer"), 1);
     return (HTTP_STATUS_NOT_ACCEPTABLE);
   }
@@ -196,6 +199,8 @@ cupsGetPPD3(http_t     *http,               /* I  - HTTP connection or @code CUPS_HTTP_DEFAUL
 
       if (buffer[0])
       {
+        DEBUG_printf(("2cupsGetPPD3: Using filename \"%s\".", buffer));
+
         unlink(buffer);
 
        if (symlink(ppdname, buffer) && errno != EEXIST)
@@ -211,6 +216,29 @@ 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.  Currently this only happens if TMPDIR is not set or is set to
+       * "/Users/...".
+       */
+
+        char           tmppath[1024];  /* Temporary directory */
+
+       if ((tmpdir = getenv("TMPDIR")) != NULL && !strncmp(tmpdir, "/Users/", 7)))
+         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
@@ -219,11 +247,10 @@ 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__ */
+
+        DEBUG_printf(("2cupsGetPPD3: tmpdir=\"%s\".", tmpdir));
 
        /*
        * Make the temporary name using the specified directory...
@@ -254,12 +281,16 @@ cupsGetPPD3(http_t     *http,             /* I  - HTTP connection or @code CUPS_HTTP_DEFAUL
          if (!symlink(ppdname, buffer))
            break;
 
+         DEBUG_printf(("2cupsGetPPD3: Symlink \"%s\" to \"%s\" failed: %s", ppdname, buffer, strerror(errno)));
+
          tries ++;
        }
        while (tries < 1000);
 
         if (tries >= 1000)
        {
+         DEBUG_puts("2cupsGetPPD3: Unable to symlink after 1000 tries, returning error.");
+
           _cupsSetError(IPP_STATUS_ERROR_INTERNAL, NULL, 0);
 
          return (HTTP_STATUS_SERVER_ERROR);
@@ -267,9 +298,13 @@ cupsGetPPD3(http_t     *http,              /* I  - HTTP connection or @code CUPS_HTTP_DEFAUL
       }
 
       if (*modtime >= ppdinfo.st_mtime)
+      {
+        DEBUG_printf(("2cupsGetPPD3: Returning not-modified, filename=\"%s\".", buffer));
         return (HTTP_STATUS_NOT_MODIFIED);
+      }
       else
       {
+        DEBUG_printf(("2cupsGetPPD3: Returning ok, filename=\"%s\", modtime=%ld.", buffer, (long)ppdinfo.st_mtime));
         *modtime = ppdinfo.st_mtime;
        return (HTTP_STATUS_OK);
       }
@@ -281,16 +316,24 @@ cupsGetPPD3(http_t     *http,             /* I  - HTTP connection or @code CUPS_HTTP_DEFAUL
   * Try finding a printer URI for this printer...
   */
 
+  DEBUG_puts("2cupsGetPPD3: Unable to access local file, copying...");
+
   if (!http)
+  {
     if ((http = _cupsConnect()) == NULL)
+    {
+      DEBUG_puts("2cupsGetPPD3: Unable to connect to scheduler.");
       return (HTTP_STATUS_SERVICE_UNAVAILABLE);
+    }
+  }
 
-  if (!cups_get_printer_uri(http, name, hostname, sizeof(hostname), &port,
-                            resource, sizeof(resource), 0))
+  if (!cups_get_printer_uri(http, name, hostname, sizeof(hostname), &port, resource, sizeof(resource), 0))
+  {
+    DEBUG_puts("2cupsGetPPD3: Unable to get printer URI.");
     return (HTTP_STATUS_NOT_FOUND);
+  }
 
-  DEBUG_printf(("2cupsGetPPD3: Printer hostname=\"%s\", port=%d", hostname,
-                port));
+  DEBUG_printf(("2cupsGetPPD3: Printer hostname=\"%s\", port=%d", hostname, port));
 
   if (cupsServer()[0] == '/' && !_cups_strcasecmp(hostname, "localhost") && port == ippPort())
   {
@@ -334,7 +377,7 @@ cupsGetPPD3(http_t     *http,               /* I  - HTTP connection or @code CUPS_HTTP_DEFAUL
   else if ((http2 = httpConnect2(hostname, port, NULL, AF_UNSPEC,
                                 cupsEncryption(), 1, 30000, NULL)) == NULL)
   {
-    DEBUG_puts("1cupsGetPPD3: Unable to connect to server");
+    DEBUG_puts("2cupsGetPPD3: Unable to connect to server");
 
     return (HTTP_STATUS_SERVICE_UNAVAILABLE);
   }
@@ -406,7 +449,7 @@ cupsGetPPD3(http_t     *http,               /* I  - HTTP connection or @code CUPS_HTTP_DEFAUL
   * Return the PPD file...
   */
 
-  DEBUG_printf(("1cupsGetPPD3: Returning status %d", status));
+  DEBUG_printf(("2cupsGetPPD3: Returning status %d", status));
 
   return (status);
 }