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