s4:dsdb Print the partition we failed to suggest replication for
[ira/wip.git] / source4 / dsdb / repl / drepl_notify.c
1 /* 
2    Unix SMB/CIFS mplementation.
3
4    DSDB replication service periodic notification handling
5    
6    Copyright (C) Andrew Tridgell 2009
7    based on drepl_periodic
8     
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21    
22 */
23
24 #include "includes.h"
25 #include "lib/events/events.h"
26 #include "dsdb/samdb/samdb.h"
27 #include "auth/auth.h"
28 #include "smbd/service.h"
29 #include "lib/messaging/irpc.h"
30 #include "dsdb/repl/drepl_service.h"
31 #include "lib/ldb/include/ldb_errors.h"
32 #include "../lib/util/dlinklist.h"
33 #include "librpc/gen_ndr/ndr_misc.h"
34 #include "librpc/gen_ndr/ndr_drsuapi.h"
35 #include "librpc/gen_ndr/ndr_drsblobs.h"
36 #include "libcli/composite/composite.h"
37
38
39 struct dreplsrv_op_notify_state {
40         struct composite_context *creq;
41
42         struct dreplsrv_out_connection *conn;
43
44         struct dreplsrv_drsuapi_connection *drsuapi;
45
46         struct drsuapi_DsBindInfoCtr bind_info_ctr;
47         struct drsuapi_DsBind bind_r;
48         struct dreplsrv_notify_operation *op;
49 };
50
51 /*
52   receive a DsReplicaSync reply
53  */
54 static void dreplsrv_op_notify_replica_sync_recv(struct rpc_request *req)
55 {
56         struct dreplsrv_op_notify_state *st = talloc_get_type(req->async.private_data,
57                                                               struct dreplsrv_op_notify_state);
58         struct composite_context *c = st->creq;
59         struct drsuapi_DsReplicaSync *r = talloc_get_type(req->ndr.struct_ptr,
60                                                           struct drsuapi_DsReplicaSync);
61
62         c->status = dcerpc_ndr_request_recv(req);
63         if (!composite_is_ok(c)) return;
64
65         if (!W_ERROR_IS_OK(r->out.result)) {
66                 composite_error(c, werror_to_ntstatus(r->out.result));
67                 return;
68         }
69
70         composite_done(c);
71 }
72
73 /*
74   send a DsReplicaSync
75 */
76 static void dreplsrv_op_notify_replica_sync_send(struct dreplsrv_op_notify_state *st)
77 {
78         struct composite_context *c = st->creq;
79         struct dreplsrv_partition *partition = st->op->source_dsa->partition;
80         struct dreplsrv_drsuapi_connection *drsuapi = st->op->source_dsa->conn->drsuapi;
81         struct rpc_request *req;
82         struct drsuapi_DsReplicaSync *r;
83
84         r = talloc_zero(st, struct drsuapi_DsReplicaSync);
85         if (composite_nomem(r, c)) return;
86
87         r->in.bind_handle       = &drsuapi->bind_handle;
88         r->in.level = 1;
89         r->in.req.req1.naming_context = &partition->nc;
90         r->in.req.req1.source_dsa_guid = st->op->service->ntds_guid;
91         r->in.req.req1.options = 
92                 DRSUAPI_DS_REPLICA_SYNC_ASYNCHRONOUS_OPERATION |
93                 DRSUAPI_DS_REPLICA_SYNC_WRITEABLE |
94                 DRSUAPI_DS_REPLICA_SYNC_ALL_SOURCES;
95         
96
97         req = dcerpc_drsuapi_DsReplicaSync_send(drsuapi->pipe, r, r);
98         composite_continue_rpc(c, req, dreplsrv_op_notify_replica_sync_recv, st);
99 }
100
101 /*
102   called when we have an established connection
103  */
104 static void dreplsrv_op_notify_connect_recv(struct composite_context *creq)
105 {
106         struct dreplsrv_op_notify_state *st = talloc_get_type(creq->async.private_data,
107                                                               struct dreplsrv_op_notify_state);
108         struct composite_context *c = st->creq;
109
110         c->status = dreplsrv_out_drsuapi_recv(creq);
111         if (!composite_is_ok(c)) return;
112
113         dreplsrv_op_notify_replica_sync_send(st);
114 }
115
116 /*
117   start the ReplicaSync async call
118  */
119 static struct composite_context *dreplsrv_op_notify_send(struct dreplsrv_notify_operation *op)
120 {
121         struct composite_context *c;
122         struct composite_context *creq;
123         struct dreplsrv_op_notify_state *st;
124
125         c = composite_create(op, op->service->task->event_ctx);
126         if (c == NULL) return NULL;
127
128         st = talloc_zero(c, struct dreplsrv_op_notify_state);
129         if (composite_nomem(st, c)) return c;
130
131         st->creq        = c;
132         st->op          = op;
133
134         creq = dreplsrv_out_drsuapi_send(op->source_dsa->conn);
135         composite_continue(c, creq, dreplsrv_op_notify_connect_recv, st);
136
137         return c;
138 }
139
140 static void dreplsrv_notify_del_repsTo(struct dreplsrv_notify_operation *op)
141 {
142         uint32_t count;
143         struct repsFromToBlob *reps;
144         WERROR werr;
145         struct dreplsrv_service *s = op->service;
146         int i;
147
148         werr = dsdb_loadreps(s->samdb, op, op->source_dsa->partition->dn, "repsTo", &reps, &count);
149         if (!W_ERROR_IS_OK(werr)) {
150                 DEBUG(0,(__location__ ": Failed to load repsTo for %s\n",
151                          ldb_dn_get_linearized(op->source_dsa->partition->dn)));
152                 return;
153         }
154
155         for (i=0; i<count; i++) {
156                 if (GUID_compare(&reps[i].ctr.ctr1.source_dsa_obj_guid, 
157                                  &op->source_dsa->repsFrom1->source_dsa_obj_guid) == 0) {
158                         memmove(&reps[i], &reps[i+1],
159                                 sizeof(reps[i])*(count-(i+1)));
160                         count--;
161                 }
162         }
163
164         werr = dsdb_savereps(s->samdb, op, op->source_dsa->partition->dn, "repsTo", reps, count);
165         if (!W_ERROR_IS_OK(werr)) {
166                 DEBUG(0,(__location__ ": Failed to save repsTo for %s\n",
167                          ldb_dn_get_linearized(op->source_dsa->partition->dn)));
168                 return;
169         }
170 }
171
172 /*
173   called when a notify operation has completed
174  */
175 static void dreplsrv_notify_op_callback(struct dreplsrv_notify_operation *op)
176 {
177         NTSTATUS status;
178         struct dreplsrv_service *s = op->service;
179
180         status = composite_wait(op->creq);
181         if (!NT_STATUS_IS_OK(status)) {
182                 DEBUG(0,("dreplsrv_notify: Failed to send DsReplicaSync to %s for %s - %s\n",
183                          op->source_dsa->repsFrom1->other_info->dns_name,
184                          ldb_dn_get_linearized(op->source_dsa->partition->dn),
185                          nt_errstr(status)));
186         } else {
187                 DEBUG(2,("dreplsrv_notify: DsReplicaSync OK for %s\n",
188                          op->source_dsa->repsFrom1->other_info->dns_name));
189                 op->source_dsa->notify_uSN = op->uSN;
190                 /* delete the repsTo for this replication partner in the
191                    partition, as we have successfully told him to sync */
192                 dreplsrv_notify_del_repsTo(op);
193         }
194         talloc_free(op->creq);
195
196         talloc_free(op);
197         s->ops.n_current = NULL;
198         dreplsrv_notify_run_ops(s);
199 }
200
201
202 static void dreplsrv_notify_op_callback_creq(struct composite_context *creq)
203 {
204         struct dreplsrv_notify_operation *op = talloc_get_type(creq->async.private_data,
205                                                                struct dreplsrv_notify_operation);
206         dreplsrv_notify_op_callback(op);
207 }
208
209 /*
210   run any pending replica sync calls
211  */
212 void dreplsrv_notify_run_ops(struct dreplsrv_service *s)
213 {
214         struct dreplsrv_notify_operation *op;
215
216         if (s->ops.n_current || s->ops.current) {
217                 /* if there's still one running, we're done */
218                 return;
219         }
220
221         if (!s->ops.notifies) {
222                 /* if there're no pending operations, we're done */
223                 return;
224         }
225
226         op = s->ops.notifies;
227         s->ops.n_current = op;
228         DLIST_REMOVE(s->ops.notifies, op);
229
230         op->creq = dreplsrv_op_notify_send(op);
231         if (!op->creq) {
232                 dreplsrv_notify_op_callback(op);
233                 return;
234         }
235
236         op->creq->async.fn              = dreplsrv_notify_op_callback_creq;
237         op->creq->async.private_data    = op;
238 }
239
240
241 /*
242   find a source_dsa for a given guid
243  */
244 static struct dreplsrv_partition_source_dsa *dreplsrv_find_source_dsa(struct dreplsrv_partition *p,
245                                                                       struct GUID *guid)
246 {
247         struct dreplsrv_partition_source_dsa *s;
248
249         for (s=p->sources; s; s=s->next) {
250                 if (GUID_compare(&s->repsFrom1->source_dsa_obj_guid, guid) == 0) {
251                         return s;
252                 }
253         }
254         return NULL;
255 }
256
257
258 /*
259   schedule a replicaSync message
260  */
261 static WERROR dreplsrv_schedule_notify_sync(struct dreplsrv_service *service,
262                                             struct dreplsrv_partition *p,
263                                             struct repsFromToBlob *reps,
264                                             TALLOC_CTX *mem_ctx,
265                                             uint64_t uSN)
266 {
267         struct dreplsrv_notify_operation *op;
268         struct dreplsrv_partition_source_dsa *s;
269
270         s = dreplsrv_find_source_dsa(p, &reps->ctr.ctr1.source_dsa_obj_guid);
271         if (s == NULL) {
272                 DEBUG(0,(__location__ ": Unable to find source_dsa for %s\n",
273                          GUID_string(mem_ctx, &reps->ctr.ctr1.source_dsa_obj_guid)));
274                 return WERR_DS_UNAVAILABLE;
275         }
276
277         op = talloc_zero(mem_ctx, struct dreplsrv_notify_operation);
278         W_ERROR_HAVE_NO_MEMORY(op);
279
280         op->service     = service;
281         op->source_dsa  = s;
282         op->uSN         = uSN;
283
284         DLIST_ADD_END(service->ops.notifies, op, struct dreplsrv_notify_operation *);
285         talloc_steal(service, op);
286         return WERR_OK;
287 }
288
289 /*
290   see if a partition has a hugher uSN than what is in the repsTo and
291   if so then send a DsReplicaSync
292  */
293 static WERROR dreplsrv_notify_check(struct dreplsrv_service *s, 
294                                     struct dreplsrv_partition *p,
295                                     TALLOC_CTX *mem_ctx)
296 {
297         uint32_t count=0;
298         struct repsFromToBlob *reps;
299         WERROR werr;
300         uint64_t uSN;
301         int ret, i;
302
303         werr = dsdb_loadreps(s->samdb, mem_ctx, p->dn, "repsTo", &reps, &count);
304         if (count == 0) {
305                 werr = dsdb_loadreps(s->samdb, mem_ctx, p->dn, "repsFrom", &reps, &count);
306         }
307         if (!W_ERROR_IS_OK(werr)) {
308                 DEBUG(0,(__location__ ": Failed to load repsTo for %s\n",
309                          ldb_dn_get_linearized(p->dn)));
310                 return werr;
311         }
312
313         /* loads the partition uSNHighest */
314         ret = dsdb_load_partition_usn(s->samdb, p->dn, &uSN);
315         if (ret != LDB_SUCCESS || uSN == 0) {
316                 /* nothing to do */
317                 return WERR_OK;
318         }
319
320         /* see if any of our partners need some of our objects */
321         for (i=0; i<count; i++) {
322                 struct dreplsrv_partition_source_dsa *sdsa;
323                 sdsa = dreplsrv_find_source_dsa(p, &reps[i].ctr.ctr1.source_dsa_obj_guid);
324                 if (sdsa == NULL) continue;
325                 if (sdsa->notify_uSN < uSN) {
326                         /* we need to tell this partner to replicate
327                            with us */
328                         werr = dreplsrv_schedule_notify_sync(s, p, &reps[i], mem_ctx, uSN);
329                         if (!W_ERROR_IS_OK(werr)) {
330                                 DEBUG(0,(__location__ ": Failed to setup notify to %s for %s\n",
331                                          reps[i].ctr.ctr1.other_info->dns_name,
332                                          ldb_dn_get_linearized(p->dn)));
333                                 return werr;
334                         }
335                 }
336         }
337
338         return WERR_OK;
339 }
340
341 /*
342   see if any of the partitions have changed, and if so then send a
343   DsReplicaSync to all the replica partners in the repsTo object
344  */
345 static WERROR dreplsrv_notify_check_all(struct dreplsrv_service *s, TALLOC_CTX *mem_ctx)
346 {
347         WERROR status;
348         struct dreplsrv_partition *p;
349
350         for (p = s->partitions; p; p = p->next) {
351                 status = dreplsrv_notify_check(s, p, mem_ctx);
352                 W_ERROR_NOT_OK_RETURN(status);
353         }
354
355         return WERR_OK;
356 }
357
358 static void dreplsrv_notify_run(struct dreplsrv_service *service);
359
360 static void dreplsrv_notify_handler_te(struct tevent_context *ev, struct tevent_timer *te,
361                                        struct timeval t, void *ptr)
362 {
363         struct dreplsrv_service *service = talloc_get_type(ptr, struct dreplsrv_service);
364         WERROR status;
365
366         service->notify.te = NULL;
367
368         dreplsrv_notify_run(service);
369
370         status = dreplsrv_notify_schedule(service, service->notify.interval);
371         if (!W_ERROR_IS_OK(status)) {
372                 task_server_terminate(service->task, win_errstr(status), false);
373                 return;
374         }
375 }
376
377 WERROR dreplsrv_notify_schedule(struct dreplsrv_service *service, uint32_t next_interval)
378 {
379         TALLOC_CTX *tmp_mem;
380         struct tevent_timer *new_te;
381         struct timeval next_time;
382
383         /* prevent looping */
384         if (next_interval == 0) next_interval = 1;
385
386         next_time = timeval_current_ofs(next_interval, 50);
387
388         if (service->notify.te) {
389                 /*
390                  * if the timestamp of the new event is higher,
391                  * as current next we don't need to reschedule
392                  */
393                 if (timeval_compare(&next_time, &service->notify.next_event) > 0) {
394                         return WERR_OK;
395                 }
396         }
397
398         /* reset the next scheduled timestamp */
399         service->notify.next_event = next_time;
400
401         new_te = event_add_timed(service->task->event_ctx, service,
402                                  service->notify.next_event,
403                                  dreplsrv_notify_handler_te, service);
404         W_ERROR_HAVE_NO_MEMORY(new_te);
405
406         tmp_mem = talloc_new(service);
407         DEBUG(2,("dreplsrv_notify_schedule(%u) %sscheduled for: %s\n",
408                 next_interval,
409                 (service->notify.te?"re":""),
410                 nt_time_string(tmp_mem, timeval_to_nttime(&next_time))));
411         talloc_free(tmp_mem);
412
413         talloc_free(service->notify.te);
414         service->notify.te = new_te;
415
416         return WERR_OK;
417 }
418
419 static void dreplsrv_notify_run(struct dreplsrv_service *service)
420 {
421         TALLOC_CTX *mem_ctx;
422
423         mem_ctx = talloc_new(service);
424         dreplsrv_notify_check_all(service, mem_ctx);
425         talloc_free(mem_ctx);
426
427         dreplsrv_run_pending_ops(service);
428         dreplsrv_notify_run_ops(service);
429 }