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/>.
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"
36 #include "kdc/samba_kdc.h"
37 #include "kdc/db-glue.h"
38 #include "librpc/gen_ndr/ndr_irpc_c.h"
39 #include "lib/messaging/irpc.h"
44 #define SAMBA_KVNO_GET_KRBTGT(kvno) \
45 ((uint16_t)(((uint32_t)kvno) >> 16))
47 #define SAMBA_KVNO_GET_VALUE(kvno) \
48 ((uint16_t)(((uint32_t)kvno) & 0xFFFF))
50 #define SAMBA_KVNO_AND_KRBTGT(kvno, krbtgt) \
51 ((krb5_kvno)((((uint32_t)kvno) & 0xFFFF) | \
52 ((((uint32_t)krbtgt) << 16) & 0xFFFF0000)))
54 enum samba_kdc_ent_type
55 { SAMBA_KDC_ENT_TYPE_CLIENT, SAMBA_KDC_ENT_TYPE_SERVER,
56 SAMBA_KDC_ENT_TYPE_KRBTGT, SAMBA_KDC_ENT_TYPE_TRUST, SAMBA_KDC_ENT_TYPE_ANY };
58 enum trust_direction {
60 INBOUND = LSA_TRUST_DIRECTION_INBOUND,
61 OUTBOUND = LSA_TRUST_DIRECTION_OUTBOUND
64 static const char *trust_attrs[] = {
71 "msDS-TrustForestTrustInfo",
75 "msDS-SupportedEncryptionTypes",
80 send a message to the drepl server telling it to initiate a
81 REPL_SECRET getncchanges extended op to fetch the users secrets
83 static void auth_sam_trigger_repl_secret(TALLOC_CTX *mem_ctx,
84 struct imessaging_context *msg_ctx,
85 struct tevent_context *event_ctx,
86 struct ldb_dn *user_dn)
88 struct dcerpc_binding_handle *irpc_handle;
89 struct drepl_trigger_repl_secret r;
90 struct tevent_req *req;
93 tmp_ctx = talloc_new(mem_ctx);
94 if (tmp_ctx == NULL) {
98 irpc_handle = irpc_binding_handle_by_name(tmp_ctx, msg_ctx,
101 if (irpc_handle == NULL) {
102 DEBUG(1,(__location__ ": Unable to get binding handle for dreplsrv\n"));
103 TALLOC_FREE(tmp_ctx);
107 r.in.user_dn = ldb_dn_get_linearized(user_dn);
110 * This seem to rely on the current IRPC implementation,
111 * which delivers the message in the _send function.
113 * TODO: we need a ONE_WAY IRPC handle and register
114 * a callback and wait for it to be triggered!
116 req = dcerpc_drepl_trigger_repl_secret_r_send(tmp_ctx,
121 /* we aren't interested in a reply */
123 TALLOC_FREE(tmp_ctx);
126 static time_t ldb_msg_find_krb5time_ldap_time(struct ldb_message *msg, const char *attr, time_t default_val)
132 gentime = ldb_msg_find_attr_as_string(msg, attr, NULL);
136 tmp = strptime(gentime, "%Y%m%d%H%M%SZ", &tm);
144 static struct SDBFlags uf2SDBFlags(krb5_context context, uint32_t userAccountControl, enum samba_kdc_ent_type ent_type)
146 struct SDBFlags flags = int2SDBFlags(0);
148 /* we don't allow kadmin deletes */
151 /* mark the principal as invalid to start with */
156 /* All accounts are servers, but this may be disabled again in the caller */
159 /* Account types - clear the invalid bit if it turns out to be valid */
160 if (userAccountControl & UF_NORMAL_ACCOUNT) {
161 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
167 if (userAccountControl & UF_INTERDOMAIN_TRUST_ACCOUNT) {
168 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
173 if (userAccountControl & UF_WORKSTATION_TRUST_ACCOUNT) {
174 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
179 if (userAccountControl & UF_SERVER_TRUST_ACCOUNT) {
180 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
186 /* Not permitted to act as a client if disabled */
187 if (userAccountControl & UF_ACCOUNTDISABLE) {
190 if (userAccountControl & UF_LOCKOUT) {
191 flags.locked_out = 1;
194 if (userAccountControl & UF_PASSWORD_NOTREQD) {
199 UF_PASSWORD_CANT_CHANGE and UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED are irrelevent
201 if (userAccountControl & UF_TEMP_DUPLICATE_ACCOUNT) {
205 /* UF_DONT_EXPIRE_PASSWD and UF_USE_DES_KEY_ONLY handled in samba_kdc_message2entry() */
208 if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
212 if (userAccountControl & UF_SMARTCARD_REQUIRED) {
213 flags.require_hwauth = 1;
215 if (userAccountControl & UF_TRUSTED_FOR_DELEGATION) {
216 flags.ok_as_delegate = 1;
218 if (userAccountControl & UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION) {
220 * this is confusing...
222 * UF_TRUSTED_FOR_DELEGATION
227 * UF_TRUSTED_TO_AUTHENTICATE_FOR_DELEGATION
228 * => trusted_for_delegation
230 flags.trusted_for_delegation = 1;
232 if (!(userAccountControl & UF_NOT_DELEGATED)) {
233 flags.forwardable = 1;
237 if (userAccountControl & UF_DONT_REQUIRE_PREAUTH) {
238 flags.require_preauth = 0;
240 flags.require_preauth = 1;
246 static int samba_kdc_entry_destructor(struct samba_kdc_entry *p)
248 if (p->entry_ex != NULL) {
249 struct sdb_entry_ex *entry_ex = p->entry_ex;
250 free_sdb_entry(&entry_ex->entry);
257 * Sort keys in descending order of strength.
259 * Explanaton from Greg Hudson:
261 * To encrypt tickets only the first returned key is used by the MIT KDC. The
262 * other keys just communicate support for session key enctypes, and aren't
263 * really used. The encryption key for the ticket enc part doesn't have
264 * to be of a type requested by the client. The session key enctype is chosen
265 * based on the client preference order, limited by the set of enctypes present
266 * in the server keys (unless the string attribute is set on the server
267 * principal overriding that set).
269 static int samba_kdc_sort_encryption_keys(struct sdb_entry_ex *entry_ex)
271 unsigned int i, j, idx = 0;
272 static const krb5_enctype etype_list[] = {
273 ENCTYPE_AES256_CTS_HMAC_SHA1_96,
274 ENCTYPE_AES128_CTS_HMAC_SHA1_96,
275 ENCTYPE_DES3_CBC_SHA1,
276 ENCTYPE_ARCFOUR_HMAC,
282 size_t etype_len = ARRAY_SIZE(etype_list);
283 size_t keys_size = entry_ex->entry.keys.len;
284 struct sdb_key *keys = entry_ex->entry.keys.val;
285 struct sdb_key *sorted_keys;
287 sorted_keys = calloc(keys_size, sizeof(struct sdb_key));
288 if (sorted_keys == NULL) {
292 for (i = 0; i < etype_len; i++) {
293 for (j = 0; j < keys_size; j++) {
294 const struct sdb_key skey = keys[j];
296 if (idx == keys_size) {
300 if (KRB5_KEY_TYPE(&skey.key) == etype_list[i]) {
301 sorted_keys[idx] = skey;
307 /* Paranoia: Something went wrong during data copy */
308 if (idx != keys_size) {
313 free(entry_ex->entry.keys.val);
314 entry_ex->entry.keys.val = sorted_keys;
319 static krb5_error_code samba_kdc_message2entry_keys(krb5_context context,
320 struct samba_kdc_db_context *kdc_db_ctx,
322 struct ldb_message *msg,
325 uint32_t userAccountControl,
326 enum samba_kdc_ent_type ent_type,
327 struct sdb_entry_ex *entry_ex)
329 struct sdb_entry *entry = &entry_ex->entry;
330 krb5_error_code ret = 0;
331 enum ndr_err_code ndr_err;
332 struct samr_Password *hash;
333 const struct ldb_val *sc_val;
334 struct supplementalCredentialsBlob scb;
335 struct supplementalCredentialsPackage *scpk = NULL;
336 bool newer_keys = false;
337 struct package_PrimaryKerberosBlob _pkb;
338 struct package_PrimaryKerberosCtr3 *pkb3 = NULL;
339 struct package_PrimaryKerberosCtr4 *pkb4 = NULL;
340 bool is_krbtgt = false;
341 int krbtgt_number = 0;
342 uint32_t current_kvno;
343 uint32_t returned_kvno = 0;
345 uint16_t allocated_keys = 0;
346 uint32_t supported_enctypes
347 = ldb_msg_find_attr_as_uint(msg,
348 "msDS-SupportedEncryptionTypes",
351 if (rid == DOMAIN_RID_KRBTGT || is_rodc) {
352 /* KDCs (and KDCs on RODCs) use AES */
353 supported_enctypes |= ENC_HMAC_SHA1_96_AES128 | ENC_HMAC_SHA1_96_AES256;
355 } else if (userAccountControl & (UF_PARTIAL_SECRETS_ACCOUNT|UF_SERVER_TRUST_ACCOUNT)) {
356 /* DCs and RODCs comptuer accounts use AES */
357 supported_enctypes |= ENC_HMAC_SHA1_96_AES128 | ENC_HMAC_SHA1_96_AES256;
358 } else if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT ||
359 (ent_type == SAMBA_KDC_ENT_TYPE_ANY)) {
360 /* for AS-REQ the client chooses the enc types it
361 * supports, and this will vary between computers a
364 * likewise for 'any' return as much as is supported,
365 * to export into a keytab */
366 supported_enctypes = ENC_ALL_TYPES;
369 /* If UF_USE_DES_KEY_ONLY has been set, then don't allow use of the newer enc types */
370 if (userAccountControl & UF_USE_DES_KEY_ONLY) {
371 supported_enctypes = 0;
373 /* Otherwise, add in the default enc types */
374 supported_enctypes |= ENC_RC4_HMAC_MD5;
377 /* Is this the krbtgt or a RODC krbtgt */
379 krbtgt_number = ldb_msg_find_attr_as_int(msg, "msDS-SecondaryKrbTgtNumber", -1);
381 if (krbtgt_number == -1) {
384 if (krbtgt_number == 0) {
389 entry_ex->entry.keys.val = NULL;
390 entry_ex->entry.keys.len = 0;
391 entry_ex->entry.kvno = 0;
393 if ((ent_type == SAMBA_KDC_ENT_TYPE_CLIENT)
394 && (userAccountControl & UF_SMARTCARD_REQUIRED)) {
395 uint8_t secretbuffer[32];
398 * Fake keys until we have a better way to reject
399 * non-pkinit requests.
401 * We just need to indicate which encryption types are
404 generate_secret_buffer(secretbuffer, sizeof(secretbuffer));
407 entry_ex->entry.keys.len = 0;
408 entry_ex->entry.keys.val = calloc(allocated_keys, sizeof(struct sdb_key));
409 if (entry_ex->entry.keys.val == NULL) {
410 ZERO_STRUCT(secretbuffer);
415 if (supported_enctypes & ENC_HMAC_SHA1_96_AES256) {
416 struct sdb_key key = {};
418 ret = smb_krb5_keyblock_init_contents(context,
419 ENCTYPE_AES256_CTS_HMAC_SHA1_96,
423 ZERO_STRUCT(secretbuffer);
427 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
428 entry_ex->entry.keys.len++;
431 if (supported_enctypes & ENC_HMAC_SHA1_96_AES128) {
432 struct sdb_key key = {};
434 ret = smb_krb5_keyblock_init_contents(context,
435 ENCTYPE_AES128_CTS_HMAC_SHA1_96,
439 ZERO_STRUCT(secretbuffer);
443 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
444 entry_ex->entry.keys.len++;
447 if (supported_enctypes & ENC_RC4_HMAC_MD5) {
448 struct sdb_key key = {};
450 ret = smb_krb5_keyblock_init_contents(context,
451 ENCTYPE_ARCFOUR_HMAC,
455 ZERO_STRUCT(secretbuffer);
459 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
460 entry_ex->entry.keys.len++;
467 current_kvno = ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0);
470 * Even for the main krbtgt account
471 * we have to strictly split the kvno into
472 * two 16-bit parts and the upper 16-bit
473 * need to be all zero, even if
474 * the msDS-KeyVersionNumber has a value
477 * See https://bugzilla.samba.org/show_bug.cgi?id=14951
479 current_kvno = SAMBA_KVNO_GET_VALUE(current_kvno);
482 /* Get keys from the db */
484 hash = samdb_result_hash(mem_ctx, msg, "unicodePwd");
485 sc_val = ldb_msg_find_ldb_val(msg, "supplementalCredentials");
487 /* unicodePwd for enctype 0x17 (23) if present */
492 /* supplementalCredentials if present */
494 ndr_err = ndr_pull_struct_blob_all(sc_val, mem_ctx, &scb,
495 (ndr_pull_flags_fn_t)ndr_pull_supplementalCredentialsBlob);
496 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
497 dump_data(0, sc_val->data, sc_val->length);
502 if (scb.sub.signature != SUPPLEMENTAL_CREDENTIALS_SIGNATURE) {
503 if (scb.sub.num_packages != 0) {
504 NDR_PRINT_DEBUG(supplementalCredentialsBlob, &scb);
510 for (i=0; i < scb.sub.num_packages; i++) {
511 if (strcmp("Primary:Kerberos-Newer-Keys", scb.sub.packages[i].name) == 0) {
512 scpk = &scb.sub.packages[i];
513 if (!scpk->data || !scpk->data[0]) {
519 } else if (strcmp("Primary:Kerberos", scb.sub.packages[i].name) == 0) {
520 scpk = &scb.sub.packages[i];
521 if (!scpk->data || !scpk->data[0]) {
525 * we don't break here in hope to find
526 * a Kerberos-Newer-Keys package
532 * Primary:Kerberos-Newer-Keys or Primary:Kerberos element
533 * of supplementalCredentials
538 blob = strhex_to_data_blob(mem_ctx, scpk->data);
544 /* we cannot use ndr_pull_struct_blob_all() here, as w2k and w2k3 add padding bytes */
545 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &_pkb,
546 (ndr_pull_flags_fn_t)ndr_pull_package_PrimaryKerberosBlob);
547 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
549 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
550 krb5_warnx(context, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
554 if (newer_keys && _pkb.version != 4) {
556 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
557 krb5_warnx(context, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
561 if (!newer_keys && _pkb.version != 3) {
563 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
564 krb5_warnx(context, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
568 if (_pkb.version == 4) {
569 pkb4 = &_pkb.ctr.ctr4;
570 allocated_keys += pkb4->num_keys;
571 } else if (_pkb.version == 3) {
572 pkb3 = &_pkb.ctr.ctr3;
573 allocated_keys += pkb3->num_keys;
577 if (allocated_keys == 0) {
578 if (kdc_db_ctx->rodc) {
579 /* We are on an RODC, but don't have keys for this account. Signal this to the caller */
580 auth_sam_trigger_repl_secret(kdc_db_ctx, kdc_db_ctx->msg_ctx,
581 kdc_db_ctx->ev_ctx, msg->dn);
582 return SDB_ERR_NOT_FOUND_HERE;
585 /* oh, no password. Apparently (comment in
586 * hdb-ldap.c) this violates the ASN.1, but this
587 * allows an entry with no keys (yet). */
591 /* allocate space to decode into */
592 entry_ex->entry.keys.len = 0;
593 entry_ex->entry.keys.val = calloc(allocated_keys, sizeof(struct sdb_key));
594 if (entry_ex->entry.keys.val == NULL) {
599 if (hash && (supported_enctypes & ENC_RC4_HMAC_MD5)) {
600 struct sdb_key key = {};
602 ret = smb_krb5_keyblock_init_contents(context,
603 ENCTYPE_ARCFOUR_HMAC,
611 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
612 entry_ex->entry.keys.len++;
616 for (i=0; i < pkb4->num_keys; i++) {
617 struct sdb_key key = {};
619 if (!pkb4->keys[i].value) continue;
621 if (!(kerberos_enctype_to_bitmap(pkb4->keys[i].keytype) & supported_enctypes)) {
625 if (pkb4->salt.string) {
628 salt = data_blob_string_const(pkb4->salt.string);
630 key.salt = calloc(1, sizeof(*key.salt));
631 if (key.salt == NULL) {
636 key.salt->type = KRB5_PW_SALT;
638 ret = smb_krb5_copy_data_contents(&key.salt->salt,
648 /* TODO: maybe pass the iteration_count somehow... */
650 ret = smb_krb5_keyblock_init_contents(context,
651 pkb4->keys[i].keytype,
652 pkb4->keys[i].value->data,
653 pkb4->keys[i].value->length,
657 smb_krb5_free_data_contents(context, &key.salt->salt);
661 if (ret == KRB5_PROG_ETYPE_NOSUPP) {
662 DEBUG(2,("Unsupported keytype ignored - type %u\n",
663 pkb4->keys[i].keytype));
670 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
671 entry_ex->entry.keys.len++;
674 for (i=0; i < pkb3->num_keys; i++) {
675 struct sdb_key key = {};
677 if (!pkb3->keys[i].value) continue;
679 if (!(kerberos_enctype_to_bitmap(pkb3->keys[i].keytype) & supported_enctypes)) {
683 if (pkb3->salt.string) {
686 salt = data_blob_string_const(pkb3->salt.string);
688 key.salt = calloc(1, sizeof(*key.salt));
689 if (key.salt == NULL) {
694 key.salt->type = KRB5_PW_SALT;
696 ret = smb_krb5_copy_data_contents(&key.salt->salt,
706 ret = smb_krb5_keyblock_init_contents(context,
707 pkb3->keys[i].keytype,
708 pkb3->keys[i].value->data,
709 pkb3->keys[i].value->length,
713 smb_krb5_free_data_contents(context, &key.salt->salt);
717 if (ret == KRB5_PROG_ETYPE_NOSUPP) {
718 DEBUG(2,("Unsupported keytype ignored - type %u\n",
719 pkb3->keys[i].keytype));
726 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
727 entry_ex->entry.keys.len++;
731 returned_kvno = current_kvno;
734 * Even for the main krbtgt account
735 * we have to strictly split the kvno into
736 * two 16-bit parts and the upper 16-bit
737 * need to be all zero, even if
738 * the msDS-KeyVersionNumber has a value
741 * See https://bugzilla.samba.org/show_bug.cgi?id=14951
743 returned_kvno = SAMBA_KVNO_AND_KRBTGT(returned_kvno, krbtgt_number);
745 entry->kvno = returned_kvno;
749 entry_ex->entry.keys.len = 0;
750 } else if (entry_ex->entry.keys.len > 0 &&
751 entry_ex->entry.keys.val != NULL) {
752 ret = samba_kdc_sort_encryption_keys(entry_ex);
754 entry_ex->entry.keys.len = 0;
758 if (entry_ex->entry.keys.len == 0 && entry_ex->entry.keys.val) {
759 free(entry_ex->entry.keys.val);
760 entry_ex->entry.keys.val = NULL;
765 static int principal_comp_strcmp_int(krb5_context context,
766 krb5_const_principal principal,
767 unsigned int component,
774 #if defined(HAVE_KRB5_PRINCIPAL_GET_COMP_STRING)
775 p = krb5_principal_get_comp_string(context, principal, component);
782 if (component >= krb5_princ_size(context, principal)) {
786 d = krb5_princ_component(context, principal, component);
795 return strncasecmp(p, string, len);
797 return strncmp(p, string, len);
801 static int principal_comp_strcasecmp(krb5_context context,
802 krb5_const_principal principal,
803 unsigned int component,
806 return principal_comp_strcmp_int(context, principal,
807 component, string, true);
810 static int principal_comp_strcmp(krb5_context context,
811 krb5_const_principal principal,
812 unsigned int component,
815 return principal_comp_strcmp_int(context, principal,
816 component, string, false);
819 static bool is_kadmin_changepw(krb5_context context,
820 krb5_const_principal principal)
822 return krb5_princ_size(context, principal) == 2 &&
823 (principal_comp_strcmp(context, principal, 0, "kadmin") == 0) &&
824 (principal_comp_strcmp(context, principal, 1, "changepw") == 0);
827 static krb5_error_code samba_kdc_get_entry_principal(
828 krb5_context context,
829 struct samba_kdc_db_context *kdc_db_ctx,
830 const char *samAccountName,
831 enum samba_kdc_ent_type ent_type,
833 krb5_const_principal in_princ,
834 krb5_principal *out_princ)
836 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
837 krb5_error_code ret = 0;
840 * If we are set to canonicalize, we get back the fixed UPPER
841 * case realm, and the real username (ie matching LDAP
844 * Otherwise, if we are set to enterprise, we
845 * get back the whole principal as-sent
847 * Finally, if we are not set to canonicalize, we get back the
848 * fixed UPPER case realm, but the as-sent username
851 if (ent_type == SAMBA_KDC_ENT_TYPE_KRBTGT) {
852 if (flags & (SDB_F_CANON|SDB_F_FORCE_CANON)) {
854 * When requested to do so, ensure that the
855 * both realm values in the principal are set
856 * to the upper case, canonical realm
858 ret = smb_krb5_make_principal(context, out_princ,
859 lpcfg_realm(lp_ctx), "krbtgt",
860 lpcfg_realm(lp_ctx), NULL);
864 smb_krb5_principal_set_type(context, *out_princ, KRB5_NT_SRV_INST);
866 ret = krb5_copy_principal(context, in_princ, out_princ);
871 * this appears to be required regardless of
872 * the canonicalize flag from the client
874 ret = smb_krb5_principal_set_realm(context, *out_princ, lpcfg_realm(lp_ctx));
880 } else if (ent_type == SAMBA_KDC_ENT_TYPE_ANY && in_princ == NULL) {
881 ret = smb_krb5_make_principal(context, out_princ, lpcfg_realm(lp_ctx), samAccountName, NULL);
885 } else if ((flags & SDB_F_FORCE_CANON) ||
886 ((flags & SDB_F_CANON) && (flags & SDB_F_FOR_AS_REQ))) {
888 * SDB_F_CANON maps from the canonicalize flag in the
889 * packet, and has a different meaning between AS-REQ
890 * and TGS-REQ. We only change the principal in the AS-REQ case
892 * The SDB_F_FORCE_CANON if for new MIT KDC code that wants
893 * the canonical name in all lookups, and takes care to
894 * canonicalize only when appropriate.
896 ret = smb_krb5_make_principal(context, out_princ, lpcfg_realm(lp_ctx), samAccountName, NULL);
901 ret = krb5_copy_principal(context, in_princ, out_princ);
906 /* While we have copied the client principal, tests
907 * show that Win2k3 returns the 'corrected' realm, not
908 * the client-specified realm. This code attempts to
909 * replace the client principal's realm with the one
910 * we determine from our records */
912 /* this has to be with malloc() */
913 ret = smb_krb5_principal_set_realm(context, *out_princ, lpcfg_realm(lp_ctx));
923 * Construct an hdb_entry from a directory entry.
925 static krb5_error_code samba_kdc_message2entry(krb5_context context,
926 struct samba_kdc_db_context *kdc_db_ctx,
928 krb5_const_principal principal,
929 enum samba_kdc_ent_type ent_type,
931 struct ldb_dn *realm_dn,
932 struct ldb_message *msg,
933 struct sdb_entry_ex *entry_ex)
935 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
936 uint32_t userAccountControl;
937 uint32_t msDS_User_Account_Control_Computed;
938 krb5_error_code ret = 0;
939 krb5_boolean is_computer = FALSE;
941 struct samba_kdc_entry *p;
946 bool is_rodc = false;
947 struct ldb_message_element *objectclasses;
948 struct ldb_val computer_val;
949 const char *samAccountName = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
950 computer_val.data = discard_const_p(uint8_t,"computer");
951 computer_val.length = strlen((const char *)computer_val.data);
953 if (ldb_msg_find_element(msg, "msDS-SecondaryKrbTgtNumber")) {
957 if (!samAccountName) {
959 krb5_set_error_message(context, ret, "samba_kdc_message2entry: no samAccountName present");
963 objectclasses = ldb_msg_find_element(msg, "objectClass");
965 if (objectclasses && ldb_msg_find_val(objectclasses, &computer_val)) {
969 ZERO_STRUCTP(entry_ex);
971 p = talloc_zero(mem_ctx, struct samba_kdc_entry);
977 p->is_rodc = is_rodc;
978 p->kdc_db_ctx = kdc_db_ctx;
979 p->realm_dn = talloc_reference(p, realm_dn);
985 talloc_set_destructor(p, samba_kdc_entry_destructor);
989 userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
991 msDS_User_Account_Control_Computed
992 = ldb_msg_find_attr_as_uint(msg,
993 "msDS-User-Account-Control-Computed",
997 * This brings in the lockout flag, block the account if not
998 * found. We need the weird UF_ACCOUNTDISABLE check because
999 * we do not want to fail open if the value is not returned,
1000 * but 0 is a valid value (all OK)
1002 if (msDS_User_Account_Control_Computed == UF_ACCOUNTDISABLE) {
1004 krb5_set_error_message(context, ret, "samba_kdc_message2entry: "
1005 "no msDS-User-Account-Control-Computed present");
1008 userAccountControl |= msDS_User_Account_Control_Computed;
1011 if (ent_type == SAMBA_KDC_ENT_TYPE_KRBTGT) {
1012 p->is_krbtgt = true;
1015 /* First try and figure out the flags based on the userAccountControl */
1016 entry_ex->entry.flags = uf2SDBFlags(context, userAccountControl, ent_type);
1018 /* Windows 2008 seems to enforce this (very sensible) rule by
1019 * default - don't allow offline attacks on a user's password
1020 * by asking for a ticket to them as a service (encrypted with
1021 * their probably patheticly insecure password) */
1023 if (entry_ex->entry.flags.server
1024 && lpcfg_parm_bool(lp_ctx, NULL, "kdc", "require spn for service", true)) {
1025 if (!is_computer && !ldb_msg_find_attr_as_string(msg, "servicePrincipalName", NULL)) {
1026 entry_ex->entry.flags.server = 0;
1031 * We restrict a 3-part SPN ending in my domain/realm to full
1032 * domain controllers.
1034 * This avoids any cases where (eg) a demoted DC still has
1035 * these more restricted SPNs.
1037 if (krb5_princ_size(context, principal) > 2) {
1039 = smb_krb5_principal_get_comp_string(mem_ctx,
1044 lpcfg_is_my_domain_or_realm(lp_ctx,
1046 bool is_dc = userAccountControl &
1047 (UF_SERVER_TRUST_ACCOUNT | UF_PARTIAL_SECRETS_ACCOUNT);
1048 if (is_our_realm && !is_dc) {
1049 entry_ex->entry.flags.server = 0;
1053 * To give the correct type of error to the client, we must
1054 * not just return the entry without .server set, we must
1055 * pretend the principal does not exist. Otherwise we may
1056 * return ERR_POLICY instead of
1057 * KRB5KDC_ERR_S_PRINCIPAL_UNKNOWN
1059 if (ent_type == SAMBA_KDC_ENT_TYPE_SERVER && entry_ex->entry.flags.server == 0) {
1060 ret = SDB_ERR_NOENTRY;
1061 krb5_set_error_message(context, ret, "samba_kdc_message2entry: no servicePrincipalName present for this server, refusing with no-such-entry");
1064 if (flags & SDB_F_ADMIN_DATA) {
1065 /* These (created_by, modified_by) parts of the entry are not relevant for Samba4's use
1066 * of the Heimdal KDC. They are stored in a the traditional
1067 * DB for audit purposes, and still form part of the structure
1070 /* use 'whenCreated' */
1071 entry_ex->entry.created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
1072 /* use 'kadmin' for now (needed by mit_samba) */
1074 ret = smb_krb5_make_principal(context,
1075 &entry_ex->entry.created_by.principal,
1076 lpcfg_realm(lp_ctx), "kadmin", NULL);
1078 krb5_clear_error_message(context);
1082 entry_ex->entry.modified_by = (struct sdb_event *) malloc(sizeof(struct sdb_event));
1083 if (entry_ex->entry.modified_by == NULL) {
1085 krb5_set_error_message(context, ret, "malloc: out of memory");
1089 /* use 'whenChanged' */
1090 entry_ex->entry.modified_by->time = ldb_msg_find_krb5time_ldap_time(msg, "whenChanged", 0);
1091 /* use 'kadmin' for now (needed by mit_samba) */
1092 ret = smb_krb5_make_principal(context,
1093 &entry_ex->entry.modified_by->principal,
1094 lpcfg_realm(lp_ctx), "kadmin", NULL);
1096 krb5_clear_error_message(context);
1102 /* The lack of password controls etc applies to krbtgt by
1103 * virtue of being that particular RID */
1104 status = dom_sid_split_rid(NULL, samdb_result_dom_sid(mem_ctx, msg, "objectSid"), NULL, &rid);
1106 if (!NT_STATUS_IS_OK(status)) {
1111 if (rid == DOMAIN_RID_KRBTGT) {
1114 entry_ex->entry.valid_end = NULL;
1115 entry_ex->entry.pw_end = NULL;
1117 entry_ex->entry.flags.invalid = 0;
1118 entry_ex->entry.flags.server = 1;
1120 realm = smb_krb5_principal_get_realm(
1121 mem_ctx, context, principal);
1122 if (realm == NULL) {
1127 /* Don't mark all requests for the krbtgt/realm as
1128 * 'change password', as otherwise we could get into
1129 * trouble, and not enforce the password expirty.
1130 * Instead, only do it when request is for the kpasswd service */
1131 if (ent_type == SAMBA_KDC_ENT_TYPE_SERVER &&
1132 is_kadmin_changepw(context, principal) &&
1133 lpcfg_is_my_domain_or_realm(lp_ctx, realm)) {
1134 entry_ex->entry.flags.change_pw = 1;
1139 entry_ex->entry.flags.client = 0;
1140 entry_ex->entry.flags.forwardable = 1;
1141 entry_ex->entry.flags.ok_as_delegate = 1;
1142 } else if (is_rodc) {
1143 /* The RODC krbtgt account is like the main krbtgt,
1144 * but it does not have a changepw or kadmin
1147 entry_ex->entry.valid_end = NULL;
1148 entry_ex->entry.pw_end = NULL;
1150 /* Also don't allow the RODC krbtgt to be a client (it should not be needed) */
1151 entry_ex->entry.flags.client = 0;
1152 entry_ex->entry.flags.invalid = 0;
1153 entry_ex->entry.flags.server = 1;
1155 entry_ex->entry.flags.client = 0;
1156 entry_ex->entry.flags.forwardable = 1;
1157 entry_ex->entry.flags.ok_as_delegate = 0;
1158 } else if (entry_ex->entry.flags.server && ent_type == SAMBA_KDC_ENT_TYPE_SERVER) {
1159 /* The account/password expiry only applies when the account is used as a
1160 * client (ie password login), not when used as a server */
1162 /* Make very well sure we don't use this for a client,
1163 * it could bypass the password restrictions */
1164 entry_ex->entry.flags.client = 0;
1166 entry_ex->entry.valid_end = NULL;
1167 entry_ex->entry.pw_end = NULL;
1170 NTTIME must_change_time
1171 = samdb_result_nttime(msg,
1172 "msDS-UserPasswordExpiryTimeComputed",
1174 if (must_change_time == 0x7FFFFFFFFFFFFFFFULL) {
1175 entry_ex->entry.pw_end = NULL;
1177 entry_ex->entry.pw_end = malloc(sizeof(*entry_ex->entry.pw_end));
1178 if (entry_ex->entry.pw_end == NULL) {
1182 *entry_ex->entry.pw_end = nt_time_to_unix(must_change_time);
1185 acct_expiry = samdb_result_account_expires(msg);
1186 if (acct_expiry == 0x7FFFFFFFFFFFFFFFULL) {
1187 entry_ex->entry.valid_end = NULL;
1189 entry_ex->entry.valid_end = malloc(sizeof(*entry_ex->entry.valid_end));
1190 if (entry_ex->entry.valid_end == NULL) {
1194 *entry_ex->entry.valid_end = nt_time_to_unix(acct_expiry);
1198 ret = samba_kdc_get_entry_principal(context,
1204 &entry_ex->entry.principal);
1206 krb5_clear_error_message(context);
1210 entry_ex->entry.valid_start = NULL;
1212 entry_ex->entry.max_life = malloc(sizeof(*entry_ex->entry.max_life));
1213 if (entry_ex->entry.max_life == NULL) {
1218 if (ent_type == SAMBA_KDC_ENT_TYPE_SERVER) {
1219 *entry_ex->entry.max_life = kdc_db_ctx->policy.svc_tkt_lifetime;
1220 } else if (ent_type == SAMBA_KDC_ENT_TYPE_KRBTGT || ent_type == SAMBA_KDC_ENT_TYPE_CLIENT) {
1221 *entry_ex->entry.max_life = kdc_db_ctx->policy.usr_tkt_lifetime;
1223 *entry_ex->entry.max_life = MIN(kdc_db_ctx->policy.svc_tkt_lifetime,
1224 kdc_db_ctx->policy.usr_tkt_lifetime);
1227 entry_ex->entry.max_renew = malloc(sizeof(*entry_ex->entry.max_life));
1228 if (entry_ex->entry.max_renew == NULL) {
1233 *entry_ex->entry.max_renew = kdc_db_ctx->policy.renewal_lifetime;
1235 /* Get keys from the db */
1236 ret = samba_kdc_message2entry_keys(context, kdc_db_ctx, p, msg,
1237 rid, is_rodc, userAccountControl,
1238 ent_type, entry_ex);
1240 /* Could be bogus data in the entry, or out of memory */
1244 p->msg = talloc_steal(p, msg);
1248 /* This doesn't free ent itself, that is for the eventual caller to do */
1249 sdb_free_entry(entry_ex);
1250 ZERO_STRUCTP(entry_ex);
1252 talloc_steal(kdc_db_ctx, entry_ex->ctx);
1259 * Construct an hdb_entry from a directory entry.
1260 * The kvno is what the remote client asked for
1262 static krb5_error_code samba_kdc_trust_message2entry(krb5_context context,
1263 struct samba_kdc_db_context *kdc_db_ctx,
1264 TALLOC_CTX *mem_ctx, krb5_const_principal principal,
1265 enum trust_direction direction,
1266 struct ldb_dn *realm_dn,
1269 struct ldb_message *msg,
1270 struct sdb_entry_ex *entry_ex)
1272 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
1273 const char *our_realm = lpcfg_realm(lp_ctx);
1274 char *partner_realm = NULL;
1275 const char *realm = NULL;
1276 const char *krbtgt_realm = NULL;
1277 DATA_BLOB password_utf16 = data_blob_null;
1278 DATA_BLOB password_utf8 = data_blob_null;
1279 struct samr_Password _password_hash;
1280 const struct samr_Password *password_hash = NULL;
1281 const struct ldb_val *password_val;
1282 struct trustAuthInOutBlob password_blob;
1283 struct samba_kdc_entry *p;
1284 bool use_previous = false;
1285 uint32_t current_kvno;
1286 uint32_t previous_kvno;
1287 uint32_t num_keys = 0;
1288 enum ndr_err_code ndr_err;
1291 struct AuthenticationInformationArray *auth_array;
1294 uint32_t *auth_kvno;
1295 bool preferr_current = false;
1296 uint32_t supported_enctypes = ENC_RC4_HMAC_MD5;
1297 struct lsa_TrustDomainInfoInfoEx *tdo = NULL;
1300 if (dsdb_functional_level(kdc_db_ctx->samdb) >= DS_DOMAIN_FUNCTION_2008) {
1301 supported_enctypes = ldb_msg_find_attr_as_uint(msg,
1302 "msDS-SupportedEncryptionTypes",
1303 supported_enctypes);
1306 status = dsdb_trust_parse_tdo_info(mem_ctx, msg, &tdo);
1307 if (!NT_STATUS_IS_OK(status)) {
1308 krb5_clear_error_message(context);
1313 if (!(tdo->trust_direction & direction)) {
1314 krb5_clear_error_message(context);
1315 ret = SDB_ERR_NOENTRY;
1319 if (tdo->trust_type != LSA_TRUST_TYPE_UPLEVEL) {
1321 * Only UPLEVEL domains support kerberos here,
1322 * as we don't support LSA_TRUST_TYPE_MIT.
1324 krb5_clear_error_message(context);
1325 ret = SDB_ERR_NOENTRY;
1329 if (tdo->trust_attributes & LSA_TRUST_ATTRIBUTE_CROSS_ORGANIZATION) {
1331 * We don't support selective authentication yet.
1333 krb5_clear_error_message(context);
1334 ret = SDB_ERR_NOENTRY;
1338 if (tdo->domain_name.string == NULL) {
1339 krb5_clear_error_message(context);
1340 ret = SDB_ERR_NOENTRY;
1343 partner_realm = strupper_talloc(mem_ctx, tdo->domain_name.string);
1344 if (partner_realm == NULL) {
1345 krb5_clear_error_message(context);
1350 if (direction == INBOUND) {
1352 krbtgt_realm = partner_realm;
1354 password_val = ldb_msg_find_ldb_val(msg, "trustAuthIncoming");
1355 } else { /* OUTBOUND */
1356 realm = partner_realm;
1357 krbtgt_realm = our_realm;
1359 password_val = ldb_msg_find_ldb_val(msg, "trustAuthOutgoing");
1362 if (password_val == NULL) {
1363 krb5_clear_error_message(context);
1364 ret = SDB_ERR_NOENTRY;
1368 ndr_err = ndr_pull_struct_blob(password_val, mem_ctx, &password_blob,
1369 (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
1370 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
1371 krb5_clear_error_message(context);
1376 p = talloc_zero(mem_ctx, struct samba_kdc_entry);
1383 p->kdc_db_ctx = kdc_db_ctx;
1384 p->realm_dn = realm_dn;
1386 talloc_set_destructor(p, samba_kdc_entry_destructor);
1388 /* make sure we do not have bogus data in there */
1389 memset(&entry_ex->entry, 0, sizeof(struct sdb_entry));
1393 /* use 'whenCreated' */
1394 entry_ex->entry.created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
1395 /* use 'kadmin' for now (needed by mit_samba) */
1396 ret = smb_krb5_make_principal(context,
1397 &entry_ex->entry.created_by.principal,
1398 realm, "kadmin", NULL);
1400 krb5_clear_error_message(context);
1405 * We always need to generate the canonicalized principal
1406 * with the values of our database.
1408 ret = smb_krb5_make_principal(context, &entry_ex->entry.principal, realm,
1409 "krbtgt", krbtgt_realm, NULL);
1411 krb5_clear_error_message(context);
1414 smb_krb5_principal_set_type(context, entry_ex->entry.principal,
1417 entry_ex->entry.valid_start = NULL;
1419 /* we need to work out if we are going to use the current or
1420 * the previous password hash.
1421 * We base this on the kvno the client passes in. If the kvno
1422 * passed in is equal to the current kvno in our database then
1423 * we use the current structure. If it is the current kvno-1,
1424 * then we use the previous substrucure.
1428 * Windows preferrs the previous key for one hour.
1430 tv = timeval_current();
1431 if (tv.tv_sec > 3600) {
1434 an_hour_ago = timeval_to_nttime(&tv);
1436 /* first work out the current kvno */
1438 for (i=0; i < password_blob.count; i++) {
1439 struct AuthenticationInformation *a =
1440 &password_blob.current.array[i];
1442 if (a->LastUpdateTime <= an_hour_ago) {
1443 preferr_current = true;
1446 if (a->AuthType == TRUST_AUTH_TYPE_VERSION) {
1447 current_kvno = a->AuthInfo.version.version;
1450 if (current_kvno == 0) {
1451 previous_kvno = 255;
1453 previous_kvno = current_kvno - 1;
1455 for (i=0; i < password_blob.count; i++) {
1456 struct AuthenticationInformation *a =
1457 &password_blob.previous.array[i];
1459 if (a->AuthType == TRUST_AUTH_TYPE_VERSION) {
1460 previous_kvno = a->AuthInfo.version.version;
1464 /* work out whether we will use the previous or current
1466 if (password_blob.previous.count == 0) {
1467 /* there is no previous password */
1468 use_previous = false;
1469 } else if (!(flags & SDB_F_KVNO_SPECIFIED)) {
1471 * If not specified we use the lowest kvno
1472 * for the first hour after an update.
1474 if (preferr_current) {
1475 use_previous = false;
1476 } else if (previous_kvno < current_kvno) {
1477 use_previous = true;
1479 use_previous = false;
1481 } else if (kvno == current_kvno) {
1485 use_previous = false;
1486 } else if (kvno == previous_kvno) {
1490 use_previous = true;
1493 * Fallback to the current one for anything else
1495 use_previous = false;
1499 auth_array = &password_blob.previous;
1500 auth_kvno = &previous_kvno;
1502 auth_array = &password_blob.current;
1503 auth_kvno = ¤t_kvno;
1506 /* use the kvno the client specified, if available */
1507 if (flags & SDB_F_KVNO_SPECIFIED) {
1508 entry_ex->entry.kvno = kvno;
1510 entry_ex->entry.kvno = *auth_kvno;
1513 for (i=0; i < auth_array->count; i++) {
1514 if (auth_array->array[i].AuthType == TRUST_AUTH_TYPE_CLEAR) {
1517 password_utf16 = data_blob_const(auth_array->array[i].AuthInfo.clear.password,
1518 auth_array->array[i].AuthInfo.clear.size);
1519 if (password_utf16.length == 0) {
1523 if (supported_enctypes & ENC_RC4_HMAC_MD5) {
1524 mdfour(_password_hash.hash, password_utf16.data, password_utf16.length);
1525 if (password_hash == NULL) {
1528 password_hash = &_password_hash;
1531 if (!(supported_enctypes & (ENC_HMAC_SHA1_96_AES128|ENC_HMAC_SHA1_96_AES256))) {
1535 ok = convert_string_talloc(mem_ctx,
1536 CH_UTF16MUNGED, CH_UTF8,
1537 password_utf16.data,
1538 password_utf16.length,
1539 (void *)&password_utf8.data,
1540 &password_utf8.length);
1542 krb5_clear_error_message(context);
1547 if (supported_enctypes & ENC_HMAC_SHA1_96_AES128) {
1550 if (supported_enctypes & ENC_HMAC_SHA1_96_AES256) {
1554 } else if (auth_array->array[i].AuthType == TRUST_AUTH_TYPE_NT4OWF) {
1555 if (supported_enctypes & ENC_RC4_HMAC_MD5) {
1556 password_hash = &auth_array->array[i].AuthInfo.nt4owf.password;
1562 /* Must have found a cleartext or MD4 password */
1563 if (num_keys == 0) {
1564 DEBUG(1,(__location__ ": no usable key found\n"));
1565 krb5_clear_error_message(context);
1566 ret = SDB_ERR_NOENTRY;
1570 entry_ex->entry.keys.val = calloc(num_keys, sizeof(struct sdb_key));
1571 if (entry_ex->entry.keys.val == NULL) {
1572 krb5_clear_error_message(context);
1577 if (password_utf8.length != 0) {
1578 struct sdb_key key = {};
1579 krb5_const_principal salt_principal = entry_ex->entry.principal;
1581 krb5_data cleartext_data;
1583 cleartext_data.data = discard_const_p(char, password_utf8.data);
1584 cleartext_data.length = password_utf8.length;
1586 ret = smb_krb5_get_pw_salt(context,
1593 if (supported_enctypes & ENC_HMAC_SHA1_96_AES256) {
1594 ret = smb_krb5_create_key_from_string(context,
1598 ENCTYPE_AES256_CTS_HMAC_SHA1_96,
1601 smb_krb5_free_data_contents(context, &salt);
1605 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
1606 entry_ex->entry.keys.len++;
1609 if (supported_enctypes & ENC_HMAC_SHA1_96_AES128) {
1610 ret = smb_krb5_create_key_from_string(context,
1614 ENCTYPE_AES128_CTS_HMAC_SHA1_96,
1617 smb_krb5_free_data_contents(context, &salt);
1621 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
1622 entry_ex->entry.keys.len++;
1625 smb_krb5_free_data_contents(context, &salt);
1628 if (password_hash != NULL) {
1629 struct sdb_key key = {};
1631 ret = smb_krb5_keyblock_init_contents(context,
1632 ENCTYPE_ARCFOUR_HMAC,
1633 password_hash->hash,
1634 sizeof(password_hash->hash),
1640 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
1641 entry_ex->entry.keys.len++;
1644 entry_ex->entry.flags = int2SDBFlags(0);
1645 entry_ex->entry.flags.immutable = 1;
1646 entry_ex->entry.flags.invalid = 0;
1647 entry_ex->entry.flags.server = 1;
1648 entry_ex->entry.flags.require_preauth = 1;
1650 entry_ex->entry.pw_end = NULL;
1652 entry_ex->entry.max_life = NULL;
1654 entry_ex->entry.max_renew = NULL;
1656 /* Match Windows behavior and allow forwardable flag in cross-realm. */
1657 entry_ex->entry.flags.forwardable = 1;
1659 ret = samba_kdc_sort_encryption_keys(entry_ex);
1661 krb5_clear_error_message(context);
1666 p->msg = talloc_steal(p, msg);
1669 TALLOC_FREE(partner_realm);
1672 /* This doesn't free ent itself, that is for the eventual caller to do */
1673 sdb_free_entry(entry_ex);
1675 talloc_steal(kdc_db_ctx, entry_ex->ctx);
1682 static krb5_error_code samba_kdc_lookup_trust(krb5_context context, struct ldb_context *ldb_ctx,
1683 TALLOC_CTX *mem_ctx,
1685 struct ldb_dn *realm_dn,
1686 struct ldb_message **pmsg)
1689 const char * const *attrs = trust_attrs;
1691 status = dsdb_trust_search_tdo(ldb_ctx, realm, realm,
1692 attrs, mem_ctx, pmsg);
1693 if (NT_STATUS_IS_OK(status)) {
1695 } else if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1696 return SDB_ERR_NOENTRY;
1697 } else if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1699 krb5_set_error_message(context, ret, "get_sam_result_trust: out of memory");
1703 krb5_set_error_message(context, ret, "get_sam_result_trust: %s", nt_errstr(status));
1708 static krb5_error_code samba_kdc_lookup_client(krb5_context context,
1709 struct samba_kdc_db_context *kdc_db_ctx,
1710 TALLOC_CTX *mem_ctx,
1711 krb5_const_principal principal,
1713 struct ldb_dn **realm_dn,
1714 struct ldb_message **msg)
1717 char *principal_string = NULL;
1719 if (smb_krb5_principal_get_type(context, principal) == KRB5_NT_ENTERPRISE_PRINCIPAL) {
1720 principal_string = smb_krb5_principal_get_comp_string(mem_ctx, context,
1722 if (principal_string == NULL) {
1726 char *principal_string_m = NULL;
1727 krb5_error_code ret;
1729 ret = krb5_unparse_name(context, principal, &principal_string_m);
1734 principal_string = talloc_strdup(mem_ctx, principal_string_m);
1735 SAFE_FREE(principal_string_m);
1736 if (principal_string == NULL) {
1741 nt_status = sam_get_results_principal(kdc_db_ctx->samdb,
1742 mem_ctx, principal_string, attrs,
1744 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
1745 krb5_principal fallback_principal = NULL;
1746 unsigned int num_comp;
1747 char *fallback_realm = NULL;
1748 char *fallback_account = NULL;
1749 krb5_error_code ret;
1751 ret = krb5_parse_name(context, principal_string,
1752 &fallback_principal);
1753 TALLOC_FREE(principal_string);
1758 num_comp = krb5_princ_size(context, fallback_principal);
1759 fallback_realm = smb_krb5_principal_get_realm(
1760 mem_ctx, context, fallback_principal);
1761 if (fallback_realm == NULL) {
1762 krb5_free_principal(context, fallback_principal);
1766 if (num_comp == 1) {
1769 fallback_account = smb_krb5_principal_get_comp_string(mem_ctx,
1770 context, fallback_principal, 0);
1771 if (fallback_account == NULL) {
1772 krb5_free_principal(context, fallback_principal);
1773 TALLOC_FREE(fallback_realm);
1777 len = strlen(fallback_account);
1778 if (len >= 2 && fallback_account[len - 1] == '$') {
1779 TALLOC_FREE(fallback_account);
1782 krb5_free_principal(context, fallback_principal);
1783 fallback_principal = NULL;
1785 if (fallback_account != NULL) {
1788 with_dollar = talloc_asprintf(mem_ctx, "%s$",
1790 if (with_dollar == NULL) {
1791 TALLOC_FREE(fallback_realm);
1794 TALLOC_FREE(fallback_account);
1796 ret = smb_krb5_make_principal(context,
1797 &fallback_principal,
1800 TALLOC_FREE(with_dollar);
1802 TALLOC_FREE(fallback_realm);
1806 TALLOC_FREE(fallback_realm);
1808 if (fallback_principal != NULL) {
1809 char *fallback_string = NULL;
1811 ret = krb5_unparse_name(context,
1815 krb5_free_principal(context, fallback_principal);
1819 nt_status = sam_get_results_principal(kdc_db_ctx->samdb,
1824 SAFE_FREE(fallback_string);
1826 krb5_free_principal(context, fallback_principal);
1827 fallback_principal = NULL;
1829 TALLOC_FREE(principal_string);
1831 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
1832 return SDB_ERR_NOENTRY;
1833 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
1835 } else if (!NT_STATUS_IS_OK(nt_status)) {
1842 static krb5_error_code samba_kdc_fetch_client(krb5_context context,
1843 struct samba_kdc_db_context *kdc_db_ctx,
1844 TALLOC_CTX *mem_ctx,
1845 krb5_const_principal principal,
1847 struct sdb_entry_ex *entry_ex) {
1848 struct ldb_dn *realm_dn;
1849 krb5_error_code ret;
1850 struct ldb_message *msg = NULL;
1852 ret = samba_kdc_lookup_client(context, kdc_db_ctx,
1853 mem_ctx, principal, user_attrs,
1859 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1860 principal, SAMBA_KDC_ENT_TYPE_CLIENT,
1862 realm_dn, msg, entry_ex);
1866 static krb5_error_code samba_kdc_fetch_krbtgt(krb5_context context,
1867 struct samba_kdc_db_context *kdc_db_ctx,
1868 TALLOC_CTX *mem_ctx,
1869 krb5_const_principal principal,
1872 struct sdb_entry_ex *entry_ex)
1874 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
1875 krb5_error_code ret;
1876 struct ldb_message *msg = NULL;
1877 struct ldb_dn *realm_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
1878 char *realm_from_princ;
1879 char *realm_princ_comp = smb_krb5_principal_get_comp_string(mem_ctx, context, principal, 1);
1881 realm_from_princ = smb_krb5_principal_get_realm(
1882 mem_ctx, context, principal);
1883 if (realm_from_princ == NULL) {
1885 return SDB_ERR_NOENTRY;
1888 if (krb5_princ_size(context, principal) != 2
1889 || (principal_comp_strcmp(context, principal, 0, KRB5_TGS_NAME) != 0)) {
1891 return SDB_ERR_NOENTRY;
1894 /* krbtgt case. Either us or a trusted realm */
1896 if (lpcfg_is_my_domain_or_realm(lp_ctx, realm_from_princ)
1897 && lpcfg_is_my_domain_or_realm(lp_ctx, realm_princ_comp)) {
1898 /* us, or someone quite like us */
1899 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
1900 * is in our db, then direct the caller at our primary
1904 unsigned int krbtgt_number;
1905 /* w2k8r2 sometimes gives us a kvno of 255 for inter-domain
1906 trust tickets. We don't yet know what this means, but we do
1907 seem to need to treat it as unspecified */
1908 if (flags & SDB_F_KVNO_SPECIFIED) {
1909 krbtgt_number = SAMBA_KVNO_GET_KRBTGT(kvno);
1910 if (kdc_db_ctx->rodc) {
1911 if (krbtgt_number != kdc_db_ctx->my_krbtgt_number) {
1912 return SDB_ERR_NOT_FOUND_HERE;
1916 krbtgt_number = kdc_db_ctx->my_krbtgt_number;
1919 if (krbtgt_number == kdc_db_ctx->my_krbtgt_number) {
1920 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx,
1921 &msg, kdc_db_ctx->krbtgt_dn, LDB_SCOPE_BASE,
1922 krbtgt_attrs, DSDB_SEARCH_NO_GLOBAL_CATALOG,
1923 "(objectClass=user)");
1925 /* We need to look up an RODC krbtgt (perhaps
1926 * ours, if we are an RODC, perhaps another
1927 * RODC if we are a read-write DC */
1928 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx,
1929 &msg, realm_dn, LDB_SCOPE_SUBTREE,
1931 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
1932 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=%u))", (unsigned)(krbtgt_number));
1935 if (lret == LDB_ERR_NO_SUCH_OBJECT) {
1936 krb5_warnx(context, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1937 (unsigned)(krbtgt_number));
1938 krb5_set_error_message(context, SDB_ERR_NOENTRY,
1939 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1940 (unsigned)(krbtgt_number));
1941 return SDB_ERR_NOENTRY;
1942 } else if (lret != LDB_SUCCESS) {
1943 krb5_warnx(context, "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1944 (unsigned)(krbtgt_number));
1945 krb5_set_error_message(context, SDB_ERR_NOENTRY,
1946 "samba_kdc_fetch: could not find KRBTGT number %u in DB!",
1947 (unsigned)(krbtgt_number));
1948 return SDB_ERR_NOENTRY;
1951 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1952 principal, SAMBA_KDC_ENT_TYPE_KRBTGT,
1953 flags, realm_dn, msg, entry_ex);
1955 krb5_warnx(context, "samba_kdc_fetch: self krbtgt message2entry failed");
1960 enum trust_direction direction = UNKNOWN;
1961 const char *realm = NULL;
1963 /* Either an inbound or outbound trust */
1965 if (strcasecmp(lpcfg_realm(lp_ctx), realm_from_princ) == 0) {
1966 /* look for inbound trust */
1967 direction = INBOUND;
1968 realm = realm_princ_comp;
1969 } else if (principal_comp_strcasecmp(context, principal, 1, lpcfg_realm(lp_ctx)) == 0) {
1970 /* look for outbound trust */
1971 direction = OUTBOUND;
1972 realm = realm_from_princ;
1974 krb5_warnx(context, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
1977 krb5_set_error_message(context, SDB_ERR_NOENTRY, "samba_kdc_fetch: not our realm for trusts ('%s', '%s')",
1980 return SDB_ERR_NOENTRY;
1983 /* Trusted domains are under CN=system */
1985 ret = samba_kdc_lookup_trust(context, kdc_db_ctx->samdb,
1987 realm, realm_dn, &msg);
1990 krb5_warnx(context, "samba_kdc_fetch: could not find principal in DB");
1991 krb5_set_error_message(context, ret, "samba_kdc_fetch: could not find principal in DB");
1995 ret = samba_kdc_trust_message2entry(context, kdc_db_ctx, mem_ctx,
1996 principal, direction,
1997 realm_dn, flags, kvno, msg, entry_ex);
1999 krb5_warnx(context, "samba_kdc_fetch: trust_message2entry failed for %s",
2000 ldb_dn_get_linearized(msg->dn));
2001 krb5_set_error_message(context, ret, "samba_kdc_fetch: "
2002 "trust_message2entry failed for %s",
2003 ldb_dn_get_linearized(msg->dn));
2010 static krb5_error_code samba_kdc_lookup_server(krb5_context context,
2011 struct samba_kdc_db_context *kdc_db_ctx,
2012 TALLOC_CTX *mem_ctx,
2013 krb5_const_principal principal,
2016 struct ldb_dn **realm_dn,
2017 struct ldb_message **msg)
2019 krb5_error_code ret;
2020 if ((smb_krb5_principal_get_type(context, principal) != KRB5_NT_ENTERPRISE_PRINCIPAL)
2021 && krb5_princ_size(context, principal) >= 2) {
2022 /* 'normal server' case */
2025 struct ldb_dn *user_dn;
2026 char *principal_string;
2028 ret = krb5_unparse_name_flags(context, principal,
2029 KRB5_PRINCIPAL_UNPARSE_NO_REALM,
2035 /* At this point we may find the host is known to be
2036 * in a different realm, so we should generate a
2037 * referral instead */
2038 nt_status = crack_service_principal_name(kdc_db_ctx->samdb,
2039 mem_ctx, principal_string,
2040 &user_dn, realm_dn);
2041 free(principal_string);
2043 if (!NT_STATUS_IS_OK(nt_status)) {
2044 return SDB_ERR_NOENTRY;
2047 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb,
2049 msg, user_dn, LDB_SCOPE_BASE,
2051 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
2053 if (ldb_ret != LDB_SUCCESS) {
2054 return SDB_ERR_NOENTRY;
2057 } else if (!(flags & SDB_F_FOR_AS_REQ)
2058 && smb_krb5_principal_get_type(context, principal) == KRB5_NT_ENTERPRISE_PRINCIPAL) {
2060 * The behaviour of accepting an
2061 * KRB5_NT_ENTERPRISE_PRINCIPAL server principal
2062 * containing a UPN only applies to TGS-REQ packets,
2063 * not AS-REQ packets.
2065 return samba_kdc_lookup_client(context, kdc_db_ctx,
2066 mem_ctx, principal, attrs,
2071 * - the AS-REQ, where we only accept
2072 * samAccountName based lookups for the server, no
2073 * matter if the name is an
2074 * KRB5_NT_ENTERPRISE_PRINCIPAL or not
2075 * - for the TGS-REQ when we are not given an
2076 * KRB5_NT_ENTERPRISE_PRINCIPAL, which also must
2077 * only lookup samAccountName based names.
2081 krb5_principal enterprise_principal = NULL;
2082 krb5_const_principal used_principal = NULL;
2085 char *filter = NULL;
2087 if (smb_krb5_principal_get_type(context, principal) == KRB5_NT_ENTERPRISE_PRINCIPAL) {
2089 /* Need to reparse the enterprise principal to find the real target */
2090 if (krb5_princ_size(context, principal) != 1) {
2091 ret = KRB5_PARSE_MALFORMED;
2092 krb5_set_error_message(context, ret, "samba_kdc_lookup_server: request for an "
2093 "enterprise principal with wrong (%d) number of components",
2094 krb5_princ_size(context, principal));
2097 str = smb_krb5_principal_get_comp_string(mem_ctx, context, principal, 0);
2099 return KRB5_PARSE_MALFORMED;
2101 ret = krb5_parse_name(context, str,
2102 &enterprise_principal);
2107 used_principal = enterprise_principal;
2109 used_principal = principal;
2112 /* server as client principal case, but we must not lookup userPrincipalNames */
2113 *realm_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
2115 /* TODO: Check if it is our realm, otherwise give referral */
2117 ret = krb5_unparse_name_flags(context, used_principal,
2118 KRB5_PRINCIPAL_UNPARSE_NO_REALM |
2119 KRB5_PRINCIPAL_UNPARSE_DISPLAY,
2121 used_principal = NULL;
2122 krb5_free_principal(context, enterprise_principal);
2123 enterprise_principal = NULL;
2126 krb5_set_error_message(context, ret, "samba_kdc_lookup_principal: could not parse principal");
2127 krb5_warnx(context, "samba_kdc_lookup_principal: could not parse principal");
2131 name1 = ldb_binary_encode_string(mem_ctx, short_princ);
2132 SAFE_FREE(short_princ);
2133 if (name1 == NULL) {
2136 len1 = strlen(name1);
2137 if (len1 >= 1 && name1[len1 - 1] != '$') {
2138 filter = talloc_asprintf(mem_ctx,
2139 "(&(objectClass=user)(|(samAccountName=%s)(samAccountName=%s$)))",
2141 if (filter == NULL) {
2145 filter = talloc_asprintf(mem_ctx,
2146 "(&(objectClass=user)(samAccountName=%s))",
2148 if (filter == NULL) {
2153 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx, msg,
2154 *realm_dn, LDB_SCOPE_SUBTREE,
2156 DSDB_SEARCH_SHOW_EXTENDED_DN | DSDB_SEARCH_NO_GLOBAL_CATALOG,
2158 if (lret == LDB_ERR_NO_SUCH_OBJECT) {
2159 DEBUG(10, ("Failed to find an entry for %s filter:%s\n",
2161 return SDB_ERR_NOENTRY;
2163 if (lret == LDB_ERR_CONSTRAINT_VIOLATION) {
2164 DEBUG(10, ("Failed to find unique entry for %s filter:%s\n",
2166 return SDB_ERR_NOENTRY;
2168 if (lret != LDB_SUCCESS) {
2169 DEBUG(0, ("Failed single search for %s - %s\n",
2170 name1, ldb_errstring(kdc_db_ctx->samdb)));
2171 return SDB_ERR_NOENTRY;
2175 return SDB_ERR_NOENTRY;
2180 static krb5_error_code samba_kdc_fetch_server(krb5_context context,
2181 struct samba_kdc_db_context *kdc_db_ctx,
2182 TALLOC_CTX *mem_ctx,
2183 krb5_const_principal principal,
2185 struct sdb_entry_ex *entry_ex)
2187 krb5_error_code ret;
2188 struct ldb_dn *realm_dn;
2189 struct ldb_message *msg;
2191 ret = samba_kdc_lookup_server(context, kdc_db_ctx, mem_ctx, principal,
2192 flags, server_attrs, &realm_dn, &msg);
2197 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
2198 principal, SAMBA_KDC_ENT_TYPE_SERVER,
2200 realm_dn, msg, entry_ex);
2202 krb5_warnx(context, "samba_kdc_fetch: message2entry failed");
2208 static krb5_error_code samba_kdc_lookup_realm(krb5_context context,
2209 struct samba_kdc_db_context *kdc_db_ctx,
2210 TALLOC_CTX *mem_ctx,
2211 krb5_const_principal principal,
2213 struct sdb_entry_ex *entry_ex)
2215 TALLOC_CTX *frame = talloc_stackframe();
2217 krb5_error_code ret;
2218 bool check_realm = false;
2219 const char *realm = NULL;
2220 struct dsdb_trust_routing_table *trt = NULL;
2221 const struct lsa_TrustDomainInfoInfoEx *tdo = NULL;
2222 unsigned int num_comp;
2226 num_comp = krb5_princ_size(context, principal);
2228 if (flags & SDB_F_GET_CLIENT) {
2229 if (flags & SDB_F_FOR_AS_REQ) {
2233 if (flags & SDB_F_GET_SERVER) {
2234 if (flags & SDB_F_FOR_TGS_REQ) {
2244 realm = smb_krb5_principal_get_realm(frame, context, principal);
2245 if (realm == NULL) {
2251 * The requested realm needs to be our own
2253 ok = lpcfg_is_my_domain_or_realm(kdc_db_ctx->lp_ctx, realm);
2256 * The request is not for us...
2259 return SDB_ERR_NOENTRY;
2262 if (smb_krb5_principal_get_type(context, principal) == KRB5_NT_ENTERPRISE_PRINCIPAL) {
2263 char *principal_string = NULL;
2264 krb5_principal enterprise_principal = NULL;
2265 char *enterprise_realm = NULL;
2267 if (num_comp != 1) {
2269 return SDB_ERR_NOENTRY;
2272 principal_string = smb_krb5_principal_get_comp_string(frame, context,
2274 if (principal_string == NULL) {
2279 ret = krb5_parse_name(context, principal_string,
2280 &enterprise_principal);
2281 TALLOC_FREE(principal_string);
2287 enterprise_realm = smb_krb5_principal_get_realm(
2288 frame, context, enterprise_principal);
2289 krb5_free_principal(context, enterprise_principal);
2290 if (enterprise_realm != NULL) {
2291 realm = enterprise_realm;
2295 if (flags & SDB_F_GET_SERVER) {
2296 char *service_realm = NULL;
2298 ret = principal_comp_strcmp(context, principal, 0, KRB5_TGS_NAME);
2301 * we need to search krbtgt/ locally
2308 * We need to check the last component against the routing table.
2310 * Note this works only with 2 or 3 component principals, e.g:
2312 * servicePrincipalName: ldap/W2K8R2-219.bla.base
2313 * servicePrincipalName: ldap/W2K8R2-219.bla.base/bla.base
2314 * servicePrincipalName: ldap/W2K8R2-219.bla.base/ForestDnsZones.bla.base
2315 * servicePrincipalName: ldap/W2K8R2-219.bla.base/DomainDnsZones.bla.base
2318 if (num_comp == 2 || num_comp == 3) {
2319 service_realm = smb_krb5_principal_get_comp_string(frame,
2325 if (service_realm != NULL) {
2326 realm = service_realm;
2330 ok = lpcfg_is_my_domain_or_realm(kdc_db_ctx->lp_ctx, realm);
2333 * skip the expensive routing lookup
2339 status = dsdb_trust_routing_table_load(kdc_db_ctx->samdb,
2341 if (!NT_STATUS_IS_OK(status)) {
2346 tdo = dsdb_trust_routing_by_name(trt, realm);
2349 * This principal has to be local
2355 if (tdo->trust_attributes & LSA_TRUST_ATTRIBUTE_WITHIN_FOREST) {
2357 * TODO: handle the routing within the forest
2359 * This should likely be handled in
2360 * samba_kdc_message2entry() in case we're
2361 * a global catalog. We'd need to check
2362 * if realm_dn is our own domain and derive
2363 * the dns domain name from realm_dn and check that
2364 * against the routing table or fallback to
2365 * the tdo we found here.
2367 * But for now we don't support multiple domains
2368 * in our forest correctly anyway.
2370 * Just search in our local database.
2376 ZERO_STRUCT(entry_ex->entry);
2378 ret = krb5_copy_principal(context, principal,
2379 &entry_ex->entry.principal);
2385 upper = strupper_talloc(frame, tdo->domain_name.string);
2386 if (upper == NULL) {
2391 ret = smb_krb5_principal_set_realm(context,
2392 entry_ex->entry.principal,
2400 return SDB_ERR_WRONG_REALM;
2403 krb5_error_code samba_kdc_fetch(krb5_context context,
2404 struct samba_kdc_db_context *kdc_db_ctx,
2405 krb5_const_principal principal,
2408 struct sdb_entry_ex *entry_ex)
2410 krb5_error_code ret = SDB_ERR_NOENTRY;
2411 TALLOC_CTX *mem_ctx;
2413 mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_fetch context");
2416 krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
2420 ret = samba_kdc_lookup_realm(context, kdc_db_ctx, mem_ctx,
2421 principal, flags, entry_ex);
2426 ret = SDB_ERR_NOENTRY;
2428 if (flags & SDB_F_GET_CLIENT) {
2429 ret = samba_kdc_fetch_client(context, kdc_db_ctx, mem_ctx, principal, flags, entry_ex);
2430 if (ret != SDB_ERR_NOENTRY) goto done;
2432 if (flags & SDB_F_GET_SERVER) {
2433 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
2434 ret = samba_kdc_fetch_krbtgt(context, kdc_db_ctx, mem_ctx, principal, flags, kvno, entry_ex);
2435 if (ret != SDB_ERR_NOENTRY) goto done;
2437 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
2438 ret = samba_kdc_fetch_server(context, kdc_db_ctx, mem_ctx, principal, flags, entry_ex);
2439 if (ret != SDB_ERR_NOENTRY) goto done;
2441 if (flags & SDB_F_GET_KRBTGT) {
2442 ret = samba_kdc_fetch_krbtgt(context, kdc_db_ctx, mem_ctx, principal, flags, kvno, entry_ex);
2443 if (ret != SDB_ERR_NOENTRY) goto done;
2447 talloc_free(mem_ctx);
2451 struct samba_kdc_seq {
2454 struct ldb_message **msgs;
2455 struct ldb_dn *realm_dn;
2458 static krb5_error_code samba_kdc_seq(krb5_context context,
2459 struct samba_kdc_db_context *kdc_db_ctx,
2460 struct sdb_entry_ex *entry)
2462 krb5_error_code ret;
2463 struct samba_kdc_seq *priv = kdc_db_ctx->seq_ctx;
2464 const char *realm = lpcfg_realm(kdc_db_ctx->lp_ctx);
2465 struct ldb_message *msg = NULL;
2466 const char *sAMAccountName = NULL;
2467 krb5_principal principal = NULL;
2468 TALLOC_CTX *mem_ctx;
2471 return SDB_ERR_NOENTRY;
2474 mem_ctx = talloc_named(priv, 0, "samba_kdc_seq context");
2478 krb5_set_error_message(context, ret, "samba_kdc_seq: talloc_named() failed!");
2482 while (priv->index < priv->count) {
2483 msg = priv->msgs[priv->index++];
2485 sAMAccountName = ldb_msg_find_attr_as_string(msg, "sAMAccountName", NULL);
2486 if (sAMAccountName != NULL) {
2491 if (sAMAccountName == NULL) {
2492 ret = SDB_ERR_NOENTRY;
2496 ret = smb_krb5_make_principal(context, &principal,
2497 realm, sAMAccountName, NULL);
2502 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
2503 principal, SAMBA_KDC_ENT_TYPE_ANY,
2504 SDB_F_ADMIN_DATA|SDB_F_GET_ANY,
2505 priv->realm_dn, msg, entry);
2508 if (principal != NULL) {
2509 krb5_free_principal(context, principal);
2514 kdc_db_ctx->seq_ctx = NULL;
2516 talloc_free(mem_ctx);
2522 krb5_error_code samba_kdc_firstkey(krb5_context context,
2523 struct samba_kdc_db_context *kdc_db_ctx,
2524 struct sdb_entry_ex *entry)
2526 struct ldb_context *ldb_ctx = kdc_db_ctx->samdb;
2527 struct samba_kdc_seq *priv = kdc_db_ctx->seq_ctx;
2529 struct ldb_result *res = NULL;
2530 krb5_error_code ret;
2531 TALLOC_CTX *mem_ctx;
2536 kdc_db_ctx->seq_ctx = NULL;
2539 priv = (struct samba_kdc_seq *) talloc(kdc_db_ctx, struct samba_kdc_seq);
2542 krb5_set_error_message(context, ret, "talloc: out of memory");
2548 priv->realm_dn = ldb_get_default_basedn(ldb_ctx);
2551 mem_ctx = talloc_named(priv, 0, "samba_kdc_firstkey context");
2555 krb5_set_error_message(context, ret, "samba_kdc_firstkey: talloc_named() failed!");
2559 ret = krb5_get_default_realm(context, &realm);
2564 krb5_free_default_realm(context, realm);
2566 lret = dsdb_search(ldb_ctx, priv, &res,
2567 priv->realm_dn, LDB_SCOPE_SUBTREE, user_attrs,
2568 DSDB_SEARCH_NO_GLOBAL_CATALOG,
2569 "(objectClass=user)");
2571 if (lret != LDB_SUCCESS) {
2573 return SDB_ERR_NOENTRY;
2576 priv->count = res->count;
2577 priv->msgs = talloc_steal(priv, res->msgs);
2580 kdc_db_ctx->seq_ctx = priv;
2582 ret = samba_kdc_seq(context, kdc_db_ctx, entry);
2586 kdc_db_ctx->seq_ctx = NULL;
2588 talloc_free(mem_ctx);
2593 krb5_error_code samba_kdc_nextkey(krb5_context context,
2594 struct samba_kdc_db_context *kdc_db_ctx,
2595 struct sdb_entry_ex *entry)
2597 return samba_kdc_seq(context, kdc_db_ctx, entry);
2600 /* Check if a given entry may delegate or do s4u2self to this target principal
2602 * The safest way to determine 'self' is to check the DB record made at
2603 * the time the principal was presented to the KDC.
2606 samba_kdc_check_s4u2self(krb5_context context,
2607 struct samba_kdc_entry *skdc_entry_client,
2608 struct samba_kdc_entry *skdc_entry_server_target)
2610 struct dom_sid *orig_sid;
2611 struct dom_sid *target_sid;
2612 TALLOC_CTX *frame = talloc_stackframe();
2614 orig_sid = samdb_result_dom_sid(frame,
2615 skdc_entry_client->msg,
2617 target_sid = samdb_result_dom_sid(frame,
2618 skdc_entry_server_target->msg,
2622 * Allow delegation to the same record (representing a
2623 * principal), even if by a different name. The easy and safe
2624 * way to prove this is by SID comparison
2626 if (!(orig_sid && target_sid && dom_sid_equal(orig_sid, target_sid))) {
2628 return KRB5KRB_AP_ERR_BADMATCH;
2635 /* Certificates printed by a the Certificate Authority might have a
2636 * slightly different form of the user principal name to that in the
2637 * database. Allow a mismatch where they both refer to the same
2641 samba_kdc_check_pkinit_ms_upn_match(krb5_context context,
2642 struct samba_kdc_db_context *kdc_db_ctx,
2643 struct samba_kdc_entry *skdc_entry,
2644 krb5_const_principal certificate_principal)
2646 krb5_error_code ret;
2647 struct ldb_dn *realm_dn;
2648 struct ldb_message *msg;
2649 struct dom_sid *orig_sid;
2650 struct dom_sid *target_sid;
2651 const char *ms_upn_check_attrs[] = {
2655 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_pkinit_ms_upn_match");
2659 krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
2663 ret = samba_kdc_lookup_client(context, kdc_db_ctx,
2664 mem_ctx, certificate_principal,
2665 ms_upn_check_attrs, &realm_dn, &msg);
2668 talloc_free(mem_ctx);
2672 orig_sid = samdb_result_dom_sid(mem_ctx, skdc_entry->msg, "objectSid");
2673 target_sid = samdb_result_dom_sid(mem_ctx, msg, "objectSid");
2675 /* Consider these to be the same principal, even if by a different
2676 * name. The easy and safe way to prove this is by SID
2678 if (!(orig_sid && target_sid && dom_sid_equal(orig_sid, target_sid))) {
2679 talloc_free(mem_ctx);
2680 #if defined(KRB5KDC_ERR_CLIENT_NAME_MISMATCH) /* MIT */
2681 return KRB5KDC_ERR_CLIENT_NAME_MISMATCH;
2682 #else /* Heimdal (where this is an enum) */
2683 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH;
2687 talloc_free(mem_ctx);
2692 * Check if a given entry may delegate to this target principal
2696 samba_kdc_check_s4u2proxy(krb5_context context,
2697 struct samba_kdc_db_context *kdc_db_ctx,
2698 struct samba_kdc_entry *skdc_entry,
2699 krb5_const_principal target_principal)
2701 krb5_error_code ret;
2703 const char *client_dn = NULL;
2704 const char *target_principal_name = NULL;
2705 struct ldb_message_element *el;
2710 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_s4u2proxy");
2714 krb5_set_error_message(context, ret,
2715 "samba_kdc_check_s4u2proxy:"
2716 " talloc_named() failed!");
2720 client_dn = ldb_dn_get_linearized(skdc_entry->msg->dn);
2726 krb5_set_error_message(context, ret,
2727 "samba_kdc_check_s4u2proxy:"
2728 " ldb_dn_get_linearized() failed!");
2733 * The main heimdal code already checked that the target_principal
2734 * belongs to the same realm as the client.
2736 * So we just need the principal without the realm,
2737 * as that is what is configured in the "msDS-AllowedToDelegateTo"
2740 ret = krb5_unparse_name_flags(context, target_principal,
2741 KRB5_PRINCIPAL_UNPARSE_NO_REALM, &tmp);
2743 talloc_free(mem_ctx);
2744 krb5_set_error_message(context, ret,
2745 "samba_kdc_check_s4u2proxy:"
2746 " krb5_unparse_name() failed!");
2749 DEBUG(10,("samba_kdc_check_s4u2proxy: client[%s] for target[%s]\n",
2752 target_principal_name = talloc_strdup(mem_ctx, tmp);
2754 if (target_principal_name == NULL) {
2756 krb5_set_error_message(context, ret,
2757 "samba_kdc_check_s4u2proxy:"
2758 " talloc_strdup() failed!");
2762 el = ldb_msg_find_element(skdc_entry->msg, "msDS-AllowedToDelegateTo");
2767 val = data_blob_string_const(target_principal_name);
2769 for (i=0; i<el->num_values; i++) {
2770 struct ldb_val *val1 = &val;
2771 struct ldb_val *val2 = &el->values[i];
2774 if (val1->length != val2->length) {
2778 cmp = strncasecmp((const char *)val1->data,
2779 (const char *)val2->data,
2793 DEBUG(10,("samba_kdc_check_s4u2proxy: client[%s] allowed target[%s]\n",
2795 talloc_free(mem_ctx);
2799 krb5_set_error_message(context, ret,
2800 "samba_kdc_check_s4u2proxy: client[%s] "
2801 "not allowed for delegation to target[%s]",
2803 target_principal_name);
2804 talloc_free(mem_ctx);
2805 return KRB5KDC_ERR_BADOPTION;
2808 NTSTATUS samba_kdc_setup_db_ctx(TALLOC_CTX *mem_ctx, struct samba_kdc_base_context *base_ctx,
2809 struct samba_kdc_db_context **kdc_db_ctx_out)
2812 struct ldb_message *msg;
2813 struct auth_session_info *session_info;
2814 struct samba_kdc_db_context *kdc_db_ctx;
2815 /* The idea here is very simple. Using Kerberos to
2816 * authenticate the KDC to the LDAP server is higly likely to
2819 * In future we may set this up to use EXERNAL and SSL
2820 * certificates, for now it will almost certainly be NTLMSSP_SET_USERNAME
2823 kdc_db_ctx = talloc_zero(mem_ctx, struct samba_kdc_db_context);
2824 if (kdc_db_ctx == NULL) {
2825 return NT_STATUS_NO_MEMORY;
2827 kdc_db_ctx->ev_ctx = base_ctx->ev_ctx;
2828 kdc_db_ctx->lp_ctx = base_ctx->lp_ctx;
2829 kdc_db_ctx->msg_ctx = base_ctx->msg_ctx;
2831 /* get default kdc policy */
2832 lpcfg_default_kdc_policy(mem_ctx,
2834 &kdc_db_ctx->policy.svc_tkt_lifetime,
2835 &kdc_db_ctx->policy.usr_tkt_lifetime,
2836 &kdc_db_ctx->policy.renewal_lifetime);
2838 session_info = system_session(kdc_db_ctx->lp_ctx);
2839 if (session_info == NULL) {
2840 return NT_STATUS_INTERNAL_ERROR;
2843 /* Setup the link to LDB */
2844 kdc_db_ctx->samdb = samdb_connect(kdc_db_ctx,
2850 if (kdc_db_ctx->samdb == NULL) {
2851 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot open samdb for KDC backend!"));
2852 talloc_free(kdc_db_ctx);
2853 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
2856 /* Find out our own krbtgt kvno */
2857 ldb_ret = samdb_rodc(kdc_db_ctx->samdb, &kdc_db_ctx->rodc);
2858 if (ldb_ret != LDB_SUCCESS) {
2859 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine if we are an RODC in KDC backend: %s\n",
2860 ldb_errstring(kdc_db_ctx->samdb)));
2861 talloc_free(kdc_db_ctx);
2862 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
2864 if (kdc_db_ctx->rodc) {
2865 int my_krbtgt_number;
2866 const char *secondary_keytab[] = { "msDS-SecondaryKrbTgtNumber", NULL };
2867 struct ldb_dn *account_dn;
2868 struct ldb_dn *server_dn = samdb_server_dn(kdc_db_ctx->samdb, kdc_db_ctx);
2870 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine server DN in KDC backend: %s\n",
2871 ldb_errstring(kdc_db_ctx->samdb)));
2872 talloc_free(kdc_db_ctx);
2873 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
2876 ldb_ret = samdb_reference_dn(kdc_db_ctx->samdb, kdc_db_ctx, server_dn,
2877 "serverReference", &account_dn);
2878 if (ldb_ret != LDB_SUCCESS) {
2879 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine server account in KDC backend: %s\n",
2880 ldb_errstring(kdc_db_ctx->samdb)));
2881 talloc_free(kdc_db_ctx);
2882 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
2885 ldb_ret = samdb_reference_dn(kdc_db_ctx->samdb, kdc_db_ctx, account_dn,
2886 "msDS-KrbTgtLink", &kdc_db_ctx->krbtgt_dn);
2887 talloc_free(account_dn);
2888 if (ldb_ret != LDB_SUCCESS) {
2889 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot determine RODC krbtgt account in KDC backend: %s\n",
2890 ldb_errstring(kdc_db_ctx->samdb)));
2891 talloc_free(kdc_db_ctx);
2892 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
2895 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb, kdc_db_ctx,
2896 &msg, kdc_db_ctx->krbtgt_dn, LDB_SCOPE_BASE,
2898 DSDB_SEARCH_NO_GLOBAL_CATALOG,
2899 "(&(objectClass=user)(msDS-SecondaryKrbTgtNumber=*))");
2900 if (ldb_ret != LDB_SUCCESS) {
2901 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot read krbtgt account %s in KDC backend to get msDS-SecondaryKrbTgtNumber: %s: %s\n",
2902 ldb_dn_get_linearized(kdc_db_ctx->krbtgt_dn),
2903 ldb_errstring(kdc_db_ctx->samdb),
2904 ldb_strerror(ldb_ret)));
2905 talloc_free(kdc_db_ctx);
2906 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
2908 my_krbtgt_number = ldb_msg_find_attr_as_int(msg, "msDS-SecondaryKrbTgtNumber", -1);
2909 if (my_krbtgt_number == -1) {
2910 DEBUG(1, ("samba_kdc_setup_db_ctx: Cannot read msDS-SecondaryKrbTgtNumber from krbtgt account %s in KDC backend: got %d\n",
2911 ldb_dn_get_linearized(kdc_db_ctx->krbtgt_dn),
2913 talloc_free(kdc_db_ctx);
2914 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
2916 kdc_db_ctx->my_krbtgt_number = my_krbtgt_number;
2919 kdc_db_ctx->my_krbtgt_number = 0;
2920 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb, kdc_db_ctx,
2922 ldb_get_default_basedn(kdc_db_ctx->samdb),
2925 DSDB_SEARCH_NO_GLOBAL_CATALOG,
2926 "(&(objectClass=user)(samAccountName=krbtgt))");
2928 if (ldb_ret != LDB_SUCCESS) {
2929 DEBUG(1, ("samba_kdc_fetch: could not find own KRBTGT in DB: %s\n", ldb_errstring(kdc_db_ctx->samdb)));
2930 talloc_free(kdc_db_ctx);
2931 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
2933 kdc_db_ctx->krbtgt_dn = talloc_steal(kdc_db_ctx, msg->dn);
2934 kdc_db_ctx->my_krbtgt_number = 0;
2937 *kdc_db_ctx_out = kdc_db_ctx;
2938 return NT_STATUS_OK;