]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
7e86f2f6 | 2 | * Authentication functions for CUPS. |
ef416fc2 | 3 | * |
7ec11630 | 4 | * Copyright 2007-2017 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 | * |
e3101897 | 10 | * Licensed under Apache License v2.0. See the file "LICENSE" for more information. |
ef416fc2 | 11 | */ |
12 | ||
13 | /* | |
14 | * Include necessary headers... | |
15 | */ | |
16 | ||
71e16022 | 17 | #include "cups-private.h" |
ef416fc2 | 18 | #include <fcntl.h> |
19 | #include <sys/stat.h> | |
20 | #if defined(WIN32) || defined(__EMX__) | |
21 | # include <io.h> | |
22 | #else | |
23 | # include <unistd.h> | |
24 | #endif /* WIN32 || __EMX__ */ | |
25 | ||
f7deaa1a | 26 | #if HAVE_AUTHORIZATION_H |
27 | # include <Security/Authorization.h> | |
28 | # ifdef HAVE_SECBASEPRIV_H | |
29 | # include <Security/SecBasePriv.h> | |
30 | # else | |
31 | extern const char *cssmErrorString(int error); | |
32 | # endif /* HAVE_SECBASEPRIV_H */ | |
33 | #endif /* HAVE_AUTHORIZATION_H */ | |
34 | ||
bc44d920 | 35 | #if defined(SO_PEERCRED) && defined(AF_LOCAL) |
36 | # include <pwd.h> | |
37 | #endif /* SO_PEERCRED && AF_LOCAL */ | |
38 | ||
ef416fc2 | 39 | |
40 | /* | |
41 | * Local functions... | |
42 | */ | |
43 | ||
7ec11630 MS |
44 | static const char *cups_auth_find(const char *www_authenticate, const char *scheme); |
45 | static const char *cups_auth_param(const char *scheme, const char *name, char *value, size_t valsize); | |
46 | static const char *cups_auth_scheme(const char *www_authenticate, char *scheme, size_t schemesize); | |
47 | ||
f7deaa1a | 48 | #ifdef HAVE_GSSAPI |
eac3a0a0 MS |
49 | # ifdef HAVE_GSS_ACQUIRE_CRED_EX_F |
50 | # ifdef HAVE_GSS_GSSAPI_SPI_H | |
51 | # include <GSS/gssapi_spi.h> | |
52 | # else | |
a2326b5b MS |
53 | # define GSS_AUTH_IDENTITY_TYPE_1 1 |
54 | # define gss_acquire_cred_ex_f __ApplePrivate_gss_acquire_cred_ex_f | |
53af7f21 | 55 | typedef struct gss_auth_identity /* @private@ */ |
eac3a0a0 MS |
56 | { |
57 | uint32_t type; | |
58 | uint32_t flags; | |
59 | char *username; | |
60 | char *realm; | |
61 | char *password; | |
62 | gss_buffer_t *credentialsRef; | |
49c59293 | 63 | } gss_auth_identity_desc; |
eac3a0a0 MS |
64 | extern OM_uint32 gss_acquire_cred_ex_f(gss_status_id_t, const gss_name_t, |
65 | OM_uint32, OM_uint32, const gss_OID, | |
66 | gss_cred_usage_t, gss_auth_identity_t, | |
67 | void *, void (*)(void *, OM_uint32, | |
68 | gss_status_id_t, | |
69 | gss_cred_id_t, | |
70 | gss_OID_set, | |
71 | OM_uint32)); | |
72 | # endif /* HAVE_GSS_GSSAPI_SPI_H */ | |
73 | # include <dispatch/dispatch.h> | |
74 | typedef struct _cups_gss_acquire_s /* Acquire callback data */ | |
75 | { | |
76 | dispatch_semaphore_t sem; /* Synchronization semaphore */ | |
77 | OM_uint32 major; /* Returned status code */ | |
78 | gss_cred_id_t creds; /* Returned credentials */ | |
79 | } _cups_gss_acquire_t; | |
80 | ||
81 | static void cups_gss_acquire(void *ctx, OM_uint32 major, | |
82 | gss_status_id_t status, | |
83 | gss_cred_id_t creds, gss_OID_set oids, | |
84 | OM_uint32 time_rec); | |
85 | # endif /* HAVE_GSS_ACQUIRE_CRED_EX_F */ | |
86 | static gss_name_t cups_gss_getname(http_t *http, const char *service_name); | |
f7deaa1a | 87 | # ifdef DEBUG |
e07d4801 MS |
88 | static void cups_gss_printf(OM_uint32 major_status, OM_uint32 minor_status, |
89 | const char *message); | |
355e94dc | 90 | # else |
e07d4801 MS |
91 | # define cups_gss_printf(major, minor, message) |
92 | # endif /* DEBUG */ | |
f7deaa1a | 93 | #endif /* HAVE_GSSAPI */ |
ef416fc2 | 94 | static int cups_local_auth(http_t *http); |
95 | ||
96 | ||
97 | /* | |
98 | * 'cupsDoAuthentication()' - Authenticate a request. | |
99 | * | |
cb7f98ee | 100 | * This function should be called in response to a @code HTTP_STATUS_UNAUTHORIZED@ |
ef416fc2 | 101 | * status, prior to resubmitting your request. |
102 | * | |
8072030b | 103 | * @since CUPS 1.1.20/macOS 10.4@ |
ef416fc2 | 104 | */ |
105 | ||
106 | int /* O - 0 on success, -1 on error */ | |
f11a948a MS |
107 | cupsDoAuthentication( |
108 | http_t *http, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */ | |
109 | const char *method, /* I - Request method ("GET", "POST", "PUT") */ | |
110 | const char *resource) /* I - Resource path */ | |
ef416fc2 | 111 | { |
3e7fe0ca | 112 | const char *password, /* Password string */ |
7ec11630 MS |
113 | *www_auth, /* WWW-Authenticate header */ |
114 | *schemedata; /* Scheme-specific data */ | |
115 | char scheme[256], /* Scheme name */ | |
116 | prompt[1024], /* Prompt for user */ | |
ef416fc2 | 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 | /* | |
7ec11630 | 166 | * Nope, loop through the authentication schemes to find the first we support. |
ef416fc2 | 167 | */ |
168 | ||
7ec11630 | 169 | www_auth = httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE); |
3e7fe0ca | 170 | |
7ec11630 | 171 | for (schemedata = cups_auth_scheme(www_auth, scheme, sizeof(scheme)); schemedata; schemedata = cups_auth_scheme(schemedata + strlen(scheme), scheme, sizeof(scheme))) |
ef416fc2 | 172 | { |
173 | /* | |
7ec11630 | 174 | * Check the scheme name... |
ef416fc2 | 175 | */ |
176 | ||
7ec11630 MS |
177 | #ifdef HAVE_GSSAPI |
178 | if (!_cups_strcasecmp(scheme, "Negotiate")) | |
179 | { | |
180 | /* | |
181 | * Kerberos authentication... | |
182 | */ | |
5a9febac | 183 | |
7ec11630 MS |
184 | if (_cupsSetNegotiateAuthString(http, method, resource)) |
185 | { | |
186 | http->status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED; | |
187 | return (-1); | |
188 | } | |
b86bc4cf | 189 | |
7ec11630 MS |
190 | break; |
191 | } | |
192 | else | |
193 | #endif /* HAVE_GSSAPI */ | |
194 | if (_cups_strcasecmp(scheme, "Basic") && _cups_strcasecmp(scheme, "Digest")) | |
195 | continue; /* Not supported (yet) */ | |
b86bc4cf | 196 | |
7ec11630 MS |
197 | /* |
198 | * See if we should retry the current username:password... | |
199 | */ | |
5a9febac | 200 | |
7ec11630 MS |
201 | if ((http->digest_tries > 1 || !http->userpass[0]) && (!_cups_strcasecmp(scheme, "Basic") || (!_cups_strcasecmp(scheme, "Digest")))) |
202 | { | |
203 | /* | |
204 | * Nope - get a new password from the user... | |
205 | */ | |
ef416fc2 | 206 | |
7ec11630 MS |
207 | char default_username[HTTP_MAX_VALUE]; |
208 | /* Default username */ | |
ef416fc2 | 209 | |
7ec11630 | 210 | cg = _cupsGlobals(); |
ef416fc2 | 211 | |
7ec11630 MS |
212 | if (!cg->lang_default) |
213 | cg->lang_default = cupsLangDefault(); | |
ef416fc2 | 214 | |
44cb0dd2 | 215 | if (cups_auth_param(schemedata, "username", default_username, sizeof(default_username))) |
7ec11630 | 216 | cupsSetUser(default_username); |
5a6b583a | 217 | |
7ec11630 | 218 | snprintf(prompt, sizeof(prompt), _cupsLangString(cg->lang_default, _("Password for %s on %s? ")), cupsUser(), http->hostname[0] == '/' ? "localhost" : http->hostname); |
5a6b583a | 219 | |
7ec11630 MS |
220 | http->digest_tries = _cups_strncasecmp(scheme, "Digest", 6) != 0; |
221 | http->userpass[0] = '\0'; | |
ef416fc2 | 222 | |
7ec11630 MS |
223 | if ((password = cupsGetPassword2(prompt, http, method, resource)) == NULL) |
224 | { | |
225 | http->status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED; | |
226 | return (-1); | |
227 | } | |
f7deaa1a | 228 | |
7ec11630 MS |
229 | snprintf(http->userpass, sizeof(http->userpass), "%s:%s", cupsUser(), password); |
230 | } | |
231 | else if (http->status == HTTP_STATUS_UNAUTHORIZED) | |
232 | http->digest_tries ++; | |
233 | ||
234 | if (http->status == HTTP_STATUS_UNAUTHORIZED && http->digest_tries >= 3) | |
f7deaa1a | 235 | { |
7ec11630 MS |
236 | DEBUG_printf(("1cupsDoAuthentication: Too many authentication tries (%d)", http->digest_tries)); |
237 | ||
cb7f98ee | 238 | http->status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED; |
f7deaa1a | 239 | return (-1); |
240 | } | |
7ec11630 | 241 | |
ef416fc2 | 242 | /* |
7ec11630 | 243 | * Got a password; encode it for the server... |
ef416fc2 | 244 | */ |
245 | ||
7ec11630 MS |
246 | if (!_cups_strcasecmp(scheme, "Basic")) |
247 | { | |
248 | /* | |
249 | * Basic authentication... | |
250 | */ | |
5a738aea | 251 | |
7ec11630 | 252 | char encode[256]; /* Base64 buffer */ |
5a738aea | 253 | |
7ec11630 MS |
254 | httpEncode64_2(encode, sizeof(encode), http->userpass, (int)strlen(http->userpass)); |
255 | httpSetAuthString(http, "Basic", encode); | |
256 | } | |
257 | else if (!_cups_strcasecmp(scheme, "Digest")) | |
258 | { | |
259 | /* | |
260 | * Digest authentication... | |
261 | */ | |
262 | ||
263 | int i; /* Looping var */ | |
264 | char algorithm[65], /* Hashing algorithm */ | |
265 | opaque[HTTP_MAX_VALUE], | |
266 | /* Opaque data from server */ | |
267 | cnonce[65], /* cnonce value */ | |
268 | kd[65], /* Final MD5/SHA-256 digest */ | |
7ec11630 | 269 | digest[1024]; /* Digest auth data */ |
7ec11630 MS |
270 | |
271 | if (strcmp(nonce, http->nonce)) | |
272 | { | |
273 | strlcpy(http->nonce, nonce, sizeof(http->nonce)); | |
274 | http->nonce_count = 1; | |
275 | } | |
276 | else | |
277 | http->nonce_count ++; | |
ef416fc2 | 278 | |
7ec11630 MS |
279 | cups_auth_param(schemedata, "opaque", opaque, sizeof(opaque)); |
280 | cups_auth_param(schemedata, "nonce", nonce, sizeof(nonce)); | |
281 | cups_auth_param(schemedata, "realm", realm, sizeof(realm)); | |
355e94dc | 282 | |
7ec11630 MS |
283 | for (i = 0; i < 64; i ++) |
284 | cnonce[i] = "0123456789ABCDEF"[CUPS_RAND() & 15]; | |
285 | cnonce[64] = '\0'; | |
ef416fc2 | 286 | |
7ec11630 MS |
287 | if (cups_auth_param(schemedata, "algorithm", algorithm, sizeof(algorithm))) |
288 | { | |
289 | /* | |
3aeb120e | 290 | * Calculate and pass the RFC 2617/7616 WWW-Authenticate header... |
7ec11630 MS |
291 | */ |
292 | ||
3aeb120e MS |
293 | if (!_httpDigest(kd, sizeof(kd), algorithm, cupsUser(), realm, strchr(http->userpass, ':') + 1, nonce, http->nonce_count, cnonce, "auth", method, resource)) |
294 | continue; | |
7ec11630 | 295 | |
7ec11630 MS |
296 | if (opaque[0]) |
297 | snprintf(digest, sizeof(digest), "username=\"%s\", realm=\"%s\", nonce=\"%s\", algorithm=%s, qop=auth, opaque=\"%s\", cnonce=\"%s\", nc=%08x, uri=\"%s\", response=\"%s\"", cupsUser(), realm, nonce, algorithm, opaque, cnonce, http->nonce_count, resource, kd); | |
298 | else | |
299 | snprintf(digest, sizeof(digest), "username=\"%s\", realm=\"%s\", nonce=\"%s\", algorithm=%s, qop=auth, cnonce=\"%s\", nc=%08x, uri=\"%s\", response=\"%s\"", cupsUser(), realm, nonce, algorithm, cnonce, http->nonce_count, resource, kd); | |
300 | } | |
301 | else | |
302 | { | |
303 | /* | |
3aeb120e | 304 | * Calculate and pass the old RFC 2069 WWW-Authenticate header... |
7ec11630 MS |
305 | */ |
306 | ||
3aeb120e MS |
307 | if (!_httpDigest(kd, sizeof(kd), NULL, cupsUser(), realm, strchr(http->userpass, ':') + 1, nonce, http->nonce_count, NULL, NULL, method, resource)) |
308 | continue; | |
7ec11630 | 309 | |
7ec11630 MS |
310 | snprintf(digest, sizeof(digest), "username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"", cupsUser(), realm, nonce, resource, kd); |
311 | } | |
312 | ||
313 | httpSetAuthString(http, "Digest", digest); | |
314 | } | |
315 | } | |
316 | ||
317 | if (http->authstring) | |
318 | { | |
319 | DEBUG_printf(("1cupsDoAuthentication: authstring=\"%s\"", http->authstring)); | |
320 | ||
321 | return (0); | |
ef416fc2 | 322 | } |
f14324a7 MS |
323 | else |
324 | { | |
7ec11630 | 325 | DEBUG_printf(("1cupsDoAuthentication: Unknown auth type: \"%s\"", www_auth)); |
cb7f98ee | 326 | http->status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED; |
7ec11630 | 327 | |
f14324a7 MS |
328 | return (-1); |
329 | } | |
ef416fc2 | 330 | } |
331 | ||
332 | ||
f7deaa1a | 333 | #ifdef HAVE_GSSAPI |
f14324a7 MS |
334 | /* |
335 | * '_cupsSetNegotiateAuthString()' - Set the Kerberos authentication string. | |
336 | */ | |
337 | ||
338 | int /* O - 0 on success, -1 on error */ | |
339 | _cupsSetNegotiateAuthString( | |
eac3a0a0 MS |
340 | http_t *http, /* I - Connection to server */ |
341 | const char *method, /* I - Request method ("GET", "POST", "PUT") */ | |
342 | const char *resource) /* I - Resource path */ | |
f14324a7 MS |
343 | { |
344 | OM_uint32 minor_status, /* Minor status code */ | |
345 | major_status; /* Major status code */ | |
346 | gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER; | |
07ed0e9a | 347 | /* Output token */ |
f14324a7 MS |
348 | |
349 | ||
321d8d57 MS |
350 | (void)method; |
351 | (void)resource; | |
352 | ||
f14324a7 MS |
353 | # ifdef __APPLE__ |
354 | /* | |
355 | * If the weak-linked GSSAPI/Kerberos library is not present, don't try | |
356 | * to use it... | |
357 | */ | |
358 | ||
6d8021f4 | 359 | if (&gss_init_sec_context == NULL) |
f14324a7 MS |
360 | { |
361 | DEBUG_puts("1_cupsSetNegotiateAuthString: Weak-linked GSSAPI/Kerberos " | |
362 | "framework is not present"); | |
363 | return (-1); | |
364 | } | |
365 | # endif /* __APPLE__ */ | |
366 | ||
367 | if (http->gssname == GSS_C_NO_NAME) | |
368 | { | |
eac3a0a0 | 369 | http->gssname = cups_gss_getname(http, _cupsGSSServiceName()); |
f14324a7 MS |
370 | } |
371 | ||
372 | if (http->gssctx != GSS_C_NO_CONTEXT) | |
373 | { | |
374 | gss_delete_sec_context(&minor_status, &http->gssctx, GSS_C_NO_BUFFER); | |
375 | http->gssctx = GSS_C_NO_CONTEXT; | |
376 | } | |
377 | ||
eac3a0a0 MS |
378 | major_status = gss_init_sec_context(&minor_status, GSS_C_NO_CREDENTIAL, |
379 | &http->gssctx, | |
380 | http->gssname, http->gssmech, | |
381 | GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG, | |
382 | GSS_C_INDEFINITE, | |
383 | GSS_C_NO_CHANNEL_BINDINGS, | |
384 | GSS_C_NO_BUFFER, &http->gssmech, | |
385 | &output_token, NULL, NULL); | |
386 | ||
accedcf8 | 387 | # ifdef HAVE_GSS_ACQUIRE_CRED_EX_F |
eac3a0a0 MS |
388 | if (major_status == GSS_S_NO_CRED) |
389 | { | |
390 | /* | |
391 | * Ask the user for credentials... | |
392 | */ | |
393 | ||
394 | char prompt[1024], /* Prompt for user */ | |
395 | userbuf[256]; /* Kerberos username */ | |
396 | const char *username, /* Username string */ | |
397 | *password; /* Password string */ | |
398 | _cups_gss_acquire_t data; /* Callback data */ | |
49c59293 | 399 | gss_auth_identity_desc identity; /* Kerberos user identity */ |
eac3a0a0 MS |
400 | _cups_globals_t *cg = _cupsGlobals(); |
401 | /* Per-thread global data */ | |
402 | ||
403 | if (!cg->lang_default) | |
404 | cg->lang_default = cupsLangDefault(); | |
405 | ||
406 | snprintf(prompt, sizeof(prompt), | |
407 | _cupsLangString(cg->lang_default, _("Password for %s on %s? ")), | |
f228370c | 408 | cupsUser(), http->gsshost); |
eac3a0a0 MS |
409 | |
410 | if ((password = cupsGetPassword2(prompt, http, method, resource)) == NULL) | |
411 | return (-1); | |
412 | ||
413 | /* | |
414 | * Try to acquire credentials... | |
415 | */ | |
416 | ||
417 | username = cupsUser(); | |
418 | if (!strchr(username, '@')) | |
419 | { | |
f228370c | 420 | snprintf(userbuf, sizeof(userbuf), "%s@%s", username, http->gsshost); |
eac3a0a0 MS |
421 | username = userbuf; |
422 | } | |
423 | ||
424 | identity.type = GSS_AUTH_IDENTITY_TYPE_1; | |
425 | identity.flags = 0; | |
426 | identity.username = (char *)username; | |
427 | identity.realm = (char *)""; | |
428 | identity.password = (char *)password; | |
429 | identity.credentialsRef = NULL; | |
430 | ||
431 | data.sem = dispatch_semaphore_create(0); | |
432 | data.major = 0; | |
433 | data.creds = NULL; | |
434 | ||
435 | if (data.sem) | |
436 | { | |
5dcbe84d | 437 | 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 |
438 | |
439 | if (major_status == GSS_S_COMPLETE) | |
440 | { | |
441 | dispatch_semaphore_wait(data.sem, DISPATCH_TIME_FOREVER); | |
442 | major_status = data.major; | |
443 | } | |
444 | ||
445 | dispatch_release(data.sem); | |
446 | ||
447 | if (major_status == GSS_S_COMPLETE) | |
448 | { | |
449 | OM_uint32 release_minor; /* Minor status from releasing creds */ | |
450 | ||
451 | major_status = gss_init_sec_context(&minor_status, data.creds, | |
452 | &http->gssctx, | |
453 | http->gssname, http->gssmech, | |
454 | GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG, | |
455 | GSS_C_INDEFINITE, | |
456 | GSS_C_NO_CHANNEL_BINDINGS, | |
457 | GSS_C_NO_BUFFER, &http->gssmech, | |
458 | &output_token, NULL, NULL); | |
459 | gss_release_cred(&release_minor, &data.creds); | |
460 | } | |
461 | } | |
462 | } | |
accedcf8 | 463 | # endif /* HAVE_GSS_ACQUIRED_CRED_EX_F */ |
f14324a7 MS |
464 | |
465 | if (GSS_ERROR(major_status)) | |
466 | { | |
467 | cups_gss_printf(major_status, minor_status, | |
468 | "_cupsSetNegotiateAuthString: Unable to initialize " | |
469 | "security context"); | |
470 | return (-1); | |
471 | } | |
472 | ||
accedcf8 | 473 | # ifdef DEBUG |
eac3a0a0 | 474 | else if (major_status == GSS_S_CONTINUE_NEEDED) |
f14324a7 MS |
475 | cups_gss_printf(major_status, minor_status, |
476 | "_cupsSetNegotiateAuthString: Continuation needed!"); | |
accedcf8 | 477 | # endif /* DEBUG */ |
f14324a7 MS |
478 | |
479 | if (output_token.length > 0 && output_token.length <= 65536) | |
480 | { | |
481 | /* | |
482 | * Allocate the authorization string since Windows KDCs can have | |
483 | * arbitrarily large credentials... | |
484 | */ | |
485 | ||
7e86f2f6 MS |
486 | int authsize = 10 + /* "Negotiate " */ |
487 | (int)output_token.length * 4 / 3 + 1 + 1; | |
488 | /* Base64 + nul */ | |
f14324a7 MS |
489 | |
490 | httpSetAuthString(http, NULL, NULL); | |
491 | ||
7e86f2f6 | 492 | if ((http->authstring = malloc((size_t)authsize)) == NULL) |
f14324a7 MS |
493 | { |
494 | http->authstring = http->_authstring; | |
495 | authsize = sizeof(http->_authstring); | |
496 | } | |
497 | ||
07623986 | 498 | strlcpy(http->authstring, "Negotiate ", (size_t)authsize); |
f14324a7 | 499 | httpEncode64_2(http->authstring + 10, authsize - 10, output_token.value, |
7e86f2f6 | 500 | (int)output_token.length); |
f14324a7 MS |
501 | |
502 | gss_release_buffer(&minor_status, &output_token); | |
503 | } | |
504 | else | |
505 | { | |
506 | DEBUG_printf(("1_cupsSetNegotiateAuthString: Kerberos credentials too " | |
507 | "large - %d bytes!", (int)output_token.length)); | |
508 | gss_release_buffer(&minor_status, &output_token); | |
509 | ||
510 | return (-1); | |
511 | } | |
512 | ||
513 | return (0); | |
514 | } | |
accedcf8 | 515 | #endif /* HAVE_GSSAPI */ |
f14324a7 MS |
516 | |
517 | ||
7ec11630 MS |
518 | /* |
519 | * 'cups_auth_find()' - Find the named WWW-Authenticate scheme. | |
520 | * | |
521 | * The "www_authenticate" parameter points to the current position in the header. | |
522 | * | |
523 | * Returns @code NULL@ if the auth scheme is not present. | |
524 | */ | |
525 | ||
526 | static const char * /* O - Start of matching scheme or @code NULL@ if not found */ | |
527 | cups_auth_find(const char *www_authenticate, /* I - Pointer into WWW-Authenticate header */ | |
528 | const char *scheme) /* I - Authentication scheme */ | |
529 | { | |
530 | size_t schemelen = strlen(scheme); /* Length of scheme */ | |
531 | ||
532 | ||
91bc88eb MS |
533 | DEBUG_printf(("8cups_auth_find(www_authenticate=\"%s\", scheme=\"%s\"(%d))", www_authenticate, scheme, (int)schemelen)); |
534 | ||
7ec11630 MS |
535 | while (*www_authenticate) |
536 | { | |
537 | /* | |
538 | * Skip leading whitespace and commas... | |
539 | */ | |
540 | ||
91bc88eb | 541 | DEBUG_printf(("9cups_auth_find: Before whitespace: \"%s\"", www_authenticate)); |
7ec11630 MS |
542 | while (isspace(*www_authenticate & 255) || *www_authenticate == ',') |
543 | www_authenticate ++; | |
91bc88eb | 544 | DEBUG_printf(("9cups_auth_find: After whitespace: \"%s\"", www_authenticate)); |
7ec11630 MS |
545 | |
546 | /* | |
547 | * See if this is "Scheme" followed by whitespace or the end of the string. | |
548 | */ | |
549 | ||
c944b62b | 550 | if (!strncmp(www_authenticate, scheme, schemelen) && (isspace(www_authenticate[schemelen] & 255) || www_authenticate[schemelen] == ',' || !www_authenticate[schemelen])) |
7ec11630 MS |
551 | { |
552 | /* | |
553 | * Yes, this is the start of the scheme-specific information... | |
554 | */ | |
555 | ||
91bc88eb MS |
556 | DEBUG_printf(("9cups_auth_find: Returning \"%s\".", www_authenticate)); |
557 | ||
7ec11630 MS |
558 | return (www_authenticate); |
559 | } | |
560 | ||
561 | /* | |
562 | * Skip the scheme name or param="value" string... | |
563 | */ | |
564 | ||
565 | while (!isspace(*www_authenticate & 255) && *www_authenticate) | |
566 | { | |
567 | if (*www_authenticate == '\"') | |
568 | { | |
569 | /* | |
570 | * Skip quoted value... | |
571 | */ | |
572 | ||
91bc88eb MS |
573 | www_authenticate ++; |
574 | while (*www_authenticate && *www_authenticate != '\"') | |
7ec11630 | 575 | www_authenticate ++; |
91bc88eb MS |
576 | |
577 | DEBUG_printf(("9cups_auth_find: After quoted: \"%s\"", www_authenticate)); | |
7ec11630 MS |
578 | } |
579 | ||
580 | www_authenticate ++; | |
581 | } | |
91bc88eb MS |
582 | |
583 | DEBUG_printf(("9cups_auth_find: After skip: \"%s\"", www_authenticate)); | |
7ec11630 MS |
584 | } |
585 | ||
91bc88eb MS |
586 | DEBUG_puts("9cups_auth_find: Returning NULL."); |
587 | ||
7ec11630 MS |
588 | return (NULL); |
589 | } | |
590 | ||
591 | ||
592 | /* | |
593 | * 'cups_auth_param()' - Copy the value for the named authentication parameter, | |
594 | * if present. | |
595 | */ | |
596 | ||
597 | static const char * /* O - Parameter value or @code NULL@ if not present */ | |
598 | cups_auth_param(const char *scheme, /* I - Pointer to auth data */ | |
599 | const char *name, /* I - Name of parameter */ | |
600 | char *value, /* I - Value buffer */ | |
601 | size_t valsize) /* I - Size of value buffer */ | |
602 | { | |
603 | char *valptr = value, /* Pointer into value buffer */ | |
604 | *valend = value + valsize - 1; /* Pointer to end of buffer */ | |
605 | size_t namelen = strlen(name); /* Name length */ | |
606 | int param; /* Is this a parameter? */ | |
607 | ||
608 | ||
91bc88eb MS |
609 | DEBUG_printf(("8cups_auth_param(scheme=\"%s\", name=\"%s\", value=%p, valsize=%d)", scheme, name, (void *)value, (int)valsize)); |
610 | ||
7ec11630 MS |
611 | while (!isspace(*scheme & 255) && *scheme) |
612 | scheme ++; | |
613 | ||
614 | while (*scheme) | |
615 | { | |
616 | while (isspace(*scheme & 255) || *scheme == ',') | |
617 | scheme ++; | |
618 | ||
619 | if (!strncmp(scheme, name, namelen) && scheme[namelen] == '=') | |
620 | { | |
621 | /* | |
622 | * Found the parameter, copy the value... | |
623 | */ | |
624 | ||
625 | scheme += namelen + 1; | |
626 | if (*scheme == '\"') | |
627 | { | |
628 | scheme ++; | |
629 | ||
630 | while (*scheme && *scheme != '\"') | |
631 | { | |
632 | if (valptr < valend) | |
633 | *valptr++ = *scheme; | |
634 | ||
635 | scheme ++; | |
636 | } | |
637 | } | |
638 | else | |
639 | { | |
640 | while (*scheme && strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~+/=", *scheme)) | |
641 | { | |
642 | if (valptr < valend) | |
643 | *valptr++ = *scheme; | |
644 | ||
645 | scheme ++; | |
646 | } | |
647 | } | |
648 | ||
649 | *valptr = '\0'; | |
650 | ||
91bc88eb MS |
651 | DEBUG_printf(("9cups_auth_param: Returning \"%s\".", value)); |
652 | ||
7ec11630 MS |
653 | return (value); |
654 | } | |
655 | ||
656 | /* | |
657 | * Skip the param=value string... | |
658 | */ | |
659 | ||
660 | param = 0; | |
661 | ||
662 | while (!isspace(*scheme & 255) && *scheme) | |
663 | { | |
664 | if (*scheme == '=') | |
665 | param = 1; | |
666 | else if (*scheme == '\"') | |
667 | { | |
668 | /* | |
669 | * Skip quoted value... | |
670 | */ | |
671 | ||
91bc88eb MS |
672 | scheme ++; |
673 | while (*scheme && *scheme != '\"') | |
7ec11630 MS |
674 | scheme ++; |
675 | } | |
676 | ||
677 | scheme ++; | |
678 | } | |
679 | ||
680 | /* | |
681 | * If this wasn't a parameter, we are at the end of this scheme's | |
682 | * parameters... | |
683 | */ | |
684 | ||
685 | if (!param) | |
686 | break; | |
687 | } | |
688 | ||
689 | *value = '\0'; | |
690 | ||
91bc88eb MS |
691 | DEBUG_puts("9cups_auth_param: Returning NULL."); |
692 | ||
7ec11630 MS |
693 | return (NULL); |
694 | } | |
695 | ||
696 | ||
697 | /* | |
698 | * 'cups_auth_scheme()' - Get the (next) WWW-Authenticate scheme. | |
699 | * | |
700 | * The "www_authenticate" parameter points to the current position in the header. | |
701 | * | |
702 | * Returns @code NULL@ if there are no (more) auth schemes present. | |
703 | */ | |
704 | ||
705 | static const char * /* O - Start of scheme or @code NULL@ if not found */ | |
706 | cups_auth_scheme(const char *www_authenticate, /* I - Pointer into WWW-Authenticate header */ | |
707 | char *scheme, /* I - Scheme name buffer */ | |
708 | size_t schemesize) /* I - Size of buffer */ | |
709 | { | |
710 | const char *start; /* Start of scheme data */ | |
711 | char *sptr = scheme, /* Pointer into scheme buffer */ | |
712 | *send = scheme + schemesize - 1;/* End of scheme buffer */ | |
713 | int param; /* Is this a parameter? */ | |
714 | ||
715 | ||
91bc88eb MS |
716 | DEBUG_printf(("8cups_auth_scheme(www_authenticate=\"%s\", scheme=%p, schemesize=%d)", www_authenticate, (void *)scheme, (int)schemesize)); |
717 | ||
7ec11630 MS |
718 | while (*www_authenticate) |
719 | { | |
720 | /* | |
721 | * Skip leading whitespace and commas... | |
722 | */ | |
723 | ||
724 | while (isspace(*www_authenticate & 255) || *www_authenticate == ',') | |
725 | www_authenticate ++; | |
726 | ||
727 | /* | |
728 | * Parse the scheme name or param="value" string... | |
729 | */ | |
730 | ||
44cb0dd2 | 731 | for (sptr = scheme, start = www_authenticate, param = 0; *www_authenticate && *www_authenticate != ',' && !isspace(*www_authenticate & 255); www_authenticate ++) |
7ec11630 MS |
732 | { |
733 | if (*www_authenticate == '=') | |
734 | param = 1; | |
735 | else if (!param && sptr < send) | |
736 | *sptr++ = *www_authenticate; | |
737 | else if (*www_authenticate == '\"') | |
738 | { | |
739 | /* | |
740 | * Skip quoted value... | |
741 | */ | |
742 | ||
91bc88eb MS |
743 | www_authenticate ++; |
744 | while (*www_authenticate && *www_authenticate != '\"') | |
7ec11630 MS |
745 | www_authenticate ++; |
746 | } | |
747 | } | |
748 | ||
749 | if (sptr > scheme && !param) | |
750 | { | |
751 | *sptr = '\0'; | |
91bc88eb MS |
752 | |
753 | DEBUG_printf(("9cups_auth_scheme: Returning \"%s\".", start)); | |
754 | ||
7ec11630 MS |
755 | return (start); |
756 | } | |
757 | } | |
758 | ||
759 | *scheme = '\0'; | |
760 | ||
91bc88eb MS |
761 | DEBUG_puts("9cups_auth_scheme: Returning NULL."); |
762 | ||
7ec11630 MS |
763 | return (NULL); |
764 | } | |
765 | ||
766 | ||
accedcf8 | 767 | #ifdef HAVE_GSSAPI |
eac3a0a0 | 768 | # ifdef HAVE_GSS_ACQUIRE_CRED_EX_F |
f7deaa1a | 769 | /* |
eac3a0a0 MS |
770 | * 'cups_gss_acquire()' - Kerberos credentials callback. |
771 | */ | |
772 | static void | |
773 | cups_gss_acquire( | |
774 | void *ctx, /* I - Caller context */ | |
775 | OM_uint32 major, /* I - Major error code */ | |
776 | gss_status_id_t status, /* I - Status (unused) */ | |
777 | gss_cred_id_t creds, /* I - Credentials (if any) */ | |
778 | gss_OID_set oids, /* I - Mechanism OIDs (unused) */ | |
779 | OM_uint32 time_rec) /* I - Timestamp (unused) */ | |
780 | { | |
781 | uint32_t min; /* Minor error code */ | |
782 | _cups_gss_acquire_t *data; /* Callback data */ | |
783 | ||
784 | ||
785 | (void)status; | |
786 | (void)time_rec; | |
787 | ||
788 | data = (_cups_gss_acquire_t *)ctx; | |
789 | data->major = major; | |
790 | data->creds = creds; | |
791 | ||
792 | gss_release_oid_set(&min, &oids); | |
793 | dispatch_semaphore_signal(data->sem); | |
794 | } | |
795 | # endif /* HAVE_GSS_ACQUIRE_CRED_EX_F */ | |
796 | ||
797 | ||
798 | /* | |
799 | * 'cups_gss_getname()' - Get CUPS service credentials for authentication. | |
f7deaa1a | 800 | */ |
801 | ||
802 | static gss_name_t /* O - Server name */ | |
eac3a0a0 | 803 | cups_gss_getname( |
f7deaa1a | 804 | http_t *http, /* I - Connection to server */ |
805 | const char *service_name) /* I - Service name */ | |
806 | { | |
807 | gss_buffer_desc token = GSS_C_EMPTY_BUFFER; | |
808 | /* Service token */ | |
bc44d920 | 809 | OM_uint32 major_status, /* Major status code */ |
810 | minor_status; /* Minor status code */ | |
811 | gss_name_t server_name; /* Server name */ | |
eac3a0a0 | 812 | char buf[1024]; /* Name buffer */ |
bc44d920 | 813 | |
814 | ||
eac3a0a0 | 815 | DEBUG_printf(("7cups_gss_getname(http=%p, service_name=\"%s\")", http, |
e07d4801 MS |
816 | service_name)); |
817 | ||
818 | ||
bc44d920 | 819 | /* |
820 | * Get the hostname... | |
821 | */ | |
f7deaa1a | 822 | |
eac3a0a0 MS |
823 | if (!http->gsshost[0]) |
824 | { | |
825 | httpGetHostname(http, http->gsshost, sizeof(http->gsshost)); | |
bc44d920 | 826 | |
eac3a0a0 MS |
827 | if (!strcmp(http->gsshost, "localhost")) |
828 | { | |
829 | if (gethostname(http->gsshost, sizeof(http->gsshost)) < 0) | |
830 | { | |
831 | DEBUG_printf(("1cups_gss_getname: gethostname() failed: %s", | |
832 | strerror(errno))); | |
833 | http->gsshost[0] = '\0'; | |
834 | return (NULL); | |
835 | } | |
836 | ||
837 | if (!strchr(http->gsshost, '.')) | |
838 | { | |
839 | /* | |
840 | * The hostname is not a FQDN, so look it up... | |
841 | */ | |
842 | ||
843 | struct hostent *host; /* Host entry to get FQDN */ | |
844 | ||
845 | if ((host = gethostbyname(http->gsshost)) != NULL && host->h_name) | |
846 | { | |
847 | /* | |
848 | * Use the resolved hostname... | |
849 | */ | |
850 | ||
851 | strlcpy(http->gsshost, host->h_name, sizeof(http->gsshost)); | |
852 | } | |
853 | else | |
854 | { | |
855 | DEBUG_printf(("1cups_gss_getname: gethostbyname(\"%s\") failed.", | |
856 | http->gsshost)); | |
857 | http->gsshost[0] = '\0'; | |
858 | return (NULL); | |
859 | } | |
860 | } | |
861 | } | |
862 | } | |
f7deaa1a | 863 | |
864 | /* | |
eac3a0a0 | 865 | * Get a service name we can use for authentication purposes... |
f7deaa1a | 866 | */ |
867 | ||
eac3a0a0 | 868 | snprintf(buf, sizeof(buf), "%s@%s", service_name, http->gsshost); |
f7deaa1a | 869 | |
eac3a0a0 | 870 | DEBUG_printf(("8cups_gss_getname: Looking up \"%s\".", buf)); |
7ff4fea9 | 871 | |
f7deaa1a | 872 | token.value = buf; |
873 | token.length = strlen(buf); | |
874 | server_name = GSS_C_NO_NAME; | |
875 | major_status = gss_import_name(&minor_status, &token, | |
876 | GSS_C_NT_HOSTBASED_SERVICE, | |
877 | &server_name); | |
878 | ||
879 | if (GSS_ERROR(major_status)) | |
880 | { | |
e07d4801 | 881 | cups_gss_printf(major_status, minor_status, |
eac3a0a0 | 882 | "cups_gss_getname: gss_import_name() failed"); |
f7deaa1a | 883 | return (NULL); |
884 | } | |
885 | ||
886 | return (server_name); | |
887 | } | |
e07d4801 MS |
888 | |
889 | ||
890 | # ifdef DEBUG | |
891 | /* | |
eac3a0a0 | 892 | * 'cups_gss_printf()' - Show debug error messages from GSSAPI. |
e07d4801 MS |
893 | */ |
894 | ||
895 | static void | |
896 | cups_gss_printf(OM_uint32 major_status,/* I - Major status code */ | |
897 | OM_uint32 minor_status,/* I - Minor status code */ | |
898 | const char *message) /* I - Prefix for error message */ | |
899 | { | |
900 | OM_uint32 err_major_status, /* Major status code for display */ | |
901 | err_minor_status; /* Minor status code for display */ | |
902 | OM_uint32 msg_ctx; /* Message context */ | |
903 | gss_buffer_desc major_status_string = GSS_C_EMPTY_BUFFER, | |
904 | /* Major status message */ | |
905 | minor_status_string = GSS_C_EMPTY_BUFFER; | |
906 | /* Minor status message */ | |
907 | ||
908 | ||
909 | msg_ctx = 0; | |
910 | err_major_status = gss_display_status(&err_minor_status, | |
911 | major_status, | |
912 | GSS_C_GSS_CODE, | |
913 | GSS_C_NO_OID, | |
914 | &msg_ctx, | |
915 | &major_status_string); | |
916 | ||
917 | if (!GSS_ERROR(err_major_status)) | |
918 | gss_display_status(&err_minor_status, minor_status, GSS_C_MECH_CODE, | |
919 | GSS_C_NULL_OID, &msg_ctx, &minor_status_string); | |
920 | ||
68b10830 | 921 | DEBUG_printf(("1%s: %s, %s", message, (char *)major_status_string.value, |
e07d4801 MS |
922 | (char *)minor_status_string.value)); |
923 | ||
924 | gss_release_buffer(&err_minor_status, &major_status_string); | |
925 | gss_release_buffer(&err_minor_status, &minor_status_string); | |
926 | } | |
927 | # endif /* DEBUG */ | |
f7deaa1a | 928 | #endif /* HAVE_GSSAPI */ |
929 | ||
930 | ||
ef416fc2 | 931 | /* |
932 | * 'cups_local_auth()' - Get the local authorization certificate if | |
eac3a0a0 | 933 | * available/applicable. |
ef416fc2 | 934 | */ |
935 | ||
f7deaa1a | 936 | static int /* O - 0 if available */ |
bc44d920 | 937 | /* 1 if not available */ |
f7deaa1a | 938 | /* -1 error */ |
ef416fc2 | 939 | cups_local_auth(http_t *http) /* I - HTTP connection to server */ |
940 | { | |
941 | #if defined(WIN32) || defined(__EMX__) | |
942 | /* | |
943 | * Currently WIN32 and OS-2 do not support the CUPS server... | |
944 | */ | |
945 | ||
bc44d920 | 946 | return (1); |
ef416fc2 | 947 | #else |
f7deaa1a | 948 | int pid; /* Current process ID */ |
949 | FILE *fp; /* Certificate file */ | |
e07d4801 | 950 | char trc[16], /* Try Root Certificate parameter */ |
6961465f | 951 | filename[1024]; /* Certificate filename */ |
7ec11630 MS |
952 | const char *www_auth, /* WWW-Authenticate header */ |
953 | *schemedata; /* Data for the named auth scheme */ | |
ef416fc2 | 954 | _cups_globals_t *cg = _cupsGlobals(); /* Global data */ |
bc44d920 | 955 | # if defined(HAVE_AUTHORIZATION_H) |
f7deaa1a | 956 | OSStatus status; /* Status */ |
957 | AuthorizationItem auth_right; /* Authorization right */ | |
958 | AuthorizationRights auth_rights; /* Authorization rights */ | |
959 | AuthorizationFlags auth_flags; /* Authorization flags */ | |
960 | AuthorizationExternalForm auth_extrn; /* Authorization ref external */ | |
961 | char auth_key[1024]; /* Buffer */ | |
962 | char buffer[1024]; /* Buffer */ | |
bc44d920 | 963 | # endif /* HAVE_AUTHORIZATION_H */ |
ef416fc2 | 964 | |
965 | ||
807315e6 | 966 | DEBUG_printf(("7cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"", (void *)http, httpAddrString(http->hostaddr, filename, sizeof(filename)), http->hostname)); |
ef416fc2 | 967 | |
968 | /* | |
969 | * See if we are accessing localhost... | |
970 | */ | |
971 | ||
7ec11630 | 972 | if (!httpAddrLocalhost(http->hostaddr) && _cups_strcasecmp(http->hostname, "localhost") != 0) |
ef416fc2 | 973 | { |
e07d4801 | 974 | DEBUG_puts("8cups_local_auth: Not a local connection!"); |
bc44d920 | 975 | return (1); |
ef416fc2 | 976 | } |
977 | ||
accedcf8 MS |
978 | www_auth = httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE); |
979 | ||
bc44d920 | 980 | # if defined(HAVE_AUTHORIZATION_H) |
f7deaa1a | 981 | /* |
982 | * Delete any previous authorization reference... | |
983 | */ | |
f14324a7 | 984 | |
b94498cf | 985 | if (http->auth_ref) |
f7deaa1a | 986 | { |
b94498cf | 987 | AuthorizationFree(http->auth_ref, kAuthorizationFlagDefaults); |
988 | http->auth_ref = NULL; | |
f7deaa1a | 989 | } |
990 | ||
91bc88eb | 991 | if (!getenv("GATEWAY_INTERFACE") && (schemedata = cups_auth_find(www_auth, "AuthRef")) != NULL && cups_auth_param(schemedata, "key", auth_key, sizeof(auth_key))) |
f7deaa1a | 992 | { |
7ec11630 | 993 | status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, kAuthorizationFlagDefaults, &http->auth_ref); |
f7deaa1a | 994 | if (status != errAuthorizationSuccess) |
995 | { | |
e07d4801 | 996 | DEBUG_printf(("8cups_local_auth: AuthorizationCreate() returned %d (%s)", |
f7deaa1a | 997 | (int)status, cssmErrorString(status))); |
998 | return (-1); | |
999 | } | |
1000 | ||
1001 | auth_right.name = auth_key; | |
1002 | auth_right.valueLength = 0; | |
1003 | auth_right.value = NULL; | |
1004 | auth_right.flags = 0; | |
1005 | ||
1006 | auth_rights.count = 1; | |
1007 | auth_rights.items = &auth_right; | |
1008 | ||
f14324a7 | 1009 | auth_flags = kAuthorizationFlagDefaults | |
f7deaa1a | 1010 | kAuthorizationFlagPreAuthorize | |
f14324a7 | 1011 | kAuthorizationFlagInteractionAllowed | |
f7deaa1a | 1012 | kAuthorizationFlagExtendRights; |
1013 | ||
f14324a7 MS |
1014 | status = AuthorizationCopyRights(http->auth_ref, &auth_rights, |
1015 | kAuthorizationEmptyEnvironment, | |
f7deaa1a | 1016 | auth_flags, NULL); |
1017 | if (status == errAuthorizationSuccess) | |
b94498cf | 1018 | status = AuthorizationMakeExternalForm(http->auth_ref, &auth_extrn); |
f7deaa1a | 1019 | |
1020 | if (status == errAuthorizationSuccess) | |
1021 | { | |
1022 | /* | |
1023 | * Set the authorization string and return... | |
1024 | */ | |
1025 | ||
f14324a7 | 1026 | httpEncode64_2(buffer, sizeof(buffer), (void *)&auth_extrn, |
f7deaa1a | 1027 | sizeof(auth_extrn)); |
1028 | ||
355e94dc | 1029 | httpSetAuthString(http, "AuthRef", buffer); |
f7deaa1a | 1030 | |
e07d4801 | 1031 | DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"", |
f7deaa1a | 1032 | http->authstring)); |
1033 | return (0); | |
1034 | } | |
1035 | else if (status == errAuthorizationCanceled) | |
1036 | return (-1); | |
1037 | ||
e07d4801 | 1038 | DEBUG_printf(("9cups_local_auth: AuthorizationCopyRights() returned %d (%s)", |
f7deaa1a | 1039 | (int)status, cssmErrorString(status))); |
1040 | ||
1041 | /* | |
1042 | * Fall through to try certificates... | |
1043 | */ | |
1044 | } | |
bc44d920 | 1045 | # endif /* HAVE_AUTHORIZATION_H */ |
f7deaa1a | 1046 | |
7ec11630 MS |
1047 | # ifdef HAVE_GSSAPI |
1048 | if (cups_auth_find(www_auth, "Negotiate")) | |
1049 | return (1); | |
1050 | # endif /* HAVE_GSSAPI */ | |
7ec11630 | 1051 | |
f11a948a MS |
1052 | # if defined(SO_PEERCRED) && defined(AF_LOCAL) |
1053 | /* | |
1054 | * See if we can authenticate using the peer credentials provided over a | |
1055 | * domain socket; if so, specify "PeerCred username" as the authentication | |
1056 | * information... | |
1057 | */ | |
1058 | ||
7ec11630 MS |
1059 | if (http->hostaddr->addr.sa_family == AF_LOCAL && |
1060 | !getenv("GATEWAY_INTERFACE") && /* Not via CGI programs... */ | |
1061 | cups_auth_find(www_auth, "PeerCred")) | |
f11a948a MS |
1062 | { |
1063 | /* | |
1064 | * Verify that the current cupsUser() matches the current UID... | |
1065 | */ | |
1066 | ||
1067 | struct passwd *pwd; /* Password information */ | |
1068 | const char *username; /* Current username */ | |
1069 | ||
1070 | username = cupsUser(); | |
1071 | ||
1072 | if ((pwd = getpwnam(username)) != NULL && pwd->pw_uid == getuid()) | |
1073 | { | |
1074 | httpSetAuthString(http, "PeerCred", username); | |
1075 | ||
1076 | DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"", | |
1077 | http->authstring)); | |
1078 | ||
1079 | return (0); | |
1080 | } | |
1081 | } | |
1082 | # endif /* SO_PEERCRED && AF_LOCAL */ | |
1083 | ||
7ec11630 MS |
1084 | if ((schemedata = cups_auth_find(www_auth, "Local")) == NULL) |
1085 | return (1); | |
1086 | ||
ef416fc2 | 1087 | /* |
1088 | * Try opening a certificate file for this PID. If that fails, | |
1089 | * try the root certificate... | |
1090 | */ | |
1091 | ||
1092 | pid = getpid(); | |
1093 | snprintf(filename, sizeof(filename), "%s/certs/%d", cg->cups_statedir, pid); | |
1094 | if ((fp = fopen(filename, "r")) == NULL && pid > 0) | |
1095 | { | |
bc44d920 | 1096 | /* |
e07d4801 | 1097 | * No certificate for this PID; see if we can get the root certificate... |
bc44d920 | 1098 | */ |
1099 | ||
bf265b3a | 1100 | DEBUG_printf(("9cups_local_auth: Unable to open file \"%s\": %s", filename, strerror(errno))); |
e07d4801 | 1101 | |
7ec11630 | 1102 | if (!cups_auth_param(schemedata, "trc", trc, sizeof(trc))) |
e07d4801 MS |
1103 | { |
1104 | /* | |
1105 | * Scheduler doesn't want us to use the root certificate... | |
1106 | */ | |
1107 | ||
1108 | return (1); | |
1109 | } | |
1110 | ||
ef416fc2 | 1111 | snprintf(filename, sizeof(filename), "%s/certs/0", cg->cups_statedir); |
bf265b3a MS |
1112 | if ((fp = fopen(filename, "r")) == NULL) |
1113 | DEBUG_printf(("9cups_local_auth: Unable to open file \"%s\": %s", filename, strerror(errno))); | |
ef416fc2 | 1114 | } |
1115 | ||
f7deaa1a | 1116 | if (fp) |
ef416fc2 | 1117 | { |
f7deaa1a | 1118 | /* |
1119 | * Read the certificate from the file... | |
1120 | */ | |
ef416fc2 | 1121 | |
6961465f MS |
1122 | char certificate[33], /* Certificate string */ |
1123 | *certptr; /* Pointer to certificate string */ | |
1124 | ||
1125 | certptr = fgets(certificate, sizeof(certificate), fp); | |
f7deaa1a | 1126 | fclose(fp); |
ef416fc2 | 1127 | |
6961465f MS |
1128 | if (certptr) |
1129 | { | |
1130 | /* | |
1131 | * Set the authorization string and return... | |
1132 | */ | |
ef416fc2 | 1133 | |
6961465f | 1134 | httpSetAuthString(http, "Local", certificate); |
ef416fc2 | 1135 | |
6961465f MS |
1136 | DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"", |
1137 | http->authstring)); | |
ef416fc2 | 1138 | |
6961465f MS |
1139 | return (0); |
1140 | } | |
f7deaa1a | 1141 | } |
1142 | ||
1143 | return (1); | |
ef416fc2 | 1144 | #endif /* WIN32 || __EMX__ */ |
1145 | } |