4 * Authentication functions for CUPS.
6 * Copyright 2007-2010 by Apple Inc.
7 * Copyright 1997-2007 by Easy Software Products.
9 * This file contains Kerberos support code, copyright 2006 by
12 * These coded instructions, statements, and computer programs are the
13 * property of Apple Inc. and are protected by Federal copyright
14 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
15 * which should have been included with this file. If this file is
16 * file is missing or damaged, see the license at "http://www.cups.org/".
18 * This file is subject to the Apple OS-Developed Software exception.
22 * cupsDoAuthentication() - Authenticate a request.
23 * cups_get_gssname() - Get GSSAPI name for authentication.
24 * cups_gss_printf() - Show error messages from GSSAPI...
25 * cups_local_auth() - Get the local authorization certificate if
26 * available/applicable...
30 * Include necessary headers...
33 #include "cups-private.h"
36 #if defined(WIN32) || defined(__EMX__)
40 #endif /* WIN32 || __EMX__ */
42 #if HAVE_AUTHORIZATION_H
43 # include <Security/Authorization.h>
44 # ifdef HAVE_SECBASEPRIV_H
45 # include <Security/SecBasePriv.h>
47 extern const char *cssmErrorString(int error
);
48 # endif /* HAVE_SECBASEPRIV_H */
49 #endif /* HAVE_AUTHORIZATION_H */
51 #if defined(SO_PEERCRED) && defined(AF_LOCAL)
53 #endif /* SO_PEERCRED && AF_LOCAL */
61 static gss_name_t
cups_get_gssname(http_t
*http
, const char *service_name
);
63 static void cups_gss_printf(OM_uint32 major_status
, OM_uint32 minor_status
,
66 # define cups_gss_printf(major, minor, message)
68 #endif /* HAVE_GSSAPI */
69 static int cups_local_auth(http_t
*http
);
73 * 'cupsDoAuthentication()' - Authenticate a request.
75 * This function should be called in response to a @code HTTP_UNAUTHORIZED@
76 * status, prior to resubmitting your request.
78 * @since CUPS 1.1.20/Mac OS X 10.4@
81 int /* O - 0 on success, -1 on error */
83 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
84 const char *method
, /* I - Request method ("GET", "POST", "PUT") */
85 const char *resource
) /* I - Resource path */
87 const char *password
; /* Password string */
88 char prompt
[1024], /* Prompt for user */
89 realm
[HTTP_MAX_VALUE
], /* realm="xyz" string */
90 nonce
[HTTP_MAX_VALUE
]; /* nonce="xyz" string */
91 int localauth
; /* Local authentication result */
92 _cups_globals_t
*cg
; /* Global data */
95 DEBUG_printf(("cupsDoAuthentication(http=%p, method=\"%s\", resource=\"%s\")",
96 http
, method
, resource
));
99 http
= _cupsConnect();
101 if (!http
|| !method
|| !resource
)
104 DEBUG_printf(("2cupsDoAuthentication: digest_tries=%d, userpass=\"%s\"",
105 http
->digest_tries
, http
->userpass
));
106 DEBUG_printf(("2cupsDoAuthentication: WWW-Authenticate=\"%s\"",
107 httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
)));
110 * Clear the current authentication string...
113 httpSetAuthString(http
, NULL
, NULL
);
116 * See if we can do local authentication...
119 if (http
->digest_tries
< 3)
121 if ((localauth
= cups_local_auth(http
)) == 0)
123 DEBUG_printf(("2cupsDoAuthentication: authstring=\"%s\"",
126 if (http
->status
== HTTP_UNAUTHORIZED
)
127 http
->digest_tries
++;
131 else if (localauth
== -1)
133 http
->status
= HTTP_AUTHORIZATION_CANCELED
;
134 return (-1); /* Error or canceled */
139 * Nope, see if we should retry the current username:password...
142 if ((http
->digest_tries
> 1 || !http
->userpass
[0]) &&
143 strncmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], "Negotiate", 9))
146 * Nope - get a new password from the user...
151 if (!cg
->lang_default
)
152 cg
->lang_default
= cupsLangDefault();
154 snprintf(prompt
, sizeof(prompt
),
155 _cupsLangString(cg
->lang_default
, _("Password for %s on %s? ")),
157 http
->hostname
[0] == '/' ? "localhost" : http
->hostname
);
159 http
->digest_tries
= strncasecmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
],
161 http
->userpass
[0] = '\0';
163 if ((password
= cupsGetPassword2(prompt
, http
, method
, resource
)) == NULL
)
165 http
->status
= HTTP_AUTHORIZATION_CANCELED
;
169 snprintf(http
->userpass
, sizeof(http
->userpass
), "%s:%s", cupsUser(),
172 else if (http
->status
== HTTP_UNAUTHORIZED
)
173 http
->digest_tries
++;
175 if (http
->status
== HTTP_UNAUTHORIZED
&& http
->digest_tries
>= 3)
177 DEBUG_printf(("1cupsDoAuthentication: Too many authentication tries (%d)",
178 http
->digest_tries
));
180 http
->status
= HTTP_AUTHORIZATION_CANCELED
;
185 * Got a password; encode it for the server...
188 if (!strncmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], "Negotiate", 9))
192 * Kerberos authentication...
195 OM_uint32 minor_status
, /* Minor status code */
196 major_status
; /* Major status code */
197 gss_buffer_desc output_token
= GSS_C_EMPTY_BUFFER
,
199 input_token
= GSS_C_EMPTY_BUFFER
;
201 char *gss_service_name
;
202 /* GSS service name */
204 const char *authorization
;
205 /* Pointer into Authorization string */
206 # endif /* USE_SPNEGO */
211 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
215 if (gss_init_sec_context
== NULL
)
217 DEBUG_puts("1cupsDoAuthentication: Weak-linked GSSAPI/Kerberos framework "
219 http
->status
= HTTP_AUTHORIZATION_CANCELED
;
223 # endif /* __APPLE__ */
225 if (http
->gssname
== GSS_C_NO_NAME
)
227 if ((gss_service_name
= getenv("CUPS_GSSSERVICENAME")) == NULL
)
228 gss_service_name
= CUPS_DEFAULT_GSSSERVICENAME
;
230 DEBUG_puts("2cupsDoAuthentication: GSS service name set via "
231 "environment variable");
233 http
->gssname
= cups_get_gssname(http
, gss_service_name
);
236 # ifdef USE_SPNEGO /* We don't implement SPNEGO just yet... */
238 * Find the start of the Kerberos input token...
241 authorization
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
244 while (*authorization
&& _cups_isspace(*authorization
))
250 * Decode the authorization string to get the input token...
253 int len
= strlen(authorization
);
255 input_token
.value
= malloc(len
);
256 input_token
.value
= httpDecode64_2(input_token
.value
, &len
,
258 input_token
.length
= len
;
262 char *ptr
= (char *)input_token
.value
;
265 fputs("input_token=", stdout
);
269 printf("\\%03o", *ptr
& 255);
279 # endif /* USE_SPNEGO */
281 if (http
->gssctx
!= GSS_C_NO_CONTEXT
)
283 gss_delete_sec_context(&minor_status
, &http
->gssctx
, GSS_C_NO_BUFFER
);
284 http
->gssctx
= GSS_C_NO_CONTEXT
;
287 major_status
= gss_init_sec_context(&minor_status
, GSS_C_NO_CREDENTIAL
,
289 http
->gssname
, http
->gssmech
,
290 #ifdef GSS_C_DELEG_POLICY_FLAG
291 GSS_C_DELEG_POLICY_FLAG
|
292 #endif /* GSS_C_DELEG_POLICY_FLAG */
293 GSS_C_MUTUAL_FLAG
| GSS_C_INTEG_FLAG
,
295 GSS_C_NO_CHANNEL_BINDINGS
,
296 &input_token
, &http
->gssmech
,
297 &output_token
, NULL
, NULL
);
299 if (input_token
.value
)
300 free(input_token
.value
);
302 if (GSS_ERROR(major_status
))
304 cups_gss_printf(major_status
, minor_status
,
305 "cupsDoAuthentication: Unable to initialize security "
307 http
->status
= HTTP_AUTHORIZATION_CANCELED
;
312 if (major_status
== GSS_S_CONTINUE_NEEDED
)
313 cups_gss_printf(major_status
, minor_status
,
314 "cupsDoAuthentication: Continuation needed!");
316 if (output_token
.length
> 0 && output_token
.length
<= 65536)
319 * Allocate the authorization string since Windows KDCs can have
320 * arbitrarily large credentials...
323 int authsize
= 10 + /* "Negotiate " */
324 output_token
.length
* 4 / 3 + 1 + /* Base64 */
327 httpSetAuthString(http
, NULL
, NULL
);
329 if ((http
->authstring
= malloc(authsize
)) == NULL
)
331 http
->authstring
= http
->_authstring
;
332 authsize
= sizeof(http
->_authstring
);
335 strcpy(http
->authstring
, "Negotiate ");
336 httpEncode64_2(http
->authstring
+ 10, authsize
- 10, output_token
.value
,
337 output_token
.length
);
339 gss_release_buffer(&minor_status
, &output_token
);
343 DEBUG_printf(("1cupsDoAuthentication: Kerberos credentials too large - "
344 "%d bytes!", (int)output_token
.length
));
345 http
->status
= HTTP_AUTHORIZATION_CANCELED
;
346 gss_release_buffer(&minor_status
, &output_token
);
350 #endif /* HAVE_GSSAPI */
352 else if (strncmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], "Digest", 6))
355 * Basic authentication...
358 char encode
[256]; /* Base64 buffer */
361 httpEncode64_2(encode
, sizeof(encode
), http
->userpass
,
362 (int)strlen(http
->userpass
));
363 httpSetAuthString(http
, "Basic", encode
);
368 * Digest authentication...
371 char encode
[33], /* MD5 buffer */
372 digest
[1024]; /* Digest auth data */
375 httpGetSubField(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "realm", realm
);
376 httpGetSubField(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "nonce", nonce
);
378 httpMD5(cupsUser(), realm
, strchr(http
->userpass
, ':') + 1, encode
);
379 httpMD5Final(nonce
, method
, resource
, encode
);
380 snprintf(digest
, sizeof(digest
),
381 "username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", "
382 "response=\"%s\"", cupsUser(), realm
, nonce
, resource
, encode
);
383 httpSetAuthString(http
, "Digest", digest
);
386 DEBUG_printf(("1cupsDoAuthentication: authstring=\"%s\"", http
->authstring
));
394 * 'cups_get_gssname()' - Get CUPS service credentials for authentication.
397 static gss_name_t
/* O - Server name */
399 http_t
*http
, /* I - Connection to server */
400 const char *service_name
) /* I - Service name */
402 gss_buffer_desc token
= GSS_C_EMPTY_BUFFER
;
404 OM_uint32 major_status
, /* Major status code */
405 minor_status
; /* Minor status code */
406 gss_name_t server_name
; /* Server name */
407 char buf
[1024], /* Name buffer */
408 fqdn
[HTTP_MAX_URI
]; /* Server name buffer */
411 DEBUG_printf(("7cups_get_gssname(http=%p, service_name=\"%s\")", http
,
416 * Get the hostname...
419 httpGetHostname(http
, fqdn
, sizeof(fqdn
));
421 if (!strcmp(fqdn
, "localhost"))
422 httpGetHostname(NULL
, fqdn
, sizeof(fqdn
));
425 * Get a server name we can use for authentication purposes...
428 snprintf(buf
, sizeof(buf
), "%s@%s", service_name
, fqdn
);
430 DEBUG_printf(("9cups_get_gssname: Looking up %s...", buf
));
433 token
.length
= strlen(buf
);
434 server_name
= GSS_C_NO_NAME
;
435 major_status
= gss_import_name(&minor_status
, &token
,
436 GSS_C_NT_HOSTBASED_SERVICE
,
439 if (GSS_ERROR(major_status
))
441 cups_gss_printf(major_status
, minor_status
,
442 "cups_get_gssname: gss_import_name() failed");
446 return (server_name
);
452 * 'cups_gss_printf()' - Show debug error messages from GSSAPI...
456 cups_gss_printf(OM_uint32 major_status
,/* I - Major status code */
457 OM_uint32 minor_status
,/* I - Minor status code */
458 const char *message
) /* I - Prefix for error message */
460 OM_uint32 err_major_status
, /* Major status code for display */
461 err_minor_status
; /* Minor status code for display */
462 OM_uint32 msg_ctx
; /* Message context */
463 gss_buffer_desc major_status_string
= GSS_C_EMPTY_BUFFER
,
464 /* Major status message */
465 minor_status_string
= GSS_C_EMPTY_BUFFER
;
466 /* Minor status message */
470 err_major_status
= gss_display_status(&err_minor_status
,
475 &major_status_string
);
477 if (!GSS_ERROR(err_major_status
))
478 gss_display_status(&err_minor_status
, minor_status
, GSS_C_MECH_CODE
,
479 GSS_C_NULL_OID
, &msg_ctx
, &minor_status_string
);
481 DEBUG_printf(("1%s: %s, %s", message
, (char *)major_status_string
.value
,
482 (char *)minor_status_string
.value
));
484 gss_release_buffer(&err_minor_status
, &major_status_string
);
485 gss_release_buffer(&err_minor_status
, &minor_status_string
);
488 #endif /* HAVE_GSSAPI */
492 * 'cups_local_auth()' - Get the local authorization certificate if
493 * available/applicable...
496 static int /* O - 0 if available */
497 /* 1 if not available */
499 cups_local_auth(http_t
*http
) /* I - HTTP connection to server */
501 #if defined(WIN32) || defined(__EMX__)
503 * Currently WIN32 and OS-2 do not support the CUPS server...
508 int pid
; /* Current process ID */
509 FILE *fp
; /* Certificate file */
510 char trc
[16], /* Try Root Certificate parameter */
511 filename
[1024], /* Certificate filename */
512 certificate
[33];/* Certificate string */
513 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
514 # if defined(HAVE_AUTHORIZATION_H)
515 OSStatus status
; /* Status */
516 AuthorizationItem auth_right
; /* Authorization right */
517 AuthorizationRights auth_rights
; /* Authorization rights */
518 AuthorizationFlags auth_flags
; /* Authorization flags */
519 AuthorizationExternalForm auth_extrn
; /* Authorization ref external */
520 char auth_key
[1024]; /* Buffer */
521 char buffer
[1024]; /* Buffer */
522 # endif /* HAVE_AUTHORIZATION_H */
525 DEBUG_printf(("7cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"",
526 http
, httpAddrString(http
->hostaddr
, filename
, sizeof(filename
)), http
->hostname
));
529 * See if we are accessing localhost...
532 if (!httpAddrLocalhost(http
->hostaddr
) &&
533 strcasecmp(http
->hostname
, "localhost") != 0)
535 DEBUG_puts("8cups_local_auth: Not a local connection!");
539 # if defined(HAVE_AUTHORIZATION_H)
541 * Delete any previous authorization reference...
546 AuthorizationFree(http
->auth_ref
, kAuthorizationFlagDefaults
);
547 http
->auth_ref
= NULL
;
550 if (!getenv("GATEWAY_INTERFACE") &&
551 httpGetSubField2(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "authkey",
552 auth_key
, sizeof(auth_key
)))
554 status
= AuthorizationCreate(NULL
, kAuthorizationEmptyEnvironment
,
555 kAuthorizationFlagDefaults
, &http
->auth_ref
);
556 if (status
!= errAuthorizationSuccess
)
558 DEBUG_printf(("8cups_local_auth: AuthorizationCreate() returned %d (%s)",
559 (int)status
, cssmErrorString(status
)));
563 auth_right
.name
= auth_key
;
564 auth_right
.valueLength
= 0;
565 auth_right
.value
= NULL
;
566 auth_right
.flags
= 0;
568 auth_rights
.count
= 1;
569 auth_rights
.items
= &auth_right
;
571 auth_flags
= kAuthorizationFlagDefaults
|
572 kAuthorizationFlagPreAuthorize
|
573 kAuthorizationFlagInteractionAllowed
|
574 kAuthorizationFlagExtendRights
;
576 status
= AuthorizationCopyRights(http
->auth_ref
, &auth_rights
,
577 kAuthorizationEmptyEnvironment
,
579 if (status
== errAuthorizationSuccess
)
580 status
= AuthorizationMakeExternalForm(http
->auth_ref
, &auth_extrn
);
582 if (status
== errAuthorizationSuccess
)
585 * Set the authorization string and return...
588 httpEncode64_2(buffer
, sizeof(buffer
), (void *)&auth_extrn
,
591 httpSetAuthString(http
, "AuthRef", buffer
);
593 DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
597 else if (status
== errAuthorizationCanceled
)
600 DEBUG_printf(("9cups_local_auth: AuthorizationCopyRights() returned %d (%s)",
601 (int)status
, cssmErrorString(status
)));
604 * Fall through to try certificates...
607 # endif /* HAVE_AUTHORIZATION_H */
609 # if defined(SO_PEERCRED) && defined(AF_LOCAL)
611 * See if we can authenticate using the peer credentials provided over a
612 * domain socket; if so, specify "PeerCred username" as the authentication
617 if (strncmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], "Negotiate", 9) &&
620 # endif /* HAVE_GSSAPI */
621 http
->hostaddr
->addr
.sa_family
== AF_LOCAL
&&
622 !getenv("GATEWAY_INTERFACE")) /* Not via CGI programs... */
625 * Verify that the current cupsUser() matches the current UID...
628 struct passwd
*pwd
; /* Password information */
629 const char *username
; /* Current username */
631 username
= cupsUser();
633 if ((pwd
= getpwnam(username
)) != NULL
&& pwd
->pw_uid
== getuid())
635 httpSetAuthString(http
, "PeerCred", username
);
637 DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
643 # endif /* SO_PEERCRED && AF_LOCAL */
646 * Try opening a certificate file for this PID. If that fails,
647 * try the root certificate...
651 snprintf(filename
, sizeof(filename
), "%s/certs/%d", cg
->cups_statedir
, pid
);
652 if ((fp
= fopen(filename
, "r")) == NULL
&& pid
> 0)
655 * No certificate for this PID; see if we can get the root certificate...
658 DEBUG_printf(("9cups_local_auth: Unable to open file %s: %s",
659 filename
, strerror(errno
)));
662 if (!strncmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], "Negotiate", 9))
665 * Kerberos required, don't try the root certificate...
670 #endif /* HAVE_GSSAPI */
672 if (!httpGetSubField2(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "trc", trc
,
676 * Scheduler doesn't want us to use the root certificate...
682 snprintf(filename
, sizeof(filename
), "%s/certs/0", cg
->cups_statedir
);
683 fp
= fopen(filename
, "r");
689 * Read the certificate from the file...
692 fgets(certificate
, sizeof(certificate
), fp
);
696 * Set the authorization string and return...
699 httpSetAuthString(http
, "Local", certificate
);
701 DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
708 #endif /* WIN32 || __EMX__ */