2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
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"
29 #include "system/time.h"
32 return the new maxVersion and save it
34 static uint64_t winsdb_allocate_version(struct wins_server *winssrv)
38 struct ldb_context *ldb = winssrv->wins_db;
40 struct ldb_message **res = NULL;
41 struct ldb_message *msg = NULL;
42 TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
43 uint64_t maxVersion = 0;
45 trans = ldb_transaction_start(ldb);
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(ldb, dn, LDB_SCOPE_BASE,
55 talloc_steal(tmp_ctx, res);
57 if (ret < 0) goto failed;
58 if (ret > 1) goto failed;
61 maxVersion = ldb_msg_find_uint64(res[0], "maxVersion", 0);
65 msg = ldb_msg_new(tmp_ctx);
66 if (!msg) goto failed;
70 ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE);
71 if (ret != 0) goto failed;
72 ret = ldb_msg_add_string(msg, "objectClass", "winsMaxVersion");
73 if (ret != 0) goto failed;
74 ret = ldb_msg_add_empty(msg, "maxVersion", LDB_FLAG_MOD_REPLACE);
75 if (ret != 0) goto failed;
76 ret = ldb_msg_add_fmt(msg, "maxVersion", "%llu", maxVersion);
77 if (ret != 0) goto failed;
79 ret = ldb_modify(ldb, msg);
80 if (ret != 0) ret = ldb_add(ldb, msg);
81 if (ret != 0) goto failed;
83 trans = ldb_transaction_commit(ldb);
84 if (trans != LDB_SUCCESS) goto failed;
90 if (trans == LDB_SUCCESS) ldb_transaction_cancel(ldb);
96 return a DN for a nbt_name
98 static struct ldb_dn *winsdb_dn(TALLOC_CTX *mem_ctx, struct nbt_name *name)
102 dn = ldb_dn_string_compose(mem_ctx, NULL, "type=0x%02X", name->type);
103 if (dn && name->name && *name->name) {
104 dn = ldb_dn_string_compose(mem_ctx, dn, "name=%s", name->name);
106 if (dn && name->scope && *name->scope) {
107 dn = ldb_dn_string_compose(mem_ctx, dn, "scope=%s", name->scope);
112 static NTSTATUS winsdb_nbt_name(TALLOC_CTX *mem_ctx, struct ldb_dn *dn, struct nbt_name **_name)
115 struct nbt_name *name;
118 name = talloc(mem_ctx, struct nbt_name);
120 status = NT_STATUS_NO_MEMORY;
124 if (dn->comp_num > 3) {
125 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
129 if (dn->comp_num > cur && strcasecmp("scope", dn->components[cur].name) == 0) {
130 name->scope = talloc_steal(name, dn->components[cur].value.data);
136 if (dn->comp_num > cur && strcasecmp("name", dn->components[cur].name) == 0) {
137 name->name = talloc_steal(name, dn->components[cur].value.data);
140 name->name = talloc_strdup(name, "");
142 status = NT_STATUS_NO_MEMORY;
147 if (dn->comp_num > cur && strcasecmp("type", dn->components[cur].name) == 0) {
148 name->type = strtoul((char *)dn->components[cur].value.data, NULL, 0);
151 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
163 decode the winsdb_addr("address") attribute:
165 "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
168 static NTSTATUS winsdb_addr_decode(struct winsdb_record *rec, struct ldb_val *val,
169 TALLOC_CTX *mem_ctx, struct winsdb_addr **_addr)
172 struct winsdb_addr *addr;
178 addr = talloc(mem_ctx, struct winsdb_addr);
180 status = NT_STATUS_NO_MEMORY;
184 address = (char *)val->data;
186 p = strchr(address, ';');
188 /* support old entries, with only the address */
189 addr->address = talloc_steal(addr, val->data);
190 addr->wins_owner = talloc_reference(addr, rec->wins_owner);
191 if (!addr->wins_owner) {
192 status = NT_STATUS_NO_MEMORY;
195 addr->expire_time = rec->expire_time;
201 addr->address = talloc_strdup(addr, address);
202 if (!addr->address) {
203 status = NT_STATUS_NO_MEMORY;
207 if (strncmp("winsOwner:", p, 10) != 0) {
208 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
212 p = strchr(wins_owner, ';');
214 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
219 addr->wins_owner = talloc_strdup(addr, wins_owner);
220 if (!addr->wins_owner) {
221 status = NT_STATUS_NO_MEMORY;
225 if (strncmp("expireTime:", p, 11) != 0) {
226 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
230 expire_time = p + 11;
231 p = strchr(expire_time, ';');
233 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
238 addr->expire_time = ldb_string_to_time(expire_time);
248 encode the winsdb_addr("address") attribute like this:
249 "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
251 static int ldb_msg_add_winsdb_addr(struct ldb_message *msg,
252 const char *attr_name, struct winsdb_addr *addr)
257 str = talloc_asprintf(msg, "%s;winsOwner:%s;expireTime:%s;",
258 addr->address, addr->wins_owner,
259 ldb_timestring(msg, addr->expire_time));
262 val.data = discard_const_p(uint8_t, str);
263 val.length = strlen(str);
265 return ldb_msg_add_value(msg, attr_name, &val);
268 struct winsdb_addr **winsdb_addr_list_make(TALLOC_CTX *mem_ctx)
270 struct winsdb_addr **addresses;
272 addresses = talloc_array(mem_ctx, struct winsdb_addr *, 1);
273 if (!addresses) return NULL;
280 struct winsdb_addr **winsdb_addr_list_add(struct winsdb_addr **addresses, const char *address,
281 const char *wins_owner, time_t expire_time)
283 size_t len = winsdb_addr_list_length(addresses);
285 addresses = talloc_realloc(addresses, addresses, struct winsdb_addr *, len + 2);
286 if (!addresses) return NULL;
288 addresses[len] = talloc(addresses, struct winsdb_addr);
289 if (!addresses[len]) {
290 talloc_free(addresses);
294 addresses[len]->address = talloc_strdup(addresses[len], address);
295 if (!addresses[len]->address) {
296 talloc_free(addresses);
300 addresses[len]->wins_owner = talloc_strdup(addresses[len], wins_owner);
301 if (!addresses[len]->wins_owner) {
302 talloc_free(addresses);
306 addresses[len]->expire_time = expire_time;
308 addresses[len+1] = NULL;
313 void winsdb_addr_list_remove(struct winsdb_addr **addresses, const char *address)
317 for (i=0; addresses[i]; i++) {
318 if (strcmp(addresses[i]->address, address) == 0) {
322 if (!addresses[i]) return;
324 for (; addresses[i]; i++) {
325 addresses[i] = addresses[i+1];
331 struct winsdb_addr *winsdb_addr_list_check(struct winsdb_addr **addresses, const char *address)
335 for (i=0; addresses[i]; i++) {
336 if (strcmp(addresses[i]->address, address) == 0) {
344 size_t winsdb_addr_list_length(struct winsdb_addr **addresses)
347 for (i=0; addresses[i]; i++);
351 const char **winsdb_addr_string_list(TALLOC_CTX *mem_ctx, struct winsdb_addr **addresses)
353 size_t len = winsdb_addr_list_length(addresses);
354 const char **str_list;
357 str_list = talloc_array(mem_ctx, const char *, len + 1);
358 if (!str_list) return NULL;
360 for (i=0; i < len; i++) {
361 str_list[i] = talloc_strdup(str_list, addresses[i]->address);
363 talloc_free(str_list);
368 str_list[len] = NULL;
373 load a WINS entry from the database
375 NTSTATUS winsdb_lookup(struct ldb_context *wins_db,
376 struct nbt_name *name,
378 struct winsdb_record **_rec)
381 struct ldb_message **res = NULL;
383 struct winsdb_record *rec;
384 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
386 /* find the record in the WINS database */
387 ret = ldb_search(wins_db, winsdb_dn(tmp_ctx, name), LDB_SCOPE_BASE,
390 talloc_steal(tmp_ctx, res);
393 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
395 } else if (ret != 1) {
396 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
400 status = winsdb_record(res[0], name, tmp_ctx, &rec);
401 if (!NT_STATUS_IS_OK(status)) goto failed;
403 /* see if it has already expired */
404 if (rec->state == WREPL_STATE_ACTIVE &&
405 rec->expire_time <= time(NULL)) {
406 DEBUG(5,("WINS: expiring name %s (expired at %s)\n",
407 nbt_name_string(tmp_ctx, rec->name), timestring(tmp_ctx, rec->expire_time)));
408 rec->state = WREPL_STATE_RELEASED;
411 talloc_steal(mem_ctx, rec);
412 talloc_free(tmp_ctx);
417 talloc_free(tmp_ctx);
421 NTSTATUS winsdb_record(struct ldb_message *msg, struct nbt_name *name, TALLOC_CTX *mem_ctx, struct winsdb_record **_rec)
424 struct winsdb_record *rec;
425 struct ldb_message_element *el;
428 rec = talloc(mem_ctx, struct winsdb_record);
430 status = NT_STATUS_NO_MEMORY;
435 status = winsdb_nbt_name(rec, msg->dn, &name);
436 if (!NT_STATUS_IS_OK(status)) goto failed;
439 if (strlen(name->name) > 15) {
440 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
443 if (name->scope && strlen(name->scope) > 238) {
444 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
448 /* parse it into a more convenient winsdb_record structure */
450 rec->type = ldb_msg_find_int(msg, "recordType", WREPL_TYPE_UNIQUE);
451 rec->state = ldb_msg_find_int(msg, "recordState", WREPL_STATE_RELEASED);
452 rec->node = ldb_msg_find_int(msg, "nodeType", WREPL_NODE_B);
453 rec->is_static = ldb_msg_find_int(msg, "isStatic", 0);
454 rec->expire_time = ldb_string_to_time(ldb_msg_find_string(msg, "expireTime", NULL));
455 rec->version = ldb_msg_find_uint64(msg, "versionID", 0);
456 rec->wins_owner = ldb_msg_find_string(msg, "winsOwner", NULL);
457 rec->registered_by = ldb_msg_find_string(msg, "registeredBy", NULL);
458 talloc_steal(rec, rec->wins_owner);
459 talloc_steal(rec, rec->registered_by);
461 if (!rec->wins_owner) {
462 rec->wins_owner = talloc_strdup(rec, WINSDB_OWNER_LOCAL);
463 if (rec->wins_owner == NULL) {
464 status = NT_STATUS_NO_MEMORY;
469 el = ldb_msg_find_element(msg, "address");
471 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
475 if (rec->type == WREPL_TYPE_UNIQUE || rec->type == WREPL_TYPE_GROUP) {
476 if (el->num_values != 1) {
477 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
482 rec->addresses = talloc_array(rec, struct winsdb_addr *, el->num_values+1);
483 if (rec->addresses == NULL) {
484 status = NT_STATUS_NO_MEMORY;
488 for (i=0;i<el->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;
497 if (NT_STATUS_EQUAL(NT_STATUS_INTERNAL_DB_CORRUPTION, status)) {
498 DEBUG(1,("winsdb_record: corrupted record: %s\n", ldb_dn_linearize(rec, msg->dn)));
505 form a ldb_message from a winsdb_record
507 struct ldb_message *winsdb_message(struct ldb_context *ldb,
508 struct winsdb_record *rec, TALLOC_CTX *mem_ctx)
511 struct ldb_message *msg = ldb_msg_new(mem_ctx);
512 if (msg == NULL) goto failed;
514 msg->dn = winsdb_dn(msg, rec->name);
515 if (msg->dn == NULL) goto failed;
516 ret |= ldb_msg_add_fmt(msg, "objectClass", "winsRecord");
517 ret |= ldb_msg_add_fmt(msg, "recordType", "%u", rec->type);
518 ret |= ldb_msg_add_fmt(msg, "recordState", "%u", rec->state);
519 ret |= ldb_msg_add_fmt(msg, "nodeType", "%u", rec->node);
520 ret |= ldb_msg_add_fmt(msg, "isStatic", "%u", rec->is_static);
521 ret |= ldb_msg_add_string(msg, "expireTime",
522 ldb_timestring(msg, rec->expire_time));
523 ret |= ldb_msg_add_fmt(msg, "versionID", "%llu", rec->version);
524 ret |= ldb_msg_add_string(msg, "winsOwner", rec->wins_owner);
525 for (i=0;rec->addresses[i];i++) {
526 ret |= ldb_msg_add_winsdb_addr(msg, "address", rec->addresses[i]);
528 ret |= ldb_msg_add_string(msg, "registeredBy", rec->registered_by);
529 if (ret != 0) goto failed;
538 save a WINS record into the database
540 uint8_t winsdb_add(struct wins_server *winssrv, struct winsdb_record *rec)
542 struct ldb_context *ldb = winssrv->wins_db;
543 struct ldb_message *msg;
544 TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
548 trans = ldb_transaction_start(ldb);
549 if (trans != LDB_SUCCESS) goto failed;
551 rec->version = winsdb_allocate_version(winssrv);
552 if (rec->version == 0) goto failed;
553 rec->wins_owner = WINSDB_OWNER_LOCAL;
555 msg = winsdb_message(winssrv->wins_db, rec, tmp_ctx);
556 if (msg == NULL) goto failed;
557 ret = ldb_add(ldb, msg);
558 if (ret != 0) goto failed;
560 trans = ldb_transaction_commit(ldb);
561 if (trans != LDB_SUCCESS) goto failed;
563 talloc_free(tmp_ctx);
567 if (trans == LDB_SUCCESS) ldb_transaction_cancel(ldb);
568 talloc_free(tmp_ctx);
569 return NBT_RCODE_SVR;
574 modify a WINS record in the database
576 uint8_t winsdb_modify(struct wins_server *winssrv, struct winsdb_record *rec)
578 struct ldb_context *ldb = winssrv->wins_db;
579 struct ldb_message *msg;
580 TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
585 trans = ldb_transaction_start(ldb);
586 if (trans != LDB_SUCCESS) goto failed;
588 rec->version = winsdb_allocate_version(winssrv);
589 if (rec->version == 0) goto failed;
590 rec->wins_owner = WINSDB_OWNER_LOCAL;
592 msg = winsdb_message(winssrv->wins_db, rec, tmp_ctx);
593 if (msg == NULL) goto failed;
595 for (i=0;i<msg->num_elements;i++) {
596 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
599 ret = ldb_modify(ldb, msg);
600 if (ret != 0) goto failed;
602 trans = ldb_transaction_commit(ldb);
603 if (trans != LDB_SUCCESS) goto failed;
605 talloc_free(tmp_ctx);
609 if (trans == LDB_SUCCESS) ldb_transaction_cancel(ldb);
610 talloc_free(tmp_ctx);
611 return NBT_RCODE_SVR;
616 delete a WINS record from the database
618 uint8_t winsdb_delete(struct wins_server *winssrv, struct winsdb_record *rec)
620 struct ldb_context *ldb = winssrv->wins_db;
621 TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
622 const struct ldb_dn *dn;
626 trans = ldb_transaction_start(ldb);
627 if (trans != LDB_SUCCESS) goto failed;
629 dn = winsdb_dn(tmp_ctx, rec->name);
630 if (dn == NULL) goto failed;
632 ret = ldb_delete(ldb, dn);
633 if (ret != 0) goto failed;
635 trans = ldb_transaction_commit(ldb);
636 if (trans != LDB_SUCCESS) goto failed;
638 talloc_free(tmp_ctx);
642 if (trans == LDB_SUCCESS) ldb_transaction_cancel(ldb);
643 talloc_free(tmp_ctx);
644 return NBT_RCODE_SVR;
647 struct ldb_context *winsdb_connect(TALLOC_CTX *mem_ctx)
649 return ldb_wrap_connect(mem_ctx, lp_wins_url(), 0, NULL);