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 "system/time.h"
30 #include "auth/auth.h"
32 uint64_t winsdb_get_maxVersion(struct winsdb_handle *h)
35 struct ldb_context *ldb = h->ldb;
37 struct ldb_result *res = NULL;
38 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
39 uint64_t maxVersion = 0;
41 dn = ldb_dn_explode(tmp_ctx, "CN=VERSION");
44 /* find the record in the WINS database */
45 ret = ldb_search(ldb, dn, LDB_SCOPE_BASE,
47 if (ret != LDB_SUCCESS) goto failed;
48 talloc_steal(tmp_ctx, res);
49 if (res->count > 1) goto failed;
51 if (res->count == 1) {
52 maxVersion = ldb_msg_find_uint64(res->msgs[0], "maxVersion", 0);
61 if newVersion == 0 return the old maxVersion + 1 and save it
62 if newVersion > 0 return MAX(oldMaxVersion, newMaxVersion) and save it
64 uint64_t winsdb_set_maxVersion(struct winsdb_handle *h, uint64_t newMaxVersion)
69 struct ldb_result *res = NULL;
70 struct ldb_message *msg = NULL;
71 struct ldb_context *wins_db = h->ldb;
72 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
73 uint64_t oldMaxVersion = 0;
75 trans = ldb_transaction_start(wins_db);
76 if (trans != LDB_SUCCESS) goto failed;
78 dn = ldb_dn_explode(tmp_ctx, "CN=VERSION");
81 /* find the record in the WINS database */
82 ret = ldb_search(wins_db, dn, LDB_SCOPE_BASE, NULL, NULL, &res);
84 if (ret != LDB_SUCCESS) goto failed;
85 if (res->count > 1) goto failed;
87 talloc_steal(tmp_ctx, res);
89 if (res->count == 1) {
90 oldMaxVersion = ldb_msg_find_uint64(res->msgs[0], "maxVersion", 0);
93 if (newMaxVersion == 0) {
94 newMaxVersion = oldMaxVersion + 1;
96 newMaxVersion = MAX(oldMaxVersion, newMaxVersion);
99 msg = ldb_msg_new(tmp_ctx);
100 if (!msg) goto failed;
104 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE);
105 if (ret != 0) goto failed;
106 ret = ldb_msg_add_string(msg, "objectClass", "winsMaxVersion");
107 if (ret != 0) goto failed;
108 ret = ldb_msg_add_empty(msg, "maxVersion", LDB_FLAG_MOD_REPLACE);
109 if (ret != 0) goto failed;
110 ret = ldb_msg_add_fmt(msg, "maxVersion", "%llu", (long long)newMaxVersion);
111 if (ret != 0) goto failed;
113 ret = ldb_modify(wins_db, msg);
114 if (ret != 0) ret = ldb_add(wins_db, msg);
115 if (ret != 0) goto failed;
117 trans = ldb_transaction_commit(wins_db);
118 if (trans != LDB_SUCCESS) goto failed;
120 talloc_free(tmp_ctx);
121 return newMaxVersion;
124 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
125 talloc_free(tmp_ctx);
129 uint64_t winsdb_get_seqnumber(struct winsdb_handle *h)
132 struct ldb_context *ldb = h->ldb;
134 struct ldb_result *res = NULL;
135 TALLOC_CTX *tmp_ctx = talloc_new(ldb);
136 uint64_t seqnumber = 0;
138 dn = ldb_dn_explode(tmp_ctx, "@BASEINFO");
139 if (!dn) goto failed;
141 /* find the record in the WINS database */
142 ret = ldb_search(ldb, dn, LDB_SCOPE_BASE,
144 if (ret != LDB_SUCCESS) goto failed;
145 talloc_steal(tmp_ctx, res);
146 if (res->count > 1) goto failed;
148 if (res->count == 1) {
149 seqnumber = ldb_msg_find_uint64(res->msgs[0], "sequenceNumber", 0);
153 talloc_free(tmp_ctx);
158 return a DN for a nbt_name
160 static struct ldb_dn *winsdb_dn(TALLOC_CTX *mem_ctx, struct nbt_name *name)
164 dn = ldb_dn_string_compose(mem_ctx, NULL, "type=0x%02X", name->type);
165 if (dn && name->name && *name->name) {
166 dn = ldb_dn_string_compose(mem_ctx, dn, "name=%s", name->name);
168 if (dn && name->scope && *name->scope) {
169 dn = ldb_dn_string_compose(mem_ctx, dn, "scope=%s", name->scope);
174 static NTSTATUS winsdb_nbt_name(TALLOC_CTX *mem_ctx, struct ldb_dn *dn, struct nbt_name **_name)
177 struct nbt_name *name;
180 name = talloc(mem_ctx, struct nbt_name);
182 status = NT_STATUS_NO_MEMORY;
186 if (dn->comp_num > 3) {
187 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
191 if (dn->comp_num > cur && strcasecmp("scope", dn->components[cur].name) == 0) {
192 name->scope = talloc_steal(name, dn->components[cur].value.data);
198 if (dn->comp_num > cur && strcasecmp("name", dn->components[cur].name) == 0) {
199 name->name = talloc_steal(name, dn->components[cur].value.data);
202 name->name = talloc_strdup(name, "");
204 status = NT_STATUS_NO_MEMORY;
209 if (dn->comp_num > cur && strcasecmp("type", dn->components[cur].name) == 0) {
210 name->type = strtoul((char *)dn->components[cur].value.data, NULL, 0);
213 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
225 decode the winsdb_addr("address") attribute:
227 "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
230 static NTSTATUS winsdb_addr_decode(struct winsdb_handle *h, struct winsdb_record *rec, struct ldb_val *val,
231 TALLOC_CTX *mem_ctx, struct winsdb_addr **_addr)
234 struct winsdb_addr *addr;
236 const char *wins_owner;
237 const char *expire_time;
240 addr = talloc(mem_ctx, struct winsdb_addr);
242 status = NT_STATUS_NO_MEMORY;
246 address = (char *)val->data;
248 p = strchr(address, ';');
250 /* support old entries, with only the address */
251 addr->address = talloc_steal(addr, val->data);
252 addr->wins_owner = talloc_reference(addr, rec->wins_owner);
253 if (!addr->wins_owner) {
254 status = NT_STATUS_NO_MEMORY;
257 addr->expire_time = rec->expire_time;
263 addr->address = talloc_strdup(addr, address);
264 if (!addr->address) {
265 status = NT_STATUS_NO_MEMORY;
269 if (strncmp("winsOwner:", p, 10) != 0) {
270 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
274 p = strchr(wins_owner, ';');
276 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
281 if (strcmp(wins_owner, "0.0.0.0") == 0) {
282 wins_owner = h->local_owner;
284 addr->wins_owner = talloc_strdup(addr, wins_owner);
285 if (!addr->wins_owner) {
286 status = NT_STATUS_NO_MEMORY;
290 if (strncmp("expireTime:", p, 11) != 0) {
291 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
295 expire_time = p + 11;
296 p = strchr(expire_time, ';');
298 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
303 addr->expire_time = ldb_string_to_time(expire_time);
313 encode the winsdb_addr("address") attribute like this:
314 "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
316 static int ldb_msg_add_winsdb_addr(struct ldb_message *msg,
317 const char *attr_name, struct winsdb_addr *addr)
323 expire_time = ldb_timestring(msg, addr->expire_time);
324 if (!expire_time) return -1;
325 str = talloc_asprintf(msg, "%s;winsOwner:%s;expireTime:%s;",
326 addr->address, addr->wins_owner,
328 talloc_free(expire_time);
331 val.data = discard_const_p(uint8_t, str);
332 val.length = strlen(str);
334 return ldb_msg_add_value(msg, attr_name, &val);
337 struct winsdb_addr **winsdb_addr_list_make(TALLOC_CTX *mem_ctx)
339 struct winsdb_addr **addresses;
341 addresses = talloc_array(mem_ctx, struct winsdb_addr *, 1);
342 if (!addresses) return NULL;
349 struct winsdb_addr **winsdb_addr_list_add(struct winsdb_addr **addresses, const char *address,
350 const char *wins_owner, time_t expire_time)
352 size_t len = winsdb_addr_list_length(addresses);
354 addresses = talloc_realloc(addresses, addresses, struct winsdb_addr *, len + 2);
355 if (!addresses) return NULL;
357 addresses[len] = talloc(addresses, struct winsdb_addr);
358 if (!addresses[len]) {
359 talloc_free(addresses);
363 addresses[len]->address = talloc_strdup(addresses[len], address);
364 if (!addresses[len]->address) {
365 talloc_free(addresses);
369 addresses[len]->wins_owner = talloc_strdup(addresses[len], wins_owner);
370 if (!addresses[len]->wins_owner) {
371 talloc_free(addresses);
375 addresses[len]->expire_time = expire_time;
377 addresses[len+1] = NULL;
382 void winsdb_addr_list_remove(struct winsdb_addr **addresses, const char *address)
386 for (i=0; addresses[i]; i++) {
387 if (strcmp(addresses[i]->address, address) == 0) {
391 if (!addresses[i]) return;
393 for (; addresses[i]; i++) {
394 addresses[i] = addresses[i+1];
400 struct winsdb_addr *winsdb_addr_list_check(struct winsdb_addr **addresses, const char *address)
404 for (i=0; addresses[i]; i++) {
405 if (strcmp(addresses[i]->address, address) == 0) {
413 size_t winsdb_addr_list_length(struct winsdb_addr **addresses)
416 for (i=0; addresses[i]; i++);
420 const char **winsdb_addr_string_list(TALLOC_CTX *mem_ctx, struct winsdb_addr **addresses)
422 size_t len = winsdb_addr_list_length(addresses);
423 const char **str_list=NULL;
426 for (i=0; i < len; i++) {
427 str_list = str_list_add(str_list, addresses[i]->address);
432 talloc_steal(mem_ctx, str_list);
437 load a WINS entry from the database
439 NTSTATUS winsdb_lookup(struct winsdb_handle *h,
440 struct nbt_name *name,
442 struct winsdb_record **_rec)
445 struct ldb_result *res = NULL;
447 struct winsdb_record *rec;
448 struct ldb_context *wins_db = h->ldb;
449 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
451 /* find the record in the WINS database */
452 ret = ldb_search(wins_db, winsdb_dn(tmp_ctx, name), LDB_SCOPE_BASE,
455 if (ret != LDB_SUCCESS || res->count > 1) {
456 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
458 } else if (res->count== 0) {
459 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
463 talloc_steal(tmp_ctx, res);
465 status = winsdb_record(h, res->msgs[0], tmp_ctx, &rec);
466 if (!NT_STATUS_IS_OK(status)) goto failed;
468 /* see if it has already expired */
469 if (rec->state == WREPL_STATE_ACTIVE &&
470 rec->expire_time <= time(NULL)) {
471 DEBUG(5,("WINS: expiring name %s (expired at %s)\n",
472 nbt_name_string(tmp_ctx, rec->name), timestring(tmp_ctx, rec->expire_time)));
473 rec->state = WREPL_STATE_RELEASED;
476 talloc_steal(mem_ctx, rec);
477 talloc_free(tmp_ctx);
482 talloc_free(tmp_ctx);
486 NTSTATUS winsdb_record(struct winsdb_handle *h, struct ldb_message *msg, TALLOC_CTX *mem_ctx, struct winsdb_record **_rec)
489 struct winsdb_record *rec;
490 struct ldb_message_element *el;
491 struct nbt_name *name;
492 uint32_t i, num_values;
494 rec = talloc(mem_ctx, struct winsdb_record);
496 status = NT_STATUS_NO_MEMORY;
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;
507 if (name->scope && strlen(name->scope) > 238) {
508 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
512 /* parse it into a more convenient winsdb_record structure */
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");
531 num_values = el->num_values;
536 if (rec->type == WREPL_TYPE_UNIQUE || rec->type == WREPL_TYPE_GROUP) {
537 if (num_values != 1) {
538 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
542 if (rec->state == WREPL_STATE_ACTIVE) {
543 if (num_values < 1) {
544 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
548 if (num_values > 25) {
549 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
553 rec->addresses = talloc_array(rec, struct winsdb_addr *, num_values+1);
554 if (rec->addresses == NULL) {
555 status = NT_STATUS_NO_MEMORY;
559 for (i=0;i<num_values;i++) {
560 status = winsdb_addr_decode(h, rec, &el->values[i], rec->addresses, &rec->addresses[i]);
561 if (!NT_STATUS_IS_OK(status)) goto failed;
563 rec->addresses[i] = NULL;
565 if (rec->is_static && rec->state == WREPL_STATE_ACTIVE) {
566 rec->expire_time = get_time_t_max();
567 for (i=0;rec->addresses[i];i++) {
568 rec->addresses[i]->expire_time = rec->expire_time;
575 if (NT_STATUS_EQUAL(NT_STATUS_INTERNAL_DB_CORRUPTION, status)) {
576 DEBUG(1,("winsdb_record: corrupted record: %s\n", ldb_dn_linearize(rec, msg->dn)));
583 form a ldb_message from a winsdb_record
585 struct ldb_message *winsdb_message(struct ldb_context *ldb,
586 struct winsdb_record *rec, TALLOC_CTX *mem_ctx)
590 const char *expire_time;
591 struct ldb_message *msg = ldb_msg_new(mem_ctx);
592 if (msg == NULL) goto failed;
594 if (rec->is_static && rec->state == WREPL_STATE_ACTIVE) {
595 rec->expire_time = get_time_t_max();
596 for (i=0;rec->addresses[i];i++) {
597 rec->addresses[i]->expire_time = rec->expire_time;
601 /* make sure we don't put in corrupted records */
602 addr_count = winsdb_addr_list_length(rec->addresses);
603 if (rec->state == WREPL_STATE_ACTIVE && addr_count == 0) {
604 rec->state = WREPL_STATE_RELEASED;
606 if (rec->type == WREPL_TYPE_UNIQUE && addr_count > 1) {
607 rec->type = WREPL_TYPE_MHOMED;
610 expire_time = ldb_timestring(msg, rec->expire_time);
615 msg->dn = winsdb_dn(msg, rec->name);
616 if (msg->dn == NULL) goto failed;
617 ret |= ldb_msg_add_fmt(msg, "type", "0x%02X", rec->name->type);
618 if (rec->name->name && *rec->name->name) {
619 ret |= ldb_msg_add_string(msg, "name", rec->name->name);
621 if (rec->name->scope && *rec->name->scope) {
622 ret |= ldb_msg_add_string(msg, "scope", rec->name->scope);
624 ret |= ldb_msg_add_fmt(msg, "objectClass", "winsRecord");
625 ret |= ldb_msg_add_fmt(msg, "recordType", "%u", rec->type);
626 ret |= ldb_msg_add_fmt(msg, "recordState", "%u", rec->state);
627 ret |= ldb_msg_add_fmt(msg, "nodeType", "%u", rec->node);
628 ret |= ldb_msg_add_fmt(msg, "isStatic", "%u", rec->is_static);
629 ret |= ldb_msg_add_string(msg, "expireTime", expire_time);
630 ret |= ldb_msg_add_fmt(msg, "versionID", "%llu", (long long)rec->version);
631 ret |= ldb_msg_add_string(msg, "winsOwner", rec->wins_owner);
632 ret |= ldb_msg_add_empty(msg, "address", 0);
633 for (i=0;rec->addresses[i];i++) {
634 ret |= ldb_msg_add_winsdb_addr(msg, "address", rec->addresses[i]);
636 ret |= ldb_msg_add_empty(msg, "registeredBy", 0);
637 if (rec->registered_by) {
638 ret |= ldb_msg_add_string(msg, "registeredBy", rec->registered_by);
639 if (ret != 0) goto failed;
649 save a WINS record into the database
651 uint8_t winsdb_add(struct winsdb_handle *h, struct winsdb_record *rec, uint32_t flags)
653 struct ldb_message *msg;
654 struct ldb_context *wins_db = h->ldb;
655 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
659 trans = ldb_transaction_start(wins_db);
660 if (trans != LDB_SUCCESS) goto failed;
662 if (flags & WINSDB_FLAG_ALLOC_VERSION) {
663 /* passing '0' means auto-allocate a new one */
664 rec->version = winsdb_set_maxVersion(h, 0);
665 if (rec->version == 0) goto failed;
667 if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
668 rec->wins_owner = h->local_owner;
671 msg = winsdb_message(wins_db, rec, tmp_ctx);
672 if (msg == NULL) goto failed;
673 ret = ldb_add(wins_db, msg);
674 if (ret != 0) goto failed;
676 trans = ldb_transaction_commit(wins_db);
677 if (trans != LDB_SUCCESS) goto failed;
679 wins_hook(h, rec, WINS_HOOK_ADD);
681 talloc_free(tmp_ctx);
685 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
686 talloc_free(tmp_ctx);
687 return NBT_RCODE_SVR;
692 modify a WINS record in the database
694 uint8_t winsdb_modify(struct winsdb_handle *h, struct winsdb_record *rec, uint32_t flags)
696 struct ldb_message *msg;
697 struct ldb_context *wins_db = h->ldb;
698 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
703 trans = ldb_transaction_start(wins_db);
704 if (trans != LDB_SUCCESS) goto failed;
706 if (flags & WINSDB_FLAG_ALLOC_VERSION) {
707 /* passing '0' means auto-allocate a new one */
708 rec->version = winsdb_set_maxVersion(h, 0);
709 if (rec->version == 0) goto failed;
711 if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
712 rec->wins_owner = h->local_owner;
715 msg = winsdb_message(wins_db, rec, tmp_ctx);
716 if (msg == NULL) goto failed;
718 for (i=0;i<msg->num_elements;i++) {
719 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
722 ret = ldb_modify(wins_db, msg);
723 if (ret != 0) goto failed;
725 trans = ldb_transaction_commit(wins_db);
726 if (trans != LDB_SUCCESS) goto failed;
728 wins_hook(h, rec, WINS_HOOK_MODIFY);
730 talloc_free(tmp_ctx);
734 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
735 talloc_free(tmp_ctx);
736 return NBT_RCODE_SVR;
741 delete a WINS record from the database
743 uint8_t winsdb_delete(struct winsdb_handle *h, struct winsdb_record *rec)
745 struct ldb_context *wins_db = h->ldb;
746 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
747 const struct ldb_dn *dn;
751 trans = ldb_transaction_start(wins_db);
752 if (trans != LDB_SUCCESS) goto failed;
754 dn = winsdb_dn(tmp_ctx, rec->name);
755 if (dn == NULL) goto failed;
757 ret = ldb_delete(wins_db, dn);
758 if (ret != 0) goto failed;
760 trans = ldb_transaction_commit(wins_db);
761 if (trans != LDB_SUCCESS) goto failed;
763 wins_hook(h, rec, WINS_HOOK_DELETE);
765 talloc_free(tmp_ctx);
769 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
770 talloc_free(tmp_ctx);
771 return NBT_RCODE_SVR;
774 struct winsdb_handle *winsdb_connect(TALLOC_CTX *mem_ctx)
776 struct winsdb_handle *h = NULL;
778 unsigned int flags = 0;
780 h = talloc(mem_ctx, struct winsdb_handle);
783 if (lp_parm_bool(-1,"winsdb", "nosync", False)) {
784 flags |= LDB_FLG_NOSYNC;
787 h->ldb = ldb_wrap_connect(h, lock_path(h, lp_wins_url()),
788 system_session(h), NULL, flags, NULL);
789 if (!h->ldb) goto failed;
791 owner = lp_parm_string(-1, "winsdb", "local_owner");
793 owner = iface_n_ip(0);
796 h->local_owner = talloc_strdup(h, owner);
797 if (!h->local_owner) goto failed;