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 3 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, see <http://www.gnu.org/licenses/>.
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"
28 #include "librpc/gen_ndr/ndr_nbt.h"
29 #include "system/time.h"
31 #include "system/network.h"
32 #include "lib/socket/netif.h"
33 #include "param/param.h"
35 uint64_t winsdb_get_maxVersion(struct winsdb_handle
*h
)
38 struct ldb_context
*ldb
= h
->ldb
;
40 struct ldb_result
*res
= NULL
;
41 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
42 uint64_t maxVersion
= 0;
44 dn
= ldb_dn_new(tmp_ctx
, ldb
, "CN=VERSION");
47 /* find the record in the WINS database */
48 ret
= ldb_search(ldb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
, NULL
, NULL
);
49 if (ret
!= LDB_SUCCESS
) goto failed
;
50 if (res
->count
> 1) goto failed
;
52 if (res
->count
== 1) {
53 maxVersion
= ldb_msg_find_attr_as_uint64(res
->msgs
[0], "maxVersion", 0);
62 if newVersion == 0 return the old maxVersion + 1 and save it
63 if newVersion > 0 return MAX(oldMaxVersion, newMaxVersion) and save it
65 uint64_t winsdb_set_maxVersion(struct winsdb_handle
*h
, uint64_t newMaxVersion
)
70 struct ldb_result
*res
= NULL
;
71 struct ldb_message
*msg
= NULL
;
72 struct ldb_context
*wins_db
= h
->ldb
;
73 TALLOC_CTX
*tmp_ctx
= talloc_new(wins_db
);
74 uint64_t oldMaxVersion
= 0;
76 trans
= ldb_transaction_start(wins_db
);
77 if (trans
!= LDB_SUCCESS
) goto failed
;
79 dn
= ldb_dn_new(tmp_ctx
, wins_db
, "CN=VERSION");
82 /* find the record in the WINS database */
83 ret
= ldb_search(wins_db
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
, NULL
, NULL
);
84 if (ret
!= LDB_SUCCESS
) goto failed
;
85 if (res
->count
> 1) goto failed
;
87 if (res
->count
== 1) {
88 oldMaxVersion
= ldb_msg_find_attr_as_uint64(res
->msgs
[0], "maxVersion", 0);
91 if (newMaxVersion
== 0) {
92 newMaxVersion
= oldMaxVersion
+ 1;
94 newMaxVersion
= MAX(oldMaxVersion
, newMaxVersion
);
97 msg
= ldb_msg_new(tmp_ctx
);
98 if (!msg
) goto failed
;
102 ret
= ldb_msg_add_empty(msg
, "objectClass", LDB_FLAG_MOD_REPLACE
, NULL
);
103 if (ret
!= 0) goto failed
;
104 ret
= ldb_msg_add_string(msg
, "objectClass", "winsMaxVersion");
105 if (ret
!= 0) goto failed
;
106 ret
= ldb_msg_add_empty(msg
, "maxVersion", LDB_FLAG_MOD_REPLACE
, NULL
);
107 if (ret
!= 0) goto failed
;
108 ret
= ldb_msg_add_fmt(msg
, "maxVersion", "%llu", (long long)newMaxVersion
);
109 if (ret
!= 0) goto failed
;
111 ret
= ldb_modify(wins_db
, msg
);
112 if (ret
!= 0) ret
= ldb_add(wins_db
, msg
);
113 if (ret
!= 0) goto failed
;
115 trans
= ldb_transaction_commit(wins_db
);
116 if (trans
!= LDB_SUCCESS
) goto failed
;
118 talloc_free(tmp_ctx
);
119 return newMaxVersion
;
122 if (trans
== LDB_SUCCESS
) ldb_transaction_cancel(wins_db
);
123 talloc_free(tmp_ctx
);
127 uint64_t winsdb_get_seqnumber(struct winsdb_handle
*h
)
130 struct ldb_context
*ldb
= h
->ldb
;
132 struct ldb_result
*res
= NULL
;
133 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
134 uint64_t seqnumber
= 0;
136 dn
= ldb_dn_new(tmp_ctx
, ldb
, "@BASEINFO");
137 if (!dn
) goto failed
;
139 /* find the record in the WINS database */
140 ret
= ldb_search(ldb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
, NULL
, NULL
);
141 if (ret
!= LDB_SUCCESS
) goto failed
;
142 if (res
->count
> 1) goto failed
;
144 if (res
->count
== 1) {
145 seqnumber
= ldb_msg_find_attr_as_uint64(res
->msgs
[0], "sequenceNumber", 0);
149 talloc_free(tmp_ctx
);
154 return a DN for a nbt_name
156 static struct ldb_dn
*winsdb_dn(TALLOC_CTX
*mem_ctx
, struct ldb_context
*ldb
, struct nbt_name
*name
)
160 dn
= ldb_dn_new_fmt(mem_ctx
, ldb
, "type=0x%02X", name
->type
);
161 if (ldb_dn_is_valid(dn
) && name
->name
&& *name
->name
) {
162 ldb_dn_add_child_fmt(dn
, "name=%s", name
->name
);
164 if (ldb_dn_is_valid(dn
) && name
->scope
&& *name
->scope
) {
165 ldb_dn_add_child_fmt(dn
, "scope=%s", name
->scope
);
170 static NTSTATUS
winsdb_nbt_name(TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
, struct nbt_name
**_name
)
173 struct nbt_name
*name
;
174 unsigned int comp_num
;
177 name
= talloc(mem_ctx
, struct nbt_name
);
179 status
= NT_STATUS_NO_MEMORY
;
183 comp_num
= ldb_dn_get_comp_num(dn
);
186 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
190 if (comp_num
> cur
&& strcasecmp("scope", ldb_dn_get_component_name(dn
, cur
)) == 0) {
191 name
->scope
= (const char *)talloc_strdup(name
, (char *)ldb_dn_get_component_val(dn
, cur
)->data
);
197 if (comp_num
> cur
&& strcasecmp("name", ldb_dn_get_component_name(dn
, cur
)) == 0) {
198 name
->name
= (const char *)talloc_strdup(name
, (char *)ldb_dn_get_component_val(dn
, cur
)->data
);
201 name
->name
= talloc_strdup(name
, "");
203 status
= NT_STATUS_NO_MEMORY
;
208 if (comp_num
> cur
&& strcasecmp("type", ldb_dn_get_component_name(dn
, cur
)) == 0) {
209 name
->type
= strtoul((char *)ldb_dn_get_component_val(dn
, cur
)->data
, NULL
, 0);
212 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
224 decode the winsdb_addr("address") attribute:
226 "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
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
)
233 struct winsdb_addr
*addr
;
235 const char *wins_owner
;
236 const char *expire_time
;
239 addr
= talloc(mem_ctx
, struct winsdb_addr
);
241 status
= NT_STATUS_NO_MEMORY
;
245 address
= (char *)val
->data
;
247 p
= strchr(address
, ';');
249 /* support old entries, with only the address */
250 addr
->address
= (const char *)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
;
256 addr
->expire_time
= rec
->expire_time
;
262 addr
->address
= talloc_strdup(addr
, address
);
263 if (!addr
->address
) {
264 status
= NT_STATUS_NO_MEMORY
;
268 if (strncmp("winsOwner:", p
, 10) != 0) {
269 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
273 p
= strchr(wins_owner
, ';');
275 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
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
;
289 if (strncmp("expireTime:", p
, 11) != 0) {
290 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
294 expire_time
= p
+ 11;
295 p
= strchr(expire_time
, ';');
297 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
302 addr
->expire_time
= ldb_string_to_time(expire_time
);
312 encode the winsdb_addr("address") attribute like this:
314 "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
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
)
324 if (rec
->is_static
) {
325 str
= talloc_strdup(msg
, addr
->address
);
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
,
334 talloc_free(expire_time
);
338 val
.data
= discard_const_p(uint8_t, str
);
339 val
.length
= strlen(str
);
341 return ldb_msg_add_value(msg
, attr_name
, &val
, NULL
);
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
;
356 static int winsdb_addr_sort_list (struct winsdb_addr
**p1
, struct winsdb_addr
**p2
, void *opaque
)
358 struct winsdb_addr
*a1
= talloc_get_type(*p1
, struct winsdb_addr
);
359 struct winsdb_addr
*a2
= talloc_get_type(*p2
, struct winsdb_addr
);
360 struct winsdb_handle
*h
= talloc_get_type(opaque
, struct winsdb_handle
);
361 bool a1_owned
= false;
362 bool a2_owned
= false;
365 * first the owned addresses with the newest to the oldest address
366 * then the replica addresses with the newest to the oldest address
368 if (a2
->expire_time
!= a1
->expire_time
) {
369 return a2
->expire_time
- a1
->expire_time
;
372 if (strcmp(a2
->wins_owner
, h
->local_owner
) == 0) {
376 if (strcmp(a1
->wins_owner
, h
->local_owner
) == 0) {
380 return a2_owned
- a1_owned
;
383 struct winsdb_addr
**winsdb_addr_list_add(struct winsdb_handle
*h
, const struct winsdb_record
*rec
,
384 struct winsdb_addr
**addresses
, const char *address
,
385 const char *wins_owner
, time_t expire_time
,
386 bool is_name_registration
)
388 struct winsdb_addr
*old_addr
= NULL
;
391 bool found_old_replica
= false;
394 * count the addresses and maybe
395 * find an old entry for the new address
397 for (i
=0; addresses
[i
]; i
++) {
398 if (old_addr
) continue;
399 if (strcmp(addresses
[i
]->address
, address
) == 0) {
400 old_addr
= addresses
[i
];
406 * the address is already there
407 * and we can replace it
410 goto remove_old_addr
;
414 * if we don't have 25 addresses already,
415 * we can just add the new address
422 * if we haven't found the address,
423 * and we have already have 25 addresses
424 * if so then we need to do the following:
425 * - if it isn't a name registration, then just ignore the new address
426 * - if it is a name registration, then first search for
427 * the oldest replica and if there's no replica address
428 * search the oldest owned address
430 if (!is_name_registration
) {
435 * find the oldest replica address, if there's no replica
436 * record at all, find the oldest owned address
438 for (i
=0; addresses
[i
]; i
++) {
439 bool cur_is_replica
= false;
440 /* find out if the current address is a replica */
441 if (strcmp(addresses
[i
]->wins_owner
, h
->local_owner
) != 0) {
442 cur_is_replica
= true;
446 * if we already found a replica address and the current address
447 * is not a replica, then skip it
449 if (found_old_replica
&& !cur_is_replica
) continue;
452 * if we found the first replica address, reset the address
453 * that would be replaced
455 if (!found_old_replica
&& cur_is_replica
) {
456 found_old_replica
= true;
457 old_addr
= addresses
[i
];
462 * if the first address isn't a replica, just start with
466 old_addr
= addresses
[i
];
471 * see if we find an older address
473 if (addresses
[i
]->expire_time
< old_addr
->expire_time
) {
474 old_addr
= addresses
[i
];
480 winsdb_addr_list_remove(addresses
, old_addr
->address
);
484 addresses
= talloc_realloc(addresses
, addresses
, struct winsdb_addr
*, len
+ 2);
485 if (!addresses
) return NULL
;
487 addresses
[len
] = talloc(addresses
, struct winsdb_addr
);
488 if (!addresses
[len
]) {
489 talloc_free(addresses
);
493 addresses
[len
]->address
= talloc_strdup(addresses
[len
], address
);
494 if (!addresses
[len
]->address
) {
495 talloc_free(addresses
);
499 addresses
[len
]->wins_owner
= talloc_strdup(addresses
[len
], wins_owner
);
500 if (!addresses
[len
]->wins_owner
) {
501 talloc_free(addresses
);
505 addresses
[len
]->expire_time
= expire_time
;
507 addresses
[len
+1] = NULL
;
509 ldb_qsort(addresses
, len
+1 , sizeof(addresses
[0]), h
, (ldb_qsort_cmp_fn_t
)winsdb_addr_sort_list
);
514 void winsdb_addr_list_remove(struct winsdb_addr
**addresses
, const char *address
)
518 for (i
=0; addresses
[i
]; i
++) {
519 if (strcmp(addresses
[i
]->address
, address
) == 0) {
524 for (; addresses
[i
]; i
++) {
525 addresses
[i
] = addresses
[i
+1];
531 struct winsdb_addr
*winsdb_addr_list_check(struct winsdb_addr
**addresses
, const char *address
)
535 for (i
=0; addresses
[i
]; i
++) {
536 if (strcmp(addresses
[i
]->address
, address
) == 0) {
544 size_t winsdb_addr_list_length(struct winsdb_addr
**addresses
)
547 for (i
=0; addresses
[i
]; i
++);
551 const char **winsdb_addr_string_list(TALLOC_CTX
*mem_ctx
, struct winsdb_addr
**addresses
)
553 size_t len
= winsdb_addr_list_length(addresses
);
554 const char **str_list
=NULL
;
557 for (i
=0; i
< len
; i
++) {
558 str_list
= str_list_add(str_list
, addresses
[i
]->address
);
563 talloc_steal(mem_ctx
, str_list
);
568 load a WINS entry from the database
570 NTSTATUS
winsdb_lookup(struct winsdb_handle
*h
,
571 struct nbt_name
*name
,
573 struct winsdb_record
**_rec
)
576 struct ldb_result
*res
= NULL
;
578 struct winsdb_record
*rec
;
579 struct ldb_context
*wins_db
= h
->ldb
;
580 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
581 time_t now
= time(NULL
);
583 /* find the record in the WINS database */
584 ret
= ldb_search(wins_db
, tmp_ctx
, &res
,
585 winsdb_dn(tmp_ctx
, wins_db
, name
),
586 LDB_SCOPE_BASE
, NULL
, NULL
);
588 if (ret
!= LDB_SUCCESS
|| res
->count
> 1) {
589 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
591 } else if (res
->count
== 0) {
592 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
596 status
= winsdb_record(h
, res
->msgs
[0], tmp_ctx
, now
, &rec
);
597 if (!NT_STATUS_IS_OK(status
)) goto failed
;
599 talloc_steal(mem_ctx
, rec
);
600 talloc_free(tmp_ctx
);
605 talloc_free(tmp_ctx
);
609 NTSTATUS
winsdb_record(struct winsdb_handle
*h
, struct ldb_message
*msg
, TALLOC_CTX
*mem_ctx
, time_t now
, struct winsdb_record
**_rec
)
612 struct winsdb_record
*rec
;
613 struct ldb_message_element
*el
;
614 struct nbt_name
*name
;
615 uint32_t i
, j
, num_values
;
616 bool we_are_owner
= false;
618 rec
= talloc(mem_ctx
, struct winsdb_record
);
620 status
= NT_STATUS_NO_MEMORY
;
624 status
= winsdb_nbt_name(rec
, msg
->dn
, &name
);
625 if (!NT_STATUS_IS_OK(status
)) goto failed
;
627 if (strlen(name
->name
) > 15) {
628 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
631 if (name
->scope
&& strlen(name
->scope
) > 238) {
632 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
636 /* parse it into a more convenient winsdb_record structure */
638 rec
->type
= ldb_msg_find_attr_as_int(msg
, "recordType", WREPL_TYPE_UNIQUE
);
639 rec
->state
= ldb_msg_find_attr_as_int(msg
, "recordState", WREPL_STATE_RELEASED
);
640 rec
->node
= ldb_msg_find_attr_as_int(msg
, "nodeType", WREPL_NODE_B
);
641 rec
->is_static
= ldb_msg_find_attr_as_int(msg
, "isStatic", 0);
642 rec
->expire_time
= ldb_string_to_time(ldb_msg_find_attr_as_string(msg
, "expireTime", NULL
));
643 rec
->version
= ldb_msg_find_attr_as_uint64(msg
, "versionID", 0);
644 rec
->wins_owner
= ldb_msg_find_attr_as_string(msg
, "winsOwner", NULL
);
645 rec
->registered_by
= ldb_msg_find_attr_as_string(msg
, "registeredBy", NULL
);
646 talloc_steal(rec
, rec
->wins_owner
);
647 talloc_steal(rec
, rec
->registered_by
);
649 if (!rec
->wins_owner
|| strcmp(rec
->wins_owner
, "0.0.0.0") == 0) {
650 rec
->wins_owner
= h
->local_owner
;
653 el
= ldb_msg_find_element(msg
, "address");
655 num_values
= el
->num_values
;
660 if (rec
->type
== WREPL_TYPE_UNIQUE
|| rec
->type
== WREPL_TYPE_GROUP
) {
661 if (num_values
!= 1) {
662 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
666 if (rec
->state
== WREPL_STATE_ACTIVE
) {
667 if (num_values
< 1) {
668 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
672 if (num_values
> 25) {
673 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
677 if (strcmp(rec
->wins_owner
, h
->local_owner
) == 0) {
682 * see if it has already expired
684 * NOTE: only expire owned records this way!
685 * w2k3 resolves expired replicas
686 * which are in active state
688 if (!rec
->is_static
&&
689 rec
->expire_time
<= now
&&
690 rec
->state
== WREPL_STATE_ACTIVE
&&
692 DEBUG(5,("WINS: expiring name %s (expired at %s)\n",
693 nbt_name_string(mem_ctx
, rec
->name
), timestring(mem_ctx
, rec
->expire_time
)));
694 rec
->state
= WREPL_STATE_RELEASED
;
697 rec
->addresses
= talloc_array(rec
, struct winsdb_addr
*, num_values
+1);
698 if (rec
->addresses
== NULL
) {
699 status
= NT_STATUS_NO_MEMORY
;
703 for (i
=0,j
=0;i
<num_values
;i
++) {
704 status
= winsdb_addr_decode(h
, rec
, &el
->values
[i
], rec
->addresses
, &rec
->addresses
[j
]);
705 if (!NT_STATUS_IS_OK(status
)) goto failed
;
708 * the record isn't static and is active
709 * then don't add the address if it's expired
711 if (!rec
->is_static
&&
712 rec
->addresses
[j
]->expire_time
<= now
&&
713 rec
->state
== WREPL_STATE_ACTIVE
&&
715 DEBUG(5,("WINS: expiring name addr %s of %s (expired at %s)\n",
716 rec
->addresses
[j
]->address
, nbt_name_string(rec
->addresses
[j
], rec
->name
),
717 timestring(rec
->addresses
[j
], rec
->addresses
[j
]->expire_time
)));
718 talloc_free(rec
->addresses
[j
]);
719 rec
->addresses
[j
] = NULL
;
724 rec
->addresses
[j
] = NULL
;
727 if (rec
->is_static
&& rec
->state
== WREPL_STATE_ACTIVE
) {
728 rec
->expire_time
= get_time_t_max();
729 for (i
=0;rec
->addresses
[i
];i
++) {
730 rec
->addresses
[i
]->expire_time
= rec
->expire_time
;
734 if (rec
->state
== WREPL_STATE_ACTIVE
) {
735 if (num_values
< 1) {
736 DEBUG(5,("WINS: expiring name %s (because it has no active addresses)\n",
737 nbt_name_string(mem_ctx
, rec
->name
)));
738 rec
->state
= WREPL_STATE_RELEASED
;
745 if (NT_STATUS_EQUAL(NT_STATUS_INTERNAL_DB_CORRUPTION
, status
)) {
746 DEBUG(1,("winsdb_record: corrupted record: %s\n", ldb_dn_get_linearized(msg
->dn
)));
753 form a ldb_message from a winsdb_record
755 struct ldb_message
*winsdb_message(struct ldb_context
*ldb
,
756 struct winsdb_record
*rec
, TALLOC_CTX
*mem_ctx
)
760 const char *expire_time
;
761 struct ldb_message
*msg
= ldb_msg_new(mem_ctx
);
762 if (msg
== NULL
) goto failed
;
764 /* make sure we don't put in corrupted records */
765 addr_count
= winsdb_addr_list_length(rec
->addresses
);
766 if (rec
->state
== WREPL_STATE_ACTIVE
&& addr_count
== 0) {
767 rec
->state
= WREPL_STATE_RELEASED
;
769 if (rec
->type
== WREPL_TYPE_UNIQUE
&& addr_count
> 1) {
770 rec
->type
= WREPL_TYPE_MHOMED
;
773 expire_time
= ldb_timestring(msg
, rec
->expire_time
);
778 msg
->dn
= winsdb_dn(msg
, ldb
, rec
->name
);
779 if (msg
->dn
== NULL
) goto failed
;
780 ret
|= ldb_msg_add_fmt(msg
, "type", "0x%02X", rec
->name
->type
);
781 if (rec
->name
->name
&& *rec
->name
->name
) {
782 ret
|= ldb_msg_add_string(msg
, "name", rec
->name
->name
);
784 if (rec
->name
->scope
&& *rec
->name
->scope
) {
785 ret
|= ldb_msg_add_string(msg
, "scope", rec
->name
->scope
);
787 ret
|= ldb_msg_add_fmt(msg
, "objectClass", "winsRecord");
788 ret
|= ldb_msg_add_fmt(msg
, "recordType", "%u", rec
->type
);
789 ret
|= ldb_msg_add_fmt(msg
, "recordState", "%u", rec
->state
);
790 ret
|= ldb_msg_add_fmt(msg
, "nodeType", "%u", rec
->node
);
791 ret
|= ldb_msg_add_fmt(msg
, "isStatic", "%u", rec
->is_static
);
792 ret
|= ldb_msg_add_empty(msg
, "expireTime", 0, NULL
);
793 if (!(rec
->is_static
&& rec
->state
== WREPL_STATE_ACTIVE
)) {
794 ret
|= ldb_msg_add_string(msg
, "expireTime", expire_time
);
796 ret
|= ldb_msg_add_fmt(msg
, "versionID", "%llu", (long long)rec
->version
);
797 ret
|= ldb_msg_add_string(msg
, "winsOwner", rec
->wins_owner
);
798 ret
|= ldb_msg_add_empty(msg
, "address", 0, NULL
);
799 for (i
=0;rec
->addresses
[i
];i
++) {
800 ret
|= ldb_msg_add_winsdb_addr(msg
, rec
, "address", rec
->addresses
[i
]);
802 ret
|= ldb_msg_add_empty(msg
, "registeredBy", 0, NULL
);
803 if (rec
->registered_by
) {
804 ret
|= ldb_msg_add_string(msg
, "registeredBy", rec
->registered_by
);
805 if (ret
!= 0) goto failed
;
815 save a WINS record into the database
817 uint8_t winsdb_add(struct winsdb_handle
*h
, struct winsdb_record
*rec
, uint32_t flags
)
819 struct ldb_message
*msg
;
820 struct ldb_context
*wins_db
= h
->ldb
;
821 TALLOC_CTX
*tmp_ctx
= talloc_new(wins_db
);
825 trans
= ldb_transaction_start(wins_db
);
826 if (trans
!= LDB_SUCCESS
) goto failed
;
828 if (flags
& WINSDB_FLAG_ALLOC_VERSION
) {
829 /* passing '0' means auto-allocate a new one */
830 rec
->version
= winsdb_set_maxVersion(h
, 0);
831 if (rec
->version
== 0) goto failed
;
833 if (flags
& WINSDB_FLAG_TAKE_OWNERSHIP
) {
834 rec
->wins_owner
= h
->local_owner
;
837 msg
= winsdb_message(wins_db
, rec
, tmp_ctx
);
838 if (msg
== NULL
) goto failed
;
839 ret
= ldb_add(wins_db
, msg
);
840 if (ret
!= 0) goto failed
;
842 trans
= ldb_transaction_commit(wins_db
);
843 if (trans
!= LDB_SUCCESS
) goto failed
;
845 wins_hook(h
, rec
, WINS_HOOK_ADD
, h
->hook_script
);
847 talloc_free(tmp_ctx
);
851 if (trans
== LDB_SUCCESS
) ldb_transaction_cancel(wins_db
);
852 talloc_free(tmp_ctx
);
853 return NBT_RCODE_SVR
;
858 modify a WINS record in the database
860 uint8_t winsdb_modify(struct winsdb_handle
*h
, struct winsdb_record
*rec
, uint32_t flags
)
862 struct ldb_message
*msg
;
863 struct ldb_context
*wins_db
= h
->ldb
;
864 TALLOC_CTX
*tmp_ctx
= talloc_new(wins_db
);
869 trans
= ldb_transaction_start(wins_db
);
870 if (trans
!= LDB_SUCCESS
) goto failed
;
872 if (flags
& WINSDB_FLAG_ALLOC_VERSION
) {
873 /* passing '0' means auto-allocate a new one */
874 rec
->version
= winsdb_set_maxVersion(h
, 0);
875 if (rec
->version
== 0) goto failed
;
877 if (flags
& WINSDB_FLAG_TAKE_OWNERSHIP
) {
878 rec
->wins_owner
= h
->local_owner
;
881 msg
= winsdb_message(wins_db
, rec
, tmp_ctx
);
882 if (msg
== NULL
) goto failed
;
884 for (i
=0;i
<msg
->num_elements
;i
++) {
885 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
888 ret
= ldb_modify(wins_db
, msg
);
889 if (ret
!= 0) goto failed
;
891 trans
= ldb_transaction_commit(wins_db
);
892 if (trans
!= LDB_SUCCESS
) goto failed
;
894 wins_hook(h
, rec
, WINS_HOOK_MODIFY
, h
->hook_script
);
896 talloc_free(tmp_ctx
);
900 if (trans
== LDB_SUCCESS
) ldb_transaction_cancel(wins_db
);
901 talloc_free(tmp_ctx
);
902 return NBT_RCODE_SVR
;
907 delete a WINS record from the database
909 uint8_t winsdb_delete(struct winsdb_handle
*h
, struct winsdb_record
*rec
)
911 struct ldb_context
*wins_db
= h
->ldb
;
912 TALLOC_CTX
*tmp_ctx
= talloc_new(wins_db
);
917 trans
= ldb_transaction_start(wins_db
);
918 if (trans
!= LDB_SUCCESS
) goto failed
;
920 dn
= winsdb_dn(tmp_ctx
, wins_db
, rec
->name
);
921 if (dn
== NULL
) goto failed
;
923 ret
= ldb_delete(wins_db
, dn
);
924 if (ret
!= 0) goto failed
;
926 trans
= ldb_transaction_commit(wins_db
);
927 if (trans
!= LDB_SUCCESS
) goto failed
;
929 wins_hook(h
, rec
, WINS_HOOK_DELETE
, h
->hook_script
);
931 talloc_free(tmp_ctx
);
935 if (trans
== LDB_SUCCESS
) ldb_transaction_cancel(wins_db
);
936 talloc_free(tmp_ctx
);
937 return NBT_RCODE_SVR
;
940 static bool winsdb_check_or_add_module_list(struct event_context
*ev_ctx
,
941 struct loadparm_context
*lp_ctx
, struct winsdb_handle
*h
)
946 struct ldb_result
*res
= NULL
;
947 struct ldb_message
*msg
= NULL
;
948 TALLOC_CTX
*tmp_ctx
= talloc_new(h
);
949 unsigned int flags
= 0;
951 trans
= ldb_transaction_start(h
->ldb
);
952 if (trans
!= LDB_SUCCESS
) goto failed
;
954 /* check if we have a special @MODULES record already */
955 dn
= ldb_dn_new(tmp_ctx
, h
->ldb
, "@MODULES");
956 if (!dn
) goto failed
;
958 /* find the record in the WINS database */
959 ret
= ldb_search(h
->ldb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
, NULL
, NULL
);
960 if (ret
!= LDB_SUCCESS
) goto failed
;
962 if (res
->count
> 0) goto skip
;
964 /* if there's no record, add one */
965 msg
= ldb_msg_new(tmp_ctx
);
966 if (!msg
) goto failed
;
969 ret
= ldb_msg_add_string(msg
, "@LIST", "wins_ldb");
970 if (ret
!= 0) goto failed
;
972 ret
= ldb_add(h
->ldb
, msg
);
973 if (ret
!= 0) goto failed
;
975 trans
= ldb_transaction_commit(h
->ldb
);
976 if (trans
!= LDB_SUCCESS
) goto failed
;
978 /* close and reopen the database, with the modules */
979 trans
= LDB_ERR_OTHER
;
983 if (lp_parm_bool(lp_ctx
, NULL
,"winsdb", "nosync", false)) {
984 flags
|= LDB_FLG_NOSYNC
;
987 h
->ldb
= ldb_wrap_connect(h
, ev_ctx
, lp_ctx
, lock_path(h
, lp_ctx
, lp_wins_url(lp_ctx
)),
988 NULL
, NULL
, flags
, NULL
);
989 if (!h
->ldb
) goto failed
;
991 talloc_free(tmp_ctx
);
995 if (trans
== LDB_SUCCESS
) ldb_transaction_cancel(h
->ldb
);
996 talloc_free(tmp_ctx
);
1000 if (trans
== LDB_SUCCESS
) ldb_transaction_cancel(h
->ldb
);
1001 talloc_free(tmp_ctx
);
1005 struct winsdb_handle
*winsdb_connect(TALLOC_CTX
*mem_ctx
,
1006 struct event_context
*ev_ctx
,
1007 struct loadparm_context
*lp_ctx
,
1009 enum winsdb_handle_caller caller
)
1011 struct winsdb_handle
*h
= NULL
;
1012 unsigned int flags
= 0;
1016 h
= talloc(mem_ctx
, struct winsdb_handle
);
1017 if (!h
) return NULL
;
1019 if (lp_parm_bool(lp_ctx
, NULL
,"winsdb", "nosync", false)) {
1020 flags
|= LDB_FLG_NOSYNC
;
1023 h
->ldb
= ldb_wrap_connect(h
, ev_ctx
, lp_ctx
, lock_path(h
, lp_ctx
, lp_wins_url(lp_ctx
)),
1024 NULL
, NULL
, flags
, NULL
);
1025 if (!h
->ldb
) goto failed
;
1028 h
->hook_script
= lp_wins_hook(lp_ctx
);
1030 h
->local_owner
= talloc_strdup(h
, owner
);
1031 if (!h
->local_owner
) goto failed
;
1033 /* make sure the module list is available and used */
1034 ret
= winsdb_check_or_add_module_list(ev_ctx
, lp_ctx
, h
);
1035 if (!ret
) goto failed
;
1037 ldb_err
= ldb_set_opaque(h
->ldb
, "winsdb_handle", h
);
1038 if (ldb_err
!= LDB_SUCCESS
) goto failed
;