2 * "$Id: auth.c 6253 2007-02-10 18:48:40Z mike $"
4 * Authentication functions for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2007 by Easy Software Products.
8 * This file contains Kerberos support code, copyright 2006 by
11 * These coded instructions, statements, and computer programs are the
12 * property of Easy Software Products and are protected by Federal
13 * copyright law. Distribution and use rights are outlined in the file
14 * "LICENSE.txt" which should have been included with this file. If this
15 * file is missing or damaged please contact Easy Software Products
18 * Attn: CUPS Licensing Information
19 * Easy Software Products
20 * 44141 Airport View Drive, Suite 204
21 * Hollywood, Maryland 20636 USA
23 * Voice: (301) 373-9600
24 * EMail: cups-info@cups.org
25 * WWW: http://www.cups.org
27 * This file is subject to the Apple OS-Developed Software exception.
31 * cupsDoAuthentication() - Authenticate a request.
32 * DEBUG_gss_printf() - Show debug error messages from GSSAPI...
33 * cups_get_gss_creds() - Get CUPS service credentials for authentication.
34 * cups_local_auth() - Get the local authorization certificate if
35 * available/applicable...
39 * Include necessary headers...
49 #if defined(WIN32) || defined(__EMX__)
53 #endif /* WIN32 || __EMX__ */
55 #if HAVE_AUTHORIZATION_H
56 # include <Security/Authorization.h>
57 # ifdef HAVE_SECBASEPRIV_H
58 # include <Security/SecBasePriv.h>
60 extern const char *cssmErrorString(int error
);
61 # endif /* HAVE_SECBASEPRIV_H */
62 #endif /* HAVE_AUTHORIZATION_H */
71 static void DEBUG_gss_printf(OM_uint32 major_status
, OM_uint32 minor_status
,
74 static gss_name_t
cups_get_gss_creds(http_t
*http
, const char *service_name
);
75 #endif /* HAVE_GSSAPI */
76 static int cups_local_auth(http_t
*http
);
80 * 'cupsDoAuthentication()' - Authenticate a request.
82 * This function should be called in response to a HTTP_UNAUTHORIZED
83 * status, prior to resubmitting your request.
88 int /* O - 0 on success, -1 on error */
89 cupsDoAuthentication(http_t
*http
, /* I - HTTP connection to server */
90 const char *method
,/* I - Request method (GET, POST, PUT) */
92 /* I - Resource path */
94 const char *password
; /* Password string */
95 char prompt
[1024], /* Prompt for user */
96 realm
[HTTP_MAX_VALUE
], /* realm="xyz" string */
97 nonce
[HTTP_MAX_VALUE
], /* nonce="xyz" string */
98 encode
[2048]; /* Encoded username:password */
99 int localauth
; /* Local authentication result */
100 _cups_globals_t
*cg
; /* Global data */
103 DEBUG_printf(("cupsDoAuthentication(http=%p, method=\"%s\", resource=\"%s\")\n",
104 http
, method
, resource
));
105 DEBUG_printf(("cupsDoAuthentication: digest_tries=%d, userpass=\"%s\"\n",
106 http
->digest_tries
, http
->userpass
));
107 DEBUG_printf(("cupsDoAuthentication: WWW-Authenticate=\"%s\"\n",
108 httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
)));
111 * Clear the current authentication string...
114 http
->_authstring
[0] = '\0';
116 if (http
->authstring
&& http
->authstring
!= http
->_authstring
)
117 free(http
->authstring
);
119 http
->authstring
= http
->_authstring
;
122 * See if we can do local authentication...
125 if (http
->digest_tries
< 3)
127 if ((localauth
= cups_local_auth(http
)) == 0)
129 DEBUG_printf(("cupsDoAuthentication: authstring=\"%s\"\n",
132 if (http
->status
== HTTP_UNAUTHORIZED
)
133 http
->digest_tries
++;
137 else if (localauth
== -1)
138 return (-1); /* Error or canceled */
142 * Nope, see if we should retry the current username:password...
145 if ((http
->digest_tries
> 1 || !http
->userpass
[0]) &&
146 strncmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], "Negotiate", 9))
149 * Nope - get a new password from the user...
154 if (!cg
->lang_default
)
155 cg
->lang_default
= cupsLangDefault();
157 snprintf(prompt
, sizeof(prompt
),
158 _cupsLangString(cg
->lang_default
, _("Password for %s on %s? ")),
160 http
->hostname
[0] == '/' ? "localhost" : http
->hostname
);
162 http
->digest_tries
= strncasecmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
],
164 http
->userpass
[0] = '\0';
166 if ((password
= cupsGetPassword(prompt
)) == NULL
)
172 snprintf(http
->userpass
, sizeof(http
->userpass
), "%s:%s", cupsUser(),
175 else if (http
->status
== HTTP_UNAUTHORIZED
)
176 http
->digest_tries
++;
179 * Got a password; encode it for the server...
182 if (!strncmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], "Negotiate", 9))
186 * Kerberos authentication...
189 OM_uint32 minor_status
, /* Minor status code */
190 major_status
; /* Major status code */
191 gss_buffer_desc output_token
= GSS_C_EMPTY_BUFFER
,
193 input_token
= GSS_C_EMPTY_BUFFER
;
195 char *gss_service_name
;
196 /* GSS service name */
197 const char *authorization
;
198 /* Pointer into Authorization string */
201 if (http
->gssname
== GSS_C_NO_NAME
)
203 if ((gss_service_name
= getenv("CUPS_GSSSERVICENAME")) == NULL
)
204 gss_service_name
= CUPS_DEFAULT_GSSSERVICENAME
;
206 DEBUG_puts("cupsDoAuthentication: GSS service name set via environment");
208 http
->gssname
= cups_get_gss_creds(http
, gss_service_name
);
212 * Find the start of the Kerberos input token...
215 authorization
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
218 while (*authorization
&& isspace(*authorization
& 255))
224 * For SPNEGO, this is where we'll feed the server's authorization data
225 * back into gss via input_token...
230 if (http
->gssctx
!= GSS_C_NO_CONTEXT
)
232 major_status
= gss_delete_sec_context(&minor_status
, &http
->gssctx
,
234 http
->gssctx
= GSS_C_NO_CONTEXT
;
238 major_status
= gss_init_sec_context(&minor_status
, GSS_C_NO_CREDENTIAL
,
240 http
->gssname
, http
->gssmech
,
241 GSS_C_MUTUAL_FLAG
, GSS_C_INDEFINITE
,
242 GSS_C_NO_CHANNEL_BINDINGS
,
243 &input_token
, &http
->gssmech
,
244 &output_token
, NULL
, NULL
);
246 if (input_token
.value
)
247 free(input_token
.value
);
249 if (GSS_ERROR(major_status
))
252 DEBUG_gss_printf(major_status
, minor_status
,
253 "Unable to initialise security context");
259 if (major_status
== GSS_S_CONTINUE_NEEDED
)
260 DEBUG_gss_printf(major_status
, minor_status
, "Continuation needed!");
263 if (output_token
.length
)
265 httpEncode64_2(encode
, sizeof(encode
), output_token
.value
,
266 output_token
.length
);
268 http
->authstring
= malloc(strlen(encode
) + 11);
269 sprintf(http
->authstring
, "Negotiate %s", encode
); /* Safe because allocated */
271 major_status
= gss_release_buffer(&minor_status
, &output_token
);
275 * Copy back what we can to _authstring for backwards compatibility...
278 strlcpy(http
->_authstring
, http
->authstring
, sizeof(http
->_authstring
));
279 #endif /* HAVE_GSSAPI */
281 else if (strncmp(http
->fields
[HTTP_FIELD_WWW_AUTHENTICATE
], "Digest", 6))
284 * Basic authentication...
287 httpEncode64_2(encode
, sizeof(encode
), http
->userpass
,
288 (int)strlen(http
->userpass
));
289 snprintf(http
->_authstring
, sizeof(http
->_authstring
), "Basic %s", encode
);
294 * Digest authentication...
297 httpGetSubField(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "realm", realm
);
298 httpGetSubField(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "nonce", nonce
);
300 httpMD5(cupsUser(), realm
, strchr(http
->userpass
, ':') + 1, encode
);
301 httpMD5Final(nonce
, method
, resource
, encode
);
302 snprintf(http
->_authstring
, sizeof(http
->_authstring
),
303 "Digest username=\"%s\", realm=\"%s\", nonce=\"%s\", "
304 "uri=\"%s\", response=\"%s\"", cupsUser(), realm
, nonce
,
308 DEBUG_printf(("cupsDoAuthentication: authstring=\"%s\"\n", http
->authstring
));
317 * 'DEBUG_gss_printf()' - Show debug error messages from GSSAPI...
321 DEBUG_gss_printf(OM_uint32 major_status
,/* I - Major status code */
322 OM_uint32 minor_status
,/* I - Minor status code */
323 const char *message
) /* I - Prefix for error message */
325 OM_uint32 err_major_status
, /* Major status code for display */
326 err_minor_status
; /* Minor status code for display */
327 OM_uint32 msg_ctx
; /* Message context */
328 gss_buffer_desc major_status_string
= GSS_C_EMPTY_BUFFER
,
329 /* Major status message */
330 minor_status_string
= GSS_C_EMPTY_BUFFER
;
331 /* Minor status message */
335 err_major_status
= gss_display_status(&err_minor_status
,
340 &major_status_string
);
342 if (!GSS_ERROR(err_major_status
))
343 err_major_status
= gss_display_status(&err_minor_status
,
348 &minor_status_string
);
350 printf("%s: %s, %s\n", message
, (char *)major_status_string
.value
,
351 (char *)minor_status_string
.value
);
353 gss_release_buffer(&err_minor_status
, &major_status_string
);
354 gss_release_buffer(&err_minor_status
, &minor_status_string
);
360 * 'cups_get_gss_creds()' - Get CUPS service credentials for authentication.
363 static gss_name_t
/* O - Server name */
365 http_t
*http
, /* I - Connection to server */
366 const char *service_name
) /* I - Service name */
368 gss_buffer_desc token
= GSS_C_EMPTY_BUFFER
;
370 OM_uint32 major_status
, /* Major status code */
371 minor_status
; /* Minor status code */
372 gss_name_t server_name
; /* Server name */
373 char buf
[1024], /* Name buffer */
374 fqdn
[HTTP_MAX_URI
]; /* Server name buffer */
378 * Get a server name we can use for authentication purposes...
381 snprintf(buf
, sizeof(buf
), "%s@%s", service_name
,
382 httpGetHostname(http
, fqdn
, sizeof(fqdn
)));
385 token
.length
= strlen(buf
);
386 server_name
= GSS_C_NO_NAME
;
387 major_status
= gss_import_name(&minor_status
, &token
,
388 GSS_C_NT_HOSTBASED_SERVICE
,
391 if (GSS_ERROR(major_status
))
394 DEBUG_gss_printf(major_status
, minor_status
, "gss_import_name() failed");
400 return (server_name
);
402 #endif /* HAVE_GSSAPI */
406 * 'cups_local_auth()' - Get the local authorization certificate if
407 * available/applicable...
410 static int /* O - 0 if available */
411 /* 1 if not available */
413 cups_local_auth(http_t
*http
) /* I - HTTP connection to server */
415 #if defined(WIN32) || defined(__EMX__)
417 * Currently WIN32 and OS-2 do not support the CUPS server...
422 int pid
; /* Current process ID */
423 FILE *fp
; /* Certificate file */
424 char filename
[1024], /* Certificate filename */
425 certificate
[33];/* Certificate string */
426 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
427 #if defined(HAVE_AUTHORIZATION_H)
428 OSStatus status
; /* Status */
429 AuthorizationItem auth_right
; /* Authorization right */
430 AuthorizationRights auth_rights
; /* Authorization rights */
431 AuthorizationFlags auth_flags
; /* Authorization flags */
432 AuthorizationExternalForm auth_extrn
; /* Authorization ref external */
433 char auth_key
[1024]; /* Buffer */
434 char buffer
[1024]; /* Buffer */
435 #endif /* HAVE_AUTHORIZATION_H */
438 DEBUG_printf(("cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"\n",
439 http
, httpAddrString(http
->hostaddr
, filename
, sizeof(filename
)), http
->hostname
));
442 * See if we are accessing localhost...
445 if (!httpAddrLocalhost(http
->hostaddr
) &&
446 strcasecmp(http
->hostname
, "localhost") != 0)
448 DEBUG_puts("cups_local_auth: Not a local connection!");
452 #if defined(HAVE_AUTHORIZATION_H)
454 * Delete any previous authorization reference...
459 AuthorizationFree(cg
->auth_ref
, kAuthorizationFlagDefaults
);
463 if (httpGetSubField2(http
, HTTP_FIELD_WWW_AUTHENTICATE
, "authkey",
464 auth_key
, sizeof(auth_key
)))
466 status
= AuthorizationCreate(NULL
, kAuthorizationEmptyEnvironment
,
467 kAuthorizationFlagDefaults
, &cg
->auth_ref
);
468 if (status
!= errAuthorizationSuccess
)
470 DEBUG_printf(("cups_local_auth: AuthorizationCreate() returned %d (%s)\n",
471 (int)status
, cssmErrorString(status
)));
475 auth_right
.name
= auth_key
;
476 auth_right
.valueLength
= 0;
477 auth_right
.value
= NULL
;
478 auth_right
.flags
= 0;
480 auth_rights
.count
= 1;
481 auth_rights
.items
= &auth_right
;
483 auth_flags
= kAuthorizationFlagDefaults
|
484 kAuthorizationFlagPreAuthorize
|
485 kAuthorizationFlagInteractionAllowed
|
486 kAuthorizationFlagExtendRights
;
488 status
= AuthorizationCopyRights(cg
->auth_ref
, &auth_rights
,
489 kAuthorizationEmptyEnvironment
,
491 if (status
== errAuthorizationSuccess
)
492 status
= AuthorizationMakeExternalForm(cg
->auth_ref
, &auth_extrn
);
494 if (status
== errAuthorizationSuccess
)
497 * Set the authorization string and return...
500 httpEncode64_2(buffer
, sizeof(buffer
), (void *)&auth_extrn
,
503 http
->authstring
= malloc(strlen(buffer
) + 9);
504 sprintf(http
->authstring
, "AuthRef %s", buffer
);
506 /* Copy back to _authstring for backwards compatibility */
507 strlcpy(http
->_authstring
, http
->authstring
, sizeof(http
->_authstring
));
509 DEBUG_printf(("cups_local_auth: Returning authstring = \"%s\"\n",
513 else if (status
== errAuthorizationCanceled
)
516 DEBUG_printf(("cups_local_auth: AuthorizationCopyRights() returned %d (%s)\n",
517 (int)status
, cssmErrorString(status
)));
520 * Fall through to try certificates...
523 #endif /* HAVE_AUTHORIZATION_H */
526 * Try opening a certificate file for this PID. If that fails,
527 * try the root certificate...
531 snprintf(filename
, sizeof(filename
), "%s/certs/%d", cg
->cups_statedir
, pid
);
532 if ((fp
= fopen(filename
, "r")) == NULL
&& pid
> 0)
534 DEBUG_printf(("cups_local_auth: Unable to open file %s: %s\n",
535 filename
, strerror(errno
)));
537 snprintf(filename
, sizeof(filename
), "%s/certs/0", cg
->cups_statedir
);
538 fp
= fopen(filename
, "r");
544 * Read the certificate from the file...
547 fgets(certificate
, sizeof(certificate
), fp
);
551 * Set the authorization string and return...
554 http
->authstring
= malloc(strlen(certificate
) + 10);
555 sprintf(http
->authstring
, "Local %s", certificate
);
557 /* Copy back to _authstring for backwards compatibility */
558 strlcpy(http
->_authstring
, http
->authstring
, sizeof(http
->_authstring
));
560 DEBUG_printf(("cups_local_auth: Returning authstring = \"%s\"\n",
567 #endif /* WIN32 || __EMX__ */
572 * End of "$Id: auth.c 6253 2007-02-10 18:48:40Z mike $".