2 Unix SMB/CIFS mplementation.
4 DSDB replication service periodic notification handling
6 Copyright (C) Andrew Tridgell 2009
7 based on drepl_periodic
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "lib/events/events.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "auth/auth.h"
28 #include "smbd/service.h"
29 #include "lib/messaging/irpc.h"
30 #include "dsdb/repl/drepl_service.h"
31 #include "lib/ldb/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 "libcli/composite/composite.h"
37 #include "../lib/util/tevent_ntstatus.h"
40 struct dreplsrv_op_notify_state {
41 struct tevent_context *ev;
42 struct dreplsrv_notify_operation *op;
46 static void dreplsrv_op_notify_connect_done(struct tevent_req *subreq);
49 start the ReplicaSync async call
51 static struct tevent_req *dreplsrv_op_notify_send(TALLOC_CTX *mem_ctx,
52 struct tevent_context *ev,
53 struct dreplsrv_notify_operation *op)
55 struct tevent_req *req;
56 struct dreplsrv_op_notify_state *state;
57 struct tevent_req *subreq;
59 req = tevent_req_create(mem_ctx, &state,
60 struct dreplsrv_op_notify_state);
67 subreq = dreplsrv_out_drsuapi_send(state,
69 op->source_dsa->conn);
70 if (tevent_req_nomem(subreq, req)) {
71 return tevent_req_post(req, ev);
73 tevent_req_set_callback(subreq, dreplsrv_op_notify_connect_done, req);
78 static void dreplsrv_op_notify_replica_sync_trigger(struct tevent_req *req);
80 static void dreplsrv_op_notify_connect_done(struct tevent_req *subreq)
82 struct tevent_req *req = tevent_req_callback_data(subreq,
86 status = dreplsrv_out_drsuapi_recv(subreq);
88 if (tevent_req_nterror(req, status)) {
92 dreplsrv_op_notify_replica_sync_trigger(req);
95 static void dreplsrv_op_notify_replica_sync_done(struct tevent_req *subreq);
97 static void dreplsrv_op_notify_replica_sync_trigger(struct tevent_req *req)
99 struct dreplsrv_op_notify_state *state =
101 struct dreplsrv_op_notify_state);
102 struct dreplsrv_partition *partition = state->op->source_dsa->partition;
103 struct dreplsrv_drsuapi_connection *drsuapi = state->op->source_dsa->conn->drsuapi;
104 struct drsuapi_DsReplicaSync *r;
105 struct tevent_req *subreq;
107 r = talloc_zero(state, struct drsuapi_DsReplicaSync);
108 if (tevent_req_nomem(r, req)) {
111 r->in.req = talloc_zero(r, union drsuapi_DsReplicaSyncRequest);
112 if (tevent_req_nomem(r, req)) {
115 r->in.bind_handle = &drsuapi->bind_handle;
117 r->in.req->req1.naming_context = &partition->nc;
118 r->in.req->req1.source_dsa_guid = state->op->service->ntds_guid;
119 r->in.req->req1.options =
120 DRSUAPI_DRS_ASYNC_OP |
121 DRSUAPI_DRS_UPDATE_NOTIFICATION |
122 DRSUAPI_DRS_WRIT_REP;
124 if (state->op->is_urgent) {
125 r->in.req->req1.options |= DRSUAPI_DRS_SYNC_URGENT;
128 state->ndr_struct_ptr = r;
131 NDR_PRINT_IN_DEBUG(drsuapi_DsReplicaSync, r);
134 subreq = dcerpc_drsuapi_DsReplicaSync_r_send(state,
136 drsuapi->drsuapi_handle,
138 if (tevent_req_nomem(subreq, req)) {
141 tevent_req_set_callback(subreq, dreplsrv_op_notify_replica_sync_done, req);
144 static void dreplsrv_op_notify_replica_sync_done(struct tevent_req *subreq)
146 struct tevent_req *req =
147 tevent_req_callback_data(subreq,
149 struct dreplsrv_op_notify_state *state =
151 struct dreplsrv_op_notify_state);
152 struct drsuapi_DsReplicaSync *r = talloc_get_type(state->ndr_struct_ptr,
153 struct drsuapi_DsReplicaSync);
156 state->ndr_struct_ptr = NULL;
158 status = dcerpc_drsuapi_DsReplicaSync_r_recv(subreq, r);
160 if (tevent_req_nterror(req, status)) {
164 if (!W_ERROR_IS_OK(r->out.result)) {
165 status = werror_to_ntstatus(r->out.result);
166 tevent_req_nterror(req, status);
170 tevent_req_done(req);
173 static NTSTATUS dreplsrv_op_notify_recv(struct tevent_req *req)
175 return tevent_req_simple_recv_ntstatus(req);
179 called when a notify operation has completed
181 static void dreplsrv_notify_op_callback(struct tevent_req *subreq)
183 struct dreplsrv_notify_operation *op =
184 tevent_req_callback_data(subreq,
185 struct dreplsrv_notify_operation);
187 struct dreplsrv_service *s = op->service;
190 status = dreplsrv_op_notify_recv(subreq);
191 werr = ntstatus_to_werror(status);
193 if (!NT_STATUS_IS_OK(status)) {
194 DEBUG(4,("dreplsrv_notify: Failed to send DsReplicaSync to %s for %s - %s : %s\n",
195 op->source_dsa->repsFrom1->other_info->dns_name,
196 ldb_dn_get_linearized(op->source_dsa->partition->dn),
197 nt_errstr(status), win_errstr(werr)));
198 if (W_ERROR_EQUAL(werr, WERR_DS_DRA_NO_REPLICA)) {
199 DEBUG(0,("Enabling SYNC_ALL workaround\n"));
200 op->service->syncall_workaround = true;
203 DEBUG(2,("dreplsrv_notify: DsReplicaSync OK for %s\n",
204 op->source_dsa->repsFrom1->other_info->dns_name));
205 op->source_dsa->notify_uSN = op->uSN;
208 drepl_reps_update(s, "repsTo", op->source_dsa->partition->dn,
209 &op->source_dsa->repsFrom1->source_dsa_obj_guid,
213 s->ops.n_current = NULL;
214 dreplsrv_run_pending_ops(s);
218 run any pending replica sync calls
220 void dreplsrv_notify_run_ops(struct dreplsrv_service *s)
222 struct dreplsrv_notify_operation *op;
223 struct tevent_req *subreq;
225 if (s->ops.n_current || s->ops.current) {
226 /* if there's still one running, we're done */
230 if (!s->ops.notifies) {
231 /* if there're no pending operations, we're done */
235 op = s->ops.notifies;
236 s->ops.n_current = op;
237 DLIST_REMOVE(s->ops.notifies, op);
239 subreq = dreplsrv_op_notify_send(op, s->task->event_ctx, op);
241 DEBUG(0,("dreplsrv_notify_run_ops: dreplsrv_op_notify_send[%s][%s] - no memory\n",
242 op->source_dsa->repsFrom1->other_info->dns_name,
243 ldb_dn_get_linearized(op->source_dsa->partition->dn)));
246 tevent_req_set_callback(subreq, dreplsrv_notify_op_callback, op);
247 DEBUG(4,("started DsReplicaSync for %s to %s\n",
248 ldb_dn_get_linearized(op->source_dsa->partition->dn),
249 op->source_dsa->repsFrom1->other_info->dns_name));
254 find a source_dsa for a given guid
256 static struct dreplsrv_partition_source_dsa *dreplsrv_find_notify_dsa(struct dreplsrv_partition *p,
259 struct dreplsrv_partition_source_dsa *s;
261 /* first check the sources list */
262 for (s=p->sources; s; s=s->next) {
263 if (GUID_compare(&s->repsFrom1->source_dsa_obj_guid, guid) == 0) {
268 /* then the notifies list */
269 for (s=p->notifies; s; s=s->next) {
270 if (GUID_compare(&s->repsFrom1->source_dsa_obj_guid, guid) == 0) {
279 schedule a replicaSync message
281 static WERROR dreplsrv_schedule_notify_sync(struct dreplsrv_service *service,
282 struct dreplsrv_partition *p,
283 struct repsFromToBlob *reps,
287 uint32_t replica_flags)
289 struct dreplsrv_notify_operation *op;
290 struct dreplsrv_partition_source_dsa *s;
292 s = dreplsrv_find_notify_dsa(p, &reps->ctr.ctr1.source_dsa_obj_guid);
294 DEBUG(0,(__location__ ": Unable to find source_dsa for %s\n",
295 GUID_string(mem_ctx, &reps->ctr.ctr1.source_dsa_obj_guid)));
296 return WERR_DS_UNAVAILABLE;
299 /* first try to find an existing notify operation */
300 for (op = service->ops.notifies; op; op = op->next) {
301 if (op->source_dsa != s) {
305 if (op->is_urgent != is_urgent) {
309 if (op->replica_flags != replica_flags) {
317 /* reuse the notify operation, as it's not yet started */
321 op = talloc_zero(mem_ctx, struct dreplsrv_notify_operation);
322 W_ERROR_HAVE_NO_MEMORY(op);
324 op->service = service;
327 op->is_urgent = is_urgent;
328 op->replica_flags = replica_flags;
329 op->schedule_time = time(NULL);
331 DLIST_ADD_END(service->ops.notifies, op, struct dreplsrv_notify_operation *);
332 talloc_steal(service, op);
337 see if a partition has a hugher uSN than what is in the repsTo and
338 if so then send a DsReplicaSync
340 static WERROR dreplsrv_notify_check(struct dreplsrv_service *s,
341 struct dreplsrv_partition *p,
345 struct repsFromToBlob *reps;
352 werr = dsdb_loadreps(s->samdb, mem_ctx, p->dn, "repsTo", &reps, &count);
353 if (!W_ERROR_IS_OK(werr)) {
354 DEBUG(0,(__location__ ": Failed to load repsTo for %s\n",
355 ldb_dn_get_linearized(p->dn)));
359 /* loads the partition uSNHighest and uSNUrgent */
360 ret = dsdb_load_partition_usn(s->samdb, p->dn, &uSNHighest, &uSNUrgent);
361 if (ret != LDB_SUCCESS || uSNHighest == 0) {
366 /* see if any of our partners need some of our objects */
367 for (i=0; i<count; i++) {
368 struct dreplsrv_partition_source_dsa *sdsa;
369 uint32_t replica_flags;
370 sdsa = dreplsrv_find_notify_dsa(p, &reps[i].ctr.ctr1.source_dsa_obj_guid);
371 replica_flags = reps[i].ctr.ctr1.replica_flags;
372 if (sdsa == NULL) continue;
373 if (sdsa->notify_uSN < uSNHighest) {
374 /* we need to tell this partner to replicate
376 bool is_urgent = sdsa->notify_uSN < uSNUrgent;
378 /* check if urgent replication is needed */
379 werr = dreplsrv_schedule_notify_sync(s, p, &reps[i], mem_ctx,
380 uSNHighest, is_urgent, replica_flags);
381 if (!W_ERROR_IS_OK(werr)) {
382 DEBUG(0,(__location__ ": Failed to setup notify to %s for %s\n",
383 reps[i].ctr.ctr1.other_info->dns_name,
384 ldb_dn_get_linearized(p->dn)));
387 DEBUG(4,("queued DsReplicaSync for %s to %s (urgent=%s) uSN=%llu:%llu\n",
388 ldb_dn_get_linearized(p->dn),
389 reps[i].ctr.ctr1.other_info->dns_name,
390 is_urgent?"true":"false",
391 (unsigned long long)sdsa->notify_uSN,
392 (unsigned long long)uSNHighest));
400 see if any of the partitions have changed, and if so then send a
401 DsReplicaSync to all the replica partners in the repsTo object
403 static WERROR dreplsrv_notify_check_all(struct dreplsrv_service *s, TALLOC_CTX *mem_ctx)
406 struct dreplsrv_partition *p;
408 for (p = s->partitions; p; p = p->next) {
409 status = dreplsrv_notify_check(s, p, mem_ctx);
410 W_ERROR_NOT_OK_RETURN(status);
416 static void dreplsrv_notify_run(struct dreplsrv_service *service);
418 static void dreplsrv_notify_handler_te(struct tevent_context *ev, struct tevent_timer *te,
419 struct timeval t, void *ptr)
421 struct dreplsrv_service *service = talloc_get_type(ptr, struct dreplsrv_service);
424 service->notify.te = NULL;
426 dreplsrv_notify_run(service);
428 status = dreplsrv_notify_schedule(service, service->notify.interval);
429 if (!W_ERROR_IS_OK(status)) {
430 task_server_terminate(service->task, win_errstr(status), false);
435 WERROR dreplsrv_notify_schedule(struct dreplsrv_service *service, uint32_t next_interval)
438 struct tevent_timer *new_te;
439 struct timeval next_time;
441 /* prevent looping */
442 if (next_interval == 0) next_interval = 1;
444 next_time = timeval_current_ofs(next_interval, 50);
446 if (service->notify.te) {
448 * if the timestamp of the new event is higher,
449 * as current next we don't need to reschedule
451 if (timeval_compare(&next_time, &service->notify.next_event) > 0) {
456 /* reset the next scheduled timestamp */
457 service->notify.next_event = next_time;
459 new_te = event_add_timed(service->task->event_ctx, service,
460 service->notify.next_event,
461 dreplsrv_notify_handler_te, service);
462 W_ERROR_HAVE_NO_MEMORY(new_te);
464 tmp_mem = talloc_new(service);
465 DEBUG(4,("dreplsrv_notify_schedule(%u) %sscheduled for: %s\n",
467 (service->notify.te?"re":""),
468 nt_time_string(tmp_mem, timeval_to_nttime(&next_time))));
469 talloc_free(tmp_mem);
471 talloc_free(service->notify.te);
472 service->notify.te = new_te;
477 static void dreplsrv_notify_run(struct dreplsrv_service *service)
481 mem_ctx = talloc_new(service);
482 dreplsrv_notify_check_all(service, mem_ctx);
483 talloc_free(mem_ctx);
485 dreplsrv_run_pending_ops(service);