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