s4-kcc: if we are a GC, auto-add partial replicas
[nivanova/samba-autobuild/.git] / source4 / dsdb / kcc / kcc_periodic.c
1 /* 
2    Unix SMB/CIFS mplementation.
3    KCC service periodic handling
4    
5    Copyright (C) Andrew Tridgell 2009
6    based on repl service code
7     
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20    
21 */
22
23 #include "includes.h"
24 #include "lib/events/events.h"
25 #include "dsdb/samdb/samdb.h"
26 #include "auth/auth.h"
27 #include "smbd/service.h"
28 #include "lib/messaging/irpc.h"
29 #include "dsdb/kcc/kcc_connection.h"
30 #include "dsdb/kcc/kcc_service.h"
31 #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 "librpc/gen_ndr/ndr_irpc_c.h"
37 #include "param/param.h"
38 #include "dsdb/common/util.h"
39
40 /*
41  * see if two repsFromToBlob blobs are for the same source DSA
42  */
43 static bool kccsrv_same_source_dsa(struct repsFromToBlob *r1, struct repsFromToBlob *r2)
44 {
45         return GUID_compare(&r1->ctr.ctr1.source_dsa_obj_guid,
46                             &r2->ctr.ctr1.source_dsa_obj_guid) == 0;
47 }
48
49 /*
50  * see if a repsFromToBlob is in a list
51  */
52 static bool reps_in_list(struct repsFromToBlob *r, struct repsFromToBlob *reps, uint32_t count)
53 {
54         uint32_t i;
55         for (i=0; i<count; i++) {
56                 if (kccsrv_same_source_dsa(r, &reps[i])) {
57                         return true;
58                 }
59         }
60         return false;
61 }
62
63 /*
64   make sure we only add repsFrom entries for DCs who are masters for
65   the partition
66  */
67 static bool check_MasterNC(struct kccsrv_partition *p, struct repsFromToBlob *r,
68                            struct ldb_result *res)
69 {
70         struct repsFromTo1 *r1 = &r->ctr.ctr1;
71         struct GUID invocation_id = r1->source_dsa_invocation_id;
72         unsigned int i, j;
73
74         /* we are expecting only version 1 */
75         SMB_ASSERT(r->version == 1);
76
77         for (i=0; i<res->count; i++) {
78                 struct ldb_message *msg = res->msgs[i];
79                 struct ldb_message_element *el;
80                 struct ldb_dn *dn;
81
82                 struct GUID id2 = samdb_result_guid(msg, "invocationID");
83                 if (GUID_all_zero(&id2) ||
84                     !GUID_equal(&invocation_id, &id2)) {
85                         continue;
86                 }
87
88                 el = ldb_msg_find_element(msg, "msDS-hasMasterNCs");
89                 if (!el || el->num_values == 0) {
90                         el = ldb_msg_find_element(msg, "hasMasterNCs");
91                         if (!el || el->num_values == 0) {
92                                 continue;
93                         }
94                 }
95                 for (j=0; j<el->num_values; j++) {
96                         dn = ldb_dn_from_ldb_val(p, p->service->samdb, &el->values[j]);
97                         if (!ldb_dn_validate(dn)) {
98                                 talloc_free(dn);
99                                 continue;
100                         }
101                         if (ldb_dn_compare(dn, p->dn) == 0) {
102                                 talloc_free(dn);
103                                 DEBUG(5,("%s %s match on %s in %s\n",
104                                          r1->other_info->dns_name,
105                                          el->name,
106                                          ldb_dn_get_linearized(dn),
107                                          ldb_dn_get_linearized(msg->dn)));
108                                 return true;
109                         }
110                         talloc_free(dn);
111                 }
112         }
113         return false;
114 }
115
116 struct kccsrv_notify_drepl_server_state {
117         struct dreplsrv_refresh r;
118 };
119
120 static void kccsrv_notify_drepl_server_done(struct tevent_req *subreq);
121
122 /**
123  * Force dreplsrv to update its state as topology is changed
124  */
125 static void kccsrv_notify_drepl_server(struct kccsrv_service *s,
126                                        TALLOC_CTX *mem_ctx)
127 {
128         struct kccsrv_notify_drepl_server_state *state;
129         struct dcerpc_binding_handle *irpc_handle;
130         struct tevent_req *subreq;
131
132         state = talloc_zero(s, struct kccsrv_notify_drepl_server_state);
133         if (state == NULL) {
134                 return;
135         }
136
137         irpc_handle = irpc_binding_handle_by_name(state, s->task->msg_ctx,
138                                                   "dreplsrv", &ndr_table_irpc);
139         if (irpc_handle == NULL) {
140                 /* dreplsrv is not running yet */
141                 TALLOC_FREE(state);
142                 return;
143         }
144
145         subreq = dcerpc_dreplsrv_refresh_r_send(state, s->task->event_ctx,
146                                                 irpc_handle, &state->r);
147         if (subreq == NULL) {
148                 TALLOC_FREE(state);
149                 return;
150         }
151         tevent_req_set_callback(subreq, kccsrv_notify_drepl_server_done, state);
152 }
153
154 static void kccsrv_notify_drepl_server_done(struct tevent_req *subreq)
155 {
156         struct kccsrv_notify_drepl_server_state *state =
157                 tevent_req_callback_data(subreq,
158                 struct kccsrv_notify_drepl_server_state);
159         NTSTATUS status;
160
161         status = dcerpc_dreplsrv_refresh_r_recv(subreq, state);
162         TALLOC_FREE(subreq);
163
164         /* we don't care about errors */
165         TALLOC_FREE(state);
166 }
167
168 uint32_t kccsrv_replica_flags(struct kccsrv_service *s)
169 {
170         if (s->am_rodc) {
171                 return DRSUAPI_DRS_INIT_SYNC |
172                         DRSUAPI_DRS_PER_SYNC |
173                         DRSUAPI_DRS_ADD_REF |
174                         DRSUAPI_DRS_SPECIAL_SECRET_PROCESSING |
175                         DRSUAPI_DRS_GET_ALL_GROUP_MEMBERSHIP |
176                         DRSUAPI_DRS_NONGC_RO_REP;
177         }
178         return DRSUAPI_DRS_INIT_SYNC |
179                 DRSUAPI_DRS_PER_SYNC |
180                 DRSUAPI_DRS_ADD_REF |
181                 DRSUAPI_DRS_WRIT_REP;
182 }
183
184 /*
185  * add any missing repsFrom structures to our partitions
186  */
187 NTSTATUS kccsrv_add_repsFrom(struct kccsrv_service *s, TALLOC_CTX *mem_ctx,
188                             struct repsFromToBlob *reps, uint32_t count,
189                             struct ldb_result *res)
190 {
191         struct kccsrv_partition *p;
192         bool notify_dreplsrv = false;
193         uint32_t replica_flags = kccsrv_replica_flags(s);
194
195         /* update the repsFrom on all partitions */
196         for (p=s->partitions; p; p=p->next) {
197                 struct repsFromToBlob *our_reps;
198                 uint32_t our_count;
199                 WERROR werr;
200                 uint32_t i, j;
201                 bool modified = false;
202
203                 werr = dsdb_loadreps(s->samdb, mem_ctx, p->dn, "repsFrom", &our_reps, &our_count);
204                 if (!W_ERROR_IS_OK(werr)) {
205                         DEBUG(0,(__location__ ": Failed to load repsFrom from %s - %s\n", 
206                                  ldb_dn_get_linearized(p->dn), ldb_errstring(s->samdb)));
207                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
208                 }
209
210                 /* see if the entry already exists */
211                 for (i=0; i<count; i++) {
212                         for (j=0; j<our_count; j++) {
213                                 if (kccsrv_same_source_dsa(&reps[i], &our_reps[j])) {
214                                         /* we already have this one -
215                                            check the replica_flags are right */
216                                         if (replica_flags != our_reps[j].ctr.ctr1.replica_flags) {
217                                                 /* we need to update the old one with
218                                                  * the new flags
219                                                  */
220                                                 our_reps[j].ctr.ctr1.replica_flags = replica_flags;
221                                                 modified = true;
222                                         }
223                                         break;
224                                 }
225                         }
226                         if (j == our_count) {
227                                 /* we don't have the new one - add it
228                                  * if it is a master
229                                  */
230                                 if (res && !check_MasterNC(p, &reps[i], res)) {
231                                         /* its not a master, we don't
232                                            want to pull from it */
233                                         continue;
234                                 }
235                                 /* we need to add it to our repsFrom */
236                                 our_reps = talloc_realloc(mem_ctx, our_reps, struct repsFromToBlob, our_count+1);
237                                 NT_STATUS_HAVE_NO_MEMORY(our_reps);
238                                 our_reps[our_count] = reps[i];
239                                 our_reps[our_count].ctr.ctr1.replica_flags = replica_flags;
240                                 our_count++;
241                                 modified = true;
242                                 DEBUG(4,(__location__ ": Added repsFrom for %s\n",
243                                          reps[i].ctr.ctr1.other_info->dns_name));
244                         }
245                 }
246
247                 /* remove any stale ones */
248                 for (i=0; i<our_count; i++) {
249                         if (!reps_in_list(&our_reps[i], reps, count) ||
250                             (res && !check_MasterNC(p, &our_reps[i], res))) {
251                                 DEBUG(4,(__location__ ": Removed repsFrom for %s\n",
252                                          our_reps[i].ctr.ctr1.other_info->dns_name));
253                                 memmove(&our_reps[i], &our_reps[i+1], (our_count-(i+1))*sizeof(our_reps[0]));
254                                 our_count--;
255                                 i--;
256                                 modified = true;
257                         }
258                 }
259
260                 if (modified) {
261                         werr = dsdb_savereps(s->samdb, mem_ctx, p->dn, "repsFrom", our_reps, our_count);
262                         if (!W_ERROR_IS_OK(werr)) {
263                                 DEBUG(0,(__location__ ": Failed to save repsFrom to %s - %s\n", 
264                                          ldb_dn_get_linearized(p->dn), ldb_errstring(s->samdb)));
265                                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
266                         }
267                         /* dreplsrv should refresh its state */
268                         notify_dreplsrv = true;
269                 }
270
271                 /* remove stale repsTo entries */
272                 modified = false;
273                 werr = dsdb_loadreps(s->samdb, mem_ctx, p->dn, "repsTo", &our_reps, &our_count);
274                 if (!W_ERROR_IS_OK(werr)) {
275                         DEBUG(0,(__location__ ": Failed to load repsTo from %s - %s\n", 
276                                  ldb_dn_get_linearized(p->dn), ldb_errstring(s->samdb)));
277                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
278                 }
279
280                 /* remove any stale ones */
281                 for (i=0; i<our_count; i++) {
282                         if (!reps_in_list(&our_reps[i], reps, count)) {
283                                 DEBUG(4,(__location__ ": Removed repsTo for %s\n",
284                                          our_reps[i].ctr.ctr1.other_info->dns_name));
285                                 memmove(&our_reps[i], &our_reps[i+1], (our_count-(i+1))*sizeof(our_reps[0]));
286                                 our_count--;
287                                 i--;
288                                 modified = true;
289                         }
290                 }
291
292                 if (modified) {
293                         werr = dsdb_savereps(s->samdb, mem_ctx, p->dn, "repsTo", our_reps, our_count);
294                         if (!W_ERROR_IS_OK(werr)) {
295                                 DEBUG(0,(__location__ ": Failed to save repsTo to %s - %s\n", 
296                                          ldb_dn_get_linearized(p->dn), ldb_errstring(s->samdb)));
297                                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
298                         }
299                         /* dreplsrv should refresh its state */
300                         notify_dreplsrv = true;
301                 }
302         }
303
304         /* notify dreplsrv toplogy has changed */
305         if (notify_dreplsrv) {
306                 kccsrv_notify_drepl_server(s, mem_ctx);
307         }
308
309         return NT_STATUS_OK;
310
311 }
312
313
314 /*
315   form a unique list of DNs from a search result and a given set of attributes
316  */
317 static int kccsrv_dn_list(struct ldb_context *ldb, struct ldb_result *res,
318                           TALLOC_CTX *mem_ctx,
319                           const char **attrs,
320                           struct ldb_dn ***dn_list, int *dn_count)
321 {
322         int i;
323         struct ldb_dn **nc_list = NULL;
324         int nc_count = 0;
325
326         nc_list = talloc_array(mem_ctx, struct ldb_dn *, 0);
327         if (nc_list == NULL) {
328                 return LDB_ERR_OPERATIONS_ERROR;
329         }
330
331         /* gather up a list of all NCs in this forest */
332         for (i=0; i<res->count; i++) {
333                 struct ldb_message *msg = res->msgs[i];
334                 int j;
335                 for (j=0; attrs[j]; j++) {
336                         struct ldb_message_element *el;
337                         int k;
338
339                         el = ldb_msg_find_element(msg, attrs[j]);
340                         if (el == NULL) continue;
341                         for (k=0; k<el->num_values; k++) {
342                                 struct ldb_dn *dn;
343                                 dn = ldb_dn_from_ldb_val(nc_list, ldb, &el->values[k]);
344                                 if (dn != NULL) {
345                                         int l;
346                                         for (l=0; l<nc_count; l++) {
347                                                 if (ldb_dn_compare(nc_list[l], dn) == 0) break;
348                                         }
349                                         if (l < nc_count) continue;
350                                         nc_list = talloc_realloc(mem_ctx, nc_list, struct ldb_dn *, nc_count+1);
351                                         if (nc_list == NULL) {
352                                                 return LDB_ERR_OPERATIONS_ERROR;
353                                         }
354                                         nc_list[nc_count] = dn;
355                                         nc_count++;
356                                 }
357                         }
358                 }
359         }
360
361         (*dn_list) = nc_list;
362         (*dn_count) = nc_count;
363         return LDB_SUCCESS;
364 }
365
366
367 /*
368   look for any additional global catalog partitions that we should be
369   replicating (by looking for msDS-HasDomainNCs), and add them to our
370   hasPartialReplicaNCs NTDS attribute
371  */
372 static int kccsrv_gc_update(struct kccsrv_service *s, struct ldb_result *res)
373 {
374         int i;
375         struct ldb_dn **nc_list = NULL;
376         int nc_count = 0;
377         struct ldb_dn **our_nc_list = NULL;
378         int our_nc_count = 0;
379         const char *attrs1[] = { "msDS-hasMasterNCs", "hasMasterNCs", "msDS-HasDomainNCs", NULL };
380         const char *attrs2[] = { "msDS-hasMasterNCs", "hasMasterNCs", "msDS-HasDomainNCs", "hasPartialReplicaNCs", NULL };
381         int ret;
382         TALLOC_CTX *tmp_ctx = talloc_new(res);
383         struct ldb_result *res2;
384         struct ldb_message *msg;
385
386         /* get a complete list of NCs for the forest */
387         ret = kccsrv_dn_list(s->samdb, res, tmp_ctx, attrs1, &nc_list, &nc_count);
388         if (ret != LDB_SUCCESS) {
389                 DEBUG(1,("Failed to get NC list for GC update - %s\n", ldb_errstring(s->samdb)));
390                 talloc_free(tmp_ctx);
391                 return ret;
392         }
393
394         /* get a list of what NCs we are already replicating */
395         ret = dsdb_search_dn(s->samdb, tmp_ctx, &res2, samdb_ntds_settings_dn(s->samdb), attrs2, 0);
396         if (ret != LDB_SUCCESS) {
397                 DEBUG(1,("Failed to get our NC list attributes for GC update - %s\n", ldb_errstring(s->samdb)));
398                 talloc_free(tmp_ctx);
399                 return ret;
400         }
401
402         ret = kccsrv_dn_list(s->samdb, res2, tmp_ctx, attrs2, &our_nc_list, &our_nc_count);
403         if (ret != LDB_SUCCESS) {
404                 DEBUG(1,("Failed to get our NC list for GC update - %s\n", ldb_errstring(s->samdb)));
405                 talloc_free(tmp_ctx);
406                 return ret;
407         }
408
409         msg = ldb_msg_new(tmp_ctx);
410         if (msg == NULL) {
411                 talloc_free(tmp_ctx);
412                 return LDB_ERR_OPERATIONS_ERROR;
413         }
414         msg->dn = res2->msgs[0]->dn;
415
416         /* see if we are missing any */
417         for (i=0; i<nc_count; i++) {
418                 int j;
419                 for (j=0; j<our_nc_count; j++) {
420                         if (ldb_dn_compare(nc_list[i], our_nc_list[j]) == 0) break;
421                 }
422                 if (j == our_nc_count) {
423                         /* its a new one */
424                         ret = ldb_msg_add_string(msg, "hasPartialReplicaNCs",
425                                                  ldb_dn_get_extended_linearized(msg, nc_list[i], 1));
426                         if (ret != LDB_SUCCESS) {
427                                 talloc_free(tmp_ctx);
428                                 return ret;
429                         }
430
431                 }
432         }
433
434         if (msg->num_elements == 0) {
435                 /* none to add */
436                 talloc_free(tmp_ctx);
437                 return LDB_SUCCESS;
438         }
439         msg->elements[0].flags = LDB_FLAG_MOD_ADD;
440
441         ret = dsdb_modify(s->samdb, msg, 0);
442         if (ret != LDB_SUCCESS) {
443                 DEBUG(0,("Failed to add hasPartialReplicaNCs - %s\n",
444                          ldb_errstring(s->samdb)));
445         }
446
447         talloc_free(tmp_ctx);
448         return ret;
449 }
450
451
452 /*
453   this is the core of our initial simple KCC
454   We just add a repsFrom entry for all DCs we find that have nTDSDSA
455   objects, except for ourselves
456  */
457 NTSTATUS kccsrv_simple_update(struct kccsrv_service *s, TALLOC_CTX *mem_ctx)
458 {
459         struct ldb_result *res;
460         unsigned int i;
461         int ret;
462         const char *attrs[] = { "objectGUID", "invocationID", "msDS-hasMasterNCs", "hasMasterNCs", "msDS-HasDomainNCs", NULL };
463         struct repsFromToBlob *reps = NULL;
464         uint32_t count = 0;
465         struct kcc_connection_list *ntds_conn, *dsa_conn;
466
467         ret = dsdb_search(s->samdb, mem_ctx, &res, s->config_dn, LDB_SCOPE_SUBTREE,
468                           attrs, DSDB_SEARCH_SHOW_EXTENDED_DN, "objectClass=nTDSDSA");
469         if (ret != LDB_SUCCESS) {
470                 DEBUG(0,(__location__ ": Failed nTDSDSA search - %s\n", ldb_errstring(s->samdb)));
471                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
472         }
473
474         if (samdb_is_gc(s->samdb)) {
475                 kccsrv_gc_update(s, res);
476         }
477
478         /* get the current list of connections */
479         ntds_conn = kccsrv_find_connections(s, mem_ctx);
480
481         dsa_conn = talloc_zero(mem_ctx, struct kcc_connection_list);
482
483         for (i=0; i<res->count; i++) {
484                 struct repsFromTo1 *r1;
485                 struct GUID ntds_guid, invocation_id;
486
487                 ntds_guid = samdb_result_guid(res->msgs[i], "objectGUID");
488                 if (GUID_compare(&ntds_guid, &s->ntds_guid) == 0) {
489                         /* don't replicate with ourselves */
490                         continue;
491                 }
492
493                 invocation_id = samdb_result_guid(res->msgs[i], "invocationID");
494
495                 reps = talloc_realloc(mem_ctx, reps, struct repsFromToBlob, count+1);
496                 NT_STATUS_HAVE_NO_MEMORY(reps);
497
498                 ZERO_STRUCT(reps[count]);
499                 reps[count].version = 1;
500                 r1 = &reps[count].ctr.ctr1;
501
502                 r1->other_info               = talloc_zero(reps, struct repsFromTo1OtherInfo);
503                 r1->other_info->dns_name     = samdb_ntds_msdcs_dns_name(s->samdb, reps, &ntds_guid);
504                 r1->source_dsa_obj_guid      = ntds_guid;
505                 r1->source_dsa_invocation_id = invocation_id;
506                 r1->replica_flags = kccsrv_replica_flags(s);
507                 memset(r1->schedule, 0x11, sizeof(r1->schedule));
508
509                 dsa_conn->servers = talloc_realloc(dsa_conn, dsa_conn->servers,
510                                                   struct kcc_connection,
511                                                   dsa_conn->count + 1);
512                 NT_STATUS_HAVE_NO_MEMORY(dsa_conn->servers);
513                 dsa_conn->servers[dsa_conn->count].dsa_guid = r1->source_dsa_obj_guid;
514                 dsa_conn->count++;
515
516                 count++;
517         }
518
519         kccsrv_apply_connections(s, ntds_conn, dsa_conn);
520
521         return kccsrv_add_repsFrom(s, mem_ctx, reps, count, res);
522 }
523
524
525 static void kccsrv_periodic_run(struct kccsrv_service *service);
526
527 static void kccsrv_periodic_handler_te(struct tevent_context *ev, struct tevent_timer *te,
528                                          struct timeval t, void *ptr)
529 {
530         struct kccsrv_service *service = talloc_get_type(ptr, struct kccsrv_service);
531         WERROR status;
532
533         service->periodic.te = NULL;
534
535         kccsrv_periodic_run(service);
536
537         status = kccsrv_periodic_schedule(service, service->periodic.interval);
538         if (!W_ERROR_IS_OK(status)) {
539                 task_server_terminate(service->task, win_errstr(status), true);
540                 return;
541         }
542 }
543
544 WERROR kccsrv_periodic_schedule(struct kccsrv_service *service, uint32_t next_interval)
545 {
546         TALLOC_CTX *tmp_mem;
547         struct tevent_timer *new_te;
548         struct timeval next_time;
549
550         /* prevent looping */
551         if (next_interval == 0) next_interval = 1;
552
553         next_time = timeval_current_ofs(next_interval, 50);
554
555         if (service->periodic.te) {
556                 /*
557                  * if the timestamp of the new event is higher,
558                  * as current next we don't need to reschedule
559                  */
560                 if (timeval_compare(&next_time, &service->periodic.next_event) > 0) {
561                         return WERR_OK;
562                 }
563         }
564
565         /* reset the next scheduled timestamp */
566         service->periodic.next_event = next_time;
567
568         new_te = tevent_add_timer(service->task->event_ctx, service,
569                                  service->periodic.next_event,
570                                  kccsrv_periodic_handler_te, service);
571         W_ERROR_HAVE_NO_MEMORY(new_te);
572
573         tmp_mem = talloc_new(service);
574         DEBUG(4,("kccsrv_periodic_schedule(%u) %sscheduled for: %s\n",
575                 next_interval,
576                 (service->periodic.te?"re":""),
577                 nt_time_string(tmp_mem, timeval_to_nttime(&next_time))));
578         talloc_free(tmp_mem);
579
580         talloc_free(service->periodic.te);
581         service->periodic.te = new_te;
582
583         return WERR_OK;
584 }
585
586 static void kccsrv_periodic_run(struct kccsrv_service *service)
587 {
588         TALLOC_CTX *mem_ctx;
589         NTSTATUS status;
590
591         DEBUG(4,("kccsrv_periodic_run(): simple update\n"));
592
593         mem_ctx = talloc_new(service);
594         status = kccsrv_simple_update(service, mem_ctx);
595         if (!NT_STATUS_IS_OK(status)) {
596                 DEBUG(0,("kccsrv_simple_update failed - %s\n", nt_errstr(status)));
597         }
598
599         status = kccsrv_check_deleted(service, mem_ctx);
600         if (!NT_STATUS_IS_OK(status)) {
601                 DEBUG(0,("kccsrv_check_deleted failed - %s\n", nt_errstr(status)));
602         }
603         talloc_free(mem_ctx);
604 }