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