1 /*
2  * Copyright (c) 1999-2001, 2003, PADL Software Pty Ltd.
3  * Copyright (c) 2004-2009, Andrew Bartlett <abartlet@samba.org>.
4  * Copyright (c) 2004, Stefan Metzmacher <metze@samba.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  *
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.
17  *
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.
21  *
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.
33  */
34 
35 #include "includes.h"
36 #include "kdc/kdc-glue.h"
37 #include "kdc/db-glue.h"
38 #include "auth/auth_sam.h"
39 #include "auth/common_auth.h"
40 #include <ldb.h>
41 #include "sdb.h"
42 #include "sdb_hdb.h"
43 #include "dsdb/samdb/samdb.h"
44 #include "param/param.h"
45 #include "../lib/tsocket/tsocket.h"
46 #include "librpc/gen_ndr/ndr_winbind_c.h"
47 #include "lib/messaging/irpc.h"
48 
hdb_samba4_open(krb5_context context,HDB * db,int flags,mode_t mode)49 static krb5_error_code hdb_samba4_open(krb5_context context, HDB *db, int flags, mode_t mode)
50 {
51 	if (db->hdb_master_key_set) {
52 		krb5_error_code ret = HDB_ERR_NOENTRY;
53 		krb5_warnx(context, "hdb_samba4_open: use of a master key incompatible with LDB\n");
54 		krb5_set_error_message(context, ret, "hdb_samba4_open: use of a master key incompatible with LDB\n");
55 		return ret;
56 	}
57 
58 	return 0;
59 }
60 
hdb_samba4_close(krb5_context context,HDB * db)61 static krb5_error_code hdb_samba4_close(krb5_context context, HDB *db)
62 {
63 	return 0;
64 }
65 
hdb_samba4_lock(krb5_context context,HDB * db,int operation)66 static krb5_error_code hdb_samba4_lock(krb5_context context, HDB *db, int operation)
67 {
68 	return 0;
69 }
70 
hdb_samba4_unlock(krb5_context context,HDB * db)71 static krb5_error_code hdb_samba4_unlock(krb5_context context, HDB *db)
72 {
73 	return 0;
74 }
75 
hdb_samba4_rename(krb5_context context,HDB * db,const char * new_name)76 static krb5_error_code hdb_samba4_rename(krb5_context context, HDB *db, const char *new_name)
77 {
78 	return HDB_ERR_DB_INUSE;
79 }
80 
hdb_samba4_store(krb5_context context,HDB * db,unsigned flags,hdb_entry_ex * entry)81 static krb5_error_code hdb_samba4_store(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
82 {
83 	return HDB_ERR_DB_INUSE;
84 }
85 
hdb_samba4_remove(krb5_context context,HDB * db,krb5_const_principal principal)86 static krb5_error_code hdb_samba4_remove(krb5_context context, HDB *db, krb5_const_principal principal)
87 {
88 	return HDB_ERR_DB_INUSE;
89 }
90 
hdb_samba4_fetch_kvno(krb5_context context,HDB * db,krb5_const_principal principal,unsigned flags,krb5_kvno kvno,hdb_entry_ex * entry_ex)91 static krb5_error_code hdb_samba4_fetch_kvno(krb5_context context, HDB *db,
92 					     krb5_const_principal principal,
93 					     unsigned flags,
94 					     krb5_kvno kvno,
95 					     hdb_entry_ex *entry_ex)
96 {
97 	struct samba_kdc_db_context *kdc_db_ctx;
98 	struct sdb_entry_ex sdb_entry_ex = {};
99 	krb5_error_code code, ret;
100 
101 	kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
102 					   struct samba_kdc_db_context);
103 
104 	ret = samba_kdc_fetch(context,
105 			      kdc_db_ctx,
106 			      principal,
107 			      flags,
108 			      kvno,
109 			      &sdb_entry_ex);
110 	switch (ret) {
111 	case 0:
112 		code = 0;
113 		break;
114 	case SDB_ERR_WRONG_REALM:
115 		/*
116 		 * If SDB_ERR_WRONG_REALM is returned we need to process the
117 		 * sdb_entry to fill the principal in the HDB entry.
118 		 */
119 		code = HDB_ERR_WRONG_REALM;
120 		break;
121 	case SDB_ERR_NOENTRY:
122 		return HDB_ERR_NOENTRY;
123 	case SDB_ERR_NOT_FOUND_HERE:
124 		return HDB_ERR_NOT_FOUND_HERE;
125 	default:
126 		return ret;
127 	}
128 
129 	ret = sdb_entry_ex_to_hdb_entry_ex(context, &sdb_entry_ex, entry_ex);
130 	sdb_free_entry(&sdb_entry_ex);
131 
132 	if (code != 0 && ret != 0) {
133 		code = ret;
134 	}
135 
136 	return code;
137 }
138 
hdb_samba4_firstkey(krb5_context context,HDB * db,unsigned flags,hdb_entry_ex * entry)139 static krb5_error_code hdb_samba4_firstkey(krb5_context context, HDB *db, unsigned flags,
140 					hdb_entry_ex *entry)
141 {
142 	struct samba_kdc_db_context *kdc_db_ctx;
143 	struct sdb_entry_ex sdb_entry_ex = {};
144 	krb5_error_code ret;
145 
146 	kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
147 					   struct samba_kdc_db_context);
148 
149 	ret = samba_kdc_firstkey(context, kdc_db_ctx, &sdb_entry_ex);
150 	switch (ret) {
151 	case 0:
152 		break;
153 	case SDB_ERR_WRONG_REALM:
154 		return HDB_ERR_WRONG_REALM;
155 	case SDB_ERR_NOENTRY:
156 		return HDB_ERR_NOENTRY;
157 	case SDB_ERR_NOT_FOUND_HERE:
158 		return HDB_ERR_NOT_FOUND_HERE;
159 	default:
160 		return ret;
161 	}
162 
163 	ret = sdb_entry_ex_to_hdb_entry_ex(context, &sdb_entry_ex, entry);
164 	sdb_free_entry(&sdb_entry_ex);
165 	return ret;
166 }
167 
hdb_samba4_nextkey(krb5_context context,HDB * db,unsigned flags,hdb_entry_ex * entry)168 static krb5_error_code hdb_samba4_nextkey(krb5_context context, HDB *db, unsigned flags,
169 				   hdb_entry_ex *entry)
170 {
171 	struct samba_kdc_db_context *kdc_db_ctx;
172 	struct sdb_entry_ex sdb_entry_ex = {};
173 	krb5_error_code ret;
174 
175 	kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
176 					   struct samba_kdc_db_context);
177 
178 	ret = samba_kdc_nextkey(context, kdc_db_ctx, &sdb_entry_ex);
179 	switch (ret) {
180 	case 0:
181 		break;
182 	case SDB_ERR_WRONG_REALM:
183 		return HDB_ERR_WRONG_REALM;
184 	case SDB_ERR_NOENTRY:
185 		return HDB_ERR_NOENTRY;
186 	case SDB_ERR_NOT_FOUND_HERE:
187 		return HDB_ERR_NOT_FOUND_HERE;
188 	default:
189 		return ret;
190 	}
191 
192 	ret = sdb_entry_ex_to_hdb_entry_ex(context, &sdb_entry_ex, entry);
193 	sdb_free_entry(&sdb_entry_ex);
194 	return ret;
195 }
196 
hdb_samba4_destroy(krb5_context context,HDB * db)197 static krb5_error_code hdb_samba4_destroy(krb5_context context, HDB *db)
198 {
199 	talloc_free(db);
200 	return 0;
201 }
202 
203 static krb5_error_code
hdb_samba4_check_constrained_delegation(krb5_context context,HDB * db,hdb_entry_ex * entry,krb5_const_principal target_principal)204 hdb_samba4_check_constrained_delegation(krb5_context context, HDB *db,
205 					hdb_entry_ex *entry,
206 					krb5_const_principal target_principal)
207 {
208 	struct samba_kdc_db_context *kdc_db_ctx;
209 	struct samba_kdc_entry *skdc_entry;
210 	krb5_error_code ret;
211 
212 	kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
213 					   struct samba_kdc_db_context);
214 	skdc_entry = talloc_get_type_abort(entry->ctx,
215 					   struct samba_kdc_entry);
216 
217 	ret = samba_kdc_check_s4u2proxy(context, kdc_db_ctx,
218 					skdc_entry,
219 					target_principal);
220 	switch (ret) {
221 	case 0:
222 		break;
223 	case SDB_ERR_WRONG_REALM:
224 		ret = HDB_ERR_WRONG_REALM;
225 		break;
226 	case SDB_ERR_NOENTRY:
227 		ret = HDB_ERR_NOENTRY;
228 		break;
229 	case SDB_ERR_NOT_FOUND_HERE:
230 		ret = HDB_ERR_NOT_FOUND_HERE;
231 		break;
232 	default:
233 		break;
234 	}
235 
236 	return ret;
237 }
238 
239 static krb5_error_code
hdb_samba4_check_pkinit_ms_upn_match(krb5_context context,HDB * db,hdb_entry_ex * entry,krb5_const_principal certificate_principal)240 hdb_samba4_check_pkinit_ms_upn_match(krb5_context context, HDB *db,
241 				     hdb_entry_ex *entry,
242 				     krb5_const_principal certificate_principal)
243 {
244 	struct samba_kdc_db_context *kdc_db_ctx;
245 	struct samba_kdc_entry *skdc_entry;
246 	krb5_error_code ret;
247 
248 	kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
249 					   struct samba_kdc_db_context);
250 	skdc_entry = talloc_get_type_abort(entry->ctx,
251 					   struct samba_kdc_entry);
252 
253 	ret = samba_kdc_check_pkinit_ms_upn_match(context, kdc_db_ctx,
254 						  skdc_entry,
255 						  certificate_principal);
256 	switch (ret) {
257 	case 0:
258 		break;
259 	case SDB_ERR_WRONG_REALM:
260 		ret = HDB_ERR_WRONG_REALM;
261 		break;
262 	case SDB_ERR_NOENTRY:
263 		ret = HDB_ERR_NOENTRY;
264 		break;
265 	case SDB_ERR_NOT_FOUND_HERE:
266 		ret = HDB_ERR_NOT_FOUND_HERE;
267 		break;
268 	default:
269 		break;
270 	}
271 
272 	return ret;
273 }
274 
275 static krb5_error_code
hdb_samba4_check_s4u2self(krb5_context context,HDB * db,hdb_entry_ex * entry,krb5_const_principal target_principal)276 hdb_samba4_check_s4u2self(krb5_context context, HDB *db,
277 			  hdb_entry_ex *entry,
278 			  krb5_const_principal target_principal)
279 {
280 	struct samba_kdc_db_context *kdc_db_ctx;
281 	struct samba_kdc_entry *skdc_entry;
282 	krb5_error_code ret;
283 
284 	kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
285 					   struct samba_kdc_db_context);
286 	skdc_entry = talloc_get_type_abort(entry->ctx,
287 					   struct samba_kdc_entry);
288 
289 	ret = samba_kdc_check_s4u2self(context, kdc_db_ctx,
290 				       skdc_entry,
291 				       target_principal);
292 	switch (ret) {
293 	case 0:
294 		break;
295 	case SDB_ERR_WRONG_REALM:
296 		ret = HDB_ERR_WRONG_REALM;
297 		break;
298 	case SDB_ERR_NOENTRY:
299 		ret = HDB_ERR_NOENTRY;
300 		break;
301 	case SDB_ERR_NOT_FOUND_HERE:
302 		ret = HDB_ERR_NOT_FOUND_HERE;
303 		break;
304 	default:
305 		break;
306 	}
307 
308 	return ret;
309 }
310 
reset_bad_password_netlogon(TALLOC_CTX * mem_ctx,struct samba_kdc_db_context * kdc_db_ctx,struct netr_SendToSamBase * send_to_sam)311 static void reset_bad_password_netlogon(TALLOC_CTX *mem_ctx,
312 					struct samba_kdc_db_context *kdc_db_ctx,
313 					struct netr_SendToSamBase *send_to_sam)
314 {
315 	struct dcerpc_binding_handle *irpc_handle;
316 	struct winbind_SendToSam req;
317 
318 	irpc_handle = irpc_binding_handle_by_name(mem_ctx, kdc_db_ctx->msg_ctx,
319 						  "winbind_server",
320 						  &ndr_table_winbind);
321 
322 	if (irpc_handle == NULL) {
323 		DEBUG(0, ("No winbind_server running!\n"));
324 		return;
325 	}
326 
327 	req.in.message = *send_to_sam;
328 
329 	dcerpc_winbind_SendToSam_r_send(mem_ctx, kdc_db_ctx->ev_ctx,
330 					irpc_handle, &req);
331 }
332 
send_bad_password_netlogon(TALLOC_CTX * mem_ctx,struct samba_kdc_db_context * kdc_db_ctx,struct auth_usersupplied_info * user_info)333 static void send_bad_password_netlogon(TALLOC_CTX *mem_ctx,
334 				       struct samba_kdc_db_context *kdc_db_ctx,
335 				       struct auth_usersupplied_info *user_info)
336 {
337 	struct dcerpc_binding_handle *irpc_handle;
338 	struct winbind_SamLogon req;
339 	struct netr_IdentityInfo *identity_info;
340 	struct netr_NetworkInfo *network_info;
341 
342 	irpc_handle = irpc_binding_handle_by_name(mem_ctx, kdc_db_ctx->msg_ctx,
343 						  "winbind_server",
344 						  &ndr_table_winbind);
345 	if (irpc_handle == NULL) {
346 		DEBUG(0, ("Winbind forwarding for [%s]\\[%s] failed, "
347 			  "no winbind_server running!\n",
348 			  user_info->mapped.domain_name, user_info->mapped.account_name));
349 		return;
350 	}
351 
352 	network_info = talloc_zero(mem_ctx, struct netr_NetworkInfo);
353 	if (network_info == NULL) {
354 		DEBUG(0, ("Winbind forwarding failed: No memory\n"));
355 		return;
356 	}
357 
358 	identity_info = &network_info->identity_info;
359 	req.in.logon_level = 2;
360 	req.in.logon.network = network_info;
361 
362 	identity_info->domain_name.string = user_info->mapped.domain_name;
363 	identity_info->parameter_control = user_info->logon_parameters; /* TODO */
364 	identity_info->logon_id = user_info->logon_id;
365 	identity_info->account_name.string = user_info->mapped.account_name;
366 	identity_info->workstation.string
367 		= talloc_asprintf(identity_info, "krb5-bad-pw on RODC from %s",
368 				  tsocket_address_string(user_info->remote_host,
369 							 identity_info));
370 	if (identity_info->workstation.string == NULL) {
371 		DEBUG(0, ("Winbind forwarding failed: No memory allocating workstation string\n"));
372 		return;
373 	}
374 
375 	req.in.validation_level = 3;
376 
377 	/*
378 	 * The memory in identity_info and user_info only needs to be
379 	 * valid until the end of this function call, as it will be
380 	 * pushed to NDR during this call
381 	 */
382 
383 	dcerpc_winbind_SamLogon_r_send(mem_ctx, kdc_db_ctx->ev_ctx,
384 				       irpc_handle, &req);
385 }
386 
hdb_samba4_auth_status(krb5_context context,HDB * db,hdb_entry_ex * entry,struct sockaddr * from_addr,struct timeval * start_time,const char * original_client_name,const char * auth_type,int hdb_auth_status)387 static krb5_error_code hdb_samba4_auth_status(krb5_context context, HDB *db,
388 					      hdb_entry_ex *entry,
389 					      struct sockaddr *from_addr,
390 					      struct timeval *start_time,
391 					      const char *original_client_name,
392 					      const char *auth_type,
393 					      int hdb_auth_status)
394 {
395 	struct samba_kdc_db_context *kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
396 									struct samba_kdc_db_context);
397 
398 	struct ldb_dn *domain_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
399 	uint64_t logon_id = generate_random_u64();
400 
401 	/*
402 	 * Forcing this via the NTLM auth structure is not ideal, but
403 	 * it is the most practical option right now, and ensures the
404 	 * logs are consistent, even if some elements are always NULL.
405 	 */
406 	struct auth_usersupplied_info ui = {
407 		.mapped_state = true,
408 		.was_mapped = true,
409 		.client = {
410 			.account_name = original_client_name,
411 			.domain_name = NULL,
412 		},
413 		.service_description = "Kerberos KDC",
414 		.auth_description = "ENC-TS Pre-authentication",
415 		.password_type = auth_type,
416 		.logon_id = logon_id
417 	};
418 
419 	size_t sa_socklen = 0;
420 
421 	switch (from_addr->sa_family) {
422 	case AF_INET:
423 		sa_socklen = sizeof(struct sockaddr_in);
424 		break;
425 #ifdef HAVE_IPV6
426 	case AF_INET6:
427 		sa_socklen = sizeof(struct sockaddr_in6);
428 		break;
429 #endif
430 	}
431 
432 	switch (hdb_auth_status) {
433 	case HDB_AUTHZ_SUCCESS:
434 	{
435 		TALLOC_CTX *frame = talloc_stackframe();
436 		struct samba_kdc_entry *p = talloc_get_type(entry->ctx,
437 							    struct samba_kdc_entry);
438 		struct netr_SendToSamBase *send_to_sam = NULL;
439 
440 		/*
441 		 * TODO: We could log the AS-REQ authorization success here as
442 		 * well.  However before we do that, we need to pass
443 		 * in the PAC here or re-calculate it.
444 		 */
445 		authsam_logon_success_accounting(kdc_db_ctx->samdb, p->msg,
446 						 domain_dn, true, &send_to_sam);
447 		if (kdc_db_ctx->rodc && send_to_sam != NULL) {
448 			reset_bad_password_netlogon(frame, kdc_db_ctx, send_to_sam);
449 		}
450 		talloc_free(frame);
451 		break;
452 	}
453 	case HDB_AUTH_INVALID_SIGNATURE:
454 		break;
455 	case HDB_AUTH_CORRECT_PASSWORD:
456 	case HDB_AUTH_WRONG_PASSWORD:
457 	{
458 		TALLOC_CTX *frame = talloc_stackframe();
459 		struct samba_kdc_entry *p = talloc_get_type(entry->ctx,
460 							    struct samba_kdc_entry);
461 		struct dom_sid *sid
462 			= samdb_result_dom_sid(frame, p->msg, "objectSid");
463 		const char *account_name
464 			= ldb_msg_find_attr_as_string(p->msg, "sAMAccountName", NULL);
465 		const char *domain_name = lpcfg_sam_name(p->kdc_db_ctx->lp_ctx);
466 		struct tsocket_address *remote_host;
467 		NTSTATUS status;
468 		int ret;
469 
470 		ret = tsocket_address_bsd_from_sockaddr(frame, from_addr,
471 							sa_socklen,
472 							&remote_host);
473 		if (ret != 0) {
474 			ui.remote_host = NULL;
475 		} else {
476 			ui.remote_host = remote_host;
477 		}
478 
479 		ui.mapped.account_name = account_name;
480 		ui.mapped.domain_name = domain_name;
481 
482 		if (hdb_auth_status == HDB_AUTH_WRONG_PASSWORD) {
483 			authsam_update_bad_pwd_count(kdc_db_ctx->samdb, p->msg, domain_dn);
484 			status = NT_STATUS_WRONG_PASSWORD;
485 			/*
486 			 * TODO We currently send a bad password via NETLOGON,
487 			 * however, it should probably forward the ticket to
488 			 * another KDC to allow login after password changes.
489 			 */
490 			if (kdc_db_ctx->rodc) {
491 				send_bad_password_netlogon(frame, kdc_db_ctx, &ui);
492 			}
493 		} else {
494 			status = NT_STATUS_OK;
495 		}
496 
497 		log_authentication_event(kdc_db_ctx->msg_ctx,
498 					 kdc_db_ctx->lp_ctx,
499 					 start_time,
500 					 &ui,
501 					 status,
502 					 domain_name,
503 					 account_name,
504 					 sid);
505 		TALLOC_FREE(frame);
506 		break;
507 	}
508 	case HDB_AUTH_CLIENT_UNKNOWN:
509 	{
510 		struct tsocket_address *remote_host;
511 		int ret;
512 		TALLOC_CTX *frame = talloc_stackframe();
513 		ret = tsocket_address_bsd_from_sockaddr(frame, from_addr,
514 							sa_socklen,
515 							&remote_host);
516 		if (ret != 0) {
517 			ui.remote_host = NULL;
518 		} else {
519 			ui.remote_host = remote_host;
520 		}
521 
522 		log_authentication_event(kdc_db_ctx->msg_ctx,
523 					 kdc_db_ctx->lp_ctx,
524 					 start_time,
525 					 &ui,
526 					 NT_STATUS_NO_SUCH_USER,
527 					 NULL, NULL,
528 					 NULL);
529 		TALLOC_FREE(frame);
530 		break;
531 	}
532 	}
533 	return 0;
534 }
535 
536 /* This interface is to be called by the KDC and libnet_keytab_dump,
537  * which is expecting Samba calling conventions.
538  * It is also called by a wrapper (hdb_samba4_create) from the
539  * kpasswdd -> krb5 -> keytab_hdb -> hdb code */
540 
hdb_samba4_create_kdc(struct samba_kdc_base_context * base_ctx,krb5_context context,struct HDB ** db)541 NTSTATUS hdb_samba4_create_kdc(struct samba_kdc_base_context *base_ctx,
542 			       krb5_context context, struct HDB **db)
543 {
544 	struct samba_kdc_db_context *kdc_db_ctx;
545 	NTSTATUS nt_status;
546 
547 	if (hdb_interface_version != HDB_INTERFACE_VERSION) {
548 		krb5_set_error_message(context, EINVAL, "Heimdal HDB interface version mismatch between build-time and run-time libraries!");
549 		return NT_STATUS_ERROR_DS_INCOMPATIBLE_VERSION;
550 	}
551 
552 	*db = talloc(base_ctx, HDB);
553 	if (!*db) {
554 		krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
555 		return NT_STATUS_NO_MEMORY;
556 	}
557 
558 	(*db)->hdb_master_key_set = 0;
559 	(*db)->hdb_db = NULL;
560 	(*db)->hdb_capability_flags = HDB_CAP_F_HANDLE_ENTERPRISE_PRINCIPAL;
561 
562 	nt_status = samba_kdc_setup_db_ctx(*db, base_ctx, &kdc_db_ctx);
563 	if (!NT_STATUS_IS_OK(nt_status)) {
564 		talloc_free(*db);
565 		return nt_status;
566 	}
567 	(*db)->hdb_db = kdc_db_ctx;
568 
569 	(*db)->hdb_dbc = NULL;
570 	(*db)->hdb_open = hdb_samba4_open;
571 	(*db)->hdb_close = hdb_samba4_close;
572 	(*db)->hdb_fetch_kvno = hdb_samba4_fetch_kvno;
573 	(*db)->hdb_store = hdb_samba4_store;
574 	(*db)->hdb_remove = hdb_samba4_remove;
575 	(*db)->hdb_firstkey = hdb_samba4_firstkey;
576 	(*db)->hdb_nextkey = hdb_samba4_nextkey;
577 	(*db)->hdb_lock = hdb_samba4_lock;
578 	(*db)->hdb_unlock = hdb_samba4_unlock;
579 	(*db)->hdb_rename = hdb_samba4_rename;
580 	/* we don't implement these, as we are not a lockable database */
581 	(*db)->hdb__get = NULL;
582 	(*db)->hdb__put = NULL;
583 	/* kadmin should not be used for deletes - use other tools instead */
584 	(*db)->hdb__del = NULL;
585 	(*db)->hdb_destroy = hdb_samba4_destroy;
586 
587 	(*db)->hdb_auth_status = hdb_samba4_auth_status;
588 	(*db)->hdb_check_constrained_delegation = hdb_samba4_check_constrained_delegation;
589 	(*db)->hdb_check_pkinit_ms_upn_match = hdb_samba4_check_pkinit_ms_upn_match;
590 	(*db)->hdb_check_s4u2self = hdb_samba4_check_s4u2self;
591 
592 	return NT_STATUS_OK;
593 }
594