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 "system/time.h"
26 #include "../libds/common/flags.h"
27 #include "lib/ldb/include/ldb.h"
28 #include "librpc/gen_ndr/netlogon.h"
29 #include "libcli/security/security.h"
30 #include "auth/auth.h"
31 #include "auth/credentials/credentials.h"
32 #include "auth/auth_sam.h"
33 #include "../lib/util/util_ldb.h"
34 #include "dsdb/samdb/samdb.h"
35 #include "dsdb/common/util.h"
36 #include "librpc/ndr/libndr.h"
37 #include "librpc/gen_ndr/ndr_drsblobs.h"
38 #include "librpc/gen_ndr/lsa.h"
39 #include "libcli/auth/libcli_auth.h"
40 #include "param/param.h"
41 #include "../lib/crypto/md4.h"
42 #include "system/kerberos.h"
44 #include "kdc/samba_kdc.h"
45 #include "kdc/db-glue.h"
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 {
53 INBOUND = LSA_TRUST_DIRECTION_INBOUND,
54 OUTBOUND = LSA_TRUST_DIRECTION_OUTBOUND
57 static const char *trust_attrs[] = {
62 "msDS-SupportedEncryptionTypes",
69 static KerberosTime ldb_msg_find_krb5time_ldap_time(struct ldb_message *msg, const char *attr, KerberosTime default_val)
75 gentime = ldb_msg_find_attr_as_string(msg, attr, NULL);
79 tmp = strptime(gentime, "%Y%m%d%H%M%SZ", &tm);
87 static HDBFlags uf2HDBFlags(krb5_context context, int userAccountControl, enum samba_kdc_ent_type ent_type)
89 HDBFlags flags = int2HDBFlags(0);
91 /* we don't allow kadmin deletes */
94 /* mark the principal as invalid to start with */
99 /* All accounts are servers, but this may be disabled again in the caller */
102 /* Account types - clear the invalid bit if it turns out to be valid */
103 if (userAccountControl & UF_NORMAL_ACCOUNT) {
104 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
110 if (userAccountControl & UF_INTERDOMAIN_TRUST_ACCOUNT) {
111 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
116 if (userAccountControl & UF_WORKSTATION_TRUST_ACCOUNT) {
117 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
122 if (userAccountControl & UF_SERVER_TRUST_ACCOUNT) {
123 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
129 /* Not permitted to act as a client if disabled */
130 if (userAccountControl & UF_ACCOUNTDISABLE) {
133 if (userAccountControl & UF_LOCKOUT) {
137 if (userAccountControl & UF_PASSWORD_NOTREQD) {
142 UF_PASSWORD_CANT_CHANGE and UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED are irrelevent
144 if (userAccountControl & UF_TEMP_DUPLICATE_ACCOUNT) {
148 /* UF_DONT_EXPIRE_PASSWD and UF_USE_DES_KEY_ONLY handled in samba_kdc_message2entry() */
151 if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
155 if (userAccountControl & UF_SMARTCARD_REQUIRED) {
156 flags.require_hwauth = 1;
158 if (userAccountControl & UF_TRUSTED_FOR_DELEGATION) {
159 flags.ok_as_delegate = 1;
161 if (!(userAccountControl & UF_NOT_DELEGATED)) {
162 flags.forwardable = 1;
166 if (userAccountControl & UF_DONT_REQUIRE_PREAUTH) {
167 flags.require_preauth = 0;
169 flags.require_preauth = 1;
175 static int samba_kdc_entry_destructor(struct samba_kdc_entry *p)
177 hdb_entry_ex *entry_ex = p->entry_ex;
178 free_hdb_entry(&entry_ex->entry);
182 static void samba_kdc_free_entry(krb5_context context, hdb_entry_ex *entry_ex)
184 /* this function is called only from hdb_free_entry().
185 * Make sure we neutralize the destructor or we will
186 * get a double free later when hdb_free_entry() will
187 * try to call free_hdb_entry() */
188 talloc_set_destructor(entry_ex->ctx, NULL);
190 /* now proceed to free the talloc part */
191 talloc_free(entry_ex->ctx);
194 static krb5_error_code samba_kdc_message2entry_keys(krb5_context context,
196 struct ldb_message *msg,
197 unsigned int userAccountControl,
198 hdb_entry_ex *entry_ex)
200 krb5_error_code ret = 0;
201 enum ndr_err_code ndr_err;
202 struct samr_Password *hash;
203 const struct ldb_val *sc_val;
204 struct supplementalCredentialsBlob scb;
205 struct supplementalCredentialsPackage *scpk = NULL;
206 bool newer_keys = false;
207 struct package_PrimaryKerberosBlob _pkb;
208 struct package_PrimaryKerberosCtr3 *pkb3 = NULL;
209 struct package_PrimaryKerberosCtr4 *pkb4 = NULL;
211 uint16_t allocated_keys = 0;
213 entry_ex->entry.keys.val = NULL;
214 entry_ex->entry.keys.len = 0;
216 entry_ex->entry.kvno = ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0);
218 /* Get keys from the db */
220 hash = samdb_result_hash(mem_ctx, msg, "unicodePwd");
221 sc_val = ldb_msg_find_ldb_val(msg, "supplementalCredentials");
223 /* unicodePwd for enctype 0x17 (23) if present */
228 /* supplementalCredentials if present */
230 ndr_err = ndr_pull_struct_blob_all(sc_val, mem_ctx, &scb,
231 (ndr_pull_flags_fn_t)ndr_pull_supplementalCredentialsBlob);
232 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
233 dump_data(0, sc_val->data, sc_val->length);
238 if (scb.sub.signature != SUPPLEMENTAL_CREDENTIALS_SIGNATURE) {
239 NDR_PRINT_DEBUG(supplementalCredentialsBlob, &scb);
244 for (i=0; i < scb.sub.num_packages; i++) {
245 if (strcmp("Primary:Kerberos-Newer-Keys", scb.sub.packages[i].name) == 0) {
246 scpk = &scb.sub.packages[i];
247 if (!scpk->data || !scpk->data[0]) {
253 } else if (strcmp("Primary:Kerberos", scb.sub.packages[i].name) == 0) {
254 scpk = &scb.sub.packages[i];
255 if (!scpk->data || !scpk->data[0]) {
259 * we don't break here in hope to find
260 * a Kerberos-Newer-Keys package
266 * Primary:Kerberos-Newer-Keys or Primary:Kerberos element
267 * of supplementalCredentials
272 blob = strhex_to_data_blob(mem_ctx, scpk->data);
278 /* we cannot use ndr_pull_struct_blob_all() here, as w2k and w2k3 add padding bytes */
279 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, &_pkb,
280 (ndr_pull_flags_fn_t)ndr_pull_package_PrimaryKerberosBlob);
281 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
283 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
284 krb5_warnx(context, "samba_kdc_message2entry_keys: could not parse package_PrimaryKerberosBlob");
288 if (newer_keys && _pkb.version != 4) {
290 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
291 krb5_warnx(context, "samba_kdc_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
295 if (!newer_keys && _pkb.version != 3) {
297 krb5_set_error_message(context, ret, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
298 krb5_warnx(context, "samba_kdc_message2entry_keys: could not parse Primary:Kerberos not version 3");
302 if (_pkb.version == 4) {
303 pkb4 = &_pkb.ctr.ctr4;
304 allocated_keys += pkb4->num_keys;
305 } else if (_pkb.version == 3) {
306 pkb3 = &_pkb.ctr.ctr3;
307 allocated_keys += pkb3->num_keys;
311 if (allocated_keys == 0) {
312 /* oh, no password. Apparently (comment in
313 * hdb-ldap.c) this violates the ASN.1, but this
314 * allows an entry with no keys (yet). */
318 /* allocate space to decode into */
319 entry_ex->entry.keys.len = 0;
320 entry_ex->entry.keys.val = calloc(allocated_keys, sizeof(Key));
321 if (entry_ex->entry.keys.val == NULL) {
326 if (hash && !(userAccountControl & UF_USE_DES_KEY_ONLY)) {
330 key.salt = NULL; /* No salt for this enc type */
332 ret = krb5_keyblock_init(context,
333 ENCTYPE_ARCFOUR_HMAC,
334 hash->hash, sizeof(hash->hash),
340 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
341 entry_ex->entry.keys.len++;
345 for (i=0; i < pkb4->num_keys; i++) {
349 if (!pkb4->keys[i].value) continue;
351 if (userAccountControl & UF_USE_DES_KEY_ONLY) {
352 switch (pkb4->keys[i].keytype) {
353 case ENCTYPE_DES_CBC_CRC:
354 case ENCTYPE_DES_CBC_MD5:
367 if (pkb4->salt.string) {
370 salt = data_blob_string_const(pkb4->salt.string);
372 key.salt = calloc(1, sizeof(*key.salt));
373 if (key.salt == NULL) {
378 key.salt->type = hdb_pw_salt;
380 ret = krb5_data_copy(&key.salt->salt, salt.data, salt.length);
388 /* TODO: maybe pass the iteration_count somehow... */
390 ret = krb5_keyblock_init(context,
391 pkb4->keys[i].keytype,
392 pkb4->keys[i].value->data,
393 pkb4->keys[i].value->length,
395 if (ret == KRB5_PROG_ETYPE_NOSUPP) {
396 DEBUG(2,("Unsupported keytype ignored - type %u\n",
397 pkb4->keys[i].keytype));
410 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
411 entry_ex->entry.keys.len++;
414 for (i=0; i < pkb3->num_keys; i++) {
418 if (!pkb3->keys[i].value) continue;
420 if (userAccountControl & UF_USE_DES_KEY_ONLY) {
421 switch (pkb3->keys[i].keytype) {
422 case ENCTYPE_DES_CBC_CRC:
423 case ENCTYPE_DES_CBC_MD5:
436 if (pkb3->salt.string) {
439 salt = data_blob_string_const(pkb3->salt.string);
441 key.salt = calloc(1, sizeof(*key.salt));
442 if (key.salt == NULL) {
447 key.salt->type = hdb_pw_salt;
449 ret = krb5_data_copy(&key.salt->salt, salt.data, salt.length);
457 ret = krb5_keyblock_init(context,
458 pkb3->keys[i].keytype,
459 pkb3->keys[i].value->data,
460 pkb3->keys[i].value->length,
471 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
472 entry_ex->entry.keys.len++;
478 entry_ex->entry.keys.len = 0;
480 if (entry_ex->entry.keys.len == 0 && entry_ex->entry.keys.val) {
481 free(entry_ex->entry.keys.val);
482 entry_ex->entry.keys.val = NULL;
488 * Construct an hdb_entry from a directory entry.
490 static krb5_error_code samba_kdc_message2entry(krb5_context context,
491 struct samba_kdc_db_context *kdc_db_ctx,
492 TALLOC_CTX *mem_ctx, krb5_const_principal principal,
493 enum samba_kdc_ent_type ent_type,
494 struct ldb_dn *realm_dn,
495 struct ldb_message *msg,
496 hdb_entry_ex *entry_ex)
498 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
499 unsigned int userAccountControl;
501 krb5_error_code ret = 0;
502 krb5_boolean is_computer = FALSE;
503 char *realm = strupper_talloc(mem_ctx, lp_realm(lp_ctx));
505 struct samba_kdc_entry *p;
510 struct ldb_message_element *objectclasses;
511 struct ldb_val computer_val;
512 const char *samAccountName = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
513 computer_val.data = discard_const_p(uint8_t,"computer");
514 computer_val.length = strlen((const char *)computer_val.data);
516 if (!samAccountName) {
518 krb5_set_error_message(context, ret, "samba_kdc_message2entry: no samAccountName present");
522 objectclasses = ldb_msg_find_element(msg, "objectClass");
524 if (objectclasses && ldb_msg_find_val(objectclasses, &computer_val)) {
528 memset(entry_ex, 0, sizeof(*entry_ex));
532 krb5_set_error_message(context, ret, "talloc_strdup: out of memory");
536 p = talloc(mem_ctx, struct samba_kdc_entry);
542 p->kdc_db_ctx = kdc_db_ctx;
543 p->entry_ex = entry_ex;
544 p->realm_dn = talloc_reference(p, realm_dn);
550 talloc_set_destructor(p, samba_kdc_entry_destructor);
552 /* make sure we do not have bogus data in there */
553 memset(&entry_ex->entry, 0, sizeof(hdb_entry));
556 entry_ex->free_entry = samba_kdc_free_entry;
558 userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
561 entry_ex->entry.principal = malloc(sizeof(*(entry_ex->entry.principal)));
562 if (ent_type == SAMBA_KDC_ENT_TYPE_ANY && principal == NULL) {
563 krb5_make_principal(context, &entry_ex->entry.principal, realm, samAccountName, NULL);
565 ret = copy_Principal(principal, entry_ex->entry.principal);
567 krb5_clear_error_message(context);
571 /* While we have copied the client principal, tests
572 * show that Win2k3 returns the 'corrected' realm, not
573 * the client-specified realm. This code attempts to
574 * replace the client principal's realm with the one
575 * we determine from our records */
577 /* this has to be with malloc() */
578 krb5_principal_set_realm(context, entry_ex->entry.principal, realm);
581 /* First try and figure out the flags based on the userAccountControl */
582 entry_ex->entry.flags = uf2HDBFlags(context, userAccountControl, ent_type);
584 /* Windows 2008 seems to enforce this (very sensible) rule by
585 * default - don't allow offline attacks on a user's password
586 * by asking for a ticket to them as a service (encrypted with
587 * their probably patheticly insecure password) */
589 if (entry_ex->entry.flags.server
590 && lp_parm_bool(lp_ctx, NULL, "kdc", "require spn for service", true)) {
591 if (!is_computer && !ldb_msg_find_attr_as_string(msg, "servicePrincipalName", NULL)) {
592 entry_ex->entry.flags.server = 0;
597 /* These (created_by, modified_by) parts of the entry are not relevant for Samba4's use
598 * of the Heimdal KDC. They are stored in a the traditional
599 * DB for audit purposes, and still form part of the structure
602 /* use 'whenCreated' */
603 entry_ex->entry.created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
604 /* use 'kadmin' for now (needed by mit_samba) */
605 krb5_make_principal(context,
606 &entry_ex->entry.created_by.principal,
607 realm, "kadmin", NULL);
609 entry_ex->entry.modified_by = (Event *) malloc(sizeof(Event));
610 if (entry_ex->entry.modified_by == NULL) {
612 krb5_set_error_message(context, ret, "malloc: out of memory");
616 /* use 'whenChanged' */
617 entry_ex->entry.modified_by->time = ldb_msg_find_krb5time_ldap_time(msg, "whenChanged", 0);
618 /* use 'kadmin' for now (needed by mit_samba) */
619 krb5_make_principal(context,
620 &entry_ex->entry.modified_by->principal,
621 realm, "kadmin", NULL);
625 /* The lack of password controls etc applies to krbtgt by
626 * virtue of being that particular RID */
627 status = dom_sid_split_rid(NULL, samdb_result_dom_sid(mem_ctx, msg, "objectSid"), NULL, &rid);
629 if (!NT_STATUS_IS_OK(status)) {
634 if (rid == DOMAIN_RID_KRBTGT) {
635 entry_ex->entry.valid_end = NULL;
636 entry_ex->entry.pw_end = NULL;
638 entry_ex->entry.flags.invalid = 0;
639 entry_ex->entry.flags.server = 1;
641 /* Don't mark all requests for the krbtgt/realm as
642 * 'change password', as otherwise we could get into
643 * trouble, and not enforce the password expirty.
644 * Instead, only do it when request is for the kpasswd service */
645 if (ent_type == SAMBA_KDC_ENT_TYPE_SERVER
646 && principal->name.name_string.len == 2
647 && (strcmp(principal->name.name_string.val[0], "kadmin") == 0)
648 && (strcmp(principal->name.name_string.val[1], "changepw") == 0)
649 && lp_is_my_domain_or_realm(lp_ctx, principal->realm)) {
650 entry_ex->entry.flags.change_pw = 1;
652 entry_ex->entry.flags.client = 0;
653 entry_ex->entry.flags.forwardable = 1;
654 entry_ex->entry.flags.ok_as_delegate = 1;
655 } else if (entry_ex->entry.flags.server && ent_type == SAMBA_KDC_ENT_TYPE_SERVER) {
656 /* The account/password expiry only applies when the account is used as a
657 * client (ie password login), not when used as a server */
659 /* Make very well sure we don't use this for a client,
660 * it could bypass the password restrictions */
661 entry_ex->entry.flags.client = 0;
663 entry_ex->entry.valid_end = NULL;
664 entry_ex->entry.pw_end = NULL;
667 NTTIME must_change_time
668 = samdb_result_force_password_change(kdc_db_ctx->samdb, mem_ctx,
670 if (must_change_time == 0x7FFFFFFFFFFFFFFFULL) {
671 entry_ex->entry.pw_end = NULL;
673 entry_ex->entry.pw_end = malloc(sizeof(*entry_ex->entry.pw_end));
674 if (entry_ex->entry.pw_end == NULL) {
678 *entry_ex->entry.pw_end = nt_time_to_unix(must_change_time);
681 acct_expiry = samdb_result_account_expires(msg);
682 if (acct_expiry == 0x7FFFFFFFFFFFFFFFULL) {
683 entry_ex->entry.valid_end = NULL;
685 entry_ex->entry.valid_end = malloc(sizeof(*entry_ex->entry.valid_end));
686 if (entry_ex->entry.valid_end == NULL) {
690 *entry_ex->entry.valid_end = nt_time_to_unix(acct_expiry);
694 entry_ex->entry.valid_start = NULL;
696 entry_ex->entry.max_life = NULL;
698 entry_ex->entry.max_renew = NULL;
700 entry_ex->entry.generation = NULL;
702 /* Get keys from the db */
703 ret = samba_kdc_message2entry_keys(context, p, msg, userAccountControl,
706 /* Could be bougus data in the entry, or out of memory */
710 entry_ex->entry.etypes = malloc(sizeof(*(entry_ex->entry.etypes)));
711 if (entry_ex->entry.etypes == NULL) {
712 krb5_clear_error_message(context);
716 entry_ex->entry.etypes->len = entry_ex->entry.keys.len;
717 entry_ex->entry.etypes->val = calloc(entry_ex->entry.etypes->len, sizeof(int));
718 if (entry_ex->entry.etypes->val == NULL) {
719 krb5_clear_error_message(context);
723 for (i=0; i < entry_ex->entry.etypes->len; i++) {
724 entry_ex->entry.etypes->val[i] = entry_ex->entry.keys.val[i].key.keytype;
728 p->msg = talloc_steal(p, msg);
732 /* This doesn't free ent itself, that is for the eventual caller to do */
733 hdb_free_entry(context, entry_ex);
735 talloc_steal(kdc_db_ctx, entry_ex->ctx);
742 * Construct an hdb_entry from a directory entry.
744 static krb5_error_code samba_kdc_trust_message2entry(krb5_context context,
745 struct samba_kdc_db_context *kdc_db_ctx,
746 TALLOC_CTX *mem_ctx, krb5_const_principal principal,
747 enum trust_direction direction,
748 struct ldb_dn *realm_dn,
749 struct ldb_message *msg,
750 hdb_entry_ex *entry_ex)
752 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
753 const char *dnsdomain;
754 char *realm = strupper_talloc(mem_ctx, lp_realm(lp_ctx));
755 DATA_BLOB password_utf16;
756 struct samr_Password password_hash;
757 const struct ldb_val *password_val;
758 struct trustAuthInOutBlob password_blob;
759 struct samba_kdc_entry *p;
761 enum ndr_err_code ndr_err;
762 int ret, trust_direction_flags;
765 p = talloc(mem_ctx, struct samba_kdc_entry);
771 p->kdc_db_ctx = kdc_db_ctx;
772 p->entry_ex = entry_ex;
773 p->realm_dn = realm_dn;
775 talloc_set_destructor(p, samba_kdc_entry_destructor);
777 /* make sure we do not have bogus data in there */
778 memset(&entry_ex->entry, 0, sizeof(hdb_entry));
781 entry_ex->free_entry = samba_kdc_free_entry;
783 /* use 'whenCreated' */
784 entry_ex->entry.created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
785 /* use 'kadmin' for now (needed by mit_samba) */
786 krb5_make_principal(context,
787 &entry_ex->entry.created_by.principal,
788 realm, "kadmin", NULL);
790 entry_ex->entry.valid_start = NULL;
792 trust_direction_flags = ldb_msg_find_attr_as_int(msg, "trustDirection", 0);
794 if (direction == INBOUND) {
795 password_val = ldb_msg_find_ldb_val(msg, "trustAuthIncoming");
797 } else { /* OUTBOUND */
798 dnsdomain = ldb_msg_find_attr_as_string(msg, "trustPartner", NULL);
801 realm = strupper_talloc(mem_ctx, dnsdomain);
802 password_val = ldb_msg_find_ldb_val(msg, "trustAuthOutgoing");
805 if (!password_val || !(trust_direction_flags & direction)) {
810 ndr_err = ndr_pull_struct_blob(password_val, mem_ctx, &password_blob,
811 (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
812 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
817 entry_ex->entry.kvno = -1;
818 for (i=0; i < password_blob.count; i++) {
819 if (password_blob.current->array[i].AuthType == TRUST_AUTH_TYPE_VERSION) {
820 entry_ex->entry.kvno = password_blob.current->array[i].AuthInfo.version.version;
824 for (i=0; i < password_blob.count; i++) {
825 if (password_blob.current->array[i].AuthType == TRUST_AUTH_TYPE_CLEAR) {
826 password_utf16 = data_blob_const(password_blob.current->array[i].AuthInfo.clear.password,
827 password_blob.current->array[i].AuthInfo.clear.size);
828 /* In the future, generate all sorts of
829 * hashes, but for now we can't safely convert
830 * the random strings windows uses into
833 /* but as it is utf16 already, we can get the NT password/arcfour-hmac-md5 key */
834 mdfour(password_hash.hash, password_utf16.data, password_utf16.length);
836 } else if (password_blob.current->array[i].AuthType == TRUST_AUTH_TYPE_NT4OWF) {
837 password_hash = password_blob.current->array[i].AuthInfo.nt4owf.password;
842 if (i < password_blob.count) {
844 /* Must have found a cleartext or MD4 password */
845 entry_ex->entry.keys.val = calloc(1, sizeof(Key));
848 key.salt = NULL; /* No salt for this enc type */
850 if (entry_ex->entry.keys.val == NULL) {
855 ret = krb5_keyblock_init(context,
856 ENCTYPE_ARCFOUR_HMAC,
857 password_hash.hash, sizeof(password_hash.hash),
860 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
861 entry_ex->entry.keys.len++;
864 entry_ex->entry.principal = malloc(sizeof(*(entry_ex->entry.principal)));
866 ret = copy_Principal(principal, entry_ex->entry.principal);
868 krb5_clear_error_message(context);
872 /* While we have copied the client principal, tests
873 * show that Win2k3 returns the 'corrected' realm, not
874 * the client-specified realm. This code attempts to
875 * replace the client principal's realm with the one
876 * we determine from our records */
878 krb5_principal_set_realm(context, entry_ex->entry.principal, realm);
879 entry_ex->entry.flags = int2HDBFlags(0);
880 entry_ex->entry.flags.immutable = 1;
881 entry_ex->entry.flags.invalid = 0;
882 entry_ex->entry.flags.server = 1;
883 entry_ex->entry.flags.require_preauth = 1;
885 entry_ex->entry.pw_end = NULL;
887 entry_ex->entry.max_life = NULL;
889 entry_ex->entry.max_renew = NULL;
891 entry_ex->entry.generation = NULL;
893 entry_ex->entry.etypes = malloc(sizeof(*(entry_ex->entry.etypes)));
894 if (entry_ex->entry.etypes == NULL) {
895 krb5_clear_error_message(context);
899 entry_ex->entry.etypes->len = entry_ex->entry.keys.len;
900 entry_ex->entry.etypes->val = calloc(entry_ex->entry.etypes->len, sizeof(int));
901 if (entry_ex->entry.etypes->val == NULL) {
902 krb5_clear_error_message(context);
906 for (i=0; i < entry_ex->entry.etypes->len; i++) {
907 entry_ex->entry.etypes->val[i] = entry_ex->entry.keys.val[i].key.keytype;
911 p->msg = talloc_steal(p, msg);
915 /* This doesn't free ent itself, that is for the eventual caller to do */
916 hdb_free_entry(context, entry_ex);
918 talloc_steal(kdc_db_ctx, entry_ex->ctx);
925 static krb5_error_code samba_kdc_lookup_trust(krb5_context context, struct ldb_context *ldb_ctx,
928 struct ldb_dn *realm_dn,
929 struct ldb_message **pmsg)
934 const char * const *attrs = trust_attrs;
936 struct ldb_result *res = NULL;
937 filter = talloc_asprintf(mem_ctx, "(&(objectClass=trustedDomain)(|(flatname=%s)(trustPartner=%s)))", realm, realm);
941 krb5_set_error_message(context, ret, "talloc_asprintf: out of memory");
945 lret = ldb_search(ldb_ctx, mem_ctx, &res,
946 ldb_get_default_basedn(ldb_ctx),
947 LDB_SCOPE_SUBTREE, attrs, "%s", filter);
948 if (lret != LDB_SUCCESS) {
949 DEBUG(3, ("Failed to search for %s: %s\n", filter, ldb_errstring(ldb_ctx)));
950 return HDB_ERR_NOENTRY;
951 } else if (res->count == 0 || res->count > 1) {
952 DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter, res->count));
954 return HDB_ERR_NOENTRY;
956 talloc_steal(mem_ctx, res->msgs);
957 *pmsg = res->msgs[0];
962 static krb5_error_code samba_kdc_lookup_client(krb5_context context,
963 struct samba_kdc_db_context *kdc_db_ctx,
965 krb5_const_principal principal,
967 struct ldb_dn **realm_dn,
968 struct ldb_message **msg) {
970 char *principal_string;
973 ret = krb5_unparse_name(context, principal, &principal_string);
979 nt_status = sam_get_results_principal(kdc_db_ctx->samdb,
980 mem_ctx, principal_string, attrs,
982 free(principal_string);
983 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
984 return HDB_ERR_NOENTRY;
985 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
987 } else if (!NT_STATUS_IS_OK(nt_status)) {
994 static krb5_error_code samba_kdc_fetch_client(krb5_context context,
995 struct samba_kdc_db_context *kdc_db_ctx,
997 krb5_const_principal principal,
998 hdb_entry_ex *entry_ex) {
999 struct ldb_dn *realm_dn;
1000 krb5_error_code ret;
1001 struct ldb_message *msg = NULL;
1003 ret = samba_kdc_lookup_client(context, kdc_db_ctx,
1004 mem_ctx, principal, user_attrs,
1010 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1011 principal, SAMBA_KDC_ENT_TYPE_CLIENT,
1012 realm_dn, msg, entry_ex);
1016 static krb5_error_code samba_kdc_fetch_krbtgt(krb5_context context,
1017 struct samba_kdc_db_context *kdc_db_ctx,
1018 TALLOC_CTX *mem_ctx,
1019 krb5_const_principal principal,
1020 hdb_entry_ex *entry_ex)
1022 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
1023 krb5_error_code ret;
1024 struct ldb_message *msg = NULL;
1025 struct ldb_dn *realm_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
1028 krb5_principal alloc_principal = NULL;
1029 if (principal->name.name_string.len != 2
1030 || (strcmp(principal->name.name_string.val[0], KRB5_TGS_NAME) != 0)) {
1032 return HDB_ERR_NOENTRY;
1035 /* krbtgt case. Either us or a trusted realm */
1037 if (lp_is_my_domain_or_realm(lp_ctx, principal->realm)
1038 && lp_is_my_domain_or_realm(lp_ctx, principal->name.name_string.val[1])) {
1040 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
1041 * is in our db, then direct the caller at our primary
1047 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx,
1048 &msg, realm_dn, LDB_SCOPE_SUBTREE,
1050 DSDB_SEARCH_SHOW_EXTENDED_DN,
1051 "(&(objectClass=user)(samAccountName=krbtgt))");
1052 if (lret == LDB_ERR_NO_SUCH_OBJECT) {
1053 krb5_warnx(context, "samba_kdc_fetch: could not find own KRBTGT in DB!");
1054 krb5_set_error_message(context, HDB_ERR_NOENTRY, "samba_kdc_fetch: could not find own KRBTGT in DB!");
1055 return HDB_ERR_NOENTRY;
1056 } else if (lret != LDB_SUCCESS) {
1057 krb5_warnx(context, "samba_kdc_fetch: could not find own KRBTGT in DB: %s", ldb_errstring(kdc_db_ctx->samdb));
1058 krb5_set_error_message(context, HDB_ERR_NOENTRY, "samba_kdc_fetch: could not find own KRBTGT in DB: %s", ldb_errstring(kdc_db_ctx->samdb));
1059 return HDB_ERR_NOENTRY;
1062 realm_fixed = strupper_talloc(mem_ctx, lp_realm(lp_ctx));
1065 krb5_set_error_message(context, ret, "strupper_talloc: out of memory");
1069 ret = krb5_copy_principal(context, principal, &alloc_principal);
1074 free(alloc_principal->name.name_string.val[1]);
1075 alloc_principal->name.name_string.val[1] = strdup(realm_fixed);
1076 talloc_free(realm_fixed);
1077 if (!alloc_principal->name.name_string.val[1]) {
1079 krb5_set_error_message(context, ret, "samba_kdc_fetch: strdup() failed!");
1082 principal = alloc_principal;
1084 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1085 principal, SAMBA_KDC_ENT_TYPE_KRBTGT,
1086 realm_dn, msg, entry_ex);
1088 krb5_warnx(context, "samba_kdc_fetch: self krbtgt message2entry failed");
1093 enum trust_direction direction = UNKNOWN;
1095 /* Either an inbound or outbound trust */
1097 if (strcasecmp(lp_realm(lp_ctx), principal->realm) == 0) {
1098 /* look for inbound trust */
1099 direction = INBOUND;
1100 realm = principal->name.name_string.val[1];
1103 if (strcasecmp(lp_realm(lp_ctx), principal->name.name_string.val[1]) == 0) {
1104 /* look for outbound trust */
1105 direction = OUTBOUND;
1106 realm = principal->realm;
1109 /* Trusted domains are under CN=system */
1111 ret = samba_kdc_lookup_trust(context, kdc_db_ctx->samdb,
1113 realm, realm_dn, &msg);
1116 krb5_warnx(context, "samba_kdc_fetch: could not find principal in DB");
1117 krb5_set_error_message(context, ret, "samba_kdc_fetch: could not find principal in DB");
1121 ret = samba_kdc_trust_message2entry(context, kdc_db_ctx, mem_ctx,
1122 principal, direction,
1123 realm_dn, msg, entry_ex);
1125 krb5_warnx(context, "samba_kdc_fetch: trust_message2entry failed");
1130 /* we should lookup trusted domains */
1131 return HDB_ERR_NOENTRY;
1136 static krb5_error_code samba_kdc_lookup_server(krb5_context context,
1137 struct samba_kdc_db_context *kdc_db_ctx,
1138 TALLOC_CTX *mem_ctx,
1139 krb5_const_principal principal,
1141 struct ldb_dn **realm_dn,
1142 struct ldb_message **msg)
1144 krb5_error_code ret;
1146 if (principal->name.name_string.len >= 2) {
1147 /* 'normal server' case */
1150 struct ldb_dn *user_dn;
1151 char *principal_string;
1153 ret = krb5_unparse_name_flags(context, principal,
1154 KRB5_PRINCIPAL_UNPARSE_NO_REALM,
1160 /* At this point we may find the host is known to be
1161 * in a different realm, so we should generate a
1162 * referral instead */
1163 nt_status = crack_service_principal_name(kdc_db_ctx->samdb,
1164 mem_ctx, principal_string,
1165 &user_dn, realm_dn);
1166 free(principal_string);
1168 if (!NT_STATUS_IS_OK(nt_status)) {
1169 return HDB_ERR_NOENTRY;
1172 ldb_ret = dsdb_search_one(kdc_db_ctx->samdb,
1174 msg, user_dn, LDB_SCOPE_BASE,
1175 attrs, DSDB_SEARCH_SHOW_EXTENDED_DN, "(objectClass=*)");
1176 if (ldb_ret != LDB_SUCCESS) {
1177 return HDB_ERR_NOENTRY;
1182 char *filter = NULL;
1184 /* server as client principal case, but we must not lookup userPrincipalNames */
1185 *realm_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
1186 realm = krb5_principal_get_realm(context, principal);
1188 /* TODO: Check if it is our realm, otherwise give referall */
1190 ret = krb5_unparse_name_flags(context, principal, KRB5_PRINCIPAL_UNPARSE_NO_REALM, &short_princ);
1193 krb5_set_error_message(context, ret, "samba_kdc_lookup_principal: could not parse principal");
1194 krb5_warnx(context, "samba_kdc_lookup_principal: could not parse principal");
1198 lret = dsdb_search_one(kdc_db_ctx->samdb, mem_ctx, msg,
1199 *realm_dn, LDB_SCOPE_SUBTREE,
1201 DSDB_SEARCH_SHOW_EXTENDED_DN,
1202 "(&(objectClass=user)(samAccountName=%s))",
1203 ldb_binary_encode_string(mem_ctx, short_princ));
1205 if (lret == LDB_ERR_NO_SUCH_OBJECT) {
1206 DEBUG(3, ("Failed find a entry for %s\n", filter));
1207 return HDB_ERR_NOENTRY;
1209 if (lret != LDB_SUCCESS) {
1210 DEBUG(3, ("Failed single search for for %s - %s\n",
1211 filter, ldb_errstring(kdc_db_ctx->samdb)));
1212 return HDB_ERR_NOENTRY;
1219 static krb5_error_code samba_kdc_fetch_server(krb5_context context,
1220 struct samba_kdc_db_context *kdc_db_ctx,
1221 TALLOC_CTX *mem_ctx,
1222 krb5_const_principal principal,
1223 hdb_entry_ex *entry_ex)
1225 krb5_error_code ret;
1226 struct ldb_dn *realm_dn;
1227 struct ldb_message *msg;
1229 ret = samba_kdc_lookup_server(context, kdc_db_ctx, mem_ctx, principal,
1230 server_attrs, &realm_dn, &msg);
1235 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1236 principal, SAMBA_KDC_ENT_TYPE_SERVER,
1237 realm_dn, msg, entry_ex);
1239 krb5_warnx(context, "samba_kdc_fetch: message2entry failed");
1245 krb5_error_code samba_kdc_fetch(krb5_context context,
1246 struct samba_kdc_db_context *kdc_db_ctx,
1247 krb5_const_principal principal,
1249 hdb_entry_ex *entry_ex)
1251 krb5_error_code ret = HDB_ERR_NOENTRY;
1252 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_fetch context");
1256 krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
1260 if (flags & HDB_F_GET_CLIENT) {
1261 ret = samba_kdc_fetch_client(context, kdc_db_ctx, mem_ctx, principal, entry_ex);
1262 if (ret != HDB_ERR_NOENTRY) goto done;
1264 if (flags & HDB_F_GET_SERVER) {
1265 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
1266 ret = samba_kdc_fetch_krbtgt(context, kdc_db_ctx, mem_ctx, principal, entry_ex);
1267 if (ret != HDB_ERR_NOENTRY) goto done;
1269 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
1270 ret = samba_kdc_fetch_server(context, kdc_db_ctx, mem_ctx, principal, entry_ex);
1271 if (ret != HDB_ERR_NOENTRY) goto done;
1273 if (flags & HDB_F_GET_KRBTGT) {
1274 ret = samba_kdc_fetch_krbtgt(context, kdc_db_ctx, mem_ctx, principal, entry_ex);
1275 if (ret != HDB_ERR_NOENTRY) goto done;
1279 talloc_free(mem_ctx);
1283 struct samba_kdc_seq {
1286 struct ldb_message **msgs;
1287 struct ldb_dn *realm_dn;
1290 static krb5_error_code samba_kdc_seq(krb5_context context,
1291 struct samba_kdc_db_context *kdc_db_ctx,
1292 hdb_entry_ex *entry)
1294 krb5_error_code ret;
1295 struct samba_kdc_seq *priv = kdc_db_ctx->seq_ctx;
1296 TALLOC_CTX *mem_ctx;
1297 hdb_entry_ex entry_ex;
1298 memset(&entry_ex, '\0', sizeof(entry_ex));
1301 return HDB_ERR_NOENTRY;
1304 mem_ctx = talloc_named(priv, 0, "samba_kdc_seq context");
1308 krb5_set_error_message(context, ret, "samba_kdc_seq: talloc_named() failed!");
1312 if (priv->index < priv->count) {
1313 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1314 NULL, SAMBA_KDC_ENT_TYPE_ANY,
1315 priv->realm_dn, priv->msgs[priv->index++], entry);
1317 ret = HDB_ERR_NOENTRY;
1322 kdc_db_ctx->seq_ctx = NULL;
1324 talloc_free(mem_ctx);
1330 krb5_error_code samba_kdc_firstkey(krb5_context context,
1331 struct samba_kdc_db_context *kdc_db_ctx,
1332 hdb_entry_ex *entry)
1334 struct ldb_context *ldb_ctx = kdc_db_ctx->samdb;
1335 struct samba_kdc_seq *priv = kdc_db_ctx->seq_ctx;
1337 struct ldb_result *res = NULL;
1338 krb5_error_code ret;
1339 TALLOC_CTX *mem_ctx;
1344 kdc_db_ctx->seq_ctx = NULL;
1347 priv = (struct samba_kdc_seq *) talloc(kdc_db_ctx, struct samba_kdc_seq);
1350 krb5_set_error_message(context, ret, "talloc: out of memory");
1356 priv->realm_dn = ldb_get_default_basedn(ldb_ctx);
1359 mem_ctx = talloc_named(priv, 0, "samba_kdc_firstkey context");
1363 krb5_set_error_message(context, ret, "samba_kdc_firstkey: talloc_named() failed!");
1367 ret = krb5_get_default_realm(context, &realm);
1373 lret = ldb_search(ldb_ctx, priv, &res,
1374 priv->realm_dn, LDB_SCOPE_SUBTREE, user_attrs,
1375 "(objectClass=user)");
1377 if (lret != LDB_SUCCESS) {
1379 return HDB_ERR_NOENTRY;
1382 priv->count = res->count;
1383 priv->msgs = talloc_steal(priv, res->msgs);
1386 kdc_db_ctx->seq_ctx = priv;
1388 ret = samba_kdc_seq(context, kdc_db_ctx, entry);
1392 kdc_db_ctx->seq_ctx = NULL;
1394 talloc_free(mem_ctx);
1399 krb5_error_code samba_kdc_nextkey(krb5_context context,
1400 struct samba_kdc_db_context *kdc_db_ctx,
1401 hdb_entry_ex *entry)
1403 return samba_kdc_seq(context, kdc_db_ctx, entry);
1406 /* Check if a given entry may delegate or do s4u2self to this target principal
1408 * This is currently a very nasty hack - allowing only delegation to itself.
1410 * This is shared between the constrained delegation and S4U2Self code.
1413 samba_kdc_check_identical_client_and_server(krb5_context context,
1414 struct samba_kdc_db_context *kdc_db_ctx,
1415 hdb_entry_ex *entry,
1416 krb5_const_principal target_principal)
1418 krb5_error_code ret;
1419 krb5_principal enterprise_prinicpal = NULL;
1420 struct ldb_dn *realm_dn;
1421 struct ldb_message *msg;
1422 struct dom_sid *orig_sid;
1423 struct dom_sid *target_sid;
1424 struct samba_kdc_entry *p = talloc_get_type(entry->ctx, struct samba_kdc_entry);
1425 const char *delegation_check_attrs[] = {
1429 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_constrained_delegation");
1433 krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
1437 if (target_principal->name.name_type == KRB5_NT_ENTERPRISE_PRINCIPAL) {
1438 /* Need to reparse the enterprise principal to find the real target */
1439 if (target_principal->name.name_string.len != 1) {
1440 ret = KRB5_PARSE_MALFORMED;
1441 krb5_set_error_message(context, ret, "samba_kdc_check_constrained_delegation: request for delegation to enterprise principal with wrong (%d) number of components",
1442 target_principal->name.name_string.len);
1443 talloc_free(mem_ctx);
1446 ret = krb5_parse_name(context, target_principal->name.name_string.val[0],
1447 &enterprise_prinicpal);
1449 talloc_free(mem_ctx);
1452 target_principal = enterprise_prinicpal;
1455 ret = samba_kdc_lookup_server(context, kdc_db_ctx, mem_ctx, target_principal,
1456 delegation_check_attrs, &realm_dn, &msg);
1458 krb5_free_principal(context, enterprise_prinicpal);
1461 talloc_free(mem_ctx);
1465 orig_sid = samdb_result_dom_sid(mem_ctx, p->msg, "objectSid");
1466 target_sid = samdb_result_dom_sid(mem_ctx, msg, "objectSid");
1468 /* Allow delegation to the same principal, even if by a different
1469 * name. The easy and safe way to prove this is by SID
1471 if (!(orig_sid && target_sid && dom_sid_equal(orig_sid, target_sid))) {
1472 talloc_free(mem_ctx);
1473 return KRB5KDC_ERR_BADOPTION;
1476 talloc_free(mem_ctx);
1480 /* Certificates printed by a the Certificate Authority might have a
1481 * slightly different form of the user principal name to that in the
1482 * database. Allow a mismatch where they both refer to the same
1486 samba_kdc_check_pkinit_ms_upn_match(krb5_context context,
1487 struct samba_kdc_db_context *kdc_db_ctx,
1488 hdb_entry_ex *entry,
1489 krb5_const_principal certificate_principal)
1491 krb5_error_code ret;
1492 struct ldb_dn *realm_dn;
1493 struct ldb_message *msg;
1494 struct dom_sid *orig_sid;
1495 struct dom_sid *target_sid;
1496 struct samba_kdc_entry *p = talloc_get_type(entry->ctx, struct samba_kdc_entry);
1497 const char *ms_upn_check_attrs[] = {
1501 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_pkinit_ms_upn_match");
1505 krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
1509 ret = samba_kdc_lookup_client(context, kdc_db_ctx,
1510 mem_ctx, certificate_principal,
1511 ms_upn_check_attrs, &realm_dn, &msg);
1514 talloc_free(mem_ctx);
1518 orig_sid = samdb_result_dom_sid(mem_ctx, p->msg, "objectSid");
1519 target_sid = samdb_result_dom_sid(mem_ctx, msg, "objectSid");
1521 /* Consider these to be the same principal, even if by a different
1522 * name. The easy and safe way to prove this is by SID
1524 if (!(orig_sid && target_sid && dom_sid_equal(orig_sid, target_sid))) {
1525 talloc_free(mem_ctx);
1526 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH;
1529 talloc_free(mem_ctx);