2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
10 * DEBUG: 11 Hypertext Transfer Protocol (HTTP)
15 #if HAVE_KRB5 && HAVE_GSSAPI
19 #include "peer_proxy_negotiate_auth.h"
27 #endif /* HAVE_PROFILE_H */
29 #if HAVE_BROKEN_SOLARIS_KRB5_H
30 #if defined(__cplusplus)
31 #define KRB5INT_BEGIN_DECLS extern "C" {
32 #define KRB5INT_END_DECLS
37 #elif HAVE_ET_COM_ERR_H
38 #include <et/com_err.h>
39 #endif /* HAVE_COM_ERR_H */
42 #endif /* HAVE_COM_ERR_H */
44 #if HAVE_GSSAPI_GSSAPI_H
45 #include <gssapi/gssapi.h>
48 #endif /* HAVE_GSSAPI_H */
50 #if HAVE_GSSAPI_GSSAPI_EXT_H
51 #include <gssapi/gssapi_ext.h>
52 #endif /* HAVE_GSSAPI_GSSAPI_EXT_H */
53 #if HAVE_GSSAPI_GSSAPI_KRB5_H
54 #include <gssapi/gssapi_krb5.h>
55 #endif /* HAVE_GSSAPI_GSSAPI_KRB5_H */
56 #if HAVE_GSSAPI_GSSAPI_GENERIC_H
57 #include <gssapi/gssapi_generic.h>
58 #endif /* HAVE_GSSAPI_GSSAPI_GENERIC_H */
59 #endif /* !USE_HEIMDAL_KRB5 */
61 #ifndef gss_nt_service_name
62 #define gss_nt_service_name GSS_C_NT_HOSTBASED_SERVICE
65 #if !HAVE_ERROR_MESSAGE && HAVE_KRB5_GET_ERR_MESSAGE
66 #define error_message(code) krb5_get_error_message(kparam.context,code)
67 #elif !HAVE_ERROR_MESSAGE && HAVE_KRB5_GET_ERROR_TEXT
68 #define error_message(code) krb5_get_err_text(kparam.context,code)
69 #elif !HAVE_ERROR_MESSAGE
70 static char err_code
[17];
71 const char *KRB5_CALLCONV
72 error_message(long code
) {
73 snprintf(err_code
,16,"%ld",code
);
78 #ifndef gss_mech_spnego
79 static gss_OID_desc _gss_mech_spnego
=
80 { 6, (void *) "\x2b\x06\x01\x05\x05\x02" };
81 gss_OID gss_mech_spnego
= &_gss_mech_spnego
;
85 #include <ibm_svc/krb5_svc.h>
86 const char *KRB5_CALLCONV
error_message(long code
) {
88 krb5_svc_get_msg(code
, &msg
);
94 * Kerberos context and cache structure
95 * Caches authentication details to reduce
96 * number of authentication requests to kdc
98 static struct kstruct
{
105 * krb5_create_cache creates a Kerberos file credential cache or a memory
106 * credential cache if supported. The initial key for the principal
107 * principal_name is extracted from the keytab keytab_filename.
109 * If keytab_filename is NULL the default will be used.
110 * If principal_name is NULL the first working entry of the keytab will be used.
112 int krb5_create_cache(char *keytab_filename
, char *principal_name
);
115 * krb5_cleanup clears used Keberos memory
117 void krb5_cleanup(void);
120 * check_gss_err checks for gssapi error codes, extracts the error message
123 int check_gss_err(OM_uint32 major_status
, OM_uint32 minor_status
,
124 const char *function
);
126 int check_gss_err(OM_uint32 major_status
, OM_uint32 minor_status
,
127 const char *function
) {
128 if (GSS_ERROR(major_status
)) {
129 OM_uint32 maj_stat
, min_stat
;
130 OM_uint32 msg_ctx
= 0;
131 gss_buffer_desc status_string
;
138 /* convert major status code (GSS-API error) to text */
139 maj_stat
= gss_display_status(&min_stat
, major_status
,
140 GSS_C_GSS_CODE
, GSS_C_NULL_OID
, &msg_ctx
, &status_string
);
141 if (maj_stat
== GSS_S_COMPLETE
) {
142 if (sizeof(buf
) > len
+ status_string
.length
+ 1) {
143 memcpy(buf
+ len
, status_string
.value
,
144 status_string
.length
);
145 len
+= status_string
.length
;
147 gss_release_buffer(&min_stat
, &status_string
);
150 gss_release_buffer(&min_stat
, &status_string
);
152 if (sizeof(buf
) > len
+ 2) {
153 strcpy(buf
+ len
, ". ");
158 /* convert minor status code (underlying routine error) to text */
159 maj_stat
= gss_display_status(&min_stat
, minor_status
,
160 GSS_C_MECH_CODE
, GSS_C_NULL_OID
, &msg_ctx
, &status_string
);
161 if (maj_stat
== GSS_S_COMPLETE
) {
162 if (sizeof(buf
) > len
+ status_string
.length
) {
163 memcpy(buf
+ len
, status_string
.value
,
164 status_string
.length
);
165 len
+= status_string
.length
;
167 gss_release_buffer(&min_stat
, &status_string
);
170 gss_release_buffer(&min_stat
, &status_string
);
172 debugs(11, 5, HERE
<< function
<< "failed: " << buf
);
178 void krb5_cleanup() {
179 debugs(11, 5, HERE
<< "Cleanup kerberos context");
180 if (kparam
.context
) {
182 krb5_cc_destroy(kparam
.context
, kparam
.cc
);
184 krb5_free_context(kparam
.context
);
185 kparam
.context
= NULL
;
189 int krb5_create_cache(char *kf
, char *pn
) {
191 #define KT_PATH_MAX 256
192 #define MAX_RENEW_TIME "365d"
193 #define DEFAULT_SKEW (krb5_deltat) 600
195 static char *keytab_filename
= NULL
, *principal_name
= NULL
;
196 static krb5_keytab keytab
= 0;
197 static krb5_keytab_entry entry
;
198 static krb5_kt_cursor cursor
;
199 static krb5_creds
*creds
= NULL
;
200 #if USE_HEIMDAL_KRB5 && !HAVE_KRB5_GET_RENEWED_CREDS
201 static krb5_creds creds2
;
203 static krb5_principal principal
= NULL
;
204 static krb5_deltat skew
;
206 #if HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
207 krb5_get_init_creds_opt
*options
;
209 krb5_get_init_creds_opt options
;
211 krb5_error_code code
= 0;
213 #if HAVE_PROFILE_H && HAVE_KRB5_GET_PROFILE && HAVE_PROFILE_GET_INTEGER && HAVE_PROFILE_RELEASE
216 #if USE_HEIMDAL_KRB5 && !HAVE_KRB5_GET_RENEWED_CREDS
217 krb5_kdc_flags flags
;
218 #if HAVE_KRB5_PRINCIPAL_GET_REALM
219 const char *client_realm
;
221 krb5_realm client_realm
;
228 * Check if credentials need to be renewed
231 (creds
->times
.endtime
- time(0) > skew
) &&
232 (creds
->times
.renew_till
- time(0) > 2 * skew
)) {
233 if (creds
->times
.endtime
- time(0) < 2 * skew
) {
234 #if HAVE_KRB5_GET_RENEWED_CREDS
237 krb5_get_renewed_creds(kparam
.context
, creds
, principal
,
242 flags
.b
.renewable
= flags
.b
.renew
= 1;
245 krb5_cc_get_principal(kparam
.context
, kparam
.cc
,
250 "Error while getting principal from credential cache : "
251 << error_message(code
));
254 #if HAVE_KRB5_PRINCIPAL_GET_REALM
255 client_realm
= krb5_principal_get_realm(kparam
.context
, principal
);
257 client_realm
= krb5_princ_realm(kparam
.context
, creds2
.client
);
260 krb5_make_principal(kparam
.context
, &creds2
.server
,
261 (krb5_const_realm
)&client_realm
, KRB5_TGS_NAME
,
262 (krb5_const_realm
)&client_realm
, NULL
);
265 HERE
<< "Error while getting krbtgt principal : " <<
266 error_message(code
));
270 krb5_get_kdc_cred(kparam
.context
, kparam
.cc
, flags
, NULL
,
271 NULL
, &creds2
, &creds
);
272 krb5_free_creds(kparam
.context
, &creds2
);
275 if (code
== KRB5KRB_AP_ERR_TKT_EXPIRED
) {
276 krb5_free_creds(kparam
.context
, creds
);
278 /* this can happen because of clock skew */
282 HERE
<< "Error while get credentials : " <<
283 error_message(code
));
289 if (!kparam
.context
) {
290 code
= krb5_init_context(&kparam
.context
);
293 HERE
<< "Error while initialising Kerberos library : "
294 << error_message(code
));
298 #if HAVE_PROFILE_H && HAVE_KRB5_GET_PROFILE && HAVE_PROFILE_GET_INTEGER && HAVE_PROFILE_RELEASE
299 code
= krb5_get_profile(kparam
.context
, &profile
);
302 profile_release(profile
);
304 HERE
<< "Error while getting profile : " <<
305 error_message(code
));
309 profile_get_integer(profile
, "libdefaults", "clockskew", 0,
312 profile_release(profile
);
315 HERE
<< "Error while getting clockskew : " <<
316 error_message(code
));
319 #elif USE_HEIMDAL_KRB5 && HAVE_KRB5_GET_MAX_TIME_SKEW
320 skew
= krb5_get_max_time_skew(kparam
.context
);
321 #elif USE_HEIMDAL_KRB5 && HAVE_MAX_SKEW_IN_KRB5_CONTEXT
322 skew
= kparam
.context
->max_skew
;
328 char buf
[KT_PATH_MAX
], *p
;
330 krb5_kt_default_name(kparam
.context
, buf
, KT_PATH_MAX
);
331 p
= strchr(buf
, ':');
334 xfree(keytab_filename
);
335 keytab_filename
= xstrdup(p
? p
: buf
);
337 keytab_filename
= xstrdup(kf
);
340 code
= krb5_kt_resolve(kparam
.context
, keytab_filename
, &keytab
);
343 HERE
<< "Error while resolving keytab filename " <<
344 keytab_filename
<< " : " << error_message(code
));
349 code
= krb5_kt_start_seq_get(kparam
.context
, keytab
, &cursor
);
352 HERE
<< "Error while starting keytab scan : " <<
353 error_message(code
));
357 krb5_kt_next_entry(kparam
.context
, keytab
, &entry
, &cursor
);
358 krb5_copy_principal(kparam
.context
, entry
.principal
,
360 if (code
&& code
!= KRB5_KT_END
) {
362 HERE
<< "Error while scanning keytab : " <<
363 error_message(code
));
367 code
= krb5_kt_end_seq_get(kparam
.context
, keytab
, &cursor
);
370 HERE
<< "Error while ending keytab scan : " <<
371 error_message(code
));
374 #if USE_HEIMDAL_KRB5 || ( HAVE_KRB5_KT_FREE_ENTRY && HAVE_DECL_KRB5_KT_FREE_ENTRY)
375 code
= krb5_kt_free_entry(kparam
.context
, &entry
);
377 code
= krb5_free_keytab_entry_contents(kparam
.context
, &entry
);
381 HERE
<< "Error while freeing keytab entry : " <<
382 error_message(code
));
387 principal_name
= xstrdup(pn
);
392 krb5_parse_name(kparam
.context
, principal_name
, &principal
);
395 HERE
<< "Error while parsing principal name " <<
396 principal_name
<< " : " << error_message(code
));
401 creds
= (krb5_creds
*) xmalloc(sizeof(*creds
));
402 memset(creds
, 0, sizeof(*creds
));
403 #if HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
404 krb5_get_init_creds_opt_alloc(kparam
.context
, &options
);
406 krb5_get_init_creds_opt_init(&options
);
408 code
= krb5_string_to_deltat((char *) MAX_RENEW_TIME
, &rlife
);
409 if (code
!= 0 || rlife
== 0) {
411 HERE
<< "Error bad lifetime value " << MAX_RENEW_TIME
<<
412 " : " << error_message(code
));
415 #if HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
416 krb5_get_init_creds_opt_set_renew_life(options
, rlife
);
418 krb5_get_init_creds_keytab(kparam
.context
, creds
, principal
,
419 keytab
, 0, NULL
, options
);
420 #if HAVE_KRB5_GET_INIT_CREDS_FREE_CONTEXT
421 krb5_get_init_creds_opt_free(kparam
.context
, options
);
423 krb5_get_init_creds_opt_free(options
);
426 krb5_get_init_creds_opt_set_renew_life(&options
, rlife
);
428 krb5_get_init_creds_keytab(kparam
.context
, creds
, principal
,
429 keytab
, 0, NULL
, &options
);
434 "Error while initializing credentials from keytab : " <<
435 error_message(code
));
438 #if !HAVE_KRB5_MEMORY_CACHE
440 (char *) xmalloc(strlen("FILE:/tmp/peer_proxy_negotiate_auth_")
443 debugs(11, 5, "Error while allocating memory");
447 strlen("FILE:/tmp/peer_proxy_negotiate_auth_") + 16,
448 "FILE:/tmp/peer_proxy_negotiate_auth_%d", (int) getpid());
451 (char *) xmalloc(strlen("MEMORY:peer_proxy_negotiate_auth_") +
454 debugs(11, 5, "Error while allocating memory");
458 strlen("MEMORY:peer_proxy_negotiate_auth_") + 16,
459 "MEMORY:peer_proxy_negotiate_auth_%d", (int) getpid());
462 setenv("KRB5CCNAME", mem_cache
, 1);
463 code
= krb5_cc_resolve(kparam
.context
, mem_cache
, &kparam
.cc
);
467 HERE
<< "Error while resolving memory credential cache : "
468 << error_message(code
));
471 code
= krb5_cc_initialize(kparam
.context
, kparam
.cc
, principal
);
475 "Error while initializing memory credential cache : " <<
476 error_message(code
));
479 code
= krb5_cc_store_cred(kparam
.context
, kparam
.cc
, creds
);
482 HERE
<< "Error while storing credentials : " <<
483 error_message(code
));
487 if (!creds
->times
.starttime
)
488 creds
->times
.starttime
= creds
->times
.authtime
;
494 * peer_proxy_negotiate_auth gets a GSSAPI token for principal_name
495 * and base64 encodes it.
497 char *peer_proxy_negotiate_auth(char *principal_name
, char *proxy
) {
499 OM_uint32 major_status
, minor_status
;
500 gss_ctx_id_t gss_context
= GSS_C_NO_CONTEXT
;
501 gss_name_t server_name
= GSS_C_NO_NAME
;
502 gss_buffer_desc service
= GSS_C_EMPTY_BUFFER
;
503 gss_buffer_desc input_token
= GSS_C_EMPTY_BUFFER
;
504 gss_buffer_desc output_token
= GSS_C_EMPTY_BUFFER
;
507 setbuf(stdout
, NULL
);
511 debugs(11, 5, HERE
<< "Error : No proxy server name");
517 HERE
<< "Creating credential cache for " << principal_name
);
519 debugs(11, 5, HERE
<< "Creating credential cache");
520 rc
= krb5_create_cache(NULL
, principal_name
);
522 debugs(11, 5, HERE
<< "Error : Failed to create Kerberos cache");
527 service
.value
= (void *) xmalloc(strlen("HTTP") + strlen(proxy
) + 2);
528 snprintf((char *) service
.value
, strlen("HTTP") + strlen(proxy
) + 2,
529 "%s@%s", "HTTP", proxy
);
530 service
.length
= strlen((char *) service
.value
);
532 debugs(11, 5, HERE
<< "Import gss name");
533 major_status
= gss_import_name(&minor_status
, &service
,
534 gss_nt_service_name
, &server_name
);
536 if (check_gss_err(major_status
, minor_status
, "gss_import_name()"))
539 debugs(11, 5, HERE
<< "Initialize gss security context");
540 major_status
= gss_init_sec_context(&minor_status
,
547 GSS_C_NO_CHANNEL_BINDINGS
,
548 &input_token
, NULL
, &output_token
, NULL
, NULL
);
550 if (check_gss_err(major_status
, minor_status
, "gss_init_sec_context()"))
553 debugs(11, 5, HERE
<< "Got token with length " << output_token
.length
);
554 if (output_token
.length
) {
557 (char *) base64_encode_bin((const char *) output_token
.value
,
558 output_token
.length
);
562 gss_delete_sec_context(&minor_status
, &gss_context
, NULL
);
563 gss_release_buffer(&minor_status
, &service
);
564 gss_release_buffer(&minor_status
, &input_token
);
565 gss_release_buffer(&minor_status
, &output_token
);
566 gss_release_name(&minor_status
, &server_name
);
574 #endif /* HAVE_KRB5 && HAVE_GSSAPI */