Text of code-reference.squid-cache.org › support__krb5_8cc_source.html as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Squid Web Cache: squid/src/acl/external/kerberos_ldap_group/support_krb5.cc Source File

Squid Web Cache master Loading... Searching... No Matches support_krb5.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 * ---------------------------------------------------------------------------- 11 * 12 * Author: Markus Moeller (markus_moeller at compuserve.com) 13 * 14 * Copyright (C) 2007 Markus Moeller.

All rights reserved. 15 * 16 * This program is free software; you can redistribute it and/or modify 17 * it under the terms of the GNU General Public License as published by 18 * the Free Software Foundation; either version 2 of the License, or 19 * (at your option) any later version. 20 * 21 * This program is distributed in the hope that it will be useful, 22 * but WITHOUT ANY WARRANTY; without even the implied warranty of 23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the 24 * GNU General Public License for more details. 25 * 26 * You should have received a copy of the GNU General Public License along 27 * with this program; if not, see <https://www.gnu.org/licenses/>. 28 * 29 * ----------------------------------------------------------------------------- 30 */ 31 32 #include " squid.h " 33 34 #include <ctime> 35 36 #if HAVE_LDAP && HAVE_KRB5 37 38 #include "support.h" 39 40 #if HAVE_KRB5 41 extern struct kstruct kparam; 42 #endif 43 44 #define KT_PATH_MAX 256 45 46 void 47 krb5_cleanup() 48 { 49 if (kparam.context) 50 for ( int i=0; i<MAX_DOMAINS; i++) { 51 if (kparam.cc[i]) 52 krb5_cc_destroy(kparam.context, kparam.cc[i]); 53 safe_free (kparam.mem_ccache[i]); 54 } 55 krb5_free_context(kparam.context); 56 } 57 58 static void 59 k5_error2( const char * msg, char * msg2, krb5_error_code code) 60 { 61 const char *errmsg; 62 errmsg = krb5_get_error_message(kparam.context, code); 63 error (( char *) "%s| %s: ERROR: %s%s : %s\n" , LogTime (), PROGRAM , msg, msg2, errmsg); 64 #if HAVE_KRB5_FREE_ERROR_MESSAGE 65 krb5_free_error_message(kparam.context, errmsg); 66 #elif HAVE_KRB5_FREE_ERROR_STRING 67 krb5_free_error_string(kparam.context, ( char *)errmsg); 68 #else 69 xfree (errmsg); 70 #endif 71 } 72 73 static void 74 k5_debug( const char * msg, krb5_error_code code) 75 { 76 const char *errmsg; 77 errmsg = krb5_get_error_message(kparam.context, code); 78 debug (( char *) "%s| %s: DEBUG: %s : %s\n" , LogTime (), PROGRAM , msg, errmsg); 79 #if HAVE_KRB5_FREE_ERROR_MESSAGE 80 krb5_free_error_message(kparam.context, errmsg); 81 #elif HAVE_KRB5_FREE_ERROR_STRING 82 krb5_free_error_string(kparam.context, ( char *)errmsg); 83 #else 84 xfree (errmsg); 85 #endif 86 } 87 88 static void 89 k5_error( const char * msg, krb5_error_code code) 90 { 91 k5_error2(msg, ( char *) "" , code); 92 } 93 94 /* 95 * create Kerberos memory cache 96 */ 97 int 98 krb5_create_cache( char *domain, char *service_principal_name) 99 { 100 101 krb5_keytab keytab = nullptr ; 102 krb5_keytab_entry entry; 103 krb5_kt_cursor cursor; 104 krb5_cc_cursor ccursor; 105 krb5_creds *creds = nullptr ; 106 krb5_principal *principal_list = nullptr ; 107 krb5_principal principal = nullptr ; 108 char *service; 109 char *keytab_name = nullptr , *principal_name = nullptr , *mem_cache = nullptr ; 110 char buf[KT_PATH_MAX], *p; 111 size_t j,nprinc = 0; 112 int retval = 0; 113 krb5_error_code code = 0; 114 int ccindex=-1; 115 116 if (!domain || !strcmp(domain, "" )) 117 return (1); 118 119 /* 120 * prepare memory credential cache 121 */ 122 #if !HAVE_KRB5_MEMORY_CACHE || HAVE_SUN_LDAP_SDK 123 mem_cache = ( char *) xmalloc (strlen( "FILE:/tmp/squid_ldap_" ) + strlen(domain) + 1 + 16); 124 snprintf(mem_cache, strlen( "FILE:/tmp/squid_ldap_" ) + strlen(domain) + 1 + 16, "FILE:/tmp/squid_ldap_%s_%d" , domain, ( int ) getpid()); 125 #else 126 mem_cache = ( char *) xmalloc (strlen( "MEMORY:squid_ldap_" ) + strlen(domain) + 1 + 16); 127 snprintf(mem_cache, strlen( "MEMORY:squid_ldap_" ) + strlen(domain) + 1 + 16, "MEMORY:squid_ldap_%s_%d" , domain, ( int ) getpid()); 128 #endif 129 130 setenv( "KRB5CCNAME" , mem_cache, 1); 131 debug (( char *) "%s| %s: DEBUG: Set credential cache to %s\n" , LogTime (), PROGRAM , mem_cache); 132 for ( int i=0; i<MAX_DOMAINS; i++) { 133 if (kparam.mem_ccache[i] && !strcmp(mem_cache,kparam.mem_ccache[i])) { 134 ccindex=i; 135 break ; 136 } 137 } 138 if ( ccindex == -1 ) { 139 kparam.mem_ccache[kparam.ncache]= xstrdup (mem_cache); 140 ccindex=kparam.ncache; 141 kparam.ncache++; 142 if ( kparam.ncache == MAX_DOMAINS ) { 143 error (( char *) "%s| %s: ERROR: Too many domains to support: # domains %d\n" , LogTime (), PROGRAM , kparam.ncache); 144 retval = 1; 145 goto cleanup; 146 } 147 code = krb5_cc_resolve(kparam.context, mem_cache, &kparam.cc[ccindex]); 148 if (code) { 149 k5_error( "Error while resolving memory ccache" , code); 150 retval = 1; 151 goto cleanup; 152 } 153 } 154 /* 155 * getting default principal from cache 156 */ 157 158 code = krb5_cc_get_principal(kparam.context, kparam.cc[ccindex], &principal); 159 if (code) { 160 if (principal) 161 krb5_free_principal(kparam.context, principal); 162 principal = nullptr ; 163 k5_debug( "No default principal found in ccache" , code); 164 } else { 165 /* 166 * Look for krbtgt and check if it is expired (or soon to be expired) 167 */ 168 code = krb5_cc_start_seq_get(kparam.context, kparam.cc[ccindex], &ccursor); 169 if (code) { 170 k5_error( "Error while starting ccache scan" , code); 171 code = krb5_cc_close (kparam.context, kparam.cc[ccindex]); 172 if (code) { 173 k5_error( "Error while closing ccache" , code); 174 } 175 if (kparam.cc[ccindex]) { 176 code = krb5_cc_destroy(kparam.context, kparam.cc[ccindex]); 177 if (code) { 178 k5_error( "Error while destroying ccache" , code); 179 } 180 } 181 } else { 182 krb5_error_code code2 = 0; 183 creds = static_cast< krb5_creds * > ( xcalloc (1, sizeof (*creds))); 184 while ((krb5_cc_next_cred(kparam.context, kparam.cc[ccindex], &ccursor, creds)) == 0) { 185 code2 = krb5_unparse_name(kparam.context, creds->server, &principal_name); 186 if (code2) { 187 k5_error( "Error while unparsing principal" , code2); 188 code = krb5_cc_destroy(kparam.context, kparam.cc[ccindex]); 189 if (code) { 190 k5_error( "Error while destroying ccache" , code); 191 } 192 assert (creds != nullptr ); 193 krb5_free_creds(kparam.context, creds); 194 creds = nullptr ; 195 safe_free (principal_name); 196 debug (( char *) "%s| %s: DEBUG: Reset credential cache to %s\n" , LogTime (), PROGRAM , mem_cache); 197 code = krb5_cc_resolve(kparam.context, mem_cache, &kparam.cc[ccindex]); 198 if (code) { 199 k5_error( "Error while resolving memory ccache" , code); 200 retval = 1; 201 goto cleanup; 202 } 203 code =1; 204 break ; 205 } 206 if (!strncmp(KRB5_TGS_NAME,principal_name,KRB5_TGS_NAME_SIZE)) { 207 time_t now; 208 static krb5_deltat skew=MAX_SKEW; 209 210 debug (( char *) "%s| %s: DEBUG: Found %s in cache : %s\n" , LogTime (), PROGRAM ,KRB5_TGS_NAME,principal_name); 211 /* 212 * Check time 213 */ 214 time(&now); 215 debug (( char *) "%s| %s: DEBUG: credential time diff %d\n" , LogTime (), PROGRAM , ( int )(creds->times.endtime - now)); 216 if (creds->times.endtime - now < 2*skew) { 217 debug (( char *) "%s| %s: DEBUG: credential will soon expire %d\n" , LogTime (), PROGRAM , ( int )(creds->times.endtime - now)); 218 if (principal) 219 krb5_free_principal(kparam.context, principal); 220 principal = nullptr ; 221 code = krb5_cc_destroy(kparam.context, kparam.cc[ccindex]); 222 if (code) { 223 k5_error( "Error while destroying ccache" , code); 224 } 225 assert (creds != nullptr ); 226 krb5_free_creds(kparam.context, creds); 227 creds = nullptr ; 228 safe_free (principal_name); 229 debug (( char *) "%s| %s: DEBUG: Reset credential cache to %s\n" , LogTime (), PROGRAM , mem_cache); 230 code = krb5_cc_resolve(kparam.context, mem_cach

The rest of this page is on the live site.