2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2005
7 Copyright (C) Stefan Metzmacher 2005
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include "nbt_server/nbt_server.h"
25 #include "nbt_server/wins/winsdb.h"
27 #include <ldb_errors.h>
28 #include "librpc/gen_ndr/ndr_nbt.h"
29 #include "system/time.h"
31 #include "system/network.h"
32 #include "lib/socket/netif.h"
33 #include "param/param.h"
34 #include "lib/util/smb_strtox.h"
38 uint64_t winsdb_get_maxVersion(struct winsdb_handle *h)
41 struct ldb_context *ldb = h->ldb;
43 struct ldb_result *res = NULL;
44 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
45 uint64_t maxVersion = 0;
47 dn = ldb_dn_new(tmp_ctx, ldb, "CN=VERSION");
50 /* find the record in the WINS database */
51 ret = ldb_search(ldb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL);
52 if (ret != LDB_SUCCESS) goto failed;
53 if (res->count > 1) goto failed;
55 if (res->count == 1) {
56 maxVersion = ldb_msg_find_attr_as_uint64(res->msgs[0], "maxVersion", 0);
65 if newVersion == 0 return the old maxVersion + 1 and save it
66 if newVersion > 0 return MAX(oldMaxVersion, newMaxVersion) and save it
68 uint64_t winsdb_set_maxVersion(struct winsdb_handle *h, uint64_t newMaxVersion)
73 struct ldb_result *res = NULL;
74 struct ldb_message *msg = NULL;
75 struct ldb_context *wins_db = h->ldb;
76 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
77 uint64_t oldMaxVersion = 0;
79 trans = ldb_transaction_start(wins_db);
80 if (trans != LDB_SUCCESS) goto failed;
82 dn = ldb_dn_new(tmp_ctx, wins_db, "CN=VERSION");
85 /* find the record in the WINS database */
86 ret = ldb_search(wins_db, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL);
87 if (ret != LDB_SUCCESS) goto failed;
88 if (res->count > 1) goto failed;
90 if (res->count == 1) {
91 oldMaxVersion = ldb_msg_find_attr_as_uint64(res->msgs[0], "maxVersion", 0);
94 if (newMaxVersion == 0) {
95 newMaxVersion = oldMaxVersion + 1;
97 newMaxVersion = MAX(oldMaxVersion, newMaxVersion);
100 msg = ldb_msg_new(tmp_ctx);
101 if (!msg) goto failed;
105 ret = ldb_msg_append_string(msg, "objectClass", "winsMaxVersion",
106 LDB_FLAG_MOD_REPLACE);
107 if (ret != LDB_SUCCESS) goto failed;
108 ret = ldb_msg_append_fmt(msg, LDB_FLAG_MOD_REPLACE,
109 "maxVersion", "%llu", (long long)newMaxVersion);
110 if (ret != LDB_SUCCESS) goto failed;
112 ret = ldb_modify(wins_db, msg);
113 if (ret != LDB_SUCCESS) ret = ldb_add(wins_db, msg);
114 if (ret != LDB_SUCCESS) goto failed;
116 trans = ldb_transaction_commit(wins_db);
117 if (trans != LDB_SUCCESS) goto failed;
119 talloc_free(tmp_ctx);
120 return newMaxVersion;
123 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
124 talloc_free(tmp_ctx);
129 return a DN for a nbt_name
131 static struct ldb_dn *winsdb_dn(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
132 const struct nbt_name *name)
136 dn = ldb_dn_new_fmt(mem_ctx, ldb, "type=0x%02X", name->type);
137 if (ldb_dn_is_valid(dn) && name->name && *name->name) {
138 ldb_dn_add_child_fmt(dn, "name=%s", name->name);
140 if (ldb_dn_is_valid(dn) && name->scope && *name->scope) {
141 ldb_dn_add_child_fmt(dn, "scope=%s", name->scope);
146 static NTSTATUS winsdb_nbt_name(TALLOC_CTX *mem_ctx, struct ldb_dn *dn, struct nbt_name **_name)
149 struct nbt_name *name;
150 unsigned int comp_num;
154 name = talloc(mem_ctx, struct nbt_name);
156 status = NT_STATUS_NO_MEMORY;
160 comp_num = ldb_dn_get_comp_num(dn);
163 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
167 if (comp_num > cur && strcasecmp("scope", ldb_dn_get_component_name(dn, cur)) == 0) {
168 name->scope = (const char *)talloc_strdup(name, (char *)ldb_dn_get_component_val(dn, cur)->data);
174 if (comp_num > cur && strcasecmp("name", ldb_dn_get_component_name(dn, cur)) == 0) {
175 name->name = (const char *)talloc_strdup(name, (char *)ldb_dn_get_component_val(dn, cur)->data);
178 name->name = talloc_strdup(name, "");
180 status = NT_STATUS_NO_MEMORY;
185 if (comp_num > cur && strcasecmp("type", ldb_dn_get_component_name(dn, cur)) == 0) {
188 (char *)ldb_dn_get_component_val(dn, cur)->data,
194 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
199 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
211 decode the winsdb_addr("address") attribute:
213 "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
216 static NTSTATUS winsdb_addr_decode(struct winsdb_handle *h, struct winsdb_record *rec, struct ldb_val *val,
217 TALLOC_CTX *mem_ctx, struct winsdb_addr **_addr)
220 struct winsdb_addr *addr;
222 const char *wins_owner;
223 const char *expire_time;
226 addr = talloc(mem_ctx, struct winsdb_addr);
228 status = NT_STATUS_NO_MEMORY;
232 address = (char *)val->data;
234 p = strchr(address, ';');
236 /* support old entries, with only the address */
237 addr->address = (const char *)talloc_steal(addr, val->data);
238 addr->wins_owner = talloc_strdup(addr, rec->wins_owner);
239 if (!addr->wins_owner) {
240 status = NT_STATUS_NO_MEMORY;
243 addr->expire_time = rec->expire_time;
249 addr->address = talloc_strdup(addr, address);
250 if (!addr->address) {
251 status = NT_STATUS_NO_MEMORY;
255 if (strncmp("winsOwner:", p, 10) != 0) {
256 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
260 p = strchr(wins_owner, ';');
262 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
267 if (strcmp(wins_owner, "0.0.0.0") == 0) {
268 wins_owner = h->local_owner;
270 addr->wins_owner = talloc_strdup(addr, wins_owner);
271 if (!addr->wins_owner) {
272 status = NT_STATUS_NO_MEMORY;
276 if (strncmp("expireTime:", p, 11) != 0) {
277 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
281 expire_time = p + 11;
282 p = strchr(expire_time, ';');
284 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
289 addr->expire_time = ldb_string_to_time(expire_time);
299 encode the winsdb_addr("address") attribute like this:
301 "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
305 static int ldb_msg_add_winsdb_addr(struct ldb_message *msg, struct winsdb_record *rec,
306 const char *attr_name, struct winsdb_addr *addr)
310 if (rec->is_static) {
311 str = talloc_strdup(msg, addr->address);
312 if (!str) return LDB_ERR_OPERATIONS_ERROR;
315 expire_time = ldb_timestring(msg, addr->expire_time);
316 if (!expire_time) return LDB_ERR_OPERATIONS_ERROR;
317 str = talloc_asprintf(msg, "%s;winsOwner:%s;expireTime:%s;",
318 addr->address, addr->wins_owner,
320 talloc_free(expire_time);
321 if (!str) return LDB_ERR_OPERATIONS_ERROR;
324 return ldb_msg_add_string(msg, attr_name, str);
327 struct winsdb_addr **winsdb_addr_list_make(TALLOC_CTX *mem_ctx)
329 struct winsdb_addr **addresses;
331 addresses = talloc_array(mem_ctx, struct winsdb_addr *, 1);
332 if (!addresses) return NULL;
339 static int winsdb_addr_sort_list (struct winsdb_addr **p1, struct winsdb_addr **p2, void *opaque)
341 struct winsdb_addr *a1 = talloc_get_type(*p1, struct winsdb_addr);
342 struct winsdb_addr *a2 = talloc_get_type(*p2, struct winsdb_addr);
343 struct winsdb_handle *h= talloc_get_type(opaque, struct winsdb_handle);
344 bool a1_owned = false;
345 bool a2_owned = false;
348 * first the owned addresses with the newest to the oldest address
349 * then the replica addresses with the newest to the oldest address
351 if (a2->expire_time != a1->expire_time) {
352 return a2->expire_time - a1->expire_time;
355 if (strcmp(a2->wins_owner, h->local_owner) == 0) {
359 if (strcmp(a1->wins_owner, h->local_owner) == 0) {
363 return a2_owned - a1_owned;
366 struct winsdb_addr **winsdb_addr_list_add(struct winsdb_handle *h, const struct winsdb_record *rec,
367 struct winsdb_addr **addresses, const char *address,
368 const char *wins_owner, time_t expire_time,
369 bool is_name_registration)
371 struct winsdb_addr *old_addr = NULL;
374 bool found_old_replica = false;
377 * count the addresses and maybe
378 * find an old entry for the new address
380 for (i=0; addresses[i]; i++) {
381 if (old_addr) continue;
382 if (strcmp(addresses[i]->address, address) == 0) {
383 old_addr = addresses[i];
389 * the address is already there
390 * and we can replace it
393 goto remove_old_addr;
397 * if we don't have 25 addresses already,
398 * we can just add the new address
405 * if we haven't found the address,
406 * and we have already have 25 addresses
407 * if so then we need to do the following:
408 * - if it isn't a name registration, then just ignore the new address
409 * - if it is a name registration, then first search for
410 * the oldest replica and if there's no replica address
411 * search the oldest owned address
413 if (!is_name_registration) {
418 * find the oldest replica address, if there's no replica
419 * record at all, find the oldest owned address
421 for (i=0; addresses[i]; i++) {
422 bool cur_is_replica = false;
423 /* find out if the current address is a replica */
424 if (strcmp(addresses[i]->wins_owner, h->local_owner) != 0) {
425 cur_is_replica = true;
429 * if we already found a replica address and the current address
430 * is not a replica, then skip it
432 if (found_old_replica && !cur_is_replica) continue;
435 * if we found the first replica address, reset the address
436 * that would be replaced
438 if (!found_old_replica && cur_is_replica) {
439 found_old_replica = true;
440 old_addr = addresses[i];
445 * if the first address isn't a replica, just start with
449 old_addr = addresses[i];
454 * see if we find an older address
456 if (addresses[i]->expire_time < old_addr->expire_time) {
457 old_addr = addresses[i];
463 winsdb_addr_list_remove(addresses, old_addr->address);
467 addresses = talloc_realloc(addresses, addresses, struct winsdb_addr *, len + 2);
468 if (!addresses) return NULL;
470 addresses[len] = talloc(addresses, struct winsdb_addr);
471 if (!addresses[len]) {
472 talloc_free(addresses);
476 addresses[len]->address = talloc_strdup(addresses[len], address);
477 if (!addresses[len]->address) {
478 talloc_free(addresses);
482 addresses[len]->wins_owner = talloc_strdup(addresses[len], wins_owner);
483 if (!addresses[len]->wins_owner) {
484 talloc_free(addresses);
488 addresses[len]->expire_time = expire_time;
490 addresses[len+1] = NULL;
492 LDB_TYPESAFE_QSORT(addresses, len+1, h, winsdb_addr_sort_list);
497 void winsdb_addr_list_remove(struct winsdb_addr **addresses, const char *address)
501 for (i=0; addresses[i]; i++) {
502 if (strcmp(addresses[i]->address, address) == 0) {
507 for (; addresses[i]; i++) {
508 addresses[i] = addresses[i+1];
514 struct winsdb_addr *winsdb_addr_list_check(struct winsdb_addr **addresses, const char *address)
518 for (i=0; addresses[i]; i++) {
519 if (strcmp(addresses[i]->address, address) == 0) {
527 size_t winsdb_addr_list_length(struct winsdb_addr **addresses)
530 for (i=0; addresses[i]; i++);
534 const char **winsdb_addr_string_list(TALLOC_CTX *mem_ctx, struct winsdb_addr **addresses)
536 size_t len = winsdb_addr_list_length(addresses);
537 const char **str_list=NULL;
540 for (i=0; i < len; i++) {
541 str_list = str_list_add(str_list, addresses[i]->address);
546 talloc_steal(mem_ctx, str_list);
551 load a WINS entry from the database
553 NTSTATUS winsdb_lookup(struct winsdb_handle *h,
554 const struct nbt_name *name,
556 struct winsdb_record **_rec)
559 struct ldb_result *res = NULL;
561 struct winsdb_record *rec;
562 struct ldb_context *wins_db = h->ldb;
563 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
564 time_t now = time(NULL);
566 /* find the record in the WINS database */
567 ret = ldb_search(wins_db, tmp_ctx, &res,
568 winsdb_dn(tmp_ctx, wins_db, name),
569 LDB_SCOPE_BASE, NULL, NULL);
571 if (ret != LDB_SUCCESS || res->count > 1) {
572 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
574 } else if (res->count== 0) {
575 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
579 status = winsdb_record(h, res->msgs[0], tmp_ctx, now, &rec);
580 if (!NT_STATUS_IS_OK(status)) goto failed;
582 talloc_steal(mem_ctx, rec);
583 talloc_free(tmp_ctx);
588 talloc_free(tmp_ctx);
592 NTSTATUS winsdb_record(struct winsdb_handle *h, struct ldb_message *msg, TALLOC_CTX *mem_ctx, time_t now, struct winsdb_record **_rec)
595 struct winsdb_record *rec;
596 struct ldb_message_element *el;
597 struct nbt_name *name;
598 uint32_t i, j, num_values;
600 rec = talloc(mem_ctx, struct winsdb_record);
602 status = NT_STATUS_NO_MEMORY;
606 status = winsdb_nbt_name(rec, msg->dn, &name);
607 if (!NT_STATUS_IS_OK(status)) goto failed;
609 if (strlen(name->name) > 15) {
610 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
613 if (name->scope && strlen(name->scope) > 238) {
614 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
618 /* parse it into a more convenient winsdb_record structure */
620 rec->type = ldb_msg_find_attr_as_int(msg, "recordType", WREPL_TYPE_UNIQUE);
621 rec->state = ldb_msg_find_attr_as_int(msg, "recordState", WREPL_STATE_RELEASED);
622 rec->node = ldb_msg_find_attr_as_int(msg, "nodeType", WREPL_NODE_B);
623 rec->is_static = ldb_msg_find_attr_as_int(msg, "isStatic", 0);
624 rec->expire_time = ldb_string_to_time(ldb_msg_find_attr_as_string(msg, "expireTime", NULL));
625 rec->version = ldb_msg_find_attr_as_uint64(msg, "versionID", 0);
626 rec->wins_owner = ldb_msg_find_attr_as_string(msg, "winsOwner", NULL);
627 rec->registered_by = ldb_msg_find_attr_as_string(msg, "registeredBy", NULL);
628 talloc_steal(rec, rec->wins_owner);
629 talloc_steal(rec, rec->registered_by);
631 if (!rec->wins_owner || strcmp(rec->wins_owner, "0.0.0.0") == 0) {
632 rec->wins_owner = h->local_owner;
635 el = ldb_msg_find_element(msg, "address");
637 num_values = el->num_values;
642 if (rec->type == WREPL_TYPE_UNIQUE || rec->type == WREPL_TYPE_GROUP) {
643 if (num_values != 1) {
644 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
648 if (rec->state == WREPL_STATE_ACTIVE) {
649 if (num_values < 1) {
650 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
654 if (num_values > 25) {
655 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
659 rec->addresses = talloc_array(rec, struct winsdb_addr *, num_values+1);
660 if (rec->addresses == NULL) {
661 status = NT_STATUS_NO_MEMORY;
665 for (i=0,j=0;i<num_values;i++) {
666 bool we_are_owner = false;
668 status = winsdb_addr_decode(h, rec, &el->values[i], rec->addresses, &rec->addresses[j]);
669 if (!NT_STATUS_IS_OK(status)) goto failed;
671 if (strcmp(rec->addresses[j]->wins_owner, h->local_owner) == 0) {
676 * the record isn't static and is active
677 * then don't add the address if it's expired,
678 * but only if we're the owner of the address
680 * This is important for SGROUP records,
681 * because each server thinks he's the owner of the
682 * record and the record isn't replicated on a
683 * name_refresh. So addresses owned by another owner
684 * could expire, but we still need to return them
687 if (!rec->is_static &&
688 rec->addresses[j]->expire_time <= now &&
689 rec->state == WREPL_STATE_ACTIVE &&
691 DEBUG(5,("WINS: expiring name addr %s of %s (expired at %s)\n",
692 rec->addresses[j]->address, nbt_name_string(rec->addresses[j], rec->name),
693 timestring(rec->addresses[j], rec->addresses[j]->expire_time)));
694 talloc_free(rec->addresses[j]);
695 rec->addresses[j] = NULL;
700 rec->addresses[j] = NULL;
703 if (rec->is_static && rec->state == WREPL_STATE_ACTIVE) {
704 rec->expire_time = get_time_t_max();
705 for (i=0;rec->addresses[i];i++) {
706 rec->addresses[i]->expire_time = rec->expire_time;
710 if (rec->state == WREPL_STATE_ACTIVE) {
711 if (num_values < 1) {
712 DEBUG(5,("WINS: expiring name %s (because it has no active addresses)\n",
713 nbt_name_string(mem_ctx, rec->name)));
714 rec->state = WREPL_STATE_RELEASED;
721 if (NT_STATUS_EQUAL(NT_STATUS_INTERNAL_DB_CORRUPTION, status)) {
722 DEBUG(1,("winsdb_record: corrupted record: %s\n", ldb_dn_get_linearized(msg->dn)));
729 form a ldb_message from a winsdb_record
731 static struct ldb_message *winsdb_message(struct ldb_context *ldb,
732 struct winsdb_record *rec,
737 const char *expire_time;
738 struct ldb_message *msg = ldb_msg_new(mem_ctx);
739 if (msg == NULL) goto failed;
741 /* make sure we don't put in corrupted records */
742 addr_count = winsdb_addr_list_length(rec->addresses);
743 if (rec->state == WREPL_STATE_ACTIVE && addr_count == 0) {
744 rec->state = WREPL_STATE_RELEASED;
746 if (rec->type == WREPL_TYPE_UNIQUE && addr_count > 1) {
747 rec->type = WREPL_TYPE_MHOMED;
750 expire_time = ldb_timestring(msg, rec->expire_time);
755 msg->dn = winsdb_dn(msg, ldb, rec->name);
756 if (msg->dn == NULL) goto failed;
757 ret = ldb_msg_add_fmt(msg, "type", "0x%02X", rec->name->type);
758 if (rec->name->name && *rec->name->name) {
759 ret |= ldb_msg_add_string(msg, "name", rec->name->name);
761 if (rec->name->scope && *rec->name->scope) {
762 ret |= ldb_msg_add_string(msg, "scope", rec->name->scope);
764 ret |= ldb_msg_add_fmt(msg, "objectClass", "winsRecord");
765 ret |= ldb_msg_add_fmt(msg, "recordType", "%u", rec->type);
766 ret |= ldb_msg_add_fmt(msg, "recordState", "%u", rec->state);
767 ret |= ldb_msg_add_fmt(msg, "nodeType", "%u", rec->node);
768 ret |= ldb_msg_add_fmt(msg, "isStatic", "%u", rec->is_static);
769 ret |= ldb_msg_add_empty(msg, "expireTime", 0, NULL);
770 if (!(rec->is_static && rec->state == WREPL_STATE_ACTIVE)) {
771 ret |= ldb_msg_add_string(msg, "expireTime", expire_time);
773 ret |= ldb_msg_add_fmt(msg, "versionID", "%llu", (long long)rec->version);
774 ret |= ldb_msg_add_string(msg, "winsOwner", rec->wins_owner);
775 ret |= ldb_msg_add_empty(msg, "address", 0, NULL);
776 for (i=0;rec->addresses[i];i++) {
777 ret |= ldb_msg_add_winsdb_addr(msg, rec, "address", rec->addresses[i]);
779 if (rec->registered_by) {
780 ret |= ldb_msg_append_string(msg, "registeredBy", rec->registered_by, 0);
782 if (ret != LDB_SUCCESS) goto failed;
791 save a WINS record into the database
793 uint8_t winsdb_add(struct winsdb_handle *h, struct winsdb_record *rec, uint32_t flags)
795 struct ldb_message *msg;
796 struct ldb_context *wins_db = h->ldb;
797 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
801 trans = ldb_transaction_start(wins_db);
802 if (trans != LDB_SUCCESS) goto failed;
804 if (flags & WINSDB_FLAG_ALLOC_VERSION) {
805 /* passing '0' means auto-allocate a new one */
806 rec->version = winsdb_set_maxVersion(h, 0);
807 if (rec->version == 0) goto failed;
809 if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
810 rec->wins_owner = h->local_owner;
813 msg = winsdb_message(wins_db, rec, tmp_ctx);
814 if (msg == NULL) goto failed;
815 ret = ldb_add(wins_db, msg);
816 if (ret != LDB_SUCCESS) goto failed;
818 trans = ldb_transaction_commit(wins_db);
819 if (trans != LDB_SUCCESS) goto failed;
821 wins_hook(h, rec, WINS_HOOK_ADD, h->hook_script);
823 talloc_free(tmp_ctx);
827 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
828 talloc_free(tmp_ctx);
829 return NBT_RCODE_SVR;
834 modify a WINS record in the database
836 uint8_t winsdb_modify(struct winsdb_handle *h, struct winsdb_record *rec, uint32_t flags)
838 struct ldb_message *msg;
839 struct ldb_context *wins_db = h->ldb;
840 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
845 trans = ldb_transaction_start(wins_db);
846 if (trans != LDB_SUCCESS) goto failed;
848 if (flags & WINSDB_FLAG_ALLOC_VERSION) {
849 /* passing '0' means auto-allocate a new one */
850 rec->version = winsdb_set_maxVersion(h, 0);
851 if (rec->version == 0) goto failed;
853 if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
854 rec->wins_owner = h->local_owner;
857 msg = winsdb_message(wins_db, rec, tmp_ctx);
858 if (msg == NULL) goto failed;
860 for (i=0;i<msg->num_elements;i++) {
861 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
864 ret = ldb_modify(wins_db, msg);
865 if (ret != LDB_SUCCESS) goto failed;
867 trans = ldb_transaction_commit(wins_db);
868 if (trans != LDB_SUCCESS) goto failed;
870 wins_hook(h, rec, WINS_HOOK_MODIFY, h->hook_script);
872 talloc_free(tmp_ctx);
876 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
877 talloc_free(tmp_ctx);
878 return NBT_RCODE_SVR;
883 delete a WINS record from the database
885 uint8_t winsdb_delete(struct winsdb_handle *h, struct winsdb_record *rec)
887 struct ldb_context *wins_db = h->ldb;
888 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
893 trans = ldb_transaction_start(wins_db);
894 if (trans != LDB_SUCCESS) goto failed;
896 dn = winsdb_dn(tmp_ctx, wins_db, rec->name);
897 if (dn == NULL) goto failed;
899 ret = ldb_delete(wins_db, dn);
900 if (ret != LDB_SUCCESS) goto failed;
902 trans = ldb_transaction_commit(wins_db);
903 if (trans != LDB_SUCCESS) goto failed;
905 wins_hook(h, rec, WINS_HOOK_DELETE, h->hook_script);
907 talloc_free(tmp_ctx);
911 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
912 talloc_free(tmp_ctx);
913 return NBT_RCODE_SVR;
916 static bool winsdb_check_or_add_module_list(struct tevent_context *ev_ctx,
917 struct loadparm_context *lp_ctx, struct winsdb_handle *h,
918 const char *wins_path)
923 struct ldb_result *res = NULL;
924 struct ldb_message *msg = NULL;
925 TALLOC_CTX *tmp_ctx = talloc_new(h);
926 unsigned int flags = 0;
928 trans = ldb_transaction_start(h->ldb);
929 if (trans != LDB_SUCCESS) goto failed;
931 /* check if we have a special @MODULES record already */
932 dn = ldb_dn_new(tmp_ctx, h->ldb, "@MODULES");
933 if (!dn) goto failed;
935 /* find the record in the WINS database */
936 ret = ldb_search(h->ldb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL);
937 if (ret != LDB_SUCCESS) goto failed;
939 if (res->count > 0) goto skip;
941 /* if there's no record, add one */
942 msg = ldb_msg_new(tmp_ctx);
943 if (!msg) goto failed;
946 ret = ldb_msg_add_string(msg, "@LIST", "wins_ldb");
947 if (ret != LDB_SUCCESS) goto failed;
949 ret = ldb_add(h->ldb, msg);
950 if (ret != LDB_SUCCESS) goto failed;
952 trans = ldb_transaction_commit(h->ldb);
953 if (trans != LDB_SUCCESS) goto failed;
955 /* close and reopen the database, with the modules */
956 trans = LDB_ERR_OTHER;
960 if (lpcfg_parm_bool(lp_ctx, NULL,"winsdb", "nosync", false)) {
961 flags |= LDB_FLG_NOSYNC;
964 h->ldb = ldb_wrap_connect(h, ev_ctx, lp_ctx, wins_path,
966 if (!h->ldb) goto failed;
968 talloc_free(tmp_ctx);
972 if (trans == LDB_SUCCESS) ldb_transaction_cancel(h->ldb);
973 talloc_free(tmp_ctx);
977 if (trans == LDB_SUCCESS) ldb_transaction_cancel(h->ldb);
978 talloc_free(tmp_ctx);
982 struct winsdb_handle *winsdb_connect(TALLOC_CTX *mem_ctx,
983 struct tevent_context *ev_ctx,
984 struct loadparm_context *lp_ctx,
986 enum winsdb_handle_caller caller)
988 const struct loadparm_substitution *lp_sub =
989 lpcfg_noop_substitution();
990 struct winsdb_handle *h = NULL;
991 unsigned int flags = 0;
996 h = talloc_zero(mem_ctx, struct winsdb_handle);
999 wins_path = lpcfg_state_path(h, lp_ctx, "wins.ldb");
1001 if (lpcfg_parm_bool(lp_ctx, NULL,"winsdb", "nosync", false)) {
1002 flags |= LDB_FLG_NOSYNC;
1005 h->ldb = ldb_wrap_connect(h, ev_ctx, lp_ctx, wins_path,
1007 if (!h->ldb) goto failed;
1010 h->hook_script = lpcfg_wins_hook(lp_ctx, lp_sub, h);
1012 h->local_owner = talloc_strdup(h, owner);
1013 if (!h->local_owner) goto failed;
1015 /* make sure the module list is available and used */
1016 ret = winsdb_check_or_add_module_list(ev_ctx, lp_ctx, h, wins_path);
1017 if (!ret) goto failed;
1019 ldb_err = ldb_set_opaque(h->ldb, "winsdb_handle", h);
1020 if (ldb_err != LDB_SUCCESS) goto failed;