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"
27 #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
!= LDB_SUCCESS
) goto failed
;
104 ret
= ldb_msg_add_string(msg
, "objectClass", "winsMaxVersion");
105 if (ret
!= LDB_SUCCESS
) goto failed
;
106 ret
= ldb_msg_add_empty(msg
, "maxVersion", LDB_FLAG_MOD_REPLACE
, NULL
);
107 if (ret
!= LDB_SUCCESS
) goto failed
;
108 ret
= ldb_msg_add_fmt(msg
, "maxVersion", "%llu", (long long)newMaxVersion
);
109 if (ret
!= LDB_SUCCESS
) goto failed
;
111 ret
= ldb_modify(wins_db
, msg
);
112 if (ret
!= LDB_SUCCESS
) ret
= ldb_add(wins_db
, msg
);
113 if (ret
!= LDB_SUCCESS
) 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
,
157 const struct nbt_name
*name
)
161 dn
= ldb_dn_new_fmt(mem_ctx
, ldb
, "type=0x%02X", name
->type
);
162 if (ldb_dn_is_valid(dn
) && name
->name
&& *name
->name
) {
163 ldb_dn_add_child_fmt(dn
, "name=%s", name
->name
);
165 if (ldb_dn_is_valid(dn
) && name
->scope
&& *name
->scope
) {
166 ldb_dn_add_child_fmt(dn
, "scope=%s", name
->scope
);
171 static NTSTATUS
winsdb_nbt_name(TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
, struct nbt_name
**_name
)
174 struct nbt_name
*name
;
175 unsigned int comp_num
;
178 name
= talloc(mem_ctx
, struct nbt_name
);
180 status
= NT_STATUS_NO_MEMORY
;
184 comp_num
= ldb_dn_get_comp_num(dn
);
187 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
191 if (comp_num
> cur
&& strcasecmp("scope", ldb_dn_get_component_name(dn
, cur
)) == 0) {
192 name
->scope
= (const char *)talloc_strdup(name
, (char *)ldb_dn_get_component_val(dn
, cur
)->data
);
198 if (comp_num
> cur
&& strcasecmp("name", ldb_dn_get_component_name(dn
, cur
)) == 0) {
199 name
->name
= (const char *)talloc_strdup(name
, (char *)ldb_dn_get_component_val(dn
, cur
)->data
);
202 name
->name
= talloc_strdup(name
, "");
204 status
= NT_STATUS_NO_MEMORY
;
209 if (comp_num
> cur
&& strcasecmp("type", ldb_dn_get_component_name(dn
, cur
)) == 0) {
210 name
->type
= strtoul((char *)ldb_dn_get_component_val(dn
, cur
)->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
= (const char *)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:
315 "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
319 static int ldb_msg_add_winsdb_addr(struct ldb_message
*msg
, struct winsdb_record
*rec
,
320 const char *attr_name
, struct winsdb_addr
*addr
)
324 if (rec
->is_static
) {
325 str
= talloc_strdup(msg
, addr
->address
);
326 if (!str
) return LDB_ERR_OPERATIONS_ERROR
;
329 expire_time
= ldb_timestring(msg
, addr
->expire_time
);
330 if (!expire_time
) return LDB_ERR_OPERATIONS_ERROR
;
331 str
= talloc_asprintf(msg
, "%s;winsOwner:%s;expireTime:%s;",
332 addr
->address
, addr
->wins_owner
,
334 talloc_free(expire_time
);
335 if (!str
) return LDB_ERR_OPERATIONS_ERROR
;
338 return ldb_msg_add_string(msg
, attr_name
, str
);
341 struct winsdb_addr
**winsdb_addr_list_make(TALLOC_CTX
*mem_ctx
)
343 struct winsdb_addr
**addresses
;
345 addresses
= talloc_array(mem_ctx
, struct winsdb_addr
*, 1);
346 if (!addresses
) return NULL
;
353 static int winsdb_addr_sort_list (struct winsdb_addr
**p1
, struct winsdb_addr
**p2
, void *opaque
)
355 struct winsdb_addr
*a1
= talloc_get_type(*p1
, struct winsdb_addr
);
356 struct winsdb_addr
*a2
= talloc_get_type(*p2
, struct winsdb_addr
);
357 struct winsdb_handle
*h
= talloc_get_type(opaque
, struct winsdb_handle
);
358 bool a1_owned
= false;
359 bool a2_owned
= false;
362 * first the owned addresses with the newest to the oldest address
363 * then the replica addresses with the newest to the oldest address
365 if (a2
->expire_time
!= a1
->expire_time
) {
366 return a2
->expire_time
- a1
->expire_time
;
369 if (strcmp(a2
->wins_owner
, h
->local_owner
) == 0) {
373 if (strcmp(a1
->wins_owner
, h
->local_owner
) == 0) {
377 return a2_owned
- a1_owned
;
380 struct winsdb_addr
**winsdb_addr_list_add(struct winsdb_handle
*h
, const struct winsdb_record
*rec
,
381 struct winsdb_addr
**addresses
, const char *address
,
382 const char *wins_owner
, time_t expire_time
,
383 bool is_name_registration
)
385 struct winsdb_addr
*old_addr
= NULL
;
388 bool found_old_replica
= false;
391 * count the addresses and maybe
392 * find an old entry for the new address
394 for (i
=0; addresses
[i
]; i
++) {
395 if (old_addr
) continue;
396 if (strcmp(addresses
[i
]->address
, address
) == 0) {
397 old_addr
= addresses
[i
];
403 * the address is already there
404 * and we can replace it
407 goto remove_old_addr
;
411 * if we don't have 25 addresses already,
412 * we can just add the new address
419 * if we haven't found the address,
420 * and we have already have 25 addresses
421 * if so then we need to do the following:
422 * - if it isn't a name registration, then just ignore the new address
423 * - if it is a name registration, then first search for
424 * the oldest replica and if there's no replica address
425 * search the oldest owned address
427 if (!is_name_registration
) {
432 * find the oldest replica address, if there's no replica
433 * record at all, find the oldest owned address
435 for (i
=0; addresses
[i
]; i
++) {
436 bool cur_is_replica
= false;
437 /* find out if the current address is a replica */
438 if (strcmp(addresses
[i
]->wins_owner
, h
->local_owner
) != 0) {
439 cur_is_replica
= true;
443 * if we already found a replica address and the current address
444 * is not a replica, then skip it
446 if (found_old_replica
&& !cur_is_replica
) continue;
449 * if we found the first replica address, reset the address
450 * that would be replaced
452 if (!found_old_replica
&& cur_is_replica
) {
453 found_old_replica
= true;
454 old_addr
= addresses
[i
];
459 * if the first address isn't a replica, just start with
463 old_addr
= addresses
[i
];
468 * see if we find an older address
470 if (addresses
[i
]->expire_time
< old_addr
->expire_time
) {
471 old_addr
= addresses
[i
];
477 winsdb_addr_list_remove(addresses
, old_addr
->address
);
481 addresses
= talloc_realloc(addresses
, addresses
, struct winsdb_addr
*, len
+ 2);
482 if (!addresses
) return NULL
;
484 addresses
[len
] = talloc(addresses
, struct winsdb_addr
);
485 if (!addresses
[len
]) {
486 talloc_free(addresses
);
490 addresses
[len
]->address
= talloc_strdup(addresses
[len
], address
);
491 if (!addresses
[len
]->address
) {
492 talloc_free(addresses
);
496 addresses
[len
]->wins_owner
= talloc_strdup(addresses
[len
], wins_owner
);
497 if (!addresses
[len
]->wins_owner
) {
498 talloc_free(addresses
);
502 addresses
[len
]->expire_time
= expire_time
;
504 addresses
[len
+1] = NULL
;
506 LDB_TYPESAFE_QSORT(addresses
, len
+1, h
, winsdb_addr_sort_list
);
511 void winsdb_addr_list_remove(struct winsdb_addr
**addresses
, const char *address
)
515 for (i
=0; addresses
[i
]; i
++) {
516 if (strcmp(addresses
[i
]->address
, address
) == 0) {
521 for (; addresses
[i
]; i
++) {
522 addresses
[i
] = addresses
[i
+1];
528 struct winsdb_addr
*winsdb_addr_list_check(struct winsdb_addr
**addresses
, const char *address
)
532 for (i
=0; addresses
[i
]; i
++) {
533 if (strcmp(addresses
[i
]->address
, address
) == 0) {
541 size_t winsdb_addr_list_length(struct winsdb_addr
**addresses
)
544 for (i
=0; addresses
[i
]; i
++);
548 const char **winsdb_addr_string_list(TALLOC_CTX
*mem_ctx
, struct winsdb_addr
**addresses
)
550 size_t len
= winsdb_addr_list_length(addresses
);
551 const char **str_list
=NULL
;
554 for (i
=0; i
< len
; i
++) {
555 str_list
= str_list_add(str_list
, addresses
[i
]->address
);
560 talloc_steal(mem_ctx
, str_list
);
565 load a WINS entry from the database
567 NTSTATUS
winsdb_lookup(struct winsdb_handle
*h
,
568 const struct nbt_name
*name
,
570 struct winsdb_record
**_rec
)
573 struct ldb_result
*res
= NULL
;
575 struct winsdb_record
*rec
;
576 struct ldb_context
*wins_db
= h
->ldb
;
577 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
578 time_t now
= time(NULL
);
580 /* find the record in the WINS database */
581 ret
= ldb_search(wins_db
, tmp_ctx
, &res
,
582 winsdb_dn(tmp_ctx
, wins_db
, name
),
583 LDB_SCOPE_BASE
, NULL
, NULL
);
585 if (ret
!= LDB_SUCCESS
|| res
->count
> 1) {
586 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
588 } else if (res
->count
== 0) {
589 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
593 status
= winsdb_record(h
, res
->msgs
[0], tmp_ctx
, now
, &rec
);
594 if (!NT_STATUS_IS_OK(status
)) goto failed
;
596 talloc_steal(mem_ctx
, rec
);
597 talloc_free(tmp_ctx
);
602 talloc_free(tmp_ctx
);
606 NTSTATUS
winsdb_record(struct winsdb_handle
*h
, struct ldb_message
*msg
, TALLOC_CTX
*mem_ctx
, time_t now
, struct winsdb_record
**_rec
)
609 struct winsdb_record
*rec
;
610 struct ldb_message_element
*el
;
611 struct nbt_name
*name
;
612 uint32_t i
, j
, num_values
;
614 rec
= talloc(mem_ctx
, struct winsdb_record
);
616 status
= NT_STATUS_NO_MEMORY
;
620 status
= winsdb_nbt_name(rec
, msg
->dn
, &name
);
621 if (!NT_STATUS_IS_OK(status
)) goto failed
;
623 if (strlen(name
->name
) > 15) {
624 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
627 if (name
->scope
&& strlen(name
->scope
) > 238) {
628 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
632 /* parse it into a more convenient winsdb_record structure */
634 rec
->type
= ldb_msg_find_attr_as_int(msg
, "recordType", WREPL_TYPE_UNIQUE
);
635 rec
->state
= ldb_msg_find_attr_as_int(msg
, "recordState", WREPL_STATE_RELEASED
);
636 rec
->node
= ldb_msg_find_attr_as_int(msg
, "nodeType", WREPL_NODE_B
);
637 rec
->is_static
= ldb_msg_find_attr_as_int(msg
, "isStatic", 0);
638 rec
->expire_time
= ldb_string_to_time(ldb_msg_find_attr_as_string(msg
, "expireTime", NULL
));
639 rec
->version
= ldb_msg_find_attr_as_uint64(msg
, "versionID", 0);
640 rec
->wins_owner
= ldb_msg_find_attr_as_string(msg
, "winsOwner", NULL
);
641 rec
->registered_by
= ldb_msg_find_attr_as_string(msg
, "registeredBy", NULL
);
642 talloc_steal(rec
, rec
->wins_owner
);
643 talloc_steal(rec
, rec
->registered_by
);
645 if (!rec
->wins_owner
|| strcmp(rec
->wins_owner
, "0.0.0.0") == 0) {
646 rec
->wins_owner
= h
->local_owner
;
649 el
= ldb_msg_find_element(msg
, "address");
651 num_values
= el
->num_values
;
656 if (rec
->type
== WREPL_TYPE_UNIQUE
|| rec
->type
== WREPL_TYPE_GROUP
) {
657 if (num_values
!= 1) {
658 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
662 if (rec
->state
== WREPL_STATE_ACTIVE
) {
663 if (num_values
< 1) {
664 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
668 if (num_values
> 25) {
669 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
673 rec
->addresses
= talloc_array(rec
, struct winsdb_addr
*, num_values
+1);
674 if (rec
->addresses
== NULL
) {
675 status
= NT_STATUS_NO_MEMORY
;
679 for (i
=0,j
=0;i
<num_values
;i
++) {
680 bool we_are_owner
= false;
682 status
= winsdb_addr_decode(h
, rec
, &el
->values
[i
], rec
->addresses
, &rec
->addresses
[j
]);
683 if (!NT_STATUS_IS_OK(status
)) goto failed
;
685 if (strcmp(rec
->addresses
[j
]->wins_owner
, h
->local_owner
) == 0) {
690 * the record isn't static and is active
691 * then don't add the address if it's expired,
692 * but only if we're the owner of the address
694 * This is important for SGROUP records,
695 * because each server thinks he's the owner of the
696 * record and the record isn't replicated on a
697 * name_refresh. So addresses owned by another owner
698 * could expire, but we still need to return them
701 if (!rec
->is_static
&&
702 rec
->addresses
[j
]->expire_time
<= now
&&
703 rec
->state
== WREPL_STATE_ACTIVE
&&
705 DEBUG(5,("WINS: expiring name addr %s of %s (expired at %s)\n",
706 rec
->addresses
[j
]->address
, nbt_name_string(rec
->addresses
[j
], rec
->name
),
707 timestring(rec
->addresses
[j
], rec
->addresses
[j
]->expire_time
)));
708 talloc_free(rec
->addresses
[j
]);
709 rec
->addresses
[j
] = NULL
;
714 rec
->addresses
[j
] = NULL
;
717 if (rec
->is_static
&& rec
->state
== WREPL_STATE_ACTIVE
) {
718 rec
->expire_time
= get_time_t_max();
719 for (i
=0;rec
->addresses
[i
];i
++) {
720 rec
->addresses
[i
]->expire_time
= rec
->expire_time
;
724 if (rec
->state
== WREPL_STATE_ACTIVE
) {
725 if (num_values
< 1) {
726 DEBUG(5,("WINS: expiring name %s (because it has no active addresses)\n",
727 nbt_name_string(mem_ctx
, rec
->name
)));
728 rec
->state
= WREPL_STATE_RELEASED
;
735 if (NT_STATUS_EQUAL(NT_STATUS_INTERNAL_DB_CORRUPTION
, status
)) {
736 DEBUG(1,("winsdb_record: corrupted record: %s\n", ldb_dn_get_linearized(msg
->dn
)));
743 form a ldb_message from a winsdb_record
745 static struct ldb_message
*winsdb_message(struct ldb_context
*ldb
,
746 struct winsdb_record
*rec
,
751 const char *expire_time
;
752 struct ldb_message
*msg
= ldb_msg_new(mem_ctx
);
753 if (msg
== NULL
) goto failed
;
755 /* make sure we don't put in corrupted records */
756 addr_count
= winsdb_addr_list_length(rec
->addresses
);
757 if (rec
->state
== WREPL_STATE_ACTIVE
&& addr_count
== 0) {
758 rec
->state
= WREPL_STATE_RELEASED
;
760 if (rec
->type
== WREPL_TYPE_UNIQUE
&& addr_count
> 1) {
761 rec
->type
= WREPL_TYPE_MHOMED
;
764 expire_time
= ldb_timestring(msg
, rec
->expire_time
);
769 msg
->dn
= winsdb_dn(msg
, ldb
, rec
->name
);
770 if (msg
->dn
== NULL
) goto failed
;
771 ret
= ldb_msg_add_fmt(msg
, "type", "0x%02X", rec
->name
->type
);
772 if (rec
->name
->name
&& *rec
->name
->name
) {
773 ret
|= ldb_msg_add_string(msg
, "name", rec
->name
->name
);
775 if (rec
->name
->scope
&& *rec
->name
->scope
) {
776 ret
|= ldb_msg_add_string(msg
, "scope", rec
->name
->scope
);
778 ret
|= ldb_msg_add_fmt(msg
, "objectClass", "winsRecord");
779 ret
|= ldb_msg_add_fmt(msg
, "recordType", "%u", rec
->type
);
780 ret
|= ldb_msg_add_fmt(msg
, "recordState", "%u", rec
->state
);
781 ret
|= ldb_msg_add_fmt(msg
, "nodeType", "%u", rec
->node
);
782 ret
|= ldb_msg_add_fmt(msg
, "isStatic", "%u", rec
->is_static
);
783 ret
|= ldb_msg_add_empty(msg
, "expireTime", 0, NULL
);
784 if (!(rec
->is_static
&& rec
->state
== WREPL_STATE_ACTIVE
)) {
785 ret
|= ldb_msg_add_string(msg
, "expireTime", expire_time
);
787 ret
|= ldb_msg_add_fmt(msg
, "versionID", "%llu", (long long)rec
->version
);
788 ret
|= ldb_msg_add_string(msg
, "winsOwner", rec
->wins_owner
);
789 ret
|= ldb_msg_add_empty(msg
, "address", 0, NULL
);
790 for (i
=0;rec
->addresses
[i
];i
++) {
791 ret
|= ldb_msg_add_winsdb_addr(msg
, rec
, "address", rec
->addresses
[i
]);
793 if (rec
->registered_by
) {
794 ret
|= ldb_msg_add_empty(msg
, "registeredBy", 0, NULL
);
795 ret
|= ldb_msg_add_string(msg
, "registeredBy", rec
->registered_by
);
797 if (ret
!= LDB_SUCCESS
) goto failed
;
806 save a WINS record into the database
808 uint8_t winsdb_add(struct winsdb_handle
*h
, struct winsdb_record
*rec
, uint32_t flags
)
810 struct ldb_message
*msg
;
811 struct ldb_context
*wins_db
= h
->ldb
;
812 TALLOC_CTX
*tmp_ctx
= talloc_new(wins_db
);
816 trans
= ldb_transaction_start(wins_db
);
817 if (trans
!= LDB_SUCCESS
) goto failed
;
819 if (flags
& WINSDB_FLAG_ALLOC_VERSION
) {
820 /* passing '0' means auto-allocate a new one */
821 rec
->version
= winsdb_set_maxVersion(h
, 0);
822 if (rec
->version
== 0) goto failed
;
824 if (flags
& WINSDB_FLAG_TAKE_OWNERSHIP
) {
825 rec
->wins_owner
= h
->local_owner
;
828 msg
= winsdb_message(wins_db
, rec
, tmp_ctx
);
829 if (msg
== NULL
) goto failed
;
830 ret
= ldb_add(wins_db
, msg
);
831 if (ret
!= LDB_SUCCESS
) goto failed
;
833 trans
= ldb_transaction_commit(wins_db
);
834 if (trans
!= LDB_SUCCESS
) goto failed
;
836 wins_hook(h
, rec
, WINS_HOOK_ADD
, h
->hook_script
);
838 talloc_free(tmp_ctx
);
842 if (trans
== LDB_SUCCESS
) ldb_transaction_cancel(wins_db
);
843 talloc_free(tmp_ctx
);
844 return NBT_RCODE_SVR
;
849 modify a WINS record in the database
851 uint8_t winsdb_modify(struct winsdb_handle
*h
, struct winsdb_record
*rec
, uint32_t flags
)
853 struct ldb_message
*msg
;
854 struct ldb_context
*wins_db
= h
->ldb
;
855 TALLOC_CTX
*tmp_ctx
= talloc_new(wins_db
);
860 trans
= ldb_transaction_start(wins_db
);
861 if (trans
!= LDB_SUCCESS
) goto failed
;
863 if (flags
& WINSDB_FLAG_ALLOC_VERSION
) {
864 /* passing '0' means auto-allocate a new one */
865 rec
->version
= winsdb_set_maxVersion(h
, 0);
866 if (rec
->version
== 0) goto failed
;
868 if (flags
& WINSDB_FLAG_TAKE_OWNERSHIP
) {
869 rec
->wins_owner
= h
->local_owner
;
872 msg
= winsdb_message(wins_db
, rec
, tmp_ctx
);
873 if (msg
== NULL
) goto failed
;
875 for (i
=0;i
<msg
->num_elements
;i
++) {
876 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
879 ret
= ldb_modify(wins_db
, msg
);
880 if (ret
!= LDB_SUCCESS
) goto failed
;
882 trans
= ldb_transaction_commit(wins_db
);
883 if (trans
!= LDB_SUCCESS
) goto failed
;
885 wins_hook(h
, rec
, WINS_HOOK_MODIFY
, h
->hook_script
);
887 talloc_free(tmp_ctx
);
891 if (trans
== LDB_SUCCESS
) ldb_transaction_cancel(wins_db
);
892 talloc_free(tmp_ctx
);
893 return NBT_RCODE_SVR
;
898 delete a WINS record from the database
900 uint8_t winsdb_delete(struct winsdb_handle
*h
, struct winsdb_record
*rec
)
902 struct ldb_context
*wins_db
= h
->ldb
;
903 TALLOC_CTX
*tmp_ctx
= talloc_new(wins_db
);
908 trans
= ldb_transaction_start(wins_db
);
909 if (trans
!= LDB_SUCCESS
) goto failed
;
911 dn
= winsdb_dn(tmp_ctx
, wins_db
, rec
->name
);
912 if (dn
== NULL
) goto failed
;
914 ret
= ldb_delete(wins_db
, dn
);
915 if (ret
!= LDB_SUCCESS
) goto failed
;
917 trans
= ldb_transaction_commit(wins_db
);
918 if (trans
!= LDB_SUCCESS
) goto failed
;
920 wins_hook(h
, rec
, WINS_HOOK_DELETE
, h
->hook_script
);
922 talloc_free(tmp_ctx
);
926 if (trans
== LDB_SUCCESS
) ldb_transaction_cancel(wins_db
);
927 talloc_free(tmp_ctx
);
928 return NBT_RCODE_SVR
;
931 static bool winsdb_check_or_add_module_list(struct tevent_context
*ev_ctx
,
932 struct loadparm_context
*lp_ctx
, struct winsdb_handle
*h
)
937 struct ldb_result
*res
= NULL
;
938 struct ldb_message
*msg
= NULL
;
939 TALLOC_CTX
*tmp_ctx
= talloc_new(h
);
940 unsigned int flags
= 0;
942 trans
= ldb_transaction_start(h
->ldb
);
943 if (trans
!= LDB_SUCCESS
) goto failed
;
945 /* check if we have a special @MODULES record already */
946 dn
= ldb_dn_new(tmp_ctx
, h
->ldb
, "@MODULES");
947 if (!dn
) goto failed
;
949 /* find the record in the WINS database */
950 ret
= ldb_search(h
->ldb
, tmp_ctx
, &res
, dn
, LDB_SCOPE_BASE
, NULL
, NULL
);
951 if (ret
!= LDB_SUCCESS
) goto failed
;
953 if (res
->count
> 0) goto skip
;
955 /* if there's no record, add one */
956 msg
= ldb_msg_new(tmp_ctx
);
957 if (!msg
) goto failed
;
960 ret
= ldb_msg_add_string(msg
, "@LIST", "wins_ldb");
961 if (ret
!= LDB_SUCCESS
) goto failed
;
963 ret
= ldb_add(h
->ldb
, msg
);
964 if (ret
!= LDB_SUCCESS
) goto failed
;
966 trans
= ldb_transaction_commit(h
->ldb
);
967 if (trans
!= LDB_SUCCESS
) goto failed
;
969 /* close and reopen the database, with the modules */
970 trans
= LDB_ERR_OTHER
;
974 if (lpcfg_parm_bool(lp_ctx
, NULL
,"winsdb", "nosync", false)) {
975 flags
|= LDB_FLG_NOSYNC
;
978 h
->ldb
= ldb_wrap_connect(h
, ev_ctx
, lp_ctx
, lock_path(h
, lp_ctx
, lpcfg_wins_url(lp_ctx
)),
980 if (!h
->ldb
) goto failed
;
982 talloc_free(tmp_ctx
);
986 if (trans
== LDB_SUCCESS
) ldb_transaction_cancel(h
->ldb
);
987 talloc_free(tmp_ctx
);
991 if (trans
== LDB_SUCCESS
) ldb_transaction_cancel(h
->ldb
);
992 talloc_free(tmp_ctx
);
996 struct winsdb_handle
*winsdb_connect(TALLOC_CTX
*mem_ctx
,
997 struct tevent_context
*ev_ctx
,
998 struct loadparm_context
*lp_ctx
,
1000 enum winsdb_handle_caller caller
)
1002 struct winsdb_handle
*h
= NULL
;
1003 unsigned int flags
= 0;
1007 h
= talloc_zero(mem_ctx
, struct winsdb_handle
);
1008 if (!h
) return NULL
;
1010 if (lpcfg_parm_bool(lp_ctx
, NULL
,"winsdb", "nosync", false)) {
1011 flags
|= LDB_FLG_NOSYNC
;
1014 h
->ldb
= ldb_wrap_connect(h
, ev_ctx
, lp_ctx
, lock_path(h
, lp_ctx
, lpcfg_wins_url(lp_ctx
)),
1016 if (!h
->ldb
) goto failed
;
1019 h
->hook_script
= lpcfg_wins_hook(lp_ctx
);
1021 h
->local_owner
= talloc_strdup(h
, owner
);
1022 if (!h
->local_owner
) goto failed
;
1024 /* make sure the module list is available and used */
1025 ret
= winsdb_check_or_add_module_list(ev_ctx
, lp_ctx
, h
);
1026 if (!ret
) goto failed
;
1028 ldb_err
= ldb_set_opaque(h
->ldb
, "winsdb_handle", h
);
1029 if (ldb_err
!= LDB_SUCCESS
) goto failed
;