LDB ASYNC: samba4 modules
[abartlet/samba.git/.git] / source4 / dsdb / samdb / ldb_modules / update_keytab.c
1 /* 
2    ldb database library
3
4    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2007
5
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.
10    
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.
15    
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/>.
18 */
19
20 /*
21  *  Name: ldb
22  *
23  *  Component: ldb update_keytabs module
24  *
25  *  Description: Update keytabs whenever their matching secret record changes
26  *
27  *  Author: Andrew Bartlett
28  */
29
30 #include "includes.h"
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"
36
37 struct dn_list {
38         struct cli_credentials *creds;
39         struct dn_list *prev, *next;
40 };
41
42 struct update_kt_private {
43         struct dn_list *changed_dns;
44 };
45
46 struct update_kt_ctx {
47         struct ldb_module *module;
48         struct ldb_request *req;
49
50         struct ldb_dn *dn;
51         bool delete;
52
53         struct ldb_reply *op_reply;
54         bool found;
55 };
56
57 struct update_kt_ctx *update_kt_ctx_init(struct ldb_module *module,
58                                          struct ldb_request *req)
59 {
60         struct update_kt_ctx *ac;
61
62         ac = talloc_zero(req, struct update_kt_ctx);
63         if (ac == NULL) {
64                 ldb_oom(module->ldb);
65                 return NULL;
66         }
67
68         ac->module = module;
69         ac->req = req;
70
71         return ac;
72 }
73
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
78  */
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);
81         struct dn_list *item;
82         char *filter;
83         struct ldb_result *res;
84         const char *attrs[] = { NULL };
85         int ret;
86         NTSTATUS status;
87
88         filter = talloc_asprintf(data, "(&(dn=%s)(&(objectClass=kerberosSecret)(privateKeytab=*)))",
89                                  ldb_dn_get_linearized(dn));
90         if (!filter) {
91                 ldb_oom(module->ldb);
92                 return LDB_ERR_OPERATIONS_ERROR;
93         }
94
95         ret = ldb_search(module->ldb, data, &res,
96                          dn, LDB_SCOPE_BASE, attrs, "%s", filter);
97         if (ret != LDB_SUCCESS) {
98                 talloc_free(filter);
99                 return ret;
100         }
101
102         if (res->count != 1) {
103                 /* if it's not a kerberosSecret then we don't have anything to update */
104                 talloc_free(res);
105                 talloc_free(filter);
106                 return LDB_SUCCESS;
107         }
108         talloc_free(res);
109
110         item = talloc(data->changed_dns? (void *)data->changed_dns: (void *)data, struct dn_list);
111         if (!item) {
112                 talloc_free(filter);
113                 ldb_oom(module->ldb);
114                 return LDB_ERR_OPERATIONS_ERROR;
115         }
116
117         item->creds = cli_credentials_init(item);
118         if (!item->creds) {
119                 DEBUG(1, ("cli_credentials_init failed!"));
120                 talloc_free(filter);
121                 ldb_oom(module->ldb);
122                 return LDB_ERR_OPERATIONS_ERROR;
123         }
124
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);
127         talloc_free(filter);
128         if (NT_STATUS_IS_OK(status)) {
129                 if (delete) {
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); 
132                         /* Wipe passwords */
133                         cli_credentials_set_nt_hash(item->creds, NULL, 
134                                                     CRED_SPECIFIED);
135                 }
136                 DLIST_ADD_END(data->changed_dns, item, struct dn_list *);
137         }
138         return LDB_SUCCESS;
139 }
140
141 static int ukt_search_modified(struct update_kt_ctx *ac);
142
143 static int update_kt_op_callback(struct ldb_request *req,
144                                  struct ldb_reply *ares)
145 {
146         struct update_kt_ctx *ac;
147         int ret;
148
149         ac = talloc_get_type(req->context, struct update_kt_ctx);
150
151         if (!ares) {
152                 return ldb_module_done(ac->req, NULL, NULL,
153                                         LDB_ERR_OPERATIONS_ERROR);
154         }
155         if (ares->error != LDB_SUCCESS) {
156                 return ldb_module_done(ac->req, ares->controls,
157                                         ares->response, ares->error);
158         }
159
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);
164         }
165
166         if (ac->delete) {
167                 return ldb_module_done(ac->req, ares->controls,
168                                         ares->response, LDB_SUCCESS);
169         }
170
171         ac->op_reply = talloc_steal(ac, ares);
172
173         ret = ukt_search_modified(ac);
174         if (ret != LDB_SUCCESS) {
175                 return ldb_module_done(ac->req, NULL, NULL, ret);
176         }
177
178         return LDB_SUCCESS;
179 }
180
181 static int ukt_del_op(struct update_kt_ctx *ac)
182 {
183         struct ldb_request *down_req;
184         int ret;
185
186         ret = ldb_build_del_req(&down_req, ac->module->ldb, ac,
187                                 ac->dn,
188                                 ac->req->controls,
189                                 ac, update_kt_op_callback,
190                                 ac->req);
191         if (ret != LDB_SUCCESS) {
192                 return ret;
193         }
194         return ldb_next_request(ac->module, down_req);
195 }
196
197 static int ukt_search_modified_callback(struct ldb_request *req,
198                                         struct ldb_reply *ares)
199 {
200         struct update_kt_ctx *ac;
201         int ret;
202
203         ac = talloc_get_type(req->context, struct update_kt_ctx);
204
205         if (!ares) {
206                 return ldb_module_done(ac->req, NULL, NULL,
207                                         LDB_ERR_OPERATIONS_ERROR);
208         }
209         if (ares->error != LDB_SUCCESS) {
210                 return ldb_module_done(ac->req, ares->controls,
211                                         ares->response, ares->error);
212         }
213
214         switch (ares->type) {
215         case LDB_REPLY_ENTRY:
216
217                 ac->found = true;
218                 break;
219
220         case LDB_REPLY_REFERRAL:
221                 /* ignore */
222                 break;
223
224         case LDB_REPLY_DONE:
225
226                 if (ac->found) {
227                         /* do the dirty sync job here :/ */
228                         ret = add_modified(ac->module, ac->dn, ac->delete);
229                 }
230
231                 if (ac->delete) {
232                         ret = ukt_del_op(ac);
233                         if (ret != LDB_SUCCESS) {
234                                 return ldb_module_done(ac->req,
235                                                         NULL, NULL, ret);
236                         }
237                         break;
238                 }
239
240                 return ldb_module_done(ac->req, ac->op_reply->controls,
241                                         ac->op_reply->response, LDB_SUCCESS);
242         }
243
244         talloc_free(ares);
245         return LDB_SUCCESS;
246 }
247
248 static int ukt_search_modified(struct update_kt_ctx *ac)
249 {
250         static const char * const attrs[] = { "distinguishedName", NULL };
251         struct ldb_request *search_req;
252         int ret;
253
254         ret = ldb_build_search_req(&search_req, ac->module->ldb, ac,
255                                    ac->dn, LDB_SCOPE_BASE,
256                                    "(&(objectClass=kerberosSecret)"
257                                      "(privateKeytab=*))", attrs,
258                                    NULL,
259                                    ac, ukt_search_modified_callback,
260                                    ac->req);
261         if (ret != LDB_SUCCESS) {
262                 return ret;
263         }
264         return ldb_next_request(ac->module, search_req);
265 }
266
267
268 /* add */
269 static int update_kt_add(struct ldb_module *module, struct ldb_request *req)
270 {
271         struct update_kt_ctx *ac;
272         struct ldb_request *down_req;
273         int ret;
274
275         ac = update_kt_ctx_init(module, req);
276         if (ac == NULL) {
277                 return LDB_ERR_OPERATIONS_ERROR;
278         }
279
280         ac->dn = req->op.add.message->dn;
281
282         ret = ldb_build_add_req(&down_req, module->ldb, ac,
283                                 req->op.add.message,
284                                 req->controls,
285                                 ac, update_kt_op_callback,
286                                 req);
287         if (ret != LDB_SUCCESS) {
288                 return ret;
289         }
290
291         return ldb_next_request(module, down_req);
292 }
293
294 /* modify */
295 static int update_kt_modify(struct ldb_module *module, struct ldb_request *req)
296 {
297         struct update_kt_ctx *ac;
298         struct ldb_request *down_req;
299         int ret;
300
301         ac = update_kt_ctx_init(module, req);
302         if (ac == NULL) {
303                 return LDB_ERR_OPERATIONS_ERROR;
304         }
305
306         ac->dn = req->op.mod.message->dn;
307
308         ret = ldb_build_mod_req(&down_req, module->ldb, ac,
309                                 req->op.mod.message,
310                                 req->controls,
311                                 ac, update_kt_op_callback,
312                                 req);
313         if (ret != LDB_SUCCESS) {
314                 return ret;
315         }
316
317         return ldb_next_request(module, down_req);
318 }
319
320 /* delete */
321 static int update_kt_delete(struct ldb_module *module, struct ldb_request *req)
322 {
323         struct update_kt_ctx *ac;
324
325         ac = update_kt_ctx_init(module, req);
326         if (ac == NULL) {
327                 return LDB_ERR_OPERATIONS_ERROR;
328         }
329
330         ac->dn = req->op.del.dn;
331         ac->delete = true;
332
333         return ukt_search_modified(ac);
334 }
335
336 /* rename */
337 static int update_kt_rename(struct ldb_module *module, struct ldb_request *req)
338 {
339         struct update_kt_ctx *ac;
340         struct ldb_request *down_req;
341         int ret;
342
343         ac = update_kt_ctx_init(module, req);
344         if (ac == NULL) {
345                 return LDB_ERR_OPERATIONS_ERROR;
346         }
347
348         ac->dn = req->op.rename.newdn;
349
350         ret = ldb_build_rename_req(&down_req, module->ldb, ac,
351                                 req->op.rename.olddn,
352                                 req->op.rename.newdn,
353                                 req->controls,
354                                 ac, update_kt_op_callback,
355                                 req);
356         if (ret != LDB_SUCCESS) {
357                 return ret;
358         }
359
360         return ldb_next_request(module, down_req);
361 }
362
363 /* end a transaction */
364 static int update_kt_end_trans(struct ldb_module *module)
365 {
366         struct update_kt_private *data = talloc_get_type(module->private_data, struct update_kt_private);
367         struct dn_list *p;
368
369         for (p=data->changed_dns; p; p = p->next) {
370                 int kret;
371                 kret = cli_credentials_update_keytab(p->creds, ldb_get_event_context(module->ldb), ldb_get_opaque(module->ldb, "loadparm"));
372                 if (kret != 0) {
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;
377                 }
378         }
379
380         talloc_free(data->changed_dns);
381         data->changed_dns = NULL;
382
383         return ldb_next_end_trans(module);
384 }
385
386 /* end a transaction */
387 static int update_kt_del_trans(struct ldb_module *module)
388 {
389         struct update_kt_private *data = talloc_get_type(module->private_data, struct update_kt_private);
390
391         talloc_free(data->changed_dns);
392         data->changed_dns = NULL;
393
394         return ldb_next_del_trans(module);
395 }
396
397 static int update_kt_init(struct ldb_module *module)
398 {
399         struct update_kt_private *data;
400
401         data = talloc(module, struct update_kt_private);
402         if (data == NULL) {
403                 ldb_oom(module->ldb);
404                 return LDB_ERR_OPERATIONS_ERROR;
405         }
406
407         module->private_data = data;
408         data->changed_dns = NULL;
409
410         return ldb_next_init(module);
411 }
412
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,
422 };