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 2 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, write to the Free Software
21 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include "nbt_server/nbt_server.h"
26 #include "nbt_server/wins/winsdb.h"
27 #include "lib/ldb/include/ldb.h"
28 #include "lib/ldb/include/ldb_errors.h"
29 #include "librpc/gen_ndr/ndr_nbt.h"
30 #include "system/time.h"
32 #include "system/network.h"
33 #include "lib/socket/netif.h"
35 uint64_t winsdb_get_maxVersion(struct winsdb_handle *h)
38 struct ldb_context *ldb = h->ldb;
40 struct ldb_result *res = NULL;
41 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
42 uint64_t maxVersion = 0;
44 dn = ldb_dn_explode(tmp_ctx, "CN=VERSION");
47 /* find the record in the WINS database */
48 ret = ldb_search(ldb, dn, LDB_SCOPE_BASE,
50 if (ret != LDB_SUCCESS) goto failed;
51 talloc_steal(tmp_ctx, res);
52 if (res->count > 1) goto failed;
54 if (res->count == 1) {
55 maxVersion = ldb_msg_find_attr_as_uint64(res->msgs[0], "maxVersion", 0);
64 if newVersion == 0 return the old maxVersion + 1 and save it
65 if newVersion > 0 return MAX(oldMaxVersion, newMaxVersion) and save it
67 uint64_t winsdb_set_maxVersion(struct winsdb_handle *h, uint64_t newMaxVersion)
72 struct ldb_result *res = NULL;
73 struct ldb_message *msg = NULL;
74 struct ldb_context *wins_db = h->ldb;
75 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
76 uint64_t oldMaxVersion = 0;
78 trans = ldb_transaction_start(wins_db);
79 if (trans != LDB_SUCCESS) goto failed;
81 dn = ldb_dn_explode(tmp_ctx, "CN=VERSION");
84 /* find the record in the WINS database */
85 ret = ldb_search(wins_db, dn, LDB_SCOPE_BASE, NULL, NULL, &res);
87 if (ret != LDB_SUCCESS) goto failed;
88 if (res->count > 1) goto failed;
90 talloc_steal(tmp_ctx, res);
92 if (res->count == 1) {
93 oldMaxVersion = ldb_msg_find_attr_as_uint64(res->msgs[0], "maxVersion", 0);
96 if (newMaxVersion == 0) {
97 newMaxVersion = oldMaxVersion + 1;
99 newMaxVersion = MAX(oldMaxVersion, newMaxVersion);
102 msg = ldb_msg_new(tmp_ctx);
103 if (!msg) goto failed;
107 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE);
108 if (ret != 0) goto failed;
109 ret = ldb_msg_add_string(msg, "objectClass", "winsMaxVersion");
110 if (ret != 0) goto failed;
111 ret = ldb_msg_add_empty(msg, "maxVersion", LDB_FLAG_MOD_REPLACE);
112 if (ret != 0) goto failed;
113 ret = ldb_msg_add_fmt(msg, "maxVersion", "%llu", (long long)newMaxVersion);
114 if (ret != 0) goto failed;
116 ret = ldb_modify(wins_db, msg);
117 if (ret != 0) ret = ldb_add(wins_db, msg);
118 if (ret != 0) goto failed;
120 trans = ldb_transaction_commit(wins_db);
121 if (trans != LDB_SUCCESS) goto failed;
123 talloc_free(tmp_ctx);
124 return newMaxVersion;
127 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
128 talloc_free(tmp_ctx);
132 uint64_t winsdb_get_seqnumber(struct winsdb_handle *h)
135 struct ldb_context *ldb = h->ldb;
137 struct ldb_result *res = NULL;
138 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
139 uint64_t seqnumber = 0;
141 dn = ldb_dn_explode(tmp_ctx, "@BASEINFO");
142 if (!dn) goto failed;
144 /* find the record in the WINS database */
145 ret = ldb_search(ldb, dn, LDB_SCOPE_BASE,
147 if (ret != LDB_SUCCESS) goto failed;
148 talloc_steal(tmp_ctx, res);
149 if (res->count > 1) goto failed;
151 if (res->count == 1) {
152 seqnumber = ldb_msg_find_attr_as_uint64(res->msgs[0], "sequenceNumber", 0);
156 talloc_free(tmp_ctx);
161 return a DN for a nbt_name
163 static struct ldb_dn *winsdb_dn(TALLOC_CTX *mem_ctx, struct nbt_name *name)
167 dn = ldb_dn_string_compose(mem_ctx, NULL, "type=0x%02X", name->type);
168 if (dn && name->name && *name->name) {
169 dn = ldb_dn_string_compose(mem_ctx, dn, "name=%s", name->name);
171 if (dn && name->scope && *name->scope) {
172 dn = ldb_dn_string_compose(mem_ctx, dn, "scope=%s", name->scope);
177 static NTSTATUS winsdb_nbt_name(TALLOC_CTX *mem_ctx, struct ldb_dn *dn, struct nbt_name **_name)
180 struct nbt_name *name;
183 name = talloc(mem_ctx, struct nbt_name);
185 status = NT_STATUS_NO_MEMORY;
189 if (dn->comp_num > 3) {
190 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
194 if (dn->comp_num > cur && strcasecmp("scope", dn->components[cur].name) == 0) {
195 name->scope = (const char *)talloc_steal(name, dn->components[cur].value.data);
201 if (dn->comp_num > cur && strcasecmp("name", dn->components[cur].name) == 0) {
202 name->name = (const char *)talloc_steal(name, dn->components[cur].value.data);
205 name->name = talloc_strdup(name, "");
207 status = NT_STATUS_NO_MEMORY;
212 if (dn->comp_num > cur && strcasecmp("type", dn->components[cur].name) == 0) {
213 name->type = strtoul((char *)dn->components[cur].value.data, NULL, 0);
216 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
228 decode the winsdb_addr("address") attribute:
230 "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
233 static NTSTATUS winsdb_addr_decode(struct winsdb_handle *h, struct winsdb_record *rec, struct ldb_val *val,
234 TALLOC_CTX *mem_ctx, struct winsdb_addr **_addr)
237 struct winsdb_addr *addr;
239 const char *wins_owner;
240 const char *expire_time;
243 addr = talloc(mem_ctx, struct winsdb_addr);
245 status = NT_STATUS_NO_MEMORY;
249 address = (char *)val->data;
251 p = strchr(address, ';');
253 /* support old entries, with only the address */
254 addr->address = (const char *)talloc_steal(addr, val->data);
255 addr->wins_owner = talloc_reference(addr, rec->wins_owner);
256 if (!addr->wins_owner) {
257 status = NT_STATUS_NO_MEMORY;
260 addr->expire_time = rec->expire_time;
266 addr->address = talloc_strdup(addr, address);
267 if (!addr->address) {
268 status = NT_STATUS_NO_MEMORY;
272 if (strncmp("winsOwner:", p, 10) != 0) {
273 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
277 p = strchr(wins_owner, ';');
279 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
284 if (strcmp(wins_owner, "0.0.0.0") == 0) {
285 wins_owner = h->local_owner;
287 addr->wins_owner = talloc_strdup(addr, wins_owner);
288 if (!addr->wins_owner) {
289 status = NT_STATUS_NO_MEMORY;
293 if (strncmp("expireTime:", p, 11) != 0) {
294 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
298 expire_time = p + 11;
299 p = strchr(expire_time, ';');
301 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
306 addr->expire_time = ldb_string_to_time(expire_time);
316 encode the winsdb_addr("address") attribute like this:
318 "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
322 static int ldb_msg_add_winsdb_addr(struct ldb_message *msg, struct winsdb_record *rec,
323 const char *attr_name, struct winsdb_addr *addr)
328 if (rec->is_static) {
329 str = talloc_strdup(msg, addr->address);
333 expire_time = ldb_timestring(msg, addr->expire_time);
334 if (!expire_time) return -1;
335 str = talloc_asprintf(msg, "%s;winsOwner:%s;expireTime:%s;",
336 addr->address, addr->wins_owner,
338 talloc_free(expire_time);
342 val.data = discard_const_p(uint8_t, str);
343 val.length = strlen(str);
345 return ldb_msg_add_value(msg, attr_name, &val);
348 struct winsdb_addr **winsdb_addr_list_make(TALLOC_CTX *mem_ctx)
350 struct winsdb_addr **addresses;
352 addresses = talloc_array(mem_ctx, struct winsdb_addr *, 1);
353 if (!addresses) return NULL;
360 static int winsdb_addr_sort_list (struct winsdb_addr **p1, struct winsdb_addr **p2, void *opaque)
362 struct winsdb_addr *a1 = talloc_get_type(*p1, struct winsdb_addr);
363 struct winsdb_addr *a2 = talloc_get_type(*p2, struct winsdb_addr);
364 struct winsdb_handle *h= talloc_get_type(opaque, struct winsdb_handle);
365 BOOL a1_owned = False;
366 BOOL a2_owned = False;
369 * first the owned addresses with the newest to the oldest address
370 * then the replica addresses with the newest to the oldest address
372 if (a2->expire_time != a1->expire_time) {
373 return a2->expire_time - a1->expire_time;
376 if (strcmp(a2->wins_owner, h->local_owner) == 0) {
380 if (strcmp(a1->wins_owner, h->local_owner) == 0) {
384 return a2_owned - a1_owned;
387 struct winsdb_addr **winsdb_addr_list_add(struct winsdb_handle *h, const struct winsdb_record *rec,
388 struct winsdb_addr **addresses, const char *address,
389 const char *wins_owner, time_t expire_time,
390 BOOL is_name_registration)
392 struct winsdb_addr *old_addr = NULL;
395 BOOL found_old_replica = False;
398 * count the addresses and maybe
399 * find an old entry for the new address
401 for (i=0; addresses[i]; i++) {
402 if (old_addr) continue;
403 if (strcmp(addresses[i]->address, address) == 0) {
404 old_addr = addresses[i];
410 * the address is already there
411 * and we can replace it
414 goto remove_old_addr;
418 * if we don't have 25 addresses already,
419 * we can just add the new address
426 * if we haven't found the address,
427 * and we have already have 25 addresses
428 * if so then we need to do the following:
429 * - if it isn't a name registration, then just ignore the new address
430 * - if it is a name registration, then first search for
431 * the oldest replica and if there's no replica address
432 * search the oldest owned address
434 if (!is_name_registration) {
439 * find the oldest replica address, if there's no replica
440 * record at all, find the oldest owned address
442 for (i=0; addresses[i]; i++) {
443 BOOL cur_is_replica = False;
444 /* find out if the current address is a replica */
445 if (strcmp(addresses[i]->wins_owner, h->local_owner) != 0) {
446 cur_is_replica = True;
450 * if we already found a replica address and the current address
451 * is not a replica, then skip it
453 if (found_old_replica && !cur_is_replica) continue;
456 * if we found the first replica address, reset the address
457 * that would be replaced
459 if (!found_old_replica && cur_is_replica) {
460 found_old_replica = True;
461 old_addr = addresses[i];
466 * if the first address isn't a replica, just start with
470 old_addr = addresses[i];
475 * see if we find an older address
477 if (addresses[i]->expire_time < old_addr->expire_time) {
478 old_addr = addresses[i];
484 winsdb_addr_list_remove(addresses, old_addr->address);
488 addresses = talloc_realloc(addresses, addresses, struct winsdb_addr *, len + 2);
489 if (!addresses) return NULL;
491 addresses[len] = talloc(addresses, struct winsdb_addr);
492 if (!addresses[len]) {
493 talloc_free(addresses);
497 addresses[len]->address = talloc_strdup(addresses[len], address);
498 if (!addresses[len]->address) {
499 talloc_free(addresses);
503 addresses[len]->wins_owner = talloc_strdup(addresses[len], wins_owner);
504 if (!addresses[len]->wins_owner) {
505 talloc_free(addresses);
509 addresses[len]->expire_time = expire_time;
511 addresses[len+1] = NULL;
513 ldb_qsort(addresses, len+1 , sizeof(addresses[0]), h, (ldb_qsort_cmp_fn_t)winsdb_addr_sort_list);
518 void winsdb_addr_list_remove(struct winsdb_addr **addresses, const char *address)
522 for (i=0; addresses[i]; i++) {
523 if (strcmp(addresses[i]->address, address) == 0) {
528 for (; addresses[i]; i++) {
529 addresses[i] = addresses[i+1];
535 struct winsdb_addr *winsdb_addr_list_check(struct winsdb_addr **addresses, const char *address)
539 for (i=0; addresses[i]; i++) {
540 if (strcmp(addresses[i]->address, address) == 0) {
548 size_t winsdb_addr_list_length(struct winsdb_addr **addresses)
551 for (i=0; addresses[i]; i++);
555 const char **winsdb_addr_string_list(TALLOC_CTX *mem_ctx, struct winsdb_addr **addresses)
557 size_t len = winsdb_addr_list_length(addresses);
558 const char **str_list=NULL;
561 for (i=0; i < len; i++) {
562 str_list = str_list_add(str_list, addresses[i]->address);
567 talloc_steal(mem_ctx, str_list);
572 load a WINS entry from the database
574 NTSTATUS winsdb_lookup(struct winsdb_handle *h,
575 struct nbt_name *name,
577 struct winsdb_record **_rec)
580 struct ldb_result *res = NULL;
582 struct winsdb_record *rec;
583 struct ldb_context *wins_db = h->ldb;
584 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
585 time_t now = time(NULL);
587 /* find the record in the WINS database */
588 ret = ldb_search(wins_db, winsdb_dn(tmp_ctx, name), LDB_SCOPE_BASE,
591 if (ret != LDB_SUCCESS || res->count > 1) {
592 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
594 } else if (res->count== 0) {
595 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
599 talloc_steal(tmp_ctx, res);
601 status = winsdb_record(h, res->msgs[0], tmp_ctx, now, &rec);
602 if (!NT_STATUS_IS_OK(status)) goto failed;
604 talloc_steal(mem_ctx, rec);
605 talloc_free(tmp_ctx);
610 talloc_free(tmp_ctx);
614 NTSTATUS winsdb_record(struct winsdb_handle *h, struct ldb_message *msg, TALLOC_CTX *mem_ctx, time_t now, struct winsdb_record **_rec)
617 struct winsdb_record *rec;
618 struct ldb_message_element *el;
619 struct nbt_name *name;
620 uint32_t i, j, num_values;
622 rec = talloc(mem_ctx, struct winsdb_record);
624 status = NT_STATUS_NO_MEMORY;
628 status = winsdb_nbt_name(rec, msg->dn, &name);
629 if (!NT_STATUS_IS_OK(status)) goto failed;
631 if (strlen(name->name) > 15) {
632 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
635 if (name->scope && strlen(name->scope) > 238) {
636 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
640 /* parse it into a more convenient winsdb_record structure */
642 rec->type = ldb_msg_find_attr_as_int(msg, "recordType", WREPL_TYPE_UNIQUE);
643 rec->state = ldb_msg_find_attr_as_int(msg, "recordState", WREPL_STATE_RELEASED);
644 rec->node = ldb_msg_find_attr_as_int(msg, "nodeType", WREPL_NODE_B);
645 rec->is_static = ldb_msg_find_attr_as_int(msg, "isStatic", 0);
646 rec->expire_time = ldb_string_to_time(ldb_msg_find_attr_as_string(msg, "expireTime", NULL));
647 rec->version = ldb_msg_find_attr_as_uint64(msg, "versionID", 0);
648 rec->wins_owner = ldb_msg_find_attr_as_string(msg, "winsOwner", NULL);
649 rec->registered_by = ldb_msg_find_attr_as_string(msg, "registeredBy", NULL);
650 talloc_steal(rec, rec->wins_owner);
651 talloc_steal(rec, rec->registered_by);
653 if (!rec->wins_owner || strcmp(rec->wins_owner, "0.0.0.0") == 0) {
654 rec->wins_owner = h->local_owner;
657 el = ldb_msg_find_element(msg, "address");
659 num_values = el->num_values;
664 if (rec->type == WREPL_TYPE_UNIQUE || rec->type == WREPL_TYPE_GROUP) {
665 if (num_values != 1) {
666 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
670 if (rec->state == WREPL_STATE_ACTIVE) {
671 if (num_values < 1) {
672 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
676 if (num_values > 25) {
677 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
681 /* see if it has already expired */
682 if (!rec->is_static &&
683 rec->expire_time <= now &&
684 rec->state == WREPL_STATE_ACTIVE) {
685 DEBUG(5,("WINS: expiring name %s (expired at %s)\n",
686 nbt_name_string(mem_ctx, rec->name), timestring(mem_ctx, rec->expire_time)));
687 rec->state = WREPL_STATE_RELEASED;
690 rec->addresses = talloc_array(rec, struct winsdb_addr *, num_values+1);
691 if (rec->addresses == NULL) {
692 status = NT_STATUS_NO_MEMORY;
696 for (i=0,j=0;i<num_values;i++) {
697 status = winsdb_addr_decode(h, rec, &el->values[i], rec->addresses, &rec->addresses[j]);
698 if (!NT_STATUS_IS_OK(status)) goto failed;
701 * the record isn't static and is active
702 * then don't add the address if it's expired
704 if (!rec->is_static &&
705 rec->addresses[j]->expire_time <= now &&
706 rec->state == WREPL_STATE_ACTIVE) {
707 DEBUG(5,("WINS: expiring name addr %s of %s (expired at %s)\n",
708 rec->addresses[j]->address, nbt_name_string(rec->addresses[j], rec->name),
709 timestring(rec->addresses[j], rec->addresses[j]->expire_time)));
710 talloc_free(rec->addresses[j]);
711 rec->addresses[j] = NULL;
716 rec->addresses[j] = NULL;
719 if (rec->is_static && rec->state == WREPL_STATE_ACTIVE) {
720 rec->expire_time = get_time_t_max();
721 for (i=0;rec->addresses[i];i++) {
722 rec->addresses[i]->expire_time = rec->expire_time;
726 if (rec->state == WREPL_STATE_ACTIVE) {
727 if (num_values < 1) {
728 DEBUG(5,("WINS: expiring name %s (because it has no active addresses)\n",
729 nbt_name_string(mem_ctx, rec->name)));
730 rec->state = WREPL_STATE_RELEASED;
737 if (NT_STATUS_EQUAL(NT_STATUS_INTERNAL_DB_CORRUPTION, status)) {
738 DEBUG(1,("winsdb_record: corrupted record: %s\n", ldb_dn_linearize(rec, msg->dn)));
745 form a ldb_message from a winsdb_record
747 struct ldb_message *winsdb_message(struct ldb_context *ldb,
748 struct winsdb_record *rec, TALLOC_CTX *mem_ctx)
752 const char *expire_time;
753 struct ldb_message *msg = ldb_msg_new(mem_ctx);
754 if (msg == NULL) goto failed;
756 /* make sure we don't put in corrupted records */
757 addr_count = winsdb_addr_list_length(rec->addresses);
758 if (rec->state == WREPL_STATE_ACTIVE && addr_count == 0) {
759 rec->state = WREPL_STATE_RELEASED;
761 if (rec->type == WREPL_TYPE_UNIQUE && addr_count > 1) {
762 rec->type = WREPL_TYPE_MHOMED;
765 expire_time = ldb_timestring(msg, rec->expire_time);
770 msg->dn = winsdb_dn(msg, rec->name);
771 if (msg->dn == NULL) goto failed;
772 ret |= ldb_msg_add_fmt(msg, "type", "0x%02X", rec->name->type);
773 if (rec->name->name && *rec->name->name) {
774 ret |= ldb_msg_add_string(msg, "name", rec->name->name);
776 if (rec->name->scope && *rec->name->scope) {
777 ret |= ldb_msg_add_string(msg, "scope", rec->name->scope);
779 ret |= ldb_msg_add_fmt(msg, "objectClass", "winsRecord");
780 ret |= ldb_msg_add_fmt(msg, "recordType", "%u", rec->type);
781 ret |= ldb_msg_add_fmt(msg, "recordState", "%u", rec->state);
782 ret |= ldb_msg_add_fmt(msg, "nodeType", "%u", rec->node);
783 ret |= ldb_msg_add_fmt(msg, "isStatic", "%u", rec->is_static);
784 ret |= ldb_msg_add_empty(msg, "expireTime", 0);
785 if (!(rec->is_static && rec->state == WREPL_STATE_ACTIVE)) {
786 ret |= ldb_msg_add_string(msg, "expireTime", expire_time);
788 ret |= ldb_msg_add_fmt(msg, "versionID", "%llu", (long long)rec->version);
789 ret |= ldb_msg_add_string(msg, "winsOwner", rec->wins_owner);
790 ret |= ldb_msg_add_empty(msg, "address", 0);
791 for (i=0;rec->addresses[i];i++) {
792 ret |= ldb_msg_add_winsdb_addr(msg, rec, "address", rec->addresses[i]);
794 ret |= ldb_msg_add_empty(msg, "registeredBy", 0);
795 if (rec->registered_by) {
796 ret |= ldb_msg_add_string(msg, "registeredBy", rec->registered_by);
797 if (ret != 0) goto failed;
807 save a WINS record into the database
809 uint8_t winsdb_add(struct winsdb_handle *h, struct winsdb_record *rec, uint32_t flags)
811 struct ldb_message *msg;
812 struct ldb_context *wins_db = h->ldb;
813 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
817 trans = ldb_transaction_start(wins_db);
818 if (trans != LDB_SUCCESS) goto failed;
820 if (flags & WINSDB_FLAG_ALLOC_VERSION) {
821 /* passing '0' means auto-allocate a new one */
822 rec->version = winsdb_set_maxVersion(h, 0);
823 if (rec->version == 0) goto failed;
825 if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
826 rec->wins_owner = h->local_owner;
829 msg = winsdb_message(wins_db, rec, tmp_ctx);
830 if (msg == NULL) goto failed;
831 ret = ldb_add(wins_db, msg);
832 if (ret != 0) goto failed;
834 trans = ldb_transaction_commit(wins_db);
835 if (trans != LDB_SUCCESS) goto failed;
837 wins_hook(h, rec, WINS_HOOK_ADD);
839 talloc_free(tmp_ctx);
843 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
844 talloc_free(tmp_ctx);
845 return NBT_RCODE_SVR;
850 modify a WINS record in the database
852 uint8_t winsdb_modify(struct winsdb_handle *h, struct winsdb_record *rec, uint32_t flags)
854 struct ldb_message *msg;
855 struct ldb_context *wins_db = h->ldb;
856 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
861 trans = ldb_transaction_start(wins_db);
862 if (trans != LDB_SUCCESS) goto failed;
864 if (flags & WINSDB_FLAG_ALLOC_VERSION) {
865 /* passing '0' means auto-allocate a new one */
866 rec->version = winsdb_set_maxVersion(h, 0);
867 if (rec->version == 0) goto failed;
869 if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
870 rec->wins_owner = h->local_owner;
873 msg = winsdb_message(wins_db, rec, tmp_ctx);
874 if (msg == NULL) goto failed;
876 for (i=0;i<msg->num_elements;i++) {
877 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
880 ret = ldb_modify(wins_db, msg);
881 if (ret != 0) goto failed;
883 trans = ldb_transaction_commit(wins_db);
884 if (trans != LDB_SUCCESS) goto failed;
886 wins_hook(h, rec, WINS_HOOK_MODIFY);
888 talloc_free(tmp_ctx);
892 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
893 talloc_free(tmp_ctx);
894 return NBT_RCODE_SVR;
899 delete a WINS record from the database
901 uint8_t winsdb_delete(struct winsdb_handle *h, struct winsdb_record *rec)
903 struct ldb_context *wins_db = h->ldb;
904 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
905 const struct ldb_dn *dn;
909 trans = ldb_transaction_start(wins_db);
910 if (trans != LDB_SUCCESS) goto failed;
912 dn = winsdb_dn(tmp_ctx, rec->name);
913 if (dn == NULL) goto failed;
915 ret = ldb_delete(wins_db, dn);
916 if (ret != 0) goto failed;
918 trans = ldb_transaction_commit(wins_db);
919 if (trans != LDB_SUCCESS) goto failed;
921 wins_hook(h, rec, WINS_HOOK_DELETE);
923 talloc_free(tmp_ctx);
927 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
928 talloc_free(tmp_ctx);
929 return NBT_RCODE_SVR;
932 static BOOL winsdb_check_or_add_module_list(struct winsdb_handle *h)
937 struct ldb_result *res = NULL;
938 struct ldb_message *msg = NULL;
939 TALLOC_CTX *tmp_ctx = talloc_new(h);
940 unsigned int flags = 0;
942 trans = ldb_transaction_start(h->ldb);
943 if (trans != LDB_SUCCESS) goto failed;
945 /* check if we have a special @MODULES record already */
946 dn = ldb_dn_explode(tmp_ctx, "@MODULES");
947 if (!dn) goto failed;
949 /* find the record in the WINS database */
950 ret = ldb_search(h->ldb, dn, LDB_SCOPE_BASE, NULL, NULL, &res);
951 if (ret != LDB_SUCCESS) goto failed;
952 talloc_steal(tmp_ctx, res);
954 if (res->count > 0) goto skip;
956 /* if there's no record, add one */
957 msg = ldb_msg_new(tmp_ctx);
958 if (!msg) goto failed;
961 ret = ldb_msg_add_string(msg, "@LIST", "wins_ldb");
962 if (ret != 0) goto failed;
964 ret = ldb_add(h->ldb, msg);
965 if (ret != 0) goto failed;
967 trans = ldb_transaction_commit(h->ldb);
968 if (trans != LDB_SUCCESS) goto failed;
970 /* close and reopen the database, with the modules */
971 trans = LDB_ERR_OTHER;
975 if (lp_parm_bool(-1,"winsdb", "nosync", False)) {
976 flags |= LDB_FLG_NOSYNC;
979 h->ldb = ldb_wrap_connect(h, lock_path(h, lp_wins_url()),
980 NULL, NULL, flags, NULL);
981 if (!h->ldb) goto failed;
983 talloc_free(tmp_ctx);
987 if (trans == LDB_SUCCESS) ldb_transaction_cancel(h->ldb);
988 talloc_free(tmp_ctx);
992 if (trans == LDB_SUCCESS) ldb_transaction_cancel(h->ldb);
993 talloc_free(tmp_ctx);
997 struct winsdb_handle *winsdb_connect(TALLOC_CTX *mem_ctx, enum winsdb_handle_caller caller)
999 struct winsdb_handle *h = NULL;
1001 unsigned int flags = 0;
1005 h = talloc(mem_ctx, struct winsdb_handle);
1006 if (!h) return NULL;
1008 if (lp_parm_bool(-1,"winsdb", "nosync", False)) {
1009 flags |= LDB_FLG_NOSYNC;
1012 h->ldb = ldb_wrap_connect(h, lock_path(h, lp_wins_url()),
1013 NULL, NULL, flags, NULL);
1014 if (!h->ldb) goto failed;
1018 owner = lp_parm_string(-1, "winsdb", "local_owner");
1020 owner = iface_n_ip(0);
1023 h->local_owner = talloc_strdup(h, owner);
1024 if (!h->local_owner) goto failed;
1026 /* make sure the module list is available and used */
1027 ret = winsdb_check_or_add_module_list(h);
1028 if (!ret) goto failed;
1030 ldb_err = ldb_set_opaque(h->ldb, "winsdb_handle", h);
1031 if (ldb_err != LDB_SUCCESS) goto failed;