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