s4-repl: removed the syncall_workaround code
[kai/samba.git] / source4 / dsdb / repl / drepl_out_pull.c
1 /* 
2    Unix SMB/CIFS mplementation.
3    DSDB replication service outgoing Pull-Replication
4    
5    Copyright (C) Stefan Metzmacher 2007
6     
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.
11    
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.
16    
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/>.
19    
20 */
21
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 "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/security.h"
36
37 /*
38   update repsFrom/repsTo error information
39  */
40 void drepl_reps_update(struct dreplsrv_service *s, const char *reps_attr,
41                        struct ldb_dn *dn,
42                        struct GUID *source_dsa_obj_guid, WERROR status)
43 {
44         struct repsFromToBlob *reps;
45         uint32_t count, i;
46         WERROR werr;
47         TALLOC_CTX *tmp_ctx = talloc_new(s);
48         time_t t;
49         NTTIME now;
50
51         t = time(NULL);
52         unix_to_nt_time(&now, t);
53
54         werr = dsdb_loadreps(s->samdb, tmp_ctx, dn, reps_attr, &reps, &count);
55         if (!W_ERROR_IS_OK(werr)) {
56                 talloc_free(tmp_ctx);
57                 return;
58         }
59
60         for (i=0; i<count; i++) {
61                 if (GUID_compare(source_dsa_obj_guid,
62                                  &reps[i].ctr.ctr1.source_dsa_obj_guid) == 0) {
63                         break;
64                 }
65         }
66
67         if (i == count) {
68                 /* no record to update */
69                 talloc_free(tmp_ctx);
70                 return;
71         }
72
73         /* only update the status fields */
74         reps[i].ctr.ctr1.last_attempt = now;
75         reps[i].ctr.ctr1.result_last_attempt = status;
76         if (W_ERROR_IS_OK(status)) {
77                 reps[i].ctr.ctr1.last_success = now;
78                 reps[i].ctr.ctr1.consecutive_sync_failures = 0;
79         } else {
80                 reps[i].ctr.ctr1.consecutive_sync_failures++;
81         }
82
83         werr = dsdb_savereps(s->samdb, tmp_ctx, dn, reps_attr, reps, count);
84         if (!W_ERROR_IS_OK(werr)) {
85                 DEBUG(2,("drepl_reps_update: Failed to save %s for %s: %s\n",
86                          reps_attr, ldb_dn_get_linearized(dn), win_errstr(werr)));
87         }
88         talloc_free(tmp_ctx);
89 }
90
91 WERROR dreplsrv_schedule_partition_pull_source(struct dreplsrv_service *s,
92                                                struct dreplsrv_partition_source_dsa *source,
93                                                enum drsuapi_DsExtendedOperation extended_op,
94                                                uint64_t fsmo_info,
95                                                dreplsrv_extended_callback_t callback,
96                                                void *cb_data)
97 {
98         struct dreplsrv_out_operation *op;
99
100         op = talloc_zero(s, struct dreplsrv_out_operation);
101         W_ERROR_HAVE_NO_MEMORY(op);
102
103         op->service     = s;
104         op->source_dsa  = source;
105         op->extended_op = extended_op;
106         op->fsmo_info   = fsmo_info;
107         op->callback    = callback;
108         op->cb_data     = cb_data;
109         op->schedule_time = time(NULL);
110
111         DLIST_ADD_END(s->ops.pending, op, struct dreplsrv_out_operation *);
112
113         return WERR_OK;
114 }
115
116 static WERROR dreplsrv_schedule_partition_pull(struct dreplsrv_service *s,
117                                                struct dreplsrv_partition *p,
118                                                TALLOC_CTX *mem_ctx)
119 {
120         WERROR status;
121         struct dreplsrv_partition_source_dsa *cur;
122
123         for (cur = p->sources; cur; cur = cur->next) {
124                 status = dreplsrv_schedule_partition_pull_source(s, cur,
125                                                                  DRSUAPI_EXOP_NONE, 0,
126                                                                  NULL, NULL);
127                 W_ERROR_NOT_OK_RETURN(status);
128         }
129
130         return WERR_OK;
131 }
132
133 WERROR dreplsrv_schedule_pull_replication(struct dreplsrv_service *s, TALLOC_CTX *mem_ctx)
134 {
135         WERROR status;
136         struct dreplsrv_partition *p;
137
138         for (p = s->partitions; p; p = p->next) {
139                 status = dreplsrv_schedule_partition_pull(s, p, mem_ctx);
140                 W_ERROR_NOT_OK_RETURN(status);
141         }
142
143         return WERR_OK;
144 }
145
146
147 static void dreplsrv_pending_op_callback(struct tevent_req *subreq)
148 {
149         struct dreplsrv_out_operation *op = tevent_req_callback_data(subreq,
150                                             struct dreplsrv_out_operation);
151         struct repsFromTo1 *rf = op->source_dsa->repsFrom1;
152         struct dreplsrv_service *s = op->service;
153         WERROR werr;
154
155         werr = dreplsrv_op_pull_source_recv(subreq);
156         TALLOC_FREE(subreq);
157
158         DEBUG(4,("dreplsrv_op_pull_source(%s) for %s\n", win_errstr(werr),
159                  ldb_dn_get_linearized(op->source_dsa->partition->dn)));
160
161         if (op->extended_op == DRSUAPI_EXOP_NONE) {
162                 drepl_reps_update(s, "repsFrom", op->source_dsa->partition->dn,
163                                   &rf->source_dsa_obj_guid, werr);
164         }
165
166         if (op->callback) {
167                 op->callback(s, werr, op->extended_ret, op->cb_data);
168         }
169         talloc_free(op);
170         s->ops.current = NULL;
171         dreplsrv_run_pending_ops(s);
172 }
173
174 void dreplsrv_run_pull_ops(struct dreplsrv_service *s)
175 {
176         struct dreplsrv_out_operation *op;
177         time_t t;
178         NTTIME now;
179         struct tevent_req *subreq;
180
181         if (s->ops.current) {
182                 /* if there's still one running, we're done */
183                 return;
184         }
185
186         if (!s->ops.pending) {
187                 /* if there're no pending operations, we're done */
188                 return;
189         }
190
191         t = time(NULL);
192         unix_to_nt_time(&now, t);
193
194         op = s->ops.pending;
195         s->ops.current = op;
196         DLIST_REMOVE(s->ops.pending, op);
197
198         op->source_dsa->repsFrom1->last_attempt = now;
199
200         subreq = dreplsrv_op_pull_source_send(op, s->task->event_ctx, op);
201         if (!subreq) {
202                 struct repsFromTo1 *rf = op->source_dsa->repsFrom1;
203
204                 if (op->extended_op == DRSUAPI_EXOP_NONE) {
205                         drepl_reps_update(s, "repsFrom", op->source_dsa->partition->dn,
206                                           &rf->source_dsa_obj_guid, WERR_NOMEM);
207                 }
208                 s->ops.current = NULL;
209
210                 /*
211                  * call the callback (if any) so it gets the chance
212                  * to do its job just like in any other failure situation
213                  */
214                 if (op->callback) {
215                         op->callback(s, WERR_NOMEM, op->extended_ret, op->cb_data);
216                 }
217                 return;
218         }
219         tevent_req_set_callback(subreq, dreplsrv_pending_op_callback, op);
220 }