44a50ed6d3b3ce94bf8c5d84f7d9536f9f912631
[gd/samba-autobuild/.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 "dsdb/repl/drepl_service.h"
30 #include "lib/ldb/include/ldb_errors.h"
31 #include "../lib/util/dlinklist.h"
32 #include "librpc/gen_ndr/ndr_misc.h"
33 #include "librpc/gen_ndr/ndr_drsuapi.h"
34 #include "librpc/gen_ndr/ndr_drsblobs.h"
35 #include "libcli/composite/composite.h"
36 #include "../lib/util/tevent_ntstatus.h"
37
38
39 struct dreplsrv_op_notify_state {
40         struct tevent_context *ev;
41         struct dreplsrv_notify_operation *op;
42         void *ndr_struct_ptr;
43 };
44
45 static void dreplsrv_op_notify_connect_done(struct tevent_req *subreq);
46
47 /*
48   start the ReplicaSync async call
49  */
50 static struct tevent_req *dreplsrv_op_notify_send(TALLOC_CTX *mem_ctx,
51                                                   struct tevent_context *ev,
52                                                   struct dreplsrv_notify_operation *op)
53 {
54         struct tevent_req *req;
55         struct dreplsrv_op_notify_state *state;
56         struct tevent_req *subreq;
57
58         req = tevent_req_create(mem_ctx, &state,
59                                 struct dreplsrv_op_notify_state);
60         if (req == NULL) {
61                 return NULL;
62         }
63         state->ev = ev;
64         state->op = op;
65
66         subreq = dreplsrv_out_drsuapi_send(state,
67                                            ev,
68                                            op->source_dsa->conn);
69         if (tevent_req_nomem(subreq, req)) {
70                 return tevent_req_post(req, ev);
71         }
72         tevent_req_set_callback(subreq, dreplsrv_op_notify_connect_done, req);
73
74         return req;
75 }
76
77 static void dreplsrv_op_notify_replica_sync_trigger(struct tevent_req *req);
78
79 static void dreplsrv_op_notify_connect_done(struct tevent_req *subreq)
80 {
81         struct tevent_req *req = tevent_req_callback_data(subreq,
82                                                           struct tevent_req);
83         NTSTATUS status;
84
85         status = dreplsrv_out_drsuapi_recv(subreq);
86         TALLOC_FREE(subreq);
87         if (tevent_req_nterror(req, status)) {
88                 return;
89         }
90
91         dreplsrv_op_notify_replica_sync_trigger(req);
92 }
93
94 static void dreplsrv_op_notify_replica_sync_done(struct tevent_req *subreq);
95
96 static void dreplsrv_op_notify_replica_sync_trigger(struct tevent_req *req)
97 {
98         struct dreplsrv_op_notify_state *state =
99                 tevent_req_data(req,
100                 struct dreplsrv_op_notify_state);
101         struct dreplsrv_partition *partition = state->op->source_dsa->partition;
102         struct dreplsrv_drsuapi_connection *drsuapi = state->op->source_dsa->conn->drsuapi;
103         struct drsuapi_DsReplicaSync *r;
104         struct tevent_req *subreq;
105
106         r = talloc_zero(state, struct drsuapi_DsReplicaSync);
107         if (tevent_req_nomem(r, req)) {
108                 return;
109         }
110         r->in.req = talloc_zero(r, union drsuapi_DsReplicaSyncRequest);
111         if (tevent_req_nomem(r, req)) {
112                 return;
113         }
114         r->in.bind_handle       = &drsuapi->bind_handle;
115         r->in.level = 1;
116         r->in.req->req1.naming_context = &partition->nc;
117         r->in.req->req1.source_dsa_guid = state->op->service->ntds_guid;
118         r->in.req->req1.options =
119                 DRSUAPI_DRS_ASYNC_OP |
120                 DRSUAPI_DRS_UPDATE_NOTIFICATION |
121                 DRSUAPI_DRS_WRIT_REP;
122
123         if (state->op->is_urgent) {
124                 r->in.req->req1.options |= DRSUAPI_DRS_SYNC_URGENT;
125         }
126
127         state->ndr_struct_ptr = r;
128
129         if (DEBUGLVL(10)) {
130                 NDR_PRINT_IN_DEBUG(drsuapi_DsReplicaSync, r);
131         }
132
133         subreq = dcerpc_drsuapi_DsReplicaSync_r_send(state,
134                                                      state->ev,
135                                                      drsuapi->drsuapi_handle,
136                                                      r);
137         if (tevent_req_nomem(subreq, req)) {
138                 return;
139         }
140         tevent_req_set_callback(subreq, dreplsrv_op_notify_replica_sync_done, req);
141 }
142
143 static void dreplsrv_op_notify_replica_sync_done(struct tevent_req *subreq)
144 {
145         struct tevent_req *req =
146                 tevent_req_callback_data(subreq,
147                 struct tevent_req);
148         struct dreplsrv_op_notify_state *state =
149                 tevent_req_data(req,
150                 struct dreplsrv_op_notify_state);
151         struct drsuapi_DsReplicaSync *r = talloc_get_type(state->ndr_struct_ptr,
152                                                           struct drsuapi_DsReplicaSync);
153         NTSTATUS status;
154
155         state->ndr_struct_ptr = NULL;
156
157         status = dcerpc_drsuapi_DsReplicaSync_r_recv(subreq, r);
158         TALLOC_FREE(subreq);
159         if (tevent_req_nterror(req, status)) {
160                 return;
161         }
162
163         if (!W_ERROR_IS_OK(r->out.result)) {
164                 status = werror_to_ntstatus(r->out.result);
165                 tevent_req_nterror(req, status);
166                 return;
167         }
168
169         tevent_req_done(req);
170 }
171
172 static NTSTATUS dreplsrv_op_notify_recv(struct tevent_req *req)
173 {
174         return tevent_req_simple_recv_ntstatus(req);
175 }
176
177 /*
178   called when a notify operation has completed
179  */
180 static void dreplsrv_notify_op_callback(struct tevent_req *subreq)
181 {
182         struct dreplsrv_notify_operation *op =
183                 tevent_req_callback_data(subreq,
184                 struct dreplsrv_notify_operation);
185         NTSTATUS status;
186         struct dreplsrv_service *s = op->service;
187         WERROR werr;
188
189         status = dreplsrv_op_notify_recv(subreq);
190         werr = ntstatus_to_werror(status);
191         TALLOC_FREE(subreq);
192         if (!NT_STATUS_IS_OK(status)) {
193                 DEBUG(4,("dreplsrv_notify: Failed to send DsReplicaSync to %s for %s - %s : %s\n",
194                          op->source_dsa->repsFrom1->other_info->dns_name,
195                          ldb_dn_get_linearized(op->source_dsa->partition->dn),
196                          nt_errstr(status), win_errstr(werr)));
197         } else {
198                 DEBUG(2,("dreplsrv_notify: DsReplicaSync OK for %s\n",
199                          op->source_dsa->repsFrom1->other_info->dns_name));
200                 op->source_dsa->notify_uSN = op->uSN;
201         }
202
203         drepl_reps_update(s, "repsTo", op->source_dsa->partition->dn,
204                           &op->source_dsa->repsFrom1->source_dsa_obj_guid,
205                           werr);
206
207         talloc_free(op);
208         s->ops.n_current = NULL;
209         dreplsrv_run_pending_ops(s);
210 }
211
212 /*
213   run any pending replica sync calls
214  */
215 void dreplsrv_notify_run_ops(struct dreplsrv_service *s)
216 {
217         struct dreplsrv_notify_operation *op;
218         struct tevent_req *subreq;
219
220         if (s->ops.n_current || s->ops.current) {
221                 /* if there's still one running, we're done */
222                 return;
223         }
224
225         if (!s->ops.notifies) {
226                 /* if there're no pending operations, we're done */
227                 return;
228         }
229
230         op = s->ops.notifies;
231         s->ops.n_current = op;
232         DLIST_REMOVE(s->ops.notifies, op);
233
234         subreq = dreplsrv_op_notify_send(op, s->task->event_ctx, op);
235         if (!subreq) {
236                 DEBUG(0,("dreplsrv_notify_run_ops: dreplsrv_op_notify_send[%s][%s] - no memory\n",
237                          op->source_dsa->repsFrom1->other_info->dns_name,
238                          ldb_dn_get_linearized(op->source_dsa->partition->dn)));
239                 return;
240         }
241         tevent_req_set_callback(subreq, dreplsrv_notify_op_callback, op);
242         DEBUG(4,("started DsReplicaSync for %s to %s\n",
243                  ldb_dn_get_linearized(op->source_dsa->partition->dn),
244                  op->source_dsa->repsFrom1->other_info->dns_name));
245 }
246
247
248 /*
249   find a source_dsa for a given guid
250  */
251 static struct dreplsrv_partition_source_dsa *dreplsrv_find_notify_dsa(struct dreplsrv_partition *p,
252                                                                       struct GUID *guid)
253 {
254         struct dreplsrv_partition_source_dsa *s;
255
256         /* first check the sources list */
257         for (s=p->sources; s; s=s->next) {
258                 if (GUID_compare(&s->repsFrom1->source_dsa_obj_guid, guid) == 0) {
259                         return s;
260                 }
261         }
262
263         /* then the notifies list */
264         for (s=p->notifies; s; s=s->next) {
265                 if (GUID_compare(&s->repsFrom1->source_dsa_obj_guid, guid) == 0) {
266                         return s;
267                 }
268         }
269         return NULL;
270 }
271
272
273 /*
274   schedule a replicaSync message
275  */
276 static WERROR dreplsrv_schedule_notify_sync(struct dreplsrv_service *service,
277                                             struct dreplsrv_partition *p,
278                                             struct repsFromToBlob *reps,
279                                             TALLOC_CTX *mem_ctx,
280                                             uint64_t uSN,
281                                             bool is_urgent,
282                                             uint32_t replica_flags)
283 {
284         struct dreplsrv_notify_operation *op;
285         struct dreplsrv_partition_source_dsa *s;
286
287         s = dreplsrv_find_notify_dsa(p, &reps->ctr.ctr1.source_dsa_obj_guid);
288         if (s == NULL) {
289                 DEBUG(0,(__location__ ": Unable to find source_dsa for %s\n",
290                          GUID_string(mem_ctx, &reps->ctr.ctr1.source_dsa_obj_guid)));
291                 return WERR_DS_UNAVAILABLE;
292         }
293
294         /* first try to find an existing notify operation */
295         for (op = service->ops.notifies; op; op = op->next) {
296                 if (op->source_dsa != s) {
297                         continue;
298                 }
299
300                 if (op->is_urgent != is_urgent) {
301                         continue;
302                 }
303
304                 if (op->replica_flags != replica_flags) {
305                         continue;
306                 }
307
308                 if (op->uSN < uSN) {
309                         op->uSN = uSN;
310                 }
311
312                 /* reuse the notify operation, as it's not yet started */
313                 return WERR_OK;
314         }
315
316         op = talloc_zero(mem_ctx, struct dreplsrv_notify_operation);
317         W_ERROR_HAVE_NO_MEMORY(op);
318
319         op->service       = service;
320         op->source_dsa    = s;
321         op->uSN           = uSN;
322         op->is_urgent     = is_urgent;
323         op->replica_flags = replica_flags;
324         op->schedule_time = time(NULL);
325
326         DLIST_ADD_END(service->ops.notifies, op, struct dreplsrv_notify_operation *);
327         talloc_steal(service, op);
328         return WERR_OK;
329 }
330
331 /*
332   see if a partition has a hugher uSN than what is in the repsTo and
333   if so then send a DsReplicaSync
334  */
335 static WERROR dreplsrv_notify_check(struct dreplsrv_service *s, 
336                                     struct dreplsrv_partition *p,
337                                     TALLOC_CTX *mem_ctx)
338 {
339         uint32_t count=0;
340         struct repsFromToBlob *reps;
341         WERROR werr;
342         uint64_t uSNHighest;
343         uint64_t uSNUrgent;
344         uint32_t i;
345         int ret;
346
347         werr = dsdb_loadreps(s->samdb, mem_ctx, p->dn, "repsTo", &reps, &count);
348         if (!W_ERROR_IS_OK(werr)) {
349                 DEBUG(0,(__location__ ": Failed to load repsTo for %s\n",
350                          ldb_dn_get_linearized(p->dn)));
351                 return werr;
352         }
353
354         /* loads the partition uSNHighest and uSNUrgent */
355         ret = dsdb_load_partition_usn(s->samdb, p->dn, &uSNHighest, &uSNUrgent);
356         if (ret != LDB_SUCCESS || uSNHighest == 0) {
357                 /* nothing to do */
358                 return WERR_OK;
359         }
360
361         /* see if any of our partners need some of our objects */
362         for (i=0; i<count; i++) {
363                 struct dreplsrv_partition_source_dsa *sdsa;
364                 uint32_t replica_flags;
365                 sdsa = dreplsrv_find_notify_dsa(p, &reps[i].ctr.ctr1.source_dsa_obj_guid);
366                 replica_flags = reps[i].ctr.ctr1.replica_flags;
367                 if (sdsa == NULL) continue;
368                 if (sdsa->notify_uSN < uSNHighest) {
369                         /* we need to tell this partner to replicate
370                            with us */
371                         bool is_urgent = sdsa->notify_uSN < uSNUrgent;
372
373                         /* check if urgent replication is needed */
374                         werr = dreplsrv_schedule_notify_sync(s, p, &reps[i], mem_ctx,
375                                                              uSNHighest, is_urgent, replica_flags);
376                         if (!W_ERROR_IS_OK(werr)) {
377                                 DEBUG(0,(__location__ ": Failed to setup notify to %s for %s\n",
378                                          reps[i].ctr.ctr1.other_info->dns_name,
379                                          ldb_dn_get_linearized(p->dn)));
380                                 return werr;
381                         }
382                         DEBUG(4,("queued DsReplicaSync for %s to %s (urgent=%s) uSN=%llu:%llu\n",
383                                  ldb_dn_get_linearized(p->dn),
384                                  reps[i].ctr.ctr1.other_info->dns_name,
385                                  is_urgent?"true":"false",
386                                  (unsigned long long)sdsa->notify_uSN,
387                                  (unsigned long long)uSNHighest));
388                 }
389         }
390
391         return WERR_OK;
392 }
393
394 /*
395   see if any of the partitions have changed, and if so then send a
396   DsReplicaSync to all the replica partners in the repsTo object
397  */
398 static WERROR dreplsrv_notify_check_all(struct dreplsrv_service *s, TALLOC_CTX *mem_ctx)
399 {
400         WERROR status;
401         struct dreplsrv_partition *p;
402
403         for (p = s->partitions; p; p = p->next) {
404                 status = dreplsrv_notify_check(s, p, mem_ctx);
405                 W_ERROR_NOT_OK_RETURN(status);
406         }
407
408         return WERR_OK;
409 }
410
411 static void dreplsrv_notify_run(struct dreplsrv_service *service);
412
413 static void dreplsrv_notify_handler_te(struct tevent_context *ev, struct tevent_timer *te,
414                                        struct timeval t, void *ptr)
415 {
416         struct dreplsrv_service *service = talloc_get_type(ptr, struct dreplsrv_service);
417         WERROR status;
418
419         service->notify.te = NULL;
420
421         dreplsrv_notify_run(service);
422
423         status = dreplsrv_notify_schedule(service, service->notify.interval);
424         if (!W_ERROR_IS_OK(status)) {
425                 task_server_terminate(service->task, win_errstr(status), false);
426                 return;
427         }
428 }
429
430 WERROR dreplsrv_notify_schedule(struct dreplsrv_service *service, uint32_t next_interval)
431 {
432         TALLOC_CTX *tmp_mem;
433         struct tevent_timer *new_te;
434         struct timeval next_time;
435
436         /* prevent looping */
437         if (next_interval == 0) next_interval = 1;
438
439         next_time = timeval_current_ofs(next_interval, 50);
440
441         if (service->notify.te) {
442                 /*
443                  * if the timestamp of the new event is higher,
444                  * as current next we don't need to reschedule
445                  */
446                 if (timeval_compare(&next_time, &service->notify.next_event) > 0) {
447                         return WERR_OK;
448                 }
449         }
450
451         /* reset the next scheduled timestamp */
452         service->notify.next_event = next_time;
453
454         new_te = event_add_timed(service->task->event_ctx, service,
455                                  service->notify.next_event,
456                                  dreplsrv_notify_handler_te, service);
457         W_ERROR_HAVE_NO_MEMORY(new_te);
458
459         tmp_mem = talloc_new(service);
460         DEBUG(4,("dreplsrv_notify_schedule(%u) %sscheduled for: %s\n",
461                 next_interval,
462                 (service->notify.te?"re":""),
463                 nt_time_string(tmp_mem, timeval_to_nttime(&next_time))));
464         talloc_free(tmp_mem);
465
466         talloc_free(service->notify.te);
467         service->notify.te = new_te;
468
469         return WERR_OK;
470 }
471
472 static void dreplsrv_notify_run(struct dreplsrv_service *service)
473 {
474         TALLOC_CTX *mem_ctx;
475
476         mem_ctx = talloc_new(service);
477         dreplsrv_notify_check_all(service, mem_ctx);
478         talloc_free(mem_ctx);
479
480         dreplsrv_run_pending_ops(service);
481 }