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