2 Unix SMB/CIFS implementation.
4 WINS Replication server
6 Copyright (C) Stefan Metzmacher 2005
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.
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.
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.
24 #include "dlinklist.h"
25 #include "smbd/service_task.h"
26 #include "lib/messaging/irpc.h"
27 #include "librpc/gen_ndr/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"
34 static struct ldb_context *wins_config_db_connect(TALLOC_CTX *mem_ctx)
36 return ldb_wrap_connect(mem_ctx, private_path(mem_ctx, lp_wins_config_url()),
37 system_session(mem_ctx), NULL, 0, NULL);
43 static NTSTATUS wreplsrv_open_winsdb(struct wreplsrv_service *service)
45 service->wins_db = winsdb_connect(service);
46 if (!service->wins_db) {
47 return NT_STATUS_INTERNAL_DB_ERROR;
50 service->config.ldb = wins_config_db_connect(service);
51 if (!service->config.ldb) {
52 return NT_STATUS_INTERNAL_DB_ERROR;
55 /* the default renew interval is 6 days */
56 service->config.renew_interval = lp_parm_int(-1,"wreplsrv","renew_interval", 6*24*60*60);
58 /* the default tombstone (extinction) interval is 6 days */
59 service->config.tombstone_interval= lp_parm_int(-1,"wreplsrv","tombstone_interval", 6*24*60*60);
61 /* the default tombstone (extinction) timeout is 1 day */
62 service->config.tombstone_timeout = lp_parm_int(-1,"wreplsrv","tombstone_timeout", 1*24*60*60);
64 /* the default tombstone extra timeout is 3 days */
65 service->config.tombstone_extra_timeout = lp_parm_int(-1,"wreplsrv","tombstone_extra_timeout", 3*24*60*60);
67 /* the default verify interval is 24 days */
68 service->config.verify_interval = lp_parm_int(-1,"wreplsrv","verify_interval", 24*24*60*60);
70 /* the default scavenging interval is 'renew_interval/2' */
71 service->config.scavenging_interval=lp_parm_int(-1,"wreplsrv","scavenging_interval",
72 service->config.renew_interval/2);
74 /* the maximun interval to the next periodic processing event */
75 service->config.periodic_interval = lp_parm_int(-1,"wreplsrv","periodic_interval", 60);
80 struct wreplsrv_partner *wreplsrv_find_partner(struct wreplsrv_service *service, const char *peer_addr)
82 struct wreplsrv_partner *cur;
84 for (cur = service->partners; cur; cur = cur->next) {
85 if (strcmp(cur->address, peer_addr) == 0) {
94 load our replication partners
96 static NTSTATUS wreplsrv_load_partners(struct wreplsrv_service *service)
98 struct ldb_result *res = NULL;
100 TALLOC_CTX *tmp_ctx = talloc_new(service);
103 /* find the record in the WINS database */
104 ret = ldb_search(service->config.ldb, ldb_dn_explode(tmp_ctx, "CN=PARTNERS"), LDB_SCOPE_SUBTREE,
105 "(objectClass=wreplPartner)", NULL, &res);
106 if (ret != LDB_SUCCESS) goto failed;
107 talloc_steal(tmp_ctx, res);
108 if (res->count == 0) goto done;
110 for (i=0; i < res->count; i++) {
111 struct wreplsrv_partner *partner;
113 partner = talloc_zero(service, struct wreplsrv_partner);
114 if (partner == NULL) goto failed;
116 partner->service = service;
117 partner->address = ldb_msg_find_string(res->msgs[i], "address", NULL);
118 if (!partner->address) goto failed;
119 partner->name = ldb_msg_find_string(res->msgs[i], "name", partner->address);
120 partner->type = ldb_msg_find_uint(res->msgs[i], "type", WINSREPL_PARTNER_BOTH);
121 partner->pull.interval = ldb_msg_find_uint(res->msgs[i], "pullInterval",
122 WINSREPL_DEFAULT_PULL_INTERVAL);
123 partner->pull.retry_interval = ldb_msg_find_uint(res->msgs[i], "pullRetryInterval",
124 WINSREPL_DEFAULT_PULL_RETRY_INTERVAL);
125 partner->our_address = ldb_msg_find_string(res->msgs[i], "ourAddress", NULL);
126 partner->push.change_count = ldb_msg_find_uint(res->msgs[i], "pushChangeCount",
127 WINSREPL_DEFAULT_PUSH_CHANGE_COUNT);
128 partner->push.use_inform = ldb_msg_find_uint(res->msgs[i], "pushUseInform", False);
130 talloc_steal(partner, partner->address);
131 talloc_steal(partner, partner->name);
132 talloc_steal(partner, partner->our_address);
134 DLIST_ADD(service->partners, partner);
136 DEBUG(3,("wreplsrv_load_partners: found partner: %s type: 0x%X\n",
137 partner->address, partner->type));
140 DEBUG(1,("wreplsrv_load_partners: %u partners found\n", res->count));
142 talloc_free(tmp_ctx);
145 talloc_free(tmp_ctx);
146 return NT_STATUS_FOOBAR;
149 BOOL wreplsrv_is_our_address(struct wreplsrv_service *service, const char *address)
151 const char *our_address;
153 if (lp_interfaces() && lp_bind_interfaces_only()) {
154 int num_interfaces = iface_count();
156 for(i = 0; i < num_interfaces; i++) {
157 our_address = iface_n_ip(i);
158 if (strcasecmp(our_address, address) == 0) {
163 our_address = lp_socket_address();
164 if (strcasecmp(our_address, address) == 0) {
172 uint64_t wreplsrv_local_max_version(struct wreplsrv_service *service)
175 struct ldb_context *ldb = service->wins_db->ldb;
177 struct ldb_result *res = NULL;
178 TALLOC_CTX *tmp_ctx = talloc_new(service);
179 uint64_t maxVersion = 0;
181 dn = ldb_dn_explode(tmp_ctx, "CN=VERSION");
182 if (!dn) goto failed;
184 /* find the record in the WINS database */
185 ret = ldb_search(ldb, dn, LDB_SCOPE_BASE,
187 if (ret != LDB_SUCCESS) goto failed;
188 talloc_steal(tmp_ctx, res);
189 if (res->count > 1) goto failed;
191 if (res->count == 1) {
192 maxVersion = ldb_msg_find_uint64(res->msgs[0], "maxVersion", 0);
196 talloc_free(tmp_ctx);
200 NTSTATUS wreplsrv_fill_wrepl_table(struct wreplsrv_service *service,
202 struct wrepl_table *table_out,
204 const char *initiator,
207 struct wreplsrv_owner *cur;
208 uint64_t local_max_version;
211 table_out->partner_count = 0;
212 table_out->partners = NULL;
213 table_out->initiator = initiator;
215 local_max_version = wreplsrv_local_max_version(service);
216 if (local_max_version > 0) {
217 table_out->partner_count++;
220 for (cur = service->table; full_table && cur; cur = cur->next) {
221 table_out->partner_count++;
224 table_out->partners = talloc_array(mem_ctx, struct wrepl_wins_owner, table_out->partner_count);
225 NT_STATUS_HAVE_NO_MEMORY(table_out->partners);
227 if (local_max_version > 0) {
228 table_out->partners[i].address = our_ip;
229 table_out->partners[i].min_version = 0;
230 table_out->partners[i].max_version = local_max_version;
231 table_out->partners[i].type = 1;
235 for (cur = service->table; full_table && cur; cur = cur->next) {
236 table_out->partners[i] = cur->owner;
243 struct wreplsrv_owner *wreplsrv_find_owner(struct wreplsrv_owner *table, const char *wins_owner)
245 struct wreplsrv_owner *cur;
247 for (cur = table; cur; cur = cur->next) {
248 if (strcmp(cur->owner.address, wins_owner) == 0) {
257 update the wins_owner_table max_version, if the given version is the highest version
258 if no entry for the wins_owner exists yet, create one
260 NTSTATUS wreplsrv_add_table(struct wreplsrv_service *service,
261 TALLOC_CTX *mem_ctx, struct wreplsrv_owner **_table,
262 const char *wins_owner, uint64_t version)
264 struct wreplsrv_owner *table = *_table;
265 struct wreplsrv_owner *cur;
267 if (strcmp(WINSDB_OWNER_LOCAL, wins_owner) == 0) {
271 cur = wreplsrv_find_owner(table, wins_owner);
273 /* if it doesn't exists yet, create one */
275 cur = talloc_zero(mem_ctx, struct wreplsrv_owner);
276 NT_STATUS_HAVE_NO_MEMORY(cur);
278 cur->owner.address = talloc_strdup(cur, wins_owner);
279 NT_STATUS_HAVE_NO_MEMORY(cur->owner.address);
280 cur->owner.min_version = 0;
281 cur->owner.max_version = 0;
282 cur->owner.type = 1; /* don't know why this is always 1 */
284 cur->partner = wreplsrv_find_partner(service, wins_owner);
286 DLIST_ADD(table, cur);
290 /* the min_version is always 0 here, and won't be updated */
292 /* if the given version is higher the then current nax_version, update */
293 if (cur->owner.max_version < version) {
294 cur->owner.max_version = version;
301 load the partner table
303 static NTSTATUS wreplsrv_load_table(struct wreplsrv_service *service)
305 struct ldb_result *res = NULL;
308 TALLOC_CTX *tmp_ctx = talloc_new(service);
309 struct ldb_context *ldb = service->wins_db->ldb;
311 const char *wins_owner;
313 const char * const attrs[] = {
319 /* find the record in the WINS database */
320 ret = ldb_search(ldb, NULL, LDB_SCOPE_SUBTREE,
321 "(objectClass=winsRecord)", attrs, &res);
322 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
323 if (ret != LDB_SUCCESS) goto failed;
324 talloc_steal(tmp_ctx, res);
325 if (res->count == 0) goto done;
327 for (i=0; i < res->count; i++) {
328 wins_owner = ldb_msg_find_string(res->msgs[i], "winsOwner", NULL);
329 version = ldb_msg_find_uint64(res->msgs[i], "versionID", 0);
332 status = wreplsrv_add_table(service,
333 service, &service->table,
334 wins_owner, version);
335 if (!NT_STATUS_IS_OK(status)) goto failed;
337 talloc_free(res->msgs[i]);
339 /* TODO: what's abut the per address owners? */
342 talloc_free(tmp_ctx);
345 talloc_free(tmp_ctx);
350 setup our replication partners
352 static NTSTATUS wreplsrv_setup_partners(struct wreplsrv_service *service)
356 status = wreplsrv_load_partners(service);
357 NT_STATUS_NOT_OK_RETURN(status);
359 status = wreplsrv_load_table(service);
360 NT_STATUS_NOT_OK_RETURN(status);
366 startup the wrepl task
368 static void wreplsrv_task_init(struct task_server *task)
371 struct wreplsrv_service *service;
373 service = talloc_zero(task, struct wreplsrv_service);
375 task_server_terminate(task, "wreplsrv_task_init: out of memory");
378 service->task = task;
379 service->startup_time = timeval_current();
380 task->private = service;
383 * setup up all partners, and open the winsdb
385 status = wreplsrv_open_winsdb(service);
386 if (!NT_STATUS_IS_OK(status)) {
387 task_server_terminate(task, "wreplsrv_task_init: wreplsrv_open_winsdb() failed");
392 * setup timed events for each partner we want to pull from
394 status = wreplsrv_setup_partners(service);
395 if (!NT_STATUS_IS_OK(status)) {
396 task_server_terminate(task, "wreplsrv_task_init: wreplsrv_setup_partners() failed");
401 * setup listen sockets, so we can anwser requests from our partners,
404 status = wreplsrv_setup_sockets(service);
405 if (!NT_STATUS_IS_OK(status)) {
406 task_server_terminate(task, "wreplsrv_task_init: wreplsrv_setup_sockets() failed");
410 status = wreplsrv_setup_periodic(service);
411 if (!NT_STATUS_IS_OK(status)) {
412 task_server_terminate(task, "wreplsrv_task_init: wreplsrv_setup_periodic() failed");
416 irpc_add_name(task->msg_ctx, "wrepl_server");
420 initialise the WREPL server
422 static NTSTATUS wreplsrv_init(struct event_context *event_ctx, const struct model_ops *model_ops)
424 if (!lp_wins_support()) {
428 return task_server_startup(event_ctx, model_ops, wreplsrv_task_init);
432 register ourselves as a available server
434 NTSTATUS server_service_wrepl_init(void)
436 return register_server_service("wrepl", wreplsrv_init);