071974f5e40af983e67c0166e52d52083ac33ef0
[kai/samba.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_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
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 do_delete;
52
53         struct ldb_reply *op_reply;
54         bool found;
55 };
56
57 static 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(ldb_module_get_ctx(module));
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 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);
82         struct dn_list *item;
83         char *filter;
84         char *errstring;
85         struct ldb_result *res;
86         const char *attrs[] = { NULL };
87         int ret;
88         NTSTATUS status;
89
90         filter = talloc_asprintf(data, "(&(dn=%s)(&(objectClass=kerberosSecret)(privateKeytab=*)))",
91                                  ldb_dn_get_linearized(dn));
92         if (!filter) {
93                 return ldb_oom(ldb);
94         }
95
96         ret = ldb_search(ldb, data, &res,
97                          dn, LDB_SCOPE_BASE, attrs, "%s", filter);
98         if (ret != LDB_SUCCESS) {
99                 talloc_free(filter);
100                 return ret;
101         }
102
103         if (res->count != 1) {
104                 /* if it's not a kerberosSecret then we don't have anything to update */
105                 talloc_free(res);
106                 talloc_free(filter);
107                 return LDB_SUCCESS;
108         }
109         talloc_free(res);
110
111         item = talloc(data->changed_dns? (void *)data->changed_dns: (void *)data, struct dn_list);
112         if (!item) {
113                 talloc_free(filter);
114                 return ldb_oom(ldb);
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                 return ldb_oom(ldb);
122         }
123
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);
126         talloc_free(filter);
127         if (NT_STATUS_IS_OK(status)) {
128                 if (do_delete) {
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); 
131                         /* Wipe passwords */
132                         cli_credentials_set_nt_hash(item->creds, NULL, 
133                                                     CRED_SPECIFIED);
134                 }
135                 DLIST_ADD_END(data->changed_dns, item, struct dn_list *);
136         }
137         return LDB_SUCCESS;
138 }
139
140 static int ukt_search_modified(struct update_kt_ctx *ac);
141
142 static int update_kt_op_callback(struct ldb_request *req,
143                                  struct ldb_reply *ares)
144 {
145         struct ldb_context *ldb;
146         struct update_kt_ctx *ac;
147         int ret;
148
149         ac = talloc_get_type(req->context, struct update_kt_ctx);
150         ldb = ldb_module_get_ctx(ac->module);
151
152         if (!ares) {
153                 return ldb_module_done(ac->req, NULL, NULL,
154                                         LDB_ERR_OPERATIONS_ERROR);
155         }
156         if (ares->error != LDB_SUCCESS) {
157                 return ldb_module_done(ac->req, ares->controls,
158                                         ares->response, ares->error);
159         }
160
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);
165         }
166
167         if (ac->do_delete) {
168                 return ldb_module_done(ac->req, ares->controls,
169                                         ares->response, LDB_SUCCESS);
170         }
171
172         ac->op_reply = talloc_steal(ac, ares);
173
174         ret = ukt_search_modified(ac);
175         if (ret != LDB_SUCCESS) {
176                 return ldb_module_done(ac->req, NULL, NULL, ret);
177         }
178
179         return LDB_SUCCESS;
180 }
181
182 static int ukt_del_op(struct update_kt_ctx *ac)
183 {
184         struct ldb_context *ldb;
185         struct ldb_request *down_req;
186         int ret;
187
188         ldb = ldb_module_get_ctx(ac->module);
189
190         ret = ldb_build_del_req(&down_req, ldb, ac,
191                                 ac->dn,
192                                 ac->req->controls,
193                                 ac, update_kt_op_callback,
194                                 ac->req);
195         if (ret != LDB_SUCCESS) {
196                 return ret;
197         }
198         return ldb_next_request(ac->module, down_req);
199 }
200
201 static int ukt_search_modified_callback(struct ldb_request *req,
202                                         struct ldb_reply *ares)
203 {
204         struct update_kt_ctx *ac;
205         int ret;
206
207         ac = talloc_get_type(req->context, struct update_kt_ctx);
208
209         if (!ares) {
210                 return ldb_module_done(ac->req, NULL, NULL,
211                                         LDB_ERR_OPERATIONS_ERROR);
212         }
213         if (ares->error != LDB_SUCCESS) {
214                 return ldb_module_done(ac->req, ares->controls,
215                                         ares->response, ares->error);
216         }
217
218         switch (ares->type) {
219         case LDB_REPLY_ENTRY:
220
221                 ac->found = true;
222                 break;
223
224         case LDB_REPLY_REFERRAL:
225                 /* ignore */
226                 break;
227
228         case LDB_REPLY_DONE:
229
230                 if (ac->found) {
231                         /* do the dirty sync job here :/ */
232                         ret = add_modified(ac->module, ac->dn, ac->do_delete);
233                 }
234
235                 if (ac->do_delete) {
236                         ret = ukt_del_op(ac);
237                         if (ret != LDB_SUCCESS) {
238                                 return ldb_module_done(ac->req,
239                                                         NULL, NULL, ret);
240                         }
241                         break;
242                 }
243
244                 return ldb_module_done(ac->req, ac->op_reply->controls,
245                                         ac->op_reply->response, LDB_SUCCESS);
246         }
247
248         talloc_free(ares);
249         return LDB_SUCCESS;
250 }
251
252 static int ukt_search_modified(struct update_kt_ctx *ac)
253 {
254         struct ldb_context *ldb;
255         static const char * const attrs[] = { "distinguishedName", NULL };
256         struct ldb_request *search_req;
257         int ret;
258
259         ldb = ldb_module_get_ctx(ac->module);
260
261         ret = ldb_build_search_req(&search_req, ldb, ac,
262                                    ac->dn, LDB_SCOPE_BASE,
263                                    "(&(objectClass=kerberosSecret)"
264                                      "(privateKeytab=*))", attrs,
265                                    NULL,
266                                    ac, ukt_search_modified_callback,
267                                    ac->req);
268         if (ret != LDB_SUCCESS) {
269                 return ret;
270         }
271         return ldb_next_request(ac->module, search_req);
272 }
273
274
275 /* add */
276 static int update_kt_add(struct ldb_module *module, struct ldb_request *req)
277 {
278         struct ldb_context *ldb;
279         struct update_kt_ctx *ac;
280         struct ldb_request *down_req;
281         int ret;
282
283         ldb = ldb_module_get_ctx(module);
284
285         ac = update_kt_ctx_init(module, req);
286         if (ac == NULL) {
287                 return ldb_operr(ldb);
288         }
289
290         ac->dn = req->op.add.message->dn;
291
292         ret = ldb_build_add_req(&down_req, ldb, ac,
293                                 req->op.add.message,
294                                 req->controls,
295                                 ac, update_kt_op_callback,
296                                 req);
297         if (ret != LDB_SUCCESS) {
298                 return ret;
299         }
300
301         return ldb_next_request(module, down_req);
302 }
303
304 /* modify */
305 static int update_kt_modify(struct ldb_module *module, struct ldb_request *req)
306 {
307         struct ldb_context *ldb;
308         struct update_kt_ctx *ac;
309         struct ldb_request *down_req;
310         int ret;
311
312         ldb = ldb_module_get_ctx(module);
313
314         ac = update_kt_ctx_init(module, req);
315         if (ac == NULL) {
316                 return ldb_operr(ldb);
317         }
318
319         ac->dn = req->op.mod.message->dn;
320
321         ret = ldb_build_mod_req(&down_req, ldb, ac,
322                                 req->op.mod.message,
323                                 req->controls,
324                                 ac, update_kt_op_callback,
325                                 req);
326         if (ret != LDB_SUCCESS) {
327                 return ret;
328         }
329
330         return ldb_next_request(module, down_req);
331 }
332
333 /* delete */
334 static int update_kt_delete(struct ldb_module *module, struct ldb_request *req)
335 {
336         struct update_kt_ctx *ac;
337
338         ac = update_kt_ctx_init(module, req);
339         if (ac == NULL) {
340                 return ldb_operr(ldb_module_get_ctx(module));
341         }
342
343         ac->dn = req->op.del.dn;
344         ac->do_delete = true;
345
346         return ukt_search_modified(ac);
347 }
348
349 /* rename */
350 static int update_kt_rename(struct ldb_module *module, struct ldb_request *req)
351 {
352         struct ldb_context *ldb;
353         struct update_kt_ctx *ac;
354         struct ldb_request *down_req;
355         int ret;
356
357         ldb = ldb_module_get_ctx(module);
358
359         ac = update_kt_ctx_init(module, req);
360         if (ac == NULL) {
361                 return ldb_operr(ldb);
362         }
363
364         ac->dn = req->op.rename.newdn;
365
366         ret = ldb_build_rename_req(&down_req, ldb, ac,
367                                 req->op.rename.olddn,
368                                 req->op.rename.newdn,
369                                 req->controls,
370                                 ac, update_kt_op_callback,
371                                 req);
372         if (ret != LDB_SUCCESS) {
373                 return ret;
374         }
375
376         return ldb_next_request(module, down_req);
377 }
378
379 /* prepare for a commit */
380 static int update_kt_prepare_commit(struct ldb_module *module)
381 {
382         struct ldb_context *ldb;
383         struct update_kt_private *data = talloc_get_type(ldb_module_get_private(module), struct update_kt_private);
384         struct dn_list *p;
385
386         ldb = ldb_module_get_ctx(module);
387
388         for (p=data->changed_dns; p; p = p->next) {
389                 int kret;
390                 kret = cli_credentials_update_keytab(p->creds, ldb_get_event_context(ldb), ldb_get_opaque(ldb, "loadparm"));
391                 if (kret != 0) {
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;
396                 }
397         }
398
399         talloc_free(data->changed_dns);
400         data->changed_dns = NULL;
401
402         return ldb_next_prepare_commit(module);
403 }
404
405 /* end a transaction */
406 static int update_kt_del_trans(struct ldb_module *module)
407 {
408         struct update_kt_private *data = talloc_get_type(ldb_module_get_private(module), struct update_kt_private);
409
410         talloc_free(data->changed_dns);
411         data->changed_dns = NULL;
412
413         return ldb_next_del_trans(module);
414 }
415
416 static int update_kt_init(struct ldb_module *module)
417 {
418         struct ldb_context *ldb;
419         struct update_kt_private *data;
420
421         ldb = ldb_module_get_ctx(module);
422
423         data = talloc(module, struct update_kt_private);
424         if (data == NULL) {
425                 return ldb_oom(ldb);
426         }
427
428         data->changed_dns = NULL;
429
430         ldb_module_set_private(module, data);
431
432         return ldb_next_init(module);
433 }
434
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,
444 };