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