2 * Copyright (C) 1996-2015 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_ERROR_MESSAGE
66 #define error_message(code) krb5_get_error_message(kparam.context,code)
67 #elif !HAVE_ERROR_MESSAGE && HAVE_KRB5_GET_ERR_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
{
106 * krb5_create_cache creates a Kerberos file credential cache or a memory
107 * credential cache if supported. The initial key for the principal
108 * principal_name is extracted from the keytab keytab_filename.
110 * If keytab_filename is NULL the default will be used.
111 * If principal_name is NULL the first working entry of the keytab will be used.
113 int krb5_create_cache(char *keytab_filename
, char *principal_name
);
116 * krb5_cleanup clears used Keberos memory
118 void krb5_cleanup(void);
121 * check_gss_err checks for gssapi error codes, extracts the error message
124 int check_gss_err(OM_uint32 major_status
, OM_uint32 minor_status
,
125 const char *function
);
127 int check_gss_err(OM_uint32 major_status
, OM_uint32 minor_status
,
128 const char *function
) {
129 if (GSS_ERROR(major_status
)) {
130 OM_uint32 maj_stat
, min_stat
;
131 OM_uint32 msg_ctx
= 0;
132 gss_buffer_desc status_string
;
139 /* convert major status code (GSS-API error) to text */
140 maj_stat
= gss_display_status(&min_stat
, major_status
,
141 GSS_C_GSS_CODE
, GSS_C_NULL_OID
, &msg_ctx
, &status_string
);
142 if (maj_stat
== GSS_S_COMPLETE
) {
143 if (sizeof(buf
) > len
+ status_string
.length
+ 1) {
144 memcpy(buf
+ len
, status_string
.value
,
145 status_string
.length
);
146 len
+= status_string
.length
;
148 gss_release_buffer(&min_stat
, &status_string
);
151 gss_release_buffer(&min_stat
, &status_string
);
153 if (sizeof(buf
) > len
+ 2) {
154 strcpy(buf
+ len
, ". ");
159 /* convert minor status code (underlying routine error) to text */
160 maj_stat
= gss_display_status(&min_stat
, minor_status
,
161 GSS_C_MECH_CODE
, GSS_C_NULL_OID
, &msg_ctx
, &status_string
);
162 if (maj_stat
== GSS_S_COMPLETE
) {
163 if (sizeof(buf
) > len
+ status_string
.length
) {
164 memcpy(buf
+ len
, status_string
.value
,
165 status_string
.length
);
166 len
+= status_string
.length
;
168 gss_release_buffer(&min_stat
, &status_string
);
171 gss_release_buffer(&min_stat
, &status_string
);
173 debugs(11, 5, HERE
<< function
<< "failed: " << buf
);
179 void krb5_cleanup() {
180 debugs(11, 5, HERE
<< "Cleanup kerberos context");
181 if (kparam
.context
) {
183 krb5_cc_destroy(kparam
.context
, kparam
.cc
);
185 krb5_free_context(kparam
.context
);
186 kparam
.context
= NULL
;
190 int krb5_create_cache(char *kf
, char *pn
) {
192 #define KT_PATH_MAX 256
193 #define MAX_RENEW_TIME "365d"
194 #define DEFAULT_SKEW (krb5_deltat) 600
196 static char *keytab_filename
= NULL
, *principal_name
= NULL
;
197 static krb5_keytab keytab
= 0;
198 static krb5_keytab_entry entry
;
199 static krb5_kt_cursor cursor
;
200 static krb5_creds
*creds
= NULL
;
201 #if USE_HEIMDAL_KRB5 && !HAVE_KRB5_GET_RENEWED_CREDS
202 static krb5_creds creds2
;
204 static krb5_principal principal
= NULL
;
205 static krb5_deltat skew
;
207 #if HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
208 krb5_get_init_creds_opt
*options
;
210 krb5_get_init_creds_opt options
;
212 krb5_error_code code
= 0;
214 #if HAVE_PROFILE_H && HAVE_KRB5_GET_PROFILE && HAVE_PROFILE_GET_INTEGER && HAVE_PROFILE_RELEASE
217 #if USE_HEIMDAL_KRB5 && !HAVE_KRB5_GET_RENEWED_CREDS
218 krb5_kdc_flags flags
;
219 #if HAVE_KRB5_PRINCIPAL_GET_REALM
220 const char *client_realm
;
222 krb5_realm client_realm
;
229 * Check if credentials need to be renewed
232 (creds
->times
.endtime
- time(0) > skew
) &&
233 (creds
->times
.renew_till
- time(0) > 2 * skew
)) {
234 if (creds
->times
.endtime
- time(0) < 2 * skew
) {
235 #if HAVE_KRB5_GET_RENEWED_CREDS
238 krb5_get_renewed_creds(kparam
.context
, creds
, principal
,
243 flags
.b
.renewable
= flags
.b
.renew
= 1;
246 krb5_cc_get_principal(kparam
.context
, kparam
.cc
,
251 "Error while getting principal from credential cache : "
252 << error_message(code
));
255 #if HAVE_KRB5_PRINCIPAL_GET_REALM
256 client_realm
= krb5_principal_get_realm(kparam
.context
, principal
);
258 client_realm
= krb5_princ_realm(kparam
.context
, creds2
.client
);
261 krb5_make_principal(kparam
.context
, &creds2
.server
,
262 (krb5_const_realm
)&client_realm
, KRB5_TGS_NAME
,
263 (krb5_const_realm
)&client_realm
, NULL
);
266 HERE
<< "Error while getting krbtgt principal : " <<
267 error_message(code
));
271 krb5_get_kdc_cred(kparam
.context
, kparam
.cc
, flags
, NULL
,
272 NULL
, &creds2
, &creds
);
273 krb5_free_creds(kparam
.context
, &creds2
);
276 if (code
== KRB5KRB_AP_ERR_TKT_EXPIRED
) {
277 krb5_free_creds(kparam
.context
, creds
);
279 /* this can happen because of clock skew */
283 HERE
<< "Error while get credentials : " <<
284 error_message(code
));
290 if (!kparam
.context
) {
291 code
= krb5_init_context(&kparam
.context
);
294 HERE
<< "Error while initialising Kerberos library : "
295 << error_message(code
));
299 #if HAVE_PROFILE_H && HAVE_KRB5_GET_PROFILE && HAVE_PROFILE_GET_INTEGER && HAVE_PROFILE_RELEASE
300 code
= krb5_get_profile(kparam
.context
, &profile
);
303 profile_release(profile
);
305 HERE
<< "Error while getting profile : " <<
306 error_message(code
));
310 profile_get_integer(profile
, "libdefaults", "clockskew", 0,
313 profile_release(profile
);
316 HERE
<< "Error while getting clockskew : " <<
317 error_message(code
));
320 #elif USE_HEIMDAL_KRB5 && HAVE_KRB5_GET_MAX_TIME_SKEW
321 skew
= krb5_get_max_time_skew(kparam
.context
);
322 #elif USE_HEIMDAL_KRB5 && HAVE_MAX_SKEW_IN_KRB5_CONTEXT
323 skew
= kparam
.context
->max_skew
;
329 char buf
[KT_PATH_MAX
], *p
;
331 krb5_kt_default_name(kparam
.context
, buf
, KT_PATH_MAX
);
332 p
= strchr(buf
, ':');
335 xfree(keytab_filename
);
336 keytab_filename
= xstrdup(p
? p
: buf
);
338 keytab_filename
= xstrdup(kf
);
341 code
= krb5_kt_resolve(kparam
.context
, keytab_filename
, &keytab
);
344 HERE
<< "Error while resolving keytab filename " <<
345 keytab_filename
<< " : " << error_message(code
));
350 code
= krb5_kt_start_seq_get(kparam
.context
, keytab
, &cursor
);
353 HERE
<< "Error while starting keytab scan : " <<
354 error_message(code
));
358 krb5_kt_next_entry(kparam
.context
, keytab
, &entry
, &cursor
);
359 krb5_copy_principal(kparam
.context
, entry
.principal
,
361 if (code
&& code
!= KRB5_KT_END
) {
363 HERE
<< "Error while scanning keytab : " <<
364 error_message(code
));
368 code
= krb5_kt_end_seq_get(kparam
.context
, keytab
, &cursor
);
371 HERE
<< "Error while ending keytab scan : " <<
372 error_message(code
));
375 #if USE_HEIMDAL_KRB5 || ( HAVE_KRB5_KT_FREE_ENTRY && HAVE_DECL_KRB5_KT_FREE_ENTRY)
376 code
= krb5_kt_free_entry(kparam
.context
, &entry
);
378 code
= krb5_free_keytab_entry_contents(kparam
.context
, &entry
);
382 HERE
<< "Error while freeing keytab entry : " <<
383 error_message(code
));
388 principal_name
= xstrdup(pn
);
393 krb5_parse_name(kparam
.context
, principal_name
, &principal
);
396 HERE
<< "Error while parsing principal name " <<
397 principal_name
<< " : " << error_message(code
));
402 creds
= (krb5_creds
*) xmalloc(sizeof(*creds
));
403 memset(creds
, 0, sizeof(*creds
));
404 #if HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
405 krb5_get_init_creds_opt_alloc(kparam
.context
, &options
);
407 krb5_get_init_creds_opt_init(&options
);
409 code
= krb5_string_to_deltat((char *) MAX_RENEW_TIME
, &rlife
);
410 if (code
!= 0 || rlife
== 0) {
412 HERE
<< "Error bad lifetime value " << MAX_RENEW_TIME
<<
413 " : " << error_message(code
));
416 #if HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
417 krb5_get_init_creds_opt_set_renew_life(options
, rlife
);
419 krb5_get_init_creds_keytab(kparam
.context
, creds
, principal
,
420 keytab
, 0, NULL
, options
);
421 #if HAVE_KRB5_GET_INIT_CREDS_FREE_CONTEXT
422 krb5_get_init_creds_opt_free(kparam
.context
, options
);
424 krb5_get_init_creds_opt_free(options
);
427 krb5_get_init_creds_opt_set_renew_life(&options
, rlife
);
429 krb5_get_init_creds_keytab(kparam
.context
, creds
, principal
,
430 keytab
, 0, NULL
, &options
);
435 "Error while initializing credentials from keytab : " <<
436 error_message(code
));
439 #if !HAVE_KRB5_MEMORY_CACHE
441 (char *) xmalloc(strlen("FILE:/tmp/peer_proxy_negotiate_auth_")
444 debugs(11, 5, "Error while allocating memory");
448 strlen("FILE:/tmp/peer_proxy_negotiate_auth_") + 16,
449 "FILE:/tmp/peer_proxy_negotiate_auth_%d", (int) getpid());
452 (char *) xmalloc(strlen("MEMORY:peer_proxy_negotiate_auth_") +
455 debugs(11, 5, "Error while allocating memory");
459 strlen("MEMORY:peer_proxy_negotiate_auth_") + 16,
460 "MEMORY:peer_proxy_negotiate_auth_%d", (int) getpid());
463 setenv("KRB5CCNAME", mem_cache
, 1);
464 code
= krb5_cc_resolve(kparam
.context
, mem_cache
, &kparam
.cc
);
468 HERE
<< "Error while resolving memory credential cache : "
469 << error_message(code
));
472 code
= krb5_cc_initialize(kparam
.context
, kparam
.cc
, principal
);
476 "Error while initializing memory credential cache : " <<
477 error_message(code
));
480 code
= krb5_cc_store_cred(kparam
.context
, kparam
.cc
, creds
);
483 HERE
<< "Error while storing credentials : " <<
484 error_message(code
));
488 if (!creds
->times
.starttime
)
489 creds
->times
.starttime
= creds
->times
.authtime
;
495 * peer_proxy_negotiate_auth gets a GSSAPI token for principal_name
496 * and base64 encodes it.
498 char *peer_proxy_negotiate_auth(char *principal_name
, char *proxy
) {
500 OM_uint32 major_status
, minor_status
;
501 gss_ctx_id_t gss_context
= GSS_C_NO_CONTEXT
;
502 gss_name_t server_name
= GSS_C_NO_NAME
;
503 gss_buffer_desc service
= GSS_C_EMPTY_BUFFER
;
504 gss_buffer_desc input_token
= GSS_C_EMPTY_BUFFER
;
505 gss_buffer_desc output_token
= GSS_C_EMPTY_BUFFER
;
508 setbuf(stdout
, NULL
);
512 debugs(11, 5, HERE
<< "Error : No proxy server name");
518 HERE
<< "Creating credential cache for " << principal_name
);
520 debugs(11, 5, HERE
<< "Creating credential cache");
521 rc
= krb5_create_cache(NULL
, principal_name
);
523 debugs(11, 5, HERE
<< "Error : Failed to create Kerberos cache");
528 service
.value
= (void *) xmalloc(strlen("HTTP") + strlen(proxy
) + 2);
529 snprintf((char *) service
.value
, strlen("HTTP") + strlen(proxy
) + 2,
530 "%s@%s", "HTTP", proxy
);
531 service
.length
= strlen((char *) service
.value
);
533 debugs(11, 5, HERE
<< "Import gss name");
534 major_status
= gss_import_name(&minor_status
, &service
,
535 gss_nt_service_name
, &server_name
);
537 if (check_gss_err(major_status
, minor_status
, "gss_import_name()"))
540 debugs(11, 5, HERE
<< "Initialize gss security context");
541 major_status
= gss_init_sec_context(&minor_status
,
548 GSS_C_NO_CHANNEL_BINDINGS
,
549 &input_token
, NULL
, &output_token
, NULL
, NULL
);
551 if (check_gss_err(major_status
, minor_status
, "gss_init_sec_context()"))
554 debugs(11, 5, HERE
<< "Got token with length " << output_token
.length
);
555 if (output_token
.length
) {
556 static uint8_t b64buf
[8192]; // XXX: 8KB only because base64_encode_bin() used to.
557 struct base64_encode_ctx ctx
;
558 base64_encode_init(&ctx
);
559 size_t blen
= base64_encode_update(&ctx
, b64buf
, output_token
.length
, reinterpret_cast<const uint8_t*>(output_token
.value
));
560 blen
+= base64_encode_final(&ctx
, b64buf
+blen
);
563 token
= reinterpret_cast<char*>(b64buf
);
567 gss_delete_sec_context(&minor_status
, &gss_context
, NULL
);
568 gss_release_buffer(&minor_status
, &service
);
569 gss_release_buffer(&minor_status
, &input_token
);
570 gss_release_buffer(&minor_status
, &output_token
);
571 gss_release_name(&minor_status
, &server_name
);
579 #endif /* HAVE_KRB5 && HAVE_GSSAPI */