4 * Authentication functions for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 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 * DEBUG_gss_printf() - Show debug error messages from GSSAPI...
24 * cups_get_gss_creds() - Get CUPS service credentials for authentication.
25 * cups_local_auth() - Get the local authorization certificate if
26 * available/applicable...
30 * Include necessary headers...
40 #if defined(WIN32) || defined(__EMX__)
44 #endif /* WIN32 || __EMX__ */
46 #if HAVE_AUTHORIZATION_H
47 # include <Security/Authorization.h>
48 # ifdef HAVE_SECBASEPRIV_H
49 # include <Security/SecBasePriv.h>
51 extern const char *cssmErrorString(int error
);
52 # endif /* HAVE_SECBASEPRIV_H */
53 #endif /* HAVE_AUTHORIZATION_H */
55 #if defined(SO_PEERCRED) && defined(AF_LOCAL)
57 #endif /* SO_PEERCRED && AF_LOCAL */
66 static void DEBUG_gss_printf(OM_uint32 major_status
, OM_uint32 minor_status
,
69 static gss_name_t
cups_get_gss_creds(http_t
*http
, const char *service_name
);
70 #endif /* HAVE_GSSAPI */
71 static int cups_local_auth(http_t
*http
);
75 * 'cupsDoAuthentication()' - Authenticate a request.
77 * This function should be called in response to a HTTP_UNAUTHORIZED
78 * status, prior to resubmitting your request.
83 int /* O - 0 on success, -1 on error */
84 cupsDoAuthentication(http_t
*http
, /* I - HTTP connection to server */
85 const char *method
,/* I - Request method (GET, POST, PUT) */
87 /* I - Resource path */
89 const char *password
; /* Password string */
90 char prompt
[1024], /* Prompt for user */
91 realm
[HTTP_MAX_VALUE
], /* realm="xyz" string */
92 nonce
[HTTP_MAX_VALUE
], /* nonce="xyz" string */
93 encode
[2048]; /* Encoded username:password */
94 int localauth
; /* Local authentication result */
95 _cups_globals_t
*cg
; /* Global data */
98 DEBUG_printf(("cupsDoAuthentication(http=%p, method=\"%s\", resource=\"%s\")\n",
99 http
, method
, resource
));
100 DEBUG_printf(("cupsDoAuthentication: digest_tries=%d, userpass=\"%s\"\n",
101 http
->digest_tries
, http
->userpass
));
102 DEBUG_printf(("cupsDoAuthentication: WWW-Authenticate=\"%s\"\n",
103 httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
)));
106 * Clear the current authentication string...
109 http
->_authstring
[0] = '\0';
111 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
112 free(http
->authstring
);
114 http
->authstring
= http
->_authstring
;
117 * See if we can do local authentication...
120 if (http
->digest_tries
< 3)
122 if ((localauth
= cups_local_auth(http
)) == 0)
124 DEBUG_printf(("cupsDoAuthentication: authstring=\"%s\"\n",
127 if (http
->status
== HTTP_UNAUTHORIZED
)
128 http
->digest_tries
++;
132 else if (localauth
== -1)
133 return (-1); /* Error or canceled */
137 * Nope, see if we should retry the current username:password...
140 if ((http
->digest_tries
> 1 || !http
->userpass
[0]) &&
141 strncmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], "Negotiate", 9))
144 * Nope - get a new password from the user...
149 if (!cg
->lang_default
)
150 cg
->lang_default
= cupsLangDefault();
152 snprintf(prompt
, sizeof(prompt
),
153 _cupsLangString(cg
->lang_default
, _("Password for %s on %s? ")),
155 http
->hostname
[0] == '/' ? "localhost" : http
->hostname
);
157 http
->digest_tries
= strncasecmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
],
159 http
->userpass
[0] = '\0';
161 if ((password
= cupsGetPassword(prompt
)) == NULL
)
167 snprintf(http
->userpass
, sizeof(http
->userpass
), "%s:%s", cupsUser(),
170 else if (http
->status
== HTTP_UNAUTHORIZED
)
171 http
->digest_tries
++;
174 * Got a password; encode it for the server...
177 if (!strncmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], "Negotiate", 9))
179 if (http
->status
== HTTP_UNAUTHORIZED
&& http
->digest_tries
>= 3)
181 DEBUG_printf(("cupsDoAuthentication: too many Negotiate tries (%d)\n",
182 http
->digest_tries
));
188 * Kerberos authentication...
191 OM_uint32 minor_status
, /* Minor status code */
192 major_status
; /* Major status code */
193 gss_buffer_desc output_token
= GSS_C_EMPTY_BUFFER
,
195 input_token
= GSS_C_EMPTY_BUFFER
;
197 char *gss_service_name
;
198 /* GSS service name */
199 const char *authorization
;
200 /* Pointer into Authorization string */
205 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
209 if (gss_init_sec_context
== NULL
)
211 DEBUG_puts("cupsDoAuthentication: Weak-linked GSSAPI/Kerberos framework "
215 # endif /* __APPLE__ */
217 if (http
->gssname
== GSS_C_NO_NAME
)
219 if ((gss_service_name
= getenv("CUPS_GSSSERVICENAME")) == NULL
)
220 gss_service_name
= CUPS_DEFAULT_GSSSERVICENAME
;
222 DEBUG_puts("cupsDoAuthentication: GSS service name set via environment");
224 http
->gssname
= cups_get_gss_creds(http
, gss_service_name
);
228 * Find the start of the Kerberos input token...
231 authorization
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
234 while (*authorization
&& isspace(*authorization
& 255))
240 * For SPNEGO, this is where we'll feed the server's authorization data
241 * back into gss via input_token...
245 if (http
->gssctx
!= GSS_C_NO_CONTEXT
)
247 major_status
= gss_delete_sec_context(&minor_status
, &http
->gssctx
,
249 http
->gssctx
= GSS_C_NO_CONTEXT
;
252 major_status
= gss_init_sec_context(&minor_status
, GSS_C_NO_CREDENTIAL
,
254 http
->gssname
, http
->gssmech
,
255 GSS_C_DELEG_FLAG
| GSS_C_MUTUAL_FLAG
,
257 GSS_C_NO_CHANNEL_BINDINGS
,
258 &input_token
, &http
->gssmech
,
259 &output_token
, NULL
, NULL
);
261 if (input_token
.value
)
262 free(input_token
.value
);
264 if (GSS_ERROR(major_status
))
267 DEBUG_gss_printf(major_status
, minor_status
,
268 "Unable to initialise security context");
274 if (major_status
== GSS_S_CONTINUE_NEEDED
)
275 DEBUG_gss_printf(major_status
, minor_status
, "Continuation needed!");
278 if (output_token
.length
)
280 httpEncode64_2(encode
, sizeof(encode
), output_token
.value
,
281 output_token
.length
);
283 http
->authstring
= malloc(strlen(encode
) + 11);
284 sprintf(http
->authstring
, "Negotiate %s", encode
); /* Safe because allocated */
286 major_status
= gss_release_buffer(&minor_status
, &output_token
);
290 * Copy back what we can to _authstring for backwards compatibility...
293 strlcpy(http
->_authstring
, http
->authstring
, sizeof(http
->_authstring
));
294 #endif /* HAVE_GSSAPI */
296 else if (strncmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], "Digest", 6))
299 * Basic authentication...
302 httpEncode64_2(encode
, sizeof(encode
), http
->userpass
,
303 (int)strlen(http
->userpass
));
304 snprintf(http
->_authstring
, sizeof(http
->_authstring
), "Basic %s", encode
);
309 * Digest authentication...
312 httpGetSubField(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "realm", realm
);
313 httpGetSubField(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "nonce", nonce
);
315 httpMD5(cupsUser(), realm
, strchr(http
->userpass
, ':') + 1, encode
);
316 httpMD5Final(nonce
, method
, resource
, encode
);
317 snprintf(http
->_authstring
, sizeof(http
->_authstring
),
318 "Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", "
319 "uri=\"%s\", response=\"%s\"", cupsUser(), realm
, nonce
,
323 DEBUG_printf(("cupsDoAuthentication: authstring=\"%s\"\n", http
->authstring
));
332 * 'DEBUG_gss_printf()' - Show debug error messages from GSSAPI...
336 DEBUG_gss_printf(OM_uint32 major_status
,/* I - Major status code */
337 OM_uint32 minor_status
,/* I - Minor status code */
338 const char *message
) /* I - Prefix for error message */
340 OM_uint32 err_major_status
, /* Major status code for display */
341 err_minor_status
; /* Minor status code for display */
342 OM_uint32 msg_ctx
; /* Message context */
343 gss_buffer_desc major_status_string
= GSS_C_EMPTY_BUFFER
,
344 /* Major status message */
345 minor_status_string
= GSS_C_EMPTY_BUFFER
;
346 /* Minor status message */
350 err_major_status
= gss_display_status(&err_minor_status
,
355 &major_status_string
);
357 if (!GSS_ERROR(err_major_status
))
358 err_major_status
= gss_display_status(&err_minor_status
,
363 &minor_status_string
);
365 printf("%s: %s, %s\n", message
, (char *)major_status_string
.value
,
366 (char *)minor_status_string
.value
);
368 gss_release_buffer(&err_minor_status
, &major_status_string
);
369 gss_release_buffer(&err_minor_status
, &minor_status_string
);
375 * 'cups_get_gss_creds()' - Get CUPS service credentials for authentication.
378 static gss_name_t
/* O - Server name */
380 http_t
*http
, /* I - Connection to server */
381 const char *service_name
) /* I - Service name */
383 gss_buffer_desc token
= GSS_C_EMPTY_BUFFER
;
385 OM_uint32 major_status
, /* Major status code */
386 minor_status
; /* Minor status code */
387 gss_name_t server_name
; /* Server name */
388 char buf
[1024], /* Name buffer */
389 fqdn
[HTTP_MAX_URI
]; /* Server name buffer */
393 * Get the hostname...
396 httpGetHostname(http
, fqdn
, sizeof(fqdn
));
398 if (!strcmp(fqdn
, "localhost"))
399 httpGetHostname(NULL
, fqdn
, sizeof(fqdn
));
402 * Get a server name we can use for authentication purposes...
405 snprintf(buf
, sizeof(buf
), "%s@%s", service_name
, fqdn
);
408 token
.length
= strlen(buf
);
409 server_name
= GSS_C_NO_NAME
;
410 major_status
= gss_import_name(&minor_status
, &token
,
411 GSS_C_NT_HOSTBASED_SERVICE
,
414 if (GSS_ERROR(major_status
))
417 DEBUG_gss_printf(major_status
, minor_status
, "gss_import_name() failed");
423 return (server_name
);
425 #endif /* HAVE_GSSAPI */
429 * 'cups_local_auth()' - Get the local authorization certificate if
430 * available/applicable...
433 static int /* O - 0 if available */
434 /* 1 if not available */
436 cups_local_auth(http_t
*http
) /* I - HTTP connection to server */
438 #if defined(WIN32) || defined(__EMX__)
440 * Currently WIN32 and OS-2 do not support the CUPS server...
445 int pid
; /* Current process ID */
446 FILE *fp
; /* Certificate file */
447 char filename
[1024], /* Certificate filename */
448 certificate
[33];/* Certificate string */
449 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
450 # if defined(HAVE_AUTHORIZATION_H)
451 OSStatus status
; /* Status */
452 AuthorizationItem auth_right
; /* Authorization right */
453 AuthorizationRights auth_rights
; /* Authorization rights */
454 AuthorizationFlags auth_flags
; /* Authorization flags */
455 AuthorizationExternalForm auth_extrn
; /* Authorization ref external */
456 char auth_key
[1024]; /* Buffer */
457 char buffer
[1024]; /* Buffer */
458 # endif /* HAVE_AUTHORIZATION_H */
461 DEBUG_printf(("cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"\n",
462 http
, httpAddrString(http
->hostaddr
, filename
, sizeof(filename
)), http
->hostname
));
465 * See if we are accessing localhost...
468 if (!httpAddrLocalhost(http
->hostaddr
) &&
469 strcasecmp(http
->hostname
, "localhost") != 0)
471 DEBUG_puts("cups_local_auth: Not a local connection!");
475 # if defined(HAVE_AUTHORIZATION_H)
477 * Delete any previous authorization reference...
482 AuthorizationFree(http
->auth_ref
, kAuthorizationFlagDefaults
);
483 http
->auth_ref
= NULL
;
486 if (httpGetSubField2(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "authkey",
487 auth_key
, sizeof(auth_key
)))
489 status
= AuthorizationCreate(NULL
, kAuthorizationEmptyEnvironment
,
490 kAuthorizationFlagDefaults
, &http
->auth_ref
);
491 if (status
!= errAuthorizationSuccess
)
493 DEBUG_printf(("cups_local_auth: AuthorizationCreate() returned %d (%s)\n",
494 (int)status
, cssmErrorString(status
)));
498 auth_right
.name
= auth_key
;
499 auth_right
.valueLength
= 0;
500 auth_right
.value
= NULL
;
501 auth_right
.flags
= 0;
503 auth_rights
.count
= 1;
504 auth_rights
.items
= &auth_right
;
506 auth_flags
= kAuthorizationFlagDefaults
|
507 kAuthorizationFlagPreAuthorize
|
508 kAuthorizationFlagInteractionAllowed
|
509 kAuthorizationFlagExtendRights
;
511 status
= AuthorizationCopyRights(http
->auth_ref
, &auth_rights
,
512 kAuthorizationEmptyEnvironment
,
514 if (status
== errAuthorizationSuccess
)
515 status
= AuthorizationMakeExternalForm(http
->auth_ref
, &auth_extrn
);
517 if (status
== errAuthorizationSuccess
)
520 * Set the authorization string and return...
523 httpEncode64_2(buffer
, sizeof(buffer
), (void *)&auth_extrn
,
526 http
->authstring
= malloc(strlen(buffer
) + 9);
527 sprintf(http
->authstring
, "AuthRef %s", buffer
);
529 /* Copy back to _authstring for backwards compatibility */
530 strlcpy(http
->_authstring
, http
->authstring
, sizeof(http
->_authstring
));
532 DEBUG_printf(("cups_local_auth: Returning authstring = \"%s\"\n",
536 else if (status
== errAuthorizationCanceled
)
539 DEBUG_printf(("cups_local_auth: AuthorizationCopyRights() returned %d (%s)\n",
540 (int)status
, cssmErrorString(status
)));
543 * Fall through to try certificates...
546 # endif /* HAVE_AUTHORIZATION_H */
549 * Try opening a certificate file for this PID. If that fails,
550 * try the root certificate...
554 snprintf(filename
, sizeof(filename
), "%s/certs/%d", cg
->cups_statedir
, pid
);
555 if ((fp
= fopen(filename
, "r")) == NULL
&& pid
> 0)
557 DEBUG_printf(("cups_local_auth: Unable to open file %s: %s\n",
558 filename
, strerror(errno
)));
562 * If local certificate authentication isn't available for this PID,
563 * check if we need Kerberos authentication...
566 if (!strcmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], "Negotiate"))
569 * Yes, don't try the root certificate...
574 #endif /* HAVE_GSSAPI */
576 snprintf(filename
, sizeof(filename
), "%s/certs/0", cg
->cups_statedir
);
577 fp
= fopen(filename
, "r");
583 * Read the certificate from the file...
586 fgets(certificate
, sizeof(certificate
), fp
);
590 * Set the authorization string and return...
593 http
->authstring
= malloc(strlen(certificate
) + 7);
594 sprintf(http
->authstring
, "Local %s", certificate
);
596 /* Copy back to _authstring for backwards compatibility */
597 strlcpy(http
->_authstring
, http
->authstring
, sizeof(http
->_authstring
));
599 DEBUG_printf(("cups_local_auth: Returning authstring = \"%s\"\n",
605 # if defined(SO_PEERCRED) && defined(AF_LOCAL)
607 * See if we can authenticate using the peer credentials provided over a
608 * domain socket; if so, specify "PeerCred username" as the authentication
612 if (http
->hostaddr
->addr
.sa_family
== AF_LOCAL
&&
613 !getenv("GATEWAY_INTERFACE")) /* Not via CGI programs... */
616 * Verify that the current cupsUser() matches the current UID...
619 struct passwd
*pwd
; /* Password information */
620 const char *username
; /* Current username */
622 username
= cupsUser();
624 if ((pwd
= getpwnam(username
)) != NULL
&& pwd
->pw_uid
== getuid())
626 http
->authstring
= malloc(strlen(username
) + 10);
627 sprintf(http
->authstring
, "PeerCred %s", username
);
629 /* Copy back to _authstring for backwards compatibility */
630 strlcpy(http
->_authstring
, http
->authstring
, sizeof(http
->_authstring
));
632 DEBUG_printf(("cups_local_auth: Returning authstring = \"%s\"\n",
638 # endif /* SO_PEERCRED && AF_LOCAL */
641 #endif /* WIN32 || __EMX__ */