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