s4:dsdb/repl/drepl* - move "lib/messaging/irpc.h" include into "drepl_service.h"
[metze/samba/wip.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 "dsdb/repl/drepl_service.h"
28 #include "lib/ldb/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"
35
36 /*
37   update repsFrom/repsTo error information
38  */
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)
42 {
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;
49
50         t = time(NULL);
51         unix_to_nt_time(&now, t);
52
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;
57         }
58
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;
63                 }
64         }
65
66         if (i == count) {
67                 /* no record to update */
68                 talloc_free(tmp_ctx);
69                 return;
70         }
71
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++;
80         }
81
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)));
86         }
87         talloc_free(tmp_ctx);
88 }
89
90 WERROR dreplsrv_schedule_partition_pull_source(struct dreplsrv_service *s,
91                                                struct dreplsrv_partition_source_dsa *source,
92                                                enum drsuapi_DsExtendedOperation extended_op,
93                                                uint64_t fsmo_info,
94                                                dreplsrv_extended_callback_t callback,
95                                                void *cb_data)
96 {
97         struct dreplsrv_out_operation *op;
98
99         op = talloc_zero(s, struct dreplsrv_out_operation);
100         W_ERROR_HAVE_NO_MEMORY(op);
101
102         op->service     = s;
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);
109
110         DLIST_ADD_END(s->ops.pending, op, struct dreplsrv_out_operation *);
111
112         return WERR_OK;
113 }
114
115 static WERROR dreplsrv_schedule_partition_pull(struct dreplsrv_service *s,
116                                                struct dreplsrv_partition *p,
117                                                TALLOC_CTX *mem_ctx)
118 {
119         WERROR status;
120         struct dreplsrv_partition_source_dsa *cur;
121
122         for (cur = p->sources; cur; cur = cur->next) {
123                 status = dreplsrv_schedule_partition_pull_source(s, cur,
124                                                                  DRSUAPI_EXOP_NONE, 0,
125                                                                  NULL, NULL);
126                 W_ERROR_NOT_OK_RETURN(status);
127         }
128
129         return WERR_OK;
130 }
131
132 WERROR dreplsrv_schedule_pull_replication(struct dreplsrv_service *s, TALLOC_CTX *mem_ctx)
133 {
134         WERROR status;
135         struct dreplsrv_partition *p;
136
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);
140         }
141
142         return WERR_OK;
143 }
144
145
146 static void dreplsrv_pending_op_callback(struct tevent_req *subreq)
147 {
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;
152         WERROR werr;
153
154         werr = dreplsrv_op_pull_source_recv(subreq);
155         TALLOC_FREE(subreq);
156
157         DEBUG(4,("dreplsrv_op_pull_source(%s) for %s\n", win_errstr(werr),
158                  ldb_dn_get_linearized(op->source_dsa->partition->dn)));
159
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);
163         }
164
165         if (op->callback) {
166                 op->callback(s, werr, op->extended_ret, op->cb_data);
167         }
168         talloc_free(op);
169         s->ops.current = NULL;
170         dreplsrv_run_pending_ops(s);
171 }
172
173 void dreplsrv_run_pull_ops(struct dreplsrv_service *s)
174 {
175         struct dreplsrv_out_operation *op;
176         time_t t;
177         NTTIME now;
178         struct tevent_req *subreq;
179
180         if (s->ops.current) {
181                 /* if there's still one running, we're done */
182                 return;
183         }
184
185         if (!s->ops.pending) {
186                 /* if there're no pending operations, we're done */
187                 return;
188         }
189
190         t = time(NULL);
191         unix_to_nt_time(&now, t);
192
193         op = s->ops.pending;
194         s->ops.current = op;
195         DLIST_REMOVE(s->ops.pending, op);
196
197         op->source_dsa->repsFrom1->last_attempt = now;
198
199         subreq = dreplsrv_op_pull_source_send(op, s->task->event_ctx, op);
200         if (!subreq) {
201                 struct repsFromTo1 *rf = op->source_dsa->repsFrom1;
202
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);
206                 }
207                 s->ops.current = NULL;
208
209                 /*
210                  * call the callback (if any) so it gets the chance
211                  * to do its job just like in any other failure situation
212                  */
213                 if (op->callback) {
214                         op->callback(s, WERR_NOMEM, op->extended_ret, op->cb_data);
215                 }
216                 return;
217         }
218         tevent_req_set_callback(subreq, dreplsrv_pending_op_callback, op);
219 }