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/events/events.h"
33 #include "lib/messaging/irpc.h"
34 #include "lib/ldb/include/ldb_errors.h"
35 #include "param/param.h"
36 #include "system/filesys.h"
37 #include "lib/tevent/tevent.h"
39 struct dnsupdate_service {
40 struct task_server *task;
41 struct auth_session_info *system_session_info;
42 struct ldb_context *samdb;
46 struct tevent_timer *te;
51 called every dnsupdate:interval seconds
53 static void dnsupdate_rebuild(struct dnsupdate_service *service)
56 struct ldb_result *res;
57 const char *tmp_path, *path;
59 const char *attrs[] = { "sAMAccountName", NULL };
60 const char *realm = lp_realm(service->task->lp_ctx);
61 TALLOC_CTX *tmp_ctx = talloc_new(service);
64 ret = ldb_search(service->samdb, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
65 attrs, "(&(primaryGroupID=%u)(objectClass=computer))",
67 if (ret != LDB_SUCCESS) {
68 DEBUG(0,(__location__ ": Unable to find DCs list - %s", ldb_errstring(service->samdb)));
73 path = lp_parm_string(service->task->lp_ctx, NULL, "dnsupdate", "path");
75 path = private_path(tmp_ctx, service->task->lp_ctx, "named.conf.update");
78 tmp_path = talloc_asprintf(tmp_ctx, "%s.tmp", path);
79 if (path == NULL || tmp_path == NULL) {
80 DEBUG(0,(__location__ ": Unable to get paths"));
85 fd = open(tmp_path, O_CREAT|O_TRUNC|O_WRONLY, 0444);
87 DEBUG(1,(__location__ ": Unable to open %s - %s\n", tmp_path, strerror(errno)));
92 dprintf(fd, "/* this file is auto-generated - do not edit */\n");
93 dprintf(fd, "update-policy {\n");
94 dprintf(fd, "\tgrant %s ms-self * A AAAA;\n", realm);
95 dprintf(fd, "\tgrant administrator@%s wildcard * A AAAA SRV CNAME TXT;\n", realm);
97 for (i=0; i<res->count; i++) {
99 acctname = ldb_msg_find_attr_as_string(res->msgs[i],
100 "sAMAccountName", NULL);
101 if (!acctname) continue;
102 dprintf(fd, "\tgrant %s@%s wildcard * A AAAA SRV CNAME;\n",
108 if (file_compare(tmp_path, path) == true) {
109 talloc_free(tmp_ctx);
113 if (rename(tmp_path, path) != 0) {
114 DEBUG(0,(__location__ ": Failed to rename %s to %s - %s\n",
115 tmp_path, path, strerror(errno)));
116 talloc_free(tmp_ctx);
120 DEBUG(1,("Loaded new DNS update grant rules\n"));
122 rndc_cmd = lp_parm_string(service->task->lp_ctx, NULL, "dnsupdate", "rndc reload command");
124 rndc_cmd = "/usr/sbin/rndc reload";
126 ret = system(rndc_cmd);
128 DEBUG(0,(__location__ ": Failed rndc reload command: '%s' - %d\n",
132 talloc_free(tmp_ctx);
135 static NTSTATUS dnsupdate_periodic_schedule(struct dnsupdate_service *service);
138 called every dnsupdate:interval seconds
140 static void dnsupdate_periodic_handler_te(struct tevent_context *ev, struct tevent_timer *te,
141 struct timeval t, void *ptr)
143 struct dnsupdate_service *service = talloc_get_type(ptr, struct dnsupdate_service);
145 dnsupdate_rebuild(service);
146 dnsupdate_periodic_schedule(service);
150 static NTSTATUS dnsupdate_periodic_schedule(struct dnsupdate_service *service)
152 service->periodic.te = tevent_add_timer(service->task->event_ctx, service,
153 timeval_current_ofs(service->periodic.interval, 0),
154 dnsupdate_periodic_handler_te, service);
155 NT_STATUS_HAVE_NO_MEMORY(service->periodic.te);
160 startup the dns update task
162 static void dnsupdate_task_init(struct task_server *task)
165 struct dnsupdate_service *service;
167 if (lp_server_role(task->lp_ctx) != ROLE_DOMAIN_CONTROLLER) {
168 /* not useful for non-DC */
172 task_server_set_title(task, "task[dnsupdate]");
174 service = talloc_zero(task, struct dnsupdate_service);
176 task_server_terminate(task, "dnsupdate_task_init: out of memory", true);
179 service->task = task;
180 task->private_data = service;
182 service->system_session_info = system_session(service->task->lp_ctx);
183 if (!service->system_session_info) {
184 task_server_terminate(task,
185 "dnsupdate: Failed to obtain server credentials\n",
190 service->samdb = samdb_connect(service, service->task->event_ctx, task->lp_ctx,
191 service->system_session_info);
192 if (!service->samdb) {
193 task_server_terminate(task, "dnsupdate: Failed to connect to local samdb\n",
198 service->periodic.interval = lp_parm_int(task->lp_ctx, NULL,
199 "dnsupdate", "interval", 60); /* in seconds */
201 status = dnsupdate_periodic_schedule(service);
202 if (!NT_STATUS_IS_OK(status)) {
203 task_server_terminate(task, talloc_asprintf(task,
204 "dnsupdate: Failed to periodic schedule: %s\n",
205 nt_errstr(status)), true);
209 irpc_add_name(task->msg_ctx, "dnsupdate");
211 /* create the intial file */
212 dnsupdate_rebuild(service);
217 register ourselves as a available server
219 NTSTATUS server_service_dnsupdate_init(void)
221 return register_server_service("dnsupdate", dnsupdate_task_init);