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