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 "dsdb/repl/drepl_service.h"
28 #include <ldb_errors.h>
29 #include "../lib/util/dlinklist.h"
30 #include "librpc/gen_ndr/ndr_misc.h"
31 #include "librpc/gen_ndr/ndr_drsuapi.h"
32 #include "librpc/gen_ndr/ndr_drsblobs.h"
33 #include "libcli/composite/composite.h"
34 #include "libcli/security/security.h"
37 update repsFrom/repsTo error information
39 void drepl_reps_update(struct dreplsrv_service *s, const char *reps_attr,
41 struct GUID *source_dsa_obj_guid, WERROR status)
43 struct repsFromToBlob *reps;
46 TALLOC_CTX *tmp_ctx = talloc_new(s);
51 unix_to_nt_time(&now, t);
53 werr = dsdb_loadreps(s->samdb, tmp_ctx, dn, reps_attr, &reps, &count);
54 if (!W_ERROR_IS_OK(werr)) {
59 for (i=0; i<count; i++) {
60 if (GUID_compare(source_dsa_obj_guid,
61 &reps[i].ctr.ctr1.source_dsa_obj_guid) == 0) {
67 /* no record to update */
72 /* only update the status fields */
73 reps[i].ctr.ctr1.last_attempt = now;
74 reps[i].ctr.ctr1.result_last_attempt = status;
75 if (W_ERROR_IS_OK(status)) {
76 reps[i].ctr.ctr1.last_success = now;
77 reps[i].ctr.ctr1.consecutive_sync_failures = 0;
79 reps[i].ctr.ctr1.consecutive_sync_failures++;
82 werr = dsdb_savereps(s->samdb, tmp_ctx, dn, reps_attr, reps, count);
83 if (!W_ERROR_IS_OK(werr)) {
84 DEBUG(2,("drepl_reps_update: Failed to save %s for %s: %s\n",
85 reps_attr, ldb_dn_get_linearized(dn), win_errstr(werr)));
90 WERROR dreplsrv_schedule_partition_pull_source(struct dreplsrv_service *s,
91 struct dreplsrv_partition_source_dsa *source,
92 enum drsuapi_DsExtendedOperation extended_op,
94 dreplsrv_extended_callback_t callback,
97 struct dreplsrv_out_operation *op;
99 op = talloc_zero(s, struct dreplsrv_out_operation);
100 W_ERROR_HAVE_NO_MEMORY(op);
103 op->source_dsa = source;
104 op->extended_op = extended_op;
105 op->fsmo_info = fsmo_info;
106 op->callback = callback;
107 op->cb_data = cb_data;
108 op->schedule_time = time(NULL);
110 DLIST_ADD_END(s->ops.pending, op, struct dreplsrv_out_operation *);
115 static WERROR dreplsrv_schedule_partition_pull(struct dreplsrv_service *s,
116 struct dreplsrv_partition *p,
120 struct dreplsrv_partition_source_dsa *cur;
122 for (cur = p->sources; cur; cur = cur->next) {
123 status = dreplsrv_schedule_partition_pull_source(s, cur,
124 DRSUAPI_EXOP_NONE, 0,
126 W_ERROR_NOT_OK_RETURN(status);
132 WERROR dreplsrv_schedule_pull_replication(struct dreplsrv_service *s, TALLOC_CTX *mem_ctx)
135 struct dreplsrv_partition *p;
137 for (p = s->partitions; p; p = p->next) {
138 status = dreplsrv_schedule_partition_pull(s, p, mem_ctx);
139 W_ERROR_NOT_OK_RETURN(status);
146 static void dreplsrv_pending_op_callback(struct tevent_req *subreq)
148 struct dreplsrv_out_operation *op = tevent_req_callback_data(subreq,
149 struct dreplsrv_out_operation);
150 struct repsFromTo1 *rf = op->source_dsa->repsFrom1;
151 struct dreplsrv_service *s = op->service;
154 werr = dreplsrv_op_pull_source_recv(subreq);
157 DEBUG(4,("dreplsrv_op_pull_source(%s) for %s\n", win_errstr(werr),
158 ldb_dn_get_linearized(op->source_dsa->partition->dn)));
160 if (op->extended_op == DRSUAPI_EXOP_NONE) {
161 drepl_reps_update(s, "repsFrom", op->source_dsa->partition->dn,
162 &rf->source_dsa_obj_guid, werr);
166 op->callback(s, werr, op->extended_ret, op->cb_data);
169 s->ops.current = NULL;
170 dreplsrv_run_pending_ops(s);
173 void dreplsrv_run_pull_ops(struct dreplsrv_service *s)
175 struct dreplsrv_out_operation *op;
178 struct tevent_req *subreq;
180 if (s->ops.current) {
181 /* if there's still one running, we're done */
185 if (!s->ops.pending) {
186 /* if there're no pending operations, we're done */
191 unix_to_nt_time(&now, t);
195 DLIST_REMOVE(s->ops.pending, op);
197 op->source_dsa->repsFrom1->last_attempt = now;
199 subreq = dreplsrv_op_pull_source_send(op, s->task->event_ctx, op);
201 struct repsFromTo1 *rf = op->source_dsa->repsFrom1;
203 if (op->extended_op == DRSUAPI_EXOP_NONE) {
204 drepl_reps_update(s, "repsFrom", op->source_dsa->partition->dn,
205 &rf->source_dsa_obj_guid, WERR_NOMEM);
207 s->ops.current = NULL;
210 * call the callback (if any) so it gets the chance
211 * to do its job just like in any other failure situation
214 op->callback(s, WERR_NOMEM, op->extended_ret, op->cb_data);
218 tevent_req_set_callback(subreq, dreplsrv_pending_op_callback, op);