r13121: Tag 4.0.0TP1
[Samba.git] / source / nbt_server / wins / winsdb.c
blob625d4e68dcbdb116f69ccef8762f052be85b8b6c
1 /*
2 Unix SMB/CIFS implementation.
4 WINS database routines
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.
24 #include "includes.h"
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 "system/time.h"
31 uint64_t winsdb_get_maxVersion(struct winsdb_handle *h)
33 int ret;
34 struct ldb_context *ldb = h->ldb;
35 struct ldb_dn *dn;
36 struct ldb_result *res = NULL;
37 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
38 uint64_t maxVersion = 0;
40 dn = ldb_dn_explode(tmp_ctx, "CN=VERSION");
41 if (!dn) goto failed;
43 /* find the record in the WINS database */
44 ret = ldb_search(ldb, dn, LDB_SCOPE_BASE,
45 NULL, NULL, &res);
46 if (ret != LDB_SUCCESS) goto failed;
47 talloc_steal(tmp_ctx, res);
48 if (res->count > 1) goto failed;
50 if (res->count == 1) {
51 maxVersion = ldb_msg_find_uint64(res->msgs[0], "maxVersion", 0);
54 failed:
55 talloc_free(tmp_ctx);
56 return maxVersion;
60 if newVersion == 0 return the old maxVersion + 1 and save it
61 if newVersion > 0 return MAX(oldMaxVersion, newMaxVersion) and save it
63 uint64_t winsdb_set_maxVersion(struct winsdb_handle *h, uint64_t newMaxVersion)
65 int trans;
66 int ret;
67 struct ldb_dn *dn;
68 struct ldb_result *res = NULL;
69 struct ldb_message *msg = NULL;
70 struct ldb_context *wins_db = h->ldb;
71 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
72 uint64_t oldMaxVersion = 0;
74 trans = ldb_transaction_start(wins_db);
75 if (trans != LDB_SUCCESS) goto failed;
77 dn = ldb_dn_explode(tmp_ctx, "CN=VERSION");
78 if (!dn) goto failed;
80 /* find the record in the WINS database */
81 ret = ldb_search(wins_db, dn, LDB_SCOPE_BASE, NULL, NULL, &res);
83 if (ret != LDB_SUCCESS) goto failed;
84 if (res->count > 1) goto failed;
86 talloc_steal(tmp_ctx, res);
88 if (res->count == 1) {
89 oldMaxVersion = ldb_msg_find_uint64(res->msgs[0], "maxVersion", 0);
92 if (newMaxVersion == 0) {
93 newMaxVersion = oldMaxVersion + 1;
94 } else {
95 newMaxVersion = MAX(oldMaxVersion, newMaxVersion);
98 msg = ldb_msg_new(tmp_ctx);
99 if (!msg) goto failed;
100 msg->dn = dn;
103 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE);
104 if (ret != 0) goto failed;
105 ret = ldb_msg_add_string(msg, "objectClass", "winsMaxVersion");
106 if (ret != 0) goto failed;
107 ret = ldb_msg_add_empty(msg, "maxVersion", LDB_FLAG_MOD_REPLACE);
108 if (ret != 0) goto failed;
109 ret = ldb_msg_add_fmt(msg, "maxVersion", "%llu", (long long)newMaxVersion);
110 if (ret != 0) goto failed;
112 ret = ldb_modify(wins_db, msg);
113 if (ret != 0) ret = ldb_add(wins_db, msg);
114 if (ret != 0) goto failed;
116 trans = ldb_transaction_commit(wins_db);
117 if (trans != LDB_SUCCESS) goto failed;
119 talloc_free(tmp_ctx);
120 return newMaxVersion;
122 failed:
123 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
124 talloc_free(tmp_ctx);
125 return 0;
128 uint64_t winsdb_get_seqnumber(struct winsdb_handle *h)
130 int ret;
131 struct ldb_context *ldb = h->ldb;
132 struct ldb_dn *dn;
133 struct ldb_result *res = NULL;
134 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
135 uint64_t seqnumber = 0;
137 dn = ldb_dn_explode(tmp_ctx, "@BASEINFO");
138 if (!dn) goto failed;
140 /* find the record in the WINS database */
141 ret = ldb_search(ldb, dn, LDB_SCOPE_BASE,
142 NULL, NULL, &res);
143 if (ret != LDB_SUCCESS) goto failed;
144 talloc_steal(tmp_ctx, res);
145 if (res->count > 1) goto failed;
147 if (res->count == 1) {
148 seqnumber = ldb_msg_find_uint64(res->msgs[0], "sequenceNumber", 0);
151 failed:
152 talloc_free(tmp_ctx);
153 return seqnumber;
157 return a DN for a nbt_name
159 static struct ldb_dn *winsdb_dn(TALLOC_CTX *mem_ctx, struct nbt_name *name)
161 struct ldb_dn *dn;
163 dn = ldb_dn_string_compose(mem_ctx, NULL, "type=0x%02X", name->type);
164 if (dn && name->name && *name->name) {
165 dn = ldb_dn_string_compose(mem_ctx, dn, "name=%s", name->name);
167 if (dn && name->scope && *name->scope) {
168 dn = ldb_dn_string_compose(mem_ctx, dn, "scope=%s", name->scope);
170 return dn;
173 static NTSTATUS winsdb_nbt_name(TALLOC_CTX *mem_ctx, struct ldb_dn *dn, struct nbt_name **_name)
175 NTSTATUS status;
176 struct nbt_name *name;
177 uint32_t cur = 0;
179 name = talloc(mem_ctx, struct nbt_name);
180 if (!name) {
181 status = NT_STATUS_NO_MEMORY;
182 goto failed;
185 if (dn->comp_num > 3) {
186 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
187 goto failed;
190 if (dn->comp_num > cur && strcasecmp("scope", dn->components[cur].name) == 0) {
191 name->scope = talloc_steal(name, dn->components[cur].value.data);
192 cur++;
193 } else {
194 name->scope = NULL;
197 if (dn->comp_num > cur && strcasecmp("name", dn->components[cur].name) == 0) {
198 name->name = talloc_steal(name, dn->components[cur].value.data);
199 cur++;
200 } else {
201 name->name = talloc_strdup(name, "");
202 if (!name->name) {
203 status = NT_STATUS_NO_MEMORY;
204 goto failed;
208 if (dn->comp_num > cur && strcasecmp("type", dn->components[cur].name) == 0) {
209 name->type = strtoul((char *)dn->components[cur].value.data, NULL, 0);
210 cur++;
211 } else {
212 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
213 goto failed;
216 *_name = name;
217 return NT_STATUS_OK;
218 failed:
219 talloc_free(name);
220 return status;
224 decode the winsdb_addr("address") attribute:
225 "172.31.1.1" or
226 "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
227 are valid records
229 static NTSTATUS winsdb_addr_decode(struct winsdb_handle *h, struct winsdb_record *rec, struct ldb_val *val,
230 TALLOC_CTX *mem_ctx, struct winsdb_addr **_addr)
232 NTSTATUS status;
233 struct winsdb_addr *addr;
234 const char *address;
235 const char *wins_owner;
236 const char *expire_time;
237 char *p;
239 addr = talloc(mem_ctx, struct winsdb_addr);
240 if (!addr) {
241 status = NT_STATUS_NO_MEMORY;
242 goto failed;
245 address = (char *)val->data;
247 p = strchr(address, ';');
248 if (!p) {
249 /* support old entries, with only the address */
250 addr->address = talloc_steal(addr, val->data);
251 addr->wins_owner = talloc_reference(addr, rec->wins_owner);
252 if (!addr->wins_owner) {
253 status = NT_STATUS_NO_MEMORY;
254 goto failed;
256 addr->expire_time = rec->expire_time;
257 *_addr = addr;
258 return NT_STATUS_OK;
261 *p = '\0';p++;
262 addr->address = talloc_strdup(addr, address);
263 if (!addr->address) {
264 status = NT_STATUS_NO_MEMORY;
265 goto failed;
268 if (strncmp("winsOwner:", p, 10) != 0) {
269 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
270 goto failed;
272 wins_owner = p + 10;
273 p = strchr(wins_owner, ';');
274 if (!p) {
275 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
276 goto failed;
279 *p = '\0';p++;
280 if (strcmp(wins_owner, "0.0.0.0") == 0) {
281 wins_owner = h->local_owner;
283 addr->wins_owner = talloc_strdup(addr, wins_owner);
284 if (!addr->wins_owner) {
285 status = NT_STATUS_NO_MEMORY;
286 goto failed;
289 if (strncmp("expireTime:", p, 11) != 0) {
290 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
291 goto failed;
294 expire_time = p + 11;
295 p = strchr(expire_time, ';');
296 if (!p) {
297 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
298 goto failed;
301 *p = '\0';p++;
302 addr->expire_time = ldb_string_to_time(expire_time);
304 *_addr = addr;
305 return NT_STATUS_OK;
306 failed:
307 talloc_free(addr);
308 return status;
312 encode the winsdb_addr("address") attribute like this:
313 non-static record:
314 "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
315 static record:
316 "172.31.1.1"
318 static int ldb_msg_add_winsdb_addr(struct ldb_message *msg, struct winsdb_record *rec,
319 const char *attr_name, struct winsdb_addr *addr)
321 struct ldb_val val;
322 const char *str;
324 if (rec->is_static) {
325 str = talloc_strdup(msg, addr->address);
326 if (!str) return -1;
327 } else {
328 char *expire_time;
329 expire_time = ldb_timestring(msg, addr->expire_time);
330 if (!expire_time) return -1;
331 str = talloc_asprintf(msg, "%s;winsOwner:%s;expireTime:%s;",
332 addr->address, addr->wins_owner,
333 expire_time);
334 talloc_free(expire_time);
335 if (!str) return -1;
338 val.data = discard_const_p(uint8_t, str);
339 val.length = strlen(str);
341 return ldb_msg_add_value(msg, attr_name, &val);
344 struct winsdb_addr **winsdb_addr_list_make(TALLOC_CTX *mem_ctx)
346 struct winsdb_addr **addresses;
348 addresses = talloc_array(mem_ctx, struct winsdb_addr *, 1);
349 if (!addresses) return NULL;
351 addresses[0] = NULL;
353 return addresses;
356 struct winsdb_addr **winsdb_addr_list_add(struct winsdb_addr **addresses, const char *address,
357 const char *wins_owner, time_t expire_time)
359 size_t len = winsdb_addr_list_length(addresses);
361 addresses = talloc_realloc(addresses, addresses, struct winsdb_addr *, len + 2);
362 if (!addresses) return NULL;
364 addresses[len] = talloc(addresses, struct winsdb_addr);
365 if (!addresses[len]) {
366 talloc_free(addresses);
367 return NULL;
370 addresses[len]->address = talloc_strdup(addresses[len], address);
371 if (!addresses[len]->address) {
372 talloc_free(addresses);
373 return NULL;
376 addresses[len]->wins_owner = talloc_strdup(addresses[len], wins_owner);
377 if (!addresses[len]->wins_owner) {
378 talloc_free(addresses);
379 return NULL;
382 addresses[len]->expire_time = expire_time;
384 addresses[len+1] = NULL;
386 return addresses;
389 void winsdb_addr_list_remove(struct winsdb_addr **addresses, const char *address)
391 size_t i;
393 for (i=0; addresses[i]; i++) {
394 if (strcmp(addresses[i]->address, address) == 0) {
395 break;
398 if (!addresses[i]) return;
400 for (; addresses[i]; i++) {
401 addresses[i] = addresses[i+1];
404 return;
407 struct winsdb_addr *winsdb_addr_list_check(struct winsdb_addr **addresses, const char *address)
409 size_t i;
411 for (i=0; addresses[i]; i++) {
412 if (strcmp(addresses[i]->address, address) == 0) {
413 return addresses[i];
417 return NULL;
420 size_t winsdb_addr_list_length(struct winsdb_addr **addresses)
422 size_t i;
423 for (i=0; addresses[i]; i++);
424 return i;
427 const char **winsdb_addr_string_list(TALLOC_CTX *mem_ctx, struct winsdb_addr **addresses)
429 size_t len = winsdb_addr_list_length(addresses);
430 const char **str_list=NULL;
431 size_t i;
433 for (i=0; i < len; i++) {
434 str_list = str_list_add(str_list, addresses[i]->address);
435 if (!str_list[i]) {
436 return NULL;
439 talloc_steal(mem_ctx, str_list);
440 return str_list;
444 load a WINS entry from the database
446 NTSTATUS winsdb_lookup(struct winsdb_handle *h,
447 struct nbt_name *name,
448 TALLOC_CTX *mem_ctx,
449 struct winsdb_record **_rec)
451 NTSTATUS status;
452 struct ldb_result *res = NULL;
453 int ret;
454 struct winsdb_record *rec;
455 struct ldb_context *wins_db = h->ldb;
456 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
457 time_t now = time(NULL);
459 /* find the record in the WINS database */
460 ret = ldb_search(wins_db, winsdb_dn(tmp_ctx, name), LDB_SCOPE_BASE,
461 NULL, NULL, &res);
463 if (ret != LDB_SUCCESS || res->count > 1) {
464 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
465 goto failed;
466 } else if (res->count== 0) {
467 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
468 goto failed;
471 talloc_steal(tmp_ctx, res);
473 status = winsdb_record(h, res->msgs[0], tmp_ctx, now, &rec);
474 if (!NT_STATUS_IS_OK(status)) goto failed;
476 talloc_steal(mem_ctx, rec);
477 talloc_free(tmp_ctx);
478 *_rec = rec;
479 return NT_STATUS_OK;
481 failed:
482 talloc_free(tmp_ctx);
483 return status;
486 NTSTATUS winsdb_record(struct winsdb_handle *h, struct ldb_message *msg, TALLOC_CTX *mem_ctx, time_t now, struct winsdb_record **_rec)
488 NTSTATUS status;
489 struct winsdb_record *rec;
490 struct ldb_message_element *el;
491 struct nbt_name *name;
492 uint32_t i, j, num_values;
494 rec = talloc(mem_ctx, struct winsdb_record);
495 if (rec == NULL) {
496 status = NT_STATUS_NO_MEMORY;
497 goto failed;
500 status = winsdb_nbt_name(rec, msg->dn, &name);
501 if (!NT_STATUS_IS_OK(status)) goto failed;
503 if (strlen(name->name) > 15) {
504 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
505 goto failed;
507 if (name->scope && strlen(name->scope) > 238) {
508 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
509 goto failed;
512 /* parse it into a more convenient winsdb_record structure */
513 rec->name = name;
514 rec->type = ldb_msg_find_int(msg, "recordType", WREPL_TYPE_UNIQUE);
515 rec->state = ldb_msg_find_int(msg, "recordState", WREPL_STATE_RELEASED);
516 rec->node = ldb_msg_find_int(msg, "nodeType", WREPL_NODE_B);
517 rec->is_static = ldb_msg_find_int(msg, "isStatic", 0);
518 rec->expire_time = ldb_string_to_time(ldb_msg_find_string(msg, "expireTime", NULL));
519 rec->version = ldb_msg_find_uint64(msg, "versionID", 0);
520 rec->wins_owner = ldb_msg_find_string(msg, "winsOwner", NULL);
521 rec->registered_by = ldb_msg_find_string(msg, "registeredBy", NULL);
522 talloc_steal(rec, rec->wins_owner);
523 talloc_steal(rec, rec->registered_by);
525 if (!rec->wins_owner || strcmp(rec->wins_owner, "0.0.0.0") == 0) {
526 rec->wins_owner = h->local_owner;
529 el = ldb_msg_find_element(msg, "address");
530 if (el) {
531 num_values = el->num_values;
532 } else {
533 num_values = 0;
536 if (rec->type == WREPL_TYPE_UNIQUE || rec->type == WREPL_TYPE_GROUP) {
537 if (num_values != 1) {
538 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
539 goto failed;
542 if (rec->state == WREPL_STATE_ACTIVE) {
543 if (num_values < 1) {
544 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
545 goto failed;
548 if (num_values > 25) {
549 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
550 goto failed;
553 /* see if it has already expired */
554 if (!rec->is_static &&
555 rec->expire_time <= now &&
556 rec->state == WREPL_STATE_ACTIVE) {
557 DEBUG(5,("WINS: expiring name %s (expired at %s)\n",
558 nbt_name_string(mem_ctx, rec->name), timestring(mem_ctx, rec->expire_time)));
559 rec->state = WREPL_STATE_RELEASED;
562 rec->addresses = talloc_array(rec, struct winsdb_addr *, num_values+1);
563 if (rec->addresses == NULL) {
564 status = NT_STATUS_NO_MEMORY;
565 goto failed;
568 for (i=0,j=0;i<num_values;i++) {
569 status = winsdb_addr_decode(h, rec, &el->values[i], rec->addresses, &rec->addresses[j]);
570 if (!NT_STATUS_IS_OK(status)) goto failed;
573 * the record isn't static and is active
574 * then don't add the address if it's expired
576 if (!rec->is_static &&
577 rec->addresses[j]->expire_time <= now &&
578 rec->state == WREPL_STATE_ACTIVE) {
579 DEBUG(5,("WINS: expiring name addr %s of %s (expired at %s)\n",
580 rec->addresses[j]->address, nbt_name_string(rec->addresses[j], rec->name),
581 timestring(rec->addresses[j], rec->addresses[j]->expire_time)));
582 talloc_free(rec->addresses[j]);
583 rec->addresses[j] = NULL;
584 continue;
586 j++;
588 rec->addresses[j] = NULL;
589 num_values = j;
591 if (rec->is_static && rec->state == WREPL_STATE_ACTIVE) {
592 rec->expire_time = get_time_t_max();
593 for (i=0;rec->addresses[i];i++) {
594 rec->addresses[i]->expire_time = rec->expire_time;
598 if (rec->state == WREPL_STATE_ACTIVE) {
599 if (num_values < 1) {
600 DEBUG(5,("WINS: expiring name %s (because it has no active addresses)\n",
601 nbt_name_string(mem_ctx, rec->name)));
602 rec->state = WREPL_STATE_RELEASED;
606 *_rec = rec;
607 return NT_STATUS_OK;
608 failed:
609 if (NT_STATUS_EQUAL(NT_STATUS_INTERNAL_DB_CORRUPTION, status)) {
610 DEBUG(1,("winsdb_record: corrupted record: %s\n", ldb_dn_linearize(rec, msg->dn)));
612 talloc_free(rec);
613 return status;
617 form a ldb_message from a winsdb_record
619 struct ldb_message *winsdb_message(struct ldb_context *ldb,
620 struct winsdb_record *rec, TALLOC_CTX *mem_ctx)
622 int i, ret=0;
623 size_t addr_count;
624 const char *expire_time;
625 struct ldb_message *msg = ldb_msg_new(mem_ctx);
626 if (msg == NULL) goto failed;
628 /* make sure we don't put in corrupted records */
629 addr_count = winsdb_addr_list_length(rec->addresses);
630 if (rec->state == WREPL_STATE_ACTIVE && addr_count == 0) {
631 rec->state = WREPL_STATE_RELEASED;
633 if (rec->type == WREPL_TYPE_UNIQUE && addr_count > 1) {
634 rec->type = WREPL_TYPE_MHOMED;
637 expire_time = ldb_timestring(msg, rec->expire_time);
638 if (!expire_time) {
639 goto failed;
642 msg->dn = winsdb_dn(msg, rec->name);
643 if (msg->dn == NULL) goto failed;
644 ret |= ldb_msg_add_fmt(msg, "type", "0x%02X", rec->name->type);
645 if (rec->name->name && *rec->name->name) {
646 ret |= ldb_msg_add_string(msg, "name", rec->name->name);
648 if (rec->name->scope && *rec->name->scope) {
649 ret |= ldb_msg_add_string(msg, "scope", rec->name->scope);
651 ret |= ldb_msg_add_fmt(msg, "objectClass", "winsRecord");
652 ret |= ldb_msg_add_fmt(msg, "recordType", "%u", rec->type);
653 ret |= ldb_msg_add_fmt(msg, "recordState", "%u", rec->state);
654 ret |= ldb_msg_add_fmt(msg, "nodeType", "%u", rec->node);
655 ret |= ldb_msg_add_fmt(msg, "isStatic", "%u", rec->is_static);
656 ret |= ldb_msg_add_empty(msg, "expireTime", 0);
657 if (!(rec->is_static && rec->state == WREPL_STATE_ACTIVE)) {
658 ret |= ldb_msg_add_string(msg, "expireTime", expire_time);
660 ret |= ldb_msg_add_fmt(msg, "versionID", "%llu", (long long)rec->version);
661 ret |= ldb_msg_add_string(msg, "winsOwner", rec->wins_owner);
662 ret |= ldb_msg_add_empty(msg, "address", 0);
663 for (i=0;rec->addresses[i];i++) {
664 ret |= ldb_msg_add_winsdb_addr(msg, rec, "address", rec->addresses[i]);
666 ret |= ldb_msg_add_empty(msg, "registeredBy", 0);
667 if (rec->registered_by) {
668 ret |= ldb_msg_add_string(msg, "registeredBy", rec->registered_by);
669 if (ret != 0) goto failed;
671 return msg;
673 failed:
674 talloc_free(msg);
675 return NULL;
679 save a WINS record into the database
681 uint8_t winsdb_add(struct winsdb_handle *h, struct winsdb_record *rec, uint32_t flags)
683 struct ldb_message *msg;
684 struct ldb_context *wins_db = h->ldb;
685 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
686 int trans = -1;
687 int ret = 0;
689 trans = ldb_transaction_start(wins_db);
690 if (trans != LDB_SUCCESS) goto failed;
692 if (flags & WINSDB_FLAG_ALLOC_VERSION) {
693 /* passing '0' means auto-allocate a new one */
694 rec->version = winsdb_set_maxVersion(h, 0);
695 if (rec->version == 0) goto failed;
697 if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
698 rec->wins_owner = h->local_owner;
701 msg = winsdb_message(wins_db, rec, tmp_ctx);
702 if (msg == NULL) goto failed;
703 ret = ldb_add(wins_db, msg);
704 if (ret != 0) goto failed;
706 trans = ldb_transaction_commit(wins_db);
707 if (trans != LDB_SUCCESS) goto failed;
709 wins_hook(h, rec, WINS_HOOK_ADD);
711 talloc_free(tmp_ctx);
712 return NBT_RCODE_OK;
714 failed:
715 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
716 talloc_free(tmp_ctx);
717 return NBT_RCODE_SVR;
722 modify a WINS record in the database
724 uint8_t winsdb_modify(struct winsdb_handle *h, struct winsdb_record *rec, uint32_t flags)
726 struct ldb_message *msg;
727 struct ldb_context *wins_db = h->ldb;
728 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
729 int trans;
730 int ret;
731 int i;
733 trans = ldb_transaction_start(wins_db);
734 if (trans != LDB_SUCCESS) goto failed;
736 if (flags & WINSDB_FLAG_ALLOC_VERSION) {
737 /* passing '0' means auto-allocate a new one */
738 rec->version = winsdb_set_maxVersion(h, 0);
739 if (rec->version == 0) goto failed;
741 if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
742 rec->wins_owner = h->local_owner;
745 msg = winsdb_message(wins_db, rec, tmp_ctx);
746 if (msg == NULL) goto failed;
748 for (i=0;i<msg->num_elements;i++) {
749 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
752 ret = ldb_modify(wins_db, msg);
753 if (ret != 0) goto failed;
755 trans = ldb_transaction_commit(wins_db);
756 if (trans != LDB_SUCCESS) goto failed;
758 wins_hook(h, rec, WINS_HOOK_MODIFY);
760 talloc_free(tmp_ctx);
761 return NBT_RCODE_OK;
763 failed:
764 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
765 talloc_free(tmp_ctx);
766 return NBT_RCODE_SVR;
771 delete a WINS record from the database
773 uint8_t winsdb_delete(struct winsdb_handle *h, struct winsdb_record *rec)
775 struct ldb_context *wins_db = h->ldb;
776 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
777 const struct ldb_dn *dn;
778 int trans;
779 int ret;
781 trans = ldb_transaction_start(wins_db);
782 if (trans != LDB_SUCCESS) goto failed;
784 dn = winsdb_dn(tmp_ctx, rec->name);
785 if (dn == NULL) goto failed;
787 ret = ldb_delete(wins_db, dn);
788 if (ret != 0) goto failed;
790 trans = ldb_transaction_commit(wins_db);
791 if (trans != LDB_SUCCESS) goto failed;
793 wins_hook(h, rec, WINS_HOOK_DELETE);
795 talloc_free(tmp_ctx);
796 return NBT_RCODE_OK;
798 failed:
799 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
800 talloc_free(tmp_ctx);
801 return NBT_RCODE_SVR;
804 static BOOL winsdb_check_or_add_module_list(struct winsdb_handle *h)
806 int trans;
807 int ret;
808 struct ldb_dn *dn;
809 struct ldb_result *res = NULL;
810 struct ldb_message *msg = NULL;
811 TALLOC_CTX *tmp_ctx = talloc_new(h);
812 unsigned int flags = 0;
814 trans = ldb_transaction_start(h->ldb);
815 if (trans != LDB_SUCCESS) goto failed;
817 /* check if we have a special @MODULES record already */
818 dn = ldb_dn_explode(tmp_ctx, "@MODULES");
819 if (!dn) goto failed;
821 /* find the record in the WINS database */
822 ret = ldb_search(h->ldb, dn, LDB_SCOPE_BASE, NULL, NULL, &res);
823 if (ret != LDB_SUCCESS) goto failed;
824 talloc_steal(tmp_ctx, res);
826 if (res->count > 0) goto skip;
828 /* if there's no record, add one */
829 msg = ldb_msg_new(tmp_ctx);
830 if (!msg) goto failed;
831 msg->dn = dn;
833 ret = ldb_msg_add_string(msg, "@LIST", "wins_ldb");
834 if (ret != 0) goto failed;
836 ret = ldb_add(h->ldb, msg);
837 if (ret != 0) goto failed;
839 trans = ldb_transaction_commit(h->ldb);
840 if (trans != LDB_SUCCESS) goto failed;
842 /* close and reopen the database, with the modules */
843 trans = LDB_ERR_OTHER;
844 talloc_free(h->ldb);
845 h->ldb = NULL;
847 if (lp_parm_bool(-1,"winsdb", "nosync", False)) {
848 flags |= LDB_FLG_NOSYNC;
851 h->ldb = ldb_wrap_connect(h, lock_path(h, lp_wins_url()),
852 NULL, NULL, flags, NULL);
853 if (!h->ldb) goto failed;
855 talloc_free(tmp_ctx);
856 return True;
858 skip:
859 if (trans == LDB_SUCCESS) ldb_transaction_cancel(h->ldb);
860 talloc_free(tmp_ctx);
861 return True;
863 failed:
864 if (trans == LDB_SUCCESS) ldb_transaction_cancel(h->ldb);
865 talloc_free(tmp_ctx);
866 return False;
869 struct winsdb_handle *winsdb_connect(TALLOC_CTX *mem_ctx, enum winsdb_handle_caller caller)
871 struct winsdb_handle *h = NULL;
872 const char *owner;
873 unsigned int flags = 0;
874 BOOL ret;
875 int ldb_err;
877 h = talloc(mem_ctx, struct winsdb_handle);
878 if (!h) return NULL;
880 if (lp_parm_bool(-1,"winsdb", "nosync", False)) {
881 flags |= LDB_FLG_NOSYNC;
884 h->ldb = ldb_wrap_connect(h, lock_path(h, lp_wins_url()),
885 NULL, NULL, flags, NULL);
886 if (!h->ldb) goto failed;
888 h->caller = caller;
890 owner = lp_parm_string(-1, "winsdb", "local_owner");
891 if (!owner) {
892 owner = iface_n_ip(0);
895 h->local_owner = talloc_strdup(h, owner);
896 if (!h->local_owner) goto failed;
898 /* make sure the module list is available and used */
899 ret = winsdb_check_or_add_module_list(h);
900 if (!ret) goto failed;
902 ldb_err = ldb_set_opaque(h->ldb, "winsdb_handle", h);
903 if (ldb_err != LDB_SUCCESS) goto failed;
905 return h;
906 failed:
907 talloc_free(h);
908 return NULL;