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;
356 str_list = talloc_array(mem_ctx, const char *, len + 1);
357 if (!str_list) return NULL;
359 for (i=0; i < len; i++) {
360 str_list[i] = talloc_strdup(str_list, addresses[i]->address);
362 talloc_free(str_list);
367 str_list[len] = NULL;
372 load a WINS entry from the database
374 NTSTATUS winsdb_lookup(struct ldb_context *wins_db,
375 struct nbt_name *name,
377 struct winsdb_record **_rec)
380 struct ldb_result *res = NULL;
382 struct winsdb_record *rec;
383 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
385 /* find the record in the WINS database */
386 ret = ldb_search(wins_db, winsdb_dn(tmp_ctx, name), LDB_SCOPE_BASE,
389 if (ret != LDB_SUCCESS || res->count > 1) {
390 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
392 } else if (res->count== 0) {
393 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
397 talloc_steal(tmp_ctx, res);
399 status = winsdb_record(res->msgs[0], tmp_ctx, &rec);
400 if (!NT_STATUS_IS_OK(status)) goto failed;
402 /* see if it has already expired */
403 if (rec->state == WREPL_STATE_ACTIVE &&
404 rec->expire_time <= time(NULL)) {
405 DEBUG(5,("WINS: expiring name %s (expired at %s)\n",
406 nbt_name_string(tmp_ctx, rec->name), timestring(tmp_ctx, rec->expire_time)));
407 rec->state = WREPL_STATE_RELEASED;
410 talloc_steal(mem_ctx, rec);
411 talloc_free(tmp_ctx);
416 talloc_free(tmp_ctx);
420 NTSTATUS winsdb_record(struct ldb_message *msg, TALLOC_CTX *mem_ctx, struct winsdb_record **_rec)
423 struct winsdb_record *rec;
424 struct ldb_message_element *el;
425 struct nbt_name *name;
426 uint32_t i, num_values;
428 rec = talloc(mem_ctx, struct winsdb_record);
430 status = NT_STATUS_NO_MEMORY;
434 status = winsdb_nbt_name(rec, msg->dn, &name);
435 if (!NT_STATUS_IS_OK(status)) goto failed;
437 if (strlen(name->name) > 15) {
438 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
441 if (name->scope && strlen(name->scope) > 238) {
442 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
446 /* parse it into a more convenient winsdb_record structure */
448 rec->type = ldb_msg_find_int(msg, "recordType", WREPL_TYPE_UNIQUE);
449 rec->state = ldb_msg_find_int(msg, "recordState", WREPL_STATE_RELEASED);
450 rec->node = ldb_msg_find_int(msg, "nodeType", WREPL_NODE_B);
451 rec->is_static = ldb_msg_find_int(msg, "isStatic", 0);
452 rec->expire_time = ldb_string_to_time(ldb_msg_find_string(msg, "expireTime", NULL));
453 rec->version = ldb_msg_find_uint64(msg, "versionID", 0);
454 rec->wins_owner = ldb_msg_find_string(msg, "winsOwner", NULL);
455 rec->registered_by = ldb_msg_find_string(msg, "registeredBy", NULL);
456 talloc_steal(rec, rec->wins_owner);
457 talloc_steal(rec, rec->registered_by);
459 if (!rec->wins_owner) {
460 rec->wins_owner = talloc_strdup(rec, WINSDB_OWNER_LOCAL);
461 if (rec->wins_owner == NULL) {
462 status = NT_STATUS_NO_MEMORY;
467 el = ldb_msg_find_element(msg, "address");
469 num_values = el->num_values;
474 if (rec->type == WREPL_TYPE_UNIQUE || rec->type == WREPL_TYPE_GROUP) {
475 if (num_values != 1) {
476 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
480 if (rec->state == WREPL_STATE_ACTIVE) {
481 if (num_values < 1) {
482 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
487 rec->addresses = talloc_array(rec, struct winsdb_addr *, num_values+1);
488 if (rec->addresses == NULL) {
489 status = NT_STATUS_NO_MEMORY;
493 for (i=0;i<num_values;i++) {
494 status = winsdb_addr_decode(rec, &el->values[i], rec->addresses, &rec->addresses[i]);
495 if (!NT_STATUS_IS_OK(status)) goto failed;
497 rec->addresses[i] = NULL;
502 if (NT_STATUS_EQUAL(NT_STATUS_INTERNAL_DB_CORRUPTION, status)) {
503 DEBUG(1,("winsdb_record: corrupted record: %s\n", ldb_dn_linearize(rec, msg->dn)));
510 form a ldb_message from a winsdb_record
512 struct ldb_message *winsdb_message(struct ldb_context *ldb,
513 struct winsdb_record *rec, TALLOC_CTX *mem_ctx)
517 struct ldb_message *msg = ldb_msg_new(mem_ctx);
518 if (msg == NULL) goto failed;
520 /* make sure we don't put in corrupted records */
521 addr_count = winsdb_addr_list_length(rec->addresses);
522 if (rec->state == WREPL_STATE_ACTIVE && addr_count == 0) {
523 rec->state = WREPL_STATE_RELEASED;
525 if (rec->type == WREPL_TYPE_UNIQUE && addr_count > 1) {
526 rec->type = WREPL_TYPE_MHOMED;
529 msg->dn = winsdb_dn(msg, rec->name);
530 if (msg->dn == NULL) goto failed;
531 ret |= ldb_msg_add_fmt(msg, "type", "0x%02X", rec->name->type);
532 if (rec->name->name && *rec->name->name) {
533 ret |= ldb_msg_add_string(msg, "name", rec->name->name);
535 if (rec->name->scope && *rec->name->scope) {
536 ret |= ldb_msg_add_string(msg, "scope", rec->name->scope);
538 ret |= ldb_msg_add_fmt(msg, "objectClass", "winsRecord");
539 ret |= ldb_msg_add_fmt(msg, "recordType", "%u", rec->type);
540 ret |= ldb_msg_add_fmt(msg, "recordState", "%u", rec->state);
541 ret |= ldb_msg_add_fmt(msg, "nodeType", "%u", rec->node);
542 ret |= ldb_msg_add_fmt(msg, "isStatic", "%u", rec->is_static);
543 ret |= ldb_msg_add_string(msg, "expireTime",
544 ldb_timestring(msg, rec->expire_time));
545 ret |= ldb_msg_add_fmt(msg, "versionID", "%llu", (long long)rec->version);
546 ret |= ldb_msg_add_string(msg, "winsOwner", rec->wins_owner);
547 ret |= ldb_msg_add_empty(msg, "address", 0);
548 for (i=0;rec->addresses[i];i++) {
549 ret |= ldb_msg_add_winsdb_addr(msg, "address", rec->addresses[i]);
551 ret |= ldb_msg_add_empty(msg, "registeredBy", 0);
552 if (rec->registered_by) {
553 ret |= ldb_msg_add_string(msg, "registeredBy", rec->registered_by);
554 if (ret != 0) goto failed;
564 save a WINS record into the database
566 uint8_t winsdb_add(struct ldb_context *wins_db, struct winsdb_record *rec, uint32_t flags)
568 struct ldb_message *msg;
569 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
573 trans = ldb_transaction_start(wins_db);
574 if (trans != LDB_SUCCESS) goto failed;
576 if (flags & WINSDB_FLAG_ALLOC_VERSION) {
577 rec->version = winsdb_allocate_version(wins_db);
578 if (rec->version == 0) goto failed;
580 if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
581 rec->wins_owner = WINSDB_OWNER_LOCAL;
584 msg = winsdb_message(wins_db, rec, tmp_ctx);
585 if (msg == NULL) goto failed;
586 ret = ldb_add(wins_db, msg);
587 if (ret != 0) goto failed;
589 trans = ldb_transaction_commit(wins_db);
590 if (trans != LDB_SUCCESS) goto failed;
592 talloc_free(tmp_ctx);
596 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
597 talloc_free(tmp_ctx);
598 return NBT_RCODE_SVR;
603 modify a WINS record in the database
605 uint8_t winsdb_modify(struct ldb_context *wins_db, struct winsdb_record *rec, uint32_t flags)
607 struct ldb_message *msg;
608 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
613 trans = ldb_transaction_start(wins_db);
614 if (trans != LDB_SUCCESS) goto failed;
616 if (flags & WINSDB_FLAG_ALLOC_VERSION) {
617 rec->version = winsdb_allocate_version(wins_db);
618 if (rec->version == 0) goto failed;
620 if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
621 rec->wins_owner = WINSDB_OWNER_LOCAL;
624 msg = winsdb_message(wins_db, rec, tmp_ctx);
625 if (msg == NULL) goto failed;
627 for (i=0;i<msg->num_elements;i++) {
628 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
631 ret = ldb_modify(wins_db, msg);
632 if (ret != 0) goto failed;
634 trans = ldb_transaction_commit(wins_db);
635 if (trans != LDB_SUCCESS) goto failed;
637 talloc_free(tmp_ctx);
641 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
642 talloc_free(tmp_ctx);
643 return NBT_RCODE_SVR;
648 delete a WINS record from the database
650 uint8_t winsdb_delete(struct ldb_context *wins_db, struct winsdb_record *rec)
652 TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
653 const struct ldb_dn *dn;
657 trans = ldb_transaction_start(wins_db);
658 if (trans != LDB_SUCCESS) goto failed;
660 dn = winsdb_dn(tmp_ctx, rec->name);
661 if (dn == NULL) goto failed;
663 ret = ldb_delete(wins_db, dn);
664 if (ret != 0) goto failed;
666 trans = ldb_transaction_commit(wins_db);
667 if (trans != LDB_SUCCESS) goto failed;
669 talloc_free(tmp_ctx);
673 if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
674 talloc_free(tmp_ctx);
675 return NBT_RCODE_SVR;
678 struct ldb_context *winsdb_connect(TALLOC_CTX *mem_ctx)
680 return ldb_wrap_connect(mem_ctx, lp_wins_url(), 0, NULL);