| 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 | /* Precalculated MD5sum of NULL channel bindings (20 bytes) */
|
|---|
| 700 | /* Channel bindings are: (all ints encoded as little endian)
|
|---|
| 701 |
|
|---|
| 702 | [4 bytes] initiator_addrtype (255 for null bindings)
|
|---|
| 703 | [4 bytes] initiator_address length
|
|---|
| 704 | [n bytes] .. initiator_address data - not present
|
|---|
| 705 | in null bindings.
|
|---|
| 706 | [4 bytes] acceptor_addrtype (255 for null bindings)
|
|---|
| 707 | [4 bytes] acceptor_address length
|
|---|
| 708 | [n bytes] .. acceptor_address data - not present
|
|---|
| 709 | in null bindings.
|
|---|
| 710 | [4 bytes] application_data length
|
|---|
| 711 | [n bytes] .. application_ data - not present
|
|---|
| 712 | in null bindings.
|
|---|
| 713 | MD5 of this is ""\x14\x8f\x0c\xf7\xb1u\xdey*J\x9a%\xdfV\xc5\x18"
|
|---|
| 714 | */
|
|---|
| 715 |
|
|---|
| 716 | memcpy(&gss_cksum[4],
|
|---|
| 717 | "\x14\x8f\x0c\xf7\xb1u\xdey*J\x9a%\xdfV\xc5\x18",
|
|---|
| 718 | GSSAPI_BNDLENGTH);
|
|---|
| 719 |
|
|---|
| 720 | SIVAL(gss_cksum, 20, gss_flags);
|
|---|
| 721 |
|
|---|
| 722 | if (orig_length) {
|
|---|
| 723 | SSVAL(gss_cksum, 24, 1); /* The Delegation Option identifier */
|
|---|
| 724 | SSVAL(gss_cksum, 26, orig_length);
|
|---|
| 725 | /* Copy the kerberos KRB_CRED data */
|
|---|
| 726 | memcpy(gss_cksum + 28, in_data->data, orig_length);
|
|---|
| 727 | free(in_data->data);
|
|---|
| 728 | in_data->data = NULL;
|
|---|
| 729 | in_data->length = 0;
|
|---|
| 730 | }
|
|---|
| 731 | in_data->data = gss_cksum;
|
|---|
| 732 | in_data->length = base_cksum_size + orig_length;
|
|---|
| 733 | return 0;
|
|---|
| 734 | }
|
|---|
| 735 |
|
|---|
| 736 | /*
|
|---|
| 737 | we can't use krb5_mk_req because w2k wants the service to be in a particular format
|
|---|
| 738 | */
|
|---|
| 739 | static krb5_error_code ads_krb5_mk_req(krb5_context context,
|
|---|
| 740 | krb5_auth_context *auth_context,
|
|---|
| 741 | const krb5_flags ap_req_options,
|
|---|
| 742 | const char *principal,
|
|---|
| 743 | krb5_ccache ccache,
|
|---|
| 744 | krb5_data *outbuf,
|
|---|
| 745 | time_t *expire_time,
|
|---|
| 746 | const char *impersonate_princ_s)
|
|---|
| 747 | {
|
|---|
| 748 | krb5_error_code retval;
|
|---|
| 749 | krb5_principal server;
|
|---|
| 750 | krb5_principal impersonate_princ = NULL;
|
|---|
| 751 | krb5_creds * credsp;
|
|---|
| 752 | krb5_creds creds;
|
|---|
| 753 | krb5_data in_data;
|
|---|
| 754 | bool creds_ready = False;
|
|---|
| 755 | int i = 0, maxtries = 3;
|
|---|
| 756 | uint32_t gss_flags = 0;
|
|---|
| 757 |
|
|---|
| 758 | ZERO_STRUCT(in_data);
|
|---|
| 759 |
|
|---|
| 760 | retval = smb_krb5_parse_name(context, principal, &server);
|
|---|
| 761 | if (retval) {
|
|---|
| 762 | DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", principal));
|
|---|
| 763 | return retval;
|
|---|
| 764 | }
|
|---|
| 765 |
|
|---|
| 766 | if (impersonate_princ_s) {
|
|---|
| 767 | retval = smb_krb5_parse_name(context, impersonate_princ_s,
|
|---|
| 768 | &impersonate_princ);
|
|---|
| 769 | if (retval) {
|
|---|
| 770 | DEBUG(1,("ads_krb5_mk_req: Failed to parse principal %s\n", impersonate_princ_s));
|
|---|
| 771 | goto cleanup_princ;
|
|---|
| 772 | }
|
|---|
| 773 | }
|
|---|
| 774 |
|
|---|
| 775 | /* obtain ticket & session key */
|
|---|
| 776 | ZERO_STRUCT(creds);
|
|---|
| 777 | if ((retval = krb5_copy_principal(context, server, &creds.server))) {
|
|---|
| 778 | DEBUG(1,("ads_krb5_mk_req: krb5_copy_principal failed (%s)\n",
|
|---|
| 779 | error_message(retval)));
|
|---|
| 780 | goto cleanup_princ;
|
|---|
| 781 | }
|
|---|
| 782 |
|
|---|
| 783 | if ((retval = krb5_cc_get_principal(context, ccache, &creds.client))) {
|
|---|
| 784 | /* This can commonly fail on smbd startup with no ticket in the cache.
|
|---|
| 785 | * Report at higher level than 1. */
|
|---|
| 786 | DEBUG(3,("ads_krb5_mk_req: krb5_cc_get_principal failed (%s)\n",
|
|---|
| 787 | error_message(retval)));
|
|---|
| 788 | goto cleanup_creds;
|
|---|
| 789 | }
|
|---|
| 790 |
|
|---|
| 791 | while (!creds_ready && (i < maxtries)) {
|
|---|
| 792 |
|
|---|
| 793 | if ((retval = smb_krb5_get_credentials(context, ccache,
|
|---|
| 794 | creds.client,
|
|---|
| 795 | creds.server,
|
|---|
| 796 | impersonate_princ,
|
|---|
| 797 | &credsp))) {
|
|---|
| 798 | DEBUG(1,("ads_krb5_mk_req: smb_krb5_get_credentials failed for %s (%s)\n",
|
|---|
| 799 | principal, error_message(retval)));
|
|---|
| 800 | goto cleanup_creds;
|
|---|
| 801 | }
|
|---|
| 802 |
|
|---|
| 803 | /* cope with ticket being in the future due to clock skew */
|
|---|
| 804 | if ((unsigned)credsp->times.starttime > time(NULL)) {
|
|---|
| 805 | time_t t = time(NULL);
|
|---|
| 806 | int time_offset =(int)((unsigned)credsp->times.starttime-t);
|
|---|
| 807 | DEBUG(4,("ads_krb5_mk_req: Advancing clock by %d seconds to cope with clock skew\n", time_offset));
|
|---|
| 808 | krb5_set_real_time(context, t + time_offset + 1, 0);
|
|---|
| 809 | }
|
|---|
| 810 |
|
|---|
| 811 | if (!ads_cleanup_expired_creds(context, ccache, credsp)) {
|
|---|
| 812 | creds_ready = True;
|
|---|
| 813 | }
|
|---|
| 814 |
|
|---|
| 815 | i++;
|
|---|
| 816 | }
|
|---|
| 817 |
|
|---|
| 818 | DEBUG(10,("ads_krb5_mk_req: Ticket (%s) in ccache (%s:%s) is valid until: (%s - %u)\n",
|
|---|
| 819 | principal, krb5_cc_get_type(context, ccache), krb5_cc_get_name(context, ccache),
|
|---|
| 820 | http_timestring(talloc_tos(), (unsigned)credsp->times.endtime),
|
|---|
| 821 | (unsigned)credsp->times.endtime));
|
|---|
| 822 |
|
|---|
| 823 | if (expire_time) {
|
|---|
| 824 | *expire_time = (time_t)credsp->times.endtime;
|
|---|
| 825 | }
|
|---|
| 826 |
|
|---|
| 827 | /* Allocate the auth_context. */
|
|---|
| 828 | retval = setup_auth_context(context, auth_context);
|
|---|
| 829 | if (retval) {
|
|---|
| 830 | DEBUG(1,("setup_auth_context failed (%s)\n",
|
|---|
| 831 | error_message(retval)));
|
|---|
| 832 | goto cleanup_creds;
|
|---|
| 833 | }
|
|---|
| 834 |
|
|---|
| 835 | #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)
|
|---|
| 836 | if( credsp->ticket_flags & TKT_FLG_OK_AS_DELEGATE ) {
|
|---|
| 837 | /* Fetch a forwarded TGT from the KDC so that we can hand off a 2nd ticket
|
|---|
| 838 | as part of the kerberos exchange. */
|
|---|
| 839 |
|
|---|
| 840 | DEBUG( 3, ("ads_krb5_mk_req: server marked as OK to delegate to, building forwardable TGT\n") );
|
|---|
| 841 |
|
|---|
| 842 | retval = krb5_auth_con_setuseruserkey(context,
|
|---|
| 843 | *auth_context,
|
|---|
| 844 | &credsp->keyblock );
|
|---|
| 845 | if (retval) {
|
|---|
| 846 | DEBUG(1,("krb5_auth_con_setuseruserkey failed (%s)\n",
|
|---|
| 847 | error_message(retval)));
|
|---|
| 848 | goto cleanup_creds;
|
|---|
| 849 | }
|
|---|
| 850 |
|
|---|
| 851 | /* Must use a subkey for forwarded tickets. */
|
|---|
| 852 | retval = krb5_auth_con_setflags(context,
|
|---|
| 853 | *auth_context,
|
|---|
| 854 | KRB5_AUTH_CONTEXT_USE_SUBKEY);
|
|---|
| 855 | if (retval) {
|
|---|
| 856 | DEBUG(1,("krb5_auth_con_setflags failed (%s)\n",
|
|---|
| 857 | error_message(retval)));
|
|---|
| 858 | goto cleanup_creds;
|
|---|
| 859 | }
|
|---|
| 860 |
|
|---|
| 861 | retval = krb5_fwd_tgt_creds(context,/* Krb5 context [in] */
|
|---|
| 862 | *auth_context, /* Authentication context [in] */
|
|---|
| 863 | CONST_DISCARD(char *, KRB5_TGS_NAME), /* Ticket service name ("krbtgt") [in] */
|
|---|
| 864 | credsp->client, /* Client principal for the tgt [in] */
|
|---|
| 865 | credsp->server, /* Server principal for the tgt [in] */
|
|---|
| 866 | ccache, /* Credential cache to use for storage [in] */
|
|---|
| 867 | 1, /* Turn on for "Forwardable ticket" [in] */
|
|---|
| 868 | &in_data ); /* Resulting response [out] */
|
|---|
| 869 |
|
|---|
| 870 | if (retval) {
|
|---|
| 871 | DEBUG( 3, ("krb5_fwd_tgt_creds failed (%s)\n",
|
|---|
| 872 | error_message( retval ) ) );
|
|---|
| 873 |
|
|---|
| 874 | /*
|
|---|
| 875 | * This is not fatal. Delete the *auth_context and continue
|
|---|
| 876 | * with krb5_mk_req_extended to get a non-forwardable ticket.
|
|---|
| 877 | */
|
|---|
| 878 |
|
|---|
| 879 | if (in_data.data) {
|
|---|
| 880 | free( in_data.data );
|
|---|
| 881 | in_data.data = NULL;
|
|---|
| 882 | in_data.length = 0;
|
|---|
| 883 | }
|
|---|
| 884 | krb5_auth_con_free(context, *auth_context);
|
|---|
| 885 | *auth_context = NULL;
|
|---|
| 886 | retval = setup_auth_context(context, auth_context);
|
|---|
| 887 | if (retval) {
|
|---|
| 888 | DEBUG(1,("setup_auth_context failed (%s)\n",
|
|---|
| 889 | error_message(retval)));
|
|---|
| 890 | goto cleanup_creds;
|
|---|
| 891 | }
|
|---|
| 892 | } else {
|
|---|
| 893 | /* We got a delegated ticket. */
|
|---|
| 894 | gss_flags |= GSS_C_DELEG_FLAG;
|
|---|
| 895 | }
|
|---|
| 896 | }
|
|---|
| 897 | #endif
|
|---|
| 898 |
|
|---|
| 899 | /* Frees and reallocates in_data into a GSS checksum blob. */
|
|---|
| 900 | retval = create_gss_checksum(&in_data, gss_flags);
|
|---|
| 901 | if (retval) {
|
|---|
| 902 | goto cleanup_data;
|
|---|
| 903 | }
|
|---|
| 904 |
|
|---|
| 905 | #if defined(HAVE_KRB5_AUTH_CON_SET_REQ_CKSUMTYPE)
|
|---|
| 906 | /* We always want GSS-checksum types. */
|
|---|
| 907 | retval = krb5_auth_con_set_req_cksumtype(context, *auth_context, GSSAPI_CHECKSUM );
|
|---|
| 908 | if (retval) {
|
|---|
| 909 | DEBUG(1,("krb5_auth_con_set_req_cksumtype failed (%s)\n",
|
|---|
| 910 | error_message(retval)));
|
|---|
| 911 | goto cleanup_data;
|
|---|
| 912 | }
|
|---|
| 913 | #endif
|
|---|
| 914 |
|
|---|
| 915 | retval = krb5_mk_req_extended(context, auth_context, ap_req_options,
|
|---|
| 916 | &in_data, credsp, outbuf);
|
|---|
| 917 | if (retval) {
|
|---|
| 918 | DEBUG(1,("ads_krb5_mk_req: krb5_mk_req_extended failed (%s)\n",
|
|---|
| 919 | error_message(retval)));
|
|---|
| 920 | }
|
|---|
| 921 |
|
|---|
| 922 | cleanup_data:
|
|---|
| 923 | if (in_data.data) {
|
|---|
| 924 | free( in_data.data );
|
|---|
| 925 | in_data.length = 0;
|
|---|
| 926 | }
|
|---|
| 927 |
|
|---|
| 928 | krb5_free_creds(context, credsp);
|
|---|
| 929 |
|
|---|
| 930 | cleanup_creds:
|
|---|
| 931 | krb5_free_cred_contents(context, &creds);
|
|---|
| 932 |
|
|---|
| 933 | cleanup_princ:
|
|---|
| 934 | krb5_free_principal(context, server);
|
|---|
| 935 | if (impersonate_princ) {
|
|---|
| 936 | krb5_free_principal(context, impersonate_princ);
|
|---|
| 937 | }
|
|---|
| 938 |
|
|---|
| 939 | return retval;
|
|---|
| 940 | }
|
|---|
| 941 |
|
|---|
| 942 | /*
|
|---|
| 943 | get a kerberos5 ticket for the given service
|
|---|
| 944 | */
|
|---|
| 945 | int cli_krb5_get_ticket(const char *principal, time_t time_offset,
|
|---|
| 946 | DATA_BLOB *ticket, DATA_BLOB *session_key_krb5,
|
|---|
| 947 | uint32 extra_ap_opts, const char *ccname,
|
|---|
| 948 | time_t *tgs_expire,
|
|---|
| 949 | const char *impersonate_princ_s)
|
|---|
| 950 |
|
|---|
| 951 | {
|
|---|
| 952 | krb5_error_code retval;
|
|---|
| 953 | krb5_data packet;
|
|---|
| 954 | krb5_context context = NULL;
|
|---|
| 955 | krb5_ccache ccdef = NULL;
|
|---|
| 956 | krb5_auth_context auth_context = NULL;
|
|---|
| 957 | krb5_enctype enc_types[] = {
|
|---|
| 958 | #ifdef ENCTYPE_ARCFOUR_HMAC
|
|---|
| 959 | ENCTYPE_ARCFOUR_HMAC,
|
|---|
| 960 | #endif
|
|---|
| 961 | ENCTYPE_DES_CBC_MD5,
|
|---|
| 962 | ENCTYPE_DES_CBC_CRC,
|
|---|
| 963 | ENCTYPE_NULL};
|
|---|
| 964 |
|
|---|
| 965 | initialize_krb5_error_table();
|
|---|
| 966 | retval = krb5_init_context(&context);
|
|---|
| 967 | if (retval) {
|
|---|
| 968 | DEBUG(1,("cli_krb5_get_ticket: krb5_init_context failed (%s)\n",
|
|---|
| 969 | error_message(retval)));
|
|---|
| 970 | goto failed;
|
|---|
| 971 | }
|
|---|
| 972 |
|
|---|
| 973 | if (time_offset != 0) {
|
|---|
| 974 | krb5_set_real_time(context, time(NULL) + time_offset, 0);
|
|---|
| 975 | }
|
|---|
| 976 |
|
|---|
| 977 | if ((retval = krb5_cc_resolve(context, ccname ?
|
|---|
| 978 | ccname : krb5_cc_default_name(context), &ccdef))) {
|
|---|
| 979 | DEBUG(1,("cli_krb5_get_ticket: krb5_cc_default failed (%s)\n",
|
|---|
| 980 | error_message(retval)));
|
|---|
| 981 | goto failed;
|
|---|
| 982 | }
|
|---|
| 983 |
|
|---|
| 984 | if ((retval = krb5_set_default_tgs_ktypes(context, enc_types))) {
|
|---|
| 985 | DEBUG(1,("cli_krb5_get_ticket: krb5_set_default_tgs_ktypes failed (%s)\n",
|
|---|
| 986 | error_message(retval)));
|
|---|
| 987 | goto failed;
|
|---|
| 988 | }
|
|---|
| 989 |
|
|---|
| 990 | if ((retval = ads_krb5_mk_req(context,
|
|---|
| 991 | &auth_context,
|
|---|
| 992 | AP_OPTS_USE_SUBKEY | (krb5_flags)extra_ap_opts,
|
|---|
| 993 | principal,
|
|---|
| 994 | ccdef, &packet,
|
|---|
| 995 | tgs_expire,
|
|---|
| 996 | impersonate_princ_s))) {
|
|---|
| 997 | goto failed;
|
|---|
| 998 | }
|
|---|
| 999 |
|
|---|
| 1000 | get_krb5_smb_session_key(context, auth_context, session_key_krb5, False);
|
|---|
| 1001 |
|
|---|
| 1002 | *ticket = data_blob(packet.data, packet.length);
|
|---|
| 1003 |
|
|---|
| 1004 | kerberos_free_data_contents(context, &packet);
|
|---|
| 1005 |
|
|---|
| 1006 | failed:
|
|---|
| 1007 |
|
|---|
| 1008 | if ( context ) {
|
|---|
| 1009 | if (ccdef)
|
|---|
| 1010 | krb5_cc_close(context, ccdef);
|
|---|
| 1011 | if (auth_context)
|
|---|
| 1012 | krb5_auth_con_free(context, auth_context);
|
|---|
| 1013 | krb5_free_context(context);
|
|---|
| 1014 | }
|
|---|
| 1015 |
|
|---|
| 1016 | return retval;
|
|---|
| 1017 | }
|
|---|
| 1018 |
|
|---|
| 1019 | bool get_krb5_smb_session_key(krb5_context context, krb5_auth_context auth_context, DATA_BLOB *session_key, bool remote)
|
|---|
| 1020 | {
|
|---|
| 1021 | krb5_keyblock *skey = NULL;
|
|---|
| 1022 | krb5_error_code err = 0;
|
|---|
| 1023 | bool ret = false;
|
|---|
| 1024 |
|
|---|
| 1025 | if (remote) {
|
|---|
| 1026 | err = krb5_auth_con_getremotesubkey(context, auth_context, &skey);
|
|---|
| 1027 | } else {
|
|---|
| 1028 | err = krb5_auth_con_getlocalsubkey(context, auth_context, &skey);
|
|---|
| 1029 | }
|
|---|
| 1030 |
|
|---|
| 1031 | if (err || skey == NULL) {
|
|---|
| 1032 | DEBUG(10, ("KRB5 error getting session key %d\n", err));
|
|---|
| 1033 | goto done;
|
|---|
| 1034 | }
|
|---|
| 1035 |
|
|---|
| 1036 | DEBUG(10, ("Got KRB5 session key of length %d\n", (int)KRB5_KEY_LENGTH(skey)));
|
|---|
| 1037 | *session_key = data_blob(KRB5_KEY_DATA(skey), KRB5_KEY_LENGTH(skey));
|
|---|
| 1038 | dump_data_pw("KRB5 Session Key:\n", session_key->data, session_key->length);
|
|---|
| 1039 |
|
|---|
| 1040 | ret = true;
|
|---|
| 1041 |
|
|---|
| 1042 | done:
|
|---|
| 1043 | if (skey) {
|
|---|
| 1044 | krb5_free_keyblock(context, skey);
|
|---|
| 1045 | }
|
|---|
| 1046 |
|
|---|
| 1047 | return ret;
|
|---|
| 1048 | }
|
|---|
| 1049 |
|
|---|
| 1050 |
|
|---|
| 1051 | #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING) && !defined(HAVE_KRB5_PRINC_COMPONENT)
|
|---|
| 1052 | const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i );
|
|---|
| 1053 |
|
|---|
| 1054 | const krb5_data *krb5_princ_component(krb5_context context, krb5_principal principal, int i )
|
|---|
| 1055 | {
|
|---|
| 1056 | static krb5_data kdata;
|
|---|
| 1057 |
|
|---|
| 1058 | kdata.data = (char *)krb5_principal_get_comp_string(context, principal, i);
|
|---|
| 1059 | kdata.length = strlen((const char *)kdata.data);
|
|---|
| 1060 | return &kdata;
|
|---|
| 1061 | }
|
|---|
| 1062 | #endif
|
|---|
| 1063 |
|
|---|
| 1064 | krb5_error_code smb_krb5_kt_free_entry(krb5_context context, krb5_keytab_entry *kt_entry)
|
|---|
| 1065 | {
|
|---|
| 1066 | /* Try krb5_free_keytab_entry_contents first, since
|
|---|
| 1067 | * MIT Kerberos >= 1.7 has both krb5_free_keytab_entry_contents and
|
|---|
| 1068 | * krb5_kt_free_entry but only has a prototype for the first, while the
|
|---|
| 1069 | * second is considered private.
|
|---|
| 1070 | */
|
|---|
| 1071 | #if defined(HAVE_KRB5_FREE_KEYTAB_ENTRY_CONTENTS)
|
|---|
| 1072 | return krb5_free_keytab_entry_contents(context, kt_entry);
|
|---|
| 1073 | #elif defined(HAVE_KRB5_KT_FREE_ENTRY)
|
|---|
| 1074 | return krb5_kt_free_entry(context, kt_entry);
|
|---|
| 1075 | #else
|
|---|
| 1076 | #error UNKNOWN_KT_FREE_FUNCTION
|
|---|
| 1077 | #endif
|
|---|
| 1078 | }
|
|---|
| 1079 |
|
|---|
| 1080 | void smb_krb5_checksum_from_pac_sig(krb5_checksum *cksum,
|
|---|
| 1081 | struct PAC_SIGNATURE_DATA *sig)
|
|---|
| 1082 | {
|
|---|
| 1083 | #ifdef HAVE_CHECKSUM_IN_KRB5_CHECKSUM
|
|---|
| 1084 | cksum->cksumtype = (krb5_cksumtype)sig->type;
|
|---|
| 1085 | cksum->checksum.length = sig->signature.length;
|
|---|
| 1086 | cksum->checksum.data = sig->signature.data;
|
|---|
| 1087 | #else
|
|---|
| 1088 | cksum->checksum_type = (krb5_cksumtype)sig->type;
|
|---|
| 1089 | cksum->length = sig->signature.length;
|
|---|
| 1090 | cksum->contents = sig->signature.data;
|
|---|
| 1091 | #endif
|
|---|
| 1092 | }
|
|---|
| 1093 |
|
|---|
| 1094 | krb5_error_code smb_krb5_verify_checksum(krb5_context context,
|
|---|
| 1095 | const krb5_keyblock *keyblock,
|
|---|
| 1096 | krb5_keyusage usage,
|
|---|
| 1097 | krb5_checksum *cksum,
|
|---|
| 1098 | uint8 *data,
|
|---|
| 1099 | size_t length)
|
|---|
| 1100 | {
|
|---|
| 1101 | krb5_error_code ret;
|
|---|
| 1102 |
|
|---|
| 1103 | /* verify the checksum */
|
|---|
| 1104 |
|
|---|
| 1105 | /* welcome to the wonderful world of samba's kerberos abstraction layer:
|
|---|
| 1106 | *
|
|---|
| 1107 | * function heimdal 0.6.1rc3 heimdal 0.7 MIT krb 1.4.2
|
|---|
| 1108 | * -----------------------------------------------------------------------------
|
|---|
| 1109 | * krb5_c_verify_checksum - works works
|
|---|
| 1110 | * krb5_verify_checksum works (6 args) works (6 args) broken (7 args)
|
|---|
| 1111 | */
|
|---|
| 1112 |
|
|---|
| 1113 | #if defined(HAVE_KRB5_C_VERIFY_CHECKSUM)
|
|---|
| 1114 | {
|
|---|
| 1115 | krb5_boolean checksum_valid = False;
|
|---|
| 1116 | krb5_data input;
|
|---|
| 1117 |
|
|---|
| 1118 | input.data = (char *)data;
|
|---|
| 1119 | input.length = length;
|
|---|
| 1120 |
|
|---|
| 1121 | ret = krb5_c_verify_checksum(context,
|
|---|
| 1122 | keyblock,
|
|---|
| 1123 | usage,
|
|---|
| 1124 | &input,
|
|---|
| 1125 | cksum,
|
|---|
| 1126 | &checksum_valid);
|
|---|
| 1127 | if (ret) {
|
|---|
| 1128 | DEBUG(3,("smb_krb5_verify_checksum: krb5_c_verify_checksum() failed: %s\n",
|
|---|
| 1129 | error_message(ret)));
|
|---|
| 1130 | return ret;
|
|---|
| 1131 | }
|
|---|
| 1132 |
|
|---|
| 1133 | if (!checksum_valid)
|
|---|
| 1134 | ret = KRB5KRB_AP_ERR_BAD_INTEGRITY;
|
|---|
| 1135 | }
|
|---|
| 1136 |
|
|---|
| 1137 | #elif KRB5_VERIFY_CHECKSUM_ARGS == 6 && defined(HAVE_KRB5_CRYPTO_INIT) && defined(HAVE_KRB5_CRYPTO) && defined(HAVE_KRB5_CRYPTO_DESTROY)
|
|---|
| 1138 |
|
|---|
| 1139 | /* Warning: MIT's krb5_verify_checksum cannot be used as it will use a key
|
|---|
| 1140 | * without enctype and it ignores any key_usage types - Guenther */
|
|---|
| 1141 |
|
|---|
| 1142 | {
|
|---|
| 1143 |
|
|---|
| 1144 | krb5_crypto crypto;
|
|---|
| 1145 | ret = krb5_crypto_init(context,
|
|---|
| 1146 | keyblock,
|
|---|
| 1147 | 0,
|
|---|
| 1148 | &crypto);
|
|---|
| 1149 | if (ret) {
|
|---|
| 1150 | DEBUG(0,("smb_krb5_verify_checksum: krb5_crypto_init() failed: %s\n",
|
|---|
| 1151 | error_message(ret)));
|
|---|
| 1152 | return ret;
|
|---|
| 1153 | }
|
|---|
| 1154 |
|
|---|
| 1155 | ret = krb5_verify_checksum(context,
|
|---|
| 1156 | crypto,
|
|---|
| 1157 | usage,
|
|---|
| 1158 | data,
|
|---|
| 1159 | length,
|
|---|
| 1160 | cksum);
|
|---|
| 1161 |
|
|---|
| 1162 | krb5_crypto_destroy(context, crypto);
|
|---|
| 1163 | }
|
|---|
| 1164 |
|
|---|
| 1165 | #else
|
|---|
| 1166 | #error UNKNOWN_KRB5_VERIFY_CHECKSUM_FUNCTION
|
|---|
| 1167 | #endif
|
|---|
| 1168 |
|
|---|
| 1169 | return ret;
|
|---|
| 1170 | }
|
|---|
| 1171 |
|
|---|
| 1172 | time_t get_authtime_from_tkt(krb5_ticket *tkt)
|
|---|
| 1173 | {
|
|---|
| 1174 | #if defined(HAVE_KRB5_TKT_ENC_PART2)
|
|---|
| 1175 | return tkt->enc_part2->times.authtime;
|
|---|
| 1176 | #else
|
|---|
| 1177 | return tkt->ticket.authtime;
|
|---|
| 1178 | #endif
|
|---|
| 1179 | }
|
|---|
| 1180 |
|
|---|
| 1181 | #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
|
|---|
| 1182 | static int get_kvno_from_ap_req(krb5_ap_req *ap_req)
|
|---|
| 1183 | {
|
|---|
| 1184 | #ifdef HAVE_TICKET_POINTER_IN_KRB5_AP_REQ /* MIT */
|
|---|
| 1185 | if (ap_req->ticket->enc_part.kvno)
|
|---|
| 1186 | return ap_req->ticket->enc_part.kvno;
|
|---|
| 1187 | #else /* Heimdal */
|
|---|
| 1188 | if (ap_req->ticket.enc_part.kvno)
|
|---|
| 1189 | return *ap_req->ticket.enc_part.kvno;
|
|---|
| 1190 | #endif
|
|---|
| 1191 | return 0;
|
|---|
| 1192 | }
|
|---|
| 1193 |
|
|---|
| 1194 | static krb5_enctype get_enctype_from_ap_req(krb5_ap_req *ap_req)
|
|---|
| 1195 | {
|
|---|
| 1196 | #ifdef HAVE_ETYPE_IN_ENCRYPTEDDATA /* Heimdal */
|
|---|
| 1197 | return ap_req->ticket.enc_part.etype;
|
|---|
| 1198 | #else /* MIT */
|
|---|
| 1199 | return ap_req->ticket->enc_part.enctype;
|
|---|
| 1200 | #endif
|
|---|
| 1201 | }
|
|---|
| 1202 | #endif /* HAVE_KRB5_DECODE_AP_REQ */
|
|---|
| 1203 |
|
|---|
| 1204 | static krb5_error_code
|
|---|
| 1205 | get_key_from_keytab(krb5_context context,
|
|---|
| 1206 | krb5_const_principal server,
|
|---|
| 1207 | krb5_enctype enctype,
|
|---|
| 1208 | krb5_kvno kvno,
|
|---|
| 1209 | krb5_keyblock **out_key)
|
|---|
| 1210 | {
|
|---|
| 1211 | krb5_keytab_entry entry;
|
|---|
| 1212 | krb5_error_code ret;
|
|---|
| 1213 | krb5_keytab keytab;
|
|---|
| 1214 | char *name = NULL;
|
|---|
| 1215 | krb5_keyblock *keyp;
|
|---|
| 1216 |
|
|---|
| 1217 | /* We have to open a new keytab handle here, as MIT does
|
|---|
| 1218 | an implicit open/getnext/close on krb5_kt_get_entry. We
|
|---|
| 1219 | may be in the middle of a keytab enumeration when this is
|
|---|
| 1220 | called. JRA. */
|
|---|
| 1221 |
|
|---|
| 1222 | ret = smb_krb5_open_keytab(context, NULL, False, &keytab);
|
|---|
| 1223 | if (ret) {
|
|---|
| 1224 | DEBUG(1,("get_key_from_keytab: smb_krb5_open_keytab failed (%s)\n", error_message(ret)));
|
|---|
| 1225 | return ret;
|
|---|
| 1226 | }
|
|---|
| 1227 |
|
|---|
| 1228 | if ( DEBUGLEVEL >= 10 ) {
|
|---|
| 1229 | if (smb_krb5_unparse_name(talloc_tos(), context, server, &name) == 0) {
|
|---|
| 1230 | DEBUG(10,("get_key_from_keytab: will look for kvno %d, enctype %d and name: %s\n",
|
|---|
| 1231 | kvno, enctype, name));
|
|---|
| 1232 | TALLOC_FREE(name);
|
|---|
| 1233 | }
|
|---|
| 1234 | }
|
|---|
| 1235 |
|
|---|
| 1236 | ret = krb5_kt_get_entry(context,
|
|---|
| 1237 | keytab,
|
|---|
| 1238 | server,
|
|---|
| 1239 | kvno,
|
|---|
| 1240 | enctype,
|
|---|
| 1241 | &entry);
|
|---|
| 1242 |
|
|---|
| 1243 | if (ret) {
|
|---|
| 1244 | DEBUG(0,("get_key_from_keytab: failed to retrieve key: %s\n", error_message(ret)));
|
|---|
| 1245 | goto out;
|
|---|
| 1246 | }
|
|---|
| 1247 |
|
|---|
| 1248 | keyp = KRB5_KT_KEY(&entry);
|
|---|
| 1249 |
|
|---|
| 1250 | ret = krb5_copy_keyblock(context, keyp, out_key);
|
|---|
| 1251 | if (ret) {
|
|---|
| 1252 | DEBUG(0,("get_key_from_keytab: failed to copy key: %s\n", error_message(ret)));
|
|---|
| 1253 | goto out;
|
|---|
| 1254 | }
|
|---|
| 1255 |
|
|---|
| 1256 | smb_krb5_kt_free_entry(context, &entry);
|
|---|
| 1257 |
|
|---|
| 1258 | out:
|
|---|
| 1259 | krb5_kt_close(context, keytab);
|
|---|
| 1260 | return ret;
|
|---|
| 1261 | }
|
|---|
| 1262 |
|
|---|
| 1263 | /* Prototypes */
|
|---|
| 1264 |
|
|---|
| 1265 | krb5_error_code smb_krb5_get_keyinfo_from_ap_req(krb5_context context,
|
|---|
| 1266 | const krb5_data *inbuf,
|
|---|
| 1267 | krb5_kvno *kvno,
|
|---|
| 1268 | krb5_enctype *enctype)
|
|---|
| 1269 | {
|
|---|
| 1270 | #ifdef HAVE_KRB5_DECODE_AP_REQ /* Heimdal */
|
|---|
| 1271 | {
|
|---|
| 1272 | krb5_error_code ret;
|
|---|
| 1273 | krb5_ap_req ap_req;
|
|---|
| 1274 |
|
|---|
| 1275 | ret = krb5_decode_ap_req(context, inbuf, &ap_req);
|
|---|
| 1276 | if (ret)
|
|---|
| 1277 | return ret;
|
|---|
| 1278 |
|
|---|
| 1279 | *kvno = get_kvno_from_ap_req(&ap_req);
|
|---|
| 1280 | *enctype = get_enctype_from_ap_req(&ap_req);
|
|---|
| 1281 |
|
|---|
| 1282 | free_AP_REQ(&ap_req);
|
|---|
| 1283 | return 0;
|
|---|
| 1284 | }
|
|---|
| 1285 | #endif
|
|---|
| 1286 |
|
|---|
| 1287 | /* Possibly not an appropriate error code. */
|
|---|
| 1288 | return KRB5KDC_ERR_BADOPTION;
|
|---|
| 1289 | }
|
|---|
| 1290 |
|
|---|
| 1291 | krb5_error_code krb5_rd_req_return_keyblock_from_keytab(krb5_context context,
|
|---|
| 1292 | krb5_auth_context *auth_context,
|
|---|
| 1293 | const krb5_data *inbuf,
|
|---|
| 1294 | krb5_const_principal server,
|
|---|
| 1295 | krb5_keytab keytab,
|
|---|
| 1296 | krb5_flags *ap_req_options,
|
|---|
| 1297 | krb5_ticket **ticket,
|
|---|
| 1298 | krb5_keyblock **keyblock)
|
|---|
| 1299 | {
|
|---|
| 1300 | krb5_error_code ret;
|
|---|
| 1301 | krb5_kvno kvno;
|
|---|
| 1302 | krb5_enctype enctype;
|
|---|
| 1303 | krb5_keyblock *local_keyblock;
|
|---|
| 1304 |
|
|---|
| 1305 | ret = krb5_rd_req(context,
|
|---|
| 1306 | auth_context,
|
|---|
| 1307 | inbuf,
|
|---|
| 1308 | server,
|
|---|
| 1309 | keytab,
|
|---|
| 1310 | ap_req_options,
|
|---|
| 1311 | ticket);
|
|---|
| 1312 | if (ret) {
|
|---|
| 1313 | return ret;
|
|---|
| 1314 | }
|
|---|
| 1315 |
|
|---|
| 1316 | #ifdef KRB5_TICKET_HAS_KEYINFO
|
|---|
| 1317 | enctype = (*ticket)->enc_part.enctype;
|
|---|
| 1318 | kvno = (*ticket)->enc_part.kvno;
|
|---|
| 1319 | #else
|
|---|
| 1320 | ret = smb_krb5_get_keyinfo_from_ap_req(context, inbuf, &kvno, &enctype);
|
|---|
| 1321 | if (ret) {
|
|---|
| 1322 | return ret;
|
|---|
| 1323 | }
|
|---|
| 1324 | #endif
|
|---|
| 1325 |
|
|---|
| 1326 | ret = get_key_from_keytab(context,
|
|---|
| 1327 | server,
|
|---|
| 1328 | enctype,
|
|---|
| 1329 | kvno,
|
|---|
| 1330 | &local_keyblock);
|
|---|
| 1331 | if (ret) {
|
|---|
| 1332 | DEBUG(0,("krb5_rd_req_return_keyblock_from_keytab: failed to call get_key_from_keytab\n"));
|
|---|
| 1333 | goto out;
|
|---|
| 1334 | }
|
|---|
| 1335 |
|
|---|
| 1336 | out:
|
|---|
| 1337 | if (ret && local_keyblock != NULL) {
|
|---|
| 1338 | krb5_free_keyblock(context, local_keyblock);
|
|---|
| 1339 | } else {
|
|---|
| 1340 | *keyblock = local_keyblock;
|
|---|
| 1341 | }
|
|---|
| 1342 |
|
|---|
| 1343 | return ret;
|
|---|
| 1344 | }
|
|---|
| 1345 |
|
|---|
| 1346 | krb5_error_code smb_krb5_parse_name_norealm(krb5_context context,
|
|---|
| 1347 | const char *name,
|
|---|
| 1348 | krb5_principal *principal)
|
|---|
| 1349 | {
|
|---|
| 1350 | #ifdef HAVE_KRB5_PARSE_NAME_NOREALM
|
|---|
| 1351 | return smb_krb5_parse_name_norealm_conv(context, name, principal);
|
|---|
| 1352 | #endif
|
|---|
| 1353 |
|
|---|
| 1354 | /* we are cheating here because parse_name will in fact set the realm.
|
|---|
| 1355 | * We don't care as the only caller of smb_krb5_parse_name_norealm
|
|---|
| 1356 | * ignores the realm anyway when calling
|
|---|
| 1357 | * smb_krb5_principal_compare_any_realm later - Guenther */
|
|---|
| 1358 |
|
|---|
| 1359 | return smb_krb5_parse_name(context, name, principal);
|
|---|
| 1360 | }
|
|---|
| 1361 |
|
|---|
| 1362 | bool smb_krb5_principal_compare_any_realm(krb5_context context,
|
|---|
| 1363 | krb5_const_principal princ1,
|
|---|
| 1364 | krb5_const_principal princ2)
|
|---|
| 1365 | {
|
|---|
| 1366 | #ifdef HAVE_KRB5_PRINCIPAL_COMPARE_ANY_REALM
|
|---|
| 1367 |
|
|---|
| 1368 | return krb5_principal_compare_any_realm(context, princ1, princ2);
|
|---|
| 1369 |
|
|---|
| 1370 | /* krb5_princ_size is a macro in MIT */
|
|---|
| 1371 | #elif defined(HAVE_KRB5_PRINC_SIZE) || defined(krb5_princ_size)
|
|---|
| 1372 |
|
|---|
| 1373 | int i, len1, len2;
|
|---|
| 1374 | const krb5_data *p1, *p2;
|
|---|
| 1375 |
|
|---|
| 1376 | len1 = krb5_princ_size(context, princ1);
|
|---|
| 1377 | len2 = krb5_princ_size(context, princ2);
|
|---|
| 1378 |
|
|---|
| 1379 | if (len1 != len2)
|
|---|
| 1380 | return False;
|
|---|
| 1381 |
|
|---|
| 1382 | for (i = 0; i < len1; i++) {
|
|---|
| 1383 |
|
|---|
| 1384 | p1 = krb5_princ_component(context, CONST_DISCARD(krb5_principal, princ1), i);
|
|---|
| 1385 | p2 = krb5_princ_component(context, CONST_DISCARD(krb5_principal, princ2), i);
|
|---|
| 1386 |
|
|---|
| 1387 | if (p1->length != p2->length || memcmp(p1->data, p2->data, p1->length))
|
|---|
| 1388 | return False;
|
|---|
| 1389 | }
|
|---|
| 1390 |
|
|---|
| 1391 | return True;
|
|---|
| 1392 | #else
|
|---|
| 1393 | #error NO_SUITABLE_PRINCIPAL_COMPARE_FUNCTION
|
|---|
| 1394 | #endif
|
|---|
| 1395 | }
|
|---|
| 1396 |
|
|---|
| 1397 | krb5_error_code smb_krb5_renew_ticket(const char *ccache_string, /* FILE:/tmp/krb5cc_0 */
|
|---|
| 1398 | const char *client_string, /* gd@BER.SUSE.DE */
|
|---|
| 1399 | const char *service_string, /* krbtgt/BER.SUSE.DE@BER.SUSE.DE */
|
|---|
| 1400 | time_t *expire_time)
|
|---|
| 1401 | {
|
|---|
| 1402 | krb5_error_code ret;
|
|---|
| 1403 | krb5_context context = NULL;
|
|---|
| 1404 | krb5_ccache ccache = NULL;
|
|---|
| 1405 | krb5_principal client = NULL;
|
|---|
| 1406 | krb5_creds creds, creds_in, *creds_out = NULL;
|
|---|
| 1407 |
|
|---|
| 1408 | ZERO_STRUCT(creds);
|
|---|
| 1409 | ZERO_STRUCT(creds_in);
|
|---|
| 1410 |
|
|---|
| 1411 | initialize_krb5_error_table();
|
|---|
| 1412 | ret = krb5_init_context(&context);
|
|---|
| 1413 | if (ret) {
|
|---|
| 1414 | goto done;
|
|---|
| 1415 | }
|
|---|
| 1416 |
|
|---|
| 1417 | if (!ccache_string) {
|
|---|
| 1418 | ccache_string = krb5_cc_default_name(context);
|
|---|
| 1419 | }
|
|---|
| 1420 |
|
|---|
| 1421 | if (!ccache_string) {
|
|---|
| 1422 | ret = EINVAL;
|
|---|
| 1423 | goto done;
|
|---|
| 1424 | }
|
|---|
| 1425 |
|
|---|
| 1426 | DEBUG(10,("smb_krb5_renew_ticket: using %s as ccache\n", ccache_string));
|
|---|
| 1427 |
|
|---|
| 1428 | /* FIXME: we should not fall back to defaults */
|
|---|
| 1429 | ret = krb5_cc_resolve(context, CONST_DISCARD(char *, ccache_string), &ccache);
|
|---|
| 1430 | if (ret) {
|
|---|
| 1431 | goto done;
|
|---|
| 1432 | }
|
|---|
| 1433 |
|
|---|
| 1434 | if (client_string) {
|
|---|
| 1435 | ret = smb_krb5_parse_name(context, client_string, &client);
|
|---|
| 1436 | if (ret) {
|
|---|
| 1437 | goto done;
|
|---|
| 1438 | }
|
|---|
| 1439 | } else {
|
|---|
| 1440 | ret = krb5_cc_get_principal(context, ccache, &client);
|
|---|
| 1441 | if (ret) {
|
|---|
| 1442 | goto done;
|
|---|
| 1443 | }
|
|---|
| 1444 | }
|
|---|
| 1445 |
|
|---|
| 1446 | #ifdef HAVE_KRB5_GET_RENEWED_CREDS /* MIT */
|
|---|
| 1447 | {
|
|---|
| 1448 | ret = krb5_get_renewed_creds(context, &creds, client, ccache, CONST_DISCARD(char *, service_string));
|
|---|
| 1449 | if (ret) {
|
|---|
| 1450 | DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
|
|---|
| 1451 | goto done;
|
|---|
| 1452 | }
|
|---|
| 1453 | }
|
|---|
| 1454 | #elif defined(HAVE_KRB5_GET_KDC_CRED) /* Heimdal */
|
|---|
| 1455 | {
|
|---|
| 1456 | krb5_kdc_flags flags;
|
|---|
| 1457 | krb5_realm *client_realm = NULL;
|
|---|
| 1458 |
|
|---|
| 1459 | ret = krb5_copy_principal(context, client, &creds_in.client);
|
|---|
| 1460 | if (ret) {
|
|---|
| 1461 | goto done;
|
|---|
| 1462 | }
|
|---|
| 1463 |
|
|---|
| 1464 | if (service_string) {
|
|---|
| 1465 | ret = smb_krb5_parse_name(context, service_string, &creds_in.server);
|
|---|
| 1466 | if (ret) {
|
|---|
| 1467 | goto done;
|
|---|
| 1468 | }
|
|---|
| 1469 | } else {
|
|---|
| 1470 | /* build tgt service by default */
|
|---|
| 1471 | client_realm = krb5_princ_realm(context, creds_in.client);
|
|---|
| 1472 | if (!client_realm) {
|
|---|
| 1473 | ret = ENOMEM;
|
|---|
| 1474 | goto done;
|
|---|
| 1475 | }
|
|---|
| 1476 | ret = krb5_make_principal(context, &creds_in.server, *client_realm, KRB5_TGS_NAME, *client_realm, NULL);
|
|---|
| 1477 | if (ret) {
|
|---|
| 1478 | goto done;
|
|---|
| 1479 | }
|
|---|
| 1480 | }
|
|---|
| 1481 |
|
|---|
| 1482 | flags.i = 0;
|
|---|
| 1483 | flags.b.renewable = flags.b.renew = True;
|
|---|
| 1484 |
|
|---|
| 1485 | ret = krb5_get_kdc_cred(context, ccache, flags, NULL, NULL, &creds_in, &creds_out);
|
|---|
| 1486 | if (ret) {
|
|---|
| 1487 | DEBUG(10,("smb_krb5_renew_ticket: krb5_get_kdc_cred failed: %s\n", error_message(ret)));
|
|---|
| 1488 | goto done;
|
|---|
| 1489 | }
|
|---|
| 1490 |
|
|---|
| 1491 | creds = *creds_out;
|
|---|
| 1492 | }
|
|---|
| 1493 | #else
|
|---|
| 1494 | #error NO_SUITABLE_KRB5_TICKET_RENEW_FUNCTION_AVAILABLE
|
|---|
| 1495 | #endif
|
|---|
| 1496 |
|
|---|
| 1497 | /* hm, doesn't that create a new one if the old one wasn't there? - Guenther */
|
|---|
| 1498 | ret = krb5_cc_initialize(context, ccache, client);
|
|---|
| 1499 | if (ret) {
|
|---|
| 1500 | goto done;
|
|---|
| 1501 | }
|
|---|
| 1502 |
|
|---|
| 1503 | ret = krb5_cc_store_cred(context, ccache, &creds);
|
|---|
| 1504 |
|
|---|
| 1505 | if (expire_time) {
|
|---|
| 1506 | *expire_time = (time_t) creds.times.endtime;
|
|---|
| 1507 | }
|
|---|
| 1508 |
|
|---|
| 1509 | done:
|
|---|
| 1510 | krb5_free_cred_contents(context, &creds_in);
|
|---|
| 1511 |
|
|---|
| 1512 | if (creds_out) {
|
|---|
| 1513 | krb5_free_creds(context, creds_out);
|
|---|
| 1514 | } else {
|
|---|
| 1515 | krb5_free_cred_contents(context, &creds);
|
|---|
| 1516 | }
|
|---|
| 1517 |
|
|---|
| 1518 | if (client) {
|
|---|
| 1519 | krb5_free_principal(context, client);
|
|---|
| 1520 | }
|
|---|
| 1521 | if (ccache) {
|
|---|
| 1522 | krb5_cc_close(context, ccache);
|
|---|
| 1523 | }
|
|---|
| 1524 | if (context) {
|
|---|
| 1525 | krb5_free_context(context);
|
|---|
| 1526 | }
|
|---|
| 1527 |
|
|---|
| 1528 | return ret;
|
|---|
| 1529 | }
|
|---|
| 1530 |
|
|---|
| 1531 | krb5_error_code smb_krb5_free_addresses(krb5_context context, smb_krb5_addresses *addr)
|
|---|
| 1532 | {
|
|---|
| 1533 | krb5_error_code ret = 0;
|
|---|
| 1534 | if (addr == NULL) {
|
|---|
| 1535 | return ret;
|
|---|
| 1536 | }
|
|---|
| 1537 | #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
|
|---|
| 1538 | krb5_free_addresses(context, addr->addrs);
|
|---|
| 1539 | #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
|
|---|
| 1540 | ret = krb5_free_addresses(context, addr->addrs);
|
|---|
| 1541 | SAFE_FREE(addr->addrs);
|
|---|
| 1542 | #endif
|
|---|
| 1543 | SAFE_FREE(addr);
|
|---|
| 1544 | addr = NULL;
|
|---|
| 1545 | return ret;
|
|---|
| 1546 | }
|
|---|
| 1547 |
|
|---|
| 1548 | krb5_error_code smb_krb5_gen_netbios_krb5_address(smb_krb5_addresses **kerb_addr)
|
|---|
| 1549 | {
|
|---|
| 1550 | krb5_error_code ret = 0;
|
|---|
| 1551 | nstring buf;
|
|---|
| 1552 | #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
|
|---|
| 1553 | krb5_address **addrs = NULL;
|
|---|
| 1554 | #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
|
|---|
| 1555 | krb5_addresses *addrs = NULL;
|
|---|
| 1556 | #endif
|
|---|
| 1557 |
|
|---|
| 1558 | *kerb_addr = (smb_krb5_addresses *)SMB_MALLOC(sizeof(smb_krb5_addresses));
|
|---|
| 1559 | if (*kerb_addr == NULL) {
|
|---|
| 1560 | return ENOMEM;
|
|---|
| 1561 | }
|
|---|
| 1562 |
|
|---|
| 1563 | put_name(buf, global_myname(), ' ', 0x20);
|
|---|
| 1564 |
|
|---|
| 1565 | #if defined(HAVE_MAGIC_IN_KRB5_ADDRESS) && defined(HAVE_ADDRTYPE_IN_KRB5_ADDRESS) /* MIT */
|
|---|
| 1566 | {
|
|---|
| 1567 | int num_addr = 2;
|
|---|
| 1568 |
|
|---|
| 1569 | addrs = (krb5_address **)SMB_MALLOC(sizeof(krb5_address *) * num_addr);
|
|---|
| 1570 | if (addrs == NULL) {
|
|---|
| 1571 | SAFE_FREE(*kerb_addr);
|
|---|
| 1572 | return ENOMEM;
|
|---|
| 1573 | }
|
|---|
| 1574 |
|
|---|
| 1575 | memset(addrs, 0, sizeof(krb5_address *) * num_addr);
|
|---|
| 1576 |
|
|---|
| 1577 | addrs[0] = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
|
|---|
| 1578 | if (addrs[0] == NULL) {
|
|---|
| 1579 | SAFE_FREE(addrs);
|
|---|
| 1580 | SAFE_FREE(*kerb_addr);
|
|---|
| 1581 | return ENOMEM;
|
|---|
| 1582 | }
|
|---|
| 1583 |
|
|---|
| 1584 | addrs[0]->magic = KV5M_ADDRESS;
|
|---|
| 1585 | addrs[0]->addrtype = KRB5_ADDR_NETBIOS;
|
|---|
| 1586 | addrs[0]->length = MAX_NETBIOSNAME_LEN;
|
|---|
| 1587 | addrs[0]->contents = (unsigned char *)SMB_MALLOC(addrs[0]->length);
|
|---|
| 1588 | if (addrs[0]->contents == NULL) {
|
|---|
| 1589 | SAFE_FREE(addrs[0]);
|
|---|
| 1590 | SAFE_FREE(addrs);
|
|---|
| 1591 | SAFE_FREE(*kerb_addr);
|
|---|
| 1592 | return ENOMEM;
|
|---|
| 1593 | }
|
|---|
| 1594 |
|
|---|
| 1595 | memcpy(addrs[0]->contents, buf, addrs[0]->length);
|
|---|
| 1596 |
|
|---|
| 1597 | addrs[1] = NULL;
|
|---|
| 1598 | }
|
|---|
| 1599 | #elif defined(HAVE_ADDR_TYPE_IN_KRB5_ADDRESS) /* Heimdal */
|
|---|
| 1600 | {
|
|---|
| 1601 | addrs = (krb5_addresses *)SMB_MALLOC(sizeof(krb5_addresses));
|
|---|
| 1602 | if (addrs == NULL) {
|
|---|
| 1603 | SAFE_FREE(*kerb_addr);
|
|---|
| 1604 | return ENOMEM;
|
|---|
| 1605 | }
|
|---|
| 1606 |
|
|---|
| 1607 | memset(addrs, 0, sizeof(krb5_addresses));
|
|---|
| 1608 |
|
|---|
| 1609 | addrs->len = 1;
|
|---|
| 1610 | addrs->val = (krb5_address *)SMB_MALLOC(sizeof(krb5_address));
|
|---|
| 1611 | if (addrs->val == NULL) {
|
|---|
| 1612 | SAFE_FREE(addrs);
|
|---|
| 1613 | SAFE_FREE(kerb_addr);
|
|---|
| 1614 | return ENOMEM;
|
|---|
| 1615 | }
|
|---|
| 1616 |
|
|---|
| 1617 | addrs->val[0].addr_type = KRB5_ADDR_NETBIOS;
|
|---|
| 1618 | addrs->val[0].address.length = MAX_NETBIOSNAME_LEN;
|
|---|
| 1619 | addrs->val[0].address.data = (unsigned char *)SMB_MALLOC(addrs->val[0].address.length);
|
|---|
| 1620 | if (addrs->val[0].address.data == NULL) {
|
|---|
| 1621 | SAFE_FREE(addrs->val);
|
|---|
| 1622 | SAFE_FREE(addrs);
|
|---|
| 1623 | SAFE_FREE(*kerb_addr);
|
|---|
| 1624 | return ENOMEM;
|
|---|
| 1625 | }
|
|---|
| 1626 |
|
|---|
| 1627 | memcpy(addrs->val[0].address.data, buf, addrs->val[0].address.length);
|
|---|
| 1628 | }
|
|---|
| 1629 | #else
|
|---|
| 1630 | #error UNKNOWN_KRB5_ADDRESS_FORMAT
|
|---|
| 1631 | #endif
|
|---|
| 1632 | (*kerb_addr)->addrs = addrs;
|
|---|
| 1633 |
|
|---|
| 1634 | return ret;
|
|---|
| 1635 | }
|
|---|
| 1636 |
|
|---|
| 1637 | void smb_krb5_free_error(krb5_context context, krb5_error *krberror)
|
|---|
| 1638 | {
|
|---|
| 1639 | #ifdef HAVE_KRB5_FREE_ERROR_CONTENTS /* Heimdal */
|
|---|
| 1640 | krb5_free_error_contents(context, krberror);
|
|---|
| 1641 | #else /* MIT */
|
|---|
| 1642 | krb5_free_error(context, krberror);
|
|---|
| 1643 | #endif
|
|---|
| 1644 | }
|
|---|
| 1645 |
|
|---|
| 1646 | krb5_error_code handle_krberror_packet(krb5_context context,
|
|---|
| 1647 | krb5_data *packet)
|
|---|
| 1648 | {
|
|---|
| 1649 | krb5_error_code ret;
|
|---|
| 1650 | bool got_error_code = False;
|
|---|
| 1651 |
|
|---|
| 1652 | DEBUG(10,("handle_krberror_packet: got error packet\n"));
|
|---|
| 1653 |
|
|---|
| 1654 | #ifdef HAVE_E_DATA_POINTER_IN_KRB5_ERROR /* Heimdal */
|
|---|
| 1655 | {
|
|---|
| 1656 | krb5_error krberror;
|
|---|
| 1657 |
|
|---|
| 1658 | if ((ret = krb5_rd_error(context, packet, &krberror))) {
|
|---|
| 1659 | DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
|
|---|
| 1660 | error_message(ret)));
|
|---|
| 1661 | return ret;
|
|---|
| 1662 | }
|
|---|
| 1663 |
|
|---|
| 1664 | if (krberror.e_data == NULL || krberror.e_data->data == NULL) {
|
|---|
| 1665 | ret = (krb5_error_code) krberror.error_code;
|
|---|
| 1666 | got_error_code = True;
|
|---|
| 1667 | }
|
|---|
| 1668 |
|
|---|
| 1669 | smb_krb5_free_error(context, &krberror);
|
|---|
| 1670 | }
|
|---|
| 1671 | #else /* MIT */
|
|---|
| 1672 | {
|
|---|
| 1673 | krb5_error *krberror;
|
|---|
| 1674 |
|
|---|
| 1675 | if ((ret = krb5_rd_error(context, packet, &krberror))) {
|
|---|
| 1676 | DEBUG(10,("handle_krberror_packet: krb5_rd_error failed with: %s\n",
|
|---|
| 1677 | error_message(ret)));
|
|---|
| 1678 | return ret;
|
|---|
| 1679 | }
|
|---|
| 1680 |
|
|---|
| 1681 | if (krberror->e_data.data == NULL) {
|
|---|
| 1682 | #if defined(ERROR_TABLE_BASE_krb5)
|
|---|
| 1683 | ret = ERROR_TABLE_BASE_krb5 + (krb5_error_code) krberror->error;
|
|---|
| 1684 | #else
|
|---|
| 1685 | ret = (krb5_error_code)krberror->error;
|
|---|
| 1686 | #endif
|
|---|
| 1687 | got_error_code = True;
|
|---|
| 1688 | }
|
|---|
| 1689 | smb_krb5_free_error(context, krberror);
|
|---|
| 1690 | }
|
|---|
| 1691 | #endif
|
|---|
| 1692 | if (got_error_code) {
|
|---|
| 1693 | DEBUG(5,("handle_krberror_packet: got KERBERR from kpasswd: %s (%d)\n",
|
|---|
| 1694 | error_message(ret), ret));
|
|---|
| 1695 | }
|
|---|
| 1696 | return ret;
|
|---|
| 1697 | }
|
|---|
| 1698 |
|
|---|
| 1699 | krb5_error_code smb_krb5_get_init_creds_opt_alloc(krb5_context context,
|
|---|
| 1700 | krb5_get_init_creds_opt **opt)
|
|---|
| 1701 | {
|
|---|
| 1702 | #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC
|
|---|
| 1703 | /* Heimdal or modern MIT version */
|
|---|
| 1704 | return krb5_get_init_creds_opt_alloc(context, opt);
|
|---|
| 1705 | #else
|
|---|
| 1706 | /* Historical MIT version */
|
|---|
| 1707 | krb5_get_init_creds_opt *my_opt;
|
|---|
| 1708 |
|
|---|
| 1709 | *opt = NULL;
|
|---|
| 1710 |
|
|---|
| 1711 | if ((my_opt = SMB_MALLOC_P(krb5_get_init_creds_opt)) == NULL) {
|
|---|
| 1712 | return ENOMEM;
|
|---|
| 1713 | }
|
|---|
| 1714 |
|
|---|
| 1715 | krb5_get_init_creds_opt_init(my_opt);
|
|---|
| 1716 |
|
|---|
| 1717 | *opt = my_opt;
|
|---|
| 1718 | return 0;
|
|---|
| 1719 | #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_ALLOC */
|
|---|
| 1720 | }
|
|---|
| 1721 |
|
|---|
| 1722 | void smb_krb5_get_init_creds_opt_free(krb5_context context,
|
|---|
| 1723 | krb5_get_init_creds_opt *opt)
|
|---|
| 1724 | {
|
|---|
| 1725 | #ifdef HAVE_KRB5_GET_INIT_CREDS_OPT_FREE
|
|---|
| 1726 |
|
|---|
| 1727 | #ifdef KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT
|
|---|
| 1728 | /* Modern MIT or Heimdal version */
|
|---|
| 1729 | krb5_get_init_creds_opt_free(context, opt);
|
|---|
| 1730 | #else
|
|---|
| 1731 | /* Heimdal version */
|
|---|
| 1732 | krb5_get_init_creds_opt_free(opt);
|
|---|
| 1733 | #endif /* KRB5_CREDS_OPT_FREE_REQUIRES_CONTEXT */
|
|---|
| 1734 |
|
|---|
| 1735 | #else /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
|
|---|
| 1736 | /* Historical MIT version */
|
|---|
| 1737 | SAFE_FREE(opt);
|
|---|
| 1738 | opt = NULL;
|
|---|
| 1739 | #endif /* HAVE_KRB5_GET_INIT_CREDS_OPT_FREE */
|
|---|
| 1740 | }
|
|---|
| 1741 |
|
|---|
| 1742 | krb5_enctype smb_get_enctype_from_kt_entry(krb5_keytab_entry *kt_entry)
|
|---|
| 1743 | {
|
|---|
| 1744 | return KRB5_KEY_TYPE(KRB5_KT_KEY(kt_entry));
|
|---|
| 1745 | }
|
|---|
| 1746 |
|
|---|
| 1747 |
|
|---|
| 1748 | /* caller needs to free etype_s */
|
|---|
| 1749 | krb5_error_code smb_krb5_enctype_to_string(krb5_context context,
|
|---|
| 1750 | krb5_enctype enctype,
|
|---|
| 1751 | char **etype_s)
|
|---|
| 1752 | {
|
|---|
| 1753 | #ifdef HAVE_KRB5_ENCTYPE_TO_STRING_WITH_KRB5_CONTEXT_ARG
|
|---|
| 1754 | return krb5_enctype_to_string(context, enctype, etype_s); /* Heimdal */
|
|---|
| 1755 | #elif defined(HAVE_KRB5_ENCTYPE_TO_STRING_WITH_SIZE_T_ARG)
|
|---|
| 1756 | char buf[256];
|
|---|
| 1757 | krb5_error_code ret = krb5_enctype_to_string(enctype, buf, 256); /* MIT */
|
|---|
| 1758 | if (ret) {
|
|---|
| 1759 | return ret;
|
|---|
| 1760 | }
|
|---|
| 1761 | *etype_s = SMB_STRDUP(buf);
|
|---|
| 1762 | if (!*etype_s) {
|
|---|
| 1763 | return ENOMEM;
|
|---|
| 1764 | }
|
|---|
| 1765 | return ret;
|
|---|
| 1766 | #else
|
|---|
| 1767 | #error UNKNOWN_KRB5_ENCTYPE_TO_STRING_FUNCTION
|
|---|
| 1768 | #endif
|
|---|
| 1769 | }
|
|---|
| 1770 |
|
|---|
| 1771 | krb5_error_code smb_krb5_mk_error(krb5_context context,
|
|---|
| 1772 | krb5_error_code error_code,
|
|---|
| 1773 | const krb5_principal server,
|
|---|
| 1774 | krb5_data *reply)
|
|---|
| 1775 | {
|
|---|
| 1776 | #ifdef HAVE_SHORT_KRB5_MK_ERROR_INTERFACE /* MIT */
|
|---|
| 1777 | /*
|
|---|
| 1778 | * The MIT interface is *terrible*.
|
|---|
| 1779 | * We have to construct this ourselves...
|
|---|
| 1780 | */
|
|---|
| 1781 | krb5_error e;
|
|---|
| 1782 |
|
|---|
| 1783 | memset(&e, 0, sizeof(e));
|
|---|
| 1784 | krb5_us_timeofday(context, &e.stime, &e.susec);
|
|---|
| 1785 | e.server = server;
|
|---|
| 1786 | #if defined(krb5_err_base)
|
|---|
| 1787 | e.error = error_code - krb5_err_base;
|
|---|
| 1788 | #elif defined(ERROR_TABLE_BASE_krb5)
|
|---|
| 1789 | e.error = error_code - ERROR_TABLE_BASE_krb5;
|
|---|
| 1790 | #else
|
|---|
| 1791 | e.error = error_code; /* Almost certainly wrong, but what can we do... ? */
|
|---|
| 1792 | #endif
|
|---|
| 1793 |
|
|---|
| 1794 | return krb5_mk_error(context, &e, reply);
|
|---|
| 1795 | #else /* Heimdal. */
|
|---|
| 1796 | return krb5_mk_error(context,
|
|---|
| 1797 | error_code,
|
|---|
| 1798 | NULL,
|
|---|
| 1799 | NULL, /* e_data */
|
|---|
| 1800 | NULL,
|
|---|
| 1801 | server,
|
|---|
| 1802 | NULL,
|
|---|
| 1803 | NULL,
|
|---|
| 1804 | reply);
|
|---|
| 1805 | #endif
|
|---|
| 1806 | }
|
|---|
| 1807 |
|
|---|
| 1808 | /**********************************************************************
|
|---|
| 1809 | * Open a krb5 keytab with flags, handles readonly or readwrite access and
|
|---|
| 1810 | * allows to process non-default keytab names.
|
|---|
| 1811 | * @param context krb5_context
|
|---|
| 1812 | * @param keytab_name_req string
|
|---|
| 1813 | * @param write_access bool if writable keytab is required
|
|---|
| 1814 | * @param krb5_keytab pointer to krb5_keytab (close with krb5_kt_close())
|
|---|
| 1815 | * @return krb5_error_code
|
|---|
| 1816 | **********************************************************************/
|
|---|
| 1817 |
|
|---|
| 1818 | /* This MAX_NAME_LEN is a constant defined in krb5.h */
|
|---|
| 1819 | #ifndef MAX_KEYTAB_NAME_LEN
|
|---|
| 1820 | #define MAX_KEYTAB_NAME_LEN 1100
|
|---|
| 1821 | #endif
|
|---|
| 1822 |
|
|---|
| 1823 | krb5_error_code smb_krb5_open_keytab(krb5_context context,
|
|---|
| 1824 | const char *keytab_name_req,
|
|---|
| 1825 | bool write_access,
|
|---|
| 1826 | krb5_keytab *keytab)
|
|---|
| 1827 | {
|
|---|
| 1828 | krb5_error_code ret = 0;
|
|---|
| 1829 | TALLOC_CTX *mem_ctx;
|
|---|
| 1830 | char keytab_string[MAX_KEYTAB_NAME_LEN];
|
|---|
| 1831 | char *kt_str = NULL;
|
|---|
| 1832 | bool found_valid_name = False;
|
|---|
| 1833 | const char *pragma = "FILE";
|
|---|
| 1834 | const char *tmp = NULL;
|
|---|
| 1835 |
|
|---|
| 1836 | if (!write_access && !keytab_name_req) {
|
|---|
| 1837 | /* caller just wants to read the default keytab readonly, so be it */
|
|---|
| 1838 | return krb5_kt_default(context, keytab);
|
|---|
| 1839 | }
|
|---|
| 1840 |
|
|---|
| 1841 | mem_ctx = talloc_init("smb_krb5_open_keytab");
|
|---|
| 1842 | if (!mem_ctx) {
|
|---|
| 1843 | return ENOMEM;
|
|---|
| 1844 | }
|
|---|
| 1845 |
|
|---|
| 1846 | #ifdef HAVE_WRFILE_KEYTAB
|
|---|
| 1847 | if (write_access) {
|
|---|
| 1848 | pragma = "WRFILE";
|
|---|
| 1849 | }
|
|---|
| 1850 | #endif
|
|---|
| 1851 |
|
|---|
| 1852 | if (keytab_name_req) {
|
|---|
| 1853 |
|
|---|
| 1854 | if (strlen(keytab_name_req) > MAX_KEYTAB_NAME_LEN) {
|
|---|
| 1855 | ret = KRB5_CONFIG_NOTENUFSPACE;
|
|---|
| 1856 | goto out;
|
|---|
| 1857 | }
|
|---|
| 1858 |
|
|---|
| 1859 | if ((strncmp(keytab_name_req, "WRFILE:/", 8) == 0) ||
|
|---|
| 1860 | (strncmp(keytab_name_req, "FILE:/", 6) == 0)) {
|
|---|
| 1861 | tmp = keytab_name_req;
|
|---|
| 1862 | goto resolve;
|
|---|
| 1863 | }
|
|---|
| 1864 |
|
|---|
| 1865 | if (keytab_name_req[0] != '/') {
|
|---|
| 1866 | ret = KRB5_KT_BADNAME;
|
|---|
| 1867 | goto out;
|
|---|
| 1868 | }
|
|---|
| 1869 |
|
|---|
| 1870 | tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, keytab_name_req);
|
|---|
| 1871 | if (!tmp) {
|
|---|
| 1872 | ret = ENOMEM;
|
|---|
| 1873 | goto out;
|
|---|
| 1874 | }
|
|---|
| 1875 |
|
|---|
| 1876 | goto resolve;
|
|---|
| 1877 | }
|
|---|
| 1878 |
|
|---|
| 1879 | /* we need to handle more complex keytab_strings, like:
|
|---|
| 1880 | * "ANY:FILE:/etc/krb5.keytab,krb4:/etc/srvtab" */
|
|---|
| 1881 |
|
|---|
| 1882 | ret = krb5_kt_default_name(context, &keytab_string[0], MAX_KEYTAB_NAME_LEN - 2);
|
|---|
| 1883 | if (ret) {
|
|---|
| 1884 | goto out;
|
|---|
| 1885 | }
|
|---|
| 1886 |
|
|---|
| 1887 | DEBUG(10,("smb_krb5_open_keytab: krb5_kt_default_name returned %s\n", keytab_string));
|
|---|
| 1888 |
|
|---|
| 1889 | tmp = talloc_strdup(mem_ctx, keytab_string);
|
|---|
| 1890 | if (!tmp) {
|
|---|
| 1891 | ret = ENOMEM;
|
|---|
| 1892 | goto out;
|
|---|
| 1893 | }
|
|---|
| 1894 |
|
|---|
| 1895 | if (strncmp(tmp, "ANY:", 4) == 0) {
|
|---|
| 1896 | tmp += 4;
|
|---|
| 1897 | }
|
|---|
| 1898 |
|
|---|
| 1899 | memset(&keytab_string, '\0', sizeof(keytab_string));
|
|---|
| 1900 |
|
|---|
| 1901 | while (next_token_talloc(mem_ctx, &tmp, &kt_str, ",")) {
|
|---|
| 1902 | if (strncmp(kt_str, "WRFILE:", 7) == 0) {
|
|---|
| 1903 | found_valid_name = True;
|
|---|
| 1904 | tmp = kt_str;
|
|---|
| 1905 | tmp += 7;
|
|---|
| 1906 | }
|
|---|
| 1907 |
|
|---|
| 1908 | if (strncmp(kt_str, "FILE:", 5) == 0) {
|
|---|
| 1909 | found_valid_name = True;
|
|---|
| 1910 | tmp = kt_str;
|
|---|
| 1911 | tmp += 5;
|
|---|
| 1912 | }
|
|---|
| 1913 |
|
|---|
| 1914 | if (tmp[0] == '/') {
|
|---|
| 1915 | /* Treat as a FILE: keytab definition. */
|
|---|
| 1916 | found_valid_name = true;
|
|---|
| 1917 | }
|
|---|
| 1918 |
|
|---|
| 1919 | if (found_valid_name) {
|
|---|
| 1920 | if (tmp[0] != '/') {
|
|---|
| 1921 | ret = KRB5_KT_BADNAME;
|
|---|
| 1922 | goto out;
|
|---|
| 1923 | }
|
|---|
| 1924 |
|
|---|
| 1925 | tmp = talloc_asprintf(mem_ctx, "%s:%s", pragma, tmp);
|
|---|
| 1926 | if (!tmp) {
|
|---|
| 1927 | ret = ENOMEM;
|
|---|
| 1928 | goto out;
|
|---|
| 1929 | }
|
|---|
| 1930 | break;
|
|---|
| 1931 | }
|
|---|
| 1932 | }
|
|---|
| 1933 |
|
|---|
| 1934 | if (!found_valid_name) {
|
|---|
| 1935 | ret = KRB5_KT_UNKNOWN_TYPE;
|
|---|
| 1936 | goto out;
|
|---|
| 1937 | }
|
|---|
| 1938 |
|
|---|
| 1939 | resolve:
|
|---|
| 1940 | DEBUG(10,("smb_krb5_open_keytab: resolving: %s\n", tmp));
|
|---|
| 1941 | ret = krb5_kt_resolve(context, tmp, keytab);
|
|---|
| 1942 |
|
|---|
| 1943 | out:
|
|---|
| 1944 | TALLOC_FREE(mem_ctx);
|
|---|
| 1945 | return ret;
|
|---|
| 1946 | }
|
|---|
| 1947 |
|
|---|
| 1948 | krb5_error_code smb_krb5_keytab_name(TALLOC_CTX *mem_ctx,
|
|---|
| 1949 | krb5_context context,
|
|---|
| 1950 | krb5_keytab keytab,
|
|---|
| 1951 | const char **keytab_name)
|
|---|
| 1952 | {
|
|---|
| 1953 | char keytab_string[MAX_KEYTAB_NAME_LEN];
|
|---|
| 1954 | krb5_error_code ret = 0;
|
|---|
| 1955 |
|
|---|
| 1956 | ret = krb5_kt_get_name(context, keytab,
|
|---|
| 1957 | keytab_string, MAX_KEYTAB_NAME_LEN - 2);
|
|---|
| 1958 | if (ret) {
|
|---|
| 1959 | return ret;
|
|---|
| 1960 | }
|
|---|
| 1961 |
|
|---|
| 1962 | *keytab_name = talloc_strdup(mem_ctx, keytab_string);
|
|---|
| 1963 | if (!*keytab_name) {
|
|---|
| 1964 | return ENOMEM;
|
|---|
| 1965 | }
|
|---|
| 1966 |
|
|---|
| 1967 | return ret;
|
|---|
| 1968 | }
|
|---|
| 1969 |
|
|---|
| 1970 | #if defined(HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE) && \
|
|---|
| 1971 | defined(HAVE_KRB5_GET_CREDS_OPT_ALLOC) && \
|
|---|
| 1972 | defined(HAVE_KRB5_GET_CREDS)
|
|---|
| 1973 | static krb5_error_code smb_krb5_get_credentials_for_user_opt(krb5_context context,
|
|---|
| 1974 | krb5_ccache ccache,
|
|---|
| 1975 | krb5_principal me,
|
|---|
| 1976 | krb5_principal server,
|
|---|
| 1977 | krb5_principal impersonate_princ,
|
|---|
| 1978 | krb5_creds **out_creds)
|
|---|
| 1979 | {
|
|---|
| 1980 | krb5_error_code ret;
|
|---|
| 1981 | krb5_get_creds_opt opt;
|
|---|
| 1982 |
|
|---|
| 1983 | ret = krb5_get_creds_opt_alloc(context, &opt);
|
|---|
| 1984 | if (ret) {
|
|---|
| 1985 | goto done;
|
|---|
| 1986 | }
|
|---|
| 1987 | krb5_get_creds_opt_add_options(context, opt, KRB5_GC_FORWARDABLE);
|
|---|
| 1988 |
|
|---|
| 1989 | if (impersonate_princ) {
|
|---|
| 1990 | ret = krb5_get_creds_opt_set_impersonate(context, opt,
|
|---|
| 1991 | impersonate_princ);
|
|---|
| 1992 | if (ret) {
|
|---|
| 1993 | goto done;
|
|---|
| 1994 | }
|
|---|
| 1995 | }
|
|---|
| 1996 |
|
|---|
| 1997 | ret = krb5_get_creds(context, opt, ccache, server, out_creds);
|
|---|
| 1998 | if (ret) {
|
|---|
| 1999 | goto done;
|
|---|
| 2000 | }
|
|---|
| 2001 |
|
|---|
| 2002 | done:
|
|---|
| 2003 | if (opt) {
|
|---|
| 2004 | krb5_get_creds_opt_free(context, opt);
|
|---|
| 2005 | }
|
|---|
| 2006 | return ret;
|
|---|
| 2007 | }
|
|---|
| 2008 | #endif /* HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE */
|
|---|
| 2009 |
|
|---|
| 2010 | #ifdef HAVE_KRB5_GET_CREDENTIALS_FOR_USER
|
|---|
| 2011 | static krb5_error_code smb_krb5_get_credentials_for_user(krb5_context context,
|
|---|
| 2012 | krb5_ccache ccache,
|
|---|
| 2013 | krb5_principal me,
|
|---|
| 2014 | krb5_principal server,
|
|---|
| 2015 | krb5_principal impersonate_princ,
|
|---|
| 2016 | krb5_creds **out_creds)
|
|---|
| 2017 | {
|
|---|
| 2018 | krb5_error_code ret;
|
|---|
| 2019 | krb5_creds in_creds;
|
|---|
| 2020 |
|
|---|
| 2021 | #if !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER
|
|---|
| 2022 | krb5_error_code KRB5_CALLCONV
|
|---|
| 2023 | krb5_get_credentials_for_user(krb5_context context, krb5_flags options,
|
|---|
| 2024 | krb5_ccache ccache, krb5_creds *in_creds,
|
|---|
| 2025 | krb5_data *subject_cert,
|
|---|
| 2026 | krb5_creds **out_creds);
|
|---|
| 2027 | #endif /* !HAVE_DECL_KRB5_GET_CREDENTIALS_FOR_USER */
|
|---|
| 2028 |
|
|---|
| 2029 | ZERO_STRUCT(in_creds);
|
|---|
| 2030 |
|
|---|
| 2031 | if (impersonate_princ) {
|
|---|
| 2032 |
|
|---|
| 2033 | in_creds.server = me;
|
|---|
| 2034 | in_creds.client = impersonate_princ;
|
|---|
| 2035 |
|
|---|
| 2036 | ret = krb5_get_credentials_for_user(context,
|
|---|
| 2037 | 0, /* krb5_flags options */
|
|---|
| 2038 | ccache,
|
|---|
| 2039 | &in_creds,
|
|---|
| 2040 | NULL, /* krb5_data *subject_cert */
|
|---|
| 2041 | out_creds);
|
|---|
| 2042 | } else {
|
|---|
| 2043 | in_creds.client = me;
|
|---|
| 2044 | in_creds.server = server;
|
|---|
| 2045 |
|
|---|
| 2046 | ret = krb5_get_credentials(context, 0, ccache,
|
|---|
| 2047 | &in_creds, out_creds);
|
|---|
| 2048 | }
|
|---|
| 2049 |
|
|---|
| 2050 | return ret;
|
|---|
| 2051 | }
|
|---|
| 2052 | #endif /* HAVE_KRB5_GET_CREDENTIALS_FOR_USER */
|
|---|
| 2053 |
|
|---|
| 2054 | /*
|
|---|
| 2055 | * smb_krb5_get_credentials
|
|---|
| 2056 | *
|
|---|
| 2057 | * @brief Get krb5 credentials for a server
|
|---|
| 2058 | *
|
|---|
| 2059 | * @param[in] context An initialized krb5_context
|
|---|
| 2060 | * @param[in] ccache An initialized krb5_ccache
|
|---|
| 2061 | * @param[in] me The krb5_principal of the caller
|
|---|
| 2062 | * @param[in] server The krb5_principal of the requested service
|
|---|
| 2063 | * @param[in] impersonate_princ The krb5_principal of a user to impersonate as (optional)
|
|---|
| 2064 | * @param[out] out_creds The returned krb5_creds structure
|
|---|
| 2065 | * @return krb5_error_code
|
|---|
| 2066 | *
|
|---|
| 2067 | */
|
|---|
| 2068 | krb5_error_code smb_krb5_get_credentials(krb5_context context,
|
|---|
| 2069 | krb5_ccache ccache,
|
|---|
| 2070 | krb5_principal me,
|
|---|
| 2071 | krb5_principal server,
|
|---|
| 2072 | krb5_principal impersonate_princ,
|
|---|
| 2073 | krb5_creds **out_creds)
|
|---|
| 2074 | {
|
|---|
| 2075 | krb5_error_code ret;
|
|---|
| 2076 | krb5_creds *creds = NULL;
|
|---|
| 2077 |
|
|---|
| 2078 | *out_creds = NULL;
|
|---|
| 2079 |
|
|---|
| 2080 | if (impersonate_princ) {
|
|---|
| 2081 | #ifdef HAVE_KRB5_GET_CREDS_OPT_SET_IMPERSONATE /* Heimdal */
|
|---|
| 2082 | ret = smb_krb5_get_credentials_for_user_opt(context, ccache, me, server, impersonate_princ, &creds);
|
|---|
| 2083 | #elif defined(HAVE_KRB5_GET_CREDENTIALS_FOR_USER) /* MIT */
|
|---|
| 2084 | ret = smb_krb5_get_credentials_for_user(context, ccache, me, server, impersonate_princ, &creds);
|
|---|
| 2085 | #else
|
|---|
| 2086 | ret = ENOTSUP;
|
|---|
| 2087 | #endif
|
|---|
| 2088 | } else {
|
|---|
| 2089 | krb5_creds in_creds;
|
|---|
| 2090 |
|
|---|
| 2091 | ZERO_STRUCT(in_creds);
|
|---|
| 2092 |
|
|---|
| 2093 | in_creds.client = me;
|
|---|
| 2094 | in_creds.server = server;
|
|---|
| 2095 |
|
|---|
| 2096 | ret = krb5_get_credentials(context, 0, ccache,
|
|---|
| 2097 | &in_creds, &creds);
|
|---|
| 2098 | }
|
|---|
| 2099 | if (ret) {
|
|---|
| 2100 | goto done;
|
|---|
| 2101 | }
|
|---|
| 2102 |
|
|---|
| 2103 | ret = krb5_cc_store_cred(context, ccache, creds);
|
|---|
| 2104 | if (ret) {
|
|---|
| 2105 | goto done;
|
|---|
| 2106 | }
|
|---|
| 2107 |
|
|---|
| 2108 | if (out_creds) {
|
|---|
| 2109 | *out_creds = creds;
|
|---|
| 2110 | }
|
|---|
| 2111 |
|
|---|
| 2112 | done:
|
|---|
| 2113 | if (creds && ret) {
|
|---|
| 2114 | krb5_free_creds(context, creds);
|
|---|
| 2115 | }
|
|---|
| 2116 |
|
|---|
| 2117 | return ret;
|
|---|
| 2118 | }
|
|---|
| 2119 |
|
|---|
| 2120 | /*
|
|---|
| 2121 | * smb_krb5_get_creds
|
|---|
| 2122 | *
|
|---|
| 2123 | * @brief Get krb5 credentials for a server
|
|---|
| 2124 | *
|
|---|
| 2125 | * @param[in] server_s The string name of the service
|
|---|
| 2126 | * @param[in] time_offset The offset to the KDCs time in seconds (optional)
|
|---|
| 2127 | * @param[in] cc The krb5 credential cache string name (optional)
|
|---|
| 2128 | * @param[in] impersonate_princ_s The string principal name to impersonate (optional)
|
|---|
| 2129 | * @param[out] creds_p The returned krb5_creds structure
|
|---|
| 2130 | * @return krb5_error_code
|
|---|
| 2131 | *
|
|---|
| 2132 | */
|
|---|
| 2133 | krb5_error_code smb_krb5_get_creds(const char *server_s,
|
|---|
| 2134 | time_t time_offset,
|
|---|
| 2135 | const char *cc,
|
|---|
| 2136 | const char *impersonate_princ_s,
|
|---|
| 2137 | krb5_creds **creds_p)
|
|---|
| 2138 | {
|
|---|
| 2139 | krb5_error_code ret;
|
|---|
| 2140 | krb5_context context = NULL;
|
|---|
| 2141 | krb5_principal me = NULL;
|
|---|
| 2142 | krb5_principal server = NULL;
|
|---|
| 2143 | krb5_principal impersonate_princ = NULL;
|
|---|
| 2144 | krb5_creds *creds = NULL;
|
|---|
| 2145 | krb5_ccache ccache = NULL;
|
|---|
| 2146 |
|
|---|
| 2147 | *creds_p = NULL;
|
|---|
| 2148 |
|
|---|
| 2149 | initialize_krb5_error_table();
|
|---|
| 2150 | ret = krb5_init_context(&context);
|
|---|
| 2151 | if (ret) {
|
|---|
| 2152 | goto done;
|
|---|
| 2153 | }
|
|---|
| 2154 |
|
|---|
| 2155 | if (time_offset != 0) {
|
|---|
| 2156 | krb5_set_real_time(context, time(NULL) + time_offset, 0);
|
|---|
| 2157 | }
|
|---|
| 2158 |
|
|---|
| 2159 | ret = krb5_cc_resolve(context, cc ? cc :
|
|---|
| 2160 | krb5_cc_default_name(context), &ccache);
|
|---|
| 2161 | if (ret) {
|
|---|
| 2162 | goto done;
|
|---|
| 2163 | }
|
|---|
| 2164 |
|
|---|
| 2165 | ret = krb5_cc_get_principal(context, ccache, &me);
|
|---|
| 2166 | if (ret) {
|
|---|
| 2167 | goto done;
|
|---|
| 2168 | }
|
|---|
| 2169 |
|
|---|
| 2170 | ret = smb_krb5_parse_name(context, server_s, &server);
|
|---|
| 2171 | if (ret) {
|
|---|
| 2172 | goto done;
|
|---|
| 2173 | }
|
|---|
| 2174 |
|
|---|
| 2175 | if (impersonate_princ_s) {
|
|---|
| 2176 | ret = smb_krb5_parse_name(context, impersonate_princ_s,
|
|---|
| 2177 | &impersonate_princ);
|
|---|
| 2178 | if (ret) {
|
|---|
| 2179 | goto done;
|
|---|
| 2180 | }
|
|---|
| 2181 | }
|
|---|
| 2182 |
|
|---|
| 2183 | ret = smb_krb5_get_credentials(context, ccache,
|
|---|
| 2184 | me, server, impersonate_princ,
|
|---|
| 2185 | &creds);
|
|---|
| 2186 | if (ret) {
|
|---|
| 2187 | goto done;
|
|---|
| 2188 | }
|
|---|
| 2189 |
|
|---|
| 2190 | ret = krb5_cc_store_cred(context, ccache, creds);
|
|---|
| 2191 | if (ret) {
|
|---|
| 2192 | goto done;
|
|---|
| 2193 | }
|
|---|
| 2194 |
|
|---|
| 2195 | if (creds_p) {
|
|---|
| 2196 | *creds_p = creds;
|
|---|
| 2197 | }
|
|---|
| 2198 |
|
|---|
| 2199 | DEBUG(1,("smb_krb5_get_creds: got ticket for %s\n",
|
|---|
| 2200 | server_s));
|
|---|
| 2201 |
|
|---|
| 2202 | if (impersonate_princ_s) {
|
|---|
| 2203 | char *client = NULL;
|
|---|
| 2204 |
|
|---|
| 2205 | ret = smb_krb5_unparse_name(talloc_tos(), context, creds->client, &client);
|
|---|
| 2206 | if (ret) {
|
|---|
| 2207 | goto done;
|
|---|
| 2208 | }
|
|---|
| 2209 | DEBUGADD(1,("smb_krb5_get_creds: using S4U2SELF impersonation as %s\n",
|
|---|
| 2210 | client));
|
|---|
| 2211 | TALLOC_FREE(client);
|
|---|
| 2212 | }
|
|---|
| 2213 |
|
|---|
| 2214 | done:
|
|---|
| 2215 | if (!context) {
|
|---|
| 2216 | return ret;
|
|---|
| 2217 | }
|
|---|
| 2218 |
|
|---|
| 2219 | if (creds && ret) {
|
|---|
| 2220 | krb5_free_creds(context, creds);
|
|---|
| 2221 | }
|
|---|
| 2222 | if (server) {
|
|---|
| 2223 | krb5_free_principal(context, server);
|
|---|
| 2224 | }
|
|---|
| 2225 | if (me) {
|
|---|
| 2226 | krb5_free_principal(context, me);
|
|---|
| 2227 | }
|
|---|
| 2228 | if (impersonate_princ) {
|
|---|
| 2229 | krb5_free_principal(context, impersonate_princ);
|
|---|
| 2230 | }
|
|---|
| 2231 | if (ccache) {
|
|---|
| 2232 | krb5_cc_close(context, ccache);
|
|---|
| 2233 | }
|
|---|
| 2234 | krb5_free_context(context);
|
|---|
| 2235 |
|
|---|
| 2236 | return ret;
|
|---|
| 2237 | }
|
|---|
| 2238 |
|
|---|
| 2239 | /*
|
|---|
| 2240 | * smb_krb5_principal_get_realm
|
|---|
| 2241 | *
|
|---|
| 2242 | * @brief Get realm of a principal
|
|---|
| 2243 | *
|
|---|
| 2244 | * @param[in] context The krb5_context
|
|---|
| 2245 | * @param[in] principal The principal
|
|---|
| 2246 | * @return pointer to the realm
|
|---|
| 2247 | *
|
|---|
| 2248 | */
|
|---|
| 2249 |
|
|---|
| 2250 | char *smb_krb5_principal_get_realm(krb5_context context,
|
|---|
| 2251 | krb5_principal principal)
|
|---|
| 2252 | {
|
|---|
| 2253 | #ifdef HAVE_KRB5_PRINCIPAL_GET_REALM /* Heimdal */
|
|---|
| 2254 | return krb5_principal_get_realm(context, principal);
|
|---|
| 2255 | #elif defined(krb5_princ_realm) /* MIT */
|
|---|
| 2256 | krb5_data *realm;
|
|---|
| 2257 | realm = krb5_princ_realm(context, principal);
|
|---|
| 2258 | return (char *)realm->data;
|
|---|
| 2259 | #else
|
|---|
| 2260 | return NULL;
|
|---|
| 2261 | #endif
|
|---|
| 2262 | }
|
|---|
| 2263 |
|
|---|
| 2264 | #else /* HAVE_KRB5 */
|
|---|
| 2265 | /* this saves a few linking headaches */
|
|---|
| 2266 | int cli_krb5_get_ticket(const char *principal, time_t time_offset,
|
|---|
| 2267 | DATA_BLOB *ticket, DATA_BLOB *session_key_krb5, uint32 extra_ap_opts,
|
|---|
| 2268 | const char *ccname, time_t *tgs_expire,
|
|---|
| 2269 | const char *impersonate_princ_s)
|
|---|
| 2270 | {
|
|---|
| 2271 | DEBUG(0,("NO KERBEROS SUPPORT\n"));
|
|---|
| 2272 | return 1;
|
|---|
| 2273 | }
|
|---|
| 2274 |
|
|---|
| 2275 | #endif
|
|---|