s4:auth: Add password lockout support to the AD DC
[samba.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
132         kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
133                                            struct samba_kdc_db_context);
134
135         return samba_kdc_check_s4u2proxy(context, kdc_db_ctx,
136                                          entry,
137                                          target_principal);
138 }
139
140 static krb5_error_code
141 hdb_samba4_check_pkinit_ms_upn_match(krb5_context context, HDB *db,
142                                      hdb_entry_ex *entry,
143                                      krb5_const_principal certificate_principal)
144 {
145         struct samba_kdc_db_context *kdc_db_ctx;
146
147         kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
148                                            struct samba_kdc_db_context);
149
150         return samba_kdc_check_pkinit_ms_upn_match(context, kdc_db_ctx,
151                                                    entry,
152                                                    certificate_principal);
153 }
154
155 static krb5_error_code
156 hdb_samba4_check_s4u2self(krb5_context context, HDB *db,
157                           hdb_entry_ex *entry,
158                           krb5_const_principal target_principal)
159 {
160         struct samba_kdc_db_context *kdc_db_ctx;
161
162         kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
163                                            struct samba_kdc_db_context);
164
165         return samba_kdc_check_s4u2self(context, kdc_db_ctx,
166                                         entry,
167                                         target_principal);
168 }
169
170 static krb5_error_code hdb_samba4_auth_status(krb5_context context, HDB *db,
171                                               hdb_entry_ex *entry,
172                                               int hdb_auth_status)
173 {
174         struct samba_kdc_db_context *kdc_db_ctx = talloc_get_type_abort(db->hdb_db,
175                                                                         struct samba_kdc_db_context);
176         struct samba_kdc_entry *p = talloc_get_type(entry->ctx, struct samba_kdc_entry);
177
178         if (hdb_auth_status == HDB_AUTH_WRONG_PASSWORD) {
179                 authsam_update_bad_pwd_count(kdc_db_ctx->samdb, p->msg, ldb_get_default_basedn(kdc_db_ctx->samdb));
180         }
181         return 0;
182 }
183
184 /* This interface is to be called by the KDC and libnet_keytab_dump,
185  * which is expecting Samba calling conventions.
186  * It is also called by a wrapper (hdb_samba4_create) from the
187  * kpasswdd -> krb5 -> keytab_hdb -> hdb code */
188
189 NTSTATUS hdb_samba4_create_kdc(struct samba_kdc_base_context *base_ctx,
190                                krb5_context context, struct HDB **db)
191 {
192         struct samba_kdc_db_context *kdc_db_ctx;
193         NTSTATUS nt_status;
194
195         if (hdb_interface_version != HDB_INTERFACE_VERSION) {
196                 krb5_set_error_message(context, EINVAL, "Heimdal HDB interface version mismatch between build-time and run-time libraries!");
197                 return NT_STATUS_ERROR_DS_INCOMPATIBLE_VERSION;
198         }
199
200         *db = talloc(base_ctx, HDB);
201         if (!*db) {
202                 krb5_set_error_message(context, ENOMEM, "malloc: out of memory");
203                 return NT_STATUS_NO_MEMORY;
204         }
205
206         (*db)->hdb_master_key_set = 0;
207         (*db)->hdb_db = NULL;
208         (*db)->hdb_capability_flags = 0;
209
210         nt_status = samba_kdc_setup_db_ctx(*db, base_ctx, &kdc_db_ctx);
211         if (!NT_STATUS_IS_OK(nt_status)) {
212                 talloc_free(*db);
213                 return nt_status;
214         }
215         (*db)->hdb_db = kdc_db_ctx;
216
217         (*db)->hdb_dbc = NULL;
218         (*db)->hdb_open = hdb_samba4_open;
219         (*db)->hdb_close = hdb_samba4_close;
220         (*db)->hdb_fetch_kvno = hdb_samba4_fetch_kvno;
221         (*db)->hdb_store = hdb_samba4_store;
222         (*db)->hdb_remove = hdb_samba4_remove;
223         (*db)->hdb_firstkey = hdb_samba4_firstkey;
224         (*db)->hdb_nextkey = hdb_samba4_nextkey;
225         (*db)->hdb_lock = hdb_samba4_lock;
226         (*db)->hdb_unlock = hdb_samba4_unlock;
227         (*db)->hdb_rename = hdb_samba4_rename;
228         /* we don't implement these, as we are not a lockable database */
229         (*db)->hdb__get = NULL;
230         (*db)->hdb__put = NULL;
231         /* kadmin should not be used for deletes - use other tools instead */
232         (*db)->hdb__del = NULL;
233         (*db)->hdb_destroy = hdb_samba4_destroy;
234
235         (*db)->hdb_auth_status = hdb_samba4_auth_status;
236         (*db)->hdb_check_constrained_delegation = hdb_samba4_check_constrained_delegation;
237         (*db)->hdb_check_pkinit_ms_upn_match = hdb_samba4_check_pkinit_ms_upn_match;
238         (*db)->hdb_check_s4u2self = hdb_samba4_check_s4u2self;
239
240         return NT_STATUS_OK;
241 }