]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
f2d18633 | 2 | * "$Id$" |
ef416fc2 | 3 | * |
71e16022 | 4 | * Authentication functions for CUPS. |
ef416fc2 | 5 | * |
cb7f98ee | 6 | * Copyright 2007-2013 by Apple Inc. |
b86bc4cf | 7 | * Copyright 1997-2007 by Easy Software Products. |
ef416fc2 | 8 | * |
f7deaa1a | 9 | * This file contains Kerberos support code, copyright 2006 by |
10 | * Jelmer Vernooij. | |
11 | * | |
ef416fc2 | 12 | * These coded instructions, statements, and computer programs are the |
bc44d920 | 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/". | |
ef416fc2 | 17 | * |
18 | * This file is subject to the Apple OS-Developed Software exception. | |
19 | * | |
20 | * Contents: | |
21 | * | |
eac3a0a0 MS |
22 | * cupsDoAuthentication() - Authenticate a request. |
23 | * _cupsSetNegotiateAuthString() - Set the Kerberos authentication string. | |
24 | * cups_gss_acquire() - Kerberos credentials callback. | |
25 | * cups_gss_getname() - Get CUPS service credentials for | |
26 | * authentication. | |
27 | * cups_gss_printf() - Show debug error messages from GSSAPI. | |
28 | * cups_local_auth() - Get the local authorization certificate if | |
29 | * available/applicable. | |
ef416fc2 | 30 | */ |
31 | ||
32 | /* | |
33 | * Include necessary headers... | |
34 | */ | |
35 | ||
71e16022 | 36 | #include "cups-private.h" |
ef416fc2 | 37 | #include <fcntl.h> |
38 | #include <sys/stat.h> | |
39 | #if defined(WIN32) || defined(__EMX__) | |
40 | # include <io.h> | |
41 | #else | |
42 | # include <unistd.h> | |
43 | #endif /* WIN32 || __EMX__ */ | |
44 | ||
f7deaa1a | 45 | #if HAVE_AUTHORIZATION_H |
46 | # include <Security/Authorization.h> | |
47 | # ifdef HAVE_SECBASEPRIV_H | |
48 | # include <Security/SecBasePriv.h> | |
49 | # else | |
50 | extern const char *cssmErrorString(int error); | |
51 | # endif /* HAVE_SECBASEPRIV_H */ | |
52 | #endif /* HAVE_AUTHORIZATION_H */ | |
53 | ||
bc44d920 | 54 | #if defined(SO_PEERCRED) && defined(AF_LOCAL) |
55 | # include <pwd.h> | |
56 | #endif /* SO_PEERCRED && AF_LOCAL */ | |
57 | ||
ef416fc2 | 58 | |
59 | /* | |
60 | * Local functions... | |
61 | */ | |
62 | ||
f7deaa1a | 63 | #ifdef HAVE_GSSAPI |
eac3a0a0 MS |
64 | # ifdef HAVE_GSS_ACQUIRE_CRED_EX_F |
65 | # ifdef HAVE_GSS_GSSAPI_SPI_H | |
66 | # include <GSS/gssapi_spi.h> | |
67 | # else | |
a2326b5b MS |
68 | # define GSS_AUTH_IDENTITY_TYPE_1 1 |
69 | # define gss_acquire_cred_ex_f __ApplePrivate_gss_acquire_cred_ex_f | |
eac3a0a0 MS |
70 | typedef struct gss_auth_identity |
71 | { | |
72 | uint32_t type; | |
73 | uint32_t flags; | |
74 | char *username; | |
75 | char *realm; | |
76 | char *password; | |
77 | gss_buffer_t *credentialsRef; | |
78 | } gss_auth_identity_desc; | |
79 | extern OM_uint32 gss_acquire_cred_ex_f(gss_status_id_t, const gss_name_t, | |
80 | OM_uint32, OM_uint32, const gss_OID, | |
81 | gss_cred_usage_t, gss_auth_identity_t, | |
82 | void *, void (*)(void *, OM_uint32, | |
83 | gss_status_id_t, | |
84 | gss_cred_id_t, | |
85 | gss_OID_set, | |
86 | OM_uint32)); | |
87 | # endif /* HAVE_GSS_GSSAPI_SPI_H */ | |
88 | # include <dispatch/dispatch.h> | |
89 | typedef struct _cups_gss_acquire_s /* Acquire callback data */ | |
90 | { | |
91 | dispatch_semaphore_t sem; /* Synchronization semaphore */ | |
92 | OM_uint32 major; /* Returned status code */ | |
93 | gss_cred_id_t creds; /* Returned credentials */ | |
94 | } _cups_gss_acquire_t; | |
95 | ||
96 | static void cups_gss_acquire(void *ctx, OM_uint32 major, | |
97 | gss_status_id_t status, | |
98 | gss_cred_id_t creds, gss_OID_set oids, | |
99 | OM_uint32 time_rec); | |
100 | # endif /* HAVE_GSS_ACQUIRE_CRED_EX_F */ | |
101 | static gss_name_t cups_gss_getname(http_t *http, const char *service_name); | |
f7deaa1a | 102 | # ifdef DEBUG |
e07d4801 MS |
103 | static void cups_gss_printf(OM_uint32 major_status, OM_uint32 minor_status, |
104 | const char *message); | |
355e94dc | 105 | # else |
e07d4801 MS |
106 | # define cups_gss_printf(major, minor, message) |
107 | # endif /* DEBUG */ | |
f7deaa1a | 108 | #endif /* HAVE_GSSAPI */ |
ef416fc2 | 109 | static int cups_local_auth(http_t *http); |
110 | ||
111 | ||
112 | /* | |
113 | * 'cupsDoAuthentication()' - Authenticate a request. | |
114 | * | |
cb7f98ee | 115 | * This function should be called in response to a @code HTTP_STATUS_UNAUTHORIZED@ |
ef416fc2 | 116 | * status, prior to resubmitting your request. |
117 | * | |
f3c17241 | 118 | * @since CUPS 1.1.20/OS X 10.4@ |
ef416fc2 | 119 | */ |
120 | ||
121 | int /* O - 0 on success, -1 on error */ | |
f11a948a MS |
122 | cupsDoAuthentication( |
123 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
124 | const char *method, /* I - Request method ("GET", "POST", "PUT") */ | |
125 | const char *resource) /* I - Resource path */ | |
ef416fc2 | 126 | { |
3e7fe0ca MS |
127 | const char *password, /* Password string */ |
128 | *www_auth; /* WWW-Authenticate header */ | |
ef416fc2 | 129 | char prompt[1024], /* Prompt for user */ |
130 | realm[HTTP_MAX_VALUE], /* realm="xyz" string */ | |
5a738aea | 131 | nonce[HTTP_MAX_VALUE]; /* nonce="xyz" string */ |
f7deaa1a | 132 | int localauth; /* Local authentication result */ |
b86bc4cf | 133 | _cups_globals_t *cg; /* Global data */ |
ef416fc2 | 134 | |
135 | ||
e07d4801 | 136 | DEBUG_printf(("cupsDoAuthentication(http=%p, method=\"%s\", resource=\"%s\")", |
ef416fc2 | 137 | http, method, resource)); |
ef416fc2 | 138 | |
f11a948a MS |
139 | if (!http) |
140 | http = _cupsConnect(); | |
141 | ||
142 | if (!http || !method || !resource) | |
143 | return (-1); | |
144 | ||
5a6b583a MS |
145 | DEBUG_printf(("2cupsDoAuthentication: digest_tries=%d, userpass=\"%s\"", |
146 | http->digest_tries, http->userpass)); | |
147 | DEBUG_printf(("2cupsDoAuthentication: WWW-Authenticate=\"%s\"", | |
148 | httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE))); | |
149 | ||
ef416fc2 | 150 | /* |
151 | * Clear the current authentication string... | |
152 | */ | |
153 | ||
355e94dc | 154 | httpSetAuthString(http, NULL, NULL); |
ef416fc2 | 155 | |
156 | /* | |
157 | * See if we can do local authentication... | |
158 | */ | |
159 | ||
f7deaa1a | 160 | if (http->digest_tries < 3) |
ef416fc2 | 161 | { |
f7deaa1a | 162 | if ((localauth = cups_local_auth(http)) == 0) |
163 | { | |
e07d4801 | 164 | DEBUG_printf(("2cupsDoAuthentication: authstring=\"%s\"", |
f7deaa1a | 165 | http->authstring)); |
f14324a7 | 166 | |
cb7f98ee | 167 | if (http->status == HTTP_STATUS_UNAUTHORIZED) |
f7deaa1a | 168 | http->digest_tries ++; |
f14324a7 | 169 | |
f7deaa1a | 170 | return (0); |
171 | } | |
172 | else if (localauth == -1) | |
f11a948a | 173 | { |
cb7f98ee | 174 | http->status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED; |
f7deaa1a | 175 | return (-1); /* Error or canceled */ |
f11a948a | 176 | } |
ef416fc2 | 177 | } |
178 | ||
179 | /* | |
180 | * Nope, see if we should retry the current username:password... | |
181 | */ | |
182 | ||
3e7fe0ca MS |
183 | www_auth = http->fields[HTTP_FIELD_WWW_AUTHENTICATE]; |
184 | ||
f7deaa1a | 185 | if ((http->digest_tries > 1 || !http->userpass[0]) && |
3e7fe0ca MS |
186 | (!_cups_strncasecmp(www_auth, "Basic", 5) || |
187 | !_cups_strncasecmp(www_auth, "Digest", 6))) | |
ef416fc2 | 188 | { |
189 | /* | |
190 | * Nope - get a new password from the user... | |
191 | */ | |
192 | ||
5a9febac MS |
193 | char default_username[HTTP_MAX_VALUE]; |
194 | /* Default username */ | |
195 | ||
b86bc4cf | 196 | cg = _cupsGlobals(); |
197 | ||
198 | if (!cg->lang_default) | |
199 | cg->lang_default = cupsLangDefault(); | |
200 | ||
5a9febac MS |
201 | if (httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE, "username", |
202 | default_username)) | |
203 | cupsSetUser(default_username); | |
204 | ||
b86bc4cf | 205 | snprintf(prompt, sizeof(prompt), |
206 | _cupsLangString(cg->lang_default, _("Password for %s on %s? ")), | |
207 | cupsUser(), | |
208 | http->hostname[0] == '/' ? "localhost" : http->hostname); | |
ef416fc2 | 209 | |
3e7fe0ca | 210 | http->digest_tries = _cups_strncasecmp(www_auth, "Digest", 6) != 0; |
ef416fc2 | 211 | http->userpass[0] = '\0'; |
212 | ||
f11a948a MS |
213 | if ((password = cupsGetPassword2(prompt, http, method, resource)) == NULL) |
214 | { | |
cb7f98ee | 215 | http->status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED; |
ef416fc2 | 216 | return (-1); |
f11a948a | 217 | } |
ef416fc2 | 218 | |
ef416fc2 | 219 | snprintf(http->userpass, sizeof(http->userpass), "%s:%s", cupsUser(), |
220 | password); | |
221 | } | |
cb7f98ee | 222 | else if (http->status == HTTP_STATUS_UNAUTHORIZED) |
ef416fc2 | 223 | http->digest_tries ++; |
224 | ||
cb7f98ee | 225 | if (http->status == HTTP_STATUS_UNAUTHORIZED && http->digest_tries >= 3) |
5a6b583a MS |
226 | { |
227 | DEBUG_printf(("1cupsDoAuthentication: Too many authentication tries (%d)", | |
228 | http->digest_tries)); | |
229 | ||
cb7f98ee | 230 | http->status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED; |
5a6b583a MS |
231 | return (-1); |
232 | } | |
233 | ||
ef416fc2 | 234 | /* |
235 | * Got a password; encode it for the server... | |
236 | */ | |
237 | ||
f14324a7 | 238 | #ifdef HAVE_GSSAPI |
3e7fe0ca | 239 | if (!_cups_strncasecmp(www_auth, "Negotiate", 9)) |
f7deaa1a | 240 | { |
f7deaa1a | 241 | /* |
242 | * Kerberos authentication... | |
243 | */ | |
244 | ||
eac3a0a0 | 245 | if (_cupsSetNegotiateAuthString(http, method, resource)) |
f7deaa1a | 246 | { |
cb7f98ee | 247 | http->status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED; |
f7deaa1a | 248 | return (-1); |
249 | } | |
f7deaa1a | 250 | } |
f14324a7 MS |
251 | else |
252 | #endif /* HAVE_GSSAPI */ | |
3e7fe0ca | 253 | if (!_cups_strncasecmp(www_auth, "Basic", 5)) |
ef416fc2 | 254 | { |
255 | /* | |
256 | * Basic authentication... | |
257 | */ | |
258 | ||
5a738aea MS |
259 | char encode[256]; /* Base64 buffer */ |
260 | ||
261 | ||
ef416fc2 | 262 | httpEncode64_2(encode, sizeof(encode), http->userpass, |
b86bc4cf | 263 | (int)strlen(http->userpass)); |
355e94dc | 264 | httpSetAuthString(http, "Basic", encode); |
ef416fc2 | 265 | } |
3e7fe0ca | 266 | else if (!_cups_strncasecmp(www_auth, "Digest", 6)) |
ef416fc2 | 267 | { |
268 | /* | |
269 | * Digest authentication... | |
270 | */ | |
271 | ||
5a738aea MS |
272 | char encode[33], /* MD5 buffer */ |
273 | digest[1024]; /* Digest auth data */ | |
355e94dc MS |
274 | |
275 | ||
ef416fc2 | 276 | httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE, "realm", realm); |
277 | httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE, "nonce", nonce); | |
278 | ||
279 | httpMD5(cupsUser(), realm, strchr(http->userpass, ':') + 1, encode); | |
280 | httpMD5Final(nonce, method, resource, encode); | |
355e94dc MS |
281 | snprintf(digest, sizeof(digest), |
282 | "username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", " | |
283 | "response=\"%s\"", cupsUser(), realm, nonce, resource, encode); | |
284 | httpSetAuthString(http, "Digest", digest); | |
ef416fc2 | 285 | } |
f14324a7 MS |
286 | else |
287 | { | |
288 | DEBUG_printf(("1cupsDoAuthentication: Unknown auth type: \"%s\"", | |
3e7fe0ca | 289 | www_auth)); |
cb7f98ee | 290 | http->status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED; |
f14324a7 MS |
291 | return (-1); |
292 | } | |
ef416fc2 | 293 | |
e07d4801 | 294 | DEBUG_printf(("1cupsDoAuthentication: authstring=\"%s\"", http->authstring)); |
ef416fc2 | 295 | |
296 | return (0); | |
297 | } | |
298 | ||
299 | ||
f7deaa1a | 300 | #ifdef HAVE_GSSAPI |
f14324a7 MS |
301 | /* |
302 | * '_cupsSetNegotiateAuthString()' - Set the Kerberos authentication string. | |
303 | */ | |
304 | ||
305 | int /* O - 0 on success, -1 on error */ | |
306 | _cupsSetNegotiateAuthString( | |
eac3a0a0 MS |
307 | http_t *http, /* I - Connection to server */ |
308 | const char *method, /* I - Request method ("GET", "POST", "PUT") */ | |
309 | const char *resource) /* I - Resource path */ | |
f14324a7 MS |
310 | { |
311 | OM_uint32 minor_status, /* Minor status code */ | |
312 | major_status; /* Major status code */ | |
313 | gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER; | |
07ed0e9a | 314 | /* Output token */ |
f14324a7 MS |
315 | |
316 | ||
321d8d57 MS |
317 | (void)method; |
318 | (void)resource; | |
319 | ||
f14324a7 MS |
320 | # ifdef __APPLE__ |
321 | /* | |
322 | * If the weak-linked GSSAPI/Kerberos library is not present, don't try | |
323 | * to use it... | |
324 | */ | |
325 | ||
326 | if (gss_init_sec_context == NULL) | |
327 | { | |
328 | DEBUG_puts("1_cupsSetNegotiateAuthString: Weak-linked GSSAPI/Kerberos " | |
329 | "framework is not present"); | |
330 | return (-1); | |
331 | } | |
332 | # endif /* __APPLE__ */ | |
333 | ||
334 | if (http->gssname == GSS_C_NO_NAME) | |
335 | { | |
eac3a0a0 | 336 | http->gssname = cups_gss_getname(http, _cupsGSSServiceName()); |
f14324a7 MS |
337 | } |
338 | ||
339 | if (http->gssctx != GSS_C_NO_CONTEXT) | |
340 | { | |
341 | gss_delete_sec_context(&minor_status, &http->gssctx, GSS_C_NO_BUFFER); | |
342 | http->gssctx = GSS_C_NO_CONTEXT; | |
343 | } | |
344 | ||
eac3a0a0 MS |
345 | major_status = gss_init_sec_context(&minor_status, GSS_C_NO_CREDENTIAL, |
346 | &http->gssctx, | |
347 | http->gssname, http->gssmech, | |
348 | GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG, | |
349 | GSS_C_INDEFINITE, | |
350 | GSS_C_NO_CHANNEL_BINDINGS, | |
351 | GSS_C_NO_BUFFER, &http->gssmech, | |
352 | &output_token, NULL, NULL); | |
353 | ||
354 | #ifdef HAVE_GSS_ACQUIRE_CRED_EX_F | |
355 | if (major_status == GSS_S_NO_CRED) | |
356 | { | |
357 | /* | |
358 | * Ask the user for credentials... | |
359 | */ | |
360 | ||
361 | char prompt[1024], /* Prompt for user */ | |
362 | userbuf[256]; /* Kerberos username */ | |
363 | const char *username, /* Username string */ | |
364 | *password; /* Password string */ | |
365 | _cups_gss_acquire_t data; /* Callback data */ | |
366 | gss_auth_identity_desc identity; /* Kerberos user identity */ | |
367 | _cups_globals_t *cg = _cupsGlobals(); | |
368 | /* Per-thread global data */ | |
369 | ||
370 | if (!cg->lang_default) | |
371 | cg->lang_default = cupsLangDefault(); | |
372 | ||
373 | snprintf(prompt, sizeof(prompt), | |
374 | _cupsLangString(cg->lang_default, _("Password for %s on %s? ")), | |
f228370c | 375 | cupsUser(), http->gsshost); |
eac3a0a0 MS |
376 | |
377 | if ((password = cupsGetPassword2(prompt, http, method, resource)) == NULL) | |
378 | return (-1); | |
379 | ||
380 | /* | |
381 | * Try to acquire credentials... | |
382 | */ | |
383 | ||
384 | username = cupsUser(); | |
385 | if (!strchr(username, '@')) | |
386 | { | |
f228370c | 387 | snprintf(userbuf, sizeof(userbuf), "%s@%s", username, http->gsshost); |
eac3a0a0 MS |
388 | username = userbuf; |
389 | } | |
390 | ||
391 | identity.type = GSS_AUTH_IDENTITY_TYPE_1; | |
392 | identity.flags = 0; | |
393 | identity.username = (char *)username; | |
394 | identity.realm = (char *)""; | |
395 | identity.password = (char *)password; | |
396 | identity.credentialsRef = NULL; | |
397 | ||
398 | data.sem = dispatch_semaphore_create(0); | |
399 | data.major = 0; | |
400 | data.creds = NULL; | |
401 | ||
402 | if (data.sem) | |
403 | { | |
404 | major_status = gss_acquire_cred_ex_f(NULL, GSS_C_NO_NAME, 0, | |
405 | GSS_C_INDEFINITE, GSS_KRB5_MECHANISM, | |
406 | GSS_C_INITIATE, &identity, &data, | |
407 | cups_gss_acquire); | |
408 | ||
409 | if (major_status == GSS_S_COMPLETE) | |
410 | { | |
411 | dispatch_semaphore_wait(data.sem, DISPATCH_TIME_FOREVER); | |
412 | major_status = data.major; | |
413 | } | |
414 | ||
415 | dispatch_release(data.sem); | |
416 | ||
417 | if (major_status == GSS_S_COMPLETE) | |
418 | { | |
419 | OM_uint32 release_minor; /* Minor status from releasing creds */ | |
420 | ||
421 | major_status = gss_init_sec_context(&minor_status, data.creds, | |
422 | &http->gssctx, | |
423 | http->gssname, http->gssmech, | |
424 | GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG, | |
425 | GSS_C_INDEFINITE, | |
426 | GSS_C_NO_CHANNEL_BINDINGS, | |
427 | GSS_C_NO_BUFFER, &http->gssmech, | |
428 | &output_token, NULL, NULL); | |
429 | gss_release_cred(&release_minor, &data.creds); | |
430 | } | |
431 | } | |
432 | } | |
eac3a0a0 | 433 | #endif /* HAVE_GSS_ACQUIRED_CRED_EX_F */ |
f14324a7 MS |
434 | |
435 | if (GSS_ERROR(major_status)) | |
436 | { | |
437 | cups_gss_printf(major_status, minor_status, | |
438 | "_cupsSetNegotiateAuthString: Unable to initialize " | |
439 | "security context"); | |
440 | return (-1); | |
441 | } | |
442 | ||
22c9029b | 443 | #ifdef DEBUG |
eac3a0a0 | 444 | else if (major_status == GSS_S_CONTINUE_NEEDED) |
f14324a7 MS |
445 | cups_gss_printf(major_status, minor_status, |
446 | "_cupsSetNegotiateAuthString: Continuation needed!"); | |
22c9029b | 447 | #endif /* DEBUG */ |
f14324a7 MS |
448 | |
449 | if (output_token.length > 0 && output_token.length <= 65536) | |
450 | { | |
451 | /* | |
452 | * Allocate the authorization string since Windows KDCs can have | |
453 | * arbitrarily large credentials... | |
454 | */ | |
455 | ||
456 | int authsize = 10 + /* "Negotiate " */ | |
457 | output_token.length * 4 / 3 + 1 + /* Base64 */ | |
458 | 1; /* nul */ | |
459 | ||
460 | httpSetAuthString(http, NULL, NULL); | |
461 | ||
462 | if ((http->authstring = malloc(authsize)) == NULL) | |
463 | { | |
464 | http->authstring = http->_authstring; | |
465 | authsize = sizeof(http->_authstring); | |
466 | } | |
467 | ||
5a9febac | 468 | strlcpy(http->authstring, "Negotiate ", authsize); |
f14324a7 MS |
469 | httpEncode64_2(http->authstring + 10, authsize - 10, output_token.value, |
470 | output_token.length); | |
471 | ||
472 | gss_release_buffer(&minor_status, &output_token); | |
473 | } | |
474 | else | |
475 | { | |
476 | DEBUG_printf(("1_cupsSetNegotiateAuthString: Kerberos credentials too " | |
477 | "large - %d bytes!", (int)output_token.length)); | |
478 | gss_release_buffer(&minor_status, &output_token); | |
479 | ||
480 | return (-1); | |
481 | } | |
482 | ||
483 | return (0); | |
484 | } | |
485 | ||
486 | ||
eac3a0a0 | 487 | # ifdef HAVE_GSS_ACQUIRE_CRED_EX_F |
f7deaa1a | 488 | /* |
eac3a0a0 MS |
489 | * 'cups_gss_acquire()' - Kerberos credentials callback. |
490 | */ | |
491 | static void | |
492 | cups_gss_acquire( | |
493 | void *ctx, /* I - Caller context */ | |
494 | OM_uint32 major, /* I - Major error code */ | |
495 | gss_status_id_t status, /* I - Status (unused) */ | |
496 | gss_cred_id_t creds, /* I - Credentials (if any) */ | |
497 | gss_OID_set oids, /* I - Mechanism OIDs (unused) */ | |
498 | OM_uint32 time_rec) /* I - Timestamp (unused) */ | |
499 | { | |
500 | uint32_t min; /* Minor error code */ | |
501 | _cups_gss_acquire_t *data; /* Callback data */ | |
502 | ||
503 | ||
504 | (void)status; | |
505 | (void)time_rec; | |
506 | ||
507 | data = (_cups_gss_acquire_t *)ctx; | |
508 | data->major = major; | |
509 | data->creds = creds; | |
510 | ||
511 | gss_release_oid_set(&min, &oids); | |
512 | dispatch_semaphore_signal(data->sem); | |
513 | } | |
514 | # endif /* HAVE_GSS_ACQUIRE_CRED_EX_F */ | |
515 | ||
516 | ||
517 | /* | |
518 | * 'cups_gss_getname()' - Get CUPS service credentials for authentication. | |
f7deaa1a | 519 | */ |
520 | ||
521 | static gss_name_t /* O - Server name */ | |
eac3a0a0 | 522 | cups_gss_getname( |
f7deaa1a | 523 | http_t *http, /* I - Connection to server */ |
524 | const char *service_name) /* I - Service name */ | |
525 | { | |
526 | gss_buffer_desc token = GSS_C_EMPTY_BUFFER; | |
527 | /* Service token */ | |
bc44d920 | 528 | OM_uint32 major_status, /* Major status code */ |
529 | minor_status; /* Minor status code */ | |
530 | gss_name_t server_name; /* Server name */ | |
eac3a0a0 | 531 | char buf[1024]; /* Name buffer */ |
bc44d920 | 532 | |
533 | ||
eac3a0a0 | 534 | DEBUG_printf(("7cups_gss_getname(http=%p, service_name=\"%s\")", http, |
e07d4801 MS |
535 | service_name)); |
536 | ||
537 | ||
bc44d920 | 538 | /* |
539 | * Get the hostname... | |
540 | */ | |
f7deaa1a | 541 | |
eac3a0a0 MS |
542 | if (!http->gsshost[0]) |
543 | { | |
544 | httpGetHostname(http, http->gsshost, sizeof(http->gsshost)); | |
bc44d920 | 545 | |
eac3a0a0 MS |
546 | if (!strcmp(http->gsshost, "localhost")) |
547 | { | |
548 | if (gethostname(http->gsshost, sizeof(http->gsshost)) < 0) | |
549 | { | |
550 | DEBUG_printf(("1cups_gss_getname: gethostname() failed: %s", | |
551 | strerror(errno))); | |
552 | http->gsshost[0] = '\0'; | |
553 | return (NULL); | |
554 | } | |
555 | ||
556 | if (!strchr(http->gsshost, '.')) | |
557 | { | |
558 | /* | |
559 | * The hostname is not a FQDN, so look it up... | |
560 | */ | |
561 | ||
562 | struct hostent *host; /* Host entry to get FQDN */ | |
563 | ||
564 | if ((host = gethostbyname(http->gsshost)) != NULL && host->h_name) | |
565 | { | |
566 | /* | |
567 | * Use the resolved hostname... | |
568 | */ | |
569 | ||
570 | strlcpy(http->gsshost, host->h_name, sizeof(http->gsshost)); | |
571 | } | |
572 | else | |
573 | { | |
574 | DEBUG_printf(("1cups_gss_getname: gethostbyname(\"%s\") failed.", | |
575 | http->gsshost)); | |
576 | http->gsshost[0] = '\0'; | |
577 | return (NULL); | |
578 | } | |
579 | } | |
580 | } | |
581 | } | |
f7deaa1a | 582 | |
583 | /* | |
eac3a0a0 | 584 | * Get a service name we can use for authentication purposes... |
f7deaa1a | 585 | */ |
586 | ||
eac3a0a0 | 587 | snprintf(buf, sizeof(buf), "%s@%s", service_name, http->gsshost); |
f7deaa1a | 588 | |
eac3a0a0 | 589 | DEBUG_printf(("8cups_gss_getname: Looking up \"%s\".", buf)); |
7ff4fea9 | 590 | |
f7deaa1a | 591 | token.value = buf; |
592 | token.length = strlen(buf); | |
593 | server_name = GSS_C_NO_NAME; | |
594 | major_status = gss_import_name(&minor_status, &token, | |
595 | GSS_C_NT_HOSTBASED_SERVICE, | |
596 | &server_name); | |
597 | ||
598 | if (GSS_ERROR(major_status)) | |
599 | { | |
e07d4801 | 600 | cups_gss_printf(major_status, minor_status, |
eac3a0a0 | 601 | "cups_gss_getname: gss_import_name() failed"); |
f7deaa1a | 602 | return (NULL); |
603 | } | |
604 | ||
605 | return (server_name); | |
606 | } | |
e07d4801 MS |
607 | |
608 | ||
609 | # ifdef DEBUG | |
610 | /* | |
eac3a0a0 | 611 | * 'cups_gss_printf()' - Show debug error messages from GSSAPI. |
e07d4801 MS |
612 | */ |
613 | ||
614 | static void | |
615 | cups_gss_printf(OM_uint32 major_status,/* I - Major status code */ | |
616 | OM_uint32 minor_status,/* I - Minor status code */ | |
617 | const char *message) /* I - Prefix for error message */ | |
618 | { | |
619 | OM_uint32 err_major_status, /* Major status code for display */ | |
620 | err_minor_status; /* Minor status code for display */ | |
621 | OM_uint32 msg_ctx; /* Message context */ | |
622 | gss_buffer_desc major_status_string = GSS_C_EMPTY_BUFFER, | |
623 | /* Major status message */ | |
624 | minor_status_string = GSS_C_EMPTY_BUFFER; | |
625 | /* Minor status message */ | |
626 | ||
627 | ||
628 | msg_ctx = 0; | |
629 | err_major_status = gss_display_status(&err_minor_status, | |
630 | major_status, | |
631 | GSS_C_GSS_CODE, | |
632 | GSS_C_NO_OID, | |
633 | &msg_ctx, | |
634 | &major_status_string); | |
635 | ||
636 | if (!GSS_ERROR(err_major_status)) | |
637 | gss_display_status(&err_minor_status, minor_status, GSS_C_MECH_CODE, | |
638 | GSS_C_NULL_OID, &msg_ctx, &minor_status_string); | |
639 | ||
68b10830 | 640 | DEBUG_printf(("1%s: %s, %s", message, (char *)major_status_string.value, |
e07d4801 MS |
641 | (char *)minor_status_string.value)); |
642 | ||
643 | gss_release_buffer(&err_minor_status, &major_status_string); | |
644 | gss_release_buffer(&err_minor_status, &minor_status_string); | |
645 | } | |
646 | # endif /* DEBUG */ | |
f7deaa1a | 647 | #endif /* HAVE_GSSAPI */ |
648 | ||
649 | ||
ef416fc2 | 650 | /* |
651 | * 'cups_local_auth()' - Get the local authorization certificate if | |
eac3a0a0 | 652 | * available/applicable. |
ef416fc2 | 653 | */ |
654 | ||
f7deaa1a | 655 | static int /* O - 0 if available */ |
bc44d920 | 656 | /* 1 if not available */ |
f7deaa1a | 657 | /* -1 error */ |
ef416fc2 | 658 | cups_local_auth(http_t *http) /* I - HTTP connection to server */ |
659 | { | |
660 | #if defined(WIN32) || defined(__EMX__) | |
661 | /* | |
662 | * Currently WIN32 and OS-2 do not support the CUPS server... | |
663 | */ | |
664 | ||
bc44d920 | 665 | return (1); |
ef416fc2 | 666 | #else |
f7deaa1a | 667 | int pid; /* Current process ID */ |
668 | FILE *fp; /* Certificate file */ | |
e07d4801 | 669 | char trc[16], /* Try Root Certificate parameter */ |
6961465f | 670 | filename[1024]; /* Certificate filename */ |
ef416fc2 | 671 | _cups_globals_t *cg = _cupsGlobals(); /* Global data */ |
bc44d920 | 672 | # if defined(HAVE_AUTHORIZATION_H) |
f7deaa1a | 673 | OSStatus status; /* Status */ |
674 | AuthorizationItem auth_right; /* Authorization right */ | |
675 | AuthorizationRights auth_rights; /* Authorization rights */ | |
676 | AuthorizationFlags auth_flags; /* Authorization flags */ | |
677 | AuthorizationExternalForm auth_extrn; /* Authorization ref external */ | |
678 | char auth_key[1024]; /* Buffer */ | |
679 | char buffer[1024]; /* Buffer */ | |
bc44d920 | 680 | # endif /* HAVE_AUTHORIZATION_H */ |
ef416fc2 | 681 | |
682 | ||
e07d4801 | 683 | DEBUG_printf(("7cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"", |
ef416fc2 | 684 | http, httpAddrString(http->hostaddr, filename, sizeof(filename)), http->hostname)); |
685 | ||
686 | /* | |
687 | * See if we are accessing localhost... | |
688 | */ | |
689 | ||
690 | if (!httpAddrLocalhost(http->hostaddr) && | |
88f9aafc | 691 | _cups_strcasecmp(http->hostname, "localhost") != 0) |
ef416fc2 | 692 | { |
e07d4801 | 693 | DEBUG_puts("8cups_local_auth: Not a local connection!"); |
bc44d920 | 694 | return (1); |
ef416fc2 | 695 | } |
696 | ||
bc44d920 | 697 | # if defined(HAVE_AUTHORIZATION_H) |
f7deaa1a | 698 | /* |
699 | * Delete any previous authorization reference... | |
700 | */ | |
f14324a7 | 701 | |
b94498cf | 702 | if (http->auth_ref) |
f7deaa1a | 703 | { |
b94498cf | 704 | AuthorizationFree(http->auth_ref, kAuthorizationFlagDefaults); |
705 | http->auth_ref = NULL; | |
f7deaa1a | 706 | } |
707 | ||
e4572d57 | 708 | if (!getenv("GATEWAY_INTERFACE") && |
f14324a7 | 709 | httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "authkey", |
f7deaa1a | 710 | auth_key, sizeof(auth_key))) |
711 | { | |
f14324a7 | 712 | status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, |
b94498cf | 713 | kAuthorizationFlagDefaults, &http->auth_ref); |
f7deaa1a | 714 | if (status != errAuthorizationSuccess) |
715 | { | |
e07d4801 | 716 | DEBUG_printf(("8cups_local_auth: AuthorizationCreate() returned %d (%s)", |
f7deaa1a | 717 | (int)status, cssmErrorString(status))); |
718 | return (-1); | |
719 | } | |
720 | ||
721 | auth_right.name = auth_key; | |
722 | auth_right.valueLength = 0; | |
723 | auth_right.value = NULL; | |
724 | auth_right.flags = 0; | |
725 | ||
726 | auth_rights.count = 1; | |
727 | auth_rights.items = &auth_right; | |
728 | ||
f14324a7 | 729 | auth_flags = kAuthorizationFlagDefaults | |
f7deaa1a | 730 | kAuthorizationFlagPreAuthorize | |
f14324a7 | 731 | kAuthorizationFlagInteractionAllowed | |
f7deaa1a | 732 | kAuthorizationFlagExtendRights; |
733 | ||
f14324a7 MS |
734 | status = AuthorizationCopyRights(http->auth_ref, &auth_rights, |
735 | kAuthorizationEmptyEnvironment, | |
f7deaa1a | 736 | auth_flags, NULL); |
737 | if (status == errAuthorizationSuccess) | |
b94498cf | 738 | status = AuthorizationMakeExternalForm(http->auth_ref, &auth_extrn); |
f7deaa1a | 739 | |
740 | if (status == errAuthorizationSuccess) | |
741 | { | |
742 | /* | |
743 | * Set the authorization string and return... | |
744 | */ | |
745 | ||
f14324a7 | 746 | httpEncode64_2(buffer, sizeof(buffer), (void *)&auth_extrn, |
f7deaa1a | 747 | sizeof(auth_extrn)); |
748 | ||
355e94dc | 749 | httpSetAuthString(http, "AuthRef", buffer); |
f7deaa1a | 750 | |
e07d4801 | 751 | DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"", |
f7deaa1a | 752 | http->authstring)); |
753 | return (0); | |
754 | } | |
755 | else if (status == errAuthorizationCanceled) | |
756 | return (-1); | |
757 | ||
e07d4801 | 758 | DEBUG_printf(("9cups_local_auth: AuthorizationCopyRights() returned %d (%s)", |
f7deaa1a | 759 | (int)status, cssmErrorString(status))); |
760 | ||
761 | /* | |
762 | * Fall through to try certificates... | |
763 | */ | |
764 | } | |
bc44d920 | 765 | # endif /* HAVE_AUTHORIZATION_H */ |
f7deaa1a | 766 | |
f11a948a MS |
767 | # if defined(SO_PEERCRED) && defined(AF_LOCAL) |
768 | /* | |
769 | * See if we can authenticate using the peer credentials provided over a | |
770 | * domain socket; if so, specify "PeerCred username" as the authentication | |
771 | * information... | |
772 | */ | |
773 | ||
68b10830 | 774 | if ( |
f14324a7 MS |
775 | # ifdef HAVE_GSSAPI |
776 | strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9) && | |
68b10830 | 777 | # endif /* HAVE_GSSAPI */ |
f14324a7 MS |
778 | # ifdef HAVE_AUTHORIZATION_H |
779 | !httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "authkey", | |
780 | auth_key, sizeof(auth_key)) && | |
781 | # endif /* HAVE_AUTHORIZATION_H */ | |
68b10830 | 782 | http->hostaddr->addr.sa_family == AF_LOCAL && |
f11a948a MS |
783 | !getenv("GATEWAY_INTERFACE")) /* Not via CGI programs... */ |
784 | { | |
785 | /* | |
786 | * Verify that the current cupsUser() matches the current UID... | |
787 | */ | |
788 | ||
789 | struct passwd *pwd; /* Password information */ | |
790 | const char *username; /* Current username */ | |
791 | ||
792 | username = cupsUser(); | |
793 | ||
794 | if ((pwd = getpwnam(username)) != NULL && pwd->pw_uid == getuid()) | |
795 | { | |
796 | httpSetAuthString(http, "PeerCred", username); | |
797 | ||
798 | DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"", | |
799 | http->authstring)); | |
800 | ||
801 | return (0); | |
802 | } | |
803 | } | |
804 | # endif /* SO_PEERCRED && AF_LOCAL */ | |
805 | ||
ef416fc2 | 806 | /* |
807 | * Try opening a certificate file for this PID. If that fails, | |
808 | * try the root certificate... | |
809 | */ | |
810 | ||
811 | pid = getpid(); | |
812 | snprintf(filename, sizeof(filename), "%s/certs/%d", cg->cups_statedir, pid); | |
813 | if ((fp = fopen(filename, "r")) == NULL && pid > 0) | |
814 | { | |
bc44d920 | 815 | /* |
e07d4801 | 816 | * No certificate for this PID; see if we can get the root certificate... |
bc44d920 | 817 | */ |
818 | ||
e07d4801 MS |
819 | DEBUG_printf(("9cups_local_auth: Unable to open file %s: %s", |
820 | filename, strerror(errno))); | |
821 | ||
07ed0e9a | 822 | # ifdef HAVE_GSSAPI |
355e94dc | 823 | if (!strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9)) |
bc44d920 | 824 | { |
825 | /* | |
e07d4801 | 826 | * Kerberos required, don't try the root certificate... |
bc44d920 | 827 | */ |
828 | ||
829 | return (1); | |
830 | } | |
07ed0e9a | 831 | # endif /* HAVE_GSSAPI */ |
bc44d920 | 832 | |
07ed0e9a MS |
833 | # ifdef HAVE_AUTHORIZATION_H |
834 | if (httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "authkey", | |
835 | auth_key, sizeof(auth_key))) | |
836 | { | |
837 | /* | |
838 | * Don't use the root certificate as a replacement for an authkey... | |
839 | */ | |
840 | ||
841 | return (1); | |
842 | } | |
843 | # endif /* HAVE_AUTHORIZATION_H */ | |
e07d4801 MS |
844 | if (!httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "trc", trc, |
845 | sizeof(trc))) | |
846 | { | |
847 | /* | |
848 | * Scheduler doesn't want us to use the root certificate... | |
849 | */ | |
850 | ||
851 | return (1); | |
852 | } | |
853 | ||
ef416fc2 | 854 | snprintf(filename, sizeof(filename), "%s/certs/0", cg->cups_statedir); |
855 | fp = fopen(filename, "r"); | |
856 | } | |
857 | ||
f7deaa1a | 858 | if (fp) |
ef416fc2 | 859 | { |
f7deaa1a | 860 | /* |
861 | * Read the certificate from the file... | |
862 | */ | |
ef416fc2 | 863 | |
6961465f MS |
864 | char certificate[33], /* Certificate string */ |
865 | *certptr; /* Pointer to certificate string */ | |
866 | ||
867 | certptr = fgets(certificate, sizeof(certificate), fp); | |
f7deaa1a | 868 | fclose(fp); |
ef416fc2 | 869 | |
6961465f MS |
870 | if (certptr) |
871 | { | |
872 | /* | |
873 | * Set the authorization string and return... | |
874 | */ | |
ef416fc2 | 875 | |
6961465f | 876 | httpSetAuthString(http, "Local", certificate); |
ef416fc2 | 877 | |
6961465f MS |
878 | DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"", |
879 | http->authstring)); | |
ef416fc2 | 880 | |
6961465f MS |
881 | return (0); |
882 | } | |
f7deaa1a | 883 | } |
884 | ||
885 | return (1); | |
ef416fc2 | 886 | #endif /* WIN32 || __EMX__ */ |
887 | } | |
888 | ||
889 | ||
890 | /* | |
f2d18633 | 891 | * End of "$Id$". |
ef416fc2 | 892 | */ |