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;
123 if (state->op->service->syncall_workaround) {
124 DEBUG(3,("sending DsReplicaSync with SYNC_ALL workaround\n"));
125 r->in.req->req1.options |= DRSUAPI_DRS_SYNC_ALL;
128 if (state->op->is_urgent) {
129 r->in.req->req1.options |= DRSUAPI_DRS_SYNC_URGENT;
132 state->ndr_struct_ptr = r;
135 NDR_PRINT_IN_DEBUG(drsuapi_DsReplicaSync, r);
138 subreq = dcerpc_drsuapi_DsReplicaSync_r_send(state,
140 drsuapi->drsuapi_handle,
142 if (tevent_req_nomem(subreq, req)) {
145 tevent_req_set_callback(subreq, dreplsrv_op_notify_replica_sync_done, req);
148 static void dreplsrv_op_notify_replica_sync_done(struct tevent_req *subreq)
150 struct tevent_req *req =
151 tevent_req_callback_data(subreq,
153 struct dreplsrv_op_notify_state *state =
155 struct dreplsrv_op_notify_state);
156 struct drsuapi_DsReplicaSync *r = talloc_get_type(state->ndr_struct_ptr,
157 struct drsuapi_DsReplicaSync);
160 state->ndr_struct_ptr = NULL;
162 status = dcerpc_drsuapi_DsReplicaSync_r_recv(subreq, r);
164 if (tevent_req_nterror(req, status)) {
168 if (!W_ERROR_IS_OK(r->out.result)) {
169 status = werror_to_ntstatus(r->out.result);
170 tevent_req_nterror(req, status);
174 tevent_req_done(req);
177 static NTSTATUS dreplsrv_op_notify_recv(struct tevent_req *req)
179 return tevent_req_simple_recv_ntstatus(req);
183 called when a notify operation has completed
185 static void dreplsrv_notify_op_callback(struct tevent_req *subreq)
187 struct dreplsrv_notify_operation *op =
188 tevent_req_callback_data(subreq,
189 struct dreplsrv_notify_operation);
191 struct dreplsrv_service *s = op->service;
193 status = dreplsrv_op_notify_recv(subreq);
195 if (!NT_STATUS_IS_OK(status)) {
197 unsigned int msg_debug_level = 0;
198 werr = ntstatus_to_werror(status);
199 if (W_ERROR_EQUAL(werr, WERR_BADFILE)) {
203 * we should better fix the bug regarding
204 * non-linked attribute handling, instead
205 * of just hiding the failures.
207 * we should also remove the dc from our repsTo
208 * if it failed to often, instead of retrying
214 DEBUG(msg_debug_level,
215 ("dreplsrv_notify: Failed to send DsReplicaSync to %s for %s - %s : %s\n",
216 op->source_dsa->repsFrom1->other_info->dns_name,
217 ldb_dn_get_linearized(op->source_dsa->partition->dn),
218 nt_errstr(status), win_errstr(werr)));
219 if (W_ERROR_EQUAL(werr, WERR_DS_DRA_NO_REPLICA)) {
220 DEBUG(0,("Enabling SYNC_ALL workaround\n"));
221 op->service->syncall_workaround = true;
224 DEBUG(2,("dreplsrv_notify: DsReplicaSync OK for %s\n",
225 op->source_dsa->repsFrom1->other_info->dns_name));
226 op->source_dsa->notify_uSN = op->uSN;
230 s->ops.n_current = NULL;
231 dreplsrv_notify_run_ops(s);
235 run any pending replica sync calls
237 void dreplsrv_notify_run_ops(struct dreplsrv_service *s)
239 struct dreplsrv_notify_operation *op;
240 struct tevent_req *subreq;
242 if (s->ops.n_current || s->ops.current) {
243 /* if there's still one running, we're done */
247 if (!s->ops.notifies) {
248 /* if there're no pending operations, we're done */
252 op = s->ops.notifies;
253 s->ops.n_current = op;
254 DLIST_REMOVE(s->ops.notifies, op);
256 subreq = dreplsrv_op_notify_send(op, s->task->event_ctx, op);
258 DEBUG(0,("dreplsrv_notify_run_ops: dreplsrv_op_notify_send[%s][%s] - no memory\n",
259 op->source_dsa->repsFrom1->other_info->dns_name,
260 ldb_dn_get_linearized(op->source_dsa->partition->dn)));
263 tevent_req_set_callback(subreq, dreplsrv_notify_op_callback, op);
264 DEBUG(4,("started DsReplicaSync for %s to %s\n",
265 ldb_dn_get_linearized(op->source_dsa->partition->dn),
266 op->source_dsa->repsFrom1->other_info->dns_name));
271 find a source_dsa for a given guid
273 static struct dreplsrv_partition_source_dsa *dreplsrv_find_source_dsa(struct dreplsrv_partition *p,
276 struct dreplsrv_partition_source_dsa *s;
278 for (s=p->sources; s; s=s->next) {
279 if (GUID_compare(&s->repsFrom1->source_dsa_obj_guid, guid) == 0) {
288 schedule a replicaSync message
290 static WERROR dreplsrv_schedule_notify_sync(struct dreplsrv_service *service,
291 struct dreplsrv_partition *p,
292 struct repsFromToBlob *reps,
297 struct dreplsrv_notify_operation *op;
298 struct dreplsrv_partition_source_dsa *s;
300 s = dreplsrv_find_source_dsa(p, &reps->ctr.ctr1.source_dsa_obj_guid);
302 DEBUG(0,(__location__ ": Unable to find source_dsa for %s\n",
303 GUID_string(mem_ctx, &reps->ctr.ctr1.source_dsa_obj_guid)));
304 return WERR_DS_UNAVAILABLE;
307 op = talloc_zero(mem_ctx, struct dreplsrv_notify_operation);
308 W_ERROR_HAVE_NO_MEMORY(op);
310 op->service = service;
313 op->is_urgent = is_urgent;
315 DLIST_ADD_END(service->ops.notifies, op, struct dreplsrv_notify_operation *);
316 talloc_steal(service, op);
321 see if a partition has a hugher uSN than what is in the repsTo and
322 if so then send a DsReplicaSync
324 static WERROR dreplsrv_notify_check(struct dreplsrv_service *s,
325 struct dreplsrv_partition *p,
329 struct repsFromToBlob *reps;
336 werr = dsdb_loadreps(s->samdb, mem_ctx, p->dn, "repsTo", &reps, &count);
337 if (!W_ERROR_IS_OK(werr)) {
338 DEBUG(0,(__location__ ": Failed to load repsTo for %s\n",
339 ldb_dn_get_linearized(p->dn)));
343 /* loads the partition uSNHighest and uSNUrgent */
344 ret = dsdb_load_partition_usn(s->samdb, p->dn, &uSNHighest, &uSNUrgent);
345 if (ret != LDB_SUCCESS || uSNHighest == 0) {
350 /* see if any of our partners need some of our objects */
351 for (i=0; i<count; i++) {
352 struct dreplsrv_partition_source_dsa *sdsa;
353 sdsa = dreplsrv_find_source_dsa(p, &reps[i].ctr.ctr1.source_dsa_obj_guid);
354 if (sdsa == NULL) continue;
355 if (sdsa->notify_uSN < uSNHighest) {
356 /* we need to tell this partner to replicate
358 bool is_urgent = sdsa->notify_uSN < uSNUrgent;
360 /* check if urgent replication is needed */
361 werr = dreplsrv_schedule_notify_sync(s, p, &reps[i], mem_ctx,
362 uSNHighest, is_urgent);
363 if (!W_ERROR_IS_OK(werr)) {
364 DEBUG(0,(__location__ ": Failed to setup notify to %s for %s\n",
365 reps[i].ctr.ctr1.other_info->dns_name,
366 ldb_dn_get_linearized(p->dn)));
369 DEBUG(4,("queued DsReplicaSync for %s to %s (urgent=%s) uSN=%llu:%llu\n",
370 ldb_dn_get_linearized(p->dn),
371 reps[i].ctr.ctr1.other_info->dns_name,
372 is_urgent?"true":"false",
373 (unsigned long long)sdsa->notify_uSN,
374 (unsigned long long)uSNHighest));
382 see if any of the partitions have changed, and if so then send a
383 DsReplicaSync to all the replica partners in the repsTo object
385 static WERROR dreplsrv_notify_check_all(struct dreplsrv_service *s, TALLOC_CTX *mem_ctx)
388 struct dreplsrv_partition *p;
390 for (p = s->partitions; p; p = p->next) {
391 status = dreplsrv_notify_check(s, p, mem_ctx);
392 W_ERROR_NOT_OK_RETURN(status);
398 static void dreplsrv_notify_run(struct dreplsrv_service *service);
400 static void dreplsrv_notify_handler_te(struct tevent_context *ev, struct tevent_timer *te,
401 struct timeval t, void *ptr)
403 struct dreplsrv_service *service = talloc_get_type(ptr, struct dreplsrv_service);
406 service->notify.te = NULL;
408 dreplsrv_notify_run(service);
410 status = dreplsrv_notify_schedule(service, service->notify.interval);
411 if (!W_ERROR_IS_OK(status)) {
412 task_server_terminate(service->task, win_errstr(status), false);
417 WERROR dreplsrv_notify_schedule(struct dreplsrv_service *service, uint32_t next_interval)
420 struct tevent_timer *new_te;
421 struct timeval next_time;
423 /* prevent looping */
424 if (next_interval == 0) next_interval = 1;
426 next_time = timeval_current_ofs(next_interval, 50);
428 if (service->notify.te) {
430 * if the timestamp of the new event is higher,
431 * as current next we don't need to reschedule
433 if (timeval_compare(&next_time, &service->notify.next_event) > 0) {
438 /* reset the next scheduled timestamp */
439 service->notify.next_event = next_time;
441 new_te = event_add_timed(service->task->event_ctx, service,
442 service->notify.next_event,
443 dreplsrv_notify_handler_te, service);
444 W_ERROR_HAVE_NO_MEMORY(new_te);
446 tmp_mem = talloc_new(service);
447 DEBUG(4,("dreplsrv_notify_schedule(%u) %sscheduled for: %s\n",
449 (service->notify.te?"re":""),
450 nt_time_string(tmp_mem, timeval_to_nttime(&next_time))));
451 talloc_free(tmp_mem);
453 talloc_free(service->notify.te);
454 service->notify.te = new_te;
459 static void dreplsrv_notify_run(struct dreplsrv_service *service)
463 mem_ctx = talloc_new(service);
464 dreplsrv_notify_check_all(service, mem_ctx);
465 talloc_free(mem_ctx);
467 dreplsrv_run_pending_ops(service);
468 dreplsrv_notify_run_ops(service);