]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/getputfile.c
Move debug printfs to internal usage only.
[thirdparty/cups.git] / cups / getputfile.c
index 43952dc1f647d219f453e5e3842d33d4dcf2ac1f..818d5e9f61df8dfcde367fa66ca679737719c504 100644 (file)
@@ -1,53 +1,34 @@
 /*
- * "$Id: getputfile.c 7359 2008-02-29 19:01:35Z mike $"
+ * Get/put file functions for CUPS.
  *
- *   Get/put file functions for the Common UNIX Printing System (CUPS).
+ * Copyright 2007-2018 by Apple Inc.
+ * Copyright 1997-2006 by Easy Software Products.
  *
- *   Copyright 2007-2009 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
- *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
- *   which should have been included with this file.  If this file is
- *   file is missing or damaged, see the license at "http://www.cups.org/".
- *
- *   This file is subject to the Apple OS-Developed Software exception.
- *
- * Contents:
- *
- *   cupsGetFd()   - Get a file from the server.
- *   cupsGetFile() - Get a file from the server.
- *   cupsPutFd()   - Put a file on the server.
- *   cupsPutFile() - Put a file on the server.
+ * Licensed under Apache License v2.0.  See the file "LICENSE" for more
+ * information.
  */
 
 /*
  * Include necessary headers...
  */
 
-#include "globals.h"
-#include "cups.h"
-#include "language.h"
-#include "debug.h"
-#include <stdlib.h>
-#include <ctype.h>
-#include <errno.h>
+#include "cups-private.h"
+#include "debug-internal.h"
 #include <fcntl.h>
 #include <sys/stat.h>
-#if defined(WIN32) || defined(__EMX__)
+#if defined(_WIN32) || defined(__EMX__)
 #  include <io.h>
 #else
 #  include <unistd.h>
-#endif /* WIN32 || __EMX__ */
+#endif /* _WIN32 || __EMX__ */
 
 
 /*
  * 'cupsGetFd()' - Get a file from the server.
  *
- * This function returns @code HTTP_OK@ when the file is successfully retrieved.
+ * This function returns @code HTTP_STATUS_OK@ when the file is successfully retrieved.
  *
- * @since CUPS 1.1.20/Mac OS X 10.4@
+ * @since CUPS 1.1.20/macOS 10.4@
  */
 
 http_status_t                          /* O - HTTP status */
@@ -55,31 +36,32 @@ cupsGetFd(http_t     *http,         /* I - Connection to server or @code CUPS_HTTP_DEFA
          const char *resource,         /* I - Resource name */
          int        fd)                /* I - File descriptor */
 {
-  int          bytes;                  /* Number of bytes read */
+  ssize_t      bytes;                  /* Number of bytes read */
   char         buffer[8192];           /* Buffer for file */
   http_status_t        status;                 /* HTTP status from server */
   char         if_modified_since[HTTP_MAX_VALUE];
                                        /* If-Modified-Since header */
+  int          new_auth = 0;           /* Using new auth information? */
+  int          digest;                 /* Are we using Digest authentication? */
 
 
  /*
   * Range check input...
   */
 
-  DEBUG_printf(("cupsGetFd(http=%p, resource=\"%s\", fd=%d)", http,
-                resource, fd));
+  DEBUG_printf(("cupsGetFd(http=%p, resource=\"%s\", fd=%d)", (void *)http, resource, fd));
 
   if (!resource || fd < 0)
   {
     if (http)
       http->error = EINVAL;
 
-    return (HTTP_ERROR);
+    return (HTTP_STATUS_ERROR);
   }
 
   if (!http)
     if ((http = _cupsConnect()) == NULL)
-      return (HTTP_SERVICE_UNAVAILABLE);
+      return (HTTP_STATUS_SERVICE_UNAVAILABLE);
 
  /*
   * Then send GET requests to the HTTP server...
@@ -90,27 +72,63 @@ cupsGetFd(http_t     *http,         /* I - Connection to server or @code CUPS_HTTP_DEFA
 
   do
   {
+    if (!_cups_strcasecmp(httpGetField(http, HTTP_FIELD_CONNECTION), "close"))
+    {
+      httpClearFields(http);
+      if (httpReconnect2(http, 30000, NULL))
+      {
+       status = HTTP_STATUS_ERROR;
+       break;
+      }
+    }
+
     httpClearFields(http);
-    httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring);
     httpSetField(http, HTTP_FIELD_IF_MODIFIED_SINCE, if_modified_since);
 
+    digest = http->authstring && !strncmp(http->authstring, "Digest ", 7);
+
+    if (digest && !new_auth)
+    {
+     /*
+      * Update the Digest authentication string...
+      */
+
+      _httpSetDigestAuthString(http, http->nextnonce, "GET", resource);
+    }
+
+#ifdef HAVE_GSSAPI
+    if (http->authstring && !strncmp(http->authstring, "Negotiate", 9) && !new_auth)
+    {
+     /*
+      * Do not use cached Kerberos credentials since they will look like a
+      * "replay" attack...
+      */
+
+      _cupsSetNegotiateAuthString(http, "GET", resource);
+    }
+#endif /* HAVE_GSSAPI */
+
+    httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring);
+
     if (httpGet(http, resource))
     {
-      if (httpReconnect(http))
+      if (httpReconnect2(http, 30000, NULL))
       {
-        status = HTTP_ERROR;
+        status = HTTP_STATUS_ERROR;
        break;
       }
       else
       {
-        status = HTTP_UNAUTHORIZED;
+        status = HTTP_STATUS_UNAUTHORIZED;
         continue;
       }
     }
 
-    while ((status = httpUpdate(http)) == HTTP_CONTINUE);
+    new_auth = 0;
 
-    if (status == HTTP_UNAUTHORIZED)
+    while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE);
+
+    if (status == HTTP_STATUS_UNAUTHORIZED)
     {
      /*
       * Flush any error message...
@@ -122,52 +140,57 @@ cupsGetFd(http_t     *http,               /* I - Connection to server or @code CUPS_HTTP_DEFA
       * See if we can do authentication...
       */
 
+      new_auth = 1;
+
       if (cupsDoAuthentication(http, "GET", resource))
+      {
+        status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
         break;
+      }
 
-      if (httpReconnect(http))
+      if (httpReconnect2(http, 30000, NULL))
       {
-        status = HTTP_ERROR;
+        status = HTTP_STATUS_ERROR;
         break;
       }
 
       continue;
     }
 #ifdef HAVE_SSL
-    else if (status == HTTP_UPGRADE_REQUIRED)
+    else if (status == HTTP_STATUS_UPGRADE_REQUIRED)
     {
       /* Flush any error message... */
       httpFlush(http);
 
       /* Reconnect... */
-      if (httpReconnect(http))
+      if (httpReconnect2(http, 30000, NULL))
       {
-        status = HTTP_ERROR;
+        status = HTTP_STATUS_ERROR;
         break;
       }
 
       /* Upgrade with encryption... */
-      httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
+      httpEncryption(http, HTTP_ENCRYPTION_REQUIRED);
 
       /* Try again, this time with encryption enabled... */
       continue;
     }
 #endif /* HAVE_SSL */
   }
-  while (status == HTTP_UNAUTHORIZED || status == HTTP_UPGRADE_REQUIRED);
+  while (status == HTTP_STATUS_UNAUTHORIZED || status == HTTP_STATUS_UPGRADE_REQUIRED);
 
  /*
   * See if we actually got the file or an error...
   */
 
-  if (status == HTTP_OK)
+  if (status == HTTP_STATUS_OK)
   {
    /*
     * Yes, copy the file...
     */
 
     while ((bytes = httpRead2(http, buffer, sizeof(buffer))) > 0)
-      write(fd, buffer, bytes);
+      write(fd, buffer, (size_t)bytes);
   }
   else
   {
@@ -188,9 +211,9 @@ cupsGetFd(http_t     *http,         /* I - Connection to server or @code CUPS_HTTP_DEFA
 /*
  * 'cupsGetFile()' - Get a file from the server.
  *
- * This function returns @code HTTP_OK@ when the file is successfully retrieved.
+ * This function returns @code HTTP_STATUS_OK@ when the file is successfully retrieved.
  *
- * @since CUPS 1.1.20/Mac OS X 10.4@
+ * @since CUPS 1.1.20/macOS 10.4@
  */
 
 http_status_t                          /* O - HTTP status */
@@ -211,7 +234,7 @@ cupsGetFile(http_t     *http,               /* I - Connection to server or @code CUPS_HTTP_DE
     if (http)
       http->error = EINVAL;
 
-    return (HTTP_ERROR);
+    return (HTTP_STATUS_ERROR);
   }
 
  /*
@@ -226,7 +249,7 @@ cupsGetFile(http_t     *http,               /* I - Connection to server or @code CUPS_HTTP_DE
 
     http->error = errno;
 
-    return (HTTP_ERROR);
+    return (HTTP_STATUS_ERROR);
   }
 
  /*
@@ -241,7 +264,7 @@ cupsGetFile(http_t     *http,               /* I - Connection to server or @code CUPS_HTTP_DE
 
   close(fd);
 
-  if (status != HTTP_OK)
+  if (status != HTTP_STATUS_OK)
     unlink(filename);
 
  /*
@@ -255,10 +278,10 @@ cupsGetFile(http_t     *http,             /* I - Connection to server or @code CUPS_HTTP_DE
 /*
  * 'cupsPutFd()' - Put a file on the server.
  *
- * This function returns @code HTTP_CREATED@ when the file is stored
+ * This function returns @code HTTP_STATUS_CREATED@ when the file is stored
  * successfully.
  *
- * @since CUPS 1.1.20/Mac OS X 10.4@
+ * @since CUPS 1.1.20/macOS 10.4@
  */
 
 http_status_t                          /* O - HTTP status */
@@ -266,30 +289,31 @@ cupsPutFd(http_t     *http,               /* I - Connection to server or @code CUPS_HTTP_DEFA
           const char *resource,                /* I - Resource name */
          int        fd)                /* I - File descriptor */
 {
-  int          bytes,                  /* Number of bytes read */
-               retries;                /* Number of retries */
+  ssize_t      bytes;                  /* Number of bytes read */
+  int          retries;                /* Number of retries */
   char         buffer[8192];           /* Buffer for file */
   http_status_t        status;                 /* HTTP status from server */
+  int          new_auth = 0;           /* Using new auth information? */
+  int          digest;                 /* Are we using Digest authentication? */
 
 
  /*
   * Range check input...
   */
 
-  DEBUG_printf(("cupsPutFd(http=%p, resource=\"%s\", fd=%d)", http,
-                resource, fd));
+  DEBUG_printf(("cupsPutFd(http=%p, resource=\"%s\", fd=%d)", (void *)http, resource, fd));
 
   if (!resource || fd < 0)
   {
     if (http)
       http->error = EINVAL;
 
-    return (HTTP_ERROR);
+    return (HTTP_STATUS_ERROR);
   }
 
   if (!http)
     if ((http = _cupsConnect()) == NULL)
-      return (HTTP_SERVICE_UNAVAILABLE);
+      return (HTTP_STATUS_SERVICE_UNAVAILABLE);
 
  /*
   * Then send PUT requests to the HTTP server...
@@ -299,24 +323,58 @@ cupsPutFd(http_t     *http,               /* I - Connection to server or @code CUPS_HTTP_DEFA
 
   do
   {
+    if (!_cups_strcasecmp(httpGetField(http, HTTP_FIELD_CONNECTION), "close"))
+    {
+      httpClearFields(http);
+      if (httpReconnect2(http, 30000, NULL))
+      {
+       status = HTTP_STATUS_ERROR;
+       break;
+      }
+    }
+
     DEBUG_printf(("2cupsPutFd: starting attempt, authstring=\"%s\"...",
                   http->authstring));
 
     httpClearFields(http);
-    httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring);
     httpSetField(http, HTTP_FIELD_TRANSFER_ENCODING, "chunked");
-    httpSetExpect(http, HTTP_CONTINUE);
+    httpSetExpect(http, HTTP_STATUS_CONTINUE);
+
+    digest = http->authstring && !strncmp(http->authstring, "Digest ", 7);
+
+    if (digest && !new_auth)
+    {
+     /*
+      * Update the Digest authentication string...
+      */
+
+      _httpSetDigestAuthString(http, http->nextnonce, "PUT", resource);
+    }
+
+#ifdef HAVE_GSSAPI
+    if (http->authstring && !strncmp(http->authstring, "Negotiate", 9) && !new_auth)
+    {
+     /*
+      * Do not use cached Kerberos credentials since they will look like a
+      * "replay" attack...
+      */
+
+      _cupsSetNegotiateAuthString(http, "PUT", resource);
+    }
+#endif /* HAVE_GSSAPI */
+
+    httpSetField(http, HTTP_FIELD_AUTHORIZATION, http->authstring);
 
     if (httpPut(http, resource))
     {
-      if (httpReconnect(http))
+      if (httpReconnect2(http, 30000, NULL))
       {
-        status = HTTP_ERROR;
+        status = HTTP_STATUS_ERROR;
        break;
       }
       else
       {
-        status = HTTP_UNAUTHORIZED;
+        status = HTTP_STATUS_UNAUTHORIZED;
         continue;
       }
     }
@@ -328,9 +386,9 @@ cupsPutFd(http_t     *http,         /* I - Connection to server or @code CUPS_HTTP_DEFA
     if (httpWait(http, 1000))
       status = httpUpdate(http);
     else
-      status = HTTP_CONTINUE;
+      status = HTTP_STATUS_CONTINUE;
 
-    if (status == HTTP_CONTINUE)
+    if (status == HTTP_STATUS_CONTINUE)
     {
      /*
       * Copy the file...
@@ -341,21 +399,21 @@ cupsPutFd(http_t     *http,               /* I - Connection to server or @code CUPS_HTTP_DEFA
       while ((bytes = read(fd, buffer, sizeof(buffer))) > 0)
        if (httpCheck(http))
        {
-          if ((status = httpUpdate(http)) != HTTP_CONTINUE)
+          if ((status = httpUpdate(http)) != HTTP_STATUS_CONTINUE)
             break;
        }
        else
-          httpWrite2(http, buffer, bytes);
+          httpWrite2(http, buffer, (size_t)bytes);
     }
 
-    if (status == HTTP_CONTINUE)
+    if (status == HTTP_STATUS_CONTINUE)
     {
       httpWrite2(http, buffer, 0);
 
-      while ((status = httpUpdate(http)) == HTTP_CONTINUE);
+      while ((status = httpUpdate(http)) == HTTP_STATUS_CONTINUE);
     }
 
-    if (status == HTTP_ERROR && !retries)
+    if (status == HTTP_STATUS_ERROR && !retries)
     {
       DEBUG_printf(("2cupsPutFd: retry on status %d", status));
 
@@ -365,9 +423,9 @@ cupsPutFd(http_t     *http,         /* I - Connection to server or @code CUPS_HTTP_DEFA
       httpFlush(http);
 
       /* Reconnect... */
-      if (httpReconnect(http))
+      if (httpReconnect2(http, 30000, NULL))
       {
-        status = HTTP_ERROR;
+        status = HTTP_STATUS_ERROR;
         break;
       }
 
@@ -377,7 +435,9 @@ cupsPutFd(http_t     *http,         /* I - Connection to server or @code CUPS_HTTP_DEFA
 
     DEBUG_printf(("2cupsPutFd: status=%d", status));
 
-    if (status == HTTP_UNAUTHORIZED)
+    new_auth = 0;
+
+    if (status == HTTP_STATUS_UNAUTHORIZED)
     {
      /*
       * Flush any error message...
@@ -389,46 +449,51 @@ cupsPutFd(http_t     *http,               /* I - Connection to server or @code CUPS_HTTP_DEFA
       * See if we can do authentication...
       */
 
+      new_auth = 1;
+
       if (cupsDoAuthentication(http, "PUT", resource))
+      {
+        status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
         break;
+      }
 
-      if (httpReconnect(http))
+      if (httpReconnect2(http, 30000, NULL))
       {
-        status = HTTP_ERROR;
+        status = HTTP_STATUS_ERROR;
         break;
       }
 
       continue;
     }
 #ifdef HAVE_SSL
-    else if (status == HTTP_UPGRADE_REQUIRED)
+    else if (status == HTTP_STATUS_UPGRADE_REQUIRED)
     {
       /* Flush any error message... */
       httpFlush(http);
 
       /* Reconnect... */
-      if (httpReconnect(http))
+      if (httpReconnect2(http, 30000, NULL))
       {
-        status = HTTP_ERROR;
+        status = HTTP_STATUS_ERROR;
         break;
       }
 
       /* Upgrade with encryption... */
-      httpEncryption(http, HTTP_ENCRYPT_REQUIRED);
+      httpEncryption(http, HTTP_ENCRYPTION_REQUIRED);
 
       /* Try again, this time with encryption enabled... */
       continue;
     }
 #endif /* HAVE_SSL */
   }
-  while (status == HTTP_UNAUTHORIZED || status == HTTP_UPGRADE_REQUIRED ||
-         (status == HTTP_ERROR && retries < 2));
+  while (status == HTTP_STATUS_UNAUTHORIZED || status == HTTP_STATUS_UPGRADE_REQUIRED ||
+         (status == HTTP_STATUS_ERROR && retries < 2));
 
  /*
   * See if we actually put the file or an error...
   */
 
-  if (status != HTTP_CREATED)
+  if (status != HTTP_STATUS_CREATED)
   {
     _cupsSetHTTPError(status);
     httpFlush(http);
@@ -446,7 +511,7 @@ cupsPutFd(http_t     *http,         /* I - Connection to server or @code CUPS_HTTP_DEFA
  * This function returns @code HTTP_CREATED@ when the file is stored
  * successfully.
  *
- * @since CUPS 1.1.20/Mac OS X 10.4@
+ * @since CUPS 1.1.20/macOS 10.4@
  */
 
 http_status_t                          /* O - HTTP status */
@@ -467,7 +532,7 @@ cupsPutFile(http_t     *http,               /* I - Connection to server or @code CUPS_HTTP_DE
     if (http)
       http->error = EINVAL;
 
-    return (HTTP_ERROR);
+    return (HTTP_STATUS_ERROR);
   }
 
  /*
@@ -482,7 +547,7 @@ cupsPutFile(http_t     *http,               /* I - Connection to server or @code CUPS_HTTP_DE
 
     http->error = errno;
 
-    return (HTTP_ERROR);
+    return (HTTP_STATUS_ERROR);
   }
 
  /*
@@ -495,8 +560,3 @@ cupsPutFile(http_t     *http,               /* I - Connection to server or @code CUPS_HTTP_DE
 
   return (status);
 }
-
-
-/*
- * End of "$Id: getputfile.c 7359 2008-02-29 19:01:35Z mike $".
- */