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 "dsdb/repl/drepl_service.h"
30 #include <ldb_errors.h>
31 #include "../lib/util/dlinklist.h"
32 #include "librpc/gen_ndr/ndr_misc.h"
33 #include "librpc/gen_ndr/ndr_drsuapi.h"
34 #include "librpc/gen_ndr/ndr_drsblobs.h"
35 #include "libcli/composite/composite.h"
36 #include "../lib/util/tevent_ntstatus.h"
39 struct dreplsrv_op_notify_state
{
40 struct tevent_context
*ev
;
41 struct dreplsrv_notify_operation
*op
;
45 static void dreplsrv_op_notify_connect_done(struct tevent_req
*subreq
);
48 start the ReplicaSync async call
50 static struct tevent_req
*dreplsrv_op_notify_send(TALLOC_CTX
*mem_ctx
,
51 struct tevent_context
*ev
,
52 struct dreplsrv_notify_operation
*op
)
54 struct tevent_req
*req
;
55 struct dreplsrv_op_notify_state
*state
;
56 struct tevent_req
*subreq
;
58 req
= tevent_req_create(mem_ctx
, &state
,
59 struct dreplsrv_op_notify_state
);
66 subreq
= dreplsrv_out_drsuapi_send(state
,
68 op
->source_dsa
->conn
);
69 if (tevent_req_nomem(subreq
, req
)) {
70 return tevent_req_post(req
, ev
);
72 tevent_req_set_callback(subreq
, dreplsrv_op_notify_connect_done
, req
);
77 static void dreplsrv_op_notify_replica_sync_trigger(struct tevent_req
*req
);
79 static void dreplsrv_op_notify_connect_done(struct tevent_req
*subreq
)
81 struct tevent_req
*req
= tevent_req_callback_data(subreq
,
85 status
= dreplsrv_out_drsuapi_recv(subreq
);
87 if (tevent_req_nterror(req
, status
)) {
91 dreplsrv_op_notify_replica_sync_trigger(req
);
94 static void dreplsrv_op_notify_replica_sync_done(struct tevent_req
*subreq
);
96 static void dreplsrv_op_notify_replica_sync_trigger(struct tevent_req
*req
)
98 struct dreplsrv_op_notify_state
*state
=
100 struct dreplsrv_op_notify_state
);
101 struct dreplsrv_partition
*partition
= state
->op
->source_dsa
->partition
;
102 struct dreplsrv_drsuapi_connection
*drsuapi
= state
->op
->source_dsa
->conn
->drsuapi
;
103 struct drsuapi_DsReplicaSync
*r
;
104 struct tevent_req
*subreq
;
106 r
= talloc_zero(state
, struct drsuapi_DsReplicaSync
);
107 if (tevent_req_nomem(r
, req
)) {
110 r
->in
.req
= talloc_zero(r
, union drsuapi_DsReplicaSyncRequest
);
111 if (tevent_req_nomem(r
, req
)) {
114 r
->in
.bind_handle
= &drsuapi
->bind_handle
;
116 r
->in
.req
->req1
.naming_context
= &partition
->nc
;
117 r
->in
.req
->req1
.source_dsa_guid
= state
->op
->service
->ntds_guid
;
118 r
->in
.req
->req1
.options
=
119 DRSUAPI_DRS_ASYNC_OP
|
120 DRSUAPI_DRS_UPDATE_NOTIFICATION
|
121 DRSUAPI_DRS_WRIT_REP
;
123 if (state
->op
->is_urgent
) {
124 r
->in
.req
->req1
.options
|= DRSUAPI_DRS_SYNC_URGENT
;
127 state
->ndr_struct_ptr
= r
;
130 NDR_PRINT_IN_DEBUG(drsuapi_DsReplicaSync
, r
);
133 subreq
= dcerpc_drsuapi_DsReplicaSync_r_send(state
,
135 drsuapi
->drsuapi_handle
,
137 if (tevent_req_nomem(subreq
, req
)) {
140 tevent_req_set_callback(subreq
, dreplsrv_op_notify_replica_sync_done
, req
);
143 static void dreplsrv_op_notify_replica_sync_done(struct tevent_req
*subreq
)
145 struct tevent_req
*req
=
146 tevent_req_callback_data(subreq
,
148 struct dreplsrv_op_notify_state
*state
=
150 struct dreplsrv_op_notify_state
);
151 struct drsuapi_DsReplicaSync
*r
= talloc_get_type(state
->ndr_struct_ptr
,
152 struct drsuapi_DsReplicaSync
);
155 state
->ndr_struct_ptr
= NULL
;
157 status
= dcerpc_drsuapi_DsReplicaSync_r_recv(subreq
, r
);
159 if (tevent_req_nterror(req
, status
)) {
163 if (!W_ERROR_IS_OK(r
->out
.result
)) {
164 status
= werror_to_ntstatus(r
->out
.result
);
165 tevent_req_nterror(req
, status
);
169 tevent_req_done(req
);
172 static NTSTATUS
dreplsrv_op_notify_recv(struct tevent_req
*req
)
174 return tevent_req_simple_recv_ntstatus(req
);
178 called when a notify operation has completed
180 static void dreplsrv_notify_op_callback(struct tevent_req
*subreq
)
182 struct dreplsrv_notify_operation
*op
=
183 tevent_req_callback_data(subreq
,
184 struct dreplsrv_notify_operation
);
186 struct dreplsrv_service
*s
= op
->service
;
189 status
= dreplsrv_op_notify_recv(subreq
);
190 werr
= ntstatus_to_werror(status
);
192 if (!NT_STATUS_IS_OK(status
)) {
193 DEBUG(4,("dreplsrv_notify: Failed to send DsReplicaSync to %s for %s - %s : %s\n",
194 op
->source_dsa
->repsFrom1
->other_info
->dns_name
,
195 ldb_dn_get_linearized(op
->source_dsa
->partition
->dn
),
196 nt_errstr(status
), win_errstr(werr
)));
198 DEBUG(2,("dreplsrv_notify: DsReplicaSync successfuly sent to %s\n",
199 op
->source_dsa
->repsFrom1
->other_info
->dns_name
));
200 op
->source_dsa
->notify_uSN
= op
->uSN
;
203 drepl_reps_update(s
, "repsTo", op
->source_dsa
->partition
->dn
,
204 &op
->source_dsa
->repsFrom1
->source_dsa_obj_guid
,
208 s
->ops
.n_current
= NULL
;
209 dreplsrv_run_pending_ops(s
);
213 run any pending replica sync calls
215 void dreplsrv_notify_run_ops(struct dreplsrv_service
*s
)
217 struct dreplsrv_notify_operation
*op
;
218 struct tevent_req
*subreq
;
220 if (s
->ops
.n_current
|| s
->ops
.current
) {
221 /* if there's still one running, we're done */
225 if (!s
->ops
.notifies
) {
226 /* if there're no pending operations, we're done */
230 op
= s
->ops
.notifies
;
231 s
->ops
.n_current
= op
;
232 DLIST_REMOVE(s
->ops
.notifies
, op
);
234 subreq
= dreplsrv_op_notify_send(op
, s
->task
->event_ctx
, op
);
236 DEBUG(0,("dreplsrv_notify_run_ops: dreplsrv_op_notify_send[%s][%s] - no memory\n",
237 op
->source_dsa
->repsFrom1
->other_info
->dns_name
,
238 ldb_dn_get_linearized(op
->source_dsa
->partition
->dn
)));
241 tevent_req_set_callback(subreq
, dreplsrv_notify_op_callback
, op
);
242 DEBUG(4,("started DsReplicaSync for %s to %s\n",
243 ldb_dn_get_linearized(op
->source_dsa
->partition
->dn
),
244 op
->source_dsa
->repsFrom1
->other_info
->dns_name
));
249 find a source_dsa for a given guid
251 static struct dreplsrv_partition_source_dsa
*dreplsrv_find_notify_dsa(struct dreplsrv_partition
*p
,
254 struct dreplsrv_partition_source_dsa
*s
;
256 /* first check the sources list */
257 for (s
=p
->sources
; s
; s
=s
->next
) {
258 if (GUID_compare(&s
->repsFrom1
->source_dsa_obj_guid
, guid
) == 0) {
263 /* then the notifies list */
264 for (s
=p
->notifies
; s
; s
=s
->next
) {
265 if (GUID_compare(&s
->repsFrom1
->source_dsa_obj_guid
, guid
) == 0) {
274 schedule a replicaSync message
276 static WERROR
dreplsrv_schedule_notify_sync(struct dreplsrv_service
*service
,
277 struct dreplsrv_partition
*p
,
278 struct repsFromToBlob
*reps
,
282 uint32_t replica_flags
)
284 struct dreplsrv_notify_operation
*op
;
285 struct dreplsrv_partition_source_dsa
*s
;
287 s
= dreplsrv_find_notify_dsa(p
, &reps
->ctr
.ctr1
.source_dsa_obj_guid
);
289 DEBUG(0,(__location__
": Unable to find source_dsa for %s\n",
290 GUID_string(mem_ctx
, &reps
->ctr
.ctr1
.source_dsa_obj_guid
)));
291 return WERR_DS_UNAVAILABLE
;
294 /* first try to find an existing notify operation */
295 for (op
= service
->ops
.notifies
; op
; op
= op
->next
) {
296 if (op
->source_dsa
!= s
) {
300 if (op
->is_urgent
!= is_urgent
) {
304 if (op
->replica_flags
!= replica_flags
) {
312 /* reuse the notify operation, as it's not yet started */
316 op
= talloc_zero(mem_ctx
, struct dreplsrv_notify_operation
);
317 W_ERROR_HAVE_NO_MEMORY(op
);
319 op
->service
= service
;
322 op
->is_urgent
= is_urgent
;
323 op
->replica_flags
= replica_flags
;
324 op
->schedule_time
= time(NULL
);
326 DLIST_ADD_END(service
->ops
.notifies
, op
, struct dreplsrv_notify_operation
*);
327 talloc_steal(service
, op
);
332 see if a partition has a hugher uSN than what is in the repsTo and
333 if so then send a DsReplicaSync
335 static WERROR
dreplsrv_notify_check(struct dreplsrv_service
*s
,
336 struct dreplsrv_partition
*p
,
340 struct repsFromToBlob
*reps
;
347 werr
= dsdb_loadreps(s
->samdb
, mem_ctx
, p
->dn
, "repsTo", &reps
, &count
);
348 if (!W_ERROR_IS_OK(werr
)) {
349 DEBUG(0,(__location__
": Failed to load repsTo for %s\n",
350 ldb_dn_get_linearized(p
->dn
)));
354 /* loads the partition uSNHighest and uSNUrgent */
355 ret
= dsdb_load_partition_usn(s
->samdb
, p
->dn
, &uSNHighest
, &uSNUrgent
);
356 if (ret
!= LDB_SUCCESS
|| uSNHighest
== 0) {
361 /* see if any of our partners need some of our objects */
362 for (i
=0; i
<count
; i
++) {
363 struct dreplsrv_partition_source_dsa
*sdsa
;
364 uint32_t replica_flags
;
365 sdsa
= dreplsrv_find_notify_dsa(p
, &reps
[i
].ctr
.ctr1
.source_dsa_obj_guid
);
366 replica_flags
= reps
[i
].ctr
.ctr1
.replica_flags
;
367 if (sdsa
== NULL
) continue;
368 if (sdsa
->notify_uSN
< uSNHighest
) {
369 /* we need to tell this partner to replicate
371 bool is_urgent
= sdsa
->notify_uSN
< uSNUrgent
;
373 /* check if urgent replication is needed */
374 werr
= dreplsrv_schedule_notify_sync(s
, p
, &reps
[i
], mem_ctx
,
375 uSNHighest
, is_urgent
, replica_flags
);
376 if (!W_ERROR_IS_OK(werr
)) {
377 DEBUG(0,(__location__
": Failed to setup notify to %s for %s\n",
378 reps
[i
].ctr
.ctr1
.other_info
->dns_name
,
379 ldb_dn_get_linearized(p
->dn
)));
382 DEBUG(4,("queued DsReplicaSync for %s to %s (urgent=%s) uSN=%llu:%llu\n",
383 ldb_dn_get_linearized(p
->dn
),
384 reps
[i
].ctr
.ctr1
.other_info
->dns_name
,
385 is_urgent
?"true":"false",
386 (unsigned long long)sdsa
->notify_uSN
,
387 (unsigned long long)uSNHighest
));
395 see if any of the partitions have changed, and if so then send a
396 DsReplicaSync to all the replica partners in the repsTo object
398 static WERROR
dreplsrv_notify_check_all(struct dreplsrv_service
*s
, TALLOC_CTX
*mem_ctx
)
401 struct dreplsrv_partition
*p
;
403 for (p
= s
->partitions
; p
; p
= p
->next
) {
404 status
= dreplsrv_notify_check(s
, p
, mem_ctx
);
405 W_ERROR_NOT_OK_RETURN(status
);
411 static void dreplsrv_notify_run(struct dreplsrv_service
*service
);
413 static void dreplsrv_notify_handler_te(struct tevent_context
*ev
, struct tevent_timer
*te
,
414 struct timeval t
, void *ptr
)
416 struct dreplsrv_service
*service
= talloc_get_type(ptr
, struct dreplsrv_service
);
419 service
->notify
.te
= NULL
;
421 dreplsrv_notify_run(service
);
423 status
= dreplsrv_notify_schedule(service
, service
->notify
.interval
);
424 if (!W_ERROR_IS_OK(status
)) {
425 task_server_terminate(service
->task
, win_errstr(status
), false);
430 WERROR
dreplsrv_notify_schedule(struct dreplsrv_service
*service
, uint32_t next_interval
)
433 struct tevent_timer
*new_te
;
434 struct timeval next_time
;
436 /* prevent looping */
437 if (next_interval
== 0) next_interval
= 1;
439 next_time
= timeval_current_ofs(next_interval
, 50);
441 if (service
->notify
.te
) {
443 * if the timestamp of the new event is higher,
444 * as current next we don't need to reschedule
446 if (timeval_compare(&next_time
, &service
->notify
.next_event
) > 0) {
451 /* reset the next scheduled timestamp */
452 service
->notify
.next_event
= next_time
;
454 new_te
= tevent_add_timer(service
->task
->event_ctx
, service
,
455 service
->notify
.next_event
,
456 dreplsrv_notify_handler_te
, service
);
457 W_ERROR_HAVE_NO_MEMORY(new_te
);
459 tmp_mem
= talloc_new(service
);
460 DEBUG(4,("dreplsrv_notify_schedule(%u) %sscheduled for: %s\n",
462 (service
->notify
.te
?"re":""),
463 nt_time_string(tmp_mem
, timeval_to_nttime(&next_time
))));
464 talloc_free(tmp_mem
);
466 talloc_free(service
->notify
.te
);
467 service
->notify
.te
= new_te
;
472 static void dreplsrv_notify_run(struct dreplsrv_service
*service
)
476 mem_ctx
= talloc_new(service
);
477 dreplsrv_notify_check_all(service
, mem_ctx
);
478 talloc_free(mem_ctx
);
480 dreplsrv_run_pending_ops(service
);