]> git.ipfire.org Git - thirdparty/cups.git/blob - cups/auth.c
Fix build errors on Fedora.
[thirdparty/cups.git] / cups / auth.c
1 /*
2 * "$Id$"
3 *
4 * Authentication functions for CUPS.
5 *
6 * Copyright 2007-2014 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
21 /*
22 * Include necessary headers...
23 */
24
25 #include "cups-private.h"
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
34 #if HAVE_AUTHORIZATION_H
35 # include <Security/Authorization.h>
36 # ifdef HAVE_SECBASEPRIV_H
37 # include <Security/SecBasePriv.h>
38 # else
39 extern const char *cssmErrorString(int error);
40 # endif /* HAVE_SECBASEPRIV_H */
41 #endif /* HAVE_AUTHORIZATION_H */
42
43 #if defined(SO_PEERCRED) && defined(AF_LOCAL)
44 # include <pwd.h>
45 #endif /* SO_PEERCRED && AF_LOCAL */
46
47
48 /*
49 * Local functions...
50 */
51
52 #ifdef HAVE_GSSAPI
53 # ifdef HAVE_GSS_ACQUIRE_CRED_EX_F
54 # ifdef HAVE_GSS_GSSAPI_SPI_H
55 # include <GSS/gssapi_spi.h>
56 # else
57 # define GSS_AUTH_IDENTITY_TYPE_1 1
58 # define gss_acquire_cred_ex_f __ApplePrivate_gss_acquire_cred_ex_f
59 typedef 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;
68 extern 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>
78 typedef 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
85 static 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 */
90 static gss_name_t cups_gss_getname(http_t *http, const char *service_name);
91 # ifdef DEBUG
92 static void cups_gss_printf(OM_uint32 major_status, OM_uint32 minor_status,
93 const char *message);
94 # else
95 # define cups_gss_printf(major, minor, message)
96 # endif /* DEBUG */
97 #endif /* HAVE_GSSAPI */
98 static int cups_local_auth(http_t *http);
99
100
101 /*
102 * 'cupsDoAuthentication()' - Authenticate a request.
103 *
104 * This function should be called in response to a @code HTTP_STATUS_UNAUTHORIZED@
105 * status, prior to resubmitting your request.
106 *
107 * @since CUPS 1.1.20/OS X 10.4@
108 */
109
110 int /* O - 0 on success, -1 on error */
111 cupsDoAuthentication(
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 */
115 {
116 const char *password, /* Password string */
117 *www_auth; /* WWW-Authenticate header */
118 char prompt[1024], /* Prompt for user */
119 realm[HTTP_MAX_VALUE], /* realm="xyz" string */
120 nonce[HTTP_MAX_VALUE]; /* nonce="xyz" string */
121 int localauth; /* Local authentication result */
122 _cups_globals_t *cg; /* Global data */
123
124
125 DEBUG_printf(("cupsDoAuthentication(http=%p, method=\"%s\", resource=\"%s\")",
126 http, method, resource));
127
128 if (!http)
129 http = _cupsConnect();
130
131 if (!http || !method || !resource)
132 return (-1);
133
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
139 /*
140 * Clear the current authentication string...
141 */
142
143 httpSetAuthString(http, NULL, NULL);
144
145 /*
146 * See if we can do local authentication...
147 */
148
149 if (http->digest_tries < 3)
150 {
151 if ((localauth = cups_local_auth(http)) == 0)
152 {
153 DEBUG_printf(("2cupsDoAuthentication: authstring=\"%s\"",
154 http->authstring));
155
156 if (http->status == HTTP_STATUS_UNAUTHORIZED)
157 http->digest_tries ++;
158
159 return (0);
160 }
161 else if (localauth == -1)
162 {
163 http->status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
164 return (-1); /* Error or canceled */
165 }
166 }
167
168 /*
169 * Nope, see if we should retry the current username:password...
170 */
171
172 www_auth = http->fields[HTTP_FIELD_WWW_AUTHENTICATE];
173
174 if ((http->digest_tries > 1 || !http->userpass[0]) &&
175 (!_cups_strncasecmp(www_auth, "Basic", 5) ||
176 !_cups_strncasecmp(www_auth, "Digest", 6)))
177 {
178 /*
179 * Nope - get a new password from the user...
180 */
181
182 char default_username[HTTP_MAX_VALUE];
183 /* Default username */
184
185 cg = _cupsGlobals();
186
187 if (!cg->lang_default)
188 cg->lang_default = cupsLangDefault();
189
190 if (httpGetSubField(http, HTTP_FIELD_WWW_AUTHENTICATE, "username",
191 default_username))
192 cupsSetUser(default_username);
193
194 snprintf(prompt, sizeof(prompt),
195 _cupsLangString(cg->lang_default, _("Password for %s on %s? ")),
196 cupsUser(),
197 http->hostname[0] == '/' ? "localhost" : http->hostname);
198
199 http->digest_tries = _cups_strncasecmp(www_auth, "Digest", 6) != 0;
200 http->userpass[0] = '\0';
201
202 if ((password = cupsGetPassword2(prompt, http, method, resource)) == NULL)
203 {
204 http->status = HTTP_STATUS_CUPS_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_STATUS_UNAUTHORIZED)
212 http->digest_tries ++;
213
214 if (http->status == HTTP_STATUS_UNAUTHORIZED && http->digest_tries >= 3)
215 {
216 DEBUG_printf(("1cupsDoAuthentication: Too many authentication tries (%d)",
217 http->digest_tries));
218
219 http->status = HTTP_STATUS_CUPS_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 (!_cups_strncasecmp(www_auth, "Negotiate", 9))
229 {
230 /*
231 * Kerberos authentication...
232 */
233
234 if (_cupsSetNegotiateAuthString(http, method, resource))
235 {
236 http->status = HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED;
237 return (-1);
238 }
239 }
240 else
241 #endif /* HAVE_GSSAPI */
242 if (!_cups_strncasecmp(www_auth, "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 (!_cups_strncasecmp(www_auth, "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 www_auth));
279 http->status = HTTP_STATUS_CUPS_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 #endif /* HAVE_GSS_ACQUIRED_CRED_EX_F */
423
424 if (GSS_ERROR(major_status))
425 {
426 cups_gss_printf(major_status, minor_status,
427 "_cupsSetNegotiateAuthString: Unable to initialize "
428 "security context");
429 return (-1);
430 }
431
432 #ifdef DEBUG
433 else if (major_status == GSS_S_CONTINUE_NEEDED)
434 cups_gss_printf(major_status, minor_status,
435 "_cupsSetNegotiateAuthString: Continuation needed!");
436 #endif /* DEBUG */
437
438 if (output_token.length > 0 && output_token.length <= 65536)
439 {
440 /*
441 * Allocate the authorization string since Windows KDCs can have
442 * arbitrarily large credentials...
443 */
444
445 int authsize = 10 + /* "Negotiate " */
446 (int)output_token.length * 4 / 3 + 1 + 1;
447 /* Base64 + nul */
448
449 httpSetAuthString(http, NULL, NULL);
450
451 if ((http->authstring = malloc((size_t)authsize)) == NULL)
452 {
453 http->authstring = http->_authstring;
454 authsize = sizeof(http->_authstring);
455 }
456
457 strlcpy(http->authstring, "Negotiate ", (size_t)authsize);
458 httpEncode64_2(http->authstring + 10, authsize - 10, output_token.value,
459 (int)output_token.length);
460
461 gss_release_buffer(&minor_status, &output_token);
462 }
463 else
464 {
465 DEBUG_printf(("1_cupsSetNegotiateAuthString: Kerberos credentials too "
466 "large - %d bytes!", (int)output_token.length));
467 gss_release_buffer(&minor_status, &output_token);
468
469 return (-1);
470 }
471
472 return (0);
473 }
474
475
476 # ifdef HAVE_GSS_ACQUIRE_CRED_EX_F
477 /*
478 * 'cups_gss_acquire()' - Kerberos credentials callback.
479 */
480 static void
481 cups_gss_acquire(
482 void *ctx, /* I - Caller context */
483 OM_uint32 major, /* I - Major error code */
484 gss_status_id_t status, /* I - Status (unused) */
485 gss_cred_id_t creds, /* I - Credentials (if any) */
486 gss_OID_set oids, /* I - Mechanism OIDs (unused) */
487 OM_uint32 time_rec) /* I - Timestamp (unused) */
488 {
489 uint32_t min; /* Minor error code */
490 _cups_gss_acquire_t *data; /* Callback data */
491
492
493 (void)status;
494 (void)time_rec;
495
496 data = (_cups_gss_acquire_t *)ctx;
497 data->major = major;
498 data->creds = creds;
499
500 gss_release_oid_set(&min, &oids);
501 dispatch_semaphore_signal(data->sem);
502 }
503 # endif /* HAVE_GSS_ACQUIRE_CRED_EX_F */
504
505
506 /*
507 * 'cups_gss_getname()' - Get CUPS service credentials for authentication.
508 */
509
510 static gss_name_t /* O - Server name */
511 cups_gss_getname(
512 http_t *http, /* I - Connection to server */
513 const char *service_name) /* I - Service name */
514 {
515 gss_buffer_desc token = GSS_C_EMPTY_BUFFER;
516 /* Service token */
517 OM_uint32 major_status, /* Major status code */
518 minor_status; /* Minor status code */
519 gss_name_t server_name; /* Server name */
520 char buf[1024]; /* Name buffer */
521
522
523 DEBUG_printf(("7cups_gss_getname(http=%p, service_name=\"%s\")", http,
524 service_name));
525
526
527 /*
528 * Get the hostname...
529 */
530
531 if (!http->gsshost[0])
532 {
533 httpGetHostname(http, http->gsshost, sizeof(http->gsshost));
534
535 if (!strcmp(http->gsshost, "localhost"))
536 {
537 if (gethostname(http->gsshost, sizeof(http->gsshost)) < 0)
538 {
539 DEBUG_printf(("1cups_gss_getname: gethostname() failed: %s",
540 strerror(errno)));
541 http->gsshost[0] = '\0';
542 return (NULL);
543 }
544
545 if (!strchr(http->gsshost, '.'))
546 {
547 /*
548 * The hostname is not a FQDN, so look it up...
549 */
550
551 struct hostent *host; /* Host entry to get FQDN */
552
553 if ((host = gethostbyname(http->gsshost)) != NULL && host->h_name)
554 {
555 /*
556 * Use the resolved hostname...
557 */
558
559 strlcpy(http->gsshost, host->h_name, sizeof(http->gsshost));
560 }
561 else
562 {
563 DEBUG_printf(("1cups_gss_getname: gethostbyname(\"%s\") failed.",
564 http->gsshost));
565 http->gsshost[0] = '\0';
566 return (NULL);
567 }
568 }
569 }
570 }
571
572 /*
573 * Get a service name we can use for authentication purposes...
574 */
575
576 snprintf(buf, sizeof(buf), "%s@%s", service_name, http->gsshost);
577
578 DEBUG_printf(("8cups_gss_getname: Looking up \"%s\".", buf));
579
580 token.value = buf;
581 token.length = strlen(buf);
582 server_name = GSS_C_NO_NAME;
583 major_status = gss_import_name(&minor_status, &token,
584 GSS_C_NT_HOSTBASED_SERVICE,
585 &server_name);
586
587 if (GSS_ERROR(major_status))
588 {
589 cups_gss_printf(major_status, minor_status,
590 "cups_gss_getname: gss_import_name() failed");
591 return (NULL);
592 }
593
594 return (server_name);
595 }
596
597
598 # ifdef DEBUG
599 /*
600 * 'cups_gss_printf()' - Show debug error messages from GSSAPI.
601 */
602
603 static void
604 cups_gss_printf(OM_uint32 major_status,/* I - Major status code */
605 OM_uint32 minor_status,/* I - Minor status code */
606 const char *message) /* I - Prefix for error message */
607 {
608 OM_uint32 err_major_status, /* Major status code for display */
609 err_minor_status; /* Minor status code for display */
610 OM_uint32 msg_ctx; /* Message context */
611 gss_buffer_desc major_status_string = GSS_C_EMPTY_BUFFER,
612 /* Major status message */
613 minor_status_string = GSS_C_EMPTY_BUFFER;
614 /* Minor status message */
615
616
617 msg_ctx = 0;
618 err_major_status = gss_display_status(&err_minor_status,
619 major_status,
620 GSS_C_GSS_CODE,
621 GSS_C_NO_OID,
622 &msg_ctx,
623 &major_status_string);
624
625 if (!GSS_ERROR(err_major_status))
626 gss_display_status(&err_minor_status, minor_status, GSS_C_MECH_CODE,
627 GSS_C_NULL_OID, &msg_ctx, &minor_status_string);
628
629 DEBUG_printf(("1%s: %s, %s", message, (char *)major_status_string.value,
630 (char *)minor_status_string.value));
631
632 gss_release_buffer(&err_minor_status, &major_status_string);
633 gss_release_buffer(&err_minor_status, &minor_status_string);
634 }
635 # endif /* DEBUG */
636 #endif /* HAVE_GSSAPI */
637
638
639 /*
640 * 'cups_local_auth()' - Get the local authorization certificate if
641 * available/applicable.
642 */
643
644 static int /* O - 0 if available */
645 /* 1 if not available */
646 /* -1 error */
647 cups_local_auth(http_t *http) /* I - HTTP connection to server */
648 {
649 #if defined(WIN32) || defined(__EMX__)
650 /*
651 * Currently WIN32 and OS-2 do not support the CUPS server...
652 */
653
654 return (1);
655 #else
656 int pid; /* Current process ID */
657 FILE *fp; /* Certificate file */
658 char trc[16], /* Try Root Certificate parameter */
659 filename[1024]; /* Certificate filename */
660 _cups_globals_t *cg = _cupsGlobals(); /* Global data */
661 # if defined(HAVE_AUTHORIZATION_H)
662 OSStatus status; /* Status */
663 AuthorizationItem auth_right; /* Authorization right */
664 AuthorizationRights auth_rights; /* Authorization rights */
665 AuthorizationFlags auth_flags; /* Authorization flags */
666 AuthorizationExternalForm auth_extrn; /* Authorization ref external */
667 char auth_key[1024]; /* Buffer */
668 char buffer[1024]; /* Buffer */
669 # endif /* HAVE_AUTHORIZATION_H */
670
671
672 DEBUG_printf(("7cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"",
673 http, httpAddrString(http->hostaddr, filename, sizeof(filename)), http->hostname));
674
675 /*
676 * See if we are accessing localhost...
677 */
678
679 if (!httpAddrLocalhost(http->hostaddr) &&
680 _cups_strcasecmp(http->hostname, "localhost") != 0)
681 {
682 DEBUG_puts("8cups_local_auth: Not a local connection!");
683 return (1);
684 }
685
686 # if defined(HAVE_AUTHORIZATION_H)
687 /*
688 * Delete any previous authorization reference...
689 */
690
691 if (http->auth_ref)
692 {
693 AuthorizationFree(http->auth_ref, kAuthorizationFlagDefaults);
694 http->auth_ref = NULL;
695 }
696
697 if (!getenv("GATEWAY_INTERFACE") &&
698 httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "authkey",
699 auth_key, sizeof(auth_key)))
700 {
701 status = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment,
702 kAuthorizationFlagDefaults, &http->auth_ref);
703 if (status != errAuthorizationSuccess)
704 {
705 DEBUG_printf(("8cups_local_auth: AuthorizationCreate() returned %d (%s)",
706 (int)status, cssmErrorString(status)));
707 return (-1);
708 }
709
710 auth_right.name = auth_key;
711 auth_right.valueLength = 0;
712 auth_right.value = NULL;
713 auth_right.flags = 0;
714
715 auth_rights.count = 1;
716 auth_rights.items = &auth_right;
717
718 auth_flags = kAuthorizationFlagDefaults |
719 kAuthorizationFlagPreAuthorize |
720 kAuthorizationFlagInteractionAllowed |
721 kAuthorizationFlagExtendRights;
722
723 status = AuthorizationCopyRights(http->auth_ref, &auth_rights,
724 kAuthorizationEmptyEnvironment,
725 auth_flags, NULL);
726 if (status == errAuthorizationSuccess)
727 status = AuthorizationMakeExternalForm(http->auth_ref, &auth_extrn);
728
729 if (status == errAuthorizationSuccess)
730 {
731 /*
732 * Set the authorization string and return...
733 */
734
735 httpEncode64_2(buffer, sizeof(buffer), (void *)&auth_extrn,
736 sizeof(auth_extrn));
737
738 httpSetAuthString(http, "AuthRef", buffer);
739
740 DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
741 http->authstring));
742 return (0);
743 }
744 else if (status == errAuthorizationCanceled)
745 return (-1);
746
747 DEBUG_printf(("9cups_local_auth: AuthorizationCopyRights() returned %d (%s)",
748 (int)status, cssmErrorString(status)));
749
750 /*
751 * Fall through to try certificates...
752 */
753 }
754 # endif /* HAVE_AUTHORIZATION_H */
755
756 # if defined(SO_PEERCRED) && defined(AF_LOCAL)
757 /*
758 * See if we can authenticate using the peer credentials provided over a
759 * domain socket; if so, specify "PeerCred username" as the authentication
760 * information...
761 */
762
763 if (
764 # ifdef HAVE_GSSAPI
765 strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9) &&
766 # endif /* HAVE_GSSAPI */
767 # ifdef HAVE_AUTHORIZATION_H
768 !httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "authkey",
769 auth_key, sizeof(auth_key)) &&
770 # endif /* HAVE_AUTHORIZATION_H */
771 http->hostaddr->addr.sa_family == AF_LOCAL &&
772 !getenv("GATEWAY_INTERFACE")) /* Not via CGI programs... */
773 {
774 /*
775 * Verify that the current cupsUser() matches the current UID...
776 */
777
778 struct passwd *pwd; /* Password information */
779 const char *username; /* Current username */
780
781 username = cupsUser();
782
783 if ((pwd = getpwnam(username)) != NULL && pwd->pw_uid == getuid())
784 {
785 httpSetAuthString(http, "PeerCred", username);
786
787 DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
788 http->authstring));
789
790 return (0);
791 }
792 }
793 # endif /* SO_PEERCRED && AF_LOCAL */
794
795 /*
796 * Try opening a certificate file for this PID. If that fails,
797 * try the root certificate...
798 */
799
800 pid = getpid();
801 snprintf(filename, sizeof(filename), "%s/certs/%d", cg->cups_statedir, pid);
802 if ((fp = fopen(filename, "r")) == NULL && pid > 0)
803 {
804 /*
805 * No certificate for this PID; see if we can get the root certificate...
806 */
807
808 DEBUG_printf(("9cups_local_auth: Unable to open file %s: %s",
809 filename, strerror(errno)));
810
811 # ifdef HAVE_GSSAPI
812 if (!strncmp(http->fields[HTTP_FIELD_WWW_AUTHENTICATE], "Negotiate", 9))
813 {
814 /*
815 * Kerberos required, don't try the root certificate...
816 */
817
818 return (1);
819 }
820 # endif /* HAVE_GSSAPI */
821
822 # ifdef HAVE_AUTHORIZATION_H
823 if (httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "authkey",
824 auth_key, sizeof(auth_key)))
825 {
826 /*
827 * Don't use the root certificate as a replacement for an authkey...
828 */
829
830 return (1);
831 }
832 # endif /* HAVE_AUTHORIZATION_H */
833 if (!httpGetSubField2(http, HTTP_FIELD_WWW_AUTHENTICATE, "trc", trc,
834 sizeof(trc)))
835 {
836 /*
837 * Scheduler doesn't want us to use the root certificate...
838 */
839
840 return (1);
841 }
842
843 snprintf(filename, sizeof(filename), "%s/certs/0", cg->cups_statedir);
844 fp = fopen(filename, "r");
845 }
846
847 if (fp)
848 {
849 /*
850 * Read the certificate from the file...
851 */
852
853 char certificate[33], /* Certificate string */
854 *certptr; /* Pointer to certificate string */
855
856 certptr = fgets(certificate, sizeof(certificate), fp);
857 fclose(fp);
858
859 if (certptr)
860 {
861 /*
862 * Set the authorization string and return...
863 */
864
865 httpSetAuthString(http, "Local", certificate);
866
867 DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
868 http->authstring));
869
870 return (0);
871 }
872 }
873
874 return (1);
875 #endif /* WIN32 || __EMX__ */
876 }
877
878
879 /*
880 * End of "$Id$".
881 */