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