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"
36 #include "auth/kerberos/kerberos.h"
37 #include "auth/kerberos/kerberos_srv_keytab.h"
38 #include "dsdb/samdb/ldb_modules/util.h"
41 struct ldb_message *msg;
43 struct dn_list *prev, *next;
46 struct update_kt_private {
47 struct dn_list *changed_dns;
50 struct update_kt_ctx {
51 struct ldb_module *module;
52 struct ldb_request *req;
57 struct ldb_reply *op_reply;
61 static struct update_kt_ctx *update_kt_ctx_init(struct ldb_module *module,
62 struct ldb_request *req)
64 struct update_kt_ctx *ac;
66 ac = talloc_zero(req, struct update_kt_ctx);
68 ldb_oom(ldb_module_get_ctx(module));
78 /* FIXME: too many semi-async searches here for my taste, direct and indirect as
79 * cli_credentials_set_secrets() performs a sync ldb search.
80 * Just hope we are lucky and nothing breaks (using the tdb backend masks a lot
81 * of async issues). -SSS
83 static int add_modified(struct ldb_module *module, struct ldb_dn *dn, bool do_delete,
84 struct ldb_request *parent)
86 struct ldb_context *ldb = ldb_module_get_ctx(module);
87 struct update_kt_private *data = talloc_get_type(ldb_module_get_private(module), struct update_kt_private);
90 struct ldb_result *res;
93 filter = talloc_asprintf(data,
94 "(&(objectClass=kerberosSecret)(privateKeytab=*))");
99 ret = dsdb_module_search(module, data, &res,
100 dn, LDB_SCOPE_BASE, NULL,
101 DSDB_FLAG_NEXT_MODULE, parent,
104 if (ret != LDB_SUCCESS) {
108 if (res->count != 1) {
109 /* if it's not a kerberosSecret then we don't have anything to update */
115 item = talloc(data->changed_dns? (void *)data->changed_dns: (void *)data, struct dn_list);
122 item->msg = talloc_steal(item, res->msgs[0]);
123 item->do_delete = do_delete;
126 DLIST_ADD_END(data->changed_dns, item, struct dn_list *);
130 static int ukt_search_modified(struct update_kt_ctx *ac);
132 static int update_kt_op_callback(struct ldb_request *req,
133 struct ldb_reply *ares)
135 struct ldb_context *ldb;
136 struct update_kt_ctx *ac;
139 ac = talloc_get_type(req->context, struct update_kt_ctx);
140 ldb = ldb_module_get_ctx(ac->module);
143 return ldb_module_done(ac->req, NULL, NULL,
144 LDB_ERR_OPERATIONS_ERROR);
146 if (ares->error != LDB_SUCCESS) {
147 return ldb_module_done(ac->req, ares->controls,
148 ares->response, ares->error);
151 if (ares->type != LDB_REPLY_DONE) {
152 ldb_set_errstring(ldb, "Invalid request type!\n");
153 return ldb_module_done(ac->req, NULL, NULL,
154 LDB_ERR_OPERATIONS_ERROR);
158 return ldb_module_done(ac->req, ares->controls,
159 ares->response, LDB_SUCCESS);
162 ac->op_reply = talloc_steal(ac, ares);
164 ret = ukt_search_modified(ac);
165 if (ret != LDB_SUCCESS) {
166 return ldb_module_done(ac->req, NULL, NULL, ret);
172 static int ukt_del_op(struct update_kt_ctx *ac)
174 struct ldb_context *ldb;
175 struct ldb_request *down_req;
178 ldb = ldb_module_get_ctx(ac->module);
180 ret = ldb_build_del_req(&down_req, ldb, ac,
183 ac, update_kt_op_callback,
185 LDB_REQ_SET_LOCATION(down_req);
186 if (ret != LDB_SUCCESS) {
189 return ldb_next_request(ac->module, down_req);
192 static int ukt_search_modified_callback(struct ldb_request *req,
193 struct ldb_reply *ares)
195 struct update_kt_ctx *ac;
198 ac = talloc_get_type(req->context, struct update_kt_ctx);
201 return ldb_module_done(ac->req, NULL, NULL,
202 LDB_ERR_OPERATIONS_ERROR);
204 if (ares->error != LDB_SUCCESS) {
205 return ldb_module_done(ac->req, ares->controls,
206 ares->response, ares->error);
209 switch (ares->type) {
210 case LDB_REPLY_ENTRY:
215 case LDB_REPLY_REFERRAL:
222 /* do the dirty sync job here :/ */
223 ret = add_modified(ac->module, ac->dn, ac->do_delete, ac->req);
227 ret = ukt_del_op(ac);
228 if (ret != LDB_SUCCESS) {
229 return ldb_module_done(ac->req,
235 return ldb_module_done(ac->req, ac->op_reply->controls,
236 ac->op_reply->response, LDB_SUCCESS);
243 static int ukt_search_modified(struct update_kt_ctx *ac)
245 struct ldb_context *ldb;
246 static const char * const no_attrs[] = { NULL };
247 struct ldb_request *search_req;
250 ldb = ldb_module_get_ctx(ac->module);
252 ret = ldb_build_search_req(&search_req, ldb, ac,
253 ac->dn, LDB_SCOPE_BASE,
254 "(&(objectClass=kerberosSecret)"
255 "(privateKeytab=*))", no_attrs,
257 ac, ukt_search_modified_callback,
259 LDB_REQ_SET_LOCATION(search_req);
260 if (ret != LDB_SUCCESS) {
263 return ldb_next_request(ac->module, search_req);
268 static int update_kt_add(struct ldb_module *module, struct ldb_request *req)
270 struct ldb_context *ldb;
271 struct update_kt_ctx *ac;
272 struct ldb_request *down_req;
275 ldb = ldb_module_get_ctx(module);
277 ac = update_kt_ctx_init(module, req);
279 return ldb_operr(ldb);
282 ac->dn = req->op.add.message->dn;
284 ret = ldb_build_add_req(&down_req, ldb, ac,
287 ac, update_kt_op_callback,
289 LDB_REQ_SET_LOCATION(down_req);
290 if (ret != LDB_SUCCESS) {
294 return ldb_next_request(module, down_req);
298 static int update_kt_modify(struct ldb_module *module, struct ldb_request *req)
300 struct ldb_context *ldb;
301 struct update_kt_ctx *ac;
302 struct ldb_request *down_req;
305 ldb = ldb_module_get_ctx(module);
307 ac = update_kt_ctx_init(module, req);
309 return ldb_operr(ldb);
312 ac->dn = req->op.mod.message->dn;
314 ret = ldb_build_mod_req(&down_req, ldb, ac,
317 ac, update_kt_op_callback,
319 LDB_REQ_SET_LOCATION(down_req);
320 if (ret != LDB_SUCCESS) {
324 return ldb_next_request(module, down_req);
328 static int update_kt_delete(struct ldb_module *module, struct ldb_request *req)
330 struct update_kt_ctx *ac;
332 ac = update_kt_ctx_init(module, req);
334 return ldb_operr(ldb_module_get_ctx(module));
337 ac->dn = req->op.del.dn;
338 ac->do_delete = true;
340 return ukt_search_modified(ac);
344 static int update_kt_rename(struct ldb_module *module, struct ldb_request *req)
346 struct ldb_context *ldb;
347 struct update_kt_ctx *ac;
348 struct ldb_request *down_req;
351 ldb = ldb_module_get_ctx(module);
353 ac = update_kt_ctx_init(module, req);
355 return ldb_operr(ldb);
358 ac->dn = req->op.rename.newdn;
360 ret = ldb_build_rename_req(&down_req, ldb, ac,
361 req->op.rename.olddn,
362 req->op.rename.newdn,
364 ac, update_kt_op_callback,
366 LDB_REQ_SET_LOCATION(down_req);
367 if (ret != LDB_SUCCESS) {
371 return ldb_next_request(module, down_req);
374 /* prepare for a commit */
375 static int update_kt_prepare_commit(struct ldb_module *module)
377 struct ldb_context *ldb = ldb_module_get_ctx(module);
378 struct update_kt_private *data = talloc_get_type(ldb_module_get_private(module), struct update_kt_private);
380 struct smb_krb5_context *smb_krb5_context;
381 int krb5_ret = smb_krb5_init_context(data, ldb_get_event_context(ldb), ldb_get_opaque(ldb, "loadparm"),
384 talloc_free(data->changed_dns);
385 data->changed_dns = NULL;
386 ldb_asprintf_errstring(ldb, "Failed to setup krb5_context: %s", error_message(krb5_ret));
387 return LDB_ERR_OPERATIONS_ERROR;
390 ldb = ldb_module_get_ctx(module);
392 for (p=data->changed_dns; p; p = p->next) {
393 const char *error_string;
394 krb5_ret = smb_krb5_update_keytab(data, smb_krb5_context, ldb, p->msg, p->do_delete, &error_string);
396 talloc_free(data->changed_dns);
397 data->changed_dns = NULL;
398 ldb_asprintf_errstring(ldb, "Failed to update keytab from entry %s in %s: %s",
399 ldb_dn_get_linearized(p->msg->dn),
400 (const char *)ldb_get_opaque(ldb, "ldb_url"),
402 return LDB_ERR_OPERATIONS_ERROR;
406 talloc_free(data->changed_dns);
407 data->changed_dns = NULL;
409 return ldb_next_prepare_commit(module);
412 /* end a transaction */
413 static int update_kt_del_trans(struct ldb_module *module)
415 struct update_kt_private *data = talloc_get_type(ldb_module_get_private(module), struct update_kt_private);
417 talloc_free(data->changed_dns);
418 data->changed_dns = NULL;
420 return ldb_next_del_trans(module);
423 static int update_kt_init(struct ldb_module *module)
425 struct ldb_context *ldb;
426 struct update_kt_private *data;
428 ldb = ldb_module_get_ctx(module);
430 data = talloc(module, struct update_kt_private);
435 data->changed_dns = NULL;
437 ldb_module_set_private(module, data);
439 return ldb_next_init(module);
442 static const struct ldb_module_ops ldb_update_keytab_module_ops = {
443 .name = "update_keytab",
444 .init_context = update_kt_init,
445 .add = update_kt_add,
446 .modify = update_kt_modify,
447 .rename = update_kt_rename,
448 .del = update_kt_delete,
449 .prepare_commit = update_kt_prepare_commit,
450 .del_transaction = update_kt_del_trans,
453 int ldb_update_keytab_module_init(const char *version)
455 LDB_MODULE_CHECK_VERSION(version);
456 return ldb_register_module(&ldb_update_keytab_module_ops);