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 "auth/auth.h"
37 #include "auth/credentials/credentials.h"
38 #include "dsdb/samdb/samdb.h"
39 #include "param/param.h"
41 #include "kdc/db-glue.h"
43 static krb5_error_code hdb_samba4_open(krb5_context context, HDB *db, int flags, mode_t mode)
45 if (db->hdb_master_key_set) {
46 krb5_error_code ret = HDB_ERR_NOENTRY;
47 krb5_warnx(context, "hdb_samba4_open: use of a master key incompatible with LDB\n");
48 krb5_set_error_message(context, ret, "hdb_samba4_open: use of a master key incompatible with LDB\n");
55 static krb5_error_code hdb_samba4_close(krb5_context context, HDB *db)
60 static krb5_error_code hdb_samba4_lock(krb5_context context, HDB *db, int operation)
65 static krb5_error_code hdb_samba4_unlock(krb5_context context, HDB *db)
70 static krb5_error_code hdb_samba4_rename(krb5_context context, HDB *db, const char *new_name)
72 return HDB_ERR_DB_INUSE;
75 static krb5_error_code hdb_samba4_store(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
77 return HDB_ERR_DB_INUSE;
80 static krb5_error_code hdb_samba4_remove(krb5_context context, HDB *db, krb5_const_principal principal)
82 return HDB_ERR_DB_INUSE;
85 static krb5_error_code hdb_samba4_fetch(krb5_context context, HDB *db,
86 krb5_const_principal principal,
88 hdb_entry_ex *entry_ex)
90 struct samba_kdc_db_context *kdc_db_ctx;
92 kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
93 struct samba_kdc_db_context);
95 return samba_kdc_fetch(context, kdc_db_ctx, principal, flags, entry_ex);
98 static krb5_error_code hdb_samba4_firstkey(krb5_context context, HDB *db, unsigned flags,
101 struct samba_kdc_db_context *kdc_db_ctx;
103 kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
104 struct samba_kdc_db_context);
106 return samba_kdc_firstkey(context, kdc_db_ctx, entry);
109 static krb5_error_code hdb_samba4_nextkey(krb5_context context, HDB *db, unsigned flags,
112 struct samba_kdc_db_context *kdc_db_ctx;
114 kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
115 struct samba_kdc_db_context);
117 return samba_kdc_nextkey(context, kdc_db_ctx, entry);
120 static krb5_error_code hdb_samba4_destroy(krb5_context context, HDB *db)
126 static krb5_error_code
127 hdb_samba4_check_identical_client_and_server(krb5_context context, HDB *db,
129 krb5_const_principal target_principal)
131 struct samba_kdc_db_context *kdc_db_ctx;
133 kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
134 struct samba_kdc_db_context);
136 return samba_kdc_check_identical_client_and_server(context, kdc_db_ctx,
141 static krb5_error_code
142 hdb_samba4_check_pkinit_ms_upn_match(krb5_context context, HDB *db,
144 krb5_const_principal certificate_principal)
146 struct samba_kdc_db_context *kdc_db_ctx;
148 kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
149 struct samba_kdc_db_context);
151 return samba_kdc_check_pkinit_ms_upn_match(context, kdc_db_ctx,
153 certificate_principal);
156 /* This interface is to be called by the KDC and libnet_keytab_dump,
157 * which is expecting Samba calling conventions.
158 * It is also called by a wrapper (hdb_samba4_create) from the
159 * kpasswdd -> krb5 -> keytab_hdb -> hdb code */
161 NTSTATUS hdb_samba4_create_kdc(struct samba_kdc_base_context *base_ctx,
162 krb5_context context, struct HDB **db)
164 struct samba_kdc_db_context *kdc_db_ctx;
167 *db = talloc(base_ctx, HDB);
169 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
170 return NT_STATUS_NO_MEMORY;
173 (*db)->hdb_master_key_set = 0;
174 (*db)->hdb_db = NULL;
175 (*db)->hdb_capability_flags = 0;
177 nt_status = samba_kdc_setup_db_ctx(*db, base_ctx, &kdc_db_ctx);
178 if (!NT_STATUS_IS_OK(nt_status)) {
182 (*db)->hdb_db = kdc_db_ctx;
184 (*db)->hdb_dbc = NULL;
185 (*db)->hdb_open = hdb_samba4_open;
186 (*db)->hdb_close = hdb_samba4_close;
187 (*db)->hdb_fetch = hdb_samba4_fetch;
188 (*db)->hdb_store = hdb_samba4_store;
189 (*db)->hdb_remove = hdb_samba4_remove;
190 (*db)->hdb_firstkey = hdb_samba4_firstkey;
191 (*db)->hdb_nextkey = hdb_samba4_nextkey;
192 (*db)->hdb_lock = hdb_samba4_lock;
193 (*db)->hdb_unlock = hdb_samba4_unlock;
194 (*db)->hdb_rename = hdb_samba4_rename;
195 /* we don't implement these, as we are not a lockable database */
196 (*db)->hdb__get = NULL;
197 (*db)->hdb__put = NULL;
198 /* kadmin should not be used for deletes - use other tools instead */
199 (*db)->hdb__del = NULL;
200 (*db)->hdb_destroy = hdb_samba4_destroy;
202 (*db)->hdb_auth_status = NULL;
203 (*db)->hdb_check_constrained_delegation = hdb_samba4_check_identical_client_and_server;
204 (*db)->hdb_check_pkinit_ms_upn_match = hdb_samba4_check_pkinit_ms_upn_match;
205 (*db)->hdb_check_s4u2self = hdb_samba4_check_identical_client_and_server;
210 static krb5_error_code hdb_samba4_create(krb5_context context, struct HDB **db, const char *arg)
214 struct samba_kdc_base_context *base_ctx;
216 if (sscanf(arg, "&%p", &ptr) != 1) {
219 base_ctx = talloc_get_type_abort(ptr, struct samba_kdc_base_context);
220 /* The global kdc_mem_ctx and kdc_lp_ctx, Disgusting, ugly hack, but it means one less private hook */
221 nt_status = hdb_samba4_create_kdc(base_ctx, context, db);
223 if (NT_STATUS_IS_OK(nt_status)) {
229 /* Only used in the hdb-backed keytab code
230 * for a keytab of 'samba4&<address>', to find
231 * kpasswd's key in the main DB, and to
232 * copy all the keys into a file (libnet_keytab_export)
234 * The <address> is the string form of a pointer to a talloced struct hdb_samba_context
236 struct hdb_method hdb_samba4 = {
237 .interface_version = HDB_INTERFACE_VERSION,
239 .create = hdb_samba4_create