s3:smbd/close: remove unused goto out from close_directory()
[Samba/gebeck_regimport.git] / source4 / dsdb / repl / drepl_out_pull.c
blob86b513d21d2f6de39e57400f827848beeec335e3
1 /*
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/>.
22 #include "includes.h"
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,
40 struct ldb_dn *dn,
41 struct GUID *source_dsa_obj_guid, WERROR status)
43 struct repsFromToBlob *reps;
44 uint32_t count, i;
45 WERROR werr;
46 TALLOC_CTX *tmp_ctx = talloc_new(s);
47 time_t t;
48 NTTIME now;
50 t = time(NULL);
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)) {
55 talloc_free(tmp_ctx);
56 return;
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) {
62 break;
66 if (i == count) {
67 /* no record to update */
68 talloc_free(tmp_ctx);
69 return;
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;
78 } else {
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)));
87 talloc_free(tmp_ctx);
90 WERROR dreplsrv_schedule_partition_pull_source(struct dreplsrv_service *s,
91 struct dreplsrv_partition_source_dsa *source,
92 uint32_t options,
93 enum drsuapi_DsExtendedOperation extended_op,
94 uint64_t fsmo_info,
95 dreplsrv_extended_callback_t callback,
96 void *cb_data)
98 struct dreplsrv_out_operation *op;
100 op = talloc_zero(s, struct dreplsrv_out_operation);
101 W_ERROR_HAVE_NO_MEMORY(op);
103 op->service = s;
104 op->source_dsa = source;
105 op->options = options;
106 op->extended_op = extended_op;
107 op->fsmo_info = fsmo_info;
108 op->callback = callback;
109 op->cb_data = cb_data;
110 op->schedule_time = time(NULL);
112 DLIST_ADD_END(s->ops.pending, op, struct dreplsrv_out_operation *);
114 return WERR_OK;
117 static WERROR dreplsrv_schedule_partition_pull(struct dreplsrv_service *s,
118 struct dreplsrv_partition *p,
119 TALLOC_CTX *mem_ctx)
121 WERROR status;
122 struct dreplsrv_partition_source_dsa *cur;
124 for (cur = p->sources; cur; cur = cur->next) {
125 status = dreplsrv_schedule_partition_pull_source(s, cur,
126 0, DRSUAPI_EXOP_NONE, 0,
127 NULL, NULL);
128 W_ERROR_NOT_OK_RETURN(status);
131 return WERR_OK;
134 WERROR dreplsrv_schedule_pull_replication(struct dreplsrv_service *s, TALLOC_CTX *mem_ctx)
136 WERROR status;
137 struct dreplsrv_partition *p;
139 for (p = s->partitions; p; p = p->next) {
140 status = dreplsrv_schedule_partition_pull(s, p, mem_ctx);
141 W_ERROR_NOT_OK_RETURN(status);
144 return WERR_OK;
148 static void dreplsrv_pending_op_callback(struct tevent_req *subreq)
150 struct dreplsrv_out_operation *op = tevent_req_callback_data(subreq,
151 struct dreplsrv_out_operation);
152 struct repsFromTo1 *rf = op->source_dsa->repsFrom1;
153 struct dreplsrv_service *s = op->service;
154 WERROR werr;
156 werr = dreplsrv_op_pull_source_recv(subreq);
157 TALLOC_FREE(subreq);
159 DEBUG(4,("dreplsrv_op_pull_source(%s) for %s\n", win_errstr(werr),
160 ldb_dn_get_linearized(op->source_dsa->partition->dn)));
162 if (op->extended_op == DRSUAPI_EXOP_NONE) {
163 drepl_reps_update(s, "repsFrom", op->source_dsa->partition->dn,
164 &rf->source_dsa_obj_guid, werr);
167 if (op->callback) {
168 op->callback(s, werr, op->extended_ret, op->cb_data);
170 talloc_free(op);
171 s->ops.current = NULL;
172 dreplsrv_run_pending_ops(s);
175 void dreplsrv_run_pull_ops(struct dreplsrv_service *s)
177 struct dreplsrv_out_operation *op;
178 time_t t;
179 NTTIME now;
180 struct tevent_req *subreq;
181 WERROR werr;
183 if (s->ops.current) {
184 /* if there's still one running, we're done */
185 return;
188 if (!s->ops.pending) {
189 /* if there're no pending operations, we're done */
190 return;
193 t = time(NULL);
194 unix_to_nt_time(&now, t);
196 op = s->ops.pending;
197 s->ops.current = op;
198 DLIST_REMOVE(s->ops.pending, op);
200 op->source_dsa->repsFrom1->last_attempt = now;
202 /* check if inbound replication is enabled */
203 if (!(op->options & DRSUAPI_DRS_SYNC_FORCED)) {
204 uint32_t rep_options;
205 if (samdb_ntds_options(op->service->samdb, &rep_options) != LDB_SUCCESS) {
206 werr = WERR_DS_DRA_INTERNAL_ERROR;
207 goto failed;
210 if ((rep_options & DS_NTDSDSA_OPT_DISABLE_INBOUND_REPL)) {
211 werr = WERR_DS_DRA_SINK_DISABLED;
212 goto failed;
216 subreq = dreplsrv_op_pull_source_send(op, s->task->event_ctx, op);
217 if (!subreq) {
218 werr = WERR_NOMEM;
219 goto failed;
222 tevent_req_set_callback(subreq, dreplsrv_pending_op_callback, op);
223 return;
225 failed:
226 if (op->extended_op == DRSUAPI_EXOP_NONE) {
227 drepl_reps_update(s, "repsFrom", op->source_dsa->partition->dn,
228 &op->source_dsa->repsFrom1->source_dsa_obj_guid, werr);
230 /* unblock queue processing */
231 s->ops.current = NULL;
233 * let the callback do its job just like in any other failure situation
235 if (op->callback) {
236 op->callback(s, werr, op->extended_ret, op->cb_data);