15#if HAVE_AUTH_MODULE_NEGOTIATE && HAVE_KRB5 && HAVE_GSSAPI
17#define GSSKRB_APPLE_DEPRECATED(x)
28#if HAVE_ET_COM_ERR_H && !HAVE_KRB5_H
29#include <et/com_err.h>
35#if HAVE_GSSAPI_GSSAPI_H
36#include <gssapi/gssapi.h>
41#if HAVE_GSSAPI_GSSAPI_EXT_H
42#include <gssapi/gssapi_ext.h>
44#if HAVE_GSSAPI_GSSAPI_KRB5_H
45#include <gssapi/gssapi_krb5.h>
47#if HAVE_GSSAPI_GSSAPI_GENERIC_H
48#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 USE_HEIMDAL_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 USE_HEIMDAL_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 USE_HEIMDAL_KRB5 && HAVE_KRB5_GET_MAX_TIME_SKEW
312 skew = krb5_get_max_time_skew(kparam.context);
313#elif USE_HEIMDAL_KRB5 && HAVE_MAX_SKEW_IN_KRB5_CONTEXT
314 skew = kparam.context->max_skew;
320 char buf[KT_PATH_MAX], *p;
322 krb5_kt_default_name(kparam.context, buf, KT_PATH_MAX);
323 p = strchr(buf,
':');
326 xfree(keytab_filename);
327 keytab_filename =
xstrdup(p ? p : buf);
332 code = krb5_kt_resolve(kparam.context, keytab_filename, &keytab);
335 "Error while resolving keytab filename " <<
336 keytab_filename <<
" : " << error_message(
code));
341 code = krb5_kt_start_seq_get(kparam.context, keytab, &cursor);
344 "Error while starting keytab scan : " <<
345 error_message(
code));
349 krb5_kt_next_entry(kparam.context, keytab, &entry, &cursor);
350 krb5_copy_principal(kparam.context, entry.principal,
354 "Error while scanning keytab : " <<
355 error_message(
code));
359 code = krb5_kt_end_seq_get(kparam.context, keytab, &cursor);
362 "Error while ending keytab scan : " <<
363 error_message(
code));
366#if USE_HEIMDAL_KRB5 || ( HAVE_KRB5_KT_FREE_ENTRY && HAVE_DECL_KRB5_KT_FREE_ENTRY)
367 code = krb5_kt_free_entry(kparam.context, &entry);
369 code = krb5_free_keytab_entry_contents(kparam.context, &entry);
373 "Error while freeing keytab entry : " <<
374 error_message(
code));
384 krb5_parse_name(kparam.context, principal_name, &principal);
387 "Error while parsing principal name " <<
388 principal_name <<
" : " << error_message(
code));
393 creds = (krb5_creds *)
xmalloc(
sizeof(*creds));
394 memset(creds, 0,
sizeof(*creds));
395#if HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
396 krb5_get_init_creds_opt_alloc(kparam.context, &options);
398 krb5_get_init_creds_opt_init(&options);
400 code = krb5_string_to_deltat((
char *) MAX_RENEW_TIME, &rlife);
401 if (
code != 0 || rlife == 0) {
403 "Error bad lifetime value " << MAX_RENEW_TIME <<
404 " : " << error_message(
code));
407#if HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
408 krb5_get_init_creds_opt_set_renew_life(options, rlife);
410 krb5_get_init_creds_keytab(kparam.context, creds, principal,
411 keytab, 0,
nullptr, options);
412#if HAVE_KRB5_GET_INIT_CREDS_FREE_CONTEXT
413 krb5_get_init_creds_opt_free(kparam.context, options);
415 krb5_get_init_creds_opt_free(options);
418 krb5_get_init_creds_opt_set_renew_life(&options, rlife);
420 krb5_get_init_creds_keytab(kparam.context, creds, principal,
421 keytab, 0,
nullptr, &options);
426 "Error while initializing credentials from keytab : " <<
427 error_message(
code));
430#if !HAVE_KRB5_MEMORY_CACHE
432 (
char *)
xmalloc(strlen(
"FILE:/tmp/peer_proxy_negotiate_auth_")
435 debugs(11, 5,
"Error while allocating memory");
439 strlen(
"FILE:/tmp/peer_proxy_negotiate_auth_") + 16,
440 "FILE:/tmp/peer_proxy_negotiate_auth_%d", (
int) getpid());
443 (
char *)
xmalloc(strlen(
"MEMORY:peer_proxy_negotiate_auth_") +
446 debugs(11, 5,
"Error while allocating memory");
450 strlen(
"MEMORY:peer_proxy_negotiate_auth_") + 16,
451 "MEMORY:peer_proxy_negotiate_auth_%d", (
int) getpid());
454 setenv(
"KRB5CCNAME", mem_cache, 1);
455 code = krb5_cc_resolve(kparam.context, mem_cache, &kparam.cc);
459 "Error while resolving memory credential cache : "
460 << error_message(
code));
463 code = krb5_cc_initialize(kparam.context, kparam.cc, principal);
467 "Error while initializing memory credential cache : " <<
468 error_message(
code));
471 code = krb5_cc_store_cred(kparam.context, kparam.cc, creds);
474 "Error while storing credentials : " <<
475 error_message(
code));
479 if (!creds->times.starttime)
480 creds->times.starttime = creds->times.authtime;
489char *peer_proxy_negotiate_auth(
char *principal_name,
char *proxy,
int flags) {
491 OM_uint32 major_status, minor_status;
492 gss_ctx_id_t gss_context = GSS_C_NO_CONTEXT;
493 gss_name_t server_name = GSS_C_NO_NAME;
494 gss_buffer_desc service = GSS_C_EMPTY_BUFFER;
495 gss_buffer_desc input_token = GSS_C_EMPTY_BUFFER;
496 gss_buffer_desc output_token = GSS_C_EMPTY_BUFFER;
497 char *token =
nullptr;
499 setbuf(stdout,
nullptr);
500 setbuf(stdin,
nullptr);
503 debugs(11, 5,
"Error : No proxy server name");
510 "Creating credential cache for " << principal_name);
512 debugs(11, 5,
"Creating credential cache");
513 rc = krb5_create_cache(
nullptr, principal_name);
515 debugs(11, 5,
"Error : Failed to create Kerberos cache");
521 service.value = (
void *)
xmalloc(strlen(
"HTTP") + strlen(proxy) + 2);
522 snprintf((
char *) service.value, strlen(
"HTTP") + strlen(proxy) + 2,
523 "%s@%s",
"HTTP", proxy);
524 service.length = strlen((
char *) service.value);
526 debugs(11, 5,
"Import gss name");
527 major_status = gss_import_name(&minor_status, &service,
530 if (
check_gss_err(major_status, minor_status,
"gss_import_name()"))
533 debugs(11, 5,
"Initialize gss security context");
534 major_status = gss_init_sec_context(&minor_status,
541 GSS_C_NO_CHANNEL_BINDINGS,
542 &input_token,
nullptr, &output_token,
nullptr,
nullptr);
544 if (
check_gss_err(major_status, minor_status,
"gss_init_sec_context()"))
547 debugs(11, 5,
"Got token with length " << output_token.length);
548 if (output_token.length) {
549 static char b64buf[8192];
552 size_t blen =
base64_encode_update(&ctx, b64buf, output_token.length,
reinterpret_cast<const uint8_t*
>(output_token.value));
556 token =
reinterpret_cast<char*
>(b64buf);
560 gss_delete_sec_context(&minor_status, &gss_context,
nullptr);
561 gss_release_buffer(&minor_status, &service);
562 gss_release_buffer(&minor_status, &input_token);
563 gss_release_buffer(&minor_status, &output_token);
564 gss_release_name(&minor_status, &server_name);
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 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