s4-dns: don't leave behind a tmp file
[Samba/nascimento.git] / source4 / dsdb / dns / dns_update.c
blob13f37e3c6b1e6e869a62654e4a85934c0e92dd5b
1 /*
2 Unix SMB/CIFS mplementation.
4 DNS udpate service
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
28 #include "includes.h"
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;
44 struct {
45 uint32_t interval;
46 struct tevent_timer *te;
47 } periodic;
51 called every dnsupdate:interval seconds
53 static void dnsupdate_rebuild(struct dnsupdate_service *service)
55 int ret;
56 struct ldb_result *res;
57 const char *tmp_path, *path;
58 int fd, i;
59 const char *attrs[] = { "sAMAccountName", NULL };
60 const char *realm = lp_realm(service->task->lp_ctx);
61 TALLOC_CTX *tmp_ctx = talloc_new(service);
62 const char *rndc_cmd;
64 ret = ldb_search(service->samdb, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
65 attrs, "(&(primaryGroupID=%u)(objectClass=computer))",
66 DOMAIN_RID_DCS);
67 if (ret != LDB_SUCCESS) {
68 DEBUG(0,(__location__ ": Unable to find DCs list - %s", ldb_errstring(service->samdb)));
69 talloc_free(tmp_ctx);
70 return;
73 path = lp_parm_string(service->task->lp_ctx, NULL, "dnsupdate", "path");
74 if (path == NULL) {
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"));
81 talloc_free(tmp_ctx);
82 return;
85 unlink(tmp_path);
86 fd = open(tmp_path, O_CREAT|O_TRUNC|O_WRONLY, 0444);
87 if (fd == -1) {
88 DEBUG(1,(__location__ ": Unable to open %s - %s\n", tmp_path, strerror(errno)));
89 talloc_free(tmp_ctx);
90 return;
93 dprintf(fd, "/* this file is auto-generated - do not edit */\n");
94 dprintf(fd, "update-policy {\n");
95 dprintf(fd, "\tgrant %s ms-self * A AAAA;\n", realm);
96 dprintf(fd, "\tgrant administrator@%s wildcard * A AAAA SRV CNAME TXT;\n", realm);
98 for (i=0; i<res->count; i++) {
99 const char *acctname;
100 acctname = ldb_msg_find_attr_as_string(res->msgs[i],
101 "sAMAccountName", NULL);
102 if (!acctname) continue;
103 dprintf(fd, "\tgrant %s@%s wildcard * A AAAA SRV CNAME;\n",
104 acctname, realm);
106 dprintf(fd, "};\n");
107 close(fd);
109 if (file_compare(tmp_path, path) == true) {
110 unlink(tmp_path);
111 talloc_free(tmp_ctx);
112 return;
115 if (rename(tmp_path, path) != 0) {
116 DEBUG(0,(__location__ ": Failed to rename %s to %s - %s\n",
117 tmp_path, path, strerror(errno)));
118 talloc_free(tmp_ctx);
119 return;
122 DEBUG(1,("Loaded new DNS update grant rules\n"));
124 rndc_cmd = lp_parm_string(service->task->lp_ctx, NULL, "dnsupdate", "rndc reload command");
125 if (!rndc_cmd) {
126 rndc_cmd = "/usr/sbin/rndc reload";
128 ret = system(rndc_cmd);
129 if (ret != 0) {
130 DEBUG(0,(__location__ ": Failed rndc reload command: '%s' - %d\n",
131 rndc_cmd, ret));
134 talloc_free(tmp_ctx);
137 static NTSTATUS dnsupdate_periodic_schedule(struct dnsupdate_service *service);
140 called every dnsupdate:interval seconds
142 static void dnsupdate_periodic_handler_te(struct tevent_context *ev, struct tevent_timer *te,
143 struct timeval t, void *ptr)
145 struct dnsupdate_service *service = talloc_get_type(ptr, struct dnsupdate_service);
147 dnsupdate_rebuild(service);
148 dnsupdate_periodic_schedule(service);
152 static NTSTATUS dnsupdate_periodic_schedule(struct dnsupdate_service *service)
154 service->periodic.te = tevent_add_timer(service->task->event_ctx, service,
155 timeval_current_ofs(service->periodic.interval, 0),
156 dnsupdate_periodic_handler_te, service);
157 NT_STATUS_HAVE_NO_MEMORY(service->periodic.te);
158 return NT_STATUS_OK;
162 startup the dns update task
164 static void dnsupdate_task_init(struct task_server *task)
166 NTSTATUS status;
167 struct dnsupdate_service *service;
169 if (lp_server_role(task->lp_ctx) != ROLE_DOMAIN_CONTROLLER) {
170 /* not useful for non-DC */
171 return;
174 task_server_set_title(task, "task[dnsupdate]");
176 service = talloc_zero(task, struct dnsupdate_service);
177 if (!service) {
178 task_server_terminate(task, "dnsupdate_task_init: out of memory", true);
179 return;
181 service->task = task;
182 task->private_data = service;
184 service->system_session_info = system_session(service->task->lp_ctx);
185 if (!service->system_session_info) {
186 task_server_terminate(task,
187 "dnsupdate: Failed to obtain server credentials\n",
188 true);
189 return;
192 service->samdb = samdb_connect(service, service->task->event_ctx, task->lp_ctx,
193 service->system_session_info);
194 if (!service->samdb) {
195 task_server_terminate(task, "dnsupdate: Failed to connect to local samdb\n",
196 true);
197 return;
200 service->periodic.interval = lp_parm_int(task->lp_ctx, NULL,
201 "dnsupdate", "interval", 60); /* in seconds */
203 status = dnsupdate_periodic_schedule(service);
204 if (!NT_STATUS_IS_OK(status)) {
205 task_server_terminate(task, talloc_asprintf(task,
206 "dnsupdate: Failed to periodic schedule: %s\n",
207 nt_errstr(status)), true);
208 return;
211 irpc_add_name(task->msg_ctx, "dnsupdate");
213 /* create the intial file */
214 dnsupdate_rebuild(service);
219 register ourselves as a available server
221 NTSTATUS server_service_dnsupdate_init(void)
223 return register_server_service("dnsupdate", dnsupdate_task_init);