]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cups/auth.c
Merge changes from CUPS 1.4svn-r8606.
[thirdparty/cups.git] / cups / auth.c
index d3d38b47ed5a64d8cfaba5e82ad34f399b243138..ca877a398f577a2cec400038c2122816b589aae6 100644 (file)
@@ -1,9 +1,9 @@
 /*
- * "$Id: auth.c 6779 2007-08-08 19:49:48Z mike $"
+ * "$Id: auth.c 7720 2008-07-11 22:46:21Z mike $"
  *
  *   Authentication functions for the Common UNIX Printing System (CUPS).
  *
- *   Copyright 2007-2008 by Apple Inc.
+ *   Copyright 2007-2009 by Apple Inc.
  *   Copyright 1997-2007 by Easy Software Products.
  *
  *   This file contains Kerberos support code, copyright 2006 by
@@ -20,8 +20,8 @@
  * Contents:
  *
  *   cupsDoAuthentication() - Authenticate a request.
- *   DEBUG_gss_printf()     - Show debug error messages from GSSAPI...
- *   cups_get_gss_creds()   - Get CUPS service credentials for authentication.
+ *   cups_get_gssname()     - Get GSSAPI name for authentication.
+ *   cups_gss_printf()      - Show error messages from GSSAPI...
  *   cups_local_auth()      - Get the local authorization certificate if
  *                            available/applicable...
  */
@@ -62,13 +62,13 @@ extern const char *cssmErrorString(int error);
  */
 
 #ifdef HAVE_GSSAPI
+static gss_name_t cups_get_gssname(http_t *http, const char *service_name);
 #  ifdef DEBUG
-static void    DEBUG_gss_printf(OM_uint32 major_status, OM_uint32 minor_status,
-                                const char *message);
+static void    cups_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);
+#    define    cups_gss_printf(major, minor, message)
+#  endif /* DEBUG */
 #endif /* HAVE_GSSAPI */
 static int     cups_local_auth(http_t *http);
 
@@ -79,14 +79,14 @@ static int  cups_local_auth(http_t *http);
  * This function should be called in response to a @code HTTP_UNAUTHORIZED@
  * status, prior to resubmitting your request.
  *
- * @since CUPS 1.1.20@
+ * @since CUPS 1.1.20/Mac OS X 10.4@
  */
 
 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 */
@@ -96,13 +96,19 @@ cupsDoAuthentication(http_t     *http,      /* I - Connection to server or @code CUPS
   _cups_globals_t *cg;                 /* Global data */
 
 
-  DEBUG_printf(("cupsDoAuthentication(http=%p, method=\"%s\", resource=\"%s\")\n",
+  DEBUG_printf(("cupsDoAuthentication(http=%p, method=\"%s\", resource=\"%s\")",
                 http, method, resource));
-  DEBUG_printf(("cupsDoAuthentication: digest_tries=%d, userpass=\"%s\"\n",
+  DEBUG_printf(("2cupsDoAuthentication: digest_tries=%d, userpass=\"%s\"",
                 http->digest_tries, http->userpass));
-  DEBUG_printf(("cupsDoAuthentication: WWW-Authenticate=\"%s\"\n",
+  DEBUG_printf(("2cupsDoAuthentication: WWW-Authenticate=\"%s\"",
                 httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE)));
 
+  if (!http)
+    http = _cupsConnect();
+
+  if (!http || !method || !resource)
+    return (-1);
+
  /*
   * Clear the current authentication string...
   */
@@ -117,7 +123,7 @@ cupsDoAuthentication(http_t     *http,      /* I - Connection to server or @code CUPS
   {
     if ((localauth = cups_local_auth(http)) == 0)
     {
-      DEBUG_printf(("cupsDoAuthentication: authstring=\"%s\"\n",
+      DEBUG_printf(("2cupsDoAuthentication: authstring=\"%s\"",
                     http->authstring));
   
       if (http->status == HTTP_UNAUTHORIZED)
@@ -126,7 +132,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 +163,17 @@ cupsDoAuthentication(http_t     *http,    /* I - Connection to server or @code CUPS
                                       "Digest", 5) != 0;
     http->userpass[0]   = '\0';
 
-    if ((password = cupsGetPassword(prompt)) == NULL)
+    if ((password = cupsGetPassword2(prompt, http, method, resource)) == NULL)
+    {
+      http->status = HTTP_AUTHORIZATION_CANCELED;
       return (-1);
+    }
 
     if (!password[0])
+    {
+      http->status = HTTP_AUTHORIZATION_CANCELED;
       return (-1);
+    }
 
     snprintf(http->userpass, sizeof(http->userpass), "%s:%s", cupsUser(),
              password);
@@ -199,16 +214,19 @@ cupsDoAuthentication(http_t     *http,    /* I - Connection to server or @code CUPS
 
     if (gss_init_sec_context == NULL)
     {
-      DEBUG_puts("cupsDoAuthentication: Weak-linked GSSAPI/Kerberos framework "
+      DEBUG_puts("1cupsDoAuthentication: Weak-linked GSSAPI/Kerberos framework "
                  "is not present");
+      http->status = HTTP_AUTHORIZATION_CANCELED;
+
       return (-1);
     }
 #  endif /* __APPLE__ */
 
     if (http->status == HTTP_UNAUTHORIZED && http->digest_tries >= 3)
     {
-      DEBUG_printf(("cupsDoAuthentication: too many Negotiate tries (%d)\n",
+      DEBUG_printf(("1cupsDoAuthentication: too many Negotiate tries (%d)",
                     http->digest_tries));
+      http->status = HTTP_AUTHORIZATION_CANCELED;
   
       return (-1);
     }
@@ -218,9 +236,10 @@ cupsDoAuthentication(http_t     *http,     /* I - Connection to server or @code CUPS
       if ((gss_service_name = getenv("CUPS_GSSSERVICENAME")) == NULL)
        gss_service_name = CUPS_DEFAULT_GSSSERVICENAME;
       else
-       DEBUG_puts("cupsDoAuthentication: GSS service name set via environment");
+       DEBUG_puts("2cupsDoAuthentication: GSS service name set via "
+                  "environment variable");
 
-      http->gssname = cups_get_gss_creds(http, gss_service_name);
+      http->gssname = cups_get_gssname(http, gss_service_name);
     }
 
 #  ifdef USE_SPNEGO /* We don't implement SPNEGO just yet... */
@@ -270,16 +289,17 @@ cupsDoAuthentication(http_t     *http,    /* I - Connection to server or @code CUPS
 
     if (http->gssctx != GSS_C_NO_CONTEXT)
     {
-      major_status = gss_delete_sec_context(&minor_status, &http->gssctx,
-                                           GSS_C_NO_BUFFER);
+      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_DELEG_FLAG | GSS_C_MUTUAL_FLAG |
-                                            GSS_C_INTEG_FLAG,
+#ifdef GSS_C_DELEG_POLICY_FLAG
+                                        GSS_C_DELEG_POLICY_FLAG |
+#endif /* GSS_C_DELEG_POLICY_FLAG */
+                                        GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG,
                                         GSS_C_INDEFINITE,
                                         GSS_C_NO_CHANNEL_BINDINGS,
                                         &input_token, &http->gssmech,
@@ -290,15 +310,17 @@ cupsDoAuthentication(http_t     *http,    /* I - Connection to server or @code CUPS
 
     if (GSS_ERROR(major_status))
     {
-#  ifdef DEBUG
-      DEBUG_gss_printf(major_status, minor_status,
-                      "Unable to initialize security context");
-#  endif /* DEBUG */
+      cups_gss_printf(major_status, minor_status,
+                     "cupsDoAuthentication: Unable to initialize security "
+                     "context");
+      http->status = HTTP_AUTHORIZATION_CANCELED;
+
       return (-1);
     }
 
     if (major_status == GSS_S_CONTINUE_NEEDED)
-      DEBUG_gss_printf(major_status, minor_status, "Continuation needed!");
+      cups_gss_printf(major_status, minor_status,
+                     "cupsDoAuthentication: Continuation needed!");
 
     if (output_token.length > 0 && output_token.length <= 65536)
     {
@@ -323,14 +345,14 @@ cupsDoAuthentication(http_t     *http,    /* I - Connection to server or @code CUPS
       httpEncode64_2(http->authstring + 10, authsize - 10, output_token.value,
                     output_token.length);
  
-      major_status = gss_release_buffer(&minor_status, &output_token);
+      gss_release_buffer(&minor_status, &output_token);
     }
     else
     {
-      DEBUG_printf(("cupsDoAuthentication: Kerberos credentials too large - "
-                    "%d bytes!\n", (int)output_token.length));
-
-      major_status = gss_release_buffer(&minor_status, &output_token);
+      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);
     }
@@ -370,63 +392,19 @@ cupsDoAuthentication(http_t     *http,    /* I - Connection to server or @code CUPS
     httpSetAuthString(http, "Digest", digest);
   }
 
-  DEBUG_printf(("cupsDoAuthentication: authstring=\"%s\"\n", http->authstring));
+  DEBUG_printf(("1cupsDoAuthentication: authstring=\"%s\"", http->authstring));
 
   return (0);
 }
 
 
 #ifdef HAVE_GSSAPI
-#  ifdef DEBUG
-/*
- * 'DEBUG_gss_printf()' - Show debug error messages from GSSAPI...
- */
-
-static void
-DEBUG_gss_printf(OM_uint32 major_status,/* I - Major status code */
-                 OM_uint32 minor_status,/* I - Minor status code */
-                const char *message)   /* I - Prefix for error message */
-{
-  OM_uint32    err_major_status,       /* Major status code for display */
-               err_minor_status;       /* Minor status code for display */
-  OM_uint32    msg_ctx;                /* Message context */
-  gss_buffer_desc major_status_string = GSS_C_EMPTY_BUFFER,
-                                       /* Major status message */
-               minor_status_string = GSS_C_EMPTY_BUFFER;
-                                       /* Minor status message */
-
-
-  msg_ctx          = 0;
-  err_major_status = gss_display_status(&err_minor_status,
-                                       major_status,
-                                       GSS_C_GSS_CODE,
-                                       GSS_C_NO_OID,
-                                       &msg_ctx,
-                                       &major_status_string);
-
-  if (!GSS_ERROR(err_major_status))
-    err_major_status = gss_display_status(&err_minor_status,
-                                         minor_status,
-                                         GSS_C_MECH_CODE,
-                                         GSS_C_NULL_OID,
-                                         &msg_ctx,
-                                         &minor_status_string);
-
-  printf("%s: %s, %s\n", message, (char *)major_status_string.value,
-        (char *)minor_status_string.value);
-
-  gss_release_buffer(&err_minor_status, &major_status_string);
-  gss_release_buffer(&err_minor_status, &minor_status_string);
-}
-#  endif /* DEBUG */
-
-
 /*
- * 'cups_get_gss_creds()' - Get CUPS service credentials for authentication.
+ * 'cups_get_gssname()' - Get CUPS service credentials for authentication.
  */
 
 static gss_name_t                      /* O - Server name */
-cups_get_gss_creds(
+cups_get_gssname(
     http_t     *http,                  /* I - Connection to server */
     const char *service_name)          /* I - Service name */
 {
@@ -439,6 +417,10 @@ cups_get_gss_creds(
                  fqdn[HTTP_MAX_URI];   /* Server name buffer */
 
 
+  DEBUG_printf(("7cups_get_gssname(http=%p, service_name=\"%s\")", http,
+                service_name));
+
+
  /*
   * Get the hostname...
   */
@@ -454,7 +436,7 @@ cups_get_gss_creds(
 
   snprintf(buf, sizeof(buf), "%s@%s", service_name, fqdn);
 
-  DEBUG_printf(("cups_get_gss_creds: Looking up %s...\n", buf));
+  DEBUG_printf(("9cups_get_gssname: Looking up %s...", buf));
 
   token.value  = buf;
   token.length = strlen(buf);
@@ -465,15 +447,53 @@ cups_get_gss_creds(
 
   if (GSS_ERROR(major_status))
   {
-#  ifdef DEBUG
-    DEBUG_gss_printf(major_status, minor_status, "gss_import_name() failed");
-#  endif /* DEBUG */
-
+    cups_gss_printf(major_status, minor_status,
+                    "cups_get_gssname: gss_import_name() failed");
     return (NULL);
   }
 
   return (server_name);
 }
+
+
+#  ifdef DEBUG
+/*
+ * 'cups_gss_printf()' - Show debug error messages from GSSAPI...
+ */
+
+static void
+cups_gss_printf(OM_uint32  major_status,/* I - Major status code */
+               OM_uint32  minor_status,/* I - Minor status code */
+               const char *message)    /* I - Prefix for error message */
+{
+  OM_uint32    err_major_status,       /* Major status code for display */
+               err_minor_status;       /* Minor status code for display */
+  OM_uint32    msg_ctx;                /* Message context */
+  gss_buffer_desc major_status_string = GSS_C_EMPTY_BUFFER,
+                                       /* Major status message */
+               minor_status_string = GSS_C_EMPTY_BUFFER;
+                                       /* Minor status message */
+
+
+  msg_ctx          = 0;
+  err_major_status = gss_display_status(&err_minor_status,
+                                       major_status,
+                                       GSS_C_GSS_CODE,
+                                       GSS_C_NO_OID,
+                                       &msg_ctx,
+                                       &major_status_string);
+
+  if (!GSS_ERROR(err_major_status))
+    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,
+               (char *)minor_status_string.value));
+
+  gss_release_buffer(&err_minor_status, &major_status_string);
+  gss_release_buffer(&err_minor_status, &minor_status_string);
+}
+#  endif /* DEBUG */
 #endif /* HAVE_GSSAPI */
 
 
@@ -496,7 +516,8 @@ cups_local_auth(http_t *http)               /* I - HTTP connection to server */
 #else
   int                  pid;            /* Current process ID */
   FILE                 *fp;            /* Certificate file */
-  char                 filename[1024], /* Certificate filename */
+  char                 trc[16],        /* Try Root Certificate parameter */
+                       filename[1024], /* Certificate filename */
                        certificate[33];/* Certificate string */
   _cups_globals_t *cg = _cupsGlobals();        /* Global data */
 #  if defined(HAVE_AUTHORIZATION_H)
@@ -510,7 +531,7 @@ cups_local_auth(http_t *http)               /* I - HTTP connection to server */
 #  endif /* HAVE_AUTHORIZATION_H */
 
 
-  DEBUG_printf(("cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"\n",
+  DEBUG_printf(("7cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"",
                 http, httpAddrString(http->hostaddr, filename, sizeof(filename)), http->hostname));
 
  /*
@@ -520,7 +541,7 @@ cups_local_auth(http_t *http)               /* I - HTTP connection to server */
   if (!httpAddrLocalhost(http->hostaddr) &&
       strcasecmp(http->hostname, "localhost") != 0)
   {
-    DEBUG_puts("cups_local_auth: Not a local connection!");
+    DEBUG_puts("8cups_local_auth: Not a local connection!");
     return (1);
   }
 
@@ -535,14 +556,15 @@ cups_local_auth(http_t *http)             /* I - HTTP connection to server */
     http->auth_ref = NULL;
   }
 
-  if (httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "authkey", 
+  if (!getenv("GATEWAY_INTERFACE") &&
+      httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "authkey", 
                       auth_key, sizeof(auth_key)))
   {
     status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, 
                                 kAuthorizationFlagDefaults, &http->auth_ref);
     if (status != errAuthorizationSuccess)
     {
-      DEBUG_printf(("cups_local_auth: AuthorizationCreate() returned %d (%s)\n",
+      DEBUG_printf(("8cups_local_auth: AuthorizationCreate() returned %d (%s)",
                    (int)status, cssmErrorString(status)));
       return (-1);
     }
@@ -577,14 +599,14 @@ cups_local_auth(http_t *http)             /* I - HTTP connection to server */
 
       httpSetAuthString(http, "AuthRef", buffer);
 
-      DEBUG_printf(("cups_local_auth: Returning authstring = \"%s\"\n",
+      DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
                    http->authstring));
       return (0);
     }
     else if (status == errAuthorizationCanceled)
       return (-1);
 
-    DEBUG_printf(("cups_local_auth: AuthorizationCopyRights() returned %d (%s)\n",
+    DEBUG_printf(("9cups_local_auth: AuthorizationCopyRights() returned %d (%s)",
                  (int)status, cssmErrorString(status)));
 
   /*
@@ -593,6 +615,37 @@ 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...
+  */
+
+  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 */
+
  /*
   * Try opening a certificate file for this PID.  If that fails,
   * try the root certificate...
@@ -602,25 +655,34 @@ cups_local_auth(http_t *http)             /* I - HTTP connection to server */
   snprintf(filename, sizeof(filename), "%s/certs/%d", cg->cups_statedir, pid);
   if ((fp = fopen(filename, "r")) == NULL && pid > 0)
   {
-    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...
+    * No certificate for this PID; see if we can get the root certificate...
     */
 
+    DEBUG_printf(("9cups_local_auth: Unable to open file %s: %s",
+                  filename, strerror(errno)));
+
+#ifdef HAVE_GSSAPI
     if (!strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9))
     {
      /*
-      * Yes, don't try the root certificate...
+      * Kerberos required, don't try the root certificate...
       */
 
       return (1);
     }
 #endif /* HAVE_GSSAPI */
 
+    if (!httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "trc", trc,
+                         sizeof(trc)))
+    {
+     /*
+      * Scheduler doesn't want us to use the root certificate...
+      */
+
+      return (1);
+    }
+
     snprintf(filename, sizeof(filename), "%s/certs/0", cg->cups_statedir);
     fp = fopen(filename, "r");
   }
@@ -640,48 +702,17 @@ cups_local_auth(http_t *http)             /* I - HTTP connection to server */
 
     httpSetAuthString(http, "Local", certificate);
 
-    DEBUG_printf(("cups_local_auth: Returning authstring = \"%s\"\n",
+    DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
                  http->authstring));
 
     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 6779 2007-08-08 19:49:48Z mike $".
+ * End of "$Id: auth.c 7720 2008-07-11 22:46:21Z mike $".
  */