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 "librpc/ndr/libndr.h"
36 #include "librpc/gen_ndr/ndr_drsblobs.h"
37 #include "librpc/gen_ndr/lsa.h"
38 #include "libcli/auth/libcli_auth.h"
39 #include "param/param.h"
40 #include "../lib/crypto/md4.h"
41 #include "system/kerberos.h"
43 #include "kdc/samba_kdc.h"
44 #include "kdc/db-glue.h"
46 enum samba_kdc_ent_type
47 { SAMBA_KDC_ENT_TYPE_CLIENT, SAMBA_KDC_ENT_TYPE_SERVER,
48 SAMBA_KDC_ENT_TYPE_KRBTGT, SAMBA_KDC_ENT_TYPE_TRUST, SAMBA_KDC_ENT_TYPE_ANY };
50 enum trust_direction {
52 INBOUND = LSA_TRUST_DIRECTION_INBOUND,
53 OUTBOUND = LSA_TRUST_DIRECTION_OUTBOUND
56 static const char *trust_attrs[] = {
61 "msDS-SupportedEncryptionTypes",
68 static KerberosTime ldb_msg_find_krb5time_ldap_time(struct ldb_message *msg, const char *attr, KerberosTime default_val)
74 gentime = ldb_msg_find_attr_as_string(msg, attr, NULL);
78 tmp = strptime(gentime, "%Y%m%d%H%M%SZ", &tm);
86 static HDBFlags uf2HDBFlags(krb5_context context, int userAccountControl, enum samba_kdc_ent_type ent_type)
88 HDBFlags flags = int2HDBFlags(0);
90 /* we don't allow kadmin deletes */
93 /* mark the principal as invalid to start with */
98 /* All accounts are servers, but this may be disabled again in the caller */
101 /* Account types - clear the invalid bit if it turns out to be valid */
102 if (userAccountControl & UF_NORMAL_ACCOUNT) {
103 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
109 if (userAccountControl & UF_INTERDOMAIN_TRUST_ACCOUNT) {
110 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
115 if (userAccountControl & UF_WORKSTATION_TRUST_ACCOUNT) {
116 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
121 if (userAccountControl & UF_SERVER_TRUST_ACCOUNT) {
122 if (ent_type == SAMBA_KDC_ENT_TYPE_CLIENT || ent_type == SAMBA_KDC_ENT_TYPE_ANY) {
128 /* Not permitted to act as a client if disabled */
129 if (userAccountControl & UF_ACCOUNTDISABLE) {
132 if (userAccountControl & UF_LOCKOUT) {
136 if (userAccountControl & UF_PASSWORD_NOTREQD) {
141 UF_PASSWORD_CANT_CHANGE and UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED are irrelevent
143 if (userAccountControl & UF_TEMP_DUPLICATE_ACCOUNT) {
147 /* UF_DONT_EXPIRE_PASSWD and UF_USE_DES_KEY_ONLY handled in samba_kdc_message2entry() */
150 if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
154 if (userAccountControl & UF_SMARTCARD_REQUIRED) {
155 flags.require_hwauth = 1;
157 if (userAccountControl & UF_TRUSTED_FOR_DELEGATION) {
158 flags.ok_as_delegate = 1;
160 if (!(userAccountControl & UF_NOT_DELEGATED)) {
161 flags.forwardable = 1;
165 if (userAccountControl & UF_DONT_REQUIRE_PREAUTH) {
166 flags.require_preauth = 0;
168 flags.require_preauth = 1;
174 static int samba_kdc_entry_destructor(struct samba_kdc_entry *p)
176 hdb_entry_ex *entry_ex = p->entry_ex;
177 free_hdb_entry(&entry_ex->entry);
181 static void samba_kdc_free_entry(krb5_context context, hdb_entry_ex *entry_ex)
183 /* this function is called only from hdb_free_entry().
184 * Make sure we neutralize the destructor or we will
185 * get a double free later when hdb_free_entry() will
186 * try to call free_hdb_entry() */
187 talloc_set_destructor(entry_ex->ctx, NULL);
189 /* now proceed to free the talloc part */
190 talloc_free(entry_ex->ctx);
193 static krb5_error_code samba_kdc_message2entry_keys(krb5_context context,
194 struct smb_iconv_convenience *iconv_convenience,
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 uint32_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, iconv_convenience, &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, iconv_convenience, &_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->kdc_db_ctx->ic_ctx, p,
704 msg, userAccountControl, entry_ex);
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;
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 i, ret, trust_direction_flags;
764 p = talloc(mem_ctx, struct samba_kdc_entry);
770 p->kdc_db_ctx = kdc_db_ctx;
771 p->entry_ex = entry_ex;
772 p->realm_dn = realm_dn;
774 talloc_set_destructor(p, samba_kdc_entry_destructor);
776 /* make sure we do not have bogus data in there */
777 memset(&entry_ex->entry, 0, sizeof(hdb_entry));
780 entry_ex->free_entry = samba_kdc_free_entry;
782 /* use 'whenCreated' */
783 entry_ex->entry.created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
785 entry_ex->entry.created_by.principal = NULL;
787 entry_ex->entry.valid_start = NULL;
789 trust_direction_flags = ldb_msg_find_attr_as_int(msg, "trustDirection", 0);
791 if (direction == INBOUND) {
792 realm = strupper_talloc(mem_ctx, lp_realm(lp_ctx));
793 password_val = ldb_msg_find_ldb_val(msg, "trustAuthIncoming");
795 } else { /* OUTBOUND */
796 dnsdomain = ldb_msg_find_attr_as_string(msg, "trustPartner", NULL);
797 realm = strupper_talloc(mem_ctx, dnsdomain);
798 password_val = ldb_msg_find_ldb_val(msg, "trustAuthOutgoing");
801 if (!password_val || !(trust_direction_flags & direction)) {
806 ndr_err = ndr_pull_struct_blob(password_val, mem_ctx, p->kdc_db_ctx->ic_ctx, &password_blob,
807 (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
808 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
813 entry_ex->entry.kvno = -1;
814 for (i=0; i < password_blob.count; i++) {
815 if (password_blob.current->array[i].AuthType == TRUST_AUTH_TYPE_VERSION) {
816 entry_ex->entry.kvno = password_blob.current->array[i].AuthInfo.version.version;
820 for (i=0; i < password_blob.count; i++) {
821 if (password_blob.current->array[i].AuthType == TRUST_AUTH_TYPE_CLEAR) {
822 password_utf16 = data_blob_const(password_blob.current->array[i].AuthInfo.clear.password,
823 password_blob.current->array[i].AuthInfo.clear.size);
824 /* In the future, generate all sorts of
825 * hashes, but for now we can't safely convert
826 * the random strings windows uses into
829 /* but as it is utf16 already, we can get the NT password/arcfour-hmac-md5 key */
830 mdfour(password_hash.hash, password_utf16.data, password_utf16.length);
832 } else if (password_blob.current->array[i].AuthType == TRUST_AUTH_TYPE_NT4OWF) {
833 password_hash = password_blob.current->array[i].AuthInfo.nt4owf.password;
838 if (i < password_blob.count) {
840 /* Must have found a cleartext or MD4 password */
841 entry_ex->entry.keys.val = calloc(1, sizeof(Key));
844 key.salt = NULL; /* No salt for this enc type */
846 if (entry_ex->entry.keys.val == NULL) {
851 ret = krb5_keyblock_init(context,
852 ENCTYPE_ARCFOUR_HMAC,
853 password_hash.hash, sizeof(password_hash.hash),
856 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
857 entry_ex->entry.keys.len++;
860 entry_ex->entry.principal = malloc(sizeof(*(entry_ex->entry.principal)));
862 ret = copy_Principal(principal, entry_ex->entry.principal);
864 krb5_clear_error_message(context);
868 /* While we have copied the client principal, tests
869 * show that Win2k3 returns the 'corrected' realm, not
870 * the client-specified realm. This code attempts to
871 * replace the client principal's realm with the one
872 * we determine from our records */
874 krb5_principal_set_realm(context, entry_ex->entry.principal, realm);
875 entry_ex->entry.flags = int2HDBFlags(0);
876 entry_ex->entry.flags.immutable = 1;
877 entry_ex->entry.flags.invalid = 0;
878 entry_ex->entry.flags.server = 1;
879 entry_ex->entry.flags.require_preauth = 1;
881 entry_ex->entry.pw_end = NULL;
883 entry_ex->entry.max_life = NULL;
885 entry_ex->entry.max_renew = NULL;
887 entry_ex->entry.generation = NULL;
889 entry_ex->entry.etypes = malloc(sizeof(*(entry_ex->entry.etypes)));
890 if (entry_ex->entry.etypes == NULL) {
891 krb5_clear_error_message(context);
895 entry_ex->entry.etypes->len = entry_ex->entry.keys.len;
896 entry_ex->entry.etypes->val = calloc(entry_ex->entry.etypes->len, sizeof(int));
897 if (entry_ex->entry.etypes->val == NULL) {
898 krb5_clear_error_message(context);
902 for (i=0; i < entry_ex->entry.etypes->len; i++) {
903 entry_ex->entry.etypes->val[i] = entry_ex->entry.keys.val[i].key.keytype;
907 p->msg = talloc_steal(p, msg);
911 /* This doesn't free ent itself, that is for the eventual caller to do */
912 hdb_free_entry(context, entry_ex);
914 talloc_steal(kdc_db_ctx, entry_ex->ctx);
921 static krb5_error_code samba_kdc_lookup_trust(krb5_context context, struct ldb_context *ldb_ctx,
924 struct ldb_dn *realm_dn,
925 struct ldb_message **pmsg)
930 const char * const *attrs = trust_attrs;
932 struct ldb_result *res = NULL;
933 filter = talloc_asprintf(mem_ctx, "(&(objectClass=trustedDomain)(|(flatname=%s)(trustPartner=%s)))", realm, realm);
937 krb5_set_error_message(context, ret, "talloc_asprintf: out of memory");
941 lret = ldb_search(ldb_ctx, mem_ctx, &res,
942 ldb_get_default_basedn(ldb_ctx),
943 LDB_SCOPE_SUBTREE, attrs, "%s", filter);
944 if (lret != LDB_SUCCESS) {
945 DEBUG(3, ("Failed to search for %s: %s\n", filter, ldb_errstring(ldb_ctx)));
946 return HDB_ERR_NOENTRY;
947 } else if (res->count == 0 || res->count > 1) {
948 DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter, res->count));
950 return HDB_ERR_NOENTRY;
952 talloc_steal(mem_ctx, res->msgs);
953 *pmsg = res->msgs[0];
958 static krb5_error_code samba_kdc_lookup_client(krb5_context context,
959 struct samba_kdc_db_context *kdc_db_ctx,
961 krb5_const_principal principal,
963 struct ldb_dn **realm_dn,
964 struct ldb_message **msg) {
966 char *principal_string;
969 ret = krb5_unparse_name(context, principal, &principal_string);
975 nt_status = sam_get_results_principal(kdc_db_ctx->samdb,
976 mem_ctx, principal_string, attrs,
978 free(principal_string);
979 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
980 return HDB_ERR_NOENTRY;
981 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
983 } else if (!NT_STATUS_IS_OK(nt_status)) {
990 static krb5_error_code samba_kdc_fetch_client(krb5_context context,
991 struct samba_kdc_db_context *kdc_db_ctx,
993 krb5_const_principal principal,
994 hdb_entry_ex *entry_ex) {
995 struct ldb_dn *realm_dn;
997 struct ldb_message *msg = NULL;
999 ret = samba_kdc_lookup_client(context, kdc_db_ctx,
1000 mem_ctx, principal, user_attrs,
1006 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1007 principal, SAMBA_KDC_ENT_TYPE_CLIENT,
1008 realm_dn, msg, entry_ex);
1012 static krb5_error_code samba_kdc_fetch_krbtgt(krb5_context context,
1013 struct samba_kdc_db_context *kdc_db_ctx,
1014 TALLOC_CTX *mem_ctx,
1015 krb5_const_principal principal,
1016 hdb_entry_ex *entry_ex)
1018 struct loadparm_context *lp_ctx = kdc_db_ctx->lp_ctx;
1019 krb5_error_code ret;
1020 struct ldb_message *msg = NULL;
1021 struct ldb_dn *realm_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
1024 krb5_principal alloc_principal = NULL;
1025 if (principal->name.name_string.len != 2
1026 || (strcmp(principal->name.name_string.val[0], KRB5_TGS_NAME) != 0)) {
1028 return HDB_ERR_NOENTRY;
1031 /* krbtgt case. Either us or a trusted realm */
1033 if (lp_is_my_domain_or_realm(lp_ctx, principal->realm)
1034 && lp_is_my_domain_or_realm(lp_ctx, principal->name.name_string.val[1])) {
1036 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
1037 * is in our db, then direct the caller at our primary
1043 lret = gendb_search_single_extended_dn(kdc_db_ctx->samdb, mem_ctx,
1044 realm_dn, LDB_SCOPE_SUBTREE,
1046 "(&(objectClass=user)(samAccountName=krbtgt))");
1047 if (lret == LDB_ERR_NO_SUCH_OBJECT) {
1048 krb5_warnx(context, "samba_kdc_fetch: could not find own KRBTGT in DB!");
1049 krb5_set_error_message(context, HDB_ERR_NOENTRY, "samba_kdc_fetch: could not find own KRBTGT in DB!");
1050 return HDB_ERR_NOENTRY;
1051 } else if (lret != LDB_SUCCESS) {
1052 krb5_warnx(context, "samba_kdc_fetch: could not find own KRBTGT in DB: %s", ldb_errstring(kdc_db_ctx->samdb));
1053 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));
1054 return HDB_ERR_NOENTRY;
1057 realm_fixed = strupper_talloc(mem_ctx, lp_realm(lp_ctx));
1060 krb5_set_error_message(context, ret, "strupper_talloc: out of memory");
1064 ret = krb5_copy_principal(context, principal, &alloc_principal);
1069 free(alloc_principal->name.name_string.val[1]);
1070 alloc_principal->name.name_string.val[1] = strdup(realm_fixed);
1071 talloc_free(realm_fixed);
1072 if (!alloc_principal->name.name_string.val[1]) {
1074 krb5_set_error_message(context, ret, "samba_kdc_fetch: strdup() failed!");
1077 principal = alloc_principal;
1079 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1080 principal, SAMBA_KDC_ENT_TYPE_KRBTGT,
1081 realm_dn, msg, entry_ex);
1083 krb5_warnx(context, "samba_kdc_fetch: self krbtgt message2entry failed");
1088 enum trust_direction direction = UNKNOWN;
1090 /* Either an inbound or outbound trust */
1092 if (strcasecmp(lp_realm(lp_ctx), principal->realm) == 0) {
1093 /* look for inbound trust */
1094 direction = INBOUND;
1095 realm = principal->name.name_string.val[1];
1098 if (strcasecmp(lp_realm(lp_ctx), principal->name.name_string.val[1]) == 0) {
1099 /* look for outbound trust */
1100 direction = OUTBOUND;
1101 realm = principal->realm;
1104 /* Trusted domains are under CN=system */
1106 ret = samba_kdc_lookup_trust(context, kdc_db_ctx->samdb,
1108 realm, realm_dn, &msg);
1111 krb5_warnx(context, "samba_kdc_fetch: could not find principal in DB");
1112 krb5_set_error_message(context, ret, "samba_kdc_fetch: could not find principal in DB");
1116 ret = samba_kdc_trust_message2entry(context, kdc_db_ctx, mem_ctx,
1117 principal, direction,
1118 realm_dn, msg, entry_ex);
1120 krb5_warnx(context, "samba_kdc_fetch: trust_message2entry failed");
1125 /* we should lookup trusted domains */
1126 return HDB_ERR_NOENTRY;
1131 static krb5_error_code samba_kdc_lookup_server(krb5_context context,
1132 struct samba_kdc_db_context *kdc_db_ctx,
1133 TALLOC_CTX *mem_ctx,
1134 krb5_const_principal principal,
1136 struct ldb_dn **realm_dn,
1137 struct ldb_message **msg)
1139 krb5_error_code ret;
1141 if (principal->name.name_string.len >= 2) {
1142 /* 'normal server' case */
1145 struct ldb_dn *user_dn;
1146 char *principal_string;
1148 ret = krb5_unparse_name_flags(context, principal,
1149 KRB5_PRINCIPAL_UNPARSE_NO_REALM,
1155 /* At this point we may find the host is known to be
1156 * in a different realm, so we should generate a
1157 * referral instead */
1158 nt_status = crack_service_principal_name(kdc_db_ctx->samdb,
1159 mem_ctx, principal_string,
1160 &user_dn, realm_dn);
1161 free(principal_string);
1163 if (!NT_STATUS_IS_OK(nt_status)) {
1164 return HDB_ERR_NOENTRY;
1167 ldb_ret = gendb_search_single_extended_dn(kdc_db_ctx->samdb,
1169 user_dn, LDB_SCOPE_BASE,
1172 if (ldb_ret != LDB_SUCCESS) {
1173 return HDB_ERR_NOENTRY;
1178 char *filter = NULL;
1180 /* server as client principal case, but we must not lookup userPrincipalNames */
1181 *realm_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
1182 realm = krb5_principal_get_realm(context, principal);
1184 /* TODO: Check if it is our realm, otherwise give referall */
1186 ret = krb5_unparse_name_flags(context, principal, KRB5_PRINCIPAL_UNPARSE_NO_REALM, &short_princ);
1189 krb5_set_error_message(context, ret, "samba_kdc_lookup_principal: could not parse principal");
1190 krb5_warnx(context, "samba_kdc_lookup_principal: could not parse principal");
1194 lret = gendb_search_single_extended_dn(kdc_db_ctx->samdb, mem_ctx,
1195 *realm_dn, LDB_SCOPE_SUBTREE,
1196 msg, attrs, "(&(objectClass=user)(samAccountName=%s))",
1197 ldb_binary_encode_string(mem_ctx, short_princ));
1199 if (lret == LDB_ERR_NO_SUCH_OBJECT) {
1200 DEBUG(3, ("Failed find a entry for %s\n", filter));
1201 return HDB_ERR_NOENTRY;
1203 if (lret != LDB_SUCCESS) {
1204 DEBUG(3, ("Failed single search for for %s - %s\n",
1205 filter, ldb_errstring(kdc_db_ctx->samdb)));
1206 return HDB_ERR_NOENTRY;
1213 static krb5_error_code samba_kdc_fetch_server(krb5_context context,
1214 struct samba_kdc_db_context *kdc_db_ctx,
1215 TALLOC_CTX *mem_ctx,
1216 krb5_const_principal principal,
1217 hdb_entry_ex *entry_ex)
1219 krb5_error_code ret;
1220 struct ldb_dn *realm_dn;
1221 struct ldb_message *msg;
1223 ret = samba_kdc_lookup_server(context, kdc_db_ctx, mem_ctx, principal,
1224 server_attrs, &realm_dn, &msg);
1229 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1230 principal, SAMBA_KDC_ENT_TYPE_SERVER,
1231 realm_dn, msg, entry_ex);
1233 krb5_warnx(context, "samba_kdc_fetch: message2entry failed");
1239 krb5_error_code samba_kdc_fetch(krb5_context context,
1240 struct samba_kdc_db_context *kdc_db_ctx,
1241 krb5_const_principal principal,
1243 hdb_entry_ex *entry_ex)
1245 krb5_error_code ret = HDB_ERR_NOENTRY;
1246 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_fetch context");
1250 krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
1254 if (flags & HDB_F_GET_CLIENT) {
1255 ret = samba_kdc_fetch_client(context, kdc_db_ctx, mem_ctx, principal, entry_ex);
1256 if (ret != HDB_ERR_NOENTRY) goto done;
1258 if (flags & HDB_F_GET_SERVER) {
1259 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
1260 ret = samba_kdc_fetch_krbtgt(context, kdc_db_ctx, mem_ctx, principal, entry_ex);
1261 if (ret != HDB_ERR_NOENTRY) goto done;
1263 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
1264 ret = samba_kdc_fetch_server(context, kdc_db_ctx, mem_ctx, principal, entry_ex);
1265 if (ret != HDB_ERR_NOENTRY) goto done;
1267 if (flags & HDB_F_GET_KRBTGT) {
1268 ret = samba_kdc_fetch_krbtgt(context, kdc_db_ctx, mem_ctx, principal, entry_ex);
1269 if (ret != HDB_ERR_NOENTRY) goto done;
1273 talloc_free(mem_ctx);
1277 struct samba_kdc_seq {
1280 struct ldb_message **msgs;
1281 struct ldb_dn *realm_dn;
1284 static krb5_error_code samba_kdc_seq(krb5_context context,
1285 struct samba_kdc_db_context *kdc_db_ctx,
1286 hdb_entry_ex *entry)
1288 krb5_error_code ret;
1289 struct samba_kdc_seq *priv = kdc_db_ctx->seq_ctx;
1290 TALLOC_CTX *mem_ctx;
1291 hdb_entry_ex entry_ex;
1292 memset(&entry_ex, '\0', sizeof(entry_ex));
1295 return HDB_ERR_NOENTRY;
1298 mem_ctx = talloc_named(priv, 0, "samba_kdc_seq context");
1302 krb5_set_error_message(context, ret, "samba_kdc_seq: talloc_named() failed!");
1306 if (priv->index < priv->count) {
1307 ret = samba_kdc_message2entry(context, kdc_db_ctx, mem_ctx,
1308 NULL, SAMBA_KDC_ENT_TYPE_ANY,
1309 priv->realm_dn, priv->msgs[priv->index++], entry);
1311 ret = HDB_ERR_NOENTRY;
1316 kdc_db_ctx->seq_ctx = NULL;
1318 talloc_free(mem_ctx);
1324 krb5_error_code samba_kdc_firstkey(krb5_context context,
1325 struct samba_kdc_db_context *kdc_db_ctx,
1326 hdb_entry_ex *entry)
1328 struct ldb_context *ldb_ctx = kdc_db_ctx->samdb;
1329 struct samba_kdc_seq *priv = kdc_db_ctx->seq_ctx;
1331 struct ldb_result *res = NULL;
1332 krb5_error_code ret;
1333 TALLOC_CTX *mem_ctx;
1338 kdc_db_ctx->seq_ctx = NULL;
1341 priv = (struct samba_kdc_seq *) talloc(kdc_db_ctx, struct samba_kdc_seq);
1344 krb5_set_error_message(context, ret, "talloc: out of memory");
1350 priv->realm_dn = ldb_get_default_basedn(ldb_ctx);
1353 mem_ctx = talloc_named(priv, 0, "samba_kdc_firstkey context");
1357 krb5_set_error_message(context, ret, "samba_kdc_firstkey: talloc_named() failed!");
1361 ret = krb5_get_default_realm(context, &realm);
1367 lret = ldb_search(ldb_ctx, priv, &res,
1368 priv->realm_dn, LDB_SCOPE_SUBTREE, user_attrs,
1369 "(objectClass=user)");
1371 if (lret != LDB_SUCCESS) {
1373 return HDB_ERR_NOENTRY;
1376 priv->count = res->count;
1377 priv->msgs = talloc_steal(priv, res->msgs);
1380 kdc_db_ctx->seq_ctx = priv;
1382 ret = samba_kdc_seq(context, kdc_db_ctx, entry);
1386 kdc_db_ctx->seq_ctx = NULL;
1388 talloc_free(mem_ctx);
1393 krb5_error_code samba_kdc_nextkey(krb5_context context,
1394 struct samba_kdc_db_context *kdc_db_ctx,
1395 hdb_entry_ex *entry)
1397 return samba_kdc_seq(context, kdc_db_ctx, entry);
1400 /* Check if a given entry may delegate to this target principal
1402 * This is currently a very nasty hack - allowing only delegation to itself.
1405 samba_kdc_check_constrained_delegation(krb5_context context,
1406 struct samba_kdc_db_context *kdc_db_ctx,
1407 hdb_entry_ex *entry,
1408 krb5_const_principal target_principal)
1410 krb5_error_code ret;
1411 krb5_principal enterprise_prinicpal = NULL;
1412 struct ldb_dn *realm_dn;
1413 struct ldb_message *msg;
1414 struct dom_sid *orig_sid;
1415 struct dom_sid *target_sid;
1416 struct samba_kdc_entry *p = talloc_get_type(entry->ctx, struct samba_kdc_entry);
1417 const char *delegation_check_attrs[] = {
1421 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_constrained_delegation");
1425 krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
1429 if (target_principal->name.name_type == KRB5_NT_ENTERPRISE_PRINCIPAL) {
1430 /* Need to reparse the enterprise principal to find the real target */
1431 if (target_principal->name.name_string.len != 1) {
1432 ret = KRB5_PARSE_MALFORMED;
1433 krb5_set_error_message(context, ret, "samba_kdc_check_constrained_delegation: request for delegation to enterprise principal with wrong (%d) number of components",
1434 target_principal->name.name_string.len);
1435 talloc_free(mem_ctx);
1438 ret = krb5_parse_name(context, target_principal->name.name_string.val[0],
1439 &enterprise_prinicpal);
1441 talloc_free(mem_ctx);
1444 target_principal = enterprise_prinicpal;
1447 ret = samba_kdc_lookup_server(context, kdc_db_ctx, mem_ctx, target_principal,
1448 delegation_check_attrs, &realm_dn, &msg);
1450 krb5_free_principal(context, enterprise_prinicpal);
1453 talloc_free(mem_ctx);
1457 orig_sid = samdb_result_dom_sid(mem_ctx, p->msg, "objectSid");
1458 target_sid = samdb_result_dom_sid(mem_ctx, msg, "objectSid");
1460 /* Allow delegation to the same principal, even if by a different
1461 * name. The easy and safe way to prove this is by SID
1463 if (!(orig_sid && target_sid && dom_sid_equal(orig_sid, target_sid))) {
1464 talloc_free(mem_ctx);
1465 return KRB5KDC_ERR_BADOPTION;
1468 talloc_free(mem_ctx);
1472 /* Certificates printed by a the Certificate Authority might have a
1473 * slightly different form of the user principal name to that in the
1474 * database. Allow a mismatch where they both refer to the same
1478 samba_kdc_check_pkinit_ms_upn_match(krb5_context context,
1479 struct samba_kdc_db_context *kdc_db_ctx,
1480 hdb_entry_ex *entry,
1481 krb5_const_principal certificate_principal)
1483 krb5_error_code ret;
1484 struct ldb_dn *realm_dn;
1485 struct ldb_message *msg;
1486 struct dom_sid *orig_sid;
1487 struct dom_sid *target_sid;
1488 struct samba_kdc_entry *p = talloc_get_type(entry->ctx, struct samba_kdc_entry);
1489 const char *ms_upn_check_attrs[] = {
1493 TALLOC_CTX *mem_ctx = talloc_named(kdc_db_ctx, 0, "samba_kdc_check_pkinit_ms_upn_match");
1497 krb5_set_error_message(context, ret, "samba_kdc_fetch: talloc_named() failed!");
1501 ret = samba_kdc_lookup_client(context, kdc_db_ctx,
1502 mem_ctx, certificate_principal,
1503 ms_upn_check_attrs, &realm_dn, &msg);
1506 talloc_free(mem_ctx);
1510 orig_sid = samdb_result_dom_sid(mem_ctx, p->msg, "objectSid");
1511 target_sid = samdb_result_dom_sid(mem_ctx, msg, "objectSid");
1513 /* Consider these to be the same principal, even if by a different
1514 * name. The easy and safe way to prove this is by SID
1516 if (!(orig_sid && target_sid && dom_sid_equal(orig_sid, target_sid))) {
1517 talloc_free(mem_ctx);
1518 return KRB5_KDC_ERR_CLIENT_NAME_MISMATCH;
1521 talloc_free(mem_ctx);