#include "globals.h"
#include "debug.h"
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#if defined(WIN32) || defined(__EMX__)
# include <io.h>
#else
# include <unistd.h>
#endif
#if HAVE_AUTHORIZATION_H
# include <Security/Authorization.h>
# ifdef HAVE_SECBASEPRIV_H
# include <Security/SecBasePriv.h>
# else
extern const char *cssmErrorString(int error);
# endif
#endif
#if defined(SO_PEERCRED) && defined(AF_LOCAL)
# include <pwd.h>
#endif
#ifdef HAVE_GSSAPI
static gss_name_t cups_get_gssname(http_t *http, const char *service_name);
# ifdef DEBUG
static void cups_gss_printf(OM_uint32 major_status, OM_uint32 minor_status,
const char *message);
# else
# define cups_gss_printf(major, minor, message)
# endif
#endif
static int cups_local_auth(http_t *http);
int
cupsDoAuthentication(
http_t *http,
const char *method,
const char *resource)
{
const char *password;
char prompt[1024],
realm[HTTP_MAX_VALUE],
nonce[HTTP_MAX_VALUE];
int localauth;
_cups_globals_t *cg;
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\"",
httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE)));
httpSetAuthString(http, NULL, NULL);
if (http->digest_tries < 3)
{
if ((localauth = cups_local_auth(http)) == 0)
{
DEBUG_printf(("2cupsDoAuthentication: authstring=\"%s\"",
http->authstring));
if (http->status == HTTP_UNAUTHORIZED)
http->digest_tries ++;
return (0);
}
else if (localauth == -1)
{
http->status = HTTP_AUTHORIZATION_CANCELED;
return (-1);
}
}
if ((http->digest_tries > 1 || !http->userpass[0]) &&
strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9))
{
cg = _cupsGlobals();
if (!cg->lang_default)
cg->lang_default = cupsLangDefault();
snprintf(prompt, sizeof(prompt),
_cupsLangString(cg->lang_default, _("Password for %s on %s? ")),
cupsUser(),
http->hostname[0] == '/' ? "localhost" : http->hostname);
http->digest_tries = strncasecmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE],
"Digest", 5) != 0;
http->userpass[0] = '\0';
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);
}
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);
}
if (!strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9))
{
#ifdef HAVE_GSSAPI
OM_uint32 minor_status,
major_status;
gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER,
input_token = GSS_C_EMPTY_BUFFER;
char *gss_service_name;
# ifdef USE_SPNEGO
const char *authorization;
# endif
# ifdef __APPLE__
if (gss_init_sec_context == NULL)
{
DEBUG_puts("1cupsDoAuthentication: Weak-linked GSSAPI/Kerberos framework "
"is not present");
http->status = HTTP_AUTHORIZATION_CANCELED;
return (-1);
}
# endif
if (http->gssname == GSS_C_NO_NAME)
{
if ((gss_service_name = getenv("CUPS_GSSSERVICENAME")) == NULL)
gss_service_name = CUPS_DEFAULT_GSSSERVICENAME;
else
DEBUG_puts("2cupsDoAuthentication: GSS service name set via "
"environment variable");
http->gssname = cups_get_gssname(http, gss_service_name);
}
# ifdef USE_SPNEGO
authorization = httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE);
authorization += 9;
while (*authorization && _cups_isspace(*authorization))
authorization ++;
if (*authorization)
{
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
{
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
}
# endif
if (http->gssctx != GSS_C_NO_CONTEXT)
{
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,
#ifdef GSS_C_DELEG_POLICY_FLAG
GSS_C_DELEG_POLICY_FLAG |
#endif
GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG,
GSS_C_INDEFINITE,
GSS_C_NO_CHANNEL_BINDINGS,
&input_token, &http->gssmech,
&output_token, NULL, NULL);
if (input_token.value)
free(input_token.value);
if (GSS_ERROR(major_status))
{
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)
cups_gss_printf(major_status, minor_status,
"cupsDoAuthentication: Continuation needed!");
if (output_token.length > 0 && output_token.length <= 65536)
{
int authsize = 10 +
output_token.length * 4 / 3 + 1 +
1;
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);
gss_release_buffer(&minor_status, &output_token);
}
else
{
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);
}
#endif
}
else if (strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Digest", 6))
{
char encode[256];
httpEncode64_2(encode, sizeof(encode), http->userpass,
(int)strlen(http->userpass));
httpSetAuthString(http, "Basic", encode);
}
else
{
char encode[33],
digest[1024];
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(digest, sizeof(digest),
"username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", "
"response=\"%s\"", cupsUser(), realm, nonce, resource, encode);
httpSetAuthString(http, "Digest", digest);
}
DEBUG_printf(("1cupsDoAuthentication: authstring=\"%s\"", http->authstring));
return (0);
}
#ifdef HAVE_GSSAPI
static gss_name_t
cups_get_gssname(
http_t *http,
const char *service_name)
{
gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
OM_uint32 major_status,
minor_status;
gss_name_t server_name;
char buf[1024],
fqdn[HTTP_MAX_URI];
DEBUG_printf(("7cups_get_gssname(http=%p, service_name=\"%s\")", http,
service_name));
httpGetHostname(http, fqdn, sizeof(fqdn));
if (!strcmp(fqdn, "localhost"))
httpGetHostname(NULL, fqdn, sizeof(fqdn));
snprintf(buf, sizeof(buf), "%s@%s", service_name, fqdn);
DEBUG_printf(("9cups_get_gssname: Looking up %s...", buf));
token.value = buf;
token.length = strlen(buf);
server_name = GSS_C_NO_NAME;
major_status = gss_import_name(&minor_status, &token,
GSS_C_NT_HOSTBASED_SERVICE,
&server_name);
if (GSS_ERROR(major_status))
{
cups_gss_printf(major_status, minor_status,
"cups_get_gssname: gss_import_name() failed");
return (NULL);
}
return (server_name);
}
# ifdef DEBUG
static void
cups_gss_printf(OM_uint32 major_status,
OM_uint32 minor_status,
const char *message)
{
OM_uint32 err_major_status,
err_minor_status;
OM_uint32 msg_ctx;
gss_buffer_desc major_status_string = GSS_C_EMPTY_BUFFER,
minor_status_string = GSS_C_EMPTY_BUFFER;
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(("1%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
#endif
static int
cups_local_auth(http_t *http)
{
#if defined(WIN32) || defined(__EMX__)
return (1);
#else
int pid;
FILE *fp;
char trc[16],
filename[1024],
certificate[33];
_cups_globals_t *cg = _cupsGlobals();
# if defined(HAVE_AUTHORIZATION_H)
OSStatus status;
AuthorizationItem auth_right;
AuthorizationRights auth_rights;
AuthorizationFlags auth_flags;
AuthorizationExternalForm auth_extrn;
char auth_key[1024];
char buffer[1024];
# endif
DEBUG_printf(("7cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"",
http, httpAddrString(http->hostaddr, filename, sizeof(filename)), http->hostname));
if (!httpAddrLocalhost(http->hostaddr) &&
strcasecmp(http->hostname, "localhost") != 0)
{
DEBUG_puts("8cups_local_auth: Not a local connection!");
return (1);
}
# if defined(HAVE_AUTHORIZATION_H)
if (http->auth_ref)
{
AuthorizationFree(http->auth_ref, kAuthorizationFlagDefaults);
http->auth_ref = NULL;
}
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(("8cups_local_auth: AuthorizationCreate() returned %d (%s)",
(int)status, cssmErrorString(status)));
return (-1);
}
auth_right.name = auth_key;
auth_right.valueLength = 0;
auth_right.value = NULL;
auth_right.flags = 0;
auth_rights.count = 1;
auth_rights.items = &auth_right;
auth_flags = kAuthorizationFlagDefaults |
kAuthorizationFlagPreAuthorize |
kAuthorizationFlagInteractionAllowed |
kAuthorizationFlagExtendRights;
status = AuthorizationCopyRights(http->auth_ref, &auth_rights,
kAuthorizationEmptyEnvironment,
auth_flags, NULL);
if (status == errAuthorizationSuccess)
status = AuthorizationMakeExternalForm(http->auth_ref, &auth_extrn);
if (status == errAuthorizationSuccess)
{
httpEncode64_2(buffer, sizeof(buffer), (void *)&auth_extrn,
sizeof(auth_extrn));
httpSetAuthString(http, "AuthRef", buffer);
DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
http->authstring));
return (0);
}
else if (status == errAuthorizationCanceled)
return (-1);
DEBUG_printf(("9cups_local_auth: AuthorizationCopyRights() returned %d (%s)",
(int)status, cssmErrorString(status)));
}
# endif
# if defined(SO_PEERCRED) && defined(AF_LOCAL)
# ifdef HAVE_GSSAPI
if (strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9) &&
# else
if (
# endif
http->hostaddr->addr.sa_family == AF_LOCAL &&
!getenv("GATEWAY_INTERFACE"))
{
struct passwd *pwd;
const char *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
pid = getpid();
snprintf(filename, sizeof(filename), "%s/certs/%d", cg->cups_statedir, pid);
if ((fp = fopen(filename, "r")) == NULL && pid > 0)
{
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))
{
return (1);
}
#endif
if (!httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "trc", trc,
sizeof(trc)))
{
return (1);
}
snprintf(filename, sizeof(filename), "%s/certs/0", cg->cups_statedir);
fp = fopen(filename, "r");
}
if (fp)
{
fgets(certificate, sizeof(certificate), fp);
fclose(fp);
httpSetAuthString(http, "Local", certificate);
DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
http->authstring));
return (0);
}
return (1);
#endif
}