r25026: Move param/param.h out of includes.h
[kai/samba-autobuild/.git] / source4 / 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, 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_new(tmp_ctx, ldb, "@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;
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         tmp_ctx = talloc_new(service);
142         NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
143
144         service->config.seqnumber = new_seqnumber;
145
146         /* find the record in the WINS database */
147         ret = ldb_search(service->config.ldb, ldb_dn_new(tmp_ctx, service->config.ldb, "CN=PARTNERS"), LDB_SCOPE_SUBTREE,
148                          "(objectClass=wreplPartner)", NULL, &res);
149         if (ret != LDB_SUCCESS) goto failed;
150         talloc_steal(tmp_ctx, res);
151
152         /* first disable all existing partners */
153         for (partner=service->partners; partner; partner = partner->next) {
154                 partner->type = WINSREPL_PARTNER_NONE;
155         }
156
157         for (i=0; i < res->count; i++) {
158                 const char *address;
159
160                 address = ldb_msg_find_attr_as_string(res->msgs[i], "address", NULL);
161                 if (!address) {
162                         goto failed;
163                 }
164
165                 partner = wreplsrv_find_partner(service, address);
166                 if (partner) {
167                         if (partner->name != partner->address) {
168                                 talloc_free(discard_const(partner->name));
169                         }
170                         partner->name = NULL;
171                         talloc_free(discard_const(partner->our_address));
172                         partner->our_address = NULL;
173
174                         /* force rescheduling of pulling */
175                         partner->pull.next_run = timeval_zero();
176                 } else {
177                         partner = talloc_zero(service, struct wreplsrv_partner);
178                         if (partner == NULL) goto failed;
179
180                         partner->service = service;
181                         partner->address = address;
182                         talloc_steal(partner, partner->address);
183
184                         DLIST_ADD_END(service->partners, partner, struct wreplsrv_partner *);
185                 }
186
187                 partner->name                   = ldb_msg_find_attr_as_string(res->msgs[i], "name", partner->address);
188                 talloc_steal(partner, partner->name);
189                 partner->our_address            = ldb_msg_find_attr_as_string(res->msgs[i], "ourAddress", NULL);
190                 talloc_steal(partner, partner->our_address);
191
192                 partner->type                   = ldb_msg_find_attr_as_uint(res->msgs[i], "type", WINSREPL_PARTNER_BOTH);
193                 partner->pull.interval          = ldb_msg_find_attr_as_uint(res->msgs[i], "pullInterval",
194                                                                     WINSREPL_DEFAULT_PULL_INTERVAL);
195                 partner->pull.retry_interval    = ldb_msg_find_attr_as_uint(res->msgs[i], "pullRetryInterval",
196                                                                     WINSREPL_DEFAULT_PULL_RETRY_INTERVAL);
197                 partner->push.change_count      = ldb_msg_find_attr_as_uint(res->msgs[i], "pushChangeCount",
198                                                                     WINSREPL_DEFAULT_PUSH_CHANGE_COUNT);
199                 partner->push.use_inform        = ldb_msg_find_attr_as_uint(res->msgs[i], "pushUseInform", False);
200
201                 DEBUG(3,("wreplsrv_load_partners: found partner: %s type: 0x%X\n",
202                         partner->address, partner->type));
203         }
204
205         DEBUG(2,("wreplsrv_load_partners: %u partners found: wins_config_db seqnumber %llu\n",
206                 res->count, (unsigned long long)service->config.seqnumber));
207
208         talloc_free(tmp_ctx);
209         return NT_STATUS_OK;
210 failed:
211         talloc_free(tmp_ctx);
212         return NT_STATUS_FOOBAR;
213 }
214
215 NTSTATUS wreplsrv_fill_wrepl_table(struct wreplsrv_service *service,
216                                    TALLOC_CTX *mem_ctx,
217                                    struct wrepl_table *table_out,
218                                    const char *initiator,
219                                    BOOL full_table)
220 {
221         struct wreplsrv_owner *cur;
222         uint32_t i = 0;
223
224         table_out->partner_count        = 0;
225         table_out->partners             = NULL;
226         table_out->initiator            = initiator;
227
228         for (cur = service->table; cur; cur = cur->next) {
229                 if (full_table) {
230                         table_out->partner_count++;
231                         continue;
232                 }
233
234                 if (strcmp(initiator, cur->owner.address) != 0) continue;
235
236                 table_out->partner_count++;
237                 break;
238         }
239
240         table_out->partners = talloc_array(mem_ctx, struct wrepl_wins_owner, table_out->partner_count);
241         NT_STATUS_HAVE_NO_MEMORY(table_out->partners);
242
243         for (cur = service->table; cur && i < table_out->partner_count; cur = cur->next) {
244                 /*
245                  * if it's our local entry
246                  * update the max version
247                  */
248                 if (cur == service->owner) {
249                         cur->owner.max_version = winsdb_get_maxVersion(service->wins_db);
250                 }
251
252                 if (full_table) {
253                         table_out->partners[i] = cur->owner;
254                         i++;
255                         continue;
256                 }
257
258                 if (strcmp(initiator, cur->owner.address) != 0) continue;
259
260                 table_out->partners[i] = cur->owner;
261                 i++;
262                 break;
263         }
264
265         return NT_STATUS_OK;
266 }
267
268 struct wreplsrv_owner *wreplsrv_find_owner(struct wreplsrv_service *service,
269                                            struct wreplsrv_owner *table,
270                                            const char *wins_owner)
271 {
272         struct wreplsrv_owner *cur;
273
274         for (cur = table; cur; cur = cur->next) {
275                 if (strcmp(cur->owner.address, wins_owner) == 0) {
276                         /*
277                          * if it's our local entry
278                          * update the max version
279                          */
280                         if (cur == service->owner) {
281                                 cur->owner.max_version = winsdb_get_maxVersion(service->wins_db);
282                         }
283                         return cur;
284                 }
285         }
286
287         return NULL;
288 }
289
290 /*
291  update the wins_owner_table max_version, if the given version is the highest version
292  if no entry for the wins_owner exists yet, create one
293 */
294 NTSTATUS wreplsrv_add_table(struct wreplsrv_service *service,
295                             TALLOC_CTX *mem_ctx, struct wreplsrv_owner **_table,
296                             const char *wins_owner, uint64_t version)
297 {
298         struct wreplsrv_owner *table = *_table;
299         struct wreplsrv_owner *cur;
300
301         if (!wins_owner || strcmp(wins_owner, "0.0.0.0") == 0) {
302                 wins_owner = service->wins_db->local_owner;
303         }
304
305         cur = wreplsrv_find_owner(service, table, wins_owner);
306
307         /* if it doesn't exists yet, create one */
308         if (!cur) {
309                 cur = talloc_zero(mem_ctx, struct wreplsrv_owner);
310                 NT_STATUS_HAVE_NO_MEMORY(cur);
311
312                 cur->owner.address      = talloc_strdup(cur, wins_owner);
313                 NT_STATUS_HAVE_NO_MEMORY(cur->owner.address);
314                 cur->owner.min_version  = 0;
315                 cur->owner.max_version  = 0;
316                 cur->owner.type         = 1; /* don't know why this is always 1 */
317
318                 cur->partner            = wreplsrv_find_partner(service, wins_owner);
319
320                 DLIST_ADD_END(table, cur, struct wreplsrv_owner *);
321                 *_table = table;
322         }
323
324         /* the min_version is always 0 here, and won't be updated */
325
326         /* if the given version is higher than the current max_version, update */
327         if (cur->owner.max_version < version) {
328                 cur->owner.max_version = version;
329                 /* if it's for our local db, we need to update the wins.ldb too */
330                 if (cur == service->owner) {
331                         uint64_t ret;
332                         ret = winsdb_set_maxVersion(service->wins_db, cur->owner.max_version);
333                         if (ret != cur->owner.max_version) {
334                                 DEBUG(0,("winsdb_set_maxVersion(%llu) failed: %llu\n",
335                                          (unsigned long long)cur->owner.max_version, 
336                                          (unsigned long long)ret));
337                                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
338                         }
339                 }
340         }
341
342         return NT_STATUS_OK;
343 }
344
345 /*
346   load the partner table
347 */
348 static NTSTATUS wreplsrv_load_table(struct wreplsrv_service *service)
349 {
350         struct ldb_result *res = NULL;
351         int ret;
352         NTSTATUS status;
353         TALLOC_CTX *tmp_ctx = talloc_new(service);
354         struct ldb_context *ldb = service->wins_db->ldb;
355         int i;
356         struct wreplsrv_owner *local_owner;
357         const char *wins_owner;
358         uint64_t version;
359         const char * const attrs[] = {
360                 "winsOwner",
361                 "versionID",
362                 NULL
363         };
364
365         /*
366          * make sure we have our local entry in the list,
367          * but we set service->owner when we're done
368          * to avoid to many calls to wreplsrv_local_max_version()
369          */
370         status = wreplsrv_add_table(service,
371                                     service, &service->table,
372                                     service->wins_db->local_owner, 0);
373         if (!NT_STATUS_IS_OK(status)) goto failed;
374         local_owner = wreplsrv_find_owner(service, service->table, service->wins_db->local_owner);
375         if (!local_owner) {
376                 status = NT_STATUS_INTERNAL_ERROR;
377                 goto failed;
378         }
379
380         /* find the record in the WINS database */
381         ret = ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE,
382                          "(objectClass=winsRecord)", attrs, &res);
383         status = NT_STATUS_INTERNAL_DB_CORRUPTION;
384         if (ret != LDB_SUCCESS) goto failed;
385         talloc_steal(tmp_ctx, res);
386
387         for (i=0; i < res->count; i++) {
388                 wins_owner     = ldb_msg_find_attr_as_string(res->msgs[i], "winsOwner", NULL);
389                 version        = ldb_msg_find_attr_as_uint64(res->msgs[i], "versionID", 0);
390
391                 status = wreplsrv_add_table(service,
392                                             service, &service->table,
393                                             wins_owner, version);
394                 if (!NT_STATUS_IS_OK(status)) goto failed;
395                 talloc_free(res->msgs[i]);
396         }
397
398         /*
399          * this makes sure we call wreplsrv_local_max_version() before returning in
400          * wreplsrv_find_owner()
401          */
402         service->owner = local_owner;
403
404         /*
405          * this makes sure the maxVersion in the database is updated,
406          * with the highest version we found, if this is higher than the current stored one
407          */
408         status = wreplsrv_add_table(service,
409                                     service, &service->table,
410                                     service->wins_db->local_owner, local_owner->owner.max_version);
411         if (!NT_STATUS_IS_OK(status)) goto failed;
412
413         talloc_free(tmp_ctx);
414         return NT_STATUS_OK;
415 failed:
416         talloc_free(tmp_ctx);
417         return status;
418 }
419
420 /*
421   setup our replication partners
422 */
423 static NTSTATUS wreplsrv_setup_partners(struct wreplsrv_service *service)
424 {
425         NTSTATUS status;
426
427         status = wreplsrv_load_partners(service);
428         NT_STATUS_NOT_OK_RETURN(status);
429
430         status = wreplsrv_load_table(service);
431         NT_STATUS_NOT_OK_RETURN(status);
432
433         return NT_STATUS_OK;
434 }
435
436 /*
437   startup the wrepl task
438 */
439 static void wreplsrv_task_init(struct task_server *task)
440 {
441         NTSTATUS status;
442         struct wreplsrv_service *service;
443
444         task_server_set_title(task, "task[wreplsrv]");
445
446         service = talloc_zero(task, struct wreplsrv_service);
447         if (!service) {
448                 task_server_terminate(task, "wreplsrv_task_init: out of memory");
449                 return;
450         }
451         service->task           = task;
452         service->startup_time   = timeval_current();
453         task->private           = service;
454
455         /*
456          * setup up all partners, and open the winsdb
457          */
458         status = wreplsrv_open_winsdb(service);
459         if (!NT_STATUS_IS_OK(status)) {
460                 task_server_terminate(task, "wreplsrv_task_init: wreplsrv_open_winsdb() failed");
461                 return;
462         }
463
464         /*
465          * setup timed events for each partner we want to pull from
466          */
467         status = wreplsrv_setup_partners(service);
468         if (!NT_STATUS_IS_OK(status)) {
469                 task_server_terminate(task, "wreplsrv_task_init: wreplsrv_setup_partners() failed");
470                 return;
471         }
472
473         /* 
474          * setup listen sockets, so we can anwser requests from our partners,
475          * which pull from us
476          */
477         status = wreplsrv_setup_sockets(service);
478         if (!NT_STATUS_IS_OK(status)) {
479                 task_server_terminate(task, "wreplsrv_task_init: wreplsrv_setup_sockets() failed");
480                 return;
481         }
482
483         status = wreplsrv_setup_periodic(service);
484         if (!NT_STATUS_IS_OK(status)) {
485                 task_server_terminate(task, "wreplsrv_task_init: wreplsrv_setup_periodic() failed");
486                 return;
487         }
488
489         irpc_add_name(task->msg_ctx, "wrepl_server");
490 }
491
492 /*
493   initialise the WREPL server
494  */
495 static NTSTATUS wreplsrv_init(struct event_context *event_ctx, const struct model_ops *model_ops)
496 {
497         if (!lp_wins_support()) {
498                 return NT_STATUS_OK;
499         }
500
501         return task_server_startup(event_ctx, model_ops, wreplsrv_task_init);
502 }
503
504 /*
505   register ourselves as a available server
506 */
507 NTSTATUS server_service_wrepl_init(void)
508 {
509         return register_server_service("wrepl", wreplsrv_init);
510 }