[740] | 1 | /*
|
---|
| 2 | Unix SMB/CIFS implementation.
|
---|
| 3 |
|
---|
| 4 | Database Glue between Samba and the KDC
|
---|
| 5 |
|
---|
| 6 | Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2009
|
---|
| 7 | Copyright (C) Simo Sorce <idra@samba.org> 2010
|
---|
| 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 |
|
---|
| 20 | You should have received a copy of the GNU General Public License
|
---|
| 21 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 22 | */
|
---|
| 23 |
|
---|
| 24 | #include "includes.h"
|
---|
| 25 | #include "libcli/security/security.h"
|
---|
| 26 | #include "auth/auth.h"
|
---|
| 27 | #include "auth/auth_sam.h"
|
---|
| 28 | #include "dsdb/samdb/samdb.h"
|
---|
| 29 | #include "dsdb/common/util.h"
|
---|
| 30 | #include "librpc/gen_ndr/ndr_drsblobs.h"
|
---|
| 31 | #include "param/param.h"
|
---|
| 32 | #include "../lib/crypto/md4.h"
|
---|
| 33 | #include "system/kerberos.h"
|
---|
| 34 | #include "auth/kerberos/kerberos.h"
|
---|
| 35 | #include <hdb.h>
|
---|
| 36 | #include "kdc/samba_kdc.h"
|
---|
| 37 | #include "kdc/kdc-policy.h"
|
---|
| 38 |
|
---|
| 39 | #define SAMBA_KVNO_GET_KRBTGT(kvno) \
|
---|
| 40 | ((uint16_t)(((uint32_t)kvno) >> 16))
|
---|
| 41 |
|
---|
| 42 | #define SAMBA_KVNO_AND_KRBTGT(kvno, krbtgt) \
|
---|
| 43 | ((krb5_kvno)((((uint32_t)kvno) & 0xFFFF) | \
|
---|
| 44 | ((((uint32_t)krbtgt) << 16) & 0xFFFF0000)))
|
---|
| 45 |
|
---|
| 46 | enum samba_kdc_ent_type
|
---|
| 47 | { SAMBA_KDC_ENT_TYPE_CLIENT, SAMBA_KDC_ENT_TYPE_SERVER,
|
---|
| 48 | SAMBA_KDC_ENT_TYPE_KRBTGT, SAMBA_KDC_ENT_TYPE_TRUST, SAMBA_KDC_ENT_TYPE_ANY };
|
---|
| 49 |
|
---|
| 50 | enum trust_direction {
|
---|
| 51 | UNKNOWN = 0,
|
---|
| 52 | INBOUND = LSA_TRUST_DIRECTION_INBOUND,
|
---|
| 53 | OUTBOUND = LSA_TRUST_DIRECTION_OUTBOUND
|
---|
| 54 | };
|
---|
| 55 |
|
---|
| 56 | static const char *trust_attrs[] = {
|
---|
| 57 | "trustPartner",
|
---|
| 58 | "trustAuthIncoming",
|
---|
| 59 | "trustAuthOutgoing",
|
---|
| 60 | "whenCreated",
|
---|
| 61 | "msDS-SupportedEncryptionTypes",
|
---|
| 62 | "trustAttributes",
|
---|
| 63 | "trustDirection",
|
---|
| 64 | "trustType",
|
---|
| 65 | NULL
|
---|
| 66 | };
|
---|
| 67 |
|
---|
| 68 | static KerberosTime ldb_msg_find_krb5time_ldap_time(struct ldb_message *msg, const char *attr, KerberosTime default_val)
|
---|
| 69 | {
|
---|
| 70 | const char *tmp;
|
---|
| 71 | const char *gentime;
|
---|
| 72 | struct tm tm;
|
---|
| 73 |
|
---|
| 74 | gentime = ldb_msg_find_attr_as_string(msg, attr, NULL);
|
---|
| 75 | if (!gentime)
|
---|
| 76 | return default_val;
|
---|
| 77 |
|
---|
| 78 | tmp = strptime(gentime, "%Y%m%d%H%M%SZ", &tm);
|
---|
| 79 | if (tmp == NULL) {
|
---|
| 80 | return default_val;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | return timegm(&tm);
|
---|
| 84 | }
|
---|
| 85 |
|
---|
| 86 | static HDBFlags uf2HDBFlags(krb5_context context, uint32_t userAccountControl, enum samba_kdc_ent_type ent_type)
|
---|
| 87 | {
|
---|
| 88 | HDBFlags flags = int2HDBFlags(0);
|
---|
| 89 |
|
---|
| 90 | /* we don't allow kadmin deletes */
|
---|
| 91 | flags.immutable = 1;
|
---|
| 92 |
|
---|
| 93 | /* mark the principal as invalid to start with */
|
---|
| 94 | flags.invalid = 1;
|
---|
| 95 |
|
---|
| 96 | flags.renewable = 1;
|
---|
| 97 |
|
---|
| 98 | /* All accounts are servers, but this may be disabled again in the caller */
|
---|
| 99 | flags.server = 1;
|
---|
| 100 |
|
---|
| 101 | /* Account types - clear the invalid bit if it turns out to be valid */
|
---|
| 102 | if (userAccountControl & UF_NORMAL_ACCOUNT) {
|
---|
| 103 | if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
|
---|
| 104 | flags.client = 1;
|
---|
| 105 | }
|
---|
| 106 | flags.invalid = 0;
|
---|
| 107 | }
|
---|
| 108 |
|
---|
| 109 | if (userAccountControl & UF_INTERDOMAIN_TRUST_ACCOUNT) {
|
---|
| 110 | if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
|
---|
| 111 | flags.client = 1;
|
---|
| 112 | }
|
---|
| 113 | flags.invalid = 0;
|
---|
| 114 | }
|
---|
| 115 | if (userAccountControl & UF_WORKSTATION_TRUST_ACCOUNT) {
|
---|
| 116 | if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
|
---|
| 117 | flags.client = 1;
|
---|
| 118 | }
|
---|
| 119 | flags.invalid = 0;
|
---|
| 120 | }
|
---|
| 121 | if (userAccountControl & UF_SERVER_TRUST_ACCOUNT) {
|
---|
| 122 | if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
|
---|
| 123 | flags.client = 1;
|
---|
| 124 | }
|
---|
| 125 | flags.invalid = 0;
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | /* Not permitted to act as a client if disabled */
|
---|
| 129 | if (userAccountControl & UF_ACCOUNTDISABLE) {
|
---|
| 130 | flags.client = 0;
|
---|
| 131 | }
|
---|
| 132 | if (userAccountControl & UF_LOCKOUT) {
|
---|
| 133 | flags.invalid = 1;
|
---|
| 134 | }
|
---|
| 135 | /*
|
---|
| 136 | if (userAccountControl & UF_PASSWORD_NOTREQD) {
|
---|
| 137 | flags.invalid = 1;
|
---|
| 138 | }
|
---|
| 139 | */
|
---|
| 140 | /*
|
---|
| 141 | UF_PASSWORD_CANT_CHANGE and UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED are irrelevent
|
---|
| 142 | */
|
---|
| 143 | if (userAccountControl & UF_TEMP_DUPLICATE_ACCOUNT) {
|
---|
| 144 | flags.invalid = 1;
|
---|
| 145 | }
|
---|
| 146 |
|
---|
| 147 | /* UF_DONT_EXPIRE_PASSWD and UF_USE_DES_KEY_ONLY handled in samba_kdc_message2entry() */
|
---|
| 148 |
|
---|
| 149 | /*
|
---|
| 150 | if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
|
---|
| 151 | flags.invalid = 1;
|
---|
| 152 | }
|
---|
| 153 | */
|
---|
| 154 | if (userAccountControl & UF_SMARTCARD_REQUIRED) {
|
---|
| 155 | flags.require_hwauth = 1;
|
---|
| 156 | }
|
---|
| 157 | if (userAccountControl & UF_TRUSTED_FOR_DELEGATION) {
|
---|
| 158 | flags.ok_as_delegate = 1;
|
---|
| 159 | }
|
---|
| 160 | if (!(userAccountControl & UF_NOT_DELEGATED)) {
|
---|
| 161 | flags.forwardable = 1;
|
---|
| 162 | flags.proxiable = 1;
|
---|
| 163 | }
|
---|
| 164 |
|
---|
| 165 | if (userAccountControl & UF_DONT_REQUIRE_PREAUTH) {
|
---|
| 166 | flags.require_preauth = 0;
|
---|
| 167 | } else {
|
---|
| 168 | flags.require_preauth = 1;
|
---|
| 169 |
|
---|
| 170 | }
|
---|
| 171 | return flags;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | static int samba_kdc_entry_destructor(struct samba_kdc_entry *p)
|
---|
| 175 | {
|
---|
| 176 | hdb_entry_ex *entry_ex = p->entry_ex;
|
---|
| 177 | free_hdb_entry(&entry_ex->entry);
|
---|
| 178 | return 0;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
| 181 | static void samba_kdc_free_entry(krb5_context context, hdb_entry_ex *entry_ex)
|
---|
| 182 | {
|
---|
| 183 | /* this function is called only from hdb_free_entry().
|
---|
| 184 | * Make sure we neutralize the destructor or we will
|
---|
| 185 | * get a double free later when hdb_free_entry() will
|
---|
| 186 | * try to call free_hdb_entry() */
|
---|
| 187 | talloc_set_destructor(entry_ex->ctx, NULL);
|
---|
| 188 |
|
---|
| 189 | /* now proceed to free the talloc part */
|
---|
| 190 | talloc_free(entry_ex->ctx);
|
---|
| 191 | }
|
---|
| 192 |
|
---|
| 193 | static krb5_error_code samba_kdc_message2entry_keys(krb5_context context,
|
---|
| 194 | struct samba_kdc_db_context *kdc_db_ctx,
|
---|
| 195 | TALLOC_CTX *mem_ctx,
|
---|
| 196 | struct ldb_message *msg,
|
---|
| 197 | uint32_t rid,
|
---|
| 198 | bool is_rodc,
|
---|
| 199 | uint32_t userAccountControl,
|
---|
| 200 | enum samba_kdc_ent_type ent_type,
|
---|
| 201 | hdb_entry_ex *entry_ex)
|
---|
| 202 | {
|
---|
| 203 | krb5_error_code ret = 0;
|
---|
| 204 | enum ndr_err_code ndr_err;
|
---|
| 205 | struct samr_Password *hash;
|
---|
| 206 | const struct ldb_val *sc_val;
|
---|
| 207 | struct supplementalCredentialsBlob scb;
|
---|
| 208 | struct supplementalCredentialsPackage *scpk = NULL;
|
---|
| 209 | bool newer_keys = false;
|
---|
| 210 | struct package_PrimaryKerberosBlob _pkb;
|
---|
| 211 | struct package_PrimaryKerberosCtr3 *pkb3 = NULL;
|
---|
| 212 | struct package_PrimaryKerberosCtr4 *pkb4 = NULL;
|
---|
| 213 | uint16_t i;
|
---|
| 214 | uint16_t allocated_keys = 0;
|
---|
| 215 | int rodc_krbtgt_number = 0;
|
---|
| 216 | int kvno = 0;
|
---|
| 217 | uint32_t supported_enctypes
|
---|
| 218 | = ldb_msg_find_attr_as_uint(msg,
|
---|
| 219 | "msDS-SupportedEncryptionTypes",
|
---|
| 220 | 0);
|
---|
| 221 |
|
---|
| 222 | if (rid == DOMAIN_RID_KRBTGT || is_rodc) {
|
---|
| 223 | /* KDCs (and KDCs on RODCs) use AES */
|
---|
| 224 | supported_enctypes |= ENC_HMAC_SHA1_96_AES128 | ENC_HMAC_SHA1_96_AES256;
|
---|
| 225 | } else if (userAccountControl & (UF_PARTIAL_SECRETS_ACCOUNT|UF_SERVER_TRUST_ACCOUNT)) {
|
---|
| 226 | /* DCs and RODCs comptuer accounts use AES */
|
---|
| 227 | supported_enctypes |= ENC_HMAC_SHA1_96_AES128 | ENC_HMAC_SHA1_96_AES256;
|
---|
| 228 | } else if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT ||
|
---|
| 229 | (ent_type == SAMBA_KDC_ENT_TYPE_ANY)) {
|
---|
| 230 | /* for AS-REQ the client chooses the enc types it
|
---|
| 231 | * supports, and this will vary between computers a
|
---|
| 232 | * user logs in from.
|
---|
| 233 | *
|
---|
| 234 | * likewise for 'any' return as much as is supported,
|
---|
| 235 | * to export into a keytab */
|
---|
| 236 | supported_enctypes = ENC_ALL_TYPES;
|
---|
| 237 | }
|
---|
| 238 |
|
---|
| 239 | /* If UF_USE_DES_KEY_ONLY has been set, then don't allow use of the newer enc types */
|
---|
| 240 | if (userAccountControl & UF_USE_DES_KEY_ONLY) {
|
---|
| 241 | supported_enctypes = ENC_CRC32|ENC_RSA_MD5;
|
---|
| 242 | } else {
|
---|
| 243 | /* Otherwise, add in the default enc types */
|
---|
| 244 | supported_enctypes |= ENC_CRC32 | ENC_RSA_MD5 | ENC_RC4_HMAC_MD5;
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 | /* Is this the krbtgt or a RODC krbtgt */
|
---|
| 248 | if (is_rodc) {
|
---|
| 249 | rodc_krbtgt_number = ldb_msg_find_attr_as_int(msg, "msDS-SecondaryKrbTgtNumber", -1);
|
---|
| 250 |
|
---|
| 251 | if (rodc_krbtgt_number == -1) {
|
---|
| 252 | return EINVAL;
|
---|
| 253 | }
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | entry_ex->entry.keys.val = NULL;
|
---|
| 257 | entry_ex->entry.keys.len = 0;
|
---|
| 258 |
|
---|
| 259 | kvno = ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0);
|
---|
| 260 | if (is_rodc) {
|
---|
| 261 | kvno = SAMBA_KVNO_AND_KRBTGT(kvno, rodc_krbtgt_number);
|
---|
| 262 | }
|
---|
| 263 | entry_ex->entry.kvno = kvno;
|
---|
| 264 |
|
---|
| 265 | /* Get keys from the db */
|
---|
| 266 |
|
---|
| 267 | hash = samdb_result_hash(mem_ctx, msg, "unicodePwd");
|
---|
| 268 | sc_val = ldb_msg_find_ldb_val(msg, "supplementalCredentials");
|
---|
| 269 |
|
---|
| 270 | /* unicodePwd for enctype 0x17 (23) if present */
|
---|
| 271 | if (hash) {
|
---|
| 272 | allocated_keys++;
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | /* supplementalCredentials if present */
|
---|
| 276 | if (sc_val) {
|
---|
| 277 | ndr_err = ndr_pull_struct_blob_all(sc_val, mem_ctx, &scb,
|
---|
| 278 | (ndr_pull_flags_fn_t)ndr_pull_supplementalCredentialsBlob);
|
---|
| 279 | if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
|
---|
| 280 | dump_data(0, sc_val->data, sc_val->length);
|
---|
| 281 | ret = EINVAL;
|
---|
| 282 | goto out;
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | if (scb.sub.signature != SUPPLEMENTAL_CREDENTIALS_SIGNATURE) {
|
---|
| 286 | NDR_PRINT_DEBUG(supplementalCredentialsBlob, &scb);
|
---|
| 287 | ret = EINVAL;
|
---|
| 288 | goto out;
|
---|
| 289 | }
|
---|
| 290 |
|
---|
| 291 | for (i=0; i < scb.sub.num_packages; i++) {
|
---|
| 292 | if (strcmp("Primary:Kerberos-Newer-Keys", scb.sub.packages[i].name) == 0) {
|
---|
| 293 | scpk = &scb.sub.packages[i];
|
---|
| 294 | if (!scpk->data || !scpk->data[0]) {
|
---|
| 295 | scpk = NULL;
|
---|
| 296 | continue;
|
---|
| 297 | }
|
---|
| 298 | newer_keys = true;
|
---|
| 299 | break;
|
---|
| 300 | } else if (strcmp("Primary:Kerberos", scb.sub.packages[i].name) == 0) {
|
---|
| 301 | scpk = &scb.sub.packages[i];
|
---|
| 302 | if (!scpk->data || !scpk->data[0]) {
|
---|
| 303 | scpk = NULL;
|
---|
| 304 | }
|
---|
| 305 | /*
|
---|
| 306 | * we don't break here in hope to find
|
---|
| 307 | * a Kerberos-Newer-Keys package
|
---|
| 308 | */
|
---|
| 309 | }
|
---|
| 310 | }
|
---|
| 311 | }
|
---|
| 312 | /*
|
---|
| 313 | * Primary:Kerberos-Newer-Keys or Primary:Kerberos element
|
---|
| 314 | * of supplementalCredentials
|
---|
| 315 | */
|
---|
| 316 | if (scpk) {
|
---|
| 317 | DATA_BLOB blob;
|
---|
| 318 |
|
---|
| 319 | blob = strhex_to_data_blob(mem_ctx, scpk->data);
|
---|
| 320 | if (!blob.data) {
|
---|
| 321 | ret = ENOMEM;
|
---|
| 322 | goto out;
|
---|
| 323 | }
|
---|
| 324 |
|
---|
| 325 | /* we cannot use ndr_pull_struct_blob_all() here, as w2k and w2k3 add padding bytes */
|
---|
| 326 | ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &_pkb,
|
---|
| 327 | (ndr_pull_flags_fn_t)ndr_pull_package_PrimaryKerberosBlob);
|
---|
| 328 | if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
|
---|
| 329 | ret = EINVAL;
|
---|
| 330 | krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
|
---|
| 331 | krb5_warnx(context, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
|
---|
| 332 | goto out;
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | if (newer_keys && _pkb.version != 4) {
|
---|
| 336 | ret = EINVAL;
|
---|
| 337 | krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
|
---|
| 338 | krb5_warnx(context, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
|
---|
| 339 | goto out;
|
---|
| 340 | }
|
---|
| 341 |
|
---|
| 342 | if (!newer_keys && _pkb.version != 3) {
|
---|
| 343 | ret = EINVAL;
|
---|
| 344 | krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
|
---|
| 345 | krb5_warnx(context, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
|
---|
| 346 | goto out;
|
---|
| 347 | }
|
---|
| 348 |
|
---|
| 349 | if (_pkb.version == 4) {
|
---|
| 350 | pkb4 = &_pkb.ctr.ctr4;
|
---|
| 351 | allocated_keys += pkb4->num_keys;
|
---|
| 352 | } else if (_pkb.version == 3) {
|
---|
| 353 | pkb3 = &_pkb.ctr.ctr3;
|
---|
| 354 | allocated_keys += pkb3->num_keys;
|
---|
| 355 | }
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | if (allocated_keys == 0) {
|
---|
| 359 | if (kdc_db_ctx->rodc) {
|
---|
| 360 | /* We are on an RODC, but don't have keys for this account. Signal this to the caller */
|
---|
| 361 | return HDB_ERR_NOT_FOUND_HERE;
|
---|
| 362 | }
|
---|
| 363 |
|
---|
| 364 | /* oh, no password. Apparently (comment in
|
---|
| 365 | * hdb-ldap.c) this violates the ASN.1, but this
|
---|
| 366 | * allows an entry with no keys (yet). */
|
---|
| 367 | return 0;
|
---|
| 368 | }
|
---|
| 369 |
|
---|
| 370 | /* allocate space to decode into */
|
---|
| 371 | entry_ex->entry.keys.len = 0;
|
---|
| 372 | entry_ex->entry.keys.val = calloc(allocated_keys, sizeof(Key));
|
---|
| 373 | if (entry_ex->entry.keys.val == NULL) {
|
---|
| 374 | ret = ENOMEM;
|
---|
| 375 | goto out;
|
---|
| 376 | }
|
---|
| 377 |
|
---|
| 378 | if (hash && (supported_enctypes & ENC_RC4_HMAC_MD5)) {
|
---|
| 379 | Key key;
|
---|
| 380 |
|
---|
| 381 | key.mkvno = 0;
|
---|
| 382 | key.salt = NULL; /* No salt for this enc type */
|
---|
| 383 |
|
---|
| 384 | ret = krb5_keyblock_init(context,
|
---|
| 385 | ENCTYPE_ARCFOUR_HMAC,
|
---|
| 386 | hash->hash, sizeof(hash->hash),
|
---|
| 387 | &key.key);
|
---|
| 388 | if (ret) {
|
---|
| 389 | goto out;
|
---|
| 390 | }
|
---|
| 391 |
|
---|
| 392 | entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
|
---|
| 393 | entry_ex->entry.keys.len++;
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 | if (pkb4) {
|
---|
| 397 | for (i=0; i < pkb4->num_keys; i++) {
|
---|
| 398 | Key key;
|
---|
| 399 |
|
---|
| 400 | if (!pkb4->keys[i].value) continue;
|
---|
| 401 |
|
---|
| 402 | if (!(kerberos_enctype_to_bitmap(pkb4->keys[i].keytype) & supported_enctypes)) {
|
---|
| 403 | continue;
|
---|
| 404 | }
|
---|
| 405 |
|
---|
| 406 | key.mkvno = 0;
|
---|
| 407 | key.salt = NULL;
|
---|
| 408 |
|
---|
| 409 | if (pkb4->salt.string) {
|
---|
| 410 | DATA_BLOB salt;
|
---|
| 411 |
|
---|
| 412 | salt = data_blob_string_const(pkb4->salt.string);
|
---|
| 413 |
|
---|
| 414 | key.salt = calloc(1, sizeof(*key.salt));
|
---|
| 415 | if (key.salt == NULL) {
|
---|
| 416 | ret = ENOMEM;
|
---|
| 417 | goto out;
|
---|
| 418 | }
|
---|
| 419 |
|
---|
| 420 | key.salt->type = hdb_pw_salt;
|
---|
| 421 |
|
---|
| 422 | ret = krb5_data_copy(&key.salt->salt, salt.data, salt.length);
|
---|
| 423 | if (ret) {
|
---|
| 424 | free(key.salt);
|
---|
| 425 | key.salt = NULL;
|
---|
| 426 | goto out;
|
---|
| 427 | }
|
---|
| 428 | }
|
---|
| 429 |
|
---|
| 430 | /* TODO: maybe pass the iteration_count somehow... */
|
---|
| 431 |
|
---|
| 432 | ret = krb5_keyblock_init(context,
|
---|
| 433 | pkb4->keys[i].keytype,
|
---|
| 434 | pkb4->keys[i].value->data,
|
---|
| 435 | pkb4->keys[i].value->length,
|
---|
| 436 | &key.key);
|
---|
| 437 | if (ret == KRB5_PROG_ETYPE_NOSUPP) {
|
---|
| 438 | DEBUG(2,("Unsupported keytype ignored - type %u\n",
|
---|
| 439 | pkb4->keys[i].keytype));
|
---|
| 440 | ret = 0;
|
---|
| 441 | continue;
|
---|
| 442 | }
|
---|
| 443 | if (ret) {
|
---|
| 444 | if (key.salt) {
|
---|
| 445 | free_Salt(key.salt);
|
---|
| 446 | free(key.salt);
|
---|
| 447 | key.salt = NULL;
|
---|
| 448 | }
|
---|
| 449 | goto out;
|
---|
| 450 | }
|
---|
| 451 |
|
---|
| 452 | entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
|
---|
| 453 | entry_ex->entry.keys.len++;
|
---|
| 454 | }
|
---|
| 455 | } else if (pkb3) {
|
---|
| 456 | for (i=0; i < pkb3->num_keys; i++) {
|
---|
| 457 | Key key;
|
---|
| 458 |
|
---|
| 459 | if (!pkb3->keys[i].value) continue;
|
---|
| 460 |
|
---|
| 461 | if (!(kerberos_enctype_to_bitmap(pkb3->keys[i].keytype) & supported_enctypes)) {
|
---|
| 462 | continue;
|
---|
| 463 | }
|
---|
| 464 |
|
---|
| 465 | key.mkvno = 0;
|
---|
| 466 | key.salt = NULL;
|
---|
| 467 |
|
---|
| 468 | if (pkb3->salt.string) {
|
---|
| 469 | DATA_BLOB salt;
|
---|
| 470 |
|
---|
| 471 | salt = data_blob_string_const(pkb3->salt.string);
|
---|
| 472 |
|
---|
| 473 | key.salt = calloc(1, sizeof(*key.salt));
|
---|
| 474 | if (key.salt == NULL) {
|
---|
| 475 | ret = ENOMEM;
|
---|
| 476 | goto out;
|
---|
| 477 | }
|
---|
| 478 |
|
---|
| 479 | key.salt->type = hdb_pw_salt;
|
---|
| 480 |
|
---|
| 481 | ret = krb5_data_copy(&key.salt->salt, salt.data, salt.length);
|
---|
| 482 | if (ret) {
|
---|
| 483 | free(key.salt);
|
---|
| 484 | key.salt = NULL;
|
---|
| 485 | goto out;
|
---|
| 486 | }
|
---|
| 487 | }
|
---|
| 488 |
|
---|
| 489 | ret = krb5_keyblock_init(context,
|
---|
| 490 | pkb3->keys[i].keytype,
|
---|
| 491 | pkb3->keys[i].value->data,
|
---|
| 492 | pkb3->keys[i].value->length,
|
---|
| 493 | &key.key);
|
---|
| 494 | if (ret) {
|
---|
| 495 | if (key.salt) {
|
---|
| 496 | free_Salt(key.salt);
|
---|
| 497 | free(key.salt);
|
---|
| 498 | key.salt = NULL;
|
---|
| 499 | }
|
---|
| 500 | goto out;
|
---|
| 501 | }
|
---|
| 502 |
|
---|
| 503 | entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
|
---|
| 504 | entry_ex->entry.keys.len++;
|
---|
| 505 | }
|
---|
| 506 | }
|
---|
| 507 |
|
---|
| 508 | out:
|
---|
| 509 | if (ret != 0) {
|
---|
| 510 | entry_ex->entry.keys.len = 0;
|
---|
| 511 | }
|
---|
| 512 | if (entry_ex->entry.keys.len == 0 && entry_ex->entry.keys.val) {
|
---|
| 513 | free(entry_ex->entry.keys.val);
|
---|
| 514 | entry_ex->entry.keys.val = NULL;
|
---|
| 515 | }
|
---|
| 516 | return ret;
|
---|
| 517 | }
|
---|
| 518 |
|
---|
| 519 | /*
|
---|
| 520 | * Construct an hdb_entry from a directory entry.
|
---|
| 521 | */
|
---|
| 522 | static krb5_error_code samba_kdc_message2entry(krb5_context context,
|
---|
| 523 | struct samba_kdc_db_context *kdc_db_ctx,
|
---|
| 524 | TALLOC_CTX *mem_ctx, krb5_const_principal principal,
|
---|
| 525 | enum samba_kdc_ent_type ent_type,
|
---|
| 526 | unsigned flags,
|
---|
| 527 | struct ldb_dn *realm_dn,
|
---|
| 528 | struct ldb_message *msg,
|
---|
| 529 | hdb_entry_ex *entry_ex)
|
---|
| 530 | {
|
---|
| 531 | struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
|
---|
| 532 | uint32_t userAccountControl;
|
---|
| 533 | unsigned int i;
|
---|
| 534 | krb5_error_code ret = 0;
|
---|
| 535 | krb5_boolean is_computer = FALSE;
|
---|
| 536 |
|
---|
| 537 | struct samba_kdc_entry *p;
|
---|
| 538 | NTTIME acct_expiry;
|
---|
| 539 | NTSTATUS status;
|
---|
| 540 |
|
---|
| 541 | uint32_t rid;
|
---|
| 542 | bool is_rodc = false;
|
---|
| 543 | struct ldb_message_element *objectclasses;
|
---|
| 544 | struct ldb_val computer_val;
|
---|
| 545 | const char *samAccountName = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
|
---|
| 546 | computer_val.data = discard_const_p(uint8_t,"computer");
|
---|
| 547 | computer_val.length = strlen((const char *)computer_val.data);
|
---|
| 548 |
|
---|
| 549 | if (ldb_msg_find_element(msg, "msDS-SecondaryKrbTgtNumber")) {
|
---|
| 550 | is_rodc = true;
|
---|
| 551 | }
|
---|
| 552 |
|
---|
| 553 | if (!samAccountName) {
|
---|
| 554 | ret = ENOENT;
|
---|
| 555 | krb5_set_error_message(context, ret, "samba_kdc_message2entry: no samAccountName present");
|
---|
| 556 | goto out;
|
---|
| 557 | }
|
---|
| 558 |
|
---|
| 559 | objectclasses = ldb_msg_find_element(msg, "objectClass");
|
---|
| 560 |
|
---|
| 561 | if (objectclasses && ldb_msg_find_val(objectclasses, &computer_val)) {
|
---|
| 562 | is_computer = TRUE;
|
---|
| 563 | }
|
---|
| 564 |
|
---|
| 565 | memset(entry_ex, 0, sizeof(*entry_ex));
|
---|
| 566 |
|
---|
| 567 | p = talloc(mem_ctx, struct samba_kdc_entry);
|
---|
| 568 | if (!p) {
|
---|
| 569 | ret = ENOMEM;
|
---|
| 570 | goto out;
|
---|
| 571 | }
|
---|
| 572 |
|
---|
| 573 | p->kdc_db_ctx = kdc_db_ctx;
|
---|
| 574 | p->entry_ex = entry_ex;
|
---|
| 575 | p->realm_dn = talloc_reference(p, realm_dn);
|
---|
| 576 | if (!p->realm_dn) {
|
---|
| 577 | ret = ENOMEM;
|
---|
| 578 | goto out;
|
---|
| 579 | }
|
---|
| 580 |
|
---|
| 581 | talloc_set_destructor(p, samba_kdc_entry_destructor);
|
---|
| 582 |
|
---|
| 583 | /* make sure we do not have bogus data in there */
|
---|
| 584 | memset(&entry_ex->entry, 0, sizeof(hdb_entry));
|
---|
| 585 |
|
---|
| 586 | entry_ex->ctx = p;
|
---|
| 587 | entry_ex->free_entry = samba_kdc_free_entry;
|
---|
| 588 |
|
---|
| 589 | userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
|
---|
| 590 |
|
---|
| 591 |
|
---|
| 592 | entry_ex->entry.principal = malloc(sizeof(*(entry_ex->entry.principal)));
|
---|
| 593 | if (ent_type == SAMBA_KDC_ENT_TYPE_ANY && principal == NULL) {
|
---|
| 594 | krb5_make_principal(context, &entry_ex->entry.principal, lpcfg_realm(lp_ctx), samAccountName, NULL);
|
---|
| 595 | } else {
|
---|
| 596 | ret = copy_Principal(principal, entry_ex->entry.principal);
|
---|
| 597 | if (ret) {
|
---|
| 598 | krb5_clear_error_message(context);
|
---|
| 599 | goto out;
|
---|
| 600 | }
|
---|
| 601 |
|
---|
| 602 | /* While we have copied the client principal, tests
|
---|
| 603 | * show that Win2k3 returns the 'corrected' realm, not
|
---|
| 604 | * the client-specified realm. This code attempts to
|
---|
| 605 | * replace the client principal's realm with the one
|
---|
| 606 | * we determine from our records */
|
---|
| 607 |
|
---|
| 608 | /* this has to be with malloc() */
|
---|
| 609 | krb5_principal_set_realm(context, entry_ex->entry.principal, lpcfg_realm(lp_ctx));
|
---|
| 610 | }
|
---|
| 611 |
|
---|
| 612 | /* First try and figure out the flags based on the userAccountControl */
|
---|
| 613 | entry_ex->entry.flags = uf2HDBFlags(context, userAccountControl, ent_type);
|
---|
| 614 |
|
---|
| 615 | /* Windows 2008 seems to enforce this (very sensible) rule by
|
---|
| 616 | * default - don't allow offline attacks on a user's password
|
---|
| 617 | * by asking for a ticket to them as a service (encrypted with
|
---|
| 618 | * their probably patheticly insecure password) */
|
---|
| 619 |
|
---|
| 620 | if (entry_ex->entry.flags.server
|
---|
| 621 | && lpcfg_parm_bool(lp_ctx, NULL, "kdc", "require spn for service", true)) {
|
---|
| 622 | if (!is_computer && !ldb_msg_find_attr_as_string(msg, "servicePrincipalName", NULL)) {
|
---|
| 623 | entry_ex->entry.flags.server = 0;
|
---|
| 624 | }
|
---|
| 625 | }
|
---|
| 626 |
|
---|
| 627 | if (flags & HDB_F_ADMIN_DATA) {
|
---|
| 628 | /* These (created_by, modified_by) parts of the entry are not relevant for Samba4's use
|
---|
| 629 | * of the Heimdal KDC. They are stored in a the traditional
|
---|
| 630 | * DB for audit purposes, and still form part of the structure
|
---|
| 631 | * we must return */
|
---|
| 632 |
|
---|
| 633 | /* use 'whenCreated' */
|
---|
| 634 | entry_ex->entry.created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
|
---|
| 635 | /* use 'kadmin' for now (needed by mit_samba) */
|
---|
| 636 | krb5_make_principal(context,
|
---|
| 637 | &entry_ex->entry.created_by.principal,
|
---|
| 638 | lpcfg_realm(lp_ctx), "kadmin", NULL);
|
---|
| 639 |
|
---|
| 640 | entry_ex->entry.modified_by = (Event *) malloc(sizeof(Event));
|
---|
| 641 | if (entry_ex->entry.modified_by == NULL) {
|
---|
| 642 | ret = ENOMEM;
|
---|
| 643 | krb5_set_error_message(context, ret, "malloc: out of memory");
|
---|
| 644 | goto out;
|
---|
| 645 | }
|
---|
| 646 |
|
---|
| 647 | /* use 'whenChanged' */
|
---|
| 648 | entry_ex->entry.modified_by->time = ldb_msg_find_krb5time_ldap_time(msg, "whenChanged", 0);
|
---|
| 649 | /* use 'kadmin' for now (needed by mit_samba) */
|
---|
| 650 | krb5_make_principal(context,
|
---|
| 651 | &entry_ex->entry.modified_by->principal,
|
---|
| 652 | lpcfg_realm(lp_ctx), "kadmin", NULL);
|
---|
| 653 | }
|
---|
| 654 |
|
---|
| 655 |
|
---|
| 656 | /* The lack of password controls etc applies to krbtgt by
|
---|
| 657 | * virtue of being that particular RID */
|
---|
| 658 | status = dom_sid_split_rid(NULL, samdb_result_dom_sid(mem_ctx, msg, "objectSid"), NULL, &rid);
|
---|
| 659 |
|
---|
| 660 | if (!NT_STATUS_IS_OK(status)) {
|
---|
| 661 | ret = EINVAL;
|
---|
| 662 | goto out;
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 | if (rid == DOMAIN_RID_KRBTGT) {
|
---|
| 666 | entry_ex->entry.valid_end = NULL;
|
---|
| 667 | entry_ex->entry.pw_end = NULL;
|
---|
| 668 |
|
---|
| 669 | entry_ex->entry.flags.invalid = 0;
|
---|
| 670 | entry_ex->entry.flags.server = 1;
|
---|
| 671 |
|
---|
| 672 | /* Don't mark all requests for the krbtgt/realm as
|
---|
| 673 | * 'change password', as otherwise we could get into
|
---|
| 674 | * trouble, and not enforce the password expirty.
|
---|
| 675 | * Instead, only do it when request is for the kpasswd service */
|
---|
| 676 | if (ent_type == SAMBA_KDC_ENT_TYPE_SERVER
|
---|
| 677 | && principal->name.name_string.len == 2
|
---|
| 678 | && (strcmp(principal->name.name_string.val[0], "kadmin") == 0)
|
---|
| 679 | && (strcmp(principal->name.name_string.val[1], "changepw") == 0)
|
---|
| 680 | && lpcfg_is_my_domain_or_realm(lp_ctx, principal->realm)) {
|
---|
| 681 | entry_ex->entry.flags.change_pw = 1;
|
---|
| 682 | }
|
---|
| 683 | entry_ex->entry.flags.client = 0;
|
---|
| 684 | entry_ex->entry.flags.forwardable = 1;
|
---|
| 685 | entry_ex->entry.flags.ok_as_delegate = 1;
|
---|
| 686 | } else if (is_rodc) {
|
---|
| 687 | /* The RODC krbtgt account is like the main krbtgt,
|
---|
| 688 | * but it does not have a changepw or kadmin
|
---|
| 689 | * service */
|
---|
| 690 |
|
---|
| 691 | entry_ex->entry.valid_end = NULL;
|
---|
| 692 | entry_ex->entry.pw_end = NULL;
|
---|
| 693 |
|
---|
| 694 | /* Also don't allow the RODC krbtgt to be a client (it should not be needed) */
|
---|
| 695 | entry_ex->entry.flags.client = 0;
|
---|
| 696 | entry_ex->entry.flags.invalid = 0;
|
---|
| 697 | entry_ex->entry.flags.server = 1;
|
---|
| 698 |
|
---|
| 699 | entry_ex->entry.flags.client = 0;
|
---|
| 700 | entry_ex->entry.flags.forwardable = 1;
|
---|
| 701 | entry_ex->entry.flags.ok_as_delegate = 0;
|
---|
| 702 | } else if (entry_ex->entry.flags.server && ent_type == SAMBA_KDC_ENT_TYPE_SERVER) {
|
---|
| 703 | /* The account/password expiry only applies when the account is used as a
|
---|
| 704 | * client (ie password login), not when used as a server */
|
---|
| 705 |
|
---|
| 706 | /* Make very well sure we don't use this for a client,
|
---|
| 707 | * it could bypass the password restrictions */
|
---|
| 708 | entry_ex->entry.flags.client = 0;
|
---|
| 709 |
|
---|
| 710 | entry_ex->entry.valid_end = NULL;
|
---|
| 711 | entry_ex->entry.pw_end = NULL;
|
---|
| 712 |
|
---|
| 713 | } else {
|
---|
| 714 | NTTIME must_change_time
|
---|
| 715 | = samdb_result_force_password_change(kdc_db_ctx->samdb, mem_ctx,
|
---|
| 716 | realm_dn, msg);
|
---|
| 717 | if (must_change_time == 0x7FFFFFFFFFFFFFFFULL) {
|
---|
| 718 | entry_ex->entry.pw_end = NULL;
|
---|
| 719 | } else {
|
---|
| 720 | entry_ex->entry.pw_end = malloc(sizeof(*entry_ex->entry.pw_end));
|
---|
| 721 | if (entry_ex->entry.pw_end == NULL) {
|
---|
| 722 | ret = ENOMEM;
|
---|
| 723 | goto out;
|
---|
| 724 | }
|
---|
| 725 | *entry_ex->entry.pw_end = nt_time_to_unix(must_change_time);
|
---|
| 726 | }
|
---|
| 727 |
|
---|
| 728 | acct_expiry = samdb_result_account_expires(msg);
|
---|
| 729 | if (acct_expiry == 0x7FFFFFFFFFFFFFFFULL) {
|
---|
| 730 | entry_ex->entry.valid_end = NULL;
|
---|
| 731 | } else {
|
---|
| 732 | entry_ex->entry.valid_end = malloc(sizeof(*entry_ex->entry.valid_end));
|
---|
| 733 | if (entry_ex->entry.valid_end == NULL) {
|
---|
| 734 | ret = ENOMEM;
|
---|
| 735 | goto out;
|
---|
| 736 | }
|
---|
| 737 | *entry_ex->entry.valid_end = nt_time_to_unix(acct_expiry);
|
---|
| 738 | }
|
---|
| 739 | }
|
---|
| 740 |
|
---|
| 741 | entry_ex->entry.valid_start = NULL;
|
---|
| 742 |
|
---|
| 743 | entry_ex->entry.max_life = malloc(sizeof(*entry_ex->entry.max_life));
|
---|
| 744 | if (entry_ex->entry.max_life == NULL) {
|
---|
| 745 | ret = ENOMEM;
|
---|
| 746 | goto out;
|
---|
| 747 | }
|
---|
| 748 |
|
---|
| 749 | if (ent_type == SAMBA_KDC_ENT_TYPE_SERVER) {
|
---|
| 750 | *entry_ex->entry.max_life = nt_time_to_unix(kdc_db_ctx->policy.service_tkt_lifetime);
|
---|
| 751 | } else if (ent_type == SAMBA_KDC_ENT_TYPE_KRBTGT || ent_type == SAMBA_KDC_ENT_TYPE_CLIENT) {
|
---|
| 752 | *entry_ex->entry.max_life = nt_time_to_unix(kdc_db_ctx->policy.user_tkt_lifetime);
|
---|
| 753 | } else {
|
---|
| 754 | *entry_ex->entry.max_life = MIN(nt_time_to_unix(kdc_db_ctx->policy.service_tkt_lifetime),
|
---|
| 755 | nt_time_to_unix(kdc_db_ctx->policy.user_tkt_lifetime));
|
---|
| 756 | }
|
---|
| 757 |
|
---|
| 758 | entry_ex->entry.max_renew = malloc(sizeof(*entry_ex->entry.max_life));
|
---|
| 759 | if (entry_ex->entry.max_renew == NULL) {
|
---|
| 760 | ret = ENOMEM;
|
---|
| 761 | goto out;
|
---|
| 762 | }
|
---|
| 763 |
|
---|
| 764 | *entry_ex->entry.max_renew = nt_time_to_unix(kdc_db_ctx->policy.user_tkt_renewaltime);
|
---|
| 765 |
|
---|
| 766 | entry_ex->entry.generation = NULL;
|
---|
| 767 |
|
---|
| 768 | /* Get keys from the db */
|
---|
| 769 | ret = samba_kdc_message2entry_keys(context, kdc_db_ctx, p, msg,
|
---|
| 770 | rid, is_rodc, userAccountControl,
|
---|
| 771 | ent_type, entry_ex);
|
---|
| 772 | if (ret) {
|
---|
| 773 | /* Could be bougus data in the entry, or out of memory */
|
---|
| 774 | goto out;
|
---|
| 775 | }
|
---|
| 776 |
|
---|
| 777 | entry_ex->entry.etypes = malloc(sizeof(*(entry_ex->entry.etypes)));
|
---|
| 778 | if (entry_ex->entry.etypes == NULL) {
|
---|
| 779 | krb5_clear_error_message(context);
|
---|
| 780 | ret = ENOMEM;
|
---|
| 781 | goto out;
|
---|
| 782 | }
|
---|
| 783 | entry_ex->entry.etypes->len = entry_ex->entry.keys.len;
|
---|
| 784 | entry_ex->entry.etypes->val = calloc(entry_ex->entry.etypes->len, sizeof(int));
|
---|
| 785 | if (entry_ex->entry.etypes->val == NULL) {
|
---|
| 786 | krb5_clear_error_message(context);
|
---|
| 787 | ret = ENOMEM;
|
---|
| 788 | goto out;
|
---|
| 789 | }
|
---|
| 790 | for (i=0; i < entry_ex->entry.etypes->len; i++) {
|
---|
| 791 | entry_ex->entry.etypes->val[i] = entry_ex->entry.keys.val[i].key.keytype;
|
---|
| 792 | }
|
---|
| 793 |
|
---|
| 794 |
|
---|
| 795 | p->msg = talloc_steal(p, msg);
|
---|
| 796 |
|
---|
| 797 | out:
|
---|
| 798 | if (ret != 0) {
|
---|
| 799 | /* This doesn't free ent itself, that is for the eventual caller to do */
|
---|
| 800 | hdb_free_entry(context, entry_ex);
|
---|
| 801 | } else {
|
---|
| 802 | talloc_steal(kdc_db_ctx, entry_ex->ctx);
|
---|
| 803 | }
|
---|
| 804 |
|
---|
| 805 | return ret;
|
---|
| 806 | }
|
---|
| 807 |
|
---|
| 808 | /*
|
---|
| 809 | * Construct an hdb_entry from a directory entry.
|
---|
| 810 | */
|
---|
| 811 | static krb5_error_code samba_kdc_trust_message2entry(krb5_context context,
|
---|
| 812 | struct samba_kdc_db_context *kdc_db_ctx,
|
---|
| 813 | TALLOC_CTX *mem_ctx, krb5_const_principal principal,
|
---|
| 814 | enum trust_direction direction,
|
---|
| 815 | struct ldb_dn *realm_dn,
|
---|
| 816 | struct ldb_message *msg,
|
---|
| 817 | hdb_entry_ex *entry_ex)
|
---|
| 818 | {
|
---|
| 819 | struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
|
---|
| 820 | const char *dnsdomain;
|
---|
| 821 | const char *realm = lpcfg_realm(lp_ctx);
|
---|
| 822 | DATA_BLOB password_utf16;
|
---|
| 823 | struct samr_Password password_hash;
|
---|
| 824 | const struct ldb_val *password_val;
|
---|
| 825 | struct trustAuthInOutBlob password_blob;
|
---|
| 826 | struct samba_kdc_entry *p;
|
---|
| 827 |
|
---|
| 828 | enum ndr_err_code ndr_err;
|
---|
| 829 | int ret, trust_direction_flags;
|
---|
| 830 | unsigned int i;
|
---|
| 831 |
|
---|
| 832 | p = talloc(mem_ctx, struct samba_kdc_entry);
|
---|
| 833 | if (!p) {
|
---|
| 834 | ret = ENOMEM;
|
---|
| 835 | goto out;
|
---|
| 836 | }
|
---|
| 837 |
|
---|
| 838 | p->kdc_db_ctx = kdc_db_ctx;
|
---|
| 839 | p->entry_ex = entry_ex;
|
---|
| 840 | p->realm_dn = realm_dn;
|
---|
| 841 |
|
---|
| 842 | talloc_set_destructor(p, samba_kdc_entry_destructor);
|
---|
| 843 |
|
---|
| 844 | /* make sure we do not have bogus data in there */
|
---|
| 845 | memset(&entry_ex->entry, 0, sizeof(hdb_entry));
|
---|
| 846 |
|
---|
| 847 | entry_ex->ctx = p;
|
---|
| 848 | entry_ex->free_entry = samba_kdc_free_entry;
|
---|
| 849 |
|
---|
| 850 | /* use 'whenCreated' */
|
---|
| 851 | entry_ex->entry.created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
|
---|
| 852 | /* use 'kadmin' for now (needed by mit_samba) */
|
---|
| 853 | krb5_make_principal(context,
|
---|
| 854 | &entry_ex->entry.created_by.principal,
|
---|
| 855 | realm, "kadmin", NULL);
|
---|
| 856 |
|
---|
| 857 | entry_ex->entry.valid_start = NULL;
|
---|
| 858 |
|
---|
| 859 | trust_direction_flags = ldb_msg_find_attr_as_int(msg, "trustDirection", 0);
|
---|
| 860 |
|
---|
| 861 | if (direction == INBOUND) {
|
---|
| 862 | password_val = ldb_msg_find_ldb_val(msg, "trustAuthIncoming");
|
---|
| 863 |
|
---|
| 864 | } else { /* OUTBOUND */
|
---|
| 865 | dnsdomain = ldb_msg_find_attr_as_string(msg, "trustPartner", NULL);
|
---|
| 866 | /* replace realm */
|
---|
| 867 | realm = strupper_talloc(mem_ctx, dnsdomain);
|
---|
| 868 | password_val = ldb_msg_find_ldb_val(msg, "trustAuthOutgoing");
|
---|
| 869 | }
|
---|
| 870 |
|
---|
| 871 | if (!password_val || !(trust_direction_flags & direction)) {
|
---|
| 872 | ret = ENOENT;
|
---|
| 873 | goto out;
|
---|
| 874 | }
|
---|
| 875 |
|
---|
| 876 | ndr_err = ndr_pull_struct_blob(password_val, mem_ctx, &password_blob,
|
---|
| 877 | (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
|
---|
| 878 | if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
|
---|
| 879 | ret = EINVAL;
|
---|
| 880 | goto out;
|
---|
| 881 | }
|
---|
| 882 |
|
---|
| 883 | entry_ex->entry.kvno = -1;
|
---|
| 884 | for (i=0; i < password_blob.count; i++) {
|
---|
| 885 | if (password_blob.current.array[i].AuthType == TRUST_AUTH_TYPE_VERSION) {
|
---|
| 886 | entry_ex->entry.kvno = password_blob.current.array[i].AuthInfo.version.version;
|
---|
| 887 | }
|
---|
| 888 | }
|
---|
| 889 |
|
---|
| 890 | for (i=0; i < password_blob.count; i++) {
|
---|
| 891 | if (password_blob.current.array[i].AuthType == TRUST_AUTH_TYPE_CLEAR) {
|
---|
| 892 | password_utf16 = data_blob_const(password_blob.current.array[i].AuthInfo.clear.password,
|
---|
| 893 | password_blob.current.array[i].AuthInfo.clear.size);
|
---|
| 894 | /* In the future, generate all sorts of
|
---|
| 895 | * hashes, but for now we can't safely convert
|
---|
| 896 | * the random strings windows uses into
|
---|
| 897 | * utf8 */
|
---|
| 898 |
|
---|
| 899 | /* but as it is utf16 already, we can get the NT password/arcfour-hmac-md5 key */
|
---|
| 900 | mdfour(password_hash.hash, password_utf16.data, password_utf16.length);
|
---|
| 901 | break;
|
---|
| 902 | } else if (password_blob.current.array[i].AuthType == TRUST_AUTH_TYPE_NT4OWF) {
|
---|
| 903 | password_hash = password_blob.current.array[i].AuthInfo.nt4owf.password;
|
---|
| 904 | break;
|
---|
| 905 | }
|
---|
| 906 | }
|
---|
| 907 |
|
---|
| 908 | if (i < password_blob.count) {
|
---|
| 909 | Key key;
|
---|
| 910 | /* Must have found a cleartext or MD4 password */
|
---|
| 911 | entry_ex->entry.keys.val = calloc(1, sizeof(Key));
|
---|
| 912 |
|
---|
| 913 | key.mkvno = 0;
|
---|
| 914 | key.salt = NULL; /* No salt for this enc type */
|
---|
| 915 |
|
---|
| 916 | if (entry_ex->entry.keys.val == NULL) {
|
---|
| 917 | ret = ENOMEM;
|
---|
| 918 | goto out;
|
---|
| 919 | }
|
---|
| 920 |
|
---|
| 921 | ret = krb5_keyblock_init(context,
|
---|
| 922 | ENCTYPE_ARCFOUR_HMAC,
|
---|
| 923 | password_hash.hash, sizeof(password_hash.hash),
|
---|
| 924 | &key.key);
|
---|
| 925 |
|
---|
| 926 | entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
|
---|
| 927 | entry_ex->entry.keys.len++;
|
---|
| 928 | }
|
---|
| 929 |
|
---|
| 930 | entry_ex->entry.principal = malloc(sizeof(*(entry_ex->entry.principal)));
|
---|
| 931 |
|
---|
| 932 | ret = copy_Principal(principal, entry_ex->entry.principal);
|
---|
| 933 | if (ret) {
|
---|
| 934 | krb5_clear_error_message(context);
|
---|
| 935 | goto out;
|
---|
| 936 | }
|
---|
| 937 |
|
---|
| 938 | /* While we have copied the client principal, tests
|
---|
| 939 | * show that Win2k3 returns the 'corrected' realm, not
|
---|
| 940 | * the client-specified realm. This code attempts to
|
---|
| 941 | * replace the client principal's realm with the one
|
---|
| 942 | * we determine from our records */
|
---|
| 943 |
|
---|
| 944 | krb5_principal_set_realm(context, entry_ex->entry.principal, realm);
|
---|
| 945 | entry_ex->entry.flags = int2HDBFlags(0);
|
---|
| 946 | entry_ex->entry.flags.immutable = 1;
|
---|
| 947 | entry_ex->entry.flags.invalid = 0;
|
---|
| 948 | entry_ex->entry.flags.server = 1;
|
---|
| 949 | entry_ex->entry.flags.require_preauth = 1;
|
---|
| 950 |
|
---|
| 951 | entry_ex->entry.pw_end = NULL;
|
---|
| 952 |
|
---|
| 953 | entry_ex->entry.max_life = NULL;
|
---|
| 954 |
|
---|
| 955 | entry_ex->entry.max_renew = NULL;
|
---|
| 956 |
|
---|
| 957 | entry_ex->entry.generation = NULL;
|
---|
| 958 |
|
---|
| 959 | entry_ex->entry.etypes = malloc(sizeof(*(entry_ex->entry.etypes)));
|
---|
| 960 | if (entry_ex->entry.etypes == NULL) {
|
---|
| 961 | krb5_clear_error_message(context);
|
---|
| 962 | ret = ENOMEM;
|
---|
| 963 | goto out;
|
---|
| 964 | }
|
---|
| 965 | entry_ex->entry.etypes->len = entry_ex->entry.keys.len;
|
---|
| 966 | entry_ex->entry.etypes->val = calloc(entry_ex->entry.etypes->len, sizeof(int));
|
---|
| 967 | if (entry_ex->entry.etypes->val == NULL) {
|
---|
| 968 | krb5_clear_error_message(context);
|
---|
| 969 | ret = ENOMEM;
|
---|
| 970 | goto out;
|
---|
| 971 | }
|
---|
| 972 | for (i=0; i < entry_ex->entry.etypes->len; i++) {
|
---|
| 973 | entry_ex->entry.etypes->val[i] = entry_ex->entry.keys.val[i].key.keytype;
|
---|
| 974 | }
|
---|
| 975 |
|
---|
| 976 |
|
---|
| 977 | p->msg = talloc_steal(p, msg);
|
---|
| 978 |
|
---|
| 979 | out:
|
---|
| 980 | if (ret != 0) {
|
---|
| 981 | /* This doesn't free ent itself, that is for the eventual caller to do */
|
---|
| 982 | hdb_free_entry(context, entry_ex);
|
---|
| 983 | } else {
|
---|
| 984 | talloc_steal(kdc_db_ctx, entry_ex->ctx);
|
---|
| 985 | }
|
---|
| 986 |
|
---|
| 987 | return ret;
|
---|
| 988 |
|
---|
| 989 | }
|
---|
| 990 |
|
---|
| 991 | static krb5_error_code samba_kdc_lookup_trust(krb5_context context, struct ldb_context *ldb_ctx,
|
---|
| 992 | TALLOC_CTX *mem_ctx,
|
---|
| 993 | const char *realm,
|
---|
| 994 | struct ldb_dn *realm_dn,
|
---|
| 995 | struct ldb_message **pmsg)
|
---|
| 996 | {
|
---|
| 997 | int lret;
|
---|
| 998 | krb5_error_code ret;
|
---|
| 999 | char *filter = NULL;
|
---|
| 1000 | const char * const *attrs = trust_attrs;
|
---|
| 1001 |
|
---|
| 1002 | struct ldb_result *res = NULL;
|
---|
| 1003 | filter = talloc_asprintf(mem_ctx, "(&(objectClass=trustedDomain)(|(flatname=%s)(trustPartner=%s)))", realm, realm);
|
---|
| 1004 |
|
---|
| 1005 | if (!filter) {
|
---|
| 1006 | ret = ENOMEM;
|
---|
| 1007 | krb5_set_error_message(context, ret, "talloc_asprintf: out of memory");
|
---|
| 1008 | return ret;
|
---|
| 1009 | }
|
---|
| 1010 |
|
---|
| 1011 | lret = ldb_search(ldb_ctx, mem_ctx, &res,
|
---|
| 1012 | ldb_get_default_basedn(ldb_ctx),
|
---|
| 1013 | LDB_SCOPE_SUBTREE, attrs, "%s", filter);
|
---|
| 1014 | if (lret != LDB_SUCCESS) {
|
---|
| 1015 | DEBUG(3, ("Failed to search for %s: %s\n", filter, ldb_errstring(ldb_ctx)));
|
---|
| 1016 | return HDB_ERR_NOENTRY;
|
---|
| 1017 | } else if (res->count == 0 || res->count > 1) {
|
---|
| 1018 | DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter, res->count));
|
---|
| 1019 | talloc_free(res);
|
---|
| 1020 | return HDB_ERR_NOENTRY;
|
---|
| 1021 | }
|
---|
| 1022 | talloc_steal(mem_ctx, res->msgs);
|
---|
| 1023 | *pmsg = res->msgs[0];
|
---|
| 1024 | talloc_free(res);
|
---|
| 1025 | return 0;
|
---|
| 1026 | }
|
---|
| 1027 |
|
---|
| 1028 | static krb5_error_code samba_kdc_lookup_client(krb5_context context,
|
---|
| 1029 | struct samba_kdc_db_context *kdc_db_ctx,
|
---|
| 1030 | TALLOC_CTX *mem_ctx,
|
---|
| 1031 | krb5_const_principal principal,
|
---|
| 1032 | const char **attrs,
|
---|
| 1033 | struct ldb_dn **realm_dn,
|
---|
| 1034 | struct ldb_message **msg) {
|
---|
| 1035 | NTSTATUS nt_status;
|
---|
| 1036 | char *principal_string;
|
---|
| 1037 | krb5_error_code ret;
|
---|
| 1038 |
|
---|
| 1039 | ret = krb5_unparse_name(context, principal, &principal_string);
|
---|
| 1040 |
|
---|
| 1041 | if (ret != 0) {
|
---|
| 1042 | return ret;
|
---|
| 1043 | }
|
---|
| 1044 |
|
---|
| 1045 | nt_status = sam_get_results_principal(kdc_db_ctx->samdb,
|
---|
| 1046 | mem_ctx, principal_string, attrs,
|
---|
| 1047 | realm_dn, msg);
|
---|
| 1048 | free(principal_string);
|
---|
| 1049 | if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
|
---|
| 1050 | return HDB_ERR_NOENTRY;
|
---|
| 1051 | } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
|
---|
| 1052 | return ENOMEM;
|
---|
| 1053 | } else if (!NT_STATUS_IS_OK(nt_status)) {
|
---|
| 1054 | return EINVAL;
|
---|
| 1055 | }
|
---|
| 1056 |
|
---|
| 1057 | return ret;
|
---|
| 1058 | }
|
---|
| 1059 |
|
---|
| 1060 | static krb5_error_code samba_kdc_fetch_client(krb5_context context,
|
---|
| 1061 | struct samba_kdc_db_context *kdc_db_ctx,
|
---|
| 1062 | TALLOC_CTX *mem_ctx,
|
---|
| 1063 | krb5_const_principal principal,
|
---|
| 1064 | unsigned flags,
|
---|
| 1065 | hdb_entry_ex *entry_ex) {
|
---|
| 1066 | struct ldb_dn *realm_dn;
|
---|
| 1067 | krb5_error_code ret;
|
---|
| 1068 | struct ldb_message *msg = NULL;
|
---|
| 1069 |
|
---|
| 1070 | ret = samba_kdc_lookup_client(context, kdc_db_ctx,
|
---|
| 1071 | mem_ctx, principal, user_attrs,
|
---|
| 1072 | &realm_dn, &msg);
|
---|
| 1073 | if (ret != 0) {
|
---|
| 1074 | return ret;
|
---|
| 1075 | }
|
---|
| 1076 |
|
---|
| 1077 | ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
|
---|
| 1078 | principal, SAMBA_KDC_ENT_TYPE_CLIENT,
|
---|
| 1079 | flags,
|
---|
| 1080 | realm_dn, msg, entry_ex);
|
---|
| 1081 | return ret;
|
---|
| 1082 | }
|
---|
| 1083 |
|
---|
| 1084 | static krb5_error_code samba_kdc_fetch_krbtgt(krb5_context context,
|
---|
| 1085 | struct samba_kdc_db_context *kdc_db_ctx,
|
---|
| 1086 | TALLOC_CTX *mem_ctx,
|
---|
| 1087 | krb5_const_principal principal,
|
---|
| 1088 | unsigned flags,
|
---|
| 1089 | uint32_t krbtgt_number,
|
---|
| 1090 | hdb_entry_ex *entry_ex)
|
---|
| 1091 | {
|
---|
| 1092 | struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
|
---|
| 1093 | krb5_error_code ret;
|
---|
| 1094 | struct ldb_message *msg = NULL;
|
---|
| 1095 | struct ldb_dn *realm_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
|
---|
| 1096 |
|
---|
| 1097 | krb5_principal alloc_principal = NULL;
|
---|
| 1098 | if (principal->name.name_string.len != 2
|
---|
| 1099 | || (strcmp(principal->name.name_string.val[0], KRB5_TGS_NAME) != 0)) {
|
---|
| 1100 | /* Not a krbtgt */
|
---|
| 1101 | return HDB_ERR_NOENTRY;
|
---|
| 1102 | }
|
---|
| 1103 |
|
---|
| 1104 | /* krbtgt case. Either us or a trusted realm */
|
---|
| 1105 |
|
---|
| 1106 | if (lpcfg_is_my_domain_or_realm(lp_ctx, principal->realm)
|
---|
| 1107 | && lpcfg_is_my_domain_or_realm(lp_ctx, principal->name.name_string.val[1])) {
|
---|
| 1108 | /* us, or someone quite like us */
|
---|
| 1109 | /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
|
---|
| 1110 | * is in our db, then direct the caller at our primary
|
---|
| 1111 | * krbtgt */
|
---|
| 1112 |
|
---|
| 1113 | int lret;
|
---|
| 1114 |
|
---|
| 1115 | if (krbtgt_number == kdc_db_ctx->my_krbtgt_number) {
|
---|
| 1116 | lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx,
|
---|
| 1117 | &msg, kdc_db_ctx->krbtgt_dn, LDB_SCOPE_BASE,
|
---|
| 1118 | krbtgt_attrs, 0,
|
---|
| 1119 | "(objectClass=user)");
|
---|
| 1120 | } else {
|
---|
| 1121 | /* We need to look up an RODC krbtgt (perhaps
|
---|
| 1122 | * ours, if we are an RODC, perhaps another
|
---|
| 1123 | * RODC if we are a read-write DC */
|
---|
| 1124 | lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx,
|
---|
| 1125 | &msg, realm_dn, LDB_SCOPE_SUBTREE,
|
---|
| 1126 | krbtgt_attrs,
|
---|
| 1127 | DSDB_SEARCH_SHOW_EXTENDED_DN,
|
---|
| 1128 | "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=%u))", (unsigned)(krbtgt_number));
|
---|
| 1129 | }
|
---|
| 1130 |
|
---|
| 1131 | if (lret == LDB_ERR_NO_SUCH_OBJECT) {
|
---|
| 1132 | krb5_warnx(context, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
|
---|
| 1133 | (unsigned)(krbtgt_number));
|
---|
| 1134 | krb5_set_error_message(context, HDB_ERR_NOENTRY,
|
---|
| 1135 | "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
|
---|
| 1136 | (unsigned)(krbtgt_number));
|
---|
| 1137 | return HDB_ERR_NOENTRY;
|
---|
| 1138 | } else if (lret != LDB_SUCCESS) {
|
---|
| 1139 | krb5_warnx(context, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
|
---|
| 1140 | (unsigned)(krbtgt_number));
|
---|
| 1141 | krb5_set_error_message(context, HDB_ERR_NOENTRY,
|
---|
| 1142 | "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
|
---|
| 1143 | (unsigned)(krbtgt_number));
|
---|
| 1144 | return HDB_ERR_NOENTRY;
|
---|
| 1145 | }
|
---|
| 1146 |
|
---|
| 1147 | if (flags & HDB_F_CANON) {
|
---|
| 1148 | ret = krb5_copy_principal(context, principal, &alloc_principal);
|
---|
| 1149 | if (ret) {
|
---|
| 1150 | return ret;
|
---|
| 1151 | }
|
---|
| 1152 |
|
---|
| 1153 | /* When requested to do so, ensure that the
|
---|
| 1154 | * both realm values in the principal are set
|
---|
| 1155 | * to the upper case, canonical realm */
|
---|
| 1156 | free(alloc_principal->name.name_string.val[1]);
|
---|
| 1157 | alloc_principal->name.name_string.val[1] = strdup(lpcfg_realm(lp_ctx));
|
---|
| 1158 | if (!alloc_principal->name.name_string.val[1]) {
|
---|
| 1159 | ret = ENOMEM;
|
---|
| 1160 | krb5_set_error_message(context, ret, "samba_kdc_fetch: strdup() failed!");
|
---|
| 1161 | return ret;
|
---|
| 1162 | }
|
---|
| 1163 | principal = alloc_principal;
|
---|
| 1164 | }
|
---|
| 1165 |
|
---|
| 1166 | ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
|
---|
| 1167 | principal, SAMBA_KDC_ENT_TYPE_KRBTGT,
|
---|
| 1168 | flags, realm_dn, msg, entry_ex);
|
---|
| 1169 | if (flags & HDB_F_CANON) {
|
---|
| 1170 | /* This is again copied in the message2entry call */
|
---|
| 1171 | krb5_free_principal(context, alloc_principal);
|
---|
| 1172 | }
|
---|
| 1173 | if (ret != 0) {
|
---|
| 1174 | krb5_warnx(context, "samba_kdc_fetch: self krbtgt message2entry failed");
|
---|
| 1175 | }
|
---|
| 1176 | return ret;
|
---|
| 1177 |
|
---|
| 1178 | } else {
|
---|
| 1179 | enum trust_direction direction = UNKNOWN;
|
---|
| 1180 | const char *realm = NULL;
|
---|
| 1181 |
|
---|
| 1182 | /* Either an inbound or outbound trust */
|
---|
| 1183 |
|
---|
| 1184 | if (strcasecmp(lpcfg_realm(lp_ctx), principal->realm) == 0) {
|
---|
| 1185 | /* look for inbound trust */
|
---|
| 1186 | direction = INBOUND;
|
---|
| 1187 | realm = principal->name.name_string.val[1];
|
---|
| 1188 | } else if (strcasecmp(lpcfg_realm(lp_ctx), principal->name.name_string.val[1]) == 0) {
|
---|
| 1189 | /* look for outbound trust */
|
---|
| 1190 | direction = OUTBOUND;
|
---|
| 1191 | realm = principal->realm;
|
---|
| 1192 | } else {
|
---|
| 1193 | krb5_warnx(context, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
|
---|
| 1194 | principal->realm, principal->name.name_string.val[1]);
|
---|
| 1195 | krb5_set_error_message(context, HDB_ERR_NOENTRY, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
|
---|
| 1196 | principal->realm, principal->name.name_string.val[1]);
|
---|
| 1197 | return HDB_ERR_NOENTRY;
|
---|
| 1198 | }
|
---|
| 1199 |
|
---|
| 1200 | /* Trusted domains are under CN=system */
|
---|
| 1201 |
|
---|
| 1202 | ret = samba_kdc_lookup_trust(context, kdc_db_ctx->samdb,
|
---|
| 1203 | mem_ctx,
|
---|
| 1204 | realm, realm_dn, &msg);
|
---|
| 1205 |
|
---|
| 1206 | if (ret != 0) {
|
---|
| 1207 | krb5_warnx(context, "samba_kdc_fetch: could not find principal in DB");
|
---|
| 1208 | krb5_set_error_message(context, ret, "samba_kdc_fetch: could not find principal in DB");
|
---|
| 1209 | return ret;
|
---|
| 1210 | }
|
---|
| 1211 |
|
---|
| 1212 | ret = samba_kdc_trust_message2entry(context, kdc_db_ctx, mem_ctx,
|
---|
| 1213 | principal, direction,
|
---|
| 1214 | realm_dn, msg, entry_ex);
|
---|
| 1215 | if (ret != 0) {
|
---|
| 1216 | krb5_warnx(context, "samba_kdc_fetch: trust_message2entry failed");
|
---|
| 1217 | }
|
---|
| 1218 | return ret;
|
---|
| 1219 | }
|
---|
| 1220 |
|
---|
| 1221 | }
|
---|
| 1222 |
|
---|
| 1223 | static krb5_error_code samba_kdc_lookup_server(krb5_context context,
|
---|
| 1224 | struct samba_kdc_db_context *kdc_db_ctx,
|
---|
| 1225 | TALLOC_CTX *mem_ctx,
|
---|
| 1226 | krb5_const_principal principal,
|
---|
| 1227 | const char **attrs,
|
---|
| 1228 | struct ldb_dn **realm_dn,
|
---|
| 1229 | struct ldb_message **msg)
|
---|
| 1230 | {
|
---|
| 1231 | krb5_error_code ret;
|
---|
| 1232 | if (principal->name.name_string.len >= 2) {
|
---|
| 1233 | /* 'normal server' case */
|
---|
| 1234 | int ldb_ret;
|
---|
| 1235 | NTSTATUS nt_status;
|
---|
| 1236 | struct ldb_dn *user_dn;
|
---|
| 1237 | char *principal_string;
|
---|
| 1238 |
|
---|
| 1239 | ret = krb5_unparse_name_flags(context, principal,
|
---|
| 1240 | KRB5_PRINCIPAL_UNPARSE_NO_REALM,
|
---|
| 1241 | &principal_string);
|
---|
| 1242 | if (ret != 0) {
|
---|
| 1243 | return ret;
|
---|
| 1244 | }
|
---|
| 1245 |
|
---|
| 1246 | /* At this point we may find the host is known to be
|
---|
| 1247 | * in a different realm, so we should generate a
|
---|
| 1248 | * referral instead */
|
---|
| 1249 | nt_status = crack_service_principal_name(kdc_db_ctx->samdb,
|
---|
| 1250 | mem_ctx, principal_string,
|
---|
| 1251 | &user_dn, realm_dn);
|
---|
| 1252 | free(principal_string);
|
---|
| 1253 |
|
---|
| 1254 | if (!NT_STATUS_IS_OK(nt_status)) {
|
---|
| 1255 | return HDB_ERR_NOENTRY;
|
---|
| 1256 | }
|
---|
| 1257 |
|
---|
| 1258 | ldb_ret = dsdb_search_one(kdc_db_ctx->samdb,
|
---|
| 1259 | mem_ctx,
|
---|
| 1260 | msg, user_dn, LDB_SCOPE_BASE,
|
---|
| 1261 | attrs, DSDB_SEARCH_SHOW_EXTENDED_DN, "(objectClass=*)");
|
---|
| 1262 | if (ldb_ret != LDB_SUCCESS) {
|
---|
| 1263 | return HDB_ERR_NOENTRY;
|
---|
| 1264 | }
|
---|
| 1265 |
|
---|
| 1266 | } else {
|
---|
| 1267 | int lret;
|
---|
| 1268 | char *filter = NULL;
|
---|
| 1269 | char *short_princ;
|
---|
| 1270 | const char *realm;
|
---|
| 1271 | /* server as client principal case, but we must not lookup userPrincipalNames */
|
---|
| 1272 | *realm_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
|
---|
| 1273 | realm = krb5_principal_get_realm(context, principal);
|
---|
| 1274 |
|
---|
| 1275 | /* TODO: Check if it is our realm, otherwise give referall */
|
---|
| 1276 |
|
---|
| 1277 | ret = krb5_unparse_name_flags(context, principal, KRB5_PRINCIPAL_UNPARSE_NO_REALM, &short_princ);
|
---|
| 1278 |
|
---|
| 1279 | if (ret != 0) {
|
---|
| 1280 | krb5_set_error_message(context, ret, "samba_kdc_lookup_principal: could not parse principal");
|
---|
| 1281 | krb5_warnx(context, "samba_kdc_lookup_principal: could not parse principal");
|
---|
| 1282 | return ret;
|
---|
| 1283 | }
|
---|
| 1284 |
|
---|
| 1285 | lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx, msg,
|
---|
| 1286 | *realm_dn, LDB_SCOPE_SUBTREE,
|
---|
| 1287 | attrs,
|
---|
| 1288 | DSDB_SEARCH_SHOW_EXTENDED_DN,
|
---|
| 1289 | "(&(objectClass=user)(samAccountName=%s))",
|
---|
| 1290 | ldb_binary_encode_string(mem_ctx, short_princ));
|
---|
| 1291 | free(short_princ);
|
---|
| 1292 | if (lret == LDB_ERR_NO_SUCH_OBJECT) {
|
---|
| 1293 | DEBUG(3, ("Failed find a entry for %s\n", filter));
|
---|
| 1294 | return HDB_ERR_NOENTRY;
|
---|
| 1295 | }
|
---|
| 1296 | if (lret != LDB_SUCCESS) {
|
---|
| 1297 | DEBUG(3, ("Failed single search for for %s - %s\n",
|
---|
| 1298 | filter, ldb_errstring(kdc_db_ctx->samdb)));
|
---|
| 1299 | return HDB_ERR_NOENTRY;
|
---|
| 1300 | }
|
---|
| 1301 | }
|
---|
| 1302 |
|
---|
| 1303 | return 0;
|
---|
| 1304 | }
|
---|
| 1305 |
|
---|
| 1306 | static krb5_error_code samba_kdc_fetch_server(krb5_context context,
|
---|
| 1307 | struct samba_kdc_db_context *kdc_db_ctx,
|
---|
| 1308 | TALLOC_CTX *mem_ctx,
|
---|
| 1309 | krb5_const_principal principal,
|
---|
| 1310 | unsigned flags,
|
---|
| 1311 | hdb_entry_ex *entry_ex)
|
---|
| 1312 | {
|
---|
| 1313 | krb5_error_code ret;
|
---|
| 1314 | struct ldb_dn *realm_dn;
|
---|
| 1315 | struct ldb_message *msg;
|
---|
| 1316 |
|
---|
| 1317 | ret = samba_kdc_lookup_server(context, kdc_db_ctx, mem_ctx, principal,
|
---|
| 1318 | server_attrs, &realm_dn, &msg);
|
---|
| 1319 | if (ret != 0) {
|
---|
| 1320 | return ret;
|
---|
| 1321 | }
|
---|
| 1322 |
|
---|
| 1323 | ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
|
---|
| 1324 | principal, SAMBA_KDC_ENT_TYPE_SERVER,
|
---|
| 1325 | flags,
|
---|
| 1326 | realm_dn, msg, entry_ex);
|
---|
| 1327 | if (ret != 0) {
|
---|
| 1328 | krb5_warnx(context, "samba_kdc_fetch: message2entry failed");
|
---|
| 1329 | }
|
---|
| 1330 |
|
---|
| 1331 | return ret;
|
---|
| 1332 | }
|
---|
| 1333 |
|
---|
| 1334 | krb5_error_code samba_kdc_fetch(krb5_context context,
|
---|
| 1335 | struct samba_kdc_db_context *kdc_db_ctx,
|
---|
| 1336 | krb5_const_principal principal,
|
---|
| 1337 | unsigned flags,
|
---|
| 1338 | krb5_kvno kvno,
|
---|
| 1339 | hdb_entry_ex *entry_ex)
|
---|
| 1340 | {
|
---|
| 1341 | krb5_error_code ret = HDB_ERR_NOENTRY;
|
---|
| 1342 | TALLOC_CTX *mem_ctx;
|
---|
| 1343 | unsigned int krbtgt_number;
|
---|
| 1344 | if (flags & HDB_F_KVNO_SPECIFIED) {
|
---|
| 1345 | krbtgt_number = SAMBA_KVNO_GET_KRBTGT(kvno);
|
---|
| 1346 | if (kdc_db_ctx->rodc) {
|
---|
| 1347 | if (krbtgt_number != kdc_db_ctx->my_krbtgt_number) {
|
---|
| 1348 | return HDB_ERR_NOT_FOUND_HERE;
|
---|
| 1349 | }
|
---|
| 1350 | }
|
---|
| 1351 | } else {
|
---|
| 1352 | krbtgt_number = kdc_db_ctx->my_krbtgt_number;
|
---|
| 1353 | }
|
---|
| 1354 |
|
---|
| 1355 | mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_fetch context");
|
---|
| 1356 | if (!mem_ctx) {
|
---|
| 1357 | ret = ENOMEM;
|
---|
| 1358 | krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
|
---|
| 1359 | return ret;
|
---|
| 1360 | }
|
---|
| 1361 |
|
---|
| 1362 | if (flags & HDB_F_GET_CLIENT) {
|
---|
| 1363 | ret = samba_kdc_fetch_client(context, kdc_db_ctx, mem_ctx, principal, flags, entry_ex);
|
---|
| 1364 | if (ret != HDB_ERR_NOENTRY) goto done;
|
---|
| 1365 | }
|
---|
| 1366 | if (flags & HDB_F_GET_SERVER) {
|
---|
| 1367 | /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
|
---|
| 1368 | ret = samba_kdc_fetch_krbtgt(context, kdc_db_ctx, mem_ctx, principal, flags, krbtgt_number, entry_ex);
|
---|
| 1369 | if (ret != HDB_ERR_NOENTRY) goto done;
|
---|
| 1370 |
|
---|
| 1371 | /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
|
---|
| 1372 | ret = samba_kdc_fetch_server(context, kdc_db_ctx, mem_ctx, principal, flags, entry_ex);
|
---|
| 1373 | if (ret != HDB_ERR_NOENTRY) goto done;
|
---|
| 1374 | }
|
---|
| 1375 | if (flags & HDB_F_GET_KRBTGT) {
|
---|
| 1376 | ret = samba_kdc_fetch_krbtgt(context, kdc_db_ctx, mem_ctx, principal, flags, krbtgt_number, entry_ex);
|
---|
| 1377 | if (ret != HDB_ERR_NOENTRY) goto done;
|
---|
| 1378 | }
|
---|
| 1379 |
|
---|
| 1380 | done:
|
---|
| 1381 | talloc_free(mem_ctx);
|
---|
| 1382 | return ret;
|
---|
| 1383 | }
|
---|
| 1384 |
|
---|
| 1385 | struct samba_kdc_seq {
|
---|
| 1386 | unsigned int index;
|
---|
| 1387 | unsigned int count;
|
---|
| 1388 | struct ldb_message **msgs;
|
---|
| 1389 | struct ldb_dn *realm_dn;
|
---|
| 1390 | };
|
---|
| 1391 |
|
---|
| 1392 | static krb5_error_code samba_kdc_seq(krb5_context context,
|
---|
| 1393 | struct samba_kdc_db_context *kdc_db_ctx,
|
---|
| 1394 | hdb_entry_ex *entry)
|
---|
| 1395 | {
|
---|
| 1396 | krb5_error_code ret;
|
---|
| 1397 | struct samba_kdc_seq *priv = kdc_db_ctx->seq_ctx;
|
---|
| 1398 | TALLOC_CTX *mem_ctx;
|
---|
| 1399 | hdb_entry_ex entry_ex;
|
---|
| 1400 | memset(&entry_ex, '\0', sizeof(entry_ex));
|
---|
| 1401 |
|
---|
| 1402 | if (!priv) {
|
---|
| 1403 | return HDB_ERR_NOENTRY;
|
---|
| 1404 | }
|
---|
| 1405 |
|
---|
| 1406 | mem_ctx = talloc_named(priv, 0, "samba_kdc_seq context");
|
---|
| 1407 |
|
---|
| 1408 | if (!mem_ctx) {
|
---|
| 1409 | ret = ENOMEM;
|
---|
| 1410 | krb5_set_error_message(context, ret, "samba_kdc_seq: talloc_named() failed!");
|
---|
| 1411 | return ret;
|
---|
| 1412 | }
|
---|
| 1413 |
|
---|
| 1414 | if (priv->index < priv->count) {
|
---|
| 1415 | ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
|
---|
| 1416 | NULL, SAMBA_KDC_ENT_TYPE_ANY,
|
---|
| 1417 | HDB_F_ADMIN_DATA|HDB_F_GET_ANY,
|
---|
| 1418 | priv->realm_dn, priv->msgs[priv->index++], entry);
|
---|
| 1419 | } else {
|
---|
| 1420 | ret = HDB_ERR_NOENTRY;
|
---|
| 1421 | }
|
---|
| 1422 |
|
---|
| 1423 | if (ret != 0) {
|
---|
| 1424 | TALLOC_FREE(priv);
|
---|
| 1425 | kdc_db_ctx->seq_ctx = NULL;
|
---|
| 1426 | } else {
|
---|
| 1427 | talloc_free(mem_ctx);
|
---|
| 1428 | }
|
---|
| 1429 |
|
---|
| 1430 | return ret;
|
---|
| 1431 | }
|
---|
| 1432 |
|
---|
| 1433 | krb5_error_code samba_kdc_firstkey(krb5_context context,
|
---|
| 1434 | struct samba_kdc_db_context *kdc_db_ctx,
|
---|
| 1435 | hdb_entry_ex *entry)
|
---|
| 1436 | {
|
---|
| 1437 | struct ldb_context *ldb_ctx = kdc_db_ctx->samdb;
|
---|
| 1438 | struct samba_kdc_seq *priv = kdc_db_ctx->seq_ctx;
|
---|
| 1439 | char *realm;
|
---|
| 1440 | struct ldb_result *res = NULL;
|
---|
| 1441 | krb5_error_code ret;
|
---|
| 1442 | TALLOC_CTX *mem_ctx;
|
---|
| 1443 | int lret;
|
---|
| 1444 |
|
---|
| 1445 | if (priv) {
|
---|
| 1446 | TALLOC_FREE(priv);
|
---|
| 1447 | kdc_db_ctx->seq_ctx = NULL;
|
---|
| 1448 | }
|
---|
| 1449 |
|
---|
| 1450 | priv = (struct samba_kdc_seq *) talloc(kdc_db_ctx, struct samba_kdc_seq);
|
---|
| 1451 | if (!priv) {
|
---|
| 1452 | ret = ENOMEM;
|
---|
| 1453 | krb5_set_error_message(context, ret, "talloc: out of memory");
|
---|
| 1454 | return ret;
|
---|
| 1455 | }
|
---|
| 1456 |
|
---|
| 1457 | priv->index = 0;
|
---|
| 1458 | priv->msgs = NULL;
|
---|
| 1459 | priv->realm_dn = ldb_get_default_basedn(ldb_ctx);
|
---|
| 1460 | priv->count = 0;
|
---|
| 1461 |
|
---|
| 1462 | mem_ctx = talloc_named(priv, 0, "samba_kdc_firstkey context");
|
---|
| 1463 |
|
---|
| 1464 | if (!mem_ctx) {
|
---|
| 1465 | ret = ENOMEM;
|
---|
| 1466 | krb5_set_error_message(context, ret, "samba_kdc_firstkey: talloc_named() failed!");
|
---|
| 1467 | return ret;
|
---|
| 1468 | }
|
---|
| 1469 |
|
---|
| 1470 | ret = krb5_get_default_realm(context, &realm);
|
---|
| 1471 | if (ret != 0) {
|
---|
| 1472 | TALLOC_FREE(priv);
|
---|
| 1473 | return ret;
|
---|
| 1474 | }
|
---|
| 1475 |
|
---|
| 1476 | lret = ldb_search(ldb_ctx, priv, &res,
|
---|
| 1477 | priv->realm_dn, LDB_SCOPE_SUBTREE, user_attrs,
|
---|
| 1478 | "(objectClass=user)");
|
---|
| 1479 |
|
---|
| 1480 | if (lret != LDB_SUCCESS) {
|
---|
| 1481 | TALLOC_FREE(priv);
|
---|
| 1482 | return HDB_ERR_NOENTRY;
|
---|
| 1483 | }
|
---|
| 1484 |
|
---|
| 1485 | priv->count = res->count;
|
---|
| 1486 | priv->msgs = talloc_steal(priv, res->msgs);
|
---|
| 1487 | talloc_free(res);
|
---|
| 1488 |
|
---|
| 1489 | kdc_db_ctx->seq_ctx = priv;
|
---|
| 1490 |
|
---|
| 1491 | ret = samba_kdc_seq(context, kdc_db_ctx, entry);
|
---|
| 1492 |
|
---|
| 1493 | if (ret != 0) {
|
---|
| 1494 | TALLOC_FREE(priv);
|
---|
| 1495 | kdc_db_ctx->seq_ctx = NULL;
|
---|
| 1496 | } else {
|
---|
| 1497 | talloc_free(mem_ctx);
|
---|
| 1498 | }
|
---|
| 1499 | return ret;
|
---|
| 1500 | }
|
---|
| 1501 |
|
---|
| 1502 | krb5_error_code samba_kdc_nextkey(krb5_context context,
|
---|
| 1503 | struct samba_kdc_db_context *kdc_db_ctx,
|
---|
| 1504 | hdb_entry_ex *entry)
|
---|
| 1505 | {
|
---|
| 1506 | return samba_kdc_seq(context, kdc_db_ctx, entry);
|
---|
| 1507 | }
|
---|
| 1508 |
|
---|
| 1509 | /* Check if a given entry may delegate or do s4u2self to this target principal
|
---|
| 1510 | *
|
---|
| 1511 | * This is currently a very nasty hack - allowing only delegation to itself.
|
---|
| 1512 | *
|
---|
| 1513 | * This is shared between the constrained delegation and S4U2Self code.
|
---|
| 1514 | */
|
---|
| 1515 | krb5_error_code
|
---|
| 1516 | samba_kdc_check_identical_client_and_server(krb5_context context,
|
---|
| 1517 | struct samba_kdc_db_context *kdc_db_ctx,
|
---|
| 1518 | hdb_entry_ex *entry,
|
---|
| 1519 | krb5_const_principal target_principal)
|
---|
| 1520 | {
|
---|
| 1521 | krb5_error_code ret;
|
---|
| 1522 | krb5_principal enterprise_prinicpal = NULL;
|
---|
| 1523 | struct ldb_dn *realm_dn;
|
---|
| 1524 | struct ldb_message *msg;
|
---|
| 1525 | struct dom_sid *orig_sid;
|
---|
| 1526 | struct dom_sid *target_sid;
|
---|
| 1527 | struct samba_kdc_entry *p = talloc_get_type(entry->ctx, struct samba_kdc_entry);
|
---|
| 1528 | const char *delegation_check_attrs[] = {
|
---|
| 1529 | "objectSid", NULL
|
---|
| 1530 | };
|
---|
| 1531 |
|
---|
| 1532 | TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_constrained_delegation");
|
---|
| 1533 |
|
---|
| 1534 | if (!mem_ctx) {
|
---|
| 1535 | ret = ENOMEM;
|
---|
| 1536 | krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
|
---|
| 1537 | return ret;
|
---|
| 1538 | }
|
---|
| 1539 |
|
---|
| 1540 | if (target_principal->name.name_type == KRB5_NT_ENTERPRISE_PRINCIPAL) {
|
---|
| 1541 | /* Need to reparse the enterprise principal to find the real target */
|
---|
| 1542 | if (target_principal->name.name_string.len != 1) {
|
---|
| 1543 | ret = KRB5_PARSE_MALFORMED;
|
---|
| 1544 | krb5_set_error_message(context, ret, "samba_kdc_check_constrained_delegation: request for delegation to enterprise principal with wrong (%d) number of components",
|
---|
| 1545 | target_principal->name.name_string.len);
|
---|
| 1546 | talloc_free(mem_ctx);
|
---|
| 1547 | return ret;
|
---|
| 1548 | }
|
---|
| 1549 | ret = krb5_parse_name(context, target_principal->name.name_string.val[0],
|
---|
| 1550 | &enterprise_prinicpal);
|
---|
| 1551 | if (ret) {
|
---|
| 1552 | talloc_free(mem_ctx);
|
---|
| 1553 | return ret;
|
---|
| 1554 | }
|
---|
| 1555 | target_principal = enterprise_prinicpal;
|
---|
| 1556 | }
|
---|
| 1557 |
|
---|
| 1558 | ret = samba_kdc_lookup_server(context, kdc_db_ctx, mem_ctx, target_principal,
|
---|
| 1559 | delegation_check_attrs, &realm_dn, &msg);
|
---|
| 1560 |
|
---|
| 1561 | krb5_free_principal(context, enterprise_prinicpal);
|
---|
| 1562 |
|
---|
| 1563 | if (ret != 0) {
|
---|
| 1564 | talloc_free(mem_ctx);
|
---|
| 1565 | return ret;
|
---|
| 1566 | }
|
---|
| 1567 |
|
---|
| 1568 | orig_sid = samdb_result_dom_sid(mem_ctx, p->msg, "objectSid");
|
---|
| 1569 | target_sid = samdb_result_dom_sid(mem_ctx, msg, "objectSid");
|
---|
| 1570 |
|
---|
| 1571 | /* Allow delegation to the same principal, even if by a different
|
---|
| 1572 | * name. The easy and safe way to prove this is by SID
|
---|
| 1573 | * comparison */
|
---|
| 1574 | if (!(orig_sid && target_sid && dom_sid_equal(orig_sid, target_sid))) {
|
---|
| 1575 | talloc_free(mem_ctx);
|
---|
| 1576 | return KRB5KDC_ERR_BADOPTION;
|
---|
| 1577 | }
|
---|
| 1578 |
|
---|
| 1579 | talloc_free(mem_ctx);
|
---|
| 1580 | return ret;
|
---|
| 1581 | }
|
---|
| 1582 |
|
---|
| 1583 | /* Certificates printed by a the Certificate Authority might have a
|
---|
| 1584 | * slightly different form of the user principal name to that in the
|
---|
| 1585 | * database. Allow a mismatch where they both refer to the same
|
---|
| 1586 | * SID */
|
---|
| 1587 |
|
---|
| 1588 | krb5_error_code
|
---|
| 1589 | samba_kdc_check_pkinit_ms_upn_match(krb5_context context,
|
---|
| 1590 | struct samba_kdc_db_context *kdc_db_ctx,
|
---|
| 1591 | hdb_entry_ex *entry,
|
---|
| 1592 | krb5_const_principal certificate_principal)
|
---|
| 1593 | {
|
---|
| 1594 | krb5_error_code ret;
|
---|
| 1595 | struct ldb_dn *realm_dn;
|
---|
| 1596 | struct ldb_message *msg;
|
---|
| 1597 | struct dom_sid *orig_sid;
|
---|
| 1598 | struct dom_sid *target_sid;
|
---|
| 1599 | struct samba_kdc_entry *p = talloc_get_type(entry->ctx, struct samba_kdc_entry);
|
---|
| 1600 | const char *ms_upn_check_attrs[] = {
|
---|
| 1601 | "objectSid", NULL
|
---|
| 1602 | };
|
---|
| 1603 |
|
---|
| 1604 | TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_pkinit_ms_upn_match");
|
---|
| 1605 |
|
---|
| 1606 | if (!mem_ctx) {
|
---|
| 1607 | ret = ENOMEM;
|
---|
| 1608 | krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
|
---|
| 1609 | return ret;
|
---|
| 1610 | }
|
---|
| 1611 |
|
---|
| 1612 | ret = samba_kdc_lookup_client(context, kdc_db_ctx,
|
---|
| 1613 | mem_ctx, certificate_principal,
|
---|
| 1614 | ms_upn_check_attrs, &realm_dn, &msg);
|
---|
| 1615 |
|
---|
| 1616 | if (ret != 0) {
|
---|
| 1617 | talloc_free(mem_ctx);
|
---|
| 1618 | return ret;
|
---|
| 1619 | }
|
---|
| 1620 |
|
---|
| 1621 | orig_sid = samdb_result_dom_sid(mem_ctx, p->msg, "objectSid");
|
---|
| 1622 | target_sid = samdb_result_dom_sid(mem_ctx, msg, "objectSid");
|
---|
| 1623 |
|
---|
| 1624 | /* Consider these to be the same principal, even if by a different
|
---|
| 1625 | * name. The easy and safe way to prove this is by SID
|
---|
| 1626 | * comparison */
|
---|
| 1627 | if (!(orig_sid && target_sid && dom_sid_equal(orig_sid, target_sid))) {
|
---|
| 1628 | talloc_free(mem_ctx);
|
---|
| 1629 | return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH;
|
---|
| 1630 | }
|
---|
| 1631 |
|
---|
| 1632 | talloc_free(mem_ctx);
|
---|
| 1633 | return ret;
|
---|
| 1634 | }
|
---|
| 1635 |
|
---|
| 1636 | NTSTATUS samba_kdc_setup_db_ctx(TALLOC_CTX *mem_ctx, struct samba_kdc_base_context *base_ctx,
|
---|
| 1637 | struct samba_kdc_db_context **kdc_db_ctx_out)
|
---|
| 1638 | {
|
---|
| 1639 | int ldb_ret;
|
---|
| 1640 | struct ldb_message *msg;
|
---|
| 1641 | struct auth_session_info *session_info;
|
---|
| 1642 | struct samba_kdc_db_context *kdc_db_ctx;
|
---|
| 1643 | /* The idea here is very simple. Using Kerberos to
|
---|
| 1644 | * authenticate the KDC to the LDAP server is higly likely to
|
---|
| 1645 | * be circular.
|
---|
| 1646 | *
|
---|
| 1647 | * In future we may set this up to use EXERNAL and SSL
|
---|
| 1648 | * certificates, for now it will almost certainly be NTLMSSP_SET_USERNAME
|
---|
| 1649 | */
|
---|
| 1650 |
|
---|
| 1651 | kdc_db_ctx = talloc_zero(mem_ctx, struct samba_kdc_db_context);
|
---|
| 1652 | if (kdc_db_ctx == NULL) {
|
---|
| 1653 | return NT_STATUS_NO_MEMORY;
|
---|
| 1654 | }
|
---|
| 1655 | kdc_db_ctx->ev_ctx = base_ctx->ev_ctx;
|
---|
| 1656 | kdc_db_ctx->lp_ctx = base_ctx->lp_ctx;
|
---|
| 1657 |
|
---|
| 1658 | kdc_get_policy(base_ctx->lp_ctx, NULL, &kdc_db_ctx->policy);
|
---|
| 1659 |
|
---|
| 1660 | session_info = system_session(kdc_db_ctx->lp_ctx);
|
---|
| 1661 | if (session_info == NULL) {
|
---|
| 1662 | return NT_STATUS_INTERNAL_ERROR;
|
---|
| 1663 | }
|
---|
| 1664 |
|
---|
| 1665 | /* Setup the link to LDB */
|
---|
| 1666 | kdc_db_ctx->samdb = samdb_connect(kdc_db_ctx, base_ctx->ev_ctx,
|
---|
| 1667 | base_ctx->lp_ctx, session_info, 0);
|
---|
| 1668 | if (kdc_db_ctx->samdb == NULL) {
|
---|
| 1669 | DEBUG(1, ("hdb_samba4_create: Cannot open samdb for KDC backend!"));
|
---|
| 1670 | talloc_free(kdc_db_ctx);
|
---|
| 1671 | return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
|
---|
| 1672 | }
|
---|
| 1673 |
|
---|
| 1674 | /* Find out our own krbtgt kvno */
|
---|
| 1675 | ldb_ret = samdb_rodc(kdc_db_ctx->samdb, &kdc_db_ctx->rodc);
|
---|
| 1676 | if (ldb_ret != LDB_SUCCESS) {
|
---|
| 1677 | DEBUG(1, ("hdb_samba4_create: Cannot determine if we are an RODC in KDC backend: %s\n",
|
---|
| 1678 | ldb_errstring(kdc_db_ctx->samdb)));
|
---|
| 1679 | talloc_free(kdc_db_ctx);
|
---|
| 1680 | return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
|
---|
| 1681 | }
|
---|
| 1682 | if (kdc_db_ctx->rodc) {
|
---|
| 1683 | int my_krbtgt_number;
|
---|
| 1684 | const char *secondary_keytab[] = { "msDS-SecondaryKrbTgtNumber", NULL };
|
---|
| 1685 | struct ldb_dn *account_dn;
|
---|
| 1686 | struct ldb_dn *server_dn = samdb_server_dn(kdc_db_ctx->samdb, kdc_db_ctx);
|
---|
| 1687 | if (!server_dn) {
|
---|
| 1688 | DEBUG(1, ("hdb_samba4_create: Cannot determine server DN in KDC backend: %s\n",
|
---|
| 1689 | ldb_errstring(kdc_db_ctx->samdb)));
|
---|
| 1690 | talloc_free(kdc_db_ctx);
|
---|
| 1691 | return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
|
---|
| 1692 | }
|
---|
| 1693 |
|
---|
| 1694 | ldb_ret = samdb_reference_dn(kdc_db_ctx->samdb, kdc_db_ctx, server_dn,
|
---|
| 1695 | "serverReference", &account_dn);
|
---|
| 1696 | if (ldb_ret != LDB_SUCCESS) {
|
---|
| 1697 | DEBUG(1, ("hdb_samba4_create: Cannot determine server account in KDC backend: %s\n",
|
---|
| 1698 | ldb_errstring(kdc_db_ctx->samdb)));
|
---|
| 1699 | talloc_free(kdc_db_ctx);
|
---|
| 1700 | return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
|
---|
| 1701 | }
|
---|
| 1702 |
|
---|
| 1703 | ldb_ret = samdb_reference_dn(kdc_db_ctx->samdb, kdc_db_ctx, account_dn,
|
---|
| 1704 | "msDS-KrbTgtLink", &kdc_db_ctx->krbtgt_dn);
|
---|
| 1705 | talloc_free(account_dn);
|
---|
| 1706 | if (ldb_ret != LDB_SUCCESS) {
|
---|
| 1707 | DEBUG(1, ("hdb_samba4_create: Cannot determine RODC krbtgt account in KDC backend: %s\n",
|
---|
| 1708 | ldb_errstring(kdc_db_ctx->samdb)));
|
---|
| 1709 | talloc_free(kdc_db_ctx);
|
---|
| 1710 | return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
|
---|
| 1711 | }
|
---|
| 1712 |
|
---|
| 1713 | ldb_ret = dsdb_search_one(kdc_db_ctx->samdb, kdc_db_ctx,
|
---|
| 1714 | &msg, kdc_db_ctx->krbtgt_dn, LDB_SCOPE_BASE,
|
---|
| 1715 | secondary_keytab,
|
---|
| 1716 | 0,
|
---|
| 1717 | "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=*))");
|
---|
| 1718 | if (ldb_ret != LDB_SUCCESS) {
|
---|
| 1719 | DEBUG(1, ("hdb_samba4_create: Cannot read krbtgt account %s in KDC backend to get msDS-SecondaryKrbTgtNumber: %s: %s\n",
|
---|
| 1720 | ldb_dn_get_linearized(kdc_db_ctx->krbtgt_dn),
|
---|
| 1721 | ldb_errstring(kdc_db_ctx->samdb),
|
---|
| 1722 | ldb_strerror(ldb_ret)));
|
---|
| 1723 | talloc_free(kdc_db_ctx);
|
---|
| 1724 | return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
|
---|
| 1725 | }
|
---|
| 1726 | my_krbtgt_number = ldb_msg_find_attr_as_int(msg, "msDS-SecondaryKrbTgtNumber", -1);
|
---|
| 1727 | if (my_krbtgt_number == -1) {
|
---|
| 1728 | DEBUG(1, ("hdb_samba4_create: Cannot read msDS-SecondaryKrbTgtNumber from krbtgt account %s in KDC backend: got %d\n",
|
---|
| 1729 | ldb_dn_get_linearized(kdc_db_ctx->krbtgt_dn),
|
---|
| 1730 | my_krbtgt_number));
|
---|
| 1731 | talloc_free(kdc_db_ctx);
|
---|
| 1732 | return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
|
---|
| 1733 | }
|
---|
| 1734 | kdc_db_ctx->my_krbtgt_number = my_krbtgt_number;
|
---|
| 1735 |
|
---|
| 1736 | } else {
|
---|
| 1737 | kdc_db_ctx->my_krbtgt_number = 0;
|
---|
| 1738 | ldb_ret = dsdb_search_one(kdc_db_ctx->samdb, kdc_db_ctx,
|
---|
| 1739 | &msg, NULL, LDB_SCOPE_SUBTREE,
|
---|
| 1740 | krbtgt_attrs,
|
---|
| 1741 | 0,
|
---|
| 1742 | "(&(objectClass=user)(samAccountName=krbtgt))");
|
---|
| 1743 |
|
---|
| 1744 | if (ldb_ret != LDB_SUCCESS) {
|
---|
| 1745 | DEBUG(1, ("samba_kdc_fetch: could not find own KRBTGT in DB: %s\n", ldb_errstring(kdc_db_ctx->samdb)));
|
---|
| 1746 | talloc_free(kdc_db_ctx);
|
---|
| 1747 | return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
|
---|
| 1748 | }
|
---|
| 1749 | kdc_db_ctx->krbtgt_dn = talloc_steal(kdc_db_ctx, msg->dn);
|
---|
| 1750 | kdc_db_ctx->my_krbtgt_number = 0;
|
---|
| 1751 | talloc_free(msg);
|
---|
| 1752 | }
|
---|
| 1753 | *kdc_db_ctx_out = kdc_db_ctx;
|
---|
| 1754 | return NT_STATUS_OK;
|
---|
| 1755 | }
|
---|