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