4 Copyright (C) Samuel Cabrero <samuelcabrero@kernevil.me> 2014
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 dns_notify module
25 * Description: Notify the DNS server when zones are changed, either by direct
26 * RPC management calls or DRS inbound replication.
28 * Author: Samuel Cabrero <samuelcabrero@kernevil.me>
32 #include "ldb_module.h"
33 #include "dsdb/samdb/ldb_modules/util.h"
34 #include "dsdb/samdb/samdb.h"
35 #include "dsdb/common/proto.h"
36 #include "librpc/gen_ndr/ndr_irpc.h"
37 #include "lib/messaging/irpc.h"
38 #include "librpc/gen_ndr/ndr_irpc_c.h"
39 #include "param/param.h"
40 #include "dlinklist.h"
42 struct dns_notify_watched_dn {
43 struct dns_notify_watched_dn *next, *prev;
47 struct dns_notify_private {
48 struct dns_notify_watched_dn *watched;
52 struct dns_notify_dnssrv_state {
53 struct imessaging_context *msg_ctx;
54 struct dnssrv_reload_dns_zones r;
57 static void dns_notify_dnssrv_done(struct tevent_req *req)
60 struct dns_notify_dnssrv_state *state;
62 state = tevent_req_callback_data(req, struct dns_notify_dnssrv_state);
64 status = dcerpc_dnssrv_reload_dns_zones_r_recv(req, state);
65 if (!NT_STATUS_IS_OK(status)) {
66 DEBUG(1, ("%s: Error notifiying dns server: %s\n",
67 __func__, nt_errstr(status)));
69 imessaging_cleanup(state->msg_ctx);
75 static void dns_notify_dnssrv_send(struct ldb_module *module)
77 struct ldb_context *ldb;
78 struct loadparm_context *lp_ctx;
79 struct dns_notify_dnssrv_state *state;
80 struct dcerpc_binding_handle *handle;
81 struct tevent_req *req;
83 ldb = ldb_module_get_ctx(module);
85 lp_ctx = ldb_get_opaque(ldb, "loadparm");
90 state = talloc_zero(module, struct dns_notify_dnssrv_state);
95 /* Initialize messaging client */
96 state->msg_ctx = imessaging_client_init(state, lp_ctx,
97 ldb_get_event_context(ldb));
98 if (state->msg_ctx == NULL) {
99 ldb_asprintf_errstring(ldb, "Failed to generate client messaging context in %s",
100 lpcfg_imessaging_path(state, lp_ctx));
105 /* Get a handle to notify the DNS server */
106 handle = irpc_binding_handle_by_name(state, state->msg_ctx,
109 if (handle == NULL) {
110 imessaging_cleanup(state->msg_ctx);
115 /* Send the notifications */
116 req = dcerpc_dnssrv_reload_dns_zones_r_send(state,
117 ldb_get_event_context(ldb),
121 imessaging_cleanup(state->msg_ctx);
125 tevent_req_set_callback(req, dns_notify_dnssrv_done, state);
128 static int dns_notify_add(struct ldb_module *module, struct ldb_request *req)
130 struct ldb_context *ldb;
131 struct dns_notify_private *data;
132 struct dns_notify_watched_dn *w;
133 struct dsdb_schema *schema;
134 const struct dsdb_class *objectclass;
136 if (ldb_dn_is_special(req->op.add.message->dn)) {
137 return ldb_next_request(module, req);
140 if (ldb_request_get_control(req, LDB_CONTROL_RELAX_OID)) {
141 return ldb_next_request(module, req);
144 ldb = ldb_module_get_ctx(module);
145 data = talloc_get_type(ldb_module_get_private(module),
146 struct dns_notify_private);
148 return ldb_operr(ldb);
151 for (w = data->watched; w; w = w->next) {
152 if (ldb_dn_compare_base(w->dn, req->op.add.message->dn) == 0) {
153 schema = dsdb_get_schema(ldb, req);
154 if (schema == NULL) {
155 return ldb_operr(ldb);
158 objectclass = dsdb_get_structural_oc_from_msg(schema, req->op.add.message);
159 if (objectclass == NULL) {
160 return ldb_operr(ldb);
163 if (ldb_attr_cmp(objectclass->lDAPDisplayName, "dnsZone") == 0) {
164 data->reload_zones = true;
170 return ldb_next_request(module, req);
173 static int dns_notify_modify(struct ldb_module *module, struct ldb_request *req)
176 struct ldb_context *ldb;
177 struct dns_notify_private *data;
178 struct dns_notify_watched_dn *w;
180 struct ldb_result *res;
181 struct dsdb_schema *schema;
182 const struct dsdb_class *objectclass;
183 const char * const attrs[] = { "objectClass", NULL };
186 if (ldb_dn_is_special(req->op.mod.message->dn)) {
187 return ldb_next_request(module, req);
190 if (ldb_request_get_control(req, LDB_CONTROL_RELAX_OID)) {
191 return ldb_next_request(module, req);
194 ldb = ldb_module_get_ctx(module);
195 data = talloc_get_type(ldb_module_get_private(module),
196 struct dns_notify_private);
198 return ldb_operr(ldb);
201 tmp_ctx = talloc_new(module);
202 if (tmp_ctx == NULL) {
206 for (w = data->watched; w; w = w->next) {
207 if (ldb_dn_compare_base(w->dn, req->op.add.message->dn) == 0) {
208 dn = ldb_dn_copy(tmp_ctx, req->op.mod.message->dn);
210 ret = dsdb_module_search_dn(module, tmp_ctx, &res, dn, attrs,
211 DSDB_FLAG_NEXT_MODULE |
212 DSDB_SEARCH_SHOW_RECYCLED |
213 DSDB_SEARCH_REVEAL_INTERNALS |
214 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT, req);
215 if (ret != LDB_SUCCESS) {
216 ldb_asprintf_errstring(ldb_module_get_ctx(module),
217 "%s: Failed to modify %s, because we failed to find it: %s\n",
219 ldb_dn_get_linearized(dn),
220 ldb_errstring(ldb_module_get_ctx(module)));
221 talloc_free(tmp_ctx);
225 schema = dsdb_get_schema(ldb, req);
226 if (schema == NULL) {
227 talloc_free(tmp_ctx);
228 return ldb_operr(ldb);
231 objectclass = dsdb_get_structural_oc_from_msg(schema, res->msgs[0]);
232 if (objectclass == NULL) {
233 talloc_free(tmp_ctx);
234 return ldb_operr(ldb);
237 if (ldb_attr_cmp(objectclass->lDAPDisplayName, "dnsZone") == 0) {
238 data->reload_zones = true;
244 talloc_free(tmp_ctx);
245 return ldb_next_request(module, req);
248 static int dns_notify_delete(struct ldb_module *module, struct ldb_request *req)
251 struct ldb_context *ldb;
252 struct dns_notify_private *data;
253 struct dns_notify_watched_dn *w;
254 struct ldb_dn *old_dn;
255 struct ldb_result *res;
256 struct dsdb_schema *schema;
257 const struct dsdb_class *objectclass;
258 const char * const attrs[] = { "objectClass", NULL };
261 if (ldb_dn_is_special(req->op.del.dn)) {
262 return ldb_next_request(module, req);
265 if (ldb_request_get_control(req, LDB_CONTROL_RELAX_OID)) {
266 return ldb_next_request(module, req);
269 ldb = ldb_module_get_ctx(module);
270 data = talloc_get_type(ldb_module_get_private(module),
271 struct dns_notify_private);
273 return ldb_operr(ldb);
276 tmp_ctx = talloc_new(module);
277 if (tmp_ctx == NULL) {
281 for (w = data->watched; w; w = w->next) {
282 if (ldb_dn_compare_base(w->dn, req->op.add.message->dn) == 0) {
283 old_dn = ldb_dn_copy(tmp_ctx, req->op.del.dn);
284 ret = dsdb_module_search_dn(module, tmp_ctx, &res, old_dn, attrs,
285 DSDB_FLAG_NEXT_MODULE |
286 DSDB_SEARCH_SHOW_RECYCLED |
287 DSDB_SEARCH_REVEAL_INTERNALS |
288 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT, req);
289 if (ret != LDB_SUCCESS) {
290 ldb_asprintf_errstring(ldb_module_get_ctx(module),
291 "%s: Failed to delete %s, because we failed to find it: %s\n",
293 ldb_dn_get_linearized(old_dn),
294 ldb_errstring(ldb_module_get_ctx(module)));
295 talloc_free(tmp_ctx);
299 schema = dsdb_get_schema(ldb, req);
300 if (schema == NULL) {
301 talloc_free(tmp_ctx);
302 return ldb_operr(ldb);
305 objectclass = dsdb_get_structural_oc_from_msg(schema, res->msgs[0]);
306 if (objectclass == NULL) {
307 talloc_free(tmp_ctx);
308 return ldb_operr(ldb);
311 if (ldb_attr_cmp(objectclass->lDAPDisplayName, "dnsZone") == 0) {
312 data->reload_zones = true;
318 talloc_free(tmp_ctx);
319 return ldb_next_request(module, req);
322 static int dns_notify_start_trans(struct ldb_module *module)
324 struct ldb_context *ldb;
325 struct dns_notify_private *data;
327 ldb = ldb_module_get_ctx(module);
328 data = talloc_get_type(ldb_module_get_private(module),
329 struct dns_notify_private);
331 return ldb_operr(ldb);
334 data->reload_zones = false;
336 return ldb_next_start_trans(module);
339 static int dns_notify_end_trans(struct ldb_module *module)
341 struct ldb_context *ldb;
342 struct dns_notify_private *data;
345 ldb = ldb_module_get_ctx(module);
346 data = talloc_get_type(ldb_module_get_private(module),
347 struct dns_notify_private);
349 return ldb_operr(ldb);
352 ret = ldb_next_end_trans(module);
353 if (ret == LDB_SUCCESS) {
354 if (data->reload_zones) {
355 dns_notify_dnssrv_send(module);
362 static int dns_notify_del_trans(struct ldb_module *module)
364 struct ldb_context *ldb;
365 struct dns_notify_private *data;
367 ldb = ldb_module_get_ctx(module);
368 data = talloc_get_type(ldb_module_get_private(module),
369 struct dns_notify_private);
371 return ldb_operr(ldb);
374 data->reload_zones = false;
376 return ldb_next_del_trans(module);
379 static int dns_notify_init(struct ldb_module *module)
381 struct ldb_context *ldb;
382 struct dns_notify_private *data;
383 struct dns_notify_watched_dn *watched;
384 struct ldb_dn *domain_dn;
385 struct ldb_dn *forest_dn;
387 ldb = ldb_module_get_ctx(module);
389 data = talloc_zero(module, struct dns_notify_private);
394 domain_dn = ldb_get_default_basedn(ldb);
395 forest_dn = ldb_get_root_basedn(ldb);
397 /* Register hook on domain partition */
398 watched = talloc_zero(data, struct dns_notify_watched_dn);
399 if (watched == NULL) {
403 watched->dn = ldb_dn_new_fmt(watched, ldb,
404 "CN=MicrosoftDNS,CN=System,%s",
405 ldb_dn_get_linearized(domain_dn));
406 if (watched->dn == NULL) {
410 DLIST_ADD(data->watched, watched);
412 /* Check for DomainDnsZones partition and register hook */
413 watched = talloc_zero(data, struct dns_notify_watched_dn);
414 if (watched == NULL) {
418 watched->dn = ldb_dn_new_fmt(watched, ldb, "CN=MicrosoftDNS,DC=DomainDnsZones,%s", ldb_dn_get_linearized(forest_dn));
419 DLIST_ADD(data->watched, watched);
421 /* Check for ForestDnsZones partition and register hook */
422 watched = talloc_zero(data, struct dns_notify_watched_dn);
423 if (watched == NULL) {
427 watched->dn = ldb_dn_new_fmt(watched, ldb, "CN=MicrosoftDNS,DC=ForestDnsZones,%s", ldb_dn_get_linearized(forest_dn));
428 DLIST_ADD(data->watched, watched);
430 ldb_module_set_private(module, data);
432 return ldb_next_init(module);
435 static const struct ldb_module_ops ldb_dns_notify_module_ops = {
436 .name = "dns_notify",
437 .init_context = dns_notify_init,
438 .add = dns_notify_add,
439 .modify = dns_notify_modify,
440 .del = dns_notify_delete,
441 .start_transaction = dns_notify_start_trans,
442 .end_transaction = dns_notify_end_trans,
443 .del_transaction = dns_notify_del_trans,
446 int ldb_dns_notify_module_init(const char *version)
448 LDB_MODULE_CHECK_VERSION(version);
449 return ldb_register_module(&ldb_dns_notify_module_ops);