idl: switched to using the WSPP names for the 'neighbour' DRS options
[ira/wip.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         int 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 /*
57  * add any missing repsFrom structures to our partitions
58  */
59 static NTSTATUS kccsrv_add_repsFrom(struct kccsrv_service *s, TALLOC_CTX *mem_ctx,
60                                     struct repsFromToBlob *reps, uint32_t count)
61 {
62         struct kccsrv_partition *p;
63
64         /* update the repsFrom on all partitions */
65         for (p=s->partitions; p; p=p->next) {
66                 struct repsFromToBlob *old_reps;
67                 uint32_t old_count;
68                 WERROR werr;
69                 int i;
70                 bool modified = false;
71
72                 werr = dsdb_loadreps(s->samdb, mem_ctx, p->dn, "repsFrom", &old_reps, &old_count);
73                 if (!W_ERROR_IS_OK(werr)) {
74                         DEBUG(0,(__location__ ": Failed to load repsFrom from %s - %s\n", 
75                                  ldb_dn_get_linearized(p->dn), ldb_errstring(s->samdb)));
76                         return NT_STATUS_INTERNAL_DB_CORRUPTION;
77                 }
78
79                 /* add any new ones */
80                 for (i=0; i<count; i++) {
81                         if (!reps_in_list(&reps[i], old_reps, old_count)) {
82                                 old_reps = talloc_realloc(mem_ctx, old_reps, struct repsFromToBlob, old_count+1);
83                                 NT_STATUS_HAVE_NO_MEMORY(old_reps);
84                                 old_reps[old_count] = reps[i];
85                                 old_count++;
86                                 modified = true;
87                         }
88                 }
89
90                 /* remove any stale ones */
91                 for (i=0; i<old_count; i++) {
92                         if (!reps_in_list(&old_reps[i], reps, count)) {
93                                 memmove(&old_reps[i], &old_reps[i+1], (old_count-(i+1))*sizeof(old_reps[0]));
94                                 old_count--;
95                                 i--;
96                                 modified = true;
97                         }
98                 }
99                 
100                 if (modified) {
101                         werr = dsdb_savereps(s->samdb, mem_ctx, p->dn, "repsFrom", old_reps, old_count);
102                         if (!W_ERROR_IS_OK(werr)) {
103                                 DEBUG(0,(__location__ ": Failed to save repsFrom to %s - %s\n", 
104                                          ldb_dn_get_linearized(p->dn), ldb_errstring(s->samdb)));
105                                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
106                         }
107                 }
108         }
109
110         return NT_STATUS_OK;
111
112 }
113
114 /*
115   this is the core of our initial simple KCC
116   We just add a repsFrom entry for all DCs we find that have nTDSDSA
117   objects, except for ourselves
118  */
119 NTSTATUS kccsrv_simple_update(struct kccsrv_service *s, TALLOC_CTX *mem_ctx)
120 {
121         struct ldb_result *res;
122         int ret, i;
123         const char *attrs[] = { "objectGUID", "invocationID", NULL };
124         struct repsFromToBlob *reps = NULL;
125         uint32_t count = 0;
126         struct kcc_connection_list *ntds_conn, *dsa_conn;
127
128         ret = ldb_search(s->samdb, mem_ctx, &res, s->config_dn, LDB_SCOPE_SUBTREE, 
129                          attrs, "objectClass=nTDSDSA");
130         if (ret != LDB_SUCCESS) {
131                 DEBUG(0,(__location__ ": Failed nTDSDSA search - %s\n", ldb_errstring(s->samdb)));
132                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
133         }
134
135         /* get the current list of connections */
136         ntds_conn = kccsrv_find_connections(s, mem_ctx);
137
138         dsa_conn = talloc_zero(mem_ctx, struct kcc_connection_list);
139
140         for (i=0; i<res->count; i++) {
141                 struct repsFromTo1 *r1;
142                 struct GUID ntds_guid, invocation_id;
143
144                 ntds_guid = samdb_result_guid(res->msgs[i], "objectGUID");
145                 if (GUID_compare(&ntds_guid, &s->ntds_guid) == 0) {
146                         /* don't replicate with ourselves */
147                         continue;
148                 }
149
150                 invocation_id = samdb_result_guid(res->msgs[i], "invocationID");
151
152                 reps = talloc_realloc(mem_ctx, reps, struct repsFromToBlob, count+1);
153                 NT_STATUS_HAVE_NO_MEMORY(reps);
154
155                 ZERO_STRUCT(reps[count]);
156                 reps[count].version = 1;
157                 r1 = &reps[count].ctr.ctr1;
158
159                 r1->other_info               = talloc_zero(reps, struct repsFromTo1OtherInfo);
160                 r1->other_info->dns_name     = talloc_asprintf(r1->other_info, "%s._msdcs.%s",
161                                                                GUID_string(mem_ctx, &ntds_guid),
162                                                                lp_dnsdomain(s->task->lp_ctx));
163                 r1->source_dsa_obj_guid      = ntds_guid;
164                 r1->source_dsa_invocation_id = invocation_id;
165                 r1->replica_flags            = 
166                         DRSUAPI_DRS_WRIT_REP |
167                         DRSUAPI_DRS_INIT_SYNC |
168                         DRSUAPI_DRS_PER_SYNC;
169                 memset(r1->schedule, 0x11, sizeof(r1->schedule));
170
171                 dsa_conn->servers = talloc_realloc(dsa_conn, dsa_conn->servers,
172                                                   struct kcc_connection,
173                                                   dsa_conn->count + 1);
174                 NT_STATUS_HAVE_NO_MEMORY(dsa_conn->servers);
175                 dsa_conn->servers[dsa_conn->count].dsa_guid = r1->source_dsa_obj_guid;
176                 dsa_conn->count++;
177
178                 count++;
179         }
180
181         kccsrv_apply_connections(s, ntds_conn, dsa_conn);
182
183         return kccsrv_add_repsFrom(s, mem_ctx, reps, count);
184 }
185
186
187 static void kccsrv_periodic_run(struct kccsrv_service *service);
188
189 static void kccsrv_periodic_handler_te(struct tevent_context *ev, struct tevent_timer *te,
190                                          struct timeval t, void *ptr)
191 {
192         struct kccsrv_service *service = talloc_get_type(ptr, struct kccsrv_service);
193         WERROR status;
194
195         service->periodic.te = NULL;
196
197         kccsrv_periodic_run(service);
198
199         status = kccsrv_periodic_schedule(service, service->periodic.interval);
200         if (!W_ERROR_IS_OK(status)) {
201                 task_server_terminate(service->task, win_errstr(status), true);
202                 return;
203         }
204 }
205
206 WERROR kccsrv_periodic_schedule(struct kccsrv_service *service, uint32_t next_interval)
207 {
208         TALLOC_CTX *tmp_mem;
209         struct tevent_timer *new_te;
210         struct timeval next_time;
211
212         /* prevent looping */
213         if (next_interval == 0) next_interval = 1;
214
215         next_time = timeval_current_ofs(next_interval, 50);
216
217         if (service->periodic.te) {
218                 /*
219                  * if the timestamp of the new event is higher,
220                  * as current next we don't need to reschedule
221                  */
222                 if (timeval_compare(&next_time, &service->periodic.next_event) > 0) {
223                         return WERR_OK;
224                 }
225         }
226
227         /* reset the next scheduled timestamp */
228         service->periodic.next_event = next_time;
229
230         new_te = event_add_timed(service->task->event_ctx, service,
231                                  service->periodic.next_event,
232                                  kccsrv_periodic_handler_te, service);
233         W_ERROR_HAVE_NO_MEMORY(new_te);
234
235         tmp_mem = talloc_new(service);
236         DEBUG(2,("kccsrv_periodic_schedule(%u) %sscheduled for: %s\n",
237                 next_interval,
238                 (service->periodic.te?"re":""),
239                 nt_time_string(tmp_mem, timeval_to_nttime(&next_time))));
240         talloc_free(tmp_mem);
241
242         talloc_free(service->periodic.te);
243         service->periodic.te = new_te;
244
245         return WERR_OK;
246 }
247
248 static void kccsrv_periodic_run(struct kccsrv_service *service)
249 {
250         TALLOC_CTX *mem_ctx;
251         NTSTATUS status;
252
253         DEBUG(2,("kccsrv_periodic_run(): simple update\n"));
254
255         mem_ctx = talloc_new(service);
256         status = kccsrv_simple_update(service, mem_ctx);
257         if (!NT_STATUS_IS_OK(status)) {
258                 DEBUG(0,("kccsrv_simple_update failed - %s\n", nt_errstr(status)));
259         }
260
261         status = kccsrv_check_deleted(service, mem_ctx);
262         if (!NT_STATUS_IS_OK(status)) {
263                 DEBUG(0,("kccsrv_check_deleted failed - %s\n", nt_errstr(status)));
264         }
265         talloc_free(mem_ctx);
266 }