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"
39 struct dreplsrv_op_notify_state {
40 struct composite_context *creq;
42 struct dreplsrv_out_connection *conn;
44 struct dreplsrv_drsuapi_connection *drsuapi;
46 struct drsuapi_DsBindInfoCtr bind_info_ctr;
47 struct drsuapi_DsBind bind_r;
48 struct dreplsrv_notify_operation *op;
52 receive a DsReplicaSync reply
54 static void dreplsrv_op_notify_replica_sync_recv(struct rpc_request *req)
56 struct dreplsrv_op_notify_state *st = talloc_get_type(req->async.private_data,
57 struct dreplsrv_op_notify_state);
58 struct composite_context *c = st->creq;
59 struct drsuapi_DsReplicaSync *r = talloc_get_type(req->ndr.struct_ptr,
60 struct drsuapi_DsReplicaSync);
62 c->status = dcerpc_ndr_request_recv(req);
63 if (!composite_is_ok(c)) return;
65 if (!W_ERROR_IS_OK(r->out.result)) {
66 composite_error(c, werror_to_ntstatus(r->out.result));
76 static void dreplsrv_op_notify_replica_sync_send(struct dreplsrv_op_notify_state *st)
78 struct composite_context *c = st->creq;
79 struct dreplsrv_partition *partition = st->op->source_dsa->partition;
80 struct dreplsrv_drsuapi_connection *drsuapi = st->op->source_dsa->conn->drsuapi;
81 struct rpc_request *req;
82 struct drsuapi_DsReplicaSync *r;
84 r = talloc_zero(st, struct drsuapi_DsReplicaSync);
85 if (composite_nomem(r, c)) return;
87 r->in.bind_handle = &drsuapi->bind_handle;
89 r->in.req.req1.naming_context = &partition->nc;
90 r->in.req.req1.source_dsa_guid = st->op->service->ntds_guid;
91 r->in.req.req1.options =
92 DRSUAPI_DS_REPLICA_SYNC_ASYNCHRONOUS_OPERATION |
93 DRSUAPI_DS_REPLICA_SYNC_WRITEABLE |
94 DRSUAPI_DS_REPLICA_SYNC_ALL_SOURCES;
97 req = dcerpc_drsuapi_DsReplicaSync_send(drsuapi->pipe, r, r);
98 composite_continue_rpc(c, req, dreplsrv_op_notify_replica_sync_recv, st);
102 called when we have an established connection
104 static void dreplsrv_op_notify_connect_recv(struct composite_context *creq)
106 struct dreplsrv_op_notify_state *st = talloc_get_type(creq->async.private_data,
107 struct dreplsrv_op_notify_state);
108 struct composite_context *c = st->creq;
110 c->status = dreplsrv_out_drsuapi_recv(creq);
111 if (!composite_is_ok(c)) return;
113 dreplsrv_op_notify_replica_sync_send(st);
117 start the ReplicaSync async call
119 static struct composite_context *dreplsrv_op_notify_send(struct dreplsrv_notify_operation *op)
121 struct composite_context *c;
122 struct composite_context *creq;
123 struct dreplsrv_op_notify_state *st;
125 c = composite_create(op, op->service->task->event_ctx);
126 if (c == NULL) return NULL;
128 st = talloc_zero(c, struct dreplsrv_op_notify_state);
129 if (composite_nomem(st, c)) return c;
134 creq = dreplsrv_out_drsuapi_send(op->source_dsa->conn);
135 composite_continue(c, creq, dreplsrv_op_notify_connect_recv, st);
140 static void dreplsrv_notify_del_repsTo(struct dreplsrv_notify_operation *op)
143 struct repsFromToBlob *reps;
145 struct dreplsrv_service *s = op->service;
148 werr = dsdb_loadreps(s->samdb, op, op->source_dsa->partition->dn, "repsTo", &reps, &count);
149 if (!W_ERROR_IS_OK(werr)) {
150 DEBUG(0,(__location__ ": Failed to load repsTo for %s\n",
151 ldb_dn_get_linearized(op->source_dsa->partition->dn)));
155 for (i=0; i<count; i++) {
156 if (GUID_compare(&reps[i].ctr.ctr1.source_dsa_obj_guid,
157 &op->source_dsa->repsFrom1->source_dsa_obj_guid) == 0) {
158 memmove(&reps[i], &reps[i+1],
159 sizeof(reps[i])*(count-(i+1)));
164 werr = dsdb_savereps(s->samdb, op, op->source_dsa->partition->dn, "repsTo", reps, count);
165 if (!W_ERROR_IS_OK(werr)) {
166 DEBUG(0,(__location__ ": Failed to save repsTo for %s\n",
167 ldb_dn_get_linearized(op->source_dsa->partition->dn)));
173 called when a notify operation has completed
175 static void dreplsrv_notify_op_callback(struct dreplsrv_notify_operation *op)
178 struct dreplsrv_service *s = op->service;
180 status = composite_wait(op->creq);
181 if (!NT_STATUS_IS_OK(status)) {
182 DEBUG(0,("dreplsrv_notify: Failed to send DsReplicaSync to %s for %s - %s\n",
183 op->source_dsa->repsFrom1->other_info->dns_name,
184 ldb_dn_get_linearized(op->source_dsa->partition->dn),
187 DEBUG(2,("dreplsrv_notify: DsReplicaSync OK for %s\n",
188 op->source_dsa->repsFrom1->other_info->dns_name));
189 op->source_dsa->notify_uSN = op->uSN;
190 /* delete the repsTo for this replication partner in the
191 partition, as we have successfully told him to sync */
192 dreplsrv_notify_del_repsTo(op);
194 talloc_free(op->creq);
197 s->ops.n_current = NULL;
198 dreplsrv_notify_run_ops(s);
202 static void dreplsrv_notify_op_callback_creq(struct composite_context *creq)
204 struct dreplsrv_notify_operation *op = talloc_get_type(creq->async.private_data,
205 struct dreplsrv_notify_operation);
206 dreplsrv_notify_op_callback(op);
210 run any pending replica sync calls
212 void dreplsrv_notify_run_ops(struct dreplsrv_service *s)
214 struct dreplsrv_notify_operation *op;
216 if (s->ops.n_current || s->ops.current) {
217 /* if there's still one running, we're done */
221 if (!s->ops.notifies) {
222 /* if there're no pending operations, we're done */
226 op = s->ops.notifies;
227 s->ops.n_current = op;
228 DLIST_REMOVE(s->ops.notifies, op);
230 op->creq = dreplsrv_op_notify_send(op);
232 dreplsrv_notify_op_callback(op);
236 op->creq->async.fn = dreplsrv_notify_op_callback_creq;
237 op->creq->async.private_data = op;
242 find a source_dsa for a given guid
244 static struct dreplsrv_partition_source_dsa *dreplsrv_find_source_dsa(struct dreplsrv_partition *p,
247 struct dreplsrv_partition_source_dsa *s;
249 for (s=p->sources; s; s=s->next) {
250 if (GUID_compare(&s->repsFrom1->source_dsa_obj_guid, guid) == 0) {
259 schedule a replicaSync message
261 static WERROR dreplsrv_schedule_notify_sync(struct dreplsrv_service *service,
262 struct dreplsrv_partition *p,
263 struct repsFromToBlob *reps,
267 struct dreplsrv_notify_operation *op;
268 struct dreplsrv_partition_source_dsa *s;
270 s = dreplsrv_find_source_dsa(p, &reps->ctr.ctr1.source_dsa_obj_guid);
272 DEBUG(0,(__location__ ": Unable to find source_dsa for %s\n",
273 GUID_string(mem_ctx, &reps->ctr.ctr1.source_dsa_obj_guid)));
274 return WERR_DS_UNAVAILABLE;
277 op = talloc_zero(mem_ctx, struct dreplsrv_notify_operation);
278 W_ERROR_HAVE_NO_MEMORY(op);
280 op->service = service;
284 DLIST_ADD_END(service->ops.notifies, op, struct dreplsrv_notify_operation *);
285 talloc_steal(service, op);
290 see if a partition has a hugher uSN than what is in the repsTo and
291 if so then send a DsReplicaSync
293 static WERROR dreplsrv_notify_check(struct dreplsrv_service *s,
294 struct dreplsrv_partition *p,
298 struct repsFromToBlob *reps;
303 werr = dsdb_loadreps(s->samdb, mem_ctx, p->dn, "repsTo", &reps, &count);
305 werr = dsdb_loadreps(s->samdb, mem_ctx, p->dn, "repsFrom", &reps, &count);
307 if (!W_ERROR_IS_OK(werr)) {
308 DEBUG(0,(__location__ ": Failed to load repsTo for %s\n",
309 ldb_dn_get_linearized(p->dn)));
313 /* loads the partition uSNHighest */
314 ret = dsdb_load_partition_usn(s->samdb, p->dn, &uSN);
315 if (ret != LDB_SUCCESS || uSN == 0) {
320 /* see if any of our partners need some of our objects */
321 for (i=0; i<count; i++) {
322 struct dreplsrv_partition_source_dsa *sdsa;
323 sdsa = dreplsrv_find_source_dsa(p, &reps[i].ctr.ctr1.source_dsa_obj_guid);
324 if (sdsa == NULL) continue;
325 if (sdsa->notify_uSN < uSN) {
326 /* we need to tell this partner to replicate
328 werr = dreplsrv_schedule_notify_sync(s, p, &reps[i], mem_ctx, uSN);
329 if (!W_ERROR_IS_OK(werr)) {
330 DEBUG(0,(__location__ ": Failed to setup notify to %s for %s\n",
331 reps[i].ctr.ctr1.other_info->dns_name,
332 ldb_dn_get_linearized(p->dn)));
342 see if any of the partitions have changed, and if so then send a
343 DsReplicaSync to all the replica partners in the repsTo object
345 static WERROR dreplsrv_notify_check_all(struct dreplsrv_service *s, TALLOC_CTX *mem_ctx)
348 struct dreplsrv_partition *p;
350 for (p = s->partitions; p; p = p->next) {
351 status = dreplsrv_notify_check(s, p, mem_ctx);
352 W_ERROR_NOT_OK_RETURN(status);
358 static void dreplsrv_notify_run(struct dreplsrv_service *service);
360 static void dreplsrv_notify_handler_te(struct tevent_context *ev, struct tevent_timer *te,
361 struct timeval t, void *ptr)
363 struct dreplsrv_service *service = talloc_get_type(ptr, struct dreplsrv_service);
366 service->notify.te = NULL;
368 dreplsrv_notify_run(service);
370 status = dreplsrv_notify_schedule(service, service->notify.interval);
371 if (!W_ERROR_IS_OK(status)) {
372 task_server_terminate(service->task, win_errstr(status), false);
377 WERROR dreplsrv_notify_schedule(struct dreplsrv_service *service, uint32_t next_interval)
380 struct tevent_timer *new_te;
381 struct timeval next_time;
383 /* prevent looping */
384 if (next_interval == 0) next_interval = 1;
386 next_time = timeval_current_ofs(next_interval, 50);
388 if (service->notify.te) {
390 * if the timestamp of the new event is higher,
391 * as current next we don't need to reschedule
393 if (timeval_compare(&next_time, &service->notify.next_event) > 0) {
398 /* reset the next scheduled timestamp */
399 service->notify.next_event = next_time;
401 new_te = event_add_timed(service->task->event_ctx, service,
402 service->notify.next_event,
403 dreplsrv_notify_handler_te, service);
404 W_ERROR_HAVE_NO_MEMORY(new_te);
406 tmp_mem = talloc_new(service);
407 DEBUG(2,("dreplsrv_notify_schedule(%u) %sscheduled for: %s\n",
409 (service->notify.te?"re":""),
410 nt_time_string(tmp_mem, timeval_to_nttime(&next_time))));
411 talloc_free(tmp_mem);
413 talloc_free(service->notify.te);
414 service->notify.te = new_te;
419 static void dreplsrv_notify_run(struct dreplsrv_service *service)
423 mem_ctx = talloc_new(service);
424 dreplsrv_notify_check_all(service, mem_ctx);
425 talloc_free(mem_ctx);
427 dreplsrv_run_pending_ops(service);
428 dreplsrv_notify_run_ops(service);