reparse: Tighten reparse point length check
[Samba.git] / source4 / wrepl_server / wrepl_server.c
blobc97b816b1a8997da94053e9fda972d1b6f27b5e3
1 /*
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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "../lib/util/dlinklist.h"
24 #include "samba/service_task.h"
25 #include "samba/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.h>
31 #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"
37 static struct ldb_context *wins_config_db_connect(TALLOC_CTX *mem_ctx,
38 struct tevent_context *ev_ctx,
39 struct loadparm_context *lp_ctx)
41 return ldb_wrap_connect(mem_ctx, ev_ctx, lp_ctx, lpcfg_private_path(mem_ctx,
42 lp_ctx, "wins_config.ldb"),
43 system_session(lp_ctx), NULL, 0);
46 static uint64_t wins_config_db_get_seqnumber(struct ldb_context *ldb)
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;
54 dn = ldb_dn_new(tmp_ctx, ldb, "@BASEINFO");
55 if (!dn) goto failed;
57 /* find the record in the WINS database */
58 ret = ldb_search(ldb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL);
59 if (ret != LDB_SUCCESS) goto failed;
60 if (res->count > 1) goto failed;
62 if (res->count == 1) {
63 seqnumber = ldb_msg_find_attr_as_uint64(res->msgs[0], "sequenceNumber", 0);
66 failed:
67 talloc_free(tmp_ctx);
68 return seqnumber;
72 open winsdb
74 static NTSTATUS wreplsrv_open_winsdb(struct wreplsrv_service *service,
75 struct loadparm_context *lp_ctx)
77 const char *owner = lpcfg_parm_string(lp_ctx, NULL, "winsdb", "local_owner");
79 if (owner == NULL) {
80 struct interface *ifaces;
81 load_interface_list(service, lp_ctx, &ifaces);
82 owner = iface_list_first_v4(ifaces);
85 service->wins_db = winsdb_connect(service, service->task->event_ctx, lp_ctx, owner, WINSDB_HANDLE_CALLER_WREPL);
86 if (!service->wins_db) {
87 return NT_STATUS_INTERNAL_DB_ERROR;
90 service->config.ldb = wins_config_db_connect(service, service->task->event_ctx, lp_ctx);
91 if (!service->config.ldb) {
92 return NT_STATUS_INTERNAL_DB_ERROR;
95 /* the default renew interval is 6 days */
96 service->config.renew_interval = lpcfg_parm_int(lp_ctx, NULL,"wreplsrv","renew_interval", 6*24*60*60);
98 /* the default tombstone (extinction) interval is 6 days */
99 service->config.tombstone_interval= lpcfg_parm_int(lp_ctx, NULL,"wreplsrv","tombstone_interval", 6*24*60*60);
101 /* the default tombstone (extinction) timeout is 1 day */
102 service->config.tombstone_timeout = lpcfg_parm_int(lp_ctx, NULL,"wreplsrv","tombstone_timeout", 1*24*60*60);
104 /* the default tombstone extra timeout is 3 days */
105 service->config.tombstone_extra_timeout = lpcfg_parm_int(lp_ctx, NULL,"wreplsrv","tombstone_extra_timeout", 3*24*60*60);
107 /* the default verify interval is 24 days */
108 service->config.verify_interval = lpcfg_parm_int(lp_ctx, NULL,"wreplsrv","verify_interval", 24*24*60*60);
110 /* the default scavenging interval is 'renew_interval/2' */
111 service->config.scavenging_interval=lpcfg_parm_int(lp_ctx, NULL,"wreplsrv","scavenging_interval",
112 service->config.renew_interval/2);
114 /* the maximum interval to the next periodic processing event */
115 service->config.periodic_interval = lpcfg_parm_int(lp_ctx, NULL,"wreplsrv","periodic_interval", 15);
117 return NT_STATUS_OK;
120 struct wreplsrv_partner *wreplsrv_find_partner(struct wreplsrv_service *service, const char *peer_addr)
122 struct wreplsrv_partner *cur;
124 for (cur = service->partners; cur; cur = cur->next) {
125 if (strcmp(cur->address, peer_addr) == 0) {
126 return cur;
130 return NULL;
133 static uint32_t wreplsrv_find_attr_as_uint32(const struct ldb_message *msg,
134 const char *attr_name,
135 uint32_t default_value)
137 const struct ldb_val *v = ldb_msg_find_ldb_val(msg, attr_name);
138 char buf[sizeof("-2147483648")] = {};
139 char *end = NULL;
140 uint32_t ret;
141 int base = 10;
143 if (!v || !v->data) {
144 return default_value;
147 if (v->length >= sizeof(buf)) {
148 return default_value;
151 memcpy(buf, v->data, v->length);
152 if (buf[0] == '0' && (buf[1] == 'x' || buf[1] == 'X')) {
153 base = 16;
156 errno = 0;
157 ret = strtoll(buf, &end, base);
158 if (errno == ERANGE || errno == EINVAL) {
159 errno = 0;
160 ret = strtoull(buf, &end, base);
161 if (errno == ERANGE || errno == EINVAL) {
162 return default_value;
165 if (end && end[0] != '\0') {
166 return default_value;
168 return ret;
172 load our replication partners
174 NTSTATUS wreplsrv_load_partners(struct wreplsrv_service *service)
176 struct wreplsrv_partner *partner;
177 struct ldb_result *res = NULL;
178 int ret;
179 TALLOC_CTX *tmp_ctx;
180 unsigned int i;
181 uint64_t new_seqnumber;
183 new_seqnumber = wins_config_db_get_seqnumber(service->config.ldb);
185 /* if it's not the first run and nothing changed we're done */
186 if (service->config.seqnumber != 0 && service->config.seqnumber == new_seqnumber) {
187 return NT_STATUS_OK;
190 tmp_ctx = talloc_new(service);
191 NT_STATUS_HAVE_NO_MEMORY(tmp_ctx);
193 service->config.seqnumber = new_seqnumber;
195 /* find the record in the WINS database */
196 ret = ldb_search(service->config.ldb, tmp_ctx, &res,
197 ldb_dn_new(tmp_ctx, service->config.ldb, "CN=PARTNERS"),
198 LDB_SCOPE_SUBTREE, NULL, "(objectClass=wreplPartner)");
199 if (ret != LDB_SUCCESS) goto failed;
201 /* first disable all existing partners */
202 for (partner=service->partners; partner; partner = partner->next) {
203 partner->type = WINSREPL_PARTNER_NONE;
206 for (i=0; i < res->count; i++) {
207 const char *address;
209 address = ldb_msg_find_attr_as_string(res->msgs[i], "address", NULL);
210 if (!address) {
211 goto failed;
214 partner = wreplsrv_find_partner(service, address);
215 if (partner) {
216 if (partner->name != partner->address) {
217 talloc_free(discard_const(partner->name));
219 partner->name = NULL;
220 talloc_free(discard_const(partner->our_address));
221 partner->our_address = NULL;
223 /* force rescheduling of pulling */
224 partner->pull.next_run = timeval_zero();
225 } else {
226 partner = talloc_zero(service, struct wreplsrv_partner);
227 if (partner == NULL) goto failed;
229 partner->service = service;
230 partner->address = address;
231 talloc_steal(partner, partner->address);
233 DLIST_ADD_END(service->partners, partner);
236 partner->name = ldb_msg_find_attr_as_string(res->msgs[i], "name", partner->address);
237 talloc_steal(partner, partner->name);
238 partner->our_address = ldb_msg_find_attr_as_string(res->msgs[i], "ourAddress", NULL);
239 talloc_steal(partner, partner->our_address);
241 partner->type = wreplsrv_find_attr_as_uint32(res->msgs[i], "type", WINSREPL_PARTNER_BOTH);
242 partner->pull.interval = ldb_msg_find_attr_as_uint(res->msgs[i], "pullInterval",
243 WINSREPL_DEFAULT_PULL_INTERVAL);
244 partner->pull.retry_interval = ldb_msg_find_attr_as_uint(res->msgs[i], "pullRetryInterval",
245 WINSREPL_DEFAULT_PULL_RETRY_INTERVAL);
246 partner->push.change_count = ldb_msg_find_attr_as_uint(res->msgs[i], "pushChangeCount",
247 WINSREPL_DEFAULT_PUSH_CHANGE_COUNT);
248 partner->push.use_inform = ldb_msg_find_attr_as_uint(res->msgs[i], "pushUseInform", true);
250 DEBUG(3,("wreplsrv_load_partners: found partner: %s type: 0x%X\n",
251 partner->address, partner->type));
254 DEBUG(2,("wreplsrv_load_partners: %u partners found: wins_config_db seqnumber %llu\n",
255 res->count, (unsigned long long)service->config.seqnumber));
257 talloc_free(tmp_ctx);
258 return NT_STATUS_OK;
259 failed:
260 talloc_free(tmp_ctx);
261 return NT_STATUS_FOOBAR;
264 NTSTATUS wreplsrv_fill_wrepl_table(struct wreplsrv_service *service,
265 TALLOC_CTX *mem_ctx,
266 struct wrepl_table *table_out,
267 const char *initiator,
268 bool full_table)
270 struct wreplsrv_owner *cur;
271 uint32_t i = 0;
273 table_out->partner_count = 0;
274 table_out->partners = NULL;
275 table_out->initiator = initiator;
277 for (cur = service->table; cur; cur = cur->next) {
278 if (full_table) {
279 table_out->partner_count++;
280 continue;
283 if (strcmp(initiator, cur->owner.address) != 0) continue;
285 table_out->partner_count++;
286 break;
289 table_out->partners = talloc_array(mem_ctx, struct wrepl_wins_owner, table_out->partner_count);
290 NT_STATUS_HAVE_NO_MEMORY(table_out->partners);
292 for (cur = service->table; cur && i < table_out->partner_count; cur = cur->next) {
294 * if it's our local entry
295 * update the max version
297 if (cur == service->owner) {
298 cur->owner.max_version = winsdb_get_maxVersion(service->wins_db);
301 if (full_table) {
302 table_out->partners[i] = cur->owner;
303 i++;
304 continue;
307 if (strcmp(initiator, cur->owner.address) != 0) continue;
309 table_out->partners[i] = cur->owner;
310 i++;
311 break;
314 return NT_STATUS_OK;
317 struct wreplsrv_owner *wreplsrv_find_owner(struct wreplsrv_service *service,
318 struct wreplsrv_owner *table,
319 const char *wins_owner)
321 struct wreplsrv_owner *cur;
323 for (cur = table; cur; cur = cur->next) {
324 if (strcmp(cur->owner.address, wins_owner) == 0) {
326 * if it's our local entry
327 * update the max version
329 if (cur == service->owner) {
330 cur->owner.max_version = winsdb_get_maxVersion(service->wins_db);
332 return cur;
336 return NULL;
340 update the wins_owner_table max_version, if the given version is the highest version
341 if no entry for the wins_owner exists yet, create one
343 NTSTATUS wreplsrv_add_table(struct wreplsrv_service *service,
344 TALLOC_CTX *mem_ctx, struct wreplsrv_owner **_table,
345 const char *wins_owner, uint64_t version)
347 struct wreplsrv_owner *table = *_table;
348 struct wreplsrv_owner *cur;
350 if (!wins_owner || strcmp(wins_owner, "0.0.0.0") == 0) {
351 wins_owner = service->wins_db->local_owner;
354 cur = wreplsrv_find_owner(service, table, wins_owner);
356 /* if it doesn't exists yet, create one */
357 if (!cur) {
358 cur = talloc_zero(mem_ctx, struct wreplsrv_owner);
359 NT_STATUS_HAVE_NO_MEMORY(cur);
361 cur->owner.address = talloc_strdup(cur, wins_owner);
362 NT_STATUS_HAVE_NO_MEMORY(cur->owner.address);
363 cur->owner.min_version = 0;
364 cur->owner.max_version = 0;
365 cur->owner.type = 1; /* don't know why this is always 1 */
367 cur->partner = wreplsrv_find_partner(service, wins_owner);
369 DLIST_ADD_END(table, cur);
370 *_table = table;
373 /* the min_version is always 0 here, and won't be updated */
375 /* if the given version is higher than the current max_version, update */
376 if (cur->owner.max_version < version) {
377 cur->owner.max_version = version;
378 /* if it's for our local db, we need to update the wins.ldb too */
379 if (cur == service->owner) {
380 uint64_t ret;
381 ret = winsdb_set_maxVersion(service->wins_db, cur->owner.max_version);
382 if (ret != cur->owner.max_version) {
383 DEBUG(0,("winsdb_set_maxVersion(%llu) failed: %llu\n",
384 (unsigned long long)cur->owner.max_version,
385 (unsigned long long)ret));
386 return NT_STATUS_INTERNAL_DB_CORRUPTION;
391 return NT_STATUS_OK;
395 load the partner table
397 static NTSTATUS wreplsrv_load_table(struct wreplsrv_service *service)
399 struct ldb_result *res = NULL;
400 int ret;
401 NTSTATUS status;
402 TALLOC_CTX *tmp_ctx = talloc_new(service);
403 struct ldb_context *ldb = service->wins_db->ldb;
404 unsigned int i;
405 struct wreplsrv_owner *local_owner;
406 const char *wins_owner;
407 uint64_t version;
408 const char * const attrs[] = {
409 "winsOwner",
410 "versionID",
411 NULL
415 * make sure we have our local entry in the list,
416 * but we set service->owner when we're done
417 * to avoid to many calls to wreplsrv_local_max_version()
419 status = wreplsrv_add_table(service,
420 service, &service->table,
421 service->wins_db->local_owner, 0);
422 if (!NT_STATUS_IS_OK(status)) goto failed;
423 local_owner = wreplsrv_find_owner(service, service->table, service->wins_db->local_owner);
424 if (!local_owner) {
425 status = NT_STATUS_INTERNAL_ERROR;
426 goto failed;
429 /* find the record in the WINS database */
430 ret = ldb_search(ldb, tmp_ctx, &res, NULL, LDB_SCOPE_SUBTREE,
431 attrs, "(objectClass=winsRecord)");
432 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
433 if (ret != LDB_SUCCESS) goto failed;
435 for (i=0; i < res->count; i++) {
436 wins_owner = ldb_msg_find_attr_as_string(res->msgs[i], "winsOwner", NULL);
437 version = ldb_msg_find_attr_as_uint64(res->msgs[i], "versionID", 0);
439 status = wreplsrv_add_table(service,
440 service, &service->table,
441 wins_owner, version);
442 if (!NT_STATUS_IS_OK(status)) goto failed;
443 talloc_free(res->msgs[i]);
447 * this makes sure we call wreplsrv_local_max_version() before returning in
448 * wreplsrv_find_owner()
450 service->owner = local_owner;
453 * this makes sure the maxVersion in the database is updated,
454 * with the highest version we found, if this is higher than the current stored one
456 status = wreplsrv_add_table(service,
457 service, &service->table,
458 service->wins_db->local_owner, local_owner->owner.max_version);
459 if (!NT_STATUS_IS_OK(status)) goto failed;
461 talloc_free(tmp_ctx);
462 return NT_STATUS_OK;
463 failed:
464 talloc_free(tmp_ctx);
465 return status;
469 setup our replication partners
471 static NTSTATUS wreplsrv_setup_partners(struct wreplsrv_service *service)
473 NTSTATUS status;
475 status = wreplsrv_load_partners(service);
476 NT_STATUS_NOT_OK_RETURN(status);
478 status = wreplsrv_load_table(service);
479 NT_STATUS_NOT_OK_RETURN(status);
481 return NT_STATUS_OK;
485 startup the wrepl task
487 static NTSTATUS wreplsrv_task_init(struct task_server *task)
489 NTSTATUS status;
490 struct wreplsrv_service *service;
492 if (!lpcfg_we_are_a_wins_server(task->lp_ctx)) {
493 return NT_STATUS_INVALID_DOMAIN_ROLE;
496 task_server_set_title(task, "task[wreplsrv]");
498 service = talloc_zero(task, struct wreplsrv_service);
499 if (!service) {
500 task_server_terminate(task, "wreplsrv_task_init: out of memory", true);
501 return NT_STATUS_NO_MEMORY;
503 service->task = task;
504 service->startup_time = timeval_current();
505 task->private_data = service;
508 * setup up all partners, and open the winsdb
510 status = wreplsrv_open_winsdb(service, task->lp_ctx);
511 if (!NT_STATUS_IS_OK(status)) {
512 task_server_terminate(task, "wreplsrv_task_init: wreplsrv_open_winsdb() failed", true);
513 return status;
517 * setup timed events for each partner we want to pull from
519 status = wreplsrv_setup_partners(service);
520 if (!NT_STATUS_IS_OK(status)) {
521 task_server_terminate(task, "wreplsrv_task_init: wreplsrv_setup_partners() failed", true);
522 return status;
526 * setup listen sockets, so we can answer requests from our partners,
527 * which pull from us
529 status = wreplsrv_setup_sockets(service, task->lp_ctx);
530 if (!NT_STATUS_IS_OK(status)) {
531 task_server_terminate(task, "wreplsrv_task_init: wreplsrv_setup_sockets() failed", true);
532 return status;
535 status = wreplsrv_setup_periodic(service);
536 if (!NT_STATUS_IS_OK(status)) {
537 task_server_terminate(task, "wreplsrv_task_init: wreplsrv_setup_periodic() failed", true);
538 return status;
541 irpc_add_name(task->msg_ctx, "wrepl_server");
543 return NT_STATUS_OK;
547 register ourselves as a available server
549 NTSTATUS server_service_wrepl_init(TALLOC_CTX *ctx)
551 static const struct service_details details = {
552 .inhibit_fork_on_accept = true,
553 .inhibit_pre_fork = true,
554 .task_init = wreplsrv_task_init,
555 .post_fork = NULL
557 return register_server_service(ctx, "wrepl", &details);