xattr.id: Fix a typo
[Samba.git] / source4 / kdc / hdb-samba4.c
blob552eeeedf6b378fb85c6bd522c8eea73f513adb2
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.
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 "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"
49 static krb5_error_code hdb_samba4_open(krb5_context context, HDB *db, int flags, mode_t mode)
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;
58 return 0;
61 static krb5_error_code hdb_samba4_close(krb5_context context, HDB *db)
63 return 0;
66 static krb5_error_code hdb_samba4_lock(krb5_context context, HDB *db, int operation)
68 return 0;
71 static krb5_error_code hdb_samba4_unlock(krb5_context context, HDB *db)
73 return 0;
76 static krb5_error_code hdb_samba4_rename(krb5_context context, HDB *db, const char *new_name)
78 return HDB_ERR_DB_INUSE;
81 static krb5_error_code hdb_samba4_store(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
83 return HDB_ERR_DB_INUSE;
86 static krb5_error_code hdb_samba4_remove(krb5_context context, HDB *db, krb5_const_principal principal)
88 return HDB_ERR_DB_INUSE;
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)
97 struct samba_kdc_db_context *kdc_db_ctx;
98 struct sdb_entry_ex sdb_entry_ex = {};
99 krb5_error_code code, ret;
101 kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
102 struct samba_kdc_db_context);
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:
116 * If SDB_ERR_WRONG_REALM is returned we need to process the
117 * sdb_entry to fill the principal in the HDB entry.
119 code = HDB_ERR_WRONG_REALM;
120 break;
121 case SDB_ERR_NOENTRY:
122 return HDB_ERR_NOENTRY;
123 default:
124 return HDB_ERR_NOT_FOUND_HERE;
127 ret = sdb_entry_ex_to_hdb_entry_ex(context, &sdb_entry_ex, entry_ex);
128 sdb_free_entry(&sdb_entry_ex);
130 if (code != 0 && ret != 0) {
131 code = ret;
134 return code;
137 static krb5_error_code hdb_samba4_firstkey(krb5_context context, HDB *db, unsigned flags,
138 hdb_entry_ex *entry)
140 struct samba_kdc_db_context *kdc_db_ctx;
141 struct sdb_entry_ex sdb_entry_ex = {};
142 krb5_error_code ret;
144 kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
145 struct samba_kdc_db_context);
147 ret = samba_kdc_firstkey(context, kdc_db_ctx, &sdb_entry_ex);
148 switch (ret) {
149 case 0:
150 break;
151 case SDB_ERR_WRONG_REALM:
152 return HDB_ERR_WRONG_REALM;
153 case SDB_ERR_NOENTRY:
154 return HDB_ERR_NOENTRY;
155 default:
156 return HDB_ERR_NOT_FOUND_HERE;
159 ret = sdb_entry_ex_to_hdb_entry_ex(context, &sdb_entry_ex, entry);
160 sdb_free_entry(&sdb_entry_ex);
161 return ret;
164 static krb5_error_code hdb_samba4_nextkey(krb5_context context, HDB *db, unsigned flags,
165 hdb_entry_ex *entry)
167 struct samba_kdc_db_context *kdc_db_ctx;
168 struct sdb_entry_ex sdb_entry_ex = {};
169 krb5_error_code ret;
171 kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
172 struct samba_kdc_db_context);
174 ret = samba_kdc_nextkey(context, kdc_db_ctx, &sdb_entry_ex);
175 switch (ret) {
176 case 0:
177 break;
178 case SDB_ERR_WRONG_REALM:
179 return HDB_ERR_WRONG_REALM;
180 case SDB_ERR_NOENTRY:
181 return HDB_ERR_NOENTRY;
182 default:
183 return HDB_ERR_NOT_FOUND_HERE;
186 ret = sdb_entry_ex_to_hdb_entry_ex(context, &sdb_entry_ex, entry);
187 sdb_free_entry(&sdb_entry_ex);
188 return ret;
191 static krb5_error_code hdb_samba4_destroy(krb5_context context, HDB *db)
193 talloc_free(db);
194 return 0;
197 static krb5_error_code
198 hdb_samba4_check_constrained_delegation(krb5_context context, HDB *db,
199 hdb_entry_ex *entry,
200 krb5_const_principal target_principal)
202 struct samba_kdc_db_context *kdc_db_ctx;
203 struct samba_kdc_entry *skdc_entry;
204 krb5_error_code ret;
206 kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
207 struct samba_kdc_db_context);
208 skdc_entry = talloc_get_type_abort(entry->ctx,
209 struct samba_kdc_entry);
211 ret = samba_kdc_check_s4u2proxy(context, kdc_db_ctx,
212 skdc_entry,
213 target_principal);
214 switch (ret) {
215 case 0:
216 break;
217 case SDB_ERR_WRONG_REALM:
218 ret = HDB_ERR_WRONG_REALM;
219 break;
220 case SDB_ERR_NOENTRY:
221 ret = HDB_ERR_NOENTRY;
222 break;
223 default:
224 ret = HDB_ERR_NOT_FOUND_HERE;
225 break;
228 return ret;
231 static krb5_error_code
232 hdb_samba4_check_pkinit_ms_upn_match(krb5_context context, HDB *db,
233 hdb_entry_ex *entry,
234 krb5_const_principal certificate_principal)
236 struct samba_kdc_db_context *kdc_db_ctx;
237 struct samba_kdc_entry *skdc_entry;
238 krb5_error_code ret;
240 kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
241 struct samba_kdc_db_context);
242 skdc_entry = talloc_get_type_abort(entry->ctx,
243 struct samba_kdc_entry);
245 ret = samba_kdc_check_pkinit_ms_upn_match(context, kdc_db_ctx,
246 skdc_entry,
247 certificate_principal);
248 switch (ret) {
249 case 0:
250 break;
251 case SDB_ERR_WRONG_REALM:
252 ret = HDB_ERR_WRONG_REALM;
253 break;
254 case SDB_ERR_NOENTRY:
255 ret = HDB_ERR_NOENTRY;
256 break;
257 default:
258 ret = HDB_ERR_NOT_FOUND_HERE;
259 break;
262 return ret;
265 static krb5_error_code
266 hdb_samba4_check_s4u2self(krb5_context context, HDB *db,
267 hdb_entry_ex *entry,
268 krb5_const_principal target_principal)
270 struct samba_kdc_db_context *kdc_db_ctx;
271 struct samba_kdc_entry *skdc_entry;
272 krb5_error_code ret;
274 kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
275 struct samba_kdc_db_context);
276 skdc_entry = talloc_get_type_abort(entry->ctx,
277 struct samba_kdc_entry);
279 ret = samba_kdc_check_s4u2self(context, kdc_db_ctx,
280 skdc_entry,
281 target_principal);
282 switch (ret) {
283 case 0:
284 break;
285 case SDB_ERR_WRONG_REALM:
286 ret = HDB_ERR_WRONG_REALM;
287 break;
288 case SDB_ERR_NOENTRY:
289 ret = HDB_ERR_NOENTRY;
290 break;
291 default:
292 ret = HDB_ERR_NOT_FOUND_HERE;
293 break;
296 return ret;
299 static void reset_bad_password_netlogon(TALLOC_CTX *mem_ctx,
300 struct samba_kdc_db_context *kdc_db_ctx,
301 struct netr_SendToSamBase *send_to_sam)
303 struct dcerpc_binding_handle *irpc_handle;
304 struct winbind_SendToSam req;
306 irpc_handle = irpc_binding_handle_by_name(mem_ctx, kdc_db_ctx->msg_ctx,
307 "winbind_server",
308 &ndr_table_winbind);
310 if (irpc_handle == NULL) {
311 DEBUG(0, ("No winbind_server running!\n"));
312 return;
315 req.in.message = *send_to_sam;
317 dcerpc_winbind_SendToSam_r_send(mem_ctx, kdc_db_ctx->ev_ctx,
318 irpc_handle, &req);
321 static void send_bad_password_netlogon(TALLOC_CTX *mem_ctx,
322 struct samba_kdc_db_context *kdc_db_ctx,
323 struct auth_usersupplied_info *user_info)
325 struct dcerpc_binding_handle *irpc_handle;
326 struct winbind_SamLogon req;
327 struct netr_IdentityInfo *identity_info;
328 struct netr_NetworkInfo *network_info;
330 irpc_handle = irpc_binding_handle_by_name(mem_ctx, kdc_db_ctx->msg_ctx,
331 "winbind_server",
332 &ndr_table_winbind);
333 if (irpc_handle == NULL) {
334 DEBUG(0, ("Winbind fowarding for [%s]\\[%s] failed, "
335 "no winbind_server running!\n",
336 user_info->mapped.domain_name, user_info->mapped.account_name));
337 return;
340 network_info = talloc_zero(mem_ctx, struct netr_NetworkInfo);
341 if (network_info == NULL) {
342 DEBUG(0, ("Winbind forwarding failed: No memory\n"));
343 return;
346 identity_info = &network_info->identity_info;
347 req.in.logon_level = 2;
348 req.in.logon.network = network_info;
350 identity_info->domain_name.string = user_info->mapped.domain_name;
351 identity_info->parameter_control = user_info->logon_parameters; /* TODO */
352 identity_info->logon_id_low = 0;
353 identity_info->logon_id_high = 0;
354 identity_info->account_name.string = user_info->mapped.account_name;
355 identity_info->workstation.string
356 = talloc_asprintf(identity_info, "krb5-bad-pw on RODC from %s",
357 tsocket_address_string(user_info->remote_host,
358 identity_info));
359 if (identity_info->workstation.string == NULL) {
360 DEBUG(0, ("Winbind forwarding failed: No memory allocating workstation string\n"));
361 return;
364 req.in.validation_level = 3;
367 * The memory in identity_info and user_info only needs to be
368 * valid until the end of this function call, as it will be
369 * pushed to NDR during this call
372 dcerpc_winbind_SamLogon_r_send(mem_ctx, kdc_db_ctx->ev_ctx,
373 irpc_handle, &req);
376 static krb5_error_code hdb_samba4_auth_status(krb5_context context, HDB *db,
377 hdb_entry_ex *entry,
378 struct sockaddr *from_addr,
379 const char *original_client_name,
380 const char *auth_type,
381 int hdb_auth_status)
383 struct samba_kdc_db_context *kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
384 struct samba_kdc_db_context);
386 struct ldb_dn *domain_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
389 * Forcing this via the NTLM auth structure is not ideal, but
390 * it is the most practical option right now, and ensures the
391 * logs are consistent, even if some elements are always NULL.
393 struct auth_usersupplied_info ui = {
394 .mapped_state = true,
395 .was_mapped = true,
396 .client = {
397 .account_name = original_client_name,
398 .domain_name = NULL,
400 .service_description = "Kerberos KDC",
401 .auth_description = "ENC-TS Pre-authentication",
402 .password_type = auth_type
405 size_t sa_socklen = 0;
407 switch (from_addr->sa_family) {
408 case AF_INET:
409 sa_socklen = sizeof(struct sockaddr_in);
410 break;
411 #ifdef HAVE_IPV6
412 case AF_INET6:
413 sa_socklen = sizeof(struct sockaddr_in6);
414 break;
415 #endif
418 switch (hdb_auth_status) {
419 case HDB_AUTHZ_SUCCESS:
421 TALLOC_CTX *frame = talloc_stackframe();
422 struct samba_kdc_entry *p = talloc_get_type(entry->ctx,
423 struct samba_kdc_entry);
424 struct netr_SendToSamBase *send_to_sam = NULL;
427 * TODO: We could log the AS-REQ authorization success here as
428 * well. However before we do that, we need to pass
429 * in the PAC here or re-calculate it.
431 authsam_logon_success_accounting(kdc_db_ctx->samdb, p->msg,
432 domain_dn, true, &send_to_sam);
433 if (kdc_db_ctx->rodc && send_to_sam != NULL) {
434 reset_bad_password_netlogon(frame, kdc_db_ctx, send_to_sam);
436 talloc_free(frame);
437 break;
439 case HDB_AUTH_INVALID_SIGNATURE:
440 break;
441 case HDB_AUTH_CORRECT_PASSWORD:
442 case HDB_AUTH_WRONG_PASSWORD:
444 TALLOC_CTX *frame = talloc_stackframe();
445 struct samba_kdc_entry *p = talloc_get_type(entry->ctx,
446 struct samba_kdc_entry);
447 struct dom_sid *sid
448 = samdb_result_dom_sid(frame, p->msg, "objectSid");
449 const char *account_name
450 = ldb_msg_find_attr_as_string(p->msg, "sAMAccountName", NULL);
451 const char *domain_name = lpcfg_sam_name(p->kdc_db_ctx->lp_ctx);
452 struct tsocket_address *remote_host;
453 NTSTATUS status;
454 int ret;
456 ret = tsocket_address_bsd_from_sockaddr(frame, from_addr,
457 sa_socklen,
458 &remote_host);
459 if (ret != 0) {
460 ui.remote_host = NULL;
461 } else {
462 ui.remote_host = remote_host;
465 ui.mapped.account_name = account_name;
466 ui.mapped.domain_name = domain_name;
468 if (hdb_auth_status == HDB_AUTH_WRONG_PASSWORD) {
469 authsam_update_bad_pwd_count(kdc_db_ctx->samdb, p->msg, domain_dn);
470 status = NT_STATUS_WRONG_PASSWORD;
472 * TODO We currently send a bad password via NETLOGON,
473 * however, it should probably forward the ticket to
474 * another KDC to allow login after password changes.
476 if (kdc_db_ctx->rodc) {
477 send_bad_password_netlogon(frame, kdc_db_ctx, &ui);
479 } else {
480 status = NT_STATUS_OK;
483 log_authentication_event(kdc_db_ctx->msg_ctx,
484 kdc_db_ctx->lp_ctx,
485 &ui,
486 status,
487 domain_name,
488 account_name,
489 NULL,
490 sid);
491 TALLOC_FREE(frame);
492 break;
494 case HDB_AUTH_CLIENT_UNKNOWN:
496 struct tsocket_address *remote_host;
497 int ret;
498 TALLOC_CTX *frame = talloc_stackframe();
499 ret = tsocket_address_bsd_from_sockaddr(frame, from_addr,
500 sa_socklen,
501 &remote_host);
502 if (ret != 0) {
503 ui.remote_host = NULL;
504 } else {
505 ui.remote_host = remote_host;
508 log_authentication_event(kdc_db_ctx->msg_ctx,
509 kdc_db_ctx->lp_ctx,
510 &ui,
511 NT_STATUS_NO_SUCH_USER,
512 NULL, NULL,
513 NULL, NULL);
514 TALLOC_FREE(frame);
515 break;
518 return 0;
521 /* This interface is to be called by the KDC and libnet_keytab_dump,
522 * which is expecting Samba calling conventions.
523 * It is also called by a wrapper (hdb_samba4_create) from the
524 * kpasswdd -> krb5 -> keytab_hdb -> hdb code */
526 NTSTATUS hdb_samba4_create_kdc(struct samba_kdc_base_context *base_ctx,
527 krb5_context context, struct HDB **db)
529 struct samba_kdc_db_context *kdc_db_ctx;
530 NTSTATUS nt_status;
532 if (hdb_interface_version != HDB_INTERFACE_VERSION) {
533 krb5_set_error_message(context, EINVAL, "Heimdal HDB interface version mismatch between build-time and run-time libraries!");
534 return NT_STATUS_ERROR_DS_INCOMPATIBLE_VERSION;
537 *db = talloc(base_ctx, HDB);
538 if (!*db) {
539 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
540 return NT_STATUS_NO_MEMORY;
543 (*db)->hdb_master_key_set = 0;
544 (*db)->hdb_db = NULL;
545 (*db)->hdb_capability_flags = HDB_CAP_F_HANDLE_ENTERPRISE_PRINCIPAL;
547 nt_status = samba_kdc_setup_db_ctx(*db, base_ctx, &kdc_db_ctx);
548 if (!NT_STATUS_IS_OK(nt_status)) {
549 talloc_free(*db);
550 return nt_status;
552 (*db)->hdb_db = kdc_db_ctx;
554 (*db)->hdb_dbc = NULL;
555 (*db)->hdb_open = hdb_samba4_open;
556 (*db)->hdb_close = hdb_samba4_close;
557 (*db)->hdb_fetch_kvno = hdb_samba4_fetch_kvno;
558 (*db)->hdb_store = hdb_samba4_store;
559 (*db)->hdb_remove = hdb_samba4_remove;
560 (*db)->hdb_firstkey = hdb_samba4_firstkey;
561 (*db)->hdb_nextkey = hdb_samba4_nextkey;
562 (*db)->hdb_lock = hdb_samba4_lock;
563 (*db)->hdb_unlock = hdb_samba4_unlock;
564 (*db)->hdb_rename = hdb_samba4_rename;
565 /* we don't implement these, as we are not a lockable database */
566 (*db)->hdb__get = NULL;
567 (*db)->hdb__put = NULL;
568 /* kadmin should not be used for deletes - use other tools instead */
569 (*db)->hdb__del = NULL;
570 (*db)->hdb_destroy = hdb_samba4_destroy;
572 (*db)->hdb_auth_status = hdb_samba4_auth_status;
573 (*db)->hdb_check_constrained_delegation = hdb_samba4_check_constrained_delegation;
574 (*db)->hdb_check_pkinit_ms_upn_match = hdb_samba4_check_pkinit_ms_upn_match;
575 (*db)->hdb_check_s4u2self = hdb_samba4_check_s4u2self;
577 return NT_STATUS_OK;