55ab76c2289b63dd13c68e1af4eb31dbb6700ca9
[jelmer/samba4-debian.git] / source / wrepl_server / wrepl_server.c
1 /* 
2    Unix SMB/CIFS implementation.
3    
4    WINS Replication server
5    
6    Copyright (C) Stefan Metzmacher      2005
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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "lib/util/dlinklist.h"
25 #include "smbd/service_task.h"
26 #include "smbd/service.h"
27 #include "lib/messaging/irpc.h"
28 #include "librpc/gen_ndr/winsrepl.h"
29 #include "wrepl_server/wrepl_server.h"
30 #include "nbt_server/wins/winsdb.h"
31 #include "ldb/include/ldb.h"
32 #include "ldb/include/ldb_errors.h"
33 #include "auth/auth.h"
34 #include "db_wrap.h"
35
36 static struct ldb_context *wins_config_db_connect(TALLOC_CTX *mem_ctx)
37 {
38         return ldb_wrap_connect(mem_ctx, private_path(mem_ctx, lp_wins_config_url()),
39                                 system_session(mem_ctx), NULL, 0, NULL);
40 }
41
42 static uint64_t wins_config_db_get_seqnumber(struct ldb_context *ldb)
43 {
44         int ret;
45         struct ldb_dn *dn;
46         struct ldb_result *res = NULL;
47         TALLOC_CTX *tmp_ctx = talloc_new(ldb);
48         uint64_t seqnumber = 0;
49
50         dn = ldb_dn_explode(tmp_ctx, "@BASEINFO");
51         if (!dn) goto failed;
52
53         /* find the record in the WINS database */
54         ret = ldb_search(ldb, dn, LDB_SCOPE_BASE, 
55                          NULL, NULL, &res);
56         if (ret != LDB_SUCCESS) goto failed;
57         talloc_steal(tmp_ctx, res);
58         if (res->count > 1) goto failed;
59
60         if (res->count == 1) {
61                 seqnumber = ldb_msg_find_attr_as_uint64(res->msgs[0], "sequenceNumber", 0);
62         }
63
64 failed:
65         talloc_free(tmp_ctx);
66         return seqnumber;
67 }
68
69 /*
70   open winsdb
71 */
72 static NTSTATUS wreplsrv_open_winsdb(struct wreplsrv_service *service)
73 {
74         service->wins_db     = winsdb_connect(service, WINSDB_HANDLE_CALLER_WREPL);
75         if (!service->wins_db) {
76                 return NT_STATUS_INTERNAL_DB_ERROR;
77         }
78
79         service->config.ldb = wins_config_db_connect(service);
80         if (!service->config.ldb) {
81                 return NT_STATUS_INTERNAL_DB_ERROR;
82         }
83
84         /* the default renew interval is 6 days */
85         service->config.renew_interval    = lp_parm_int(-1,"wreplsrv","renew_interval", 6*24*60*60);
86
87         /* the default tombstone (extinction) interval is 6 days */
88         service->config.tombstone_interval= lp_parm_int(-1,"wreplsrv","tombstone_interval", 6*24*60*60);
89
90         /* the default tombstone (extinction) timeout is 1 day */
91         service->config.tombstone_timeout = lp_parm_int(-1,"wreplsrv","tombstone_timeout", 1*24*60*60);
92
93         /* the default tombstone extra timeout is 3 days */
94         service->config.tombstone_extra_timeout = lp_parm_int(-1,"wreplsrv","tombstone_extra_timeout", 3*24*60*60);
95
96         /* the default verify interval is 24 days */
97         service->config.verify_interval   = lp_parm_int(-1,"wreplsrv","verify_interval", 24*24*60*60);
98
99         /* the default scavenging interval is 'renew_interval/2' */
100         service->config.scavenging_interval=lp_parm_int(-1,"wreplsrv","scavenging_interval",
101                                                         service->config.renew_interval/2);
102
103         /* the maximun interval to the next periodic processing event */
104         service->config.periodic_interval = lp_parm_int(-1,"wreplsrv","periodic_interval", 15);
105
106         return NT_STATUS_OK;
107 }
108
109 struct wreplsrv_partner *wreplsrv_find_partner(struct wreplsrv_service *service, const char *peer_addr)
110 {
111         struct wreplsrv_partner *cur;
112
113         for (cur = service->partners; cur; cur = cur->next) {
114                 if (strcmp(cur->address, peer_addr) == 0) {
115                         return cur;
116                 }
117         }
118
119         return NULL;
120 }
121
122 /*
123   load our replication partners
124 */
125 NTSTATUS wreplsrv_load_partners(struct wreplsrv_service *service)
126 {
127         struct wreplsrv_partner *partner;
128         struct ldb_result *res = NULL;
129         int ret;
130         TALLOC_CTX *tmp_ctx = talloc_new(service);
131         int i;
132         uint64_t new_seqnumber;
133
134         new_seqnumber = wins_config_db_get_seqnumber(service->config.ldb);
135
136         /* if it's not the first run and nothing changed we're done */
137         if (service->config.seqnumber != 0 && service->config.seqnumber == new_seqnumber) {
138                 return NT_STATUS_OK;
139         }
140
141         service->config.seqnumber = new_seqnumber;
142
143         /* find the record in the WINS database */
144         ret = ldb_search(service->config.ldb, ldb_dn_explode(tmp_ctx, "CN=PARTNERS"), LDB_SCOPE_SUBTREE,
145                          "(objectClass=wreplPartner)", NULL, &res);
146         if (ret != LDB_SUCCESS) goto failed;
147         talloc_steal(tmp_ctx, res);
148
149         /* first disable all existing partners */
150         for (partner=service->partners; partner; partner = partner->next) {
151                 partner->type = WINSREPL_PARTNER_NONE;
152         }
153
154         for (i=0; i < res->count; i++) {
155                 const char *address;
156
157                 address = ldb_msg_find_attr_as_string(res->msgs[i], "address", NULL);
158                 if (!address) {
159                         goto failed;
160                 }
161
162                 partner = wreplsrv_find_partner(service, address);
163                 if (partner) {
164                         if (partner->name != partner->address) {
165                                 talloc_free(discard_const(partner->name));
166                         }
167                         partner->name = NULL;
168                         talloc_free(discard_const(partner->our_address));
169                         partner->our_address = NULL;
170
171                         /* force rescheduling of pulling */
172                         partner->pull.next_run = timeval_zero();
173                 } else {
174                         partner = talloc_zero(service, struct wreplsrv_partner);
175                         if (partner == NULL) goto failed;
176
177                         partner->service = service;
178                         partner->address = address;
179                         talloc_steal(partner, partner->address);
180
181                         DLIST_ADD_END(service->partners, partner, struct wreplsrv_partner *);
182                 }
183
184                 partner->name                   = ldb_msg_find_attr_as_string(res->msgs[i], "name", partner->address);
185                 talloc_steal(partner, partner->name);
186                 partner->our_address            = ldb_msg_find_attr_as_string(res->msgs[i], "ourAddress", NULL);
187                 talloc_steal(partner, partner->our_address);
188
189                 partner->type                   = ldb_msg_find_attr_as_uint(res->msgs[i], "type", WINSREPL_PARTNER_BOTH);
190                 partner->pull.interval          = ldb_msg_find_attr_as_uint(res->msgs[i], "pullInterval",
191                                                                     WINSREPL_DEFAULT_PULL_INTERVAL);
192                 partner->pull.retry_interval    = ldb_msg_find_attr_as_uint(res->msgs[i], "pullRetryInterval",
193                                                                     WINSREPL_DEFAULT_PULL_RETRY_INTERVAL);
194                 partner->push.change_count      = ldb_msg_find_attr_as_uint(res->msgs[i], "pushChangeCount",
195                                                                     WINSREPL_DEFAULT_PUSH_CHANGE_COUNT);
196                 partner->push.use_inform        = ldb_msg_find_attr_as_uint(res->msgs[i], "pushUseInform", False);
197
198                 DEBUG(3,("wreplsrv_load_partners: found partner: %s type: 0x%X\n",
199                         partner->address, partner->type));
200         }
201
202         DEBUG(2,("wreplsrv_load_partners: %u partners found: wins_config_db seqnumber %llu\n",
203                 res->count, (unsigned long long)service->config.seqnumber));
204
205         talloc_free(tmp_ctx);
206         return NT_STATUS_OK;
207 failed:
208         talloc_free(tmp_ctx);
209         return NT_STATUS_FOOBAR;
210 }
211
212 NTSTATUS wreplsrv_fill_wrepl_table(struct wreplsrv_service *service,
213                                    TALLOC_CTX *mem_ctx,
214                                    struct wrepl_table *table_out,
215                                    const char *initiator,
216                                    BOOL full_table)
217 {
218         struct wreplsrv_owner *cur;
219         uint32_t i = 0;
220
221         table_out->partner_count        = 0;
222         table_out->partners             = NULL;
223         table_out->initiator            = initiator;
224
225         for (cur = service->table; cur; cur = cur->next) {
226                 if (full_table) {
227                         table_out->partner_count++;
228                         continue;
229                 }
230
231                 if (strcmp(initiator, cur->owner.address) != 0) continue;
232
233                 table_out->partner_count++;
234                 break;
235         }
236
237         table_out->partners = talloc_array(mem_ctx, struct wrepl_wins_owner, table_out->partner_count);
238         NT_STATUS_HAVE_NO_MEMORY(table_out->partners);
239
240         for (cur = service->table; cur && i < table_out->partner_count; cur = cur->next) {
241                 /*
242                  * if it's our local entry
243                  * update the max version
244                  */
245                 if (cur == service->owner) {
246                         cur->owner.max_version = winsdb_get_maxVersion(service->wins_db);
247                 }
248
249                 if (full_table) {
250                         table_out->partners[i] = cur->owner;
251                         i++;
252                         continue;
253                 }
254
255                 if (strcmp(initiator, cur->owner.address) != 0) continue;
256
257                 table_out->partners[i] = cur->owner;
258                 i++;
259                 break;
260         }
261
262         return NT_STATUS_OK;
263 }
264
265 struct wreplsrv_owner *wreplsrv_find_owner(struct wreplsrv_service *service,
266                                            struct wreplsrv_owner *table,
267                                            const char *wins_owner)
268 {
269         struct wreplsrv_owner *cur;
270
271         for (cur = table; cur; cur = cur->next) {
272                 if (strcmp(cur->owner.address, wins_owner) == 0) {
273                         /*
274                          * if it's our local entry
275                          * update the max version
276                          */
277                         if (cur == service->owner) {
278                                 cur->owner.max_version = winsdb_get_maxVersion(service->wins_db);
279                         }
280                         return cur;
281                 }
282         }
283
284         return NULL;
285 }
286
287 /*
288  update the wins_owner_table max_version, if the given version is the highest version
289  if no entry for the wins_owner exists yet, create one
290 */
291 NTSTATUS wreplsrv_add_table(struct wreplsrv_service *service,
292                             TALLOC_CTX *mem_ctx, struct wreplsrv_owner **_table,
293                             const char *wins_owner, uint64_t version)
294 {
295         struct wreplsrv_owner *table = *_table;
296         struct wreplsrv_owner *cur;
297
298         if (!wins_owner || strcmp(wins_owner, "0.0.0.0") == 0) {
299                 wins_owner = service->wins_db->local_owner;
300         }
301
302         cur = wreplsrv_find_owner(service, table, wins_owner);
303
304         /* if it doesn't exists yet, create one */
305         if (!cur) {
306                 cur = talloc_zero(mem_ctx, struct wreplsrv_owner);
307                 NT_STATUS_HAVE_NO_MEMORY(cur);
308
309                 cur->owner.address      = talloc_strdup(cur, wins_owner);
310                 NT_STATUS_HAVE_NO_MEMORY(cur->owner.address);
311                 cur->owner.min_version  = 0;
312                 cur->owner.max_version  = 0;
313                 cur->owner.type         = 1; /* don't know why this is always 1 */
314
315                 cur->partner            = wreplsrv_find_partner(service, wins_owner);
316
317                 DLIST_ADD_END(table, cur, struct wreplsrv_owner *);
318                 *_table = table;
319         }
320
321         /* the min_version is always 0 here, and won't be updated */
322
323         /* if the given version is higher than the current max_version, update */
324         if (cur->owner.max_version < version) {
325                 cur->owner.max_version = version;
326                 /* if it's for our local db, we need to update the wins.ldb too */
327                 if (cur == service->owner) {
328                         uint64_t ret;
329                         ret = winsdb_set_maxVersion(service->wins_db, cur->owner.max_version);
330                         if (ret != cur->owner.max_version) {
331                                 DEBUG(0,("winsdb_set_maxVersion(%llu) failed: %llu\n",
332                                          (unsigned long long)cur->owner.max_version, 
333                                          (unsigned long long)ret));
334                                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
335                         }
336                 }
337         }
338
339         return NT_STATUS_OK;
340 }
341
342 /*
343   load the partner table
344 */
345 static NTSTATUS wreplsrv_load_table(struct wreplsrv_service *service)
346 {
347         struct ldb_result *res = NULL;
348         int ret;
349         NTSTATUS status;
350         TALLOC_CTX *tmp_ctx = talloc_new(service);
351         struct ldb_context *ldb = service->wins_db->ldb;
352         int i;
353         struct wreplsrv_owner *local_owner;
354         const char *wins_owner;
355         uint64_t version;
356         const char * const attrs[] = {
357                 "winsOwner",
358                 "versionID",
359                 NULL
360         };
361
362         /*
363          * make sure we have our local entry in the list,
364          * but we set service->owner when we're done
365          * to avoid to many calls to wreplsrv_local_max_version()
366          */
367         status = wreplsrv_add_table(service,
368                                     service, &service->table,
369                                     service->wins_db->local_owner, 0);
370         if (!NT_STATUS_IS_OK(status)) goto failed;
371         local_owner = wreplsrv_find_owner(service, service->table, service->wins_db->local_owner);
372         if (!local_owner) {
373                 status = NT_STATUS_INTERNAL_ERROR;
374                 goto failed;
375         }
376
377         /* find the record in the WINS database */
378         ret = ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE,
379                          "(objectClass=winsRecord)", attrs, &res);
380         status = NT_STATUS_INTERNAL_DB_CORRUPTION;
381         if (ret != LDB_SUCCESS) goto failed;
382         talloc_steal(tmp_ctx, res);
383
384         for (i=0; i < res->count; i++) {
385                 wins_owner     = ldb_msg_find_attr_as_string(res->msgs[i], "winsOwner", NULL);
386                 version        = ldb_msg_find_attr_as_uint64(res->msgs[i], "versionID", 0);
387
388                 status = wreplsrv_add_table(service,
389                                             service, &service->table,
390                                             wins_owner, version);
391                 if (!NT_STATUS_IS_OK(status)) goto failed;
392                 talloc_free(res->msgs[i]);
393         }
394
395         /*
396          * this makes sure we call wreplsrv_local_max_version() before returning in
397          * wreplsrv_find_owner()
398          */
399         service->owner = local_owner;
400
401         /*
402          * this makes sure the maxVersion in the database is updated,
403          * with the highest version we found, if this is higher than the current stored one
404          */
405         status = wreplsrv_add_table(service,
406                                     service, &service->table,
407                                     service->wins_db->local_owner, local_owner->owner.max_version);
408         if (!NT_STATUS_IS_OK(status)) goto failed;
409
410         talloc_free(tmp_ctx);
411         return NT_STATUS_OK;
412 failed:
413         talloc_free(tmp_ctx);
414         return status;
415 }
416
417 /*
418   setup our replication partners
419 */
420 static NTSTATUS wreplsrv_setup_partners(struct wreplsrv_service *service)
421 {
422         NTSTATUS status;
423
424         status = wreplsrv_load_partners(service);
425         NT_STATUS_NOT_OK_RETURN(status);
426
427         status = wreplsrv_load_table(service);
428         NT_STATUS_NOT_OK_RETURN(status);
429
430         return NT_STATUS_OK;
431 }
432
433 /*
434   startup the wrepl task
435 */
436 static void wreplsrv_task_init(struct task_server *task)
437 {
438         NTSTATUS status;
439         struct wreplsrv_service *service;
440
441         task_server_set_title(task, "task[wreplsrv]");
442
443         service = talloc_zero(task, struct wreplsrv_service);
444         if (!service) {
445                 task_server_terminate(task, "wreplsrv_task_init: out of memory");
446                 return;
447         }
448         service->task           = task;
449         service->startup_time   = timeval_current();
450         task->private           = service;
451
452         /*
453          * setup up all partners, and open the winsdb
454          */
455         status = wreplsrv_open_winsdb(service);
456         if (!NT_STATUS_IS_OK(status)) {
457                 task_server_terminate(task, "wreplsrv_task_init: wreplsrv_open_winsdb() failed");
458                 return;
459         }
460
461         /*
462          * setup timed events for each partner we want to pull from
463          */
464         status = wreplsrv_setup_partners(service);
465         if (!NT_STATUS_IS_OK(status)) {
466                 task_server_terminate(task, "wreplsrv_task_init: wreplsrv_setup_partners() failed");
467                 return;
468         }
469
470         /* 
471          * setup listen sockets, so we can anwser requests from our partners,
472          * which pull from us
473          */
474         status = wreplsrv_setup_sockets(service);
475         if (!NT_STATUS_IS_OK(status)) {
476                 task_server_terminate(task, "wreplsrv_task_init: wreplsrv_setup_sockets() failed");
477                 return;
478         }
479
480         status = wreplsrv_setup_periodic(service);
481         if (!NT_STATUS_IS_OK(status)) {
482                 task_server_terminate(task, "wreplsrv_task_init: wreplsrv_setup_periodic() failed");
483                 return;
484         }
485
486         irpc_add_name(task->msg_ctx, "wrepl_server");
487 }
488
489 /*
490   initialise the WREPL server
491  */
492 static NTSTATUS wreplsrv_init(struct event_context *event_ctx, const struct model_ops *model_ops)
493 {
494         if (!lp_wins_support()) {
495                 return NT_STATUS_OK;
496         }
497
498         return task_server_startup(event_ctx, model_ops, wreplsrv_task_init);
499 }
500
501 /*
502   register ourselves as a available server
503 */
504 NTSTATUS server_service_wrepl_init(void)
505 {
506         return register_server_service("wrepl", wreplsrv_init);
507 }