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
, dn
, LDB_SCOPE_BASE
,
50 if (ret
!= LDB_SUCCESS
) goto failed
;
51 talloc_steal(tmp_ctx
, res
);
52 if (res
->count
> 1) goto failed
;
54 if (res
->count
== 1) {
55 maxVersion
= ldb_msg_find_attr_as_uint64(res
->msgs
[0], "maxVersion", 0);
64 if newVersion == 0 return the old maxVersion + 1 and save it
65 if newVersion > 0 return MAX(oldMaxVersion, newMaxVersion) and save it
67 uint64_t winsdb_set_maxVersion(struct winsdb_handle
*h
, uint64_t newMaxVersion
)
72 struct ldb_result
*res
= NULL
;
73 struct ldb_message
*msg
= NULL
;
74 struct ldb_context
*wins_db
= h
->ldb
;
75 TALLOC_CTX
*tmp_ctx
= talloc_new(wins_db
);
76 uint64_t oldMaxVersion
= 0;
78 trans
= ldb_transaction_start(wins_db
);
79 if (trans
!= LDB_SUCCESS
) goto failed
;
81 dn
= ldb_dn_new(tmp_ctx
, wins_db
, "CN=VERSION");
84 /* find the record in the WINS database */
85 ret
= ldb_search(wins_db
, dn
, LDB_SCOPE_BASE
, NULL
, NULL
, &res
);
87 if (ret
!= LDB_SUCCESS
) goto failed
;
88 talloc_steal(tmp_ctx
, res
);
89 if (res
->count
> 1) goto failed
;
91 talloc_steal(tmp_ctx
, res
);
93 if (res
->count
== 1) {
94 oldMaxVersion
= ldb_msg_find_attr_as_uint64(res
->msgs
[0], "maxVersion", 0);
97 if (newMaxVersion
== 0) {
98 newMaxVersion
= oldMaxVersion
+ 1;
100 newMaxVersion
= MAX(oldMaxVersion
, newMaxVersion
);
103 msg
= ldb_msg_new(tmp_ctx
);
104 if (!msg
) goto failed
;
108 ret
= ldb_msg_add_empty(msg
, "objectClass", LDB_FLAG_MOD_REPLACE
, NULL
);
109 if (ret
!= 0) goto failed
;
110 ret
= ldb_msg_add_string(msg
, "objectClass", "winsMaxVersion");
111 if (ret
!= 0) goto failed
;
112 ret
= ldb_msg_add_empty(msg
, "maxVersion", LDB_FLAG_MOD_REPLACE
, NULL
);
113 if (ret
!= 0) goto failed
;
114 ret
= ldb_msg_add_fmt(msg
, "maxVersion", "%llu", (long long)newMaxVersion
);
115 if (ret
!= 0) goto failed
;
117 ret
= ldb_modify(wins_db
, msg
);
118 if (ret
!= 0) ret
= ldb_add(wins_db
, msg
);
119 if (ret
!= 0) goto failed
;
121 trans
= ldb_transaction_commit(wins_db
);
122 if (trans
!= LDB_SUCCESS
) goto failed
;
124 talloc_free(tmp_ctx
);
125 return newMaxVersion
;
128 if (trans
== LDB_SUCCESS
) ldb_transaction_cancel(wins_db
);
129 talloc_free(tmp_ctx
);
133 uint64_t winsdb_get_seqnumber(struct winsdb_handle
*h
)
136 struct ldb_context
*ldb
= h
->ldb
;
138 struct ldb_result
*res
= NULL
;
139 TALLOC_CTX
*tmp_ctx
= talloc_new(ldb
);
140 uint64_t seqnumber
= 0;
142 dn
= ldb_dn_new(tmp_ctx
, ldb
, "@BASEINFO");
143 if (!dn
) goto failed
;
145 /* find the record in the WINS database */
146 ret
= ldb_search(ldb
, dn
, LDB_SCOPE_BASE
,
148 if (ret
!= LDB_SUCCESS
) goto failed
;
149 talloc_steal(tmp_ctx
, res
);
150 if (res
->count
> 1) goto failed
;
152 if (res
->count
== 1) {
153 seqnumber
= ldb_msg_find_attr_as_uint64(res
->msgs
[0], "sequenceNumber", 0);
157 talloc_free(tmp_ctx
);
162 return a DN for a nbt_name
164 static struct ldb_dn
*winsdb_dn(TALLOC_CTX
*mem_ctx
, struct ldb_context
*ldb
, struct nbt_name
*name
)
168 dn
= ldb_dn_new_fmt(mem_ctx
, ldb
, "type=0x%02X", name
->type
);
169 if (ldb_dn_is_valid(dn
) && name
->name
&& *name
->name
) {
170 ldb_dn_add_child_fmt(dn
, "name=%s", name
->name
);
172 if (ldb_dn_is_valid(dn
) && name
->scope
&& *name
->scope
) {
173 ldb_dn_add_child_fmt(dn
, "scope=%s", name
->scope
);
178 static NTSTATUS
winsdb_nbt_name(TALLOC_CTX
*mem_ctx
, struct ldb_dn
*dn
, struct nbt_name
**_name
)
181 struct nbt_name
*name
;
182 unsigned int comp_num
;
185 name
= talloc(mem_ctx
, struct nbt_name
);
187 status
= NT_STATUS_NO_MEMORY
;
191 comp_num
= ldb_dn_get_comp_num(dn
);
194 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
198 if (comp_num
> cur
&& strcasecmp("scope", ldb_dn_get_component_name(dn
, cur
)) == 0) {
199 name
->scope
= (const char *)talloc_strdup(name
, (char *)ldb_dn_get_component_val(dn
, cur
)->data
);
205 if (comp_num
> cur
&& strcasecmp("name", ldb_dn_get_component_name(dn
, cur
)) == 0) {
206 name
->name
= (const char *)talloc_strdup(name
, (char *)ldb_dn_get_component_val(dn
, cur
)->data
);
209 name
->name
= talloc_strdup(name
, "");
211 status
= NT_STATUS_NO_MEMORY
;
216 if (comp_num
> cur
&& strcasecmp("type", ldb_dn_get_component_name(dn
, cur
)) == 0) {
217 name
->type
= strtoul((char *)ldb_dn_get_component_val(dn
, cur
)->data
, NULL
, 0);
220 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
232 decode the winsdb_addr("address") attribute:
234 "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
237 static NTSTATUS
winsdb_addr_decode(struct winsdb_handle
*h
, struct winsdb_record
*rec
, struct ldb_val
*val
,
238 TALLOC_CTX
*mem_ctx
, struct winsdb_addr
**_addr
)
241 struct winsdb_addr
*addr
;
243 const char *wins_owner
;
244 const char *expire_time
;
247 addr
= talloc(mem_ctx
, struct winsdb_addr
);
249 status
= NT_STATUS_NO_MEMORY
;
253 address
= (char *)val
->data
;
255 p
= strchr(address
, ';');
257 /* support old entries, with only the address */
258 addr
->address
= (const char *)talloc_steal(addr
, val
->data
);
259 addr
->wins_owner
= talloc_reference(addr
, rec
->wins_owner
);
260 if (!addr
->wins_owner
) {
261 status
= NT_STATUS_NO_MEMORY
;
264 addr
->expire_time
= rec
->expire_time
;
270 addr
->address
= talloc_strdup(addr
, address
);
271 if (!addr
->address
) {
272 status
= NT_STATUS_NO_MEMORY
;
276 if (strncmp("winsOwner:", p
, 10) != 0) {
277 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
281 p
= strchr(wins_owner
, ';');
283 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
288 if (strcmp(wins_owner
, "0.0.0.0") == 0) {
289 wins_owner
= h
->local_owner
;
291 addr
->wins_owner
= talloc_strdup(addr
, wins_owner
);
292 if (!addr
->wins_owner
) {
293 status
= NT_STATUS_NO_MEMORY
;
297 if (strncmp("expireTime:", p
, 11) != 0) {
298 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
302 expire_time
= p
+ 11;
303 p
= strchr(expire_time
, ';');
305 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
310 addr
->expire_time
= ldb_string_to_time(expire_time
);
320 encode the winsdb_addr("address") attribute like this:
322 "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
326 static int ldb_msg_add_winsdb_addr(struct ldb_message
*msg
, struct winsdb_record
*rec
,
327 const char *attr_name
, struct winsdb_addr
*addr
)
332 if (rec
->is_static
) {
333 str
= talloc_strdup(msg
, addr
->address
);
337 expire_time
= ldb_timestring(msg
, addr
->expire_time
);
338 if (!expire_time
) return -1;
339 str
= talloc_asprintf(msg
, "%s;winsOwner:%s;expireTime:%s;",
340 addr
->address
, addr
->wins_owner
,
342 talloc_free(expire_time
);
346 val
.data
= discard_const_p(uint8_t, str
);
347 val
.length
= strlen(str
);
349 return ldb_msg_add_value(msg
, attr_name
, &val
, NULL
);
352 struct winsdb_addr
**winsdb_addr_list_make(TALLOC_CTX
*mem_ctx
)
354 struct winsdb_addr
**addresses
;
356 addresses
= talloc_array(mem_ctx
, struct winsdb_addr
*, 1);
357 if (!addresses
) return NULL
;
364 static int winsdb_addr_sort_list (struct winsdb_addr
**p1
, struct winsdb_addr
**p2
, void *opaque
)
366 struct winsdb_addr
*a1
= talloc_get_type(*p1
, struct winsdb_addr
);
367 struct winsdb_addr
*a2
= talloc_get_type(*p2
, struct winsdb_addr
);
368 struct winsdb_handle
*h
= talloc_get_type(opaque
, struct winsdb_handle
);
369 bool a1_owned
= false;
370 bool a2_owned
= false;
373 * first the owned addresses with the newest to the oldest address
374 * then the replica addresses with the newest to the oldest address
376 if (a2
->expire_time
!= a1
->expire_time
) {
377 return a2
->expire_time
- a1
->expire_time
;
380 if (strcmp(a2
->wins_owner
, h
->local_owner
) == 0) {
384 if (strcmp(a1
->wins_owner
, h
->local_owner
) == 0) {
388 return a2_owned
- a1_owned
;
391 struct winsdb_addr
**winsdb_addr_list_add(struct winsdb_handle
*h
, const struct winsdb_record
*rec
,
392 struct winsdb_addr
**addresses
, const char *address
,
393 const char *wins_owner
, time_t expire_time
,
394 bool is_name_registration
)
396 struct winsdb_addr
*old_addr
= NULL
;
399 bool found_old_replica
= false;
402 * count the addresses and maybe
403 * find an old entry for the new address
405 for (i
=0; addresses
[i
]; i
++) {
406 if (old_addr
) continue;
407 if (strcmp(addresses
[i
]->address
, address
) == 0) {
408 old_addr
= addresses
[i
];
414 * the address is already there
415 * and we can replace it
418 goto remove_old_addr
;
422 * if we don't have 25 addresses already,
423 * we can just add the new address
430 * if we haven't found the address,
431 * and we have already have 25 addresses
432 * if so then we need to do the following:
433 * - if it isn't a name registration, then just ignore the new address
434 * - if it is a name registration, then first search for
435 * the oldest replica and if there's no replica address
436 * search the oldest owned address
438 if (!is_name_registration
) {
443 * find the oldest replica address, if there's no replica
444 * record at all, find the oldest owned address
446 for (i
=0; addresses
[i
]; i
++) {
447 bool cur_is_replica
= false;
448 /* find out if the current address is a replica */
449 if (strcmp(addresses
[i
]->wins_owner
, h
->local_owner
) != 0) {
450 cur_is_replica
= true;
454 * if we already found a replica address and the current address
455 * is not a replica, then skip it
457 if (found_old_replica
&& !cur_is_replica
) continue;
460 * if we found the first replica address, reset the address
461 * that would be replaced
463 if (!found_old_replica
&& cur_is_replica
) {
464 found_old_replica
= true;
465 old_addr
= addresses
[i
];
470 * if the first address isn't a replica, just start with
474 old_addr
= addresses
[i
];
479 * see if we find an older address
481 if (addresses
[i
]->expire_time
< old_addr
->expire_time
) {
482 old_addr
= addresses
[i
];
488 winsdb_addr_list_remove(addresses
, old_addr
->address
);
492 addresses
= talloc_realloc(addresses
, addresses
, struct winsdb_addr
*, len
+ 2);
493 if (!addresses
) return NULL
;
495 addresses
[len
] = talloc(addresses
, struct winsdb_addr
);
496 if (!addresses
[len
]) {
497 talloc_free(addresses
);
501 addresses
[len
]->address
= talloc_strdup(addresses
[len
], address
);
502 if (!addresses
[len
]->address
) {
503 talloc_free(addresses
);
507 addresses
[len
]->wins_owner
= talloc_strdup(addresses
[len
], wins_owner
);
508 if (!addresses
[len
]->wins_owner
) {
509 talloc_free(addresses
);
513 addresses
[len
]->expire_time
= expire_time
;
515 addresses
[len
+1] = NULL
;
517 ldb_qsort(addresses
, len
+1 , sizeof(addresses
[0]), h
, (ldb_qsort_cmp_fn_t
)winsdb_addr_sort_list
);
522 void winsdb_addr_list_remove(struct winsdb_addr
**addresses
, const char *address
)
526 for (i
=0; addresses
[i
]; i
++) {
527 if (strcmp(addresses
[i
]->address
, address
) == 0) {
532 for (; addresses
[i
]; i
++) {
533 addresses
[i
] = addresses
[i
+1];
539 struct winsdb_addr
*winsdb_addr_list_check(struct winsdb_addr
**addresses
, const char *address
)
543 for (i
=0; addresses
[i
]; i
++) {
544 if (strcmp(addresses
[i
]->address
, address
) == 0) {
552 size_t winsdb_addr_list_length(struct winsdb_addr
**addresses
)
555 for (i
=0; addresses
[i
]; i
++);
559 const char **winsdb_addr_string_list(TALLOC_CTX
*mem_ctx
, struct winsdb_addr
**addresses
)
561 size_t len
= winsdb_addr_list_length(addresses
);
562 const char **str_list
=NULL
;
565 for (i
=0; i
< len
; i
++) {
566 str_list
= str_list_add(str_list
, addresses
[i
]->address
);
571 talloc_steal(mem_ctx
, str_list
);
576 load a WINS entry from the database
578 NTSTATUS
winsdb_lookup(struct winsdb_handle
*h
,
579 struct nbt_name
*name
,
581 struct winsdb_record
**_rec
)
584 struct ldb_result
*res
= NULL
;
586 struct winsdb_record
*rec
;
587 struct ldb_context
*wins_db
= h
->ldb
;
588 TALLOC_CTX
*tmp_ctx
= talloc_new(mem_ctx
);
589 time_t now
= time(NULL
);
591 /* find the record in the WINS database */
592 ret
= ldb_search(wins_db
, winsdb_dn(tmp_ctx
, wins_db
, name
), LDB_SCOPE_BASE
,
595 talloc_steal(tmp_ctx
, res
);
596 if (ret
!= LDB_SUCCESS
|| res
->count
> 1) {
597 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
599 } else if (res
->count
== 0) {
600 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
604 status
= winsdb_record(h
, res
->msgs
[0], tmp_ctx
, now
, &rec
);
605 if (!NT_STATUS_IS_OK(status
)) goto failed
;
607 talloc_steal(mem_ctx
, rec
);
608 talloc_free(tmp_ctx
);
613 talloc_free(tmp_ctx
);
617 NTSTATUS
winsdb_record(struct winsdb_handle
*h
, struct ldb_message
*msg
, TALLOC_CTX
*mem_ctx
, time_t now
, struct winsdb_record
**_rec
)
620 struct winsdb_record
*rec
;
621 struct ldb_message_element
*el
;
622 struct nbt_name
*name
;
623 uint32_t i
, j
, num_values
;
624 bool we_are_owner
= false;
626 rec
= talloc(mem_ctx
, struct winsdb_record
);
628 status
= NT_STATUS_NO_MEMORY
;
632 status
= winsdb_nbt_name(rec
, msg
->dn
, &name
);
633 if (!NT_STATUS_IS_OK(status
)) goto failed
;
635 if (strlen(name
->name
) > 15) {
636 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
639 if (name
->scope
&& strlen(name
->scope
) > 238) {
640 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
644 /* parse it into a more convenient winsdb_record structure */
646 rec
->type
= ldb_msg_find_attr_as_int(msg
, "recordType", WREPL_TYPE_UNIQUE
);
647 rec
->state
= ldb_msg_find_attr_as_int(msg
, "recordState", WREPL_STATE_RELEASED
);
648 rec
->node
= ldb_msg_find_attr_as_int(msg
, "nodeType", WREPL_NODE_B
);
649 rec
->is_static
= ldb_msg_find_attr_as_int(msg
, "isStatic", 0);
650 rec
->expire_time
= ldb_string_to_time(ldb_msg_find_attr_as_string(msg
, "expireTime", NULL
));
651 rec
->version
= ldb_msg_find_attr_as_uint64(msg
, "versionID", 0);
652 rec
->wins_owner
= ldb_msg_find_attr_as_string(msg
, "winsOwner", NULL
);
653 rec
->registered_by
= ldb_msg_find_attr_as_string(msg
, "registeredBy", NULL
);
654 talloc_steal(rec
, rec
->wins_owner
);
655 talloc_steal(rec
, rec
->registered_by
);
657 if (!rec
->wins_owner
|| strcmp(rec
->wins_owner
, "0.0.0.0") == 0) {
658 rec
->wins_owner
= h
->local_owner
;
661 el
= ldb_msg_find_element(msg
, "address");
663 num_values
= el
->num_values
;
668 if (rec
->type
== WREPL_TYPE_UNIQUE
|| rec
->type
== WREPL_TYPE_GROUP
) {
669 if (num_values
!= 1) {
670 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
674 if (rec
->state
== WREPL_STATE_ACTIVE
) {
675 if (num_values
< 1) {
676 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
680 if (num_values
> 25) {
681 status
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
685 if (strcmp(rec
->wins_owner
, h
->local_owner
) == 0) {
690 * see if it has already expired
692 * NOTE: only expire owned records this way!
693 * w2k3 resolves expired replicas
694 * which are in active state
696 if (!rec
->is_static
&&
697 rec
->expire_time
<= now
&&
698 rec
->state
== WREPL_STATE_ACTIVE
&&
700 DEBUG(5,("WINS: expiring name %s (expired at %s)\n",
701 nbt_name_string(mem_ctx
, rec
->name
), timestring(mem_ctx
, rec
->expire_time
)));
702 rec
->state
= WREPL_STATE_RELEASED
;
705 rec
->addresses
= talloc_array(rec
, struct winsdb_addr
*, num_values
+1);
706 if (rec
->addresses
== NULL
) {
707 status
= NT_STATUS_NO_MEMORY
;
711 for (i
=0,j
=0;i
<num_values
;i
++) {
712 status
= winsdb_addr_decode(h
, rec
, &el
->values
[i
], rec
->addresses
, &rec
->addresses
[j
]);
713 if (!NT_STATUS_IS_OK(status
)) goto failed
;
716 * the record isn't static and is active
717 * then don't add the address if it's expired
719 if (!rec
->is_static
&&
720 rec
->addresses
[j
]->expire_time
<= now
&&
721 rec
->state
== WREPL_STATE_ACTIVE
&&
723 DEBUG(5,("WINS: expiring name addr %s of %s (expired at %s)\n",
724 rec
->addresses
[j
]->address
, nbt_name_string(rec
->addresses
[j
], rec
->name
),
725 timestring(rec
->addresses
[j
], rec
->addresses
[j
]->expire_time
)));
726 talloc_free(rec
->addresses
[j
]);
727 rec
->addresses
[j
] = NULL
;
732 rec
->addresses
[j
] = NULL
;
735 if (rec
->is_static
&& rec
->state
== WREPL_STATE_ACTIVE
) {
736 rec
->expire_time
= get_time_t_max();
737 for (i
=0;rec
->addresses
[i
];i
++) {
738 rec
->addresses
[i
]->expire_time
= rec
->expire_time
;
742 if (rec
->state
== WREPL_STATE_ACTIVE
) {
743 if (num_values
< 1) {
744 DEBUG(5,("WINS: expiring name %s (because it has no active addresses)\n",
745 nbt_name_string(mem_ctx
, rec
->name
)));
746 rec
->state
= WREPL_STATE_RELEASED
;
753 if (NT_STATUS_EQUAL(NT_STATUS_INTERNAL_DB_CORRUPTION
, status
)) {
754 DEBUG(1,("winsdb_record: corrupted record: %s\n", ldb_dn_get_linearized(msg
->dn
)));
761 form a ldb_message from a winsdb_record
763 struct ldb_message
*winsdb_message(struct ldb_context
*ldb
,
764 struct winsdb_record
*rec
, TALLOC_CTX
*mem_ctx
)
768 const char *expire_time
;
769 struct ldb_message
*msg
= ldb_msg_new(mem_ctx
);
770 if (msg
== NULL
) goto failed
;
772 /* make sure we don't put in corrupted records */
773 addr_count
= winsdb_addr_list_length(rec
->addresses
);
774 if (rec
->state
== WREPL_STATE_ACTIVE
&& addr_count
== 0) {
775 rec
->state
= WREPL_STATE_RELEASED
;
777 if (rec
->type
== WREPL_TYPE_UNIQUE
&& addr_count
> 1) {
778 rec
->type
= WREPL_TYPE_MHOMED
;
781 expire_time
= ldb_timestring(msg
, rec
->expire_time
);
786 msg
->dn
= winsdb_dn(msg
, ldb
, rec
->name
);
787 if (msg
->dn
== NULL
) goto failed
;
788 ret
|= ldb_msg_add_fmt(msg
, "type", "0x%02X", rec
->name
->type
);
789 if (rec
->name
->name
&& *rec
->name
->name
) {
790 ret
|= ldb_msg_add_string(msg
, "name", rec
->name
->name
);
792 if (rec
->name
->scope
&& *rec
->name
->scope
) {
793 ret
|= ldb_msg_add_string(msg
, "scope", rec
->name
->scope
);
795 ret
|= ldb_msg_add_fmt(msg
, "objectClass", "winsRecord");
796 ret
|= ldb_msg_add_fmt(msg
, "recordType", "%u", rec
->type
);
797 ret
|= ldb_msg_add_fmt(msg
, "recordState", "%u", rec
->state
);
798 ret
|= ldb_msg_add_fmt(msg
, "nodeType", "%u", rec
->node
);
799 ret
|= ldb_msg_add_fmt(msg
, "isStatic", "%u", rec
->is_static
);
800 ret
|= ldb_msg_add_empty(msg
, "expireTime", 0, NULL
);
801 if (!(rec
->is_static
&& rec
->state
== WREPL_STATE_ACTIVE
)) {
802 ret
|= ldb_msg_add_string(msg
, "expireTime", expire_time
);
804 ret
|= ldb_msg_add_fmt(msg
, "versionID", "%llu", (long long)rec
->version
);
805 ret
|= ldb_msg_add_string(msg
, "winsOwner", rec
->wins_owner
);
806 ret
|= ldb_msg_add_empty(msg
, "address", 0, NULL
);
807 for (i
=0;rec
->addresses
[i
];i
++) {
808 ret
|= ldb_msg_add_winsdb_addr(msg
, rec
, "address", rec
->addresses
[i
]);
810 ret
|= ldb_msg_add_empty(msg
, "registeredBy", 0, NULL
);
811 if (rec
->registered_by
) {
812 ret
|= ldb_msg_add_string(msg
, "registeredBy", rec
->registered_by
);
813 if (ret
!= 0) goto failed
;
823 save a WINS record into the database
825 uint8_t winsdb_add(struct winsdb_handle
*h
, struct winsdb_record
*rec
, uint32_t flags
)
827 struct ldb_message
*msg
;
828 struct ldb_context
*wins_db
= h
->ldb
;
829 TALLOC_CTX
*tmp_ctx
= talloc_new(wins_db
);
833 trans
= ldb_transaction_start(wins_db
);
834 if (trans
!= LDB_SUCCESS
) goto failed
;
836 if (flags
& WINSDB_FLAG_ALLOC_VERSION
) {
837 /* passing '0' means auto-allocate a new one */
838 rec
->version
= winsdb_set_maxVersion(h
, 0);
839 if (rec
->version
== 0) goto failed
;
841 if (flags
& WINSDB_FLAG_TAKE_OWNERSHIP
) {
842 rec
->wins_owner
= h
->local_owner
;
845 msg
= winsdb_message(wins_db
, rec
, tmp_ctx
);
846 if (msg
== NULL
) goto failed
;
847 ret
= ldb_add(wins_db
, msg
);
848 if (ret
!= 0) goto failed
;
850 trans
= ldb_transaction_commit(wins_db
);
851 if (trans
!= LDB_SUCCESS
) goto failed
;
853 wins_hook(h
, rec
, WINS_HOOK_ADD
);
855 talloc_free(tmp_ctx
);
859 if (trans
== LDB_SUCCESS
) ldb_transaction_cancel(wins_db
);
860 talloc_free(tmp_ctx
);
861 return NBT_RCODE_SVR
;
866 modify a WINS record in the database
868 uint8_t winsdb_modify(struct winsdb_handle
*h
, struct winsdb_record
*rec
, uint32_t flags
)
870 struct ldb_message
*msg
;
871 struct ldb_context
*wins_db
= h
->ldb
;
872 TALLOC_CTX
*tmp_ctx
= talloc_new(wins_db
);
877 trans
= ldb_transaction_start(wins_db
);
878 if (trans
!= LDB_SUCCESS
) goto failed
;
880 if (flags
& WINSDB_FLAG_ALLOC_VERSION
) {
881 /* passing '0' means auto-allocate a new one */
882 rec
->version
= winsdb_set_maxVersion(h
, 0);
883 if (rec
->version
== 0) goto failed
;
885 if (flags
& WINSDB_FLAG_TAKE_OWNERSHIP
) {
886 rec
->wins_owner
= h
->local_owner
;
889 msg
= winsdb_message(wins_db
, rec
, tmp_ctx
);
890 if (msg
== NULL
) goto failed
;
892 for (i
=0;i
<msg
->num_elements
;i
++) {
893 msg
->elements
[i
].flags
= LDB_FLAG_MOD_REPLACE
;
896 ret
= ldb_modify(wins_db
, msg
);
897 if (ret
!= 0) goto failed
;
899 trans
= ldb_transaction_commit(wins_db
);
900 if (trans
!= LDB_SUCCESS
) goto failed
;
902 wins_hook(h
, rec
, WINS_HOOK_MODIFY
);
904 talloc_free(tmp_ctx
);
908 if (trans
== LDB_SUCCESS
) ldb_transaction_cancel(wins_db
);
909 talloc_free(tmp_ctx
);
910 return NBT_RCODE_SVR
;
915 delete a WINS record from the database
917 uint8_t winsdb_delete(struct winsdb_handle
*h
, struct winsdb_record
*rec
)
919 struct ldb_context
*wins_db
= h
->ldb
;
920 TALLOC_CTX
*tmp_ctx
= talloc_new(wins_db
);
925 trans
= ldb_transaction_start(wins_db
);
926 if (trans
!= LDB_SUCCESS
) goto failed
;
928 dn
= winsdb_dn(tmp_ctx
, wins_db
, rec
->name
);
929 if (dn
== NULL
) goto failed
;
931 ret
= ldb_delete(wins_db
, dn
);
932 if (ret
!= 0) goto failed
;
934 trans
= ldb_transaction_commit(wins_db
);
935 if (trans
!= LDB_SUCCESS
) goto failed
;
937 wins_hook(h
, rec
, WINS_HOOK_DELETE
);
939 talloc_free(tmp_ctx
);
943 if (trans
== LDB_SUCCESS
) ldb_transaction_cancel(wins_db
);
944 talloc_free(tmp_ctx
);
945 return NBT_RCODE_SVR
;
948 static bool winsdb_check_or_add_module_list(struct winsdb_handle
*h
)
953 struct ldb_result
*res
= NULL
;
954 struct ldb_message
*msg
= NULL
;
955 TALLOC_CTX
*tmp_ctx
= talloc_new(h
);
956 unsigned int flags
= 0;
958 trans
= ldb_transaction_start(h
->ldb
);
959 if (trans
!= LDB_SUCCESS
) goto failed
;
961 /* check if we have a special @MODULES record already */
962 dn
= ldb_dn_new(tmp_ctx
, h
->ldb
, "@MODULES");
963 if (!dn
) goto failed
;
965 /* find the record in the WINS database */
966 ret
= ldb_search(h
->ldb
, dn
, LDB_SCOPE_BASE
, NULL
, NULL
, &res
);
967 if (ret
!= LDB_SUCCESS
) goto failed
;
968 talloc_steal(tmp_ctx
, res
);
970 if (res
->count
> 0) goto skip
;
972 /* if there's no record, add one */
973 msg
= ldb_msg_new(tmp_ctx
);
974 if (!msg
) goto failed
;
977 ret
= ldb_msg_add_string(msg
, "@LIST", "wins_ldb");
978 if (ret
!= 0) goto failed
;
980 ret
= ldb_add(h
->ldb
, msg
);
981 if (ret
!= 0) goto failed
;
983 trans
= ldb_transaction_commit(h
->ldb
);
984 if (trans
!= LDB_SUCCESS
) goto failed
;
986 /* close and reopen the database, with the modules */
987 trans
= LDB_ERR_OTHER
;
991 if (lp_parm_bool(global_loadparm
, NULL
,"winsdb", "nosync", false)) {
992 flags
|= LDB_FLG_NOSYNC
;
995 h
->ldb
= ldb_wrap_connect(h
, global_loadparm
, lock_path(h
, global_loadparm
, lp_wins_url(global_loadparm
)),
996 NULL
, NULL
, flags
, NULL
);
997 if (!h
->ldb
) goto failed
;
999 talloc_free(tmp_ctx
);
1003 if (trans
== LDB_SUCCESS
) ldb_transaction_cancel(h
->ldb
);
1004 talloc_free(tmp_ctx
);
1008 if (trans
== LDB_SUCCESS
) ldb_transaction_cancel(h
->ldb
);
1009 talloc_free(tmp_ctx
);
1013 struct winsdb_handle
*winsdb_connect(TALLOC_CTX
*mem_ctx
, enum winsdb_handle_caller caller
)
1015 struct winsdb_handle
*h
= NULL
;
1017 unsigned int flags
= 0;
1021 h
= talloc(mem_ctx
, struct winsdb_handle
);
1022 if (!h
) return NULL
;
1024 if (lp_parm_bool(global_loadparm
, NULL
,"winsdb", "nosync", false)) {
1025 flags
|= LDB_FLG_NOSYNC
;
1028 h
->ldb
= ldb_wrap_connect(h
, global_loadparm
, lock_path(h
, global_loadparm
, lp_wins_url(global_loadparm
)),
1029 NULL
, NULL
, flags
, NULL
);
1030 if (!h
->ldb
) goto failed
;
1034 owner
= lp_parm_string(global_loadparm
, NULL
, "winsdb", "local_owner");
1036 owner
= iface_n_ip(0);
1039 h
->local_owner
= talloc_strdup(h
, owner
);
1040 if (!h
->local_owner
) goto failed
;
1042 /* make sure the module list is available and used */
1043 ret
= winsdb_check_or_add_module_list(h
);
1044 if (!ret
) goto failed
;
1046 ldb_err
= ldb_set_opaque(h
->ldb
, "winsdb_handle", h
);
1047 if (ldb_err
!= LDB_SUCCESS
) goto failed
;