]> git.ipfire.org Git - thirdparty/cups.git/blame - cups/auth.c
Changes to eliminate warnings from new Clang.
[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
807315e6 125 DEBUG_printf(("cupsDoAuthentication(http=%p, method=\"%s\", resource=\"%s\")", (void *)http, method, resource));
ef416fc2 126
f11a948a
MS
127 if (!http)
128 http = _cupsConnect();
129
130 if (!http || !method || !resource)
131 return (-1);
132
5a6b583a
MS
133 DEBUG_printf(("2cupsDoAuthentication: digest_tries=%d, userpass=\"%s\"",
134 http->digest_tries, http->userpass));
135 DEBUG_printf(("2cupsDoAuthentication: WWW-Authenticate=\"%s\"",
136 httpGetField(http, HTTP_FIELD_WWW_AUTHENTICATE)));
137
ef416fc2 138 /*
139 * Clear the current authentication string...
140 */
141
355e94dc 142 httpSetAuthString(http, NULL, NULL);
ef416fc2 143
144 /*
145 * See if we can do local authentication...
146 */
147
f7deaa1a 148 if (http->digest_tries < 3)
ef416fc2 149 {
f7deaa1a 150 if ((localauth = cups_local_auth(http)) == 0)
151 {
e07d4801 152 DEBUG_printf(("2cupsDoAuthentication: authstring=\"%s\"",
f7deaa1a 153 http->authstring));
f14324a7 154
cb7f98ee 155 if (http->status == HTTP_STATUS_UNAUTHORIZED)
f7deaa1a 156 http->digest_tries ++;
f14324a7 157
f7deaa1a 158 return (0);
159 }
160 else if (localauth == -1)
f11a948a 161 {
cb7f98ee 162 http->status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
f7deaa1a 163 return (-1); /* Error or canceled */
f11a948a 164 }
ef416fc2 165 }
166
167 /*
168 * Nope, see if we should retry the current username:password...
169 */
170
3e7fe0ca
MS
171 www_auth = http->fields[HTTP_FIELD_WWW_AUTHENTICATE];
172
f7deaa1a 173 if ((http->digest_tries > 1 || !http->userpass[0]) &&
3e7fe0ca
MS
174 (!_cups_strncasecmp(www_auth, "Basic", 5) ||
175 !_cups_strncasecmp(www_auth, "Digest", 6)))
ef416fc2 176 {
177 /*
178 * Nope - get a new password from the user...
179 */
180
5a9febac
MS
181 char default_username[HTTP_MAX_VALUE];
182 /* Default username */
183
b86bc4cf 184 cg = _cupsGlobals();
185
186 if (!cg->lang_default)
187 cg->lang_default = cupsLangDefault();
188
5a9febac
MS
189 if (httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE, "username",
190 default_username))
191 cupsSetUser(default_username);
192
b86bc4cf 193 snprintf(prompt, sizeof(prompt),
194 _cupsLangString(cg->lang_default, _("Password for %s on %s? ")),
195 cupsUser(),
196 http->hostname[0] == '/' ? "localhost" : http->hostname);
ef416fc2 197
3e7fe0ca 198 http->digest_tries = _cups_strncasecmp(www_auth, "Digest", 6) != 0;
ef416fc2 199 http->userpass[0] = '\0';
200
f11a948a
MS
201 if ((password = cupsGetPassword2(prompt, http, method, resource)) == NULL)
202 {
cb7f98ee 203 http->status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
ef416fc2 204 return (-1);
f11a948a 205 }
ef416fc2 206
ef416fc2 207 snprintf(http->userpass, sizeof(http->userpass), "%s:%s", cupsUser(),
208 password);
209 }
cb7f98ee 210 else if (http->status == HTTP_STATUS_UNAUTHORIZED)
ef416fc2 211 http->digest_tries ++;
212
cb7f98ee 213 if (http->status == HTTP_STATUS_UNAUTHORIZED && http->digest_tries >= 3)
5a6b583a
MS
214 {
215 DEBUG_printf(("1cupsDoAuthentication: Too many authentication tries (%d)",
216 http->digest_tries));
217
cb7f98ee 218 http->status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
5a6b583a
MS
219 return (-1);
220 }
221
ef416fc2 222 /*
223 * Got a password; encode it for the server...
224 */
225
f14324a7 226#ifdef HAVE_GSSAPI
3e7fe0ca 227 if (!_cups_strncasecmp(www_auth, "Negotiate", 9))
f7deaa1a 228 {
f7deaa1a 229 /*
230 * Kerberos authentication...
231 */
232
eac3a0a0 233 if (_cupsSetNegotiateAuthString(http, method, resource))
f7deaa1a 234 {
cb7f98ee 235 http->status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
f7deaa1a 236 return (-1);
237 }
f7deaa1a 238 }
f14324a7
MS
239 else
240#endif /* HAVE_GSSAPI */
3e7fe0ca 241 if (!_cups_strncasecmp(www_auth, "Basic", 5))
ef416fc2 242 {
243 /*
244 * Basic authentication...
245 */
246
5a738aea
MS
247 char encode[256]; /* Base64 buffer */
248
249
ef416fc2 250 httpEncode64_2(encode, sizeof(encode), http->userpass,
b86bc4cf 251 (int)strlen(http->userpass));
355e94dc 252 httpSetAuthString(http, "Basic", encode);
ef416fc2 253 }
3e7fe0ca 254 else if (!_cups_strncasecmp(www_auth, "Digest", 6))
ef416fc2 255 {
256 /*
257 * Digest authentication...
258 */
259
5a738aea
MS
260 char encode[33], /* MD5 buffer */
261 digest[1024]; /* Digest auth data */
355e94dc 262
ef416fc2 263 httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE, "realm", realm);
264 httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE, "nonce", nonce);
265
266 httpMD5(cupsUser(), realm, strchr(http->userpass, ':') + 1, encode);
267 httpMD5Final(nonce, method, resource, encode);
355e94dc
MS
268 snprintf(digest, sizeof(digest),
269 "username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", "
270 "response=\"%s\"", cupsUser(), realm, nonce, resource, encode);
271 httpSetAuthString(http, "Digest", digest);
ef416fc2 272 }
f14324a7
MS
273 else
274 {
275 DEBUG_printf(("1cupsDoAuthentication: Unknown auth type: \"%s\"",
3e7fe0ca 276 www_auth));
cb7f98ee 277 http->status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
f14324a7
MS
278 return (-1);
279 }
ef416fc2 280
e07d4801 281 DEBUG_printf(("1cupsDoAuthentication: authstring=\"%s\"", http->authstring));
ef416fc2 282
283 return (0);
284}
285
286
f7deaa1a 287#ifdef HAVE_GSSAPI
f14324a7
MS
288/*
289 * '_cupsSetNegotiateAuthString()' - Set the Kerberos authentication string.
290 */
291
292int /* O - 0 on success, -1 on error */
293_cupsSetNegotiateAuthString(
eac3a0a0
MS
294 http_t *http, /* I - Connection to server */
295 const char *method, /* I - Request method ("GET", "POST", "PUT") */
296 const char *resource) /* I - Resource path */
f14324a7
MS
297{
298 OM_uint32 minor_status, /* Minor status code */
299 major_status; /* Major status code */
300 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
07ed0e9a 301 /* Output token */
f14324a7
MS
302
303
321d8d57
MS
304 (void)method;
305 (void)resource;
306
f14324a7
MS
307# ifdef __APPLE__
308 /*
309 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
310 * to use it...
311 */
312
6d8021f4 313 if (&gss_init_sec_context == NULL)
f14324a7
MS
314 {
315 DEBUG_puts("1_cupsSetNegotiateAuthString: Weak-linked GSSAPI/Kerberos "
316 "framework is not present");
317 return (-1);
318 }
319# endif /* __APPLE__ */
320
321 if (http->gssname == GSS_C_NO_NAME)
322 {
eac3a0a0 323 http->gssname = cups_gss_getname(http, _cupsGSSServiceName());
f14324a7
MS
324 }
325
326 if (http->gssctx != GSS_C_NO_CONTEXT)
327 {
328 gss_delete_sec_context(&minor_status, &http->gssctx, GSS_C_NO_BUFFER);
329 http->gssctx = GSS_C_NO_CONTEXT;
330 }
331
eac3a0a0
MS
332 major_status = gss_init_sec_context(&minor_status, GSS_C_NO_CREDENTIAL,
333 &http->gssctx,
334 http->gssname, http->gssmech,
335 GSS_C_MUTUAL_FLAG | GSS_C_INTEG_FLAG,
336 GSS_C_INDEFINITE,
337 GSS_C_NO_CHANNEL_BINDINGS,
338 GSS_C_NO_BUFFER, &http->gssmech,
339 &output_token, NULL, NULL);
340
341#ifdef HAVE_GSS_ACQUIRE_CRED_EX_F
342 if (major_status == GSS_S_NO_CRED)
343 {
344 /*
345 * Ask the user for credentials...
346 */
347
348 char prompt[1024], /* Prompt for user */
349 userbuf[256]; /* Kerberos username */
350 const char *username, /* Username string */
351 *password; /* Password string */
352 _cups_gss_acquire_t data; /* Callback data */
353 gss_auth_identity_desc identity; /* Kerberos user identity */
354 _cups_globals_t *cg = _cupsGlobals();
355 /* Per-thread global data */
356
357 if (!cg->lang_default)
358 cg->lang_default = cupsLangDefault();
359
360 snprintf(prompt, sizeof(prompt),
361 _cupsLangString(cg->lang_default, _("Password for %s on %s? ")),
f228370c 362 cupsUser(), http->gsshost);
eac3a0a0
MS
363
364 if ((password = cupsGetPassword2(prompt, http, method, resource)) == NULL)
365 return (-1);
366
367 /*
368 * Try to acquire credentials...
369 */
370
371 username = cupsUser();
372 if (!strchr(username, '@'))
373 {
f228370c 374 snprintf(userbuf, sizeof(userbuf), "%s@%s", username, http->gsshost);
eac3a0a0
MS
375 username = userbuf;
376 }
377
378 identity.type = GSS_AUTH_IDENTITY_TYPE_1;
379 identity.flags = 0;
380 identity.username = (char *)username;
381 identity.realm = (char *)"";
382 identity.password = (char *)password;
383 identity.credentialsRef = NULL;
384
385 data.sem = dispatch_semaphore_create(0);
386 data.major = 0;
387 data.creds = NULL;
388
389 if (data.sem)
390 {
391 major_status = gss_acquire_cred_ex_f(NULL, GSS_C_NO_NAME, 0,
392 GSS_C_INDEFINITE, GSS_KRB5_MECHANISM,
393 GSS_C_INITIATE, &identity, &data,
394 cups_gss_acquire);
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 }
eac3a0a0 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
22c9029b 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!");
22c9029b 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}
472
473
eac3a0a0 474# ifdef HAVE_GSS_ACQUIRE_CRED_EX_F
f7deaa1a 475/*
eac3a0a0
MS
476 * 'cups_gss_acquire()' - Kerberos credentials callback.
477 */
478static void
479cups_gss_acquire(
480 void *ctx, /* I - Caller context */
481 OM_uint32 major, /* I - Major error code */
482 gss_status_id_t status, /* I - Status (unused) */
483 gss_cred_id_t creds, /* I - Credentials (if any) */
484 gss_OID_set oids, /* I - Mechanism OIDs (unused) */
485 OM_uint32 time_rec) /* I - Timestamp (unused) */
486{
487 uint32_t min; /* Minor error code */
488 _cups_gss_acquire_t *data; /* Callback data */
489
490
491 (void)status;
492 (void)time_rec;
493
494 data = (_cups_gss_acquire_t *)ctx;
495 data->major = major;
496 data->creds = creds;
497
498 gss_release_oid_set(&min, &oids);
499 dispatch_semaphore_signal(data->sem);
500}
501# endif /* HAVE_GSS_ACQUIRE_CRED_EX_F */
502
503
504/*
505 * 'cups_gss_getname()' - Get CUPS service credentials for authentication.
f7deaa1a 506 */
507
508static gss_name_t /* O - Server name */
eac3a0a0 509cups_gss_getname(
f7deaa1a 510 http_t *http, /* I - Connection to server */
511 const char *service_name) /* I - Service name */
512{
513 gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
514 /* Service token */
bc44d920 515 OM_uint32 major_status, /* Major status code */
516 minor_status; /* Minor status code */
517 gss_name_t server_name; /* Server name */
eac3a0a0 518 char buf[1024]; /* Name buffer */
bc44d920 519
520
eac3a0a0 521 DEBUG_printf(("7cups_gss_getname(http=%p, service_name=\"%s\")", http,
e07d4801
MS
522 service_name));
523
524
bc44d920 525 /*
526 * Get the hostname...
527 */
f7deaa1a 528
eac3a0a0
MS
529 if (!http->gsshost[0])
530 {
531 httpGetHostname(http, http->gsshost, sizeof(http->gsshost));
bc44d920 532
eac3a0a0
MS
533 if (!strcmp(http->gsshost, "localhost"))
534 {
535 if (gethostname(http->gsshost, sizeof(http->gsshost)) < 0)
536 {
537 DEBUG_printf(("1cups_gss_getname: gethostname() failed: %s",
538 strerror(errno)));
539 http->gsshost[0] = '\0';
540 return (NULL);
541 }
542
543 if (!strchr(http->gsshost, '.'))
544 {
545 /*
546 * The hostname is not a FQDN, so look it up...
547 */
548
549 struct hostent *host; /* Host entry to get FQDN */
550
551 if ((host = gethostbyname(http->gsshost)) != NULL && host->h_name)
552 {
553 /*
554 * Use the resolved hostname...
555 */
556
557 strlcpy(http->gsshost, host->h_name, sizeof(http->gsshost));
558 }
559 else
560 {
561 DEBUG_printf(("1cups_gss_getname: gethostbyname(\"%s\") failed.",
562 http->gsshost));
563 http->gsshost[0] = '\0';
564 return (NULL);
565 }
566 }
567 }
568 }
f7deaa1a 569
570 /*
eac3a0a0 571 * Get a service name we can use for authentication purposes...
f7deaa1a 572 */
573
eac3a0a0 574 snprintf(buf, sizeof(buf), "%s@%s", service_name, http->gsshost);
f7deaa1a 575
eac3a0a0 576 DEBUG_printf(("8cups_gss_getname: Looking up \"%s\".", buf));
7ff4fea9 577
f7deaa1a 578 token.value = buf;
579 token.length = strlen(buf);
580 server_name = GSS_C_NO_NAME;
581 major_status = gss_import_name(&minor_status, &token,
582 GSS_C_NT_HOSTBASED_SERVICE,
583 &server_name);
584
585 if (GSS_ERROR(major_status))
586 {
e07d4801 587 cups_gss_printf(major_status, minor_status,
eac3a0a0 588 "cups_gss_getname: gss_import_name() failed");
f7deaa1a 589 return (NULL);
590 }
591
592 return (server_name);
593}
e07d4801
MS
594
595
596# ifdef DEBUG
597/*
eac3a0a0 598 * 'cups_gss_printf()' - Show debug error messages from GSSAPI.
e07d4801
MS
599 */
600
601static void
602cups_gss_printf(OM_uint32 major_status,/* I - Major status code */
603 OM_uint32 minor_status,/* I - Minor status code */
604 const char *message) /* I - Prefix for error message */
605{
606 OM_uint32 err_major_status, /* Major status code for display */
607 err_minor_status; /* Minor status code for display */
608 OM_uint32 msg_ctx; /* Message context */
609 gss_buffer_desc major_status_string = GSS_C_EMPTY_BUFFER,
610 /* Major status message */
611 minor_status_string = GSS_C_EMPTY_BUFFER;
612 /* Minor status message */
613
614
615 msg_ctx = 0;
616 err_major_status = gss_display_status(&err_minor_status,
617 major_status,
618 GSS_C_GSS_CODE,
619 GSS_C_NO_OID,
620 &msg_ctx,
621 &major_status_string);
622
623 if (!GSS_ERROR(err_major_status))
624 gss_display_status(&err_minor_status, minor_status, GSS_C_MECH_CODE,
625 GSS_C_NULL_OID, &msg_ctx, &minor_status_string);
626
68b10830 627 DEBUG_printf(("1%s: %s, %s", message, (char *)major_status_string.value,
e07d4801
MS
628 (char *)minor_status_string.value));
629
630 gss_release_buffer(&err_minor_status, &major_status_string);
631 gss_release_buffer(&err_minor_status, &minor_status_string);
632}
633# endif /* DEBUG */
f7deaa1a 634#endif /* HAVE_GSSAPI */
635
636
ef416fc2 637/*
638 * 'cups_local_auth()' - Get the local authorization certificate if
eac3a0a0 639 * available/applicable.
ef416fc2 640 */
641
f7deaa1a 642static int /* O - 0 if available */
bc44d920 643 /* 1 if not available */
f7deaa1a 644 /* -1 error */
ef416fc2 645cups_local_auth(http_t *http) /* I - HTTP connection to server */
646{
647#if defined(WIN32) || defined(__EMX__)
648 /*
649 * Currently WIN32 and OS-2 do not support the CUPS server...
650 */
651
bc44d920 652 return (1);
ef416fc2 653#else
f7deaa1a 654 int pid; /* Current process ID */
655 FILE *fp; /* Certificate file */
e07d4801 656 char trc[16], /* Try Root Certificate parameter */
6961465f 657 filename[1024]; /* Certificate filename */
ef416fc2 658 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
bc44d920 659# if defined(HAVE_AUTHORIZATION_H)
f7deaa1a 660 OSStatus status; /* Status */
661 AuthorizationItem auth_right; /* Authorization right */
662 AuthorizationRights auth_rights; /* Authorization rights */
663 AuthorizationFlags auth_flags; /* Authorization flags */
664 AuthorizationExternalForm auth_extrn; /* Authorization ref external */
665 char auth_key[1024]; /* Buffer */
666 char buffer[1024]; /* Buffer */
bc44d920 667# endif /* HAVE_AUTHORIZATION_H */
ef416fc2 668
669
807315e6 670 DEBUG_printf(("7cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"", (void *)http, httpAddrString(http->hostaddr, filename, sizeof(filename)), http->hostname));
ef416fc2 671
672 /*
673 * See if we are accessing localhost...
674 */
675
676 if (!httpAddrLocalhost(http->hostaddr) &&
88f9aafc 677 _cups_strcasecmp(http->hostname, "localhost") != 0)
ef416fc2 678 {
e07d4801 679 DEBUG_puts("8cups_local_auth: Not a local connection!");
bc44d920 680 return (1);
ef416fc2 681 }
682
bc44d920 683# if defined(HAVE_AUTHORIZATION_H)
f7deaa1a 684 /*
685 * Delete any previous authorization reference...
686 */
f14324a7 687
b94498cf 688 if (http->auth_ref)
f7deaa1a 689 {
b94498cf 690 AuthorizationFree(http->auth_ref, kAuthorizationFlagDefaults);
691 http->auth_ref = NULL;
f7deaa1a 692 }
693
e4572d57 694 if (!getenv("GATEWAY_INTERFACE") &&
f14324a7 695 httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "authkey",
f7deaa1a 696 auth_key, sizeof(auth_key)))
697 {
f14324a7 698 status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
b94498cf 699 kAuthorizationFlagDefaults, &http->auth_ref);
f7deaa1a 700 if (status != errAuthorizationSuccess)
701 {
e07d4801 702 DEBUG_printf(("8cups_local_auth: AuthorizationCreate() returned %d (%s)",
f7deaa1a 703 (int)status, cssmErrorString(status)));
704 return (-1);
705 }
706
707 auth_right.name = auth_key;
708 auth_right.valueLength = 0;
709 auth_right.value = NULL;
710 auth_right.flags = 0;
711
712 auth_rights.count = 1;
713 auth_rights.items = &auth_right;
714
f14324a7 715 auth_flags = kAuthorizationFlagDefaults |
f7deaa1a 716 kAuthorizationFlagPreAuthorize |
f14324a7 717 kAuthorizationFlagInteractionAllowed |
f7deaa1a 718 kAuthorizationFlagExtendRights;
719
f14324a7
MS
720 status = AuthorizationCopyRights(http->auth_ref, &auth_rights,
721 kAuthorizationEmptyEnvironment,
f7deaa1a 722 auth_flags, NULL);
723 if (status == errAuthorizationSuccess)
b94498cf 724 status = AuthorizationMakeExternalForm(http->auth_ref, &auth_extrn);
f7deaa1a 725
726 if (status == errAuthorizationSuccess)
727 {
728 /*
729 * Set the authorization string and return...
730 */
731
f14324a7 732 httpEncode64_2(buffer, sizeof(buffer), (void *)&auth_extrn,
f7deaa1a 733 sizeof(auth_extrn));
734
355e94dc 735 httpSetAuthString(http, "AuthRef", buffer);
f7deaa1a 736
e07d4801 737 DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
f7deaa1a 738 http->authstring));
739 return (0);
740 }
741 else if (status == errAuthorizationCanceled)
742 return (-1);
743
e07d4801 744 DEBUG_printf(("9cups_local_auth: AuthorizationCopyRights() returned %d (%s)",
f7deaa1a 745 (int)status, cssmErrorString(status)));
746
747 /*
748 * Fall through to try certificates...
749 */
750 }
bc44d920 751# endif /* HAVE_AUTHORIZATION_H */
f7deaa1a 752
f11a948a
MS
753# if defined(SO_PEERCRED) && defined(AF_LOCAL)
754 /*
755 * See if we can authenticate using the peer credentials provided over a
756 * domain socket; if so, specify "PeerCred username" as the authentication
757 * information...
758 */
759
68b10830 760 if (
f14324a7 761# ifdef HAVE_GSSAPI
4978eed9 762 _cups_strncasecmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9) &&
68b10830 763# endif /* HAVE_GSSAPI */
f14324a7
MS
764# ifdef HAVE_AUTHORIZATION_H
765 !httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "authkey",
766 auth_key, sizeof(auth_key)) &&
767# endif /* HAVE_AUTHORIZATION_H */
68b10830 768 http->hostaddr->addr.sa_family == AF_LOCAL &&
f11a948a
MS
769 !getenv("GATEWAY_INTERFACE")) /* Not via CGI programs... */
770 {
771 /*
772 * Verify that the current cupsUser() matches the current UID...
773 */
774
775 struct passwd *pwd; /* Password information */
776 const char *username; /* Current username */
777
778 username = cupsUser();
779
780 if ((pwd = getpwnam(username)) != NULL && pwd->pw_uid == getuid())
781 {
782 httpSetAuthString(http, "PeerCred", username);
783
784 DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
785 http->authstring));
786
787 return (0);
788 }
789 }
790# endif /* SO_PEERCRED && AF_LOCAL */
791
ef416fc2 792 /*
793 * Try opening a certificate file for this PID. If that fails,
794 * try the root certificate...
795 */
796
797 pid = getpid();
798 snprintf(filename, sizeof(filename), "%s/certs/%d", cg->cups_statedir, pid);
799 if ((fp = fopen(filename, "r")) == NULL && pid > 0)
800 {
bc44d920 801 /*
e07d4801 802 * No certificate for this PID; see if we can get the root certificate...
bc44d920 803 */
804
e07d4801
MS
805 DEBUG_printf(("9cups_local_auth: Unable to open file %s: %s",
806 filename, strerror(errno)));
807
07ed0e9a 808# ifdef HAVE_GSSAPI
4978eed9 809 if (!_cups_strncasecmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9))
bc44d920 810 {
811 /*
e07d4801 812 * Kerberos required, don't try the root certificate...
bc44d920 813 */
814
815 return (1);
816 }
07ed0e9a 817# endif /* HAVE_GSSAPI */
bc44d920 818
07ed0e9a
MS
819# ifdef HAVE_AUTHORIZATION_H
820 if (httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "authkey",
821 auth_key, sizeof(auth_key)))
822 {
823 /*
824 * Don't use the root certificate as a replacement for an authkey...
825 */
826
827 return (1);
828 }
829# endif /* HAVE_AUTHORIZATION_H */
e07d4801
MS
830 if (!httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "trc", trc,
831 sizeof(trc)))
832 {
833 /*
834 * Scheduler doesn't want us to use the root certificate...
835 */
836
837 return (1);
838 }
839
ef416fc2 840 snprintf(filename, sizeof(filename), "%s/certs/0", cg->cups_statedir);
841 fp = fopen(filename, "r");
842 }
843
f7deaa1a 844 if (fp)
ef416fc2 845 {
f7deaa1a 846 /*
847 * Read the certificate from the file...
848 */
ef416fc2 849
6961465f
MS
850 char certificate[33], /* Certificate string */
851 *certptr; /* Pointer to certificate string */
852
853 certptr = fgets(certificate, sizeof(certificate), fp);
f7deaa1a 854 fclose(fp);
ef416fc2 855
6961465f
MS
856 if (certptr)
857 {
858 /*
859 * Set the authorization string and return...
860 */
ef416fc2 861
6961465f 862 httpSetAuthString(http, "Local", certificate);
ef416fc2 863
6961465f
MS
864 DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
865 http->authstring));
ef416fc2 866
6961465f
MS
867 return (0);
868 }
f7deaa1a 869 }
870
871 return (1);
ef416fc2 872#endif /* WIN32 || __EMX__ */
873}
874
875
876/*
f2d18633 877 * End of "$Id$".
ef416fc2 878 */