mit_samba: Add function for handling bad password count
[kai/samba-autobuild/.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         ret = samba_kdc_fetch(context,
99                               kdc_db_ctx,
100                               principal,
101                               flags,
102                               kvno,
103                               &sdb_entry_ex);
104         switch (ret) {
105         case 0:
106                 code = 0;
107                 break;
108         case SDB_ERR_WRONG_REALM:
109                 /*
110                  * If SDB_ERR_WRONG_REALM is returned we need to process the
111                  * sdb_entry to fill the principal in the HDB entry.
112                  */
113                 code = HDB_ERR_WRONG_REALM;
114                 break;
115         case SDB_ERR_NOENTRY:
116                 return HDB_ERR_NOENTRY;
117         default:
118                 return HDB_ERR_NOT_FOUND_HERE;
119         }
120
121         ret = sdb_entry_ex_to_hdb_entry_ex(context, &sdb_entry_ex, entry_ex);
122         sdb_free_entry(&sdb_entry_ex);
123
124         if (code != 0 && ret != 0) {
125                 code = ret;
126         }
127
128         return code;
129 }
130
131 static krb5_error_code hdb_samba4_firstkey(krb5_context context, HDB *db, unsigned flags,
132                                         hdb_entry_ex *entry)
133 {
134         struct samba_kdc_db_context *kdc_db_ctx;
135         struct sdb_entry_ex sdb_entry_ex = {};
136         krb5_error_code ret;
137
138         kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
139                                            struct samba_kdc_db_context);
140
141         ret = samba_kdc_firstkey(context, kdc_db_ctx, &sdb_entry_ex);
142         switch (ret) {
143         case 0:
144                 break;
145         case SDB_ERR_WRONG_REALM:
146                 return HDB_ERR_WRONG_REALM;
147         case SDB_ERR_NOENTRY:
148                 return HDB_ERR_NOENTRY;
149         default:
150                 return HDB_ERR_NOT_FOUND_HERE;
151         }
152
153         ret = sdb_entry_ex_to_hdb_entry_ex(context, &sdb_entry_ex, entry);
154         sdb_free_entry(&sdb_entry_ex);
155         return ret;
156 }
157
158 static krb5_error_code hdb_samba4_nextkey(krb5_context context, HDB *db, unsigned flags,
159                                    hdb_entry_ex *entry)
160 {
161         struct samba_kdc_db_context *kdc_db_ctx;
162         struct sdb_entry_ex sdb_entry_ex = {};
163         krb5_error_code ret;
164
165         kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
166                                            struct samba_kdc_db_context);
167
168         ret = samba_kdc_nextkey(context, kdc_db_ctx, &sdb_entry_ex);
169         switch (ret) {
170         case 0:
171                 break;
172         case SDB_ERR_WRONG_REALM:
173                 return HDB_ERR_WRONG_REALM;
174         case SDB_ERR_NOENTRY:
175                 return HDB_ERR_NOENTRY;
176         default:
177                 return HDB_ERR_NOT_FOUND_HERE;
178         }
179
180         ret = sdb_entry_ex_to_hdb_entry_ex(context, &sdb_entry_ex, entry);
181         sdb_free_entry(&sdb_entry_ex);
182         return ret;
183 }
184
185 static krb5_error_code hdb_samba4_destroy(krb5_context context, HDB *db)
186 {
187         talloc_free(db);
188         return 0;
189 }
190
191 static krb5_error_code
192 hdb_samba4_check_constrained_delegation(krb5_context context, HDB *db,
193                                         hdb_entry_ex *entry,
194                                         krb5_const_principal target_principal)
195 {
196         struct samba_kdc_db_context *kdc_db_ctx;
197         struct samba_kdc_entry *skdc_entry;
198         krb5_error_code ret;
199
200         kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
201                                            struct samba_kdc_db_context);
202         skdc_entry = talloc_get_type_abort(entry->ctx,
203                                            struct samba_kdc_entry);
204
205         ret = samba_kdc_check_s4u2proxy(context, kdc_db_ctx,
206                                         skdc_entry,
207                                         target_principal);
208         switch (ret) {
209         case 0:
210                 break;
211         case SDB_ERR_WRONG_REALM:
212                 ret = HDB_ERR_WRONG_REALM;
213                 break;
214         case SDB_ERR_NOENTRY:
215                 ret = HDB_ERR_NOENTRY;
216                 break;
217         default:
218                 ret = HDB_ERR_NOT_FOUND_HERE;
219                 break;
220         }
221
222         return ret;
223 }
224
225 static krb5_error_code
226 hdb_samba4_check_pkinit_ms_upn_match(krb5_context context, HDB *db,
227                                      hdb_entry_ex *entry,
228                                      krb5_const_principal certificate_principal)
229 {
230         struct samba_kdc_db_context *kdc_db_ctx;
231         struct samba_kdc_entry *skdc_entry;
232         krb5_error_code ret;
233
234         kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
235                                            struct samba_kdc_db_context);
236         skdc_entry = talloc_get_type_abort(entry->ctx,
237                                            struct samba_kdc_entry);
238
239         ret = samba_kdc_check_pkinit_ms_upn_match(context, kdc_db_ctx,
240                                                   skdc_entry,
241                                                   certificate_principal);
242         switch (ret) {
243         case 0:
244                 break;
245         case SDB_ERR_WRONG_REALM:
246                 ret = HDB_ERR_WRONG_REALM;
247                 break;
248         case SDB_ERR_NOENTRY:
249                 ret = HDB_ERR_NOENTRY;
250                 break;
251         default:
252                 ret = HDB_ERR_NOT_FOUND_HERE;
253                 break;
254         }
255
256         return ret;
257 }
258
259 static krb5_error_code
260 hdb_samba4_check_s4u2self(krb5_context context, HDB *db,
261                           hdb_entry_ex *entry,
262                           krb5_const_principal target_principal)
263 {
264         struct samba_kdc_db_context *kdc_db_ctx;
265         struct samba_kdc_entry *skdc_entry;
266         krb5_error_code ret;
267
268         kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
269                                            struct samba_kdc_db_context);
270         skdc_entry = talloc_get_type_abort(entry->ctx,
271                                            struct samba_kdc_entry);
272
273         ret = samba_kdc_check_s4u2self(context, kdc_db_ctx,
274                                        skdc_entry,
275                                        target_principal);
276         switch (ret) {
277         case 0:
278                 break;
279         case SDB_ERR_WRONG_REALM:
280                 ret = HDB_ERR_WRONG_REALM;
281                 break;
282         case SDB_ERR_NOENTRY:
283                 ret = HDB_ERR_NOENTRY;
284                 break;
285         default:
286                 ret = HDB_ERR_NOT_FOUND_HERE;
287                 break;
288         }
289
290         return ret;
291 }
292
293 static krb5_error_code hdb_samba4_auth_status(krb5_context context, HDB *db,
294                                               hdb_entry_ex *entry,
295                                               int hdb_auth_status)
296 {
297         struct samba_kdc_db_context *kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
298                                                                         struct samba_kdc_db_context);
299         struct samba_kdc_entry *p = talloc_get_type(entry->ctx, struct samba_kdc_entry);
300
301         struct ldb_dn *domain_dn = ldb_get_default_basedn(kdc_db_ctx->samdb);
302
303         if (hdb_auth_status == HDB_AUTH_WRONG_PASSWORD) {
304                 authsam_update_bad_pwd_count(kdc_db_ctx->samdb, p->msg, domain_dn);
305         } else if (hdb_auth_status == HDB_AUTH_SUCCESS) {
306                 authsam_logon_success_accounting(kdc_db_ctx->samdb, p->msg,
307                                                  domain_dn, true);
308         }
309         return 0;
310 }
311
312 /* This interface is to be called by the KDC and libnet_keytab_dump,
313  * which is expecting Samba calling conventions.
314  * It is also called by a wrapper (hdb_samba4_create) from the
315  * kpasswdd -> krb5 -> keytab_hdb -> hdb code */
316
317 NTSTATUS hdb_samba4_create_kdc(struct samba_kdc_base_context *base_ctx,
318                                krb5_context context, struct HDB **db)
319 {
320         struct samba_kdc_db_context *kdc_db_ctx;
321         NTSTATUS nt_status;
322
323         if (hdb_interface_version != HDB_INTERFACE_VERSION) {
324                 krb5_set_error_message(context, EINVAL, "Heimdal HDB interface version mismatch between build-time and run-time libraries!");
325                 return NT_STATUS_ERROR_DS_INCOMPATIBLE_VERSION;
326         }
327
328         *db = talloc(base_ctx, HDB);
329         if (!*db) {
330                 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
331                 return NT_STATUS_NO_MEMORY;
332         }
333
334         (*db)->hdb_master_key_set = 0;
335         (*db)->hdb_db = NULL;
336         (*db)->hdb_capability_flags = HDB_CAP_F_HANDLE_ENTERPRISE_PRINCIPAL;
337
338         nt_status = samba_kdc_setup_db_ctx(*db, base_ctx, &kdc_db_ctx);
339         if (!NT_STATUS_IS_OK(nt_status)) {
340                 talloc_free(*db);
341                 return nt_status;
342         }
343         (*db)->hdb_db = kdc_db_ctx;
344
345         (*db)->hdb_dbc = NULL;
346         (*db)->hdb_open = hdb_samba4_open;
347         (*db)->hdb_close = hdb_samba4_close;
348         (*db)->hdb_fetch_kvno = hdb_samba4_fetch_kvno;
349         (*db)->hdb_store = hdb_samba4_store;
350         (*db)->hdb_remove = hdb_samba4_remove;
351         (*db)->hdb_firstkey = hdb_samba4_firstkey;
352         (*db)->hdb_nextkey = hdb_samba4_nextkey;
353         (*db)->hdb_lock = hdb_samba4_lock;
354         (*db)->hdb_unlock = hdb_samba4_unlock;
355         (*db)->hdb_rename = hdb_samba4_rename;
356         /* we don't implement these, as we are not a lockable database */
357         (*db)->hdb__get = NULL;
358         (*db)->hdb__put = NULL;
359         /* kadmin should not be used for deletes - use other tools instead */
360         (*db)->hdb__del = NULL;
361         (*db)->hdb_destroy = hdb_samba4_destroy;
362
363         (*db)->hdb_auth_status = hdb_samba4_auth_status;
364         (*db)->hdb_check_constrained_delegation = hdb_samba4_check_constrained_delegation;
365         (*db)->hdb_check_pkinit_ms_upn_match = hdb_samba4_check_pkinit_ms_upn_match;
366         (*db)->hdb_check_s4u2self = hdb_samba4_check_s4u2self;
367
368         return NT_STATUS_OK;
369 }