Pass struct smb_request to file_new
[Samba/gebeck_regimport.git] / source4 / kdc / hdb-samba4.c
blob3a1379bee448d9d45a0f4a984c07a7ae466be198
1 /*
2 * Copyright (c) 1999-2001, 2003, PADL Software Pty Ltd.
3 * Copyright (c) 2004, Andrew Bartlett <abartlet@samba.org>.
4 * Copyright (c) 2004, Stefan Metzmacher <metze@samba.org>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of PADL Software nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY PADL SOFTWARE AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL PADL SOFTWARE OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #include "includes.h"
36 #include "system/time.h"
37 #include "dsdb/common/flags.h"
38 #include "lib/ldb/include/ldb.h"
39 #include "lib/ldb/include/ldb_errors.h"
40 #include "librpc/gen_ndr/netlogon.h"
41 #include "auth/auth.h"
42 #include "auth/credentials/credentials.h"
43 #include "auth/auth_sam.h"
44 #include "../lib/util/util_ldb.h"
45 #include "dsdb/samdb/samdb.h"
46 #include "librpc/ndr/libndr.h"
47 #include "librpc/gen_ndr/ndr_drsblobs.h"
48 #include "librpc/gen_ndr/lsa.h"
49 #include "libcli/auth/libcli_auth.h"
50 #include "param/param.h"
51 #include "events/events.h"
52 #include "kdc/kdc.h"
53 #include "../lib/crypto/md4.h"
55 enum hdb_ldb_ent_type
56 { HDB_SAMBA4_ENT_TYPE_CLIENT, HDB_SAMBA4_ENT_TYPE_SERVER,
57 HDB_SAMBA4_ENT_TYPE_KRBTGT, HDB_SAMBA4_ENT_TYPE_TRUST, HDB_SAMBA4_ENT_TYPE_ANY };
59 enum trust_direction {
60 UNKNOWN = 0,
61 INBOUND = LSA_TRUST_DIRECTION_INBOUND,
62 OUTBOUND = LSA_TRUST_DIRECTION_OUTBOUND
65 static const char *realm_ref_attrs[] = {
66 "nCName",
67 "dnsRoot",
68 NULL
71 static const char *trust_attrs[] = {
72 "trustPartner",
73 "trustAuthIncoming",
74 "trustAuthOutgoing",
75 "whenCreated",
76 "msDS-SupportedEncryptionTypes",
77 "trustAttributes",
78 "trustDirection",
79 "trustType",
80 NULL
83 static KerberosTime ldb_msg_find_krb5time_ldap_time(struct ldb_message *msg, const char *attr, KerberosTime default_val)
85 const char *tmp;
86 const char *gentime;
87 struct tm tm;
89 gentime = ldb_msg_find_attr_as_string(msg, attr, NULL);
90 if (!gentime)
91 return default_val;
93 tmp = strptime(gentime, "%Y%m%d%H%M%SZ", &tm);
94 if (tmp == NULL) {
95 return default_val;
98 return timegm(&tm);
101 static HDBFlags uf2HDBFlags(krb5_context context, int userAccountControl, enum hdb_ldb_ent_type ent_type)
103 HDBFlags flags = int2HDBFlags(0);
105 /* we don't allow kadmin deletes */
106 flags.immutable = 1;
108 /* mark the principal as invalid to start with */
109 flags.invalid = 1;
111 flags.renewable = 1;
113 /* All accounts are servers, but this may be disabled again in the caller */
114 flags.server = 1;
116 /* Account types - clear the invalid bit if it turns out to be valid */
117 if (userAccountControl & UF_NORMAL_ACCOUNT) {
118 if (ent_type == HDB_SAMBA4_ENT_TYPE_CLIENT || ent_type == HDB_SAMBA4_ENT_TYPE_ANY) {
119 flags.client = 1;
121 flags.invalid = 0;
124 if (userAccountControl & UF_INTERDOMAIN_TRUST_ACCOUNT) {
125 if (ent_type == HDB_SAMBA4_ENT_TYPE_CLIENT || ent_type == HDB_SAMBA4_ENT_TYPE_ANY) {
126 flags.client = 1;
128 flags.invalid = 0;
130 if (userAccountControl & UF_WORKSTATION_TRUST_ACCOUNT) {
131 if (ent_type == HDB_SAMBA4_ENT_TYPE_CLIENT || ent_type == HDB_SAMBA4_ENT_TYPE_ANY) {
132 flags.client = 1;
134 flags.invalid = 0;
136 if (userAccountControl & UF_SERVER_TRUST_ACCOUNT) {
137 if (ent_type == HDB_SAMBA4_ENT_TYPE_CLIENT || ent_type == HDB_SAMBA4_ENT_TYPE_ANY) {
138 flags.client = 1;
140 flags.invalid = 0;
143 /* Not permitted to act as a client if disabled */
144 if (userAccountControl & UF_ACCOUNTDISABLE) {
145 flags.client = 0;
147 if (userAccountControl & UF_LOCKOUT) {
148 flags.invalid = 1;
151 if (userAccountControl & UF_PASSWORD_NOTREQD) {
152 flags.invalid = 1;
156 UF_PASSWORD_CANT_CHANGE and UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED are irrelevent
158 if (userAccountControl & UF_TEMP_DUPLICATE_ACCOUNT) {
159 flags.invalid = 1;
162 /* UF_DONT_EXPIRE_PASSWD and UF_USE_DES_KEY_ONLY handled in LDB_message2entry() */
165 if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
166 flags.invalid = 1;
169 if (userAccountControl & UF_SMARTCARD_REQUIRED) {
170 flags.require_hwauth = 1;
172 if (userAccountControl & UF_TRUSTED_FOR_DELEGATION) {
173 flags.ok_as_delegate = 1;
175 if (!(userAccountControl & UF_NOT_DELEGATED)) {
176 flags.forwardable = 1;
177 flags.proxiable = 1;
180 if (userAccountControl & UF_DONT_REQUIRE_PREAUTH) {
181 flags.require_preauth = 0;
182 } else {
183 flags.require_preauth = 1;
186 return flags;
189 static int hdb_ldb_destructor(struct hdb_ldb_private *private)
191 hdb_entry_ex *entry_ex = private->entry_ex;
192 free_hdb_entry(&entry_ex->entry);
193 return 0;
196 static void hdb_ldb_free_entry(krb5_context context, hdb_entry_ex *entry_ex)
198 talloc_free(entry_ex->ctx);
201 static krb5_error_code LDB_message2entry_keys(krb5_context context,
202 struct smb_iconv_convenience *iconv_convenience,
203 TALLOC_CTX *mem_ctx,
204 struct ldb_message *msg,
205 unsigned int userAccountControl,
206 hdb_entry_ex *entry_ex)
208 krb5_error_code ret = 0;
209 enum ndr_err_code ndr_err;
210 struct samr_Password *hash;
211 const struct ldb_val *sc_val;
212 struct supplementalCredentialsBlob scb;
213 struct supplementalCredentialsPackage *scpk = NULL;
214 bool newer_keys = false;
215 struct package_PrimaryKerberosBlob _pkb;
216 struct package_PrimaryKerberosCtr3 *pkb3 = NULL;
217 struct package_PrimaryKerberosCtr4 *pkb4 = NULL;
218 uint32_t i;
219 uint32_t allocated_keys = 0;
221 entry_ex->entry.keys.val = NULL;
222 entry_ex->entry.keys.len = 0;
224 entry_ex->entry.kvno = ldb_msg_find_attr_as_int(msg, "msDS-KeyVersionNumber", 0);
226 /* Get keys from the db */
228 hash = samdb_result_hash(mem_ctx, msg, "unicodePwd");
229 sc_val = ldb_msg_find_ldb_val(msg, "supplementalCredentials");
231 /* unicodePwd for enctype 0x17 (23) if present */
232 if (hash) {
233 allocated_keys++;
236 /* supplementalCredentials if present */
237 if (sc_val) {
238 ndr_err = ndr_pull_struct_blob_all(sc_val, mem_ctx, iconv_convenience, &scb,
239 (ndr_pull_flags_fn_t)ndr_pull_supplementalCredentialsBlob);
240 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
241 dump_data(0, sc_val->data, sc_val->length);
242 ret = EINVAL;
243 goto out;
246 if (scb.sub.signature != SUPPLEMENTAL_CREDENTIALS_SIGNATURE) {
247 NDR_PRINT_DEBUG(supplementalCredentialsBlob, &scb);
248 ret = EINVAL;
249 goto out;
252 for (i=0; i < scb.sub.num_packages; i++) {
253 if (strcmp("Primary:Kerberos-Newer-Keys", scb.sub.packages[i].name) == 0) {
254 scpk = &scb.sub.packages[i];
255 if (!scpk->data || !scpk->data[0]) {
256 scpk = NULL;
257 continue;
259 newer_keys = true;
260 break;
261 } else if (strcmp("Primary:Kerberos", scb.sub.packages[i].name) == 0) {
262 scpk = &scb.sub.packages[i];
263 if (!scpk->data || !scpk->data[0]) {
264 scpk = NULL;
267 * we don't break here in hope to find
268 * a Kerberos-Newer-Keys package
274 * Primary:Kerberos-Newer-Keys or Primary:Kerberos element
275 * of supplementalCredentials
277 if (scpk) {
278 DATA_BLOB blob;
280 blob = strhex_to_data_blob(scpk->data);
281 if (!blob.data) {
282 ret = ENOMEM;
283 goto out;
285 talloc_steal(mem_ctx, blob.data);
287 /* we cannot use ndr_pull_struct_blob_all() here, as w2k and w2k3 add padding bytes */
288 ndr_err = ndr_pull_struct_blob(&blob, mem_ctx, iconv_convenience, &_pkb,
289 (ndr_pull_flags_fn_t)ndr_pull_package_PrimaryKerberosBlob);
290 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
291 krb5_set_error_string(context, "LDB_message2entry_keys: could not parse package_PrimaryKerberosBlob");
292 krb5_warnx(context, "LDB_message2entry_keys: could not parse package_PrimaryKerberosBlob");
293 ret = EINVAL;
294 goto out;
297 if (newer_keys && _pkb.version != 4) {
298 krb5_set_error_string(context, "LDB_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
299 krb5_warnx(context, "LDB_message2entry_keys: Primary:Kerberos-Newer-Keys not version 4");
300 ret = EINVAL;
301 goto out;
304 if (!newer_keys && _pkb.version != 3) {
305 krb5_set_error_string(context, "LDB_message2entry_keys: could not parse Primary:Kerberos not version 3");
306 krb5_warnx(context, "LDB_message2entry_keys: could not parse Primary:Kerberos not version 3");
307 ret = EINVAL;
308 goto out;
311 if (_pkb.version == 4) {
312 pkb4 = &_pkb.ctr.ctr4;
313 allocated_keys += pkb4->num_keys;
314 } else if (_pkb.version == 3) {
315 pkb3 = &_pkb.ctr.ctr3;
316 allocated_keys += pkb3->num_keys;
320 if (allocated_keys == 0) {
321 /* oh, no password. Apparently (comment in
322 * hdb-ldap.c) this violates the ASN.1, but this
323 * allows an entry with no keys (yet). */
324 return 0;
327 /* allocate space to decode into */
328 entry_ex->entry.keys.len = 0;
329 entry_ex->entry.keys.val = calloc(allocated_keys, sizeof(Key));
330 if (entry_ex->entry.keys.val == NULL) {
331 ret = ENOMEM;
332 goto out;
335 if (hash && !(userAccountControl & UF_USE_DES_KEY_ONLY)) {
336 Key key;
338 key.mkvno = 0;
339 key.salt = NULL; /* No salt for this enc type */
341 ret = krb5_keyblock_init(context,
342 ENCTYPE_ARCFOUR_HMAC_MD5,
343 hash->hash, sizeof(hash->hash),
344 &key.key);
345 if (ret) {
346 goto out;
349 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
350 entry_ex->entry.keys.len++;
353 if (pkb4) {
354 for (i=0; i < pkb4->num_keys; i++) {
355 bool use = true;
356 Key key;
358 if (!pkb4->keys[i].value) continue;
360 if (userAccountControl & UF_USE_DES_KEY_ONLY) {
361 switch (pkb4->keys[i].keytype) {
362 case ENCTYPE_DES_CBC_CRC:
363 case ENCTYPE_DES_CBC_MD5:
364 break;
365 default:
366 use = false;
367 break;
371 if (!use) continue;
373 key.mkvno = 0;
374 key.salt = NULL;
376 if (pkb4->salt.string) {
377 DATA_BLOB salt;
379 salt = data_blob_string_const(pkb4->salt.string);
381 key.salt = calloc(1, sizeof(*key.salt));
382 if (key.salt == NULL) {
383 ret = ENOMEM;
384 goto out;
387 key.salt->type = hdb_pw_salt;
389 ret = krb5_data_copy(&key.salt->salt, salt.data, salt.length);
390 if (ret) {
391 free(key.salt);
392 key.salt = NULL;
393 goto out;
397 /* TODO: maybe pass the iteration_count somehow... */
399 ret = krb5_keyblock_init(context,
400 pkb4->keys[i].keytype,
401 pkb4->keys[i].value->data,
402 pkb4->keys[i].value->length,
403 &key.key);
404 if (ret) {
405 if (key.salt) {
406 free_Salt(key.salt);
407 free(key.salt);
408 key.salt = NULL;
410 goto out;
413 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
414 entry_ex->entry.keys.len++;
416 } else if (pkb3) {
417 for (i=0; i < pkb3->num_keys; i++) {
418 bool use = true;
419 Key key;
421 if (!pkb3->keys[i].value) continue;
423 if (userAccountControl & UF_USE_DES_KEY_ONLY) {
424 switch (pkb3->keys[i].keytype) {
425 case ENCTYPE_DES_CBC_CRC:
426 case ENCTYPE_DES_CBC_MD5:
427 break;
428 default:
429 use = false;
430 break;
434 if (!use) continue;
436 key.mkvno = 0;
437 key.salt = NULL;
439 if (pkb3->salt.string) {
440 DATA_BLOB salt;
442 salt = data_blob_string_const(pkb3->salt.string);
444 key.salt = calloc(1, sizeof(*key.salt));
445 if (key.salt == NULL) {
446 ret = ENOMEM;
447 goto out;
450 key.salt->type = hdb_pw_salt;
452 ret = krb5_data_copy(&key.salt->salt, salt.data, salt.length);
453 if (ret) {
454 free(key.salt);
455 key.salt = NULL;
456 goto out;
460 ret = krb5_keyblock_init(context,
461 pkb3->keys[i].keytype,
462 pkb3->keys[i].value->data,
463 pkb3->keys[i].value->length,
464 &key.key);
465 if (ret) {
466 if (key.salt) {
467 free_Salt(key.salt);
468 free(key.salt);
469 key.salt = NULL;
471 goto out;
474 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
475 entry_ex->entry.keys.len++;
479 out:
480 if (ret != 0) {
481 entry_ex->entry.keys.len = 0;
483 if (entry_ex->entry.keys.len == 0 && entry_ex->entry.keys.val) {
484 free(entry_ex->entry.keys.val);
485 entry_ex->entry.keys.val = NULL;
487 return ret;
491 * Construct an hdb_entry from a directory entry.
493 static krb5_error_code LDB_message2entry(krb5_context context, HDB *db,
494 TALLOC_CTX *mem_ctx, krb5_const_principal principal,
495 enum hdb_ldb_ent_type ent_type,
496 struct ldb_message *msg,
497 struct ldb_message *realm_ref_msg,
498 hdb_entry_ex *entry_ex)
500 unsigned int userAccountControl;
501 int i;
502 krb5_error_code ret = 0;
503 krb5_boolean is_computer = FALSE;
504 const char *dnsdomain = ldb_msg_find_attr_as_string(realm_ref_msg, "dnsRoot", NULL);
505 char *realm = strupper_talloc(mem_ctx, dnsdomain);
506 struct loadparm_context *lp_ctx = ldb_get_opaque((struct ldb_context *)db->hdb_db, "loadparm");
507 struct ldb_dn *domain_dn = samdb_result_dn((struct ldb_context *)db->hdb_db,
508 mem_ctx,
509 realm_ref_msg,
510 "nCName",
511 ldb_dn_new(mem_ctx, (struct ldb_context *)db->hdb_db, NULL));
513 struct hdb_ldb_private *private;
514 NTTIME acct_expiry;
516 struct ldb_message_element *objectclasses;
517 struct ldb_val computer_val;
518 computer_val.data = discard_const_p(uint8_t,"computer");
519 computer_val.length = strlen((const char *)computer_val.data);
521 objectclasses = ldb_msg_find_element(msg, "objectClass");
523 if (objectclasses && ldb_msg_find_val(objectclasses, &computer_val)) {
524 is_computer = TRUE;
527 memset(entry_ex, 0, sizeof(*entry_ex));
529 if (!realm) {
530 krb5_set_error_string(context, "talloc_strdup: out of memory");
531 ret = ENOMEM;
532 goto out;
535 private = talloc(mem_ctx, struct hdb_ldb_private);
536 if (!private) {
537 ret = ENOMEM;
538 goto out;
541 private->entry_ex = entry_ex;
542 private->iconv_convenience = lp_iconv_convenience(lp_ctx);
543 private->netbios_name = lp_netbios_name(lp_ctx);
545 talloc_set_destructor(private, hdb_ldb_destructor);
547 entry_ex->ctx = private;
548 entry_ex->free_entry = hdb_ldb_free_entry;
550 userAccountControl = ldb_msg_find_attr_as_uint(msg, "userAccountControl", 0);
553 entry_ex->entry.principal = malloc(sizeof(*(entry_ex->entry.principal)));
554 if (ent_type == HDB_SAMBA4_ENT_TYPE_ANY && principal == NULL) {
555 const char *samAccountName = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
556 if (!samAccountName) {
557 krb5_set_error_string(context, "LDB_message2entry: no samAccountName present");
558 ret = ENOENT;
559 goto out;
561 samAccountName = ldb_msg_find_attr_as_string(msg, "samAccountName", NULL);
562 krb5_make_principal(context, &entry_ex->entry.principal, realm, samAccountName, NULL);
563 } else {
564 char *strdup_realm;
565 ret = copy_Principal(principal, entry_ex->entry.principal);
566 if (ret) {
567 krb5_clear_error_string(context);
568 goto out;
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 strdup_realm = strdup(realm);
579 if (!strdup_realm) {
580 ret = ENOMEM;
581 krb5_clear_error_string(context);
582 goto out;
584 free(*krb5_princ_realm(context, entry_ex->entry.principal));
585 krb5_princ_set_realm(context, entry_ex->entry.principal, &strdup_realm);
588 entry_ex->entry.flags = uf2HDBFlags(context, userAccountControl, ent_type);
590 if (ent_type == HDB_SAMBA4_ENT_TYPE_KRBTGT) {
591 entry_ex->entry.flags.invalid = 0;
592 entry_ex->entry.flags.server = 1;
593 entry_ex->entry.flags.forwardable = 1;
594 entry_ex->entry.flags.ok_as_delegate = 1;
597 if (lp_parm_bool(lp_ctx, NULL, "kdc", "require spn for service", true)) {
598 if (!is_computer && !ldb_msg_find_attr_as_string(msg, "servicePrincipalName", NULL)) {
599 entry_ex->entry.flags.server = 0;
603 /* use 'whenCreated' */
604 entry_ex->entry.created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
605 /* use '???' */
606 entry_ex->entry.created_by.principal = NULL;
608 entry_ex->entry.modified_by = (Event *) malloc(sizeof(Event));
609 if (entry_ex->entry.modified_by == NULL) {
610 krb5_set_error_string(context, "malloc: out of memory");
611 ret = ENOMEM;
612 goto out;
615 /* use 'whenChanged' */
616 entry_ex->entry.modified_by->time = ldb_msg_find_krb5time_ldap_time(msg, "whenChanged", 0);
617 /* use '???' */
618 entry_ex->entry.modified_by->principal = NULL;
620 entry_ex->entry.valid_start = NULL;
622 acct_expiry = samdb_result_account_expires(msg);
623 if (acct_expiry == 0x7FFFFFFFFFFFFFFFULL) {
624 entry_ex->entry.valid_end = NULL;
625 } else {
626 entry_ex->entry.valid_end = malloc(sizeof(*entry_ex->entry.valid_end));
627 if (entry_ex->entry.valid_end == NULL) {
628 ret = ENOMEM;
629 goto out;
631 *entry_ex->entry.valid_end = nt_time_to_unix(acct_expiry);
634 if (ent_type != HDB_SAMBA4_ENT_TYPE_KRBTGT) {
635 NTTIME must_change_time
636 = samdb_result_force_password_change((struct ldb_context *)db->hdb_db, mem_ctx,
637 domain_dn, msg);
638 if (must_change_time == 0x7FFFFFFFFFFFFFFFULL) {
639 entry_ex->entry.pw_end = NULL;
640 } else {
641 entry_ex->entry.pw_end = malloc(sizeof(*entry_ex->entry.pw_end));
642 if (entry_ex->entry.pw_end == NULL) {
643 ret = ENOMEM;
644 goto out;
646 *entry_ex->entry.pw_end = nt_time_to_unix(must_change_time);
648 } else {
649 entry_ex->entry.pw_end = NULL;
652 entry_ex->entry.max_life = NULL;
654 entry_ex->entry.max_renew = NULL;
656 entry_ex->entry.generation = NULL;
658 /* Get keys from the db */
659 ret = LDB_message2entry_keys(context, private->iconv_convenience, private, msg, userAccountControl, entry_ex);
660 if (ret) {
661 /* Could be bougus data in the entry, or out of memory */
662 goto out;
665 entry_ex->entry.etypes = malloc(sizeof(*(entry_ex->entry.etypes)));
666 if (entry_ex->entry.etypes == NULL) {
667 krb5_clear_error_string(context);
668 ret = ENOMEM;
669 goto out;
671 entry_ex->entry.etypes->len = entry_ex->entry.keys.len;
672 entry_ex->entry.etypes->val = calloc(entry_ex->entry.etypes->len, sizeof(int));
673 if (entry_ex->entry.etypes->val == NULL) {
674 krb5_clear_error_string(context);
675 ret = ENOMEM;
676 goto out;
678 for (i=0; i < entry_ex->entry.etypes->len; i++) {
679 entry_ex->entry.etypes->val[i] = entry_ex->entry.keys.val[i].key.keytype;
683 private->msg = talloc_steal(private, msg);
684 private->realm_ref_msg = talloc_steal(private, realm_ref_msg);
685 private->samdb = (struct ldb_context *)db->hdb_db;
687 out:
688 if (ret != 0) {
689 /* This doesn't free ent itself, that is for the eventual caller to do */
690 hdb_free_entry(context, entry_ex);
691 } else {
692 talloc_steal(db, entry_ex->ctx);
695 return ret;
699 * Construct an hdb_entry from a directory entry.
701 static krb5_error_code LDB_trust_message2entry(krb5_context context, HDB *db,
702 struct loadparm_context *lp_ctx,
703 TALLOC_CTX *mem_ctx, krb5_const_principal principal,
704 enum trust_direction direction,
705 struct ldb_message *msg,
706 hdb_entry_ex *entry_ex)
709 const char *dnsdomain;
710 char *realm;
711 char *strdup_realm;
712 DATA_BLOB password_utf16;
713 struct samr_Password password_hash;
714 const struct ldb_val *password_val;
715 struct trustAuthInOutBlob password_blob;
716 struct hdb_ldb_private *private;
718 enum ndr_err_code ndr_err;
719 int i, ret, trust_direction_flags;
721 private = talloc(mem_ctx, struct hdb_ldb_private);
722 if (!private) {
723 ret = ENOMEM;
724 goto out;
727 private->entry_ex = entry_ex;
728 private->iconv_convenience = lp_iconv_convenience(lp_ctx);
729 private->netbios_name = lp_netbios_name(lp_ctx);
731 talloc_set_destructor(private, hdb_ldb_destructor);
733 entry_ex->ctx = private;
734 entry_ex->free_entry = hdb_ldb_free_entry;
736 /* use 'whenCreated' */
737 entry_ex->entry.created_by.time = ldb_msg_find_krb5time_ldap_time(msg, "whenCreated", 0);
738 /* use '???' */
739 entry_ex->entry.created_by.principal = NULL;
741 entry_ex->entry.valid_start = NULL;
743 trust_direction_flags = ldb_msg_find_attr_as_int(msg, "trustDirection", 0);
745 if (direction == INBOUND) {
746 realm = strupper_talloc(mem_ctx, lp_realm(lp_ctx));
747 password_val = ldb_msg_find_ldb_val(msg, "trustAuthIncoming");
749 } else { /* OUTBOUND */
750 dnsdomain = ldb_msg_find_attr_as_string(msg, "trustPartner", NULL);
751 realm = strupper_talloc(mem_ctx, dnsdomain);
752 password_val = ldb_msg_find_ldb_val(msg, "trustAuthOutgoing");
755 if (!password_val || !(trust_direction_flags & direction)) {
756 ret = ENOENT;
757 goto out;
760 ndr_err = ndr_pull_struct_blob(password_val, mem_ctx, private->iconv_convenience, &password_blob,
761 (ndr_pull_flags_fn_t)ndr_pull_trustAuthInOutBlob);
762 if (!NDR_ERR_CODE_IS_SUCCESS(ndr_err)) {
763 ret = EINVAL;
764 goto out;
767 entry_ex->entry.kvno = -1;
768 for (i=0; i < password_blob.count; i++) {
769 if (password_blob.current->array[i].AuthType == TRUST_AUTH_TYPE_VERSION) {
770 entry_ex->entry.kvno = password_blob.current->array[i].AuthInfo.version.version;
774 for (i=0; i < password_blob.count; i++) {
775 if (password_blob.current->array[i].AuthType == TRUST_AUTH_TYPE_CLEAR) {
776 password_utf16 = data_blob_const(password_blob.current->array[i].AuthInfo.clear.password,
777 password_blob.current->array[i].AuthInfo.clear.size);
778 /* In the future, generate all sorts of
779 * hashes, but for now we can't safely convert
780 * the random strings windows uses into
781 * utf8 */
783 /* but as it is utf16 already, we can get the NT password/arcfour-hmac-md5 key */
784 mdfour(password_hash.hash, password_utf16.data, password_utf16.length);
785 break;
786 } else if (password_blob.current->array[i].AuthType == TRUST_AUTH_TYPE_NT4OWF) {
787 password_hash = password_blob.current->array[i].AuthInfo.nt4owf.password;
788 break;
791 entry_ex->entry.keys.len = 0;
792 entry_ex->entry.keys.val = NULL;
794 if (i < password_blob.count) {
795 Key key;
796 /* Must have found a cleartext or MD4 password */
797 entry_ex->entry.keys.val = calloc(1, sizeof(Key));
799 key.mkvno = 0;
800 key.salt = NULL; /* No salt for this enc type */
802 if (entry_ex->entry.keys.val == NULL) {
803 ret = ENOMEM;
804 goto out;
807 ret = krb5_keyblock_init(context,
808 ENCTYPE_ARCFOUR_HMAC_MD5,
809 password_hash.hash, sizeof(password_hash.hash),
810 &key.key);
812 entry_ex->entry.keys.val[entry_ex->entry.keys.len] = key;
813 entry_ex->entry.keys.len++;
816 entry_ex->entry.principal = malloc(sizeof(*(entry_ex->entry.principal)));
818 ret = copy_Principal(principal, entry_ex->entry.principal);
819 if (ret) {
820 krb5_clear_error_string(context);
821 goto out;
824 /* While we have copied the client principal, tests
825 * show that Win2k3 returns the 'corrected' realm, not
826 * the client-specified realm. This code attempts to
827 * replace the client principal's realm with the one
828 * we determine from our records */
830 /* this has to be with malloc() */
831 strdup_realm = strdup(realm);
832 if (!strdup_realm) {
833 ret = ENOMEM;
834 krb5_clear_error_string(context);
835 goto out;
837 free(*krb5_princ_realm(context, entry_ex->entry.principal));
838 krb5_princ_set_realm(context, entry_ex->entry.principal, &strdup_realm);
840 entry_ex->entry.flags = int2HDBFlags(0);
841 entry_ex->entry.flags.immutable = 1;
842 entry_ex->entry.flags.invalid = 0;
843 entry_ex->entry.flags.server = 1;
844 entry_ex->entry.flags.require_preauth = 1;
846 entry_ex->entry.pw_end = NULL;
848 entry_ex->entry.max_life = NULL;
850 entry_ex->entry.max_renew = NULL;
852 entry_ex->entry.generation = NULL;
854 entry_ex->entry.etypes = malloc(sizeof(*(entry_ex->entry.etypes)));
855 if (entry_ex->entry.etypes == NULL) {
856 krb5_clear_error_string(context);
857 ret = ENOMEM;
858 goto out;
860 entry_ex->entry.etypes->len = entry_ex->entry.keys.len;
861 entry_ex->entry.etypes->val = calloc(entry_ex->entry.etypes->len, sizeof(int));
862 if (entry_ex->entry.etypes->val == NULL) {
863 krb5_clear_error_string(context);
864 ret = ENOMEM;
865 goto out;
867 for (i=0; i < entry_ex->entry.etypes->len; i++) {
868 entry_ex->entry.etypes->val[i] = entry_ex->entry.keys.val[i].key.keytype;
872 private->msg = talloc_steal(private, msg);
873 private->realm_ref_msg = NULL;
874 private->samdb = (struct ldb_context *)db->hdb_db;
876 out:
877 if (ret != 0) {
878 /* This doesn't free ent itself, that is for the eventual caller to do */
879 hdb_free_entry(context, entry_ex);
880 } else {
881 talloc_steal(db, entry_ex->ctx);
884 return ret;
888 static krb5_error_code LDB_lookup_principal(krb5_context context, struct ldb_context *ldb_ctx,
889 TALLOC_CTX *mem_ctx,
890 krb5_const_principal principal,
891 enum hdb_ldb_ent_type ent_type,
892 struct ldb_dn *realm_dn,
893 struct ldb_message ***pmsg)
895 krb5_error_code ret;
896 int lret;
897 char *filter = NULL;
898 const char * const *princ_attrs = user_attrs;
900 char *short_princ;
901 char *short_princ_talloc;
903 struct ldb_result *res = NULL;
905 ret = krb5_unparse_name_flags(context, principal, KRB5_PRINCIPAL_UNPARSE_NO_REALM, &short_princ);
907 if (ret != 0) {
908 krb5_set_error_string(context, "LDB_lookup_principal: could not parse principal");
909 krb5_warnx(context, "LDB_lookup_principal: could not parse principal");
910 return ret;
913 short_princ_talloc = talloc_strdup(mem_ctx, short_princ);
914 free(short_princ);
915 if (!short_princ_talloc) {
916 krb5_set_error_string(context, "LDB_lookup_principal: talloc_strdup() failed!");
917 return ENOMEM;
920 switch (ent_type) {
921 case HDB_SAMBA4_ENT_TYPE_CLIENT:
922 case HDB_SAMBA4_ENT_TYPE_TRUST:
923 case HDB_SAMBA4_ENT_TYPE_ANY:
924 /* Can't happen */
925 return EINVAL;
926 case HDB_SAMBA4_ENT_TYPE_KRBTGT:
927 filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))",
928 KRB5_TGS_NAME);
929 break;
930 case HDB_SAMBA4_ENT_TYPE_SERVER:
931 filter = talloc_asprintf(mem_ctx, "(&(objectClass=user)(samAccountName=%s))",
932 short_princ_talloc);
933 break;
936 if (!filter) {
937 krb5_set_error_string(context, "talloc_asprintf: out of memory");
938 return ENOMEM;
941 lret = ldb_search(ldb_ctx, mem_ctx, &res, realm_dn,
942 LDB_SCOPE_SUBTREE, princ_attrs, "%s", filter);
943 if (lret != LDB_SUCCESS) {
944 DEBUG(3, ("Failed to search for %s: %s\n", filter, ldb_errstring(ldb_ctx)));
945 return HDB_ERR_NOENTRY;
946 } else if (res->count == 0 || res->count > 1) {
947 DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter, res->count));
948 talloc_free(res);
949 return HDB_ERR_NOENTRY;
951 talloc_steal(mem_ctx, res->msgs);
952 *pmsg = res->msgs;
953 talloc_free(res);
954 return 0;
957 static krb5_error_code LDB_lookup_trust(krb5_context context, struct ldb_context *ldb_ctx,
958 TALLOC_CTX *mem_ctx,
959 const char *realm,
960 struct ldb_dn *realm_dn,
961 struct ldb_message ***pmsg)
963 int lret;
964 char *filter = NULL;
965 const char * const *attrs = trust_attrs;
967 struct ldb_result *res = NULL;
968 filter = talloc_asprintf(mem_ctx, "(&(objectClass=trustedDomain)(|(flatname=%s)(trustPartner=%s)))", realm, realm);
970 if (!filter) {
971 krb5_set_error_string(context, "talloc_asprintf: out of memory");
972 return ENOMEM;
975 lret = ldb_search(ldb_ctx, mem_ctx, &res,
976 ldb_get_default_basedn(ldb_ctx),
977 LDB_SCOPE_SUBTREE, attrs, "%s", filter);
978 if (lret != LDB_SUCCESS) {
979 DEBUG(3, ("Failed to search for %s: %s\n", filter, ldb_errstring(ldb_ctx)));
980 return HDB_ERR_NOENTRY;
981 } else if (res->count == 0 || res->count > 1) {
982 DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter, res->count));
983 talloc_free(res);
984 return HDB_ERR_NOENTRY;
986 talloc_steal(mem_ctx, res->msgs);
987 *pmsg = res->msgs;
988 talloc_free(res);
989 return 0;
992 static krb5_error_code LDB_lookup_realm(krb5_context context, struct ldb_context *ldb_ctx,
993 TALLOC_CTX *mem_ctx,
994 const char *realm,
995 struct ldb_message ***pmsg)
997 int ret;
998 struct ldb_result *cross_ref_res;
999 struct ldb_dn *partitions_basedn = samdb_partitions_dn(ldb_ctx, mem_ctx);
1001 ret = ldb_search(ldb_ctx, mem_ctx, &cross_ref_res,
1002 partitions_basedn, LDB_SCOPE_SUBTREE, realm_ref_attrs,
1003 "(&(&(|(&(dnsRoot=%s)(nETBIOSName=*))(nETBIOSName=%s))(objectclass=crossRef))(ncName=*))",
1004 realm, realm);
1006 if (ret != LDB_SUCCESS) {
1007 DEBUG(3, ("Failed to search to lookup realm(%s): %s\n", realm, ldb_errstring(ldb_ctx)));
1008 talloc_free(cross_ref_res);
1009 return HDB_ERR_NOENTRY;
1010 } else if (cross_ref_res->count == 0 || cross_ref_res->count > 1) {
1011 DEBUG(3, ("Failed find a single entry for realm %s: got %d\n", realm, cross_ref_res->count));
1012 talloc_free(cross_ref_res);
1013 return HDB_ERR_NOENTRY;
1016 if (pmsg) {
1017 *pmsg = cross_ref_res->msgs;
1018 talloc_steal(mem_ctx, cross_ref_res->msgs);
1020 talloc_free(cross_ref_res);
1022 return 0;
1026 static krb5_error_code LDB_open(krb5_context context, HDB *db, int flags, mode_t mode)
1028 if (db->hdb_master_key_set) {
1029 krb5_warnx(context, "LDB_open: use of a master key incompatible with LDB\n");
1030 krb5_set_error_string(context, "LDB_open: use of a master key incompatible with LDB\n");
1031 return HDB_ERR_NOENTRY;
1034 return 0;
1037 static krb5_error_code LDB_close(krb5_context context, HDB *db)
1039 return 0;
1042 static krb5_error_code LDB_lock(krb5_context context, HDB *db, int operation)
1044 return 0;
1047 static krb5_error_code LDB_unlock(krb5_context context, HDB *db)
1049 return 0;
1052 static krb5_error_code LDB_rename(krb5_context context, HDB *db, const char *new_name)
1054 return HDB_ERR_DB_INUSE;
1057 static krb5_error_code LDB_fetch_client(krb5_context context, HDB *db,
1058 TALLOC_CTX *mem_ctx,
1059 krb5_const_principal principal,
1060 unsigned flags,
1061 hdb_entry_ex *entry_ex) {
1062 NTSTATUS nt_status;
1063 char *principal_string;
1064 krb5_error_code ret;
1065 struct ldb_message **msg = NULL;
1066 struct ldb_message **realm_ref_msg = NULL;
1068 ret = krb5_unparse_name(context, principal, &principal_string);
1070 if (ret != 0) {
1071 return ret;
1074 nt_status = sam_get_results_principal((struct ldb_context *)db->hdb_db,
1075 mem_ctx, principal_string,
1076 &msg, &realm_ref_msg);
1077 free(principal_string);
1078 if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_SUCH_USER)) {
1079 return HDB_ERR_NOENTRY;
1080 } else if (NT_STATUS_EQUAL(nt_status, NT_STATUS_NO_MEMORY)) {
1081 return ENOMEM;
1082 } else if (!NT_STATUS_IS_OK(nt_status)) {
1083 return EINVAL;
1086 ret = LDB_message2entry(context, db, mem_ctx,
1087 principal, HDB_SAMBA4_ENT_TYPE_CLIENT,
1088 msg[0], realm_ref_msg[0], entry_ex);
1089 return ret;
1092 static krb5_error_code LDB_fetch_krbtgt(krb5_context context, HDB *db,
1093 TALLOC_CTX *mem_ctx,
1094 krb5_const_principal principal,
1095 unsigned flags,
1096 hdb_entry_ex *entry_ex)
1098 krb5_error_code ret;
1099 struct ldb_message **msg = NULL;
1100 struct ldb_message **realm_ref_msg_1 = NULL;
1101 struct ldb_message **realm_ref_msg_2 = NULL;
1102 struct ldb_dn *realm_dn;
1103 const char *realm;
1105 krb5_principal alloc_principal = NULL;
1106 if (principal->name.name_string.len != 2
1107 || (strcmp(principal->name.name_string.val[0], KRB5_TGS_NAME) != 0)) {
1108 /* Not a krbtgt */
1109 return HDB_ERR_NOENTRY;
1112 /* krbtgt case. Either us or a trusted realm */
1114 if ((LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
1115 mem_ctx, principal->realm, &realm_ref_msg_1) == 0)
1116 && (LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
1117 mem_ctx, principal->name.name_string.val[1], &realm_ref_msg_2) == 0)
1118 && (ldb_dn_compare(realm_ref_msg_1[0]->dn, realm_ref_msg_1[0]->dn) == 0)) {
1119 /* us */
1120 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
1121 * is in our db, then direct the caller at our primary
1122 * krbtgt */
1124 const char *dnsdomain = ldb_msg_find_attr_as_string(realm_ref_msg_1[0], "dnsRoot", NULL);
1125 char *realm_fixed = strupper_talloc(mem_ctx, dnsdomain);
1126 if (!realm_fixed) {
1127 krb5_set_error_string(context, "strupper_talloc: out of memory");
1128 return ENOMEM;
1131 ret = krb5_copy_principal(context, principal, &alloc_principal);
1132 if (ret) {
1133 return ret;
1136 free(alloc_principal->name.name_string.val[1]);
1137 alloc_principal->name.name_string.val[1] = strdup(realm_fixed);
1138 talloc_free(realm_fixed);
1139 if (!alloc_principal->name.name_string.val[1]) {
1140 krb5_set_error_string(context, "LDB_fetch: strdup() failed!");
1141 return ENOMEM;
1143 principal = alloc_principal;
1144 realm_dn = samdb_result_dn((struct ldb_context *)db->hdb_db, mem_ctx, realm_ref_msg_1[0], "nCName", NULL);
1146 ret = LDB_lookup_principal(context, (struct ldb_context *)db->hdb_db,
1147 mem_ctx,
1148 principal, HDB_SAMBA4_ENT_TYPE_KRBTGT, realm_dn, &msg);
1150 if (ret != 0) {
1151 krb5_warnx(context, "LDB_fetch: could not find principal in DB");
1152 krb5_set_error_string(context, "LDB_fetch: could not find principal in DB");
1153 return ret;
1156 ret = LDB_message2entry(context, db, mem_ctx,
1157 principal, HDB_SAMBA4_ENT_TYPE_KRBTGT,
1158 msg[0], realm_ref_msg_1[0], entry_ex);
1159 if (ret != 0) {
1160 krb5_warnx(context, "LDB_fetch: self krbtgt message2entry failed");
1162 return ret;
1164 } else {
1165 enum trust_direction direction = UNKNOWN;
1167 struct loadparm_context *lp_ctx = talloc_get_type(ldb_get_opaque(db->hdb_db, "loadparm"), struct loadparm_context);
1168 /* Either an inbound or outbound trust */
1170 if (strcasecmp(lp_realm(lp_ctx), principal->realm) == 0) {
1171 /* look for inbound trust */
1172 direction = INBOUND;
1173 realm = principal->name.name_string.val[1];
1176 if (strcasecmp(lp_realm(lp_ctx), principal->name.name_string.val[1]) == 0) {
1177 /* look for outbound trust */
1178 direction = OUTBOUND;
1179 realm = principal->realm;
1182 /* Trusted domains are under CN=system */
1184 ret = LDB_lookup_trust(context, (struct ldb_context *)db->hdb_db,
1185 mem_ctx,
1186 realm, realm_dn, &msg);
1188 if (ret != 0) {
1189 krb5_warnx(context, "LDB_fetch: could not find principal in DB");
1190 krb5_set_error_string(context, "LDB_fetch: could not find principal in DB");
1191 return ret;
1194 ret = LDB_trust_message2entry(context, db, lp_ctx, mem_ctx,
1195 principal, direction,
1196 msg[0], entry_ex);
1197 if (ret != 0) {
1198 krb5_warnx(context, "LDB_fetch: trust_message2entry failed");
1200 return ret;
1203 /* we should lookup trusted domains */
1204 return HDB_ERR_NOENTRY;
1209 static krb5_error_code LDB_fetch_server(krb5_context context, HDB *db,
1210 TALLOC_CTX *mem_ctx,
1211 krb5_const_principal principal,
1212 unsigned flags,
1213 hdb_entry_ex *entry_ex)
1215 krb5_error_code ret;
1216 const char *realm;
1217 struct ldb_message **msg = NULL;
1218 struct ldb_message **realm_ref_msg = NULL;
1219 struct ldb_dn *partitions_basedn = samdb_partitions_dn(db->hdb_db, mem_ctx);
1220 if (principal->name.name_string.len >= 2) {
1221 /* 'normal server' case */
1222 int ldb_ret;
1223 NTSTATUS nt_status;
1224 struct ldb_dn *user_dn, *domain_dn;
1225 char *principal_string;
1227 ret = krb5_unparse_name_flags(context, principal,
1228 KRB5_PRINCIPAL_UNPARSE_NO_REALM,
1229 &principal_string);
1230 if (ret != 0) {
1231 return ret;
1234 /* At this point we may find the host is known to be
1235 * in a different realm, so we should generate a
1236 * referral instead */
1237 nt_status = crack_service_principal_name((struct ldb_context *)db->hdb_db,
1238 mem_ctx, principal_string,
1239 &user_dn, &domain_dn);
1240 free(principal_string);
1242 if (!NT_STATUS_IS_OK(nt_status)) {
1243 return HDB_ERR_NOENTRY;
1246 ldb_ret = gendb_search_dn((struct ldb_context *)db->hdb_db,
1247 mem_ctx, user_dn, &msg, user_attrs);
1249 if (ldb_ret != 1) {
1250 return HDB_ERR_NOENTRY;
1253 ldb_ret = gendb_search((struct ldb_context *)db->hdb_db,
1254 mem_ctx, partitions_basedn, &realm_ref_msg, realm_ref_attrs,
1255 "ncName=%s", ldb_dn_get_linearized(domain_dn));
1257 if (ldb_ret != 1) {
1258 return HDB_ERR_NOENTRY;
1261 } else {
1262 struct ldb_dn *realm_dn;
1263 /* server as client principal case, but we must not lookup userPrincipalNames */
1265 realm = krb5_principal_get_realm(context, principal);
1267 ret = LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
1268 mem_ctx, realm, &realm_ref_msg);
1269 if (ret != 0) {
1270 return HDB_ERR_NOENTRY;
1273 realm_dn = samdb_result_dn((struct ldb_context *)db->hdb_db, mem_ctx, realm_ref_msg[0], "nCName", NULL);
1275 ret = LDB_lookup_principal(context, (struct ldb_context *)db->hdb_db,
1276 mem_ctx,
1277 principal, HDB_SAMBA4_ENT_TYPE_SERVER, realm_dn, &msg);
1279 if (ret != 0) {
1280 return ret;
1284 ret = LDB_message2entry(context, db, mem_ctx,
1285 principal, HDB_SAMBA4_ENT_TYPE_SERVER,
1286 msg[0], realm_ref_msg[0], entry_ex);
1287 if (ret != 0) {
1288 krb5_warnx(context, "LDB_fetch: message2entry failed");
1291 return ret;
1294 static krb5_error_code LDB_fetch(krb5_context context, HDB *db,
1295 krb5_const_principal principal,
1296 unsigned flags,
1297 hdb_entry_ex *entry_ex)
1299 krb5_error_code ret = HDB_ERR_NOENTRY;
1301 TALLOC_CTX *mem_ctx = talloc_named(db, 0, "LDB_fetch context");
1303 if (!mem_ctx) {
1304 krb5_set_error_string(context, "LDB_fetch: talloc_named() failed!");
1305 return ENOMEM;
1308 if (flags & HDB_F_GET_CLIENT) {
1309 ret = LDB_fetch_client(context, db, mem_ctx, principal, flags, entry_ex);
1310 if (ret != HDB_ERR_NOENTRY) goto done;
1312 if (flags & HDB_F_GET_SERVER) {
1313 /* krbtgt fits into this situation for trusted realms, and for resolving different versions of our own realm name */
1314 ret = LDB_fetch_krbtgt(context, db, mem_ctx, principal, flags, entry_ex);
1315 if (ret != HDB_ERR_NOENTRY) goto done;
1317 /* We return 'no entry' if it does not start with krbtgt/, so move to the common case quickly */
1318 ret = LDB_fetch_server(context, db, mem_ctx, principal, flags, entry_ex);
1319 if (ret != HDB_ERR_NOENTRY) goto done;
1321 if (flags & HDB_F_GET_KRBTGT) {
1322 ret = LDB_fetch_krbtgt(context, db, mem_ctx, principal, flags, entry_ex);
1323 if (ret != HDB_ERR_NOENTRY) goto done;
1326 done:
1327 talloc_free(mem_ctx);
1328 return ret;
1331 static krb5_error_code LDB_store(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
1333 return HDB_ERR_DB_INUSE;
1336 static krb5_error_code LDB_remove(krb5_context context, HDB *db, krb5_const_principal principal)
1338 return HDB_ERR_DB_INUSE;
1341 struct hdb_ldb_seq {
1342 struct ldb_context *ctx;
1343 int index;
1344 int count;
1345 struct ldb_message **msgs;
1346 struct ldb_message **realm_ref_msgs;
1349 static krb5_error_code LDB_seq(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
1351 krb5_error_code ret;
1352 struct hdb_ldb_seq *priv = (struct hdb_ldb_seq *)db->hdb_dbc;
1353 TALLOC_CTX *mem_ctx;
1354 hdb_entry_ex entry_ex;
1355 memset(&entry_ex, '\0', sizeof(entry_ex));
1357 if (!priv) {
1358 return HDB_ERR_NOENTRY;
1361 mem_ctx = talloc_named(priv, 0, "LDB_seq context");
1363 if (!mem_ctx) {
1364 krb5_set_error_string(context, "LDB_seq: talloc_named() failed!");
1365 return ENOMEM;
1368 if (priv->index < priv->count) {
1369 ret = LDB_message2entry(context, db, mem_ctx,
1370 NULL, HDB_SAMBA4_ENT_TYPE_ANY,
1371 priv->msgs[priv->index++],
1372 priv->realm_ref_msgs[0], entry);
1373 } else {
1374 ret = HDB_ERR_NOENTRY;
1377 if (ret != 0) {
1378 talloc_free(priv);
1379 db->hdb_dbc = NULL;
1380 } else {
1381 talloc_free(mem_ctx);
1384 return ret;
1387 static krb5_error_code LDB_firstkey(krb5_context context, HDB *db, unsigned flags,
1388 hdb_entry_ex *entry)
1390 struct ldb_context *ldb_ctx = (struct ldb_context *)db->hdb_db;
1391 struct hdb_ldb_seq *priv = (struct hdb_ldb_seq *)db->hdb_dbc;
1392 char *realm;
1393 struct ldb_dn *realm_dn = NULL;
1394 struct ldb_result *res = NULL;
1395 struct ldb_message **realm_ref_msgs = NULL;
1396 krb5_error_code ret;
1397 TALLOC_CTX *mem_ctx;
1398 int lret;
1400 if (priv) {
1401 talloc_free(priv);
1402 db->hdb_dbc = NULL;
1405 priv = (struct hdb_ldb_seq *) talloc(db, struct hdb_ldb_seq);
1406 if (!priv) {
1407 krb5_set_error_string(context, "talloc: out of memory");
1408 return ENOMEM;
1411 priv->ctx = ldb_ctx;
1412 priv->index = 0;
1413 priv->msgs = NULL;
1414 priv->realm_ref_msgs = NULL;
1415 priv->count = 0;
1417 mem_ctx = talloc_named(priv, 0, "LDB_firstkey context");
1419 if (!mem_ctx) {
1420 krb5_set_error_string(context, "LDB_firstkey: talloc_named() failed!");
1421 return ENOMEM;
1424 ret = krb5_get_default_realm(context, &realm);
1425 if (ret != 0) {
1426 talloc_free(priv);
1427 return ret;
1430 ret = LDB_lookup_realm(context, (struct ldb_context *)db->hdb_db,
1431 mem_ctx, realm, &realm_ref_msgs);
1433 free(realm);
1435 if (ret != 0) {
1436 talloc_free(priv);
1437 krb5_warnx(context, "LDB_firstkey: could not find realm\n");
1438 return HDB_ERR_NOENTRY;
1441 realm_dn = samdb_result_dn((struct ldb_context *)db->hdb_db, mem_ctx, realm_ref_msgs[0], "nCName", NULL);
1443 priv->realm_ref_msgs = talloc_steal(priv, realm_ref_msgs);
1445 lret = ldb_search(ldb_ctx, priv, &res,
1446 realm_dn, LDB_SCOPE_SUBTREE, user_attrs,
1447 "(objectClass=user)");
1449 if (lret != LDB_SUCCESS) {
1450 talloc_free(priv);
1451 return HDB_ERR_NOENTRY;
1454 priv->count = res->count;
1455 priv->msgs = talloc_steal(priv, res->msgs);
1456 talloc_free(res);
1458 db->hdb_dbc = priv;
1460 ret = LDB_seq(context, db, flags, entry);
1462 if (ret != 0) {
1463 talloc_free(priv);
1464 db->hdb_dbc = NULL;
1465 } else {
1466 talloc_free(mem_ctx);
1468 return ret;
1471 static krb5_error_code LDB_nextkey(krb5_context context, HDB *db, unsigned flags,
1472 hdb_entry_ex *entry)
1474 return LDB_seq(context, db, flags, entry);
1477 static krb5_error_code LDB_destroy(krb5_context context, HDB *db)
1479 talloc_free(db);
1480 return 0;
1483 /* This interface is to be called by the KDC, which is expecting Samba
1484 * calling conventions. It is also called by a wrapper
1485 * (hdb_ldb_create) from the kpasswdd -> krb5 -> keytab_hdb -> hdb
1486 * code */
1488 NTSTATUS kdc_hdb_samba4_create(TALLOC_CTX *mem_ctx,
1489 struct event_context *ev_ctx,
1490 struct loadparm_context *lp_ctx,
1491 krb5_context context, struct HDB **db, const char *arg)
1493 NTSTATUS nt_status;
1494 struct auth_session_info *session_info;
1495 *db = talloc(mem_ctx, HDB);
1496 if (!*db) {
1497 krb5_set_error_string(context, "malloc: out of memory");
1498 return NT_STATUS_NO_MEMORY;
1501 (*db)->hdb_master_key_set = 0;
1502 (*db)->hdb_db = NULL;
1504 nt_status = auth_system_session_info(*db, lp_ctx, &session_info);
1505 if (!NT_STATUS_IS_OK(nt_status)) {
1506 return nt_status;
1509 /* The idea here is very simple. Using Kerberos to
1510 * authenticate the KDC to the LDAP server is higly likely to
1511 * be circular.
1513 * In future we may set this up to use EXERNAL and SSL
1514 * certificates, for now it will almost certainly be NTLMSSP
1517 cli_credentials_set_kerberos_state(session_info->credentials,
1518 CRED_DONT_USE_KERBEROS);
1520 /* Setup the link to LDB */
1521 (*db)->hdb_db = samdb_connect(*db, ev_ctx, lp_ctx, session_info);
1522 if ((*db)->hdb_db == NULL) {
1523 DEBUG(1, ("hdb_ldb_create: Cannot open samdb for KDC backend!"));
1524 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
1527 (*db)->hdb_dbc = NULL;
1528 (*db)->hdb_open = LDB_open;
1529 (*db)->hdb_close = LDB_close;
1530 (*db)->hdb_fetch = LDB_fetch;
1531 (*db)->hdb_store = LDB_store;
1532 (*db)->hdb_remove = LDB_remove;
1533 (*db)->hdb_firstkey = LDB_firstkey;
1534 (*db)->hdb_nextkey = LDB_nextkey;
1535 (*db)->hdb_lock = LDB_lock;
1536 (*db)->hdb_unlock = LDB_unlock;
1537 (*db)->hdb_rename = LDB_rename;
1538 /* we don't implement these, as we are not a lockable database */
1539 (*db)->hdb__get = NULL;
1540 (*db)->hdb__put = NULL;
1541 /* kadmin should not be used for deletes - use other tools instead */
1542 (*db)->hdb__del = NULL;
1543 (*db)->hdb_destroy = LDB_destroy;
1545 return NT_STATUS_OK;
1548 krb5_error_code hdb_samba4_create(krb5_context context, struct HDB **db, const char *arg)
1550 NTSTATUS nt_status;
1551 /* The global kdc_mem_ctx and kdc_lp_ctx, Disgusting, ugly hack, but it means one less private hook */
1552 nt_status = kdc_hdb_samba4_create(kdc_mem_ctx, event_context_find(kdc_mem_ctx), kdc_lp_ctx,
1553 context, db, arg);
1555 if (NT_STATUS_IS_OK(nt_status)) {
1556 return 0;
1558 return EINVAL;