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