2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 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 "nbt_server/nbt_server.h"
25 #include "nbt_server/wins/winsdb.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "lib/ldb/include/ldb_errors.h"
29 #include "system/time.h"
32 return the new maxVersion and save it
34 static uint64_t winsdb_allocate_version(struct ldb_context *wins_db)
39 struct ldb_result *res = NULL;
40 struct ldb_message *msg = NULL;
41 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
42 uint64_t maxVersion = 0;
44 trans = ldb_transaction_start(wins_db);
45 if (trans != LDB_SUCCESS) goto failed;
47 dn = ldb_dn_explode(tmp_ctx, "CN=VERSION");
50 /* find the record in the WINS database */
51 ret = ldb_search(wins_db, dn, LDB_SCOPE_BASE, NULL, NULL, &res);
53 if (ret != LDB_SUCCESS) goto failed;
54 if (res->count > 1) goto failed;
56 talloc_steal(tmp_ctx, res);
58 if (res->count == 1) {
59 maxVersion = ldb_msg_find_uint64(res->msgs[0], "maxVersion", 0);
63 msg = ldb_msg_new(tmp_ctx);
64 if (!msg) goto failed;
68 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE);
69 if (ret != 0) goto failed;
70 ret = ldb_msg_add_string(msg, "objectClass", "winsMaxVersion");
71 if (ret != 0) goto failed;
72 ret = ldb_msg_add_empty(msg, "maxVersion", LDB_FLAG_MOD_REPLACE);
73 if (ret != 0) goto failed;
74 ret = ldb_msg_add_fmt(msg, "maxVersion", "%llu", maxVersion);
75 if (ret != 0) goto failed;
77 ret = ldb_modify(wins_db, msg);
78 if (ret != 0) ret = ldb_add(wins_db, msg);
79 if (ret != 0) goto failed;
81 trans = ldb_transaction_commit(wins_db);
82 if (trans != LDB_SUCCESS) goto failed;
88 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
94 return a DN for a nbt_name
96 static struct ldb_dn *winsdb_dn(TALLOC_CTX *mem_ctx, struct nbt_name *name)
100 dn = ldb_dn_string_compose(mem_ctx, NULL, "type=0x%02X", name->type);
101 if (dn && name->name && *name->name) {
102 dn = ldb_dn_string_compose(mem_ctx, dn, "name=%s", name->name);
104 if (dn && name->scope && *name->scope) {
105 dn = ldb_dn_string_compose(mem_ctx, dn, "scope=%s", name->scope);
110 static NTSTATUS winsdb_nbt_name(TALLOC_CTX *mem_ctx, struct ldb_dn *dn, struct nbt_name **_name)
113 struct nbt_name *name;
116 name = talloc(mem_ctx, struct nbt_name);
118 status = NT_STATUS_NO_MEMORY;
122 if (dn->comp_num > 3) {
123 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
127 if (dn->comp_num > cur && strcasecmp("scope", dn->components[cur].name) == 0) {
128 name->scope = talloc_steal(name, dn->components[cur].value.data);
134 if (dn->comp_num > cur && strcasecmp("name", dn->components[cur].name) == 0) {
135 name->name = talloc_steal(name, dn->components[cur].value.data);
138 name->name = talloc_strdup(name, "");
140 status = NT_STATUS_NO_MEMORY;
145 if (dn->comp_num > cur && strcasecmp("type", dn->components[cur].name) == 0) {
146 name->type = strtoul((char *)dn->components[cur].value.data, NULL, 0);
149 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
161 decode the winsdb_addr("address") attribute:
163 "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
166 static NTSTATUS winsdb_addr_decode(struct winsdb_record *rec, struct ldb_val *val,
167 TALLOC_CTX *mem_ctx, struct winsdb_addr **_addr)
170 struct winsdb_addr *addr;
176 addr = talloc(mem_ctx, struct winsdb_addr);
178 status = NT_STATUS_NO_MEMORY;
182 address = (char *)val->data;
184 p = strchr(address, ';');
186 /* support old entries, with only the address */
187 addr->address = talloc_steal(addr, val->data);
188 addr->wins_owner = talloc_reference(addr, rec->wins_owner);
189 if (!addr->wins_owner) {
190 status = NT_STATUS_NO_MEMORY;
193 addr->expire_time = rec->expire_time;
199 addr->address = talloc_strdup(addr, address);
200 if (!addr->address) {
201 status = NT_STATUS_NO_MEMORY;
205 if (strncmp("winsOwner:", p, 10) != 0) {
206 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
210 p = strchr(wins_owner, ';');
212 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
217 addr->wins_owner = talloc_strdup(addr, wins_owner);
218 if (!addr->wins_owner) {
219 status = NT_STATUS_NO_MEMORY;
223 if (strncmp("expireTime:", p, 11) != 0) {
224 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
228 expire_time = p + 11;
229 p = strchr(expire_time, ';');
231 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
236 addr->expire_time = ldb_string_to_time(expire_time);
246 encode the winsdb_addr("address") attribute like this:
247 "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
249 static int ldb_msg_add_winsdb_addr(struct ldb_message *msg,
250 const char *attr_name, struct winsdb_addr *addr)
255 str = talloc_asprintf(msg, "%s;winsOwner:%s;expireTime:%s;",
256 addr->address, addr->wins_owner,
257 ldb_timestring(msg, addr->expire_time));
260 val.data = discard_const_p(uint8_t, str);
261 val.length = strlen(str);
263 return ldb_msg_add_value(msg, attr_name, &val);
266 struct winsdb_addr **winsdb_addr_list_make(TALLOC_CTX *mem_ctx)
268 struct winsdb_addr **addresses;
270 addresses = talloc_array(mem_ctx, struct winsdb_addr *, 1);
271 if (!addresses) return NULL;
278 struct winsdb_addr **winsdb_addr_list_add(struct winsdb_addr **addresses, const char *address,
279 const char *wins_owner, time_t expire_time)
281 size_t len = winsdb_addr_list_length(addresses);
283 addresses = talloc_realloc(addresses, addresses, struct winsdb_addr *, len + 2);
284 if (!addresses) return NULL;
286 addresses[len] = talloc(addresses, struct winsdb_addr);
287 if (!addresses[len]) {
288 talloc_free(addresses);
292 addresses[len]->address = talloc_strdup(addresses[len], address);
293 if (!addresses[len]->address) {
294 talloc_free(addresses);
298 addresses[len]->wins_owner = talloc_strdup(addresses[len], wins_owner);
299 if (!addresses[len]->wins_owner) {
300 talloc_free(addresses);
304 addresses[len]->expire_time = expire_time;
306 addresses[len+1] = NULL;
311 void winsdb_addr_list_remove(struct winsdb_addr **addresses, const char *address)
315 for (i=0; addresses[i]; i++) {
316 if (strcmp(addresses[i]->address, address) == 0) {
320 if (!addresses[i]) return;
322 for (; addresses[i]; i++) {
323 addresses[i] = addresses[i+1];
329 struct winsdb_addr *winsdb_addr_list_check(struct winsdb_addr **addresses, const char *address)
333 for (i=0; addresses[i]; i++) {
334 if (strcmp(addresses[i]->address, address) == 0) {
342 size_t winsdb_addr_list_length(struct winsdb_addr **addresses)
345 for (i=0; addresses[i]; i++);
349 const char **winsdb_addr_string_list(TALLOC_CTX *mem_ctx, struct winsdb_addr **addresses)
351 size_t len = winsdb_addr_list_length(addresses);
352 const char **str_list;
355 str_list = talloc_array(mem_ctx, const char *, len + 1);
356 if (!str_list) return NULL;
358 for (i=0; i < len; i++) {
359 str_list[i] = talloc_strdup(str_list, addresses[i]->address);
361 talloc_free(str_list);
366 str_list[len] = NULL;
371 load a WINS entry from the database
373 NTSTATUS winsdb_lookup(struct ldb_context *wins_db,
374 struct nbt_name *name,
376 struct winsdb_record **_rec)
379 struct ldb_result *res = NULL;
381 struct winsdb_record *rec;
382 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
384 /* find the record in the WINS database */
385 ret = ldb_search(wins_db, winsdb_dn(tmp_ctx, name), LDB_SCOPE_BASE,
388 if (ret != LDB_SUCCESS || res->count > 1) {
389 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
391 } else if (res->count== 0) {
392 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
396 talloc_steal(tmp_ctx, res);
398 status = winsdb_record(res->msgs[0], name, tmp_ctx, &rec);
399 if (!NT_STATUS_IS_OK(status)) goto failed;
401 /* see if it has already expired */
402 if (rec->state == WREPL_STATE_ACTIVE &&
403 rec->expire_time <= time(NULL)) {
404 DEBUG(5,("WINS: expiring name %s (expired at %s)\n",
405 nbt_name_string(tmp_ctx, rec->name), timestring(tmp_ctx, rec->expire_time)));
406 rec->state = WREPL_STATE_RELEASED;
409 talloc_steal(mem_ctx, rec);
410 talloc_free(tmp_ctx);
415 talloc_free(tmp_ctx);
419 NTSTATUS winsdb_record(struct ldb_message *msg, struct nbt_name *name, TALLOC_CTX *mem_ctx, struct winsdb_record **_rec)
422 struct winsdb_record *rec;
423 struct ldb_message_element *el;
424 uint32_t i, num_values;
426 rec = talloc(mem_ctx, struct winsdb_record);
428 status = NT_STATUS_NO_MEMORY;
433 status = winsdb_nbt_name(rec, msg->dn, &name);
434 if (!NT_STATUS_IS_OK(status)) goto failed;
437 if (strlen(name->name) > 15) {
438 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
441 if (name->scope && strlen(name->scope) > 238) {
442 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
446 /* parse it into a more convenient winsdb_record structure */
448 rec->type = ldb_msg_find_int(msg, "recordType", WREPL_TYPE_UNIQUE);
449 rec->state = ldb_msg_find_int(msg, "recordState", WREPL_STATE_RELEASED);
450 rec->node = ldb_msg_find_int(msg, "nodeType", WREPL_NODE_B);
451 rec->is_static = ldb_msg_find_int(msg, "isStatic", 0);
452 rec->expire_time = ldb_string_to_time(ldb_msg_find_string(msg, "expireTime", NULL));
453 rec->version = ldb_msg_find_uint64(msg, "versionID", 0);
454 rec->wins_owner = ldb_msg_find_string(msg, "winsOwner", NULL);
455 rec->registered_by = ldb_msg_find_string(msg, "registeredBy", NULL);
456 talloc_steal(rec, rec->wins_owner);
457 talloc_steal(rec, rec->registered_by);
459 if (!rec->wins_owner) {
460 rec->wins_owner = talloc_strdup(rec, WINSDB_OWNER_LOCAL);
461 if (rec->wins_owner == NULL) {
462 status = NT_STATUS_NO_MEMORY;
467 el = ldb_msg_find_element(msg, "address");
469 num_values = el->num_values;
474 if (rec->type == WREPL_TYPE_UNIQUE || rec->type == WREPL_TYPE_GROUP) {
475 if (num_values != 1) {
476 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
480 if (rec->state == WREPL_STATE_ACTIVE) {
481 if (num_values < 1) {
482 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
487 rec->addresses = talloc_array(rec, struct winsdb_addr *, num_values+1);
488 if (rec->addresses == NULL) {
489 status = NT_STATUS_NO_MEMORY;
493 for (i=0;i<num_values;i++) {
494 status = winsdb_addr_decode(rec, &el->values[i], rec->addresses, &rec->addresses[i]);
495 if (!NT_STATUS_IS_OK(status)) goto failed;
497 rec->addresses[i] = NULL;
502 if (NT_STATUS_EQUAL(NT_STATUS_INTERNAL_DB_CORRUPTION, status)) {
503 DEBUG(1,("winsdb_record: corrupted record: %s\n", ldb_dn_linearize(rec, msg->dn)));
510 form a ldb_message from a winsdb_record
512 struct ldb_message *winsdb_message(struct ldb_context *ldb,
513 struct winsdb_record *rec, TALLOC_CTX *mem_ctx)
517 struct ldb_message *msg = ldb_msg_new(mem_ctx);
518 if (msg == NULL) goto failed;
520 /* make sure we don't put in corrupted records */
521 addr_count = winsdb_addr_list_length(rec->addresses);
522 if (rec->state == WREPL_STATE_ACTIVE && addr_count == 0) {
523 rec->state = WREPL_STATE_RELEASED;
526 msg->dn = winsdb_dn(msg, rec->name);
527 if (msg->dn == NULL) goto failed;
528 ret |= ldb_msg_add_fmt(msg, "type", "0x%02X", rec->name->type);
529 if (rec->name->name && *rec->name->name) {
530 ret |= ldb_msg_add_string(msg, "name", rec->name->name);
532 if (rec->name->scope && *rec->name->scope) {
533 ret |= ldb_msg_add_string(msg, "scope", rec->name->scope);
535 ret |= ldb_msg_add_fmt(msg, "objectClass", "winsRecord");
536 ret |= ldb_msg_add_fmt(msg, "recordType", "%u", rec->type);
537 ret |= ldb_msg_add_fmt(msg, "recordState", "%u", rec->state);
538 ret |= ldb_msg_add_fmt(msg, "nodeType", "%u", rec->node);
539 ret |= ldb_msg_add_fmt(msg, "isStatic", "%u", rec->is_static);
540 ret |= ldb_msg_add_string(msg, "expireTime",
541 ldb_timestring(msg, rec->expire_time));
542 ret |= ldb_msg_add_fmt(msg, "versionID", "%llu", rec->version);
543 ret |= ldb_msg_add_string(msg, "winsOwner", rec->wins_owner);
544 ret |= ldb_msg_add_empty(msg, "address", 0);
545 for (i=0;rec->addresses[i];i++) {
546 ret |= ldb_msg_add_winsdb_addr(msg, "address", rec->addresses[i]);
548 ret |= ldb_msg_add_empty(msg, "registeredBy", 0);
549 if (rec->registered_by) {
550 ret |= ldb_msg_add_string(msg, "registeredBy", rec->registered_by);
551 if (ret != 0) goto failed;
561 save a WINS record into the database
563 uint8_t winsdb_add(struct ldb_context *wins_db, struct winsdb_record *rec, uint32_t flags)
565 struct ldb_message *msg;
566 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
570 trans = ldb_transaction_start(wins_db);
571 if (trans != LDB_SUCCESS) goto failed;
573 if (flags & WINSDB_FLAG_ALLOC_VERSION) {
574 rec->version = winsdb_allocate_version(wins_db);
575 if (rec->version == 0) goto failed;
577 if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
578 rec->wins_owner = WINSDB_OWNER_LOCAL;
581 msg = winsdb_message(wins_db, rec, tmp_ctx);
582 if (msg == NULL) goto failed;
583 ret = ldb_add(wins_db, msg);
584 if (ret != 0) goto failed;
586 trans = ldb_transaction_commit(wins_db);
587 if (trans != LDB_SUCCESS) goto failed;
589 talloc_free(tmp_ctx);
593 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
594 talloc_free(tmp_ctx);
595 return NBT_RCODE_SVR;
600 modify a WINS record in the database
602 uint8_t winsdb_modify(struct ldb_context *wins_db, struct winsdb_record *rec, uint32_t flags)
604 struct ldb_message *msg;
605 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
610 trans = ldb_transaction_start(wins_db);
611 if (trans != LDB_SUCCESS) goto failed;
613 if (flags & WINSDB_FLAG_ALLOC_VERSION) {
614 rec->version = winsdb_allocate_version(wins_db);
615 if (rec->version == 0) goto failed;
617 if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
618 rec->wins_owner = WINSDB_OWNER_LOCAL;
621 msg = winsdb_message(wins_db, rec, tmp_ctx);
622 if (msg == NULL) goto failed;
624 for (i=0;i<msg->num_elements;i++) {
625 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
628 ret = ldb_modify(wins_db, msg);
629 if (ret != 0) goto failed;
631 trans = ldb_transaction_commit(wins_db);
632 if (trans != LDB_SUCCESS) goto failed;
634 talloc_free(tmp_ctx);
638 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
639 talloc_free(tmp_ctx);
640 return NBT_RCODE_SVR;
645 delete a WINS record from the database
647 uint8_t winsdb_delete(struct ldb_context *wins_db, struct winsdb_record *rec)
649 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
650 const struct ldb_dn *dn;
654 trans = ldb_transaction_start(wins_db);
655 if (trans != LDB_SUCCESS) goto failed;
657 dn = winsdb_dn(tmp_ctx, rec->name);
658 if (dn == NULL) goto failed;
660 ret = ldb_delete(wins_db, dn);
661 if (ret != 0) goto failed;
663 trans = ldb_transaction_commit(wins_db);
664 if (trans != LDB_SUCCESS) goto failed;
666 talloc_free(tmp_ctx);
670 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
671 talloc_free(tmp_ctx);
672 return NBT_RCODE_SVR;
675 struct ldb_context *winsdb_connect(TALLOC_CTX *mem_ctx)
677 return ldb_wrap_connect(mem_ctx, lp_wins_url(), 0, NULL);