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>
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 "kdc/kdc-glue.h"
37 #include "kdc/db-glue.h"
38 #include "auth/auth_sam.h"
41 static krb5_error_code
hdb_samba4_open(krb5_context context
, HDB
*db
, int flags
, mode_t mode
)
43 if (db
->hdb_master_key_set
) {
44 krb5_error_code ret
= HDB_ERR_NOENTRY
;
45 krb5_warnx(context
, "hdb_samba4_open: use of a master key incompatible with LDB\n");
46 krb5_set_error_message(context
, ret
, "hdb_samba4_open: use of a master key incompatible with LDB\n");
53 static krb5_error_code
hdb_samba4_close(krb5_context context
, HDB
*db
)
58 static krb5_error_code
hdb_samba4_lock(krb5_context context
, HDB
*db
, int operation
)
63 static krb5_error_code
hdb_samba4_unlock(krb5_context context
, HDB
*db
)
68 static krb5_error_code
hdb_samba4_rename(krb5_context context
, HDB
*db
, const char *new_name
)
70 return HDB_ERR_DB_INUSE
;
73 static krb5_error_code
hdb_samba4_store(krb5_context context
, HDB
*db
, unsigned flags
, hdb_entry_ex
*entry
)
75 return HDB_ERR_DB_INUSE
;
78 static krb5_error_code
hdb_samba4_remove(krb5_context context
, HDB
*db
, krb5_const_principal principal
)
80 return HDB_ERR_DB_INUSE
;
83 static krb5_error_code
hdb_samba4_fetch_kvno(krb5_context context
, HDB
*db
,
84 krb5_const_principal principal
,
87 hdb_entry_ex
*entry_ex
)
89 struct samba_kdc_db_context
*kdc_db_ctx
;
91 kdc_db_ctx
= talloc_get_type_abort(db
->hdb_db
,
92 struct samba_kdc_db_context
);
94 return samba_kdc_fetch(context
, kdc_db_ctx
, principal
, flags
, kvno
, entry_ex
);
97 static krb5_error_code
hdb_samba4_firstkey(krb5_context context
, HDB
*db
, unsigned flags
,
100 struct samba_kdc_db_context
*kdc_db_ctx
;
102 kdc_db_ctx
= talloc_get_type_abort(db
->hdb_db
,
103 struct samba_kdc_db_context
);
105 return samba_kdc_firstkey(context
, kdc_db_ctx
, entry
);
108 static krb5_error_code
hdb_samba4_nextkey(krb5_context context
, HDB
*db
, unsigned flags
,
111 struct samba_kdc_db_context
*kdc_db_ctx
;
113 kdc_db_ctx
= talloc_get_type_abort(db
->hdb_db
,
114 struct samba_kdc_db_context
);
116 return samba_kdc_nextkey(context
, kdc_db_ctx
, entry
);
119 static krb5_error_code
hdb_samba4_destroy(krb5_context context
, HDB
*db
)
125 static krb5_error_code
126 hdb_samba4_check_constrained_delegation(krb5_context context
, HDB
*db
,
128 krb5_const_principal target_principal
)
130 struct samba_kdc_db_context
*kdc_db_ctx
;
132 kdc_db_ctx
= talloc_get_type_abort(db
->hdb_db
,
133 struct samba_kdc_db_context
);
135 return samba_kdc_check_s4u2proxy(context
, kdc_db_ctx
,
140 static krb5_error_code
141 hdb_samba4_check_pkinit_ms_upn_match(krb5_context context
, HDB
*db
,
143 krb5_const_principal certificate_principal
)
145 struct samba_kdc_db_context
*kdc_db_ctx
;
147 kdc_db_ctx
= talloc_get_type_abort(db
->hdb_db
,
148 struct samba_kdc_db_context
);
150 return samba_kdc_check_pkinit_ms_upn_match(context
, kdc_db_ctx
,
152 certificate_principal
);
155 static krb5_error_code
156 hdb_samba4_check_s4u2self(krb5_context context
, HDB
*db
,
158 krb5_const_principal target_principal
)
160 struct samba_kdc_db_context
*kdc_db_ctx
;
162 kdc_db_ctx
= talloc_get_type_abort(db
->hdb_db
,
163 struct samba_kdc_db_context
);
165 return samba_kdc_check_s4u2self(context
, kdc_db_ctx
,
170 static krb5_error_code
hdb_samba4_auth_status(krb5_context context
, HDB
*db
,
174 struct samba_kdc_db_context
*kdc_db_ctx
= talloc_get_type_abort(db
->hdb_db
,
175 struct samba_kdc_db_context
);
176 struct samba_kdc_entry
*p
= talloc_get_type(entry
->ctx
, struct samba_kdc_entry
);
178 if (hdb_auth_status
== HDB_AUTH_WRONG_PASSWORD
) {
179 authsam_update_bad_pwd_count(kdc_db_ctx
->samdb
, p
->msg
, ldb_get_default_basedn(kdc_db_ctx
->samdb
));
180 } else if (hdb_auth_status
== HDB_AUTH_SUCCESS
) {
181 authsam_zero_bad_pwd_count(kdc_db_ctx
->samdb
, p
->msg
);
186 /* This interface is to be called by the KDC and libnet_keytab_dump,
187 * which is expecting Samba calling conventions.
188 * It is also called by a wrapper (hdb_samba4_create) from the
189 * kpasswdd -> krb5 -> keytab_hdb -> hdb code */
191 NTSTATUS
hdb_samba4_create_kdc(struct samba_kdc_base_context
*base_ctx
,
192 krb5_context context
, struct HDB
**db
)
194 struct samba_kdc_db_context
*kdc_db_ctx
;
197 if (hdb_interface_version
!= HDB_INTERFACE_VERSION
) {
198 krb5_set_error_message(context
, EINVAL
, "Heimdal HDB interface version mismatch between build-time and run-time libraries!");
199 return NT_STATUS_ERROR_DS_INCOMPATIBLE_VERSION
;
202 *db
= talloc(base_ctx
, HDB
);
204 krb5_set_error_message(context
, ENOMEM
, "malloc: out of memory");
205 return NT_STATUS_NO_MEMORY
;
208 (*db
)->hdb_master_key_set
= 0;
209 (*db
)->hdb_db
= NULL
;
210 (*db
)->hdb_capability_flags
= HDB_CAP_F_HANDLE_ENTERPRISE_PRINCIPAL
;
212 nt_status
= samba_kdc_setup_db_ctx(*db
, base_ctx
, &kdc_db_ctx
);
213 if (!NT_STATUS_IS_OK(nt_status
)) {
217 (*db
)->hdb_db
= kdc_db_ctx
;
219 (*db
)->hdb_dbc
= NULL
;
220 (*db
)->hdb_open
= hdb_samba4_open
;
221 (*db
)->hdb_close
= hdb_samba4_close
;
222 (*db
)->hdb_fetch_kvno
= hdb_samba4_fetch_kvno
;
223 (*db
)->hdb_store
= hdb_samba4_store
;
224 (*db
)->hdb_remove
= hdb_samba4_remove
;
225 (*db
)->hdb_firstkey
= hdb_samba4_firstkey
;
226 (*db
)->hdb_nextkey
= hdb_samba4_nextkey
;
227 (*db
)->hdb_lock
= hdb_samba4_lock
;
228 (*db
)->hdb_unlock
= hdb_samba4_unlock
;
229 (*db
)->hdb_rename
= hdb_samba4_rename
;
230 /* we don't implement these, as we are not a lockable database */
231 (*db
)->hdb__get
= NULL
;
232 (*db
)->hdb__put
= NULL
;
233 /* kadmin should not be used for deletes - use other tools instead */
234 (*db
)->hdb__del
= NULL
;
235 (*db
)->hdb_destroy
= hdb_samba4_destroy
;
237 (*db
)->hdb_auth_status
= hdb_samba4_auth_status
;
238 (*db
)->hdb_check_constrained_delegation
= hdb_samba4_check_constrained_delegation
;
239 (*db
)->hdb_check_pkinit_ms_upn_match
= hdb_samba4_check_pkinit_ms_upn_match
;
240 (*db
)->hdb_check_s4u2self
= hdb_samba4_check_s4u2self
;