]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/auth.c
Merge changes from CUPS 1.5svn-r9313.
[thirdparty/cups.git] / cups / auth.c
index da37c0bfc7247fd669fa0b31b4409b9604f074e6..4d7facf4e1535e065dd9a275c9d1a3d36e10c6eb 100644 (file)
@@ -1,9 +1,9 @@
 /*
  * "$Id: auth.c 7720 2008-07-11 22:46:21Z mike $"
  *
- *   Authentication functions for the Common UNIX Printing System (CUPS).
+ *   Authentication functions for CUPS.
  *
- *   Copyright 2007-2009 by Apple Inc.
+ *   Copyright 2007-2010 by Apple Inc.
  *   Copyright 1997-2007 by Easy Software Products.
  *
  *   This file contains Kerberos support code, copyright 2006 by
  * Include necessary headers...
  */
 
-#include "globals.h"
-#include "debug.h"
-#include <stdlib.h>
-#include <ctype.h>
-#include <errno.h>
+#include "cups-private.h"
 #include <fcntl.h>
 #include <sys/stat.h>
 #if defined(WIN32) || defined(__EMX__)
@@ -83,10 +79,10 @@ static int  cups_local_auth(http_t *http);
  */
 
 int                                    /* O - 0 on success, -1 on error */
-cupsDoAuthentication(http_t     *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
-                     const char *method,/* I - Request method ("GET", "POST", "PUT") */
-                    const char *resource)
-                                       /* I - Resource path */
+cupsDoAuthentication(
+    http_t     *http,                  /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
+    const char *method,                        /* I - Request method ("GET", "POST", "PUT") */
+    const char *resource)              /* I - Resource path */
 {
   const char   *password;              /* Password string */
   char         prompt[1024],           /* Prompt for user */
@@ -98,6 +94,13 @@ cupsDoAuthentication(http_t     *http,       /* I - Connection to server or @code CUPS
 
   DEBUG_printf(("cupsDoAuthentication(http=%p, method=\"%s\", resource=\"%s\")",
                 http, method, resource));
+
+  if (!http)
+    http = _cupsConnect();
+
+  if (!http || !method || !resource)
+    return (-1);
+
   DEBUG_printf(("2cupsDoAuthentication: digest_tries=%d, userpass=\"%s\"",
                 http->digest_tries, http->userpass));
   DEBUG_printf(("2cupsDoAuthentication: WWW-Authenticate=\"%s\"",
@@ -126,7 +129,10 @@ cupsDoAuthentication(http_t     *http,     /* I - Connection to server or @code CUPS
       return (0);
     }
     else if (localauth == -1)
+    {
+      http->status = HTTP_AUTHORIZATION_CANCELED;
       return (-1);                     /* Error or canceled */
+    }
   }
 
  /*
@@ -154,11 +160,11 @@ cupsDoAuthentication(http_t     *http,    /* I - Connection to server or @code CUPS
                                       "Digest", 5) != 0;
     http->userpass[0]   = '\0';
 
-    if ((password = cupsGetPassword(prompt)) == NULL)
-      return (-1);
-
-    if (!password[0])
+    if ((password = cupsGetPassword2(prompt, http, method, resource)) == NULL)
+    {
+      http->status = HTTP_AUTHORIZATION_CANCELED;
       return (-1);
+    }
 
     snprintf(http->userpass, sizeof(http->userpass), "%s:%s", cupsUser(),
              password);
@@ -166,6 +172,15 @@ cupsDoAuthentication(http_t     *http,     /* I - Connection to server or @code CUPS
   else if (http->status == HTTP_UNAUTHORIZED)
     http->digest_tries ++;
 
+  if (http->status == HTTP_UNAUTHORIZED && http->digest_tries >= 3)
+  {
+    DEBUG_printf(("1cupsDoAuthentication: Too many authentication tries (%d)",
+                 http->digest_tries));
+
+    http->status = HTTP_AUTHORIZATION_CANCELED;
+    return (-1);
+  }
+
  /*
   * Got a password; encode it for the server...
   */
@@ -201,17 +216,11 @@ cupsDoAuthentication(http_t     *http,    /* I - Connection to server or @code CUPS
     {
       DEBUG_puts("1cupsDoAuthentication: Weak-linked GSSAPI/Kerberos framework "
                  "is not present");
-      return (-1);
-    }
-#  endif /* __APPLE__ */
+      http->status = HTTP_AUTHORIZATION_CANCELED;
 
-    if (http->status == HTTP_UNAUTHORIZED && http->digest_tries >= 3)
-    {
-      DEBUG_printf(("1cupsDoAuthentication: too many Negotiate tries (%d)",
-                    http->digest_tries));
-  
       return (-1);
     }
+#  endif /* __APPLE__ */
 
     if (http->gssname == GSS_C_NO_NAME)
     {
@@ -232,7 +241,7 @@ cupsDoAuthentication(http_t     *http,      /* I - Connection to server or @code CUPS
     authorization = httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE);
 
     authorization += 9;
-    while (*authorization && isspace(*authorization & 255))
+    while (*authorization && _cups_isspace(*authorization))
       authorization ++;
 
     if (*authorization)
@@ -295,6 +304,8 @@ cupsDoAuthentication(http_t     *http,      /* I - Connection to server or @code CUPS
       cups_gss_printf(major_status, minor_status,
                      "cupsDoAuthentication: Unable to initialize security "
                      "context");
+      http->status = HTTP_AUTHORIZATION_CANCELED;
+
       return (-1);
     }
 
@@ -331,7 +342,7 @@ cupsDoAuthentication(http_t     *http,      /* I - Connection to server or @code CUPS
     {
       DEBUG_printf(("1cupsDoAuthentication: Kerberos credentials too large - "
                     "%d bytes!", (int)output_token.length));
-
+      http->status = HTTP_AUTHORIZATION_CANCELED;
       gss_release_buffer(&minor_status, &output_token);
 
       return (-1);
@@ -467,7 +478,7 @@ cups_gss_printf(OM_uint32  major_status,/* I - Major status code */
     gss_display_status(&err_minor_status, minor_status, GSS_C_MECH_CODE,
                       GSS_C_NULL_OID, &msg_ctx, &minor_status_string);
 
-  DEBUG_printf(("8%s: %s, %s", message, (char *)major_status_string.value,
+  DEBUG_printf(("1%s: %s, %s", message, (char *)major_status_string.value,
                (char *)minor_status_string.value));
 
   gss_release_buffer(&err_minor_status, &major_status_string);
@@ -595,6 +606,42 @@ cups_local_auth(http_t *http)              /* I - HTTP connection to server */
   }
 #  endif /* HAVE_AUTHORIZATION_H */
 
+#  if defined(SO_PEERCRED) && defined(AF_LOCAL)
+ /*
+  * See if we can authenticate using the peer credentials provided over a
+  * domain socket; if so, specify "PeerCred username" as the authentication
+  * information...
+  */
+
+#    ifdef HAVE_GSSAPI
+  if (strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9) &&
+#    else
+  if (
+#    endif /* HAVE_GSSAPI */
+      http->hostaddr->addr.sa_family == AF_LOCAL &&
+      !getenv("GATEWAY_INTERFACE"))    /* Not via CGI programs... */
+  {
+   /*
+    * Verify that the current cupsUser() matches the current UID...
+    */
+
+    struct passwd      *pwd;           /* Password information */
+    const char         *username;      /* Current username */
+
+    username = cupsUser();
+
+    if ((pwd = getpwnam(username)) != NULL && pwd->pw_uid == getuid())
+    {
+      httpSetAuthString(http, "PeerCred", username);
+
+      DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
+                   http->authstring));
+
+      return (0);
+    }
+  }
+#  endif /* SO_PEERCRED && AF_LOCAL */
+
  /*
   * Try opening a certificate file for this PID.  If that fails,
   * try the root certificate...
@@ -657,37 +704,6 @@ cups_local_auth(http_t *http)              /* I - HTTP connection to server */
     return (0);
   }
 
-#  if defined(SO_PEERCRED) && defined(AF_LOCAL)
- /*
-  * See if we can authenticate using the peer credentials provided over a
-  * domain socket; if so, specify "PeerCred username" as the authentication
-  * information...
-  */
-
-  if (http->hostaddr->addr.sa_family == AF_LOCAL &&
-      !getenv("GATEWAY_INTERFACE"))    /* Not via CGI programs... */
-  {
-   /*
-    * Verify that the current cupsUser() matches the current UID...
-    */
-
-    struct passwd      *pwd;           /* Password information */
-    const char         *username;      /* Current username */
-
-    username = cupsUser();
-
-    if ((pwd = getpwnam(username)) != NULL && pwd->pw_uid == getuid())
-    {
-      httpSetAuthString(http, "PeerCred", username);
-
-      DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
-                   http->authstring));
-
-      return (0);
-    }
-  }
-#  endif /* SO_PEERCRED && AF_LOCAL */
-
   return (1);
 #endif /* WIN32 || __EMX__ */
 }