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