]> git.samba.org - mat/samba.git/blob - source4/dsdb/samdb/ldb_modules/update_keytab.c
762bb0a99289c1a246ffa0d8ca00d1227d78a9de
[mat/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 #include "auth/kerberos/kerberos.h"
37 #include "auth/kerberos/kerberos_srv_keytab.h"
38 #include "dsdb/samdb/ldb_modules/util.h"
39
40 struct dn_list {
41         struct ldb_message *msg;
42         bool do_delete;
43         struct dn_list *prev, *next;
44 };
45
46 struct update_kt_private {
47         struct dn_list *changed_dns;
48 };
49
50 struct update_kt_ctx {
51         struct ldb_module *module;
52         struct ldb_request *req;
53
54         struct ldb_dn *dn;
55         bool do_delete;
56
57         struct ldb_reply *op_reply;
58         bool found;
59 };
60
61 static struct update_kt_ctx *update_kt_ctx_init(struct ldb_module *module,
62                                                 struct ldb_request *req)
63 {
64         struct update_kt_ctx *ac;
65
66         ac = talloc_zero(req, struct update_kt_ctx);
67         if (ac == NULL) {
68                 ldb_oom(ldb_module_get_ctx(module));
69                 return NULL;
70         }
71
72         ac->module = module;
73         ac->req = req;
74
75         return ac;
76 }
77
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
82  */
83 static int add_modified(struct ldb_module *module, struct ldb_dn *dn, bool do_delete,
84                         struct ldb_request *parent)
85 {
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);
88         struct dn_list *item;
89         char *filter;
90         struct ldb_result *res;
91         int ret;
92
93         filter = talloc_asprintf(data,
94                                  "(&(objectClass=kerberosSecret)(privateKeytab=*))");
95         if (!filter) {
96                 return ldb_oom(ldb);
97         }
98
99         ret = dsdb_module_search(module, data, &res,
100                                  dn, LDB_SCOPE_BASE, NULL,
101                                  DSDB_FLAG_NEXT_MODULE, parent,
102                                  "%s", filter);
103         talloc_free(filter);
104         if (ret != LDB_SUCCESS) {
105                 return ret;
106         }
107
108         if (res->count != 1) {
109                 /* if it's not a kerberosSecret then we don't have anything to update */
110                 talloc_free(res);
111                 talloc_free(filter);
112                 return LDB_SUCCESS;
113         }
114
115         item = talloc(data->changed_dns? (void *)data->changed_dns: (void *)data, struct dn_list);
116         if (!item) {
117                 talloc_free(res);
118                 talloc_free(filter);
119                 return ldb_oom(ldb);
120         }
121
122         item->msg = talloc_steal(item, res->msgs[0]);
123         item->do_delete = do_delete;
124         talloc_free(res);
125
126         DLIST_ADD_END(data->changed_dns, item, struct dn_list *);
127         return LDB_SUCCESS;
128 }
129
130 static int ukt_search_modified(struct update_kt_ctx *ac);
131
132 static int update_kt_op_callback(struct ldb_request *req,
133                                  struct ldb_reply *ares)
134 {
135         struct ldb_context *ldb;
136         struct update_kt_ctx *ac;
137         int ret;
138
139         ac = talloc_get_type(req->context, struct update_kt_ctx);
140         ldb = ldb_module_get_ctx(ac->module);
141
142         if (!ares) {
143                 return ldb_module_done(ac->req, NULL, NULL,
144                                         LDB_ERR_OPERATIONS_ERROR);
145         }
146         if (ares->error != LDB_SUCCESS) {
147                 return ldb_module_done(ac->req, ares->controls,
148                                         ares->response, ares->error);
149         }
150
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);
155         }
156
157         if (ac->do_delete) {
158                 return ldb_module_done(ac->req, ares->controls,
159                                         ares->response, LDB_SUCCESS);
160         }
161
162         ac->op_reply = talloc_steal(ac, ares);
163
164         ret = ukt_search_modified(ac);
165         if (ret != LDB_SUCCESS) {
166                 return ldb_module_done(ac->req, NULL, NULL, ret);
167         }
168
169         return LDB_SUCCESS;
170 }
171
172 static int ukt_del_op(struct update_kt_ctx *ac)
173 {
174         struct ldb_context *ldb;
175         struct ldb_request *down_req;
176         int ret;
177
178         ldb = ldb_module_get_ctx(ac->module);
179
180         ret = ldb_build_del_req(&down_req, ldb, ac,
181                                 ac->dn,
182                                 ac->req->controls,
183                                 ac, update_kt_op_callback,
184                                 ac->req);
185         LDB_REQ_SET_LOCATION(down_req);
186         if (ret != LDB_SUCCESS) {
187                 return ret;
188         }
189         return ldb_next_request(ac->module, down_req);
190 }
191
192 static int ukt_search_modified_callback(struct ldb_request *req,
193                                         struct ldb_reply *ares)
194 {
195         struct update_kt_ctx *ac;
196         int ret;
197
198         ac = talloc_get_type(req->context, struct update_kt_ctx);
199
200         if (!ares) {
201                 return ldb_module_done(ac->req, NULL, NULL,
202                                         LDB_ERR_OPERATIONS_ERROR);
203         }
204         if (ares->error != LDB_SUCCESS) {
205                 return ldb_module_done(ac->req, ares->controls,
206                                         ares->response, ares->error);
207         }
208
209         switch (ares->type) {
210         case LDB_REPLY_ENTRY:
211
212                 ac->found = true;
213                 break;
214
215         case LDB_REPLY_REFERRAL:
216                 /* ignore */
217                 break;
218
219         case LDB_REPLY_DONE:
220
221                 if (ac->found) {
222                         /* do the dirty sync job here :/ */
223                         ret = add_modified(ac->module, ac->dn, ac->do_delete, ac->req);
224                 }
225
226                 if (ac->do_delete) {
227                         ret = ukt_del_op(ac);
228                         if (ret != LDB_SUCCESS) {
229                                 return ldb_module_done(ac->req,
230                                                         NULL, NULL, ret);
231                         }
232                         break;
233                 }
234
235                 return ldb_module_done(ac->req, ac->op_reply->controls,
236                                         ac->op_reply->response, LDB_SUCCESS);
237         }
238
239         talloc_free(ares);
240         return LDB_SUCCESS;
241 }
242
243 static int ukt_search_modified(struct update_kt_ctx *ac)
244 {
245         struct ldb_context *ldb;
246         static const char * const no_attrs[] = { NULL };
247         struct ldb_request *search_req;
248         int ret;
249
250         ldb = ldb_module_get_ctx(ac->module);
251
252         ret = ldb_build_search_req(&search_req, ldb, ac,
253                                    ac->dn, LDB_SCOPE_BASE,
254                                    "(&(objectClass=kerberosSecret)"
255                                      "(privateKeytab=*))", no_attrs,
256                                    NULL,
257                                    ac, ukt_search_modified_callback,
258                                    ac->req);
259         LDB_REQ_SET_LOCATION(search_req);
260         if (ret != LDB_SUCCESS) {
261                 return ret;
262         }
263         return ldb_next_request(ac->module, search_req);
264 }
265
266
267 /* add */
268 static int update_kt_add(struct ldb_module *module, struct ldb_request *req)
269 {
270         struct ldb_context *ldb;
271         struct update_kt_ctx *ac;
272         struct ldb_request *down_req;
273         int ret;
274
275         ldb = ldb_module_get_ctx(module);
276
277         ac = update_kt_ctx_init(module, req);
278         if (ac == NULL) {
279                 return ldb_operr(ldb);
280         }
281
282         ac->dn = req->op.add.message->dn;
283
284         ret = ldb_build_add_req(&down_req, ldb, ac,
285                                 req->op.add.message,
286                                 req->controls,
287                                 ac, update_kt_op_callback,
288                                 req);
289         LDB_REQ_SET_LOCATION(down_req);
290         if (ret != LDB_SUCCESS) {
291                 return ret;
292         }
293
294         return ldb_next_request(module, down_req);
295 }
296
297 /* modify */
298 static int update_kt_modify(struct ldb_module *module, struct ldb_request *req)
299 {
300         struct ldb_context *ldb;
301         struct update_kt_ctx *ac;
302         struct ldb_request *down_req;
303         int ret;
304
305         ldb = ldb_module_get_ctx(module);
306
307         ac = update_kt_ctx_init(module, req);
308         if (ac == NULL) {
309                 return ldb_operr(ldb);
310         }
311
312         ac->dn = req->op.mod.message->dn;
313
314         ret = ldb_build_mod_req(&down_req, ldb, ac,
315                                 req->op.mod.message,
316                                 req->controls,
317                                 ac, update_kt_op_callback,
318                                 req);
319         LDB_REQ_SET_LOCATION(down_req);
320         if (ret != LDB_SUCCESS) {
321                 return ret;
322         }
323
324         return ldb_next_request(module, down_req);
325 }
326
327 /* delete */
328 static int update_kt_delete(struct ldb_module *module, struct ldb_request *req)
329 {
330         struct update_kt_ctx *ac;
331
332         ac = update_kt_ctx_init(module, req);
333         if (ac == NULL) {
334                 return ldb_operr(ldb_module_get_ctx(module));
335         }
336
337         ac->dn = req->op.del.dn;
338         ac->do_delete = true;
339
340         return ukt_search_modified(ac);
341 }
342
343 /* rename */
344 static int update_kt_rename(struct ldb_module *module, struct ldb_request *req)
345 {
346         struct ldb_context *ldb;
347         struct update_kt_ctx *ac;
348         struct ldb_request *down_req;
349         int ret;
350
351         ldb = ldb_module_get_ctx(module);
352
353         ac = update_kt_ctx_init(module, req);
354         if (ac == NULL) {
355                 return ldb_operr(ldb);
356         }
357
358         ac->dn = req->op.rename.newdn;
359
360         ret = ldb_build_rename_req(&down_req, ldb, ac,
361                                 req->op.rename.olddn,
362                                 req->op.rename.newdn,
363                                 req->controls,
364                                 ac, update_kt_op_callback,
365                                 req);
366         LDB_REQ_SET_LOCATION(down_req);
367         if (ret != LDB_SUCCESS) {
368                 return ret;
369         }
370
371         return ldb_next_request(module, down_req);
372 }
373
374 /* prepare for a commit */
375 static int update_kt_prepare_commit(struct ldb_module *module)
376 {
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);
379         struct dn_list *p;
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"),
382                                              &smb_krb5_context);
383         if (krb5_ret != 0) {
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;
388         }
389
390         ldb = ldb_module_get_ctx(module);
391
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);
395                 if (krb5_ret != 0) {
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"),
401                                                error_string);
402                         return LDB_ERR_OPERATIONS_ERROR;
403                 }
404         }
405
406         talloc_free(data->changed_dns);
407         data->changed_dns = NULL;
408
409         return ldb_next_prepare_commit(module);
410 }
411
412 /* end a transaction */
413 static int update_kt_del_trans(struct ldb_module *module)
414 {
415         struct update_kt_private *data = talloc_get_type(ldb_module_get_private(module), struct update_kt_private);
416
417         talloc_free(data->changed_dns);
418         data->changed_dns = NULL;
419
420         return ldb_next_del_trans(module);
421 }
422
423 static int update_kt_init(struct ldb_module *module)
424 {
425         struct ldb_context *ldb;
426         struct update_kt_private *data;
427
428         ldb = ldb_module_get_ctx(module);
429
430         data = talloc(module, struct update_kt_private);
431         if (data == NULL) {
432                 return ldb_oom(ldb);
433         }
434
435         data->changed_dns = NULL;
436
437         ldb_module_set_private(module, data);
438
439         return ldb_next_init(module);
440 }
441
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,
451 };
452
453 int ldb_update_keytab_module_init(const char *version)
454 {
455         LDB_MODULE_CHECK_VERSION(version);
456         return ldb_register_module(&ldb_update_keytab_module_ops);
457 }