]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/auth.c
Import CUPS 1.4svn-r7356.
[thirdparty/cups.git] / cups / auth.c
index 1ee0476f2590c20255f768ca87aaa0a8b471469d..4391a3db04c6b6e4aa08021d8ca7d97ef6aadd98 100644 (file)
@@ -1,28 +1,19 @@
 /*
- * "$Id: auth.c 6397 2007-03-25 23:33:32Z mike $"
+ * "$Id: auth.c 6779 2007-08-08 19:49:48Z mike $"
  *
  *   Authentication functions for the Common UNIX Printing System (CUPS).
  *
+ *   Copyright 2007-2008 by Apple Inc.
  *   Copyright 1997-2007 by Easy Software Products.
  *
  *   This file contains Kerberos support code, copyright 2006 by
  *   Jelmer Vernooij.
  *
  *   These coded instructions, statements, and computer programs are the
- *   property of Easy Software Products 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 missing or damaged please contact Easy Software Products
- *   at:
- *
- *       Attn: CUPS Licensing Information
- *       Easy Software Products
- *       44141 Airport View Drive, Suite 204
- *       Hollywood, Maryland 20636 USA
- *
- *       Voice: (301) 373-9600
- *       EMail: cups-info@cups.org
- *         WWW: http://www.cups.org
+ *   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.
  *
@@ -61,6 +52,10 @@ extern const char *cssmErrorString(int error);
 #  endif /* HAVE_SECBASEPRIV_H */
 #endif /* HAVE_AUTHORIZATION_H */
 
+#if defined(SO_PEERCRED) && defined(AF_LOCAL)
+#  include <pwd.h>
+#endif /* SO_PEERCRED && AF_LOCAL */
+
 
 /*
  * Local functions...
@@ -70,6 +65,8 @@ extern const char *cssmErrorString(int error);
 #  ifdef DEBUG
 static void    DEBUG_gss_printf(OM_uint32 major_status, OM_uint32 minor_status,
                                 const char *message);
+#  else
+#    define DEBUG_gss_printf(major, minor, message)
 #  endif /* DEBUG  */
 static gss_name_t cups_get_gss_creds(http_t *http, const char *service_name);
 #endif /* HAVE_GSSAPI */
@@ -79,23 +76,22 @@ static int  cups_local_auth(http_t *http);
 /*
  * 'cupsDoAuthentication()' - Authenticate a request.
  *
- * This function should be called in response to a HTTP_UNAUTHORIZED
+ * This function should be called in response to a @code HTTP_UNAUTHORIZED@
  * status, prior to resubmitting your request.
  *
  * @since CUPS 1.1.20@
  */
 
 int                                    /* O - 0 on success, -1 on error */
-cupsDoAuthentication(http_t     *http, /* I - HTTP connection to server */
-                     const char *method,/* I - Request method (GET, POST, PUT) */
+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 */
                realm[HTTP_MAX_VALUE],  /* realm="xyz" string */
-               nonce[HTTP_MAX_VALUE],  /* nonce="xyz" string */
-               encode[2048];           /* Encoded username:password */
+               nonce[HTTP_MAX_VALUE];  /* nonce="xyz" string */
   int          localauth;              /* Local authentication result */
   _cups_globals_t *cg;                 /* Global data */
 
@@ -111,12 +107,7 @@ cupsDoAuthentication(http_t     *http,     /* I - HTTP connection to server */
   * Clear the current authentication string...
   */
 
-  http->_authstring[0] = '\0';
-
-  if (http->authstring && http->authstring != http->_authstring)
-    free(http->authstring);
-
-  http->authstring = http->_authstring;
+  httpSetAuthString(http, NULL, NULL);
 
  /*
   * See if we can do local authentication...
@@ -194,8 +185,10 @@ cupsDoAuthentication(http_t     *http,     /* I - HTTP connection to server */
                                        /* Input token */
     char               *gss_service_name;
                                        /* GSS service name */
+#  ifdef USE_SPNEGO
     const char         *authorization;
                                        /* Pointer into Authorization string */
+#  endif /* USE_SPNEGO */
 
 
 #  ifdef __APPLE__
@@ -212,6 +205,14 @@ cupsDoAuthentication(http_t     *http,     /* I - HTTP connection to server */
     }
 #  endif /* __APPLE__ */
 
+    if (http->status == HTTP_UNAUTHORIZED && http->digest_tries >= 3)
+    {
+      DEBUG_printf(("cupsDoAuthentication: too many Negotiate tries (%d)\n",
+                    http->digest_tries));
+  
+      return (-1);
+    }
+
     if (http->gssname == GSS_C_NO_NAME)
     {
       if ((gss_service_name = getenv("CUPS_GSSSERVICENAME")) == NULL)
@@ -222,6 +223,7 @@ cupsDoAuthentication(http_t     *http,      /* I - HTTP connection to server */
       http->gssname = cups_get_gss_creds(http, gss_service_name);
     }
 
+#  ifdef USE_SPNEGO /* We don't implement SPNEGO just yet... */
    /*
     * Find the start of the Kerberos input token...
     */
@@ -235,24 +237,50 @@ cupsDoAuthentication(http_t     *http,    /* I - HTTP connection to server */
     if (*authorization)
     {
      /*
-      * For SPNEGO, this is where we'll feed the server's authorization data
-      * back into gss via input_token...
+      * Decode the authorization string to get the input token...
       */
-    }
-    else
-    {
-      if (http->gssctx != GSS_C_NO_CONTEXT)
+
+      int len = strlen(authorization);
+
+      input_token.value  = malloc(len);
+      input_token.value  = httpDecode64_2(input_token.value, &len,
+                                         authorization);
+      input_token.length = len;
+
+#    ifdef DEBUG
       {
-       major_status = gss_delete_sec_context(&minor_status, &http->gssctx,
-                                             GSS_C_NO_BUFFER);
-       http->gssctx = GSS_C_NO_CONTEXT;
+        char *ptr = (char *)input_token.value;
+       int left = len;
+
+        fputs("input_token=", stdout);
+       while (left > 0)
+       {
+         if (*ptr < ' ')
+           printf("\\%03o", *ptr & 255);
+         else
+           putchar(*ptr);
+         ptr ++;
+         left --;
+       }
+       putchar('\n');
       }
+#    endif /* DEBUG */
+    }
+#  endif /* USE_SPNEGO */
+
+    if (http->gssctx != GSS_C_NO_CONTEXT)
+    {
+      major_status = gss_delete_sec_context(&minor_status, &http->gssctx,
+                                           GSS_C_NO_BUFFER);
+      http->gssctx = GSS_C_NO_CONTEXT;
     }
 
     major_status  = gss_init_sec_context(&minor_status, GSS_C_NO_CREDENTIAL,
                                         &http->gssctx,
                                         http->gssname, http->gssmech,
-                                        GSS_C_MUTUAL_FLAG, GSS_C_INDEFINITE,
+                                        GSS_C_DELEG_FLAG | GSS_C_MUTUAL_FLAG |
+                                            GSS_C_INTEG_FLAG,
+                                        GSS_C_INDEFINITE,
                                         GSS_C_NO_CHANNEL_BINDINGS,
                                         &input_token, &http->gssmech,
                                         &output_token, NULL, NULL);
@@ -264,32 +292,48 @@ cupsDoAuthentication(http_t     *http,    /* I - HTTP connection to server */
     {
 #  ifdef DEBUG
       DEBUG_gss_printf(major_status, minor_status,
-                      "Unable to initialise security context");
+                      "Unable to initialize security context");
 #  endif /* DEBUG */
       return (-1);
     }
 
-#  ifdef DEBUG
     if (major_status == GSS_S_CONTINUE_NEEDED)
       DEBUG_gss_printf(major_status, minor_status, "Continuation needed!");
-#  endif /* DEBUG */
 
-    if (output_token.length)
+    if (output_token.length > 0 && output_token.length <= 65536)
     {
-      httpEncode64_2(encode, sizeof(encode), output_token.value,
-                    output_token.length);
+     /*
+      * Allocate the authorization string since Windows KDCs can have
+      * arbitrarily large credentials...
+      */
+
+      int authsize = 10 +                              /* "Negotiate " */
+                     output_token.length * 4 / 3 + 1 + /* Base64 */
+                    1;                                 /* nul */
 
-      http->authstring = malloc(strlen(encode) + 11);
-      sprintf(http->authstring, "Negotiate %s", encode); /* Safe because allocated */
+      httpSetAuthString(http, NULL, NULL);
+
+      if ((http->authstring = malloc(authsize)) == NULL)
+      {
+        http->authstring = http->_authstring;
+       authsize         = sizeof(http->_authstring);
+      }
+
+      strcpy(http->authstring, "Negotiate ");
+      httpEncode64_2(http->authstring + 10, authsize - 10, output_token.value,
+                    output_token.length);
  
       major_status = gss_release_buffer(&minor_status, &output_token);
     }
+    else
+    {
+      DEBUG_printf(("cupsDoAuthentication: Kerberos credentials too large - "
+                    "%d bytes!\n", output_token.length));
 
-   /*
-    * Copy back what we can to _authstring for backwards compatibility...
-    */
+      major_status = gss_release_buffer(&minor_status, &output_token);
 
-    strlcpy(http->_authstring, http->authstring, sizeof(http->_authstring));
+      return (-1);
+    }
 #endif /* HAVE_GSSAPI */
   }
   else if (strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Digest", 6))
@@ -298,9 +342,12 @@ cupsDoAuthentication(http_t     *http,     /* I - HTTP connection to server */
     * Basic authentication...
     */
 
+    char       encode[256];            /* Base64 buffer */
+
+
     httpEncode64_2(encode, sizeof(encode), http->userpass,
                    (int)strlen(http->userpass));
-    snprintf(http->_authstring, sizeof(http->_authstring), "Basic %s", encode);
+    httpSetAuthString(http, "Basic", encode);
   }
   else
   {
@@ -308,15 +355,19 @@ cupsDoAuthentication(http_t     *http,    /* I - HTTP connection to server */
     * Digest authentication...
     */
 
+    char       encode[33],             /* MD5 buffer */
+               digest[1024];           /* Digest auth data */
+
+
     httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE, "realm", realm);
     httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE, "nonce", nonce);
 
     httpMD5(cupsUser(), realm, strchr(http->userpass, ':') + 1, encode);
     httpMD5Final(nonce, method, resource, encode);
-    snprintf(http->_authstring, sizeof(http->_authstring),
-            "Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", "
-            "uri=\"%s\", response=\"%s\"", cupsUser(), realm, nonce,
-            resource, encode);
+    snprintf(digest, sizeof(digest),
+            "username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", "
+            "response=\"%s\"", cupsUser(), realm, nonce, resource, encode);
+    httpSetAuthString(http, "Digest", digest);
   }
 
   DEBUG_printf(("cupsDoAuthentication: authstring=\"%s\"\n", http->authstring));
@@ -381,19 +432,29 @@ cups_get_gss_creds(
 {
   gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
                                        /* Service token */
-  OM_uint32    major_status,           /* Major status code */
-               minor_status;           /* Minor status code */
-  gss_name_t   server_name;            /* Server name */
-  char         buf[1024],              /* Name buffer */
-               fqdn[HTTP_MAX_URI];     /* Server name buffer */
+  OM_uint32      major_status,         /* Major status code */
+                 minor_status;         /* Minor status code */
+  gss_name_t     server_name;          /* Server name */
+  char           buf[1024],            /* Name buffer */
+                 fqdn[HTTP_MAX_URI];   /* Server name buffer */
+
+
+ /*
+  * Get the hostname...
+  */
+
+  httpGetHostname(http, fqdn, sizeof(fqdn));
 
+  if (!strcmp(fqdn, "localhost"))
+    httpGetHostname(NULL, fqdn, sizeof(fqdn));
 
  /*
   * Get a server name we can use for authentication purposes...
   */
 
-  snprintf(buf, sizeof(buf), "%s@%s", service_name,
-          httpGetHostname(http, fqdn, sizeof(fqdn)));
+  snprintf(buf, sizeof(buf), "%s@%s", service_name, fqdn);
+
+  DEBUG_printf(("cups_get_gss_creds: Looking up %s...\n", buf));
 
   token.value  = buf;
   token.length = strlen(buf);
@@ -422,7 +483,7 @@ cups_get_gss_creds(
  */
 
 static int                             /* O - 0 if available */
-                                       /*     1 if not  available */
+                                       /*     1 if not available */
                                        /*    -1 error */
 cups_local_auth(http_t *http)          /* I - HTTP connection to server */
 {
@@ -431,14 +492,14 @@ cups_local_auth(http_t *http)             /* I - HTTP connection to server */
   * Currently WIN32 and OS-2 do not support the CUPS server...
   */
 
-  return (-1);
+  return (1);
 #else
   int                  pid;            /* Current process ID */
   FILE                 *fp;            /* Certificate file */
   char                 filename[1024], /* Certificate filename */
                        certificate[33];/* Certificate string */
   _cups_globals_t *cg = _cupsGlobals();        /* Global data */
-#if defined(HAVE_AUTHORIZATION_H)
+#  if defined(HAVE_AUTHORIZATION_H)
   OSStatus             status;         /* Status */
   AuthorizationItem    auth_right;     /* Authorization right */
   AuthorizationRights  auth_rights;    /* Authorization rights */
@@ -446,7 +507,7 @@ cups_local_auth(http_t *http)               /* I - HTTP connection to server */
   AuthorizationExternalForm auth_extrn;        /* Authorization ref external */
   char                 auth_key[1024]; /* Buffer */
   char                 buffer[1024];   /* Buffer */
-#endif /* HAVE_AUTHORIZATION_H */
+#  endif /* HAVE_AUTHORIZATION_H */
 
 
   DEBUG_printf(("cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"\n",
@@ -460,25 +521,25 @@ cups_local_auth(http_t *http)             /* I - HTTP connection to server */
       strcasecmp(http->hostname, "localhost") != 0)
   {
     DEBUG_puts("cups_local_auth: Not a local connection!");
-    return (-1);
+    return (1);
   }
 
-#if defined(HAVE_AUTHORIZATION_H)
+#  if defined(HAVE_AUTHORIZATION_H)
  /*
   * Delete any previous authorization reference...
   */
   
-  if (cg->auth_ref)
+  if (http->auth_ref)
   {
-    AuthorizationFree(cg->auth_ref, kAuthorizationFlagDefaults);
-    cg->auth_ref = NULL;
+    AuthorizationFree(http->auth_ref, kAuthorizationFlagDefaults);
+    http->auth_ref = NULL;
   }
 
   if (httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "authkey", 
                       auth_key, sizeof(auth_key)))
   {
     status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, 
-                                kAuthorizationFlagDefaults, &cg->auth_ref);
+                                kAuthorizationFlagDefaults, &http->auth_ref);
     if (status != errAuthorizationSuccess)
     {
       DEBUG_printf(("cups_local_auth: AuthorizationCreate() returned %d (%s)\n",
@@ -499,11 +560,11 @@ cups_local_auth(http_t *http)             /* I - HTTP connection to server */
                 kAuthorizationFlagInteractionAllowed | 
                 kAuthorizationFlagExtendRights;
 
-    status = AuthorizationCopyRights(cg->auth_ref, &auth_rights, 
+    status = AuthorizationCopyRights(http->auth_ref, &auth_rights, 
                                     kAuthorizationEmptyEnvironment, 
                                     auth_flags, NULL);
     if (status == errAuthorizationSuccess)
-      status = AuthorizationMakeExternalForm(cg->auth_ref, &auth_extrn);
+      status = AuthorizationMakeExternalForm(http->auth_ref, &auth_extrn);
 
     if (status == errAuthorizationSuccess)
     {
@@ -514,11 +575,7 @@ cups_local_auth(http_t *http)              /* I - HTTP connection to server */
       httpEncode64_2(buffer, sizeof(buffer), (void *)&auth_extrn, 
                     sizeof(auth_extrn));
 
-      http->authstring = malloc(strlen(buffer) + 9);
-      sprintf(http->authstring, "AuthRef %s", buffer);
-
-      /* Copy back to _authstring for backwards compatibility */
-      strlcpy(http->_authstring, http->authstring, sizeof(http->_authstring));
+      httpSetAuthString(http, "AuthRef", buffer);
 
       DEBUG_printf(("cups_local_auth: Returning authstring = \"%s\"\n",
                    http->authstring));
@@ -534,7 +591,7 @@ cups_local_auth(http_t *http)               /* I - HTTP connection to server */
    * Fall through to try certificates...
    */
   }
-#endif /* HAVE_AUTHORIZATION_H */
+#  endif /* HAVE_AUTHORIZATION_H */
 
  /*
   * Try opening a certificate file for this PID.  If that fails,
@@ -548,6 +605,22 @@ cups_local_auth(http_t *http)              /* I - HTTP connection to server */
     DEBUG_printf(("cups_local_auth: Unable to open file %s: %s\n",
                   filename, strerror(errno)));
 
+#ifdef HAVE_GSSAPI
+   /*
+    * If local certificate authentication isn't available for this PID,
+    * check if we need Kerberos authentication...
+    */
+
+    if (!strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9))
+    {
+     /*
+      * Yes, don't try the root certificate...
+      */
+
+      return (1);
+    }
+#endif /* HAVE_GSSAPI */
+
     snprintf(filename, sizeof(filename), "%s/certs/0", cg->cups_statedir);
     fp = fopen(filename, "r");
   }
@@ -565,11 +638,7 @@ cups_local_auth(http_t *http)              /* I - HTTP connection to server */
     * Set the authorization string and return...
     */
 
-    http->authstring = malloc(strlen(certificate) + 10);
-    sprintf(http->authstring, "Local %s", certificate);
-
-    /* Copy back to _authstring for backwards compatibility */
-    strlcpy(http->_authstring, http->authstring, sizeof(http->_authstring));
+    httpSetAuthString(http, "Local", certificate);
 
     DEBUG_printf(("cups_local_auth: Returning authstring = \"%s\"\n",
                  http->authstring));
@@ -577,11 +646,42 @@ 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(("cups_local_auth: Returning authstring = \"%s\"\n",
+                   http->authstring));
+
+      return (0);
+    }
+  }
+#  endif /* SO_PEERCRED && AF_LOCAL */
+
   return (1);
 #endif /* WIN32 || __EMX__ */
 }
 
 
 /*
- * End of "$Id: auth.c 6397 2007-03-25 23:33:32Z mike $".
+ * End of "$Id: auth.c 6779 2007-08-08 19:49:48Z mike $".
  */