s4-drepl: don't setup a repsFrom from a DC that isn't a master for a NC
[kai/samba.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 "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 "param/param.h"
37
38 /*
39  * see if a repsFromToBlob is in a list
40  */
41 static bool reps_in_list(struct repsFromToBlob *r, struct repsFromToBlob *reps, uint32_t count)
42 {
43         uint32_t i;
44         for (i=0; i<count; i++) {
45                 if (strcmp(r->ctr.ctr1.other_info->dns_name, 
46                            reps[i].ctr.ctr1.other_info->dns_name) == 0 &&
47                     GUID_compare(&r->ctr.ctr1.source_dsa_obj_guid, 
48                                  &reps[i].ctr.ctr1.source_dsa_obj_guid) == 0) {
49                         return true;
50                 }
51         }
52         return false;
53 }
54
55 /*
56   make sure we only add repsFrom entries for DCs who are masters for
57   the partition
58  */
59 static bool check_MasterNC(struct kccsrv_partition *p, struct repsFromToBlob *r,
60                            struct ldb_result *res)
61 {
62         struct repsFromTo1 *r1;
63         r1 = &r->ctr.ctr1;
64         struct GUID invocation_id = r1->source_dsa_invocation_id;
65         int i, j;
66
67         for (i=0; i<res->count; i++) {
68                 struct ldb_message *msg = res->msgs[i];
69                 struct ldb_message_element *el;
70                 struct ldb_dn *dn;
71
72                 struct GUID id2 = samdb_result_guid(msg, "invocationID");
73                 if (!GUID_equal(&invocation_id, &id2)) {
74                         continue;
75                 }
76
77                 el = ldb_msg_find_element(msg, "hasMasterNCs");
78                 if (!el || el->num_values == 0) {
79                         continue;
80                 }
81                 for (j=0; j<el->num_values; j++) {
82                         dn = ldb_dn_from_ldb_val(p, p->service->samdb, &el->values[j]);
83                         if (!ldb_dn_validate(dn)) {
84                                 talloc_free(dn);
85                                 continue;
86                         }
87                         if (ldb_dn_compare(dn, p->dn) == 0) {
88                                 talloc_free(dn);
89                                 return true;
90                         }
91                         talloc_free(dn);
92                 }
93         }
94         return false;
95 }
96
97
98 /*
99  * add any missing repsFrom structures to our partitions
100  */
101 static NTSTATUS kccsrv_add_repsFrom(struct kccsrv_service *s, TALLOC_CTX *mem_ctx,
102                                     struct repsFromToBlob *reps, uint32_t count,
103                                     struct ldb_result *res)
104 {
105         struct kccsrv_partition *p;
106
107         /* update the repsFrom on all partitions */
108         for (p=s->partitions; p; p=p->next) {
109                 struct repsFromToBlob *old_reps;
110                 uint32_t old_count;
111                 WERROR werr;
112                 uint32_t i;
113                 bool modified = false;
114
115                 werr = dsdb_loadreps(s->samdb, mem_ctx, p->dn, "repsFrom", &old_reps, &old_count);
116                 if (!W_ERROR_IS_OK(werr)) {
117                         DEBUG(0,(__location__ ": Failed to load repsFrom from %s - %s\n", 
118                                  ldb_dn_get_linearized(p->dn), ldb_errstring(s->samdb)));
119                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
120                 }
121
122                 /* add any new ones */
123                 for (i=0; i<count; i++) {
124                         if (!reps_in_list(&reps[i], old_reps, old_count) &&
125                             check_MasterNC(p, &reps[i], res)) {
126                                 old_reps = talloc_realloc(mem_ctx, old_reps, struct repsFromToBlob, old_count+1);
127                                 NT_STATUS_HAVE_NO_MEMORY(old_reps);
128                                 old_reps[old_count] = reps[i];
129                                 old_count++;
130                                 modified = true;
131                         }
132                 }
133
134                 /* remove any stale ones */
135                 for (i=0; i<old_count; i++) {
136                         if (!reps_in_list(&old_reps[i], reps, count) ||
137                             !check_MasterNC(p, &old_reps[i], res)) {
138                                 memmove(&old_reps[i], &old_reps[i+1], (old_count-(i+1))*sizeof(old_reps[0]));
139                                 old_count--;
140                                 i--;
141                                 modified = true;
142                         }
143                 }
144                 
145                 if (modified) {
146                         werr = dsdb_savereps(s->samdb, mem_ctx, p->dn, "repsFrom", old_reps, old_count);
147                         if (!W_ERROR_IS_OK(werr)) {
148                                 DEBUG(0,(__location__ ": Failed to save repsFrom to %s - %s\n", 
149                                          ldb_dn_get_linearized(p->dn), ldb_errstring(s->samdb)));
150                                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
151                         }
152                 }
153         }
154
155         return NT_STATUS_OK;
156
157 }
158
159 /*
160   this is the core of our initial simple KCC
161   We just add a repsFrom entry for all DCs we find that have nTDSDSA
162   objects, except for ourselves
163  */
164 NTSTATUS kccsrv_simple_update(struct kccsrv_service *s, TALLOC_CTX *mem_ctx)
165 {
166         struct ldb_result *res;
167         unsigned int i;
168         int ret;
169         const char *attrs[] = { "objectGUID", "invocationID", "hasMasterNCs", NULL };
170         struct repsFromToBlob *reps = NULL;
171         uint32_t count = 0;
172         struct kcc_connection_list *ntds_conn, *dsa_conn;
173
174         ret = ldb_search(s->samdb, mem_ctx, &res, s->config_dn, LDB_SCOPE_SUBTREE, 
175                          attrs, "objectClass=nTDSDSA");
176         if (ret != LDB_SUCCESS) {
177                 DEBUG(0,(__location__ ": Failed nTDSDSA search - %s\n", ldb_errstring(s->samdb)));
178                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
179         }
180
181         /* get the current list of connections */
182         ntds_conn = kccsrv_find_connections(s, mem_ctx);
183
184         dsa_conn = talloc_zero(mem_ctx, struct kcc_connection_list);
185
186         for (i=0; i<res->count; i++) {
187                 struct repsFromTo1 *r1;
188                 struct GUID ntds_guid, invocation_id;
189
190                 ntds_guid = samdb_result_guid(res->msgs[i], "objectGUID");
191                 if (GUID_compare(&ntds_guid, &s->ntds_guid) == 0) {
192                         /* don't replicate with ourselves */
193                         continue;
194                 }
195
196                 invocation_id = samdb_result_guid(res->msgs[i], "invocationID");
197
198                 reps = talloc_realloc(mem_ctx, reps, struct repsFromToBlob, count+1);
199                 NT_STATUS_HAVE_NO_MEMORY(reps);
200
201                 ZERO_STRUCT(reps[count]);
202                 reps[count].version = 1;
203                 r1 = &reps[count].ctr.ctr1;
204
205                 r1->other_info               = talloc_zero(reps, struct repsFromTo1OtherInfo);
206                 r1->other_info->dns_name     = talloc_asprintf(r1->other_info, "%s._msdcs.%s",
207                                                                GUID_string(mem_ctx, &ntds_guid),
208                                                                lp_dnsdomain(s->task->lp_ctx));
209                 r1->source_dsa_obj_guid      = ntds_guid;
210                 r1->source_dsa_invocation_id = invocation_id;
211                 r1->replica_flags            = 
212                         DRSUAPI_DRS_WRIT_REP |
213                         DRSUAPI_DRS_INIT_SYNC |
214                         DRSUAPI_DRS_PER_SYNC;
215                 memset(r1->schedule, 0x11, sizeof(r1->schedule));
216
217                 dsa_conn->servers = talloc_realloc(dsa_conn, dsa_conn->servers,
218                                                   struct kcc_connection,
219                                                   dsa_conn->count + 1);
220                 NT_STATUS_HAVE_NO_MEMORY(dsa_conn->servers);
221                 dsa_conn->servers[dsa_conn->count].dsa_guid = r1->source_dsa_obj_guid;
222                 dsa_conn->count++;
223
224                 count++;
225         }
226
227         kccsrv_apply_connections(s, ntds_conn, dsa_conn);
228
229         return kccsrv_add_repsFrom(s, mem_ctx, reps, count, res);
230 }
231
232
233 static void kccsrv_periodic_run(struct kccsrv_service *service);
234
235 static void kccsrv_periodic_handler_te(struct tevent_context *ev, struct tevent_timer *te,
236                                          struct timeval t, void *ptr)
237 {
238         struct kccsrv_service *service = talloc_get_type(ptr, struct kccsrv_service);
239         WERROR status;
240
241         service->periodic.te = NULL;
242
243         kccsrv_periodic_run(service);
244
245         status = kccsrv_periodic_schedule(service, service->periodic.interval);
246         if (!W_ERROR_IS_OK(status)) {
247                 task_server_terminate(service->task, win_errstr(status), true);
248                 return;
249         }
250 }
251
252 WERROR kccsrv_periodic_schedule(struct kccsrv_service *service, uint32_t next_interval)
253 {
254         TALLOC_CTX *tmp_mem;
255         struct tevent_timer *new_te;
256         struct timeval next_time;
257
258         /* prevent looping */
259         if (next_interval == 0) next_interval = 1;
260
261         next_time = timeval_current_ofs(next_interval, 50);
262
263         if (service->periodic.te) {
264                 /*
265                  * if the timestamp of the new event is higher,
266                  * as current next we don't need to reschedule
267                  */
268                 if (timeval_compare(&next_time, &service->periodic.next_event) > 0) {
269                         return WERR_OK;
270                 }
271         }
272
273         /* reset the next scheduled timestamp */
274         service->periodic.next_event = next_time;
275
276         new_te = event_add_timed(service->task->event_ctx, service,
277                                  service->periodic.next_event,
278                                  kccsrv_periodic_handler_te, service);
279         W_ERROR_HAVE_NO_MEMORY(new_te);
280
281         tmp_mem = talloc_new(service);
282         DEBUG(2,("kccsrv_periodic_schedule(%u) %sscheduled for: %s\n",
283                 next_interval,
284                 (service->periodic.te?"re":""),
285                 nt_time_string(tmp_mem, timeval_to_nttime(&next_time))));
286         talloc_free(tmp_mem);
287
288         talloc_free(service->periodic.te);
289         service->periodic.te = new_te;
290
291         return WERR_OK;
292 }
293
294 static void kccsrv_periodic_run(struct kccsrv_service *service)
295 {
296         TALLOC_CTX *mem_ctx;
297         NTSTATUS status;
298
299         DEBUG(2,("kccsrv_periodic_run(): simple update\n"));
300
301         mem_ctx = talloc_new(service);
302         status = kccsrv_simple_update(service, mem_ctx);
303         if (!NT_STATUS_IS_OK(status)) {
304                 DEBUG(0,("kccsrv_simple_update failed - %s\n", nt_errstr(status)));
305         }
306
307         status = kccsrv_check_deleted(service, mem_ctx);
308         if (!NT_STATUS_IS_OK(status)) {
309                 DEBUG(0,("kccsrv_check_deleted failed - %s\n", nt_errstr(status)));
310         }
311         talloc_free(mem_ctx);
312 }