s4-dnsupdate: use samba_runcmd() in the dns update task
[abartlet/samba.git/.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
28 #include "includes.h"
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"
36
37 struct dnsupdate_service {
38         struct task_server *task;
39         struct auth_session_info *system_session_info;
40         struct ldb_context *samdb;
41
42         struct {
43                 uint32_t interval;
44                 struct tevent_timer *te;
45                 struct composite_context *c;
46                 NTSTATUS status;
47         } periodic;
48 };
49
50 /*
51   called when rndc reload has finished
52  */
53 static void dnsupdate_rndc_done(struct composite_context *c)
54 {
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)));
61                 return;
62         }
63         talloc_free(c);
64         service->periodic.c = NULL;
65 }
66
67 /*
68   called every dnsupdate:interval seconds
69  */
70 static void dnsupdate_rebuild(struct dnsupdate_service *service)
71 {
72         int ret;
73         struct ldb_result *res;
74         const char *tmp_path, *path;
75         int fd, i;
76         const char *attrs[] = { "sAMAccountName", NULL };
77         const char *realm = lp_realm(service->task->lp_ctx);
78         TALLOC_CTX *tmp_ctx = talloc_new(service);
79
80         ret = ldb_search(service->samdb, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
81                          attrs, "(&(primaryGroupID=%u)(objectClass=computer))",
82                          DOMAIN_RID_DCS);
83         if (ret != LDB_SUCCESS) {
84                 DEBUG(0,(__location__ ": Unable to find DCs list - %s", ldb_errstring(service->samdb)));
85                 talloc_free(tmp_ctx);
86                 return;
87         }
88
89         path = lp_parm_string(service->task->lp_ctx, NULL, "dnsupdate", "path");
90         if (path == NULL) {
91                 path = private_path(tmp_ctx, service->task->lp_ctx, "named.conf.update");
92         }
93
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"));
97                 talloc_free(tmp_ctx);
98                 return;
99         }
100
101         unlink(tmp_path);
102         fd = open(tmp_path, O_CREAT|O_TRUNC|O_WRONLY, 0444);
103         if (fd == -1) {
104                 DEBUG(1,(__location__ ": Unable to open %s - %s\n", tmp_path, strerror(errno)));
105                 talloc_free(tmp_ctx);
106                 return;
107         }
108
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);
113
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",
120                         acctname, realm);
121         }
122         dprintf(fd, "};\n");
123         close(fd);
124
125         if (service->periodic.c != NULL) {
126                 talloc_free(service->periodic.c);
127                 service->periodic.c = NULL;
128         }
129
130         if (NT_STATUS_IS_OK(service->periodic.status) &&
131             file_compare(tmp_path, path) == true) {
132                 unlink(tmp_path);
133                 talloc_free(tmp_ctx);
134                 return;
135         }
136
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);
141                 return;
142         }
143
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),
147                                            2, 0,
148                                            lp_rndc_command(service->task->lp_ctx),
149                                            "reload", NULL);
150         service->periodic.c->async.fn = dnsupdate_rndc_done;
151         service->periodic.c->async.private_data = service;
152
153         talloc_free(tmp_ctx);
154 }
155
156 static NTSTATUS dnsupdate_periodic_schedule(struct dnsupdate_service *service);
157
158 /*
159   called every dnsupdate:interval seconds
160  */
161 static void dnsupdate_periodic_handler_te(struct tevent_context *ev, struct tevent_timer *te,
162                                           struct timeval t, void *ptr)
163 {
164         struct dnsupdate_service *service = talloc_get_type(ptr, struct dnsupdate_service);
165
166         dnsupdate_rebuild(service);
167         dnsupdate_periodic_schedule(service);
168 }
169
170
171 static NTSTATUS dnsupdate_periodic_schedule(struct dnsupdate_service *service)
172 {
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);
177         return NT_STATUS_OK;
178 }
179
180 /*
181   startup the dns update task
182 */
183 static void dnsupdate_task_init(struct task_server *task)
184 {
185         NTSTATUS status;
186         struct dnsupdate_service *service;
187
188         if (lp_server_role(task->lp_ctx) != ROLE_DOMAIN_CONTROLLER) {
189                 /* not useful for non-DC */
190                 return;
191         }
192
193         task_server_set_title(task, "task[dnsupdate]");
194
195         service = talloc_zero(task, struct dnsupdate_service);
196         if (!service) {
197                 task_server_terminate(task, "dnsupdate_task_init: out of memory", true);
198                 return;
199         }
200         service->task           = task;
201         task->private_data      = service;
202
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",
207                                       true);
208                 return;
209         }
210
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",
215                                       true);
216                 return;
217         }
218
219         service->periodic.interval      = lp_parm_int(task->lp_ctx, NULL,
220                                                       "dnsupdate", "interval", 60); /* in seconds */
221
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);
227                 return;
228         }
229
230         irpc_add_name(task->msg_ctx, "dnsupdate");
231
232         /* create the intial file */
233         dnsupdate_rebuild(service);
234
235 }
236
237 /*
238   register ourselves as a available server
239 */
240 NTSTATUS server_service_dnsupdate_init(void)
241 {
242         return register_server_service("dnsupdate", dnsupdate_task_init);
243 }