s4-dns: implemented RODC DNS update in dns update task
[samba.git] / source4 / dsdb / dns / dns_update.c
1 /*
2    Unix SMB/CIFS mplementation.
3
4    DNS update 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 #include "libcli/security/dom_sid.h"
40 #include "librpc/gen_ndr/ndr_irpc.h"
41
42 struct dnsupdate_service {
43         struct task_server *task;
44         struct auth_session_info *system_session_info;
45         struct ldb_context *samdb;
46
47         /* status for periodic config file update */
48         struct {
49                 uint32_t interval;
50                 struct tevent_timer *te;
51                 struct tevent_req *subreq;
52                 NTSTATUS status;
53         } confupdate;
54
55         /* status for periodic DNS name check */
56         struct {
57                 uint32_t interval;
58                 struct tevent_timer *te;
59                 struct tevent_req *subreq;
60                 struct tevent_req *spnreq;
61                 NTSTATUS status;
62         } nameupdate;
63 };
64
65 /*
66   called when rndc reload has finished
67  */
68 static void dnsupdate_rndc_done(struct tevent_req *subreq)
69 {
70         struct dnsupdate_service *service = tevent_req_callback_data(subreq,
71                                             struct dnsupdate_service);
72         int ret;
73         int sys_errno;
74
75         service->confupdate.subreq = NULL;
76
77         ret = samba_runcmd_recv(subreq, &sys_errno);
78         TALLOC_FREE(subreq);
79         if (ret != 0) {
80                 service->confupdate.status = map_nt_error_from_unix(sys_errno);
81         } else {
82                 service->confupdate.status = NT_STATUS_OK;
83         }
84
85         if (!NT_STATUS_IS_OK(service->confupdate.status)) {
86                 DEBUG(0,(__location__ ": Failed rndc update - %s\n",
87                          nt_errstr(service->confupdate.status)));
88         } else {
89                 DEBUG(3,("Completed rndc reload OK\n"));
90         }
91 }
92
93 /*
94   called every 'dnsupdate:conf interval' seconds
95  */
96 static void dnsupdate_rebuild(struct dnsupdate_service *service)
97 {
98         int ret;
99         size_t size;
100         struct ldb_result *res;
101         const char *tmp_path, *path, *path_static;
102         char *static_policies;
103         int fd;
104         unsigned int i;
105         const char *attrs[] = { "sAMAccountName", NULL };
106         const char *realm = lpcfg_realm(service->task->lp_ctx);
107         TALLOC_CTX *tmp_ctx = talloc_new(service);
108         const char * const *rndc_command = lpcfg_rndc_command(service->task->lp_ctx);
109
110         /* abort any pending script run */
111         TALLOC_FREE(service->confupdate.subreq);
112
113         ret = ldb_search(service->samdb, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
114                          attrs, "(&(primaryGroupID=%u)(objectClass=computer))",
115                          DOMAIN_RID_DCS);
116         if (ret != LDB_SUCCESS) {
117                 DEBUG(0,(__location__ ": Unable to find DCs list - %s", ldb_errstring(service->samdb)));
118                 talloc_free(tmp_ctx);
119                 return;
120         }
121
122         path = lpcfg_parm_string(service->task->lp_ctx, NULL, "dnsupdate", "path");
123         if (path == NULL) {
124                 path = private_path(tmp_ctx, service->task->lp_ctx, "named.conf.update");
125         }
126
127         path_static = lpcfg_parm_string(service->task->lp_ctx, NULL, "dnsupdate", "extra_static_grant_rules");
128         if (path_static == NULL) {
129                 path_static = private_path(tmp_ctx, service->task->lp_ctx, "named.conf.update.static");
130         }
131
132         tmp_path = talloc_asprintf(tmp_ctx, "%s.tmp", path);
133         if (path == NULL || tmp_path == NULL || path_static == NULL ) {
134                 DEBUG(0,(__location__ ": Unable to get paths\n"));
135                 talloc_free(tmp_ctx);
136                 return;
137         }
138
139         static_policies = file_load(path_static, &size, 0, tmp_ctx);
140
141         unlink(tmp_path);
142         fd = open(tmp_path, O_CREAT|O_TRUNC|O_WRONLY, 0444);
143         if (fd == -1) {
144                 DEBUG(1,(__location__ ": Unable to open %s - %s\n", tmp_path, strerror(errno)));
145                 talloc_free(tmp_ctx);
146                 return;
147         }
148
149         dprintf(fd, "/* this file is auto-generated - do not edit */\n");
150         dprintf(fd, "update-policy {\n");
151         if( static_policies != NULL ) {
152                 dprintf(fd, "/* Start of static entries */\n");
153                 dprintf(fd, "%s\n",static_policies);
154                 dprintf(fd, "/* End of static entries */\n");
155         }
156         dprintf(fd, "\tgrant %s ms-self * A AAAA;\n", realm);
157         dprintf(fd, "\tgrant administrator@%s wildcard * A AAAA SRV CNAME TXT;\n", realm);
158
159         for (i=0; i<res->count; i++) {
160                 const char *acctname;
161                 acctname = ldb_msg_find_attr_as_string(res->msgs[i],
162                                                        "sAMAccountName", NULL);
163                 if (!acctname) continue;
164                 dprintf(fd, "\tgrant %s@%s wildcard * A AAAA SRV CNAME;\n",
165                         acctname, realm);
166         }
167         dprintf(fd, "};\n");
168         close(fd);
169
170
171         if (NT_STATUS_IS_OK(service->confupdate.status) &&
172             file_compare(tmp_path, path) == true) {
173                 unlink(tmp_path);
174                 talloc_free(tmp_ctx);
175                 return;
176         }
177
178         if (rename(tmp_path, path) != 0) {
179                 DEBUG(0,(__location__ ": Failed to rename %s to %s - %s\n",
180                          tmp_path, path, strerror(errno)));
181                 talloc_free(tmp_ctx);
182                 return;
183         }
184
185         DEBUG(2,("Loading new DNS update grant rules\n"));
186         service->confupdate.subreq = samba_runcmd_send(service,
187                                                        service->task->event_ctx,
188                                                        timeval_current_ofs(10, 0),
189                                                        2, 0,
190                                                        rndc_command,
191                                                        "reload", NULL);
192         if (service->confupdate.subreq == NULL) {
193                 DEBUG(0,(__location__ ": samba_runcmd_send() failed with no memory\n"));
194                 talloc_free(tmp_ctx);
195                 return;
196         }
197         tevent_req_set_callback(service->confupdate.subreq,
198                                 dnsupdate_rndc_done,
199                                 service);
200
201         talloc_free(tmp_ctx);
202 }
203
204 static NTSTATUS dnsupdate_confupdate_schedule(struct dnsupdate_service *service);
205
206 /*
207   called every 'dnsupdate:conf interval' seconds
208  */
209 static void dnsupdate_confupdate_handler_te(struct tevent_context *ev, struct tevent_timer *te,
210                                           struct timeval t, void *ptr)
211 {
212         struct dnsupdate_service *service = talloc_get_type(ptr, struct dnsupdate_service);
213
214         dnsupdate_rebuild(service);
215         dnsupdate_confupdate_schedule(service);
216 }
217
218
219 static NTSTATUS dnsupdate_confupdate_schedule(struct dnsupdate_service *service)
220 {
221         service->confupdate.te = tevent_add_timer(service->task->event_ctx, service,
222                                                 timeval_current_ofs(service->confupdate.interval, 0),
223                                                 dnsupdate_confupdate_handler_te, service);
224         NT_STATUS_HAVE_NO_MEMORY(service->confupdate.te);
225         return NT_STATUS_OK;
226 }
227
228
229 /*
230   called when dns update script has finished
231  */
232 static void dnsupdate_nameupdate_done(struct tevent_req *subreq)
233 {
234         struct dnsupdate_service *service = tevent_req_callback_data(subreq,
235                                             struct dnsupdate_service);
236         int ret;
237         int sys_errno;
238
239         service->nameupdate.subreq = NULL;
240
241         ret = samba_runcmd_recv(subreq, &sys_errno);
242         TALLOC_FREE(subreq);
243         if (ret != 0) {
244                 service->nameupdate.status = map_nt_error_from_unix(sys_errno);
245         } else {
246                 service->nameupdate.status = NT_STATUS_OK;
247         }
248
249         if (!NT_STATUS_IS_OK(service->nameupdate.status)) {
250                 DEBUG(0,(__location__ ": Failed DNS update - %s\n",
251                          nt_errstr(service->nameupdate.status)));
252         } else {
253                 DEBUG(3,("Completed DNS update check OK\n"));
254         }
255 }
256
257
258 /*
259   called when spn update script has finished
260  */
261 static void dnsupdate_spnupdate_done(struct tevent_req *subreq)
262 {
263         struct dnsupdate_service *service = tevent_req_callback_data(subreq,
264                                             struct dnsupdate_service);
265         int ret;
266         int sys_errno;
267
268         service->nameupdate.spnreq = NULL;
269
270         ret = samba_runcmd_recv(subreq, &sys_errno);
271         TALLOC_FREE(subreq);
272         if (ret != 0) {
273                 service->nameupdate.status = map_nt_error_from_unix(sys_errno);
274         } else {
275                 service->nameupdate.status = NT_STATUS_OK;
276         }
277
278         if (!NT_STATUS_IS_OK(service->nameupdate.status)) {
279                 DEBUG(0,(__location__ ": Failed SPN update - %s\n",
280                          nt_errstr(service->nameupdate.status)));
281         } else {
282                 DEBUG(3,("Completed SPN update check OK\n"));
283         }
284 }
285
286 /*
287   called every 'dnsupdate:name interval' seconds
288  */
289 static void dnsupdate_check_names(struct dnsupdate_service *service)
290 {
291         const char * const *dns_update_command = lpcfg_dns_update_command(service->task->lp_ctx);
292         const char * const *spn_update_command = lpcfg_spn_update_command(service->task->lp_ctx);
293
294         /* kill any existing child */
295         TALLOC_FREE(service->nameupdate.subreq);
296
297         DEBUG(3,("Calling DNS name update script\n"));
298         service->nameupdate.subreq = samba_runcmd_send(service,
299                                                        service->task->event_ctx,
300                                                        timeval_current_ofs(10, 0),
301                                                        2, 0,
302                                                        dns_update_command,
303                                                        NULL);
304         if (service->nameupdate.subreq == NULL) {
305                 DEBUG(0,(__location__ ": samba_runcmd_send() failed with no memory\n"));
306                 return;
307         }
308         tevent_req_set_callback(service->nameupdate.subreq,
309                                 dnsupdate_nameupdate_done,
310                                 service);
311
312         DEBUG(3,("Calling SPN name update script\n"));
313         service->nameupdate.spnreq = samba_runcmd_send(service,
314                                                        service->task->event_ctx,
315                                                        timeval_current_ofs(10, 0),
316                                                        2, 0,
317                                                        spn_update_command,
318                                                        NULL);
319         if (service->nameupdate.spnreq == NULL) {
320                 DEBUG(0,(__location__ ": samba_runcmd_send() failed with no memory\n"));
321                 return;
322         }
323         tevent_req_set_callback(service->nameupdate.spnreq,
324                                 dnsupdate_spnupdate_done,
325                                 service);
326 }
327
328 static NTSTATUS dnsupdate_nameupdate_schedule(struct dnsupdate_service *service);
329
330 /*
331   called every 'dnsupdate:name interval' seconds
332  */
333 static void dnsupdate_nameupdate_handler_te(struct tevent_context *ev, struct tevent_timer *te,
334                                             struct timeval t, void *ptr)
335 {
336         struct dnsupdate_service *service = talloc_get_type(ptr, struct dnsupdate_service);
337
338         dnsupdate_check_names(service);
339         dnsupdate_nameupdate_schedule(service);
340 }
341
342
343 static NTSTATUS dnsupdate_nameupdate_schedule(struct dnsupdate_service *service)
344 {
345         service->nameupdate.te = tevent_add_timer(service->task->event_ctx, service,
346                                                   timeval_current_ofs(service->nameupdate.interval, 0),
347                                                   dnsupdate_nameupdate_handler_te, service);
348         NT_STATUS_HAVE_NO_MEMORY(service->nameupdate.te);
349         return NT_STATUS_OK;
350 }
351
352
353 struct dnsupdate_RODC_state {
354         struct irpc_message *msg;
355         struct dnsupdate_RODC *r;
356         char *tmp_path;
357         int fd;
358 };
359
360 static int dnsupdate_RODC_destructor(struct dnsupdate_RODC_state *st)
361 {
362         if (st->fd != -1) {
363                 close(st->fd);
364         }
365         unlink(st->tmp_path);
366         return 0;
367 }
368
369 /*
370   called when the DNS update has completed
371  */
372 static void dnsupdate_RODC_callback(struct tevent_req *req)
373 {
374         struct dnsupdate_RODC_state *st =
375                 tevent_req_callback_data(req,
376                                          struct dnsupdate_RODC_state);
377         int sys_errno;
378         int i, ret;
379
380         ret = samba_runcmd_recv(req, &sys_errno);
381         talloc_free(req);
382         if (ret != 0) {
383                 st->r->out.result = map_nt_error_from_unix(sys_errno);
384                 DEBUG(2,(__location__ ": RODC DNS Update failed: %s\n", nt_errstr(st->r->out.result)));
385         } else {
386                 st->r->out.result = NT_STATUS_OK;
387                 DEBUG(3,(__location__ ": RODC DNS Update OK\n"));
388         }
389
390         for (i=0; i<st->r->in.dns_names->count; i++) {
391                 st->r->out.dns_names->names[i].status = NT_STATUS_V(st->r->out.result);
392         }
393
394         irpc_send_reply(st->msg, NT_STATUS_OK);
395 }
396
397
398 /**
399  * Called when we get a RODC DNS update request from the netlogon
400  * rpc server
401  */
402 static NTSTATUS dnsupdate_dnsupdate_RODC(struct irpc_message *msg,
403                                          struct dnsupdate_RODC *r)
404 {
405         struct dnsupdate_service *s = talloc_get_type(msg->private_data,
406                                                       struct dnsupdate_service);
407         const char * const *dns_update_command = lpcfg_dns_update_command(s->task->lp_ctx);
408         struct dnsupdate_RODC_state *st;
409         struct tevent_req *req;
410         int i, ret;
411         struct GUID ntds_guid;
412         const char *site, *dnsdomain, *dnsforest, *ntdsguid, *hostname;
413         struct ldb_dn *sid_dn;
414         const char *attrs[] = { "dNSHostName", NULL };
415         struct ldb_result *res;
416
417         st = talloc_zero(msg, struct dnsupdate_RODC_state);
418         if (!st) {
419                 r->out.result = NT_STATUS_NO_MEMORY;
420                 return NT_STATUS_OK;
421         }
422
423         st->r = r;
424         st->msg = msg;
425
426         st->tmp_path = smbd_tmp_path(st, s->task->lp_ctx, "rodcdns.XXXXXX");
427         if (!st->tmp_path) {
428                 talloc_free(st);
429                 r->out.result = NT_STATUS_NO_MEMORY;
430                 return NT_STATUS_OK;
431         }
432
433         st->fd = mkstemp(st->tmp_path);
434         if (st->fd == -1) {
435                 DEBUG(0,("Unable to create a temporary file for RODC dnsupdate\n"));
436                 talloc_free(st);
437                 r->out.result = NT_STATUS_INTERNAL_DB_CORRUPTION;
438                 return NT_STATUS_OK;
439         }
440
441         talloc_set_destructor(st, dnsupdate_RODC_destructor);
442
443         sid_dn = ldb_dn_new_fmt(st, s->samdb, "<SID=%s>", dom_sid_string(st, r->in.dom_sid));
444         if (!sid_dn) {
445                 talloc_free(st);
446                 r->out.result = NT_STATUS_NO_MEMORY;
447                 return NT_STATUS_OK;
448         }
449
450         /* work out the site */
451         ret = samdb_find_site_for_computer(s->samdb, st, sid_dn, &site);
452         if (ret != LDB_SUCCESS) {
453                 DEBUG(2, (__location__ ": Unable to find site for computer %s\n",
454                           ldb_dn_get_linearized(sid_dn)));
455                 talloc_free(st);
456                 r->out.result = NT_STATUS_NO_SUCH_USER;
457                 return NT_STATUS_OK;
458         }
459
460         /* work out the ntdsguid */
461         ret = samdb_find_ntdsguid_for_computer(s->samdb, sid_dn, &ntds_guid);
462         ntdsguid = GUID_string(st, &ntds_guid);
463         if (ret != LDB_SUCCESS || !ntdsguid) {
464                 DEBUG(2, (__location__ ": Unable to find NTDS GUID for computer %s\n",
465                           ldb_dn_get_linearized(sid_dn)));
466                 talloc_free(st);
467                 r->out.result = NT_STATUS_NO_SUCH_USER;
468                 return NT_STATUS_OK;
469         }
470
471
472         /* find dnsdomain and dnsforest */
473         dnsdomain = lpcfg_realm(s->task->lp_ctx);
474         dnsforest = dnsdomain;
475
476         /* find the hostname */
477         ret = dsdb_search_dn(s->samdb, st, &res, sid_dn, attrs, 0);
478         if (ret == LDB_SUCCESS) {
479                 hostname = ldb_msg_find_attr_as_string(res->msgs[0], "dNSHostName", NULL);
480         }
481         if (ret != LDB_SUCCESS || !hostname) {
482                 DEBUG(2, (__location__ ": Unable to find NTDS GUID for computer %s\n",
483                           ldb_dn_get_linearized(sid_dn)));
484                 talloc_free(st);
485                 r->out.result = NT_STATUS_NO_SUCH_USER;
486                 return NT_STATUS_OK;
487         }
488
489
490         for (i=0; i<st->r->in.dns_names->count; i++) {
491                 struct NL_DNS_NAME_INFO *n = &r->in.dns_names->names[i];
492                 switch (n->type) {
493                 case NlDnsLdapAtSite:
494                         dprintf(st->fd, "SRV _ldap._tcp.%s._sites.%s. %s %u\n",
495                                 site, dnsdomain, hostname, n->port);
496                         break;
497                 case NlDnsGcAtSite:
498                         dprintf(st->fd, "SRV _ldap._tcp.%s._sites.gc._msdcs.%s. %s %u\n",
499                                 site, dnsdomain, hostname, n->port);
500                         break;
501                 case NlDnsDsaCname:
502                         dprintf(st->fd, "CNAME %s._msdcs.%s. %s\n",
503                                 ntdsguid, dnsforest, hostname);
504                         break;
505                 case NlDnsKdcAtSite:
506                         dprintf(st->fd, "SRV _kerberos._tcp.%s._sites.dc._msdcs.%s. %s %u\n",
507                                 site, dnsdomain, hostname, n->port);
508                         break;
509                 case NlDnsDcAtSite:
510                         dprintf(st->fd, "SRV _ldap._tcp.%s._sites.dc._msdcs.%s. %s %u\n",
511                                 site, dnsdomain, hostname, n->port);
512                         break;
513                 case NlDnsRfc1510KdcAtSite:
514                         dprintf(st->fd, "SRV _kerberos._tcp.%s._sites.%s. %s %u\n",
515                                 site, dnsdomain, hostname, n->port);
516                         break;
517                 case NlDnsGenericGcAtSite:
518                         dprintf(st->fd, "SRV _gc._tcp.%s._sites.%s. %s %u\n",
519                                 site, dnsforest, hostname, n->port);
520                         break;
521                 }
522         }
523
524         close(st->fd);
525         st->fd = -1;
526
527         DEBUG(3,("Calling RODC DNS name update script %s\n", st->tmp_path));
528         req = samba_runcmd_send(st,
529                                 s->task->event_ctx,
530                                 timeval_current_ofs(20, 0),
531                                 2, 0,
532                                 dns_update_command,
533                                 "--update-list",
534                                 st->tmp_path,
535                                 NULL);
536         NT_STATUS_HAVE_NO_MEMORY(req);
537
538         /* setup the callback */
539         tevent_req_set_callback(req, dnsupdate_RODC_callback, st);
540
541         msg->defer_reply = true;
542
543         return NT_STATUS_OK;
544 }
545
546 /*
547   startup the dns update task
548 */
549 static void dnsupdate_task_init(struct task_server *task)
550 {
551         NTSTATUS status;
552         struct dnsupdate_service *service;
553
554         if (lpcfg_server_role(task->lp_ctx) != ROLE_DOMAIN_CONTROLLER) {
555                 /* not useful for non-DC */
556                 return;
557         }
558
559         task_server_set_title(task, "task[dnsupdate]");
560
561         service = talloc_zero(task, struct dnsupdate_service);
562         if (!service) {
563                 task_server_terminate(task, "dnsupdate_task_init: out of memory", true);
564                 return;
565         }
566         service->task           = task;
567         task->private_data      = service;
568
569         service->system_session_info = system_session(service->task->lp_ctx);
570         if (!service->system_session_info) {
571                 task_server_terminate(task,
572                                       "dnsupdate: Failed to obtain server credentials\n",
573                                       true);
574                 return;
575         }
576
577         service->samdb = samdb_connect(service, service->task->event_ctx, task->lp_ctx,
578                                        service->system_session_info);
579         if (!service->samdb) {
580                 task_server_terminate(task, "dnsupdate: Failed to connect to local samdb\n",
581                                       true);
582                 return;
583         }
584
585         service->confupdate.interval    = lpcfg_parm_int(task->lp_ctx, NULL,
586                                                       "dnsupdate", "config interval", 60); /* in seconds */
587
588         service->nameupdate.interval    = lpcfg_parm_int(task->lp_ctx, NULL,
589                                                       "dnsupdate", "name interval", 600); /* in seconds */
590
591         dnsupdate_rebuild(service);
592         status = dnsupdate_confupdate_schedule(service);
593         if (!NT_STATUS_IS_OK(status)) {
594                 task_server_terminate(task, talloc_asprintf(task,
595                                       "dnsupdate: Failed to confupdate schedule: %s\n",
596                                                             nt_errstr(status)), true);
597                 return;
598         }
599
600         dnsupdate_check_names(service);
601         status = dnsupdate_nameupdate_schedule(service);
602         if (!NT_STATUS_IS_OK(status)) {
603                 task_server_terminate(task, talloc_asprintf(task,
604                                       "dnsupdate: Failed to nameupdate schedule: %s\n",
605                                                             nt_errstr(status)), true);
606                 return;
607         }
608
609         irpc_add_name(task->msg_ctx, "dnsupdate");
610
611         IRPC_REGISTER(task->msg_ctx, irpc, DNSUPDATE_RODC,
612                       dnsupdate_dnsupdate_RODC, service);
613
614         /* create the intial file */
615         dnsupdate_rebuild(service);
616
617 }
618
619 /*
620   register ourselves as a available server
621 */
622 NTSTATUS server_service_dnsupdate_init(void)
623 {
624         return register_server_service("dnsupdate", dnsupdate_task_init);
625 }