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>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
36 #include "system/time.h"
40 #include "lib/ldb/include/ldb.h"
41 #include "lib/ldb/include/ldb_errors.h"
42 #include "librpc/gen_ndr/netlogon.h"
43 #include "auth/auth.h"
44 #include "auth/auth_sam.h"
46 #include "dsdb/samdb/samdb.h"
49 { HDB_LDB_ENT_TYPE_CLIENT
, HDB_LDB_ENT_TYPE_SERVER
,
50 HDB_LDB_ENT_TYPE_KRBTGT
, HDB_LDB_ENT_TYPE_ANY
};
52 static const char * const krb5_attrs
[] = {
57 "servicePrincipalName",
69 "msDS-KeyVersionNumber",
73 static const char *realm_ref_attrs
[] = {
79 static KerberosTime
ldb_msg_find_krb5time_ldap_time(struct ldb_message
*msg
, const char *attr
, KerberosTime default_val
)
85 gentime
= ldb_msg_find_string(msg
, attr
, NULL
);
89 tmp
= strptime(gentime
, "%Y%m%d%H%M%SZ", &tm
);
97 static HDBFlags
uf2HDBFlags(krb5_context context
, int userAccountControl
, enum hdb_ldb_ent_type ent_type
)
99 HDBFlags flags
= int2HDBFlags(0);
101 /* we don't allow kadmin deletes */
104 /* mark the principal as invalid to start with */
109 /* All accounts are servers, but this may be disabled again in the caller */
112 /* Account types - clear the invalid bit if it turns out to be valid */
113 if (userAccountControl
& UF_NORMAL_ACCOUNT
) {
114 if (ent_type
== HDB_LDB_ENT_TYPE_CLIENT
|| ent_type
== HDB_LDB_ENT_TYPE_ANY
) {
120 if (userAccountControl
& UF_INTERDOMAIN_TRUST_ACCOUNT
) {
121 if (ent_type
== HDB_LDB_ENT_TYPE_CLIENT
|| ent_type
== HDB_LDB_ENT_TYPE_ANY
) {
126 if (userAccountControl
& UF_WORKSTATION_TRUST_ACCOUNT
) {
127 if (ent_type
== HDB_LDB_ENT_TYPE_CLIENT
|| ent_type
== HDB_LDB_ENT_TYPE_ANY
) {
132 if (userAccountControl
& UF_SERVER_TRUST_ACCOUNT
) {
133 if (ent_type
== HDB_LDB_ENT_TYPE_CLIENT
|| ent_type
== HDB_LDB_ENT_TYPE_ANY
) {
139 /* Not permitted to act as a client if disabled */
140 if (userAccountControl
& UF_ACCOUNTDISABLE
) {
143 if (userAccountControl
& UF_LOCKOUT
) {
147 if (userAccountControl & UF_PASSWORD_NOTREQD) {
152 UF_PASSWORD_CANT_CHANGE and UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED are irrelevent
154 if (userAccountControl
& UF_TEMP_DUPLICATE_ACCOUNT
) {
158 /* UF_DONT_EXPIRE_PASSWD and UF_USE_DES_KEY_ONLY handled in LDB_message2entry() */
161 if (userAccountControl & UF_MNS_LOGON_ACCOUNT) {
165 if (userAccountControl
& UF_SMARTCARD_REQUIRED
) {
166 flags
.require_hwauth
= 1;
168 if (userAccountControl
& UF_TRUSTED_FOR_DELEGATION
) {
169 flags
.ok_as_delegate
= 1;
171 if (!(userAccountControl
& UF_NOT_DELEGATED
)) {
172 flags
.forwardable
= 1;
176 if (userAccountControl
& UF_DONT_REQUIRE_PREAUTH
) {
177 flags
.require_preauth
= 0;
179 flags
.require_preauth
= 1;
185 static int hdb_ldb_destrutor(struct hdb_ldb_private
*private)
187 hdb_entry_ex
*entry_ex
= private->entry_ex
;
188 free_hdb_entry(&entry_ex
->entry
);
192 static void hdb_ldb_free_entry(krb5_context context
, hdb_entry_ex
*entry_ex
)
194 talloc_free(entry_ex
->ctx
);
198 * Construct an hdb_entry from a directory entry.
200 static krb5_error_code
LDB_message2entry(krb5_context context
, HDB
*db
,
201 TALLOC_CTX
*mem_ctx
, krb5_const_principal principal
,
202 enum hdb_ldb_ent_type ent_type
,
203 struct ldb_message
*msg
,
204 struct ldb_message
*realm_ref_msg
,
205 hdb_entry_ex
*entry_ex
)
207 unsigned int userAccountControl
;
209 krb5_error_code ret
= 0;
210 krb5_boolean is_computer
= FALSE
;
211 const char *dnsdomain
= ldb_msg_find_string(realm_ref_msg
, "dnsRoot", NULL
);
212 char *realm
= strupper_talloc(mem_ctx
, dnsdomain
);
213 struct ldb_dn
*domain_dn
= samdb_result_dn(mem_ctx
, realm_ref_msg
, "nCName", ldb_dn_new(mem_ctx
));
215 struct hdb_ldb_private
*private;
217 struct ldb_message_element
*ldb_keys
;
219 struct ldb_message_element
*objectclasses
;
220 struct ldb_val computer_val
;
221 computer_val
.data
= discard_const_p(uint8_t,"computer");
222 computer_val
.length
= strlen((const char *)computer_val
.data
);
224 objectclasses
= ldb_msg_find_element(msg
, "objectClass");
226 if (objectclasses
&& ldb_msg_find_val(objectclasses
, &computer_val
)) {
230 memset(entry_ex
, 0, sizeof(*entry_ex
));
233 krb5_set_error_string(context
, "talloc_strdup: out of memory");
238 private = talloc(mem_ctx
, struct hdb_ldb_private
);
244 private->entry_ex
= entry_ex
;
246 talloc_set_destructor(private, hdb_ldb_destrutor
);
248 entry_ex
->ctx
= private;
249 entry_ex
->free_entry
= hdb_ldb_free_entry
;
251 userAccountControl
= ldb_msg_find_uint(msg
, "userAccountControl", 0);
254 entry_ex
->entry
.principal
= malloc(sizeof(*(entry_ex
->entry
.principal
)));
255 if (ent_type
== HDB_LDB_ENT_TYPE_ANY
&& principal
== NULL
) {
256 const char *samAccountName
= ldb_msg_find_string(msg
, "samAccountName", NULL
);
257 if (!samAccountName
) {
258 krb5_set_error_string(context
, "LDB_message2entry: no samAccountName present");
262 samAccountName
= ldb_msg_find_string(msg
, "samAccountName", NULL
);
263 krb5_make_principal(context
, &entry_ex
->entry
.principal
, realm
, samAccountName
, NULL
);
266 ret
= copy_Principal(principal
, entry_ex
->entry
.principal
);
268 krb5_clear_error_string(context
);
272 /* While we have copied the client principal, tests
273 * show that Win2k3 returns the 'corrected' realm, not
274 * the client-specified realm. This code attempts to
275 * replace the client principal's realm with the one
276 * we determine from our records */
278 /* this has to be with malloc() */
279 strdup_realm
= strdup(realm
);
282 krb5_clear_error_string(context
);
285 free(*krb5_princ_realm(context
, entry_ex
->entry
.principal
));
286 krb5_princ_set_realm(context
, entry_ex
->entry
.principal
, &strdup_realm
);
289 entry_ex
->entry
.kvno
= ldb_msg_find_int(msg
, "msDS-KeyVersionNumber", 0);
291 entry_ex
->entry
.flags
= uf2HDBFlags(context
, userAccountControl
, ent_type
);
293 if (ent_type
== HDB_LDB_ENT_TYPE_KRBTGT
) {
294 entry_ex
->entry
.flags
.invalid
= 0;
295 entry_ex
->entry
.flags
.server
= 1;
296 entry_ex
->entry
.flags
.forwardable
= 1;
297 entry_ex
->entry
.flags
.ok_as_delegate
= 1;
300 if (lp_parm_bool(-1, "kdc", "require spn for service", True
)) {
301 if (!is_computer
&& !ldb_msg_find_string(msg
, "servicePrincipalName", NULL
)) {
302 entry_ex
->entry
.flags
.server
= 0;
306 /* use 'whenCreated' */
307 entry_ex
->entry
.created_by
.time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenCreated", 0);
309 entry_ex
->entry
.created_by
.principal
= NULL
;
311 entry_ex
->entry
.modified_by
= (Event
*) malloc(sizeof(Event
));
312 if (entry_ex
->entry
.modified_by
== NULL
) {
313 krb5_set_error_string(context
, "malloc: out of memory");
318 /* use 'whenChanged' */
319 entry_ex
->entry
.modified_by
->time
= ldb_msg_find_krb5time_ldap_time(msg
, "whenChanged", 0);
321 entry_ex
->entry
.modified_by
->principal
= NULL
;
323 entry_ex
->entry
.valid_start
= NULL
;
325 acct_expiry
= samdb_result_nttime(msg
, "accountExpires", (NTTIME
)-1);
326 if ((acct_expiry
== (NTTIME
)-1) ||
327 (acct_expiry
== 0x7FFFFFFFFFFFFFFFULL
)) {
328 entry_ex
->entry
.valid_end
= NULL
;
330 entry_ex
->entry
.valid_end
= malloc(sizeof(*entry_ex
->entry
.valid_end
));
331 if (entry_ex
->entry
.valid_end
== NULL
) {
335 *entry_ex
->entry
.valid_end
= nt_time_to_unix(acct_expiry
);
338 if (ent_type
!= HDB_LDB_ENT_TYPE_KRBTGT
) {
339 NTTIME must_change_time
340 = samdb_result_force_password_change((struct ldb_context
*)db
->hdb_db
, mem_ctx
,
342 if (must_change_time
== 0x7FFFFFFFFFFFFFFFULL
) {
343 entry_ex
->entry
.pw_end
= NULL
;
345 entry_ex
->entry
.pw_end
= malloc(sizeof(*entry_ex
->entry
.pw_end
));
346 if (entry_ex
->entry
.pw_end
== NULL
) {
350 *entry_ex
->entry
.pw_end
= nt_time_to_unix(must_change_time
);
353 entry_ex
->entry
.pw_end
= NULL
;
356 entry_ex
->entry
.max_life
= NULL
;
358 entry_ex
->entry
.max_renew
= NULL
;
360 entry_ex
->entry
.generation
= NULL
;
362 /* Get krb5Key from the db */
364 ldb_keys
= ldb_msg_find_element(msg
, "krb5Key");
367 /* oh, no password. Apparently (comment in
368 * hdb-ldap.c) this violates the ASN.1, but this
369 * allows an entry with no keys (yet). */
370 entry_ex
->entry
.keys
.val
= NULL
;
371 entry_ex
->entry
.keys
.len
= 0;
373 /* allocate space to decode into */
374 entry_ex
->entry
.keys
.val
= calloc(ldb_keys
->num_values
, sizeof(Key
));
375 if (entry_ex
->entry
.keys
.val
== NULL
) {
380 entry_ex
->entry
.keys
.len
= 0;
382 /* Decode Kerberos keys into the hdb structure */
383 for (i
=0; i
< ldb_keys
->num_values
; i
++) {
386 ret
= decode_Key(ldb_keys
->values
[i
].data
, ldb_keys
->values
[i
].length
,
389 /* Could be bougus data in the entry, or out of memory */
393 if (userAccountControl
& UF_USE_DES_KEY_ONLY
) {
394 switch (key
.key
.keytype
) {
396 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
397 entry_ex
->entry
.keys
.len
++;
399 /* We must use DES keys only */
403 entry_ex
->entry
.keys
.val
[entry_ex
->entry
.keys
.len
] = key
;
404 entry_ex
->entry
.keys
.len
++;
409 entry_ex
->entry
.etypes
= malloc(sizeof(*(entry_ex
->entry
.etypes
)));
410 if (entry_ex
->entry
.etypes
== NULL
) {
411 krb5_clear_error_string(context
);
415 entry_ex
->entry
.etypes
->len
= entry_ex
->entry
.keys
.len
;
416 entry_ex
->entry
.etypes
->val
= calloc(entry_ex
->entry
.etypes
->len
, sizeof(int));
417 if (entry_ex
->entry
.etypes
->val
== NULL
) {
418 krb5_clear_error_string(context
);
422 for (i
=0; i
< entry_ex
->entry
.etypes
->len
; i
++) {
423 entry_ex
->entry
.etypes
->val
[i
] = entry_ex
->entry
.keys
.val
[i
].key
.keytype
;
427 private->msg
= talloc_steal(private, msg
);
428 private->realm_ref_msg
= talloc_steal(private, realm_ref_msg
);
429 private->samdb
= (struct ldb_context
*)db
->hdb_db
;
431 entry_ex
->check_client_access
= hdb_ldb_check_client_access
;
432 entry_ex
->authz_data_tgs_req
= hdb_ldb_authz_data_tgs_req
;
433 entry_ex
->authz_data_as_req
= hdb_ldb_authz_data_as_req
;
437 /* This doesn't free ent itself, that is for the eventual caller to do */
438 hdb_free_entry(context
, entry_ex
);
440 talloc_steal(db
, entry_ex
->ctx
);
446 static krb5_error_code
LDB_lookup_principal(krb5_context context
, struct ldb_context
*ldb_ctx
,
448 krb5_const_principal principal
,
449 enum hdb_ldb_ent_type ent_type
,
450 const struct ldb_dn
*realm_dn
,
451 struct ldb_message
***pmsg
)
456 const char * const *princ_attrs
= krb5_attrs
;
459 char *short_princ_talloc
;
463 struct ldb_result
*res
= NULL
;
465 ret
= krb5_unparse_name_norealm(context
, principal
, &short_princ
);
468 krb5_set_error_string(context
, "LDB_lookup_principal: could not parse principal");
469 krb5_warnx(context
, "LDB_lookup_principal: could not parse principal");
473 short_princ_talloc
= talloc_strdup(mem_ctx
, short_princ
);
475 if (!short_princ_talloc
) {
476 krb5_set_error_string(context
, "LDB_lookup_principal: talloc_strdup() failed!");
481 case HDB_LDB_ENT_TYPE_CLIENT
:
484 case HDB_LDB_ENT_TYPE_ANY
:
487 case HDB_LDB_ENT_TYPE_KRBTGT
:
488 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=user)(samAccountName=%s))",
491 case HDB_LDB_ENT_TYPE_SERVER
:
492 filter
= talloc_asprintf(mem_ctx
, "(&(objectClass=user)(samAccountName=%s))",
498 krb5_set_error_string(context
, "talloc_asprintf: out of memory");
502 lret
= ldb_search(ldb_ctx
, realm_dn
, LDB_SCOPE_SUBTREE
, filter
, princ_attrs
, &res
);
504 realm_dn_str
= ldb_dn_linearize(mem_ctx
, realm_dn
);
506 if (lret
!= LDB_SUCCESS
) {
507 DEBUG(3, ("Failed to search for %s: %s\n", filter
, ldb_errstring(ldb_ctx
)));
508 return HDB_ERR_NOENTRY
;
509 } else if (res
->count
== 0 || res
->count
> 1) {
510 DEBUG(3, ("Failed find a single entry for %s: got %d\n", filter
, res
->count
));
511 return HDB_ERR_NOENTRY
;
513 talloc_steal(mem_ctx
, res
->msgs
);
519 static krb5_error_code
LDB_lookup_realm(krb5_context context
, struct ldb_context
*ldb_ctx
,
522 struct ldb_message
***pmsg
)
525 char *cross_ref_filter
;
526 struct ldb_result
*cross_ref_res
;
527 const struct ldb_dn
*partitions_basedn
= ldb_dn_string_compose(mem_ctx
, samdb_base_dn(mem_ctx
), "CN=Partitions,CN=Configuration");
529 cross_ref_filter
= talloc_asprintf(mem_ctx
,
530 "(&(&(|(&(dnsRoot=%s)(nETBIOSName=*))(nETBIOSName=%s))(objectclass=crossRef))(ncName=*))",
532 if (!cross_ref_filter
) {
533 krb5_set_error_string(context
, "asprintf: out of memory");
537 ret
= ldb_search(ldb_ctx
, partitions_basedn
, LDB_SCOPE_SUBTREE
, cross_ref_filter
, realm_ref_attrs
, &cross_ref_res
);
539 if (ret
!= LDB_SUCCESS
) {
540 DEBUG(3, ("Failed to search for %s: %s\n", cross_ref_filter
, ldb_errstring(ldb_ctx
)));
541 talloc_free(cross_ref_res
);
542 return HDB_ERR_NOENTRY
;
543 } else if (cross_ref_res
->count
== 0 || cross_ref_res
->count
> 1) {
544 DEBUG(3, ("Failed find a single entry for %s: got %d\n", cross_ref_filter
, cross_ref_res
->count
));
545 talloc_free(cross_ref_res
);
546 return HDB_ERR_NOENTRY
;
550 *pmsg
= cross_ref_res
->msgs
;
551 talloc_steal(mem_ctx
, cross_ref_res
->msgs
);
553 talloc_free(cross_ref_res
);
559 static krb5_error_code
LDB_open(krb5_context context
, HDB
*db
, int flags
, mode_t mode
)
561 if (db
->hdb_master_key_set
) {
562 krb5_warnx(context
, "LDB_open: use of a master key incompatible with LDB\n");
563 krb5_set_error_string(context
, "LDB_open: use of a master key incompatible with LDB\n");
564 return HDB_ERR_NOENTRY
;
570 static krb5_error_code
LDB_close(krb5_context context
, HDB
*db
)
575 static krb5_error_code
LDB_lock(krb5_context context
, HDB
*db
, int operation
)
580 static krb5_error_code
LDB_unlock(krb5_context context
, HDB
*db
)
585 static krb5_error_code
LDB_rename(krb5_context context
, HDB
*db
, const char *new_name
)
587 return HDB_ERR_DB_INUSE
;
590 static krb5_error_code
LDB_fetch_client(krb5_context context
, HDB
*db
,
592 krb5_const_principal principal
,
594 hdb_entry_ex
*entry_ex
) {
596 char *principal_string
;
598 struct ldb_message
**msg
= NULL
;
599 struct ldb_message
**realm_ref_msg
= NULL
;
601 ret
= krb5_unparse_name(context
, principal
, &principal_string
);
607 nt_status
= sam_get_results_principal((struct ldb_context
*)db
->hdb_db
,
608 mem_ctx
, principal_string
,
609 &msg
, &realm_ref_msg
);
610 free(principal_string
);
611 if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_SUCH_USER
)) {
612 return HDB_ERR_NOENTRY
;
613 } else if (NT_STATUS_EQUAL(nt_status
, NT_STATUS_NO_MEMORY
)) {
615 } else if (!NT_STATUS_IS_OK(nt_status
)) {
619 ret
= LDB_message2entry(context
, db
, mem_ctx
,
620 principal
, HDB_LDB_ENT_TYPE_CLIENT
,
621 msg
[0], realm_ref_msg
[0], entry_ex
);
625 static krb5_error_code
LDB_fetch_krbtgt(krb5_context context
, HDB
*db
,
627 krb5_const_principal principal
,
629 hdb_entry_ex
*entry_ex
)
632 struct ldb_message
**msg
= NULL
;
633 struct ldb_message
**realm_ref_msg
= NULL
;
634 const struct ldb_dn
*realm_dn
;
636 krb5_principal alloc_principal
= NULL
;
637 if (principal
->name
.name_string
.len
!= 2
638 || (strcmp(principal
->name
.name_string
.val
[0], KRB5_TGS_NAME
) != 0)) {
640 return HDB_ERR_NOENTRY
;
643 /* krbtgt case. Either us or a trusted realm */
644 if ((LDB_lookup_realm(context
, (struct ldb_context
*)db
->hdb_db
,
645 mem_ctx
, principal
->name
.name_string
.val
[1], &realm_ref_msg
) == 0)) {
647 /* Cludge, cludge cludge. If the realm part of krbtgt/realm,
648 * is in our db, then direct the caller at our primary
651 const char *dnsdomain
= ldb_msg_find_string(realm_ref_msg
[0], "dnsRoot", NULL
);
652 char *realm_fixed
= strupper_talloc(mem_ctx
, dnsdomain
);
654 krb5_set_error_string(context
, "strupper_talloc: out of memory");
658 ret
= krb5_copy_principal(context
, principal
, &alloc_principal
);
663 free(alloc_principal
->name
.name_string
.val
[1]);
664 alloc_principal
->name
.name_string
.val
[1] = strdup(realm_fixed
);
665 talloc_free(realm_fixed
);
666 if (!alloc_principal
->name
.name_string
.val
[1]) {
667 krb5_set_error_string(context
, "LDB_fetch: strdup() failed!");
670 principal
= alloc_principal
;
671 realm_dn
= samdb_result_dn(mem_ctx
, realm_ref_msg
[0], "nCName", NULL
);
674 /* we should lookup trusted domains */
675 return HDB_ERR_NOENTRY
;
678 realm_dn
= samdb_result_dn(mem_ctx
, realm_ref_msg
[0], "nCName", NULL
);
680 ret
= LDB_lookup_principal(context
, (struct ldb_context
*)db
->hdb_db
,
682 principal
, HDB_LDB_ENT_TYPE_KRBTGT
, realm_dn
, &msg
);
685 krb5_warnx(context
, "LDB_fetch: could not find principal in DB");
686 krb5_set_error_string(context
, "LDB_fetch: could not find principal in DB");
690 ret
= LDB_message2entry(context
, db
, mem_ctx
,
691 principal
, HDB_LDB_ENT_TYPE_KRBTGT
,
692 msg
[0], realm_ref_msg
[0], entry_ex
);
694 krb5_warnx(context
, "LDB_fetch: message2entry failed");
699 static krb5_error_code
LDB_fetch_server(krb5_context context
, HDB
*db
,
701 krb5_const_principal principal
,
703 hdb_entry_ex
*entry_ex
)
707 struct ldb_message
**msg
= NULL
;
708 struct ldb_message
**realm_ref_msg
= NULL
;
709 const struct ldb_dn
*partitions_basedn
= ldb_dn_string_compose(mem_ctx
, samdb_base_dn(mem_ctx
), "CN=Partitions,CN=Configuration");
710 if (principal
->name
.name_string
.len
>= 2) {
711 /* 'normal server' case */
714 struct ldb_dn
*user_dn
, *domain_dn
;
715 char *principal_string
;
717 ret
= krb5_unparse_name_norealm(context
, principal
, &principal_string
);
722 /* At this point we may find the host is known to be
723 * in a different realm, so we should generate a
724 * referral instead */
725 nt_status
= crack_service_principal_name((struct ldb_context
*)db
->hdb_db
,
726 mem_ctx
, principal_string
,
727 &user_dn
, &domain_dn
);
728 free(principal_string
);
730 if (!NT_STATUS_IS_OK(nt_status
)) {
731 return HDB_ERR_NOENTRY
;
734 ldb_ret
= gendb_search_dn((struct ldb_context
*)db
->hdb_db
,
735 mem_ctx
, user_dn
, &msg
, krb5_attrs
);
738 return HDB_ERR_NOENTRY
;
741 ldb_ret
= gendb_search((struct ldb_context
*)db
->hdb_db
,
742 mem_ctx
, partitions_basedn
, &realm_ref_msg
, realm_ref_attrs
,
743 "ncName=%s", ldb_dn_linearize(mem_ctx
, domain_dn
));
746 return HDB_ERR_NOENTRY
;
750 const struct ldb_dn
*realm_dn
;
751 /* server as client principal case, but we must not lookup userPrincipalNames */
753 realm
= krb5_principal_get_realm(context
, principal
);
755 ret
= LDB_lookup_realm(context
, (struct ldb_context
*)db
->hdb_db
,
756 mem_ctx
, realm
, &realm_ref_msg
);
758 return HDB_ERR_NOENTRY
;
761 realm_dn
= samdb_result_dn(mem_ctx
, realm_ref_msg
[0], "nCName", NULL
);
763 ret
= LDB_lookup_principal(context
, (struct ldb_context
*)db
->hdb_db
,
765 principal
, HDB_LDB_ENT_TYPE_SERVER
, realm_dn
, &msg
);
772 ret
= LDB_message2entry(context
, db
, mem_ctx
,
773 principal
, HDB_LDB_ENT_TYPE_SERVER
,
774 msg
[0], realm_ref_msg
[0], entry_ex
);
776 krb5_warnx(context
, "LDB_fetch: message2entry failed");
782 static krb5_error_code
LDB_fetch(krb5_context context
, HDB
*db
,
783 krb5_const_principal principal
,
785 hdb_entry_ex
*entry_ex
)
787 krb5_error_code ret
= HDB_ERR_NOENTRY
;
789 TALLOC_CTX
*mem_ctx
= talloc_named(db
, 0, "LDB_fetch context");
792 krb5_set_error_string(context
, "LDB_fetch: talloc_named() failed!");
796 if (flags
& HDB_F_GET_CLIENT
) {
797 ret
= LDB_fetch_client(context
, db
, mem_ctx
, principal
, flags
, entry_ex
);
798 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
800 if (flags
& HDB_F_GET_SERVER
) {
801 ret
= LDB_fetch_server(context
, db
, mem_ctx
, principal
, flags
, entry_ex
);
802 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
803 ret
= LDB_fetch_krbtgt(context
, db
, mem_ctx
, principal
, flags
, entry_ex
);
804 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
806 if (flags
& HDB_F_GET_KRBTGT
) {
807 ret
= LDB_fetch_krbtgt(context
, db
, mem_ctx
, principal
, flags
, entry_ex
);
808 if (ret
!= HDB_ERR_NOENTRY
) goto done
;
812 talloc_free(mem_ctx
);
816 static krb5_error_code
LDB_store(krb5_context context
, HDB
*db
, unsigned flags
, hdb_entry_ex
*entry
)
818 return HDB_ERR_DB_INUSE
;
821 static krb5_error_code
LDB_remove(krb5_context context
, HDB
*db
, krb5_const_principal principal
)
823 return HDB_ERR_DB_INUSE
;
827 struct ldb_context
*ctx
;
830 struct ldb_message
**msgs
;
831 struct ldb_message
**realm_ref_msgs
;
834 static krb5_error_code
LDB_seq(krb5_context context
, HDB
*db
, unsigned flags
, hdb_entry_ex
*entry
)
837 struct hdb_ldb_seq
*priv
= (struct hdb_ldb_seq
*)db
->hdb_openp
;
839 hdb_entry_ex entry_ex
;
840 memset(&entry_ex
, '\0', sizeof(entry_ex
));
843 return HDB_ERR_NOENTRY
;
846 mem_ctx
= talloc_named(priv
, 0, "LDB_seq context");
849 krb5_set_error_string(context
, "LDB_seq: talloc_named() failed!");
853 if (priv
->index
< priv
->count
) {
854 ret
= LDB_message2entry(context
, db
, mem_ctx
,
855 NULL
, HDB_LDB_ENT_TYPE_ANY
,
856 priv
->msgs
[priv
->index
++],
857 priv
->realm_ref_msgs
[0], entry
);
859 ret
= HDB_ERR_NOENTRY
;
864 db
->hdb_openp
= NULL
;
866 talloc_free(mem_ctx
);
872 static krb5_error_code
LDB_firstkey(krb5_context context
, HDB
*db
, unsigned flags
,
875 struct ldb_context
*ldb_ctx
= (struct ldb_context
*)db
->hdb_db
;
876 struct hdb_ldb_seq
*priv
= (struct hdb_ldb_seq
*)db
->hdb_openp
;
878 struct ldb_dn
*realm_dn
= NULL
;
879 struct ldb_result
*res
= NULL
;
880 struct ldb_message
**realm_ref_msgs
= NULL
;
890 priv
= (struct hdb_ldb_seq
*) talloc(db
, struct hdb_ldb_seq
);
892 krb5_set_error_string(context
, "talloc: out of memory");
899 priv
->realm_ref_msgs
= NULL
;
902 mem_ctx
= talloc_named(priv
, 0, "LDB_firstkey context");
905 krb5_set_error_string(context
, "LDB_firstkey: talloc_named() failed!");
909 ret
= krb5_get_default_realm(context
, &realm
);
915 ret
= LDB_lookup_realm(context
, (struct ldb_context
*)db
->hdb_db
,
916 mem_ctx
, realm
, &realm_ref_msgs
);
922 krb5_warnx(context
, "LDB_firstkey: could not find realm\n");
923 return HDB_ERR_NOENTRY
;
926 realm_dn
= samdb_result_dn(mem_ctx
, realm_ref_msgs
[0], "nCName", NULL
);
928 priv
->realm_ref_msgs
= talloc_steal(priv
, realm_ref_msgs
);
930 lret
= ldb_search(ldb_ctx
, realm_dn
,
931 LDB_SCOPE_SUBTREE
, "(objectClass=user)",
934 if (lret
!= LDB_SUCCESS
) {
936 return HDB_ERR_NOENTRY
;
939 priv
->count
= res
->count
;
940 priv
->msgs
= talloc_steal(priv
, res
->msgs
);
943 db
->hdb_openp
= priv
;
945 ret
= LDB_seq(context
, db
, flags
, entry
);
949 db
->hdb_openp
= NULL
;
951 talloc_free(mem_ctx
);
956 static krb5_error_code
LDB_nextkey(krb5_context context
, HDB
*db
, unsigned flags
,
959 return LDB_seq(context
, db
, flags
, entry
);
962 static krb5_error_code
LDB_destroy(krb5_context context
, HDB
*db
)
968 /* This interface is to be called by the KDC, which is expecting Samba
969 * calling conventions. It is also called by a wrapper
970 * (hdb_ldb_create) from the kpasswdd -> krb5 -> keytab_hdb -> hdb
973 NTSTATUS
kdc_hdb_ldb_create(TALLOC_CTX
*mem_ctx
,
974 krb5_context context
, struct HDB
**db
, const char *arg
)
977 struct auth_session_info
*session_info
;
978 *db
= talloc(mem_ctx
, HDB
);
980 krb5_set_error_string(context
, "malloc: out of memory");
981 return NT_STATUS_NO_MEMORY
;
984 (*db
)->hdb_master_key_set
= 0;
985 (*db
)->hdb_db
= NULL
;
987 nt_status
= auth_system_session_info(*db
, &session_info
);
988 if (!NT_STATUS_IS_OK(nt_status
)) {
992 /* The idea here is very simple. Using Kerberos to
993 * authenticate the KDC to the LDAP server is higly likely to
996 * In future we may set this up to use EXERNAL and SSL
997 * certificates, for now it will almost certainly be NTLMSSP
1000 cli_credentials_set_kerberos_state(session_info
->credentials
,
1001 CRED_DONT_USE_KERBEROS
);
1003 /* Setup the link to LDB */
1004 (*db
)->hdb_db
= samdb_connect(*db
, session_info
);
1005 if ((*db
)->hdb_db
== NULL
) {
1006 DEBUG(1, ("hdb_ldb_create: Cannot open samdb for KDC backend!"));
1007 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO
;
1010 (*db
)->hdb_openp
= 0;
1011 (*db
)->hdb_open
= LDB_open
;
1012 (*db
)->hdb_close
= LDB_close
;
1013 (*db
)->hdb_fetch
= LDB_fetch
;
1014 (*db
)->hdb_store
= LDB_store
;
1015 (*db
)->hdb_remove
= LDB_remove
;
1016 (*db
)->hdb_firstkey
= LDB_firstkey
;
1017 (*db
)->hdb_nextkey
= LDB_nextkey
;
1018 (*db
)->hdb_lock
= LDB_lock
;
1019 (*db
)->hdb_unlock
= LDB_unlock
;
1020 (*db
)->hdb_rename
= LDB_rename
;
1021 /* we don't implement these, as we are not a lockable database */
1022 (*db
)->hdb__get
= NULL
;
1023 (*db
)->hdb__put
= NULL
;
1024 /* kadmin should not be used for deletes - use other tools instead */
1025 (*db
)->hdb__del
= NULL
;
1026 (*db
)->hdb_destroy
= LDB_destroy
;
1028 return NT_STATUS_OK
;
1031 krb5_error_code
hdb_ldb_create(krb5_context context
, struct HDB
**db
, const char *arg
)
1034 /* Disgusting, ugly hack, but it means one less private hook */
1035 nt_status
= kdc_hdb_ldb_create(context
->mem_ctx
, context
, db
, arg
);
1037 if (NT_STATUS_IS_OK(nt_status
)) {