swrap: Include the function name in the debug output.
[Samba.git] / source4 / kdc / db-glue.c
blobb0c3e7a05cca5f33b0121e9446e48416e2edbe5d
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 "auth/auth.h"
27 #include "auth/auth_sam.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "dsdb/common/util.h"
30 #include "librpc/gen_ndr/ndr_drsblobs.h"
31 #include "param/param.h"
32 #include "../lib/crypto/md4.h"
33 #include "system/kerberos.h"
34 #include "auth/kerberos/kerberos.h"
35 #include <hdb.h>
36 #include "kdc/samba_kdc.h"
37 #include "kdc/kdc-glue.h"
38 #include "kdc/db-glue.h"
40 #define SAMBA_KVNO_GET_KRBTGT(kvno) \
41 ((uint16_t)(((uint32_t)kvno) >> 16))
43 #define SAMBA_KVNO_AND_KRBTGT(kvno, krbtgt) \
44 ((krb5_kvno)((((uint32_t)kvno) & 0xFFFF) | \
45 ((((uint32_t)krbtgt) << 16) & 0xFFFF0000)))
47 enum samba_kdc_ent_type
48 { SAMBA_KDC_ENT_TYPE_CLIENT, SAMBA_KDC_ENT_TYPE_SERVER,
49 SAMBA_KDC_ENT_TYPE_KRBTGT, SAMBA_KDC_ENT_TYPE_TRUST, SAMBA_KDC_ENT_TYPE_ANY };
51 enum trust_direction {
52 UNKNOWN = 0,
53 INBOUND = LSA_TRUST_DIRECTION_INBOUND,
54 OUTBOUND = LSA_TRUST_DIRECTION_OUTBOUND
57 static const char *trust_attrs[] = {
58 "trustPartner",
59 "trustAuthIncoming",
60 "trustAuthOutgoing",
61 "whenCreated",
62 "msDS-SupportedEncryptionTypes",
63 "trustAttributes",
64 "trustDirection",
65 "trustType",
66 NULL
70 static KerberosTime ldb_msg_find_krb5time_ldap_time(struct ldb_message *msg, const char *attr, KerberosTime default_val)
72 const char *tmp;
73 const char *gentime;
74 struct tm tm;
76 gentime = ldb_msg_find_attr_as_string(msg, attr, NULL);
77 if (!gentime)
78 return default_val;
80 tmp = strptime(gentime, "%Y%m%d%H%M%SZ", &tm);
81 if (tmp == NULL) {
82 return default_val;
85 return timegm(&tm);
88 static HDBFlags uf2HDBFlags(krb5_context context, uint32_t userAccountControl, enum samba_kdc_ent_type ent_type)
90 HDBFlags flags = int2HDBFlags(0);
92 /* we don't allow kadmin deletes */
93 flags.immutable = 1;
95 /* mark the principal as invalid to start with */
96 flags.invalid = 1;
98 flags.renewable = 1;
100 /* All accounts are servers, but this may be disabled again in the caller */
101 flags.server = 1;
103 /* Account types - clear the invalid bit if it turns out to be valid */
104 if (userAccountControl & UF_NORMAL_ACCOUNT) {
105 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
106 flags.client = 1;
108 flags.invalid = 0;
111 if (userAccountControl & UF_INTERDOMAIN_TRUST_ACCOUNT) {
112 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
113 flags.client = 1;
115 flags.invalid = 0;
117 if (userAccountControl & UF_WORKSTATION_TRUST_ACCOUNT) {
118 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
119 flags.client = 1;
121 flags.invalid = 0;
123 if (userAccountControl & UF_SERVER_TRUST_ACCOUNT) {
124 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
125 flags.client = 1;
127 flags.invalid = 0;
130 /* Not permitted to act as a client if disabled */
131 if (userAccountControl & UF_ACCOUNTDISABLE) {
132 flags.client = 0;
134 if (userAccountControl & UF_LOCKOUT) {
135 flags.locked_out = 1;
138 if (userAccountControl & UF_PASSWORD_NOTREQD) {
139 flags.invalid = 1;
143 UF_PASSWORD_CANT_CHANGE and UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED are irrelevent
145 if (userAccountControl & UF_TEMP_DUPLICATE_ACCOUNT) {
146 flags.invalid = 1;
149 /* UF_DONT_EXPIRE_PASSWD and UF_USE_DES_KEY_ONLY handled in samba_kdc_message2entry() */
152 if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
153 flags.invalid = 1;
156 if (userAccountControl & UF_SMARTCARD_REQUIRED) {
157 flags.require_hwauth = 1;
159 if (userAccountControl & UF_TRUSTED_FOR_DELEGATION) {
160 flags.ok_as_delegate = 1;
162 if (userAccountControl & UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION) {
164 * this is confusing...
166 * UF_TRUSTED_FOR_DELEGATION
167 * => ok_as_delegate
169 * and
171 * UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION
172 * => trusted_for_delegation
174 flags.trusted_for_delegation = 1;
176 if (!(userAccountControl & UF_NOT_DELEGATED)) {
177 flags.forwardable = 1;
178 flags.proxiable = 1;
181 if (userAccountControl & UF_DONT_REQUIRE_PREAUTH) {
182 flags.require_preauth = 0;
183 } else {
184 flags.require_preauth = 1;
187 return flags;
190 static int samba_kdc_entry_destructor(struct samba_kdc_entry *p)
192 hdb_entry_ex *entry_ex = p->entry_ex;
193 free_hdb_entry(&entry_ex->entry);
194 return 0;
197 static void samba_kdc_free_entry(krb5_context context, hdb_entry_ex *entry_ex)
199 /* this function is called only from hdb_free_entry().
200 * Make sure we neutralize the destructor or we will
201 * get a double free later when hdb_free_entry() will
202 * try to call free_hdb_entry() */
203 talloc_set_destructor(entry_ex->ctx, NULL);
205 /* now proceed to free the talloc part */
206 talloc_free(entry_ex->ctx);
209 static krb5_error_code samba_kdc_message2entry_keys(krb5_context context,
210 struct samba_kdc_db_context *kdc_db_ctx,
211 TALLOC_CTX *mem_ctx,
212 struct ldb_message *msg,
213 uint32_t rid,
214 bool is_rodc,
215 uint32_t userAccountControl,
216 enum samba_kdc_ent_type ent_type,
217 hdb_entry_ex *entry_ex)
219 krb5_error_code ret = 0;
220 enum ndr_err_code ndr_err;
221 struct samr_Password *hash;
222 const struct ldb_val *sc_val;
223 struct supplementalCredentialsBlob scb;
224 struct supplementalCredentialsPackage *scpk = NULL;
225 bool newer_keys = false;
226 struct package_PrimaryKerberosBlob _pkb;
227 struct package_PrimaryKerberosCtr3 *pkb3 = NULL;
228 struct package_PrimaryKerberosCtr4 *pkb4 = NULL;
229 uint16_t i;
230 uint16_t allocated_keys = 0;
231 int rodc_krbtgt_number = 0;
232 int kvno = 0;
233 uint32_t supported_enctypes
234 = ldb_msg_find_attr_as_uint(msg,
235 "msDS-SupportedEncryptionTypes",
238 if (rid == DOMAIN_RID_KRBTGT || is_rodc) {
239 /* KDCs (and KDCs on RODCs) use AES */
240 supported_enctypes |= ENC_HMAC_SHA1_96_AES128 | ENC_HMAC_SHA1_96_AES256;
241 } else if (userAccountControl & (UF_PARTIAL_SECRETS_ACCOUNT|UF_SERVER_TRUST_ACCOUNT)) {
242 /* DCs and RODCs comptuer accounts use AES */
243 supported_enctypes |= ENC_HMAC_SHA1_96_AES128 | ENC_HMAC_SHA1_96_AES256;
244 } else if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT ||
245 (ent_type == SAMBA_KDC_ENT_TYPE_ANY)) {
246 /* for AS-REQ the client chooses the enc types it
247 * supports, and this will vary between computers a
248 * user logs in from.
250 * likewise for 'any' return as much as is supported,
251 * to export into a keytab */
252 supported_enctypes = ENC_ALL_TYPES;
255 /* If UF_USE_DES_KEY_ONLY has been set, then don't allow use of the newer enc types */
256 if (userAccountControl & UF_USE_DES_KEY_ONLY) {
257 supported_enctypes = ENC_CRC32|ENC_RSA_MD5;
258 } else {
259 /* Otherwise, add in the default enc types */
260 supported_enctypes |= ENC_CRC32 | ENC_RSA_MD5 | ENC_RC4_HMAC_MD5;
263 /* Is this the krbtgt or a RODC krbtgt */
264 if (is_rodc) {
265 rodc_krbtgt_number = ldb_msg_find_attr_as_int(msg, "msDS-SecondaryKrbTgtNumber", -1);
267 if (rodc_krbtgt_number == -1) {
268 return EINVAL;
272 entry_ex->entry.keys.val = NULL;
273 entry_ex->entry.keys.len = 0;
275 kvno = ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0);
276 if (is_rodc) {
277 kvno = SAMBA_KVNO_AND_KRBTGT(kvno, rodc_krbtgt_number);
279 entry_ex->entry.kvno = kvno;
281 /* Get keys from the db */
283 hash = samdb_result_hash(mem_ctx, msg, "unicodePwd");
284 sc_val = ldb_msg_find_ldb_val(msg, "supplementalCredentials");
286 /* unicodePwd for enctype 0x17 (23) if present */
287 if (hash) {
288 allocated_keys++;
291 /* supplementalCredentials if present */
292 if (sc_val) {
293 ndr_err = ndr_pull_struct_blob_all(sc_val, mem_ctx, &scb,
294 (ndr_pull_flags_fn_t)ndr_pull_supplementalCredentialsBlob);
295 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
296 dump_data(0, sc_val->data, sc_val->length);
297 ret = EINVAL;
298 goto out;
301 if (scb.sub.signature != SUPPLEMENTAL_CREDENTIALS_SIGNATURE) {
302 NDR_PRINT_DEBUG(supplementalCredentialsBlob, &scb);
303 ret = EINVAL;
304 goto out;
307 for (i=0; i < scb.sub.num_packages; i++) {
308 if (strcmp("Primary:Kerberos-Newer-Keys", scb.sub.packages[i].name) == 0) {
309 scpk = &scb.sub.packages[i];
310 if (!scpk->data || !scpk->data[0]) {
311 scpk = NULL;
312 continue;
314 newer_keys = true;
315 break;
316 } else if (strcmp("Primary:Kerberos", scb.sub.packages[i].name) == 0) {
317 scpk = &scb.sub.packages[i];
318 if (!scpk->data || !scpk->data[0]) {
319 scpk = NULL;
322 * we don't break here in hope to find
323 * a Kerberos-Newer-Keys package
329 * Primary:Kerberos-Newer-Keys or Primary:Kerberos element
330 * of supplementalCredentials
332 if (scpk) {
333 DATA_BLOB blob;
335 blob = strhex_to_data_blob(mem_ctx, scpk->data);
336 if (!blob.data) {
337 ret = ENOMEM;
338 goto out;
341 /* we cannot use ndr_pull_struct_blob_all() here, as w2k and w2k3 add padding bytes */
342 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &_pkb,
343 (ndr_pull_flags_fn_t)ndr_pull_package_PrimaryKerberosBlob);
344 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
345 ret = EINVAL;
346 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
347 krb5_warnx(context, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
348 goto out;
351 if (newer_keys && _pkb.version != 4) {
352 ret = EINVAL;
353 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
354 krb5_warnx(context, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
355 goto out;
358 if (!newer_keys && _pkb.version != 3) {
359 ret = EINVAL;
360 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
361 krb5_warnx(context, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
362 goto out;
365 if (_pkb.version == 4) {
366 pkb4 = &_pkb.ctr.ctr4;
367 allocated_keys += pkb4->num_keys;
368 } else if (_pkb.version == 3) {
369 pkb3 = &_pkb.ctr.ctr3;
370 allocated_keys += pkb3->num_keys;
374 if (allocated_keys == 0) {
375 if (kdc_db_ctx->rodc) {
376 /* We are on an RODC, but don't have keys for this account. Signal this to the caller */
377 return HDB_ERR_NOT_FOUND_HERE;
380 /* oh, no password. Apparently (comment in
381 * hdb-ldap.c) this violates the ASN.1, but this
382 * allows an entry with no keys (yet). */
383 return 0;
386 /* allocate space to decode into */
387 entry_ex->entry.keys.len = 0;
388 entry_ex->entry.keys.val = calloc(allocated_keys, sizeof(Key));
389 if (entry_ex->entry.keys.val == NULL) {
390 ret = ENOMEM;
391 goto out;
394 if (hash && (supported_enctypes & ENC_RC4_HMAC_MD5)) {
395 Key key;
397 key.mkvno = 0;
398 key.salt = NULL; /* No salt for this enc type */
400 ret = krb5_keyblock_init(context,
401 ENCTYPE_ARCFOUR_HMAC,
402 hash->hash, sizeof(hash->hash),
403 &key.key);
404 if (ret) {
405 goto out;
408 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
409 entry_ex->entry.keys.len++;
412 if (pkb4) {
413 for (i=0; i < pkb4->num_keys; i++) {
414 Key key;
416 if (!pkb4->keys[i].value) continue;
418 if (!(kerberos_enctype_to_bitmap(pkb4->keys[i].keytype) & supported_enctypes)) {
419 continue;
422 key.mkvno = 0;
423 key.salt = NULL;
425 if (pkb4->salt.string) {
426 DATA_BLOB salt;
428 salt = data_blob_string_const(pkb4->salt.string);
430 key.salt = calloc(1, sizeof(*key.salt));
431 if (key.salt == NULL) {
432 ret = ENOMEM;
433 goto out;
436 key.salt->type = hdb_pw_salt;
438 ret = krb5_data_copy(&key.salt->salt, salt.data, salt.length);
439 if (ret) {
440 free(key.salt);
441 key.salt = NULL;
442 goto out;
446 /* TODO: maybe pass the iteration_count somehow... */
448 ret = krb5_keyblock_init(context,
449 pkb4->keys[i].keytype,
450 pkb4->keys[i].value->data,
451 pkb4->keys[i].value->length,
452 &key.key);
453 if (ret == KRB5_PROG_ETYPE_NOSUPP) {
454 DEBUG(2,("Unsupported keytype ignored - type %u\n",
455 pkb4->keys[i].keytype));
456 ret = 0;
457 continue;
459 if (ret) {
460 if (key.salt) {
461 free_Salt(key.salt);
462 free(key.salt);
463 key.salt = NULL;
465 goto out;
468 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
469 entry_ex->entry.keys.len++;
471 } else if (pkb3) {
472 for (i=0; i < pkb3->num_keys; i++) {
473 Key key;
475 if (!pkb3->keys[i].value) continue;
477 if (!(kerberos_enctype_to_bitmap(pkb3->keys[i].keytype) & supported_enctypes)) {
478 continue;
481 key.mkvno = 0;
482 key.salt = NULL;
484 if (pkb3->salt.string) {
485 DATA_BLOB salt;
487 salt = data_blob_string_const(pkb3->salt.string);
489 key.salt = calloc(1, sizeof(*key.salt));
490 if (key.salt == NULL) {
491 ret = ENOMEM;
492 goto out;
495 key.salt->type = hdb_pw_salt;
497 ret = krb5_data_copy(&key.salt->salt, salt.data, salt.length);
498 if (ret) {
499 free(key.salt);
500 key.salt = NULL;
501 goto out;
505 ret = krb5_keyblock_init(context,
506 pkb3->keys[i].keytype,
507 pkb3->keys[i].value->data,
508 pkb3->keys[i].value->length,
509 &key.key);
510 if (ret) {
511 if (key.salt) {
512 free_Salt(key.salt);
513 free(key.salt);
514 key.salt = NULL;
516 goto out;
519 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
520 entry_ex->entry.keys.len++;
524 out:
525 if (ret != 0) {
526 entry_ex->entry.keys.len = 0;
528 if (entry_ex->entry.keys.len == 0 && entry_ex->entry.keys.val) {
529 free(entry_ex->entry.keys.val);
530 entry_ex->entry.keys.val = NULL;
532 return ret;
536 * Construct an hdb_entry from a directory entry.
538 static krb5_error_code samba_kdc_message2entry(krb5_context context,
539 struct samba_kdc_db_context *kdc_db_ctx,
540 TALLOC_CTX *mem_ctx, krb5_const_principal principal,
541 enum samba_kdc_ent_type ent_type,
542 unsigned flags,
543 struct ldb_dn *realm_dn,
544 struct ldb_message *msg,
545 hdb_entry_ex *entry_ex)
547 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
548 uint32_t userAccountControl;
549 uint32_t msDS_User_Account_Control_Computed;
550 unsigned int i;
551 krb5_error_code ret = 0;
552 krb5_boolean is_computer = FALSE;
554 struct samba_kdc_entry *p;
555 NTTIME acct_expiry;
556 NTSTATUS status;
558 uint32_t rid;
559 bool is_rodc = false;
560 struct ldb_message_element *objectclasses;
561 struct ldb_val computer_val;
562 const char *samAccountName = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
563 computer_val.data = discard_const_p(uint8_t,"computer");
564 computer_val.length = strlen((const char *)computer_val.data);
566 if (ldb_msg_find_element(msg, "msDS-SecondaryKrbTgtNumber")) {
567 is_rodc = true;
570 if (!samAccountName) {
571 ret = ENOENT;
572 krb5_set_error_message(context, ret, "samba_kdc_message2entry: no samAccountName present");
573 goto out;
576 objectclasses = ldb_msg_find_element(msg, "objectClass");
578 if (objectclasses && ldb_msg_find_val(objectclasses, &computer_val)) {
579 is_computer = TRUE;
582 memset(entry_ex, 0, sizeof(*entry_ex));
584 p = talloc(mem_ctx, struct samba_kdc_entry);
585 if (!p) {
586 ret = ENOMEM;
587 goto out;
590 p->kdc_db_ctx = kdc_db_ctx;
591 p->entry_ex = entry_ex;
592 p->realm_dn = talloc_reference(p, realm_dn);
593 if (!p->realm_dn) {
594 ret = ENOMEM;
595 goto out;
598 talloc_set_destructor(p, samba_kdc_entry_destructor);
600 /* make sure we do not have bogus data in there */
601 memset(&entry_ex->entry, 0, sizeof(hdb_entry));
603 entry_ex->ctx = p;
604 entry_ex->free_entry = samba_kdc_free_entry;
606 userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
608 msDS_User_Account_Control_Computed
609 = ldb_msg_find_attr_as_uint(msg,
610 "msDS-User-Account-Control-Computed",
611 UF_ACCOUNTDISABLE);
614 * This brings in the lockout flag, block the account if not
615 * found. We need the weird UF_ACCOUNTDISABLE check because
616 * we do not want to fail open if the value is not returned,
617 * but 0 is a valid value (all OK)
619 if (msDS_User_Account_Control_Computed == UF_ACCOUNTDISABLE) {
620 ret = EINVAL;
621 krb5_set_error_message(context, ret, "samba_kdc_message2entry: "
622 "no msDS-User-Account-Control-Computed present");
623 goto out;
624 } else {
625 userAccountControl |= msDS_User_Account_Control_Computed;
628 entry_ex->entry.principal = malloc(sizeof(*(entry_ex->entry.principal)));
629 if (ent_type == SAMBA_KDC_ENT_TYPE_ANY && principal == NULL) {
630 krb5_make_principal(context, &entry_ex->entry.principal, lpcfg_realm(lp_ctx), samAccountName, NULL);
631 } else {
632 ret = copy_Principal(principal, entry_ex->entry.principal);
633 if (ret) {
634 krb5_clear_error_message(context);
635 goto out;
638 /* While we have copied the client principal, tests
639 * show that Win2k3 returns the 'corrected' realm, not
640 * the client-specified realm. This code attempts to
641 * replace the client principal's realm with the one
642 * we determine from our records */
644 /* this has to be with malloc() */
645 krb5_principal_set_realm(context, entry_ex->entry.principal, lpcfg_realm(lp_ctx));
648 /* First try and figure out the flags based on the userAccountControl */
649 entry_ex->entry.flags = uf2HDBFlags(context, userAccountControl, ent_type);
651 /* Windows 2008 seems to enforce this (very sensible) rule by
652 * default - don't allow offline attacks on a user's password
653 * by asking for a ticket to them as a service (encrypted with
654 * their probably patheticly insecure password) */
656 if (entry_ex->entry.flags.server
657 && lpcfg_parm_bool(lp_ctx, NULL, "kdc", "require spn for service", true)) {
658 if (!is_computer && !ldb_msg_find_attr_as_string(msg, "servicePrincipalName", NULL)) {
659 entry_ex->entry.flags.server = 0;
663 if (flags & HDB_F_ADMIN_DATA) {
664 /* These (created_by, modified_by) parts of the entry are not relevant for Samba4's use
665 * of the Heimdal KDC. They are stored in a the traditional
666 * DB for audit purposes, and still form part of the structure
667 * we must return */
669 /* use 'whenCreated' */
670 entry_ex->entry.created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
671 /* use 'kadmin' for now (needed by mit_samba) */
672 krb5_make_principal(context,
673 &entry_ex->entry.created_by.principal,
674 lpcfg_realm(lp_ctx), "kadmin", NULL);
676 entry_ex->entry.modified_by = (Event *) malloc(sizeof(Event));
677 if (entry_ex->entry.modified_by == NULL) {
678 ret = ENOMEM;
679 krb5_set_error_message(context, ret, "malloc: out of memory");
680 goto out;
683 /* use 'whenChanged' */
684 entry_ex->entry.modified_by->time = ldb_msg_find_krb5time_ldap_time(msg, "whenChanged", 0);
685 /* use 'kadmin' for now (needed by mit_samba) */
686 krb5_make_principal(context,
687 &entry_ex->entry.modified_by->principal,
688 lpcfg_realm(lp_ctx), "kadmin", NULL);
692 /* The lack of password controls etc applies to krbtgt by
693 * virtue of being that particular RID */
694 status = dom_sid_split_rid(NULL, samdb_result_dom_sid(mem_ctx, msg, "objectSid"), NULL, &rid);
696 if (!NT_STATUS_IS_OK(status)) {
697 ret = EINVAL;
698 goto out;
701 if (rid == DOMAIN_RID_KRBTGT) {
702 entry_ex->entry.valid_end = NULL;
703 entry_ex->entry.pw_end = NULL;
705 entry_ex->entry.flags.invalid = 0;
706 entry_ex->entry.flags.server = 1;
708 /* Don't mark all requests for the krbtgt/realm as
709 * 'change password', as otherwise we could get into
710 * trouble, and not enforce the password expirty.
711 * Instead, only do it when request is for the kpasswd service */
712 if (ent_type == SAMBA_KDC_ENT_TYPE_SERVER
713 && principal->name.name_string.len == 2
714 && (strcmp(principal->name.name_string.val[0], "kadmin") == 0)
715 && (strcmp(principal->name.name_string.val[1], "changepw") == 0)
716 && lpcfg_is_my_domain_or_realm(lp_ctx, principal->realm)) {
717 entry_ex->entry.flags.change_pw = 1;
719 entry_ex->entry.flags.client = 0;
720 entry_ex->entry.flags.forwardable = 1;
721 entry_ex->entry.flags.ok_as_delegate = 1;
722 } else if (is_rodc) {
723 /* The RODC krbtgt account is like the main krbtgt,
724 * but it does not have a changepw or kadmin
725 * service */
727 entry_ex->entry.valid_end = NULL;
728 entry_ex->entry.pw_end = NULL;
730 /* Also don't allow the RODC krbtgt to be a client (it should not be needed) */
731 entry_ex->entry.flags.client = 0;
732 entry_ex->entry.flags.invalid = 0;
733 entry_ex->entry.flags.server = 1;
735 entry_ex->entry.flags.client = 0;
736 entry_ex->entry.flags.forwardable = 1;
737 entry_ex->entry.flags.ok_as_delegate = 0;
738 } else if (entry_ex->entry.flags.server && ent_type == SAMBA_KDC_ENT_TYPE_SERVER) {
739 /* The account/password expiry only applies when the account is used as a
740 * client (ie password login), not when used as a server */
742 /* Make very well sure we don't use this for a client,
743 * it could bypass the password restrictions */
744 entry_ex->entry.flags.client = 0;
746 entry_ex->entry.valid_end = NULL;
747 entry_ex->entry.pw_end = NULL;
749 } else {
750 NTTIME must_change_time
751 = samdb_result_force_password_change(kdc_db_ctx->samdb, mem_ctx,
752 realm_dn, msg);
753 if (must_change_time == 0x7FFFFFFFFFFFFFFFULL) {
754 entry_ex->entry.pw_end = NULL;
755 } else {
756 entry_ex->entry.pw_end = malloc(sizeof(*entry_ex->entry.pw_end));
757 if (entry_ex->entry.pw_end == NULL) {
758 ret = ENOMEM;
759 goto out;
761 *entry_ex->entry.pw_end = nt_time_to_unix(must_change_time);
764 acct_expiry = samdb_result_account_expires(msg);
765 if (acct_expiry == 0x7FFFFFFFFFFFFFFFULL) {
766 entry_ex->entry.valid_end = NULL;
767 } else {
768 entry_ex->entry.valid_end = malloc(sizeof(*entry_ex->entry.valid_end));
769 if (entry_ex->entry.valid_end == NULL) {
770 ret = ENOMEM;
771 goto out;
773 *entry_ex->entry.valid_end = nt_time_to_unix(acct_expiry);
777 entry_ex->entry.valid_start = NULL;
779 entry_ex->entry.max_life = malloc(sizeof(*entry_ex->entry.max_life));
780 if (entry_ex->entry.max_life == NULL) {
781 ret = ENOMEM;
782 goto out;
785 if (ent_type == SAMBA_KDC_ENT_TYPE_SERVER) {
786 *entry_ex->entry.max_life = kdc_db_ctx->policy.svc_tkt_lifetime;
787 } else if (ent_type == SAMBA_KDC_ENT_TYPE_KRBTGT || ent_type == SAMBA_KDC_ENT_TYPE_CLIENT) {
788 *entry_ex->entry.max_life = kdc_db_ctx->policy.usr_tkt_lifetime;
789 } else {
790 *entry_ex->entry.max_life = MIN(kdc_db_ctx->policy.svc_tkt_lifetime,
791 kdc_db_ctx->policy.usr_tkt_lifetime);
794 entry_ex->entry.max_renew = malloc(sizeof(*entry_ex->entry.max_life));
795 if (entry_ex->entry.max_renew == NULL) {
796 ret = ENOMEM;
797 goto out;
800 *entry_ex->entry.max_renew = kdc_db_ctx->policy.renewal_lifetime;
802 entry_ex->entry.generation = NULL;
804 /* Get keys from the db */
805 ret = samba_kdc_message2entry_keys(context, kdc_db_ctx, p, msg,
806 rid, is_rodc, userAccountControl,
807 ent_type, entry_ex);
808 if (ret) {
809 /* Could be bougus data in the entry, or out of memory */
810 goto out;
813 entry_ex->entry.etypes = malloc(sizeof(*(entry_ex->entry.etypes)));
814 if (entry_ex->entry.etypes == NULL) {
815 krb5_clear_error_message(context);
816 ret = ENOMEM;
817 goto out;
819 entry_ex->entry.etypes->len = entry_ex->entry.keys.len;
820 entry_ex->entry.etypes->val = calloc(entry_ex->entry.etypes->len, sizeof(int));
821 if (entry_ex->entry.etypes->val == NULL) {
822 krb5_clear_error_message(context);
823 ret = ENOMEM;
824 goto out;
826 for (i=0; i < entry_ex->entry.etypes->len; i++) {
827 entry_ex->entry.etypes->val[i] = entry_ex->entry.keys.val[i].key.keytype;
831 p->msg = talloc_steal(p, msg);
833 out:
834 if (ret != 0) {
835 /* This doesn't free ent itself, that is for the eventual caller to do */
836 hdb_free_entry(context, entry_ex);
837 } else {
838 talloc_steal(kdc_db_ctx, entry_ex->ctx);
841 return ret;
845 * Construct an hdb_entry from a directory entry.
846 * The kvno is what the remote client asked for
848 static krb5_error_code samba_kdc_trust_message2entry(krb5_context context,
849 struct samba_kdc_db_context *kdc_db_ctx,
850 TALLOC_CTX *mem_ctx, krb5_const_principal principal,
851 enum trust_direction direction,
852 struct ldb_dn *realm_dn,
853 unsigned flags,
854 uint32_t kvno,
855 struct ldb_message *msg,
856 hdb_entry_ex *entry_ex)
858 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
859 const char *dnsdomain;
860 const char *realm = lpcfg_realm(lp_ctx);
861 DATA_BLOB password_utf16;
862 struct samr_Password password_hash;
863 const struct ldb_val *password_val;
864 struct trustAuthInOutBlob password_blob;
865 struct samba_kdc_entry *p;
866 bool use_previous;
867 uint32_t current_kvno;
868 enum ndr_err_code ndr_err;
869 int ret, trust_direction_flags;
870 unsigned int i;
871 struct AuthenticationInformationArray *auth_array;
873 p = talloc(mem_ctx, struct samba_kdc_entry);
874 if (!p) {
875 ret = ENOMEM;
876 goto out;
879 p->kdc_db_ctx = kdc_db_ctx;
880 p->entry_ex = entry_ex;
881 p->realm_dn = realm_dn;
883 talloc_set_destructor(p, samba_kdc_entry_destructor);
885 /* make sure we do not have bogus data in there */
886 memset(&entry_ex->entry, 0, sizeof(hdb_entry));
888 entry_ex->ctx = p;
889 entry_ex->free_entry = samba_kdc_free_entry;
891 /* use 'whenCreated' */
892 entry_ex->entry.created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
893 /* use 'kadmin' for now (needed by mit_samba) */
894 krb5_make_principal(context,
895 &entry_ex->entry.created_by.principal,
896 realm, "kadmin", NULL);
898 entry_ex->entry.valid_start = NULL;
900 trust_direction_flags = ldb_msg_find_attr_as_int(msg, "trustDirection", 0);
902 if (direction == INBOUND) {
903 password_val = ldb_msg_find_ldb_val(msg, "trustAuthIncoming");
905 } else { /* OUTBOUND */
906 dnsdomain = ldb_msg_find_attr_as_string(msg, "trustPartner", NULL);
907 /* replace realm */
908 realm = strupper_talloc(mem_ctx, dnsdomain);
909 password_val = ldb_msg_find_ldb_val(msg, "trustAuthOutgoing");
912 if (!password_val || !(trust_direction_flags & direction)) {
913 ret = ENOENT;
914 goto out;
917 ndr_err = ndr_pull_struct_blob(password_val, mem_ctx, &password_blob,
918 (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
919 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
920 ret = EINVAL;
921 goto out;
925 /* we need to work out if we are going to use the current or
926 * the previous password hash.
927 * We base this on the kvno the client passes in. If the kvno
928 * passed in is equal to the current kvno in our database then
929 * we use the current structure. If it is the current kvno-1,
930 * then we use the previous substrucure.
933 /* first work out the current kvno */
934 current_kvno = 0;
935 for (i=0; i < password_blob.count; i++) {
936 if (password_blob.current.array[i].AuthType == TRUST_AUTH_TYPE_VERSION) {
937 current_kvno = password_blob.current.array[i].AuthInfo.version.version;
941 /* work out whether we will use the previous or current
942 password */
943 if (password_blob.previous.count == 0) {
944 /* there is no previous password */
945 use_previous = false;
946 } else if (!(flags & HDB_F_KVNO_SPECIFIED) ||
947 kvno == current_kvno) {
948 use_previous = false;
949 } else if ((kvno+1 == current_kvno) ||
950 (kvno == 255 && current_kvno == 0)) {
951 use_previous = true;
952 } else {
953 DEBUG(1,(__location__ ": Request for unknown kvno %u - current kvno is %u\n",
954 kvno, current_kvno));
955 ret = ENOENT;
956 goto out;
959 if (use_previous) {
960 auth_array = &password_blob.previous;
961 } else {
962 auth_array = &password_blob.current;
965 /* use the kvno the client specified, if available */
966 if (flags & HDB_F_KVNO_SPECIFIED) {
967 entry_ex->entry.kvno = kvno;
968 } else {
969 entry_ex->entry.kvno = current_kvno;
972 for (i=0; i < auth_array->count; i++) {
973 if (auth_array->array[i].AuthType == TRUST_AUTH_TYPE_CLEAR) {
974 password_utf16 = data_blob_const(auth_array->array[i].AuthInfo.clear.password,
975 auth_array->array[i].AuthInfo.clear.size);
976 /* In the future, generate all sorts of
977 * hashes, but for now we can't safely convert
978 * the random strings windows uses into
979 * utf8 */
981 /* but as it is utf16 already, we can get the NT password/arcfour-hmac-md5 key */
982 mdfour(password_hash.hash, password_utf16.data, password_utf16.length);
983 break;
984 } else if (auth_array->array[i].AuthType == TRUST_AUTH_TYPE_NT4OWF) {
985 password_hash = auth_array->array[i].AuthInfo.nt4owf.password;
986 break;
990 if (i < auth_array->count) {
991 Key key;
992 /* Must have found a cleartext or MD4 password */
993 entry_ex->entry.keys.val = calloc(1, sizeof(Key));
995 key.mkvno = 0;
996 key.salt = NULL; /* No salt for this enc type */
998 if (entry_ex->entry.keys.val == NULL) {
999 ret = ENOMEM;
1000 goto out;
1003 ret = krb5_keyblock_init(context,
1004 ENCTYPE_ARCFOUR_HMAC,
1005 password_hash.hash, sizeof(password_hash.hash),
1006 &key.key);
1007 if (ret != 0) {
1008 goto out;
1011 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
1012 entry_ex->entry.keys.len++;
1015 entry_ex->entry.principal = malloc(sizeof(*(entry_ex->entry.principal)));
1017 ret = copy_Principal(principal, entry_ex->entry.principal);
1018 if (ret) {
1019 krb5_clear_error_message(context);
1020 goto out;
1023 /* While we have copied the client principal, tests
1024 * show that Win2k3 returns the 'corrected' realm, not
1025 * the client-specified realm. This code attempts to
1026 * replace the client principal's realm with the one
1027 * we determine from our records */
1029 krb5_principal_set_realm(context, entry_ex->entry.principal, realm);
1030 entry_ex->entry.flags = int2HDBFlags(0);
1031 entry_ex->entry.flags.immutable = 1;
1032 entry_ex->entry.flags.invalid = 0;
1033 entry_ex->entry.flags.server = 1;
1034 entry_ex->entry.flags.require_preauth = 1;
1036 entry_ex->entry.pw_end = NULL;
1038 entry_ex->entry.max_life = NULL;
1040 entry_ex->entry.max_renew = NULL;
1042 entry_ex->entry.generation = NULL;
1044 entry_ex->entry.etypes = malloc(sizeof(*(entry_ex->entry.etypes)));
1045 if (entry_ex->entry.etypes == NULL) {
1046 krb5_clear_error_message(context);
1047 ret = ENOMEM;
1048 goto out;
1050 entry_ex->entry.etypes->len = entry_ex->entry.keys.len;
1051 entry_ex->entry.etypes->val = calloc(entry_ex->entry.etypes->len, sizeof(int));
1052 if (entry_ex->entry.etypes->val == NULL) {
1053 krb5_clear_error_message(context);
1054 ret = ENOMEM;
1055 goto out;
1057 for (i=0; i < entry_ex->entry.etypes->len; i++) {
1058 entry_ex->entry.etypes->val[i] = entry_ex->entry.keys.val[i].key.keytype;
1062 p->msg = talloc_steal(p, msg);
1064 out:
1065 if (ret != 0) {
1066 /* This doesn't free ent itself, that is for the eventual caller to do */
1067 hdb_free_entry(context, entry_ex);
1068 } else {
1069 talloc_steal(kdc_db_ctx, entry_ex->ctx);
1072 return ret;
1076 static krb5_error_code samba_kdc_lookup_trust(krb5_context context, struct ldb_context *ldb_ctx,
1077 TALLOC_CTX *mem_ctx,
1078 const char *realm,
1079 struct ldb_dn *realm_dn,
1080 struct ldb_message **pmsg)
1082 NTSTATUS status;
1083 const char * const *attrs = trust_attrs;
1085 status = sam_get_results_trust(ldb_ctx,
1086 mem_ctx, realm, realm, attrs,
1087 pmsg);
1088 if (NT_STATUS_IS_OK(status)) {
1089 return 0;
1090 } else if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
1091 return HDB_ERR_NOENTRY;
1092 } else if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1093 int ret = ENOMEM;
1094 krb5_set_error_message(context, ret, "get_sam_result_trust: out of memory");
1095 return ret;
1096 } else {
1097 int ret = EINVAL;
1098 krb5_set_error_message(context, ret, "get_sam_result_trust: %s", nt_errstr(status));
1099 return ret;
1103 static krb5_error_code samba_kdc_lookup_client(krb5_context context,
1104 struct samba_kdc_db_context *kdc_db_ctx,
1105 TALLOC_CTX *mem_ctx,
1106 krb5_const_principal principal,
1107 const char **attrs,
1108 struct ldb_dn **realm_dn,
1109 struct ldb_message **msg) {
1110 NTSTATUS nt_status;
1111 char *principal_string;
1112 krb5_error_code ret;
1114 ret = krb5_unparse_name(context, principal, &principal_string);
1116 if (ret != 0) {
1117 return ret;
1120 nt_status = sam_get_results_principal(kdc_db_ctx->samdb,
1121 mem_ctx, principal_string, attrs,
1122 realm_dn, msg);
1123 free(principal_string);
1124 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
1125 return HDB_ERR_NOENTRY;
1126 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
1127 return ENOMEM;
1128 } else if (!NT_STATUS_IS_OK(nt_status)) {
1129 return EINVAL;
1132 return ret;
1135 static krb5_error_code samba_kdc_fetch_client(krb5_context context,
1136 struct samba_kdc_db_context *kdc_db_ctx,
1137 TALLOC_CTX *mem_ctx,
1138 krb5_const_principal principal,
1139 unsigned flags,
1140 hdb_entry_ex *entry_ex) {
1141 struct ldb_dn *realm_dn;
1142 krb5_error_code ret;
1143 struct ldb_message *msg = NULL;
1145 ret = samba_kdc_lookup_client(context, kdc_db_ctx,
1146 mem_ctx, principal, user_attrs,
1147 &realm_dn, &msg);
1148 if (ret != 0) {
1149 return ret;
1152 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1153 principal, SAMBA_KDC_ENT_TYPE_CLIENT,
1154 flags,
1155 realm_dn, msg, entry_ex);
1156 return ret;
1159 static krb5_error_code samba_kdc_fetch_krbtgt(krb5_context context,
1160 struct samba_kdc_db_context *kdc_db_ctx,
1161 TALLOC_CTX *mem_ctx,
1162 krb5_const_principal principal,
1163 unsigned flags,
1164 uint32_t kvno,
1165 hdb_entry_ex *entry_ex)
1167 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
1168 krb5_error_code ret;
1169 struct ldb_message *msg = NULL;
1170 struct ldb_dn *realm_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
1172 krb5_principal alloc_principal = NULL;
1173 if (principal->name.name_string.len != 2
1174 || (strcmp(principal->name.name_string.val[0], KRB5_TGS_NAME) != 0)) {
1175 /* Not a krbtgt */
1176 return HDB_ERR_NOENTRY;
1179 /* krbtgt case. Either us or a trusted realm */
1181 if (lpcfg_is_my_domain_or_realm(lp_ctx, principal->realm)
1182 && lpcfg_is_my_domain_or_realm(lp_ctx, principal->name.name_string.val[1])) {
1183 /* us, or someone quite like us */
1184 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
1185 * is in our db, then direct the caller at our primary
1186 * krbtgt */
1188 int lret;
1189 unsigned int krbtgt_number;
1190 /* w2k8r2 sometimes gives us a kvno of 255 for inter-domain
1191 trust tickets. We don't yet know what this means, but we do
1192 seem to need to treat it as unspecified */
1193 if (flags & HDB_F_KVNO_SPECIFIED) {
1194 krbtgt_number = SAMBA_KVNO_GET_KRBTGT(kvno);
1195 if (kdc_db_ctx->rodc) {
1196 if (krbtgt_number != kdc_db_ctx->my_krbtgt_number) {
1197 return HDB_ERR_NOT_FOUND_HERE;
1200 } else {
1201 krbtgt_number = kdc_db_ctx->my_krbtgt_number;
1204 if (krbtgt_number == kdc_db_ctx->my_krbtgt_number) {
1205 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx,
1206 &msg, kdc_db_ctx->krbtgt_dn, LDB_SCOPE_BASE,
1207 krbtgt_attrs, DSDB_SEARCH_NO_GLOBAL_CATALOG,
1208 "(objectClass=user)");
1209 } else {
1210 /* We need to look up an RODC krbtgt (perhaps
1211 * ours, if we are an RODC, perhaps another
1212 * RODC if we are a read-write DC */
1213 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx,
1214 &msg, realm_dn, LDB_SCOPE_SUBTREE,
1215 krbtgt_attrs,
1216 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
1217 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=%u))", (unsigned)(krbtgt_number));
1220 if (lret == LDB_ERR_NO_SUCH_OBJECT) {
1221 krb5_warnx(context, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1222 (unsigned)(krbtgt_number));
1223 krb5_set_error_message(context, HDB_ERR_NOENTRY,
1224 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1225 (unsigned)(krbtgt_number));
1226 return HDB_ERR_NOENTRY;
1227 } else if (lret != LDB_SUCCESS) {
1228 krb5_warnx(context, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1229 (unsigned)(krbtgt_number));
1230 krb5_set_error_message(context, HDB_ERR_NOENTRY,
1231 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1232 (unsigned)(krbtgt_number));
1233 return HDB_ERR_NOENTRY;
1237 * Windows seems to canonicalize the principal
1238 * in a TGS REP even if the client did not specify
1239 * the canonicalize flag.
1241 if (flags & (HDB_F_CANON|HDB_F_FOR_TGS_REQ)) {
1242 ret = krb5_copy_principal(context, principal, &alloc_principal);
1243 if (ret) {
1244 return ret;
1247 /* When requested to do so, ensure that the
1248 * both realm values in the principal are set
1249 * to the upper case, canonical realm */
1250 free(alloc_principal->name.name_string.val[1]);
1251 alloc_principal->name.name_string.val[1] = strdup(lpcfg_realm(lp_ctx));
1252 if (!alloc_principal->name.name_string.val[1]) {
1253 ret = ENOMEM;
1254 krb5_set_error_message(context, ret, "samba_kdc_fetch: strdup() failed!");
1255 return ret;
1257 principal = alloc_principal;
1260 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1261 principal, SAMBA_KDC_ENT_TYPE_KRBTGT,
1262 flags, realm_dn, msg, entry_ex);
1263 if (alloc_principal) {
1264 /* This is again copied in the message2entry call */
1265 krb5_free_principal(context, alloc_principal);
1267 if (ret != 0) {
1268 krb5_warnx(context, "samba_kdc_fetch: self krbtgt message2entry failed");
1270 return ret;
1272 } else {
1273 enum trust_direction direction = UNKNOWN;
1274 const char *realm = NULL;
1276 /* Either an inbound or outbound trust */
1278 if (strcasecmp(lpcfg_realm(lp_ctx), principal->realm) == 0) {
1279 /* look for inbound trust */
1280 direction = INBOUND;
1281 realm = principal->name.name_string.val[1];
1282 } else if (strcasecmp(lpcfg_realm(lp_ctx), principal->name.name_string.val[1]) == 0) {
1283 /* look for outbound trust */
1284 direction = OUTBOUND;
1285 realm = principal->realm;
1286 } else {
1287 krb5_warnx(context, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
1288 principal->realm, principal->name.name_string.val[1]);
1289 krb5_set_error_message(context, HDB_ERR_NOENTRY, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
1290 principal->realm, principal->name.name_string.val[1]);
1291 return HDB_ERR_NOENTRY;
1294 /* Trusted domains are under CN=system */
1296 ret = samba_kdc_lookup_trust(context, kdc_db_ctx->samdb,
1297 mem_ctx,
1298 realm, realm_dn, &msg);
1300 if (ret != 0) {
1301 krb5_warnx(context, "samba_kdc_fetch: could not find principal in DB");
1302 krb5_set_error_message(context, ret, "samba_kdc_fetch: could not find principal in DB");
1303 return ret;
1306 ret = samba_kdc_trust_message2entry(context, kdc_db_ctx, mem_ctx,
1307 principal, direction,
1308 realm_dn, flags, kvno, msg, entry_ex);
1309 if (ret != 0) {
1310 krb5_warnx(context, "samba_kdc_fetch: trust_message2entry failed");
1312 return ret;
1317 static krb5_error_code samba_kdc_lookup_server(krb5_context context,
1318 struct samba_kdc_db_context *kdc_db_ctx,
1319 TALLOC_CTX *mem_ctx,
1320 krb5_const_principal principal,
1321 const char **attrs,
1322 struct ldb_dn **realm_dn,
1323 struct ldb_message **msg)
1325 krb5_error_code ret;
1326 if (principal->name.name_string.len >= 2) {
1327 /* 'normal server' case */
1328 int ldb_ret;
1329 NTSTATUS nt_status;
1330 struct ldb_dn *user_dn;
1331 char *principal_string;
1333 ret = krb5_unparse_name_flags(context, principal,
1334 KRB5_PRINCIPAL_UNPARSE_NO_REALM,
1335 &principal_string);
1336 if (ret != 0) {
1337 return ret;
1340 /* At this point we may find the host is known to be
1341 * in a different realm, so we should generate a
1342 * referral instead */
1343 nt_status = crack_service_principal_name(kdc_db_ctx->samdb,
1344 mem_ctx, principal_string,
1345 &user_dn, realm_dn);
1346 free(principal_string);
1348 if (!NT_STATUS_IS_OK(nt_status)) {
1349 return HDB_ERR_NOENTRY;
1352 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb,
1353 mem_ctx,
1354 msg, user_dn, LDB_SCOPE_BASE,
1355 attrs,
1356 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
1357 "(objectClass=*)");
1358 if (ldb_ret != LDB_SUCCESS) {
1359 return HDB_ERR_NOENTRY;
1362 } else {
1363 int lret;
1364 char *short_princ;
1365 const char *realm;
1366 /* server as client principal case, but we must not lookup userPrincipalNames */
1367 *realm_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
1368 realm = krb5_principal_get_realm(context, principal);
1370 /* TODO: Check if it is our realm, otherwise give referral */
1372 ret = krb5_unparse_name_flags(context, principal, KRB5_PRINCIPAL_UNPARSE_NO_REALM, &short_princ);
1374 if (ret != 0) {
1375 krb5_set_error_message(context, ret, "samba_kdc_lookup_principal: could not parse principal");
1376 krb5_warnx(context, "samba_kdc_lookup_principal: could not parse principal");
1377 return ret;
1380 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx, msg,
1381 *realm_dn, LDB_SCOPE_SUBTREE,
1382 attrs,
1383 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
1384 "(&(objectClass=user)(samAccountName=%s))",
1385 ldb_binary_encode_string(mem_ctx, short_princ));
1386 if (lret == LDB_ERR_NO_SUCH_OBJECT) {
1387 DEBUG(3, ("Failed to find an entry for %s\n", short_princ));
1388 free(short_princ);
1389 return HDB_ERR_NOENTRY;
1391 if (lret != LDB_SUCCESS) {
1392 DEBUG(3, ("Failed single search for %s - %s\n",
1393 short_princ, ldb_errstring(kdc_db_ctx->samdb)));
1394 free(short_princ);
1395 return HDB_ERR_NOENTRY;
1397 free(short_princ);
1400 return 0;
1403 static krb5_error_code samba_kdc_fetch_server(krb5_context context,
1404 struct samba_kdc_db_context *kdc_db_ctx,
1405 TALLOC_CTX *mem_ctx,
1406 krb5_const_principal principal,
1407 unsigned flags,
1408 hdb_entry_ex *entry_ex)
1410 krb5_error_code ret;
1411 struct ldb_dn *realm_dn;
1412 struct ldb_message *msg;
1414 ret = samba_kdc_lookup_server(context, kdc_db_ctx, mem_ctx, principal,
1415 server_attrs, &realm_dn, &msg);
1416 if (ret != 0) {
1417 return ret;
1420 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1421 principal, SAMBA_KDC_ENT_TYPE_SERVER,
1422 flags,
1423 realm_dn, msg, entry_ex);
1424 if (ret != 0) {
1425 krb5_warnx(context, "samba_kdc_fetch: message2entry failed");
1428 return ret;
1431 krb5_error_code samba_kdc_fetch(krb5_context context,
1432 struct samba_kdc_db_context *kdc_db_ctx,
1433 krb5_const_principal principal,
1434 unsigned flags,
1435 krb5_kvno kvno,
1436 hdb_entry_ex *entry_ex)
1438 krb5_error_code ret = HDB_ERR_NOENTRY;
1439 TALLOC_CTX *mem_ctx;
1441 mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_fetch context");
1442 if (!mem_ctx) {
1443 ret = ENOMEM;
1444 krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
1445 return ret;
1448 if (flags & HDB_F_GET_CLIENT) {
1449 ret = samba_kdc_fetch_client(context, kdc_db_ctx, mem_ctx, principal, flags, entry_ex);
1450 if (ret != HDB_ERR_NOENTRY) goto done;
1452 if (flags & HDB_F_GET_SERVER) {
1453 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
1454 ret = samba_kdc_fetch_krbtgt(context, kdc_db_ctx, mem_ctx, principal, flags, kvno, entry_ex);
1455 if (ret != HDB_ERR_NOENTRY) goto done;
1457 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
1458 ret = samba_kdc_fetch_server(context, kdc_db_ctx, mem_ctx, principal, flags, entry_ex);
1459 if (ret != HDB_ERR_NOENTRY) goto done;
1461 if (flags & HDB_F_GET_KRBTGT) {
1462 ret = samba_kdc_fetch_krbtgt(context, kdc_db_ctx, mem_ctx, principal, flags, kvno, entry_ex);
1463 if (ret != HDB_ERR_NOENTRY) goto done;
1466 done:
1467 talloc_free(mem_ctx);
1468 return ret;
1471 struct samba_kdc_seq {
1472 unsigned int index;
1473 unsigned int count;
1474 struct ldb_message **msgs;
1475 struct ldb_dn *realm_dn;
1478 static krb5_error_code samba_kdc_seq(krb5_context context,
1479 struct samba_kdc_db_context *kdc_db_ctx,
1480 hdb_entry_ex *entry)
1482 krb5_error_code ret;
1483 struct samba_kdc_seq *priv = kdc_db_ctx->seq_ctx;
1484 TALLOC_CTX *mem_ctx;
1485 hdb_entry_ex entry_ex;
1486 memset(&entry_ex, '\0', sizeof(entry_ex));
1488 if (!priv) {
1489 return HDB_ERR_NOENTRY;
1492 mem_ctx = talloc_named(priv, 0, "samba_kdc_seq context");
1494 if (!mem_ctx) {
1495 ret = ENOMEM;
1496 krb5_set_error_message(context, ret, "samba_kdc_seq: talloc_named() failed!");
1497 return ret;
1500 if (priv->index < priv->count) {
1501 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1502 NULL, SAMBA_KDC_ENT_TYPE_ANY,
1503 HDB_F_ADMIN_DATA|HDB_F_GET_ANY,
1504 priv->realm_dn, priv->msgs[priv->index++], entry);
1505 } else {
1506 ret = HDB_ERR_NOENTRY;
1509 if (ret != 0) {
1510 TALLOC_FREE(priv);
1511 kdc_db_ctx->seq_ctx = NULL;
1512 } else {
1513 talloc_free(mem_ctx);
1516 return ret;
1519 krb5_error_code samba_kdc_firstkey(krb5_context context,
1520 struct samba_kdc_db_context *kdc_db_ctx,
1521 hdb_entry_ex *entry)
1523 struct ldb_context *ldb_ctx = kdc_db_ctx->samdb;
1524 struct samba_kdc_seq *priv = kdc_db_ctx->seq_ctx;
1525 char *realm;
1526 struct ldb_result *res = NULL;
1527 krb5_error_code ret;
1528 TALLOC_CTX *mem_ctx;
1529 int lret;
1531 if (priv) {
1532 TALLOC_FREE(priv);
1533 kdc_db_ctx->seq_ctx = NULL;
1536 priv = (struct samba_kdc_seq *) talloc(kdc_db_ctx, struct samba_kdc_seq);
1537 if (!priv) {
1538 ret = ENOMEM;
1539 krb5_set_error_message(context, ret, "talloc: out of memory");
1540 return ret;
1543 priv->index = 0;
1544 priv->msgs = NULL;
1545 priv->realm_dn = ldb_get_default_basedn(ldb_ctx);
1546 priv->count = 0;
1548 mem_ctx = talloc_named(priv, 0, "samba_kdc_firstkey context");
1550 if (!mem_ctx) {
1551 ret = ENOMEM;
1552 krb5_set_error_message(context, ret, "samba_kdc_firstkey: talloc_named() failed!");
1553 return ret;
1556 ret = krb5_get_default_realm(context, &realm);
1557 if (ret != 0) {
1558 TALLOC_FREE(priv);
1559 return ret;
1561 krb5_free_default_realm(context, realm);
1563 lret = dsdb_search(ldb_ctx, priv, &res,
1564 priv->realm_dn, LDB_SCOPE_SUBTREE, user_attrs,
1565 DSDB_SEARCH_NO_GLOBAL_CATALOG,
1566 "(objectClass=user)");
1568 if (lret != LDB_SUCCESS) {
1569 TALLOC_FREE(priv);
1570 return HDB_ERR_NOENTRY;
1573 priv->count = res->count;
1574 priv->msgs = talloc_steal(priv, res->msgs);
1575 talloc_free(res);
1577 kdc_db_ctx->seq_ctx = priv;
1579 ret = samba_kdc_seq(context, kdc_db_ctx, entry);
1581 if (ret != 0) {
1582 TALLOC_FREE(priv);
1583 kdc_db_ctx->seq_ctx = NULL;
1584 } else {
1585 talloc_free(mem_ctx);
1587 return ret;
1590 krb5_error_code samba_kdc_nextkey(krb5_context context,
1591 struct samba_kdc_db_context *kdc_db_ctx,
1592 hdb_entry_ex *entry)
1594 return samba_kdc_seq(context, kdc_db_ctx, entry);
1597 /* Check if a given entry may delegate or do s4u2self to this target principal
1599 * This is currently a very nasty hack - allowing only delegation to itself.
1601 krb5_error_code
1602 samba_kdc_check_s4u2self(krb5_context context,
1603 struct samba_kdc_db_context *kdc_db_ctx,
1604 hdb_entry_ex *entry,
1605 krb5_const_principal target_principal)
1607 krb5_error_code ret;
1608 krb5_principal enterprise_prinicpal = NULL;
1609 struct ldb_dn *realm_dn;
1610 struct ldb_message *msg;
1611 struct dom_sid *orig_sid;
1612 struct dom_sid *target_sid;
1613 struct samba_kdc_entry *p = talloc_get_type(entry->ctx, struct samba_kdc_entry);
1614 const char *delegation_check_attrs[] = {
1615 "objectSid", NULL
1618 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_s4u2self");
1620 if (!mem_ctx) {
1621 ret = ENOMEM;
1622 krb5_set_error_message(context, ret, "samba_kdc_check_s4u2self: talloc_named() failed!");
1623 return ret;
1626 if (target_principal->name.name_type == KRB5_NT_ENTERPRISE_PRINCIPAL) {
1627 /* Need to reparse the enterprise principal to find the real target */
1628 if (target_principal->name.name_string.len != 1) {
1629 ret = KRB5_PARSE_MALFORMED;
1630 krb5_set_error_message(context, ret, "samba_kdc_check_s4u2self: request for delegation to enterprise principal with wrong (%d) number of components",
1631 target_principal->name.name_string.len);
1632 talloc_free(mem_ctx);
1633 return ret;
1635 ret = krb5_parse_name(context, target_principal->name.name_string.val[0],
1636 &enterprise_prinicpal);
1637 if (ret) {
1638 talloc_free(mem_ctx);
1639 return ret;
1641 target_principal = enterprise_prinicpal;
1644 ret = samba_kdc_lookup_server(context, kdc_db_ctx, mem_ctx, target_principal,
1645 delegation_check_attrs, &realm_dn, &msg);
1647 krb5_free_principal(context, enterprise_prinicpal);
1649 if (ret != 0) {
1650 talloc_free(mem_ctx);
1651 return ret;
1654 orig_sid = samdb_result_dom_sid(mem_ctx, p->msg, "objectSid");
1655 target_sid = samdb_result_dom_sid(mem_ctx, msg, "objectSid");
1657 /* Allow delegation to the same principal, even if by a different
1658 * name. The easy and safe way to prove this is by SID
1659 * comparison */
1660 if (!(orig_sid && target_sid && dom_sid_equal(orig_sid, target_sid))) {
1661 talloc_free(mem_ctx);
1662 return KRB5KDC_ERR_BADOPTION;
1665 talloc_free(mem_ctx);
1666 return ret;
1669 /* Certificates printed by a the Certificate Authority might have a
1670 * slightly different form of the user principal name to that in the
1671 * database. Allow a mismatch where they both refer to the same
1672 * SID */
1674 krb5_error_code
1675 samba_kdc_check_pkinit_ms_upn_match(krb5_context context,
1676 struct samba_kdc_db_context *kdc_db_ctx,
1677 hdb_entry_ex *entry,
1678 krb5_const_principal certificate_principal)
1680 krb5_error_code ret;
1681 struct ldb_dn *realm_dn;
1682 struct ldb_message *msg;
1683 struct dom_sid *orig_sid;
1684 struct dom_sid *target_sid;
1685 struct samba_kdc_entry *p = talloc_get_type(entry->ctx, struct samba_kdc_entry);
1686 const char *ms_upn_check_attrs[] = {
1687 "objectSid", NULL
1690 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_pkinit_ms_upn_match");
1692 if (!mem_ctx) {
1693 ret = ENOMEM;
1694 krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
1695 return ret;
1698 ret = samba_kdc_lookup_client(context, kdc_db_ctx,
1699 mem_ctx, certificate_principal,
1700 ms_upn_check_attrs, &realm_dn, &msg);
1702 if (ret != 0) {
1703 talloc_free(mem_ctx);
1704 return ret;
1707 orig_sid = samdb_result_dom_sid(mem_ctx, p->msg, "objectSid");
1708 target_sid = samdb_result_dom_sid(mem_ctx, msg, "objectSid");
1710 /* Consider these to be the same principal, even if by a different
1711 * name. The easy and safe way to prove this is by SID
1712 * comparison */
1713 if (!(orig_sid && target_sid && dom_sid_equal(orig_sid, target_sid))) {
1714 talloc_free(mem_ctx);
1715 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH;
1718 talloc_free(mem_ctx);
1719 return ret;
1723 * Check if a given entry may delegate to this target principal
1724 * with S4U2Proxy.
1726 krb5_error_code
1727 samba_kdc_check_s4u2proxy(krb5_context context,
1728 struct samba_kdc_db_context *kdc_db_ctx,
1729 hdb_entry_ex *entry,
1730 krb5_const_principal target_principal)
1732 krb5_error_code ret;
1733 char *tmp = NULL;
1734 const char *client_dn = NULL;
1735 const char *target_principal_name = NULL;
1736 struct ldb_message_element *el;
1737 struct ldb_val val;
1738 unsigned int i;
1739 bool found = false;
1740 struct samba_kdc_entry *p = talloc_get_type(entry->ctx, struct samba_kdc_entry);
1742 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_s4u2proxy");
1744 if (!mem_ctx) {
1745 ret = ENOMEM;
1746 krb5_set_error_message(context, ret,
1747 "samba_kdc_check_s4u2proxy:"
1748 " talloc_named() failed!");
1749 return ret;
1752 client_dn = ldb_dn_get_linearized(p->msg->dn);
1753 if (!client_dn) {
1754 if (errno == 0) {
1755 errno = ENOMEM;
1757 ret = errno;
1758 krb5_set_error_message(context, ret,
1759 "samba_kdc_check_s4u2proxy:"
1760 " ldb_dn_get_linearized() failed!");
1761 return ret;
1765 * The main heimdal code already checked that the target_principal
1766 * belongs to the same realm as the client.
1768 * So we just need the principal without the realm,
1769 * as that is what is configured in the "msDS-AllowedToDelegateTo"
1770 * attribute.
1772 ret = krb5_unparse_name_flags(context, target_principal,
1773 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &tmp);
1774 if (ret) {
1775 talloc_free(mem_ctx);
1776 krb5_set_error_message(context, ret,
1777 "samba_kdc_check_s4u2proxy:"
1778 " krb5_unparse_name() failed!");
1779 return ret;
1781 DEBUG(10,("samba_kdc_check_s4u2proxy: client[%s] for target[%s]\n",
1782 client_dn, tmp));
1784 target_principal_name = talloc_strdup(mem_ctx, tmp);
1785 SAFE_FREE(tmp);
1786 if (target_principal_name == NULL) {
1787 ret = ENOMEM;
1788 krb5_set_error_message(context, ret,
1789 "samba_kdc_check_s4u2proxy:"
1790 " talloc_strdup() failed!");
1791 return ret;
1794 el = ldb_msg_find_element(p->msg, "msDS-AllowedToDelegateTo");
1795 if (el == NULL) {
1796 goto bad_option;
1799 val = data_blob_string_const(target_principal_name);
1801 for (i=0; i<el->num_values; i++) {
1802 struct ldb_val *val1 = &val;
1803 struct ldb_val *val2 = &el->values[i];
1804 int cmp;
1806 if (val1->length != val2->length) {
1807 continue;
1810 cmp = strncasecmp((const char *)val1->data,
1811 (const char *)val2->data,
1812 val1->length);
1813 if (cmp != 0) {
1814 continue;
1817 found = true;
1818 break;
1821 if (!found) {
1822 goto bad_option;
1825 DEBUG(10,("samba_kdc_check_s4u2proxy: client[%s] allowed target[%s]\n",
1826 client_dn, tmp));
1827 talloc_free(mem_ctx);
1828 return 0;
1830 bad_option:
1831 krb5_set_error_message(context, ret,
1832 "samba_kdc_check_s4u2proxy: client[%s] "
1833 "not allowed for delegation to target[%s]",
1834 client_dn,
1835 target_principal_name);
1836 talloc_free(mem_ctx);
1837 return KRB5KDC_ERR_BADOPTION;
1840 NTSTATUS samba_kdc_setup_db_ctx(TALLOC_CTX *mem_ctx, struct samba_kdc_base_context *base_ctx,
1841 struct samba_kdc_db_context **kdc_db_ctx_out)
1843 int ldb_ret;
1844 struct ldb_message *msg;
1845 struct auth_session_info *session_info;
1846 struct samba_kdc_db_context *kdc_db_ctx;
1847 /* The idea here is very simple. Using Kerberos to
1848 * authenticate the KDC to the LDAP server is higly likely to
1849 * be circular.
1851 * In future we may set this up to use EXERNAL and SSL
1852 * certificates, for now it will almost certainly be NTLMSSP_SET_USERNAME
1855 kdc_db_ctx = talloc_zero(mem_ctx, struct samba_kdc_db_context);
1856 if (kdc_db_ctx == NULL) {
1857 return NT_STATUS_NO_MEMORY;
1859 kdc_db_ctx->ev_ctx = base_ctx->ev_ctx;
1860 kdc_db_ctx->lp_ctx = base_ctx->lp_ctx;
1862 /* get default kdc policy */
1863 lpcfg_default_kdc_policy(base_ctx->lp_ctx,
1864 &kdc_db_ctx->policy.svc_tkt_lifetime,
1865 &kdc_db_ctx->policy.usr_tkt_lifetime,
1866 &kdc_db_ctx->policy.renewal_lifetime);
1868 session_info = system_session(kdc_db_ctx->lp_ctx);
1869 if (session_info == NULL) {
1870 return NT_STATUS_INTERNAL_ERROR;
1873 /* Setup the link to LDB */
1874 kdc_db_ctx->samdb = samdb_connect(kdc_db_ctx, base_ctx->ev_ctx,
1875 base_ctx->lp_ctx, session_info, 0);
1876 if (kdc_db_ctx->samdb == NULL) {
1877 DEBUG(1, ("hdb_samba4_create: Cannot open samdb for KDC backend!"));
1878 talloc_free(kdc_db_ctx);
1879 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1882 /* Find out our own krbtgt kvno */
1883 ldb_ret = samdb_rodc(kdc_db_ctx->samdb, &kdc_db_ctx->rodc);
1884 if (ldb_ret != LDB_SUCCESS) {
1885 DEBUG(1, ("hdb_samba4_create: Cannot determine if we are an RODC in KDC backend: %s\n",
1886 ldb_errstring(kdc_db_ctx->samdb)));
1887 talloc_free(kdc_db_ctx);
1888 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1890 if (kdc_db_ctx->rodc) {
1891 int my_krbtgt_number;
1892 const char *secondary_keytab[] = { "msDS-SecondaryKrbTgtNumber", NULL };
1893 struct ldb_dn *account_dn;
1894 struct ldb_dn *server_dn = samdb_server_dn(kdc_db_ctx->samdb, kdc_db_ctx);
1895 if (!server_dn) {
1896 DEBUG(1, ("hdb_samba4_create: Cannot determine server DN in KDC backend: %s\n",
1897 ldb_errstring(kdc_db_ctx->samdb)));
1898 talloc_free(kdc_db_ctx);
1899 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1902 ldb_ret = samdb_reference_dn(kdc_db_ctx->samdb, kdc_db_ctx, server_dn,
1903 "serverReference", &account_dn);
1904 if (ldb_ret != LDB_SUCCESS) {
1905 DEBUG(1, ("hdb_samba4_create: Cannot determine server account in KDC backend: %s\n",
1906 ldb_errstring(kdc_db_ctx->samdb)));
1907 talloc_free(kdc_db_ctx);
1908 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1911 ldb_ret = samdb_reference_dn(kdc_db_ctx->samdb, kdc_db_ctx, account_dn,
1912 "msDS-KrbTgtLink", &kdc_db_ctx->krbtgt_dn);
1913 talloc_free(account_dn);
1914 if (ldb_ret != LDB_SUCCESS) {
1915 DEBUG(1, ("hdb_samba4_create: Cannot determine RODC krbtgt account in KDC backend: %s\n",
1916 ldb_errstring(kdc_db_ctx->samdb)));
1917 talloc_free(kdc_db_ctx);
1918 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1921 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb, kdc_db_ctx,
1922 &msg, kdc_db_ctx->krbtgt_dn, LDB_SCOPE_BASE,
1923 secondary_keytab,
1924 DSDB_SEARCH_NO_GLOBAL_CATALOG,
1925 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=*))");
1926 if (ldb_ret != LDB_SUCCESS) {
1927 DEBUG(1, ("hdb_samba4_create: Cannot read krbtgt account %s in KDC backend to get msDS-SecondaryKrbTgtNumber: %s: %s\n",
1928 ldb_dn_get_linearized(kdc_db_ctx->krbtgt_dn),
1929 ldb_errstring(kdc_db_ctx->samdb),
1930 ldb_strerror(ldb_ret)));
1931 talloc_free(kdc_db_ctx);
1932 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1934 my_krbtgt_number = ldb_msg_find_attr_as_int(msg, "msDS-SecondaryKrbTgtNumber", -1);
1935 if (my_krbtgt_number == -1) {
1936 DEBUG(1, ("hdb_samba4_create: Cannot read msDS-SecondaryKrbTgtNumber from krbtgt account %s in KDC backend: got %d\n",
1937 ldb_dn_get_linearized(kdc_db_ctx->krbtgt_dn),
1938 my_krbtgt_number));
1939 talloc_free(kdc_db_ctx);
1940 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1942 kdc_db_ctx->my_krbtgt_number = my_krbtgt_number;
1944 } else {
1945 kdc_db_ctx->my_krbtgt_number = 0;
1946 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb, kdc_db_ctx,
1947 &msg,
1948 ldb_get_default_basedn(kdc_db_ctx->samdb),
1949 LDB_SCOPE_SUBTREE,
1950 krbtgt_attrs,
1951 DSDB_SEARCH_NO_GLOBAL_CATALOG,
1952 "(&(objectClass=user)(samAccountName=krbtgt))");
1954 if (ldb_ret != LDB_SUCCESS) {
1955 DEBUG(1, ("samba_kdc_fetch: could not find own KRBTGT in DB: %s\n", ldb_errstring(kdc_db_ctx->samdb)));
1956 talloc_free(kdc_db_ctx);
1957 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1959 kdc_db_ctx->krbtgt_dn = talloc_steal(kdc_db_ctx, msg->dn);
1960 kdc_db_ctx->my_krbtgt_number = 0;
1961 talloc_free(msg);
1963 *kdc_db_ctx_out = kdc_db_ctx;
1964 return NT_STATUS_OK;