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"
26 #include "lib/ldb/include/ldb.h"
27 #include "lib/ldb/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"
34 uint64_t winsdb_get_maxVersion(struct winsdb_handle *h)
37 struct ldb_context *ldb = h->ldb;
39 struct ldb_result *res = NULL;
40 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
41 uint64_t maxVersion = 0;
43 dn = ldb_dn_new(tmp_ctx, ldb, "CN=VERSION");
46 /* find the record in the WINS database */
47 ret = ldb_search(ldb, dn, LDB_SCOPE_BASE,
49 if (ret != LDB_SUCCESS) goto failed;
50 talloc_steal(tmp_ctx, res);
51 if (res->count > 1) goto failed;
53 if (res->count == 1) {
54 maxVersion = ldb_msg_find_attr_as_uint64(res->msgs[0], "maxVersion", 0);
63 if newVersion == 0 return the old maxVersion + 1 and save it
64 if newVersion > 0 return MAX(oldMaxVersion, newMaxVersion) and save it
66 uint64_t winsdb_set_maxVersion(struct winsdb_handle *h, uint64_t newMaxVersion)
71 struct ldb_result *res = NULL;
72 struct ldb_message *msg = NULL;
73 struct ldb_context *wins_db = h->ldb;
74 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
75 uint64_t oldMaxVersion = 0;
77 trans = ldb_transaction_start(wins_db);
78 if (trans != LDB_SUCCESS) goto failed;
80 dn = ldb_dn_new(tmp_ctx, wins_db, "CN=VERSION");
83 /* find the record in the WINS database */
84 ret = ldb_search(wins_db, dn, LDB_SCOPE_BASE, NULL, NULL, &res);
86 if (ret != LDB_SUCCESS) goto failed;
87 talloc_steal(tmp_ctx, res);
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, NULL);
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, NULL);
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_new(tmp_ctx, ldb, "@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 ldb_context *ldb, struct nbt_name *name)
167 dn = ldb_dn_new_fmt(mem_ctx, ldb, "type=0x%02X", name->type);
168 if (ldb_dn_is_valid(dn) && name->name && *name->name) {
169 ldb_dn_add_child_fmt(dn, "name=%s", name->name);
171 if (ldb_dn_is_valid(dn) && name->scope && *name->scope) {
172 ldb_dn_add_child_fmt(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;
181 unsigned int comp_num;
184 name = talloc(mem_ctx, struct nbt_name);
186 status = NT_STATUS_NO_MEMORY;
190 comp_num = ldb_dn_get_comp_num(dn);
193 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
197 if (comp_num > cur && strcasecmp("scope", ldb_dn_get_component_name(dn, cur)) == 0) {
198 name->scope = (const char *)talloc_strdup(name, (char *)ldb_dn_get_component_val(dn, cur)->data);
204 if (comp_num > cur && strcasecmp("name", ldb_dn_get_component_name(dn, cur)) == 0) {
205 name->name = (const char *)talloc_strdup(name, (char *)ldb_dn_get_component_val(dn, cur)->data);
208 name->name = talloc_strdup(name, "");
210 status = NT_STATUS_NO_MEMORY;
215 if (comp_num > cur && strcasecmp("type", ldb_dn_get_component_name(dn, cur)) == 0) {
216 name->type = strtoul((char *)ldb_dn_get_component_val(dn, cur)->data, NULL, 0);
219 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
231 decode the winsdb_addr("address") attribute:
233 "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
236 static NTSTATUS winsdb_addr_decode(struct winsdb_handle *h, struct winsdb_record *rec, struct ldb_val *val,
237 TALLOC_CTX *mem_ctx, struct winsdb_addr **_addr)
240 struct winsdb_addr *addr;
242 const char *wins_owner;
243 const char *expire_time;
246 addr = talloc(mem_ctx, struct winsdb_addr);
248 status = NT_STATUS_NO_MEMORY;
252 address = (char *)val->data;
254 p = strchr(address, ';');
256 /* support old entries, with only the address */
257 addr->address = (const char *)talloc_steal(addr, val->data);
258 addr->wins_owner = talloc_reference(addr, rec->wins_owner);
259 if (!addr->wins_owner) {
260 status = NT_STATUS_NO_MEMORY;
263 addr->expire_time = rec->expire_time;
269 addr->address = talloc_strdup(addr, address);
270 if (!addr->address) {
271 status = NT_STATUS_NO_MEMORY;
275 if (strncmp("winsOwner:", p, 10) != 0) {
276 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
280 p = strchr(wins_owner, ';');
282 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
287 if (strcmp(wins_owner, "0.0.0.0") == 0) {
288 wins_owner = h->local_owner;
290 addr->wins_owner = talloc_strdup(addr, wins_owner);
291 if (!addr->wins_owner) {
292 status = NT_STATUS_NO_MEMORY;
296 if (strncmp("expireTime:", p, 11) != 0) {
297 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
301 expire_time = p + 11;
302 p = strchr(expire_time, ';');
304 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
309 addr->expire_time = ldb_string_to_time(expire_time);
319 encode the winsdb_addr("address") attribute like this:
321 "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
325 static int ldb_msg_add_winsdb_addr(struct ldb_message *msg, struct winsdb_record *rec,
326 const char *attr_name, struct winsdb_addr *addr)
331 if (rec->is_static) {
332 str = talloc_strdup(msg, addr->address);
336 expire_time = ldb_timestring(msg, addr->expire_time);
337 if (!expire_time) return -1;
338 str = talloc_asprintf(msg, "%s;winsOwner:%s;expireTime:%s;",
339 addr->address, addr->wins_owner,
341 talloc_free(expire_time);
345 val.data = discard_const_p(uint8_t, str);
346 val.length = strlen(str);
348 return ldb_msg_add_value(msg, attr_name, &val, NULL);
351 struct winsdb_addr **winsdb_addr_list_make(TALLOC_CTX *mem_ctx)
353 struct winsdb_addr **addresses;
355 addresses = talloc_array(mem_ctx, struct winsdb_addr *, 1);
356 if (!addresses) return NULL;
363 static int winsdb_addr_sort_list (struct winsdb_addr **p1, struct winsdb_addr **p2, void *opaque)
365 struct winsdb_addr *a1 = talloc_get_type(*p1, struct winsdb_addr);
366 struct winsdb_addr *a2 = talloc_get_type(*p2, struct winsdb_addr);
367 struct winsdb_handle *h= talloc_get_type(opaque, struct winsdb_handle);
368 BOOL a1_owned = False;
369 BOOL a2_owned = False;
372 * first the owned addresses with the newest to the oldest address
373 * then the replica addresses with the newest to the oldest address
375 if (a2->expire_time != a1->expire_time) {
376 return a2->expire_time - a1->expire_time;
379 if (strcmp(a2->wins_owner, h->local_owner) == 0) {
383 if (strcmp(a1->wins_owner, h->local_owner) == 0) {
387 return a2_owned - a1_owned;
390 struct winsdb_addr **winsdb_addr_list_add(struct winsdb_handle *h, const struct winsdb_record *rec,
391 struct winsdb_addr **addresses, const char *address,
392 const char *wins_owner, time_t expire_time,
393 BOOL is_name_registration)
395 struct winsdb_addr *old_addr = NULL;
398 BOOL found_old_replica = False;
401 * count the addresses and maybe
402 * find an old entry for the new address
404 for (i=0; addresses[i]; i++) {
405 if (old_addr) continue;
406 if (strcmp(addresses[i]->address, address) == 0) {
407 old_addr = addresses[i];
413 * the address is already there
414 * and we can replace it
417 goto remove_old_addr;
421 * if we don't have 25 addresses already,
422 * we can just add the new address
429 * if we haven't found the address,
430 * and we have already have 25 addresses
431 * if so then we need to do the following:
432 * - if it isn't a name registration, then just ignore the new address
433 * - if it is a name registration, then first search for
434 * the oldest replica and if there's no replica address
435 * search the oldest owned address
437 if (!is_name_registration) {
442 * find the oldest replica address, if there's no replica
443 * record at all, find the oldest owned address
445 for (i=0; addresses[i]; i++) {
446 BOOL cur_is_replica = False;
447 /* find out if the current address is a replica */
448 if (strcmp(addresses[i]->wins_owner, h->local_owner) != 0) {
449 cur_is_replica = True;
453 * if we already found a replica address and the current address
454 * is not a replica, then skip it
456 if (found_old_replica && !cur_is_replica) continue;
459 * if we found the first replica address, reset the address
460 * that would be replaced
462 if (!found_old_replica && cur_is_replica) {
463 found_old_replica = True;
464 old_addr = addresses[i];
469 * if the first address isn't a replica, just start with
473 old_addr = addresses[i];
478 * see if we find an older address
480 if (addresses[i]->expire_time < old_addr->expire_time) {
481 old_addr = addresses[i];
487 winsdb_addr_list_remove(addresses, old_addr->address);
491 addresses = talloc_realloc(addresses, addresses, struct winsdb_addr *, len + 2);
492 if (!addresses) return NULL;
494 addresses[len] = talloc(addresses, struct winsdb_addr);
495 if (!addresses[len]) {
496 talloc_free(addresses);
500 addresses[len]->address = talloc_strdup(addresses[len], address);
501 if (!addresses[len]->address) {
502 talloc_free(addresses);
506 addresses[len]->wins_owner = talloc_strdup(addresses[len], wins_owner);
507 if (!addresses[len]->wins_owner) {
508 talloc_free(addresses);
512 addresses[len]->expire_time = expire_time;
514 addresses[len+1] = NULL;
516 ldb_qsort(addresses, len+1 , sizeof(addresses[0]), h, (ldb_qsort_cmp_fn_t)winsdb_addr_sort_list);
521 void winsdb_addr_list_remove(struct winsdb_addr **addresses, const char *address)
525 for (i=0; addresses[i]; i++) {
526 if (strcmp(addresses[i]->address, address) == 0) {
531 for (; addresses[i]; i++) {
532 addresses[i] = addresses[i+1];
538 struct winsdb_addr *winsdb_addr_list_check(struct winsdb_addr **addresses, const char *address)
542 for (i=0; addresses[i]; i++) {
543 if (strcmp(addresses[i]->address, address) == 0) {
551 size_t winsdb_addr_list_length(struct winsdb_addr **addresses)
554 for (i=0; addresses[i]; i++);
558 const char **winsdb_addr_string_list(TALLOC_CTX *mem_ctx, struct winsdb_addr **addresses)
560 size_t len = winsdb_addr_list_length(addresses);
561 const char **str_list=NULL;
564 for (i=0; i < len; i++) {
565 str_list = str_list_add(str_list, addresses[i]->address);
570 talloc_steal(mem_ctx, str_list);
575 load a WINS entry from the database
577 NTSTATUS winsdb_lookup(struct winsdb_handle *h,
578 struct nbt_name *name,
580 struct winsdb_record **_rec)
583 struct ldb_result *res = NULL;
585 struct winsdb_record *rec;
586 struct ldb_context *wins_db = h->ldb;
587 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
588 time_t now = time(NULL);
590 /* find the record in the WINS database */
591 ret = ldb_search(wins_db, winsdb_dn(tmp_ctx, wins_db, name), LDB_SCOPE_BASE,
594 talloc_steal(tmp_ctx, res);
595 if (ret != LDB_SUCCESS || res->count > 1) {
596 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
598 } else if (res->count== 0) {
599 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
603 status = winsdb_record(h, res->msgs[0], tmp_ctx, now, &rec);
604 if (!NT_STATUS_IS_OK(status)) goto failed;
606 talloc_steal(mem_ctx, rec);
607 talloc_free(tmp_ctx);
612 talloc_free(tmp_ctx);
616 NTSTATUS winsdb_record(struct winsdb_handle *h, struct ldb_message *msg, TALLOC_CTX *mem_ctx, time_t now, struct winsdb_record **_rec)
619 struct winsdb_record *rec;
620 struct ldb_message_element *el;
621 struct nbt_name *name;
622 uint32_t i, j, num_values;
623 BOOL we_are_owner = False;
625 rec = talloc(mem_ctx, struct winsdb_record);
627 status = NT_STATUS_NO_MEMORY;
631 status = winsdb_nbt_name(rec, msg->dn, &name);
632 if (!NT_STATUS_IS_OK(status)) goto failed;
634 if (strlen(name->name) > 15) {
635 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
638 if (name->scope && strlen(name->scope) > 238) {
639 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
643 /* parse it into a more convenient winsdb_record structure */
645 rec->type = ldb_msg_find_attr_as_int(msg, "recordType", WREPL_TYPE_UNIQUE);
646 rec->state = ldb_msg_find_attr_as_int(msg, "recordState", WREPL_STATE_RELEASED);
647 rec->node = ldb_msg_find_attr_as_int(msg, "nodeType", WREPL_NODE_B);
648 rec->is_static = ldb_msg_find_attr_as_int(msg, "isStatic", 0);
649 rec->expire_time = ldb_string_to_time(ldb_msg_find_attr_as_string(msg, "expireTime", NULL));
650 rec->version = ldb_msg_find_attr_as_uint64(msg, "versionID", 0);
651 rec->wins_owner = ldb_msg_find_attr_as_string(msg, "winsOwner", NULL);
652 rec->registered_by = ldb_msg_find_attr_as_string(msg, "registeredBy", NULL);
653 talloc_steal(rec, rec->wins_owner);
654 talloc_steal(rec, rec->registered_by);
656 if (!rec->wins_owner || strcmp(rec->wins_owner, "0.0.0.0") == 0) {
657 rec->wins_owner = h->local_owner;
660 el = ldb_msg_find_element(msg, "address");
662 num_values = el->num_values;
667 if (rec->type == WREPL_TYPE_UNIQUE || rec->type == WREPL_TYPE_GROUP) {
668 if (num_values != 1) {
669 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
673 if (rec->state == WREPL_STATE_ACTIVE) {
674 if (num_values < 1) {
675 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
679 if (num_values > 25) {
680 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
684 if (strcmp(rec->wins_owner, h->local_owner) == 0) {
689 * see if it has already expired
691 * NOTE: only expire owned records this way!
692 * w2k3 resolves expired replicas
693 * which are in active state
695 if (!rec->is_static &&
696 rec->expire_time <= now &&
697 rec->state == WREPL_STATE_ACTIVE &&
699 DEBUG(5,("WINS: expiring name %s (expired at %s)\n",
700 nbt_name_string(mem_ctx, rec->name), timestring(mem_ctx, rec->expire_time)));
701 rec->state = WREPL_STATE_RELEASED;
704 rec->addresses = talloc_array(rec, struct winsdb_addr *, num_values+1);
705 if (rec->addresses == NULL) {
706 status = NT_STATUS_NO_MEMORY;
710 for (i=0,j=0;i<num_values;i++) {
711 status = winsdb_addr_decode(h, rec, &el->values[i], rec->addresses, &rec->addresses[j]);
712 if (!NT_STATUS_IS_OK(status)) goto failed;
715 * the record isn't static and is active
716 * then don't add the address if it's expired
718 if (!rec->is_static &&
719 rec->addresses[j]->expire_time <= now &&
720 rec->state == WREPL_STATE_ACTIVE &&
722 DEBUG(5,("WINS: expiring name addr %s of %s (expired at %s)\n",
723 rec->addresses[j]->address, nbt_name_string(rec->addresses[j], rec->name),
724 timestring(rec->addresses[j], rec->addresses[j]->expire_time)));
725 talloc_free(rec->addresses[j]);
726 rec->addresses[j] = NULL;
731 rec->addresses[j] = NULL;
734 if (rec->is_static && rec->state == WREPL_STATE_ACTIVE) {
735 rec->expire_time = get_time_t_max();
736 for (i=0;rec->addresses[i];i++) {
737 rec->addresses[i]->expire_time = rec->expire_time;
741 if (rec->state == WREPL_STATE_ACTIVE) {
742 if (num_values < 1) {
743 DEBUG(5,("WINS: expiring name %s (because it has no active addresses)\n",
744 nbt_name_string(mem_ctx, rec->name)));
745 rec->state = WREPL_STATE_RELEASED;
752 if (NT_STATUS_EQUAL(NT_STATUS_INTERNAL_DB_CORRUPTION, status)) {
753 DEBUG(1,("winsdb_record: corrupted record: %s\n", ldb_dn_get_linearized(msg->dn)));
760 form a ldb_message from a winsdb_record
762 struct ldb_message *winsdb_message(struct ldb_context *ldb,
763 struct winsdb_record *rec, TALLOC_CTX *mem_ctx)
767 const char *expire_time;
768 struct ldb_message *msg = ldb_msg_new(mem_ctx);
769 if (msg == NULL) goto failed;
771 /* make sure we don't put in corrupted records */
772 addr_count = winsdb_addr_list_length(rec->addresses);
773 if (rec->state == WREPL_STATE_ACTIVE && addr_count == 0) {
774 rec->state = WREPL_STATE_RELEASED;
776 if (rec->type == WREPL_TYPE_UNIQUE && addr_count > 1) {
777 rec->type = WREPL_TYPE_MHOMED;
780 expire_time = ldb_timestring(msg, rec->expire_time);
785 msg->dn = winsdb_dn(msg, ldb, rec->name);
786 if (msg->dn == NULL) goto failed;
787 ret |= ldb_msg_add_fmt(msg, "type", "0x%02X", rec->name->type);
788 if (rec->name->name && *rec->name->name) {
789 ret |= ldb_msg_add_string(msg, "name", rec->name->name);
791 if (rec->name->scope && *rec->name->scope) {
792 ret |= ldb_msg_add_string(msg, "scope", rec->name->scope);
794 ret |= ldb_msg_add_fmt(msg, "objectClass", "winsRecord");
795 ret |= ldb_msg_add_fmt(msg, "recordType", "%u", rec->type);
796 ret |= ldb_msg_add_fmt(msg, "recordState", "%u", rec->state);
797 ret |= ldb_msg_add_fmt(msg, "nodeType", "%u", rec->node);
798 ret |= ldb_msg_add_fmt(msg, "isStatic", "%u", rec->is_static);
799 ret |= ldb_msg_add_empty(msg, "expireTime", 0, NULL);
800 if (!(rec->is_static && rec->state == WREPL_STATE_ACTIVE)) {
801 ret |= ldb_msg_add_string(msg, "expireTime", expire_time);
803 ret |= ldb_msg_add_fmt(msg, "versionID", "%llu", (long long)rec->version);
804 ret |= ldb_msg_add_string(msg, "winsOwner", rec->wins_owner);
805 ret |= ldb_msg_add_empty(msg, "address", 0, NULL);
806 for (i=0;rec->addresses[i];i++) {
807 ret |= ldb_msg_add_winsdb_addr(msg, rec, "address", rec->addresses[i]);
809 ret |= ldb_msg_add_empty(msg, "registeredBy", 0, NULL);
810 if (rec->registered_by) {
811 ret |= ldb_msg_add_string(msg, "registeredBy", rec->registered_by);
812 if (ret != 0) goto failed;
822 save a WINS record into the database
824 uint8_t winsdb_add(struct winsdb_handle *h, struct winsdb_record *rec, uint32_t flags)
826 struct ldb_message *msg;
827 struct ldb_context *wins_db = h->ldb;
828 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
832 trans = ldb_transaction_start(wins_db);
833 if (trans != LDB_SUCCESS) goto failed;
835 if (flags & WINSDB_FLAG_ALLOC_VERSION) {
836 /* passing '0' means auto-allocate a new one */
837 rec->version = winsdb_set_maxVersion(h, 0);
838 if (rec->version == 0) goto failed;
840 if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
841 rec->wins_owner = h->local_owner;
844 msg = winsdb_message(wins_db, rec, tmp_ctx);
845 if (msg == NULL) goto failed;
846 ret = ldb_add(wins_db, msg);
847 if (ret != 0) goto failed;
849 trans = ldb_transaction_commit(wins_db);
850 if (trans != LDB_SUCCESS) goto failed;
852 wins_hook(h, rec, WINS_HOOK_ADD);
854 talloc_free(tmp_ctx);
858 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
859 talloc_free(tmp_ctx);
860 return NBT_RCODE_SVR;
865 modify a WINS record in the database
867 uint8_t winsdb_modify(struct winsdb_handle *h, struct winsdb_record *rec, uint32_t flags)
869 struct ldb_message *msg;
870 struct ldb_context *wins_db = h->ldb;
871 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
876 trans = ldb_transaction_start(wins_db);
877 if (trans != LDB_SUCCESS) goto failed;
879 if (flags & WINSDB_FLAG_ALLOC_VERSION) {
880 /* passing '0' means auto-allocate a new one */
881 rec->version = winsdb_set_maxVersion(h, 0);
882 if (rec->version == 0) goto failed;
884 if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
885 rec->wins_owner = h->local_owner;
888 msg = winsdb_message(wins_db, rec, tmp_ctx);
889 if (msg == NULL) goto failed;
891 for (i=0;i<msg->num_elements;i++) {
892 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
895 ret = ldb_modify(wins_db, msg);
896 if (ret != 0) goto failed;
898 trans = ldb_transaction_commit(wins_db);
899 if (trans != LDB_SUCCESS) goto failed;
901 wins_hook(h, rec, WINS_HOOK_MODIFY);
903 talloc_free(tmp_ctx);
907 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
908 talloc_free(tmp_ctx);
909 return NBT_RCODE_SVR;
914 delete a WINS record from the database
916 uint8_t winsdb_delete(struct winsdb_handle *h, struct winsdb_record *rec)
918 struct ldb_context *wins_db = h->ldb;
919 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
924 trans = ldb_transaction_start(wins_db);
925 if (trans != LDB_SUCCESS) goto failed;
927 dn = winsdb_dn(tmp_ctx, wins_db, rec->name);
928 if (dn == NULL) goto failed;
930 ret = ldb_delete(wins_db, dn);
931 if (ret != 0) goto failed;
933 trans = ldb_transaction_commit(wins_db);
934 if (trans != LDB_SUCCESS) goto failed;
936 wins_hook(h, rec, WINS_HOOK_DELETE);
938 talloc_free(tmp_ctx);
942 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
943 talloc_free(tmp_ctx);
944 return NBT_RCODE_SVR;
947 static BOOL winsdb_check_or_add_module_list(struct winsdb_handle *h)
952 struct ldb_result *res = NULL;
953 struct ldb_message *msg = NULL;
954 TALLOC_CTX *tmp_ctx = talloc_new(h);
955 unsigned int flags = 0;
957 trans = ldb_transaction_start(h->ldb);
958 if (trans != LDB_SUCCESS) goto failed;
960 /* check if we have a special @MODULES record already */
961 dn = ldb_dn_new(tmp_ctx, h->ldb, "@MODULES");
962 if (!dn) goto failed;
964 /* find the record in the WINS database */
965 ret = ldb_search(h->ldb, dn, LDB_SCOPE_BASE, NULL, NULL, &res);
966 if (ret != LDB_SUCCESS) goto failed;
967 talloc_steal(tmp_ctx, res);
969 if (res->count > 0) goto skip;
971 /* if there's no record, add one */
972 msg = ldb_msg_new(tmp_ctx);
973 if (!msg) goto failed;
976 ret = ldb_msg_add_string(msg, "@LIST", "wins_ldb");
977 if (ret != 0) goto failed;
979 ret = ldb_add(h->ldb, msg);
980 if (ret != 0) goto failed;
982 trans = ldb_transaction_commit(h->ldb);
983 if (trans != LDB_SUCCESS) goto failed;
985 /* close and reopen the database, with the modules */
986 trans = LDB_ERR_OTHER;
990 if (lp_parm_bool(-1,"winsdb", "nosync", False)) {
991 flags |= LDB_FLG_NOSYNC;
994 h->ldb = ldb_wrap_connect(h, lock_path(h, lp_wins_url()),
995 NULL, NULL, flags, NULL);
996 if (!h->ldb) goto failed;
998 talloc_free(tmp_ctx);
1002 if (trans == LDB_SUCCESS) ldb_transaction_cancel(h->ldb);
1003 talloc_free(tmp_ctx);
1007 if (trans == LDB_SUCCESS) ldb_transaction_cancel(h->ldb);
1008 talloc_free(tmp_ctx);
1012 struct winsdb_handle *winsdb_connect(TALLOC_CTX *mem_ctx, enum winsdb_handle_caller caller)
1014 struct winsdb_handle *h = NULL;
1016 unsigned int flags = 0;
1020 h = talloc(mem_ctx, struct winsdb_handle);
1021 if (!h) return NULL;
1023 if (lp_parm_bool(-1,"winsdb", "nosync", False)) {
1024 flags |= LDB_FLG_NOSYNC;
1027 h->ldb = ldb_wrap_connect(h, lock_path(h, lp_wins_url()),
1028 NULL, NULL, flags, NULL);
1029 if (!h->ldb) goto failed;
1033 owner = lp_parm_string(-1, "winsdb", "local_owner");
1035 owner = iface_n_ip(0);
1038 h->local_owner = talloc_strdup(h, owner);
1039 if (!h->local_owner) goto failed;
1041 /* make sure the module list is available and used */
1042 ret = winsdb_check_or_add_module_list(h);
1043 if (!ret) goto failed;
1045 ldb_err = ldb_set_opaque(h->ldb, "winsdb_handle", h);
1046 if (ldb_err != LDB_SUCCESS) goto failed;