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