r12535: - simplify string list handling in a couple of places using str_list_add()
[ira/wip.git] / source4 / nbt_server / wins / winsdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    WINS database routines
5
6    Copyright (C) Andrew Tridgell        2005
7    Copyright (C) Stefan Metzmacher      2005
8       
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.
13    
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.
18    
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.
22 */
23
24 #include "includes.h"
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"
29 #include "db_wrap.h"
30 #include "system/time.h"
31
32 /*
33   return the new maxVersion and save it
34 */
35 static uint64_t winsdb_allocate_version(struct ldb_context *wins_db)
36 {
37         int trans;
38         int ret;
39         struct ldb_dn *dn;
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;
44
45         trans = ldb_transaction_start(wins_db);
46         if (trans != LDB_SUCCESS) goto failed;
47
48         dn = ldb_dn_explode(tmp_ctx, "CN=VERSION");
49         if (!dn) goto failed;
50
51         /* find the record in the WINS database */
52         ret = ldb_search(wins_db, dn, LDB_SCOPE_BASE, NULL, NULL, &res);
53
54         if (ret != LDB_SUCCESS) goto failed;
55         if (res->count > 1) goto failed;
56
57         talloc_steal(tmp_ctx, res);
58
59         if (res->count == 1) {
60                 maxVersion = ldb_msg_find_uint64(res->msgs[0], "maxVersion", 0);
61         }
62         maxVersion++;
63
64         msg = ldb_msg_new(tmp_ctx);
65         if (!msg) goto failed;
66         msg->dn = dn;
67
68
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;
77
78         ret = ldb_modify(wins_db, msg);
79         if (ret != 0) ret = ldb_add(wins_db, msg);
80         if (ret != 0) goto failed;
81
82         trans = ldb_transaction_commit(wins_db);
83         if (trans != LDB_SUCCESS) goto failed;
84
85         talloc_free(tmp_ctx);
86         return maxVersion;
87
88 failed:
89         if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
90         talloc_free(tmp_ctx);
91         return 0;
92 }
93
94 /*
95   return a DN for a nbt_name
96 */
97 static struct ldb_dn *winsdb_dn(TALLOC_CTX *mem_ctx, struct nbt_name *name)
98 {
99         struct ldb_dn *dn;
100
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);
104         }
105         if (dn && name->scope && *name->scope) {
106                 dn = ldb_dn_string_compose(mem_ctx, dn, "scope=%s", name->scope);
107         }
108         return dn;
109 }
110
111 static NTSTATUS winsdb_nbt_name(TALLOC_CTX *mem_ctx, struct ldb_dn *dn, struct nbt_name **_name)
112 {
113         NTSTATUS status;
114         struct nbt_name *name;
115         uint32_t cur = 0;
116
117         name = talloc(mem_ctx, struct nbt_name);
118         if (!name) {
119                 status = NT_STATUS_NO_MEMORY;
120                 goto failed;
121         }
122
123         if (dn->comp_num > 3) {
124                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
125                 goto failed;
126         }
127
128         if (dn->comp_num > cur && strcasecmp("scope", dn->components[cur].name) == 0) {
129                 name->scope     = talloc_steal(name, dn->components[cur].value.data);
130                 cur++;
131         } else {
132                 name->scope     = NULL;
133         }
134
135         if (dn->comp_num > cur && strcasecmp("name", dn->components[cur].name) == 0) {
136                 name->name      = talloc_steal(name, dn->components[cur].value.data);
137                 cur++;
138         } else {
139                 name->name      = talloc_strdup(name, "");
140                 if (!name->name) {
141                         status = NT_STATUS_NO_MEMORY;
142                         goto failed;
143                 }
144         }
145
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);
148                 cur++;
149         } else {
150                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
151                 goto failed;
152         }
153
154         *_name = name;
155         return NT_STATUS_OK;
156 failed:
157         talloc_free(name);
158         return status;
159 }
160
161 /*
162  decode the winsdb_addr("address") attribute:
163  "172.31.1.1" or 
164  "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
165  are valid records
166 */
167 static NTSTATUS winsdb_addr_decode(struct winsdb_record *rec, struct ldb_val *val,
168                                    TALLOC_CTX *mem_ctx, struct winsdb_addr **_addr)
169 {
170         NTSTATUS status;
171         struct winsdb_addr *addr;
172         char *address;
173         char *wins_owner;
174         char *expire_time;
175         char *p;
176
177         addr = talloc(mem_ctx, struct winsdb_addr);
178         if (!addr) {
179                 status = NT_STATUS_NO_MEMORY;
180                 goto failed;
181         }
182
183         address = (char *)val->data;
184
185         p = strchr(address, ';');
186         if (!p) {
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;
192                         goto failed;
193                 }
194                 addr->expire_time       = rec->expire_time;
195                 *_addr = addr;
196                 return NT_STATUS_OK;
197         }
198
199         *p = '\0';p++;
200         addr->address = talloc_strdup(addr, address);
201         if (!addr->address) {
202                 status = NT_STATUS_NO_MEMORY;
203                 goto failed;
204         }
205
206         if (strncmp("winsOwner:", p, 10) != 0) {
207                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
208                 goto failed;
209         }
210         wins_owner = p + 10;
211         p = strchr(wins_owner, ';');
212         if (!p) {
213                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
214                 goto failed;
215         }
216
217         *p = '\0';p++;
218         addr->wins_owner = talloc_strdup(addr, wins_owner);
219         if (!addr->wins_owner) {
220                 status = NT_STATUS_NO_MEMORY;
221                 goto failed;
222         }
223
224         if (strncmp("expireTime:", p, 11) != 0) {
225                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
226                 goto failed;
227         }
228
229         expire_time = p + 11;
230         p = strchr(expire_time, ';');
231         if (!p) {
232                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
233                 goto failed;
234         }
235
236         *p = '\0';p++;
237         addr->expire_time = ldb_string_to_time(expire_time);
238
239         *_addr = addr;
240         return NT_STATUS_OK;
241 failed:
242         talloc_free(addr);
243         return status;
244 }
245
246 /*
247  encode the winsdb_addr("address") attribute like this:
248  "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
249 */
250 static int ldb_msg_add_winsdb_addr(struct ldb_message *msg, 
251                                    const char *attr_name, struct winsdb_addr *addr)
252 {
253         struct ldb_val val;
254         const char *str;
255
256         str = talloc_asprintf(msg, "%s;winsOwner:%s;expireTime:%s;",
257                               addr->address, addr->wins_owner,
258                               ldb_timestring(msg, addr->expire_time));
259         if (!str) return -1;
260
261         val.data = discard_const_p(uint8_t, str);
262         val.length = strlen(str);
263
264         return ldb_msg_add_value(msg, attr_name, &val);
265 }
266
267 struct winsdb_addr **winsdb_addr_list_make(TALLOC_CTX *mem_ctx)
268 {
269         struct winsdb_addr **addresses;
270
271         addresses = talloc_array(mem_ctx, struct winsdb_addr *, 1);
272         if (!addresses) return NULL;
273
274         addresses[0] = NULL;
275
276         return addresses;
277 }
278
279 struct winsdb_addr **winsdb_addr_list_add(struct winsdb_addr **addresses, const char *address,
280                                           const char *wins_owner, time_t expire_time)
281 {
282         size_t len = winsdb_addr_list_length(addresses);
283
284         addresses = talloc_realloc(addresses, addresses, struct winsdb_addr *, len + 2);
285         if (!addresses) return NULL;
286
287         addresses[len] = talloc(addresses, struct winsdb_addr);
288         if (!addresses[len]) {
289                 talloc_free(addresses);
290                 return NULL;
291         }
292
293         addresses[len]->address = talloc_strdup(addresses[len], address);
294         if (!addresses[len]->address) {
295                 talloc_free(addresses);
296                 return NULL;
297         }
298
299         addresses[len]->wins_owner = talloc_strdup(addresses[len], wins_owner);
300         if (!addresses[len]->wins_owner) {
301                 talloc_free(addresses);
302                 return NULL;
303         }
304
305         addresses[len]->expire_time = expire_time;
306
307         addresses[len+1] = NULL;
308
309         return addresses;
310 }
311
312 void winsdb_addr_list_remove(struct winsdb_addr **addresses, const char *address)
313 {
314         size_t i;
315
316         for (i=0; addresses[i]; i++) {
317                 if (strcmp(addresses[i]->address, address) == 0) {
318                         break;
319                 }
320         }
321         if (!addresses[i]) return;
322
323         for (; addresses[i]; i++) {
324                 addresses[i] = addresses[i+1];
325         }
326
327         return;
328 }
329
330 struct winsdb_addr *winsdb_addr_list_check(struct winsdb_addr **addresses, const char *address)
331 {
332         size_t i;
333
334         for (i=0; addresses[i]; i++) {
335                 if (strcmp(addresses[i]->address, address) == 0) {
336                         return addresses[i];
337                 }
338         }
339
340         return NULL;
341 }
342
343 size_t winsdb_addr_list_length(struct winsdb_addr **addresses)
344 {
345         size_t i;
346         for (i=0; addresses[i]; i++);
347         return i;
348 }
349
350 const char **winsdb_addr_string_list(TALLOC_CTX *mem_ctx, struct winsdb_addr **addresses)
351 {
352         size_t len = winsdb_addr_list_length(addresses);
353         const char **str_list=NULL;
354         size_t i;
355
356         for (i=0; i < len; i++) {
357                 str_list = str_list_add(str_list, addresses[i]->address);
358                 if (!str_list[i]) {
359                         return NULL;
360                 }
361         }
362         talloc_steal(mem_ctx, str_list);
363         return str_list;
364 }
365
366 /*
367   load a WINS entry from the database
368 */
369 NTSTATUS winsdb_lookup(struct ldb_context *wins_db, 
370                        struct nbt_name *name,
371                        TALLOC_CTX *mem_ctx,
372                        struct winsdb_record **_rec)
373 {
374         NTSTATUS status;
375         struct ldb_result *res = NULL;
376         int ret;
377         struct winsdb_record *rec;
378         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
379
380         /* find the record in the WINS database */
381         ret = ldb_search(wins_db, winsdb_dn(tmp_ctx, name), LDB_SCOPE_BASE, 
382                          NULL, NULL, &res);
383
384         if (ret != LDB_SUCCESS || res->count > 1) {
385                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
386                 goto failed;
387         } else if (res->count== 0) {
388                 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
389                 goto failed;
390         }
391
392         talloc_steal(tmp_ctx, res);
393
394         status = winsdb_record(res->msgs[0], tmp_ctx, &rec);
395         if (!NT_STATUS_IS_OK(status)) goto failed;
396
397         /* see if it has already expired */
398         if (rec->state == WREPL_STATE_ACTIVE &&
399             rec->expire_time <= time(NULL)) {
400                 DEBUG(5,("WINS: expiring name %s (expired at %s)\n", 
401                          nbt_name_string(tmp_ctx, rec->name), timestring(tmp_ctx, rec->expire_time)));
402                 rec->state = WREPL_STATE_RELEASED;
403         }
404
405         talloc_steal(mem_ctx, rec);
406         talloc_free(tmp_ctx);
407         *_rec = rec;
408         return NT_STATUS_OK;
409
410 failed:
411         talloc_free(tmp_ctx);
412         return status;
413 }
414
415 NTSTATUS winsdb_record(struct ldb_message *msg, TALLOC_CTX *mem_ctx, struct winsdb_record **_rec)
416 {
417         NTSTATUS status;
418         struct winsdb_record *rec;
419         struct ldb_message_element *el;
420         struct nbt_name *name;
421         uint32_t i, num_values;
422
423         rec = talloc(mem_ctx, struct winsdb_record);
424         if (rec == NULL) {
425                 status = NT_STATUS_NO_MEMORY;
426                 goto failed;
427         }
428
429         status = winsdb_nbt_name(rec, msg->dn, &name);
430         if (!NT_STATUS_IS_OK(status)) goto failed;
431
432         if (strlen(name->name) > 15) {
433                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
434                 goto failed;
435         }
436         if (name->scope && strlen(name->scope) > 238) {
437                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
438                 goto failed;
439         }
440
441         /* parse it into a more convenient winsdb_record structure */
442         rec->name               = name;
443         rec->type               = ldb_msg_find_int(msg, "recordType", WREPL_TYPE_UNIQUE);
444         rec->state              = ldb_msg_find_int(msg, "recordState", WREPL_STATE_RELEASED);
445         rec->node               = ldb_msg_find_int(msg, "nodeType", WREPL_NODE_B);
446         rec->is_static          = ldb_msg_find_int(msg, "isStatic", 0);
447         rec->expire_time        = ldb_string_to_time(ldb_msg_find_string(msg, "expireTime", NULL));
448         rec->version            = ldb_msg_find_uint64(msg, "versionID", 0);
449         rec->wins_owner         = ldb_msg_find_string(msg, "winsOwner", NULL);
450         rec->registered_by      = ldb_msg_find_string(msg, "registeredBy", NULL);
451         talloc_steal(rec, rec->wins_owner);
452         talloc_steal(rec, rec->registered_by);
453
454         if (!rec->wins_owner) {
455                 rec->wins_owner = talloc_strdup(rec, WINSDB_OWNER_LOCAL);
456                 if (rec->wins_owner == NULL) {
457                         status = NT_STATUS_NO_MEMORY;
458                         goto failed;
459                 }
460         }
461
462         el = ldb_msg_find_element(msg, "address");
463         if (el) {
464                 num_values = el->num_values;
465         } else {
466                 num_values = 0;
467         }
468
469         if (rec->type == WREPL_TYPE_UNIQUE || rec->type == WREPL_TYPE_GROUP) {
470                 if (num_values != 1) {
471                         status = NT_STATUS_INTERNAL_DB_CORRUPTION;
472                         goto failed;
473                 }
474         }
475         if (rec->state == WREPL_STATE_ACTIVE) {
476                 if (num_values < 1) {
477                         status = NT_STATUS_INTERNAL_DB_CORRUPTION;
478                         goto failed;
479                 }
480         }
481
482         rec->addresses     = talloc_array(rec, struct winsdb_addr *, num_values+1);
483         if (rec->addresses == NULL) {
484                 status = NT_STATUS_NO_MEMORY;
485                 goto failed;
486         }
487
488         for (i=0;i<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;
491         }
492         rec->addresses[i] = NULL;
493
494         if (rec->is_static) {
495                 if (num_values < 1) {
496                         status = NT_STATUS_INTERNAL_DB_CORRUPTION;
497                         goto failed;
498                 }
499                 rec->state = WREPL_STATE_ACTIVE;
500                 rec->expire_time = get_time_t_max();
501                 for (i=0;rec->addresses[i];i++) {
502                         rec->addresses[i]->expire_time = rec->expire_time;
503                 }
504         }
505
506         *_rec = rec;
507         return NT_STATUS_OK;
508 failed:
509         if (NT_STATUS_EQUAL(NT_STATUS_INTERNAL_DB_CORRUPTION, status)) {
510                 DEBUG(1,("winsdb_record: corrupted record: %s\n", ldb_dn_linearize(rec, msg->dn)));
511         }
512         talloc_free(rec);
513         return status;
514 }
515
516 /*
517   form a ldb_message from a winsdb_record
518 */
519 struct ldb_message *winsdb_message(struct ldb_context *ldb, 
520                                    struct winsdb_record *rec, TALLOC_CTX *mem_ctx)
521 {
522         int i, ret=0;
523         size_t addr_count;
524         struct ldb_message *msg = ldb_msg_new(mem_ctx);
525         if (msg == NULL) goto failed;
526
527         if (rec->is_static) {
528                 rec->state = WREPL_STATE_ACTIVE;
529                 rec->expire_time = get_time_t_max();
530                 for (i=0;rec->addresses[i];i++) {
531                         rec->addresses[i]->expire_time = rec->expire_time;
532                 }
533         }
534
535         /* make sure we don't put in corrupted records */
536         addr_count = winsdb_addr_list_length(rec->addresses);
537         if (rec->state == WREPL_STATE_ACTIVE && addr_count == 0) {
538                 rec->state = WREPL_STATE_RELEASED;
539         }
540         if (rec->type == WREPL_TYPE_UNIQUE && addr_count > 1) {
541                 rec->type = WREPL_TYPE_MHOMED;
542         }
543
544         msg->dn = winsdb_dn(msg, rec->name);
545         if (msg->dn == NULL) goto failed;
546         ret |= ldb_msg_add_fmt(msg, "type", "0x%02X", rec->name->type);
547         if (rec->name->name && *rec->name->name) {
548                 ret |= ldb_msg_add_string(msg, "name", rec->name->name);
549         }
550         if (rec->name->scope && *rec->name->scope) {
551                 ret |= ldb_msg_add_string(msg, "scope", rec->name->scope);
552         }
553         ret |= ldb_msg_add_fmt(msg, "objectClass", "winsRecord");
554         ret |= ldb_msg_add_fmt(msg, "recordType", "%u", rec->type);
555         ret |= ldb_msg_add_fmt(msg, "recordState", "%u", rec->state);
556         ret |= ldb_msg_add_fmt(msg, "nodeType", "%u", rec->node);
557         ret |= ldb_msg_add_fmt(msg, "isStatic", "%u", rec->is_static);
558         ret |= ldb_msg_add_string(msg, "expireTime", 
559                                   ldb_timestring(msg, rec->expire_time));
560         ret |= ldb_msg_add_fmt(msg, "versionID", "%llu", (long long)rec->version);
561         ret |= ldb_msg_add_string(msg, "winsOwner", rec->wins_owner);
562         ret |= ldb_msg_add_empty(msg, "address", 0);
563         for (i=0;rec->addresses[i];i++) {
564                 ret |= ldb_msg_add_winsdb_addr(msg, "address", rec->addresses[i]);
565         }
566         ret |= ldb_msg_add_empty(msg, "registeredBy", 0);
567         if (rec->registered_by) {
568                 ret |= ldb_msg_add_string(msg, "registeredBy", rec->registered_by);
569                 if (ret != 0) goto failed;
570         }
571         return msg;
572
573 failed:
574         talloc_free(msg);
575         return NULL;
576 }
577
578 /*
579   save a WINS record into the database
580 */
581 uint8_t winsdb_add(struct ldb_context *wins_db, struct winsdb_record *rec, uint32_t flags)
582 {
583         struct ldb_message *msg;
584         TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
585         int trans = -1;
586         int ret = 0;
587
588         trans = ldb_transaction_start(wins_db);
589         if (trans != LDB_SUCCESS) goto failed;
590
591         if (flags & WINSDB_FLAG_ALLOC_VERSION) {
592                 rec->version = winsdb_allocate_version(wins_db);
593                 if (rec->version == 0) goto failed;
594         }
595         if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
596                 rec->wins_owner = WINSDB_OWNER_LOCAL;
597         }
598
599         msg = winsdb_message(wins_db, rec, tmp_ctx);
600         if (msg == NULL) goto failed;
601         ret = ldb_add(wins_db, msg);
602         if (ret != 0) goto failed;
603
604         trans = ldb_transaction_commit(wins_db);
605         if (trans != LDB_SUCCESS) goto failed;
606
607         talloc_free(tmp_ctx);
608         return NBT_RCODE_OK;
609
610 failed:
611         if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
612         talloc_free(tmp_ctx);
613         return NBT_RCODE_SVR;
614 }
615
616
617 /*
618   modify a WINS record in the database
619 */
620 uint8_t winsdb_modify(struct ldb_context *wins_db, struct winsdb_record *rec, uint32_t flags)
621 {
622         struct ldb_message *msg;
623         TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
624         int trans;
625         int ret;
626         int i;
627
628         trans = ldb_transaction_start(wins_db);
629         if (trans != LDB_SUCCESS) goto failed;
630
631         if (flags & WINSDB_FLAG_ALLOC_VERSION) {
632                 rec->version = winsdb_allocate_version(wins_db);
633                 if (rec->version == 0) goto failed;
634         }
635         if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
636                 rec->wins_owner = WINSDB_OWNER_LOCAL;
637         }
638
639         msg = winsdb_message(wins_db, rec, tmp_ctx);
640         if (msg == NULL) goto failed;
641
642         for (i=0;i<msg->num_elements;i++) {
643                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
644         }
645
646         ret = ldb_modify(wins_db, msg);
647         if (ret != 0) goto failed;
648
649         trans = ldb_transaction_commit(wins_db);
650         if (trans != LDB_SUCCESS) goto failed;
651
652         talloc_free(tmp_ctx);
653         return NBT_RCODE_OK;
654
655 failed:
656         if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
657         talloc_free(tmp_ctx);
658         return NBT_RCODE_SVR;
659 }
660
661
662 /*
663   delete a WINS record from the database
664 */
665 uint8_t winsdb_delete(struct ldb_context *wins_db, struct winsdb_record *rec)
666 {
667         TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
668         const struct ldb_dn *dn;
669         int trans;
670         int ret;
671
672         trans = ldb_transaction_start(wins_db);
673         if (trans != LDB_SUCCESS) goto failed;
674
675         dn = winsdb_dn(tmp_ctx, rec->name);
676         if (dn == NULL) goto failed;
677
678         ret = ldb_delete(wins_db, dn);
679         if (ret != 0) goto failed;
680
681         trans = ldb_transaction_commit(wins_db);
682         if (trans != LDB_SUCCESS) goto failed;
683
684         talloc_free(tmp_ctx);
685         return NBT_RCODE_OK;
686
687 failed:
688         if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
689         talloc_free(tmp_ctx);
690         return NBT_RCODE_SVR;
691 }
692
693 struct ldb_context *winsdb_connect(TALLOC_CTX *mem_ctx)
694 {
695         return ldb_wrap_connect(mem_ctx, lock_path(mem_ctx, lp_wins_url()),
696                                 system_session(mem_ctx), NULL, 0, NULL);
697 }