/*
- * "$Id: auth.c 7720 2008-07-11 22:46:21Z mike $"
+ * Authentication functions for CUPS.
*
- * Authentication functions for CUPS.
+ * Copyright 2007-2017 by Apple Inc.
+ * Copyright 1997-2007 by Easy Software Products.
*
- * Copyright 2007-2011 by Apple Inc.
- * Copyright 1997-2007 by Easy Software Products.
+ * This file contains Kerberos support code, copyright 2006 by
+ * Jelmer Vernooij.
*
- * This file contains Kerberos support code, copyright 2006 by
- * Jelmer Vernooij.
+ * 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
+ * missing or damaged, see the license at "http://www.cups.org/".
*
- * 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:
- *
- * cupsDoAuthentication() - Authenticate a request.
- * _cupsSetNegotiateAuthString() - Set the Kerberos authentication string.
- * cups_gss_acquire() - Kerberos credentials callback.
- * cups_gss_getname() - Get CUPS service credentials for
- * authentication.
- * cups_gss_printf() - Show debug error messages from GSSAPI.
- * cups_local_auth() - Get the local authorization certificate if
- * available/applicable.
+ * This file is subject to the Apple OS-Developed Software exception.
*/
/*
* Local functions...
*/
+static const char *cups_auth_find(const char *www_authenticate, const char *scheme);
+static const char *cups_auth_param(const char *scheme, const char *name, char *value, size_t valsize);
+static const char *cups_auth_scheme(const char *www_authenticate, char *scheme, size_t schemesize);
+
#ifdef HAVE_GSSAPI
# ifdef HAVE_GSS_ACQUIRE_CRED_EX_F
# ifdef HAVE_GSS_GSSAPI_SPI_H
# include <GSS/gssapi_spi.h>
# else
-typedef struct gss_auth_identity
+# define GSS_AUTH_IDENTITY_TYPE_1 1
+# define gss_acquire_cred_ex_f __ApplePrivate_gss_acquire_cred_ex_f
+typedef struct gss_auth_identity /* @private@ */
{
uint32_t type;
uint32_t flags;
/*
* 'cupsDoAuthentication()' - Authenticate a request.
*
- * This function should be called in response to a @code HTTP_UNAUTHORIZED@
+ * This function should be called in response to a @code HTTP_STATUS_UNAUTHORIZED@
* status, prior to resubmitting your request.
*
- * @since CUPS 1.1.20/Mac OS X 10.4@
+ * @since CUPS 1.1.20/macOS 10.4@
*/
int /* O - 0 on success, -1 on error */
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 */
+ const char *password, /* Password string */
+ *www_auth, /* WWW-Authenticate header */
+ *schemedata; /* Scheme-specific data */
+ char scheme[256], /* Scheme name */
+ prompt[1024], /* Prompt for user */
realm[HTTP_MAX_VALUE], /* realm="xyz" string */
nonce[HTTP_MAX_VALUE]; /* nonce="xyz" string */
int localauth; /* Local authentication result */
_cups_globals_t *cg; /* Global data */
- DEBUG_printf(("cupsDoAuthentication(http=%p, method=\"%s\", resource=\"%s\")",
- http, method, resource));
+ DEBUG_printf(("cupsDoAuthentication(http=%p, method=\"%s\", resource=\"%s\")", (void *)http, method, resource));
if (!http)
http = _cupsConnect();
DEBUG_printf(("2cupsDoAuthentication: authstring=\"%s\"",
http->authstring));
- if (http->status == HTTP_UNAUTHORIZED)
+ if (http->status == HTTP_STATUS_UNAUTHORIZED)
http->digest_tries ++;
return (0);
}
else if (localauth == -1)
{
- http->status = HTTP_AUTHORIZATION_CANCELED;
+ http->status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
return (-1); /* Error or canceled */
}
}
/*
- * Nope, see if we should retry the current username:password...
+ * Nope, loop through the authentication schemes to find the first we support.
*/
- if ((http->digest_tries > 1 || !http->userpass[0]) &&
- (!strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Basic", 5) ||
- !strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Digest", 6)))
+ www_auth = httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE);
+
+ for (schemedata = cups_auth_scheme(www_auth, scheme, sizeof(scheme)); schemedata; schemedata = cups_auth_scheme(schemedata + strlen(scheme), scheme, sizeof(scheme)))
{
/*
- * Nope - get a new password from the user...
+ * Check the scheme name...
*/
- cg = _cupsGlobals();
+#ifdef HAVE_GSSAPI
+ if (!_cups_strcasecmp(scheme, "Negotiate"))
+ {
+ /*
+ * Kerberos authentication...
+ */
- if (!cg->lang_default)
- cg->lang_default = cupsLangDefault();
+ if (_cupsSetNegotiateAuthString(http, method, resource))
+ {
+ http->status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
+ return (-1);
+ }
- snprintf(prompt, sizeof(prompt),
- _cupsLangString(cg->lang_default, _("Password for %s on %s? ")),
- cupsUser(),
- http->hostname[0] == '/' ? "localhost" : http->hostname);
+ break;
+ }
+ else
+#endif /* HAVE_GSSAPI */
+ if (_cups_strcasecmp(scheme, "Basic") && _cups_strcasecmp(scheme, "Digest"))
+ continue; /* Not supported (yet) */
- http->digest_tries = _cups_strncasecmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE],
- "Digest", 5) != 0;
- http->userpass[0] = '\0';
+ /*
+ * See if we should retry the current username:password...
+ */
- if ((password = cupsGetPassword2(prompt, http, method, resource)) == NULL)
+ if ((http->digest_tries > 1 || !http->userpass[0]) && (!_cups_strcasecmp(scheme, "Basic") || (!_cups_strcasecmp(scheme, "Digest"))))
{
- http->status = HTTP_AUTHORIZATION_CANCELED;
- return (-1);
- }
+ /*
+ * Nope - get a new password from the user...
+ */
- snprintf(http->userpass, sizeof(http->userpass), "%s:%s", cupsUser(),
- password);
- }
- else if (http->status == HTTP_UNAUTHORIZED)
- http->digest_tries ++;
+ char default_username[HTTP_MAX_VALUE];
+ /* Default username */
- if (http->status == HTTP_UNAUTHORIZED && http->digest_tries >= 3)
- {
- DEBUG_printf(("1cupsDoAuthentication: Too many authentication tries (%d)",
- http->digest_tries));
+ cg = _cupsGlobals();
- http->status = HTTP_AUTHORIZATION_CANCELED;
- return (-1);
- }
+ if (!cg->lang_default)
+ cg->lang_default = cupsLangDefault();
- /*
- * Got a password; encode it for the server...
- */
+ if (cups_auth_param(scheme, "username", default_username, sizeof(default_username)))
+ cupsSetUser(default_username);
-#ifdef HAVE_GSSAPI
- if (!strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9))
- {
- /*
- * Kerberos authentication...
- */
+ snprintf(prompt, sizeof(prompt), _cupsLangString(cg->lang_default, _("Password for %s on %s? ")), cupsUser(), http->hostname[0] == '/' ? "localhost" : http->hostname);
+
+ http->digest_tries = _cups_strncasecmp(scheme, "Digest", 6) != 0;
+ http->userpass[0] = '\0';
+
+ if ((password = cupsGetPassword2(prompt, http, method, resource)) == NULL)
+ {
+ http->status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
+ return (-1);
+ }
+
+ snprintf(http->userpass, sizeof(http->userpass), "%s:%s", cupsUser(), password);
+ }
+ else if (http->status == HTTP_STATUS_UNAUTHORIZED)
+ http->digest_tries ++;
- if (_cupsSetNegotiateAuthString(http, method, resource))
+ if (http->status == HTTP_STATUS_UNAUTHORIZED && http->digest_tries >= 3)
{
- http->status = HTTP_AUTHORIZATION_CANCELED;
+ DEBUG_printf(("1cupsDoAuthentication: Too many authentication tries (%d)", http->digest_tries));
+
+ http->status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
return (-1);
}
- }
- else
-#endif /* HAVE_GSSAPI */
- if (!strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Basic", 5))
- {
+
/*
- * Basic authentication...
+ * Got a password; encode it for the server...
*/
- char encode[256]; /* Base64 buffer */
+ if (!_cups_strcasecmp(scheme, "Basic"))
+ {
+ /*
+ * Basic authentication...
+ */
+
+ char encode[256]; /* Base64 buffer */
+ httpEncode64_2(encode, sizeof(encode), http->userpass, (int)strlen(http->userpass));
+ httpSetAuthString(http, "Basic", encode);
+ }
+ else if (!_cups_strcasecmp(scheme, "Digest"))
+ {
+ /*
+ * Digest authentication...
+ */
- httpEncode64_2(encode, sizeof(encode), http->userpass,
- (int)strlen(http->userpass));
- httpSetAuthString(http, "Basic", encode);
- }
- else if (!strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Digest", 6))
- {
- /*
- * Digest authentication...
- */
+ int i; /* Looping var */
+ char algorithm[65], /* Hashing algorithm */
+ opaque[HTTP_MAX_VALUE],
+ /* Opaque data from server */
+ cnonce[65], /* cnonce value */
+ kd[65], /* Final MD5/SHA-256 digest */
+ ha1[65], /* Hash of username:realm:password */
+ ha2[65], /* Hash of method:request-uri */
+ hdata[65], /* Hash of auth data */
+ temp[1024], /* Temporary string */
+ digest[1024]; /* Digest auth data */
+ unsigned char hash[32]; /* Hash buffer */
+ const char *hashalg; /* Hashing algorithm */
+ size_t hashsize; /* Size of hash */
+
+ if (strcmp(nonce, http->nonce))
+ {
+ strlcpy(http->nonce, nonce, sizeof(http->nonce));
+ http->nonce_count = 1;
+ }
+ else
+ http->nonce_count ++;
- char encode[33], /* MD5 buffer */
- digest[1024]; /* Digest auth data */
+ cups_auth_param(schemedata, "opaque", opaque, sizeof(opaque));
+ cups_auth_param(schemedata, "nonce", nonce, sizeof(nonce));
+ cups_auth_param(schemedata, "realm", realm, sizeof(realm));
+ for (i = 0; i < 64; i ++)
+ cnonce[i] = "0123456789ABCDEF"[CUPS_RAND() & 15];
+ cnonce[64] = '\0';
- httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE, "realm", realm);
- httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE, "nonce", nonce);
+ if (cups_auth_param(schemedata, "algorithm", algorithm, sizeof(algorithm)))
+ {
+ /*
+ * Follow RFC 2617/7616...
+ */
- httpMD5(cupsUser(), realm, strchr(http->userpass, ':') + 1, encode);
- httpMD5Final(nonce, method, 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);
+ if (!_cups_strcasecmp(algorithm, "MD5"))
+ {
+ /*
+ * RFC 2617 Digest with MD5
+ */
+
+ hashalg = "md5";
+ }
+ else if (!_cups_strcasecmp(algorithm, "SHA-256"))
+ {
+ /*
+ * RFC 7616 Digest with SHA-256
+ */
+
+ hashalg = "sha2-256";
+ }
+ else
+ {
+ /*
+ * Some other algorithm we don't support, skip this one...
+ */
+
+ continue;
+ }
+
+ /*
+ * Calculate digest value...
+ */
+
+ /* H(A1) = H(username:realm:password) */
+ snprintf(temp, sizeof(temp), "%s:%s:%s", cupsUser(), realm, strchr(http->userpass, ':') + 1);
+ hashsize = (size_t)cupsHashData(hashalg, (unsigned char *)temp, strlen(temp), hash, sizeof(hash));
+ cupsHashString(hash, hashsize, ha1, sizeof(ha1));
+
+ /* H(A2) = H(method:uri) */
+ snprintf(temp, sizeof(temp), "%s:%s", method, resource);
+ hashsize = (size_t)cupsHashData(hashalg, (unsigned char *)temp, strlen(temp), hash, sizeof(hash));
+ cupsHashString(hash, hashsize, ha2, sizeof(ha2));
+
+ /* H(data) = H(nonce:nc:cnonce:qop:H(A2)) */
+ snprintf(temp, sizeof(temp), "%s:%08x:%s:auth:%s", nonce, http->nonce_count, cnonce, ha2);
+ hashsize = (size_t)cupsHashData(hashalg, (unsigned char *)temp, strlen(temp), hash, sizeof(hash));
+ cupsHashString(hash, hashsize, hdata, sizeof(hdata));
+
+ /* KD = H(H(A1):H(data)) */
+ snprintf(temp, sizeof(temp), "%s:%s", ha1, hdata);
+ hashsize = (size_t)cupsHashData(hashalg, (unsigned char *)temp, strlen(temp), hash, sizeof(hash));
+ cupsHashString(hash, hashsize, kd, sizeof(kd));
+
+ /* Pass the RFC 2617/7616 WWW-Authenticate header */
+ if (opaque[0])
+ snprintf(digest, sizeof(digest), "username=\"%s\", realm=\"%s\", nonce=\"%s\", algorithm=%s, qop=auth, opaque=\"%s\", cnonce=\"%s\", nc=%08x, uri=\"%s\", response=\"%s\"", cupsUser(), realm, nonce, algorithm, opaque, cnonce, http->nonce_count, resource, kd);
+ else
+ snprintf(digest, sizeof(digest), "username=\"%s\", realm=\"%s\", nonce=\"%s\", algorithm=%s, qop=auth, cnonce=\"%s\", nc=%08x, uri=\"%s\", response=\"%s\"", cupsUser(), realm, nonce, algorithm, cnonce, http->nonce_count, resource, kd);
+ }
+ else
+ {
+ /*
+ * Use old RFC 2069 Digest method...
+ */
+
+ /* H(A1) = H(username:realm:password) */
+ snprintf(temp, sizeof(temp), "%s:%s:%s", cupsUser(), realm, strchr(http->userpass, ':') + 1);
+ hashsize = (size_t)cupsHashData("md5", (unsigned char *)temp, strlen(temp), hash, sizeof(hash));
+ cupsHashString(hash, hashsize, ha1, sizeof(ha1));
+
+ /* H(A2) = H(method:uri) */
+ snprintf(temp, sizeof(temp), "%s:%s", method, resource);
+ hashsize = (size_t)cupsHashData("md5", (unsigned char *)temp, strlen(temp), hash, sizeof(hash));
+ cupsHashString(hash, hashsize, ha2, sizeof(ha2));
+
+ /* KD = H(H(A1):nonce:H(A2)) */
+ snprintf(temp, sizeof(temp), "%s:%s:%s", ha1, nonce, ha2);
+ hashsize = (size_t)cupsHashData("md5", (unsigned char *)temp, strlen(temp), hash, sizeof(hash));
+ cupsHashString(hash, hashsize, kd, sizeof(kd));
+
+ /* Pass the RFC 2069 WWW-Authenticate header */
+ snprintf(digest, sizeof(digest), "username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"", cupsUser(), realm, nonce, resource, kd);
+ }
+
+ httpSetAuthString(http, "Digest", digest);
+ }
+ }
+
+ if (http->authstring)
+ {
+ DEBUG_printf(("1cupsDoAuthentication: authstring=\"%s\"", http->authstring));
+
+ return (0);
}
else
{
- DEBUG_printf(("1cupsDoAuthentication: Unknown auth type: \"%s\"",
- http->fields[HTTP_FIELD_WWW_AUTHENTICATE]));
- http->status = HTTP_AUTHORIZATION_CANCELED;
+ DEBUG_printf(("1cupsDoAuthentication: Unknown auth type: \"%s\"", www_auth));
+ http->status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
+
return (-1);
}
-
- DEBUG_printf(("1cupsDoAuthentication: authstring=\"%s\"", http->authstring));
-
- return (0);
}
* to use it...
*/
- if (gss_init_sec_context == NULL)
+ if (&gss_init_sec_context == NULL)
{
DEBUG_puts("1_cupsSetNegotiateAuthString: Weak-linked GSSAPI/Kerberos "
"framework is not present");
snprintf(prompt, sizeof(prompt),
_cupsLangString(cg->lang_default, _("Password for %s on %s? ")),
- cupsUser(), http->gssname);
+ cupsUser(), http->gsshost);
if ((password = cupsGetPassword2(prompt, http, method, resource)) == NULL)
return (-1);
username = cupsUser();
if (!strchr(username, '@'))
{
- snprintf(userbuf, sizeof(userbuf), "%s@%s", username, http->gssname);
+ snprintf(userbuf, sizeof(userbuf), "%s@%s", username, http->gsshost);
username = userbuf;
}
if (data.sem)
{
- major_status = gss_acquire_cred_ex_f(NULL, GSS_C_NO_NAME, 0,
- GSS_C_INDEFINITE, GSS_KRB5_MECHANISM,
- GSS_C_INITIATE, &identity, &data,
- cups_gss_acquire);
+ major_status = gss_acquire_cred_ex_f(NULL, GSS_C_NO_NAME, 0, GSS_C_INDEFINITE, GSS_KRB5_MECHANISM, GSS_C_INITIATE, (gss_auth_identity_t)&identity, &data, cups_gss_acquire);
if (major_status == GSS_S_COMPLETE)
{
}
}
}
- else
#endif /* HAVE_GSS_ACQUIRED_CRED_EX_F */
if (GSS_ERROR(major_status))
* arbitrarily large credentials...
*/
- int authsize = 10 + /* "Negotiate " */
- output_token.length * 4 / 3 + 1 + /* Base64 */
- 1; /* nul */
+ int authsize = 10 + /* "Negotiate " */
+ (int)output_token.length * 4 / 3 + 1 + 1;
+ /* Base64 + nul */
httpSetAuthString(http, NULL, NULL);
- if ((http->authstring = malloc(authsize)) == NULL)
+ if ((http->authstring = malloc((size_t)authsize)) == NULL)
{
http->authstring = http->_authstring;
authsize = sizeof(http->_authstring);
}
- strcpy(http->authstring, "Negotiate ");
+ strlcpy(http->authstring, "Negotiate ", (size_t)authsize);
httpEncode64_2(http->authstring + 10, authsize - 10, output_token.value,
- output_token.length);
+ (int)output_token.length);
gss_release_buffer(&minor_status, &output_token);
}
}
+/*
+ * 'cups_auth_find()' - Find the named WWW-Authenticate scheme.
+ *
+ * The "www_authenticate" parameter points to the current position in the header.
+ *
+ * Returns @code NULL@ if the auth scheme is not present.
+ */
+
+static const char * /* O - Start of matching scheme or @code NULL@ if not found */
+cups_auth_find(const char *www_authenticate, /* I - Pointer into WWW-Authenticate header */
+ const char *scheme) /* I - Authentication scheme */
+{
+ size_t schemelen = strlen(scheme); /* Length of scheme */
+
+
+ while (*www_authenticate)
+ {
+ /*
+ * Skip leading whitespace and commas...
+ */
+
+ while (isspace(*www_authenticate & 255) || *www_authenticate == ',')
+ www_authenticate ++;
+
+ /*
+ * See if this is "Scheme" followed by whitespace or the end of the string.
+ */
+
+ if (!strncmp(www_authenticate, scheme, schemelen) && (isspace(www_authenticate[schemelen] & 255) || !www_authenticate[schemelen]))
+ {
+ /*
+ * Yes, this is the start of the scheme-specific information...
+ */
+
+ return (www_authenticate);
+ }
+
+ /*
+ * Skip the scheme name or param="value" string...
+ */
+
+ while (!isspace(*www_authenticate & 255) && *www_authenticate)
+ {
+ if (*www_authenticate == '\"')
+ {
+ /*
+ * Skip quoted value...
+ */
+
+ while (www_authenticate[1] && www_authenticate[1] != '\"')
+ www_authenticate ++;
+ }
+
+ www_authenticate ++;
+ }
+ }
+
+ return (NULL);
+}
+
+
+/*
+ * 'cups_auth_param()' - Copy the value for the named authentication parameter,
+ * if present.
+ */
+
+static const char * /* O - Parameter value or @code NULL@ if not present */
+cups_auth_param(const char *scheme, /* I - Pointer to auth data */
+ const char *name, /* I - Name of parameter */
+ char *value, /* I - Value buffer */
+ size_t valsize) /* I - Size of value buffer */
+{
+ char *valptr = value, /* Pointer into value buffer */
+ *valend = value + valsize - 1; /* Pointer to end of buffer */
+ size_t namelen = strlen(name); /* Name length */
+ int param; /* Is this a parameter? */
+
+
+ while (!isspace(*scheme & 255) && *scheme)
+ scheme ++;
+
+ while (*scheme)
+ {
+ while (isspace(*scheme & 255) || *scheme == ',')
+ scheme ++;
+
+ if (!strncmp(scheme, name, namelen) && scheme[namelen] == '=')
+ {
+ /*
+ * Found the parameter, copy the value...
+ */
+
+ scheme += namelen + 1;
+ if (*scheme == '\"')
+ {
+ scheme ++;
+
+ while (*scheme && *scheme != '\"')
+ {
+ if (valptr < valend)
+ *valptr++ = *scheme;
+
+ scheme ++;
+ }
+ }
+ else
+ {
+ while (*scheme && strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~+/=", *scheme))
+ {
+ if (valptr < valend)
+ *valptr++ = *scheme;
+
+ scheme ++;
+ }
+ }
+
+ *valptr = '\0';
+
+ return (value);
+ }
+
+ /*
+ * Skip the param=value string...
+ */
+
+ param = 0;
+
+ while (!isspace(*scheme & 255) && *scheme)
+ {
+ if (*scheme == '=')
+ param = 1;
+ else if (*scheme == '\"')
+ {
+ /*
+ * Skip quoted value...
+ */
+
+ while (scheme[1] && scheme[1] != '\"')
+ scheme ++;
+ }
+
+ scheme ++;
+ }
+
+ /*
+ * If this wasn't a parameter, we are at the end of this scheme's
+ * parameters...
+ */
+
+ if (!param)
+ break;
+ }
+
+ *value = '\0';
+
+ return (NULL);
+}
+
+
+/*
+ * 'cups_auth_scheme()' - Get the (next) WWW-Authenticate scheme.
+ *
+ * The "www_authenticate" parameter points to the current position in the header.
+ *
+ * Returns @code NULL@ if there are no (more) auth schemes present.
+ */
+
+static const char * /* O - Start of scheme or @code NULL@ if not found */
+cups_auth_scheme(const char *www_authenticate, /* I - Pointer into WWW-Authenticate header */
+ char *scheme, /* I - Scheme name buffer */
+ size_t schemesize) /* I - Size of buffer */
+{
+ const char *start; /* Start of scheme data */
+ char *sptr = scheme, /* Pointer into scheme buffer */
+ *send = scheme + schemesize - 1;/* End of scheme buffer */
+ int param; /* Is this a parameter? */
+
+
+ while (*www_authenticate)
+ {
+ /*
+ * Skip leading whitespace and commas...
+ */
+
+ while (isspace(*www_authenticate & 255) || *www_authenticate == ',')
+ www_authenticate ++;
+
+ /*
+ * Parse the scheme name or param="value" string...
+ */
+
+ for (sptr = scheme, start = www_authenticate, param = 0; *www_authenticate && !isspace(*www_authenticate & 255); www_authenticate ++)
+ {
+ if (*www_authenticate == '=')
+ param = 1;
+ else if (!param && sptr < send)
+ *sptr++ = *www_authenticate;
+ else if (*www_authenticate == '\"')
+ {
+ /*
+ * Skip quoted value...
+ */
+
+ while (www_authenticate[1] && www_authenticate[1] != '\"')
+ www_authenticate ++;
+ }
+ }
+
+ if (sptr > scheme && !param)
+ {
+ *sptr = '\0';
+ return (start);
+ }
+ }
+
+ *scheme = '\0';
+
+ return (NULL);
+}
+
+
# ifdef HAVE_GSS_ACQUIRE_CRED_EX_F
/*
* 'cups_gss_acquire()' - Kerberos credentials callback.
int pid; /* Current process ID */
FILE *fp; /* Certificate file */
char trc[16], /* Try Root Certificate parameter */
- filename[1024], /* Certificate filename */
- certificate[33];/* Certificate string */
+ filename[1024]; /* Certificate filename */
+ const char *www_auth, /* WWW-Authenticate header */
+ *schemedata; /* Data for the named auth scheme */
_cups_globals_t *cg = _cupsGlobals(); /* Global data */
# if defined(HAVE_AUTHORIZATION_H)
OSStatus status; /* Status */
# endif /* HAVE_AUTHORIZATION_H */
- DEBUG_printf(("7cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"",
- http, httpAddrString(http->hostaddr, filename, sizeof(filename)), http->hostname));
+ DEBUG_printf(("7cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"", (void *)http, httpAddrString(http->hostaddr, filename, sizeof(filename)), http->hostname));
/*
* See if we are accessing localhost...
*/
- if (!httpAddrLocalhost(http->hostaddr) &&
- _cups_strcasecmp(http->hostname, "localhost") != 0)
+ if (!httpAddrLocalhost(http->hostaddr) && _cups_strcasecmp(http->hostname, "localhost") != 0)
{
DEBUG_puts("8cups_local_auth: Not a local connection!");
return (1);
http->auth_ref = NULL;
}
- if (!getenv("GATEWAY_INTERFACE") &&
- httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "authkey",
- auth_key, sizeof(auth_key)))
+ www_auth = httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE);
+
+ if (!getenv("GATEWAY_INTERFACE") && (schemedata = cups_auth_find(www_auth, "AuthKey")) != NULL && cups_auth_param(schemedata, "key", auth_key, sizeof(auth_key)))
{
- status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
- kAuthorizationFlagDefaults, &http->auth_ref);
+ status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &http->auth_ref);
if (status != errAuthorizationSuccess)
{
DEBUG_printf(("8cups_local_auth: AuthorizationCreate() returned %d (%s)",
}
# endif /* HAVE_AUTHORIZATION_H */
+# ifdef HAVE_GSSAPI
+ if (cups_auth_find(www_auth, "Negotiate"))
+ return (1);
+# endif /* HAVE_GSSAPI */
+# ifdef HAVE_AUTHORIZATION_H
+ if (cups_auth_find(www_auth, "AuthKey"))
+ return (1);
+# endif /* HAVE_AUTHORIZATION_H */
+
# if defined(SO_PEERCRED) && defined(AF_LOCAL)
/*
* See if we can authenticate using the peer credentials provided over a
* information...
*/
- if (
-# ifdef HAVE_GSSAPI
- strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9) &&
-# endif /* HAVE_GSSAPI */
-# ifdef HAVE_AUTHORIZATION_H
- !httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "authkey",
- auth_key, sizeof(auth_key)) &&
-# endif /* HAVE_AUTHORIZATION_H */
- http->hostaddr->addr.sa_family == AF_LOCAL &&
- !getenv("GATEWAY_INTERFACE")) /* Not via CGI programs... */
+ if (http->hostaddr->addr.sa_family == AF_LOCAL &&
+ !getenv("GATEWAY_INTERFACE") && /* Not via CGI programs... */
+ cups_auth_find(www_auth, "PeerCred"))
{
/*
* Verify that the current cupsUser() matches the current UID...
}
# endif /* SO_PEERCRED && AF_LOCAL */
+ if ((schemedata = cups_auth_find(www_auth, "Local")) == NULL)
+ return (1);
+
/*
* Try opening a certificate file for this PID. If that fails,
* try the root certificate...
* 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)));
+ 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))
- {
- /*
- * Kerberos required, don't try the root certificate...
- */
-
- return (1);
- }
-# endif /* HAVE_GSSAPI */
-
-# ifdef HAVE_AUTHORIZATION_H
- if (httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "authkey",
- auth_key, sizeof(auth_key)))
- {
- /*
- * Don't use the root certificate as a replacement for an authkey...
- */
-
- return (1);
- }
-# endif /* HAVE_AUTHORIZATION_H */
- if (!httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "trc", trc,
- sizeof(trc)))
+ if (!cups_auth_param(schemedata, "trc", trc, sizeof(trc)))
{
/*
* Scheduler doesn't want us to use the root certificate...
* Read the certificate from the file...
*/
- fgets(certificate, sizeof(certificate), fp);
+ char certificate[33], /* Certificate string */
+ *certptr; /* Pointer to certificate string */
+
+ certptr = fgets(certificate, sizeof(certificate), fp);
fclose(fp);
- /*
- * Set the authorization string and return...
- */
+ if (certptr)
+ {
+ /*
+ * Set the authorization string and return...
+ */
- httpSetAuthString(http, "Local", certificate);
+ httpSetAuthString(http, "Local", certificate);
- DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
- http->authstring));
+ DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
+ http->authstring));
- return (0);
+ return (0);
+ }
}
return (1);
#endif /* WIN32 || __EMX__ */
}
-
-
-/*
- * End of "$Id: auth.c 7720 2008-07-11 22:46:21Z mike $".
- */