90ada8810e25f3b7d3d251a04f0f598a8c74ac5e
[ira/wip.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/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"
38
39 struct dnsupdate_service {
40         struct task_server *task;
41         struct auth_session_info *system_session_info;
42         struct ldb_context *samdb;
43
44         struct {
45                 uint32_t interval;
46                 struct tevent_timer *te;
47         } periodic;
48 };
49
50 /*
51   called every dnsupdate:interval seconds
52  */
53 static void dnsupdate_rebuild(struct dnsupdate_service *service)
54 {
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;
63
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;
71         }
72
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");
76         }
77
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;
83         }
84
85         fd = open(tmp_path, O_CREAT|O_TRUNC|O_WRONLY, 0444);
86         if (fd == -1) {
87                 DEBUG(1,(__location__ ": Unable to open %s - %s\n", tmp_path, strerror(errno)));
88                 talloc_free(tmp_ctx);
89                 return;
90         }
91
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);
96
97         for (i=0; i<res->count; i++) {
98                 const char *acctname;
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",
103                         acctname, realm);
104         }
105         dprintf(fd, "};\n");
106         close(fd);
107
108         if (file_compare(tmp_path, path) == true) {
109                 talloc_free(tmp_ctx);
110                 return;
111         }
112
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);
117                 return;
118         }
119
120         DEBUG(1,("Loaded new DNS update grant rules\n"));
121
122         rndc_cmd = lp_parm_string(service->task->lp_ctx, NULL, "dnsupdate", "rndc reload command");
123         if (!rndc_cmd) {
124                 rndc_cmd = "/usr/sbin/rndc reload";
125         }
126         ret = system(rndc_cmd);
127         if (ret != 0) {
128                 DEBUG(0,(__location__ ": Failed rndc reload command: '%s' - %d\n",
129                          rndc_cmd, ret));
130         }
131
132         talloc_free(tmp_ctx);
133 }
134
135 static NTSTATUS dnsupdate_periodic_schedule(struct dnsupdate_service *service);
136
137 /*
138   called every dnsupdate:interval seconds
139  */
140 static void dnsupdate_periodic_handler_te(struct tevent_context *ev, struct tevent_timer *te,
141                                           struct timeval t, void *ptr)
142 {
143         struct dnsupdate_service *service = talloc_get_type(ptr, struct dnsupdate_service);
144
145         dnsupdate_rebuild(service);
146         dnsupdate_periodic_schedule(service);
147 }
148
149
150 static NTSTATUS dnsupdate_periodic_schedule(struct dnsupdate_service *service)
151 {
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);
156         return NT_STATUS_OK;
157 }
158
159 /*
160   startup the dns update task
161 */
162 static void dnsupdate_task_init(struct task_server *task)
163 {
164         NTSTATUS status;
165         struct dnsupdate_service *service;
166
167         if (lp_server_role(task->lp_ctx) != ROLE_DOMAIN_CONTROLLER) {
168                 /* not useful for non-DC */
169                 return;
170         }
171
172         task_server_set_title(task, "task[dnsupdate]");
173
174         service = talloc_zero(task, struct dnsupdate_service);
175         if (!service) {
176                 task_server_terminate(task, "dnsupdate_task_init: out of memory", true);
177                 return;
178         }
179         service->task           = task;
180         task->private_data      = service;
181
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",
186                                       true);
187                 return;
188         }
189
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",
194                                       true);
195                 return;
196         }
197
198         service->periodic.interval      = lp_parm_int(task->lp_ctx, NULL,
199                                                       "dnsupdate", "interval", 60); /* in seconds */
200
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);
206                 return;
207         }
208
209         irpc_add_name(task->msg_ctx, "dnsupdate");
210
211         /* create the intial file */
212         dnsupdate_rebuild(service);
213
214 }
215
216 /*
217   register ourselves as a available server
218 */
219 NTSTATUS server_service_dnsupdate_init(void)
220 {
221         return register_server_service("dnsupdate", dnsupdate_task_init);
222 }