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