4 Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * Component: ldb update_keytabs module
25 * Description: Update keytabs whenever their matching secret record changes
27 * Author: Andrew Bartlett
31 #include "ldb_module.h"
32 #include "lib/util/dlinklist.h"
33 #include "auth/credentials/credentials.h"
34 #include "auth/credentials/credentials_krb5.h"
35 #include "system/kerberos.h"
38 struct cli_credentials *creds;
39 struct dn_list *prev, *next;
42 struct update_kt_private {
43 struct dn_list *changed_dns;
46 struct update_kt_ctx {
47 struct ldb_module *module;
48 struct ldb_request *req;
53 struct ldb_reply *op_reply;
57 static struct update_kt_ctx *update_kt_ctx_init(struct ldb_module *module,
58 struct ldb_request *req)
60 struct update_kt_ctx *ac;
62 ac = talloc_zero(req, struct update_kt_ctx);
64 ldb_oom(ldb_module_get_ctx(module));
74 /* FIXME: too many semi-async searches here for my taste, direct and indirect as
75 * cli_credentials_set_secrets() performs a sync ldb search.
76 * Just hope we are lucky and nothing breaks (using the tdb backend masks a lot
77 * of async issues). -SSS
79 static int add_modified(struct ldb_module *module, struct ldb_dn *dn, bool do_delete) {
80 struct ldb_context *ldb = ldb_module_get_ctx(module);
81 struct update_kt_private *data = talloc_get_type(ldb_module_get_private(module), struct update_kt_private);
85 struct ldb_result *res;
86 const char *attrs[] = { NULL };
90 filter = talloc_asprintf(data, "(&(dn=%s)(&(objectClass=kerberosSecret)(privateKeytab=*)))",
91 ldb_dn_get_linearized(dn));
96 ret = ldb_search(ldb, data, &res,
97 dn, LDB_SCOPE_BASE, attrs, "%s", filter);
98 if (ret != LDB_SUCCESS) {
103 if (res->count != 1) {
104 /* if it's not a kerberosSecret then we don't have anything to update */
111 item = talloc(data->changed_dns? (void *)data->changed_dns: (void *)data, struct dn_list);
117 item->creds = cli_credentials_init(item);
119 DEBUG(1, ("cli_credentials_init failed!"));
124 cli_credentials_set_conf(item->creds, ldb_get_opaque(ldb, "loadparm"));
125 status = cli_credentials_set_secrets(item->creds, ldb_get_event_context(ldb), ldb_get_opaque(ldb, "loadparm"), ldb, NULL, filter, &errstring);
127 if (NT_STATUS_IS_OK(status)) {
129 /* Ensure we don't helpfully keep an old keytab entry */
130 cli_credentials_set_kvno(item->creds, cli_credentials_get_kvno(item->creds)+2);
132 cli_credentials_set_nt_hash(item->creds, NULL,
135 DLIST_ADD_END(data->changed_dns, item, struct dn_list *);
140 static int ukt_search_modified(struct update_kt_ctx *ac);
142 static int update_kt_op_callback(struct ldb_request *req,
143 struct ldb_reply *ares)
145 struct ldb_context *ldb;
146 struct update_kt_ctx *ac;
149 ac = talloc_get_type(req->context, struct update_kt_ctx);
150 ldb = ldb_module_get_ctx(ac->module);
153 return ldb_module_done(ac->req, NULL, NULL,
154 LDB_ERR_OPERATIONS_ERROR);
156 if (ares->error != LDB_SUCCESS) {
157 return ldb_module_done(ac->req, ares->controls,
158 ares->response, ares->error);
161 if (ares->type != LDB_REPLY_DONE) {
162 ldb_set_errstring(ldb, "Invalid request type!\n");
163 return ldb_module_done(ac->req, NULL, NULL,
164 LDB_ERR_OPERATIONS_ERROR);
168 return ldb_module_done(ac->req, ares->controls,
169 ares->response, LDB_SUCCESS);
172 ac->op_reply = talloc_steal(ac, ares);
174 ret = ukt_search_modified(ac);
175 if (ret != LDB_SUCCESS) {
176 return ldb_module_done(ac->req, NULL, NULL, ret);
182 static int ukt_del_op(struct update_kt_ctx *ac)
184 struct ldb_context *ldb;
185 struct ldb_request *down_req;
188 ldb = ldb_module_get_ctx(ac->module);
190 ret = ldb_build_del_req(&down_req, ldb, ac,
193 ac, update_kt_op_callback,
195 if (ret != LDB_SUCCESS) {
198 return ldb_next_request(ac->module, down_req);
201 static int ukt_search_modified_callback(struct ldb_request *req,
202 struct ldb_reply *ares)
204 struct update_kt_ctx *ac;
207 ac = talloc_get_type(req->context, struct update_kt_ctx);
210 return ldb_module_done(ac->req, NULL, NULL,
211 LDB_ERR_OPERATIONS_ERROR);
213 if (ares->error != LDB_SUCCESS) {
214 return ldb_module_done(ac->req, ares->controls,
215 ares->response, ares->error);
218 switch (ares->type) {
219 case LDB_REPLY_ENTRY:
224 case LDB_REPLY_REFERRAL:
231 /* do the dirty sync job here :/ */
232 ret = add_modified(ac->module, ac->dn, ac->do_delete);
236 ret = ukt_del_op(ac);
237 if (ret != LDB_SUCCESS) {
238 return ldb_module_done(ac->req,
244 return ldb_module_done(ac->req, ac->op_reply->controls,
245 ac->op_reply->response, LDB_SUCCESS);
252 static int ukt_search_modified(struct update_kt_ctx *ac)
254 struct ldb_context *ldb;
255 static const char * const attrs[] = { "distinguishedName", NULL };
256 struct ldb_request *search_req;
259 ldb = ldb_module_get_ctx(ac->module);
261 ret = ldb_build_search_req(&search_req, ldb, ac,
262 ac->dn, LDB_SCOPE_BASE,
263 "(&(objectClass=kerberosSecret)"
264 "(privateKeytab=*))", attrs,
266 ac, ukt_search_modified_callback,
268 if (ret != LDB_SUCCESS) {
271 return ldb_next_request(ac->module, search_req);
276 static int update_kt_add(struct ldb_module *module, struct ldb_request *req)
278 struct ldb_context *ldb;
279 struct update_kt_ctx *ac;
280 struct ldb_request *down_req;
283 ldb = ldb_module_get_ctx(module);
285 ac = update_kt_ctx_init(module, req);
287 return ldb_operr(ldb);
290 ac->dn = req->op.add.message->dn;
292 ret = ldb_build_add_req(&down_req, ldb, ac,
295 ac, update_kt_op_callback,
297 if (ret != LDB_SUCCESS) {
301 return ldb_next_request(module, down_req);
305 static int update_kt_modify(struct ldb_module *module, struct ldb_request *req)
307 struct ldb_context *ldb;
308 struct update_kt_ctx *ac;
309 struct ldb_request *down_req;
312 ldb = ldb_module_get_ctx(module);
314 ac = update_kt_ctx_init(module, req);
316 return ldb_operr(ldb);
319 ac->dn = req->op.mod.message->dn;
321 ret = ldb_build_mod_req(&down_req, ldb, ac,
324 ac, update_kt_op_callback,
326 if (ret != LDB_SUCCESS) {
330 return ldb_next_request(module, down_req);
334 static int update_kt_delete(struct ldb_module *module, struct ldb_request *req)
336 struct update_kt_ctx *ac;
338 ac = update_kt_ctx_init(module, req);
340 return ldb_operr(ldb_module_get_ctx(module));
343 ac->dn = req->op.del.dn;
344 ac->do_delete = true;
346 return ukt_search_modified(ac);
350 static int update_kt_rename(struct ldb_module *module, struct ldb_request *req)
352 struct ldb_context *ldb;
353 struct update_kt_ctx *ac;
354 struct ldb_request *down_req;
357 ldb = ldb_module_get_ctx(module);
359 ac = update_kt_ctx_init(module, req);
361 return ldb_operr(ldb);
364 ac->dn = req->op.rename.newdn;
366 ret = ldb_build_rename_req(&down_req, ldb, ac,
367 req->op.rename.olddn,
368 req->op.rename.newdn,
370 ac, update_kt_op_callback,
372 if (ret != LDB_SUCCESS) {
376 return ldb_next_request(module, down_req);
379 /* prepare for a commit */
380 static int update_kt_prepare_commit(struct ldb_module *module)
382 struct ldb_context *ldb;
383 struct update_kt_private *data = talloc_get_type(ldb_module_get_private(module), struct update_kt_private);
386 ldb = ldb_module_get_ctx(module);
388 for (p=data->changed_dns; p; p = p->next) {
390 kret = cli_credentials_update_keytab(p->creds, ldb_get_event_context(ldb), ldb_get_opaque(ldb, "loadparm"));
392 talloc_free(data->changed_dns);
393 data->changed_dns = NULL;
394 ldb_asprintf_errstring(ldb, "Failed to update keytab: %s", error_message(kret));
395 return LDB_ERR_OPERATIONS_ERROR;
399 talloc_free(data->changed_dns);
400 data->changed_dns = NULL;
402 return ldb_next_prepare_commit(module);
405 /* end a transaction */
406 static int update_kt_del_trans(struct ldb_module *module)
408 struct update_kt_private *data = talloc_get_type(ldb_module_get_private(module), struct update_kt_private);
410 talloc_free(data->changed_dns);
411 data->changed_dns = NULL;
413 return ldb_next_del_trans(module);
416 static int update_kt_init(struct ldb_module *module)
418 struct ldb_context *ldb;
419 struct update_kt_private *data;
421 ldb = ldb_module_get_ctx(module);
423 data = talloc(module, struct update_kt_private);
428 data->changed_dns = NULL;
430 ldb_module_set_private(module, data);
432 return ldb_next_init(module);
435 _PUBLIC_ const struct ldb_module_ops ldb_update_keytab_module_ops = {
436 .name = "update_keytab",
437 .init_context = update_kt_init,
438 .add = update_kt_add,
439 .modify = update_kt_modify,
440 .rename = update_kt_rename,
441 .del = update_kt_delete,
442 .prepare_commit = update_kt_prepare_commit,
443 .del_transaction = update_kt_del_trans,