2 * Authentication functions for CUPS.
4 * Copyright © 2007-2018 by Apple Inc.
5 * Copyright © 1997-2007 by Easy Software Products.
7 * This file contains Kerberos support code, copyright 2006 by
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 * missing or damaged, see the license at "http://www.cups.org/".
16 * This file is subject to the Apple OS-Developed Software exception.
20 * Include necessary headers...
23 #include "cups-private.h"
26 #if defined(_WIN32) || defined(__EMX__)
30 #endif /* _WIN32 || __EMX__ */
32 #if HAVE_AUTHORIZATION_H
33 # include <Security/Authorization.h>
34 # ifdef HAVE_SECBASEPRIV_H
35 # include <Security/SecBasePriv.h>
37 extern const char *cssmErrorString(int error
);
38 # endif /* HAVE_SECBASEPRIV_H */
39 #endif /* HAVE_AUTHORIZATION_H */
41 #if defined(SO_PEERCRED) && defined(AF_LOCAL)
43 #endif /* SO_PEERCRED && AF_LOCAL */
50 static const char *cups_auth_find(const char *www_authenticate
, const char *scheme
);
51 static const char *cups_auth_param(const char *scheme
, const char *name
, char *value
, size_t valsize
);
52 static const char *cups_auth_scheme(const char *www_authenticate
, char *scheme
, size_t schemesize
);
55 # ifdef HAVE_GSS_ACQUIRE_CRED_EX_F
56 # ifdef HAVE_GSS_GSSAPI_SPI_H
57 # include <GSS/gssapi_spi.h>
59 # define GSS_AUTH_IDENTITY_TYPE_1 1
60 # define gss_acquire_cred_ex_f __ApplePrivate_gss_acquire_cred_ex_f
61 typedef struct gss_auth_identity
/* @private@ */
68 gss_buffer_t
*credentialsRef
;
69 } gss_auth_identity_desc
;
70 extern OM_uint32
gss_acquire_cred_ex_f(gss_status_id_t
, const gss_name_t
,
71 OM_uint32
, OM_uint32
, const gss_OID
,
72 gss_cred_usage_t
, gss_auth_identity_t
,
73 void *, void (*)(void *, OM_uint32
,
78 # endif /* HAVE_GSS_GSSAPI_SPI_H */
79 # include <dispatch/dispatch.h>
80 typedef struct _cups_gss_acquire_s
/* Acquire callback data */
82 dispatch_semaphore_t sem
; /* Synchronization semaphore */
83 OM_uint32 major
; /* Returned status code */
84 gss_cred_id_t creds
; /* Returned credentials */
85 } _cups_gss_acquire_t
;
87 static void cups_gss_acquire(void *ctx
, OM_uint32 major
,
88 gss_status_id_t status
,
89 gss_cred_id_t creds
, gss_OID_set oids
,
91 # endif /* HAVE_GSS_ACQUIRE_CRED_EX_F */
92 static gss_name_t
cups_gss_getname(http_t
*http
, const char *service_name
);
94 static void cups_gss_printf(OM_uint32 major_status
, OM_uint32 minor_status
,
97 # define cups_gss_printf(major, minor, message)
99 #endif /* HAVE_GSSAPI */
100 static int cups_local_auth(http_t
*http
);
104 * 'cupsDoAuthentication()' - Authenticate a request.
106 * This function should be called in response to a @code HTTP_STATUS_UNAUTHORIZED@
107 * status, prior to resubmitting your request.
109 * @since CUPS 1.1.20/macOS 10.4@
112 int /* O - 0 on success, -1 on error */
113 cupsDoAuthentication(
114 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
115 const char *method
, /* I - Request method ("GET", "POST", "PUT") */
116 const char *resource
) /* I - Resource path */
118 const char *password
, /* Password string */
119 *www_auth
, /* WWW-Authenticate header */
120 *schemedata
; /* Scheme-specific data */
121 char scheme
[256], /* Scheme name */
122 prompt
[1024]; /* Prompt for user */
123 int localauth
; /* Local authentication result */
124 _cups_globals_t
*cg
; /* Global data */
127 DEBUG_printf(("cupsDoAuthentication(http=%p, method=\"%s\", resource=\"%s\")", (void *)http
, method
, resource
));
130 http
= _cupsConnect();
132 if (!http
|| !method
|| !resource
)
135 DEBUG_printf(("2cupsDoAuthentication: digest_tries=%d, userpass=\"%s\"",
136 http
->digest_tries
, http
->userpass
));
137 DEBUG_printf(("2cupsDoAuthentication: WWW-Authenticate=\"%s\"",
138 httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
)));
141 * Clear the current authentication string...
144 httpSetAuthString(http
, NULL
, NULL
);
147 * See if we can do local authentication...
150 if (http
->digest_tries
< 3)
152 if ((localauth
= cups_local_auth(http
)) == 0)
154 DEBUG_printf(("2cupsDoAuthentication: authstring=\"%s\"",
157 if (http
->status
== HTTP_STATUS_UNAUTHORIZED
)
158 http
->digest_tries
++;
162 else if (localauth
== -1)
164 http
->status
= HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
;
165 return (-1); /* Error or canceled */
170 * Nope, loop through the authentication schemes to find the first we support.
173 www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
175 for (schemedata
= cups_auth_scheme(www_auth
, scheme
, sizeof(scheme
)); schemedata
; schemedata
= cups_auth_scheme(schemedata
+ strlen(scheme
), scheme
, sizeof(scheme
)))
178 * Check the scheme name...
182 if (!_cups_strcasecmp(scheme
, "Negotiate"))
185 * Kerberos authentication...
188 if (_cupsSetNegotiateAuthString(http
, method
, resource
))
190 http
->status
= HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
;
197 #endif /* HAVE_GSSAPI */
198 if (_cups_strcasecmp(scheme
, "Basic") && _cups_strcasecmp(scheme
, "Digest"))
199 continue; /* Not supported (yet) */
202 * See if we should retry the current username:password...
205 if ((http
->digest_tries
> 1 || !http
->userpass
[0]) && (!_cups_strcasecmp(scheme
, "Basic") || (!_cups_strcasecmp(scheme
, "Digest"))))
208 * Nope - get a new password from the user...
211 char default_username
[HTTP_MAX_VALUE
];
212 /* Default username */
216 if (!cg
->lang_default
)
217 cg
->lang_default
= cupsLangDefault();
219 if (cups_auth_param(schemedata
, "username", default_username
, sizeof(default_username
)))
220 cupsSetUser(default_username
);
222 snprintf(prompt
, sizeof(prompt
), _cupsLangString(cg
->lang_default
, _("Password for %s on %s? ")), cupsUser(), http
->hostname
[0] == '/' ? "localhost" : http
->hostname
);
224 http
->digest_tries
= _cups_strncasecmp(scheme
, "Digest", 6) != 0;
225 http
->userpass
[0] = '\0';
227 if ((password
= cupsGetPassword2(prompt
, http
, method
, resource
)) == NULL
)
229 http
->status
= HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
;
233 snprintf(http
->userpass
, sizeof(http
->userpass
), "%s:%s", cupsUser(), password
);
235 else if (http
->status
== HTTP_STATUS_UNAUTHORIZED
)
236 http
->digest_tries
++;
238 if (http
->status
== HTTP_STATUS_UNAUTHORIZED
&& http
->digest_tries
>= 3)
240 DEBUG_printf(("1cupsDoAuthentication: Too many authentication tries (%d)", http
->digest_tries
));
242 http
->status
= HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
;
247 * Got a password; encode it for the server...
250 if (!_cups_strcasecmp(scheme
, "Basic"))
253 * Basic authentication...
256 char encode
[256]; /* Base64 buffer */
258 httpEncode64_2(encode
, sizeof(encode
), http
->userpass
, (int)strlen(http
->userpass
));
259 httpSetAuthString(http
, "Basic", encode
);
262 else if (!_cups_strcasecmp(scheme
, "Digest"))
265 * Digest authentication...
268 char nonce
[HTTP_MAX_VALUE
]; /* nonce="xyz" string */
270 cups_auth_param(schemedata
, "algorithm", http
->algorithm
, sizeof(http
->algorithm
));
271 cups_auth_param(schemedata
, "opaque", http
->opaque
, sizeof(http
->opaque
));
272 cups_auth_param(schemedata
, "nonce", nonce
, sizeof(nonce
));
273 cups_auth_param(schemedata
, "realm", http
->realm
, sizeof(http
->realm
));
275 if (_httpSetDigestAuthString(http
, nonce
, method
, resource
))
280 if (http
->authstring
)
282 DEBUG_printf(("1cupsDoAuthentication: authstring=\"%s\"", http
->authstring
));
288 DEBUG_printf(("1cupsDoAuthentication: Unknown auth type: \"%s\"", www_auth
));
289 http
->status
= HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
;
298 * '_cupsSetNegotiateAuthString()' - Set the Kerberos authentication string.
301 int /* O - 0 on success, -1 on error */
302 _cupsSetNegotiateAuthString(
303 http_t
*http
, /* I - Connection to server */
304 const char *method
, /* I - Request method ("GET", "POST", "PUT") */
305 const char *resource
) /* I - Resource path */
307 OM_uint32 minor_status
, /* Minor status code */
308 major_status
; /* Major status code */
309 gss_buffer_desc output_token
= GSS_C_EMPTY_BUFFER
;
318 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
322 if (&gss_init_sec_context
== NULL
)
324 DEBUG_puts("1_cupsSetNegotiateAuthString: Weak-linked GSSAPI/Kerberos "
325 "framework is not present");
328 # endif /* __APPLE__ */
330 if (http
->gssname
== GSS_C_NO_NAME
)
332 http
->gssname
= cups_gss_getname(http
, _cupsGSSServiceName());
335 if (http
->gssctx
!= GSS_C_NO_CONTEXT
)
337 gss_delete_sec_context(&minor_status
, &http
->gssctx
, GSS_C_NO_BUFFER
);
338 http
->gssctx
= GSS_C_NO_CONTEXT
;
341 major_status
= gss_init_sec_context(&minor_status
, GSS_C_NO_CREDENTIAL
,
343 http
->gssname
, http
->gssmech
,
344 GSS_C_MUTUAL_FLAG
| GSS_C_INTEG_FLAG
,
346 GSS_C_NO_CHANNEL_BINDINGS
,
347 GSS_C_NO_BUFFER
, &http
->gssmech
,
348 &output_token
, NULL
, NULL
);
350 # ifdef HAVE_GSS_ACQUIRE_CRED_EX_F
351 if (major_status
== GSS_S_NO_CRED
)
354 * Ask the user for credentials...
357 char prompt
[1024], /* Prompt for user */
358 userbuf
[256]; /* Kerberos username */
359 const char *username
, /* Username string */
360 *password
; /* Password string */
361 _cups_gss_acquire_t data
; /* Callback data */
362 gss_auth_identity_desc identity
; /* Kerberos user identity */
363 _cups_globals_t
*cg
= _cupsGlobals();
364 /* Per-thread global data */
366 if (!cg
->lang_default
)
367 cg
->lang_default
= cupsLangDefault();
369 snprintf(prompt
, sizeof(prompt
),
370 _cupsLangString(cg
->lang_default
, _("Password for %s on %s? ")),
371 cupsUser(), http
->gsshost
);
373 if ((password
= cupsGetPassword2(prompt
, http
, method
, resource
)) == NULL
)
377 * Try to acquire credentials...
380 username
= cupsUser();
381 if (!strchr(username
, '@'))
383 snprintf(userbuf
, sizeof(userbuf
), "%s@%s", username
, http
->gsshost
);
387 identity
.type
= GSS_AUTH_IDENTITY_TYPE_1
;
389 identity
.username
= (char *)username
;
390 identity
.realm
= (char *)"";
391 identity
.password
= (char *)password
;
392 identity
.credentialsRef
= NULL
;
394 data
.sem
= dispatch_semaphore_create(0);
400 major_status
= gss_acquire_cred_ex_f(NULL
, GSS_C_NO_NAME
, 0, GSS_C_INDEFINITE
, GSS_KRB5_MECHANISM
, GSS_C_INITIATE
, (gss_auth_identity_t
)&identity
, &data
, cups_gss_acquire
);
402 if (major_status
== GSS_S_COMPLETE
)
404 dispatch_semaphore_wait(data
.sem
, DISPATCH_TIME_FOREVER
);
405 major_status
= data
.major
;
408 dispatch_release(data
.sem
);
410 if (major_status
== GSS_S_COMPLETE
)
412 OM_uint32 release_minor
; /* Minor status from releasing creds */
414 major_status
= gss_init_sec_context(&minor_status
, data
.creds
,
416 http
->gssname
, http
->gssmech
,
417 GSS_C_MUTUAL_FLAG
| GSS_C_INTEG_FLAG
,
419 GSS_C_NO_CHANNEL_BINDINGS
,
420 GSS_C_NO_BUFFER
, &http
->gssmech
,
421 &output_token
, NULL
, NULL
);
422 gss_release_cred(&release_minor
, &data
.creds
);
426 # endif /* HAVE_GSS_ACQUIRED_CRED_EX_F */
428 if (GSS_ERROR(major_status
))
430 cups_gss_printf(major_status
, minor_status
,
431 "_cupsSetNegotiateAuthString: Unable to initialize "
437 else if (major_status
== GSS_S_CONTINUE_NEEDED
)
438 cups_gss_printf(major_status
, minor_status
,
439 "_cupsSetNegotiateAuthString: Continuation needed!");
442 if (output_token
.length
> 0 && output_token
.length
<= 65536)
445 * Allocate the authorization string since Windows KDCs can have
446 * arbitrarily large credentials...
449 int authsize
= 10 + /* "Negotiate " */
450 (int)output_token
.length
* 4 / 3 + 1 + 1;
453 httpSetAuthString(http
, NULL
, NULL
);
455 if ((http
->authstring
= malloc((size_t)authsize
)) == NULL
)
457 http
->authstring
= http
->_authstring
;
458 authsize
= sizeof(http
->_authstring
);
461 strlcpy(http
->authstring
, "Negotiate ", (size_t)authsize
);
462 httpEncode64_2(http
->authstring
+ 10, authsize
- 10, output_token
.value
,
463 (int)output_token
.length
);
465 gss_release_buffer(&minor_status
, &output_token
);
469 DEBUG_printf(("1_cupsSetNegotiateAuthString: Kerberos credentials too "
470 "large - %d bytes!", (int)output_token
.length
));
471 gss_release_buffer(&minor_status
, &output_token
);
478 #endif /* HAVE_GSSAPI */
482 * 'cups_auth_find()' - Find the named WWW-Authenticate scheme.
484 * The "www_authenticate" parameter points to the current position in the header.
486 * Returns @code NULL@ if the auth scheme is not present.
489 static const char * /* O - Start of matching scheme or @code NULL@ if not found */
490 cups_auth_find(const char *www_authenticate
, /* I - Pointer into WWW-Authenticate header */
491 const char *scheme
) /* I - Authentication scheme */
493 size_t schemelen
= strlen(scheme
); /* Length of scheme */
496 DEBUG_printf(("8cups_auth_find(www_authenticate=\"%s\", scheme=\"%s\"(%d))", www_authenticate
, scheme
, (int)schemelen
));
498 while (*www_authenticate
)
501 * Skip leading whitespace and commas...
504 DEBUG_printf(("9cups_auth_find: Before whitespace: \"%s\"", www_authenticate
));
505 while (isspace(*www_authenticate
& 255) || *www_authenticate
== ',')
507 DEBUG_printf(("9cups_auth_find: After whitespace: \"%s\"", www_authenticate
));
510 * See if this is "Scheme" followed by whitespace or the end of the string.
513 if (!strncmp(www_authenticate
, scheme
, schemelen
) && (isspace(www_authenticate
[schemelen
] & 255) || www_authenticate
[schemelen
] == ',' || !www_authenticate
[schemelen
]))
516 * Yes, this is the start of the scheme-specific information...
519 DEBUG_printf(("9cups_auth_find: Returning \"%s\".", www_authenticate
));
521 return (www_authenticate
);
525 * Skip the scheme name or param="value" string...
528 while (!isspace(*www_authenticate
& 255) && *www_authenticate
)
530 if (*www_authenticate
== '\"')
533 * Skip quoted value...
537 while (*www_authenticate
&& *www_authenticate
!= '\"')
540 DEBUG_printf(("9cups_auth_find: After quoted: \"%s\"", www_authenticate
));
546 DEBUG_printf(("9cups_auth_find: After skip: \"%s\"", www_authenticate
));
549 DEBUG_puts("9cups_auth_find: Returning NULL.");
556 * 'cups_auth_param()' - Copy the value for the named authentication parameter,
560 static const char * /* O - Parameter value or @code NULL@ if not present */
561 cups_auth_param(const char *scheme
, /* I - Pointer to auth data */
562 const char *name
, /* I - Name of parameter */
563 char *value
, /* I - Value buffer */
564 size_t valsize
) /* I - Size of value buffer */
566 char *valptr
= value
, /* Pointer into value buffer */
567 *valend
= value
+ valsize
- 1; /* Pointer to end of buffer */
568 size_t namelen
= strlen(name
); /* Name length */
569 int param
; /* Is this a parameter? */
572 DEBUG_printf(("8cups_auth_param(scheme=\"%s\", name=\"%s\", value=%p, valsize=%d)", scheme
, name
, (void *)value
, (int)valsize
));
574 while (!isspace(*scheme
& 255) && *scheme
)
579 while (isspace(*scheme
& 255) || *scheme
== ',')
582 if (!strncmp(scheme
, name
, namelen
) && scheme
[namelen
] == '=')
585 * Found the parameter, copy the value...
588 scheme
+= namelen
+ 1;
593 while (*scheme
&& *scheme
!= '\"')
603 while (*scheme
&& strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~+/=", *scheme
))
614 DEBUG_printf(("9cups_auth_param: Returning \"%s\".", value
));
620 * Skip the param=value string...
625 while (!isspace(*scheme
& 255) && *scheme
)
629 else if (*scheme
== '\"')
632 * Skip quoted value...
636 while (*scheme
&& *scheme
!= '\"')
644 * If this wasn't a parameter, we are at the end of this scheme's
654 DEBUG_puts("9cups_auth_param: Returning NULL.");
661 * 'cups_auth_scheme()' - Get the (next) WWW-Authenticate scheme.
663 * The "www_authenticate" parameter points to the current position in the header.
665 * Returns @code NULL@ if there are no (more) auth schemes present.
668 static const char * /* O - Start of scheme or @code NULL@ if not found */
669 cups_auth_scheme(const char *www_authenticate
, /* I - Pointer into WWW-Authenticate header */
670 char *scheme
, /* I - Scheme name buffer */
671 size_t schemesize
) /* I - Size of buffer */
673 const char *start
; /* Start of scheme data */
674 char *sptr
= scheme
, /* Pointer into scheme buffer */
675 *send
= scheme
+ schemesize
- 1;/* End of scheme buffer */
676 int param
; /* Is this a parameter? */
679 DEBUG_printf(("8cups_auth_scheme(www_authenticate=\"%s\", scheme=%p, schemesize=%d)", www_authenticate
, (void *)scheme
, (int)schemesize
));
681 while (*www_authenticate
)
684 * Skip leading whitespace and commas...
687 while (isspace(*www_authenticate
& 255) || *www_authenticate
== ',')
691 * Parse the scheme name or param="value" string...
694 for (sptr
= scheme
, start
= www_authenticate
, param
= 0; *www_authenticate
&& *www_authenticate
!= ',' && !isspace(*www_authenticate
& 255); www_authenticate
++)
696 if (*www_authenticate
== '=')
698 else if (!param
&& sptr
< send
)
699 *sptr
++ = *www_authenticate
;
700 else if (*www_authenticate
== '\"')
703 * Skip quoted value...
707 while (*www_authenticate
&& *www_authenticate
!= '\"')
712 if (sptr
> scheme
&& !param
)
716 DEBUG_printf(("9cups_auth_scheme: Returning \"%s\".", start
));
724 DEBUG_puts("9cups_auth_scheme: Returning NULL.");
731 # ifdef HAVE_GSS_ACQUIRE_CRED_EX_F
733 * 'cups_gss_acquire()' - Kerberos credentials callback.
737 void *ctx
, /* I - Caller context */
738 OM_uint32 major
, /* I - Major error code */
739 gss_status_id_t status
, /* I - Status (unused) */
740 gss_cred_id_t creds
, /* I - Credentials (if any) */
741 gss_OID_set oids
, /* I - Mechanism OIDs (unused) */
742 OM_uint32 time_rec
) /* I - Timestamp (unused) */
744 uint32_t min
; /* Minor error code */
745 _cups_gss_acquire_t
*data
; /* Callback data */
751 data
= (_cups_gss_acquire_t
*)ctx
;
755 gss_release_oid_set(&min
, &oids
);
756 dispatch_semaphore_signal(data
->sem
);
758 # endif /* HAVE_GSS_ACQUIRE_CRED_EX_F */
762 * 'cups_gss_getname()' - Get CUPS service credentials for authentication.
765 static gss_name_t
/* O - Server name */
767 http_t
*http
, /* I - Connection to server */
768 const char *service_name
) /* I - Service name */
770 gss_buffer_desc token
= GSS_C_EMPTY_BUFFER
;
772 OM_uint32 major_status
, /* Major status code */
773 minor_status
; /* Minor status code */
774 gss_name_t server_name
; /* Server name */
775 char buf
[1024]; /* Name buffer */
778 DEBUG_printf(("7cups_gss_getname(http=%p, service_name=\"%s\")", http
,
783 * Get the hostname...
786 if (!http
->gsshost
[0])
788 httpGetHostname(http
, http
->gsshost
, sizeof(http
->gsshost
));
790 if (!strcmp(http
->gsshost
, "localhost"))
792 if (gethostname(http
->gsshost
, sizeof(http
->gsshost
)) < 0)
794 DEBUG_printf(("1cups_gss_getname: gethostname() failed: %s",
796 http
->gsshost
[0] = '\0';
800 if (!strchr(http
->gsshost
, '.'))
803 * The hostname is not a FQDN, so look it up...
806 struct hostent
*host
; /* Host entry to get FQDN */
808 if ((host
= gethostbyname(http
->gsshost
)) != NULL
&& host
->h_name
)
811 * Use the resolved hostname...
814 strlcpy(http
->gsshost
, host
->h_name
, sizeof(http
->gsshost
));
818 DEBUG_printf(("1cups_gss_getname: gethostbyname(\"%s\") failed.",
820 http
->gsshost
[0] = '\0';
828 * Get a service name we can use for authentication purposes...
831 snprintf(buf
, sizeof(buf
), "%s@%s", service_name
, http
->gsshost
);
833 DEBUG_printf(("8cups_gss_getname: Looking up \"%s\".", buf
));
836 token
.length
= strlen(buf
);
837 server_name
= GSS_C_NO_NAME
;
838 major_status
= gss_import_name(&minor_status
, &token
,
839 GSS_C_NT_HOSTBASED_SERVICE
,
842 if (GSS_ERROR(major_status
))
844 cups_gss_printf(major_status
, minor_status
,
845 "cups_gss_getname: gss_import_name() failed");
849 return (server_name
);
855 * 'cups_gss_printf()' - Show debug error messages from GSSAPI.
859 cups_gss_printf(OM_uint32 major_status
,/* I - Major status code */
860 OM_uint32 minor_status
,/* I - Minor status code */
861 const char *message
) /* I - Prefix for error message */
863 OM_uint32 err_major_status
, /* Major status code for display */
864 err_minor_status
; /* Minor status code for display */
865 OM_uint32 msg_ctx
; /* Message context */
866 gss_buffer_desc major_status_string
= GSS_C_EMPTY_BUFFER
,
867 /* Major status message */
868 minor_status_string
= GSS_C_EMPTY_BUFFER
;
869 /* Minor status message */
873 err_major_status
= gss_display_status(&err_minor_status
,
878 &major_status_string
);
880 if (!GSS_ERROR(err_major_status
))
881 gss_display_status(&err_minor_status
, minor_status
, GSS_C_MECH_CODE
,
882 GSS_C_NULL_OID
, &msg_ctx
, &minor_status_string
);
884 DEBUG_printf(("1%s: %s, %s", message
, (char *)major_status_string
.value
,
885 (char *)minor_status_string
.value
));
887 gss_release_buffer(&err_minor_status
, &major_status_string
);
888 gss_release_buffer(&err_minor_status
, &minor_status_string
);
891 #endif /* HAVE_GSSAPI */
895 * 'cups_local_auth()' - Get the local authorization certificate if
896 * available/applicable.
899 static int /* O - 0 if available */
900 /* 1 if not available */
902 cups_local_auth(http_t
*http
) /* I - HTTP connection to server */
904 #if defined(_WIN32) || defined(__EMX__)
906 * Currently _WIN32 and OS-2 do not support the CUPS server...
911 int pid
; /* Current process ID */
912 FILE *fp
; /* Certificate file */
913 char trc
[16], /* Try Root Certificate parameter */
914 filename
[1024]; /* Certificate filename */
915 const char *www_auth
, /* WWW-Authenticate header */
916 *schemedata
; /* Data for the named auth scheme */
917 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
918 # if defined(HAVE_AUTHORIZATION_H)
919 OSStatus status
; /* Status */
920 AuthorizationItem auth_right
; /* Authorization right */
921 AuthorizationRights auth_rights
; /* Authorization rights */
922 AuthorizationFlags auth_flags
; /* Authorization flags */
923 AuthorizationExternalForm auth_extrn
; /* Authorization ref external */
924 char auth_key
[1024]; /* Buffer */
925 char buffer
[1024]; /* Buffer */
926 # endif /* HAVE_AUTHORIZATION_H */
929 DEBUG_printf(("7cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"", (void *)http
, httpAddrString(http
->hostaddr
, filename
, sizeof(filename
)), http
->hostname
));
932 * See if we are accessing localhost...
935 if (!httpAddrLocalhost(http
->hostaddr
) && _cups_strcasecmp(http
->hostname
, "localhost") != 0)
937 DEBUG_puts("8cups_local_auth: Not a local connection!");
941 www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
943 # if defined(HAVE_AUTHORIZATION_H)
945 * Delete any previous authorization reference...
950 AuthorizationFree(http
->auth_ref
, kAuthorizationFlagDefaults
);
951 http
->auth_ref
= NULL
;
954 if (!getenv("GATEWAY_INTERFACE") && (schemedata
= cups_auth_find(www_auth
, "AuthRef")) != NULL
&& cups_auth_param(schemedata
, "key", auth_key
, sizeof(auth_key
)))
956 status
= AuthorizationCreate(NULL
, kAuthorizationEmptyEnvironment
, kAuthorizationFlagDefaults
, &http
->auth_ref
);
957 if (status
!= errAuthorizationSuccess
)
959 DEBUG_printf(("8cups_local_auth: AuthorizationCreate() returned %d (%s)",
960 (int)status
, cssmErrorString(status
)));
964 auth_right
.name
= auth_key
;
965 auth_right
.valueLength
= 0;
966 auth_right
.value
= NULL
;
967 auth_right
.flags
= 0;
969 auth_rights
.count
= 1;
970 auth_rights
.items
= &auth_right
;
972 auth_flags
= kAuthorizationFlagDefaults
|
973 kAuthorizationFlagPreAuthorize
|
974 kAuthorizationFlagInteractionAllowed
|
975 kAuthorizationFlagExtendRights
;
977 status
= AuthorizationCopyRights(http
->auth_ref
, &auth_rights
,
978 kAuthorizationEmptyEnvironment
,
980 if (status
== errAuthorizationSuccess
)
981 status
= AuthorizationMakeExternalForm(http
->auth_ref
, &auth_extrn
);
983 if (status
== errAuthorizationSuccess
)
986 * Set the authorization string and return...
989 httpEncode64_2(buffer
, sizeof(buffer
), (void *)&auth_extrn
,
992 httpSetAuthString(http
, "AuthRef", buffer
);
994 DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
998 else if (status
== errAuthorizationCanceled
)
1001 DEBUG_printf(("9cups_local_auth: AuthorizationCopyRights() returned %d (%s)",
1002 (int)status
, cssmErrorString(status
)));
1005 * Fall through to try certificates...
1008 # endif /* HAVE_AUTHORIZATION_H */
1011 if (cups_auth_find(www_auth
, "Negotiate"))
1013 # endif /* HAVE_GSSAPI */
1015 # if defined(SO_PEERCRED) && defined(AF_LOCAL)
1017 * See if we can authenticate using the peer credentials provided over a
1018 * domain socket; if so, specify "PeerCred username" as the authentication
1022 if (http
->hostaddr
->addr
.sa_family
== AF_LOCAL
&&
1023 !getenv("GATEWAY_INTERFACE") && /* Not via CGI programs... */
1024 cups_auth_find(www_auth
, "PeerCred"))
1027 * Verify that the current cupsUser() matches the current UID...
1030 struct passwd
*pwd
; /* Password information */
1031 const char *username
; /* Current username */
1033 username
= cupsUser();
1035 if ((pwd
= getpwnam(username
)) != NULL
&& pwd
->pw_uid
== getuid())
1037 httpSetAuthString(http
, "PeerCred", username
);
1039 DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
1045 # endif /* SO_PEERCRED && AF_LOCAL */
1047 if ((schemedata
= cups_auth_find(www_auth
, "Local")) == NULL
)
1051 * Try opening a certificate file for this PID. If that fails,
1052 * try the root certificate...
1056 snprintf(filename
, sizeof(filename
), "%s/certs/%d", cg
->cups_statedir
, pid
);
1057 if ((fp
= fopen(filename
, "r")) == NULL
&& pid
> 0)
1060 * No certificate for this PID; see if we can get the root certificate...
1063 DEBUG_printf(("9cups_local_auth: Unable to open file \"%s\": %s", filename
, strerror(errno
)));
1065 if (!cups_auth_param(schemedata
, "trc", trc
, sizeof(trc
)))
1068 * Scheduler doesn't want us to use the root certificate...
1074 snprintf(filename
, sizeof(filename
), "%s/certs/0", cg
->cups_statedir
);
1075 if ((fp
= fopen(filename
, "r")) == NULL
)
1076 DEBUG_printf(("9cups_local_auth: Unable to open file \"%s\": %s", filename
, strerror(errno
)));
1082 * Read the certificate from the file...
1085 char certificate
[33], /* Certificate string */
1086 *certptr
; /* Pointer to certificate string */
1088 certptr
= fgets(certificate
, sizeof(certificate
), fp
);
1094 * Set the authorization string and return...
1097 httpSetAuthString(http
, "Local", certificate
);
1099 DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
1107 #endif /* _WIN32 || __EMX__ */