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