| 1 | /* | 
|---|
| 2 | Unix SMB/CIFS implementation. | 
|---|
| 3 | simple kerberos5 routines for active directory | 
|---|
| 4 | Copyright (C) Andrew Tridgell 2001 | 
|---|
| 5 | Copyright (C) Luke Howard 2002-2003 | 
|---|
| 6 | Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005 | 
|---|
| 7 | Copyright (C) Guenther Deschner 2005-2009 | 
|---|
| 8 |  | 
|---|
| 9 | This program is free software; you can redistribute it and/or modify | 
|---|
| 10 | it under the terms of the GNU General Public License as published by | 
|---|
| 11 | the Free Software Foundation; either version 3 of the License, or | 
|---|
| 12 | (at your option) any later version. | 
|---|
| 13 |  | 
|---|
| 14 | This program is distributed in the hope that it will be useful, | 
|---|
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 17 | GNU General Public License for more details. | 
|---|
| 18 |  | 
|---|
| 19 | You should have received a copy of the GNU General Public License | 
|---|
| 20 | along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 21 | */ | 
|---|
| 22 |  | 
|---|
| 23 | #include "includes.h" | 
|---|
| 24 | #include "smb_krb5.h" | 
|---|
| 25 | #include "authdata.h" | 
|---|
| 26 |  | 
|---|
| 27 | #ifdef HAVE_KRB5 | 
|---|
| 28 |  | 
|---|
| 29 | #define GSSAPI_CHECKSUM      0x8003             /* Checksum type value for Kerberos */ | 
|---|
| 30 | #define GSSAPI_BNDLENGTH     16                 /* Bind Length (rfc-1964 pg.3) */ | 
|---|
| 31 | #define GSSAPI_CHECKSUM_SIZE (4+GSSAPI_BNDLENGTH+4) /* Length of bind length, | 
|---|
| 32 | bind field, flags field. */ | 
|---|
| 33 |  | 
|---|
| 34 | /* MIT krb5 1.7beta3 (in Ubuntu Karmic) is missing the prototype, | 
|---|
| 35 | but still has the symbol */ | 
|---|
| 36 | #if !HAVE_DECL_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE | 
|---|
| 37 | krb5_error_code krb5_auth_con_set_req_cksumtype( | 
|---|
| 38 | krb5_context     context, | 
|---|
| 39 | krb5_auth_context      auth_context, | 
|---|
| 40 | krb5_cksumtype     cksumtype); | 
|---|
| 41 | #endif | 
|---|
| 42 |  | 
|---|
| 43 | /************************************************************** | 
|---|
| 44 | Wrappers around kerberos string functions that convert from | 
|---|
| 45 | utf8 -> unix charset and vica versa. | 
|---|
| 46 | **************************************************************/ | 
|---|
| 47 |  | 
|---|
| 48 | /************************************************************** | 
|---|
| 49 | krb5_parse_name that takes a UNIX charset. | 
|---|
| 50 | **************************************************************/ | 
|---|
| 51 |  | 
|---|
| 52 | krb5_error_code smb_krb5_parse_name(krb5_context context, | 
|---|
| 53 | const char *name, /* in unix charset */ | 
|---|
| 54 | krb5_principal *principal) | 
|---|
| 55 | { | 
|---|
| 56 | krb5_error_code ret; | 
|---|
| 57 | char *utf8_name; | 
|---|
| 58 | size_t converted_size; | 
|---|
| 59 |  | 
|---|
| 60 | if (!push_utf8_talloc(talloc_tos(), &utf8_name, name, &converted_size)) { | 
|---|
| 61 | return ENOMEM; | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | ret = krb5_parse_name(context, utf8_name, principal); | 
|---|
| 65 | TALLOC_FREE(utf8_name); | 
|---|
| 66 | return ret; | 
|---|
| 67 | } | 
|---|
| 68 |  | 
|---|
| 69 | #ifdef HAVE_KRB5_PARSE_NAME_NOREALM | 
|---|
| 70 | /************************************************************** | 
|---|
| 71 | krb5_parse_name_norealm that takes a UNIX charset. | 
|---|
| 72 | **************************************************************/ | 
|---|
| 73 |  | 
|---|
| 74 | static krb5_error_code smb_krb5_parse_name_norealm_conv(krb5_context context, | 
|---|
| 75 | const char *name, /* in unix charset */ | 
|---|
| 76 | krb5_principal *principal) | 
|---|
| 77 | { | 
|---|
| 78 | krb5_error_code ret; | 
|---|
| 79 | char *utf8_name; | 
|---|
| 80 | size_t converted_size; | 
|---|
| 81 |  | 
|---|
| 82 | *principal = NULL; | 
|---|
| 83 | if (!push_utf8_talloc(talloc_tos(), &utf8_name, name, &converted_size)) { | 
|---|
| 84 | return ENOMEM; | 
|---|
| 85 | } | 
|---|
| 86 |  | 
|---|
| 87 | ret = krb5_parse_name_norealm(context, utf8_name, principal); | 
|---|
| 88 | TALLOC_FREE(utf8_name); | 
|---|
| 89 | return ret; | 
|---|
| 90 | } | 
|---|
| 91 | #endif | 
|---|
| 92 |  | 
|---|
| 93 | /************************************************************** | 
|---|
| 94 | krb5_parse_name that returns a UNIX charset name. Must | 
|---|
| 95 | be freed with talloc_free() call. | 
|---|
| 96 | **************************************************************/ | 
|---|
| 97 |  | 
|---|
| 98 | krb5_error_code smb_krb5_unparse_name(TALLOC_CTX *mem_ctx, | 
|---|
| 99 | krb5_context context, | 
|---|
| 100 | krb5_const_principal principal, | 
|---|
| 101 | char **unix_name) | 
|---|
| 102 | { | 
|---|
| 103 | krb5_error_code ret; | 
|---|
| 104 | char *utf8_name; | 
|---|
| 105 | size_t converted_size; | 
|---|
| 106 |  | 
|---|
| 107 | *unix_name = NULL; | 
|---|
| 108 | ret = krb5_unparse_name(context, principal, &utf8_name); | 
|---|
| 109 | if (ret) { | 
|---|
| 110 | return ret; | 
|---|
| 111 | } | 
|---|
| 112 |  | 
|---|
| 113 | if (!pull_utf8_talloc(mem_ctx, unix_name, utf8_name, &converted_size)) { | 
|---|
| 114 | krb5_free_unparsed_name(context, utf8_name); | 
|---|
| 115 | return ENOMEM; | 
|---|
| 116 | } | 
|---|
| 117 | krb5_free_unparsed_name(context, utf8_name); | 
|---|
| 118 | return 0; | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | #ifndef HAVE_KRB5_SET_REAL_TIME | 
|---|
| 122 | /* | 
|---|
| 123 | * This function is not in the Heimdal mainline. | 
|---|
| 124 | */ | 
|---|
| 125 | krb5_error_code krb5_set_real_time(krb5_context context, int32_t seconds, int32_t microseconds) | 
|---|
| 126 | { | 
|---|
| 127 | krb5_error_code ret; | 
|---|
| 128 | int32_t sec, usec; | 
|---|
| 129 |  | 
|---|
| 130 | ret = krb5_us_timeofday(context, &sec, &usec); | 
|---|
| 131 | if (ret) | 
|---|
| 132 | return ret; | 
|---|
| 133 |  | 
|---|
| 134 | context->kdc_sec_offset = seconds - sec; | 
|---|
| 135 | context->kdc_usec_offset = microseconds - usec; | 
|---|
| 136 |  | 
|---|
| 137 | return 0; | 
|---|
| 138 | } | 
|---|
| 139 | #endif | 
|---|
| 140 |  | 
|---|
| 141 | #if !defined(HAVE_KRB5_SET_DEFAULT_TGS_KTYPES) | 
|---|
| 142 |  | 
|---|
| 143 | #if defined(HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES) | 
|---|
| 144 |  | 
|---|
| 145 | /* With MIT kerberos, we should use krb5_set_default_tgs_enctypes in preference | 
|---|
| 146 | * to krb5_set_default_tgs_ktypes. See | 
|---|
| 147 | *         http://lists.samba.org/archive/samba-technical/2006-July/048271.html | 
|---|
| 148 | * | 
|---|
| 149 | * If the MIT libraries are not exporting internal symbols, we will end up in | 
|---|
| 150 | * this branch, which is correct. Otherwise we will continue to use the | 
|---|
| 151 | * internal symbol | 
|---|
| 152 | */ | 
|---|
| 153 | krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc) | 
|---|
| 154 | { | 
|---|
| 155 | return krb5_set_default_tgs_enctypes(ctx, enc); | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | #elif defined(HAVE_KRB5_SET_DEFAULT_IN_TKT_ETYPES) | 
|---|
| 159 |  | 
|---|
| 160 | /* Heimdal */ | 
|---|
| 161 | krb5_error_code krb5_set_default_tgs_ktypes(krb5_context ctx, const krb5_enctype *enc) | 
|---|
| 162 | { | 
|---|
| 163 | return krb5_set_default_in_tkt_etypes(ctx, enc); | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | #endif /* HAVE_KRB5_SET_DEFAULT_TGS_ENCTYPES */ | 
|---|
| 167 |  | 
|---|
| 168 | #endif /* HAVE_KRB5_SET_DEFAULT_TGS_KTYPES */ | 
|---|
| 169 |  | 
|---|
| 170 | #if defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) | 
|---|
| 171 | /* HEIMDAL */ | 
|---|
| 172 | bool setup_kaddr( krb5_address *pkaddr, struct sockaddr_storage *paddr) | 
|---|
| 173 | { | 
|---|
| 174 | memset(pkaddr, '\0', sizeof(krb5_address)); | 
|---|
| 175 | #if defined(HAVE_IPV6) && defined(KRB5_ADDRESS_INET6) | 
|---|
| 176 | if (paddr->ss_family == AF_INET6) { | 
|---|
| 177 | pkaddr->addr_type = KRB5_ADDRESS_INET6; | 
|---|
| 178 | pkaddr->address.length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr); | 
|---|
| 179 | pkaddr->address.data = (char *)&(((struct sockaddr_in6 *)paddr)->sin6_addr); | 
|---|
| 180 | return true; | 
|---|
| 181 | } | 
|---|
| 182 | #endif | 
|---|
| 183 | if (paddr->ss_family == AF_INET) { | 
|---|
| 184 | pkaddr->addr_type = KRB5_ADDRESS_INET; | 
|---|
| 185 | pkaddr->address.length = sizeof(((struct sockaddr_in *)paddr)->sin_addr); | 
|---|
| 186 | pkaddr->address.data = (char *)&(((struct sockaddr_in *)paddr)->sin_addr); | 
|---|
| 187 | return true; | 
|---|
| 188 | } | 
|---|
| 189 | return false; | 
|---|
| 190 | } | 
|---|
| 191 | #elif defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) | 
|---|
| 192 | /* MIT */ | 
|---|
| 193 | bool setup_kaddr( krb5_address *pkaddr, struct sockaddr_storage *paddr) | 
|---|
| 194 | { | 
|---|
| 195 | memset(pkaddr, '\0', sizeof(krb5_address)); | 
|---|
| 196 | #if defined(HAVE_IPV6) && defined(ADDRTYPE_INET6) | 
|---|
| 197 | if (paddr->ss_family == AF_INET6) { | 
|---|
| 198 | pkaddr->addrtype = ADDRTYPE_INET6; | 
|---|
| 199 | pkaddr->length = sizeof(((struct sockaddr_in6 *)paddr)->sin6_addr); | 
|---|
| 200 | pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in6 *)paddr)->sin6_addr); | 
|---|
| 201 | return true; | 
|---|
| 202 | } | 
|---|
| 203 | #endif | 
|---|
| 204 | if (paddr->ss_family == AF_INET) { | 
|---|
| 205 | pkaddr->addrtype = ADDRTYPE_INET; | 
|---|
| 206 | pkaddr->length = sizeof(((struct sockaddr_in *)paddr)->sin_addr); | 
|---|
| 207 | pkaddr->contents = (krb5_octet *)&(((struct sockaddr_in *)paddr)->sin_addr); | 
|---|
| 208 | return true; | 
|---|
| 209 | } | 
|---|
| 210 | return false; | 
|---|
| 211 | } | 
|---|
| 212 | #else | 
|---|
| 213 | #error UNKNOWN_ADDRTYPE | 
|---|
| 214 | #endif | 
|---|
| 215 |  | 
|---|
| 216 | #if defined(HAVE_KRB5_PRINCIPAL2SALT) && defined(HAVE_KRB5_USE_ENCTYPE) && defined(HAVE_KRB5_STRING_TO_KEY) && defined(HAVE_KRB5_ENCRYPT_BLOCK) | 
|---|
| 217 | static int create_kerberos_key_from_string_direct(krb5_context context, | 
|---|
| 218 | krb5_principal host_princ, | 
|---|
| 219 | krb5_data *password, | 
|---|
| 220 | krb5_keyblock *key, | 
|---|
| 221 | krb5_enctype enctype) | 
|---|
| 222 | { | 
|---|
| 223 | int ret = 0; | 
|---|
| 224 | krb5_data salt; | 
|---|
| 225 | krb5_encrypt_block eblock; | 
|---|
| 226 |  | 
|---|
| 227 | ret = krb5_principal2salt(context, host_princ, &salt); | 
|---|
| 228 | if (ret) { | 
|---|
| 229 | DEBUG(1,("krb5_principal2salt failed (%s)\n", error_message(ret))); | 
|---|
| 230 | return ret; | 
|---|
| 231 | } | 
|---|
| 232 | krb5_use_enctype(context, &eblock, enctype); | 
|---|
| 233 | ret = krb5_string_to_key(context, &eblock, key, password, &salt); | 
|---|
| 234 | SAFE_FREE(salt.data); | 
|---|
| 235 |  | 
|---|
| 236 | return ret; | 
|---|
| 237 | } | 
|---|
| 238 | #elif defined(HAVE_KRB5_GET_PW_SALT) && defined(HAVE_KRB5_STRING_TO_KEY_SALT) | 
|---|
| 239 | static int create_kerberos_key_from_string_direct(krb5_context context, | 
|---|
| 240 | krb5_principal host_princ, | 
|---|
| 241 | krb5_data *password, | 
|---|
| 242 | krb5_keyblock *key, | 
|---|
| 243 | krb5_enctype enctype) | 
|---|
| 244 | { | 
|---|
| 245 | int ret; | 
|---|
| 246 | krb5_salt salt; | 
|---|
| 247 |  | 
|---|
| 248 | ret = krb5_get_pw_salt(context, host_princ, &salt); | 
|---|
| 249 | if (ret) { | 
|---|
| 250 | DEBUG(1,("krb5_get_pw_salt failed (%s)\n", error_message(ret))); | 
|---|
| 251 | return ret; | 
|---|
| 252 | } | 
|---|
| 253 |  | 
|---|
| 254 | ret = krb5_string_to_key_salt(context, enctype, (const char *)password->data, salt, key); | 
|---|
| 255 | krb5_free_salt(context, salt); | 
|---|
| 256 |  | 
|---|
| 257 | return ret; | 
|---|
| 258 | } | 
|---|
| 259 | #else | 
|---|
| 260 | #error UNKNOWN_CREATE_KEY_FUNCTIONS | 
|---|
| 261 | #endif | 
|---|
| 262 |  | 
|---|
| 263 | int create_kerberos_key_from_string(krb5_context context, | 
|---|
| 264 | krb5_principal host_princ, | 
|---|
| 265 | krb5_data *password, | 
|---|
| 266 | krb5_keyblock *key, | 
|---|
| 267 | krb5_enctype enctype, | 
|---|
| 268 | bool no_salt) | 
|---|
| 269 | { | 
|---|
| 270 | krb5_principal salt_princ = NULL; | 
|---|
| 271 | int ret; | 
|---|
| 272 | /* | 
|---|
| 273 | * Check if we've determined that the KDC is salting keys for this | 
|---|
| 274 | * principal/enctype in a non-obvious way.  If it is, try to match | 
|---|
| 275 | * its behavior. | 
|---|
| 276 | */ | 
|---|
| 277 | if (no_salt) { | 
|---|
| 278 | KRB5_KEY_DATA(key) = (KRB5_KEY_DATA_CAST *)SMB_MALLOC(password->length); | 
|---|
| 279 | if (!KRB5_KEY_DATA(key)) { | 
|---|
| 280 | return ENOMEM; | 
|---|
| 281 | } | 
|---|
| 282 | memcpy(KRB5_KEY_DATA(key), password->data, password->length); | 
|---|
| 283 | KRB5_KEY_LENGTH(key) = password->length; | 
|---|
| 284 | KRB5_KEY_TYPE(key) = enctype; | 
|---|
| 285 | return 0; | 
|---|
| 286 | } | 
|---|
| 287 | salt_princ = kerberos_fetch_salt_princ_for_host_princ(context, host_princ, enctype); | 
|---|
| 288 | ret = create_kerberos_key_from_string_direct(context, salt_princ ? salt_princ : host_princ, password, key, enctype); | 
|---|
| 289 | if (salt_princ) { | 
|---|
| 290 | krb5_free_principal(context, salt_princ); | 
|---|
| 291 | } | 
|---|
| 292 | return ret; | 
|---|
| 293 | } | 
|---|
| 294 |  | 
|---|
| 295 | #if defined(HAVE_KRB5_GET_PERMITTED_ENCTYPES) | 
|---|
| 296 | krb5_error_code get_kerberos_allowed_etypes(krb5_context context, | 
|---|
| 297 | krb5_enctype **enctypes) | 
|---|
| 298 | { | 
|---|
| 299 | return krb5_get_permitted_enctypes(context, enctypes); | 
|---|
| 300 | } | 
|---|
| 301 | #elif defined(HAVE_KRB5_GET_DEFAULT_IN_TKT_ETYPES) | 
|---|
| 302 | krb5_error_code get_kerberos_allowed_etypes(krb5_context context, | 
|---|
| 303 | krb5_enctype **enctypes) | 
|---|
| 304 | { | 
|---|
| 305 | return krb5_get_default_in_tkt_etypes(context, enctypes); | 
|---|
| 306 | } | 
|---|
| 307 | #else | 
|---|
| 308 | #error UNKNOWN_GET_ENCTYPES_FUNCTIONS | 
|---|
| 309 | #endif | 
|---|
| 310 |  | 
|---|
| 311 | #if defined(HAVE_KRB5_AUTH_CON_SETKEY) && !defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY) | 
|---|
| 312 | krb5_error_code krb5_auth_con_setuseruserkey(krb5_context context, | 
|---|
| 313 | krb5_auth_context auth_context, | 
|---|
| 314 | krb5_keyblock *keyblock) | 
|---|
| 315 | { | 
|---|
| 316 | return krb5_auth_con_setkey(context, auth_context, keyblock); | 
|---|
| 317 | } | 
|---|
| 318 | #endif | 
|---|
| 319 |  | 
|---|
| 320 | bool unwrap_edata_ntstatus(TALLOC_CTX *mem_ctx, | 
|---|
| 321 | DATA_BLOB *edata, | 
|---|
| 322 | DATA_BLOB *edata_out) | 
|---|
| 323 | { | 
|---|
| 324 | DATA_BLOB edata_contents; | 
|---|
| 325 | ASN1_DATA *data; | 
|---|
| 326 | int edata_type; | 
|---|
| 327 |  | 
|---|
| 328 | if (!edata->length) { | 
|---|
| 329 | return False; | 
|---|
| 330 | } | 
|---|
| 331 |  | 
|---|
| 332 | data = asn1_init(mem_ctx); | 
|---|
| 333 | if (data == NULL) { | 
|---|
| 334 | return false; | 
|---|
| 335 | } | 
|---|
| 336 |  | 
|---|
| 337 | asn1_load(data, *edata); | 
|---|
| 338 | asn1_start_tag(data, ASN1_SEQUENCE(0)); | 
|---|
| 339 | asn1_start_tag(data, ASN1_CONTEXT(1)); | 
|---|
| 340 | asn1_read_Integer(data, &edata_type); | 
|---|
| 341 |  | 
|---|
| 342 | if (edata_type != KRB5_PADATA_PW_SALT) { | 
|---|
| 343 | DEBUG(0,("edata is not of required type %d but of type %d\n", | 
|---|
| 344 | KRB5_PADATA_PW_SALT, edata_type)); | 
|---|
| 345 | asn1_free(data); | 
|---|
| 346 | return False; | 
|---|
| 347 | } | 
|---|
| 348 |  | 
|---|
| 349 | asn1_start_tag(data, ASN1_CONTEXT(2)); | 
|---|
| 350 | asn1_read_OctetString(data, talloc_autofree_context(), &edata_contents); | 
|---|
| 351 | asn1_end_tag(data); | 
|---|
| 352 | asn1_end_tag(data); | 
|---|
| 353 | asn1_end_tag(data); | 
|---|
| 354 | asn1_free(data); | 
|---|
| 355 |  | 
|---|
| 356 | *edata_out = data_blob_talloc(mem_ctx, edata_contents.data, edata_contents.length); | 
|---|
| 357 |  | 
|---|
| 358 | data_blob_free(&edata_contents); | 
|---|
| 359 |  | 
|---|
| 360 | return True; | 
|---|
| 361 | } | 
|---|
| 362 |  | 
|---|
| 363 |  | 
|---|
| 364 | bool unwrap_pac(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, DATA_BLOB *unwrapped_pac_data) | 
|---|
| 365 | { | 
|---|
| 366 | DATA_BLOB pac_contents; | 
|---|
| 367 | ASN1_DATA *data; | 
|---|
| 368 | int data_type; | 
|---|
| 369 |  | 
|---|
| 370 | if (!auth_data->length) { | 
|---|
| 371 | return False; | 
|---|
| 372 | } | 
|---|
| 373 |  | 
|---|
| 374 | data = asn1_init(mem_ctx); | 
|---|
| 375 | if (data == NULL) { | 
|---|
| 376 | return false; | 
|---|
| 377 | } | 
|---|
| 378 |  | 
|---|
| 379 | asn1_load(data, *auth_data); | 
|---|
| 380 | asn1_start_tag(data, ASN1_SEQUENCE(0)); | 
|---|
| 381 | asn1_start_tag(data, ASN1_SEQUENCE(0)); | 
|---|
| 382 | asn1_start_tag(data, ASN1_CONTEXT(0)); | 
|---|
| 383 | asn1_read_Integer(data, &data_type); | 
|---|
| 384 |  | 
|---|
| 385 | if (data_type != KRB5_AUTHDATA_WIN2K_PAC ) { | 
|---|
| 386 | DEBUG(10,("authorization data is not a Windows PAC (type: %d)\n", data_type)); | 
|---|
| 387 | asn1_free(data); | 
|---|
| 388 | return False; | 
|---|
| 389 | } | 
|---|
| 390 |  | 
|---|
| 391 | asn1_end_tag(data); | 
|---|
| 392 | asn1_start_tag(data, ASN1_CONTEXT(1)); | 
|---|
| 393 | asn1_read_OctetString(data, talloc_autofree_context(), &pac_contents); | 
|---|
| 394 | asn1_end_tag(data); | 
|---|
| 395 | asn1_end_tag(data); | 
|---|
| 396 | asn1_end_tag(data); | 
|---|
| 397 | asn1_free(data); | 
|---|
| 398 |  | 
|---|
| 399 | *unwrapped_pac_data = data_blob_talloc(mem_ctx, pac_contents.data, pac_contents.length); | 
|---|
| 400 |  | 
|---|
| 401 | data_blob_free(&pac_contents); | 
|---|
| 402 |  | 
|---|
| 403 | return True; | 
|---|
| 404 | } | 
|---|
| 405 |  | 
|---|
| 406 | bool get_auth_data_from_tkt(TALLOC_CTX *mem_ctx, DATA_BLOB *auth_data, krb5_ticket *tkt) | 
|---|
| 407 | { | 
|---|
| 408 | DATA_BLOB auth_data_wrapped; | 
|---|
| 409 | bool got_auth_data_pac = False; | 
|---|
| 410 | int i; | 
|---|
| 411 |  | 
|---|
| 412 | #if defined(HAVE_KRB5_TKT_ENC_PART2) | 
|---|
| 413 | if (tkt->enc_part2 && tkt->enc_part2->authorization_data && | 
|---|
| 414 | tkt->enc_part2->authorization_data[0] && | 
|---|
| 415 | tkt->enc_part2->authorization_data[0]->length) | 
|---|
| 416 | { | 
|---|
| 417 | for (i = 0; tkt->enc_part2->authorization_data[i] != NULL; i++) { | 
|---|
| 418 |  | 
|---|
| 419 | if (tkt->enc_part2->authorization_data[i]->ad_type != | 
|---|
| 420 | KRB5_AUTHDATA_IF_RELEVANT) { | 
|---|
| 421 | DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n", | 
|---|
| 422 | tkt->enc_part2->authorization_data[i]->ad_type)); | 
|---|
| 423 | continue; | 
|---|
| 424 | } | 
|---|
| 425 |  | 
|---|
| 426 | auth_data_wrapped = data_blob(tkt->enc_part2->authorization_data[i]->contents, | 
|---|
| 427 | tkt->enc_part2->authorization_data[i]->length); | 
|---|
| 428 |  | 
|---|
| 429 | /* check if it is a PAC */ | 
|---|
| 430 | got_auth_data_pac = unwrap_pac(mem_ctx, &auth_data_wrapped, auth_data); | 
|---|
| 431 | data_blob_free(&auth_data_wrapped); | 
|---|
| 432 |  | 
|---|
| 433 | if (got_auth_data_pac) { | 
|---|
| 434 | return true; | 
|---|
| 435 | } | 
|---|
| 436 | } | 
|---|
| 437 |  | 
|---|
| 438 | return got_auth_data_pac; | 
|---|
| 439 | } | 
|---|
| 440 |  | 
|---|
| 441 | #else | 
|---|
| 442 | if (tkt->ticket.authorization_data && | 
|---|
| 443 | tkt->ticket.authorization_data->len) | 
|---|
| 444 | { | 
|---|
| 445 | for (i = 0; i < tkt->ticket.authorization_data->len; i++) { | 
|---|
| 446 |  | 
|---|
| 447 | if (tkt->ticket.authorization_data->val[i].ad_type != | 
|---|
| 448 | KRB5_AUTHDATA_IF_RELEVANT) { | 
|---|
| 449 | DEBUG(10,("get_auth_data_from_tkt: ad_type is %d\n", | 
|---|
| 450 | tkt->ticket.authorization_data->val[i].ad_type)); | 
|---|
| 451 | continue; | 
|---|
| 452 | } | 
|---|
| 453 |  | 
|---|
| 454 | auth_data_wrapped = data_blob(tkt->ticket.authorization_data->val[i].ad_data.data, | 
|---|
| 455 | tkt->ticket.authorization_data->val[i].ad_data.length); | 
|---|
| 456 |  | 
|---|
| 457 | /* check if it is a PAC */ | 
|---|
| 458 | got_auth_data_pac = unwrap_pac(mem_ctx, &auth_data_wrapped, auth_data); | 
|---|
| 459 | data_blob_free(&auth_data_wrapped); | 
|---|
| 460 |  | 
|---|
| 461 | if (got_auth_data_pac) { | 
|---|
| 462 | return true; | 
|---|
| 463 | } | 
|---|
| 464 | } | 
|---|
| 465 |  | 
|---|
| 466 | return got_auth_data_pac; | 
|---|
| 467 | } | 
|---|
| 468 | #endif | 
|---|
| 469 | return False; | 
|---|
| 470 | } | 
|---|
| 471 |  | 
|---|
| 472 | krb5_const_principal get_principal_from_tkt(krb5_ticket *tkt) | 
|---|
| 473 | { | 
|---|
| 474 | #if defined(HAVE_KRB5_TKT_ENC_PART2) | 
|---|
| 475 | return tkt->enc_part2->client; | 
|---|
| 476 | #else | 
|---|
| 477 | return tkt->client; | 
|---|
| 478 | #endif | 
|---|
| 479 | } | 
|---|
| 480 |  | 
|---|
| 481 | #if !defined(HAVE_KRB5_LOCATE_KDC) | 
|---|
| 482 |  | 
|---|
| 483 | /* krb5_locate_kdc is an internal MIT symbol. MIT are not yet willing to commit | 
|---|
| 484 | * to a public interface for this functionality, so we have to be able to live | 
|---|
| 485 | * without it if the MIT libraries are hiding their internal symbols. | 
|---|
| 486 | */ | 
|---|
| 487 |  | 
|---|
| 488 | #if defined(KRB5_KRBHST_INIT) | 
|---|
| 489 | /* Heimdal */ | 
|---|
| 490 | krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm, struct sockaddr **addr_pp, int *naddrs, int get_masters) | 
|---|
| 491 | { | 
|---|
| 492 | krb5_krbhst_handle hnd; | 
|---|
| 493 | krb5_krbhst_info *hinfo; | 
|---|
| 494 | krb5_error_code rc; | 
|---|
| 495 | int num_kdcs, i; | 
|---|
| 496 | struct sockaddr *sa; | 
|---|
| 497 | struct addrinfo *ai; | 
|---|
| 498 |  | 
|---|
| 499 | *addr_pp = NULL; | 
|---|
| 500 | *naddrs = 0; | 
|---|
| 501 |  | 
|---|
| 502 | rc = krb5_krbhst_init(ctx, realm->data, KRB5_KRBHST_KDC, &hnd); | 
|---|
| 503 | if (rc) { | 
|---|
| 504 | DEBUG(0, ("smb_krb5_locate_kdc: krb5_krbhst_init failed (%s)\n", error_message(rc))); | 
|---|
| 505 | return rc; | 
|---|
| 506 | } | 
|---|
| 507 |  | 
|---|
| 508 | for ( num_kdcs = 0; (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); num_kdcs++) | 
|---|
| 509 | ; | 
|---|
| 510 |  | 
|---|
| 511 | krb5_krbhst_reset(ctx, hnd); | 
|---|
| 512 |  | 
|---|
| 513 | if (!num_kdcs) { | 
|---|
| 514 | DEBUG(0, ("smb_krb5_locate_kdc: zero kdcs found !\n")); | 
|---|
| 515 | krb5_krbhst_free(ctx, hnd); | 
|---|
| 516 | return -1; | 
|---|
| 517 | } | 
|---|
| 518 |  | 
|---|
| 519 | sa = SMB_MALLOC_ARRAY( struct sockaddr, num_kdcs ); | 
|---|
| 520 | if (!sa) { | 
|---|
| 521 | DEBUG(0, ("smb_krb5_locate_kdc: malloc failed\n")); | 
|---|
| 522 | krb5_krbhst_free(ctx, hnd); | 
|---|
| 523 | naddrs = 0; | 
|---|
| 524 | return -1; | 
|---|
| 525 | } | 
|---|
| 526 |  | 
|---|
| 527 | memset(sa, '\0', sizeof(struct sockaddr) * num_kdcs ); | 
|---|
| 528 |  | 
|---|
| 529 | for (i = 0; i < num_kdcs && (rc = krb5_krbhst_next(ctx, hnd, &hinfo) == 0); i++) { | 
|---|
| 530 |  | 
|---|
| 531 | #if defined(HAVE_KRB5_KRBHST_GET_ADDRINFO) | 
|---|
| 532 | rc = krb5_krbhst_get_addrinfo(ctx, hinfo, &ai); | 
|---|
| 533 | if (rc) { | 
|---|
| 534 | DEBUG(0,("krb5_krbhst_get_addrinfo failed: %s\n", error_message(rc))); | 
|---|
| 535 | continue; | 
|---|
| 536 | } | 
|---|
| 537 | #endif | 
|---|
| 538 | if (hinfo->ai && hinfo->ai->ai_family == AF_INET) | 
|---|
| 539 | memcpy(&sa[i], hinfo->ai->ai_addr, sizeof(struct sockaddr)); | 
|---|
| 540 | } | 
|---|
| 541 |  | 
|---|
| 542 | krb5_krbhst_free(ctx, hnd); | 
|---|
| 543 |  | 
|---|
| 544 | *naddrs = num_kdcs; | 
|---|
| 545 | *addr_pp = sa; | 
|---|
| 546 | return 0; | 
|---|
| 547 | } | 
|---|
| 548 |  | 
|---|
| 549 | #else /* ! defined(KRB5_KRBHST_INIT) */ | 
|---|
| 550 |  | 
|---|
| 551 | krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm, | 
|---|
| 552 | struct sockaddr **addr_pp, int *naddrs, int get_masters) | 
|---|
| 553 | { | 
|---|
| 554 | DEBUG(0, ("unable to explicitly locate the KDC on this platform\n")); | 
|---|
| 555 | return KRB5_KDC_UNREACH; | 
|---|
| 556 | } | 
|---|
| 557 |  | 
|---|
| 558 | #endif /* KRB5_KRBHST_INIT */ | 
|---|
| 559 |  | 
|---|
| 560 | #else /* ! HAVE_KRB5_LOCATE_KDC */ | 
|---|
| 561 |  | 
|---|
| 562 | krb5_error_code smb_krb5_locate_kdc(krb5_context ctx, const krb5_data *realm, | 
|---|
| 563 | struct sockaddr **addr_pp, int *naddrs, int get_masters) | 
|---|
| 564 | { | 
|---|
| 565 | return krb5_locate_kdc(ctx, realm, addr_pp, naddrs, get_masters); | 
|---|
| 566 | } | 
|---|
| 567 |  | 
|---|
| 568 | #endif /* HAVE_KRB5_LOCATE_KDC */ | 
|---|
| 569 |  | 
|---|
| 570 | #if !defined(HAVE_KRB5_FREE_UNPARSED_NAME) | 
|---|
| 571 | void krb5_free_unparsed_name(krb5_context context, char *val) | 
|---|
| 572 | { | 
|---|
| 573 | SAFE_FREE(val); | 
|---|
| 574 | } | 
|---|
| 575 | #endif | 
|---|
| 576 |  | 
|---|
| 577 | void kerberos_free_data_contents(krb5_context context, krb5_data *pdata) | 
|---|
| 578 | { | 
|---|
| 579 | #if defined(HAVE_KRB5_FREE_DATA_CONTENTS) | 
|---|
| 580 | if (pdata->data) { | 
|---|
| 581 | krb5_free_data_contents(context, pdata); | 
|---|
| 582 | } | 
|---|
| 583 | #else | 
|---|
| 584 | SAFE_FREE(pdata->data); | 
|---|
| 585 | #endif | 
|---|
| 586 | } | 
|---|
| 587 |  | 
|---|
| 588 | void kerberos_set_creds_enctype(krb5_creds *pcreds, int enctype) | 
|---|
| 589 | { | 
|---|
| 590 | #if defined(HAVE_KRB5_KEYBLOCK_IN_CREDS) | 
|---|
| 591 | KRB5_KEY_TYPE((&pcreds->keyblock)) = enctype; | 
|---|
| 592 | #elif defined(HAVE_KRB5_SESSION_IN_CREDS) | 
|---|
| 593 | KRB5_KEY_TYPE((&pcreds->session)) = enctype; | 
|---|
| 594 | #else | 
|---|
| 595 | #error UNKNOWN_KEYBLOCK_MEMBER_IN_KRB5_CREDS_STRUCT | 
|---|
| 596 | #endif | 
|---|
| 597 | } | 
|---|
| 598 |  | 
|---|
| 599 | bool kerberos_compatible_enctypes(krb5_context context, | 
|---|
| 600 | krb5_enctype enctype1, | 
|---|
| 601 | krb5_enctype enctype2) | 
|---|
| 602 | { | 
|---|
| 603 | #if defined(HAVE_KRB5_C_ENCTYPE_COMPARE) | 
|---|
| 604 | krb5_boolean similar = 0; | 
|---|
| 605 |  | 
|---|
| 606 | krb5_c_enctype_compare(context, enctype1, enctype2, &similar); | 
|---|
| 607 | return similar ? True : False; | 
|---|
| 608 | #elif defined(HAVE_KRB5_ENCTYPES_COMPATIBLE_KEYS) | 
|---|
| 609 | return krb5_enctypes_compatible_keys(context, enctype1, enctype2) ? True : False; | 
|---|
| 610 | #endif | 
|---|
| 611 | } | 
|---|
| 612 |  | 
|---|
| 613 | static bool ads_cleanup_expired_creds(krb5_context context, | 
|---|
| 614 | krb5_ccache  ccache, | 
|---|
| 615 | krb5_creds  *credsp) | 
|---|
| 616 | { | 
|---|
| 617 | krb5_error_code retval; | 
|---|
| 618 | const char *cc_type = krb5_cc_get_type(context, ccache); | 
|---|
| 619 |  | 
|---|
| 620 | DEBUG(3, ("ads_cleanup_expired_creds: Ticket in ccache[%s:%s] expiration %s\n", | 
|---|
| 621 | cc_type, krb5_cc_get_name(context, ccache), | 
|---|
| 622 | http_timestring(talloc_tos(), credsp->times.endtime))); | 
|---|
| 623 |  | 
|---|
| 624 | /* we will probably need new tickets if the current ones | 
|---|
| 625 | will expire within 10 seconds. | 
|---|
| 626 | */ | 
|---|
| 627 | if (credsp->times.endtime >= (time(NULL) + 10)) | 
|---|
| 628 | return False; | 
|---|
| 629 |  | 
|---|
| 630 | /* heimdal won't remove creds from a file ccache, and | 
|---|
| 631 | perhaps we shouldn't anyway, since internally we | 
|---|
| 632 | use memory ccaches, and a FILE one probably means that | 
|---|
| 633 | we're using creds obtained outside of our exectuable | 
|---|
| 634 | */ | 
|---|
| 635 | if (strequal(cc_type, "FILE")) { | 
|---|
| 636 | DEBUG(5, ("ads_cleanup_expired_creds: We do not remove creds from a %s ccache\n", cc_type)); | 
|---|
| 637 | return False; | 
|---|
| 638 | } | 
|---|
| 639 |  | 
|---|
| 640 | retval = krb5_cc_remove_cred(context, ccache, 0, credsp); | 
|---|
| 641 | if (retval) { | 
|---|
| 642 | DEBUG(1, ("ads_cleanup_expired_creds: krb5_cc_remove_cred failed, err %s\n", | 
|---|
| 643 | error_message(retval))); | 
|---|
| 644 | /* If we have an error in this, we want to display it, | 
|---|
| 645 | but continue as though we deleted it */ | 
|---|
| 646 | } | 
|---|
| 647 | return True; | 
|---|
| 648 | } | 
|---|
| 649 |  | 
|---|
| 650 | /* Allocate and setup the auth context into the state we need. */ | 
|---|
| 651 |  | 
|---|
| 652 | static krb5_error_code setup_auth_context(krb5_context context, | 
|---|
| 653 | krb5_auth_context *auth_context) | 
|---|
| 654 | { | 
|---|
| 655 | krb5_error_code retval; | 
|---|
| 656 |  | 
|---|
| 657 | retval = krb5_auth_con_init(context, auth_context ); | 
|---|
| 658 | if (retval) { | 
|---|
| 659 | DEBUG(1,("krb5_auth_con_init failed (%s)\n", | 
|---|
| 660 | error_message(retval))); | 
|---|
| 661 | return retval; | 
|---|
| 662 | } | 
|---|
| 663 |  | 
|---|
| 664 | /* Ensure this is an addressless ticket. */ | 
|---|
| 665 | retval = krb5_auth_con_setaddrs(context, *auth_context, NULL, NULL); | 
|---|
| 666 | if (retval) { | 
|---|
| 667 | DEBUG(1,("krb5_auth_con_setaddrs failed (%s)\n", | 
|---|
| 668 | error_message(retval))); | 
|---|
| 669 | } | 
|---|
| 670 |  | 
|---|
| 671 | return retval; | 
|---|
| 672 | } | 
|---|
| 673 |  | 
|---|
| 674 | static krb5_error_code create_gss_checksum(krb5_data *in_data, /* [inout] */ | 
|---|
| 675 | uint32_t gss_flags) | 
|---|
| 676 | { | 
|---|
| 677 | unsigned int orig_length = in_data->length; | 
|---|
| 678 | unsigned int base_cksum_size = GSSAPI_CHECKSUM_SIZE; | 
|---|
| 679 | char *gss_cksum = NULL; | 
|---|
| 680 |  | 
|---|
| 681 | if (orig_length) { | 
|---|
| 682 | /* Extra length field for delgated ticket. */ | 
|---|
| 683 | base_cksum_size += 4; | 
|---|
| 684 | } | 
|---|
| 685 |  | 
|---|
| 686 | if ((unsigned int)base_cksum_size + orig_length < | 
|---|
| 687 | (unsigned int)base_cksum_size) { | 
|---|
| 688 | return EINVAL; | 
|---|
| 689 | } | 
|---|
| 690 |  | 
|---|
| 691 | gss_cksum = (char *)SMB_MALLOC(base_cksum_size + orig_length); | 
|---|
| 692 | if (gss_cksum == NULL) { | 
|---|
| 693 | return ENOMEM; | 
|---|
| 694 | } | 
|---|
| 695 |  | 
|---|
| 696 | memset(gss_cksum, '\0', base_cksum_size + orig_length); | 
|---|
| 697 | SIVAL(gss_cksum, 0, GSSAPI_BNDLENGTH); | 
|---|
| 698 |  | 
|---|
| 699 | /* | 
|---|
| 700 | * GSS_C_NO_CHANNEL_BINDINGS means 16 zero bytes. | 
|---|
| 701 | * This matches the behavior of heimdal and mit. | 
|---|
| 702 | * | 
|---|
| 703 | * And it is needed to work against some closed source | 
|---|
| 704 | * SMB servers. | 
|---|
| 705 | * | 
|---|
| 706 | * See bug #7883 | 
|---|
| 707 | */ | 
|---|
| 708 | memset(&gss_cksum[4], 0x00, GSSAPI_BNDLENGTH); | 
|---|
| 709 |  | 
|---|
| 710 | SIVAL(gss_cksum, 20, gss_flags); | 
|---|
| 711 |  | 
|---|
| 712 | if (orig_length) { | 
|---|
| 713 | SSVAL(gss_cksum, 24, 1); /* The Delegation Option identifier */ | 
|---|
| 714 | SSVAL(gss_cksum, 26, orig_length); | 
|---|
| 715 | /* Copy the kerberos KRB_CRED data */ | 
|---|
| 716 | memcpy(gss_cksum + 28, in_data->data, orig_length); | 
|---|
| 717 | free(in_data->data); | 
|---|
| 718 | in_data->data = NULL; | 
|---|
| 719 | in_data->length = 0; | 
|---|
| 720 | } | 
|---|
| 721 | in_data->data = gss_cksum; | 
|---|
| 722 | in_data->length = base_cksum_size + orig_length; | 
|---|
| 723 | return 0; | 
|---|
| 724 | } | 
|---|
| 725 |  | 
|---|
| 726 | /* | 
|---|
| 727 | we can't use krb5_mk_req because w2k wants the service to be in a particular format | 
|---|
| 728 | */ | 
|---|
| 729 | static krb5_error_code ads_krb5_mk_req(krb5_context context, | 
|---|
| 730 | krb5_auth_context *auth_context, | 
|---|
| 731 | const krb5_flags ap_req_options, | 
|---|
| 732 | const char *principal, | 
|---|
| 733 | krb5_ccache ccache, | 
|---|
| 734 | krb5_data *outbuf, | 
|---|
| 735 | time_t *expire_time, | 
|---|
| 736 | const char *impersonate_princ_s) | 
|---|
| 737 | { | 
|---|
| 738 | krb5_error_code           retval; | 
|---|
| 739 | krb5_principal    server; | 
|---|
| 740 | krb5_principal impersonate_princ = NULL; | 
|---|
| 741 | krb5_creds              * credsp; | 
|---|
| 742 | krb5_creds                creds; | 
|---|
| 743 | krb5_data in_data; | 
|---|
| 744 | bool creds_ready = False; | 
|---|
| 745 | int i = 0, maxtries = 3; | 
|---|
| 746 | uint32_t gss_flags = 0; | 
|---|
| 747 |  | 
|---|
| 748 | ZERO_STRUCT(in_data); | 
|---|
| 749 |  | 
|---|
| 750 | retval = smb_krb5_parse_name(context, principal, &server); | 
|---|
| 751 | if (retval) { | 
|---|
| 752 | DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal)); | 
|---|
| 753 | return retval; | 
|---|
| 754 | } | 
|---|
| 755 |  | 
|---|
| 756 | if (impersonate_princ_s) { | 
|---|
| 757 | retval = smb_krb5_parse_name(context, impersonate_princ_s, | 
|---|
| 758 | &impersonate_princ); | 
|---|
| 759 | if (retval) { | 
|---|
| 760 | DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", impersonate_princ_s)); | 
|---|
| 761 | goto cleanup_princ; | 
|---|
| 762 | } | 
|---|
| 763 | } | 
|---|
| 764 |  | 
|---|
| 765 | /* obtain ticket & session key */ | 
|---|
| 766 | ZERO_STRUCT(creds); | 
|---|
| 767 | if ((retval = krb5_copy_principal(context, server, &creds.server))) { | 
|---|
| 768 | DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n", | 
|---|
| 769 | error_message(retval))); | 
|---|
| 770 | goto cleanup_princ; | 
|---|
| 771 | } | 
|---|
| 772 |  | 
|---|
| 773 | if ((retval = krb5_cc_get_principal(context, ccache, &creds.client))) { | 
|---|
| 774 | /* This can commonly fail on smbd startup with no ticket in the cache. | 
|---|
| 775 | * Report at higher level than 1. */ | 
|---|
| 776 | DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n", | 
|---|
| 777 | error_message(retval))); | 
|---|
| 778 | goto cleanup_creds; | 
|---|
| 779 | } | 
|---|
| 780 |  | 
|---|
| 781 | while (!creds_ready && (i < maxtries)) { | 
|---|
| 782 |  | 
|---|
| 783 | if ((retval = smb_krb5_get_credentials(context, ccache, | 
|---|
| 784 | creds.client, | 
|---|
| 785 | creds.server, | 
|---|
| 786 | impersonate_princ, | 
|---|
| 787 | &credsp))) { | 
|---|
| 788 | DEBUG(1,("ads_krb5_mk_req: smb_krb5_get_credentials failed for %s (%s)\n", | 
|---|
| 789 | principal, error_message(retval))); | 
|---|
| 790 | goto cleanup_creds; | 
|---|
| 791 | } | 
|---|
| 792 |  | 
|---|
| 793 | /* cope with ticket being in the future due to clock skew */ | 
|---|
| 794 | if ((unsigned)credsp->times.starttime > time(NULL)) { | 
|---|
| 795 | time_t t = time(NULL); | 
|---|
| 796 | int time_offset =(int)((unsigned)credsp->times.starttime-t); | 
|---|
| 797 | DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset)); | 
|---|
| 798 | krb5_set_real_time(context, t + time_offset + 1, 0); | 
|---|
| 799 | } | 
|---|
| 800 |  | 
|---|
| 801 | if (!ads_cleanup_expired_creds(context, ccache, credsp)) { | 
|---|
| 802 | creds_ready = True; | 
|---|
| 803 | } | 
|---|
| 804 |  | 
|---|
| 805 | i++; | 
|---|
| 806 | } | 
|---|
| 807 |  | 
|---|
| 808 | DEBUG(10,("ads_krb5_mk_req: Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n", | 
|---|
| 809 | principal, krb5_cc_get_type(context, ccache), krb5_cc_get_name(context, ccache), | 
|---|
| 810 | http_timestring(talloc_tos(), (unsigned)credsp->times.endtime), | 
|---|
| 811 | (unsigned)credsp->times.endtime)); | 
|---|
| 812 |  | 
|---|
| 813 | if (expire_time) { | 
|---|
| 814 | *expire_time = (time_t)credsp->times.endtime; | 
|---|
| 815 | } | 
|---|
| 816 |  | 
|---|
| 817 | /* Allocate the auth_context. */ | 
|---|
| 818 | retval = setup_auth_context(context, auth_context); | 
|---|
| 819 | if (retval) { | 
|---|
| 820 | DEBUG(1,("setup_auth_context failed (%s)\n", | 
|---|
| 821 | error_message(retval))); | 
|---|
| 822 | goto cleanup_creds; | 
|---|
| 823 | } | 
|---|
| 824 |  | 
|---|
| 825 | #if defined(TKT_FLG_OK_AS_DELEGATE ) && defined(HAVE_KRB5_FWD_TGT_CREDS) && defined(HAVE_KRB5_AUTH_CON_SETUSERUSERKEY) && defined(KRB5_AUTH_CONTEXT_USE_SUBKEY) && defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE) | 
|---|
| 826 | if( credsp->ticket_flags & TKT_FLG_OK_AS_DELEGATE ) { | 
|---|
| 827 | /* Fetch a forwarded TGT from the KDC so that we can hand off a 2nd ticket | 
|---|
| 828 | as part of the kerberos exchange. */ | 
|---|
| 829 |  | 
|---|
| 830 | DEBUG( 3, ("ads_krb5_mk_req: server marked as OK to delegate to, building forwardable TGT\n")  ); | 
|---|
| 831 |  | 
|---|
| 832 | retval = krb5_auth_con_setuseruserkey(context, | 
|---|
| 833 | *auth_context, | 
|---|
| 834 | &credsp->keyblock ); | 
|---|
| 835 | if (retval) { | 
|---|
| 836 | DEBUG(1,("krb5_auth_con_setuseruserkey failed (%s)\n", | 
|---|
| 837 | error_message(retval))); | 
|---|
| 838 | goto cleanup_creds; | 
|---|
| 839 | } | 
|---|
| 840 |  | 
|---|
| 841 | /* Must use a subkey for forwarded tickets. */ | 
|---|
| 842 | retval = krb5_auth_con_setflags(context, | 
|---|
| 843 | *auth_context, | 
|---|
| 844 | KRB5_AUTH_CONTEXT_USE_SUBKEY); | 
|---|
| 845 | if (retval) { | 
|---|
| 846 | DEBUG(1,("krb5_auth_con_setflags failed (%s)\n", | 
|---|
| 847 | error_message(retval))); | 
|---|
| 848 | goto cleanup_creds; | 
|---|
| 849 | } | 
|---|
| 850 |  | 
|---|
| 851 | retval = krb5_fwd_tgt_creds(context,/* Krb5 context [in] */ | 
|---|
| 852 | *auth_context,  /* Authentication context [in] */ | 
|---|
| 853 | CONST_DISCARD(char *, KRB5_TGS_NAME),  /* Ticket service name ("krbtgt") [in] */ | 
|---|
| 854 | credsp->client, /* Client principal for the tgt [in] */ | 
|---|
| 855 | credsp->server, /* Server principal for the tgt [in] */ | 
|---|
| 856 | ccache,         /* Credential cache to use for storage [in] */ | 
|---|
| 857 | 1,              /* Turn on for "Forwardable ticket" [in] */ | 
|---|
| 858 | &in_data );     /* Resulting response [out] */ | 
|---|
| 859 |  | 
|---|
| 860 | if (retval) { | 
|---|
| 861 | DEBUG( 3, ("krb5_fwd_tgt_creds failed (%s)\n", | 
|---|
| 862 | error_message( retval ) ) ); | 
|---|
| 863 |  | 
|---|
| 864 | /* | 
|---|
| 865 | * This is not fatal. Delete the *auth_context and continue | 
|---|
| 866 | * with krb5_mk_req_extended to get a non-forwardable ticket. | 
|---|
| 867 | */ | 
|---|
| 868 |  | 
|---|
| 869 | if (in_data.data) { | 
|---|
| 870 | free( in_data.data ); | 
|---|
| 871 | in_data.data = NULL; | 
|---|
| 872 | in_data.length = 0; | 
|---|
| 873 | } | 
|---|
| 874 | krb5_auth_con_free(context, *auth_context); | 
|---|
| 875 | *auth_context = NULL; | 
|---|
| 876 | retval = setup_auth_context(context, auth_context); | 
|---|
| 877 | if (retval) { | 
|---|
| 878 | DEBUG(1,("setup_auth_context failed (%s)\n", | 
|---|
| 879 | error_message(retval))); | 
|---|
| 880 | goto cleanup_creds; | 
|---|
| 881 | } | 
|---|
| 882 | } else { | 
|---|
| 883 | /* We got a delegated ticket. */ | 
|---|
| 884 | gss_flags |= GSS_C_DELEG_FLAG; | 
|---|
| 885 | } | 
|---|
| 886 | } | 
|---|
| 887 |  | 
|---|
| 888 | /* Frees and reallocates in_data into a GSS checksum blob. */ | 
|---|
| 889 | retval = create_gss_checksum(&in_data, gss_flags); | 
|---|
| 890 | if (retval) { | 
|---|
| 891 | goto cleanup_data; | 
|---|
| 892 | } | 
|---|
| 893 |  | 
|---|
| 894 | /* We always want GSS-checksum types. */ | 
|---|
| 895 | retval = krb5_auth_con_set_req_cksumtype(context, *auth_context, GSSAPI_CHECKSUM ); | 
|---|
| 896 | if (retval) { | 
|---|
| 897 | DEBUG(1,("krb5_auth_con_set_req_cksumtype failed (%s)\n", | 
|---|
| 898 | error_message(retval))); | 
|---|
| 899 | goto cleanup_data; | 
|---|
| 900 | } | 
|---|
| 901 | #endif | 
|---|
| 902 |  | 
|---|
| 903 | retval = krb5_mk_req_extended(context, auth_context, ap_req_options, | 
|---|
| 904 | &in_data, credsp, outbuf); | 
|---|
| 905 | if (retval) { | 
|---|
| 906 | DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n", | 
|---|
| 907 | error_message(retval))); | 
|---|
| 908 | } | 
|---|
| 909 |  | 
|---|
| 910 | cleanup_data: | 
|---|
| 911 | if (in_data.data) { | 
|---|
| 912 | free( in_data.data ); | 
|---|
| 913 | in_data.length = 0; | 
|---|
| 914 | } | 
|---|
| 915 |  | 
|---|
| 916 | krb5_free_creds(context, credsp); | 
|---|
| 917 |  | 
|---|
| 918 | cleanup_creds: | 
|---|
| 919 | krb5_free_cred_contents(context, &creds); | 
|---|
| 920 |  | 
|---|
| 921 | cleanup_princ: | 
|---|
| 922 | krb5_free_principal(context, server); | 
|---|
| 923 | if (impersonate_princ) { | 
|---|
| 924 | krb5_free_principal(context, impersonate_princ); | 
|---|
| 925 | } | 
|---|
| 926 |  | 
|---|
| 927 | return retval; | 
|---|
| 928 | } | 
|---|
| 929 |  | 
|---|
| 930 | /* | 
|---|
| 931 | get a kerberos5 ticket for the given service | 
|---|
| 932 | */ | 
|---|
| 933 | int cli_krb5_get_ticket(const char *principal, time_t time_offset, | 
|---|
| 934 | DATA_BLOB *ticket, DATA_BLOB *session_key_krb5, | 
|---|
| 935 | uint32 extra_ap_opts, const char *ccname, | 
|---|
| 936 | time_t *tgs_expire, | 
|---|
| 937 | const char *impersonate_princ_s) | 
|---|
| 938 |  | 
|---|
| 939 | { | 
|---|
| 940 | krb5_error_code retval; | 
|---|
| 941 | krb5_data packet; | 
|---|
| 942 | krb5_context context = NULL; | 
|---|
| 943 | krb5_ccache ccdef = NULL; | 
|---|
| 944 | krb5_auth_context auth_context = NULL; | 
|---|
| 945 | krb5_enctype enc_types[] = { | 
|---|
| 946 | #ifdef ENCTYPE_ARCFOUR_HMAC | 
|---|
| 947 | ENCTYPE_ARCFOUR_HMAC, | 
|---|
| 948 | #endif | 
|---|
| 949 | ENCTYPE_DES_CBC_MD5, | 
|---|
| 950 | ENCTYPE_DES_CBC_CRC, | 
|---|
| 951 | ENCTYPE_NULL}; | 
|---|
| 952 |  | 
|---|
| 953 | initialize_krb5_error_table(); | 
|---|
| 954 | retval = krb5_init_context(&context); | 
|---|
| 955 | if (retval) { | 
|---|
| 956 | DEBUG(1,("cli_krb5_get_ticket: krb5_init_context failed (%s)\n", | 
|---|
| 957 | error_message(retval))); | 
|---|
| 958 | goto failed; | 
|---|
| 959 | } | 
|---|
| 960 |  | 
|---|
| 961 | if (time_offset != 0) { | 
|---|
| 962 | krb5_set_real_time(context, time(NULL) + time_offset, 0); | 
|---|
| 963 | } | 
|---|
| 964 |  | 
|---|
| 965 | if ((retval = krb5_cc_resolve(context, ccname ? | 
|---|
| 966 | ccname : krb5_cc_default_name(context), &ccdef))) { | 
|---|
| 967 | DEBUG(1,("cli_krb5_get_ticket: krb5_cc_default failed (%s)\n", | 
|---|
| 968 | error_message(retval))); | 
|---|
| 969 | goto failed; | 
|---|
| 970 | } | 
|---|
| 971 |  | 
|---|
| 972 | if ((retval = krb5_set_default_tgs_ktypes(context, enc_types))) { | 
|---|
| 973 | DEBUG(1,("cli_krb5_get_ticket: krb5_set_default_tgs_ktypes failed (%s)\n", | 
|---|
| 974 | error_message(retval))); | 
|---|
| 975 | goto failed; | 
|---|
| 976 | } | 
|---|
| 977 |  | 
|---|
| 978 | if ((retval = ads_krb5_mk_req(context, | 
|---|
| 979 | &auth_context, | 
|---|
| 980 | AP_OPTS_USE_SUBKEY | (krb5_flags)extra_ap_opts, | 
|---|
| 981 | principal, | 
|---|
| 982 | ccdef, &packet, | 
|---|
| 983 | tgs_expire, | 
|---|
| 984 | impersonate_princ_s))) { | 
|---|
| 985 | goto failed; | 
|---|
| 986 | } | 
|---|
| 987 |  | 
|---|
| 988 | get_krb5_smb_session_key(context, auth_context, session_key_krb5, False); | 
|---|
| 989 |  | 
|---|
| 990 | *ticket = data_blob(packet.data, packet.length); | 
|---|
| 991 |  | 
|---|
| 992 | kerberos_free_data_contents(context, &packet); | 
|---|
| 993 |  | 
|---|
| 994 | failed: | 
|---|
| 995 |  | 
|---|
| 996 | if ( context ) { | 
|---|
| 997 | if (ccdef) | 
|---|
| 998 | krb5_cc_close(context, ccdef); | 
|---|
| 999 | if (auth_context) | 
|---|
| 1000 | krb5_auth_con_free(context, auth_context); | 
|---|
| 1001 | krb5_free_context(context); | 
|---|
| 1002 | } | 
|---|
| 1003 |  | 
|---|
| 1004 | return retval; | 
|---|
| 1005 | } | 
|---|
| 1006 |  | 
|---|
| 1007 | bool get_krb5_smb_session_key(krb5_context context, krb5_auth_context auth_context, DATA_BLOB *session_key, bool remote) | 
|---|
| 1008 | { | 
|---|
| 1009 | krb5_keyblock *skey = NULL; | 
|---|
| 1010 | krb5_error_code err = 0; | 
|---|
| 1011 | bool ret = false; | 
|---|
| 1012 |  | 
|---|
| 1013 | if (remote) { | 
|---|
| 1014 | err = krb5_auth_con_getremotesubkey(context, auth_context, &skey); | 
|---|
| 1015 | } else { | 
|---|
| 1016 | err = krb5_auth_con_getlocalsubkey(context, auth_context, &skey); | 
|---|
| 1017 | } | 
|---|
| 1018 |  | 
|---|
| 1019 | if (err || skey == NULL) { | 
|---|
| 1020 | DEBUG(10, ("KRB5 error getting session key %d\n", err)); | 
|---|
| 1021 | goto done; | 
|---|
| 1022 | } | 
|---|
| 1023 |  | 
|---|
| 1024 | DEBUG(10, ("Got KRB5 session key of length %d\n",  (int)KRB5_KEY_LENGTH(skey))); | 
|---|
| 1025 | *session_key = data_blob(KRB5_KEY_DATA(skey), KRB5_KEY_LENGTH(skey)); | 
|---|
| 1026 | dump_data_pw("KRB5 Session Key:\n", session_key->data, session_key->length); | 
|---|
| 1027 |  | 
|---|
| 1028 | ret = true; | 
|---|
| 1029 |  | 
|---|
| 1030 | done: | 
|---|
| 1031 | if (skey) { | 
|---|
| 1032 | krb5_free_keyblock(context, skey); | 
|---|
| 1033 | } | 
|---|
| 1034 |  | 
|---|
| 1035 | return ret; | 
|---|
| 1036 | } | 
|---|
| 1037 |  | 
|---|
| 1038 |  | 
|---|
| 1039 | #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT) | 
|---|
| 1040 | const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i ); | 
|---|
| 1041 |  | 
|---|
| 1042 | const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i ) | 
|---|
| 1043 | { | 
|---|
| 1044 | static krb5_data kdata; | 
|---|
| 1045 |  | 
|---|
| 1046 | kdata.data = (char *)krb5_principal_get_comp_string(context, principal, i); | 
|---|
| 1047 | kdata.length = strlen((const char *)kdata.data); | 
|---|
| 1048 | return &kdata; | 
|---|
| 1049 | } | 
|---|
| 1050 | #endif | 
|---|
| 1051 |  | 
|---|
| 1052 | krb5_error_code smb_krb5_kt_free_entry(krb5_context context, krb5_keytab_entry *kt_entry) | 
|---|
| 1053 | { | 
|---|
| 1054 | /* Try krb5_free_keytab_entry_contents first, since | 
|---|
| 1055 | * MIT Kerberos >= 1.7 has both krb5_free_keytab_entry_contents and | 
|---|
| 1056 | * krb5_kt_free_entry but only has a prototype for the first, while the | 
|---|
| 1057 | * second is considered private. | 
|---|
| 1058 | */ | 
|---|
| 1059 | #if defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS) | 
|---|
| 1060 | return krb5_free_keytab_entry_contents(context, kt_entry); | 
|---|
| 1061 | #elif defined(HAVE_KRB5_KT_FREE_ENTRY) | 
|---|
| 1062 | return krb5_kt_free_entry(context, kt_entry); | 
|---|
| 1063 | #else | 
|---|
| 1064 | #error UNKNOWN_KT_FREE_FUNCTION | 
|---|
| 1065 | #endif | 
|---|
| 1066 | } | 
|---|
| 1067 |  | 
|---|
| 1068 | void smb_krb5_checksum_from_pac_sig(krb5_checksum *cksum, | 
|---|
| 1069 | struct PAC_SIGNATURE_DATA *sig) | 
|---|
| 1070 | { | 
|---|
| 1071 | #ifdef HAVE_CHECKSUM_IN_KRB5_CHECKSUM | 
|---|
| 1072 | cksum->cksumtype        = (krb5_cksumtype)sig->type; | 
|---|
| 1073 | cksum->checksum.length  = sig->signature.length; | 
|---|
| 1074 | cksum->checksum.data    = sig->signature.data; | 
|---|
| 1075 | #else | 
|---|
| 1076 | cksum->checksum_type    = (krb5_cksumtype)sig->type; | 
|---|
| 1077 | cksum->length           = sig->signature.length; | 
|---|
| 1078 | cksum->contents         = sig->signature.data; | 
|---|
| 1079 | #endif | 
|---|
| 1080 | } | 
|---|
| 1081 |  | 
|---|
| 1082 | krb5_error_code smb_krb5_verify_checksum(krb5_context context, | 
|---|
| 1083 | const krb5_keyblock *keyblock, | 
|---|
| 1084 | krb5_keyusage usage, | 
|---|
| 1085 | krb5_checksum *cksum, | 
|---|
| 1086 | uint8 *data, | 
|---|
| 1087 | size_t length) | 
|---|
| 1088 | { | 
|---|
| 1089 | krb5_error_code ret; | 
|---|
| 1090 |  | 
|---|
| 1091 | /* verify the checksum */ | 
|---|
| 1092 |  | 
|---|
| 1093 | /* welcome to the wonderful world of samba's kerberos abstraction layer: | 
|---|
| 1094 | * | 
|---|
| 1095 | * function                     heimdal 0.6.1rc3        heimdal 0.7     MIT krb 1.4.2 | 
|---|
| 1096 | * ----------------------------------------------------------------------------- | 
|---|
| 1097 | * krb5_c_verify_checksum       -                       works           works | 
|---|
| 1098 | * krb5_verify_checksum         works (6 args)          works (6 args)  broken (7 args) | 
|---|
| 1099 | */ | 
|---|
| 1100 |  | 
|---|
| 1101 | #if defined(HAVE_KRB5_C_VERIFY_CHECKSUM) | 
|---|
| 1102 | { | 
|---|
| 1103 | krb5_boolean checksum_valid = False; | 
|---|
| 1104 | krb5_data input; | 
|---|
| 1105 |  | 
|---|
| 1106 | input.data = (char *)data; | 
|---|
| 1107 | input.length = length; | 
|---|
| 1108 |  | 
|---|
| 1109 | ret = krb5_c_verify_checksum(context, | 
|---|
| 1110 | keyblock, | 
|---|
| 1111 | usage, | 
|---|
| 1112 | &input, | 
|---|
| 1113 | cksum, | 
|---|
| 1114 | &checksum_valid); | 
|---|
| 1115 | if (ret) { | 
|---|
| 1116 | DEBUG(3,("smb_krb5_verify_checksum: krb5_c_verify_checksum() failed: %s\n", | 
|---|
| 1117 | error_message(ret))); | 
|---|
| 1118 | return ret; | 
|---|
| 1119 | } | 
|---|
| 1120 |  | 
|---|
| 1121 | if (!checksum_valid) | 
|---|
| 1122 | ret = KRB5KRB_AP_ERR_BAD_INTEGRITY; | 
|---|
| 1123 | } | 
|---|
| 1124 |  | 
|---|
| 1125 | #elif KRB5_VERIFY_CHECKSUM_ARGS == 6 && defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CRYPTO) && defined(HAVE_KRB5_CRYPTO_DESTROY) | 
|---|
| 1126 |  | 
|---|
| 1127 | /* Warning: MIT's krb5_verify_checksum cannot be used as it will use a key | 
|---|
| 1128 | * without enctype and it ignores any key_usage types - Guenther */ | 
|---|
| 1129 |  | 
|---|
| 1130 | { | 
|---|
| 1131 |  | 
|---|
| 1132 | krb5_crypto crypto; | 
|---|
| 1133 | ret = krb5_crypto_init(context, | 
|---|
| 1134 | keyblock, | 
|---|
| 1135 | 0, | 
|---|
| 1136 | &crypto); | 
|---|
| 1137 | if (ret) { | 
|---|
| 1138 | DEBUG(0,("smb_krb5_verify_checksum: krb5_crypto_init() failed: %s\n", | 
|---|
| 1139 | error_message(ret))); | 
|---|
| 1140 | return ret; | 
|---|
| 1141 | } | 
|---|
| 1142 |  | 
|---|
| 1143 | ret = krb5_verify_checksum(context, | 
|---|
| 1144 | crypto, | 
|---|
| 1145 | usage, | 
|---|
| 1146 | data, | 
|---|
| 1147 | length, | 
|---|
| 1148 | cksum); | 
|---|
| 1149 |  | 
|---|
| 1150 | krb5_crypto_destroy(context, crypto); | 
|---|
| 1151 | } | 
|---|
| 1152 |  | 
|---|
| 1153 | #else | 
|---|
| 1154 | #error UNKNOWN_KRB5_VERIFY_CHECKSUM_FUNCTION | 
|---|
| 1155 | #endif | 
|---|
| 1156 |  | 
|---|
| 1157 | return ret; | 
|---|
| 1158 | } | 
|---|
| 1159 |  | 
|---|
| 1160 | time_t get_authtime_from_tkt(krb5_ticket *tkt) | 
|---|
| 1161 | { | 
|---|
| 1162 | #if defined(HAVE_KRB5_TKT_ENC_PART2) | 
|---|
| 1163 | return tkt->enc_part2->times.authtime; | 
|---|
| 1164 | #else | 
|---|
| 1165 | return tkt->ticket.authtime; | 
|---|
| 1166 | #endif | 
|---|
| 1167 | } | 
|---|
| 1168 |  | 
|---|
| 1169 | #ifdef HAVE_KRB5_DECODE_AP_REQ  /* Heimdal */ | 
|---|
| 1170 | static int get_kvno_from_ap_req(krb5_ap_req *ap_req) | 
|---|
| 1171 | { | 
|---|
| 1172 | #ifdef HAVE_TICKET_POINTER_IN_KRB5_AP_REQ /* MIT */ | 
|---|
| 1173 | if (ap_req->ticket->enc_part.kvno) | 
|---|
| 1174 | return ap_req->ticket->enc_part.kvno; | 
|---|
| 1175 | #else /* Heimdal */ | 
|---|
| 1176 | if (ap_req->ticket.enc_part.kvno) | 
|---|
| 1177 | return *ap_req->ticket.enc_part.kvno; | 
|---|
| 1178 | #endif | 
|---|
| 1179 | return 0; | 
|---|
| 1180 | } | 
|---|
| 1181 |  | 
|---|
| 1182 | static krb5_enctype get_enctype_from_ap_req(krb5_ap_req *ap_req) | 
|---|
| 1183 | { | 
|---|
| 1184 | #ifdef HAVE_ETYPE_IN_ENCRYPTEDDATA /* Heimdal */ | 
|---|
| 1185 | return ap_req->ticket.enc_part.etype; | 
|---|
| 1186 | #else /* MIT */ | 
|---|
| 1187 | return ap_req->ticket->enc_part.enctype; | 
|---|
| 1188 | #endif | 
|---|
| 1189 | } | 
|---|
| 1190 | #endif  /* HAVE_KRB5_DECODE_AP_REQ */ | 
|---|
| 1191 |  | 
|---|
| 1192 | static krb5_error_code | 
|---|
| 1193 | get_key_from_keytab(krb5_context context, | 
|---|
| 1194 | krb5_const_principal server, | 
|---|
| 1195 | krb5_enctype enctype, | 
|---|
| 1196 | krb5_kvno kvno, | 
|---|
| 1197 | krb5_keyblock **out_key) | 
|---|
| 1198 | { | 
|---|
| 1199 | krb5_keytab_entry entry; | 
|---|
| 1200 | krb5_error_code ret; | 
|---|
| 1201 | krb5_keytab keytab; | 
|---|
| 1202 | char *name = NULL; | 
|---|
| 1203 | krb5_keyblock *keyp; | 
|---|
| 1204 |  | 
|---|
| 1205 | /* We have to open a new keytab handle here, as MIT does | 
|---|
| 1206 | an implicit open/getnext/close on krb5_kt_get_entry. We | 
|---|
| 1207 | may be in the middle of a keytab enumeration when this is | 
|---|
| 1208 | called. JRA. */ | 
|---|
| 1209 |  | 
|---|
| 1210 | ret = smb_krb5_open_keytab(context, NULL, False, &keytab); | 
|---|
| 1211 | if (ret) { | 
|---|
| 1212 | DEBUG(1,("get_key_from_keytab: smb_krb5_open_keytab failed (%s)\n", error_message(ret))); | 
|---|
| 1213 | return ret; | 
|---|
| 1214 | } | 
|---|
| 1215 |  | 
|---|
| 1216 | if ( DEBUGLEVEL >= 10 ) { | 
|---|
| 1217 | if (smb_krb5_unparse_name(talloc_tos(), context, server, &name) == 0) { | 
|---|
| 1218 | DEBUG(10,("get_key_from_keytab: will look for kvno %d, enctype %d and name: %s\n", | 
|---|
| 1219 | kvno, enctype, name)); | 
|---|
| 1220 | TALLOC_FREE(name); | 
|---|
| 1221 | } | 
|---|
| 1222 | } | 
|---|
| 1223 |  | 
|---|
| 1224 | ret = krb5_kt_get_entry(context, | 
|---|
| 1225 | keytab, | 
|---|
| 1226 | server, | 
|---|
| 1227 | kvno, | 
|---|
| 1228 | enctype, | 
|---|
| 1229 | &entry); | 
|---|
| 1230 |  | 
|---|
| 1231 | if (ret) { | 
|---|
| 1232 | DEBUG(0,("get_key_from_keytab: failed to retrieve key: %s\n", error_message(ret))); | 
|---|
| 1233 | goto out; | 
|---|
| 1234 | } | 
|---|
| 1235 |  | 
|---|
| 1236 | keyp = KRB5_KT_KEY(&entry); | 
|---|
| 1237 |  | 
|---|
| 1238 | ret = krb5_copy_keyblock(context, keyp, out_key); | 
|---|
| 1239 | if (ret) { | 
|---|
| 1240 | DEBUG(0,("get_key_from_keytab: failed to copy key: %s\n", error_message(ret))); | 
|---|
| 1241 | goto out; | 
|---|
| 1242 | } | 
|---|
| 1243 |  | 
|---|
| 1244 | smb_krb5_kt_free_entry(context, &entry); | 
|---|
| 1245 |  | 
|---|
| 1246 | out: | 
|---|
| 1247 | krb5_kt_close(context, keytab); | 
|---|
| 1248 | return ret; | 
|---|
| 1249 | } | 
|---|
| 1250 |  | 
|---|
| 1251 | /* Prototypes */ | 
|---|
| 1252 |  | 
|---|
| 1253 | krb5_error_code smb_krb5_get_keyinfo_from_ap_req(krb5_context context, | 
|---|
| 1254 | const krb5_data *inbuf, | 
|---|
| 1255 | krb5_kvno *kvno, | 
|---|
| 1256 | krb5_enctype *enctype) | 
|---|
| 1257 | { | 
|---|
| 1258 | #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */ | 
|---|
| 1259 | { | 
|---|
| 1260 | krb5_error_code ret; | 
|---|
| 1261 | krb5_ap_req ap_req; | 
|---|
| 1262 |  | 
|---|
| 1263 | ret = krb5_decode_ap_req(context, inbuf, &ap_req); | 
|---|
| 1264 | if (ret) | 
|---|
| 1265 | return ret; | 
|---|
| 1266 |  | 
|---|
| 1267 | *kvno = get_kvno_from_ap_req(&ap_req); | 
|---|
| 1268 | *enctype = get_enctype_from_ap_req(&ap_req); | 
|---|
| 1269 |  | 
|---|
| 1270 | free_AP_REQ(&ap_req); | 
|---|
| 1271 | return 0; | 
|---|
| 1272 | } | 
|---|
| 1273 | #endif | 
|---|
| 1274 |  | 
|---|
| 1275 | /* Possibly not an appropriate error code. */ | 
|---|
| 1276 | return KRB5KDC_ERR_BADOPTION; | 
|---|
| 1277 | } | 
|---|
| 1278 |  | 
|---|
| 1279 | krb5_error_code krb5_rd_req_return_keyblock_from_keytab(krb5_context context, | 
|---|
| 1280 | krb5_auth_context *auth_context, | 
|---|
| 1281 | const krb5_data *inbuf, | 
|---|
| 1282 | krb5_const_principal server, | 
|---|
| 1283 | krb5_keytab keytab, | 
|---|
| 1284 | krb5_flags *ap_req_options, | 
|---|
| 1285 | krb5_ticket **ticket, | 
|---|
| 1286 | krb5_keyblock **keyblock) | 
|---|
| 1287 | { | 
|---|
| 1288 | krb5_error_code ret; | 
|---|
| 1289 | krb5_kvno kvno; | 
|---|
| 1290 | krb5_enctype enctype; | 
|---|
| 1291 | krb5_keyblock *local_keyblock; | 
|---|
| 1292 |  | 
|---|
| 1293 | ret = krb5_rd_req(context, | 
|---|
| 1294 | auth_context, | 
|---|
| 1295 | inbuf, | 
|---|
| 1296 | server, | 
|---|
| 1297 | keytab, | 
|---|
| 1298 | ap_req_options, | 
|---|
| 1299 | ticket); | 
|---|
| 1300 | if (ret) { | 
|---|
| 1301 | return ret; | 
|---|
| 1302 | } | 
|---|
| 1303 |  | 
|---|
| 1304 | #ifdef KRB5_TICKET_HAS_KEYINFO | 
|---|
| 1305 | enctype = (*ticket)->enc_part.enctype; | 
|---|
| 1306 | kvno = (*ticket)->enc_part.kvno; | 
|---|
| 1307 | #else | 
|---|
| 1308 | ret = smb_krb5_get_keyinfo_from_ap_req(context, inbuf, &kvno, &enctype); | 
|---|
| 1309 | if (ret) { | 
|---|
| 1310 | return ret; | 
|---|
| 1311 | } | 
|---|
| 1312 | #endif | 
|---|
| 1313 |  | 
|---|
| 1314 | ret = get_key_from_keytab(context, | 
|---|
| 1315 | server, | 
|---|
| 1316 | enctype, | 
|---|
| 1317 | kvno, | 
|---|
| 1318 | &local_keyblock); | 
|---|
| 1319 | if (ret) { | 
|---|
| 1320 | DEBUG(0,("krb5_rd_req_return_keyblock_from_keytab: failed to call get_key_from_keytab\n")); | 
|---|
| 1321 | goto out; | 
|---|
| 1322 | } | 
|---|
| 1323 |  | 
|---|
| 1324 | out: | 
|---|
| 1325 | if (ret && local_keyblock != NULL) { | 
|---|
| 1326 | krb5_free_keyblock(context, local_keyblock); | 
|---|
| 1327 | } else { | 
|---|
| 1328 | *keyblock = local_keyblock; | 
|---|
| 1329 | } | 
|---|
| 1330 |  | 
|---|
| 1331 | return ret; | 
|---|
| 1332 | } | 
|---|
| 1333 |  | 
|---|
| 1334 | krb5_error_code smb_krb5_parse_name_norealm(krb5_context context, | 
|---|
| 1335 | const char *name, | 
|---|
| 1336 | krb5_principal *principal) | 
|---|
| 1337 | { | 
|---|
| 1338 | #ifdef HAVE_KRB5_PARSE_NAME_NOREALM | 
|---|
| 1339 | return smb_krb5_parse_name_norealm_conv(context, name, principal); | 
|---|
| 1340 | #endif | 
|---|
| 1341 |  | 
|---|
| 1342 | /* we are cheating here because parse_name will in fact set the realm. | 
|---|
| 1343 | * We don't care as the only caller of smb_krb5_parse_name_norealm | 
|---|
| 1344 | * ignores the realm anyway when calling | 
|---|
| 1345 | * smb_krb5_principal_compare_any_realm later - Guenther */ | 
|---|
| 1346 |  | 
|---|
| 1347 | return smb_krb5_parse_name(context, name, principal); | 
|---|
| 1348 | } | 
|---|
| 1349 |  | 
|---|
| 1350 | bool smb_krb5_principal_compare_any_realm(krb5_context context, | 
|---|
| 1351 | krb5_const_principal princ1, | 
|---|
| 1352 | krb5_const_principal princ2) | 
|---|
| 1353 | { | 
|---|
| 1354 | #ifdef HAVE_KRB5_PRINCIPAL_COMPARE_ANY_REALM | 
|---|
| 1355 |  | 
|---|
| 1356 | return krb5_principal_compare_any_realm(context, princ1, princ2); | 
|---|
| 1357 |  | 
|---|
| 1358 | /* krb5_princ_size is a macro in MIT */ | 
|---|
| 1359 | #elif defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size) | 
|---|
| 1360 |  | 
|---|
| 1361 | int i, len1, len2; | 
|---|
| 1362 | const krb5_data *p1, *p2; | 
|---|
| 1363 |  | 
|---|
| 1364 | len1 = krb5_princ_size(context, princ1); | 
|---|
| 1365 | len2 = krb5_princ_size(context, princ2); | 
|---|
| 1366 |  | 
|---|
| 1367 | if (len1 != len2) | 
|---|
| 1368 | return False; | 
|---|
| 1369 |  | 
|---|
| 1370 | for (i = 0; i < len1; i++) { | 
|---|
| 1371 |  | 
|---|
| 1372 | p1 = krb5_princ_component(context, CONST_DISCARD(krb5_principal, princ1), i); | 
|---|
| 1373 | p2 = krb5_princ_component(context, CONST_DISCARD(krb5_principal, princ2), i); | 
|---|
| 1374 |  | 
|---|
| 1375 | if (p1->length != p2->length || memcmp(p1->data, p2->data, p1->length)) | 
|---|
| 1376 | return False; | 
|---|
| 1377 | } | 
|---|
| 1378 |  | 
|---|
| 1379 | return True; | 
|---|
| 1380 | #else | 
|---|
| 1381 | #error NO_SUITABLE_PRINCIPAL_COMPARE_FUNCTION | 
|---|
| 1382 | #endif | 
|---|
| 1383 | } | 
|---|
| 1384 |  | 
|---|
| 1385 | krb5_error_code smb_krb5_renew_ticket(const char *ccache_string,       /* FILE:/tmp/krb5cc_0 */ | 
|---|
| 1386 | const char *client_string,       /* gd@BER.SUSE.DE */ | 
|---|
| 1387 | const char *service_string,      /* krbtgt/BER.SUSE.DE@BER.SUSE.DE */ | 
|---|
| 1388 | time_t *expire_time) | 
|---|
| 1389 | { | 
|---|
| 1390 | krb5_error_code ret; | 
|---|
| 1391 | krb5_context context = NULL; | 
|---|
| 1392 | krb5_ccache ccache = NULL; | 
|---|
| 1393 | krb5_principal client = NULL; | 
|---|
| 1394 | krb5_creds creds, creds_in, *creds_out = NULL; | 
|---|
| 1395 |  | 
|---|
| 1396 | ZERO_STRUCT(creds); | 
|---|
| 1397 | ZERO_STRUCT(creds_in); | 
|---|
| 1398 |  | 
|---|
| 1399 | initialize_krb5_error_table(); | 
|---|
| 1400 | ret = krb5_init_context(&context); | 
|---|
| 1401 | if (ret) { | 
|---|
| 1402 | goto done; | 
|---|
| 1403 | } | 
|---|
| 1404 |  | 
|---|
| 1405 | if (!ccache_string) { | 
|---|
| 1406 | ccache_string = krb5_cc_default_name(context); | 
|---|
| 1407 | } | 
|---|
| 1408 |  | 
|---|
| 1409 | if (!ccache_string) { | 
|---|
| 1410 | ret = EINVAL; | 
|---|
| 1411 | goto done; | 
|---|
| 1412 | } | 
|---|
| 1413 |  | 
|---|
| 1414 | DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string)); | 
|---|
| 1415 |  | 
|---|
| 1416 | /* FIXME: we should not fall back to defaults */ | 
|---|
| 1417 | ret = krb5_cc_resolve(context, CONST_DISCARD(char *, ccache_string), &ccache); | 
|---|
| 1418 | if (ret) { | 
|---|
| 1419 | goto done; | 
|---|
| 1420 | } | 
|---|
| 1421 |  | 
|---|
| 1422 | if (client_string) { | 
|---|
| 1423 | ret = smb_krb5_parse_name(context, client_string, &client); | 
|---|
| 1424 | if (ret) { | 
|---|
| 1425 | goto done; | 
|---|
| 1426 | } | 
|---|
| 1427 | } else { | 
|---|
| 1428 | ret = krb5_cc_get_principal(context, ccache, &client); | 
|---|
| 1429 | if (ret) { | 
|---|
| 1430 | goto done; | 
|---|
| 1431 | } | 
|---|
| 1432 | } | 
|---|
| 1433 |  | 
|---|
| 1434 | #ifdef HAVE_KRB5_GET_RENEWED_CREDS      /* MIT */ | 
|---|
| 1435 | { | 
|---|
| 1436 | ret = krb5_get_renewed_creds(context, &creds, client, ccache, CONST_DISCARD(char *, service_string)); | 
|---|
| 1437 | if (ret) { | 
|---|
| 1438 | DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret))); | 
|---|
| 1439 | goto done; | 
|---|
| 1440 | } | 
|---|
| 1441 | } | 
|---|
| 1442 | #elif defined(HAVE_KRB5_GET_KDC_CRED)   /* Heimdal */ | 
|---|
| 1443 | { | 
|---|
| 1444 | krb5_kdc_flags flags; | 
|---|
| 1445 | krb5_realm *client_realm = NULL; | 
|---|
| 1446 |  | 
|---|
| 1447 | ret = krb5_copy_principal(context, client, &creds_in.client); | 
|---|
| 1448 | if (ret) { | 
|---|
| 1449 | goto done; | 
|---|
| 1450 | } | 
|---|
| 1451 |  | 
|---|
| 1452 | if (service_string) { | 
|---|
| 1453 | ret = smb_krb5_parse_name(context, service_string, &creds_in.server); | 
|---|
| 1454 | if (ret) { | 
|---|
| 1455 | goto done; | 
|---|
| 1456 | } | 
|---|
| 1457 | } else { | 
|---|
| 1458 | /* build tgt service by default */ | 
|---|
| 1459 | client_realm = krb5_princ_realm(context, creds_in.client); | 
|---|
| 1460 | if (!client_realm) { | 
|---|
| 1461 | ret = ENOMEM; | 
|---|
| 1462 | goto done; | 
|---|
| 1463 | } | 
|---|
| 1464 | ret = krb5_make_principal(context, &creds_in.server, *client_realm, KRB5_TGS_NAME, *client_realm, NULL); | 
|---|
| 1465 | if (ret) { | 
|---|
| 1466 | goto done; | 
|---|
| 1467 | } | 
|---|
| 1468 | } | 
|---|
| 1469 |  | 
|---|
| 1470 | flags.i = 0; | 
|---|
| 1471 | flags.b.renewable = flags.b.renew = True; | 
|---|
| 1472 |  | 
|---|
| 1473 | ret = krb5_get_kdc_cred(context, ccache, flags, NULL, NULL, &creds_in, &creds_out); | 
|---|
| 1474 | if (ret) { | 
|---|
| 1475 | DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret))); | 
|---|
| 1476 | goto done; | 
|---|
| 1477 | } | 
|---|
| 1478 |  | 
|---|
| 1479 | creds = *creds_out; | 
|---|
| 1480 | } | 
|---|
| 1481 | #else | 
|---|
| 1482 | #error NO_SUITABLE_KRB5_TICKET_RENEW_FUNCTION_AVAILABLE | 
|---|
| 1483 | #endif | 
|---|
| 1484 |  | 
|---|
| 1485 | /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */ | 
|---|
| 1486 | ret = krb5_cc_initialize(context, ccache, client); | 
|---|
| 1487 | if (ret) { | 
|---|
| 1488 | goto done; | 
|---|
| 1489 | } | 
|---|
| 1490 |  | 
|---|
| 1491 | ret = krb5_cc_store_cred(context, ccache, &creds); | 
|---|
| 1492 |  | 
|---|
| 1493 | if (expire_time) { | 
|---|
| 1494 | *expire_time = (time_t) creds.times.endtime; | 
|---|
| 1495 | } | 
|---|
| 1496 |  | 
|---|
| 1497 | done: | 
|---|
| 1498 | krb5_free_cred_contents(context, &creds_in); | 
|---|
| 1499 |  | 
|---|
| 1500 | if (creds_out) { | 
|---|
| 1501 | krb5_free_creds(context, creds_out); | 
|---|
| 1502 | } else { | 
|---|
| 1503 | krb5_free_cred_contents(context, &creds); | 
|---|
| 1504 | } | 
|---|
| 1505 |  | 
|---|
| 1506 | if (client) { | 
|---|
| 1507 | krb5_free_principal(context, client); | 
|---|
| 1508 | } | 
|---|
| 1509 | if (ccache) { | 
|---|
| 1510 | krb5_cc_close(context, ccache); | 
|---|
| 1511 | } | 
|---|
| 1512 | if (context) { | 
|---|
| 1513 | krb5_free_context(context); | 
|---|
| 1514 | } | 
|---|
| 1515 |  | 
|---|
| 1516 | return ret; | 
|---|
| 1517 | } | 
|---|
| 1518 |  | 
|---|
| 1519 | krb5_error_code smb_krb5_free_addresses(krb5_context context, smb_krb5_addresses *addr) | 
|---|
| 1520 | { | 
|---|
| 1521 | krb5_error_code ret = 0; | 
|---|
| 1522 | if (addr == NULL) { | 
|---|
| 1523 | return ret; | 
|---|
| 1524 | } | 
|---|
| 1525 | #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */ | 
|---|
| 1526 | krb5_free_addresses(context, addr->addrs); | 
|---|
| 1527 | #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */ | 
|---|
| 1528 | ret = krb5_free_addresses(context, addr->addrs); | 
|---|
| 1529 | SAFE_FREE(addr->addrs); | 
|---|
| 1530 | #endif | 
|---|
| 1531 | SAFE_FREE(addr); | 
|---|
| 1532 | addr = NULL; | 
|---|
| 1533 | return ret; | 
|---|
| 1534 | } | 
|---|
| 1535 |  | 
|---|
| 1536 | krb5_error_code smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses **kerb_addr) | 
|---|
| 1537 | { | 
|---|
| 1538 | krb5_error_code ret = 0; | 
|---|
| 1539 | nstring buf; | 
|---|
| 1540 | #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */ | 
|---|
| 1541 | krb5_address **addrs = NULL; | 
|---|
| 1542 | #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */ | 
|---|
| 1543 | krb5_addresses *addrs = NULL; | 
|---|
| 1544 | #endif | 
|---|
| 1545 |  | 
|---|
| 1546 | *kerb_addr = (smb_krb5_addresses *)SMB_MALLOC(sizeof(smb_krb5_addresses)); | 
|---|
| 1547 | if (*kerb_addr == NULL) { | 
|---|
| 1548 | return ENOMEM; | 
|---|
| 1549 | } | 
|---|
| 1550 |  | 
|---|
| 1551 | put_name(buf, global_myname(), ' ', 0x20); | 
|---|
| 1552 |  | 
|---|
| 1553 | #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */ | 
|---|
| 1554 | { | 
|---|
| 1555 | int num_addr = 2; | 
|---|
| 1556 |  | 
|---|
| 1557 | addrs = (krb5_address **)SMB_MALLOC(sizeof(krb5_address *) * num_addr); | 
|---|
| 1558 | if (addrs == NULL) { | 
|---|
| 1559 | SAFE_FREE(*kerb_addr); | 
|---|
| 1560 | return ENOMEM; | 
|---|
| 1561 | } | 
|---|
| 1562 |  | 
|---|
| 1563 | memset(addrs, 0, sizeof(krb5_address *) * num_addr); | 
|---|
| 1564 |  | 
|---|
| 1565 | addrs[0] = (krb5_address *)SMB_MALLOC(sizeof(krb5_address)); | 
|---|
| 1566 | if (addrs[0] == NULL) { | 
|---|
| 1567 | SAFE_FREE(addrs); | 
|---|
| 1568 | SAFE_FREE(*kerb_addr); | 
|---|
| 1569 | return ENOMEM; | 
|---|
| 1570 | } | 
|---|
| 1571 |  | 
|---|
| 1572 | addrs[0]->magic = KV5M_ADDRESS; | 
|---|
| 1573 | addrs[0]->addrtype = KRB5_ADDR_NETBIOS; | 
|---|
| 1574 | addrs[0]->length = MAX_NETBIOSNAME_LEN; | 
|---|
| 1575 | addrs[0]->contents = (unsigned char *)SMB_MALLOC(addrs[0]->length); | 
|---|
| 1576 | if (addrs[0]->contents == NULL) { | 
|---|
| 1577 | SAFE_FREE(addrs[0]); | 
|---|
| 1578 | SAFE_FREE(addrs); | 
|---|
| 1579 | SAFE_FREE(*kerb_addr); | 
|---|
| 1580 | return ENOMEM; | 
|---|
| 1581 | } | 
|---|
| 1582 |  | 
|---|
| 1583 | memcpy(addrs[0]->contents, buf, addrs[0]->length); | 
|---|
| 1584 |  | 
|---|
| 1585 | addrs[1] = NULL; | 
|---|
| 1586 | } | 
|---|
| 1587 | #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */ | 
|---|
| 1588 | { | 
|---|
| 1589 | addrs = (krb5_addresses *)SMB_MALLOC(sizeof(krb5_addresses)); | 
|---|
| 1590 | if (addrs == NULL) { | 
|---|
| 1591 | SAFE_FREE(*kerb_addr); | 
|---|
| 1592 | return ENOMEM; | 
|---|
| 1593 | } | 
|---|
| 1594 |  | 
|---|
| 1595 | memset(addrs, 0, sizeof(krb5_addresses)); | 
|---|
| 1596 |  | 
|---|
| 1597 | addrs->len = 1; | 
|---|
| 1598 | addrs->val = (krb5_address *)SMB_MALLOC(sizeof(krb5_address)); | 
|---|
| 1599 | if (addrs->val == NULL) { | 
|---|
| 1600 | SAFE_FREE(addrs); | 
|---|
| 1601 | SAFE_FREE(kerb_addr); | 
|---|
| 1602 | return ENOMEM; | 
|---|
| 1603 | } | 
|---|
| 1604 |  | 
|---|
| 1605 | addrs->val[0].addr_type = KRB5_ADDR_NETBIOS; | 
|---|
| 1606 | addrs->val[0].address.length = MAX_NETBIOSNAME_LEN; | 
|---|
| 1607 | addrs->val[0].address.data = (unsigned char *)SMB_MALLOC(addrs->val[0].address.length); | 
|---|
| 1608 | if (addrs->val[0].address.data == NULL) { | 
|---|
| 1609 | SAFE_FREE(addrs->val); | 
|---|
| 1610 | SAFE_FREE(addrs); | 
|---|
| 1611 | SAFE_FREE(*kerb_addr); | 
|---|
| 1612 | return ENOMEM; | 
|---|
| 1613 | } | 
|---|
| 1614 |  | 
|---|
| 1615 | memcpy(addrs->val[0].address.data, buf, addrs->val[0].address.length); | 
|---|
| 1616 | } | 
|---|
| 1617 | #else | 
|---|
| 1618 | #error UNKNOWN_KRB5_ADDRESS_FORMAT | 
|---|
| 1619 | #endif | 
|---|
| 1620 | (*kerb_addr)->addrs = addrs; | 
|---|
| 1621 |  | 
|---|
| 1622 | return ret; | 
|---|
| 1623 | } | 
|---|
| 1624 |  | 
|---|
| 1625 | void smb_krb5_free_error(krb5_context context, krb5_error *krberror) | 
|---|
| 1626 | { | 
|---|
| 1627 | #ifdef HAVE_KRB5_FREE_ERROR_CONTENTS /* Heimdal */ | 
|---|
| 1628 | krb5_free_error_contents(context, krberror); | 
|---|
| 1629 | #else /* MIT */ | 
|---|
| 1630 | krb5_free_error(context, krberror); | 
|---|
| 1631 | #endif | 
|---|
| 1632 | } | 
|---|
| 1633 |  | 
|---|
| 1634 | krb5_error_code handle_krberror_packet(krb5_context context, | 
|---|
| 1635 | krb5_data *packet) | 
|---|
| 1636 | { | 
|---|
| 1637 | krb5_error_code ret; | 
|---|
| 1638 | bool got_error_code = False; | 
|---|
| 1639 |  | 
|---|
| 1640 | DEBUG(10,("handle_krberror_packet: got error packet\n")); | 
|---|
| 1641 |  | 
|---|
| 1642 | #ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */ | 
|---|
| 1643 | { | 
|---|
| 1644 | krb5_error krberror; | 
|---|
| 1645 |  | 
|---|
| 1646 | if ((ret = krb5_rd_error(context, packet, &krberror))) { | 
|---|
| 1647 | DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n", | 
|---|
| 1648 | error_message(ret))); | 
|---|
| 1649 | return ret; | 
|---|
| 1650 | } | 
|---|
| 1651 |  | 
|---|
| 1652 | if (krberror.e_data == NULL || krberror.e_data->data == NULL) { | 
|---|
| 1653 | ret = (krb5_error_code) krberror.error_code; | 
|---|
| 1654 | got_error_code = True; | 
|---|
| 1655 | } | 
|---|
| 1656 |  | 
|---|
| 1657 | smb_krb5_free_error(context, &krberror); | 
|---|
| 1658 | } | 
|---|
| 1659 | #else /* MIT */ | 
|---|
| 1660 | { | 
|---|
| 1661 | krb5_error *krberror; | 
|---|
| 1662 |  | 
|---|
| 1663 | if ((ret = krb5_rd_error(context, packet, &krberror))) { | 
|---|
| 1664 | DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n", | 
|---|
| 1665 | error_message(ret))); | 
|---|
| 1666 | return ret; | 
|---|
| 1667 | } | 
|---|
| 1668 |  | 
|---|
| 1669 | if (krberror->e_data.data == NULL) { | 
|---|
| 1670 | #if defined(ERROR_TABLE_BASE_krb5) | 
|---|
| 1671 | ret = ERROR_TABLE_BASE_krb5 + (krb5_error_code) krberror->error; | 
|---|
| 1672 | #else | 
|---|
| 1673 | ret = (krb5_error_code)krberror->error; | 
|---|
| 1674 | #endif | 
|---|
| 1675 | got_error_code = True; | 
|---|
| 1676 | } | 
|---|
| 1677 | smb_krb5_free_error(context, krberror); | 
|---|
| 1678 | } | 
|---|
| 1679 | #endif | 
|---|
| 1680 | if (got_error_code) { | 
|---|
| 1681 | DEBUG(5,("handle_krberror_packet: got KERBERR from kpasswd: %s (%d)\n", | 
|---|
| 1682 | error_message(ret), ret)); | 
|---|
| 1683 | } | 
|---|
| 1684 | return ret; | 
|---|
| 1685 | } | 
|---|
| 1686 |  | 
|---|
| 1687 | krb5_error_code smb_krb5_get_init_creds_opt_alloc(krb5_context context, | 
|---|
| 1688 | krb5_get_init_creds_opt **opt) | 
|---|
| 1689 | { | 
|---|
| 1690 | #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC | 
|---|
| 1691 | /* Heimdal or modern MIT version */ | 
|---|
| 1692 | return krb5_get_init_creds_opt_alloc(context, opt); | 
|---|
| 1693 | #else | 
|---|
| 1694 | /* Historical MIT version */ | 
|---|
| 1695 | krb5_get_init_creds_opt *my_opt; | 
|---|
| 1696 |  | 
|---|
| 1697 | *opt = NULL; | 
|---|
| 1698 |  | 
|---|
| 1699 | if ((my_opt = SMB_MALLOC_P(krb5_get_init_creds_opt)) == NULL) { | 
|---|
| 1700 | return ENOMEM; | 
|---|
| 1701 | } | 
|---|
| 1702 |  | 
|---|
| 1703 | krb5_get_init_creds_opt_init(my_opt); | 
|---|
| 1704 |  | 
|---|
| 1705 | *opt =  my_opt; | 
|---|
| 1706 | return 0; | 
|---|
| 1707 | #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC  */ | 
|---|
| 1708 | } | 
|---|
| 1709 |  | 
|---|
| 1710 | void smb_krb5_get_init_creds_opt_free(krb5_context context, | 
|---|
| 1711 | krb5_get_init_creds_opt *opt) | 
|---|
| 1712 | { | 
|---|
| 1713 | #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_FREE | 
|---|
| 1714 |  | 
|---|
| 1715 | #ifdef KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT | 
|---|
| 1716 | /* Modern MIT or Heimdal version */ | 
|---|
| 1717 | krb5_get_init_creds_opt_free(context, opt); | 
|---|
| 1718 | #else | 
|---|
| 1719 | /* Heimdal version */ | 
|---|
| 1720 | krb5_get_init_creds_opt_free(opt); | 
|---|
| 1721 | #endif /* KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT */ | 
|---|
| 1722 |  | 
|---|
| 1723 | #else /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */ | 
|---|
| 1724 | /* Historical MIT version */ | 
|---|
| 1725 | SAFE_FREE(opt); | 
|---|
| 1726 | opt = NULL; | 
|---|
| 1727 | #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */ | 
|---|
| 1728 | } | 
|---|
| 1729 |  | 
|---|
| 1730 | krb5_enctype smb_get_enctype_from_kt_entry(krb5_keytab_entry *kt_entry) | 
|---|
| 1731 | { | 
|---|
| 1732 | return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry)); | 
|---|
| 1733 | } | 
|---|
| 1734 |  | 
|---|
| 1735 |  | 
|---|
| 1736 | /* caller needs to free etype_s */ | 
|---|
| 1737 | krb5_error_code smb_krb5_enctype_to_string(krb5_context context, | 
|---|
| 1738 | krb5_enctype enctype, | 
|---|
| 1739 | char **etype_s) | 
|---|
| 1740 | { | 
|---|
| 1741 | #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG | 
|---|
| 1742 | return krb5_enctype_to_string(context, enctype, etype_s); /* Heimdal */ | 
|---|
| 1743 | #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG) | 
|---|
| 1744 | char buf[256]; | 
|---|
| 1745 | krb5_error_code ret = krb5_enctype_to_string(enctype, buf, 256); /* MIT */ | 
|---|
| 1746 | if (ret) { | 
|---|
| 1747 | return ret; | 
|---|
| 1748 | } | 
|---|
| 1749 | *etype_s = SMB_STRDUP(buf); | 
|---|
| 1750 | if (!*etype_s) { | 
|---|
| 1751 | return ENOMEM; | 
|---|
| 1752 | } | 
|---|
| 1753 | return ret; | 
|---|
| 1754 | #else | 
|---|
| 1755 | #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION | 
|---|
| 1756 | #endif | 
|---|
| 1757 | } | 
|---|
| 1758 |  | 
|---|
| 1759 | krb5_error_code smb_krb5_mk_error(krb5_context context, | 
|---|
| 1760 | krb5_error_code error_code, | 
|---|
| 1761 | const krb5_principal server, | 
|---|
| 1762 | krb5_data *reply) | 
|---|
| 1763 | { | 
|---|
| 1764 | #ifdef HAVE_SHORT_KRB5_MK_ERROR_INTERFACE /* MIT */ | 
|---|
| 1765 | /* | 
|---|
| 1766 | * The MIT interface is *terrible*. | 
|---|
| 1767 | * We have to construct this ourselves... | 
|---|
| 1768 | */ | 
|---|
| 1769 | krb5_error e; | 
|---|
| 1770 |  | 
|---|
| 1771 | memset(&e, 0, sizeof(e)); | 
|---|
| 1772 | krb5_us_timeofday(context, &e.stime, &e.susec); | 
|---|
| 1773 | e.server = server; | 
|---|
| 1774 | #if defined(krb5_err_base) | 
|---|
| 1775 | e.error = error_code - krb5_err_base; | 
|---|
| 1776 | #elif defined(ERROR_TABLE_BASE_krb5) | 
|---|
| 1777 | e.error = error_code - ERROR_TABLE_BASE_krb5; | 
|---|
| 1778 | #else | 
|---|
| 1779 | e.error = error_code; /* Almost certainly wrong, but what can we do... ? */ | 
|---|
| 1780 | #endif | 
|---|
| 1781 |  | 
|---|
| 1782 | return krb5_mk_error(context, &e, reply); | 
|---|
| 1783 | #else /* Heimdal. */ | 
|---|
| 1784 | return krb5_mk_error(context, | 
|---|
| 1785 | error_code, | 
|---|
| 1786 | NULL, | 
|---|
| 1787 | NULL, /* e_data */ | 
|---|
| 1788 | NULL, | 
|---|
| 1789 | server, | 
|---|
| 1790 | NULL, | 
|---|
| 1791 | NULL, | 
|---|
| 1792 | reply); | 
|---|
| 1793 | #endif | 
|---|
| 1794 | } | 
|---|
| 1795 |  | 
|---|
| 1796 | /********************************************************************** | 
|---|
| 1797 | * Open a krb5 keytab with flags, handles readonly or readwrite access and | 
|---|
| 1798 | * allows to process non-default keytab names. | 
|---|
| 1799 | * @param context krb5_context | 
|---|
| 1800 | * @param keytab_name_req string | 
|---|
| 1801 | * @param write_access bool if writable keytab is required | 
|---|
| 1802 | * @param krb5_keytab pointer to krb5_keytab (close with krb5_kt_close()) | 
|---|
| 1803 | * @return krb5_error_code | 
|---|
| 1804 | **********************************************************************/ | 
|---|
| 1805 |  | 
|---|
| 1806 | /* This MAX_NAME_LEN is a constant defined in krb5.h */ | 
|---|
| 1807 | #ifndef MAX_KEYTAB_NAME_LEN | 
|---|
| 1808 | #define MAX_KEYTAB_NAME_LEN 1100 | 
|---|
| 1809 | #endif | 
|---|
| 1810 |  | 
|---|
| 1811 | krb5_error_code smb_krb5_open_keytab(krb5_context context, | 
|---|
| 1812 | const char *keytab_name_req, | 
|---|
| 1813 | bool write_access, | 
|---|
| 1814 | krb5_keytab *keytab) | 
|---|
| 1815 | { | 
|---|
| 1816 | krb5_error_code ret = 0; | 
|---|
| 1817 | TALLOC_CTX *mem_ctx; | 
|---|
| 1818 | char keytab_string[MAX_KEYTAB_NAME_LEN]; | 
|---|
| 1819 | char *kt_str = NULL; | 
|---|
| 1820 | bool found_valid_name = False; | 
|---|
| 1821 | const char *pragma = "FILE"; | 
|---|
| 1822 | const char *tmp = NULL; | 
|---|
| 1823 |  | 
|---|
| 1824 | if (!write_access && !keytab_name_req) { | 
|---|
| 1825 | /* caller just wants to read the default keytab readonly, so be it */ | 
|---|
| 1826 | return krb5_kt_default(context, keytab); | 
|---|
| 1827 | } | 
|---|
| 1828 |  | 
|---|
| 1829 | mem_ctx = talloc_init("smb_krb5_open_keytab"); | 
|---|
| 1830 | if (!mem_ctx) { | 
|---|
| 1831 | return ENOMEM; | 
|---|
| 1832 | } | 
|---|
| 1833 |  | 
|---|
| 1834 | #ifdef HAVE_WRFILE_KEYTAB | 
|---|
| 1835 | if (write_access) { | 
|---|
| 1836 | pragma = "WRFILE"; | 
|---|
| 1837 | } | 
|---|
| 1838 | #endif | 
|---|
| 1839 |  | 
|---|
| 1840 | if (keytab_name_req) { | 
|---|
| 1841 |  | 
|---|
| 1842 | if (strlen(keytab_name_req) > MAX_KEYTAB_NAME_LEN) { | 
|---|
| 1843 | ret = KRB5_CONFIG_NOTENUFSPACE; | 
|---|
| 1844 | goto out; | 
|---|
| 1845 | } | 
|---|
| 1846 |  | 
|---|
| 1847 | if ((strncmp(keytab_name_req, "WRFILE:/", 8) == 0) || | 
|---|
| 1848 | (strncmp(keytab_name_req, "FILE:/", 6) == 0)) { | 
|---|
| 1849 | tmp = keytab_name_req; | 
|---|
| 1850 | goto resolve; | 
|---|
| 1851 | } | 
|---|
| 1852 |  | 
|---|
| 1853 | if (keytab_name_req[0] != '/') { | 
|---|
| 1854 | ret = KRB5_KT_BADNAME; | 
|---|
| 1855 | goto out; | 
|---|
| 1856 | } | 
|---|
| 1857 |  | 
|---|
| 1858 | tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, keytab_name_req); | 
|---|
| 1859 | if (!tmp) { | 
|---|
| 1860 | ret = ENOMEM; | 
|---|
| 1861 | goto out; | 
|---|
| 1862 | } | 
|---|
| 1863 |  | 
|---|
| 1864 | goto resolve; | 
|---|
| 1865 | } | 
|---|
| 1866 |  | 
|---|
| 1867 | /* we need to handle more complex keytab_strings, like: | 
|---|
| 1868 | * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */ | 
|---|
| 1869 |  | 
|---|
| 1870 | ret = krb5_kt_default_name(context, &keytab_string[0], MAX_KEYTAB_NAME_LEN - 2); | 
|---|
| 1871 | if (ret) { | 
|---|
| 1872 | goto out; | 
|---|
| 1873 | } | 
|---|
| 1874 |  | 
|---|
| 1875 | DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string)); | 
|---|
| 1876 |  | 
|---|
| 1877 | tmp = talloc_strdup(mem_ctx, keytab_string); | 
|---|
| 1878 | if (!tmp) { | 
|---|
| 1879 | ret = ENOMEM; | 
|---|
| 1880 | goto out; | 
|---|
| 1881 | } | 
|---|
| 1882 |  | 
|---|
| 1883 | if (strncmp(tmp, "ANY:", 4) == 0) { | 
|---|
| 1884 | tmp += 4; | 
|---|
| 1885 | } | 
|---|
| 1886 |  | 
|---|
| 1887 | memset(&keytab_string, '\0', sizeof(keytab_string)); | 
|---|
| 1888 |  | 
|---|
| 1889 | while (next_token_talloc(mem_ctx, &tmp, &kt_str, ",")) { | 
|---|
| 1890 | if (strncmp(kt_str, "WRFILE:", 7) == 0) { | 
|---|
| 1891 | found_valid_name = True; | 
|---|
| 1892 | tmp = kt_str; | 
|---|
| 1893 | tmp += 7; | 
|---|
| 1894 | } | 
|---|
| 1895 |  | 
|---|
| 1896 | if (strncmp(kt_str, "FILE:", 5) == 0) { | 
|---|
| 1897 | found_valid_name = True; | 
|---|
| 1898 | tmp = kt_str; | 
|---|
| 1899 | tmp += 5; | 
|---|
| 1900 | } | 
|---|
| 1901 |  | 
|---|
| 1902 | if (tmp[0] == '/') { | 
|---|
| 1903 | /* Treat as a FILE: keytab definition. */ | 
|---|
| 1904 | found_valid_name = true; | 
|---|
| 1905 | } | 
|---|
| 1906 |  | 
|---|
| 1907 | if (found_valid_name) { | 
|---|
| 1908 | if (tmp[0] != '/') { | 
|---|
| 1909 | ret = KRB5_KT_BADNAME; | 
|---|
| 1910 | goto out; | 
|---|
| 1911 | } | 
|---|
| 1912 |  | 
|---|
| 1913 | tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, tmp); | 
|---|
| 1914 | if (!tmp) { | 
|---|
| 1915 | ret = ENOMEM; | 
|---|
| 1916 | goto out; | 
|---|
| 1917 | } | 
|---|
| 1918 | break; | 
|---|
| 1919 | } | 
|---|
| 1920 | } | 
|---|
| 1921 |  | 
|---|
| 1922 | if (!found_valid_name) { | 
|---|
| 1923 | ret = KRB5_KT_UNKNOWN_TYPE; | 
|---|
| 1924 | goto out; | 
|---|
| 1925 | } | 
|---|
| 1926 |  | 
|---|
| 1927 | resolve: | 
|---|
| 1928 | DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp)); | 
|---|
| 1929 | ret = krb5_kt_resolve(context, tmp, keytab); | 
|---|
| 1930 |  | 
|---|
| 1931 | out: | 
|---|
| 1932 | TALLOC_FREE(mem_ctx); | 
|---|
| 1933 | return ret; | 
|---|
| 1934 | } | 
|---|
| 1935 |  | 
|---|
| 1936 | krb5_error_code smb_krb5_keytab_name(TALLOC_CTX *mem_ctx, | 
|---|
| 1937 | krb5_context context, | 
|---|
| 1938 | krb5_keytab keytab, | 
|---|
| 1939 | const char **keytab_name) | 
|---|
| 1940 | { | 
|---|
| 1941 | char keytab_string[MAX_KEYTAB_NAME_LEN]; | 
|---|
| 1942 | krb5_error_code ret = 0; | 
|---|
| 1943 |  | 
|---|
| 1944 | ret = krb5_kt_get_name(context, keytab, | 
|---|
| 1945 | keytab_string, MAX_KEYTAB_NAME_LEN - 2); | 
|---|
| 1946 | if (ret) { | 
|---|
| 1947 | return ret; | 
|---|
| 1948 | } | 
|---|
| 1949 |  | 
|---|
| 1950 | *keytab_name = talloc_strdup(mem_ctx, keytab_string); | 
|---|
| 1951 | if (!*keytab_name) { | 
|---|
| 1952 | return ENOMEM; | 
|---|
| 1953 | } | 
|---|
| 1954 |  | 
|---|
| 1955 | return ret; | 
|---|
| 1956 | } | 
|---|
| 1957 |  | 
|---|
| 1958 | #if defined(HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE) && \ | 
|---|
| 1959 | defined(HAVE_KRB5_GET_CREDS_OPT_ALLOC) && \ | 
|---|
| 1960 | defined(HAVE_KRB5_GET_CREDS) | 
|---|
| 1961 | static krb5_error_code smb_krb5_get_credentials_for_user_opt(krb5_context context, | 
|---|
| 1962 | krb5_ccache ccache, | 
|---|
| 1963 | krb5_principal me, | 
|---|
| 1964 | krb5_principal server, | 
|---|
| 1965 | krb5_principal impersonate_princ, | 
|---|
| 1966 | krb5_creds **out_creds) | 
|---|
| 1967 | { | 
|---|
| 1968 | krb5_error_code ret; | 
|---|
| 1969 | krb5_get_creds_opt opt; | 
|---|
| 1970 |  | 
|---|
| 1971 | ret = krb5_get_creds_opt_alloc(context, &opt); | 
|---|
| 1972 | if (ret) { | 
|---|
| 1973 | goto done; | 
|---|
| 1974 | } | 
|---|
| 1975 | krb5_get_creds_opt_add_options(context, opt, KRB5_GC_FORWARDABLE); | 
|---|
| 1976 |  | 
|---|
| 1977 | if (impersonate_princ) { | 
|---|
| 1978 | ret = krb5_get_creds_opt_set_impersonate(context, opt, | 
|---|
| 1979 | impersonate_princ); | 
|---|
| 1980 | if (ret) { | 
|---|
| 1981 | goto done; | 
|---|
| 1982 | } | 
|---|
| 1983 | } | 
|---|
| 1984 |  | 
|---|
| 1985 | ret = krb5_get_creds(context, opt, ccache, server, out_creds); | 
|---|
| 1986 | if (ret) { | 
|---|
| 1987 | goto done; | 
|---|
| 1988 | } | 
|---|
| 1989 |  | 
|---|
| 1990 | done: | 
|---|
| 1991 | if (opt) { | 
|---|
| 1992 | krb5_get_creds_opt_free(context, opt); | 
|---|
| 1993 | } | 
|---|
| 1994 | return ret; | 
|---|
| 1995 | } | 
|---|
| 1996 | #endif /* HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE */ | 
|---|
| 1997 |  | 
|---|
| 1998 | #ifdef HAVE_KRB5_GET_CREDENTIALS_FOR_USER | 
|---|
| 1999 | static krb5_error_code smb_krb5_get_credentials_for_user(krb5_context context, | 
|---|
| 2000 | krb5_ccache ccache, | 
|---|
| 2001 | krb5_principal me, | 
|---|
| 2002 | krb5_principal server, | 
|---|
| 2003 | krb5_principal impersonate_princ, | 
|---|
| 2004 | krb5_creds **out_creds) | 
|---|
| 2005 | { | 
|---|
| 2006 | krb5_error_code ret; | 
|---|
| 2007 | krb5_creds in_creds; | 
|---|
| 2008 |  | 
|---|
| 2009 | #if !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER | 
|---|
| 2010 | krb5_error_code KRB5_CALLCONV | 
|---|
| 2011 | krb5_get_credentials_for_user(krb5_context context, krb5_flags options, | 
|---|
| 2012 | krb5_ccache ccache, krb5_creds *in_creds, | 
|---|
| 2013 | krb5_data *subject_cert, | 
|---|
| 2014 | krb5_creds **out_creds); | 
|---|
| 2015 | #endif /* !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER */ | 
|---|
| 2016 |  | 
|---|
| 2017 | ZERO_STRUCT(in_creds); | 
|---|
| 2018 |  | 
|---|
| 2019 | if (impersonate_princ) { | 
|---|
| 2020 |  | 
|---|
| 2021 | in_creds.server = me; | 
|---|
| 2022 | in_creds.client = impersonate_princ; | 
|---|
| 2023 |  | 
|---|
| 2024 | ret = krb5_get_credentials_for_user(context, | 
|---|
| 2025 | 0, /* krb5_flags options */ | 
|---|
| 2026 | ccache, | 
|---|
| 2027 | &in_creds, | 
|---|
| 2028 | NULL, /* krb5_data *subject_cert */ | 
|---|
| 2029 | out_creds); | 
|---|
| 2030 | } else { | 
|---|
| 2031 | in_creds.client = me; | 
|---|
| 2032 | in_creds.server = server; | 
|---|
| 2033 |  | 
|---|
| 2034 | ret = krb5_get_credentials(context, 0, ccache, | 
|---|
| 2035 | &in_creds, out_creds); | 
|---|
| 2036 | } | 
|---|
| 2037 |  | 
|---|
| 2038 | return ret; | 
|---|
| 2039 | } | 
|---|
| 2040 | #endif /* HAVE_KRB5_GET_CREDENTIALS_FOR_USER */ | 
|---|
| 2041 |  | 
|---|
| 2042 | /* | 
|---|
| 2043 | * smb_krb5_get_credentials | 
|---|
| 2044 | * | 
|---|
| 2045 | * @brief Get krb5 credentials for a server | 
|---|
| 2046 | * | 
|---|
| 2047 | * @param[in] context           An initialized krb5_context | 
|---|
| 2048 | * @param[in] ccache            An initialized krb5_ccache | 
|---|
| 2049 | * @param[in] me                The krb5_principal of the caller | 
|---|
| 2050 | * @param[in] server            The krb5_principal of the requested service | 
|---|
| 2051 | * @param[in] impersonate_princ The krb5_principal of a user to impersonate as (optional) | 
|---|
| 2052 | * @param[out] out_creds        The returned krb5_creds structure | 
|---|
| 2053 | * @return krb5_error_code | 
|---|
| 2054 | * | 
|---|
| 2055 | */ | 
|---|
| 2056 | krb5_error_code smb_krb5_get_credentials(krb5_context context, | 
|---|
| 2057 | krb5_ccache ccache, | 
|---|
| 2058 | krb5_principal me, | 
|---|
| 2059 | krb5_principal server, | 
|---|
| 2060 | krb5_principal impersonate_princ, | 
|---|
| 2061 | krb5_creds **out_creds) | 
|---|
| 2062 | { | 
|---|
| 2063 | krb5_error_code ret; | 
|---|
| 2064 | krb5_creds *creds = NULL; | 
|---|
| 2065 |  | 
|---|
| 2066 | *out_creds = NULL; | 
|---|
| 2067 |  | 
|---|
| 2068 | if (impersonate_princ) { | 
|---|
| 2069 | #ifdef HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE /* Heimdal */ | 
|---|
| 2070 | ret = smb_krb5_get_credentials_for_user_opt(context, ccache, me, server, impersonate_princ, &creds); | 
|---|
| 2071 | #elif defined(HAVE_KRB5_GET_CREDENTIALS_FOR_USER) /* MIT */ | 
|---|
| 2072 | ret = smb_krb5_get_credentials_for_user(context, ccache, me, server, impersonate_princ, &creds); | 
|---|
| 2073 | #else | 
|---|
| 2074 | ret = ENOTSUP; | 
|---|
| 2075 | #endif | 
|---|
| 2076 | } else { | 
|---|
| 2077 | krb5_creds in_creds; | 
|---|
| 2078 |  | 
|---|
| 2079 | ZERO_STRUCT(in_creds); | 
|---|
| 2080 |  | 
|---|
| 2081 | in_creds.client = me; | 
|---|
| 2082 | in_creds.server = server; | 
|---|
| 2083 |  | 
|---|
| 2084 | ret = krb5_get_credentials(context, 0, ccache, | 
|---|
| 2085 | &in_creds, &creds); | 
|---|
| 2086 | } | 
|---|
| 2087 | if (ret) { | 
|---|
| 2088 | goto done; | 
|---|
| 2089 | } | 
|---|
| 2090 |  | 
|---|
| 2091 | ret = krb5_cc_store_cred(context, ccache, creds); | 
|---|
| 2092 | if (ret) { | 
|---|
| 2093 | goto done; | 
|---|
| 2094 | } | 
|---|
| 2095 |  | 
|---|
| 2096 | if (out_creds) { | 
|---|
| 2097 | *out_creds = creds; | 
|---|
| 2098 | } | 
|---|
| 2099 |  | 
|---|
| 2100 | done: | 
|---|
| 2101 | if (creds && ret) { | 
|---|
| 2102 | krb5_free_creds(context, creds); | 
|---|
| 2103 | } | 
|---|
| 2104 |  | 
|---|
| 2105 | return ret; | 
|---|
| 2106 | } | 
|---|
| 2107 |  | 
|---|
| 2108 | /* | 
|---|
| 2109 | * smb_krb5_get_creds | 
|---|
| 2110 | * | 
|---|
| 2111 | * @brief Get krb5 credentials for a server | 
|---|
| 2112 | * | 
|---|
| 2113 | * @param[in] server_s          The string name of the service | 
|---|
| 2114 | * @param[in] time_offset       The offset to the KDCs time in seconds (optional) | 
|---|
| 2115 | * @param[in] cc                The krb5 credential cache string name (optional) | 
|---|
| 2116 | * @param[in] impersonate_princ_s The string principal name to impersonate (optional) | 
|---|
| 2117 | * @param[out] creds_p          The returned krb5_creds structure | 
|---|
| 2118 | * @return krb5_error_code | 
|---|
| 2119 | * | 
|---|
| 2120 | */ | 
|---|
| 2121 | krb5_error_code smb_krb5_get_creds(const char *server_s, | 
|---|
| 2122 | time_t time_offset, | 
|---|
| 2123 | const char *cc, | 
|---|
| 2124 | const char *impersonate_princ_s, | 
|---|
| 2125 | krb5_creds **creds_p) | 
|---|
| 2126 | { | 
|---|
| 2127 | krb5_error_code ret; | 
|---|
| 2128 | krb5_context context = NULL; | 
|---|
| 2129 | krb5_principal me = NULL; | 
|---|
| 2130 | krb5_principal server = NULL; | 
|---|
| 2131 | krb5_principal impersonate_princ = NULL; | 
|---|
| 2132 | krb5_creds *creds = NULL; | 
|---|
| 2133 | krb5_ccache ccache = NULL; | 
|---|
| 2134 |  | 
|---|
| 2135 | *creds_p = NULL; | 
|---|
| 2136 |  | 
|---|
| 2137 | initialize_krb5_error_table(); | 
|---|
| 2138 | ret = krb5_init_context(&context); | 
|---|
| 2139 | if (ret) { | 
|---|
| 2140 | goto done; | 
|---|
| 2141 | } | 
|---|
| 2142 |  | 
|---|
| 2143 | if (time_offset != 0) { | 
|---|
| 2144 | krb5_set_real_time(context, time(NULL) + time_offset, 0); | 
|---|
| 2145 | } | 
|---|
| 2146 |  | 
|---|
| 2147 | ret = krb5_cc_resolve(context, cc ? cc : | 
|---|
| 2148 | krb5_cc_default_name(context), &ccache); | 
|---|
| 2149 | if (ret) { | 
|---|
| 2150 | goto done; | 
|---|
| 2151 | } | 
|---|
| 2152 |  | 
|---|
| 2153 | ret = krb5_cc_get_principal(context, ccache, &me); | 
|---|
| 2154 | if (ret) { | 
|---|
| 2155 | goto done; | 
|---|
| 2156 | } | 
|---|
| 2157 |  | 
|---|
| 2158 | ret = smb_krb5_parse_name(context, server_s, &server); | 
|---|
| 2159 | if (ret) { | 
|---|
| 2160 | goto done; | 
|---|
| 2161 | } | 
|---|
| 2162 |  | 
|---|
| 2163 | if (impersonate_princ_s) { | 
|---|
| 2164 | ret = smb_krb5_parse_name(context, impersonate_princ_s, | 
|---|
| 2165 | &impersonate_princ); | 
|---|
| 2166 | if (ret) { | 
|---|
| 2167 | goto done; | 
|---|
| 2168 | } | 
|---|
| 2169 | } | 
|---|
| 2170 |  | 
|---|
| 2171 | ret = smb_krb5_get_credentials(context, ccache, | 
|---|
| 2172 | me, server, impersonate_princ, | 
|---|
| 2173 | &creds); | 
|---|
| 2174 | if (ret) { | 
|---|
| 2175 | goto done; | 
|---|
| 2176 | } | 
|---|
| 2177 |  | 
|---|
| 2178 | ret = krb5_cc_store_cred(context, ccache, creds); | 
|---|
| 2179 | if (ret) { | 
|---|
| 2180 | goto done; | 
|---|
| 2181 | } | 
|---|
| 2182 |  | 
|---|
| 2183 | if (creds_p) { | 
|---|
| 2184 | *creds_p = creds; | 
|---|
| 2185 | } | 
|---|
| 2186 |  | 
|---|
| 2187 | DEBUG(1,("smb_krb5_get_creds: got ticket for %s\n", | 
|---|
| 2188 | server_s)); | 
|---|
| 2189 |  | 
|---|
| 2190 | if (impersonate_princ_s) { | 
|---|
| 2191 | char *client = NULL; | 
|---|
| 2192 |  | 
|---|
| 2193 | ret = smb_krb5_unparse_name(talloc_tos(), context, creds->client, &client); | 
|---|
| 2194 | if (ret) { | 
|---|
| 2195 | goto done; | 
|---|
| 2196 | } | 
|---|
| 2197 | DEBUGADD(1,("smb_krb5_get_creds: using S4U2SELF impersonation as %s\n", | 
|---|
| 2198 | client)); | 
|---|
| 2199 | TALLOC_FREE(client); | 
|---|
| 2200 | } | 
|---|
| 2201 |  | 
|---|
| 2202 | done: | 
|---|
| 2203 | if (!context) { | 
|---|
| 2204 | return ret; | 
|---|
| 2205 | } | 
|---|
| 2206 |  | 
|---|
| 2207 | if (creds && ret) { | 
|---|
| 2208 | krb5_free_creds(context, creds); | 
|---|
| 2209 | } | 
|---|
| 2210 | if (server) { | 
|---|
| 2211 | krb5_free_principal(context, server); | 
|---|
| 2212 | } | 
|---|
| 2213 | if (me) { | 
|---|
| 2214 | krb5_free_principal(context, me); | 
|---|
| 2215 | } | 
|---|
| 2216 | if (impersonate_princ) { | 
|---|
| 2217 | krb5_free_principal(context, impersonate_princ); | 
|---|
| 2218 | } | 
|---|
| 2219 | if (ccache) { | 
|---|
| 2220 | krb5_cc_close(context, ccache); | 
|---|
| 2221 | } | 
|---|
| 2222 | krb5_free_context(context); | 
|---|
| 2223 |  | 
|---|
| 2224 | return ret; | 
|---|
| 2225 | } | 
|---|
| 2226 |  | 
|---|
| 2227 | /* | 
|---|
| 2228 | * smb_krb5_principal_get_realm | 
|---|
| 2229 | * | 
|---|
| 2230 | * @brief Get realm of a principal | 
|---|
| 2231 | * | 
|---|
| 2232 | * @param[in] context           The krb5_context | 
|---|
| 2233 | * @param[in] principal         The principal | 
|---|
| 2234 | * @return pointer to the realm | 
|---|
| 2235 | * | 
|---|
| 2236 | */ | 
|---|
| 2237 |  | 
|---|
| 2238 | char *smb_krb5_principal_get_realm(krb5_context context, | 
|---|
| 2239 | krb5_principal principal) | 
|---|
| 2240 | { | 
|---|
| 2241 | #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */ | 
|---|
| 2242 | return krb5_principal_get_realm(context, principal); | 
|---|
| 2243 | #elif defined(krb5_princ_realm) /* MIT */ | 
|---|
| 2244 | krb5_data *realm; | 
|---|
| 2245 | realm = krb5_princ_realm(context, principal); | 
|---|
| 2246 | return (char *)realm->data; | 
|---|
| 2247 | #else | 
|---|
| 2248 | return NULL; | 
|---|
| 2249 | #endif | 
|---|
| 2250 | } | 
|---|
| 2251 |  | 
|---|
| 2252 | #else /* HAVE_KRB5 */ | 
|---|
| 2253 | /* this saves a few linking headaches */ | 
|---|
| 2254 | int cli_krb5_get_ticket(const char *principal, time_t time_offset, | 
|---|
| 2255 | DATA_BLOB *ticket, DATA_BLOB *session_key_krb5, uint32 extra_ap_opts, | 
|---|
| 2256 | const char *ccname, time_t *tgs_expire, | 
|---|
| 2257 | const char *impersonate_princ_s) | 
|---|
| 2258 | { | 
|---|
| 2259 | DEBUG(0,("NO KERBEROS SUPPORT\n")); | 
|---|
| 2260 | return 1; | 
|---|
| 2261 | } | 
|---|
| 2262 |  | 
|---|
| 2263 | #endif | 
|---|