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 2 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, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * Component: ldb update_keytabs module
26 * Description: Update keytabs whenever their matching secret record changes
28 * Author: Andrew Bartlett
32 #include "ldb/include/includes.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 static int add_modified(struct ldb_module *module, struct ldb_dn *dn, BOOL delete) {
47 struct update_kt_private *data = talloc_get_type(module->private_data, struct update_kt_private);
48 struct dn_list *item = talloc(data->changed_dns? (void *)data->changed_dns: (void *)data, struct dn_list);
53 return LDB_ERR_OPERATIONS_ERROR;
56 item->creds = cli_credentials_init(item);
58 DEBUG(1, ("cli_credentials_init failed!"));
60 return LDB_ERR_OPERATIONS_ERROR;
63 cli_credentials_set_conf(item->creds);
64 /* filter = talloc_asprintf(item, "(&(&(&(objectClass=kerberosSecret)(privateKeytab=*))(|(secret=*)(ntPwdHash=*)))(distinguishedName=%s))", */
65 filter = talloc_asprintf(item, "dn=%s",
66 ldb_dn_get_linearized(dn));
67 status = cli_credentials_set_secrets(item->creds, module->ldb, NULL, filter);
69 if (NT_STATUS_IS_OK(status)) {
71 /* Ensure we don't helpfully keep an old keytab entry */
72 cli_credentials_set_kvno(item->creds, cli_credentials_get_kvno(item->creds)+2);
74 cli_credentials_set_nt_hash(item->creds, NULL,
77 DLIST_ADD_END(data->changed_dns, item, struct dn_list *);
83 static int update_kt_add(struct ldb_module *module, struct ldb_request *req)
86 ret = ldb_next_request(module, req);
87 if (ret != LDB_SUCCESS) {
90 return add_modified(module, req->op.add.message->dn, False);
94 static int update_kt_modify(struct ldb_module *module, struct ldb_request *req)
97 ret = ldb_next_request(module, req);
98 if (ret != LDB_SUCCESS) {
101 return add_modified(module, req->op.mod.message->dn, False);
105 static int update_kt_delete(struct ldb_module *module, struct ldb_request *req)
108 /* Before we delete it, record the details */
109 ret = add_modified(module, req->op.del.dn, True);
110 if (ret != LDB_SUCCESS) {
113 return ldb_next_request(module, req);
117 static int update_kt_rename(struct ldb_module *module, struct ldb_request *req)
120 ret = ldb_next_request(module, req);
121 if (ret != LDB_SUCCESS) {
124 return add_modified(module, req->op.rename.newdn, False);
127 /* end a transaction */
128 static int update_kt_end_trans(struct ldb_module *module)
130 struct update_kt_private *data = talloc_get_type(module->private_data, struct update_kt_private);
133 for (p=data->changed_dns; p; p = p->next) {
135 kret = cli_credentials_update_keytab(p->creds);
137 talloc_free(data->changed_dns);
138 data->changed_dns = NULL;
139 ldb_asprintf_errstring(module->ldb, "Failed to update keytab: %s", error_message(kret));
140 return LDB_ERR_OPERATIONS_ERROR;
144 talloc_free(data->changed_dns);
145 data->changed_dns = NULL;
146 return ldb_next_end_trans(module);
149 /* end a transaction */
150 static int update_kt_del_trans(struct ldb_module *module)
152 struct update_kt_private *data = talloc_get_type(module->private_data, struct update_kt_private);
154 talloc_free(data->changed_dns);
155 data->changed_dns = NULL;
157 return ldb_next_end_trans(module);
160 static int update_kt_init(struct ldb_module *module)
162 struct update_kt_private *data;
164 data = talloc(module, struct update_kt_private);
166 ldb_oom(module->ldb);
167 return LDB_ERR_OPERATIONS_ERROR;
170 module->private_data = data;
171 data->changed_dns = NULL;
173 return ldb_next_init(module);
176 static const struct ldb_module_ops update_kt_ops = {
177 .name = "update_keytab",
178 .init_context = update_kt_init,
179 .add = update_kt_add,
180 .modify = update_kt_modify,
181 .rename = update_kt_rename,
182 .del = update_kt_delete,
183 .end_transaction = update_kt_end_trans,
184 .del_transaction = update_kt_del_trans,
187 int ldb_update_kt_init(void)
189 return ldb_register_module(&update_kt_ops);