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"
30 #include "system/time.h"
33 return the new maxVersion and save it
35 static uint64_t winsdb_allocate_version(struct ldb_context *wins_db)
40 struct ldb_result *res = NULL;
41 struct ldb_message *msg = NULL;
42 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
43 uint64_t maxVersion = 0;
45 trans = ldb_transaction_start(wins_db);
46 if (trans != LDB_SUCCESS) goto failed;
48 dn = ldb_dn_explode(tmp_ctx, "CN=VERSION");
51 /* find the record in the WINS database */
52 ret = ldb_search(wins_db, dn, LDB_SCOPE_BASE, NULL, NULL, &res);
54 if (ret != LDB_SUCCESS) goto failed;
55 if (res->count > 1) goto failed;
57 talloc_steal(tmp_ctx, res);
59 if (res->count == 1) {
60 maxVersion = ldb_msg_find_uint64(res->msgs[0], "maxVersion", 0);
64 msg = ldb_msg_new(tmp_ctx);
65 if (!msg) goto failed;
69 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE);
70 if (ret != 0) goto failed;
71 ret = ldb_msg_add_string(msg, "objectClass", "winsMaxVersion");
72 if (ret != 0) goto failed;
73 ret = ldb_msg_add_empty(msg, "maxVersion", LDB_FLAG_MOD_REPLACE);
74 if (ret != 0) goto failed;
75 ret = ldb_msg_add_fmt(msg, "maxVersion", "%llu", (long long)maxVersion);
76 if (ret != 0) goto failed;
78 ret = ldb_modify(wins_db, msg);
79 if (ret != 0) ret = ldb_add(wins_db, msg);
80 if (ret != 0) goto failed;
82 trans = ldb_transaction_commit(wins_db);
83 if (trans != LDB_SUCCESS) goto failed;
89 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
95 return a DN for a nbt_name
97 static struct ldb_dn *winsdb_dn(TALLOC_CTX *mem_ctx, struct nbt_name *name)
101 dn = ldb_dn_string_compose(mem_ctx, NULL, "type=0x%02X", name->type);
102 if (dn && name->name && *name->name) {
103 dn = ldb_dn_string_compose(mem_ctx, dn, "name=%s", name->name);
105 if (dn && name->scope && *name->scope) {
106 dn = ldb_dn_string_compose(mem_ctx, dn, "scope=%s", name->scope);
111 static NTSTATUS winsdb_nbt_name(TALLOC_CTX *mem_ctx, struct ldb_dn *dn, struct nbt_name **_name)
114 struct nbt_name *name;
117 name = talloc(mem_ctx, struct nbt_name);
119 status = NT_STATUS_NO_MEMORY;
123 if (dn->comp_num > 3) {
124 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
128 if (dn->comp_num > cur && strcasecmp("scope", dn->components[cur].name) == 0) {
129 name->scope = talloc_steal(name, dn->components[cur].value.data);
135 if (dn->comp_num > cur && strcasecmp("name", dn->components[cur].name) == 0) {
136 name->name = talloc_steal(name, dn->components[cur].value.data);
139 name->name = talloc_strdup(name, "");
141 status = NT_STATUS_NO_MEMORY;
146 if (dn->comp_num > cur && strcasecmp("type", dn->components[cur].name) == 0) {
147 name->type = strtoul((char *)dn->components[cur].value.data, NULL, 0);
150 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
162 decode the winsdb_addr("address") attribute:
164 "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
167 static NTSTATUS winsdb_addr_decode(struct winsdb_record *rec, struct ldb_val *val,
168 TALLOC_CTX *mem_ctx, struct winsdb_addr **_addr)
171 struct winsdb_addr *addr;
177 addr = talloc(mem_ctx, struct winsdb_addr);
179 status = NT_STATUS_NO_MEMORY;
183 address = (char *)val->data;
185 p = strchr(address, ';');
187 /* support old entries, with only the address */
188 addr->address = talloc_steal(addr, val->data);
189 addr->wins_owner = talloc_reference(addr, rec->wins_owner);
190 if (!addr->wins_owner) {
191 status = NT_STATUS_NO_MEMORY;
194 addr->expire_time = rec->expire_time;
200 addr->address = talloc_strdup(addr, address);
201 if (!addr->address) {
202 status = NT_STATUS_NO_MEMORY;
206 if (strncmp("winsOwner:", p, 10) != 0) {
207 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
211 p = strchr(wins_owner, ';');
213 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
218 addr->wins_owner = talloc_strdup(addr, wins_owner);
219 if (!addr->wins_owner) {
220 status = NT_STATUS_NO_MEMORY;
224 if (strncmp("expireTime:", p, 11) != 0) {
225 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
229 expire_time = p + 11;
230 p = strchr(expire_time, ';');
232 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
237 addr->expire_time = ldb_string_to_time(expire_time);
247 encode the winsdb_addr("address") attribute like this:
248 "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
250 static int ldb_msg_add_winsdb_addr(struct ldb_message *msg,
251 const char *attr_name, struct winsdb_addr *addr)
256 str = talloc_asprintf(msg, "%s;winsOwner:%s;expireTime:%s;",
257 addr->address, addr->wins_owner,
258 ldb_timestring(msg, addr->expire_time));
261 val.data = discard_const_p(uint8_t, str);
262 val.length = strlen(str);
264 return ldb_msg_add_value(msg, attr_name, &val);
267 struct winsdb_addr **winsdb_addr_list_make(TALLOC_CTX *mem_ctx)
269 struct winsdb_addr **addresses;
271 addresses = talloc_array(mem_ctx, struct winsdb_addr *, 1);
272 if (!addresses) return NULL;
279 struct winsdb_addr **winsdb_addr_list_add(struct winsdb_addr **addresses, const char *address,
280 const char *wins_owner, time_t expire_time)
282 size_t len = winsdb_addr_list_length(addresses);
284 addresses = talloc_realloc(addresses, addresses, struct winsdb_addr *, len + 2);
285 if (!addresses) return NULL;
287 addresses[len] = talloc(addresses, struct winsdb_addr);
288 if (!addresses[len]) {
289 talloc_free(addresses);
293 addresses[len]->address = talloc_strdup(addresses[len], address);
294 if (!addresses[len]->address) {
295 talloc_free(addresses);
299 addresses[len]->wins_owner = talloc_strdup(addresses[len], wins_owner);
300 if (!addresses[len]->wins_owner) {
301 talloc_free(addresses);
305 addresses[len]->expire_time = expire_time;
307 addresses[len+1] = NULL;
312 void winsdb_addr_list_remove(struct winsdb_addr **addresses, const char *address)
316 for (i=0; addresses[i]; i++) {
317 if (strcmp(addresses[i]->address, address) == 0) {
321 if (!addresses[i]) return;
323 for (; addresses[i]; i++) {
324 addresses[i] = addresses[i+1];
330 struct winsdb_addr *winsdb_addr_list_check(struct winsdb_addr **addresses, const char *address)
334 for (i=0; addresses[i]; i++) {
335 if (strcmp(addresses[i]->address, address) == 0) {
343 size_t winsdb_addr_list_length(struct winsdb_addr **addresses)
346 for (i=0; addresses[i]; i++);
350 const char **winsdb_addr_string_list(TALLOC_CTX *mem_ctx, struct winsdb_addr **addresses)
352 size_t len = winsdb_addr_list_length(addresses);
353 const char **str_list=NULL;
356 for (i=0; i < len; i++) {
357 str_list = str_list_add(str_list, addresses[i]->address);
362 talloc_steal(mem_ctx, str_list);
367 load a WINS entry from the database
369 NTSTATUS winsdb_lookup(struct ldb_context *wins_db,
370 struct nbt_name *name,
372 struct winsdb_record **_rec)
375 struct ldb_result *res = NULL;
377 struct winsdb_record *rec;
378 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
380 /* find the record in the WINS database */
381 ret = ldb_search(wins_db, winsdb_dn(tmp_ctx, name), LDB_SCOPE_BASE,
384 if (ret != LDB_SUCCESS || res->count > 1) {
385 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
387 } else if (res->count== 0) {
388 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
392 talloc_steal(tmp_ctx, res);
394 status = winsdb_record(res->msgs[0], tmp_ctx, &rec);
395 if (!NT_STATUS_IS_OK(status)) goto failed;
397 /* see if it has already expired */
398 if (rec->state == WREPL_STATE_ACTIVE &&
399 rec->expire_time <= time(NULL)) {
400 DEBUG(5,("WINS: expiring name %s (expired at %s)\n",
401 nbt_name_string(tmp_ctx, rec->name), timestring(tmp_ctx, rec->expire_time)));
402 rec->state = WREPL_STATE_RELEASED;
405 talloc_steal(mem_ctx, rec);
406 talloc_free(tmp_ctx);
411 talloc_free(tmp_ctx);
415 NTSTATUS winsdb_record(struct ldb_message *msg, TALLOC_CTX *mem_ctx, struct winsdb_record **_rec)
418 struct winsdb_record *rec;
419 struct ldb_message_element *el;
420 struct nbt_name *name;
421 uint32_t i, num_values;
423 rec = talloc(mem_ctx, struct winsdb_record);
425 status = NT_STATUS_NO_MEMORY;
429 status = winsdb_nbt_name(rec, msg->dn, &name);
430 if (!NT_STATUS_IS_OK(status)) goto failed;
432 if (strlen(name->name) > 15) {
433 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
436 if (name->scope && strlen(name->scope) > 238) {
437 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
441 /* parse it into a more convenient winsdb_record structure */
443 rec->type = ldb_msg_find_int(msg, "recordType", WREPL_TYPE_UNIQUE);
444 rec->state = ldb_msg_find_int(msg, "recordState", WREPL_STATE_RELEASED);
445 rec->node = ldb_msg_find_int(msg, "nodeType", WREPL_NODE_B);
446 rec->is_static = ldb_msg_find_int(msg, "isStatic", 0);
447 rec->expire_time = ldb_string_to_time(ldb_msg_find_string(msg, "expireTime", NULL));
448 rec->version = ldb_msg_find_uint64(msg, "versionID", 0);
449 rec->wins_owner = ldb_msg_find_string(msg, "winsOwner", NULL);
450 rec->registered_by = ldb_msg_find_string(msg, "registeredBy", NULL);
451 talloc_steal(rec, rec->wins_owner);
452 talloc_steal(rec, rec->registered_by);
454 if (!rec->wins_owner) {
455 rec->wins_owner = talloc_strdup(rec, WINSDB_OWNER_LOCAL);
456 if (rec->wins_owner == NULL) {
457 status = NT_STATUS_NO_MEMORY;
462 el = ldb_msg_find_element(msg, "address");
464 num_values = el->num_values;
469 if (rec->type == WREPL_TYPE_UNIQUE || rec->type == WREPL_TYPE_GROUP) {
470 if (num_values != 1) {
471 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
475 if (rec->state == WREPL_STATE_ACTIVE) {
476 if (num_values < 1) {
477 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
482 rec->addresses = talloc_array(rec, struct winsdb_addr *, num_values+1);
483 if (rec->addresses == NULL) {
484 status = NT_STATUS_NO_MEMORY;
488 for (i=0;i<num_values;i++) {
489 status = winsdb_addr_decode(rec, &el->values[i], rec->addresses, &rec->addresses[i]);
490 if (!NT_STATUS_IS_OK(status)) goto failed;
492 rec->addresses[i] = NULL;
494 if (rec->is_static) {
495 if (num_values < 1) {
496 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
499 rec->state = WREPL_STATE_ACTIVE;
500 rec->expire_time = get_time_t_max();
501 for (i=0;rec->addresses[i];i++) {
502 rec->addresses[i]->expire_time = rec->expire_time;
509 if (NT_STATUS_EQUAL(NT_STATUS_INTERNAL_DB_CORRUPTION, status)) {
510 DEBUG(1,("winsdb_record: corrupted record: %s\n", ldb_dn_linearize(rec, msg->dn)));
517 form a ldb_message from a winsdb_record
519 struct ldb_message *winsdb_message(struct ldb_context *ldb,
520 struct winsdb_record *rec, TALLOC_CTX *mem_ctx)
524 struct ldb_message *msg = ldb_msg_new(mem_ctx);
525 if (msg == NULL) goto failed;
527 if (rec->is_static) {
528 rec->state = WREPL_STATE_ACTIVE;
529 rec->expire_time = get_time_t_max();
530 for (i=0;rec->addresses[i];i++) {
531 rec->addresses[i]->expire_time = rec->expire_time;
535 /* make sure we don't put in corrupted records */
536 addr_count = winsdb_addr_list_length(rec->addresses);
537 if (rec->state == WREPL_STATE_ACTIVE && addr_count == 0) {
538 rec->state = WREPL_STATE_RELEASED;
540 if (rec->type == WREPL_TYPE_UNIQUE && addr_count > 1) {
541 rec->type = WREPL_TYPE_MHOMED;
544 msg->dn = winsdb_dn(msg, rec->name);
545 if (msg->dn == NULL) goto failed;
546 ret |= ldb_msg_add_fmt(msg, "type", "0x%02X", rec->name->type);
547 if (rec->name->name && *rec->name->name) {
548 ret |= ldb_msg_add_string(msg, "name", rec->name->name);
550 if (rec->name->scope && *rec->name->scope) {
551 ret |= ldb_msg_add_string(msg, "scope", rec->name->scope);
553 ret |= ldb_msg_add_fmt(msg, "objectClass", "winsRecord");
554 ret |= ldb_msg_add_fmt(msg, "recordType", "%u", rec->type);
555 ret |= ldb_msg_add_fmt(msg, "recordState", "%u", rec->state);
556 ret |= ldb_msg_add_fmt(msg, "nodeType", "%u", rec->node);
557 ret |= ldb_msg_add_fmt(msg, "isStatic", "%u", rec->is_static);
558 ret |= ldb_msg_add_string(msg, "expireTime",
559 ldb_timestring(msg, rec->expire_time));
560 ret |= ldb_msg_add_fmt(msg, "versionID", "%llu", (long long)rec->version);
561 ret |= ldb_msg_add_string(msg, "winsOwner", rec->wins_owner);
562 ret |= ldb_msg_add_empty(msg, "address", 0);
563 for (i=0;rec->addresses[i];i++) {
564 ret |= ldb_msg_add_winsdb_addr(msg, "address", rec->addresses[i]);
566 ret |= ldb_msg_add_empty(msg, "registeredBy", 0);
567 if (rec->registered_by) {
568 ret |= ldb_msg_add_string(msg, "registeredBy", rec->registered_by);
569 if (ret != 0) goto failed;
579 save a WINS record into the database
581 uint8_t winsdb_add(struct ldb_context *wins_db, struct winsdb_record *rec, uint32_t flags)
583 struct ldb_message *msg;
584 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
588 trans = ldb_transaction_start(wins_db);
589 if (trans != LDB_SUCCESS) goto failed;
591 if (flags & WINSDB_FLAG_ALLOC_VERSION) {
592 rec->version = winsdb_allocate_version(wins_db);
593 if (rec->version == 0) goto failed;
595 if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
596 rec->wins_owner = WINSDB_OWNER_LOCAL;
599 msg = winsdb_message(wins_db, rec, tmp_ctx);
600 if (msg == NULL) goto failed;
601 ret = ldb_add(wins_db, msg);
602 if (ret != 0) goto failed;
604 trans = ldb_transaction_commit(wins_db);
605 if (trans != LDB_SUCCESS) goto failed;
607 talloc_free(tmp_ctx);
611 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
612 talloc_free(tmp_ctx);
613 return NBT_RCODE_SVR;
618 modify a WINS record in the database
620 uint8_t winsdb_modify(struct ldb_context *wins_db, struct winsdb_record *rec, uint32_t flags)
622 struct ldb_message *msg;
623 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
628 trans = ldb_transaction_start(wins_db);
629 if (trans != LDB_SUCCESS) goto failed;
631 if (flags & WINSDB_FLAG_ALLOC_VERSION) {
632 rec->version = winsdb_allocate_version(wins_db);
633 if (rec->version == 0) goto failed;
635 if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
636 rec->wins_owner = WINSDB_OWNER_LOCAL;
639 msg = winsdb_message(wins_db, rec, tmp_ctx);
640 if (msg == NULL) goto failed;
642 for (i=0;i<msg->num_elements;i++) {
643 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
646 ret = ldb_modify(wins_db, msg);
647 if (ret != 0) goto failed;
649 trans = ldb_transaction_commit(wins_db);
650 if (trans != LDB_SUCCESS) goto failed;
652 talloc_free(tmp_ctx);
656 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
657 talloc_free(tmp_ctx);
658 return NBT_RCODE_SVR;
663 delete a WINS record from the database
665 uint8_t winsdb_delete(struct ldb_context *wins_db, struct winsdb_record *rec)
667 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
668 const struct ldb_dn *dn;
672 trans = ldb_transaction_start(wins_db);
673 if (trans != LDB_SUCCESS) goto failed;
675 dn = winsdb_dn(tmp_ctx, rec->name);
676 if (dn == NULL) goto failed;
678 ret = ldb_delete(wins_db, dn);
679 if (ret != 0) goto failed;
681 trans = ldb_transaction_commit(wins_db);
682 if (trans != LDB_SUCCESS) goto failed;
684 talloc_free(tmp_ctx);
688 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
689 talloc_free(tmp_ctx);
690 return NBT_RCODE_SVR;
693 struct ldb_context *winsdb_connect(TALLOC_CTX *mem_ctx)
695 return ldb_wrap_connect(mem_ctx, lock_path(mem_ctx, lp_wins_url()),
696 system_session(mem_ctx), NULL, 0, NULL);