15#if HAVE_AUTH_MODULE_NEGOTIATE && HAVE_KRB5 && HAVE_GSSAPI
25#if HAVE_ET_COM_ERR_H && !HAVE_KRB5_H
26#include <et/com_err.h>
35#define GSSKRB_APPLE_DEPRECATED(x)
37#if HAVE_GSSAPI_GSSAPI_H
38#include <gssapi/gssapi.h>
42#if HAVE_GSSAPI_GSSAPI_EXT_H
43#include <gssapi/gssapi_ext.h>
45#if HAVE_GSSAPI_GSSAPI_KRB5_H
46#include <gssapi/gssapi_krb5.h>
48#if HAVE_GSSAPI_GSSAPI_GENERIC_H
49#include <gssapi/gssapi_generic.h>
52#ifndef gss_nt_service_name
53#define gss_nt_service_name GSS_C_NT_HOSTBASED_SERVICE
56#if !HAVE_ERROR_MESSAGE && HAVE_KRB5_GET_ERROR_MESSAGE
57#define error_message(code) krb5_get_error_message(kparam.context,code)
58#elif !HAVE_ERROR_MESSAGE && HAVE_KRB5_GET_ERR_TEXT
59#define error_message(code) krb5_get_err_text(kparam.context,code)
60#elif !HAVE_ERROR_MESSAGE
61static char err_code[17];
62const char *KRB5_CALLCONV
63error_message(
long code) {
64 snprintf(err_code,16,
"%ld",code);
69#ifndef gss_mech_spnego
70static gss_OID_desc _gss_mech_spnego =
71{ 6, (
void *)
"\x2b\x06\x01\x05\x05\x02" };
72gss_OID gss_mech_spnego = &_gss_mech_spnego;
76#include <ibm_svc/krb5_svc.h>
77const char *KRB5_CALLCONV error_message(
long code) {
79 krb5_svc_get_msg(code, &msg);
89static struct kstruct {
104int krb5_create_cache(
char *keytab_filename,
char *principal_name);
109void krb5_cleanup(
void);
115int check_gss_err(OM_uint32 major_status, OM_uint32 minor_status,
116 const char *function);
118int check_gss_err(OM_uint32 major_status, OM_uint32 minor_status,
119 const char *function) {
120 if (GSS_ERROR(major_status)) {
121 OM_uint32 maj_stat, min_stat;
122 OM_uint32 msg_ctx = 0;
123 gss_buffer_desc status_string;
131 maj_stat = gss_display_status(&min_stat, major_status,
132 GSS_C_GSS_CODE, GSS_C_NULL_OID, &msg_ctx, &status_string);
133 if (maj_stat == GSS_S_COMPLETE) {
134 if (
sizeof(buf) > len + status_string.length + 1) {
135 memcpy(buf + len, status_string.value,
136 status_string.length);
137 len += status_string.length;
139 gss_release_buffer(&min_stat, &status_string);
142 gss_release_buffer(&min_stat, &status_string);
144 if (
sizeof(buf) > len + 2) {
145 strcpy(buf + len,
". ");
151 maj_stat = gss_display_status(&min_stat, minor_status,
152 GSS_C_MECH_CODE, GSS_C_NULL_OID, &msg_ctx, &status_string);
153 if (maj_stat == GSS_S_COMPLETE) {
154 if (
sizeof(buf) > len + status_string.length) {
155 memcpy(buf + len, status_string.value,
156 status_string.length);
157 len += status_string.length;
159 gss_release_buffer(&min_stat, &status_string);
162 gss_release_buffer(&min_stat, &status_string);
164 debugs(11, 5, function <<
"failed: " << buf);
171 debugs(11, 5,
"Cleanup kerberos context");
172 if (kparam.context) {
174 krb5_cc_destroy(kparam.context, kparam.cc);
176 krb5_free_context(kparam.context);
177 kparam.context =
nullptr;
181int krb5_create_cache(
char *kf,
char *pn) {
183#define KT_PATH_MAX 256
184#define MAX_RENEW_TIME "365d"
185#define DEFAULT_SKEW (krb5_deltat) 600
187 static char *keytab_filename =
nullptr, *principal_name =
nullptr;
188 static krb5_keytab keytab =
nullptr;
189 static krb5_keytab_entry entry;
190 static krb5_kt_cursor cursor;
191 static krb5_creds *creds =
nullptr;
192#if HAVE_LIBHEIMDAL_KRB5 && !HAVE_KRB5_GET_RENEWED_CREDS
193 static krb5_creds creds2;
195 static krb5_principal principal =
nullptr;
196 static krb5_deltat skew;
198#if HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
199 krb5_get_init_creds_opt *options;
201 krb5_get_init_creds_opt options;
203 krb5_error_code code = 0;
205#if HAVE_PROFILE_H && HAVE_KRB5_GET_PROFILE && HAVE_PROFILE_GET_INTEGER && HAVE_PROFILE_RELEASE
208#if HAVE_LIBHEIMDAL_KRB5 && !HAVE_KRB5_GET_RENEWED_CREDS
209 krb5_kdc_flags flags;
210#if HAVE_KRB5_PRINCIPAL_GET_REALM
211 const char *client_realm;
213 krb5_realm client_realm;
223 (creds->times.endtime - time(
nullptr) > skew) &&
224 (creds->times.renew_till - time(
nullptr) > 2 * skew)) {
225 if (creds->times.endtime - time(
nullptr) < 2 * skew) {
226#if HAVE_KRB5_GET_RENEWED_CREDS
229 krb5_get_renewed_creds(kparam.context, creds, principal,
234 flags.b.renewable = flags.b.renew = 1;
237 krb5_cc_get_principal(kparam.context, kparam.cc,
242 "Error while getting principal from credential cache : "
243 << error_message(code));
246#if HAVE_KRB5_PRINCIPAL_GET_REALM
247 client_realm = krb5_principal_get_realm(kparam.context, principal);
249 client_realm = krb5_princ_realm(kparam.context, creds2.client);
252 krb5_make_principal(kparam.context, &creds2.server,
253 (krb5_const_realm)&client_realm, KRB5_TGS_NAME,
254 (krb5_const_realm)&client_realm,
nullptr);
257 "Error while getting krbtgt principal : " <<
258 error_message(code));
262 krb5_get_kdc_cred(kparam.context, kparam.cc, flags,
nullptr,
263 nullptr, &creds2, &creds);
264 krb5_free_creds(kparam.context, &creds2);
267 if (code == KRB5KRB_AP_ERR_TKT_EXPIRED) {
268 krb5_free_creds(kparam.context, creds);
274 "Error while get credentials : " <<
275 error_message(code));
281 if (!kparam.context) {
282 code = krb5_init_context(&kparam.context);
285 "Error while initialising Kerberos library : "
286 << error_message(code));
290#if HAVE_PROFILE_H && HAVE_KRB5_GET_PROFILE && HAVE_PROFILE_GET_INTEGER && HAVE_PROFILE_RELEASE
291 code = krb5_get_profile(kparam.context, &profile);
294 profile_release(profile);
296 "Error while getting profile : " <<
297 error_message(code));
301 profile_get_integer(profile,
"libdefaults",
"clockskew",
nullptr,
304 profile_release(profile);
307 "Error while getting clockskew : " <<
308 error_message(code));
311#elif HAVE_LIBHEIMDAL_KRB5
312 skew = krb5_get_max_time_skew(kparam.context);
318 char buf[KT_PATH_MAX], *p;
320 krb5_kt_default_name(kparam.context, buf, KT_PATH_MAX);
321 p = strchr(buf,
':');
324 xfree(keytab_filename);
325 keytab_filename =
xstrdup(p ? p : buf);
330 code = krb5_kt_resolve(kparam.context, keytab_filename, &keytab);
333 "Error while resolving keytab filename " <<
334 keytab_filename <<
" : " << error_message(code));
339 code = krb5_kt_start_seq_get(kparam.context, keytab, &cursor);
342 "Error while starting keytab scan : " <<
343 error_message(code));
347 krb5_kt_next_entry(kparam.context, keytab, &entry, &cursor);
348 krb5_copy_principal(kparam.context, entry.principal,
350 if (code && code != KRB5_KT_END) {
352 "Error while scanning keytab : " <<
353 error_message(code));
357 code = krb5_kt_end_seq_get(kparam.context, keytab, &cursor);
360 "Error while ending keytab scan : " <<
361 error_message(code));
364#if HAVE_LIBHEIMDAL_KRB5 || ( HAVE_KRB5_KT_FREE_ENTRY && HAVE_DECL_KRB5_KT_FREE_ENTRY)
365 code = krb5_kt_free_entry(kparam.context, &entry);
367 code = krb5_free_keytab_entry_contents(kparam.context, &entry);
371 "Error while freeing keytab entry : " <<
372 error_message(code));
382 krb5_parse_name(kparam.context, principal_name, &principal);
385 "Error while parsing principal name " <<
386 principal_name <<
" : " << error_message(code));
391 creds = (krb5_creds *)
xmalloc(
sizeof(*creds));
392 memset(creds, 0,
sizeof(*creds));
393#if HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
394 krb5_get_init_creds_opt_alloc(kparam.context, &options);
396 krb5_get_init_creds_opt_init(&options);
398 code = krb5_string_to_deltat((
char *) MAX_RENEW_TIME, &rlife);
399 if (code != 0 || rlife == 0) {
401 "Error bad lifetime value " << MAX_RENEW_TIME <<
402 " : " << error_message(code));
405#if HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
406 krb5_get_init_creds_opt_set_renew_life(options, rlife);
408 krb5_get_init_creds_keytab(kparam.context, creds, principal,
409 keytab, 0,
nullptr, options);
410#if HAVE_KRB5_GET_INIT_CREDS_FREE_CONTEXT
411 krb5_get_init_creds_opt_free(kparam.context, options);
413 krb5_get_init_creds_opt_free(options);
416 krb5_get_init_creds_opt_set_renew_life(&options, rlife);
418 krb5_get_init_creds_keytab(kparam.context, creds, principal,
419 keytab, 0,
nullptr, &options);
424 "Error while initializing credentials from keytab : " <<
425 error_message(code));
428#if !HAVE_KRB5_MEMORY_CACHE
430 (
char *)
xmalloc(strlen(
"FILE:/tmp/peer_proxy_negotiate_auth_")
433 debugs(11, 5,
"Error while allocating memory");
437 strlen(
"FILE:/tmp/peer_proxy_negotiate_auth_") + 16,
438 "FILE:/tmp/peer_proxy_negotiate_auth_%d", (
int) getpid());
441 (
char *)
xmalloc(strlen(
"MEMORY:peer_proxy_negotiate_auth_") +
444 debugs(11, 5,
"Error while allocating memory");
448 strlen(
"MEMORY:peer_proxy_negotiate_auth_") + 16,
449 "MEMORY:peer_proxy_negotiate_auth_%d", (
int) getpid());
452 setenv(
"KRB5CCNAME", mem_cache, 1);
453 code = krb5_cc_resolve(kparam.context, mem_cache, &kparam.cc);
457 "Error while resolving memory credential cache : "
458 << error_message(code));
461 code = krb5_cc_initialize(kparam.context, kparam.cc, principal);
465 "Error while initializing memory credential cache : " <<
466 error_message(code));
469 code = krb5_cc_store_cred(kparam.context, kparam.cc, creds);
472 "Error while storing credentials : " <<
473 error_message(code));
477 if (!creds->times.starttime)
478 creds->times.starttime = creds->times.authtime;
487char *peer_proxy_negotiate_auth(
char *principal_name,
const char *
const proxy,
int flags) {
489 OM_uint32 major_status, minor_status;
490 gss_ctx_id_t gss_context = GSS_C_NO_CONTEXT;
491 gss_name_t server_name = GSS_C_NO_NAME;
492 gss_buffer_desc service = GSS_C_EMPTY_BUFFER;
493 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
494 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
495 char *token =
nullptr;
497 setbuf(stdout,
nullptr);
498 setbuf(stdin,
nullptr);
501 debugs(11, 5,
"Error : No proxy server name");
508 "Creating credential cache for " << principal_name);
510 debugs(11, 5,
"Creating credential cache");
511 rc = krb5_create_cache(
nullptr, principal_name);
513 debugs(11, 5,
"Error : Failed to create Kerberos cache");
519 service.value = (
void *)
xmalloc(strlen(
"HTTP") + strlen(proxy) + 2);
520 snprintf((
char *) service.value, strlen(
"HTTP") + strlen(proxy) + 2,
521 "%s@%s",
"HTTP", proxy);
522 service.length = strlen((
char *) service.value);
524 debugs(11, 5,
"Import gss name");
525 major_status = gss_import_name(&minor_status, &service,
528 if (
check_gss_err(major_status, minor_status,
"gss_import_name()"))
531 debugs(11, 5,
"Initialize gss security context");
532 major_status = gss_init_sec_context(&minor_status,
539 GSS_C_NO_CHANNEL_BINDINGS,
540 &input_token,
nullptr, &output_token,
nullptr,
nullptr);
542 if (
check_gss_err(major_status, minor_status,
"gss_init_sec_context()"))
545 debugs(11, 5,
"Got token with length " << output_token.length);
546 if (output_token.length) {
547 static char b64buf[8192];
551 size_t blen =
base64_encode_update(&ctx, b64buf, output_token.length,
reinterpret_cast<const uint8_t*
>(output_token.value));
555 token =
reinterpret_cast<char*
>(b64buf);
559 gss_delete_sec_context(&minor_status, &gss_context,
nullptr);
560 gss_release_buffer(&minor_status, &service);
561 gss_release_buffer(&minor_status, &input_token);
562 gss_release_buffer(&minor_status, &output_token);
563 gss_release_name(&minor_status, &server_name);
#define Assure(condition)
void base64_encode_init(struct base64_encode_ctx *ctx)
size_t base64_encode_update(struct base64_encode_ctx *ctx, char *dst, size_t length, const uint8_t *src)
size_t base64_encode_final(struct base64_encode_ctx *ctx, char *dst)
#define base64_encode_len(length)
#define debugs(SECTION, LEVEL, CONTENT)
#define gss_nt_service_name
int check_gss_err(OM_uint32 major_status, OM_uint32 minor_status, const char *function, int log, int sout)
#define PEER_PROXY_NEGOTIATE_NOKEYTAB