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