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