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