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