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