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