2 Unix SMB/CIFS mplementation.
6 Copyright (C) Andrew Tridgell 2009
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 this module auto-creates the named.conf.update file, which tells
25 bind9 what KRB5 principals it should accept for updates to our zone
27 It also uses the samba_dnsupdate script to auto-create the right DNS
28 names for ourselves as a DC in the domain, using TSIG-GSS
32 #include "dsdb/samdb/samdb.h"
33 #include "auth/auth.h"
34 #include "smbd/service.h"
35 #include "lib/messaging/irpc.h"
36 #include "param/param.h"
37 #include "system/filesys.h"
38 #include "dsdb/common/util.h"
39 #include "libcli/composite/composite.h"
40 #include "libcli/security/dom_sid.h"
41 #include "librpc/gen_ndr/ndr_irpc.h"
42 #include "libds/common/roles.h"
44 NTSTATUS server_service_dnsupdate_init(void);
46 struct dnsupdate_service {
47 struct task_server *task;
48 struct auth_session_info *system_session_info;
49 struct ldb_context *samdb;
51 /* status for periodic config file update */
54 struct tevent_timer *te;
55 struct tevent_req *subreq;
59 /* status for periodic DNS name check */
62 struct tevent_timer *te;
63 struct tevent_req *subreq;
64 struct tevent_req *spnreq;
70 called when rndc reload has finished
72 static void dnsupdate_rndc_done(struct tevent_req *subreq)
74 struct dnsupdate_service *service = tevent_req_callback_data(subreq,
75 struct dnsupdate_service);
79 service->confupdate.subreq = NULL;
81 ret = samba_runcmd_recv(subreq, &sys_errno);
84 service->confupdate.status = map_nt_error_from_unix_common(sys_errno);
86 service->confupdate.status = NT_STATUS_OK;
89 if (!NT_STATUS_IS_OK(service->confupdate.status)) {
90 DEBUG(0,(__location__ ": Failed rndc update - %s\n",
91 nt_errstr(service->confupdate.status)));
93 DEBUG(3,("Completed rndc reload OK\n"));
98 called every 'dnsupdate:conf interval' seconds
100 static void dnsupdate_rebuild(struct dnsupdate_service *service)
104 struct ldb_result *res1, *res2;
105 const char *tmp_path, *path, *path_static;
106 char *static_policies;
109 const char *attrs1[] = { "msDS-HasDomainNCs", NULL };
110 const char *attrs2[] = { "name", NULL };
111 const char *realm = lpcfg_realm(service->task->lp_ctx);
112 TALLOC_CTX *tmp_ctx = talloc_new(service);
113 const char * const *rndc_command = lpcfg_rndc_command(service->task->lp_ctx);
114 const char **dc_list;
117 /* abort any pending script run */
118 TALLOC_FREE(service->confupdate.subreq);
120 /* find the DNs for all the non-RODC DCs in the forest */
121 ret = dsdb_search(service->samdb, tmp_ctx, &res1, ldb_get_config_basedn(service->samdb),
125 "(&(objectclass=NTDSDSA)(!(msDS-isRODC=TRUE)))");
126 if (ret != LDB_SUCCESS) {
127 DEBUG(0,(__location__ ": Unable to find DCs list - %s", ldb_errstring(service->samdb)));
128 talloc_free(tmp_ctx);
132 dc_list = talloc_array(tmp_ctx, const char *, 0);
133 for (i=0; i<res1->count; i++) {
134 struct ldb_dn *server_dn = res1->msgs[i]->dn;
135 struct ldb_dn *domain_dn;
136 const char *acct_name, *full_account, *dns_domain;
138 /* this is a nasty hack to form the account name of
139 * this DC. We do it this way as we don't necessarily
140 * have access to the domain NC, so all we have to go
141 * on is what is in the configuration partition
144 domain_dn = ldb_msg_find_attr_as_dn(service->samdb, tmp_ctx, res1->msgs[i], "msDS-HasDomainNCs");
145 if (domain_dn == NULL) continue;
147 ldb_dn_remove_child_components(server_dn, 1);
148 ret = dsdb_search_dn(service->samdb, tmp_ctx, &res2, server_dn, attrs2, 0);
149 if (ret != LDB_SUCCESS) {
153 acct_name = ldb_msg_find_attr_as_string(res2->msgs[0], "name", NULL);
154 if (acct_name == NULL) continue;
156 dns_domain = samdb_dn_to_dns_domain(tmp_ctx, domain_dn);
157 if (dns_domain == NULL) {
161 full_account = talloc_asprintf(tmp_ctx, "%s$@%s", acct_name, dns_domain);
162 if (full_account == NULL) continue;
164 dc_list = talloc_realloc(tmp_ctx, dc_list, const char *, dc_count+1);
165 if (dc_list == NULL) {
168 dc_list[dc_count++] = full_account;
171 path = lpcfg_parm_string(service->task->lp_ctx, NULL, "dnsupdate", "path");
173 path = lpcfg_private_path(tmp_ctx, service->task->lp_ctx, "named.conf.update");
176 path_static = lpcfg_parm_string(service->task->lp_ctx, NULL, "dnsupdate", "extra_static_grant_rules");
177 if (path_static == NULL) {
178 path_static = lpcfg_private_path(tmp_ctx, service->task->lp_ctx, "named.conf.update.static");
181 tmp_path = talloc_asprintf(tmp_ctx, "%s.tmp", path);
182 if (path == NULL || tmp_path == NULL || path_static == NULL ) {
183 DEBUG(0,(__location__ ": Unable to get paths\n"));
184 talloc_free(tmp_ctx);
188 static_policies = file_load(path_static, &size, 0, tmp_ctx);
191 fd = open(tmp_path, O_CREAT|O_TRUNC|O_WRONLY, 0444);
193 DEBUG(1,(__location__ ": Unable to open %s - %s\n", tmp_path, strerror(errno)));
194 talloc_free(tmp_ctx);
198 dprintf(fd, "/* this file is auto-generated - do not edit */\n");
199 dprintf(fd, "update-policy {\n");
200 if( static_policies != NULL ) {
201 dprintf(fd, "/* Start of static entries */\n");
202 dprintf(fd, "%s\n",static_policies);
203 dprintf(fd, "/* End of static entries */\n");
205 dprintf(fd, "\tgrant %s ms-self * A AAAA;\n", realm);
206 dprintf(fd, "\tgrant Administrator@%s wildcard * A AAAA SRV CNAME;\n", realm);
208 for (i=0; i<dc_count; i++) {
209 dprintf(fd, "\tgrant %s wildcard * A AAAA SRV CNAME;\n", dc_list[i]);
215 if (NT_STATUS_IS_OK(service->confupdate.status) &&
216 file_compare(tmp_path, path) == true) {
218 talloc_free(tmp_ctx);
222 if (rename(tmp_path, path) != 0) {
223 DEBUG(0,(__location__ ": Failed to rename %s to %s - %s\n",
224 tmp_path, path, strerror(errno)));
225 talloc_free(tmp_ctx);
229 DEBUG(2,("Loading new DNS update grant rules\n"));
230 service->confupdate.subreq = samba_runcmd_send(service,
231 service->task->event_ctx,
232 timeval_current_ofs(10, 0),
236 if (service->confupdate.subreq == NULL) {
237 DEBUG(0,(__location__ ": samba_runcmd_send() failed with no memory\n"));
238 talloc_free(tmp_ctx);
241 tevent_req_set_callback(service->confupdate.subreq,
245 talloc_free(tmp_ctx);
248 static NTSTATUS dnsupdate_confupdate_schedule(struct dnsupdate_service *service);
251 called every 'dnsupdate:conf interval' seconds
253 static void dnsupdate_confupdate_handler_te(struct tevent_context *ev, struct tevent_timer *te,
254 struct timeval t, void *ptr)
256 struct dnsupdate_service *service = talloc_get_type(ptr, struct dnsupdate_service);
258 dnsupdate_rebuild(service);
259 dnsupdate_confupdate_schedule(service);
263 static NTSTATUS dnsupdate_confupdate_schedule(struct dnsupdate_service *service)
265 service->confupdate.te = tevent_add_timer(service->task->event_ctx, service,
266 timeval_current_ofs(service->confupdate.interval, 0),
267 dnsupdate_confupdate_handler_te, service);
268 NT_STATUS_HAVE_NO_MEMORY(service->confupdate.te);
274 called when dns update script has finished
276 static void dnsupdate_nameupdate_done(struct tevent_req *subreq)
278 struct dnsupdate_service *service = tevent_req_callback_data(subreq,
279 struct dnsupdate_service);
283 service->nameupdate.subreq = NULL;
285 ret = samba_runcmd_recv(subreq, &sys_errno);
288 service->nameupdate.status = map_nt_error_from_unix_common(sys_errno);
290 service->nameupdate.status = NT_STATUS_OK;
293 if (!NT_STATUS_IS_OK(service->nameupdate.status)) {
294 DEBUG(0,(__location__ ": Failed DNS update - %s\n",
295 nt_errstr(service->nameupdate.status)));
297 DEBUG(3,("Completed DNS update check OK\n"));
303 called when spn update script has finished
305 static void dnsupdate_spnupdate_done(struct tevent_req *subreq)
307 struct dnsupdate_service *service = tevent_req_callback_data(subreq,
308 struct dnsupdate_service);
312 service->nameupdate.spnreq = NULL;
314 ret = samba_runcmd_recv(subreq, &sys_errno);
317 service->nameupdate.status = map_nt_error_from_unix_common(sys_errno);
319 service->nameupdate.status = NT_STATUS_OK;
322 if (!NT_STATUS_IS_OK(service->nameupdate.status)) {
323 DEBUG(0,(__location__ ": Failed SPN update - %s\n",
324 nt_errstr(service->nameupdate.status)));
326 DEBUG(3,("Completed SPN update check OK\n"));
331 called every 'dnsupdate:name interval' seconds
333 static void dnsupdate_check_names(struct dnsupdate_service *service)
335 const char * const *dns_update_command = lpcfg_dns_update_command(service->task->lp_ctx);
336 const char * const *spn_update_command = lpcfg_spn_update_command(service->task->lp_ctx);
338 /* kill any existing child */
339 TALLOC_FREE(service->nameupdate.subreq);
341 DEBUG(3,("Calling DNS name update script\n"));
342 service->nameupdate.subreq = samba_runcmd_send(service,
343 service->task->event_ctx,
344 timeval_current_ofs(20, 0),
348 if (service->nameupdate.subreq == NULL) {
349 DEBUG(0,(__location__ ": samba_runcmd_send() failed with no memory\n"));
352 tevent_req_set_callback(service->nameupdate.subreq,
353 dnsupdate_nameupdate_done,
356 DEBUG(3,("Calling SPN name update script\n"));
357 service->nameupdate.spnreq = samba_runcmd_send(service,
358 service->task->event_ctx,
359 timeval_current_ofs(20, 0),
363 if (service->nameupdate.spnreq == NULL) {
364 DEBUG(0,(__location__ ": samba_runcmd_send() failed with no memory\n"));
367 tevent_req_set_callback(service->nameupdate.spnreq,
368 dnsupdate_spnupdate_done,
372 static NTSTATUS dnsupdate_nameupdate_schedule(struct dnsupdate_service *service);
375 called every 'dnsupdate:name interval' seconds
377 static void dnsupdate_nameupdate_handler_te(struct tevent_context *ev, struct tevent_timer *te,
378 struct timeval t, void *ptr)
380 struct dnsupdate_service *service = talloc_get_type(ptr, struct dnsupdate_service);
382 dnsupdate_check_names(service);
383 dnsupdate_nameupdate_schedule(service);
387 static NTSTATUS dnsupdate_nameupdate_schedule(struct dnsupdate_service *service)
389 service->nameupdate.te = tevent_add_timer(service->task->event_ctx, service,
390 timeval_current_ofs(service->nameupdate.interval, 0),
391 dnsupdate_nameupdate_handler_te, service);
392 NT_STATUS_HAVE_NO_MEMORY(service->nameupdate.te);
397 struct dnsupdate_RODC_state {
398 struct irpc_message *msg;
399 struct dnsupdate_RODC *r;
405 static int dnsupdate_RODC_destructor(struct dnsupdate_RODC_state *st)
410 unlink(st->tmp_path);
411 if (st->tmp_path2 != NULL) {
412 unlink(st->tmp_path2);
418 called when the DNS update has completed
420 static void dnsupdate_RODC_callback(struct tevent_req *req)
422 struct dnsupdate_RODC_state *st =
423 tevent_req_callback_data(req,
424 struct dnsupdate_RODC_state);
428 ret = samba_runcmd_recv(req, &sys_errno);
431 st->r->out.result = map_nt_error_from_unix_common(sys_errno);
432 DEBUG(2,(__location__ ": RODC DNS Update failed: %s\n", nt_errstr(st->r->out.result)));
434 st->r->out.result = NT_STATUS_OK;
435 DEBUG(3,(__location__ ": RODC DNS Update OK\n"));
438 for (i=0; i<st->r->in.dns_names->count; i++) {
439 st->r->out.dns_names->names[i].status = NT_STATUS_V(st->r->out.result);
442 irpc_send_reply(st->msg, NT_STATUS_OK);
447 * Called when we get a RODC DNS update request from the netlogon
450 static NTSTATUS dnsupdate_dnsupdate_RODC(struct irpc_message *msg,
451 struct dnsupdate_RODC *r)
453 struct dnsupdate_service *s = talloc_get_type(msg->private_data,
454 struct dnsupdate_service);
455 const char * const *dns_update_command = lpcfg_dns_update_command(s->task->lp_ctx);
456 struct dnsupdate_RODC_state *st;
457 struct tevent_req *req;
459 struct GUID ntds_guid;
460 const char *site, *dnsdomain, *dnsforest, *ntdsguid;
461 const char *hostname = NULL;
462 struct ldb_dn *sid_dn;
463 const char *attrs[] = { "dNSHostName", NULL };
464 struct ldb_result *res;
466 st = talloc_zero(msg, struct dnsupdate_RODC_state);
468 r->out.result = NT_STATUS_NO_MEMORY;
475 st->tmp_path = smbd_tmp_path(st, s->task->lp_ctx, "rodcdns.XXXXXX");
478 r->out.result = NT_STATUS_NO_MEMORY;
482 st->fd = mkstemp(st->tmp_path);
484 DEBUG(0,("Unable to create a temporary file for RODC dnsupdate\n"));
486 r->out.result = NT_STATUS_INTERNAL_DB_CORRUPTION;
490 talloc_set_destructor(st, dnsupdate_RODC_destructor);
492 st->tmp_path2 = talloc_asprintf(st, "%s.cache", st->tmp_path);
493 if (!st->tmp_path2) {
495 r->out.result = NT_STATUS_NO_MEMORY;
499 sid_dn = ldb_dn_new_fmt(st, s->samdb, "<SID=%s>", dom_sid_string(st, r->in.dom_sid));
502 r->out.result = NT_STATUS_NO_MEMORY;
506 /* work out the site */
507 ret = samdb_find_site_for_computer(s->samdb, st, sid_dn, &site);
508 if (ret != LDB_SUCCESS) {
509 DEBUG(2, (__location__ ": Unable to find site for computer %s\n",
510 ldb_dn_get_linearized(sid_dn)));
512 r->out.result = NT_STATUS_NO_SUCH_USER;
516 /* work out the ntdsguid */
517 ret = samdb_find_ntdsguid_for_computer(s->samdb, sid_dn, &ntds_guid);
518 ntdsguid = GUID_string(st, &ntds_guid);
519 if (ret != LDB_SUCCESS || !ntdsguid) {
520 DEBUG(2, (__location__ ": Unable to find NTDS GUID for computer %s\n",
521 ldb_dn_get_linearized(sid_dn)));
523 r->out.result = NT_STATUS_NO_SUCH_USER;
528 /* find dnsdomain and dnsforest */
529 dnsdomain = lpcfg_realm(s->task->lp_ctx);
530 dnsforest = dnsdomain;
532 /* find the hostname */
533 ret = dsdb_search_dn(s->samdb, st, &res, sid_dn, attrs, 0);
534 if (ret == LDB_SUCCESS) {
535 hostname = ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
537 if (ret != LDB_SUCCESS || !hostname) {
538 DEBUG(2, (__location__ ": Unable to find NTDS GUID for computer %s\n",
539 ldb_dn_get_linearized(sid_dn)));
541 r->out.result = NT_STATUS_NO_SUCH_USER;
546 for (i=0; i<st->r->in.dns_names->count; i++) {
547 struct NL_DNS_NAME_INFO *n = &r->in.dns_names->names[i];
549 case NlDnsLdapAtSite:
550 dprintf(st->fd, "SRV _ldap._tcp.%s._sites.%s. %s %u\n",
551 site, dnsdomain, hostname, n->port);
554 dprintf(st->fd, "SRV _ldap._tcp.%s._sites.gc._msdcs.%s. %s %u\n",
555 site, dnsdomain, hostname, n->port);
558 dprintf(st->fd, "CNAME %s._msdcs.%s. %s\n",
559 ntdsguid, dnsforest, hostname);
562 dprintf(st->fd, "SRV _kerberos._tcp.%s._sites.dc._msdcs.%s. %s %u\n",
563 site, dnsdomain, hostname, n->port);
566 dprintf(st->fd, "SRV _ldap._tcp.%s._sites.dc._msdcs.%s. %s %u\n",
567 site, dnsdomain, hostname, n->port);
569 case NlDnsRfc1510KdcAtSite:
570 dprintf(st->fd, "SRV _kerberos._tcp.%s._sites.%s. %s %u\n",
571 site, dnsdomain, hostname, n->port);
573 case NlDnsGenericGcAtSite:
574 dprintf(st->fd, "SRV _gc._tcp.%s._sites.%s. %s %u\n",
575 site, dnsforest, hostname, n->port);
583 DEBUG(3,("Calling RODC DNS name update script %s\n", st->tmp_path));
584 req = samba_runcmd_send(st,
586 timeval_current_ofs(20, 0),
594 NT_STATUS_HAVE_NO_MEMORY(req);
596 /* setup the callback */
597 tevent_req_set_callback(req, dnsupdate_RODC_callback, st);
599 msg->defer_reply = true;
605 startup the dns update task
607 static void dnsupdate_task_init(struct task_server *task)
610 struct dnsupdate_service *service;
612 if (lpcfg_server_role(task->lp_ctx) != ROLE_ACTIVE_DIRECTORY_DC) {
613 /* not useful for non-DC */
617 task_server_set_title(task, "task[dnsupdate]");
619 service = talloc_zero(task, struct dnsupdate_service);
621 task_server_terminate(task, "dnsupdate_task_init: out of memory", true);
624 service->task = task;
625 task->private_data = service;
627 service->system_session_info = system_session(service->task->lp_ctx);
628 if (!service->system_session_info) {
629 task_server_terminate(task,
630 "dnsupdate: Failed to obtain server credentials\n",
635 service->samdb = samdb_connect(service, service->task->event_ctx, task->lp_ctx,
636 service->system_session_info, 0);
637 if (!service->samdb) {
638 task_server_terminate(task, "dnsupdate: Failed to connect to local samdb\n",
643 service->confupdate.interval = lpcfg_parm_int(task->lp_ctx, NULL,
644 "dnsupdate", "config interval", 60); /* in seconds */
646 service->nameupdate.interval = lpcfg_parm_int(task->lp_ctx, NULL,
647 "dnsupdate", "name interval", 600); /* in seconds */
649 dnsupdate_rebuild(service);
650 status = dnsupdate_confupdate_schedule(service);
651 if (!NT_STATUS_IS_OK(status)) {
652 task_server_terminate(task, talloc_asprintf(task,
653 "dnsupdate: Failed to confupdate schedule: %s\n",
654 nt_errstr(status)), true);
658 dnsupdate_check_names(service);
659 status = dnsupdate_nameupdate_schedule(service);
660 if (!NT_STATUS_IS_OK(status)) {
661 task_server_terminate(task, talloc_asprintf(task,
662 "dnsupdate: Failed to nameupdate schedule: %s\n",
663 nt_errstr(status)), true);
667 irpc_add_name(task->msg_ctx, "dnsupdate");
669 IRPC_REGISTER(task->msg_ctx, irpc, DNSUPDATE_RODC,
670 dnsupdate_dnsupdate_RODC, service);
672 /* create the intial file */
673 dnsupdate_rebuild(service);
678 register ourselves as a available server
680 NTSTATUS server_service_dnsupdate_init(void)
682 return register_server_service("dnsupdate", dnsupdate_task_init);