s4:kdc move db functions in their own file
[metze/samba/wip.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 "auth/auth.h"
37 #include "auth/credentials/credentials.h"
38 #include "dsdb/samdb/samdb.h"
39 #include "param/param.h"
40 #include "kdc/kdc.h"
41 #include "kdc/db-glue.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(krb5_context context, HDB *db,
86                                  krb5_const_principal principal,
87                                  unsigned flags,
88                                  hdb_entry_ex *entry_ex)
89 {
90         struct samba_kdc_db_context *kdc_db_ctx;
91
92         kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
93                                            struct samba_kdc_db_context);
94
95         return samba_kdc_fetch(context, kdc_db_ctx, principal, flags, entry_ex);
96 }
97
98 static krb5_error_code hdb_samba4_firstkey(krb5_context context, HDB *db, unsigned flags,
99                                         hdb_entry_ex *entry)
100 {
101         struct samba_kdc_db_context *kdc_db_ctx;
102
103         kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
104                                            struct samba_kdc_db_context);
105
106         return samba_kdc_firstkey(context, kdc_db_ctx, entry);
107 }
108
109 static krb5_error_code hdb_samba4_nextkey(krb5_context context, HDB *db, unsigned flags,
110                                    hdb_entry_ex *entry)
111 {
112         struct samba_kdc_db_context *kdc_db_ctx;
113
114         kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
115                                            struct samba_kdc_db_context);
116
117         return samba_kdc_nextkey(context, kdc_db_ctx, entry);
118 }
119
120 static krb5_error_code hdb_samba4_destroy(krb5_context context, HDB *db)
121 {
122         struct samba_kdc_db_context *kdc_db_ctx;
123
124         kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
125                                            struct samba_kdc_db_context);
126
127         if (kdc_db_ctx) {
128                 talloc_free(kdc_db_ctx->samdb);
129                 kdc_db_ctx->samdb = NULL;
130         }
131
132         talloc_free(db);
133         return 0;
134 }
135
136 static krb5_error_code
137 hdb_samba4_check_constrained_delegation(krb5_context context, HDB *db,
138                                         hdb_entry_ex *entry,
139                                         krb5_const_principal target_principal)
140 {
141         struct samba_kdc_db_context *kdc_db_ctx;
142
143         kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
144                                            struct samba_kdc_db_context);
145
146         return samba_kdc_check_constrained_delegation(context, kdc_db_ctx,
147                                                       entry,
148                                                       target_principal);
149 }
150
151 static krb5_error_code
152 hdb_samba4_check_pkinit_ms_upn_match(krb5_context context, HDB *db,
153                                      hdb_entry_ex *entry,
154                                      krb5_const_principal certificate_principal)
155 {
156         struct samba_kdc_db_context *kdc_db_ctx;
157
158         kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
159                                            struct samba_kdc_db_context);
160
161         return samba_kdc_check_pkinit_ms_upn_match(context, kdc_db_ctx,
162                                                    entry,
163                                                    certificate_principal);
164 }
165
166 /* This interface is to be called by the KDC and libnet_keytab_dump,
167  * which is expecting Samba calling conventions.
168  * It is also called by a wrapper (hdb_samba4_create) from the
169  * kpasswdd -> krb5 -> keytab_hdb -> hdb code */
170
171 NTSTATUS hdb_samba4_create_kdc(struct samba_kdc_base_context *base_ctx,
172                                krb5_context context, struct HDB **db)
173 {
174         struct samba_kdc_db_context *kdc_db_ctx;
175         struct auth_session_info *session_info;
176         NTSTATUS nt_status;
177
178         *db = talloc(base_ctx, HDB);
179         if (!*db) {
180                 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
181                 return NT_STATUS_NO_MEMORY;
182         }
183
184         (*db)->hdb_master_key_set = 0;
185         (*db)->hdb_db = NULL;
186         (*db)->hdb_capability_flags = 0;
187
188 #if 1
189         /* we would prefer to use system_session(), as that would
190          * allow us to share the samdb backend context with other parts of the
191          * system. For now we can't as we need to override the
192          * credentials to set CRED_DONT_USE_KERBEROS, which would
193          * break other users of the system_session */
194         DEBUG(0,("FIXME: Using new system session for hdb\n"));
195         nt_status = auth_system_session_info(*db, base_ctx->lp_ctx, &session_info);
196         if (!NT_STATUS_IS_OK(nt_status)) {
197                return nt_status;
198         }
199 #else
200         session_info = system_session(kdc_db_ctx->lp_ctx);
201         if (session_info == NULL) {
202                 return NT_STATUS_INTERNAL_ERROR;
203         }
204 #endif
205
206         /* The idea here is very simple.  Using Kerberos to
207          * authenticate the KDC to the LDAP server is higly likely to
208          * be circular.
209          *
210          * In future we may set this up to use EXERNAL and SSL
211          * certificates, for now it will almost certainly be NTLMSSP_SET_USERNAME
212         */
213
214         cli_credentials_set_kerberos_state(session_info->credentials,
215                                            CRED_DONT_USE_KERBEROS);
216
217         kdc_db_ctx = talloc_zero(*db, struct samba_kdc_db_context);
218         if (kdc_db_ctx == NULL) {
219                 return NT_STATUS_NO_MEMORY;
220         }
221         kdc_db_ctx->ev_ctx = base_ctx->ev_ctx;
222         kdc_db_ctx->lp_ctx = base_ctx->lp_ctx;
223         kdc_db_ctx->ic_ctx = lp_iconv_convenience(base_ctx->lp_ctx);
224
225         /* Setup the link to LDB */
226         kdc_db_ctx->samdb = samdb_connect(kdc_db_ctx, base_ctx->ev_ctx,
227                                           base_ctx->lp_ctx, session_info);
228         if (kdc_db_ctx->samdb == NULL) {
229                 DEBUG(1, ("hdb_samba4_create: Cannot open samdb for KDC backend!"));
230                 return NT_STATUS_CANT_ACCESS_DOMAIN_INFO;
231         }
232
233         (*db)->hdb_db = kdc_db_ctx;
234
235         (*db)->hdb_dbc = NULL;
236         (*db)->hdb_open = hdb_samba4_open;
237         (*db)->hdb_close = hdb_samba4_close;
238         (*db)->hdb_fetch = hdb_samba4_fetch;
239         (*db)->hdb_store = hdb_samba4_store;
240         (*db)->hdb_remove = hdb_samba4_remove;
241         (*db)->hdb_firstkey = hdb_samba4_firstkey;
242         (*db)->hdb_nextkey = hdb_samba4_nextkey;
243         (*db)->hdb_lock = hdb_samba4_lock;
244         (*db)->hdb_unlock = hdb_samba4_unlock;
245         (*db)->hdb_rename = hdb_samba4_rename;
246         /* we don't implement these, as we are not a lockable database */
247         (*db)->hdb__get = NULL;
248         (*db)->hdb__put = NULL;
249         /* kadmin should not be used for deletes - use other tools instead */
250         (*db)->hdb__del = NULL;
251         (*db)->hdb_destroy = hdb_samba4_destroy;
252
253         (*db)->hdb_auth_status = NULL;
254         (*db)->hdb_check_constrained_delegation = hdb_samba4_check_constrained_delegation;
255         (*db)->hdb_check_pkinit_ms_upn_match = hdb_samba4_check_pkinit_ms_upn_match;
256
257         return NT_STATUS_OK;
258 }
259
260 static krb5_error_code hdb_samba4_create(krb5_context context, struct HDB **db, const char *arg)
261 {
262         NTSTATUS nt_status;
263         void *ptr;
264         struct samba_kdc_base_context *base_ctx;
265
266         if (sscanf(arg, "&%p", &ptr) != 1) {
267                 return EINVAL;
268         }
269         base_ctx = talloc_get_type_abort(ptr, struct samba_kdc_base_context);
270         /* The global kdc_mem_ctx and kdc_lp_ctx, Disgusting, ugly hack, but it means one less private hook */
271         nt_status = hdb_samba4_create_kdc(base_ctx, context, db);
272
273         if (NT_STATUS_IS_OK(nt_status)) {
274                 return 0;
275         }
276         return EINVAL;
277 }
278
279 /* Only used in the hdb-backed keytab code
280  * for a keytab of 'samba4&<address>', to find
281  * kpasswd's key in the main DB, and to
282  * copy all the keys into a file (libnet_keytab_export)
283  *
284  * The <address> is the string form of a pointer to a talloced struct hdb_samba_context
285  */
286 struct hdb_method hdb_samba4 = {
287         .interface_version = HDB_INTERFACE_VERSION,
288         .prefix = "samba4",
289         .create = hdb_samba4_create
290 };