s4-kcc: avoid a false alarm with rodc
[Samba/gebeck_regimport.git] / source4 / dsdb / kcc / kcc_periodic.c
blobe3792300de0c00636f55aa128b30e32c23413ce5
1 /*
2 Unix SMB/CIFS mplementation.
3 KCC service periodic handling
5 Copyright (C) Andrew Tridgell 2009
6 based on repl service code
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.
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.
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/>.
23 #include "includes.h"
24 #include "lib/events/events.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "auth/auth.h"
27 #include "smbd/service.h"
28 #include "lib/messaging/irpc.h"
29 #include "dsdb/kcc/kcc_connection.h"
30 #include "dsdb/kcc/kcc_service.h"
31 #include <ldb_errors.h>
32 #include "../lib/util/dlinklist.h"
33 #include "librpc/gen_ndr/ndr_misc.h"
34 #include "librpc/gen_ndr/ndr_drsuapi.h"
35 #include "librpc/gen_ndr/ndr_drsblobs.h"
36 #include "librpc/gen_ndr/ndr_irpc_c.h"
37 #include "param/param.h"
38 #include "dsdb/common/util.h"
41 * see if two repsFromToBlob blobs are for the same source DSA
43 static bool kccsrv_same_source_dsa(struct repsFromToBlob *r1, struct repsFromToBlob *r2)
45 return GUID_compare(&r1->ctr.ctr1.source_dsa_obj_guid,
46 &r2->ctr.ctr1.source_dsa_obj_guid) == 0;
50 * see if a repsFromToBlob is in a list
52 static bool reps_in_list(struct repsFromToBlob *r, struct repsFromToBlob *reps, uint32_t count)
54 uint32_t i;
55 for (i=0; i<count; i++) {
56 if (kccsrv_same_source_dsa(r, &reps[i])) {
57 return true;
60 return false;
64 make sure we only add repsFrom entries for DCs who are masters for
65 the partition
67 static bool check_MasterNC(struct kccsrv_partition *p, struct repsFromToBlob *r,
68 struct ldb_result *res)
70 struct repsFromTo1 *r1 = &r->ctr.ctr1;
71 struct GUID invocation_id = r1->source_dsa_invocation_id;
72 unsigned int i, j;
74 /* we are expecting only version 1 */
75 SMB_ASSERT(r->version == 1);
77 for (i=0; i<res->count; i++) {
78 struct ldb_message *msg = res->msgs[i];
79 struct ldb_message_element *el;
80 struct ldb_dn *dn;
82 struct GUID id2 = samdb_result_guid(msg, "invocationID");
83 if (GUID_all_zero(&id2) ||
84 !GUID_equal(&invocation_id, &id2)) {
85 continue;
88 el = ldb_msg_find_element(msg, "msDS-hasMasterNCs");
89 if (!el || el->num_values == 0) {
90 el = ldb_msg_find_element(msg, "hasMasterNCs");
91 if (!el || el->num_values == 0) {
92 continue;
95 for (j=0; j<el->num_values; j++) {
96 dn = ldb_dn_from_ldb_val(p, p->service->samdb, &el->values[j]);
97 if (!ldb_dn_validate(dn)) {
98 talloc_free(dn);
99 continue;
101 if (ldb_dn_compare(dn, p->dn) == 0) {
102 talloc_free(dn);
103 DEBUG(5,("%s %s match on %s in %s\n",
104 r1->other_info->dns_name,
105 el->name,
106 ldb_dn_get_linearized(dn),
107 ldb_dn_get_linearized(msg->dn)));
108 return true;
110 talloc_free(dn);
113 return false;
116 struct kccsrv_notify_drepl_server_state {
117 struct dreplsrv_refresh r;
120 static void kccsrv_notify_drepl_server_done(struct tevent_req *subreq);
123 * Force dreplsrv to update its state as topology is changed
125 static void kccsrv_notify_drepl_server(struct kccsrv_service *s,
126 TALLOC_CTX *mem_ctx)
128 struct kccsrv_notify_drepl_server_state *state;
129 struct dcerpc_binding_handle *irpc_handle;
130 struct tevent_req *subreq;
132 state = talloc_zero(s, struct kccsrv_notify_drepl_server_state);
133 if (state == NULL) {
134 return;
137 irpc_handle = irpc_binding_handle_by_name(state, s->task->msg_ctx,
138 "dreplsrv", &ndr_table_irpc);
139 if (irpc_handle == NULL) {
140 /* dreplsrv is not running yet */
141 TALLOC_FREE(state);
142 return;
145 subreq = dcerpc_dreplsrv_refresh_r_send(state, s->task->event_ctx,
146 irpc_handle, &state->r);
147 if (subreq == NULL) {
148 TALLOC_FREE(state);
149 return;
151 tevent_req_set_callback(subreq, kccsrv_notify_drepl_server_done, state);
154 static void kccsrv_notify_drepl_server_done(struct tevent_req *subreq)
156 struct kccsrv_notify_drepl_server_state *state =
157 tevent_req_callback_data(subreq,
158 struct kccsrv_notify_drepl_server_state);
159 NTSTATUS status;
161 status = dcerpc_dreplsrv_refresh_r_recv(subreq, state);
162 TALLOC_FREE(subreq);
164 /* we don't care about errors */
165 TALLOC_FREE(state);
168 uint32_t kccsrv_replica_flags(struct kccsrv_service *s)
170 if (s->am_rodc) {
171 return DRSUAPI_DRS_INIT_SYNC |
172 DRSUAPI_DRS_PER_SYNC |
173 DRSUAPI_DRS_ADD_REF |
174 DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING |
175 DRSUAPI_DRS_GET_ALL_GROUP_MEMBERSHIP |
176 DRSUAPI_DRS_NONGC_RO_REP;
178 return DRSUAPI_DRS_INIT_SYNC |
179 DRSUAPI_DRS_PER_SYNC |
180 DRSUAPI_DRS_ADD_REF |
181 DRSUAPI_DRS_WRIT_REP;
185 * add any missing repsFrom structures to our partitions
187 NTSTATUS kccsrv_add_repsFrom(struct kccsrv_service *s, TALLOC_CTX *mem_ctx,
188 struct repsFromToBlob *reps, uint32_t count,
189 struct ldb_result *res)
191 struct kccsrv_partition *p;
192 bool notify_dreplsrv = false;
193 uint32_t replica_flags = kccsrv_replica_flags(s);
195 /* update the repsFrom on all partitions */
196 for (p=s->partitions; p; p=p->next) {
197 struct repsFromToBlob *our_reps;
198 uint32_t our_count;
199 WERROR werr;
200 uint32_t i, j;
201 bool modified = false;
203 werr = dsdb_loadreps(s->samdb, mem_ctx, p->dn, "repsFrom", &our_reps, &our_count);
204 if (!W_ERROR_IS_OK(werr)) {
205 DEBUG(0,(__location__ ": Failed to load repsFrom from %s - %s\n",
206 ldb_dn_get_linearized(p->dn), ldb_errstring(s->samdb)));
207 return NT_STATUS_INTERNAL_DB_CORRUPTION;
210 /* see if the entry already exists */
211 for (i=0; i<count; i++) {
212 for (j=0; j<our_count; j++) {
213 if (kccsrv_same_source_dsa(&reps[i], &our_reps[j])) {
214 /* we already have this one -
215 check the replica_flags are right */
216 if (replica_flags != our_reps[j].ctr.ctr1.replica_flags) {
217 /* we need to update the old one with
218 * the new flags
220 our_reps[j].ctr.ctr1.replica_flags = replica_flags;
221 modified = true;
223 break;
226 if (j == our_count) {
227 /* we don't have the new one - add it
228 * if it is a master
230 if (res && !check_MasterNC(p, &reps[i], res)) {
231 /* its not a master, we don't
232 want to pull from it */
233 continue;
235 /* we need to add it to our repsFrom */
236 our_reps = talloc_realloc(mem_ctx, our_reps, struct repsFromToBlob, our_count+1);
237 NT_STATUS_HAVE_NO_MEMORY(our_reps);
238 our_reps[our_count] = reps[i];
239 our_reps[our_count].ctr.ctr1.replica_flags = replica_flags;
240 our_count++;
241 modified = true;
242 DEBUG(4,(__location__ ": Added repsFrom for %s\n",
243 reps[i].ctr.ctr1.other_info->dns_name));
247 /* remove any stale ones */
248 for (i=0; i<our_count; i++) {
249 if (!reps_in_list(&our_reps[i], reps, count) ||
250 (res && !check_MasterNC(p, &our_reps[i], res))) {
251 DEBUG(4,(__location__ ": Removed repsFrom for %s\n",
252 our_reps[i].ctr.ctr1.other_info->dns_name));
253 memmove(&our_reps[i], &our_reps[i+1], (our_count-(i+1))*sizeof(our_reps[0]));
254 our_count--;
255 i--;
256 modified = true;
260 if (modified) {
261 werr = dsdb_savereps(s->samdb, mem_ctx, p->dn, "repsFrom", our_reps, our_count);
262 if (!W_ERROR_IS_OK(werr)) {
263 DEBUG(0,(__location__ ": Failed to save repsFrom to %s - %s\n",
264 ldb_dn_get_linearized(p->dn), ldb_errstring(s->samdb)));
265 return NT_STATUS_INTERNAL_DB_CORRUPTION;
267 /* dreplsrv should refresh its state */
268 notify_dreplsrv = true;
271 /* remove stale repsTo entries */
272 modified = false;
273 werr = dsdb_loadreps(s->samdb, mem_ctx, p->dn, "repsTo", &our_reps, &our_count);
274 if (!W_ERROR_IS_OK(werr)) {
275 DEBUG(0,(__location__ ": Failed to load repsTo from %s - %s\n",
276 ldb_dn_get_linearized(p->dn), ldb_errstring(s->samdb)));
277 return NT_STATUS_INTERNAL_DB_CORRUPTION;
280 /* remove any stale ones */
281 for (i=0; i<our_count; i++) {
282 if (!reps_in_list(&our_reps[i], reps, count)) {
283 DEBUG(4,(__location__ ": Removed repsTo for %s\n",
284 our_reps[i].ctr.ctr1.other_info->dns_name));
285 memmove(&our_reps[i], &our_reps[i+1], (our_count-(i+1))*sizeof(our_reps[0]));
286 our_count--;
287 i--;
288 modified = true;
292 if (modified) {
293 werr = dsdb_savereps(s->samdb, mem_ctx, p->dn, "repsTo", our_reps, our_count);
294 if (!W_ERROR_IS_OK(werr)) {
295 DEBUG(0,(__location__ ": Failed to save repsTo to %s - %s\n",
296 ldb_dn_get_linearized(p->dn), ldb_errstring(s->samdb)));
297 return NT_STATUS_INTERNAL_DB_CORRUPTION;
299 /* dreplsrv should refresh its state */
300 notify_dreplsrv = true;
304 /* notify dreplsrv toplogy has changed */
305 if (notify_dreplsrv) {
306 kccsrv_notify_drepl_server(s, mem_ctx);
309 return NT_STATUS_OK;
315 form a unique list of DNs from a search result and a given set of attributes
317 static int kccsrv_dn_list(struct ldb_context *ldb, struct ldb_result *res,
318 TALLOC_CTX *mem_ctx,
319 const char **attrs,
320 struct ldb_dn ***dn_list, int *dn_count)
322 int i;
323 struct ldb_dn **nc_list = NULL;
324 int nc_count = 0;
326 nc_list = talloc_array(mem_ctx, struct ldb_dn *, 0);
327 if (nc_list == NULL) {
328 return LDB_ERR_OPERATIONS_ERROR;
331 /* gather up a list of all NCs in this forest */
332 for (i=0; i<res->count; i++) {
333 struct ldb_message *msg = res->msgs[i];
334 int j;
335 for (j=0; attrs[j]; j++) {
336 struct ldb_message_element *el;
337 int k;
339 el = ldb_msg_find_element(msg, attrs[j]);
340 if (el == NULL) continue;
341 for (k=0; k<el->num_values; k++) {
342 struct ldb_dn *dn;
343 dn = ldb_dn_from_ldb_val(nc_list, ldb, &el->values[k]);
344 if (dn != NULL) {
345 int l;
346 for (l=0; l<nc_count; l++) {
347 if (ldb_dn_compare(nc_list[l], dn) == 0) break;
349 if (l < nc_count) continue;
350 nc_list = talloc_realloc(mem_ctx, nc_list, struct ldb_dn *, nc_count+1);
351 if (nc_list == NULL) {
352 return LDB_ERR_OPERATIONS_ERROR;
354 nc_list[nc_count] = dn;
355 nc_count++;
361 (*dn_list) = nc_list;
362 (*dn_count) = nc_count;
363 return LDB_SUCCESS;
368 look for any additional global catalog partitions that we should be
369 replicating (by looking for msDS-HasDomainNCs), and add them to our
370 hasPartialReplicaNCs NTDS attribute
372 static int kccsrv_gc_update(struct kccsrv_service *s, struct ldb_result *res)
374 int i;
375 struct ldb_dn **nc_list = NULL;
376 int nc_count = 0;
377 struct ldb_dn **our_nc_list = NULL;
378 int our_nc_count = 0;
379 const char *attrs1[] = { "msDS-hasMasterNCs", "hasMasterNCs", "msDS-HasDomainNCs", NULL };
380 const char *attrs2[] = { "msDS-hasMasterNCs", "hasMasterNCs", "msDS-HasDomainNCs", "hasPartialReplicaNCs", NULL };
381 int ret;
382 TALLOC_CTX *tmp_ctx = talloc_new(res);
383 struct ldb_result *res2;
384 struct ldb_message *msg;
386 /* get a complete list of NCs for the forest */
387 ret = kccsrv_dn_list(s->samdb, res, tmp_ctx, attrs1, &nc_list, &nc_count);
388 if (ret != LDB_SUCCESS) {
389 DEBUG(1,("Failed to get NC list for GC update - %s\n", ldb_errstring(s->samdb)));
390 talloc_free(tmp_ctx);
391 return ret;
394 /* get a list of what NCs we are already replicating */
395 ret = dsdb_search_dn(s->samdb, tmp_ctx, &res2, samdb_ntds_settings_dn(s->samdb), attrs2, 0);
396 if (ret != LDB_SUCCESS) {
397 DEBUG(1,("Failed to get our NC list attributes for GC update - %s\n", ldb_errstring(s->samdb)));
398 talloc_free(tmp_ctx);
399 return ret;
402 ret = kccsrv_dn_list(s->samdb, res2, tmp_ctx, attrs2, &our_nc_list, &our_nc_count);
403 if (ret != LDB_SUCCESS) {
404 DEBUG(1,("Failed to get our NC list for GC update - %s\n", ldb_errstring(s->samdb)));
405 talloc_free(tmp_ctx);
406 return ret;
409 msg = ldb_msg_new(tmp_ctx);
410 if (msg == NULL) {
411 talloc_free(tmp_ctx);
412 return LDB_ERR_OPERATIONS_ERROR;
414 msg->dn = res2->msgs[0]->dn;
416 /* see if we are missing any */
417 for (i=0; i<nc_count; i++) {
418 int j;
419 for (j=0; j<our_nc_count; j++) {
420 if (ldb_dn_compare(nc_list[i], our_nc_list[j]) == 0) break;
422 if (j == our_nc_count) {
423 /* its a new one */
424 ret = ldb_msg_add_string(msg, "hasPartialReplicaNCs",
425 ldb_dn_get_extended_linearized(msg, nc_list[i], 1));
426 if (ret != LDB_SUCCESS) {
427 talloc_free(tmp_ctx);
428 return ret;
434 if (msg->num_elements == 0) {
435 /* none to add */
436 talloc_free(tmp_ctx);
437 return LDB_SUCCESS;
440 if (s->am_rodc) {
441 DEBUG(5, ("%d partial replica should be added but we are RODC so we skip\n", msg->num_elements));
442 talloc_free(tmp_ctx);
443 return LDB_SUCCESS;
446 msg->elements[0].flags = LDB_FLAG_MOD_ADD;
448 ret = dsdb_modify(s->samdb, msg, 0);
449 if (ret != LDB_SUCCESS) {
450 DEBUG(0,("Failed to add hasPartialReplicaNCs - %s\n",
451 ldb_errstring(s->samdb)));
454 talloc_free(tmp_ctx);
455 return ret;
460 this is the core of our initial simple KCC
461 We just add a repsFrom entry for all DCs we find that have nTDSDSA
462 objects, except for ourselves
464 NTSTATUS kccsrv_simple_update(struct kccsrv_service *s, TALLOC_CTX *mem_ctx)
466 struct ldb_result *res;
467 unsigned int i;
468 int ret;
469 const char *attrs[] = { "objectGUID", "invocationID", "msDS-hasMasterNCs", "hasMasterNCs", "msDS-HasDomainNCs", NULL };
470 struct repsFromToBlob *reps = NULL;
471 uint32_t count = 0;
472 struct kcc_connection_list *ntds_conn, *dsa_conn;
474 ret = dsdb_search(s->samdb, mem_ctx, &res, s->config_dn, LDB_SCOPE_SUBTREE,
475 attrs, DSDB_SEARCH_SHOW_EXTENDED_DN, "objectClass=nTDSDSA");
476 if (ret != LDB_SUCCESS) {
477 DEBUG(0,(__location__ ": Failed nTDSDSA search - %s\n", ldb_errstring(s->samdb)));
478 return NT_STATUS_INTERNAL_DB_CORRUPTION;
481 if (samdb_is_gc(s->samdb)) {
482 kccsrv_gc_update(s, res);
485 /* get the current list of connections */
486 ntds_conn = kccsrv_find_connections(s, mem_ctx);
488 dsa_conn = talloc_zero(mem_ctx, struct kcc_connection_list);
490 for (i=0; i<res->count; i++) {
491 struct repsFromTo1 *r1;
492 struct GUID ntds_guid, invocation_id;
494 ntds_guid = samdb_result_guid(res->msgs[i], "objectGUID");
495 if (GUID_compare(&ntds_guid, &s->ntds_guid) == 0) {
496 /* don't replicate with ourselves */
497 continue;
500 invocation_id = samdb_result_guid(res->msgs[i], "invocationID");
502 reps = talloc_realloc(mem_ctx, reps, struct repsFromToBlob, count+1);
503 NT_STATUS_HAVE_NO_MEMORY(reps);
505 ZERO_STRUCT(reps[count]);
506 reps[count].version = 1;
507 r1 = &reps[count].ctr.ctr1;
509 r1->other_info = talloc_zero(reps, struct repsFromTo1OtherInfo);
510 r1->other_info->dns_name = samdb_ntds_msdcs_dns_name(s->samdb, reps, &ntds_guid);
511 r1->source_dsa_obj_guid = ntds_guid;
512 r1->source_dsa_invocation_id = invocation_id;
513 r1->replica_flags = kccsrv_replica_flags(s);
514 memset(r1->schedule, 0x11, sizeof(r1->schedule));
516 dsa_conn->servers = talloc_realloc(dsa_conn, dsa_conn->servers,
517 struct kcc_connection,
518 dsa_conn->count + 1);
519 NT_STATUS_HAVE_NO_MEMORY(dsa_conn->servers);
520 dsa_conn->servers[dsa_conn->count].dsa_guid = r1->source_dsa_obj_guid;
521 dsa_conn->count++;
523 count++;
526 kccsrv_apply_connections(s, ntds_conn, dsa_conn);
528 return kccsrv_add_repsFrom(s, mem_ctx, reps, count, res);
532 static void kccsrv_periodic_run(struct kccsrv_service *service);
534 static void kccsrv_periodic_handler_te(struct tevent_context *ev, struct tevent_timer *te,
535 struct timeval t, void *ptr)
537 struct kccsrv_service *service = talloc_get_type(ptr, struct kccsrv_service);
538 WERROR status;
540 service->periodic.te = NULL;
542 kccsrv_periodic_run(service);
544 status = kccsrv_periodic_schedule(service, service->periodic.interval);
545 if (!W_ERROR_IS_OK(status)) {
546 task_server_terminate(service->task, win_errstr(status), true);
547 return;
551 WERROR kccsrv_periodic_schedule(struct kccsrv_service *service, uint32_t next_interval)
553 TALLOC_CTX *tmp_mem;
554 struct tevent_timer *new_te;
555 struct timeval next_time;
557 /* prevent looping */
558 if (next_interval == 0) next_interval = 1;
560 next_time = timeval_current_ofs(next_interval, 50);
562 if (service->periodic.te) {
564 * if the timestamp of the new event is higher,
565 * as current next we don't need to reschedule
567 if (timeval_compare(&next_time, &service->periodic.next_event) > 0) {
568 return WERR_OK;
572 /* reset the next scheduled timestamp */
573 service->periodic.next_event = next_time;
575 new_te = tevent_add_timer(service->task->event_ctx, service,
576 service->periodic.next_event,
577 kccsrv_periodic_handler_te, service);
578 W_ERROR_HAVE_NO_MEMORY(new_te);
580 tmp_mem = talloc_new(service);
581 DEBUG(4,("kccsrv_periodic_schedule(%u) %sscheduled for: %s\n",
582 next_interval,
583 (service->periodic.te?"re":""),
584 nt_time_string(tmp_mem, timeval_to_nttime(&next_time))));
585 talloc_free(tmp_mem);
587 talloc_free(service->periodic.te);
588 service->periodic.te = new_te;
590 return WERR_OK;
593 static void kccsrv_periodic_run(struct kccsrv_service *service)
595 TALLOC_CTX *mem_ctx;
596 NTSTATUS status;
598 DEBUG(4,("kccsrv_periodic_run(): update\n"));
600 mem_ctx = talloc_new(service);
602 if (service->samba_kcc_code)
603 status = kccsrv_samba_kcc(service, mem_ctx);
604 else {
605 status = kccsrv_simple_update(service, mem_ctx);
606 if (!NT_STATUS_IS_OK(status))
607 DEBUG(0,("kccsrv_simple_update failed - %s\n",
608 nt_errstr(status)));
611 status = kccsrv_check_deleted(service, mem_ctx);
612 if (!NT_STATUS_IS_OK(status)) {
613 DEBUG(0,("kccsrv_check_deleted failed - %s\n", nt_errstr(status)));
615 talloc_free(mem_ctx);
618 /* Called when samba_kcc script has finished
620 static void samba_kcc_done(struct tevent_req *subreq)
622 struct kccsrv_service *service =
623 tevent_req_callback_data(subreq, struct kccsrv_service);
624 int rc;
625 int sys_errno;
627 service->periodic.subreq = NULL;
629 rc = samba_runcmd_recv(subreq, &sys_errno);
630 TALLOC_FREE(subreq);
632 if (rc != 0)
633 service->periodic.status =
634 map_nt_error_from_unix_common(sys_errno);
635 else
636 service->periodic.status = NT_STATUS_OK;
638 if (!NT_STATUS_IS_OK(service->periodic.status))
639 DEBUG(0,(__location__ ": Failed samba_kcc - %s\n",
640 nt_errstr(service->periodic.status)));
641 else
642 DEBUG(3,("Completed samba_kcc OK\n"));
645 /* Invocation of the samba_kcc python script for replication
646 * topology generation.
648 NTSTATUS kccsrv_samba_kcc(struct kccsrv_service *service,
649 TALLOC_CTX *ctxp)
651 NTSTATUS status = NT_STATUS_OK;
652 const char * const *samba_kcc_command =
653 lpcfg_samba_kcc_command(service->task->lp_ctx);
655 /* kill any existing child */
656 TALLOC_FREE(service->periodic.subreq);
658 DEBUG(0,("Calling samba_kcc script\n"));
659 service->periodic.subreq = samba_runcmd_send(service,
660 service->task->event_ctx,
661 timeval_current_ofs(40, 0),
662 2, 0, samba_kcc_command, NULL);
664 if (service->periodic.subreq == NULL) {
665 status = NT_STATUS_NO_MEMORY;
666 goto xerror;
668 tevent_req_set_callback(service->periodic.subreq,
669 samba_kcc_done, service);
671 xerror:
672 if (!NT_STATUS_IS_OK(status))
673 DEBUG(0,(__location__ ": failed - %s\n", nt_errstr(status)));
674 return status;