2 Unix SMB/CIFS mplementation.
3 DSDB replication service outgoing Pull-Replication
5 Copyright (C) Stefan Metzmacher 2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "dsdb/samdb/samdb.h"
24 #include "auth/auth.h"
25 #include "smbd/service.h"
26 #include "lib/events/events.h"
27 #include "lib/messaging/irpc.h"
28 #include "dsdb/repl/drepl_service.h"
29 #include "lib/ldb/include/ldb_errors.h"
30 #include "../lib/util/dlinklist.h"
31 #include "librpc/gen_ndr/ndr_misc.h"
32 #include "librpc/gen_ndr/ndr_drsuapi.h"
33 #include "librpc/gen_ndr/ndr_drsblobs.h"
34 #include "libcli/composite/composite.h"
35 #include "libcli/security/dom_sid.h"
37 WERROR dreplsrv_schedule_partition_pull_source(struct dreplsrv_service *s,
38 struct dreplsrv_partition_source_dsa *source,
39 enum drsuapi_DsExtendedOperation extended_op,
41 dreplsrv_fsmo_callback_t callback)
43 struct dreplsrv_out_operation *op;
45 op = talloc_zero(s, struct dreplsrv_out_operation);
46 W_ERROR_HAVE_NO_MEMORY(op);
49 op->source_dsa = source;
50 op->extended_op = extended_op;
51 op->fsmo_info = fsmo_info;
52 op->callback = callback;
54 DLIST_ADD_END(s->ops.pending, op, struct dreplsrv_out_operation *);
59 static WERROR dreplsrv_schedule_partition_pull(struct dreplsrv_service *s,
60 struct dreplsrv_partition *p,
64 struct dreplsrv_partition_source_dsa *cur;
66 for (cur = p->sources; cur; cur = cur->next) {
67 status = dreplsrv_schedule_partition_pull_source(s, cur, DRSUAPI_EXOP_NONE, 0, NULL);
68 W_ERROR_NOT_OK_RETURN(status);
74 WERROR dreplsrv_schedule_pull_replication(struct dreplsrv_service *s, TALLOC_CTX *mem_ctx)
77 struct dreplsrv_partition *p;
79 for (p = s->partitions; p; p = p->next) {
80 status = dreplsrv_schedule_partition_pull(s, p, mem_ctx);
81 W_ERROR_NOT_OK_RETURN(status);
88 /* force an immediate of the specified partition by GUID */
89 WERROR dreplsrv_schedule_partition_pull_by_guid(struct dreplsrv_service *s, TALLOC_CTX *mem_ctx,
92 struct dreplsrv_partition *p;
94 for (p = s->partitions; p; p = p->next) {
95 if (GUID_compare(&p->nc.guid, guid) == 0) {
96 return dreplsrv_schedule_partition_pull(s, p, mem_ctx);
100 return WERR_NOT_FOUND;
103 /* force an immediate of the specified partition by Naming Context */
104 WERROR dreplsrv_schedule_partition_pull_by_nc(struct dreplsrv_service *s, TALLOC_CTX *mem_ctx,
105 struct drsuapi_DsReplicaObjectIdentifier *nc)
107 struct dreplsrv_partition *p;
108 bool valid_sid, valid_guid;
109 struct dom_sid null_sid;
110 ZERO_STRUCT(null_sid);
112 valid_sid = !dom_sid_equal(&null_sid, &nc->sid);
113 valid_guid = !GUID_all_zero(&nc->guid);
115 if (!valid_sid && !valid_guid && !nc->dn) {
116 return WERR_DS_DRA_INVALID_PARAMETER;
119 for (p = s->partitions; p; p = p->next) {
120 if ((valid_guid && GUID_equal(&p->nc.guid, &nc->guid))
121 || strequal(p->nc.dn, nc->dn)
122 || (valid_sid && dom_sid_equal(&p->nc.sid, &nc->sid))) {
123 return dreplsrv_schedule_partition_pull(s, p, mem_ctx);
127 return WERR_DS_DRA_BAD_NC;
131 static void dreplsrv_pending_op_callback(struct tevent_req *subreq)
133 struct dreplsrv_out_operation *op = tevent_req_callback_data(subreq,
134 struct dreplsrv_out_operation);
135 struct repsFromTo1 *rf = op->source_dsa->repsFrom1;
136 struct dreplsrv_service *s = op->service;
141 unix_to_nt_time(&now, t);
143 rf->result_last_attempt = dreplsrv_op_pull_source_recv(subreq);
145 if (W_ERROR_IS_OK(rf->result_last_attempt)) {
146 rf->consecutive_sync_failures = 0;
147 rf->last_success = now;
148 DEBUG(3,("dreplsrv_op_pull_source(%s)\n",
149 win_errstr(rf->result_last_attempt)));
153 rf->consecutive_sync_failures++;
155 DEBUG(1,("dreplsrv_op_pull_source(%s/%s) for %s failures[%u]\n",
156 win_errstr(rf->result_last_attempt),
157 nt_errstr(werror_to_ntstatus(rf->result_last_attempt)),
158 ldb_dn_get_linearized(op->source_dsa->partition->dn),
159 rf->consecutive_sync_failures));
163 op->callback(s, rf->result_last_attempt, op->extended_ret);
166 s->ops.current = NULL;
167 dreplsrv_run_pending_ops(s);
168 dreplsrv_notify_run_ops(s);
171 void dreplsrv_run_pending_ops(struct dreplsrv_service *s)
173 struct dreplsrv_out_operation *op;
176 struct tevent_req *subreq;
178 if (s->ops.current || s->ops.n_current) {
179 /* if there's still one running, we're done */
183 if (!s->ops.pending) {
184 /* if there're no pending operations, we're done */
189 unix_to_nt_time(&now, t);
193 DLIST_REMOVE(s->ops.pending, op);
195 op->source_dsa->repsFrom1->last_attempt = now;
197 subreq = dreplsrv_op_pull_source_send(op, s->task->event_ctx, op);
199 struct repsFromTo1 *rf = op->source_dsa->repsFrom1;
201 rf->result_last_attempt = WERR_NOMEM;
202 rf->consecutive_sync_failures++;
203 s->ops.current = NULL;
205 DEBUG(1,("dreplsrv_op_pull_source(%s/%s) failures[%u]\n",
206 win_errstr(rf->result_last_attempt),
207 nt_errstr(werror_to_ntstatus(rf->result_last_attempt)),
208 rf->consecutive_sync_failures));
211 tevent_req_set_callback(subreq, dreplsrv_pending_op_callback, op);