s4-kdc: Use sdb in db-glue and hdb-samba4
[obnox/samba/samba-obnox.git] / source4 / kdc / hdb-samba4.c
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 <ldb.h>
40 #include "sdb.h"
41 #include "sdb_hdb.h"
42
43 static krb5_error_code hdb_samba4_open(krb5_context context, HDB *db, int flags, mode_t mode)
44 {
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");
49                 return ret;
50         }
51
52         return 0;
53 }
54
55 static krb5_error_code hdb_samba4_close(krb5_context context, HDB *db)
56 {
57         return 0;
58 }
59
60 static krb5_error_code hdb_samba4_lock(krb5_context context, HDB *db, int operation)
61 {
62         return 0;
63 }
64
65 static krb5_error_code hdb_samba4_unlock(krb5_context context, HDB *db)
66 {
67         return 0;
68 }
69
70 static krb5_error_code hdb_samba4_rename(krb5_context context, HDB *db, const char *new_name)
71 {
72         return HDB_ERR_DB_INUSE;
73 }
74
75 static krb5_error_code hdb_samba4_store(krb5_context context, HDB *db, unsigned flags, hdb_entry_ex *entry)
76 {
77         return HDB_ERR_DB_INUSE;
78 }
79
80 static krb5_error_code hdb_samba4_remove(krb5_context context, HDB *db, krb5_const_principal principal)
81 {
82         return HDB_ERR_DB_INUSE;
83 }
84
85 static krb5_error_code hdb_samba4_fetch_kvno(krb5_context context, HDB *db,
86                                              krb5_const_principal principal,
87                                              unsigned flags,
88                                              krb5_kvno kvno,
89                                              hdb_entry_ex *entry_ex)
90 {
91         struct samba_kdc_db_context *kdc_db_ctx;
92         struct sdb_entry_ex sdb_entry_ex = {};
93         krb5_error_code code, ret;
94
95         kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
96                                            struct samba_kdc_db_context);
97
98         code = samba_kdc_fetch(context,
99                                kdc_db_ctx,
100                                principal,
101                                flags,
102                                kvno,
103                                &sdb_entry_ex);
104         /*
105          * If SDB_ERR_WRONG_REALM is returned we need to process the sdb_entry
106          * to fill the principal in the HDB entry.
107          */
108         if (code != 0 && code != SDB_ERR_WRONG_REALM) {
109                 return code;
110         }
111
112         ret = sdb_entry_ex_to_hdb_entry_ex(context, &sdb_entry_ex, entry_ex);
113         sdb_free_entry(&sdb_entry_ex);
114
115         if (code == 0 && ret != 0) {
116                 code = ret;
117         }
118
119         return code;
120 }
121
122 static krb5_error_code hdb_samba4_firstkey(krb5_context context, HDB *db, unsigned flags,
123                                         hdb_entry_ex *entry)
124 {
125         struct samba_kdc_db_context *kdc_db_ctx;
126         struct sdb_entry_ex sdb_entry_ex = {};
127         krb5_error_code ret;
128
129         kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
130                                            struct samba_kdc_db_context);
131
132         ret = samba_kdc_firstkey(context, kdc_db_ctx, &sdb_entry_ex);
133         if (ret) {
134                 return ret;
135         }
136
137         ret = sdb_entry_ex_to_hdb_entry_ex(context, &sdb_entry_ex, entry);
138         sdb_free_entry(&sdb_entry_ex);
139         return ret;
140 }
141
142 static krb5_error_code hdb_samba4_nextkey(krb5_context context, HDB *db, unsigned flags,
143                                    hdb_entry_ex *entry)
144 {
145         struct samba_kdc_db_context *kdc_db_ctx;
146         struct sdb_entry_ex sdb_entry_ex = {};
147         krb5_error_code ret;
148
149         kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
150                                            struct samba_kdc_db_context);
151
152         ret = samba_kdc_nextkey(context, kdc_db_ctx, &sdb_entry_ex);
153         if (ret) {
154                 return ret;
155         }
156
157         ret = sdb_entry_ex_to_hdb_entry_ex(context, &sdb_entry_ex, entry);
158         sdb_free_entry(&sdb_entry_ex);
159         return ret;
160 }
161
162 static krb5_error_code hdb_samba4_destroy(krb5_context context, HDB *db)
163 {
164         talloc_free(db);
165         return 0;
166 }
167
168 static krb5_error_code
169 hdb_samba4_check_constrained_delegation(krb5_context context, HDB *db,
170                                         hdb_entry_ex *entry,
171                                         krb5_const_principal target_principal)
172 {
173         struct samba_kdc_db_context *kdc_db_ctx;
174         struct samba_kdc_entry *skdc_entry;
175
176         kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
177                                            struct samba_kdc_db_context);
178         skdc_entry = talloc_get_type_abort(entry->ctx,
179                                            struct samba_kdc_entry);
180
181         return samba_kdc_check_s4u2proxy(context, kdc_db_ctx,
182                                          skdc_entry,
183                                          target_principal);
184 }
185
186 static krb5_error_code
187 hdb_samba4_check_pkinit_ms_upn_match(krb5_context context, HDB *db,
188                                      hdb_entry_ex *entry,
189                                      krb5_const_principal certificate_principal)
190 {
191         struct samba_kdc_db_context *kdc_db_ctx;
192         struct samba_kdc_entry *skdc_entry;
193
194         kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
195                                            struct samba_kdc_db_context);
196         skdc_entry = talloc_get_type_abort(entry->ctx,
197                                            struct samba_kdc_entry);
198
199         return samba_kdc_check_pkinit_ms_upn_match(context, kdc_db_ctx,
200                                                    skdc_entry,
201                                                    certificate_principal);
202 }
203
204 static krb5_error_code
205 hdb_samba4_check_s4u2self(krb5_context context, HDB *db,
206                           hdb_entry_ex *entry,
207                           krb5_const_principal target_principal)
208 {
209         struct samba_kdc_db_context *kdc_db_ctx;
210         struct samba_kdc_entry *skdc_entry;
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         return samba_kdc_check_s4u2self(context, kdc_db_ctx,
218                                         skdc_entry,
219                                         target_principal);
220 }
221
222 static krb5_error_code hdb_samba4_auth_status(krb5_context context, HDB *db,
223                                               hdb_entry_ex *entry,
224                                               int hdb_auth_status)
225 {
226         struct samba_kdc_db_context *kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
227                                                                         struct samba_kdc_db_context);
228         struct samba_kdc_entry *p = talloc_get_type(entry->ctx, struct samba_kdc_entry);
229
230         if (hdb_auth_status == HDB_AUTH_WRONG_PASSWORD) {
231                 authsam_update_bad_pwd_count(kdc_db_ctx->samdb, p->msg, ldb_get_default_basedn(kdc_db_ctx->samdb));
232         } else if (hdb_auth_status == HDB_AUTH_SUCCESS) {
233                 authsam_zero_bad_pwd_count(kdc_db_ctx->samdb, p->msg);
234         }
235         return 0;
236 }
237
238 /* This interface is to be called by the KDC and libnet_keytab_dump,
239  * which is expecting Samba calling conventions.
240  * It is also called by a wrapper (hdb_samba4_create) from the
241  * kpasswdd -> krb5 -> keytab_hdb -> hdb code */
242
243 NTSTATUS hdb_samba4_create_kdc(struct samba_kdc_base_context *base_ctx,
244                                krb5_context context, struct HDB **db)
245 {
246         struct samba_kdc_db_context *kdc_db_ctx;
247         NTSTATUS nt_status;
248
249         if (hdb_interface_version != HDB_INTERFACE_VERSION) {
250                 krb5_set_error_message(context, EINVAL, "Heimdal HDB interface version mismatch between build-time and run-time libraries!");
251                 return NT_STATUS_ERROR_DS_INCOMPATIBLE_VERSION;
252         }
253
254         *db = talloc(base_ctx, HDB);
255         if (!*db) {
256                 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
257                 return NT_STATUS_NO_MEMORY;
258         }
259
260         (*db)->hdb_master_key_set = 0;
261         (*db)->hdb_db = NULL;
262         (*db)->hdb_capability_flags = HDB_CAP_F_HANDLE_ENTERPRISE_PRINCIPAL;
263
264         nt_status = samba_kdc_setup_db_ctx(*db, base_ctx, &kdc_db_ctx);
265         if (!NT_STATUS_IS_OK(nt_status)) {
266                 talloc_free(*db);
267                 return nt_status;
268         }
269         (*db)->hdb_db = kdc_db_ctx;
270
271         (*db)->hdb_dbc = NULL;
272         (*db)->hdb_open = hdb_samba4_open;
273         (*db)->hdb_close = hdb_samba4_close;
274         (*db)->hdb_fetch_kvno = hdb_samba4_fetch_kvno;
275         (*db)->hdb_store = hdb_samba4_store;
276         (*db)->hdb_remove = hdb_samba4_remove;
277         (*db)->hdb_firstkey = hdb_samba4_firstkey;
278         (*db)->hdb_nextkey = hdb_samba4_nextkey;
279         (*db)->hdb_lock = hdb_samba4_lock;
280         (*db)->hdb_unlock = hdb_samba4_unlock;
281         (*db)->hdb_rename = hdb_samba4_rename;
282         /* we don't implement these, as we are not a lockable database */
283         (*db)->hdb__get = NULL;
284         (*db)->hdb__put = NULL;
285         /* kadmin should not be used for deletes - use other tools instead */
286         (*db)->hdb__del = NULL;
287         (*db)->hdb_destroy = hdb_samba4_destroy;
288
289         (*db)->hdb_auth_status = hdb_samba4_auth_status;
290         (*db)->hdb_check_constrained_delegation = hdb_samba4_check_constrained_delegation;
291         (*db)->hdb_check_pkinit_ms_upn_match = hdb_samba4_check_pkinit_ms_upn_match;
292         (*db)->hdb_check_s4u2self = hdb_samba4_check_s4u2self;
293
294         return NT_STATUS_OK;
295 }