2 * Authentication functions for CUPS.
4 * Copyright 2007-2017 by Apple Inc.
5 * Copyright 1997-2007 by Easy Software Products.
7 * This file contains Kerberos support code, copyright 2006 by
10 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
14 * Include necessary headers...
17 #include "cups-private.h"
20 #if defined(WIN32) || defined(__EMX__)
24 #endif /* WIN32 || __EMX__ */
26 #if HAVE_AUTHORIZATION_H
27 # include <Security/Authorization.h>
28 # ifdef HAVE_SECBASEPRIV_H
29 # include <Security/SecBasePriv.h>
31 extern const char *cssmErrorString(int error
);
32 # endif /* HAVE_SECBASEPRIV_H */
33 #endif /* HAVE_AUTHORIZATION_H */
35 #if defined(SO_PEERCRED) && defined(AF_LOCAL)
37 #endif /* SO_PEERCRED && AF_LOCAL */
44 static const char *cups_auth_find(const char *www_authenticate
, const char *scheme
);
45 static const char *cups_auth_param(const char *scheme
, const char *name
, char *value
, size_t valsize
);
46 static const char *cups_auth_scheme(const char *www_authenticate
, char *scheme
, size_t schemesize
);
49 # ifdef HAVE_GSS_ACQUIRE_CRED_EX_F
50 # ifdef HAVE_GSS_GSSAPI_SPI_H
51 # include <GSS/gssapi_spi.h>
53 # define GSS_AUTH_IDENTITY_TYPE_1 1
54 # define gss_acquire_cred_ex_f __ApplePrivate_gss_acquire_cred_ex_f
55 typedef struct gss_auth_identity
/* @private@ */
62 gss_buffer_t
*credentialsRef
;
63 } gss_auth_identity_desc
;
64 extern OM_uint32
gss_acquire_cred_ex_f(gss_status_id_t
, const gss_name_t
,
65 OM_uint32
, OM_uint32
, const gss_OID
,
66 gss_cred_usage_t
, gss_auth_identity_t
,
67 void *, void (*)(void *, OM_uint32
,
72 # endif /* HAVE_GSS_GSSAPI_SPI_H */
73 # include <dispatch/dispatch.h>
74 typedef struct _cups_gss_acquire_s
/* Acquire callback data */
76 dispatch_semaphore_t sem
; /* Synchronization semaphore */
77 OM_uint32 major
; /* Returned status code */
78 gss_cred_id_t creds
; /* Returned credentials */
79 } _cups_gss_acquire_t
;
81 static void cups_gss_acquire(void *ctx
, OM_uint32 major
,
82 gss_status_id_t status
,
83 gss_cred_id_t creds
, gss_OID_set oids
,
85 # endif /* HAVE_GSS_ACQUIRE_CRED_EX_F */
86 static gss_name_t
cups_gss_getname(http_t
*http
, const char *service_name
);
88 static void cups_gss_printf(OM_uint32 major_status
, OM_uint32 minor_status
,
91 # define cups_gss_printf(major, minor, message)
93 #endif /* HAVE_GSSAPI */
94 static int cups_local_auth(http_t
*http
);
98 * 'cupsDoAuthentication()' - Authenticate a request.
100 * This function should be called in response to a @code HTTP_STATUS_UNAUTHORIZED@
101 * status, prior to resubmitting your request.
103 * @since CUPS 1.1.20/macOS 10.4@
106 int /* O - 0 on success, -1 on error */
107 cupsDoAuthentication(
108 http_t
*http
, /* I - Connection to server or @code CUPS_HTTP_DEFAULT@ */
109 const char *method
, /* I - Request method ("GET", "POST", "PUT") */
110 const char *resource
) /* I - Resource path */
112 const char *password
, /* Password string */
113 *www_auth
, /* WWW-Authenticate header */
114 *schemedata
; /* Scheme-specific data */
115 char scheme
[256], /* Scheme name */
116 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 */
123 DEBUG_printf(("cupsDoAuthentication(http=%p, method=\"%s\", resource=\"%s\")", (void *)http
, method
, resource
));
126 http
= _cupsConnect();
128 if (!http
|| !method
|| !resource
)
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
)));
137 * Clear the current authentication string...
140 httpSetAuthString(http
, NULL
, NULL
);
143 * See if we can do local authentication...
146 if (http
->digest_tries
< 3)
148 if ((localauth
= cups_local_auth(http
)) == 0)
150 DEBUG_printf(("2cupsDoAuthentication: authstring=\"%s\"",
153 if (http
->status
== HTTP_STATUS_UNAUTHORIZED
)
154 http
->digest_tries
++;
158 else if (localauth
== -1)
160 http
->status
= HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
;
161 return (-1); /* Error or canceled */
166 * Nope, loop through the authentication schemes to find the first we support.
169 www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
171 for (schemedata
= cups_auth_scheme(www_auth
, scheme
, sizeof(scheme
)); schemedata
; schemedata
= cups_auth_scheme(schemedata
+ strlen(scheme
), scheme
, sizeof(scheme
)))
174 * Check the scheme name...
178 if (!_cups_strcasecmp(scheme
, "Negotiate"))
181 * Kerberos authentication...
184 if (_cupsSetNegotiateAuthString(http
, method
, resource
))
186 http
->status
= HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
;
193 #endif /* HAVE_GSSAPI */
194 if (_cups_strcasecmp(scheme
, "Basic") && _cups_strcasecmp(scheme
, "Digest"))
195 continue; /* Not supported (yet) */
198 * See if we should retry the current username:password...
201 if ((http
->digest_tries
> 1 || !http
->userpass
[0]) && (!_cups_strcasecmp(scheme
, "Basic") || (!_cups_strcasecmp(scheme
, "Digest"))))
204 * Nope - get a new password from the user...
207 char default_username
[HTTP_MAX_VALUE
];
208 /* Default username */
212 if (!cg
->lang_default
)
213 cg
->lang_default
= cupsLangDefault();
215 if (cups_auth_param(scheme
, "username", default_username
, sizeof(default_username
)))
216 cupsSetUser(default_username
);
218 snprintf(prompt
, sizeof(prompt
), _cupsLangString(cg
->lang_default
, _("Password for %s on %s? ")), cupsUser(), http
->hostname
[0] == '/' ? "localhost" : http
->hostname
);
220 http
->digest_tries
= _cups_strncasecmp(scheme
, "Digest", 6) != 0;
221 http
->userpass
[0] = '\0';
223 if ((password
= cupsGetPassword2(prompt
, http
, method
, resource
)) == NULL
)
225 http
->status
= HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
;
229 snprintf(http
->userpass
, sizeof(http
->userpass
), "%s:%s", cupsUser(), password
);
231 else if (http
->status
== HTTP_STATUS_UNAUTHORIZED
)
232 http
->digest_tries
++;
234 if (http
->status
== HTTP_STATUS_UNAUTHORIZED
&& http
->digest_tries
>= 3)
236 DEBUG_printf(("1cupsDoAuthentication: Too many authentication tries (%d)", http
->digest_tries
));
238 http
->status
= HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
;
243 * Got a password; encode it for the server...
246 if (!_cups_strcasecmp(scheme
, "Basic"))
249 * Basic authentication...
252 char encode
[256]; /* Base64 buffer */
254 httpEncode64_2(encode
, sizeof(encode
), http
->userpass
, (int)strlen(http
->userpass
));
255 httpSetAuthString(http
, "Basic", encode
);
257 else if (!_cups_strcasecmp(scheme
, "Digest"))
260 * Digest authentication...
263 int i
; /* Looping var */
264 char algorithm
[65], /* Hashing algorithm */
265 opaque
[HTTP_MAX_VALUE
],
266 /* Opaque data from server */
267 cnonce
[65], /* cnonce value */
268 kd
[65], /* Final MD5/SHA-256 digest */
269 ha1
[65], /* Hash of username:realm:password */
270 ha2
[65], /* Hash of method:request-uri */
271 hdata
[65], /* Hash of auth data */
272 temp
[1024], /* Temporary string */
273 digest
[1024]; /* Digest auth data */
274 unsigned char hash
[32]; /* Hash buffer */
275 const char *hashalg
; /* Hashing algorithm */
276 size_t hashsize
; /* Size of hash */
278 if (strcmp(nonce
, http
->nonce
))
280 strlcpy(http
->nonce
, nonce
, sizeof(http
->nonce
));
281 http
->nonce_count
= 1;
284 http
->nonce_count
++;
286 cups_auth_param(schemedata
, "opaque", opaque
, sizeof(opaque
));
287 cups_auth_param(schemedata
, "nonce", nonce
, sizeof(nonce
));
288 cups_auth_param(schemedata
, "realm", realm
, sizeof(realm
));
290 for (i
= 0; i
< 64; i
++)
291 cnonce
[i
] = "0123456789ABCDEF"[CUPS_RAND() & 15];
294 if (cups_auth_param(schemedata
, "algorithm", algorithm
, sizeof(algorithm
)))
297 * Follow RFC 2617/7616...
300 if (!_cups_strcasecmp(algorithm
, "MD5"))
303 * RFC 2617 Digest with MD5
308 else if (!_cups_strcasecmp(algorithm
, "SHA-256"))
311 * RFC 7616 Digest with SHA-256
314 hashalg
= "sha2-256";
319 * Some other algorithm we don't support, skip this one...
326 * Calculate digest value...
329 /* H(A1) = H(username:realm:password) */
330 snprintf(temp
, sizeof(temp
), "%s:%s:%s", cupsUser(), realm
, strchr(http
->userpass
, ':') + 1);
331 hashsize
= (size_t)cupsHashData(hashalg
, (unsigned char *)temp
, strlen(temp
), hash
, sizeof(hash
));
332 cupsHashString(hash
, hashsize
, ha1
, sizeof(ha1
));
334 /* H(A2) = H(method:uri) */
335 snprintf(temp
, sizeof(temp
), "%s:%s", method
, resource
);
336 hashsize
= (size_t)cupsHashData(hashalg
, (unsigned char *)temp
, strlen(temp
), hash
, sizeof(hash
));
337 cupsHashString(hash
, hashsize
, ha2
, sizeof(ha2
));
339 /* H(data) = H(nonce:nc:cnonce:qop:H(A2)) */
340 snprintf(temp
, sizeof(temp
), "%s:%08x:%s:auth:%s", nonce
, http
->nonce_count
, cnonce
, ha2
);
341 hashsize
= (size_t)cupsHashData(hashalg
, (unsigned char *)temp
, strlen(temp
), hash
, sizeof(hash
));
342 cupsHashString(hash
, hashsize
, hdata
, sizeof(hdata
));
344 /* KD = H(H(A1):H(data)) */
345 snprintf(temp
, sizeof(temp
), "%s:%s", ha1
, hdata
);
346 hashsize
= (size_t)cupsHashData(hashalg
, (unsigned char *)temp
, strlen(temp
), hash
, sizeof(hash
));
347 cupsHashString(hash
, hashsize
, kd
, sizeof(kd
));
349 /* Pass the RFC 2617/7616 WWW-Authenticate header */
351 snprintf(digest
, sizeof(digest
), "username=\"%s\", realm=\"%s\", nonce=\"%s\", algorithm=%s, qop=auth, opaque=\"%s\", cnonce=\"%s\", nc=%08x, uri=\"%s\", response=\"%s\"", cupsUser(), realm
, nonce
, algorithm
, opaque
, cnonce
, http
->nonce_count
, resource
, kd
);
353 snprintf(digest
, sizeof(digest
), "username=\"%s\", realm=\"%s\", nonce=\"%s\", algorithm=%s, qop=auth, cnonce=\"%s\", nc=%08x, uri=\"%s\", response=\"%s\"", cupsUser(), realm
, nonce
, algorithm
, cnonce
, http
->nonce_count
, resource
, kd
);
358 * Use old RFC 2069 Digest method...
361 /* H(A1) = H(username:realm:password) */
362 snprintf(temp
, sizeof(temp
), "%s:%s:%s", cupsUser(), realm
, strchr(http
->userpass
, ':') + 1);
363 hashsize
= (size_t)cupsHashData("md5", (unsigned char *)temp
, strlen(temp
), hash
, sizeof(hash
));
364 cupsHashString(hash
, hashsize
, ha1
, sizeof(ha1
));
366 /* H(A2) = H(method:uri) */
367 snprintf(temp
, sizeof(temp
), "%s:%s", method
, resource
);
368 hashsize
= (size_t)cupsHashData("md5", (unsigned char *)temp
, strlen(temp
), hash
, sizeof(hash
));
369 cupsHashString(hash
, hashsize
, ha2
, sizeof(ha2
));
371 /* KD = H(H(A1):nonce:H(A2)) */
372 snprintf(temp
, sizeof(temp
), "%s:%s:%s", ha1
, nonce
, ha2
);
373 hashsize
= (size_t)cupsHashData("md5", (unsigned char *)temp
, strlen(temp
), hash
, sizeof(hash
));
374 cupsHashString(hash
, hashsize
, kd
, sizeof(kd
));
376 /* Pass the RFC 2069 WWW-Authenticate header */
377 snprintf(digest
, sizeof(digest
), "username=\"%s\", realm=\"%s\", nonce=\"%s\", uri=\"%s\", response=\"%s\"", cupsUser(), realm
, nonce
, resource
, kd
);
380 httpSetAuthString(http
, "Digest", digest
);
384 if (http
->authstring
)
386 DEBUG_printf(("1cupsDoAuthentication: authstring=\"%s\"", http
->authstring
));
392 DEBUG_printf(("1cupsDoAuthentication: Unknown auth type: \"%s\"", www_auth
));
393 http
->status
= HTTP_STATUS_CUPS_AUTHORIZATION_CANCELED
;
402 * '_cupsSetNegotiateAuthString()' - Set the Kerberos authentication string.
405 int /* O - 0 on success, -1 on error */
406 _cupsSetNegotiateAuthString(
407 http_t
*http
, /* I - Connection to server */
408 const char *method
, /* I - Request method ("GET", "POST", "PUT") */
409 const char *resource
) /* I - Resource path */
411 OM_uint32 minor_status
, /* Minor status code */
412 major_status
; /* Major status code */
413 gss_buffer_desc output_token
= GSS_C_EMPTY_BUFFER
;
422 * If the weak-linked GSSAPI/Kerberos library is not present, don't try
426 if (&gss_init_sec_context
== NULL
)
428 DEBUG_puts("1_cupsSetNegotiateAuthString: Weak-linked GSSAPI/Kerberos "
429 "framework is not present");
432 # endif /* __APPLE__ */
434 if (http
->gssname
== GSS_C_NO_NAME
)
436 http
->gssname
= cups_gss_getname(http
, _cupsGSSServiceName());
439 if (http
->gssctx
!= GSS_C_NO_CONTEXT
)
441 gss_delete_sec_context(&minor_status
, &http
->gssctx
, GSS_C_NO_BUFFER
);
442 http
->gssctx
= GSS_C_NO_CONTEXT
;
445 major_status
= gss_init_sec_context(&minor_status
, GSS_C_NO_CREDENTIAL
,
447 http
->gssname
, http
->gssmech
,
448 GSS_C_MUTUAL_FLAG
| GSS_C_INTEG_FLAG
,
450 GSS_C_NO_CHANNEL_BINDINGS
,
451 GSS_C_NO_BUFFER
, &http
->gssmech
,
452 &output_token
, NULL
, NULL
);
454 # ifdef HAVE_GSS_ACQUIRE_CRED_EX_F
455 if (major_status
== GSS_S_NO_CRED
)
458 * Ask the user for credentials...
461 char prompt
[1024], /* Prompt for user */
462 userbuf
[256]; /* Kerberos username */
463 const char *username
, /* Username string */
464 *password
; /* Password string */
465 _cups_gss_acquire_t data
; /* Callback data */
466 gss_auth_identity_desc identity
; /* Kerberos user identity */
467 _cups_globals_t
*cg
= _cupsGlobals();
468 /* Per-thread global data */
470 if (!cg
->lang_default
)
471 cg
->lang_default
= cupsLangDefault();
473 snprintf(prompt
, sizeof(prompt
),
474 _cupsLangString(cg
->lang_default
, _("Password for %s on %s? ")),
475 cupsUser(), http
->gsshost
);
477 if ((password
= cupsGetPassword2(prompt
, http
, method
, resource
)) == NULL
)
481 * Try to acquire credentials...
484 username
= cupsUser();
485 if (!strchr(username
, '@'))
487 snprintf(userbuf
, sizeof(userbuf
), "%s@%s", username
, http
->gsshost
);
491 identity
.type
= GSS_AUTH_IDENTITY_TYPE_1
;
493 identity
.username
= (char *)username
;
494 identity
.realm
= (char *)"";
495 identity
.password
= (char *)password
;
496 identity
.credentialsRef
= NULL
;
498 data
.sem
= dispatch_semaphore_create(0);
504 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
);
506 if (major_status
== GSS_S_COMPLETE
)
508 dispatch_semaphore_wait(data
.sem
, DISPATCH_TIME_FOREVER
);
509 major_status
= data
.major
;
512 dispatch_release(data
.sem
);
514 if (major_status
== GSS_S_COMPLETE
)
516 OM_uint32 release_minor
; /* Minor status from releasing creds */
518 major_status
= gss_init_sec_context(&minor_status
, data
.creds
,
520 http
->gssname
, http
->gssmech
,
521 GSS_C_MUTUAL_FLAG
| GSS_C_INTEG_FLAG
,
523 GSS_C_NO_CHANNEL_BINDINGS
,
524 GSS_C_NO_BUFFER
, &http
->gssmech
,
525 &output_token
, NULL
, NULL
);
526 gss_release_cred(&release_minor
, &data
.creds
);
530 # endif /* HAVE_GSS_ACQUIRED_CRED_EX_F */
532 if (GSS_ERROR(major_status
))
534 cups_gss_printf(major_status
, minor_status
,
535 "_cupsSetNegotiateAuthString: Unable to initialize "
541 else if (major_status
== GSS_S_CONTINUE_NEEDED
)
542 cups_gss_printf(major_status
, minor_status
,
543 "_cupsSetNegotiateAuthString: Continuation needed!");
546 if (output_token
.length
> 0 && output_token
.length
<= 65536)
549 * Allocate the authorization string since Windows KDCs can have
550 * arbitrarily large credentials...
553 int authsize
= 10 + /* "Negotiate " */
554 (int)output_token
.length
* 4 / 3 + 1 + 1;
557 httpSetAuthString(http
, NULL
, NULL
);
559 if ((http
->authstring
= malloc((size_t)authsize
)) == NULL
)
561 http
->authstring
= http
->_authstring
;
562 authsize
= sizeof(http
->_authstring
);
565 strlcpy(http
->authstring
, "Negotiate ", (size_t)authsize
);
566 httpEncode64_2(http
->authstring
+ 10, authsize
- 10, output_token
.value
,
567 (int)output_token
.length
);
569 gss_release_buffer(&minor_status
, &output_token
);
573 DEBUG_printf(("1_cupsSetNegotiateAuthString: Kerberos credentials too "
574 "large - %d bytes!", (int)output_token
.length
));
575 gss_release_buffer(&minor_status
, &output_token
);
582 #endif /* HAVE_GSSAPI */
586 * 'cups_auth_find()' - Find the named WWW-Authenticate scheme.
588 * The "www_authenticate" parameter points to the current position in the header.
590 * Returns @code NULL@ if the auth scheme is not present.
593 static const char * /* O - Start of matching scheme or @code NULL@ if not found */
594 cups_auth_find(const char *www_authenticate
, /* I - Pointer into WWW-Authenticate header */
595 const char *scheme
) /* I - Authentication scheme */
597 size_t schemelen
= strlen(scheme
); /* Length of scheme */
600 DEBUG_printf(("8cups_auth_find(www_authenticate=\"%s\", scheme=\"%s\"(%d))", www_authenticate
, scheme
, (int)schemelen
));
602 while (*www_authenticate
)
605 * Skip leading whitespace and commas...
608 DEBUG_printf(("9cups_auth_find: Before whitespace: \"%s\"", www_authenticate
));
609 while (isspace(*www_authenticate
& 255) || *www_authenticate
== ',')
611 DEBUG_printf(("9cups_auth_find: After whitespace: \"%s\"", www_authenticate
));
614 * See if this is "Scheme" followed by whitespace or the end of the string.
617 if (!strncmp(www_authenticate
, scheme
, schemelen
) && (isspace(www_authenticate
[schemelen
] & 255) || !www_authenticate
[schemelen
]))
620 * Yes, this is the start of the scheme-specific information...
623 DEBUG_printf(("9cups_auth_find: Returning \"%s\".", www_authenticate
));
625 return (www_authenticate
);
629 * Skip the scheme name or param="value" string...
632 while (!isspace(*www_authenticate
& 255) && *www_authenticate
)
634 if (*www_authenticate
== '\"')
637 * Skip quoted value...
641 while (*www_authenticate
&& *www_authenticate
!= '\"')
644 DEBUG_printf(("9cups_auth_find: After quoted: \"%s\"", www_authenticate
));
650 DEBUG_printf(("9cups_auth_find: After skip: \"%s\"", www_authenticate
));
653 DEBUG_puts("9cups_auth_find: Returning NULL.");
660 * 'cups_auth_param()' - Copy the value for the named authentication parameter,
664 static const char * /* O - Parameter value or @code NULL@ if not present */
665 cups_auth_param(const char *scheme
, /* I - Pointer to auth data */
666 const char *name
, /* I - Name of parameter */
667 char *value
, /* I - Value buffer */
668 size_t valsize
) /* I - Size of value buffer */
670 char *valptr
= value
, /* Pointer into value buffer */
671 *valend
= value
+ valsize
- 1; /* Pointer to end of buffer */
672 size_t namelen
= strlen(name
); /* Name length */
673 int param
; /* Is this a parameter? */
676 DEBUG_printf(("8cups_auth_param(scheme=\"%s\", name=\"%s\", value=%p, valsize=%d)", scheme
, name
, (void *)value
, (int)valsize
));
678 while (!isspace(*scheme
& 255) && *scheme
)
683 while (isspace(*scheme
& 255) || *scheme
== ',')
686 if (!strncmp(scheme
, name
, namelen
) && scheme
[namelen
] == '=')
689 * Found the parameter, copy the value...
692 scheme
+= namelen
+ 1;
697 while (*scheme
&& *scheme
!= '\"')
707 while (*scheme
&& strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~+/=", *scheme
))
718 DEBUG_printf(("9cups_auth_param: Returning \"%s\".", value
));
724 * Skip the param=value string...
729 while (!isspace(*scheme
& 255) && *scheme
)
733 else if (*scheme
== '\"')
736 * Skip quoted value...
740 while (*scheme
&& *scheme
!= '\"')
748 * If this wasn't a parameter, we are at the end of this scheme's
758 DEBUG_puts("9cups_auth_param: Returning NULL.");
765 * 'cups_auth_scheme()' - Get the (next) WWW-Authenticate scheme.
767 * The "www_authenticate" parameter points to the current position in the header.
769 * Returns @code NULL@ if there are no (more) auth schemes present.
772 static const char * /* O - Start of scheme or @code NULL@ if not found */
773 cups_auth_scheme(const char *www_authenticate
, /* I - Pointer into WWW-Authenticate header */
774 char *scheme
, /* I - Scheme name buffer */
775 size_t schemesize
) /* I - Size of buffer */
777 const char *start
; /* Start of scheme data */
778 char *sptr
= scheme
, /* Pointer into scheme buffer */
779 *send
= scheme
+ schemesize
- 1;/* End of scheme buffer */
780 int param
; /* Is this a parameter? */
783 DEBUG_printf(("8cups_auth_scheme(www_authenticate=\"%s\", scheme=%p, schemesize=%d)", www_authenticate
, (void *)scheme
, (int)schemesize
));
785 while (*www_authenticate
)
788 * Skip leading whitespace and commas...
791 while (isspace(*www_authenticate
& 255) || *www_authenticate
== ',')
795 * Parse the scheme name or param="value" string...
798 for (sptr
= scheme
, start
= www_authenticate
, param
= 0; *www_authenticate
&& !isspace(*www_authenticate
& 255); www_authenticate
++)
800 if (*www_authenticate
== '=')
802 else if (!param
&& sptr
< send
)
803 *sptr
++ = *www_authenticate
;
804 else if (*www_authenticate
== '\"')
807 * Skip quoted value...
811 while (*www_authenticate
&& *www_authenticate
!= '\"')
816 if (sptr
> scheme
&& !param
)
820 DEBUG_printf(("9cups_auth_scheme: Returning \"%s\".", start
));
828 DEBUG_puts("9cups_auth_scheme: Returning NULL.");
835 # ifdef HAVE_GSS_ACQUIRE_CRED_EX_F
837 * 'cups_gss_acquire()' - Kerberos credentials callback.
841 void *ctx
, /* I - Caller context */
842 OM_uint32 major
, /* I - Major error code */
843 gss_status_id_t status
, /* I - Status (unused) */
844 gss_cred_id_t creds
, /* I - Credentials (if any) */
845 gss_OID_set oids
, /* I - Mechanism OIDs (unused) */
846 OM_uint32 time_rec
) /* I - Timestamp (unused) */
848 uint32_t min
; /* Minor error code */
849 _cups_gss_acquire_t
*data
; /* Callback data */
855 data
= (_cups_gss_acquire_t
*)ctx
;
859 gss_release_oid_set(&min
, &oids
);
860 dispatch_semaphore_signal(data
->sem
);
862 # endif /* HAVE_GSS_ACQUIRE_CRED_EX_F */
866 * 'cups_gss_getname()' - Get CUPS service credentials for authentication.
869 static gss_name_t
/* O - Server name */
871 http_t
*http
, /* I - Connection to server */
872 const char *service_name
) /* I - Service name */
874 gss_buffer_desc token
= GSS_C_EMPTY_BUFFER
;
876 OM_uint32 major_status
, /* Major status code */
877 minor_status
; /* Minor status code */
878 gss_name_t server_name
; /* Server name */
879 char buf
[1024]; /* Name buffer */
882 DEBUG_printf(("7cups_gss_getname(http=%p, service_name=\"%s\")", http
,
887 * Get the hostname...
890 if (!http
->gsshost
[0])
892 httpGetHostname(http
, http
->gsshost
, sizeof(http
->gsshost
));
894 if (!strcmp(http
->gsshost
, "localhost"))
896 if (gethostname(http
->gsshost
, sizeof(http
->gsshost
)) < 0)
898 DEBUG_printf(("1cups_gss_getname: gethostname() failed: %s",
900 http
->gsshost
[0] = '\0';
904 if (!strchr(http
->gsshost
, '.'))
907 * The hostname is not a FQDN, so look it up...
910 struct hostent
*host
; /* Host entry to get FQDN */
912 if ((host
= gethostbyname(http
->gsshost
)) != NULL
&& host
->h_name
)
915 * Use the resolved hostname...
918 strlcpy(http
->gsshost
, host
->h_name
, sizeof(http
->gsshost
));
922 DEBUG_printf(("1cups_gss_getname: gethostbyname(\"%s\") failed.",
924 http
->gsshost
[0] = '\0';
932 * Get a service name we can use for authentication purposes...
935 snprintf(buf
, sizeof(buf
), "%s@%s", service_name
, http
->gsshost
);
937 DEBUG_printf(("8cups_gss_getname: Looking up \"%s\".", buf
));
940 token
.length
= strlen(buf
);
941 server_name
= GSS_C_NO_NAME
;
942 major_status
= gss_import_name(&minor_status
, &token
,
943 GSS_C_NT_HOSTBASED_SERVICE
,
946 if (GSS_ERROR(major_status
))
948 cups_gss_printf(major_status
, minor_status
,
949 "cups_gss_getname: gss_import_name() failed");
953 return (server_name
);
959 * 'cups_gss_printf()' - Show debug error messages from GSSAPI.
963 cups_gss_printf(OM_uint32 major_status
,/* I - Major status code */
964 OM_uint32 minor_status
,/* I - Minor status code */
965 const char *message
) /* I - Prefix for error message */
967 OM_uint32 err_major_status
, /* Major status code for display */
968 err_minor_status
; /* Minor status code for display */
969 OM_uint32 msg_ctx
; /* Message context */
970 gss_buffer_desc major_status_string
= GSS_C_EMPTY_BUFFER
,
971 /* Major status message */
972 minor_status_string
= GSS_C_EMPTY_BUFFER
;
973 /* Minor status message */
977 err_major_status
= gss_display_status(&err_minor_status
,
982 &major_status_string
);
984 if (!GSS_ERROR(err_major_status
))
985 gss_display_status(&err_minor_status
, minor_status
, GSS_C_MECH_CODE
,
986 GSS_C_NULL_OID
, &msg_ctx
, &minor_status_string
);
988 DEBUG_printf(("1%s: %s, %s", message
, (char *)major_status_string
.value
,
989 (char *)minor_status_string
.value
));
991 gss_release_buffer(&err_minor_status
, &major_status_string
);
992 gss_release_buffer(&err_minor_status
, &minor_status_string
);
995 #endif /* HAVE_GSSAPI */
999 * 'cups_local_auth()' - Get the local authorization certificate if
1000 * available/applicable.
1003 static int /* O - 0 if available */
1004 /* 1 if not available */
1006 cups_local_auth(http_t
*http
) /* I - HTTP connection to server */
1008 #if defined(WIN32) || defined(__EMX__)
1010 * Currently WIN32 and OS-2 do not support the CUPS server...
1015 int pid
; /* Current process ID */
1016 FILE *fp
; /* Certificate file */
1017 char trc
[16], /* Try Root Certificate parameter */
1018 filename
[1024]; /* Certificate filename */
1019 const char *www_auth
, /* WWW-Authenticate header */
1020 *schemedata
; /* Data for the named auth scheme */
1021 _cups_globals_t
*cg
= _cupsGlobals(); /* Global data */
1022 # if defined(HAVE_AUTHORIZATION_H)
1023 OSStatus status
; /* Status */
1024 AuthorizationItem auth_right
; /* Authorization right */
1025 AuthorizationRights auth_rights
; /* Authorization rights */
1026 AuthorizationFlags auth_flags
; /* Authorization flags */
1027 AuthorizationExternalForm auth_extrn
; /* Authorization ref external */
1028 char auth_key
[1024]; /* Buffer */
1029 char buffer
[1024]; /* Buffer */
1030 # endif /* HAVE_AUTHORIZATION_H */
1033 DEBUG_printf(("7cups_local_auth(http=%p) hostaddr=%s, hostname=\"%s\"", (void *)http
, httpAddrString(http
->hostaddr
, filename
, sizeof(filename
)), http
->hostname
));
1036 * See if we are accessing localhost...
1039 if (!httpAddrLocalhost(http
->hostaddr
) && _cups_strcasecmp(http
->hostname
, "localhost") != 0)
1041 DEBUG_puts("8cups_local_auth: Not a local connection!");
1045 www_auth
= httpGetField(http
, HTTP_FIELD_WWW_AUTHENTICATE
);
1047 # if defined(HAVE_AUTHORIZATION_H)
1049 * Delete any previous authorization reference...
1054 AuthorizationFree(http
->auth_ref
, kAuthorizationFlagDefaults
);
1055 http
->auth_ref
= NULL
;
1058 if (!getenv("GATEWAY_INTERFACE") && (schemedata
= cups_auth_find(www_auth
, "AuthRef")) != NULL
&& cups_auth_param(schemedata
, "key", auth_key
, sizeof(auth_key
)))
1060 status
= AuthorizationCreate(NULL
, kAuthorizationEmptyEnvironment
, kAuthorizationFlagDefaults
, &http
->auth_ref
);
1061 if (status
!= errAuthorizationSuccess
)
1063 DEBUG_printf(("8cups_local_auth: AuthorizationCreate() returned %d (%s)",
1064 (int)status
, cssmErrorString(status
)));
1068 auth_right
.name
= auth_key
;
1069 auth_right
.valueLength
= 0;
1070 auth_right
.value
= NULL
;
1071 auth_right
.flags
= 0;
1073 auth_rights
.count
= 1;
1074 auth_rights
.items
= &auth_right
;
1076 auth_flags
= kAuthorizationFlagDefaults
|
1077 kAuthorizationFlagPreAuthorize
|
1078 kAuthorizationFlagInteractionAllowed
|
1079 kAuthorizationFlagExtendRights
;
1081 status
= AuthorizationCopyRights(http
->auth_ref
, &auth_rights
,
1082 kAuthorizationEmptyEnvironment
,
1084 if (status
== errAuthorizationSuccess
)
1085 status
= AuthorizationMakeExternalForm(http
->auth_ref
, &auth_extrn
);
1087 if (status
== errAuthorizationSuccess
)
1090 * Set the authorization string and return...
1093 httpEncode64_2(buffer
, sizeof(buffer
), (void *)&auth_extrn
,
1094 sizeof(auth_extrn
));
1096 httpSetAuthString(http
, "AuthRef", buffer
);
1098 DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
1102 else if (status
== errAuthorizationCanceled
)
1105 DEBUG_printf(("9cups_local_auth: AuthorizationCopyRights() returned %d (%s)",
1106 (int)status
, cssmErrorString(status
)));
1109 * Fall through to try certificates...
1112 # endif /* HAVE_AUTHORIZATION_H */
1115 if (cups_auth_find(www_auth
, "Negotiate"))
1117 # endif /* HAVE_GSSAPI */
1118 # ifdef HAVE_AUTHORIZATION_H
1119 if (cups_auth_find(www_auth
, "AuthRef"))
1121 # endif /* HAVE_AUTHORIZATION_H */
1123 # if defined(SO_PEERCRED) && defined(AF_LOCAL)
1125 * See if we can authenticate using the peer credentials provided over a
1126 * domain socket; if so, specify "PeerCred username" as the authentication
1130 if (http
->hostaddr
->addr
.sa_family
== AF_LOCAL
&&
1131 !getenv("GATEWAY_INTERFACE") && /* Not via CGI programs... */
1132 cups_auth_find(www_auth
, "PeerCred"))
1135 * Verify that the current cupsUser() matches the current UID...
1138 struct passwd
*pwd
; /* Password information */
1139 const char *username
; /* Current username */
1141 username
= cupsUser();
1143 if ((pwd
= getpwnam(username
)) != NULL
&& pwd
->pw_uid
== getuid())
1145 httpSetAuthString(http
, "PeerCred", username
);
1147 DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
1153 # endif /* SO_PEERCRED && AF_LOCAL */
1155 if ((schemedata
= cups_auth_find(www_auth
, "Local")) == NULL
)
1159 * Try opening a certificate file for this PID. If that fails,
1160 * try the root certificate...
1164 snprintf(filename
, sizeof(filename
), "%s/certs/%d", cg
->cups_statedir
, pid
);
1165 if ((fp
= fopen(filename
, "r")) == NULL
&& pid
> 0)
1168 * No certificate for this PID; see if we can get the root certificate...
1171 DEBUG_printf(("9cups_local_auth: Unable to open file %s: %s", filename
, strerror(errno
)));
1173 if (!cups_auth_param(schemedata
, "trc", trc
, sizeof(trc
)))
1176 * Scheduler doesn't want us to use the root certificate...
1182 snprintf(filename
, sizeof(filename
), "%s/certs/0", cg
->cups_statedir
);
1183 fp
= fopen(filename
, "r");
1189 * Read the certificate from the file...
1192 char certificate
[33], /* Certificate string */
1193 *certptr
; /* Pointer to certificate string */
1195 certptr
= fgets(certificate
, sizeof(certificate
), fp
);
1201 * Set the authorization string and return...
1204 httpSetAuthString(http
, "Local", certificate
);
1206 DEBUG_printf(("8cups_local_auth: Returning authstring=\"%s\"",
1214 #endif /* WIN32 || __EMX__ */