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