Squid Web Cache: squid/src/peer_proxy_negotiate_auth.cc Source File
Squid Web Cache master Loading... Searching... No Matches peer_proxy_negotiate_auth.cc Go to the documentation of this file. 1 /* 2 * Copyright (C) 1996-2026 The Squid Software Foundation and contributors 3 * 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. 7 */ 8 9 /* 10 * DEBUG: 11 Hypertext Transfer Protocol (HTTP) 11 */ 12 13 #include " squid.h " 14 15 #if HAVE_AUTH_MODULE_NEGOTIATE && HAVE_KRB5 && HAVE_GSSAPI 16 #include " base/Assure.h " 17 #include " base64.h " 18 #include " compat/krb5.h " 19 #include " debug/Stream.h " 20 #include " peer_proxy_negotiate_auth.h " 21 22 #if HAVE_PROFILE_H 23 #include <profile.h> 24 #endif /* HAVE_PROFILE_H */ 25 #if HAVE_ET_COM_ERR_H && !HAVE_KRB5_H 26 #include <et/com_err.h> 27 #endif /* HAVE_COM_ERR_H */ 28 #if HAVE_COM_ERR_H 29 #include <com_err.h> 30 #endif /* HAVE_COM_ERR_H */ 31 #if HAVE_GSS_H 32 #include <gss.h> 33 #endif 34 #if USE_APPLE_KRB5 35 #define GSSKRB_APPLE_DEPRECATED(x) 36 #endif 37 #if HAVE_GSSAPI_GSSAPI_H 38 #include <gssapi/gssapi.h> 39 #elif HAVE_GSSAPI_H 40 #include <gssapi.h> 41 #endif /* HAVE_GSSAPI_H */ 42 #if HAVE_GSSAPI_GSSAPI_EXT_H 43 #include <gssapi/gssapi_ext.h> 44 #endif /* HAVE_GSSAPI_GSSAPI_EXT_H */ 45 #if HAVE_GSSAPI_GSSAPI_KRB5_H 46 #include <gssapi/gssapi_krb5.h> 47 #endif /* HAVE_GSSAPI_GSSAPI_KRB5_H */ 48 #if HAVE_GSSAPI_GSSAPI_GENERIC_H 49 #include <gssapi/gssapi_generic.h> 50 #endif /* HAVE_GSSAPI_GSSAPI_GENERIC_H */ 51 52 #ifndef gss_nt_service_name 53 #define gss_nt_service_name GSS_C_NT_HOSTBASED_SERVICE 54 #endif 55 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 61 static char err_code[17]; 62 const char *KRB5_CALLCONV 63 error_message( long code) { 64 snprintf(err_code,16, "%ld" ,code); 65 return err_code; 66 } 67 #endif 68 69 #ifndef gss_mech_spnego 70 static gss_OID_desc _gss_mech_spnego = 71 { 6, ( void *) "\x2b\x06\x01\x05\x05\x02" }; 72 gss_OID gss_mech_spnego = &_gss_mech_spnego; 73 #endif 74 75 #if USE_IBM_KERBEROS 76 #include <ibm_svc/krb5_svc.h> 77 const char *KRB5_CALLCONV error_message( long code) { 78 char *msg = nullptr ; 79 krb5_svc_get_msg(code, &msg); 80 return msg; 81 } 82 #endif 83 84 /* 85 * Kerberos context and cache structure 86 * Caches authentication details to reduce 87 * number of authentication requests to kdc 88 */ 89 static struct kstruct { 90 krb5_context context; 91 krb5_ccache cc; 92 } kparam = { 93 nullptr , nullptr 94 }; 95 96 /* 97 * krb5_create_cache creates a Kerberos file credential cache or a memory 98 * credential cache if supported.
The initial key for the principal 99 * principal_name is extracted from the keytab keytab_filename. 100 * 101 * If keytab_filename is NULL the default will be used. 102 * If principal_name is NULL the first working entry of the keytab will be used. 103 */ 104 int krb5_create_cache( char *keytab_filename, char *principal_name); 105 106 /* 107 * krb5_cleanup clears used Keberos memory 108 */ 109 void krb5_cleanup( void ); 110 111 /* 112 * check_gss_err checks for gssapi error codes, extracts the error message 113 * and prints it.
114 */ 115 int check_gss_err (OM_uint32 major_status, OM_uint32 minor_status, 116 const char *function); 117 118 int 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; 124 char buf[1024]; 125 size_t len; 126 127 len = 0; 128 msg_ctx = 0; 129 while (!msg_ctx) { 130 /* convert major status code (GSS-API error) to text */ 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; 138 } 139 gss_release_buffer(&min_stat, &status_string); 140 break ; 141 } 142 gss_release_buffer(&min_stat, &status_string); 143 } 144 if ( sizeof (buf) > len + 2) { 145 strcpy(buf + len, ".
" ); 146 len += 2; 147 } 148 msg_ctx = 0; 149 while (!msg_ctx) { 150 /* convert minor status code (underlying routine error) to text */ 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; 158 } 159 gss_release_buffer(&min_stat, &status_string); 160 break ; 161 } 162 gss_release_buffer(&min_stat, &status_string); 163 } 164 debugs (11, 5, function << "failed: " << buf); 165 return (1); 166 } 167 return (0); 168 } 169 170 void krb5_cleanup() { 171 debugs (11, 5, "Cleanup kerberos context" ); 172 if (kparam.context) { 173 if (kparam.cc) 174 krb5_cc_destroy(kparam.context, kparam.cc); 175 kparam.cc = nullptr ; 176 krb5_free_context(kparam.context); 177 kparam.context = nullptr ; 178 } 179 } 180 181 int krb5_create_cache( char *kf, char *pn) { 182 183 #define KT_PATH_MAX 256 184 #define MAX_RENEW_TIME "365d" 185 #define DEFAULT_SKEW (krb5_deltat) 600 186 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; 194 #endif 195 static krb5_principal principal = nullptr ; 196 static krb5_deltat skew; 197 198 #if HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC 199 krb5_get_init_creds_opt *options; 200 #else 201 krb5_get_init_creds_opt options; 202 #endif 203 krb5_error_code code = 0; 204 krb5_deltat rlife; 205 #if HAVE_PROFILE_H && HAVE_KRB5_GET_PROFILE && HAVE_PROFILE_GET_INTEGER && HAVE_PROFILE_RELEASE 206 profile_t profile; 207 #endif 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; 212 #else 213 krb5_realm client_realm; 214 #endif 215 #endif 216 char *mem_cache; 217 218 restart: 219 /* 220 * Check if credentials need to be renewed 221 */ 222 if (creds && 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 227 /* renew ticket */ 228 code = 229 krb5_get_renewed_creds(kparam.context, creds, principal, 230 kparam.cc, nullptr ); 231 #else 232 /* renew ticket */ 233 flags.i = 0; 234 flags.b.renewable = flags.b.renew = 1; 235 236 code = 237 krb5_cc_get_principal(kparam.context, kparam.cc, 238 &creds2.client); 239 if (code) { 240 debugs (11, 5, 241 242 "Error while getting principal from credential cache : " 243 << error_message(code)); 244 return (1); 245 } 246 #if HAVE_KRB5_PRINCIPAL_GET_REALM 247 client_realm = krb5_principal_get_realm(kparam.context, principal); 248 #else 249 client_realm = krb5_princ_realm(kparam.context, creds2.client); 250 #endif 251 code = 252 krb5_make_principal(kparam.context, &creds2.server, 253 (krb5_const_realm)&client_realm, KRB5_TGS_NAME, 254 (krb5_const_realm)&client_realm, nullptr ); 255 if (code) { 256 debugs (11, 5, 257 "Error while getting krbtgt principal : " << 258 error_message(code)); 259 return (1); 260 } 261 code = 262 krb5_get_kdc_cred(kparam.context, kparam.cc, flags, nullptr , 263 nullptr , &creds2, &creds); 264 krb5_free_creds(kparam.context, &creds2); 265 #endif 266 if (code) { 267 if (code == KRB5KRB_AP_ERR_T
The rest of this page is on the live site.