s4-dns: improved logging, and run name check at startup
[sfrench/samba-autobuild/.git] / source4 / dsdb / dns / dns_update.c
1 /*
2    Unix SMB/CIFS mplementation.
3
4    DNS udpate service
5
6    Copyright (C) Andrew Tridgell 2009
7
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.
12
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.
17
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/>.
20
21 */
22
23 /*
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
26
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
29  */
30
31 #include "includes.h"
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
40 struct dnsupdate_service {
41         struct task_server *task;
42         struct auth_session_info *system_session_info;
43         struct ldb_context *samdb;
44
45         /* status for periodic config file update */
46         struct {
47                 uint32_t interval;
48                 struct tevent_timer *te;
49                 struct composite_context *c;
50                 NTSTATUS status;
51         } confupdate;
52
53         /* status for periodic DNS name check */
54         struct {
55                 uint32_t interval;
56                 struct tevent_timer *te;
57                 struct composite_context *c;
58                 NTSTATUS status;
59         } nameupdate;
60 };
61
62 /*
63   called when rndc reload has finished
64  */
65 static void dnsupdate_rndc_done(struct composite_context *c)
66 {
67         struct dnsupdate_service *service = talloc_get_type_abort(c->async.private_data,
68                                                                   struct dnsupdate_service);
69         service->confupdate.status = composite_wait(c);
70         talloc_free(c);
71         service->confupdate.c = NULL;
72         if (!NT_STATUS_IS_OK(service->confupdate.status)) {
73                 DEBUG(0,(__location__ ": Failed rndc update - %s\n",
74                          nt_errstr(service->confupdate.status)));
75         } else {
76                 DEBUG(3,("Completed rndc reload OK\n"));
77         }
78 }
79
80 /*
81   called every 'dnsupdate:conf interval' seconds
82  */
83 static void dnsupdate_rebuild(struct dnsupdate_service *service)
84 {
85         int ret;
86         struct ldb_result *res;
87         const char *tmp_path, *path;
88         int fd, i;
89         const char *attrs[] = { "sAMAccountName", NULL };
90         const char *realm = lp_realm(service->task->lp_ctx);
91         TALLOC_CTX *tmp_ctx = talloc_new(service);
92
93         ret = ldb_search(service->samdb, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
94                          attrs, "(&(primaryGroupID=%u)(objectClass=computer))",
95                          DOMAIN_RID_DCS);
96         if (ret != LDB_SUCCESS) {
97                 DEBUG(0,(__location__ ": Unable to find DCs list - %s", ldb_errstring(service->samdb)));
98                 talloc_free(tmp_ctx);
99                 return;
100         }
101
102         path = lp_parm_string(service->task->lp_ctx, NULL, "dnsupdate", "path");
103         if (path == NULL) {
104                 path = private_path(tmp_ctx, service->task->lp_ctx, "named.conf.update");
105         }
106
107         tmp_path = talloc_asprintf(tmp_ctx, "%s.tmp", path);
108         if (path == NULL || tmp_path == NULL) {
109                 DEBUG(0,(__location__ ": Unable to get paths"));
110                 talloc_free(tmp_ctx);
111                 return;
112         }
113
114         unlink(tmp_path);
115         fd = open(tmp_path, O_CREAT|O_TRUNC|O_WRONLY, 0444);
116         if (fd == -1) {
117                 DEBUG(1,(__location__ ": Unable to open %s - %s\n", tmp_path, strerror(errno)));
118                 talloc_free(tmp_ctx);
119                 return;
120         }
121
122         dprintf(fd, "/* this file is auto-generated - do not edit */\n");
123         dprintf(fd, "update-policy {\n");
124         dprintf(fd, "\tgrant %s ms-self * A AAAA;\n", realm);
125         dprintf(fd, "\tgrant administrator@%s wildcard * A AAAA SRV CNAME TXT;\n", realm);
126
127         for (i=0; i<res->count; i++) {
128                 const char *acctname;
129                 acctname = ldb_msg_find_attr_as_string(res->msgs[i],
130                                                        "sAMAccountName", NULL);
131                 if (!acctname) continue;
132                 dprintf(fd, "\tgrant %s@%s wildcard * A AAAA SRV CNAME;\n",
133                         acctname, realm);
134         }
135         dprintf(fd, "};\n");
136         close(fd);
137
138         if (service->confupdate.c != NULL) {
139                 talloc_free(service->confupdate.c);
140                 service->confupdate.c = NULL;
141         }
142
143         if (NT_STATUS_IS_OK(service->confupdate.status) &&
144             file_compare(tmp_path, path) == true) {
145                 unlink(tmp_path);
146                 talloc_free(tmp_ctx);
147                 return;
148         }
149
150         if (rename(tmp_path, path) != 0) {
151                 DEBUG(0,(__location__ ": Failed to rename %s to %s - %s\n",
152                          tmp_path, path, strerror(errno)));
153                 talloc_free(tmp_ctx);
154                 return;
155         }
156
157         DEBUG(2,("Loading new DNS update grant rules\n"));
158         service->confupdate.c = samba_runcmd(service->task->event_ctx, service,
159                                            timeval_current_ofs(10, 0),
160                                            2, 0,
161                                            lp_rndc_command(service->task->lp_ctx),
162                                            "reload", NULL);
163         service->confupdate.c->async.fn = dnsupdate_rndc_done;
164         service->confupdate.c->async.private_data = service;
165
166         talloc_free(tmp_ctx);
167 }
168
169 static NTSTATUS dnsupdate_confupdate_schedule(struct dnsupdate_service *service);
170
171 /*
172   called every 'dnsupdate:conf interval' seconds
173  */
174 static void dnsupdate_confupdate_handler_te(struct tevent_context *ev, struct tevent_timer *te,
175                                           struct timeval t, void *ptr)
176 {
177         struct dnsupdate_service *service = talloc_get_type(ptr, struct dnsupdate_service);
178
179         dnsupdate_rebuild(service);
180         dnsupdate_confupdate_schedule(service);
181 }
182
183
184 static NTSTATUS dnsupdate_confupdate_schedule(struct dnsupdate_service *service)
185 {
186         service->confupdate.te = tevent_add_timer(service->task->event_ctx, service,
187                                                 timeval_current_ofs(service->confupdate.interval, 0),
188                                                 dnsupdate_confupdate_handler_te, service);
189         NT_STATUS_HAVE_NO_MEMORY(service->confupdate.te);
190         return NT_STATUS_OK;
191 }
192
193
194 /*
195   called when dns update script has finished
196  */
197 static void dnsupdate_nameupdate_done(struct composite_context *c)
198 {
199         struct dnsupdate_service *service = talloc_get_type_abort(c->async.private_data,
200                                                                   struct dnsupdate_service);
201         service->nameupdate.status = composite_wait(c);
202         talloc_free(c);
203         service->nameupdate.c = NULL;
204         if (!NT_STATUS_IS_OK(service->nameupdate.status)) {
205                 DEBUG(0,(__location__ ": Failed DNS update - %s\n",
206                          nt_errstr(service->nameupdate.status)));
207         } else {
208                 DEBUG(3,("Completed DNS update check OK\n"));
209         }
210 }
211
212 /*
213   called every 'dnsupdate:name interval' seconds
214  */
215 static void dnsupdate_check_names(struct dnsupdate_service *service)
216 {
217         /* kill any existing child */
218         if (service->nameupdate.c != NULL) {
219                 talloc_free(service->nameupdate.c);
220                 service->nameupdate.c = NULL;
221         }
222
223         DEBUG(3,("Calling DNS name update script\n"));
224         service->nameupdate.c = samba_runcmd(service->task->event_ctx, service,
225                                              timeval_current_ofs(10, 0),
226                                              2, 0,
227                                              lp_dns_update_command(service->task->lp_ctx),
228                                              NULL);
229         service->nameupdate.c->async.fn = dnsupdate_nameupdate_done;
230         service->nameupdate.c->async.private_data = service;
231 }
232
233 static NTSTATUS dnsupdate_nameupdate_schedule(struct dnsupdate_service *service);
234
235 /*
236   called every 'dnsupdate:name interval' seconds
237  */
238 static void dnsupdate_nameupdate_handler_te(struct tevent_context *ev, struct tevent_timer *te,
239                                             struct timeval t, void *ptr)
240 {
241         struct dnsupdate_service *service = talloc_get_type(ptr, struct dnsupdate_service);
242
243         dnsupdate_check_names(service);
244         dnsupdate_nameupdate_schedule(service);
245 }
246
247
248 static NTSTATUS dnsupdate_nameupdate_schedule(struct dnsupdate_service *service)
249 {
250         service->nameupdate.te = tevent_add_timer(service->task->event_ctx, service,
251                                                   timeval_current_ofs(service->nameupdate.interval, 0),
252                                                   dnsupdate_nameupdate_handler_te, service);
253         NT_STATUS_HAVE_NO_MEMORY(service->nameupdate.te);
254         return NT_STATUS_OK;
255 }
256
257 /*
258   startup the dns update task
259 */
260 static void dnsupdate_task_init(struct task_server *task)
261 {
262         NTSTATUS status;
263         struct dnsupdate_service *service;
264
265         if (lp_server_role(task->lp_ctx) != ROLE_DOMAIN_CONTROLLER) {
266                 /* not useful for non-DC */
267                 return;
268         }
269
270         task_server_set_title(task, "task[dnsupdate]");
271
272         service = talloc_zero(task, struct dnsupdate_service);
273         if (!service) {
274                 task_server_terminate(task, "dnsupdate_task_init: out of memory", true);
275                 return;
276         }
277         service->task           = task;
278         task->private_data      = service;
279
280         service->system_session_info = system_session(service->task->lp_ctx);
281         if (!service->system_session_info) {
282                 task_server_terminate(task,
283                                       "dnsupdate: Failed to obtain server credentials\n",
284                                       true);
285                 return;
286         }
287
288         service->samdb = samdb_connect(service, service->task->event_ctx, task->lp_ctx,
289                                        service->system_session_info);
290         if (!service->samdb) {
291                 task_server_terminate(task, "dnsupdate: Failed to connect to local samdb\n",
292                                       true);
293                 return;
294         }
295
296         service->confupdate.interval    = lp_parm_int(task->lp_ctx, NULL,
297                                                       "dnsupdate", "config interval", 60); /* in seconds */
298
299         service->nameupdate.interval    = lp_parm_int(task->lp_ctx, NULL,
300                                                       "dnsupdate", "name interval", 600); /* in seconds */
301
302         dnsupdate_rebuild(service);
303         status = dnsupdate_confupdate_schedule(service);
304         if (!NT_STATUS_IS_OK(status)) {
305                 task_server_terminate(task, talloc_asprintf(task,
306                                       "dnsupdate: Failed to confupdate schedule: %s\n",
307                                                             nt_errstr(status)), true);
308                 return;
309         }
310
311         dnsupdate_check_names(service);
312         status = dnsupdate_nameupdate_schedule(service);
313         if (!NT_STATUS_IS_OK(status)) {
314                 task_server_terminate(task, talloc_asprintf(task,
315                                       "dnsupdate: Failed to nameupdate schedule: %s\n",
316                                                             nt_errstr(status)), true);
317                 return;
318         }
319
320         irpc_add_name(task->msg_ctx, "dnsupdate");
321
322         /* create the intial file */
323         dnsupdate_rebuild(service);
324
325 }
326
327 /*
328   register ourselves as a available server
329 */
330 NTSTATUS server_service_dnsupdate_init(void)
331 {
332         return register_server_service("dnsupdate", dnsupdate_task_init);
333 }