s4:kdc: Include default groups in security token
[samba.git] / source4 / kdc / db-glue.c
blobc07c2dbc24a83b14be03d45da31e2d58e195e094
1 /*
2 Unix SMB/CIFS implementation.
4 Database Glue between Samba and the KDC
6 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2005-2009
7 Copyright (C) Simo Sorce <idra@samba.org> 2010
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.
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.
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/>.
24 #include "includes.h"
25 #include "libcli/security/security.h"
26 #include "librpc/gen_ndr/ndr_security.h"
27 #include "auth/auth.h"
28 #include "auth/auth_sam.h"
29 #include "dsdb/samdb/samdb.h"
30 #include "dsdb/common/util.h"
31 #include "librpc/gen_ndr/ndr_drsblobs.h"
32 #include "param/param.h"
33 #include "param/secrets.h"
34 #include "../lib/crypto/md4.h"
35 #include "system/kerberos.h"
36 #include "auth/kerberos/kerberos.h"
37 #include "kdc/authn_policy_util.h"
38 #include "kdc/sdb.h"
39 #include "kdc/samba_kdc.h"
40 #include "kdc/db-glue.h"
41 #include "kdc/pac-glue.h"
42 #include "librpc/gen_ndr/ndr_irpc_c.h"
43 #include "lib/messaging/irpc.h"
45 #undef DBGC_CLASS
46 #define DBGC_CLASS DBGC_KERBEROS
48 #undef strcasecmp
49 #undef strncasecmp
51 #define SAMBA_KVNO_GET_KRBTGT(kvno) \
52 ((uint16_t)(((uint32_t)kvno) >> 16))
54 #define SAMBA_KVNO_GET_VALUE(kvno) \
55 ((uint16_t)(((uint32_t)kvno) & 0xFFFF))
57 #define SAMBA_KVNO_AND_KRBTGT(kvno, krbtgt) \
58 ((krb5_kvno)((((uint32_t)kvno) & 0xFFFF) | \
59 ((((uint32_t)krbtgt) << 16) & 0xFFFF0000)))
61 enum trust_direction {
62 UNKNOWN = 0,
63 INBOUND = LSA_TRUST_DIRECTION_INBOUND,
64 OUTBOUND = LSA_TRUST_DIRECTION_OUTBOUND
67 static const char *trust_attrs[] = {
68 "securityIdentifier",
69 "flatName",
70 "trustPartner",
71 "trustAttributes",
72 "trustDirection",
73 "trustType",
74 "msDS-TrustForestTrustInfo",
75 "trustAuthIncoming",
76 "trustAuthOutgoing",
77 "whenCreated",
78 "msDS-SupportedEncryptionTypes",
79 NULL
83 send a message to the drepl server telling it to initiate a
84 REPL_SECRET getncchanges extended op to fetch the users secrets
86 static void auth_sam_trigger_repl_secret(TALLOC_CTX *mem_ctx,
87 struct imessaging_context *msg_ctx,
88 struct tevent_context *event_ctx,
89 struct ldb_dn *user_dn)
91 struct dcerpc_binding_handle *irpc_handle;
92 struct drepl_trigger_repl_secret r;
93 struct tevent_req *req;
94 TALLOC_CTX *tmp_ctx;
96 tmp_ctx = talloc_new(mem_ctx);
97 if (tmp_ctx == NULL) {
98 return;
101 irpc_handle = irpc_binding_handle_by_name(tmp_ctx, msg_ctx,
102 "dreplsrv",
103 &ndr_table_irpc);
104 if (irpc_handle == NULL) {
105 DEBUG(1,(__location__ ": Unable to get binding handle for dreplsrv\n"));
106 TALLOC_FREE(tmp_ctx);
107 return;
110 r.in.user_dn = ldb_dn_get_linearized(user_dn);
113 * This seem to rely on the current IRPC implementation,
114 * which delivers the message in the _send function.
116 * TODO: we need a ONE_WAY IRPC handle and register
117 * a callback and wait for it to be triggered!
119 req = dcerpc_drepl_trigger_repl_secret_r_send(tmp_ctx,
120 event_ctx,
121 irpc_handle,
122 &r);
124 /* we aren't interested in a reply */
125 talloc_free(req);
126 TALLOC_FREE(tmp_ctx);
129 static time_t ldb_msg_find_krb5time_ldap_time(struct ldb_message *msg, const char *attr, time_t default_val)
131 const char *tmp;
132 const char *gentime;
133 struct tm tm;
135 gentime = ldb_msg_find_attr_as_string(msg, attr, NULL);
136 if (!gentime)
137 return default_val;
139 tmp = strptime(gentime, "%Y%m%d%H%M%SZ", &tm);
140 if (tmp == NULL) {
141 return default_val;
144 return timegm(&tm);
147 static struct SDBFlags uf2SDBFlags(krb5_context context, uint32_t userAccountControl, enum samba_kdc_ent_type ent_type)
149 struct SDBFlags flags = int2SDBFlags(0);
151 /* we don't allow kadmin deletes */
152 flags.immutable = 1;
154 /* mark the principal as invalid to start with */
155 flags.invalid = 1;
157 flags.renewable = 1;
159 /* All accounts are servers, but this may be disabled again in the caller */
160 flags.server = 1;
162 /* Account types - clear the invalid bit if it turns out to be valid */
163 if (userAccountControl & UF_NORMAL_ACCOUNT) {
164 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
165 flags.client = 1;
167 flags.invalid = 0;
170 if (userAccountControl & UF_INTERDOMAIN_TRUST_ACCOUNT) {
171 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
172 flags.client = 1;
174 flags.invalid = 0;
176 if (userAccountControl & UF_WORKSTATION_TRUST_ACCOUNT) {
177 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
178 flags.client = 1;
180 flags.invalid = 0;
182 if (userAccountControl & UF_SERVER_TRUST_ACCOUNT) {
183 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
184 flags.client = 1;
186 flags.invalid = 0;
189 /* Not permitted to act as a client if disabled */
190 if (userAccountControl & UF_ACCOUNTDISABLE) {
191 flags.client = 0;
193 if (userAccountControl & UF_LOCKOUT) {
194 flags.locked_out = 1;
197 if (userAccountControl & UF_PASSWD_NOTREQD) {
198 flags.invalid = 1;
202 UF_PASSWD_CANT_CHANGE and UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED are irrelevant
204 if (userAccountControl & UF_TEMP_DUPLICATE_ACCOUNT) {
205 flags.invalid = 1;
208 /* UF_DONT_EXPIRE_PASSWD and UF_USE_DES_KEY_ONLY handled in samba_kdc_message2entry() */
211 if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
212 flags.invalid = 1;
215 if (userAccountControl & UF_SMARTCARD_REQUIRED) {
216 flags.require_hwauth = 1;
218 if (userAccountControl & UF_TRUSTED_FOR_DELEGATION) {
219 flags.ok_as_delegate = 1;
221 if (userAccountControl & UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION) {
223 * this is confusing...
225 * UF_TRUSTED_FOR_DELEGATION
226 * => ok_as_delegate
228 * and
230 * UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION
231 * => trusted_for_delegation
233 flags.trusted_for_delegation = 1;
235 if (!(userAccountControl & UF_NOT_DELEGATED)) {
236 flags.forwardable = 1;
237 flags.proxiable = 1;
240 if (userAccountControl & UF_DONT_REQUIRE_PREAUTH) {
241 flags.require_preauth = 0;
242 } else {
243 flags.require_preauth = 1;
246 if (userAccountControl & UF_NO_AUTH_DATA_REQUIRED) {
247 flags.no_auth_data_reqd = 1;
250 return flags;
253 static int samba_kdc_entry_destructor(struct samba_kdc_entry *p)
255 if (p->db_entry != NULL) {
257 * A sdb_entry still has a reference
259 return -1;
262 if (p->kdc_entry != NULL) {
264 * hdb_entry or krb5_db_entry still
265 * have a reference...
267 return -1;
270 return 0;
274 * Sort keys in descending order of strength.
276 * Explanaton from Greg Hudson:
278 * To encrypt tickets only the first returned key is used by the MIT KDC. The
279 * other keys just communicate support for session key enctypes, and aren't
280 * really used. The encryption key for the ticket enc part doesn't have
281 * to be of a type requested by the client. The session key enctype is chosen
282 * based on the client preference order, limited by the set of enctypes present
283 * in the server keys (unless the string attribute is set on the server
284 * principal overriding that set).
287 static int sdb_key_strength_priority(krb5_enctype etype)
289 static const krb5_enctype etype_list[] = {
290 ENCTYPE_AES256_CTS_HMAC_SHA1_96,
291 ENCTYPE_AES128_CTS_HMAC_SHA1_96,
292 ENCTYPE_DES3_CBC_SHA1,
293 ENCTYPE_ARCFOUR_HMAC,
294 ENCTYPE_DES_CBC_MD5,
295 ENCTYPE_DES_CBC_MD4,
296 ENCTYPE_DES_CBC_CRC,
297 ENCTYPE_NULL
299 int i;
301 for (i = 0; i < ARRAY_SIZE(etype_list); i++) {
302 if (etype == etype_list[i]) {
303 break;
307 return ARRAY_SIZE(etype_list) - i;
310 static int sdb_key_strength_cmp(const struct sdb_key *k1, const struct sdb_key *k2)
312 int p1 = sdb_key_strength_priority(KRB5_KEY_TYPE(&k1->key));
313 int p2 = sdb_key_strength_priority(KRB5_KEY_TYPE(&k2->key));
315 if (p1 == p2) {
316 return 0;
319 if (p1 > p2) {
321 * Higher priority comes first
323 return -1;
324 } else {
325 return 1;
329 static void samba_kdc_sort_keys(struct sdb_keys *keys)
331 if (keys == NULL) {
332 return;
335 TYPESAFE_QSORT(keys->val, keys->len, sdb_key_strength_cmp);
338 int samba_kdc_set_fixed_keys(krb5_context context,
339 const struct ldb_val *secretbuffer,
340 uint32_t supported_enctypes,
341 struct sdb_keys *keys)
343 uint16_t allocated_keys = 0;
344 int ret;
346 allocated_keys = 3;
347 keys->len = 0;
348 keys->val = calloc(allocated_keys, sizeof(struct sdb_key));
349 if (keys->val == NULL) {
350 memset(secretbuffer->data, 0, secretbuffer->length);
351 ret = ENOMEM;
352 goto out;
355 if (supported_enctypes & ENC_HMAC_SHA1_96_AES256) {
356 struct sdb_key key = {};
358 ret = smb_krb5_keyblock_init_contents(context,
359 ENCTYPE_AES256_CTS_HMAC_SHA1_96,
360 secretbuffer->data,
361 MIN(secretbuffer->length, 32),
362 &key.key);
363 if (ret) {
364 memset(secretbuffer->data, 0, secretbuffer->length);
365 goto out;
368 keys->val[keys->len] = key;
369 keys->len++;
372 if (supported_enctypes & ENC_HMAC_SHA1_96_AES128) {
373 struct sdb_key key = {};
375 ret = smb_krb5_keyblock_init_contents(context,
376 ENCTYPE_AES128_CTS_HMAC_SHA1_96,
377 secretbuffer->data,
378 MIN(secretbuffer->length, 16),
379 &key.key);
380 if (ret) {
381 memset(secretbuffer->data, 0, secretbuffer->length);
382 goto out;
385 keys->val[keys->len] = key;
386 keys->len++;
389 if (supported_enctypes & ENC_RC4_HMAC_MD5) {
390 struct sdb_key key = {};
392 ret = smb_krb5_keyblock_init_contents(context,
393 ENCTYPE_ARCFOUR_HMAC,
394 secretbuffer->data,
395 MIN(secretbuffer->length, 16),
396 &key.key);
397 if (ret) {
398 memset(secretbuffer->data, 0, secretbuffer->length);
399 goto out;
402 keys->val[keys->len] = key;
403 keys->len++;
405 ret = 0;
406 out:
407 return ret;
411 static int samba_kdc_set_random_keys(krb5_context context,
412 uint32_t supported_enctypes,
413 struct sdb_keys *keys)
415 struct ldb_val secret_val;
416 uint8_t secretbuffer[32];
419 * Fake keys until we have a better way to reject
420 * non-pkinit requests.
422 * We just need to indicate which encryption types are
423 * supported.
425 generate_secret_buffer(secretbuffer, sizeof(secretbuffer));
427 secret_val = data_blob_const(secretbuffer,
428 sizeof(secretbuffer));
429 return samba_kdc_set_fixed_keys(context,
430 &secret_val,
431 supported_enctypes,
432 keys);
435 struct samba_kdc_user_keys {
436 struct sdb_keys *skeys;
437 uint32_t kvno;
438 uint32_t *returned_kvno;
439 uint32_t supported_enctypes;
440 uint32_t *available_enctypes;
441 const struct samr_Password *nthash;
442 const char *salt_string;
443 uint16_t num_pkeys;
444 const struct package_PrimaryKerberosKey4 *pkeys;
447 static krb5_error_code samba_kdc_fill_user_keys(krb5_context context,
448 struct samba_kdc_user_keys *p)
451 * Make sure we'll never reveal DES keys
453 uint32_t supported_enctypes = p->supported_enctypes &= ~(ENC_CRC32 | ENC_RSA_MD5);
454 uint32_t _available_enctypes = 0;
455 uint32_t *available_enctypes = p->available_enctypes;
456 uint32_t _returned_kvno = 0;
457 uint32_t *returned_kvno = p->returned_kvno;
458 uint32_t num_pkeys = p->num_pkeys;
459 uint32_t allocated_keys = num_pkeys;
460 uint32_t i;
461 int ret;
463 if (available_enctypes == NULL) {
464 available_enctypes = &_available_enctypes;
467 *available_enctypes = 0;
469 if (returned_kvno == NULL) {
470 returned_kvno = &_returned_kvno;
473 *returned_kvno = p->kvno;
475 if (p->nthash != NULL) {
476 allocated_keys += 1;
479 allocated_keys = MAX(1, allocated_keys);
481 /* allocate space to decode into */
482 p->skeys->len = 0;
483 p->skeys->val = calloc(allocated_keys, sizeof(struct sdb_key));
484 if (p->skeys->val == NULL) {
485 return ENOMEM;
488 for (i=0; i < num_pkeys; i++) {
489 struct sdb_key key = {};
490 uint32_t enctype_bit;
492 if (p->pkeys[i].value == NULL) {
493 continue;
496 enctype_bit = kerberos_enctype_to_bitmap(p->pkeys[i].keytype);
497 if (!(enctype_bit & supported_enctypes)) {
498 continue;
501 if (p->salt_string != NULL) {
502 DATA_BLOB salt;
504 salt = data_blob_string_const(p->salt_string);
506 key.salt = calloc(1, sizeof(*key.salt));
507 if (key.salt == NULL) {
508 ret = ENOMEM;
509 goto fail;
512 key.salt->type = KRB5_PW_SALT;
514 ret = smb_krb5_copy_data_contents(&key.salt->salt,
515 salt.data,
516 salt.length);
517 if (ret) {
518 ZERO_STRUCTP(key.salt);
519 sdb_key_free(&key);
520 goto fail;
524 ret = smb_krb5_keyblock_init_contents(context,
525 p->pkeys[i].keytype,
526 p->pkeys[i].value->data,
527 p->pkeys[i].value->length,
528 &key.key);
529 if (ret == 0) {
530 p->skeys->val[p->skeys->len++] = key;
531 *available_enctypes |= enctype_bit;
532 continue;
534 ZERO_STRUCT(key.key);
535 sdb_key_free(&key);
536 if (ret == KRB5_PROG_ETYPE_NOSUPP) {
537 DEBUG(2,("Unsupported keytype ignored - type %u\n",
538 p->pkeys[i].keytype));
539 ret = 0;
540 continue;
543 goto fail;
546 if (p->nthash != NULL && (supported_enctypes & ENC_RC4_HMAC_MD5)) {
547 struct sdb_key key = {};
549 ret = smb_krb5_keyblock_init_contents(context,
550 ENCTYPE_ARCFOUR_HMAC,
551 p->nthash->hash,
552 sizeof(p->nthash->hash),
553 &key.key);
554 if (ret == 0) {
555 p->skeys->val[p->skeys->len++] = key;
557 *available_enctypes |= ENC_RC4_HMAC_MD5;
558 } else if (ret == KRB5_PROG_ETYPE_NOSUPP) {
559 DEBUG(2,("Unsupported keytype ignored - type %u\n",
560 ENCTYPE_ARCFOUR_HMAC));
561 ret = 0;
563 if (ret != 0) {
564 goto fail;
568 samba_kdc_sort_keys(p->skeys);
570 return 0;
571 fail:
572 sdb_keys_free(p->skeys);
573 return ret;
576 krb5_error_code samba_kdc_message2entry_keys(krb5_context context,
577 TALLOC_CTX *mem_ctx,
578 const struct ldb_message *msg,
579 bool is_krbtgt,
580 bool is_rodc,
581 uint32_t userAccountControl,
582 enum samba_kdc_ent_type ent_type,
583 unsigned flags,
584 krb5_kvno requested_kvno,
585 struct sdb_entry *entry,
586 const uint32_t supported_enctypes_in,
587 uint32_t *supported_enctypes_out)
589 krb5_error_code ret = 0;
590 enum ndr_err_code ndr_err;
591 struct samr_Password *hash;
592 unsigned int num_ntPwdHistory = 0;
593 struct samr_Password *ntPwdHistory = NULL;
594 struct samr_Password *old_hash = NULL;
595 struct samr_Password *older_hash = NULL;
596 const struct ldb_val *sc_val;
597 struct supplementalCredentialsBlob scb;
598 struct supplementalCredentialsPackage *scpk = NULL;
599 struct package_PrimaryKerberosBlob _pkb;
600 struct package_PrimaryKerberosCtr4 *pkb4 = NULL;
601 int krbtgt_number = 0;
602 uint32_t current_kvno;
603 uint32_t old_kvno = 0;
604 uint32_t older_kvno = 0;
605 uint32_t returned_kvno = 0;
606 uint16_t i;
607 struct samba_kdc_user_keys keys = { .num_pkeys = 0, };
608 struct samba_kdc_user_keys old_keys = { .num_pkeys = 0, };
609 struct samba_kdc_user_keys older_keys = { .num_pkeys = 0, };
610 uint32_t available_enctypes = 0;
611 uint32_t supported_enctypes = supported_enctypes_in;
613 *supported_enctypes_out = 0;
615 /* Is this the krbtgt or a RODC krbtgt */
616 if (is_rodc) {
617 krbtgt_number = ldb_msg_find_attr_as_int(msg, "msDS-SecondaryKrbTgtNumber", -1);
619 if (krbtgt_number == -1) {
620 return EINVAL;
622 if (krbtgt_number == 0) {
623 return EINVAL;
627 if ((ent_type == SAMBA_KDC_ENT_TYPE_CLIENT)
628 && (userAccountControl & UF_SMARTCARD_REQUIRED)) {
629 ret = samba_kdc_set_random_keys(context,
630 supported_enctypes,
631 &entry->keys);
633 *supported_enctypes_out = supported_enctypes & ENC_ALL_TYPES;
635 goto out;
638 current_kvno = ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0);
639 if (current_kvno > 1) {
640 old_kvno = current_kvno - 1;
642 if (current_kvno > 2) {
643 older_kvno = current_kvno - 2;
645 if (is_krbtgt) {
647 * Even for the main krbtgt account
648 * we have to strictly split the kvno into
649 * two 16-bit parts and the upper 16-bit
650 * need to be all zero, even if
651 * the msDS-KeyVersionNumber has a value
652 * larger than 65535.
654 * See https://bugzilla.samba.org/show_bug.cgi?id=14951
656 current_kvno = SAMBA_KVNO_GET_VALUE(current_kvno);
657 old_kvno = SAMBA_KVNO_GET_VALUE(old_kvno);
658 older_kvno = SAMBA_KVNO_GET_VALUE(older_kvno);
659 requested_kvno = SAMBA_KVNO_GET_VALUE(requested_kvno);
662 /* Get keys from the db */
664 hash = samdb_result_hash(mem_ctx, msg, "unicodePwd");
665 num_ntPwdHistory = samdb_result_hashes(mem_ctx, msg,
666 "ntPwdHistory",
667 &ntPwdHistory);
668 if (num_ntPwdHistory > 1) {
669 old_hash = &ntPwdHistory[1];
671 if (num_ntPwdHistory > 2) {
672 older_hash = &ntPwdHistory[1];
674 sc_val = ldb_msg_find_ldb_val(msg, "supplementalCredentials");
676 /* supplementalCredentials if present */
677 if (sc_val) {
678 ndr_err = ndr_pull_struct_blob_all(sc_val, mem_ctx, &scb,
679 (ndr_pull_flags_fn_t)ndr_pull_supplementalCredentialsBlob);
680 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
681 dump_data(0, sc_val->data, sc_val->length);
682 ret = EINVAL;
683 goto out;
686 if (scb.sub.signature != SUPPLEMENTAL_CREDENTIALS_SIGNATURE) {
687 if (scb.sub.num_packages != 0) {
688 NDR_PRINT_DEBUG(supplementalCredentialsBlob, &scb);
689 ret = EINVAL;
690 goto out;
694 for (i=0; i < scb.sub.num_packages; i++) {
695 if (strcmp("Primary:Kerberos-Newer-Keys", scb.sub.packages[i].name) == 0) {
696 scpk = &scb.sub.packages[i];
697 if (!scpk->data || !scpk->data[0]) {
698 scpk = NULL;
699 continue;
701 break;
706 * Primary:Kerberos-Newer-Keys element
707 * of supplementalCredentials
709 * The legacy Primary:Kerberos only contains
710 * single DES keys, which are completely ignored
711 * now.
713 if (scpk) {
714 DATA_BLOB blob;
716 blob = strhex_to_data_blob(mem_ctx, scpk->data);
717 if (!blob.data) {
718 ret = ENOMEM;
719 goto out;
722 /* we cannot use ndr_pull_struct_blob_all() here, as w2k and w2k3 add padding bytes */
723 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &_pkb,
724 (ndr_pull_flags_fn_t)ndr_pull_package_PrimaryKerberosBlob);
725 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
726 ret = EINVAL;
727 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
728 krb5_warnx(context, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
729 goto out;
732 if (_pkb.version != 4) {
733 ret = EINVAL;
734 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
735 krb5_warnx(context, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
736 goto out;
739 pkb4 = &_pkb.ctr.ctr4;
742 keys = (struct samba_kdc_user_keys) {
743 .kvno = current_kvno,
744 .supported_enctypes = supported_enctypes,
745 .nthash = hash,
746 .salt_string = pkb4 != NULL ? pkb4->salt.string : NULL,
747 .num_pkeys = pkb4 != NULL ? pkb4->num_keys : 0,
748 .pkeys = pkb4 != NULL ? pkb4->keys : NULL,
751 old_keys = (struct samba_kdc_user_keys) {
752 .kvno = old_kvno,
753 .supported_enctypes = supported_enctypes,
754 .nthash = old_hash,
755 .salt_string = pkb4 != NULL ? pkb4->salt.string : NULL,
756 .num_pkeys = pkb4 != NULL ? pkb4->num_old_keys : 0,
757 .pkeys = pkb4 != NULL ? pkb4->old_keys : NULL,
759 older_keys = (struct samba_kdc_user_keys) {
760 .kvno = older_kvno,
761 .supported_enctypes = supported_enctypes,
762 .nthash = older_hash,
763 .salt_string = pkb4 != NULL ? pkb4->salt.string : NULL,
764 .num_pkeys = pkb4 != NULL ? pkb4->num_older_keys : 0,
765 .pkeys = pkb4 != NULL ? pkb4->older_keys : NULL,
768 if (flags & SDB_F_KVNO_SPECIFIED) {
769 if (requested_kvno == keys.kvno) {
771 * The current kvno was requested,
772 * so we return it.
774 keys.skeys = &entry->keys;
775 keys.available_enctypes = &available_enctypes;
776 keys.returned_kvno = &returned_kvno;
777 } else if (requested_kvno == 0) {
779 * don't return any keys
781 } else if (requested_kvno == old_keys.kvno) {
783 * return the old keys as default keys
784 * with the requested kvno.
786 old_keys.skeys = &entry->keys;
787 old_keys.available_enctypes = &available_enctypes;
788 old_keys.returned_kvno = &returned_kvno;
789 } else if (requested_kvno == older_keys.kvno) {
791 * return the older keys as default keys
792 * with the requested kvno.
794 older_keys.skeys = &entry->keys;
795 older_keys.available_enctypes = &available_enctypes;
796 older_keys.returned_kvno = &returned_kvno;
797 } else {
799 * don't return any keys
802 } else {
803 bool include_history = false;
805 if ((flags & SDB_F_GET_CLIENT) && (flags & SDB_F_FOR_AS_REQ)) {
806 include_history = true;
807 } else if (flags & SDB_F_ADMIN_DATA) {
808 include_history = true;
811 keys.skeys = &entry->keys;
812 keys.available_enctypes = &available_enctypes;
813 keys.returned_kvno = &returned_kvno;
815 if (include_history && old_keys.kvno != 0) {
816 old_keys.skeys = &entry->old_keys;
818 if (include_history && older_keys.kvno != 0) {
819 older_keys.skeys = &entry->older_keys;
823 if (keys.skeys != NULL) {
824 ret = samba_kdc_fill_user_keys(context, &keys);
825 if (ret != 0) {
826 goto out;
830 if (old_keys.skeys != NULL) {
831 ret = samba_kdc_fill_user_keys(context, &old_keys);
832 if (ret != 0) {
833 goto out;
837 if (older_keys.skeys != NULL) {
838 ret = samba_kdc_fill_user_keys(context, &older_keys);
839 if (ret != 0) {
840 goto out;
844 *supported_enctypes_out |= available_enctypes;
846 if (is_krbtgt) {
848 * Even for the main krbtgt account
849 * we have to strictly split the kvno into
850 * two 16-bit parts and the upper 16-bit
851 * need to be all zero, even if
852 * the msDS-KeyVersionNumber has a value
853 * larger than 65535.
855 * See https://bugzilla.samba.org/show_bug.cgi?id=14951
857 returned_kvno = SAMBA_KVNO_AND_KRBTGT(returned_kvno, krbtgt_number);
859 entry->kvno = returned_kvno;
861 out:
862 return ret;
865 static int principal_comp_strcmp_int(krb5_context context,
866 krb5_const_principal principal,
867 unsigned int component,
868 const char *string,
869 bool do_strcasecmp)
871 const char *p;
873 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
874 p = krb5_principal_get_comp_string(context, principal, component);
875 if (p == NULL) {
876 return -1;
878 if (do_strcasecmp) {
879 return strcasecmp(p, string);
880 } else {
881 return strcmp(p, string);
883 #else
884 size_t len;
885 krb5_data *d;
886 if (component >= krb5_princ_size(context, principal)) {
887 return -1;
890 d = krb5_princ_component(context, principal, component);
891 if (d == NULL) {
892 return -1;
895 p = d->data;
897 len = strlen(string);
900 * We explicitly return -1 or 1. Subtracting of the two lengths might
901 * give the wrong result if the result overflows or loses data when
902 * narrowed to int.
904 if (d->length < len) {
905 return -1;
906 } else if (d->length > len) {
907 return 1;
910 if (do_strcasecmp) {
911 return strncasecmp(p, string, len);
912 } else {
913 return memcmp(p, string, len);
915 #endif
918 static int principal_comp_strcasecmp(krb5_context context,
919 krb5_const_principal principal,
920 unsigned int component,
921 const char *string)
923 return principal_comp_strcmp_int(context, principal,
924 component, string, true);
927 static int principal_comp_strcmp(krb5_context context,
928 krb5_const_principal principal,
929 unsigned int component,
930 const char *string)
932 return principal_comp_strcmp_int(context, principal,
933 component, string, false);
936 static bool is_kadmin_changepw(krb5_context context,
937 krb5_const_principal principal)
939 return krb5_princ_size(context, principal) == 2 &&
940 (principal_comp_strcmp(context, principal, 0, "kadmin") == 0) &&
941 (principal_comp_strcmp(context, principal, 1, "changepw") == 0);
944 static krb5_error_code samba_kdc_get_entry_principal(
945 krb5_context context,
946 struct samba_kdc_db_context *kdc_db_ctx,
947 const char *samAccountName,
948 enum samba_kdc_ent_type ent_type,
949 unsigned flags,
950 bool is_kadmin_changepw,
951 krb5_const_principal in_princ,
952 krb5_principal *out_princ)
954 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
955 krb5_error_code code = 0;
956 bool canon = flags & (SDB_F_CANON|SDB_F_FORCE_CANON);
959 * If we are set to canonicalize, we get back the fixed UPPER
960 * case realm, and the real username (ie matching LDAP
961 * samAccountName)
963 * Otherwise, if we are set to enterprise, we
964 * get back the whole principal as-sent
966 * Finally, if we are not set to canonicalize, we get back the
967 * fixed UPPER case realm, but the as-sent username
971 * We need to ensure that the kadmin/changepw principal isn't able to
972 * issue krbtgt tickets, even if canonicalization is turned on.
974 if (!is_kadmin_changepw) {
975 if (ent_type == SAMBA_KDC_ENT_TYPE_KRBTGT && canon) {
977 * When requested to do so, ensure that the
978 * both realm values in the principal are set
979 * to the upper case, canonical realm
981 code = smb_krb5_make_principal(context,
982 out_princ,
983 lpcfg_realm(lp_ctx),
984 "krbtgt",
985 lpcfg_realm(lp_ctx),
986 NULL);
987 if (code != 0) {
988 return code;
990 smb_krb5_principal_set_type(context,
991 *out_princ,
992 KRB5_NT_SRV_INST);
994 return 0;
997 if ((canon && flags & (SDB_F_FORCE_CANON|SDB_F_FOR_AS_REQ)) ||
998 (ent_type == SAMBA_KDC_ENT_TYPE_ANY && in_princ == NULL)) {
1000 * SDB_F_CANON maps from the canonicalize flag in the
1001 * packet, and has a different meaning between AS-REQ
1002 * and TGS-REQ. We only change the principal in the
1003 * AS-REQ case.
1005 * The SDB_F_FORCE_CANON if for new MIT KDC code that
1006 * wants the canonical name in all lookups, and takes
1007 * care to canonicalize only when appropriate.
1009 code = smb_krb5_make_principal(context,
1010 out_princ,
1011 lpcfg_realm(lp_ctx),
1012 samAccountName,
1013 NULL);
1014 return code;
1019 * For a krbtgt entry, this appears to be required regardless of the
1020 * canonicalize flag from the client.
1022 code = krb5_copy_principal(context, in_princ, out_princ);
1023 if (code != 0) {
1024 return code;
1028 * While we have copied the client principal, tests show that Win2k3
1029 * returns the 'corrected' realm, not the client-specified realm. This
1030 * code attempts to replace the client principal's realm with the one
1031 * we determine from our records
1033 code = smb_krb5_principal_set_realm(context,
1034 *out_princ,
1035 lpcfg_realm(lp_ctx));
1037 return code;
1041 * Construct an hdb_entry from a directory entry.
1043 static krb5_error_code samba_kdc_message2entry(krb5_context context,
1044 struct samba_kdc_db_context *kdc_db_ctx,
1045 TALLOC_CTX *mem_ctx,
1046 krb5_const_principal principal,
1047 enum samba_kdc_ent_type ent_type,
1048 unsigned flags,
1049 krb5_kvno kvno,
1050 struct ldb_dn *realm_dn,
1051 struct ldb_message *msg,
1052 struct sdb_entry *entry)
1054 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
1055 uint32_t userAccountControl;
1056 uint32_t msDS_User_Account_Control_Computed;
1057 krb5_error_code ret = 0;
1058 krb5_boolean is_computer = FALSE;
1059 struct samba_kdc_entry *p;
1060 NTTIME acct_expiry;
1061 NTSTATUS status;
1062 bool protected_user = false;
1063 uint32_t rid;
1064 bool is_krbtgt = false;
1065 bool is_rodc = false;
1066 bool force_rc4 = lpcfg_kdc_force_enable_rc4_weak_session_keys(lp_ctx);
1067 struct ldb_message_element *objectclasses;
1068 struct ldb_val computer_val = data_blob_string_const("computer");
1069 uint32_t config_default_supported_enctypes = lpcfg_kdc_default_domain_supported_enctypes(lp_ctx);
1070 uint32_t default_supported_enctypes =
1071 config_default_supported_enctypes != 0 ?
1072 config_default_supported_enctypes :
1073 ENC_RC4_HMAC_MD5 | ENC_HMAC_SHA1_96_AES256_SK;
1074 uint32_t supported_enctypes
1075 = ldb_msg_find_attr_as_uint(msg,
1076 "msDS-SupportedEncryptionTypes",
1077 default_supported_enctypes);
1078 uint32_t pa_supported_enctypes;
1079 uint32_t supported_session_etypes;
1080 uint32_t available_enctypes = 0;
1082 * also lagacy enctypes are announced,
1083 * but effectively restricted by kdc_enctypes
1085 uint32_t domain_enctypes = ENC_RC4_HMAC_MD5 | ENC_RSA_MD5 | ENC_CRC32;
1086 uint32_t config_kdc_enctypes = lpcfg_kdc_supported_enctypes(lp_ctx);
1087 uint32_t kdc_enctypes =
1088 config_kdc_enctypes != 0 ?
1089 config_kdc_enctypes :
1090 ENC_ALL_TYPES;
1091 const char *samAccountName = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
1093 const struct authn_kerberos_client_policy *authn_client_policy = NULL;
1094 const struct authn_server_policy *authn_server_policy = NULL;
1095 int64_t enforced_tgt_lifetime_raw;
1097 ZERO_STRUCTP(entry);
1099 if (supported_enctypes == 0) {
1100 supported_enctypes = default_supported_enctypes;
1103 if (dsdb_functional_level(kdc_db_ctx->samdb) >= DS_DOMAIN_FUNCTION_2008) {
1104 domain_enctypes |= ENC_HMAC_SHA1_96_AES128 | ENC_HMAC_SHA1_96_AES256;
1107 if (ldb_msg_find_element(msg, "msDS-SecondaryKrbTgtNumber")) {
1108 is_rodc = true;
1111 if (!samAccountName) {
1112 ret = ENOENT;
1113 krb5_set_error_message(context, ret, "samba_kdc_message2entry: no samAccountName present");
1114 goto out;
1117 objectclasses = ldb_msg_find_element(msg, "objectClass");
1119 if (objectclasses && ldb_msg_find_val(objectclasses, &computer_val)) {
1120 is_computer = TRUE;
1123 p = talloc_zero(mem_ctx, struct samba_kdc_entry);
1124 if (!p) {
1125 ret = ENOMEM;
1126 goto out;
1129 p->is_rodc = is_rodc;
1130 p->kdc_db_ctx = kdc_db_ctx;
1131 p->realm_dn = talloc_reference(p, realm_dn);
1132 if (!p->realm_dn) {
1133 ret = ENOMEM;
1134 goto out;
1137 talloc_set_destructor(p, samba_kdc_entry_destructor);
1139 entry->skdc_entry = p;
1141 userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
1143 msDS_User_Account_Control_Computed
1144 = ldb_msg_find_attr_as_uint(msg,
1145 "msDS-User-Account-Control-Computed",
1146 UF_ACCOUNTDISABLE);
1149 * This brings in the lockout flag, block the account if not
1150 * found. We need the weird UF_ACCOUNTDISABLE check because
1151 * we do not want to fail open if the value is not returned,
1152 * but 0 is a valid value (all OK)
1154 if (msDS_User_Account_Control_Computed == UF_ACCOUNTDISABLE) {
1155 ret = EINVAL;
1156 krb5_set_error_message(context, ret, "samba_kdc_message2entry: "
1157 "no msDS-User-Account-Control-Computed present");
1158 goto out;
1159 } else {
1160 userAccountControl |= msDS_User_Account_Control_Computed;
1163 if (ent_type == SAMBA_KDC_ENT_TYPE_KRBTGT) {
1164 p->is_krbtgt = true;
1167 /* First try and figure out the flags based on the userAccountControl */
1168 entry->flags = uf2SDBFlags(context, userAccountControl, ent_type);
1171 * Take control of the returned principal here, rather than
1172 * allowing the Heimdal code to do it as we have specific
1173 * behaviour around the forced realm to honour
1175 entry->flags.force_canonicalize = true;
1177 /* Windows 2008 seems to enforce this (very sensible) rule by
1178 * default - don't allow offline attacks on a user's password
1179 * by asking for a ticket to them as a service (encrypted with
1180 * their probably patheticly insecure password) */
1182 if (entry->flags.server
1183 && lpcfg_parm_bool(lp_ctx, NULL, "kdc", "require spn for service", true)) {
1184 if (!is_computer && !ldb_msg_find_attr_as_string(msg, "servicePrincipalName", NULL)) {
1185 entry->flags.server = 0;
1190 * We restrict a 3-part SPN ending in my domain/realm to full
1191 * domain controllers.
1193 * This avoids any cases where (eg) a demoted DC still has
1194 * these more restricted SPNs.
1196 if (krb5_princ_size(context, principal) > 2) {
1197 char *third_part
1198 = smb_krb5_principal_get_comp_string(mem_ctx,
1199 context,
1200 principal,
1202 bool is_our_realm =
1203 lpcfg_is_my_domain_or_realm(lp_ctx,
1204 third_part);
1205 bool is_dc = userAccountControl &
1206 (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT);
1207 if (is_our_realm && !is_dc) {
1208 entry->flags.server = 0;
1212 * To give the correct type of error to the client, we must
1213 * not just return the entry without .server set, we must
1214 * pretend the principal does not exist. Otherwise we may
1215 * return ERR_POLICY instead of
1216 * KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
1218 if (ent_type == SAMBA_KDC_ENT_TYPE_SERVER && entry->flags.server == 0) {
1219 ret = SDB_ERR_NOENTRY;
1220 krb5_set_error_message(context, ret, "samba_kdc_message2entry: no servicePrincipalName present for this server, refusing with no-such-entry");
1221 goto out;
1223 if (flags & SDB_F_ADMIN_DATA) {
1224 /* These (created_by, modified_by) parts of the entry are not relevant for Samba4's use
1225 * of the Heimdal KDC. They are stored in a the traditional
1226 * DB for audit purposes, and still form part of the structure
1227 * we must return */
1229 /* use 'whenCreated' */
1230 entry->created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
1231 /* use 'kadmin' for now (needed by mit_samba) */
1233 ret = smb_krb5_make_principal(context,
1234 &entry->created_by.principal,
1235 lpcfg_realm(lp_ctx), "kadmin", NULL);
1236 if (ret) {
1237 krb5_clear_error_message(context);
1238 goto out;
1241 entry->modified_by = (struct sdb_event *) malloc(sizeof(struct sdb_event));
1242 if (entry->modified_by == NULL) {
1243 ret = ENOMEM;
1244 krb5_set_error_message(context, ret, "malloc: out of memory");
1245 goto out;
1248 /* use 'whenChanged' */
1249 entry->modified_by->time = ldb_msg_find_krb5time_ldap_time(msg, "whenChanged", 0);
1250 /* use 'kadmin' for now (needed by mit_samba) */
1251 ret = smb_krb5_make_principal(context,
1252 &entry->modified_by->principal,
1253 lpcfg_realm(lp_ctx), "kadmin", NULL);
1254 if (ret) {
1255 krb5_clear_error_message(context);
1256 goto out;
1261 /* The lack of password controls etc applies to krbtgt by
1262 * virtue of being that particular RID */
1263 status = dom_sid_split_rid(NULL, samdb_result_dom_sid(mem_ctx, msg, "objectSid"), NULL, &rid);
1265 if (!NT_STATUS_IS_OK(status)) {
1266 ret = EINVAL;
1267 goto out;
1270 if (rid == DOMAIN_RID_KRBTGT) {
1271 char *realm = NULL;
1273 entry->valid_end = NULL;
1274 entry->pw_end = NULL;
1276 entry->flags.invalid = 0;
1277 entry->flags.server = 1;
1279 realm = smb_krb5_principal_get_realm(
1280 mem_ctx, context, principal);
1281 if (realm == NULL) {
1282 ret = ENOMEM;
1283 goto out;
1286 /* Don't mark all requests for the krbtgt/realm as
1287 * 'change password', as otherwise we could get into
1288 * trouble, and not enforce the password expirty.
1289 * Instead, only do it when request is for the kpasswd service */
1290 if (ent_type == SAMBA_KDC_ENT_TYPE_SERVER &&
1291 is_kadmin_changepw(context, principal) &&
1292 lpcfg_is_my_domain_or_realm(lp_ctx, realm)) {
1293 entry->flags.change_pw = 1;
1296 TALLOC_FREE(realm);
1298 entry->flags.client = 0;
1299 entry->flags.forwardable = 1;
1300 entry->flags.ok_as_delegate = 1;
1301 } else if (is_rodc) {
1302 /* The RODC krbtgt account is like the main krbtgt,
1303 * but it does not have a changepw or kadmin
1304 * service */
1306 entry->valid_end = NULL;
1307 entry->pw_end = NULL;
1309 /* Also don't allow the RODC krbtgt to be a client (it should not be needed) */
1310 entry->flags.client = 0;
1311 entry->flags.invalid = 0;
1312 entry->flags.server = 1;
1314 entry->flags.client = 0;
1315 entry->flags.forwardable = 1;
1316 entry->flags.ok_as_delegate = 0;
1317 } else if (entry->flags.server && ent_type == SAMBA_KDC_ENT_TYPE_SERVER) {
1318 /* The account/password expiry only applies when the account is used as a
1319 * client (ie password login), not when used as a server */
1321 /* Make very well sure we don't use this for a client,
1322 * it could bypass the password restrictions */
1323 entry->flags.client = 0;
1325 entry->valid_end = NULL;
1326 entry->pw_end = NULL;
1328 } else {
1329 NTTIME must_change_time
1330 = samdb_result_nttime(msg,
1331 "msDS-UserPasswordExpiryTimeComputed",
1333 if (must_change_time == 0x7FFFFFFFFFFFFFFFULL) {
1334 entry->pw_end = NULL;
1335 } else {
1336 entry->pw_end = malloc(sizeof(*entry->pw_end));
1337 if (entry->pw_end == NULL) {
1338 ret = ENOMEM;
1339 goto out;
1341 *entry->pw_end = nt_time_to_unix(must_change_time);
1344 acct_expiry = samdb_result_account_expires(msg);
1345 if (acct_expiry == 0x7FFFFFFFFFFFFFFFULL) {
1346 entry->valid_end = NULL;
1347 } else {
1348 entry->valid_end = malloc(sizeof(*entry->valid_end));
1349 if (entry->valid_end == NULL) {
1350 ret = ENOMEM;
1351 goto out;
1353 *entry->valid_end = nt_time_to_unix(acct_expiry);
1357 ret = samba_kdc_get_entry_principal(context,
1358 kdc_db_ctx,
1359 samAccountName,
1360 ent_type,
1361 flags,
1362 entry->flags.change_pw,
1363 principal,
1364 &entry->principal);
1365 if (ret != 0) {
1366 krb5_clear_error_message(context);
1367 goto out;
1370 entry->valid_start = NULL;
1372 entry->max_life = malloc(sizeof(*entry->max_life));
1373 if (entry->max_life == NULL) {
1374 ret = ENOMEM;
1375 goto out;
1378 if (ent_type == SAMBA_KDC_ENT_TYPE_SERVER) {
1379 *entry->max_life = kdc_db_ctx->policy.svc_tkt_lifetime;
1380 } else if (ent_type == SAMBA_KDC_ENT_TYPE_KRBTGT || ent_type == SAMBA_KDC_ENT_TYPE_CLIENT) {
1381 *entry->max_life = kdc_db_ctx->policy.usr_tkt_lifetime;
1382 } else {
1383 *entry->max_life = MIN(kdc_db_ctx->policy.svc_tkt_lifetime,
1384 kdc_db_ctx->policy.usr_tkt_lifetime);
1387 if (entry->flags.change_pw) {
1388 /* Limit lifetime of kpasswd tickets to two minutes or less. */
1389 *entry->max_life = MIN(*entry->max_life, CHANGEPW_LIFETIME);
1392 entry->max_renew = malloc(sizeof(*entry->max_renew));
1393 if (entry->max_renew == NULL) {
1394 ret = ENOMEM;
1395 goto out;
1398 *entry->max_renew = kdc_db_ctx->policy.renewal_lifetime;
1401 * A principal acting as a client that is not being looked up as the
1402 * principal of an armor ticket may have an authentication policy apply
1403 * to it.
1405 * We won’t get an authentication policy for the client of an S4U2Self
1406 * or S4U2Proxy request. Those clients are looked up with
1407 * SDB_F_FOR_TGS_REQ instead of with SDB_F_FOR_AS_REQ.
1409 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT &&
1410 (flags & SDB_F_FOR_AS_REQ) &&
1411 !(flags & SDB_F_ARMOR_PRINCIPAL))
1413 ret = authn_policy_kerberos_client(kdc_db_ctx->samdb, mem_ctx, msg,
1414 &authn_client_policy);
1415 if (ret) {
1416 goto out;
1421 * A principal acting as a server may have an authentication policy
1422 * apply to it.
1424 if (ent_type == SAMBA_KDC_ENT_TYPE_SERVER) {
1425 ret = authn_policy_server(kdc_db_ctx->samdb, mem_ctx, msg,
1426 &authn_server_policy);
1427 if (ret) {
1428 goto out;
1432 enforced_tgt_lifetime_raw = authn_policy_enforced_tgt_lifetime_raw(authn_client_policy);
1433 if (enforced_tgt_lifetime_raw != 0) {
1434 int64_t lifetime_secs = enforced_tgt_lifetime_raw;
1436 lifetime_secs /= INT64_C(1000) * 1000 * 10;
1437 lifetime_secs = MIN(lifetime_secs, INT_MAX);
1438 lifetime_secs = MAX(lifetime_secs, INT_MIN);
1441 * Set both lifetime and renewal time based only on the
1442 * configured maximum lifetime — not on the configured renewal
1443 * time. Yes, this is what Windows does.
1445 lifetime_secs = MIN(*entry->max_life, lifetime_secs);
1446 *entry->max_life = lifetime_secs;
1447 *entry->max_renew = lifetime_secs;
1450 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT && (flags & SDB_F_FOR_AS_REQ)) {
1451 int result;
1452 const struct auth_user_info_dc *user_info_dc = NULL;
1454 * These protections only apply to clients, so servers in the
1455 * Protected Users group may still have service tickets to them
1456 * encrypted with RC4. For accounts looked up as servers, note
1457 * that 'msg' does not contain the 'memberOf' attribute for
1458 * determining whether the account is a member of Protected
1459 * Users.
1461 * Additionally, Microsoft advises that accounts for services
1462 * and computers should never be members of Protected Users, or
1463 * they may fail to authenticate.
1465 status = samba_kdc_get_user_info_from_db(p, msg, &user_info_dc);
1466 if (!NT_STATUS_IS_OK(status)) {
1467 ret = EINVAL;
1468 goto out;
1471 result = dsdb_is_protected_user(kdc_db_ctx->samdb,
1472 user_info_dc->sids,
1473 user_info_dc->num_sids);
1474 if (result == -1) {
1475 ret = EINVAL;
1476 goto out;
1479 protected_user = result;
1481 if (protected_user && enforced_tgt_lifetime_raw == 0)
1484 * If a TGT lifetime hasn’t been set, Protected Users
1485 * enforces a four hour TGT lifetime.
1487 *entry->max_life = MIN(*entry->max_life, 4 * 60 * 60);
1488 *entry->max_renew = MIN(*entry->max_renew, 4 * 60 * 60);
1490 entry->flags.forwardable = 0;
1491 entry->flags.proxiable = 0;
1495 if (rid == DOMAIN_RID_KRBTGT || is_rodc) {
1496 bool enable_fast;
1498 is_krbtgt = true;
1501 * KDCs (and KDCs on RODCs)
1502 * ignore msDS-SupportedEncryptionTypes completely
1503 * but support all supported enctypes by the domain.
1505 supported_enctypes = domain_enctypes;
1507 enable_fast = lpcfg_kdc_enable_fast(kdc_db_ctx->lp_ctx);
1508 if (enable_fast) {
1509 supported_enctypes |= ENC_FAST_SUPPORTED;
1512 supported_enctypes |= ENC_CLAIMS_SUPPORTED;
1513 supported_enctypes |= ENC_COMPOUND_IDENTITY_SUPPORTED;
1516 * Resource SID compression is enabled implicitly, unless
1517 * disabled in msDS-SupportedEncryptionTypes.
1520 } else if (userAccountControl & (UF_PARTIAL_SECRETS_ACCOUNT|UF_SERVER_TRUST_ACCOUNT)) {
1522 * DCs and RODCs computer accounts take
1523 * msDS-SupportedEncryptionTypes unmodified, but
1524 * force all enctypes supported by the domain.
1526 supported_enctypes |= domain_enctypes;
1528 } else if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT ||
1529 (ent_type == SAMBA_KDC_ENT_TYPE_ANY)) {
1531 * for AS-REQ the client chooses the enc types it
1532 * supports, and this will vary between computers a
1533 * user logs in from. Therefore, so that we accept any
1534 * of the client's keys for decrypting padata,
1535 * supported_enctypes should not restrict etype usage.
1537 * likewise for 'any' return as much as is supported,
1538 * to export into a keytab.
1540 supported_enctypes |= ENC_ALL_TYPES;
1543 /* If UF_USE_DES_KEY_ONLY has been set, then don't allow use of the newer enc types */
1544 if (userAccountControl & UF_USE_DES_KEY_ONLY) {
1545 supported_enctypes &= ~ENC_ALL_TYPES;
1548 if (protected_user) {
1549 supported_enctypes &= ~ENC_RC4_HMAC_MD5;
1552 pa_supported_enctypes = supported_enctypes;
1553 supported_session_etypes = supported_enctypes;
1554 if (supported_session_etypes & ENC_HMAC_SHA1_96_AES256_SK) {
1555 supported_session_etypes |= ENC_HMAC_SHA1_96_AES256;
1556 supported_session_etypes |= ENC_HMAC_SHA1_96_AES128;
1558 if (force_rc4) {
1559 supported_session_etypes |= ENC_RC4_HMAC_MD5;
1562 * now that we remembered what to announce in pa_supported_enctypes
1563 * and normalized ENC_HMAC_SHA1_96_AES256_SK, we restrict the
1564 * rest to the enc types the local kdc supports.
1566 supported_enctypes &= kdc_enctypes;
1567 supported_session_etypes &= kdc_enctypes;
1569 /* Get keys from the db */
1570 ret = samba_kdc_message2entry_keys(context, p, msg,
1571 is_krbtgt, is_rodc,
1572 userAccountControl,
1573 ent_type, flags, kvno, entry,
1574 supported_enctypes,
1575 &available_enctypes);
1576 if (ret) {
1577 /* Could be bogus data in the entry, or out of memory */
1578 goto out;
1582 * If we only have a nthash stored,
1583 * but a better session key would be
1584 * available, we fallback to fetching the
1585 * RC4_HMAC_MD5, which implicitly also
1586 * would allow an RC4_HMAC_MD5 session key.
1587 * But only if the kdc actually supports
1588 * RC4_HMAC_MD5.
1590 if (available_enctypes == 0 &&
1591 (supported_enctypes & ENC_RC4_HMAC_MD5) == 0 &&
1592 (supported_enctypes & ~ENC_RC4_HMAC_MD5) != 0 &&
1593 (kdc_enctypes & ENC_RC4_HMAC_MD5) != 0)
1595 supported_enctypes = ENC_RC4_HMAC_MD5;
1596 ret = samba_kdc_message2entry_keys(context, p, msg,
1597 is_krbtgt, is_rodc,
1598 userAccountControl,
1599 ent_type, flags, kvno, entry,
1600 supported_enctypes,
1601 &available_enctypes);
1602 if (ret) {
1603 /* Could be bogus data in the entry, or out of memory */
1604 goto out;
1609 * We need to support all session keys enctypes for
1610 * all keys we provide
1612 supported_session_etypes |= available_enctypes;
1614 ret = sdb_entry_set_etypes(entry);
1615 if (ret) {
1616 goto out;
1619 if (entry->flags.server) {
1620 bool add_aes256 =
1621 supported_session_etypes & KERB_ENCTYPE_AES256_CTS_HMAC_SHA1_96;
1622 bool add_aes128 =
1623 supported_session_etypes & KERB_ENCTYPE_AES128_CTS_HMAC_SHA1_96;
1624 bool add_rc4 =
1625 supported_session_etypes & ENC_RC4_HMAC_MD5;
1626 ret = sdb_entry_set_session_etypes(entry,
1627 add_aes256,
1628 add_aes128,
1629 add_rc4);
1630 if (ret) {
1631 goto out;
1635 if (entry->keys.len != 0) {
1637 * FIXME: Currently limited to Heimdal so as not to
1638 * break MIT KDCs, for which no fix is available.
1640 #ifdef SAMBA4_USES_HEIMDAL
1641 if (is_krbtgt) {
1643 * The krbtgt account, having no reason to
1644 * issue tickets encrypted in weaker keys,
1645 * shall only make available its strongest
1646 * key. All weaker keys are stripped out. This
1647 * makes it impossible for an RC4-encrypted
1648 * TGT to be accepted when AES KDC keys exist.
1650 * This controls the ticket key and so the PAC
1651 * signature algorithms indirectly, preventing
1652 * a weak KDC checksum from being accepted
1653 * when we verify the signatures for an
1654 * S4U2Proxy evidence ticket. As such, this is
1655 * indispensable for addressing
1656 * CVE-2022-37966.
1658 * Being strict here also provides protection
1659 * against possible future attacks on weak
1660 * keys.
1662 entry->keys.len = 1;
1663 if (entry->etypes != NULL) {
1664 entry->etypes->len = 1;
1666 entry->old_keys.len = MIN(entry->old_keys.len, 1);
1667 entry->older_keys.len = MIN(entry->older_keys.len, 1);
1669 #endif
1670 } else if (kdc_db_ctx->rodc) {
1672 * We are on an RODC, but don't have keys for this
1673 * account. Signal this to the caller
1675 auth_sam_trigger_repl_secret(kdc_db_ctx,
1676 kdc_db_ctx->msg_ctx,
1677 kdc_db_ctx->ev_ctx,
1678 msg->dn);
1679 return SDB_ERR_NOT_FOUND_HERE;
1680 } else {
1682 * oh, no password. Apparently (comment in
1683 * hdb-ldap.c) this violates the ASN.1, but this
1684 * allows an entry with no keys (yet).
1688 p->msg = talloc_steal(p, msg);
1689 p->supported_enctypes = pa_supported_enctypes;
1691 p->client_policy = talloc_steal(p, authn_client_policy);
1692 p->server_policy = talloc_steal(p, authn_server_policy);
1694 out:
1695 if (ret != 0) {
1696 /* This doesn't free ent itself, that is for the eventual caller to do */
1697 sdb_entry_free(entry);
1698 } else {
1699 talloc_steal(kdc_db_ctx, p);
1702 return ret;
1706 * Construct an hdb_entry from a directory entry.
1707 * The kvno is what the remote client asked for
1709 static krb5_error_code samba_kdc_trust_message2entry(krb5_context context,
1710 struct samba_kdc_db_context *kdc_db_ctx,
1711 TALLOC_CTX *mem_ctx,
1712 enum trust_direction direction,
1713 struct ldb_dn *realm_dn,
1714 unsigned flags,
1715 uint32_t kvno,
1716 struct ldb_message *msg,
1717 struct sdb_entry *entry)
1719 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
1720 const char *our_realm = lpcfg_realm(lp_ctx);
1721 char *partner_realm = NULL;
1722 const char *realm = NULL;
1723 const char *krbtgt_realm = NULL;
1724 DATA_BLOB password_utf16 = data_blob_null;
1725 DATA_BLOB password_utf8 = data_blob_null;
1726 struct samr_Password _password_hash;
1727 const struct samr_Password *password_hash = NULL;
1728 const struct ldb_val *password_val;
1729 struct trustAuthInOutBlob password_blob;
1730 struct samba_kdc_entry *p;
1731 bool use_previous = false;
1732 uint32_t current_kvno;
1733 uint32_t previous_kvno;
1734 uint32_t num_keys = 0;
1735 enum ndr_err_code ndr_err;
1736 int ret;
1737 unsigned int i;
1738 struct AuthenticationInformationArray *auth_array;
1739 struct timeval tv;
1740 NTTIME an_hour_ago;
1741 uint32_t *auth_kvno;
1742 bool preferr_current = false;
1743 bool force_rc4 = lpcfg_kdc_force_enable_rc4_weak_session_keys(lp_ctx);
1744 uint32_t supported_enctypes = ENC_RC4_HMAC_MD5;
1745 uint32_t pa_supported_enctypes;
1746 uint32_t supported_session_etypes;
1747 uint32_t config_kdc_enctypes = lpcfg_kdc_supported_enctypes(lp_ctx);
1748 uint32_t kdc_enctypes =
1749 config_kdc_enctypes != 0 ?
1750 config_kdc_enctypes :
1751 ENC_ALL_TYPES;
1752 struct lsa_TrustDomainInfoInfoEx *tdo = NULL;
1753 NTSTATUS status;
1755 ZERO_STRUCTP(entry);
1757 if (dsdb_functional_level(kdc_db_ctx->samdb) >= DS_DOMAIN_FUNCTION_2008) {
1758 /* If not told otherwise, Windows now assumes that trusts support AES. */
1759 supported_enctypes = ldb_msg_find_attr_as_uint(msg,
1760 "msDS-SupportedEncryptionTypes",
1761 ENC_HMAC_SHA1_96_AES256);
1764 pa_supported_enctypes = supported_enctypes;
1765 supported_session_etypes = supported_enctypes;
1766 if (supported_session_etypes & ENC_HMAC_SHA1_96_AES256_SK) {
1767 supported_session_etypes |= ENC_HMAC_SHA1_96_AES256;
1768 supported_session_etypes |= ENC_HMAC_SHA1_96_AES128;
1770 if (force_rc4) {
1771 supported_session_etypes |= ENC_RC4_HMAC_MD5;
1774 * now that we remembered what to announce in pa_supported_enctypes
1775 * and normalized ENC_HMAC_SHA1_96_AES256_SK, we restrict the
1776 * rest to the enc types the local kdc supports.
1778 supported_enctypes &= kdc_enctypes;
1779 supported_session_etypes &= kdc_enctypes;
1781 status = dsdb_trust_parse_tdo_info(mem_ctx, msg, &tdo);
1782 if (!NT_STATUS_IS_OK(status)) {
1783 krb5_clear_error_message(context);
1784 ret = ENOMEM;
1785 goto out;
1788 if (!(tdo->trust_direction & direction)) {
1789 krb5_clear_error_message(context);
1790 ret = SDB_ERR_NOENTRY;
1791 goto out;
1794 if (tdo->trust_type != LSA_TRUST_TYPE_UPLEVEL) {
1796 * Only UPLEVEL domains support kerberos here,
1797 * as we don't support LSA_TRUST_TYPE_MIT.
1799 krb5_clear_error_message(context);
1800 ret = SDB_ERR_NOENTRY;
1801 goto out;
1804 if (tdo->trust_attributes & LSA_TRUST_ATTRIBUTE_CROSS_ORGANIZATION) {
1806 * We don't support selective authentication yet.
1808 krb5_clear_error_message(context);
1809 ret = SDB_ERR_NOENTRY;
1810 goto out;
1813 if (tdo->domain_name.string == NULL) {
1814 krb5_clear_error_message(context);
1815 ret = SDB_ERR_NOENTRY;
1816 goto out;
1818 partner_realm = strupper_talloc(mem_ctx, tdo->domain_name.string);
1819 if (partner_realm == NULL) {
1820 krb5_clear_error_message(context);
1821 ret = ENOMEM;
1822 goto out;
1825 if (direction == INBOUND) {
1826 realm = our_realm;
1827 krbtgt_realm = partner_realm;
1829 password_val = ldb_msg_find_ldb_val(msg, "trustAuthIncoming");
1830 } else { /* OUTBOUND */
1831 realm = partner_realm;
1832 krbtgt_realm = our_realm;
1834 password_val = ldb_msg_find_ldb_val(msg, "trustAuthOutgoing");
1837 if (password_val == NULL) {
1838 krb5_clear_error_message(context);
1839 ret = SDB_ERR_NOENTRY;
1840 goto out;
1843 ndr_err = ndr_pull_struct_blob(password_val, mem_ctx, &password_blob,
1844 (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
1845 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1846 krb5_clear_error_message(context);
1847 ret = EINVAL;
1848 goto out;
1851 p = talloc_zero(mem_ctx, struct samba_kdc_entry);
1852 if (!p) {
1853 ret = ENOMEM;
1854 goto out;
1857 p->is_trust = true;
1858 p->kdc_db_ctx = kdc_db_ctx;
1859 p->realm_dn = realm_dn;
1860 p->supported_enctypes = pa_supported_enctypes;
1862 talloc_set_destructor(p, samba_kdc_entry_destructor);
1864 entry->skdc_entry = p;
1866 /* use 'whenCreated' */
1867 entry->created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
1868 /* use 'kadmin' for now (needed by mit_samba) */
1869 ret = smb_krb5_make_principal(context,
1870 &entry->created_by.principal,
1871 realm, "kadmin", NULL);
1872 if (ret) {
1873 krb5_clear_error_message(context);
1874 goto out;
1878 * We always need to generate the canonicalized principal
1879 * with the values of our database.
1881 ret = smb_krb5_make_principal(context, &entry->principal, realm,
1882 "krbtgt", krbtgt_realm, NULL);
1883 if (ret) {
1884 krb5_clear_error_message(context);
1885 goto out;
1887 smb_krb5_principal_set_type(context, entry->principal,
1888 KRB5_NT_SRV_INST);
1890 entry->valid_start = NULL;
1892 /* we need to work out if we are going to use the current or
1893 * the previous password hash.
1894 * We base this on the kvno the client passes in. If the kvno
1895 * passed in is equal to the current kvno in our database then
1896 * we use the current structure. If it is the current kvno-1,
1897 * then we use the previous substrucure.
1901 * Windows preferrs the previous key for one hour.
1903 tv = timeval_current();
1904 if (tv.tv_sec > 3600) {
1905 tv.tv_sec -= 3600;
1907 an_hour_ago = timeval_to_nttime(&tv);
1909 /* first work out the current kvno */
1910 current_kvno = 0;
1911 for (i=0; i < password_blob.count; i++) {
1912 struct AuthenticationInformation *a =
1913 &password_blob.current.array[i];
1915 if (a->LastUpdateTime <= an_hour_ago) {
1916 preferr_current = true;
1919 if (a->AuthType == TRUST_AUTH_TYPE_VERSION) {
1920 current_kvno = a->AuthInfo.version.version;
1923 if (current_kvno == 0) {
1924 previous_kvno = 255;
1925 } else {
1926 previous_kvno = current_kvno - 1;
1928 for (i=0; i < password_blob.count; i++) {
1929 struct AuthenticationInformation *a =
1930 &password_blob.previous.array[i];
1932 if (a->AuthType == TRUST_AUTH_TYPE_VERSION) {
1933 previous_kvno = a->AuthInfo.version.version;
1937 /* work out whether we will use the previous or current
1938 password */
1939 if (password_blob.previous.count == 0) {
1940 /* there is no previous password */
1941 use_previous = false;
1942 } else if (!(flags & SDB_F_KVNO_SPECIFIED)) {
1944 * If not specified we use the lowest kvno
1945 * for the first hour after an update.
1947 if (preferr_current) {
1948 use_previous = false;
1949 } else if (previous_kvno < current_kvno) {
1950 use_previous = true;
1951 } else {
1952 use_previous = false;
1954 } else if (kvno == current_kvno) {
1956 * Exact match ...
1958 use_previous = false;
1959 } else if (kvno == previous_kvno) {
1961 * Exact match ...
1963 use_previous = true;
1964 } else {
1966 * Fallback to the current one for anything else
1968 use_previous = false;
1971 if (use_previous) {
1972 auth_array = &password_blob.previous;
1973 auth_kvno = &previous_kvno;
1974 } else {
1975 auth_array = &password_blob.current;
1976 auth_kvno = &current_kvno;
1979 /* use the kvno the client specified, if available */
1980 if (flags & SDB_F_KVNO_SPECIFIED) {
1981 entry->kvno = kvno;
1982 } else {
1983 entry->kvno = *auth_kvno;
1986 for (i=0; i < auth_array->count; i++) {
1987 if (auth_array->array[i].AuthType == TRUST_AUTH_TYPE_CLEAR) {
1988 bool ok;
1990 password_utf16 = data_blob_const(auth_array->array[i].AuthInfo.clear.password,
1991 auth_array->array[i].AuthInfo.clear.size);
1992 if (password_utf16.length == 0) {
1993 break;
1996 if (supported_enctypes & ENC_RC4_HMAC_MD5) {
1997 mdfour(_password_hash.hash, password_utf16.data, password_utf16.length);
1998 if (password_hash == NULL) {
1999 num_keys += 1;
2001 password_hash = &_password_hash;
2004 if (!(supported_enctypes & (ENC_HMAC_SHA1_96_AES128|ENC_HMAC_SHA1_96_AES256))) {
2005 break;
2008 ok = convert_string_talloc(mem_ctx,
2009 CH_UTF16MUNGED, CH_UTF8,
2010 password_utf16.data,
2011 password_utf16.length,
2012 (void *)&password_utf8.data,
2013 &password_utf8.length);
2014 if (!ok) {
2015 krb5_clear_error_message(context);
2016 ret = ENOMEM;
2017 goto out;
2020 if (supported_enctypes & ENC_HMAC_SHA1_96_AES128) {
2021 num_keys += 1;
2023 if (supported_enctypes & ENC_HMAC_SHA1_96_AES256) {
2024 num_keys += 1;
2026 break;
2027 } else if (auth_array->array[i].AuthType == TRUST_AUTH_TYPE_NT4OWF) {
2028 if (supported_enctypes & ENC_RC4_HMAC_MD5) {
2029 password_hash = &auth_array->array[i].AuthInfo.nt4owf.password;
2030 num_keys += 1;
2035 /* Must have found a cleartext or MD4 password */
2036 if (num_keys == 0) {
2037 DEBUG(1,(__location__ ": no usable key found\n"));
2038 krb5_clear_error_message(context);
2039 ret = SDB_ERR_NOENTRY;
2040 goto out;
2043 entry->keys.val = calloc(num_keys, sizeof(struct sdb_key));
2044 if (entry->keys.val == NULL) {
2045 krb5_clear_error_message(context);
2046 ret = ENOMEM;
2047 goto out;
2050 if (password_utf8.length != 0) {
2051 struct sdb_key key = {};
2052 krb5_const_principal salt_principal = entry->principal;
2053 krb5_data salt;
2054 krb5_data cleartext_data;
2056 cleartext_data.data = discard_const_p(char, password_utf8.data);
2057 cleartext_data.length = password_utf8.length;
2059 ret = smb_krb5_get_pw_salt(context,
2060 salt_principal,
2061 &salt);
2062 if (ret != 0) {
2063 goto out;
2066 if (supported_enctypes & ENC_HMAC_SHA1_96_AES256) {
2067 ret = smb_krb5_create_key_from_string(context,
2068 salt_principal,
2069 &salt,
2070 &cleartext_data,
2071 ENCTYPE_AES256_CTS_HMAC_SHA1_96,
2072 &key.key);
2073 if (ret != 0) {
2074 smb_krb5_free_data_contents(context, &salt);
2075 goto out;
2078 entry->keys.val[entry->keys.len] = key;
2079 entry->keys.len++;
2082 if (supported_enctypes & ENC_HMAC_SHA1_96_AES128) {
2083 ret = smb_krb5_create_key_from_string(context,
2084 salt_principal,
2085 &salt,
2086 &cleartext_data,
2087 ENCTYPE_AES128_CTS_HMAC_SHA1_96,
2088 &key.key);
2089 if (ret != 0) {
2090 smb_krb5_free_data_contents(context, &salt);
2091 goto out;
2094 entry->keys.val[entry->keys.len] = key;
2095 entry->keys.len++;
2098 smb_krb5_free_data_contents(context, &salt);
2101 if (password_hash != NULL) {
2102 struct sdb_key key = {};
2104 ret = smb_krb5_keyblock_init_contents(context,
2105 ENCTYPE_ARCFOUR_HMAC,
2106 password_hash->hash,
2107 sizeof(password_hash->hash),
2108 &key.key);
2109 if (ret != 0) {
2110 goto out;
2113 entry->keys.val[entry->keys.len] = key;
2114 entry->keys.len++;
2117 entry->flags = int2SDBFlags(0);
2118 entry->flags.immutable = 1;
2119 entry->flags.invalid = 0;
2120 entry->flags.server = 1;
2121 entry->flags.require_preauth = 1;
2123 entry->pw_end = NULL;
2125 entry->max_life = NULL;
2127 entry->max_renew = NULL;
2129 /* Match Windows behavior and allow forwardable flag in cross-realm. */
2130 entry->flags.forwardable = 1;
2132 samba_kdc_sort_keys(&entry->keys);
2134 ret = sdb_entry_set_etypes(entry);
2135 if (ret) {
2136 goto out;
2140 bool add_aes256 =
2141 supported_session_etypes & KERB_ENCTYPE_AES256_CTS_HMAC_SHA1_96;
2142 bool add_aes128 =
2143 supported_session_etypes & KERB_ENCTYPE_AES128_CTS_HMAC_SHA1_96;
2144 bool add_rc4 =
2145 supported_session_etypes & ENC_RC4_HMAC_MD5;
2146 ret = sdb_entry_set_session_etypes(entry,
2147 add_aes256,
2148 add_aes128,
2149 add_rc4);
2150 if (ret) {
2151 goto out;
2155 p->msg = talloc_steal(p, msg);
2157 out:
2158 TALLOC_FREE(partner_realm);
2160 if (ret != 0) {
2161 /* This doesn't free ent itself, that is for the eventual caller to do */
2162 sdb_entry_free(entry);
2163 } else {
2164 talloc_steal(kdc_db_ctx, p);
2167 return ret;
2171 static krb5_error_code samba_kdc_lookup_trust(krb5_context context, struct ldb_context *ldb_ctx,
2172 TALLOC_CTX *mem_ctx,
2173 const char *realm,
2174 struct ldb_dn *realm_dn,
2175 struct ldb_message **pmsg)
2177 NTSTATUS status;
2178 const char * const *attrs = trust_attrs;
2180 status = dsdb_trust_search_tdo(ldb_ctx, realm, realm,
2181 attrs, mem_ctx, pmsg);
2182 if (NT_STATUS_IS_OK(status)) {
2183 return 0;
2184 } else if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
2185 return SDB_ERR_NOENTRY;
2186 } else if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
2187 int ret = ENOMEM;
2188 krb5_set_error_message(context, ret, "samba_kdc_lookup_trust: out of memory");
2189 return ret;
2190 } else {
2191 int ret = EINVAL;
2192 krb5_set_error_message(context, ret, "samba_kdc_lookup_trust: %s", nt_errstr(status));
2193 return ret;
2197 static krb5_error_code samba_kdc_lookup_client(krb5_context context,
2198 struct samba_kdc_db_context *kdc_db_ctx,
2199 TALLOC_CTX *mem_ctx,
2200 krb5_const_principal principal,
2201 const char **attrs,
2202 struct ldb_dn **realm_dn,
2203 struct ldb_message **msg)
2205 NTSTATUS nt_status;
2206 char *principal_string = NULL;
2208 if (smb_krb5_principal_get_type(context, principal) == KRB5_NT_ENTERPRISE_PRINCIPAL) {
2209 principal_string = smb_krb5_principal_get_comp_string(mem_ctx, context,
2210 principal, 0);
2211 if (principal_string == NULL) {
2212 return ENOMEM;
2214 } else {
2215 char *principal_string_m = NULL;
2216 krb5_error_code ret;
2218 ret = krb5_unparse_name(context, principal, &principal_string_m);
2219 if (ret != 0) {
2220 return ret;
2223 principal_string = talloc_strdup(mem_ctx, principal_string_m);
2224 SAFE_FREE(principal_string_m);
2225 if (principal_string == NULL) {
2226 return ENOMEM;
2230 nt_status = sam_get_results_principal(kdc_db_ctx->samdb,
2231 mem_ctx, principal_string, attrs,
2232 realm_dn, msg);
2233 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
2234 krb5_principal fallback_principal = NULL;
2235 unsigned int num_comp;
2236 char *fallback_realm = NULL;
2237 char *fallback_account = NULL;
2238 krb5_error_code ret;
2240 ret = krb5_parse_name(context, principal_string,
2241 &fallback_principal);
2242 TALLOC_FREE(principal_string);
2243 if (ret != 0) {
2244 return ret;
2247 num_comp = krb5_princ_size(context, fallback_principal);
2248 fallback_realm = smb_krb5_principal_get_realm(
2249 mem_ctx, context, fallback_principal);
2250 if (fallback_realm == NULL) {
2251 krb5_free_principal(context, fallback_principal);
2252 return ENOMEM;
2255 if (num_comp == 1) {
2256 size_t len;
2258 fallback_account = smb_krb5_principal_get_comp_string(mem_ctx,
2259 context, fallback_principal, 0);
2260 if (fallback_account == NULL) {
2261 krb5_free_principal(context, fallback_principal);
2262 TALLOC_FREE(fallback_realm);
2263 return ENOMEM;
2266 len = strlen(fallback_account);
2267 if (len >= 2 && fallback_account[len - 1] == '$') {
2268 TALLOC_FREE(fallback_account);
2271 krb5_free_principal(context, fallback_principal);
2272 fallback_principal = NULL;
2274 if (fallback_account != NULL) {
2275 char *with_dollar;
2277 with_dollar = talloc_asprintf(mem_ctx, "%s$",
2278 fallback_account);
2279 if (with_dollar == NULL) {
2280 TALLOC_FREE(fallback_realm);
2281 return ENOMEM;
2283 TALLOC_FREE(fallback_account);
2285 ret = smb_krb5_make_principal(context,
2286 &fallback_principal,
2287 fallback_realm,
2288 with_dollar, NULL);
2289 TALLOC_FREE(with_dollar);
2290 if (ret != 0) {
2291 TALLOC_FREE(fallback_realm);
2292 return ret;
2295 TALLOC_FREE(fallback_realm);
2297 if (fallback_principal != NULL) {
2298 char *fallback_string = NULL;
2300 ret = krb5_unparse_name(context,
2301 fallback_principal,
2302 &fallback_string);
2303 if (ret != 0) {
2304 krb5_free_principal(context, fallback_principal);
2305 return ret;
2308 nt_status = sam_get_results_principal(kdc_db_ctx->samdb,
2309 mem_ctx,
2310 fallback_string,
2311 attrs,
2312 realm_dn, msg);
2313 SAFE_FREE(fallback_string);
2315 krb5_free_principal(context, fallback_principal);
2316 fallback_principal = NULL;
2318 TALLOC_FREE(principal_string);
2320 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
2321 return SDB_ERR_NOENTRY;
2322 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
2323 return ENOMEM;
2324 } else if (!NT_STATUS_IS_OK(nt_status)) {
2325 return EINVAL;
2328 return 0;
2331 static krb5_error_code samba_kdc_fetch_client(krb5_context context,
2332 struct samba_kdc_db_context *kdc_db_ctx,
2333 TALLOC_CTX *mem_ctx,
2334 krb5_const_principal principal,
2335 unsigned flags,
2336 krb5_kvno kvno,
2337 struct sdb_entry *entry)
2339 struct ldb_dn *realm_dn;
2340 krb5_error_code ret;
2341 struct ldb_message *msg = NULL;
2343 ret = samba_kdc_lookup_client(context, kdc_db_ctx,
2344 mem_ctx, principal, user_attrs,
2345 &realm_dn, &msg);
2346 if (ret != 0) {
2347 return ret;
2350 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
2351 principal, SAMBA_KDC_ENT_TYPE_CLIENT,
2352 flags, kvno,
2353 realm_dn, msg, entry);
2354 return ret;
2357 static krb5_error_code samba_kdc_fetch_krbtgt(krb5_context context,
2358 struct samba_kdc_db_context *kdc_db_ctx,
2359 TALLOC_CTX *mem_ctx,
2360 krb5_const_principal principal,
2361 unsigned flags,
2362 uint32_t kvno,
2363 struct sdb_entry *entry)
2365 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
2366 krb5_error_code ret;
2367 struct ldb_message *msg = NULL;
2368 struct ldb_dn *realm_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
2369 char *realm_from_princ;
2370 char *realm_princ_comp = smb_krb5_principal_get_comp_string(mem_ctx, context, principal, 1);
2372 realm_from_princ = smb_krb5_principal_get_realm(
2373 mem_ctx, context, principal);
2374 if (realm_from_princ == NULL) {
2375 /* can't happen */
2376 return SDB_ERR_NOENTRY;
2379 if (krb5_princ_size(context, principal) != 2
2380 || (principal_comp_strcmp(context, principal, 0, KRB5_TGS_NAME) != 0)) {
2381 /* Not a krbtgt */
2382 return SDB_ERR_NOENTRY;
2385 /* krbtgt case. Either us or a trusted realm */
2387 if (lpcfg_is_my_domain_or_realm(lp_ctx, realm_from_princ)
2388 && lpcfg_is_my_domain_or_realm(lp_ctx, realm_princ_comp)) {
2389 /* us, or someone quite like us */
2390 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
2391 * is in our db, then direct the caller at our primary
2392 * krbtgt */
2394 int lret;
2395 unsigned int krbtgt_number;
2396 /* w2k8r2 sometimes gives us a kvno of 255 for inter-domain
2397 trust tickets. We don't yet know what this means, but we do
2398 seem to need to treat it as unspecified */
2399 if (flags & SDB_F_KVNO_SPECIFIED) {
2400 krbtgt_number = SAMBA_KVNO_GET_KRBTGT(kvno);
2401 if (kdc_db_ctx->rodc) {
2402 if (krbtgt_number != kdc_db_ctx->my_krbtgt_number) {
2403 return SDB_ERR_NOT_FOUND_HERE;
2406 } else {
2407 krbtgt_number = kdc_db_ctx->my_krbtgt_number;
2410 if (krbtgt_number == kdc_db_ctx->my_krbtgt_number) {
2411 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx,
2412 &msg, kdc_db_ctx->krbtgt_dn, LDB_SCOPE_BASE,
2413 krbtgt_attrs, DSDB_SEARCH_NO_GLOBAL_CATALOG,
2414 "(objectClass=user)");
2415 } else {
2416 /* We need to look up an RODC krbtgt (perhaps
2417 * ours, if we are an RODC, perhaps another
2418 * RODC if we are a read-write DC */
2419 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx,
2420 &msg, realm_dn, LDB_SCOPE_SUBTREE,
2421 krbtgt_attrs,
2422 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
2423 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=%u))", (unsigned)(krbtgt_number));
2426 if (lret == LDB_ERR_NO_SUCH_OBJECT) {
2427 krb5_warnx(context, "samba_kdc_fetch_krbtgt: could not find KRBTGT number %u in DB!",
2428 (unsigned)(krbtgt_number));
2429 krb5_set_error_message(context, SDB_ERR_NOENTRY,
2430 "samba_kdc_fetch_krbtgt: could not find KRBTGT number %u in DB!",
2431 (unsigned)(krbtgt_number));
2432 return SDB_ERR_NOENTRY;
2433 } else if (lret != LDB_SUCCESS) {
2434 krb5_warnx(context, "samba_kdc_fetch_krbtgt: could not find KRBTGT number %u in DB!",
2435 (unsigned)(krbtgt_number));
2436 krb5_set_error_message(context, SDB_ERR_NOENTRY,
2437 "samba_kdc_fetch_krbtgt: could not find KRBTGT number %u in DB!",
2438 (unsigned)(krbtgt_number));
2439 return SDB_ERR_NOENTRY;
2442 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
2443 principal, SAMBA_KDC_ENT_TYPE_KRBTGT,
2444 flags, kvno, realm_dn, msg, entry);
2445 if (ret != 0) {
2446 krb5_warnx(context, "samba_kdc_fetch_krbtgt: self krbtgt message2entry failed");
2448 return ret;
2450 } else {
2451 enum trust_direction direction = UNKNOWN;
2452 const char *realm = NULL;
2454 /* Either an inbound or outbound trust */
2456 if (strcasecmp(lpcfg_realm(lp_ctx), realm_from_princ) == 0) {
2457 /* look for inbound trust */
2458 direction = INBOUND;
2459 realm = realm_princ_comp;
2460 } else if (principal_comp_strcasecmp(context, principal, 1, lpcfg_realm(lp_ctx)) == 0) {
2461 /* look for outbound trust */
2462 direction = OUTBOUND;
2463 realm = realm_from_princ;
2464 } else {
2465 krb5_warnx(context, "samba_kdc_fetch_krbtgt: not our realm for trusts ('%s', '%s')",
2466 realm_from_princ,
2467 realm_princ_comp);
2468 krb5_set_error_message(context, SDB_ERR_NOENTRY, "samba_kdc_fetch_krbtgt: not our realm for trusts ('%s', '%s')",
2469 realm_from_princ,
2470 realm_princ_comp);
2471 return SDB_ERR_NOENTRY;
2474 /* Trusted domains are under CN=system */
2476 ret = samba_kdc_lookup_trust(context, kdc_db_ctx->samdb,
2477 mem_ctx,
2478 realm, realm_dn, &msg);
2480 if (ret != 0) {
2481 krb5_warnx(context, "samba_kdc_fetch_krbtgt: could not find principal in DB");
2482 krb5_set_error_message(context, ret, "samba_kdc_fetch_krbtgt: could not find principal in DB");
2483 return ret;
2486 ret = samba_kdc_trust_message2entry(context, kdc_db_ctx, mem_ctx,
2487 direction,
2488 realm_dn, flags, kvno, msg, entry);
2489 if (ret != 0) {
2490 krb5_warnx(context, "samba_kdc_fetch_krbtgt: trust_message2entry failed for %s",
2491 ldb_dn_get_linearized(msg->dn));
2492 krb5_set_error_message(context, ret, "samba_kdc_fetch_krbtgt: "
2493 "trust_message2entry failed for %s",
2494 ldb_dn_get_linearized(msg->dn));
2496 return ret;
2501 static krb5_error_code samba_kdc_lookup_server(krb5_context context,
2502 struct samba_kdc_db_context *kdc_db_ctx,
2503 TALLOC_CTX *mem_ctx,
2504 krb5_const_principal principal,
2505 unsigned flags,
2506 const char **attrs,
2507 struct ldb_dn **realm_dn,
2508 struct ldb_message **msg)
2510 krb5_error_code ret;
2511 if ((smb_krb5_principal_get_type(context, principal) != KRB5_NT_ENTERPRISE_PRINCIPAL)
2512 && krb5_princ_size(context, principal) >= 2) {
2513 /* 'normal server' case */
2514 int ldb_ret;
2515 NTSTATUS nt_status;
2516 struct ldb_dn *user_dn;
2517 char *principal_string;
2519 ret = krb5_unparse_name_flags(context, principal,
2520 KRB5_PRINCIPAL_UNPARSE_NO_REALM,
2521 &principal_string);
2522 if (ret != 0) {
2523 return ret;
2526 /* At this point we may find the host is known to be
2527 * in a different realm, so we should generate a
2528 * referral instead */
2529 nt_status = crack_service_principal_name(kdc_db_ctx->samdb,
2530 mem_ctx, principal_string,
2531 &user_dn, realm_dn);
2532 free(principal_string);
2534 if (!NT_STATUS_IS_OK(nt_status)) {
2535 return SDB_ERR_NOENTRY;
2538 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb,
2539 mem_ctx,
2540 msg, user_dn, LDB_SCOPE_BASE,
2541 attrs,
2542 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
2543 "(objectClass=*)");
2544 if (ldb_ret != LDB_SUCCESS) {
2545 return SDB_ERR_NOENTRY;
2547 return 0;
2548 } else if (!(flags & SDB_F_FOR_AS_REQ)
2549 && smb_krb5_principal_get_type(context, principal) == KRB5_NT_ENTERPRISE_PRINCIPAL) {
2551 * The behaviour of accepting an
2552 * KRB5_NT_ENTERPRISE_PRINCIPAL server principal
2553 * containing a UPN only applies to TGS-REQ packets,
2554 * not AS-REQ packets.
2556 return samba_kdc_lookup_client(context, kdc_db_ctx,
2557 mem_ctx, principal, attrs,
2558 realm_dn, msg);
2559 } else {
2561 * This case is for:
2562 * - the AS-REQ, where we only accept
2563 * samAccountName based lookups for the server, no
2564 * matter if the name is an
2565 * KRB5_NT_ENTERPRISE_PRINCIPAL or not
2566 * - for the TGS-REQ when we are not given an
2567 * KRB5_NT_ENTERPRISE_PRINCIPAL, which also must
2568 * only lookup samAccountName based names.
2570 int lret;
2571 char *short_princ;
2572 krb5_principal enterprise_principal = NULL;
2573 krb5_const_principal used_principal = NULL;
2574 char *name1 = NULL;
2575 size_t len1 = 0;
2576 char *filter = NULL;
2578 if (smb_krb5_principal_get_type(context, principal) == KRB5_NT_ENTERPRISE_PRINCIPAL) {
2579 char *str = NULL;
2580 /* Need to reparse the enterprise principal to find the real target */
2581 if (krb5_princ_size(context, principal) != 1) {
2582 ret = KRB5_PARSE_MALFORMED;
2583 krb5_set_error_message(context, ret, "samba_kdc_lookup_server: request for an "
2584 "enterprise principal with wrong (%d) number of components",
2585 krb5_princ_size(context, principal));
2586 return ret;
2588 str = smb_krb5_principal_get_comp_string(mem_ctx, context, principal, 0);
2589 if (str == NULL) {
2590 return KRB5_PARSE_MALFORMED;
2592 ret = krb5_parse_name(context, str,
2593 &enterprise_principal);
2594 talloc_free(str);
2595 if (ret) {
2596 return ret;
2598 used_principal = enterprise_principal;
2599 } else {
2600 used_principal = principal;
2603 /* server as client principal case, but we must not lookup userPrincipalNames */
2604 *realm_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
2606 /* TODO: Check if it is our realm, otherwise give referral */
2608 ret = krb5_unparse_name_flags(context, used_principal,
2609 KRB5_PRINCIPAL_UNPARSE_NO_REALM |
2610 KRB5_PRINCIPAL_UNPARSE_DISPLAY,
2611 &short_princ);
2612 used_principal = NULL;
2613 krb5_free_principal(context, enterprise_principal);
2614 enterprise_principal = NULL;
2616 if (ret != 0) {
2617 krb5_set_error_message(context, ret, "samba_kdc_lookup_server: could not parse principal");
2618 krb5_warnx(context, "samba_kdc_lookup_server: could not parse principal");
2619 return ret;
2622 name1 = ldb_binary_encode_string(mem_ctx, short_princ);
2623 SAFE_FREE(short_princ);
2624 if (name1 == NULL) {
2625 return ENOMEM;
2627 len1 = strlen(name1);
2628 if (len1 >= 1 && name1[len1 - 1] != '$') {
2629 filter = talloc_asprintf(mem_ctx,
2630 "(&(objectClass=user)(|(samAccountName=%s)(samAccountName=%s$)))",
2631 name1, name1);
2632 if (filter == NULL) {
2633 return ENOMEM;
2635 } else {
2636 filter = talloc_asprintf(mem_ctx,
2637 "(&(objectClass=user)(samAccountName=%s))",
2638 name1);
2639 if (filter == NULL) {
2640 return ENOMEM;
2644 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx, msg,
2645 *realm_dn, LDB_SCOPE_SUBTREE,
2646 attrs,
2647 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
2648 "%s", filter);
2649 if (lret == LDB_ERR_NO_SUCH_OBJECT) {
2650 DEBUG(10, ("Failed to find an entry for %s filter:%s\n",
2651 name1, filter));
2652 return SDB_ERR_NOENTRY;
2654 if (lret == LDB_ERR_CONSTRAINT_VIOLATION) {
2655 DEBUG(10, ("Failed to find unique entry for %s filter:%s\n",
2656 name1, filter));
2657 return SDB_ERR_NOENTRY;
2659 if (lret != LDB_SUCCESS) {
2660 DEBUG(0, ("Failed single search for %s - %s\n",
2661 name1, ldb_errstring(kdc_db_ctx->samdb)));
2662 return SDB_ERR_NOENTRY;
2664 return 0;
2666 return SDB_ERR_NOENTRY;
2671 static krb5_error_code samba_kdc_fetch_server(krb5_context context,
2672 struct samba_kdc_db_context *kdc_db_ctx,
2673 TALLOC_CTX *mem_ctx,
2674 krb5_const_principal principal,
2675 unsigned flags,
2676 krb5_kvno kvno,
2677 struct sdb_entry *entry)
2679 krb5_error_code ret;
2680 struct ldb_dn *realm_dn;
2681 struct ldb_message *msg;
2683 ret = samba_kdc_lookup_server(context, kdc_db_ctx, mem_ctx, principal,
2684 flags, server_attrs, &realm_dn, &msg);
2685 if (ret != 0) {
2686 return ret;
2689 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
2690 principal, SAMBA_KDC_ENT_TYPE_SERVER,
2691 flags, kvno,
2692 realm_dn, msg, entry);
2693 if (ret != 0) {
2694 char *client_name = NULL;
2695 krb5_error_code code;
2697 code = krb5_unparse_name(context, principal, &client_name);
2698 if (code == 0) {
2699 krb5_warnx(context,
2700 "samba_kdc_fetch_server: message2entry failed for "
2701 "%s",
2702 client_name);
2703 } else {
2704 krb5_warnx(context,
2705 "samba_kdc_fetch_server: message2entry and "
2706 "krb5_unparse_name failed");
2708 SAFE_FREE(client_name);
2711 return ret;
2714 static krb5_error_code samba_kdc_lookup_realm(krb5_context context,
2715 struct samba_kdc_db_context *kdc_db_ctx,
2716 TALLOC_CTX *mem_ctx,
2717 krb5_const_principal principal,
2718 unsigned flags,
2719 struct sdb_entry *entry)
2721 TALLOC_CTX *frame = talloc_stackframe();
2722 NTSTATUS status;
2723 krb5_error_code ret;
2724 bool check_realm = false;
2725 const char *realm = NULL;
2726 struct dsdb_trust_routing_table *trt = NULL;
2727 const struct lsa_TrustDomainInfoInfoEx *tdo = NULL;
2728 unsigned int num_comp;
2729 bool ok;
2730 char *upper = NULL;
2732 num_comp = krb5_princ_size(context, principal);
2734 if (flags & SDB_F_GET_CLIENT) {
2735 if (flags & SDB_F_FOR_AS_REQ) {
2736 check_realm = true;
2739 if (flags & SDB_F_GET_SERVER) {
2740 if (flags & SDB_F_FOR_TGS_REQ) {
2741 check_realm = true;
2745 if (!check_realm) {
2746 TALLOC_FREE(frame);
2747 return 0;
2750 realm = smb_krb5_principal_get_realm(frame, context, principal);
2751 if (realm == NULL) {
2752 TALLOC_FREE(frame);
2753 return ENOMEM;
2757 * The requested realm needs to be our own
2759 ok = lpcfg_is_my_domain_or_realm(kdc_db_ctx->lp_ctx, realm);
2760 if (!ok) {
2762 * The request is not for us...
2764 TALLOC_FREE(frame);
2765 return SDB_ERR_NOENTRY;
2768 if (smb_krb5_principal_get_type(context, principal) == KRB5_NT_ENTERPRISE_PRINCIPAL) {
2769 char *principal_string = NULL;
2770 krb5_principal enterprise_principal = NULL;
2771 char *enterprise_realm = NULL;
2773 if (num_comp != 1) {
2774 TALLOC_FREE(frame);
2775 return SDB_ERR_NOENTRY;
2778 principal_string = smb_krb5_principal_get_comp_string(frame, context,
2779 principal, 0);
2780 if (principal_string == NULL) {
2781 TALLOC_FREE(frame);
2782 return ENOMEM;
2785 ret = krb5_parse_name(context, principal_string,
2786 &enterprise_principal);
2787 TALLOC_FREE(principal_string);
2788 if (ret) {
2789 TALLOC_FREE(frame);
2790 return ret;
2793 enterprise_realm = smb_krb5_principal_get_realm(
2794 frame, context, enterprise_principal);
2795 krb5_free_principal(context, enterprise_principal);
2796 if (enterprise_realm != NULL) {
2797 realm = enterprise_realm;
2801 if (flags & SDB_F_GET_SERVER) {
2802 char *service_realm = NULL;
2804 ret = principal_comp_strcmp(context, principal, 0, KRB5_TGS_NAME);
2805 if (ret == 0) {
2807 * we need to search krbtgt/ locally
2809 TALLOC_FREE(frame);
2810 return 0;
2814 * We need to check the last component against the routing table.
2816 * Note this works only with 2 or 3 component principals, e.g:
2818 * servicePrincipalName: ldap/W2K8R2-219.bla.base
2819 * servicePrincipalName: ldap/W2K8R2-219.bla.base/bla.base
2820 * servicePrincipalName: ldap/W2K8R2-219.bla.base/ForestDnsZones.bla.base
2821 * servicePrincipalName: ldap/W2K8R2-219.bla.base/DomainDnsZones.bla.base
2824 if (num_comp == 2 || num_comp == 3) {
2825 service_realm = smb_krb5_principal_get_comp_string(frame,
2826 context,
2827 principal,
2828 num_comp - 1);
2831 if (service_realm != NULL) {
2832 realm = service_realm;
2836 ok = lpcfg_is_my_domain_or_realm(kdc_db_ctx->lp_ctx, realm);
2837 if (ok) {
2839 * skip the expensive routing lookup
2841 TALLOC_FREE(frame);
2842 return 0;
2845 status = dsdb_trust_routing_table_load(kdc_db_ctx->samdb,
2846 frame, &trt);
2847 if (!NT_STATUS_IS_OK(status)) {
2848 TALLOC_FREE(frame);
2849 return EINVAL;
2852 tdo = dsdb_trust_routing_by_name(trt, realm);
2853 if (tdo == NULL) {
2855 * This principal has to be local
2857 TALLOC_FREE(frame);
2858 return 0;
2861 if (tdo->trust_attributes & LSA_TRUST_ATTRIBUTE_WITHIN_FOREST) {
2863 * TODO: handle the routing within the forest
2865 * This should likely be handled in
2866 * samba_kdc_message2entry() in case we're
2867 * a global catalog. We'd need to check
2868 * if realm_dn is our own domain and derive
2869 * the dns domain name from realm_dn and check that
2870 * against the routing table or fallback to
2871 * the tdo we found here.
2873 * But for now we don't support multiple domains
2874 * in our forest correctly anyway.
2876 * Just search in our local database.
2878 TALLOC_FREE(frame);
2879 return 0;
2882 ZERO_STRUCTP(entry);
2884 ret = krb5_copy_principal(context, principal,
2885 &entry->principal);
2886 if (ret) {
2887 TALLOC_FREE(frame);
2888 return ret;
2891 upper = strupper_talloc(frame, tdo->domain_name.string);
2892 if (upper == NULL) {
2893 TALLOC_FREE(frame);
2894 return ENOMEM;
2897 ret = smb_krb5_principal_set_realm(context,
2898 entry->principal,
2899 upper);
2900 if (ret) {
2901 TALLOC_FREE(frame);
2902 return ret;
2905 TALLOC_FREE(frame);
2906 return SDB_ERR_WRONG_REALM;
2909 krb5_error_code samba_kdc_fetch(krb5_context context,
2910 struct samba_kdc_db_context *kdc_db_ctx,
2911 krb5_const_principal principal,
2912 unsigned flags,
2913 krb5_kvno kvno,
2914 struct sdb_entry *entry)
2916 krb5_error_code ret = SDB_ERR_NOENTRY;
2917 TALLOC_CTX *mem_ctx;
2919 mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_fetch context");
2920 if (!mem_ctx) {
2921 ret = ENOMEM;
2922 krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
2923 return ret;
2926 ret = samba_kdc_lookup_realm(context, kdc_db_ctx, mem_ctx,
2927 principal, flags, entry);
2928 if (ret != 0) {
2929 goto done;
2932 ret = SDB_ERR_NOENTRY;
2934 if (flags & SDB_F_GET_CLIENT) {
2935 ret = samba_kdc_fetch_client(context, kdc_db_ctx, mem_ctx, principal, flags, kvno, entry);
2936 if (ret != SDB_ERR_NOENTRY) goto done;
2938 if (flags & SDB_F_GET_SERVER) {
2939 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
2940 ret = samba_kdc_fetch_krbtgt(context, kdc_db_ctx, mem_ctx, principal, flags, kvno, entry);
2941 if (ret != SDB_ERR_NOENTRY) goto done;
2943 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
2944 ret = samba_kdc_fetch_server(context, kdc_db_ctx, mem_ctx, principal, flags, kvno, entry);
2945 if (ret != SDB_ERR_NOENTRY) goto done;
2947 if (flags & SDB_F_GET_KRBTGT) {
2948 ret = samba_kdc_fetch_krbtgt(context, kdc_db_ctx, mem_ctx, principal, flags, kvno, entry);
2949 if (ret != SDB_ERR_NOENTRY) goto done;
2952 done:
2953 talloc_free(mem_ctx);
2954 return ret;
2957 struct samba_kdc_seq {
2958 unsigned int index;
2959 unsigned int count;
2960 struct ldb_message **msgs;
2961 struct ldb_dn *realm_dn;
2964 static krb5_error_code samba_kdc_seq(krb5_context context,
2965 struct samba_kdc_db_context *kdc_db_ctx,
2966 struct sdb_entry *entry)
2968 krb5_error_code ret;
2969 struct samba_kdc_seq *priv = kdc_db_ctx->seq_ctx;
2970 const char *realm = lpcfg_realm(kdc_db_ctx->lp_ctx);
2971 struct ldb_message *msg = NULL;
2972 const char *sAMAccountName = NULL;
2973 krb5_principal principal = NULL;
2974 TALLOC_CTX *mem_ctx;
2976 if (!priv) {
2977 return SDB_ERR_NOENTRY;
2980 mem_ctx = talloc_named(priv, 0, "samba_kdc_seq context");
2982 if (!mem_ctx) {
2983 ret = ENOMEM;
2984 krb5_set_error_message(context, ret, "samba_kdc_seq: talloc_named() failed!");
2985 return ret;
2988 while (priv->index < priv->count) {
2989 msg = priv->msgs[priv->index++];
2991 sAMAccountName = ldb_msg_find_attr_as_string(msg, "sAMAccountName", NULL);
2992 if (sAMAccountName != NULL) {
2993 break;
2997 if (sAMAccountName == NULL) {
2998 ret = SDB_ERR_NOENTRY;
2999 goto out;
3002 ret = smb_krb5_make_principal(context, &principal,
3003 realm, sAMAccountName, NULL);
3004 if (ret != 0) {
3005 goto out;
3008 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
3009 principal, SAMBA_KDC_ENT_TYPE_ANY,
3010 SDB_F_ADMIN_DATA|SDB_F_GET_ANY,
3011 0 /* kvno */,
3012 priv->realm_dn, msg, entry);
3014 out:
3015 if (principal != NULL) {
3016 krb5_free_principal(context, principal);
3019 if (ret != 0) {
3020 TALLOC_FREE(priv);
3021 kdc_db_ctx->seq_ctx = NULL;
3022 } else {
3023 talloc_free(mem_ctx);
3026 return ret;
3029 krb5_error_code samba_kdc_firstkey(krb5_context context,
3030 struct samba_kdc_db_context *kdc_db_ctx,
3031 struct sdb_entry *entry)
3033 struct ldb_context *ldb_ctx = kdc_db_ctx->samdb;
3034 struct samba_kdc_seq *priv = kdc_db_ctx->seq_ctx;
3035 char *realm;
3036 struct ldb_result *res = NULL;
3037 krb5_error_code ret;
3038 TALLOC_CTX *mem_ctx;
3039 int lret;
3041 if (priv) {
3042 TALLOC_FREE(priv);
3043 kdc_db_ctx->seq_ctx = NULL;
3046 priv = (struct samba_kdc_seq *) talloc(kdc_db_ctx, struct samba_kdc_seq);
3047 if (!priv) {
3048 ret = ENOMEM;
3049 krb5_set_error_message(context, ret, "talloc: out of memory");
3050 return ret;
3053 priv->index = 0;
3054 priv->msgs = NULL;
3055 priv->realm_dn = ldb_get_default_basedn(ldb_ctx);
3056 priv->count = 0;
3058 mem_ctx = talloc_named(priv, 0, "samba_kdc_firstkey context");
3060 if (!mem_ctx) {
3061 ret = ENOMEM;
3062 krb5_set_error_message(context, ret, "samba_kdc_firstkey: talloc_named() failed!");
3063 TALLOC_FREE(priv);
3064 return ret;
3067 ret = krb5_get_default_realm(context, &realm);
3068 if (ret != 0) {
3069 TALLOC_FREE(priv);
3070 return ret;
3072 krb5_free_default_realm(context, realm);
3074 lret = dsdb_search(ldb_ctx, priv, &res,
3075 priv->realm_dn, LDB_SCOPE_SUBTREE, user_attrs,
3076 DSDB_SEARCH_NO_GLOBAL_CATALOG,
3077 "(objectClass=user)");
3079 if (lret != LDB_SUCCESS) {
3080 TALLOC_FREE(priv);
3081 return SDB_ERR_NOENTRY;
3084 priv->count = res->count;
3085 priv->msgs = talloc_steal(priv, res->msgs);
3086 talloc_free(res);
3088 kdc_db_ctx->seq_ctx = priv;
3090 ret = samba_kdc_seq(context, kdc_db_ctx, entry);
3092 if (ret != 0) {
3093 TALLOC_FREE(priv);
3094 kdc_db_ctx->seq_ctx = NULL;
3095 } else {
3096 talloc_free(mem_ctx);
3098 return ret;
3101 krb5_error_code samba_kdc_nextkey(krb5_context context,
3102 struct samba_kdc_db_context *kdc_db_ctx,
3103 struct sdb_entry *entry)
3105 return samba_kdc_seq(context, kdc_db_ctx, entry);
3108 /* Check if a given entry may delegate or do s4u2self to this target principal
3110 * The safest way to determine 'self' is to check the DB record made at
3111 * the time the principal was presented to the KDC.
3113 krb5_error_code
3114 samba_kdc_check_client_matches_target_service(krb5_context context,
3115 struct samba_kdc_entry *skdc_entry_client,
3116 struct samba_kdc_entry *skdc_entry_server_target)
3118 struct dom_sid *orig_sid;
3119 struct dom_sid *target_sid;
3120 TALLOC_CTX *frame = talloc_stackframe();
3122 orig_sid = samdb_result_dom_sid(frame,
3123 skdc_entry_client->msg,
3124 "objectSid");
3125 target_sid = samdb_result_dom_sid(frame,
3126 skdc_entry_server_target->msg,
3127 "objectSid");
3130 * Allow delegation to the same record (representing a
3131 * principal), even if by a different name. The easy and safe
3132 * way to prove this is by SID comparison
3134 if (!(orig_sid && target_sid && dom_sid_equal(orig_sid, target_sid))) {
3135 talloc_free(frame);
3136 return KRB5KRB_AP_ERR_BADMATCH;
3139 talloc_free(frame);
3140 return 0;
3143 /* Certificates printed by a the Certificate Authority might have a
3144 * slightly different form of the user principal name to that in the
3145 * database. Allow a mismatch where they both refer to the same
3146 * SID */
3148 krb5_error_code
3149 samba_kdc_check_pkinit_ms_upn_match(krb5_context context,
3150 struct samba_kdc_db_context *kdc_db_ctx,
3151 struct samba_kdc_entry *skdc_entry,
3152 krb5_const_principal certificate_principal)
3154 krb5_error_code ret;
3155 struct ldb_dn *realm_dn;
3156 struct ldb_message *msg;
3157 struct dom_sid *orig_sid;
3158 struct dom_sid *target_sid;
3159 const char *ms_upn_check_attrs[] = {
3160 "objectSid", NULL
3163 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_pkinit_ms_upn_match");
3165 if (!mem_ctx) {
3166 ret = ENOMEM;
3167 krb5_set_error_message(context, ret, "samba_kdc_check_pkinit_ms_upn_match: talloc_named() failed!");
3168 return ret;
3171 ret = samba_kdc_lookup_client(context, kdc_db_ctx,
3172 mem_ctx, certificate_principal,
3173 ms_upn_check_attrs, &realm_dn, &msg);
3175 if (ret != 0) {
3176 talloc_free(mem_ctx);
3177 return ret;
3180 orig_sid = samdb_result_dom_sid(mem_ctx, skdc_entry->msg, "objectSid");
3181 target_sid = samdb_result_dom_sid(mem_ctx, msg, "objectSid");
3183 /* Consider these to be the same principal, even if by a different
3184 * name. The easy and safe way to prove this is by SID
3185 * comparison */
3186 if (!(orig_sid && target_sid && dom_sid_equal(orig_sid, target_sid))) {
3187 talloc_free(mem_ctx);
3188 #if defined(KRB5KDC_ERR_CLIENT_NAME_MISMATCH) /* MIT */
3189 return KRB5KDC_ERR_CLIENT_NAME_MISMATCH;
3190 #else /* Heimdal (where this is an enum) */
3191 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH;
3192 #endif
3195 talloc_free(mem_ctx);
3196 return ret;
3200 * Check if a given entry may delegate to this target principal
3201 * with S4U2Proxy.
3203 krb5_error_code
3204 samba_kdc_check_s4u2proxy(krb5_context context,
3205 struct samba_kdc_db_context *kdc_db_ctx,
3206 struct samba_kdc_entry *skdc_entry,
3207 krb5_const_principal target_principal)
3209 krb5_error_code ret;
3210 char *tmp = NULL;
3211 const char *client_dn = NULL;
3212 const char *target_principal_name = NULL;
3213 struct ldb_message_element *el;
3214 struct ldb_val val;
3215 unsigned int i;
3216 bool found = false;
3218 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_s4u2proxy");
3220 if (!mem_ctx) {
3221 ret = ENOMEM;
3222 krb5_set_error_message(context, ret,
3223 "samba_kdc_check_s4u2proxy:"
3224 " talloc_named() failed!");
3225 return ret;
3228 client_dn = ldb_dn_get_linearized(skdc_entry->msg->dn);
3229 if (!client_dn) {
3230 if (errno == 0) {
3231 errno = ENOMEM;
3233 ret = errno;
3234 krb5_set_error_message(context, ret,
3235 "samba_kdc_check_s4u2proxy:"
3236 " ldb_dn_get_linearized() failed!");
3237 talloc_free(mem_ctx);
3238 return ret;
3241 el = ldb_msg_find_element(skdc_entry->msg, "msDS-AllowedToDelegateTo");
3242 if (el == NULL) {
3243 ret = ENOENT;
3244 goto bad_option;
3246 SMB_ASSERT(el->num_values != 0);
3249 * This is the Microsoft forwardable flag behavior.
3251 * If the proxy (target) principal is NULL, and we have any authorized
3252 * delegation target, allow to forward.
3254 if (target_principal == NULL) {
3255 talloc_free(mem_ctx);
3256 return 0;
3261 * The main heimdal code already checked that the target_principal
3262 * belongs to the same realm as the client.
3264 * So we just need the principal without the realm,
3265 * as that is what is configured in the "msDS-AllowedToDelegateTo"
3266 * attribute.
3268 ret = krb5_unparse_name_flags(context, target_principal,
3269 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &tmp);
3270 if (ret) {
3271 talloc_free(mem_ctx);
3272 krb5_set_error_message(context, ret,
3273 "samba_kdc_check_s4u2proxy:"
3274 " krb5_unparse_name() failed!");
3275 return ret;
3277 DEBUG(10,("samba_kdc_check_s4u2proxy: client[%s] for target[%s]\n",
3278 client_dn, tmp));
3280 target_principal_name = talloc_strdup(mem_ctx, tmp);
3281 SAFE_FREE(tmp);
3282 if (target_principal_name == NULL) {
3283 ret = ENOMEM;
3284 krb5_set_error_message(context, ret,
3285 "samba_kdc_check_s4u2proxy:"
3286 " talloc_strdup() failed!");
3287 talloc_free(mem_ctx);
3288 return ret;
3291 val = data_blob_string_const(target_principal_name);
3293 for (i=0; i<el->num_values; i++) {
3294 struct ldb_val *val1 = &val;
3295 struct ldb_val *val2 = &el->values[i];
3296 int cmp;
3298 if (val1->length != val2->length) {
3299 continue;
3302 cmp = strncasecmp((const char *)val1->data,
3303 (const char *)val2->data,
3304 val1->length);
3305 if (cmp != 0) {
3306 continue;
3309 found = true;
3310 break;
3313 if (!found) {
3314 ret = ENOENT;
3315 goto bad_option;
3318 DEBUG(10,("samba_kdc_check_s4u2proxy: client[%s] allowed target[%s]\n",
3319 client_dn, target_principal_name));
3320 talloc_free(mem_ctx);
3321 return 0;
3323 bad_option:
3324 krb5_set_error_message(context, ret,
3325 "samba_kdc_check_s4u2proxy: client[%s] "
3326 "not allowed for delegation to target[%s]",
3327 client_dn,
3328 target_principal_name);
3329 talloc_free(mem_ctx);
3330 return KRB5KDC_ERR_BADOPTION;
3334 * This method is called for S4U2Proxy requests and implements the
3335 * resource-based constrained delegation variant, which can support
3336 * cross-realm delegation.
3338 krb5_error_code samba_kdc_check_s4u2proxy_rbcd(
3339 krb5_context context,
3340 struct samba_kdc_db_context *kdc_db_ctx,
3341 krb5_const_principal client_principal,
3342 krb5_const_principal server_principal,
3343 krb5_const_pac header_pac,
3344 struct samba_kdc_entry *proxy_skdc_entry)
3346 krb5_error_code code;
3347 enum ndr_err_code ndr_err;
3348 char *client_name = NULL;
3349 char *server_name = NULL;
3350 const char *proxy_dn = NULL;
3351 const DATA_BLOB *data = NULL;
3352 struct security_descriptor *rbcd_security_descriptor = NULL;
3353 struct auth_user_info_dc *user_info_dc = NULL;
3354 struct security_token *security_token = NULL;
3355 uint32_t session_info_flags =
3356 AUTH_SESSION_INFO_DEFAULT_GROUPS |
3357 AUTH_SESSION_INFO_SIMPLE_PRIVILEGES;
3359 * Testing shows that although Windows grants SEC_ADS_GENERIC_ALL access
3360 * in security descriptors it creates for RBCD, its KDC only requires
3361 * SEC_ADS_CONTROL_ACCESS for the access check to succeed.
3363 uint32_t access_desired = SEC_ADS_CONTROL_ACCESS;
3364 uint32_t access_granted = 0;
3365 NTSTATUS nt_status;
3366 TALLOC_CTX *mem_ctx = NULL;
3368 mem_ctx = talloc_named(kdc_db_ctx,
3370 "samba_kdc_check_s4u2proxy_rbcd");
3371 if (mem_ctx == NULL) {
3372 errno = ENOMEM;
3373 code = errno;
3375 return code;
3378 proxy_dn = ldb_dn_get_linearized(proxy_skdc_entry->msg->dn);
3379 if (proxy_dn == NULL) {
3380 DBG_ERR("ldb_dn_get_linearized failed for proxy_dn!\n");
3381 if (errno == 0) {
3382 errno = ENOMEM;
3384 code = errno;
3386 goto out;
3389 rbcd_security_descriptor = talloc_zero(mem_ctx,
3390 struct security_descriptor);
3391 if (rbcd_security_descriptor == NULL) {
3392 errno = ENOMEM;
3393 code = errno;
3395 goto out;
3398 code = krb5_unparse_name_flags(context,
3399 client_principal,
3400 KRB5_PRINCIPAL_UNPARSE_DISPLAY,
3401 &client_name);
3402 if (code != 0) {
3403 DBG_ERR("Unable to parse client_principal!\n");
3404 goto out;
3407 code = krb5_unparse_name_flags(context,
3408 server_principal,
3409 KRB5_PRINCIPAL_UNPARSE_DISPLAY,
3410 &server_name);
3411 if (code != 0) {
3412 DBG_ERR("Unable to parse server_principal!\n");
3413 goto out;
3416 DBG_INFO("Check delegation from client[%s] to server[%s] via "
3417 "proxy[%s]\n",
3418 client_name,
3419 server_name,
3420 proxy_dn);
3422 code = kerberos_pac_to_user_info_dc(mem_ctx,
3423 header_pac,
3424 context,
3425 &user_info_dc,
3426 AUTH_INCLUDE_RESOURCE_GROUPS,
3427 NULL,
3428 NULL,
3429 NULL);
3430 if (code != 0) {
3431 goto out;
3434 if (!(user_info_dc->info->user_flags & NETLOGON_GUEST)) {
3435 session_info_flags |= AUTH_SESSION_INFO_AUTHENTICATED;
3438 nt_status = auth_generate_security_token(mem_ctx,
3439 kdc_db_ctx->lp_ctx,
3440 kdc_db_ctx->samdb,
3441 user_info_dc,
3442 session_info_flags,
3443 &security_token);
3444 if (!NT_STATUS_IS_OK(nt_status)) {
3445 code = map_errno_from_nt_status(nt_status);
3446 goto out;
3449 data = ldb_msg_find_ldb_val(proxy_skdc_entry->msg,
3450 "msDS-AllowedToActOnBehalfOfOtherIdentity");
3451 if (data == NULL) {
3452 DBG_ERR("Could not find security descriptor "
3453 "msDS-AllowedToActOnBehalfOfOtherIdentity in "
3454 "proxy[%s]\n",
3455 proxy_dn);
3456 code = KRB5KDC_ERR_BADOPTION;
3457 goto out;
3460 ndr_err = ndr_pull_struct_blob(
3461 data,
3462 mem_ctx,
3463 rbcd_security_descriptor,
3464 (ndr_pull_flags_fn_t)ndr_pull_security_descriptor);
3465 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
3466 errno = ndr_map_error2errno(ndr_err);
3467 DBG_ERR("Failed to unmarshall "
3468 "msDS-AllowedToActOnBehalfOfOtherIdentity "
3469 "security descriptor of proxy[%s]\n",
3470 proxy_dn);
3471 code = KRB5KDC_ERR_BADOPTION;
3472 goto out;
3475 if (DEBUGLEVEL >= 10) {
3476 NDR_PRINT_DEBUG(security_token, security_token);
3477 NDR_PRINT_DEBUG(security_descriptor, rbcd_security_descriptor);
3480 nt_status = sec_access_check_ds(rbcd_security_descriptor,
3481 security_token,
3482 access_desired,
3483 &access_granted,
3484 NULL,
3485 NULL);
3487 if (!NT_STATUS_IS_OK(nt_status)) {
3488 DBG_WARNING("RBCD: sec_access_check_ds(access_desired=%#08x, "
3489 "access_granted:%#08x) failed with: %s\n",
3490 access_desired,
3491 access_granted,
3492 nt_errstr(nt_status));
3494 code = KRB5KDC_ERR_BADOPTION;
3495 goto out;
3498 DBG_NOTICE("RBCD: Access granted for client[%s]\n", client_name);
3500 code = 0;
3501 out:
3502 SAFE_FREE(client_name);
3503 SAFE_FREE(server_name);
3505 TALLOC_FREE(mem_ctx);
3506 return code;
3509 NTSTATUS samba_kdc_setup_db_ctx(TALLOC_CTX *mem_ctx, struct samba_kdc_base_context *base_ctx,
3510 struct samba_kdc_db_context **kdc_db_ctx_out)
3512 int ldb_ret;
3513 struct ldb_message *msg;
3514 struct auth_session_info *session_info;
3515 struct samba_kdc_db_context *kdc_db_ctx;
3516 /* The idea here is very simple. Using Kerberos to
3517 * authenticate the KDC to the LDAP server is highly likely to
3518 * be circular.
3520 * In future we may set this up to use EXERNAL and SSL
3521 * certificates, for now it will almost certainly be NTLMSSP_SET_USERNAME
3524 kdc_db_ctx = talloc_zero(mem_ctx, struct samba_kdc_db_context);
3525 if (kdc_db_ctx == NULL) {
3526 return NT_STATUS_NO_MEMORY;
3528 kdc_db_ctx->ev_ctx = base_ctx->ev_ctx;
3529 kdc_db_ctx->lp_ctx = base_ctx->lp_ctx;
3530 kdc_db_ctx->msg_ctx = base_ctx->msg_ctx;
3532 /* get default kdc policy */
3533 lpcfg_default_kdc_policy(mem_ctx,
3534 base_ctx->lp_ctx,
3535 &kdc_db_ctx->policy.svc_tkt_lifetime,
3536 &kdc_db_ctx->policy.usr_tkt_lifetime,
3537 &kdc_db_ctx->policy.renewal_lifetime);
3539 session_info = system_session(kdc_db_ctx->lp_ctx);
3540 if (session_info == NULL) {
3541 talloc_free(kdc_db_ctx);
3542 return NT_STATUS_INTERNAL_ERROR;
3545 /* Setup the link to LDB */
3546 kdc_db_ctx->samdb = samdb_connect(kdc_db_ctx,
3547 base_ctx->ev_ctx,
3548 base_ctx->lp_ctx,
3549 session_info,
3550 NULL,
3552 if (kdc_db_ctx->samdb == NULL) {
3553 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot open samdb for KDC backend!"));
3554 talloc_free(kdc_db_ctx);
3555 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3558 /* Find out our own krbtgt kvno */
3559 ldb_ret = samdb_rodc(kdc_db_ctx->samdb, &kdc_db_ctx->rodc);
3560 if (ldb_ret != LDB_SUCCESS) {
3561 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine if we are an RODC in KDC backend: %s\n",
3562 ldb_errstring(kdc_db_ctx->samdb)));
3563 talloc_free(kdc_db_ctx);
3564 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3566 if (kdc_db_ctx->rodc) {
3567 int my_krbtgt_number;
3568 const char *secondary_keytab[] = { "msDS-SecondaryKrbTgtNumber", NULL };
3569 struct ldb_dn *account_dn;
3570 struct ldb_dn *server_dn = samdb_server_dn(kdc_db_ctx->samdb, kdc_db_ctx);
3571 if (!server_dn) {
3572 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine server DN in KDC backend: %s\n",
3573 ldb_errstring(kdc_db_ctx->samdb)));
3574 talloc_free(kdc_db_ctx);
3575 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3578 ldb_ret = samdb_reference_dn(kdc_db_ctx->samdb, kdc_db_ctx, server_dn,
3579 "serverReference", &account_dn);
3580 if (ldb_ret != LDB_SUCCESS) {
3581 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine server account in KDC backend: %s\n",
3582 ldb_errstring(kdc_db_ctx->samdb)));
3583 talloc_free(kdc_db_ctx);
3584 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3587 ldb_ret = samdb_reference_dn(kdc_db_ctx->samdb, kdc_db_ctx, account_dn,
3588 "msDS-KrbTgtLink", &kdc_db_ctx->krbtgt_dn);
3589 talloc_free(account_dn);
3590 if (ldb_ret != LDB_SUCCESS) {
3591 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine RODC krbtgt account in KDC backend: %s\n",
3592 ldb_errstring(kdc_db_ctx->samdb)));
3593 talloc_free(kdc_db_ctx);
3594 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3597 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb, kdc_db_ctx,
3598 &msg, kdc_db_ctx->krbtgt_dn, LDB_SCOPE_BASE,
3599 secondary_keytab,
3600 DSDB_SEARCH_NO_GLOBAL_CATALOG,
3601 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=*))");
3602 if (ldb_ret != LDB_SUCCESS) {
3603 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot read krbtgt account %s in KDC backend to get msDS-SecondaryKrbTgtNumber: %s: %s\n",
3604 ldb_dn_get_linearized(kdc_db_ctx->krbtgt_dn),
3605 ldb_errstring(kdc_db_ctx->samdb),
3606 ldb_strerror(ldb_ret)));
3607 talloc_free(kdc_db_ctx);
3608 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3610 my_krbtgt_number = ldb_msg_find_attr_as_int(msg, "msDS-SecondaryKrbTgtNumber", -1);
3611 if (my_krbtgt_number == -1) {
3612 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot read msDS-SecondaryKrbTgtNumber from krbtgt account %s in KDC backend: got %d\n",
3613 ldb_dn_get_linearized(kdc_db_ctx->krbtgt_dn),
3614 my_krbtgt_number));
3615 talloc_free(kdc_db_ctx);
3616 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3618 kdc_db_ctx->my_krbtgt_number = my_krbtgt_number;
3620 } else {
3621 kdc_db_ctx->my_krbtgt_number = 0;
3622 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb, kdc_db_ctx,
3623 &msg,
3624 ldb_get_default_basedn(kdc_db_ctx->samdb),
3625 LDB_SCOPE_SUBTREE,
3626 krbtgt_attrs,
3627 DSDB_SEARCH_NO_GLOBAL_CATALOG,
3628 "(&(objectClass=user)(samAccountName=krbtgt))");
3630 if (ldb_ret != LDB_SUCCESS) {
3631 DEBUG(1, ("samba_kdc_setup_db_ctx: could not find own KRBTGT in DB: %s\n", ldb_errstring(kdc_db_ctx->samdb)));
3632 talloc_free(kdc_db_ctx);
3633 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
3635 kdc_db_ctx->krbtgt_dn = talloc_steal(kdc_db_ctx, msg->dn);
3636 kdc_db_ctx->my_krbtgt_number = 0;
3637 talloc_free(msg);
3639 *kdc_db_ctx_out = kdc_db_ctx;
3640 return NT_STATUS_OK;
3643 krb5_error_code dsdb_extract_aes_256_key(krb5_context context,
3644 TALLOC_CTX *mem_ctx,
3645 const struct ldb_message *msg,
3646 uint32_t user_account_control,
3647 const uint32_t *kvno,
3648 uint32_t *kvno_out,
3649 DATA_BLOB *aes_256_key,
3650 DATA_BLOB *salt)
3652 krb5_error_code krb5_ret;
3653 uint32_t supported_enctypes;
3654 unsigned flags = SDB_F_GET_CLIENT;
3655 struct sdb_entry sentry = {};
3657 if (kvno != NULL) {
3658 flags |= SDB_F_KVNO_SPECIFIED;
3661 krb5_ret = samba_kdc_message2entry_keys(context,
3662 mem_ctx,
3663 msg,
3664 false, /* is_krbtgt */
3665 false, /* is_rodc */
3666 user_account_control,
3667 SAMBA_KDC_ENT_TYPE_CLIENT,
3668 flags,
3669 (kvno != NULL) ? *kvno : 0,
3670 &sentry,
3671 ENC_HMAC_SHA1_96_AES256,
3672 &supported_enctypes);
3673 if (krb5_ret != 0) {
3674 DBG_ERR("Failed to parse supplementalCredentials "
3675 "of %s with %s kvno using "
3676 "ENCTYPE_HMAC_SHA1_96_AES256 "
3677 "Kerberos Key: %s\n",
3678 ldb_dn_get_linearized(msg->dn),
3679 (kvno != NULL) ? "previous" : "current",
3680 krb5_get_error_message(context,
3681 krb5_ret));
3682 return krb5_ret;
3685 if ((supported_enctypes & ENC_HMAC_SHA1_96_AES256) == 0 ||
3686 sentry.keys.len != 1) {
3687 DBG_INFO("Failed to find a ENCTYPE_HMAC_SHA1_96_AES256 "
3688 "key in supplementalCredentials "
3689 "of %s at KVNO %u (got %u keys, expected 1)\n",
3690 ldb_dn_get_linearized(msg->dn),
3691 sentry.kvno,
3692 sentry.keys.len);
3693 sdb_entry_free(&sentry);
3694 return ENOENT;
3697 if (sentry.keys.val[0].salt == NULL) {
3698 DBG_INFO("Failed to find a salt in "
3699 "supplementalCredentials "
3700 "of %s at KVNO %u\n",
3701 ldb_dn_get_linearized(msg->dn),
3702 sentry.kvno);
3703 sdb_entry_free(&sentry);
3704 return ENOENT;
3707 if (aes_256_key != NULL) {
3708 *aes_256_key = data_blob_talloc(mem_ctx,
3709 KRB5_KEY_DATA(&sentry.keys.val[0].key),
3710 KRB5_KEY_LENGTH(&sentry.keys.val[0].key));
3711 if (aes_256_key->data == NULL) {
3712 sdb_entry_free(&sentry);
3713 return ENOMEM;
3715 talloc_keep_secret(aes_256_key->data);
3718 if (salt != NULL) {
3719 *salt = data_blob_talloc(mem_ctx,
3720 sentry.keys.val[0].salt->salt.data,
3721 sentry.keys.val[0].salt->salt.length);
3722 if (salt->data == NULL) {
3723 sdb_entry_free(&sentry);
3724 return ENOMEM;
3728 if (kvno_out != NULL) {
3729 *kvno_out = sentry.kvno;
3732 sdb_entry_free(&sentry);
3734 return 0;