2 * "$Id: auth.c 6722 2007-07-25 17:19:09Z mike $"
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 # define DEBUG_gss_printf(major, minor, message)
71 static gss_name_t
cups_get_gss_creds(http_t
*http
, const char *service_name
);
72 #endif /* HAVE_GSSAPI */
73 static int cups_local_auth(http_t
*http
);
77 * 'cupsDoAuthentication()' - Authenticate a request.
79 * This function should be called in response to a HTTP_UNAUTHORIZED
80 * status, prior to resubmitting your request.
85 int /* O - 0 on success, -1 on error */
86 cupsDoAuthentication(http_t
*http
, /* I - HTTP connection to server */
87 const char *method
,/* I - Request method (GET, POST, PUT) */
89 /* I - Resource path */
91 const char *password
; /* Password string */
92 char prompt
[1024], /* Prompt for user */
93 realm
[HTTP_MAX_VALUE
], /* realm="xyz" string */
94 nonce
[HTTP_MAX_VALUE
], /* nonce="xyz" string */
95 encode
[2048]; /* Encoded username:password */
96 int localauth
; /* Local authentication result */
97 _cups_globals_t
*cg
; /* Global data */
100 DEBUG_printf(("cupsDoAuthentication(http=%p, method=\"%s\", resource=\"%s\")\n",
101 http
, method
, resource
));
102 DEBUG_printf(("cupsDoAuthentication: digest_tries=%d, userpass=\"%s\"\n",
103 http
->digest_tries
, http
->userpass
));
104 DEBUG_printf(("cupsDoAuthentication: WWW-Authenticate=\"%s\"\n",
105 httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
)));
108 * Clear the current authentication string...
111 httpSetAuthString(http
, NULL
, NULL
);
114 * See if we can do local authentication...
117 if (http
->digest_tries
< 3)
119 if ((localauth
= cups_local_auth(http
)) == 0)
121 DEBUG_printf(("cupsDoAuthentication: authstring=\"%s\"\n",
124 if (http
->status
== HTTP_UNAUTHORIZED
)
125 http
->digest_tries
++;
129 else if (localauth
== -1)
130 return (-1); /* Error or canceled */
134 * Nope, see if we should retry the current username:password...
137 if ((http
->digest_tries
> 1 || !http
->userpass
[0]) &&
138 strncmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], "Negotiate", 9))
141 * Nope - get a new password from the user...
146 if (!cg
->lang_default
)
147 cg
->lang_default
= cupsLangDefault();
149 snprintf(prompt
, sizeof(prompt
),
150 _cupsLangString(cg
->lang_default
, _("Password for %s on %s? ")),
152 http
->hostname
[0] == '/' ? "localhost" : http
->hostname
);
154 http
->digest_tries
= strncasecmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
],
156 http
->userpass
[0] = '\0';
158 if ((password
= cupsGetPassword(prompt
)) == NULL
)
164 snprintf(http
->userpass
, sizeof(http
->userpass
), "%s:%s", cupsUser(),
167 else if (http
->status
== HTTP_UNAUTHORIZED
)
168 http
->digest_tries
++;
171 * Got a password; encode it for the server...
174 if (!strncmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], "Negotiate", 9))
178 * Kerberos authentication...
181 OM_uint32 minor_status
, /* Minor status code */
182 major_status
; /* Major status code */
183 gss_buffer_desc output_token
= GSS_C_EMPTY_BUFFER
,
185 input_token
= GSS_C_EMPTY_BUFFER
;
187 char *gss_service_name
;
188 /* GSS service name */
190 const char *authorization
;
191 /* Pointer into Authorization string */
192 # endif /* USE_SPNEGO */
197 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
201 if (gss_init_sec_context
== NULL
)
203 DEBUG_puts("cupsDoAuthentication: Weak-linked GSSAPI/Kerberos framework "
207 # endif /* __APPLE__ */
209 if (http
->status
== HTTP_UNAUTHORIZED
&& http
->digest_tries
>= 3)
211 DEBUG_printf(("cupsDoAuthentication: too many Negotiate tries (%d)\n",
212 http
->digest_tries
));
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
);
227 # ifdef USE_SPNEGO /* We don't implement SPNEGO just yet... */
229 * Find the start of the Kerberos input token...
232 authorization
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
235 while (*authorization
&& isspace(*authorization
& 255))
241 * Decode the authorization string to get the input token...
244 int len
= strlen(authorization
);
246 input_token
.value
= malloc(len
);
247 input_token
.value
= httpDecode64_2(input_token
.value
, &len
,
249 input_token
.length
= len
;
253 char *ptr
= (char *)input_token
.value
;
256 fputs("input_token=", stdout
);
260 printf("\\%03o", *ptr
& 255);
270 # endif /* USE_SPNEGO */
272 if (http
->gssctx
!= GSS_C_NO_CONTEXT
)
274 major_status
= gss_delete_sec_context(&minor_status
, &http
->gssctx
,
276 http
->gssctx
= GSS_C_NO_CONTEXT
;
279 major_status
= gss_init_sec_context(&minor_status
, GSS_C_NO_CREDENTIAL
,
281 http
->gssname
, http
->gssmech
,
282 GSS_C_DELEG_FLAG
| GSS_C_MUTUAL_FLAG
|
285 GSS_C_NO_CHANNEL_BINDINGS
,
286 &input_token
, &http
->gssmech
,
287 &output_token
, NULL
, NULL
);
289 if (input_token
.value
)
290 free(input_token
.value
);
292 if (GSS_ERROR(major_status
))
295 DEBUG_gss_printf(major_status
, minor_status
,
296 "Unable to initialize security context");
301 if (major_status
== GSS_S_CONTINUE_NEEDED
)
302 DEBUG_gss_printf(major_status
, minor_status
, "Continuation needed!");
304 if (output_token
.length
)
306 httpEncode64_2(encode
, sizeof(encode
), output_token
.value
,
307 output_token
.length
);
308 httpSetAuthString(http
, "Negotiate", encode
);
310 major_status
= gss_release_buffer(&minor_status
, &output_token
);
312 #endif /* HAVE_GSSAPI */
314 else if (strncmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], "Digest", 6))
317 * Basic authentication...
320 httpEncode64_2(encode
, sizeof(encode
), http
->userpass
,
321 (int)strlen(http
->userpass
));
322 httpSetAuthString(http
, "Basic", encode
);
327 * Digest authentication...
330 char digest
[1024]; /* Digest auth data */
333 httpGetSubField(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "realm", realm
);
334 httpGetSubField(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "nonce", nonce
);
336 httpMD5(cupsUser(), realm
, strchr(http
->userpass
, ':') + 1, encode
);
337 httpMD5Final(nonce
, method
, resource
, encode
);
338 snprintf(digest
, sizeof(digest
),
339 "username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", "
340 "response=\"%s\"", cupsUser(), realm
, nonce
, resource
, encode
);
341 httpSetAuthString(http
, "Digest", digest
);
344 DEBUG_printf(("cupsDoAuthentication: authstring=\"%s\"\n", http
->authstring
));
353 * 'DEBUG_gss_printf()' - Show debug error messages from GSSAPI...
357 DEBUG_gss_printf(OM_uint32 major_status
,/* I - Major status code */
358 OM_uint32 minor_status
,/* I - Minor status code */
359 const char *message
) /* I - Prefix for error message */
361 OM_uint32 err_major_status
, /* Major status code for display */
362 err_minor_status
; /* Minor status code for display */
363 OM_uint32 msg_ctx
; /* Message context */
364 gss_buffer_desc major_status_string
= GSS_C_EMPTY_BUFFER
,
365 /* Major status message */
366 minor_status_string
= GSS_C_EMPTY_BUFFER
;
367 /* Minor status message */
371 err_major_status
= gss_display_status(&err_minor_status
,
376 &major_status_string
);
378 if (!GSS_ERROR(err_major_status
))
379 err_major_status
= gss_display_status(&err_minor_status
,
384 &minor_status_string
);
386 printf("%s: %s, %s\n", message
, (char *)major_status_string
.value
,
387 (char *)minor_status_string
.value
);
389 gss_release_buffer(&err_minor_status
, &major_status_string
);
390 gss_release_buffer(&err_minor_status
, &minor_status_string
);
396 * 'cups_get_gss_creds()' - Get CUPS service credentials for authentication.
399 static gss_name_t
/* O - Server name */
401 http_t
*http
, /* I - Connection to server */
402 const char *service_name
) /* I - Service name */
404 gss_buffer_desc token
= GSS_C_EMPTY_BUFFER
;
406 OM_uint32 major_status
, /* Major status code */
407 minor_status
; /* Minor status code */
408 gss_name_t server_name
; /* Server name */
409 char buf
[1024], /* Name buffer */
410 fqdn
[HTTP_MAX_URI
]; /* Server name buffer */
414 * Get the hostname...
417 httpGetHostname(http
, fqdn
, sizeof(fqdn
));
419 if (!strcmp(fqdn
, "localhost"))
420 httpGetHostname(NULL
, fqdn
, sizeof(fqdn
));
423 * Get a server name we can use for authentication purposes...
426 snprintf(buf
, sizeof(buf
), "%s@%s", service_name
, fqdn
);
428 DEBUG_printf(("cups_get_gss_creds: Looking up %s...\n", buf
));
431 token
.length
= strlen(buf
);
432 server_name
= GSS_C_NO_NAME
;
433 major_status
= gss_import_name(&minor_status
, &token
,
434 GSS_C_NT_HOSTBASED_SERVICE
,
437 if (GSS_ERROR(major_status
))
440 DEBUG_gss_printf(major_status
, minor_status
, "gss_import_name() failed");
446 return (server_name
);
448 #endif /* HAVE_GSSAPI */
452 * 'cups_local_auth()' - Get the local authorization certificate if
453 * available/applicable...
456 static int /* O - 0 if available */
457 /* 1 if not available */
459 cups_local_auth(http_t
*http
) /* I - HTTP connection to server */
461 #if defined(WIN32) || defined(__EMX__)
463 * Currently WIN32 and OS-2 do not support the CUPS server...
468 int pid
; /* Current process ID */
469 FILE *fp
; /* Certificate file */
470 char filename
[1024], /* Certificate filename */
471 certificate
[33];/* Certificate string */
472 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
473 # if defined(HAVE_AUTHORIZATION_H)
474 OSStatus status
; /* Status */
475 AuthorizationItem auth_right
; /* Authorization right */
476 AuthorizationRights auth_rights
; /* Authorization rights */
477 AuthorizationFlags auth_flags
; /* Authorization flags */
478 AuthorizationExternalForm auth_extrn
; /* Authorization ref external */
479 char auth_key
[1024]; /* Buffer */
480 char buffer
[1024]; /* Buffer */
481 # endif /* HAVE_AUTHORIZATION_H */
484 DEBUG_printf(("cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"\n",
485 http
, httpAddrString(http
->hostaddr
, filename
, sizeof(filename
)), http
->hostname
));
488 * See if we are accessing localhost...
491 if (!httpAddrLocalhost(http
->hostaddr
) &&
492 strcasecmp(http
->hostname
, "localhost") != 0)
494 DEBUG_puts("cups_local_auth: Not a local connection!");
498 # if defined(HAVE_AUTHORIZATION_H)
500 * Delete any previous authorization reference...
505 AuthorizationFree(http
->auth_ref
, kAuthorizationFlagDefaults
);
506 http
->auth_ref
= NULL
;
509 if (httpGetSubField2(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "authkey",
510 auth_key
, sizeof(auth_key
)))
512 status
= AuthorizationCreate(NULL
, kAuthorizationEmptyEnvironment
,
513 kAuthorizationFlagDefaults
, &http
->auth_ref
);
514 if (status
!= errAuthorizationSuccess
)
516 DEBUG_printf(("cups_local_auth: AuthorizationCreate() returned %d (%s)\n",
517 (int)status
, cssmErrorString(status
)));
521 auth_right
.name
= auth_key
;
522 auth_right
.valueLength
= 0;
523 auth_right
.value
= NULL
;
524 auth_right
.flags
= 0;
526 auth_rights
.count
= 1;
527 auth_rights
.items
= &auth_right
;
529 auth_flags
= kAuthorizationFlagDefaults
|
530 kAuthorizationFlagPreAuthorize
|
531 kAuthorizationFlagInteractionAllowed
|
532 kAuthorizationFlagExtendRights
;
534 status
= AuthorizationCopyRights(http
->auth_ref
, &auth_rights
,
535 kAuthorizationEmptyEnvironment
,
537 if (status
== errAuthorizationSuccess
)
538 status
= AuthorizationMakeExternalForm(http
->auth_ref
, &auth_extrn
);
540 if (status
== errAuthorizationSuccess
)
543 * Set the authorization string and return...
546 httpEncode64_2(buffer
, sizeof(buffer
), (void *)&auth_extrn
,
549 httpSetAuthString(http
, "AuthRef", buffer
);
551 DEBUG_printf(("cups_local_auth: Returning authstring = \"%s\"\n",
555 else if (status
== errAuthorizationCanceled
)
558 DEBUG_printf(("cups_local_auth: AuthorizationCopyRights() returned %d (%s)\n",
559 (int)status
, cssmErrorString(status
)));
562 * Fall through to try certificates...
565 # endif /* HAVE_AUTHORIZATION_H */
568 * Try opening a certificate file for this PID. If that fails,
569 * try the root certificate...
573 snprintf(filename
, sizeof(filename
), "%s/certs/%d", cg
->cups_statedir
, pid
);
574 if ((fp
= fopen(filename
, "r")) == NULL
&& pid
> 0)
576 DEBUG_printf(("cups_local_auth: Unable to open file %s: %s\n",
577 filename
, strerror(errno
)));
581 * If local certificate authentication isn't available for this PID,
582 * check if we need Kerberos authentication...
585 if (!strncmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], "Negotiate", 9))
588 * Yes, don't try the root certificate...
593 #endif /* HAVE_GSSAPI */
595 snprintf(filename
, sizeof(filename
), "%s/certs/0", cg
->cups_statedir
);
596 fp
= fopen(filename
, "r");
602 * Read the certificate from the file...
605 fgets(certificate
, sizeof(certificate
), fp
);
609 * Set the authorization string and return...
612 httpSetAuthString(http
, "Local", certificate
);
614 DEBUG_printf(("cups_local_auth: Returning authstring = \"%s\"\n",
620 # if defined(SO_PEERCRED) && defined(AF_LOCAL)
622 * See if we can authenticate using the peer credentials provided over a
623 * domain socket; if so, specify "PeerCred username" as the authentication
627 if (http
->hostaddr
->addr
.sa_family
== AF_LOCAL
&&
628 !getenv("GATEWAY_INTERFACE")) /* Not via CGI programs... */
631 * Verify that the current cupsUser() matches the current UID...
634 struct passwd
*pwd
; /* Password information */
635 const char *username
; /* Current username */
637 username
= cupsUser();
639 if ((pwd
= getpwnam(username
)) != NULL
&& pwd
->pw_uid
== getuid())
641 httpSetAuthString(http
, "PeerCred", username
);
643 DEBUG_printf(("cups_local_auth: Returning authstring = \"%s\"\n",
649 # endif /* SO_PEERCRED && AF_LOCAL */
652 #endif /* WIN32 || __EMX__ */
657 * End of "$Id: auth.c 6722 2007-07-25 17:19:09Z mike $".