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