/*
* "$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__)
*/
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 */
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\"",
return (0);
}
else if (localauth == -1)
+ {
+ http->status = HTTP_AUTHORIZATION_CANCELED;
return (-1); /* Error or canceled */
+ }
}
/*
"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);
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...
*/
{
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)
{
authorization = httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE);
authorization += 9;
- while (*authorization && isspace(*authorization & 255))
+ while (*authorization && _cups_isspace(*authorization))
authorization ++;
if (*authorization)
cups_gss_printf(major_status, minor_status,
"cupsDoAuthentication: Unable to initialize security "
"context");
+ http->status = HTTP_AUTHORIZATION_CANCELED;
+
return (-1);
}
{
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);
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);
}
# 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...
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__ */
}