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
29 #include "dsdb/samdb/samdb.h"
30 #include "auth/auth.h"
31 #include "smbd/service.h"
32 #include "lib/messaging/irpc.h"
33 #include "param/param.h"
34 #include "system/filesys.h"
35 #include "libcli/composite/composite.h"
37 struct dnsupdate_service {
38 struct task_server *task;
39 struct auth_session_info *system_session_info;
40 struct ldb_context *samdb;
44 struct tevent_timer *te;
45 struct composite_context *c;
51 called when rndc reload has finished
53 static void dnsupdate_rndc_done(struct composite_context *c)
55 struct dnsupdate_service *service = talloc_get_type_abort(c->async.private_data,
56 struct dnsupdate_service);
57 service->periodic.status = composite_wait(c);
58 if (!NT_STATUS_IS_OK(service->periodic.status)) {
59 DEBUG(0,(__location__ ": Failed rndc update - %s\n",
60 nt_errstr(service->periodic.status)));
64 service->periodic.c = NULL;
68 called every dnsupdate:interval seconds
70 static void dnsupdate_rebuild(struct dnsupdate_service *service)
73 struct ldb_result *res;
74 const char *tmp_path, *path;
76 const char *attrs[] = { "sAMAccountName", NULL };
77 const char *realm = lp_realm(service->task->lp_ctx);
78 TALLOC_CTX *tmp_ctx = talloc_new(service);
80 ret = ldb_search(service->samdb, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
81 attrs, "(&(primaryGroupID=%u)(objectClass=computer))",
83 if (ret != LDB_SUCCESS) {
84 DEBUG(0,(__location__ ": Unable to find DCs list - %s", ldb_errstring(service->samdb)));
89 path = lp_parm_string(service->task->lp_ctx, NULL, "dnsupdate", "path");
91 path = private_path(tmp_ctx, service->task->lp_ctx, "named.conf.update");
94 tmp_path = talloc_asprintf(tmp_ctx, "%s.tmp", path);
95 if (path == NULL || tmp_path == NULL) {
96 DEBUG(0,(__location__ ": Unable to get paths"));
102 fd = open(tmp_path, O_CREAT|O_TRUNC|O_WRONLY, 0444);
104 DEBUG(1,(__location__ ": Unable to open %s - %s\n", tmp_path, strerror(errno)));
105 talloc_free(tmp_ctx);
109 dprintf(fd, "/* this file is auto-generated - do not edit */\n");
110 dprintf(fd, "update-policy {\n");
111 dprintf(fd, "\tgrant %s ms-self * A AAAA;\n", realm);
112 dprintf(fd, "\tgrant administrator@%s wildcard * A AAAA SRV CNAME TXT;\n", realm);
114 for (i=0; i<res->count; i++) {
115 const char *acctname;
116 acctname = ldb_msg_find_attr_as_string(res->msgs[i],
117 "sAMAccountName", NULL);
118 if (!acctname) continue;
119 dprintf(fd, "\tgrant %s@%s wildcard * A AAAA SRV CNAME;\n",
125 if (service->periodic.c != NULL) {
126 talloc_free(service->periodic.c);
127 service->periodic.c = NULL;
130 if (NT_STATUS_IS_OK(service->periodic.status) &&
131 file_compare(tmp_path, path) == true) {
133 talloc_free(tmp_ctx);
137 if (rename(tmp_path, path) != 0) {
138 DEBUG(0,(__location__ ": Failed to rename %s to %s - %s\n",
139 tmp_path, path, strerror(errno)));
140 talloc_free(tmp_ctx);
144 DEBUG(2,("Loading new DNS update grant rules\n"));
145 service->periodic.c = samba_runcmd(service->task->event_ctx, service,
146 timeval_current_ofs(10, 0),
148 lp_rndc_command(service->task->lp_ctx),
150 service->periodic.c->async.fn = dnsupdate_rndc_done;
151 service->periodic.c->async.private_data = service;
153 talloc_free(tmp_ctx);
156 static NTSTATUS dnsupdate_periodic_schedule(struct dnsupdate_service *service);
159 called every dnsupdate:interval seconds
161 static void dnsupdate_periodic_handler_te(struct tevent_context *ev, struct tevent_timer *te,
162 struct timeval t, void *ptr)
164 struct dnsupdate_service *service = talloc_get_type(ptr, struct dnsupdate_service);
166 dnsupdate_rebuild(service);
167 dnsupdate_periodic_schedule(service);
171 static NTSTATUS dnsupdate_periodic_schedule(struct dnsupdate_service *service)
173 service->periodic.te = tevent_add_timer(service->task->event_ctx, service,
174 timeval_current_ofs(service->periodic.interval, 0),
175 dnsupdate_periodic_handler_te, service);
176 NT_STATUS_HAVE_NO_MEMORY(service->periodic.te);
181 startup the dns update task
183 static void dnsupdate_task_init(struct task_server *task)
186 struct dnsupdate_service *service;
188 if (lp_server_role(task->lp_ctx) != ROLE_DOMAIN_CONTROLLER) {
189 /* not useful for non-DC */
193 task_server_set_title(task, "task[dnsupdate]");
195 service = talloc_zero(task, struct dnsupdate_service);
197 task_server_terminate(task, "dnsupdate_task_init: out of memory", true);
200 service->task = task;
201 task->private_data = service;
203 service->system_session_info = system_session(service->task->lp_ctx);
204 if (!service->system_session_info) {
205 task_server_terminate(task,
206 "dnsupdate: Failed to obtain server credentials\n",
211 service->samdb = samdb_connect(service, service->task->event_ctx, task->lp_ctx,
212 service->system_session_info);
213 if (!service->samdb) {
214 task_server_terminate(task, "dnsupdate: Failed to connect to local samdb\n",
219 service->periodic.interval = lp_parm_int(task->lp_ctx, NULL,
220 "dnsupdate", "interval", 60); /* in seconds */
222 status = dnsupdate_periodic_schedule(service);
223 if (!NT_STATUS_IS_OK(status)) {
224 task_server_terminate(task, talloc_asprintf(task,
225 "dnsupdate: Failed to periodic schedule: %s\n",
226 nt_errstr(status)), true);
230 irpc_add_name(task->msg_ctx, "dnsupdate");
232 /* create the intial file */
233 dnsupdate_rebuild(service);
238 register ourselves as a available server
240 NTSTATUS server_service_dnsupdate_init(void)
242 return register_server_service("dnsupdate", dnsupdate_task_init);