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;
185 if (ldb_dn_is_special(req->op.mod.message->dn)) {
186 return ldb_next_request(module, req);
189 if (ldb_request_get_control(req, LDB_CONTROL_RELAX_OID)) {
190 return ldb_next_request(module, req);
193 ldb = ldb_module_get_ctx(module);
194 data = talloc_get_type(ldb_module_get_private(module),
195 struct dns_notify_private);
197 return ldb_operr(ldb);
200 tmp_ctx = talloc_new(module);
201 if (tmp_ctx == NULL) {
205 for (w = data->watched; w; w = w->next) {
206 if (ldb_dn_compare_base(w->dn, req->op.add.message->dn) == 0) {
207 dn = ldb_dn_copy(tmp_ctx, req->op.mod.message->dn);
209 ret = dsdb_module_search_dn(module, tmp_ctx, &res, dn, NULL,
210 DSDB_FLAG_NEXT_MODULE |
211 DSDB_SEARCH_SHOW_RECYCLED |
212 DSDB_SEARCH_REVEAL_INTERNALS |
213 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT, req);
214 if (ret != LDB_SUCCESS) {
215 ldb_asprintf_errstring(ldb_module_get_ctx(module),
216 "%s: Failed to modify %s, because we failed to find it: %s\n",
218 ldb_dn_get_linearized(dn),
219 ldb_errstring(ldb_module_get_ctx(module)));
220 talloc_free(tmp_ctx);
224 schema = dsdb_get_schema(ldb, req);
225 if (schema == NULL) {
226 talloc_free(tmp_ctx);
227 return ldb_operr(ldb);
230 objectclass = dsdb_get_structural_oc_from_msg(schema, res->msgs[0]);
231 if (objectclass == NULL) {
232 talloc_free(tmp_ctx);
233 return ldb_operr(ldb);
236 if (ldb_attr_cmp(objectclass->lDAPDisplayName, "dnsZone") == 0) {
237 data->reload_zones = true;
243 talloc_free(tmp_ctx);
244 return ldb_next_request(module, req);
247 static int dns_notify_delete(struct ldb_module *module, struct ldb_request *req)
250 struct ldb_context *ldb;
251 struct dns_notify_private *data;
252 struct dns_notify_watched_dn *w;
253 struct ldb_dn *old_dn;
254 struct ldb_result *res;
255 struct dsdb_schema *schema;
256 const struct dsdb_class *objectclass;
259 if (ldb_dn_is_special(req->op.del.dn)) {
260 return ldb_next_request(module, req);
263 if (ldb_request_get_control(req, LDB_CONTROL_RELAX_OID)) {
264 return ldb_next_request(module, req);
267 ldb = ldb_module_get_ctx(module);
268 data = talloc_get_type(ldb_module_get_private(module),
269 struct dns_notify_private);
271 return ldb_operr(ldb);
274 tmp_ctx = talloc_new(module);
275 if (tmp_ctx == NULL) {
279 for (w = data->watched; w; w = w->next) {
280 if (ldb_dn_compare_base(w->dn, req->op.add.message->dn) == 0) {
281 old_dn = ldb_dn_copy(tmp_ctx, req->op.del.dn);
282 ret = dsdb_module_search_dn(module, tmp_ctx, &res, old_dn, NULL,
283 DSDB_FLAG_NEXT_MODULE |
284 DSDB_SEARCH_SHOW_RECYCLED |
285 DSDB_SEARCH_REVEAL_INTERNALS |
286 DSDB_SEARCH_SHOW_DN_IN_STORAGE_FORMAT, req);
287 if (ret != LDB_SUCCESS) {
288 ldb_asprintf_errstring(ldb_module_get_ctx(module),
289 "%s: Failed to delete %s, because we failed to find it: %s\n",
291 ldb_dn_get_linearized(old_dn),
292 ldb_errstring(ldb_module_get_ctx(module)));
293 talloc_free(tmp_ctx);
297 schema = dsdb_get_schema(ldb, req);
298 if (schema == NULL) {
299 talloc_free(tmp_ctx);
300 return ldb_operr(ldb);
303 objectclass = dsdb_get_structural_oc_from_msg(schema, res->msgs[0]);
304 if (objectclass == NULL) {
305 talloc_free(tmp_ctx);
306 return ldb_operr(ldb);
309 if (ldb_attr_cmp(objectclass->lDAPDisplayName, "dnsZone") == 0) {
310 data->reload_zones = true;
316 talloc_free(tmp_ctx);
317 return ldb_next_request(module, req);
320 static int dns_notify_start_trans(struct ldb_module *module)
322 struct ldb_context *ldb;
323 struct dns_notify_private *data;
325 ldb = ldb_module_get_ctx(module);
326 data = talloc_get_type(ldb_module_get_private(module),
327 struct dns_notify_private);
329 return ldb_operr(ldb);
332 data->reload_zones = false;
334 return ldb_next_start_trans(module);
337 static int dns_notify_end_trans(struct ldb_module *module)
339 struct ldb_context *ldb;
340 struct dns_notify_private *data;
343 ldb = ldb_module_get_ctx(module);
344 data = talloc_get_type(ldb_module_get_private(module),
345 struct dns_notify_private);
347 return ldb_operr(ldb);
350 ret = ldb_next_end_trans(module);
351 if (ret == LDB_SUCCESS) {
352 if (data->reload_zones) {
353 dns_notify_dnssrv_send(module);
360 static int dns_notify_del_trans(struct ldb_module *module)
362 struct ldb_context *ldb;
363 struct dns_notify_private *data;
365 ldb = ldb_module_get_ctx(module);
366 data = talloc_get_type(ldb_module_get_private(module),
367 struct dns_notify_private);
369 return ldb_operr(ldb);
372 data->reload_zones = false;
374 return ldb_next_del_trans(module);
377 static int dns_notify_init(struct ldb_module *module)
379 struct ldb_context *ldb;
380 struct dns_notify_private *data;
381 struct dns_notify_watched_dn *watched;
382 struct ldb_dn *domain_dn;
383 struct ldb_dn *forest_dn;
385 ldb = ldb_module_get_ctx(module);
387 data = talloc_zero(module, struct dns_notify_private);
392 domain_dn = ldb_get_default_basedn(ldb);
393 forest_dn = ldb_get_root_basedn(ldb);
395 /* Register hook on domain partition */
396 watched = talloc_zero(data, struct dns_notify_watched_dn);
397 if (watched == NULL) {
401 watched->dn = ldb_dn_new_fmt(watched, ldb,
402 "CN=MicrosoftDNS,CN=System,%s",
403 ldb_dn_get_linearized(domain_dn));
404 if (watched->dn == NULL) {
408 DLIST_ADD(data->watched, watched);
410 /* Check for DomainDnsZones partition and register hook */
411 watched = talloc_zero(data, struct dns_notify_watched_dn);
412 if (watched == NULL) {
416 watched->dn = ldb_dn_new_fmt(watched, ldb, "CN=MicrosoftDNS,DC=DomainDnsZones,%s", ldb_dn_get_linearized(forest_dn));
417 DLIST_ADD(data->watched, watched);
419 /* Check for ForestDnsZones partition and register hook */
420 watched = talloc_zero(data, struct dns_notify_watched_dn);
421 if (watched == NULL) {
425 watched->dn = ldb_dn_new_fmt(watched, ldb, "CN=MicrosoftDNS,DC=ForestDnsZones,%s", ldb_dn_get_linearized(forest_dn));
426 DLIST_ADD(data->watched, watched);
428 ldb_module_set_private(module, data);
430 return ldb_next_init(module);
433 static const struct ldb_module_ops ldb_dns_notify_module_ops = {
434 .name = "dns_notify",
435 .init_context = dns_notify_init,
436 .add = dns_notify_add,
437 .modify = dns_notify_modify,
438 .del = dns_notify_delete,
439 .start_transaction = dns_notify_start_trans,
440 .end_transaction = dns_notify_end_trans,
441 .del_transaction = dns_notify_del_trans,
444 int ldb_dns_notify_module_init(const char *version)
446 LDB_MODULE_CHECK_VERSION(version);
447 return ldb_register_module(&ldb_dns_notify_module_ops);