3b800333edb9987cf281f5da58808b39b373c33c
[samba.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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "dlinklist.h"
25 #include "lib/events/events.h"
26 #include "lib/socket/socket.h"
27 #include "smbd/service_task.h"
28 #include "smbd/service_stream.h"
29 #include "lib/messaging/irpc.h"
30 #include "librpc/gen_ndr/ndr_winsrepl.h"
31 #include "wrepl_server/wrepl_server.h"
32 #include "nbt_server/wins/winsdb.h"
33 #include "ldb/include/ldb.h"
34 #include "ldb/include/ldb_errors.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 /*
43   open winsdb
44 */
45 static NTSTATUS wreplsrv_open_winsdb(struct wreplsrv_service *service)
46 {
47         service->wins_db     = winsdb_connect(service);
48         if (!service->wins_db) {
49                 return NT_STATUS_INTERNAL_DB_ERROR;
50         }
51
52         service->config.ldb = wins_config_db_connect(service);
53         if (!service->config.ldb) {
54                 return NT_STATUS_INTERNAL_DB_ERROR;
55         }
56
57         /* the default renew interval is 6 days */
58         service->config.renew_interval    = lp_parm_int(-1,"wreplsrv","renew_interval", 6*24*60*60);
59
60         /* the default tombstone (extinction) interval is 6 days */
61         service->config.tombstone_interval= lp_parm_int(-1,"wreplsrv","tombstone_interval", 6*24*60*60);
62
63         /* the default tombstone (extinction) timeout is 1 day */
64         service->config.tombstone_timeout = lp_parm_int(-1,"wreplsrv","tombstone_timeout", 1*24*60*60);
65
66         /* the default verify interval is 24 days */
67         service->config.verify_interval   = lp_parm_int(-1,"wreplsrv","verify_interval", 24*24*60*60);
68
69         /* the maximun interval to the next periodic processing event */
70         service->config.periodic_interval = lp_parm_int(-1,"wreplsrv","periodic_interval", 60);
71
72         return NT_STATUS_OK;
73 }
74
75 struct wreplsrv_partner *wreplsrv_find_partner(struct wreplsrv_service *service, const char *peer_addr)
76 {
77         struct wreplsrv_partner *cur;
78
79         for (cur = service->partners; cur; cur = cur->next) {
80                 if (strcmp(cur->address, peer_addr) == 0) {
81                         return cur;
82                 }
83         }
84
85         return NULL;
86 }
87
88 /*
89   load our replication partners
90 */
91 static NTSTATUS wreplsrv_load_partners(struct wreplsrv_service *service)
92 {
93         struct ldb_result *res = NULL;
94         int ret;
95         TALLOC_CTX *tmp_ctx = talloc_new(service);
96         int i;
97
98         /* find the record in the WINS database */
99         ret = ldb_search(service->config.ldb, ldb_dn_explode(tmp_ctx, "CN=PARTNERS"), LDB_SCOPE_SUBTREE,
100                          "(objectClass=wreplPartner)", NULL, &res);
101         if (ret != LDB_SUCCESS) goto failed;
102         talloc_steal(tmp_ctx, res);
103         if (res->count == 0) goto done;
104
105         for (i=0; i < res->count; i++) {
106                 struct wreplsrv_partner *partner;
107
108                 partner = talloc_zero(service, struct wreplsrv_partner);
109                 if (partner == NULL) goto failed;
110
111                 partner->service                = service;
112                 partner->address                = ldb_msg_find_string(res->msgs[i], "address", NULL);
113                 if (!partner->address) goto failed;
114                 partner->name                   = ldb_msg_find_string(res->msgs[i], "name", partner->address);
115                 partner->type                   = ldb_msg_find_uint(res->msgs[i], "type", WINSREPL_PARTNER_BOTH);
116                 partner->pull.interval          = ldb_msg_find_uint(res->msgs[i], "pullInterval",
117                                                                     WINSREPL_DEFAULT_PULL_INTERVAL);
118                 partner->pull.retry_interval    = ldb_msg_find_uint(res->msgs[i], "pullRetryInterval",
119                                                                     WINSREPL_DEFAULT_PULL_RETRY_INTERVAL);
120                 partner->our_address            = ldb_msg_find_string(res->msgs[i], "ourAddress", NULL);
121                 partner->push.change_count      = ldb_msg_find_uint(res->msgs[i], "pushChangeCount",
122                                                                     WINSREPL_DEFAULT_PUSH_CHANGE_COUNT);
123                 partner->push.use_inform        = ldb_msg_find_uint(res->msgs[i], "pushUseInform", False);
124
125                 talloc_steal(partner, partner->address);
126                 talloc_steal(partner, partner->name);
127                 talloc_steal(partner, partner->our_address);
128
129                 DLIST_ADD(service->partners, partner);
130         }
131 done:
132         talloc_free(tmp_ctx);
133         return NT_STATUS_OK;
134 failed:
135         talloc_free(tmp_ctx);
136         return NT_STATUS_FOOBAR;
137 }
138
139 BOOL wreplsrv_is_our_address(struct wreplsrv_service *service, const char *address)
140 {
141         const char *our_address;
142
143         if (lp_interfaces() && lp_bind_interfaces_only()) {
144                 int num_interfaces = iface_count();
145                 int i;
146                 for(i = 0; i < num_interfaces; i++) {
147                         our_address = iface_n_ip(i);
148                         if (strcasecmp(our_address, address) == 0) {
149                                 return True;
150                         }
151                 }
152         } else {
153                 our_address = lp_socket_address();
154                 if (strcasecmp(our_address, address) == 0) {
155                         return True;
156                 }
157         }
158
159         return False;
160 }
161
162 uint64_t wreplsrv_local_max_version(struct wreplsrv_service *service)
163 {
164         int ret;
165         struct ldb_context *ldb = service->wins_db;
166         struct ldb_dn *dn;
167         struct ldb_result *res = NULL;
168         TALLOC_CTX *tmp_ctx = talloc_new(service);
169         uint64_t maxVersion = 0;
170
171         dn = ldb_dn_explode(tmp_ctx, "CN=VERSION");
172         if (!dn) goto failed;
173
174         /* find the record in the WINS database */
175         ret = ldb_search(ldb, dn, LDB_SCOPE_BASE, 
176                          NULL, NULL, &res);
177         if (ret != LDB_SUCCESS) goto failed;
178         talloc_steal(tmp_ctx, res);
179         if (res->count > 1) goto failed;
180
181         if (res->count == 1) {
182                 maxVersion = ldb_msg_find_uint64(res->msgs[0], "maxVersion", 0);
183         }
184
185 failed:
186         talloc_free(tmp_ctx);
187         return maxVersion;
188 }
189
190 NTSTATUS wreplsrv_fill_wrepl_table(struct wreplsrv_service *service,
191                                    TALLOC_CTX *mem_ctx,
192                                    struct wrepl_table *table_out,
193                                    const char *our_ip,
194                                    const char *initiator,
195                                    BOOL full_table)
196 {
197         struct wreplsrv_owner *cur;
198         uint64_t local_max_version;
199         uint32_t i = 0;
200
201         table_out->partner_count        = 0;
202         table_out->partners             = NULL;
203         table_out->initiator            = initiator;
204
205         local_max_version = wreplsrv_local_max_version(service);
206         if (local_max_version > 0) {
207                 table_out->partner_count++;
208         }
209
210         for (cur = service->table; full_table && cur; cur = cur->next) {
211                 table_out->partner_count++;
212         }
213
214         table_out->partners = talloc_array(mem_ctx, struct wrepl_wins_owner, table_out->partner_count);
215         NT_STATUS_HAVE_NO_MEMORY(table_out->partners);
216
217         if (local_max_version > 0) {
218                 table_out->partners[i].address          = our_ip;
219                 table_out->partners[i].min_version      = 0;
220                 table_out->partners[i].max_version      = local_max_version;
221                 table_out->partners[i].type             = 1;
222                 i++;
223         }
224
225         for (cur = service->table; full_table && cur; cur = cur->next) {
226                 table_out->partners[i] = cur->owner;
227                 i++;
228         }
229
230         return NT_STATUS_OK;
231 }
232
233 struct wreplsrv_owner *wreplsrv_find_owner(struct wreplsrv_owner *table, const char *wins_owner)
234 {
235         struct wreplsrv_owner *cur;
236
237         for (cur = table; cur; cur = cur->next) {
238                 if (strcmp(cur->owner.address, wins_owner) == 0) {
239                         return cur;
240                 }
241         }
242
243         return NULL;
244 }
245
246 /*
247  update the wins_owner_table max_version, if the given version is the highest version
248  if no entry for the wins_owner exists yet, create one
249 */
250 NTSTATUS wreplsrv_add_table(struct wreplsrv_service *service,
251                             TALLOC_CTX *mem_ctx, struct wreplsrv_owner **_table,
252                             const char *wins_owner, uint64_t version)
253 {
254         struct wreplsrv_owner *table = *_table;
255         struct wreplsrv_owner *cur;
256
257         if (strcmp(WINSDB_OWNER_LOCAL, wins_owner) == 0) {
258                 return NT_STATUS_OK;
259         }
260
261         cur = wreplsrv_find_owner(table, wins_owner);
262
263         /* if it doesn't exists yet, create one */
264         if (!cur) {
265                 cur = talloc_zero(mem_ctx, struct wreplsrv_owner);
266                 NT_STATUS_HAVE_NO_MEMORY(cur);
267
268                 cur->owner.address      = talloc_strdup(cur, wins_owner);
269                 NT_STATUS_HAVE_NO_MEMORY(cur->owner.address);
270                 cur->owner.min_version  = 0;
271                 cur->owner.max_version  = 0;
272                 cur->owner.type         = 1; /* don't know why this is always 1 */
273
274                 cur->partner            = wreplsrv_find_partner(service, wins_owner);
275
276                 DLIST_ADD(table, cur);
277                 *_table = table;
278         }
279
280         /* the min_version is always 0 here, and won't be updated */
281
282         /* if the given version is higher the then current nax_version, update */
283         if (cur->owner.max_version < version) {
284                 cur->owner.max_version = version;
285         }
286
287         return NT_STATUS_OK;
288 }
289
290 /*
291   load the partner table
292 */
293 static NTSTATUS wreplsrv_load_table(struct wreplsrv_service *service)
294 {
295         struct ldb_result *res = NULL;
296         int ret;
297         NTSTATUS status;
298         TALLOC_CTX *tmp_ctx = talloc_new(service);
299         int i;
300         const char *wins_owner;
301         uint64_t version;
302         const char * const attrs[] = {
303                 "winsOwner",
304                 "versionID",
305                 NULL
306         };
307
308         /* find the record in the WINS database */
309         ret = ldb_search(service->wins_db, NULL, LDB_SCOPE_SUBTREE,
310                          "(objectClass=winsRecord)", attrs, &res);
311         status = NT_STATUS_INTERNAL_DB_CORRUPTION;
312         if (ret != LDB_SUCCESS) goto failed;
313         talloc_steal(tmp_ctx, res);
314         if (res->count == 0) goto done;
315
316         for (i=0; i < res->count; i++) {
317                 wins_owner     = ldb_msg_find_string(res->msgs[i], "winsOwner", NULL);
318                 version        = ldb_msg_find_uint64(res->msgs[i], "versionID", 0);
319
320                 if (wins_owner) { 
321                         status = wreplsrv_add_table(service,
322                                                     service, &service->table,
323                                                     wins_owner, version);
324                         if (!NT_STATUS_IS_OK(status)) goto failed;
325                 }
326                 talloc_free(res->msgs[i]);
327
328                 /* TODO: what's abut the per address owners? */
329         }
330 done:
331         talloc_free(tmp_ctx);
332         return NT_STATUS_OK;
333 failed:
334         talloc_free(tmp_ctx);
335         return status;
336 }
337
338 /*
339   setup our replication partners
340 */
341 static NTSTATUS wreplsrv_setup_partners(struct wreplsrv_service *service)
342 {
343         NTSTATUS status;
344
345         status = wreplsrv_load_partners(service);
346         NT_STATUS_NOT_OK_RETURN(status);
347
348         status = wreplsrv_load_table(service);
349         NT_STATUS_NOT_OK_RETURN(status);
350
351         return NT_STATUS_OK;
352 }
353
354 /*
355   startup the wrepl task
356 */
357 static void wreplsrv_task_init(struct task_server *task)
358 {
359         NTSTATUS status;
360         struct wreplsrv_service *service;
361
362         service = talloc_zero(task, struct wreplsrv_service);
363         if (!service) {
364                 task_server_terminate(task, "wreplsrv_task_init: out of memory");
365                 return;
366         }
367         service->task = task;
368         task->private = service;
369
370         /*
371          * setup up all partners, and open the winsdb
372          */
373         status = wreplsrv_open_winsdb(service);
374         if (!NT_STATUS_IS_OK(status)) {
375                 task_server_terminate(task, "wreplsrv_task_init: wreplsrv_open_winsdb() failed");
376                 return;
377         }
378
379         /*
380          * setup timed events for each partner we want to pull from
381          */
382         status = wreplsrv_setup_partners(service);
383         if (!NT_STATUS_IS_OK(status)) {
384                 task_server_terminate(task, "wreplsrv_task_init: wreplsrv_setup_partners() failed");
385                 return;
386         }
387
388         /* 
389          * setup listen sockets, so we can anwser requests from our partners,
390          * which pull from us
391          */
392         status = wreplsrv_setup_sockets(service);
393         if (!NT_STATUS_IS_OK(status)) {
394                 task_server_terminate(task, "wreplsrv_task_init: wreplsrv_setup_sockets() failed");
395                 return;
396         }
397
398         status = wreplsrv_setup_periodic(service);
399         if (!NT_STATUS_IS_OK(status)) {
400                 task_server_terminate(task, "wreplsrv_task_init: wreplsrv_setup_periodic() failed");
401                 return;
402         }
403
404         irpc_add_name(task->msg_ctx, "wrepl_server");
405 }
406
407 /*
408   initialise the WREPL server
409  */
410 static NTSTATUS wreplsrv_init(struct event_context *event_ctx, const struct model_ops *model_ops)
411 {
412         if (!lp_wins_support()) {
413                 return NT_STATUS_OK;
414         }
415
416         return task_server_startup(event_ctx, model_ops, wreplsrv_task_init);
417 }
418
419 /*
420   register ourselves as a available server
421 */
422 NTSTATUS server_service_wrepl_init(void)
423 {
424         return register_server_service("wrepl", wreplsrv_init);
425 }