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/include/ldb_includes.h"
32 #include "auth/credentials/credentials.h"
33 #include "auth/credentials/credentials_krb5.h"
34 #include "system/kerberos.h"
35 #include "param/param.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 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);
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 delete) {
80 struct update_kt_private *data = talloc_get_type(module->private_data, struct update_kt_private);
83 struct ldb_result *res;
84 const char *attrs[] = { NULL };
88 filter = talloc_asprintf(data, "(&(dn=%s)(&(objectClass=kerberosSecret)(privateKeytab=*)))",
89 ldb_dn_get_linearized(dn));
92 return LDB_ERR_OPERATIONS_ERROR;
95 ret = ldb_search(module->ldb, data, &res,
96 dn, LDB_SCOPE_BASE, attrs, "%s", filter);
97 if (ret != LDB_SUCCESS) {
102 if (res->count != 1) {
103 /* if it's not a kerberosSecret then we don't have anything to update */
110 item = talloc(data->changed_dns? (void *)data->changed_dns: (void *)data, struct dn_list);
113 ldb_oom(module->ldb);
114 return LDB_ERR_OPERATIONS_ERROR;
117 item->creds = cli_credentials_init(item);
119 DEBUG(1, ("cli_credentials_init failed!"));
121 ldb_oom(module->ldb);
122 return LDB_ERR_OPERATIONS_ERROR;
125 cli_credentials_set_conf(item->creds, ldb_get_opaque(module->ldb, "loadparm"));
126 status = cli_credentials_set_secrets(item->creds, ldb_get_event_context(module->ldb), ldb_get_opaque(module->ldb, "loadparm"), module->ldb, NULL, filter);
128 if (NT_STATUS_IS_OK(status)) {
130 /* Ensure we don't helpfully keep an old keytab entry */
131 cli_credentials_set_kvno(item->creds, cli_credentials_get_kvno(item->creds)+2);
133 cli_credentials_set_nt_hash(item->creds, NULL,
136 DLIST_ADD_END(data->changed_dns, item, struct dn_list *);
141 static int ukt_search_modified(struct update_kt_ctx *ac);
143 static int update_kt_op_callback(struct ldb_request *req,
144 struct ldb_reply *ares)
146 struct update_kt_ctx *ac;
149 ac = talloc_get_type(req->context, struct update_kt_ctx);
152 return ldb_module_done(ac->req, NULL, NULL,
153 LDB_ERR_OPERATIONS_ERROR);
155 if (ares->error != LDB_SUCCESS) {
156 return ldb_module_done(ac->req, ares->controls,
157 ares->response, ares->error);
160 if (ares->type != LDB_REPLY_DONE) {
161 ldb_set_errstring(ac->module->ldb, "Invalid request type!\n");
162 return ldb_module_done(ac->req, NULL, NULL,
163 LDB_ERR_OPERATIONS_ERROR);
167 return ldb_module_done(ac->req, ares->controls,
168 ares->response, LDB_SUCCESS);
171 ac->op_reply = talloc_steal(ac, ares);
173 ret = ukt_search_modified(ac);
174 if (ret != LDB_SUCCESS) {
175 return ldb_module_done(ac->req, NULL, NULL, ret);
181 static int ukt_del_op(struct update_kt_ctx *ac)
183 struct ldb_request *down_req;
186 ret = ldb_build_del_req(&down_req, ac->module->ldb, ac,
189 ac, update_kt_op_callback,
191 if (ret != LDB_SUCCESS) {
194 return ldb_next_request(ac->module, down_req);
197 static int ukt_search_modified_callback(struct ldb_request *req,
198 struct ldb_reply *ares)
200 struct update_kt_ctx *ac;
203 ac = talloc_get_type(req->context, struct update_kt_ctx);
206 return ldb_module_done(ac->req, NULL, NULL,
207 LDB_ERR_OPERATIONS_ERROR);
209 if (ares->error != LDB_SUCCESS) {
210 return ldb_module_done(ac->req, ares->controls,
211 ares->response, ares->error);
214 switch (ares->type) {
215 case LDB_REPLY_ENTRY:
220 case LDB_REPLY_REFERRAL:
227 /* do the dirty sync job here :/ */
228 ret = add_modified(ac->module, ac->dn, ac->delete);
232 ret = ukt_del_op(ac);
233 if (ret != LDB_SUCCESS) {
234 return ldb_module_done(ac->req,
240 return ldb_module_done(ac->req, ac->op_reply->controls,
241 ac->op_reply->response, LDB_SUCCESS);
248 static int ukt_search_modified(struct update_kt_ctx *ac)
250 static const char * const attrs[] = { "distinguishedName", NULL };
251 struct ldb_request *search_req;
254 ret = ldb_build_search_req(&search_req, ac->module->ldb, ac,
255 ac->dn, LDB_SCOPE_BASE,
256 "(&(objectClass=kerberosSecret)"
257 "(privateKeytab=*))", attrs,
259 ac, ukt_search_modified_callback,
261 if (ret != LDB_SUCCESS) {
264 return ldb_next_request(ac->module, search_req);
269 static int update_kt_add(struct ldb_module *module, struct ldb_request *req)
271 struct update_kt_ctx *ac;
272 struct ldb_request *down_req;
275 ac = update_kt_ctx_init(module, req);
277 return LDB_ERR_OPERATIONS_ERROR;
280 ac->dn = req->op.add.message->dn;
282 ret = ldb_build_add_req(&down_req, module->ldb, ac,
285 ac, update_kt_op_callback,
287 if (ret != LDB_SUCCESS) {
291 return ldb_next_request(module, down_req);
295 static int update_kt_modify(struct ldb_module *module, struct ldb_request *req)
297 struct update_kt_ctx *ac;
298 struct ldb_request *down_req;
301 ac = update_kt_ctx_init(module, req);
303 return LDB_ERR_OPERATIONS_ERROR;
306 ac->dn = req->op.mod.message->dn;
308 ret = ldb_build_mod_req(&down_req, module->ldb, ac,
311 ac, update_kt_op_callback,
313 if (ret != LDB_SUCCESS) {
317 return ldb_next_request(module, down_req);
321 static int update_kt_delete(struct ldb_module *module, struct ldb_request *req)
323 struct update_kt_ctx *ac;
325 ac = update_kt_ctx_init(module, req);
327 return LDB_ERR_OPERATIONS_ERROR;
330 ac->dn = req->op.del.dn;
333 return ukt_search_modified(ac);
337 static int update_kt_rename(struct ldb_module *module, struct ldb_request *req)
339 struct update_kt_ctx *ac;
340 struct ldb_request *down_req;
343 ac = update_kt_ctx_init(module, req);
345 return LDB_ERR_OPERATIONS_ERROR;
348 ac->dn = req->op.rename.newdn;
350 ret = ldb_build_rename_req(&down_req, module->ldb, ac,
351 req->op.rename.olddn,
352 req->op.rename.newdn,
354 ac, update_kt_op_callback,
356 if (ret != LDB_SUCCESS) {
360 return ldb_next_request(module, down_req);
363 /* end a transaction */
364 static int update_kt_end_trans(struct ldb_module *module)
366 struct update_kt_private *data = talloc_get_type(module->private_data, struct update_kt_private);
369 for (p=data->changed_dns; p; p = p->next) {
371 kret = cli_credentials_update_keytab(p->creds, ldb_get_event_context(module->ldb), ldb_get_opaque(module->ldb, "loadparm"));
373 talloc_free(data->changed_dns);
374 data->changed_dns = NULL;
375 ldb_asprintf_errstring(module->ldb, "Failed to update keytab: %s", error_message(kret));
376 return LDB_ERR_OPERATIONS_ERROR;
380 talloc_free(data->changed_dns);
381 data->changed_dns = NULL;
383 return ldb_next_end_trans(module);
386 /* end a transaction */
387 static int update_kt_del_trans(struct ldb_module *module)
389 struct update_kt_private *data = talloc_get_type(module->private_data, struct update_kt_private);
391 talloc_free(data->changed_dns);
392 data->changed_dns = NULL;
394 return ldb_next_del_trans(module);
397 static int update_kt_init(struct ldb_module *module)
399 struct update_kt_private *data;
401 data = talloc(module, struct update_kt_private);
403 ldb_oom(module->ldb);
404 return LDB_ERR_OPERATIONS_ERROR;
407 module->private_data = data;
408 data->changed_dns = NULL;
410 return ldb_next_init(module);
413 _PUBLIC_ const struct ldb_module_ops ldb_update_keytab_module_ops = {
414 .name = "update_keytab",
415 .init_context = update_kt_init,
416 .add = update_kt_add,
417 .modify = update_kt_modify,
418 .rename = update_kt_rename,
419 .del = update_kt_delete,
420 .end_transaction = update_kt_end_trans,
421 .del_transaction = update_kt_del_trans,