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 NTSTATUS server_service_dnsupdate_init(void);
44 struct dnsupdate_service {
45 struct task_server *task;
46 struct auth_session_info *system_session_info;
47 struct ldb_context *samdb;
49 /* status for periodic config file update */
52 struct tevent_timer *te;
53 struct tevent_req *subreq;
57 /* status for periodic DNS name check */
60 struct tevent_timer *te;
61 struct tevent_req *subreq;
62 struct tevent_req *spnreq;
68 called when rndc reload has finished
70 static void dnsupdate_rndc_done(struct tevent_req *subreq)
72 struct dnsupdate_service *service = tevent_req_callback_data(subreq,
73 struct dnsupdate_service);
77 service->confupdate.subreq = NULL;
79 ret = samba_runcmd_recv(subreq, &sys_errno);
82 service->confupdate.status = map_nt_error_from_unix(sys_errno);
84 service->confupdate.status = NT_STATUS_OK;
87 if (!NT_STATUS_IS_OK(service->confupdate.status)) {
88 DEBUG(0,(__location__ ": Failed rndc update - %s\n",
89 nt_errstr(service->confupdate.status)));
91 DEBUG(3,("Completed rndc reload OK\n"));
96 called every 'dnsupdate:conf interval' seconds
98 static void dnsupdate_rebuild(struct dnsupdate_service *service)
102 struct ldb_result *res;
103 const char *tmp_path, *path, *path_static;
104 char *static_policies;
107 const char *attrs[] = { "sAMAccountName", NULL };
108 const char *realm = lpcfg_realm(service->task->lp_ctx);
109 TALLOC_CTX *tmp_ctx = talloc_new(service);
110 const char * const *rndc_command = lpcfg_rndc_command(service->task->lp_ctx);
112 /* abort any pending script run */
113 TALLOC_FREE(service->confupdate.subreq);
115 ret = ldb_search(service->samdb, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
116 attrs, "(|(samaccountname=administrator)(&(primaryGroupID=%u)(objectClass=computer)))",
118 if (ret != LDB_SUCCESS) {
119 DEBUG(0,(__location__ ": Unable to find DCs list - %s", ldb_errstring(service->samdb)));
120 talloc_free(tmp_ctx);
124 path = lpcfg_parm_string(service->task->lp_ctx, NULL, "dnsupdate", "path");
126 path = lpcfg_private_path(tmp_ctx, service->task->lp_ctx, "named.conf.update");
129 path_static = lpcfg_parm_string(service->task->lp_ctx, NULL, "dnsupdate", "extra_static_grant_rules");
130 if (path_static == NULL) {
131 path_static = lpcfg_private_path(tmp_ctx, service->task->lp_ctx, "named.conf.update.static");
134 tmp_path = talloc_asprintf(tmp_ctx, "%s.tmp", path);
135 if (path == NULL || tmp_path == NULL || path_static == NULL ) {
136 DEBUG(0,(__location__ ": Unable to get paths\n"));
137 talloc_free(tmp_ctx);
141 static_policies = file_load(path_static, &size, 0, tmp_ctx);
144 fd = open(tmp_path, O_CREAT|O_TRUNC|O_WRONLY, 0444);
146 DEBUG(1,(__location__ ": Unable to open %s - %s\n", tmp_path, strerror(errno)));
147 talloc_free(tmp_ctx);
151 dprintf(fd, "/* this file is auto-generated - do not edit */\n");
152 dprintf(fd, "update-policy {\n");
153 if( static_policies != NULL ) {
154 dprintf(fd, "/* Start of static entries */\n");
155 dprintf(fd, "%s\n",static_policies);
156 dprintf(fd, "/* End of static entries */\n");
158 dprintf(fd, "\tgrant %s ms-self * A AAAA;\n", realm);
160 for (i=0; i<res->count; i++) {
161 const char *acctname;
162 acctname = ldb_msg_find_attr_as_string(res->msgs[i],
163 "sAMAccountName", NULL);
164 if (!acctname) continue;
165 dprintf(fd, "\tgrant %s@%s wildcard * A AAAA SRV CNAME;\n",
172 if (NT_STATUS_IS_OK(service->confupdate.status) &&
173 file_compare(tmp_path, path) == true) {
175 talloc_free(tmp_ctx);
179 if (rename(tmp_path, path) != 0) {
180 DEBUG(0,(__location__ ": Failed to rename %s to %s - %s\n",
181 tmp_path, path, strerror(errno)));
182 talloc_free(tmp_ctx);
186 DEBUG(2,("Loading new DNS update grant rules\n"));
187 service->confupdate.subreq = samba_runcmd_send(service,
188 service->task->event_ctx,
189 timeval_current_ofs(10, 0),
193 if (service->confupdate.subreq == NULL) {
194 DEBUG(0,(__location__ ": samba_runcmd_send() failed with no memory\n"));
195 talloc_free(tmp_ctx);
198 tevent_req_set_callback(service->confupdate.subreq,
202 talloc_free(tmp_ctx);
205 static NTSTATUS dnsupdate_confupdate_schedule(struct dnsupdate_service *service);
208 called every 'dnsupdate:conf interval' seconds
210 static void dnsupdate_confupdate_handler_te(struct tevent_context *ev, struct tevent_timer *te,
211 struct timeval t, void *ptr)
213 struct dnsupdate_service *service = talloc_get_type(ptr, struct dnsupdate_service);
215 dnsupdate_rebuild(service);
216 dnsupdate_confupdate_schedule(service);
220 static NTSTATUS dnsupdate_confupdate_schedule(struct dnsupdate_service *service)
222 service->confupdate.te = tevent_add_timer(service->task->event_ctx, service,
223 timeval_current_ofs(service->confupdate.interval, 0),
224 dnsupdate_confupdate_handler_te, service);
225 NT_STATUS_HAVE_NO_MEMORY(service->confupdate.te);
231 called when dns update script has finished
233 static void dnsupdate_nameupdate_done(struct tevent_req *subreq)
235 struct dnsupdate_service *service = tevent_req_callback_data(subreq,
236 struct dnsupdate_service);
240 service->nameupdate.subreq = NULL;
242 ret = samba_runcmd_recv(subreq, &sys_errno);
245 service->nameupdate.status = map_nt_error_from_unix(sys_errno);
247 service->nameupdate.status = NT_STATUS_OK;
250 if (!NT_STATUS_IS_OK(service->nameupdate.status)) {
251 DEBUG(0,(__location__ ": Failed DNS update - %s\n",
252 nt_errstr(service->nameupdate.status)));
254 DEBUG(3,("Completed DNS update check OK\n"));
260 called when spn update script has finished
262 static void dnsupdate_spnupdate_done(struct tevent_req *subreq)
264 struct dnsupdate_service *service = tevent_req_callback_data(subreq,
265 struct dnsupdate_service);
269 service->nameupdate.spnreq = NULL;
271 ret = samba_runcmd_recv(subreq, &sys_errno);
274 service->nameupdate.status = map_nt_error_from_unix(sys_errno);
276 service->nameupdate.status = NT_STATUS_OK;
279 if (!NT_STATUS_IS_OK(service->nameupdate.status)) {
280 DEBUG(0,(__location__ ": Failed SPN update - %s\n",
281 nt_errstr(service->nameupdate.status)));
283 DEBUG(3,("Completed SPN update check OK\n"));
288 called every 'dnsupdate:name interval' seconds
290 static void dnsupdate_check_names(struct dnsupdate_service *service)
292 const char * const *dns_update_command = lpcfg_dns_update_command(service->task->lp_ctx);
293 const char * const *spn_update_command = lpcfg_spn_update_command(service->task->lp_ctx);
295 /* kill any existing child */
296 TALLOC_FREE(service->nameupdate.subreq);
298 DEBUG(3,("Calling DNS name update script\n"));
299 service->nameupdate.subreq = samba_runcmd_send(service,
300 service->task->event_ctx,
301 timeval_current_ofs(20, 0),
305 if (service->nameupdate.subreq == NULL) {
306 DEBUG(0,(__location__ ": samba_runcmd_send() failed with no memory\n"));
309 tevent_req_set_callback(service->nameupdate.subreq,
310 dnsupdate_nameupdate_done,
313 DEBUG(3,("Calling SPN name update script\n"));
314 service->nameupdate.spnreq = samba_runcmd_send(service,
315 service->task->event_ctx,
316 timeval_current_ofs(20, 0),
320 if (service->nameupdate.spnreq == NULL) {
321 DEBUG(0,(__location__ ": samba_runcmd_send() failed with no memory\n"));
324 tevent_req_set_callback(service->nameupdate.spnreq,
325 dnsupdate_spnupdate_done,
329 static NTSTATUS dnsupdate_nameupdate_schedule(struct dnsupdate_service *service);
332 called every 'dnsupdate:name interval' seconds
334 static void dnsupdate_nameupdate_handler_te(struct tevent_context *ev, struct tevent_timer *te,
335 struct timeval t, void *ptr)
337 struct dnsupdate_service *service = talloc_get_type(ptr, struct dnsupdate_service);
339 dnsupdate_check_names(service);
340 dnsupdate_nameupdate_schedule(service);
344 static NTSTATUS dnsupdate_nameupdate_schedule(struct dnsupdate_service *service)
346 service->nameupdate.te = tevent_add_timer(service->task->event_ctx, service,
347 timeval_current_ofs(service->nameupdate.interval, 0),
348 dnsupdate_nameupdate_handler_te, service);
349 NT_STATUS_HAVE_NO_MEMORY(service->nameupdate.te);
354 struct dnsupdate_RODC_state {
355 struct irpc_message *msg;
356 struct dnsupdate_RODC *r;
361 static int dnsupdate_RODC_destructor(struct dnsupdate_RODC_state *st)
366 unlink(st->tmp_path);
371 called when the DNS update has completed
373 static void dnsupdate_RODC_callback(struct tevent_req *req)
375 struct dnsupdate_RODC_state *st =
376 tevent_req_callback_data(req,
377 struct dnsupdate_RODC_state);
381 ret = samba_runcmd_recv(req, &sys_errno);
384 st->r->out.result = map_nt_error_from_unix(sys_errno);
385 DEBUG(2,(__location__ ": RODC DNS Update failed: %s\n", nt_errstr(st->r->out.result)));
387 st->r->out.result = NT_STATUS_OK;
388 DEBUG(3,(__location__ ": RODC DNS Update OK\n"));
391 for (i=0; i<st->r->in.dns_names->count; i++) {
392 st->r->out.dns_names->names[i].status = NT_STATUS_V(st->r->out.result);
395 irpc_send_reply(st->msg, NT_STATUS_OK);
400 * Called when we get a RODC DNS update request from the netlogon
403 static NTSTATUS dnsupdate_dnsupdate_RODC(struct irpc_message *msg,
404 struct dnsupdate_RODC *r)
406 struct dnsupdate_service *s = talloc_get_type(msg->private_data,
407 struct dnsupdate_service);
408 const char * const *dns_update_command = lpcfg_dns_update_command(s->task->lp_ctx);
409 struct dnsupdate_RODC_state *st;
410 struct tevent_req *req;
412 struct GUID ntds_guid;
413 const char *site, *dnsdomain, *dnsforest, *ntdsguid, *hostname;
414 struct ldb_dn *sid_dn;
415 const char *attrs[] = { "dNSHostName", NULL };
416 struct ldb_result *res;
418 st = talloc_zero(msg, struct dnsupdate_RODC_state);
420 r->out.result = NT_STATUS_NO_MEMORY;
427 st->tmp_path = smbd_tmp_path(st, s->task->lp_ctx, "rodcdns.XXXXXX");
430 r->out.result = NT_STATUS_NO_MEMORY;
434 st->fd = mkstemp(st->tmp_path);
436 DEBUG(0,("Unable to create a temporary file for RODC dnsupdate\n"));
438 r->out.result = NT_STATUS_INTERNAL_DB_CORRUPTION;
442 talloc_set_destructor(st, dnsupdate_RODC_destructor);
444 sid_dn = ldb_dn_new_fmt(st, s->samdb, "<SID=%s>", dom_sid_string(st, r->in.dom_sid));
447 r->out.result = NT_STATUS_NO_MEMORY;
451 /* work out the site */
452 ret = samdb_find_site_for_computer(s->samdb, st, sid_dn, &site);
453 if (ret != LDB_SUCCESS) {
454 DEBUG(2, (__location__ ": Unable to find site for computer %s\n",
455 ldb_dn_get_linearized(sid_dn)));
457 r->out.result = NT_STATUS_NO_SUCH_USER;
461 /* work out the ntdsguid */
462 ret = samdb_find_ntdsguid_for_computer(s->samdb, sid_dn, &ntds_guid);
463 ntdsguid = GUID_string(st, &ntds_guid);
464 if (ret != LDB_SUCCESS || !ntdsguid) {
465 DEBUG(2, (__location__ ": Unable to find NTDS GUID for computer %s\n",
466 ldb_dn_get_linearized(sid_dn)));
468 r->out.result = NT_STATUS_NO_SUCH_USER;
473 /* find dnsdomain and dnsforest */
474 dnsdomain = lpcfg_realm(s->task->lp_ctx);
475 dnsforest = dnsdomain;
477 /* find the hostname */
478 ret = dsdb_search_dn(s->samdb, st, &res, sid_dn, attrs, 0);
479 if (ret == LDB_SUCCESS) {
480 hostname = ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
482 if (ret != LDB_SUCCESS || !hostname) {
483 DEBUG(2, (__location__ ": Unable to find NTDS GUID for computer %s\n",
484 ldb_dn_get_linearized(sid_dn)));
486 r->out.result = NT_STATUS_NO_SUCH_USER;
491 for (i=0; i<st->r->in.dns_names->count; i++) {
492 struct NL_DNS_NAME_INFO *n = &r->in.dns_names->names[i];
494 case NlDnsLdapAtSite:
495 dprintf(st->fd, "SRV _ldap._tcp.%s._sites.%s. %s %u\n",
496 site, dnsdomain, hostname, n->port);
499 dprintf(st->fd, "SRV _ldap._tcp.%s._sites.gc._msdcs.%s. %s %u\n",
500 site, dnsdomain, hostname, n->port);
503 dprintf(st->fd, "CNAME %s._msdcs.%s. %s\n",
504 ntdsguid, dnsforest, hostname);
507 dprintf(st->fd, "SRV _kerberos._tcp.%s._sites.dc._msdcs.%s. %s %u\n",
508 site, dnsdomain, hostname, n->port);
511 dprintf(st->fd, "SRV _ldap._tcp.%s._sites.dc._msdcs.%s. %s %u\n",
512 site, dnsdomain, hostname, n->port);
514 case NlDnsRfc1510KdcAtSite:
515 dprintf(st->fd, "SRV _kerberos._tcp.%s._sites.%s. %s %u\n",
516 site, dnsdomain, hostname, n->port);
518 case NlDnsGenericGcAtSite:
519 dprintf(st->fd, "SRV _gc._tcp.%s._sites.%s. %s %u\n",
520 site, dnsforest, hostname, n->port);
528 DEBUG(3,("Calling RODC DNS name update script %s\n", st->tmp_path));
529 req = samba_runcmd_send(st,
531 timeval_current_ofs(20, 0),
537 NT_STATUS_HAVE_NO_MEMORY(req);
539 /* setup the callback */
540 tevent_req_set_callback(req, dnsupdate_RODC_callback, st);
542 msg->defer_reply = true;
548 startup the dns update task
550 static void dnsupdate_task_init(struct task_server *task)
553 struct dnsupdate_service *service;
555 if (lpcfg_server_role(task->lp_ctx) != ROLE_DOMAIN_CONTROLLER) {
556 /* not useful for non-DC */
560 task_server_set_title(task, "task[dnsupdate]");
562 service = talloc_zero(task, struct dnsupdate_service);
564 task_server_terminate(task, "dnsupdate_task_init: out of memory", true);
567 service->task = task;
568 task->private_data = service;
570 service->system_session_info = system_session(service->task->lp_ctx);
571 if (!service->system_session_info) {
572 task_server_terminate(task,
573 "dnsupdate: Failed to obtain server credentials\n",
578 service->samdb = samdb_connect(service, service->task->event_ctx, task->lp_ctx,
579 service->system_session_info, 0);
580 if (!service->samdb) {
581 task_server_terminate(task, "dnsupdate: Failed to connect to local samdb\n",
586 service->confupdate.interval = lpcfg_parm_int(task->lp_ctx, NULL,
587 "dnsupdate", "config interval", 60); /* in seconds */
589 service->nameupdate.interval = lpcfg_parm_int(task->lp_ctx, NULL,
590 "dnsupdate", "name interval", 600); /* in seconds */
592 dnsupdate_rebuild(service);
593 status = dnsupdate_confupdate_schedule(service);
594 if (!NT_STATUS_IS_OK(status)) {
595 task_server_terminate(task, talloc_asprintf(task,
596 "dnsupdate: Failed to confupdate schedule: %s\n",
597 nt_errstr(status)), true);
601 dnsupdate_check_names(service);
602 status = dnsupdate_nameupdate_schedule(service);
603 if (!NT_STATUS_IS_OK(status)) {
604 task_server_terminate(task, talloc_asprintf(task,
605 "dnsupdate: Failed to nameupdate schedule: %s\n",
606 nt_errstr(status)), true);
610 irpc_add_name(task->msg_ctx, "dnsupdate");
612 IRPC_REGISTER(task->msg_ctx, irpc, DNSUPDATE_RODC,
613 dnsupdate_dnsupdate_RODC, service);
615 /* create the intial file */
616 dnsupdate_rebuild(service);
621 register ourselves as a available server
623 NTSTATUS server_service_dnsupdate_init(void)
625 return register_server_service("dnsupdate", dnsupdate_task_init);