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 "libcli/composite/composite.h"
39 #include "libcli/security/dom_sid.h"
40 #include "librpc/gen_ndr/ndr_irpc.h"
42 struct dnsupdate_service {
43 struct task_server *task;
44 struct auth_session_info *system_session_info;
45 struct ldb_context *samdb;
47 /* status for periodic config file update */
50 struct tevent_timer *te;
51 struct tevent_req *subreq;
55 /* status for periodic DNS name check */
58 struct tevent_timer *te;
59 struct tevent_req *subreq;
60 struct tevent_req *spnreq;
66 called when rndc reload has finished
68 static void dnsupdate_rndc_done(struct tevent_req *subreq)
70 struct dnsupdate_service *service = tevent_req_callback_data(subreq,
71 struct dnsupdate_service);
75 service->confupdate.subreq = NULL;
77 ret = samba_runcmd_recv(subreq, &sys_errno);
80 service->confupdate.status = map_nt_error_from_unix(sys_errno);
82 service->confupdate.status = NT_STATUS_OK;
85 if (!NT_STATUS_IS_OK(service->confupdate.status)) {
86 DEBUG(0,(__location__ ": Failed rndc update - %s\n",
87 nt_errstr(service->confupdate.status)));
89 DEBUG(3,("Completed rndc reload OK\n"));
94 called every 'dnsupdate:conf interval' seconds
96 static void dnsupdate_rebuild(struct dnsupdate_service *service)
100 struct ldb_result *res;
101 const char *tmp_path, *path, *path_static;
102 char *static_policies;
105 const char *attrs[] = { "sAMAccountName", NULL };
106 const char *realm = lpcfg_realm(service->task->lp_ctx);
107 TALLOC_CTX *tmp_ctx = talloc_new(service);
108 const char * const *rndc_command = lpcfg_rndc_command(service->task->lp_ctx);
110 /* abort any pending script run */
111 TALLOC_FREE(service->confupdate.subreq);
113 ret = ldb_search(service->samdb, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
114 attrs, "(&(primaryGroupID=%u)(objectClass=computer))",
116 if (ret != LDB_SUCCESS) {
117 DEBUG(0,(__location__ ": Unable to find DCs list - %s", ldb_errstring(service->samdb)));
118 talloc_free(tmp_ctx);
122 path = lpcfg_parm_string(service->task->lp_ctx, NULL, "dnsupdate", "path");
124 path = private_path(tmp_ctx, service->task->lp_ctx, "named.conf.update");
127 path_static = lpcfg_parm_string(service->task->lp_ctx, NULL, "dnsupdate", "extra_static_grant_rules");
128 if (path_static == NULL) {
129 path_static = private_path(tmp_ctx, service->task->lp_ctx, "named.conf.update.static");
132 tmp_path = talloc_asprintf(tmp_ctx, "%s.tmp", path);
133 if (path == NULL || tmp_path == NULL || path_static == NULL ) {
134 DEBUG(0,(__location__ ": Unable to get paths\n"));
135 talloc_free(tmp_ctx);
139 static_policies = file_load(path_static, &size, 0, tmp_ctx);
142 fd = open(tmp_path, O_CREAT|O_TRUNC|O_WRONLY, 0444);
144 DEBUG(1,(__location__ ": Unable to open %s - %s\n", tmp_path, strerror(errno)));
145 talloc_free(tmp_ctx);
149 dprintf(fd, "/* this file is auto-generated - do not edit */\n");
150 dprintf(fd, "update-policy {\n");
151 if( static_policies != NULL ) {
152 dprintf(fd, "/* Start of static entries */\n");
153 dprintf(fd, "%s\n",static_policies);
154 dprintf(fd, "/* End of static entries */\n");
156 dprintf(fd, "\tgrant %s ms-self * A AAAA;\n", realm);
157 dprintf(fd, "\tgrant administrator@%s wildcard * A AAAA SRV CNAME TXT;\n", realm);
159 for (i=0; i<res->count; i++) {
160 const char *acctname;
161 acctname = ldb_msg_find_attr_as_string(res->msgs[i],
162 "sAMAccountName", NULL);
163 if (!acctname) continue;
164 dprintf(fd, "\tgrant %s@%s wildcard * A AAAA SRV CNAME;\n",
171 if (NT_STATUS_IS_OK(service->confupdate.status) &&
172 file_compare(tmp_path, path) == true) {
174 talloc_free(tmp_ctx);
178 if (rename(tmp_path, path) != 0) {
179 DEBUG(0,(__location__ ": Failed to rename %s to %s - %s\n",
180 tmp_path, path, strerror(errno)));
181 talloc_free(tmp_ctx);
185 DEBUG(2,("Loading new DNS update grant rules\n"));
186 service->confupdate.subreq = samba_runcmd_send(service,
187 service->task->event_ctx,
188 timeval_current_ofs(10, 0),
192 if (service->confupdate.subreq == NULL) {
193 DEBUG(0,(__location__ ": samba_runcmd_send() failed with no memory\n"));
194 talloc_free(tmp_ctx);
197 tevent_req_set_callback(service->confupdate.subreq,
201 talloc_free(tmp_ctx);
204 static NTSTATUS dnsupdate_confupdate_schedule(struct dnsupdate_service *service);
207 called every 'dnsupdate:conf interval' seconds
209 static void dnsupdate_confupdate_handler_te(struct tevent_context *ev, struct tevent_timer *te,
210 struct timeval t, void *ptr)
212 struct dnsupdate_service *service = talloc_get_type(ptr, struct dnsupdate_service);
214 dnsupdate_rebuild(service);
215 dnsupdate_confupdate_schedule(service);
219 static NTSTATUS dnsupdate_confupdate_schedule(struct dnsupdate_service *service)
221 service->confupdate.te = tevent_add_timer(service->task->event_ctx, service,
222 timeval_current_ofs(service->confupdate.interval, 0),
223 dnsupdate_confupdate_handler_te, service);
224 NT_STATUS_HAVE_NO_MEMORY(service->confupdate.te);
230 called when dns update script has finished
232 static void dnsupdate_nameupdate_done(struct tevent_req *subreq)
234 struct dnsupdate_service *service = tevent_req_callback_data(subreq,
235 struct dnsupdate_service);
239 service->nameupdate.subreq = NULL;
241 ret = samba_runcmd_recv(subreq, &sys_errno);
244 service->nameupdate.status = map_nt_error_from_unix(sys_errno);
246 service->nameupdate.status = NT_STATUS_OK;
249 if (!NT_STATUS_IS_OK(service->nameupdate.status)) {
250 DEBUG(0,(__location__ ": Failed DNS update - %s\n",
251 nt_errstr(service->nameupdate.status)));
253 DEBUG(3,("Completed DNS update check OK\n"));
259 called when spn update script has finished
261 static void dnsupdate_spnupdate_done(struct tevent_req *subreq)
263 struct dnsupdate_service *service = tevent_req_callback_data(subreq,
264 struct dnsupdate_service);
268 service->nameupdate.spnreq = NULL;
270 ret = samba_runcmd_recv(subreq, &sys_errno);
273 service->nameupdate.status = map_nt_error_from_unix(sys_errno);
275 service->nameupdate.status = NT_STATUS_OK;
278 if (!NT_STATUS_IS_OK(service->nameupdate.status)) {
279 DEBUG(0,(__location__ ": Failed SPN update - %s\n",
280 nt_errstr(service->nameupdate.status)));
282 DEBUG(3,("Completed SPN update check OK\n"));
287 called every 'dnsupdate:name interval' seconds
289 static void dnsupdate_check_names(struct dnsupdate_service *service)
291 const char * const *dns_update_command = lpcfg_dns_update_command(service->task->lp_ctx);
292 const char * const *spn_update_command = lpcfg_spn_update_command(service->task->lp_ctx);
294 /* kill any existing child */
295 TALLOC_FREE(service->nameupdate.subreq);
297 DEBUG(3,("Calling DNS name update script\n"));
298 service->nameupdate.subreq = samba_runcmd_send(service,
299 service->task->event_ctx,
300 timeval_current_ofs(10, 0),
304 if (service->nameupdate.subreq == NULL) {
305 DEBUG(0,(__location__ ": samba_runcmd_send() failed with no memory\n"));
308 tevent_req_set_callback(service->nameupdate.subreq,
309 dnsupdate_nameupdate_done,
312 DEBUG(3,("Calling SPN name update script\n"));
313 service->nameupdate.spnreq = samba_runcmd_send(service,
314 service->task->event_ctx,
315 timeval_current_ofs(10, 0),
319 if (service->nameupdate.spnreq == NULL) {
320 DEBUG(0,(__location__ ": samba_runcmd_send() failed with no memory\n"));
323 tevent_req_set_callback(service->nameupdate.spnreq,
324 dnsupdate_spnupdate_done,
328 static NTSTATUS dnsupdate_nameupdate_schedule(struct dnsupdate_service *service);
331 called every 'dnsupdate:name interval' seconds
333 static void dnsupdate_nameupdate_handler_te(struct tevent_context *ev, struct tevent_timer *te,
334 struct timeval t, void *ptr)
336 struct dnsupdate_service *service = talloc_get_type(ptr, struct dnsupdate_service);
338 dnsupdate_check_names(service);
339 dnsupdate_nameupdate_schedule(service);
343 static NTSTATUS dnsupdate_nameupdate_schedule(struct dnsupdate_service *service)
345 service->nameupdate.te = tevent_add_timer(service->task->event_ctx, service,
346 timeval_current_ofs(service->nameupdate.interval, 0),
347 dnsupdate_nameupdate_handler_te, service);
348 NT_STATUS_HAVE_NO_MEMORY(service->nameupdate.te);
353 struct dnsupdate_RODC_state {
354 struct irpc_message *msg;
355 struct dnsupdate_RODC *r;
360 static int dnsupdate_RODC_destructor(struct dnsupdate_RODC_state *st)
365 unlink(st->tmp_path);
370 called when the DNS update has completed
372 static void dnsupdate_RODC_callback(struct tevent_req *req)
374 struct dnsupdate_RODC_state *st =
375 tevent_req_callback_data(req,
376 struct dnsupdate_RODC_state);
380 ret = samba_runcmd_recv(req, &sys_errno);
383 st->r->out.result = map_nt_error_from_unix(sys_errno);
384 DEBUG(2,(__location__ ": RODC DNS Update failed: %s\n", nt_errstr(st->r->out.result)));
386 st->r->out.result = NT_STATUS_OK;
387 DEBUG(3,(__location__ ": RODC DNS Update OK\n"));
390 for (i=0; i<st->r->in.dns_names->count; i++) {
391 st->r->out.dns_names->names[i].status = NT_STATUS_V(st->r->out.result);
394 irpc_send_reply(st->msg, NT_STATUS_OK);
399 * Called when we get a RODC DNS update request from the netlogon
402 static NTSTATUS dnsupdate_dnsupdate_RODC(struct irpc_message *msg,
403 struct dnsupdate_RODC *r)
405 struct dnsupdate_service *s = talloc_get_type(msg->private_data,
406 struct dnsupdate_service);
407 const char * const *dns_update_command = lpcfg_dns_update_command(s->task->lp_ctx);
408 struct dnsupdate_RODC_state *st;
409 struct tevent_req *req;
411 struct GUID ntds_guid;
412 const char *site, *dnsdomain, *dnsforest, *ntdsguid, *hostname;
413 struct ldb_dn *sid_dn;
414 const char *attrs[] = { "dNSHostName", NULL };
415 struct ldb_result *res;
417 st = talloc_zero(msg, struct dnsupdate_RODC_state);
419 r->out.result = NT_STATUS_NO_MEMORY;
426 st->tmp_path = smbd_tmp_path(st, s->task->lp_ctx, "rodcdns.XXXXXX");
429 r->out.result = NT_STATUS_NO_MEMORY;
433 st->fd = mkstemp(st->tmp_path);
435 DEBUG(0,("Unable to create a temporary file for RODC dnsupdate\n"));
437 r->out.result = NT_STATUS_INTERNAL_DB_CORRUPTION;
441 talloc_set_destructor(st, dnsupdate_RODC_destructor);
443 sid_dn = ldb_dn_new_fmt(st, s->samdb, "<SID=%s>", dom_sid_string(st, r->in.dom_sid));
446 r->out.result = NT_STATUS_NO_MEMORY;
450 /* work out the site */
451 ret = samdb_find_site_for_computer(s->samdb, st, sid_dn, &site);
452 if (ret != LDB_SUCCESS) {
453 DEBUG(2, (__location__ ": Unable to find site for computer %s\n",
454 ldb_dn_get_linearized(sid_dn)));
456 r->out.result = NT_STATUS_NO_SUCH_USER;
460 /* work out the ntdsguid */
461 ret = samdb_find_ntdsguid_for_computer(s->samdb, sid_dn, &ntds_guid);
462 ntdsguid = GUID_string(st, &ntds_guid);
463 if (ret != LDB_SUCCESS || !ntdsguid) {
464 DEBUG(2, (__location__ ": Unable to find NTDS GUID for computer %s\n",
465 ldb_dn_get_linearized(sid_dn)));
467 r->out.result = NT_STATUS_NO_SUCH_USER;
472 /* find dnsdomain and dnsforest */
473 dnsdomain = lpcfg_realm(s->task->lp_ctx);
474 dnsforest = dnsdomain;
476 /* find the hostname */
477 ret = dsdb_search_dn(s->samdb, st, &res, sid_dn, attrs, 0);
478 if (ret == LDB_SUCCESS) {
479 hostname = ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
481 if (ret != LDB_SUCCESS || !hostname) {
482 DEBUG(2, (__location__ ": Unable to find NTDS GUID for computer %s\n",
483 ldb_dn_get_linearized(sid_dn)));
485 r->out.result = NT_STATUS_NO_SUCH_USER;
490 for (i=0; i<st->r->in.dns_names->count; i++) {
491 struct NL_DNS_NAME_INFO *n = &r->in.dns_names->names[i];
493 case NlDnsLdapAtSite:
494 dprintf(st->fd, "SRV _ldap._tcp.%s._sites.%s. %s %u\n",
495 site, dnsdomain, hostname, n->port);
498 dprintf(st->fd, "SRV _ldap._tcp.%s._sites.gc._msdcs.%s. %s %u\n",
499 site, dnsdomain, hostname, n->port);
502 dprintf(st->fd, "CNAME %s._msdcs.%s. %s\n",
503 ntdsguid, dnsforest, hostname);
506 dprintf(st->fd, "SRV _kerberos._tcp.%s._sites.dc._msdcs.%s. %s %u\n",
507 site, dnsdomain, hostname, n->port);
510 dprintf(st->fd, "SRV _ldap._tcp.%s._sites.dc._msdcs.%s. %s %u\n",
511 site, dnsdomain, hostname, n->port);
513 case NlDnsRfc1510KdcAtSite:
514 dprintf(st->fd, "SRV _kerberos._tcp.%s._sites.%s. %s %u\n",
515 site, dnsdomain, hostname, n->port);
517 case NlDnsGenericGcAtSite:
518 dprintf(st->fd, "SRV _gc._tcp.%s._sites.%s. %s %u\n",
519 site, dnsforest, hostname, n->port);
527 DEBUG(3,("Calling RODC DNS name update script %s\n", st->tmp_path));
528 req = samba_runcmd_send(st,
530 timeval_current_ofs(20, 0),
536 NT_STATUS_HAVE_NO_MEMORY(req);
538 /* setup the callback */
539 tevent_req_set_callback(req, dnsupdate_RODC_callback, st);
541 msg->defer_reply = true;
547 startup the dns update task
549 static void dnsupdate_task_init(struct task_server *task)
552 struct dnsupdate_service *service;
554 if (lpcfg_server_role(task->lp_ctx) != ROLE_DOMAIN_CONTROLLER) {
555 /* not useful for non-DC */
559 task_server_set_title(task, "task[dnsupdate]");
561 service = talloc_zero(task, struct dnsupdate_service);
563 task_server_terminate(task, "dnsupdate_task_init: out of memory", true);
566 service->task = task;
567 task->private_data = service;
569 service->system_session_info = system_session(service->task->lp_ctx);
570 if (!service->system_session_info) {
571 task_server_terminate(task,
572 "dnsupdate: Failed to obtain server credentials\n",
577 service->samdb = samdb_connect(service, service->task->event_ctx, task->lp_ctx,
578 service->system_session_info, 0);
579 if (!service->samdb) {
580 task_server_terminate(task, "dnsupdate: Failed to connect to local samdb\n",
585 service->confupdate.interval = lpcfg_parm_int(task->lp_ctx, NULL,
586 "dnsupdate", "config interval", 60); /* in seconds */
588 service->nameupdate.interval = lpcfg_parm_int(task->lp_ctx, NULL,
589 "dnsupdate", "name interval", 600); /* in seconds */
591 dnsupdate_rebuild(service);
592 status = dnsupdate_confupdate_schedule(service);
593 if (!NT_STATUS_IS_OK(status)) {
594 task_server_terminate(task, talloc_asprintf(task,
595 "dnsupdate: Failed to confupdate schedule: %s\n",
596 nt_errstr(status)), true);
600 dnsupdate_check_names(service);
601 status = dnsupdate_nameupdate_schedule(service);
602 if (!NT_STATUS_IS_OK(status)) {
603 task_server_terminate(task, talloc_asprintf(task,
604 "dnsupdate: Failed to nameupdate schedule: %s\n",
605 nt_errstr(status)), true);
609 irpc_add_name(task->msg_ctx, "dnsupdate");
611 IRPC_REGISTER(task->msg_ctx, irpc, DNSUPDATE_RODC,
612 dnsupdate_dnsupdate_RODC, service);
614 /* create the intial file */
615 dnsupdate_rebuild(service);
620 register ourselves as a available server
622 NTSTATUS server_service_dnsupdate_init(void)
624 return register_server_service("dnsupdate", dnsupdate_task_init);