]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
7e86f2f6 | 2 | * Authentication functions for CUPS. |
ef416fc2 | 3 | * |
5dcbe84d | 4 | * Copyright 2007-2016 by Apple Inc. |
7e86f2f6 | 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 | |
57b7b66b | 14 | * 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 | |
49c59293 | 57 | typedef struct gss_auth_identity |
eac3a0a0 MS |
58 | { |
59 | uint32_t type; | |
60 | uint32_t flags; | |
61 | char *username; | |
62 | char *realm; | |
63 | char *password; | |
64 | gss_buffer_t *credentialsRef; | |
49c59293 | 65 | } gss_auth_identity_desc; |
eac3a0a0 MS |
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 */ | |
49c59293 | 351 | gss_auth_identity_desc identity; /* Kerberos user identity */ |
eac3a0a0 MS |
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 | { | |
5dcbe84d | 389 | major_status = gss_acquire_cred_ex_f(NULL, GSS_C_NO_NAME, 0, GSS_C_INDEFINITE, GSS_KRB5_MECHANISM, GSS_C_INITIATE, (gss_auth_identity_t)&identity, &data, cups_gss_acquire); |
eac3a0a0 MS |
390 | |
391 | if (major_status == GSS_S_COMPLETE) | |
392 | { | |
393 | dispatch_semaphore_wait(data.sem, DISPATCH_TIME_FOREVER); | |
394 | major_status = data.major; | |
395 | } | |
396 | ||
397 | dispatch_release(data.sem); | |
398 | ||
399 | if (major_status == GSS_S_COMPLETE) | |
400 | { | |
401 | OM_uint32 release_minor; /* Minor status from releasing creds */ | |
402 | ||
403 | major_status = gss_init_sec_context(&minor_status, data.creds, | |
404 | &http->gssctx, | |
405 | http->gssname, http->gssmech, | |
406 | GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG, | |
407 | GSS_C_INDEFINITE, | |
408 | GSS_C_NO_CHANNEL_BINDINGS, | |
409 | GSS_C_NO_BUFFER, &http->gssmech, | |
410 | &output_token, NULL, NULL); | |
411 | gss_release_cred(&release_minor, &data.creds); | |
412 | } | |
413 | } | |
414 | } | |
eac3a0a0 | 415 | #endif /* HAVE_GSS_ACQUIRED_CRED_EX_F */ |
f14324a7 MS |
416 | |
417 | if (GSS_ERROR(major_status)) | |
418 | { | |
419 | cups_gss_printf(major_status, minor_status, | |
420 | "_cupsSetNegotiateAuthString: Unable to initialize " | |
421 | "security context"); | |
422 | return (-1); | |
423 | } | |
424 | ||
22c9029b | 425 | #ifdef DEBUG |
eac3a0a0 | 426 | else if (major_status == GSS_S_CONTINUE_NEEDED) |
f14324a7 MS |
427 | cups_gss_printf(major_status, minor_status, |
428 | "_cupsSetNegotiateAuthString: Continuation needed!"); | |
22c9029b | 429 | #endif /* DEBUG */ |
f14324a7 MS |
430 | |
431 | if (output_token.length > 0 && output_token.length <= 65536) | |
432 | { | |
433 | /* | |
434 | * Allocate the authorization string since Windows KDCs can have | |
435 | * arbitrarily large credentials... | |
436 | */ | |
437 | ||
7e86f2f6 MS |
438 | int authsize = 10 + /* "Negotiate " */ |
439 | (int)output_token.length * 4 / 3 + 1 + 1; | |
440 | /* Base64 + nul */ | |
f14324a7 MS |
441 | |
442 | httpSetAuthString(http, NULL, NULL); | |
443 | ||
7e86f2f6 | 444 | if ((http->authstring = malloc((size_t)authsize)) == NULL) |
f14324a7 MS |
445 | { |
446 | http->authstring = http->_authstring; | |
447 | authsize = sizeof(http->_authstring); | |
448 | } | |
449 | ||
07623986 | 450 | strlcpy(http->authstring, "Negotiate ", (size_t)authsize); |
f14324a7 | 451 | httpEncode64_2(http->authstring + 10, authsize - 10, output_token.value, |
7e86f2f6 | 452 | (int)output_token.length); |
f14324a7 MS |
453 | |
454 | gss_release_buffer(&minor_status, &output_token); | |
455 | } | |
456 | else | |
457 | { | |
458 | DEBUG_printf(("1_cupsSetNegotiateAuthString: Kerberos credentials too " | |
459 | "large - %d bytes!", (int)output_token.length)); | |
460 | gss_release_buffer(&minor_status, &output_token); | |
461 | ||
462 | return (-1); | |
463 | } | |
464 | ||
465 | return (0); | |
466 | } | |
467 | ||
468 | ||
eac3a0a0 | 469 | # ifdef HAVE_GSS_ACQUIRE_CRED_EX_F |
f7deaa1a | 470 | /* |
eac3a0a0 MS |
471 | * 'cups_gss_acquire()' - Kerberos credentials callback. |
472 | */ | |
473 | static void | |
474 | cups_gss_acquire( | |
475 | void *ctx, /* I - Caller context */ | |
476 | OM_uint32 major, /* I - Major error code */ | |
477 | gss_status_id_t status, /* I - Status (unused) */ | |
478 | gss_cred_id_t creds, /* I - Credentials (if any) */ | |
479 | gss_OID_set oids, /* I - Mechanism OIDs (unused) */ | |
480 | OM_uint32 time_rec) /* I - Timestamp (unused) */ | |
481 | { | |
482 | uint32_t min; /* Minor error code */ | |
483 | _cups_gss_acquire_t *data; /* Callback data */ | |
484 | ||
485 | ||
486 | (void)status; | |
487 | (void)time_rec; | |
488 | ||
489 | data = (_cups_gss_acquire_t *)ctx; | |
490 | data->major = major; | |
491 | data->creds = creds; | |
492 | ||
493 | gss_release_oid_set(&min, &oids); | |
494 | dispatch_semaphore_signal(data->sem); | |
495 | } | |
496 | # endif /* HAVE_GSS_ACQUIRE_CRED_EX_F */ | |
497 | ||
498 | ||
499 | /* | |
500 | * 'cups_gss_getname()' - Get CUPS service credentials for authentication. | |
f7deaa1a | 501 | */ |
502 | ||
503 | static gss_name_t /* O - Server name */ | |
eac3a0a0 | 504 | cups_gss_getname( |
f7deaa1a | 505 | http_t *http, /* I - Connection to server */ |
506 | const char *service_name) /* I - Service name */ | |
507 | { | |
508 | gss_buffer_desc token = GSS_C_EMPTY_BUFFER; | |
509 | /* Service token */ | |
bc44d920 | 510 | OM_uint32 major_status, /* Major status code */ |
511 | minor_status; /* Minor status code */ | |
512 | gss_name_t server_name; /* Server name */ | |
eac3a0a0 | 513 | char buf[1024]; /* Name buffer */ |
bc44d920 | 514 | |
515 | ||
eac3a0a0 | 516 | DEBUG_printf(("7cups_gss_getname(http=%p, service_name=\"%s\")", http, |
e07d4801 MS |
517 | service_name)); |
518 | ||
519 | ||
bc44d920 | 520 | /* |
521 | * Get the hostname... | |
522 | */ | |
f7deaa1a | 523 | |
eac3a0a0 MS |
524 | if (!http->gsshost[0]) |
525 | { | |
526 | httpGetHostname(http, http->gsshost, sizeof(http->gsshost)); | |
bc44d920 | 527 | |
eac3a0a0 MS |
528 | if (!strcmp(http->gsshost, "localhost")) |
529 | { | |
530 | if (gethostname(http->gsshost, sizeof(http->gsshost)) < 0) | |
531 | { | |
532 | DEBUG_printf(("1cups_gss_getname: gethostname() failed: %s", | |
533 | strerror(errno))); | |
534 | http->gsshost[0] = '\0'; | |
535 | return (NULL); | |
536 | } | |
537 | ||
538 | if (!strchr(http->gsshost, '.')) | |
539 | { | |
540 | /* | |
541 | * The hostname is not a FQDN, so look it up... | |
542 | */ | |
543 | ||
544 | struct hostent *host; /* Host entry to get FQDN */ | |
545 | ||
546 | if ((host = gethostbyname(http->gsshost)) != NULL && host->h_name) | |
547 | { | |
548 | /* | |
549 | * Use the resolved hostname... | |
550 | */ | |
551 | ||
552 | strlcpy(http->gsshost, host->h_name, sizeof(http->gsshost)); | |
553 | } | |
554 | else | |
555 | { | |
556 | DEBUG_printf(("1cups_gss_getname: gethostbyname(\"%s\") failed.", | |
557 | http->gsshost)); | |
558 | http->gsshost[0] = '\0'; | |
559 | return (NULL); | |
560 | } | |
561 | } | |
562 | } | |
563 | } | |
f7deaa1a | 564 | |
565 | /* | |
eac3a0a0 | 566 | * Get a service name we can use for authentication purposes... |
f7deaa1a | 567 | */ |
568 | ||
eac3a0a0 | 569 | snprintf(buf, sizeof(buf), "%s@%s", service_name, http->gsshost); |
f7deaa1a | 570 | |
eac3a0a0 | 571 | DEBUG_printf(("8cups_gss_getname: Looking up \"%s\".", buf)); |
7ff4fea9 | 572 | |
f7deaa1a | 573 | token.value = buf; |
574 | token.length = strlen(buf); | |
575 | server_name = GSS_C_NO_NAME; | |
576 | major_status = gss_import_name(&minor_status, &token, | |
577 | GSS_C_NT_HOSTBASED_SERVICE, | |
578 | &server_name); | |
579 | ||
580 | if (GSS_ERROR(major_status)) | |
581 | { | |
e07d4801 | 582 | cups_gss_printf(major_status, minor_status, |
eac3a0a0 | 583 | "cups_gss_getname: gss_import_name() failed"); |
f7deaa1a | 584 | return (NULL); |
585 | } | |
586 | ||
587 | return (server_name); | |
588 | } | |
e07d4801 MS |
589 | |
590 | ||
591 | # ifdef DEBUG | |
592 | /* | |
eac3a0a0 | 593 | * 'cups_gss_printf()' - Show debug error messages from GSSAPI. |
e07d4801 MS |
594 | */ |
595 | ||
596 | static void | |
597 | cups_gss_printf(OM_uint32 major_status,/* I - Major status code */ | |
598 | OM_uint32 minor_status,/* I - Minor status code */ | |
599 | const char *message) /* I - Prefix for error message */ | |
600 | { | |
601 | OM_uint32 err_major_status, /* Major status code for display */ | |
602 | err_minor_status; /* Minor status code for display */ | |
603 | OM_uint32 msg_ctx; /* Message context */ | |
604 | gss_buffer_desc major_status_string = GSS_C_EMPTY_BUFFER, | |
605 | /* Major status message */ | |
606 | minor_status_string = GSS_C_EMPTY_BUFFER; | |
607 | /* Minor status message */ | |
608 | ||
609 | ||
610 | msg_ctx = 0; | |
611 | err_major_status = gss_display_status(&err_minor_status, | |
612 | major_status, | |
613 | GSS_C_GSS_CODE, | |
614 | GSS_C_NO_OID, | |
615 | &msg_ctx, | |
616 | &major_status_string); | |
617 | ||
618 | if (!GSS_ERROR(err_major_status)) | |
619 | gss_display_status(&err_minor_status, minor_status, GSS_C_MECH_CODE, | |
620 | GSS_C_NULL_OID, &msg_ctx, &minor_status_string); | |
621 | ||
68b10830 | 622 | DEBUG_printf(("1%s: %s, %s", message, (char *)major_status_string.value, |
e07d4801 MS |
623 | (char *)minor_status_string.value)); |
624 | ||
625 | gss_release_buffer(&err_minor_status, &major_status_string); | |
626 | gss_release_buffer(&err_minor_status, &minor_status_string); | |
627 | } | |
628 | # endif /* DEBUG */ | |
f7deaa1a | 629 | #endif /* HAVE_GSSAPI */ |
630 | ||
631 | ||
ef416fc2 | 632 | /* |
633 | * 'cups_local_auth()' - Get the local authorization certificate if | |
eac3a0a0 | 634 | * available/applicable. |
ef416fc2 | 635 | */ |
636 | ||
f7deaa1a | 637 | static int /* O - 0 if available */ |
bc44d920 | 638 | /* 1 if not available */ |
f7deaa1a | 639 | /* -1 error */ |
ef416fc2 | 640 | cups_local_auth(http_t *http) /* I - HTTP connection to server */ |
641 | { | |
642 | #if defined(WIN32) || defined(__EMX__) | |
643 | /* | |
644 | * Currently WIN32 and OS-2 do not support the CUPS server... | |
645 | */ | |
646 | ||
bc44d920 | 647 | return (1); |
ef416fc2 | 648 | #else |
f7deaa1a | 649 | int pid; /* Current process ID */ |
650 | FILE *fp; /* Certificate file */ | |
e07d4801 | 651 | char trc[16], /* Try Root Certificate parameter */ |
6961465f | 652 | filename[1024]; /* Certificate filename */ |
ef416fc2 | 653 | _cups_globals_t *cg = _cupsGlobals(); /* Global data */ |
bc44d920 | 654 | # if defined(HAVE_AUTHORIZATION_H) |
f7deaa1a | 655 | OSStatus status; /* Status */ |
656 | AuthorizationItem auth_right; /* Authorization right */ | |
657 | AuthorizationRights auth_rights; /* Authorization rights */ | |
658 | AuthorizationFlags auth_flags; /* Authorization flags */ | |
659 | AuthorizationExternalForm auth_extrn; /* Authorization ref external */ | |
660 | char auth_key[1024]; /* Buffer */ | |
661 | char buffer[1024]; /* Buffer */ | |
bc44d920 | 662 | # endif /* HAVE_AUTHORIZATION_H */ |
ef416fc2 | 663 | |
664 | ||
807315e6 | 665 | DEBUG_printf(("7cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"", (void *)http, httpAddrString(http->hostaddr, filename, sizeof(filename)), http->hostname)); |
ef416fc2 | 666 | |
667 | /* | |
668 | * See if we are accessing localhost... | |
669 | */ | |
670 | ||
671 | if (!httpAddrLocalhost(http->hostaddr) && | |
88f9aafc | 672 | _cups_strcasecmp(http->hostname, "localhost") != 0) |
ef416fc2 | 673 | { |
e07d4801 | 674 | DEBUG_puts("8cups_local_auth: Not a local connection!"); |
bc44d920 | 675 | return (1); |
ef416fc2 | 676 | } |
677 | ||
bc44d920 | 678 | # if defined(HAVE_AUTHORIZATION_H) |
f7deaa1a | 679 | /* |
680 | * Delete any previous authorization reference... | |
681 | */ | |
f14324a7 | 682 | |
b94498cf | 683 | if (http->auth_ref) |
f7deaa1a | 684 | { |
b94498cf | 685 | AuthorizationFree(http->auth_ref, kAuthorizationFlagDefaults); |
686 | http->auth_ref = NULL; | |
f7deaa1a | 687 | } |
688 | ||
e4572d57 | 689 | if (!getenv("GATEWAY_INTERFACE") && |
f14324a7 | 690 | httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "authkey", |
f7deaa1a | 691 | auth_key, sizeof(auth_key))) |
692 | { | |
f14324a7 | 693 | status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, |
b94498cf | 694 | kAuthorizationFlagDefaults, &http->auth_ref); |
f7deaa1a | 695 | if (status != errAuthorizationSuccess) |
696 | { | |
e07d4801 | 697 | DEBUG_printf(("8cups_local_auth: AuthorizationCreate() returned %d (%s)", |
f7deaa1a | 698 | (int)status, cssmErrorString(status))); |
699 | return (-1); | |
700 | } | |
701 | ||
702 | auth_right.name = auth_key; | |
703 | auth_right.valueLength = 0; | |
704 | auth_right.value = NULL; | |
705 | auth_right.flags = 0; | |
706 | ||
707 | auth_rights.count = 1; | |
708 | auth_rights.items = &auth_right; | |
709 | ||
f14324a7 | 710 | auth_flags = kAuthorizationFlagDefaults | |
f7deaa1a | 711 | kAuthorizationFlagPreAuthorize | |
f14324a7 | 712 | kAuthorizationFlagInteractionAllowed | |
f7deaa1a | 713 | kAuthorizationFlagExtendRights; |
714 | ||
f14324a7 MS |
715 | status = AuthorizationCopyRights(http->auth_ref, &auth_rights, |
716 | kAuthorizationEmptyEnvironment, | |
f7deaa1a | 717 | auth_flags, NULL); |
718 | if (status == errAuthorizationSuccess) | |
b94498cf | 719 | status = AuthorizationMakeExternalForm(http->auth_ref, &auth_extrn); |
f7deaa1a | 720 | |
721 | if (status == errAuthorizationSuccess) | |
722 | { | |
723 | /* | |
724 | * Set the authorization string and return... | |
725 | */ | |
726 | ||
f14324a7 | 727 | httpEncode64_2(buffer, sizeof(buffer), (void *)&auth_extrn, |
f7deaa1a | 728 | sizeof(auth_extrn)); |
729 | ||
355e94dc | 730 | httpSetAuthString(http, "AuthRef", buffer); |
f7deaa1a | 731 | |
e07d4801 | 732 | DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"", |
f7deaa1a | 733 | http->authstring)); |
734 | return (0); | |
735 | } | |
736 | else if (status == errAuthorizationCanceled) | |
737 | return (-1); | |
738 | ||
e07d4801 | 739 | DEBUG_printf(("9cups_local_auth: AuthorizationCopyRights() returned %d (%s)", |
f7deaa1a | 740 | (int)status, cssmErrorString(status))); |
741 | ||
742 | /* | |
743 | * Fall through to try certificates... | |
744 | */ | |
745 | } | |
bc44d920 | 746 | # endif /* HAVE_AUTHORIZATION_H */ |
f7deaa1a | 747 | |
f11a948a MS |
748 | # if defined(SO_PEERCRED) && defined(AF_LOCAL) |
749 | /* | |
750 | * See if we can authenticate using the peer credentials provided over a | |
751 | * domain socket; if so, specify "PeerCred username" as the authentication | |
752 | * information... | |
753 | */ | |
754 | ||
68b10830 | 755 | if ( |
f14324a7 | 756 | # ifdef HAVE_GSSAPI |
4978eed9 | 757 | _cups_strncasecmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9) && |
68b10830 | 758 | # endif /* HAVE_GSSAPI */ |
f14324a7 MS |
759 | # ifdef HAVE_AUTHORIZATION_H |
760 | !httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "authkey", | |
761 | auth_key, sizeof(auth_key)) && | |
762 | # endif /* HAVE_AUTHORIZATION_H */ | |
68b10830 | 763 | http->hostaddr->addr.sa_family == AF_LOCAL && |
f11a948a MS |
764 | !getenv("GATEWAY_INTERFACE")) /* Not via CGI programs... */ |
765 | { | |
766 | /* | |
767 | * Verify that the current cupsUser() matches the current UID... | |
768 | */ | |
769 | ||
770 | struct passwd *pwd; /* Password information */ | |
771 | const char *username; /* Current username */ | |
772 | ||
773 | username = cupsUser(); | |
774 | ||
775 | if ((pwd = getpwnam(username)) != NULL && pwd->pw_uid == getuid()) | |
776 | { | |
777 | httpSetAuthString(http, "PeerCred", username); | |
778 | ||
779 | DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"", | |
780 | http->authstring)); | |
781 | ||
782 | return (0); | |
783 | } | |
784 | } | |
785 | # endif /* SO_PEERCRED && AF_LOCAL */ | |
786 | ||
ef416fc2 | 787 | /* |
788 | * Try opening a certificate file for this PID. If that fails, | |
789 | * try the root certificate... | |
790 | */ | |
791 | ||
792 | pid = getpid(); | |
793 | snprintf(filename, sizeof(filename), "%s/certs/%d", cg->cups_statedir, pid); | |
794 | if ((fp = fopen(filename, "r")) == NULL && pid > 0) | |
795 | { | |
bc44d920 | 796 | /* |
e07d4801 | 797 | * No certificate for this PID; see if we can get the root certificate... |
bc44d920 | 798 | */ |
799 | ||
e07d4801 MS |
800 | DEBUG_printf(("9cups_local_auth: Unable to open file %s: %s", |
801 | filename, strerror(errno))); | |
802 | ||
07ed0e9a | 803 | # ifdef HAVE_GSSAPI |
4978eed9 | 804 | if (!_cups_strncasecmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9)) |
bc44d920 | 805 | { |
806 | /* | |
e07d4801 | 807 | * Kerberos required, don't try the root certificate... |
bc44d920 | 808 | */ |
809 | ||
810 | return (1); | |
811 | } | |
07ed0e9a | 812 | # endif /* HAVE_GSSAPI */ |
bc44d920 | 813 | |
07ed0e9a MS |
814 | # ifdef HAVE_AUTHORIZATION_H |
815 | if (httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "authkey", | |
816 | auth_key, sizeof(auth_key))) | |
817 | { | |
818 | /* | |
819 | * Don't use the root certificate as a replacement for an authkey... | |
820 | */ | |
821 | ||
822 | return (1); | |
823 | } | |
824 | # endif /* HAVE_AUTHORIZATION_H */ | |
e07d4801 MS |
825 | if (!httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "trc", trc, |
826 | sizeof(trc))) | |
827 | { | |
828 | /* | |
829 | * Scheduler doesn't want us to use the root certificate... | |
830 | */ | |
831 | ||
832 | return (1); | |
833 | } | |
834 | ||
ef416fc2 | 835 | snprintf(filename, sizeof(filename), "%s/certs/0", cg->cups_statedir); |
836 | fp = fopen(filename, "r"); | |
837 | } | |
838 | ||
f7deaa1a | 839 | if (fp) |
ef416fc2 | 840 | { |
f7deaa1a | 841 | /* |
842 | * Read the certificate from the file... | |
843 | */ | |
ef416fc2 | 844 | |
6961465f MS |
845 | char certificate[33], /* Certificate string */ |
846 | *certptr; /* Pointer to certificate string */ | |
847 | ||
848 | certptr = fgets(certificate, sizeof(certificate), fp); | |
f7deaa1a | 849 | fclose(fp); |
ef416fc2 | 850 | |
6961465f MS |
851 | if (certptr) |
852 | { | |
853 | /* | |
854 | * Set the authorization string and return... | |
855 | */ | |
ef416fc2 | 856 | |
6961465f | 857 | httpSetAuthString(http, "Local", certificate); |
ef416fc2 | 858 | |
6961465f MS |
859 | DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"", |
860 | http->authstring)); | |
ef416fc2 | 861 | |
6961465f MS |
862 | return (0); |
863 | } | |
f7deaa1a | 864 | } |
865 | ||
866 | return (1); | |
ef416fc2 | 867 | #endif /* WIN32 || __EMX__ */ |
868 | } |