r12618: use our primary interface address or the "winsdb:local_owner" -address
[bbaumbach/samba-autobuild/.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 "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_handle *h, 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         const char *address;
174         const char *wins_owner;
175         const 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         if (strcmp(wins_owner, "0.0.0.0") == 0) {
220                 wins_owner = h->local_owner;
221         }
222         addr->wins_owner = talloc_strdup(addr, wins_owner);
223         if (!addr->wins_owner) {
224                 status = NT_STATUS_NO_MEMORY;
225                 goto failed;
226         }
227
228         if (strncmp("expireTime:", p, 11) != 0) {
229                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
230                 goto failed;
231         }
232
233         expire_time = p + 11;
234         p = strchr(expire_time, ';');
235         if (!p) {
236                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
237                 goto failed;
238         }
239
240         *p = '\0';p++;
241         addr->expire_time = ldb_string_to_time(expire_time);
242
243         *_addr = addr;
244         return NT_STATUS_OK;
245 failed:
246         talloc_free(addr);
247         return status;
248 }
249
250 /*
251  encode the winsdb_addr("address") attribute like this:
252  "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
253 */
254 static int ldb_msg_add_winsdb_addr(struct ldb_message *msg, 
255                                    const char *attr_name, struct winsdb_addr *addr)
256 {
257         struct ldb_val val;
258         const char *str;
259
260         str = talloc_asprintf(msg, "%s;winsOwner:%s;expireTime:%s;",
261                               addr->address, addr->wins_owner,
262                               ldb_timestring(msg, addr->expire_time));
263         if (!str) return -1;
264
265         val.data = discard_const_p(uint8_t, str);
266         val.length = strlen(str);
267
268         return ldb_msg_add_value(msg, attr_name, &val);
269 }
270
271 struct winsdb_addr **winsdb_addr_list_make(TALLOC_CTX *mem_ctx)
272 {
273         struct winsdb_addr **addresses;
274
275         addresses = talloc_array(mem_ctx, struct winsdb_addr *, 1);
276         if (!addresses) return NULL;
277
278         addresses[0] = NULL;
279
280         return addresses;
281 }
282
283 struct winsdb_addr **winsdb_addr_list_add(struct winsdb_addr **addresses, const char *address,
284                                           const char *wins_owner, time_t expire_time)
285 {
286         size_t len = winsdb_addr_list_length(addresses);
287
288         addresses = talloc_realloc(addresses, addresses, struct winsdb_addr *, len + 2);
289         if (!addresses) return NULL;
290
291         addresses[len] = talloc(addresses, struct winsdb_addr);
292         if (!addresses[len]) {
293                 talloc_free(addresses);
294                 return NULL;
295         }
296
297         addresses[len]->address = talloc_strdup(addresses[len], address);
298         if (!addresses[len]->address) {
299                 talloc_free(addresses);
300                 return NULL;
301         }
302
303         addresses[len]->wins_owner = talloc_strdup(addresses[len], wins_owner);
304         if (!addresses[len]->wins_owner) {
305                 talloc_free(addresses);
306                 return NULL;
307         }
308
309         addresses[len]->expire_time = expire_time;
310
311         addresses[len+1] = NULL;
312
313         return addresses;
314 }
315
316 void winsdb_addr_list_remove(struct winsdb_addr **addresses, const char *address)
317 {
318         size_t i;
319
320         for (i=0; addresses[i]; i++) {
321                 if (strcmp(addresses[i]->address, address) == 0) {
322                         break;
323                 }
324         }
325         if (!addresses[i]) return;
326
327         for (; addresses[i]; i++) {
328                 addresses[i] = addresses[i+1];
329         }
330
331         return;
332 }
333
334 struct winsdb_addr *winsdb_addr_list_check(struct winsdb_addr **addresses, const char *address)
335 {
336         size_t i;
337
338         for (i=0; addresses[i]; i++) {
339                 if (strcmp(addresses[i]->address, address) == 0) {
340                         return addresses[i];
341                 }
342         }
343
344         return NULL;
345 }
346
347 size_t winsdb_addr_list_length(struct winsdb_addr **addresses)
348 {
349         size_t i;
350         for (i=0; addresses[i]; i++);
351         return i;
352 }
353
354 const char **winsdb_addr_string_list(TALLOC_CTX *mem_ctx, struct winsdb_addr **addresses)
355 {
356         size_t len = winsdb_addr_list_length(addresses);
357         const char **str_list=NULL;
358         size_t i;
359
360         for (i=0; i < len; i++) {
361                 str_list = str_list_add(str_list, addresses[i]->address);
362                 if (!str_list[i]) {
363                         return NULL;
364                 }
365         }
366         talloc_steal(mem_ctx, str_list);
367         return str_list;
368 }
369
370 /*
371   load a WINS entry from the database
372 */
373 NTSTATUS winsdb_lookup(struct winsdb_handle *h, 
374                        struct nbt_name *name,
375                        TALLOC_CTX *mem_ctx,
376                        struct winsdb_record **_rec)
377 {
378         NTSTATUS status;
379         struct ldb_result *res = NULL;
380         int ret;
381         struct winsdb_record *rec;
382         struct ldb_context *wins_db = h->ldb;
383         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
384
385         /* find the record in the WINS database */
386         ret = ldb_search(wins_db, winsdb_dn(tmp_ctx, name), LDB_SCOPE_BASE, 
387                          NULL, NULL, &res);
388
389         if (ret != LDB_SUCCESS || res->count > 1) {
390                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
391                 goto failed;
392         } else if (res->count== 0) {
393                 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
394                 goto failed;
395         }
396
397         talloc_steal(tmp_ctx, res);
398
399         status = winsdb_record(h, res->msgs[0], tmp_ctx, &rec);
400         if (!NT_STATUS_IS_OK(status)) goto failed;
401
402         /* see if it has already expired */
403         if (rec->state == WREPL_STATE_ACTIVE &&
404             rec->expire_time <= time(NULL)) {
405                 DEBUG(5,("WINS: expiring name %s (expired at %s)\n", 
406                          nbt_name_string(tmp_ctx, rec->name), timestring(tmp_ctx, rec->expire_time)));
407                 rec->state = WREPL_STATE_RELEASED;
408         }
409
410         talloc_steal(mem_ctx, rec);
411         talloc_free(tmp_ctx);
412         *_rec = rec;
413         return NT_STATUS_OK;
414
415 failed:
416         talloc_free(tmp_ctx);
417         return status;
418 }
419
420 NTSTATUS winsdb_record(struct winsdb_handle *h, struct ldb_message *msg, TALLOC_CTX *mem_ctx, struct winsdb_record **_rec)
421 {
422         NTSTATUS status;
423         struct winsdb_record *rec;
424         struct ldb_message_element *el;
425         struct nbt_name *name;
426         uint32_t i, num_values;
427
428         rec = talloc(mem_ctx, struct winsdb_record);
429         if (rec == NULL) {
430                 status = NT_STATUS_NO_MEMORY;
431                 goto failed;
432         }
433
434         status = winsdb_nbt_name(rec, msg->dn, &name);
435         if (!NT_STATUS_IS_OK(status)) goto failed;
436
437         if (strlen(name->name) > 15) {
438                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
439                 goto failed;
440         }
441         if (name->scope && strlen(name->scope) > 238) {
442                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
443                 goto failed;
444         }
445
446         /* parse it into a more convenient winsdb_record structure */
447         rec->name               = name;
448         rec->type               = ldb_msg_find_int(msg, "recordType", WREPL_TYPE_UNIQUE);
449         rec->state              = ldb_msg_find_int(msg, "recordState", WREPL_STATE_RELEASED);
450         rec->node               = ldb_msg_find_int(msg, "nodeType", WREPL_NODE_B);
451         rec->is_static          = ldb_msg_find_int(msg, "isStatic", 0);
452         rec->expire_time        = ldb_string_to_time(ldb_msg_find_string(msg, "expireTime", NULL));
453         rec->version            = ldb_msg_find_uint64(msg, "versionID", 0);
454         rec->wins_owner         = ldb_msg_find_string(msg, "winsOwner", NULL);
455         rec->registered_by      = ldb_msg_find_string(msg, "registeredBy", NULL);
456         talloc_steal(rec, rec->wins_owner);
457         talloc_steal(rec, rec->registered_by);
458
459         if (!rec->wins_owner || strcmp(rec->wins_owner, "0.0.0.0") == 0) {
460                 rec->wins_owner = h->local_owner;
461         }
462
463         el = ldb_msg_find_element(msg, "address");
464         if (el) {
465                 num_values = el->num_values;
466         } else {
467                 num_values = 0;
468         }
469
470         if (rec->type == WREPL_TYPE_UNIQUE || rec->type == WREPL_TYPE_GROUP) {
471                 if (num_values != 1) {
472                         status = NT_STATUS_INTERNAL_DB_CORRUPTION;
473                         goto failed;
474                 }
475         }
476         if (rec->state == WREPL_STATE_ACTIVE) {
477                 if (num_values < 1) {
478                         status = NT_STATUS_INTERNAL_DB_CORRUPTION;
479                         goto failed;
480                 }
481         }
482
483         rec->addresses     = talloc_array(rec, struct winsdb_addr *, num_values+1);
484         if (rec->addresses == NULL) {
485                 status = NT_STATUS_NO_MEMORY;
486                 goto failed;
487         }
488
489         for (i=0;i<num_values;i++) {
490                 status = winsdb_addr_decode(h, rec, &el->values[i], rec->addresses, &rec->addresses[i]);
491                 if (!NT_STATUS_IS_OK(status)) goto failed;
492         }
493         rec->addresses[i] = NULL;
494
495         if (rec->is_static) {
496                 if (num_values < 1) {
497                         status = NT_STATUS_INTERNAL_DB_CORRUPTION;
498                         goto failed;
499                 }
500                 rec->state = WREPL_STATE_ACTIVE;
501                 rec->expire_time = get_time_t_max();
502                 for (i=0;rec->addresses[i];i++) {
503                         rec->addresses[i]->expire_time = rec->expire_time;
504                 }
505         }
506
507         *_rec = rec;
508         return NT_STATUS_OK;
509 failed:
510         if (NT_STATUS_EQUAL(NT_STATUS_INTERNAL_DB_CORRUPTION, status)) {
511                 DEBUG(1,("winsdb_record: corrupted record: %s\n", ldb_dn_linearize(rec, msg->dn)));
512         }
513         talloc_free(rec);
514         return status;
515 }
516
517 /*
518   form a ldb_message from a winsdb_record
519 */
520 struct ldb_message *winsdb_message(struct ldb_context *ldb, 
521                                    struct winsdb_record *rec, TALLOC_CTX *mem_ctx)
522 {
523         int i, ret=0;
524         size_t addr_count;
525         struct ldb_message *msg = ldb_msg_new(mem_ctx);
526         if (msg == NULL) goto failed;
527
528         if (rec->is_static) {
529                 rec->state = WREPL_STATE_ACTIVE;
530                 rec->expire_time = get_time_t_max();
531                 for (i=0;rec->addresses[i];i++) {
532                         rec->addresses[i]->expire_time = rec->expire_time;
533                 }
534         }
535
536         /* make sure we don't put in corrupted records */
537         addr_count = winsdb_addr_list_length(rec->addresses);
538         if (rec->state == WREPL_STATE_ACTIVE && addr_count == 0) {
539                 rec->state = WREPL_STATE_RELEASED;
540         }
541         if (rec->type == WREPL_TYPE_UNIQUE && addr_count > 1) {
542                 rec->type = WREPL_TYPE_MHOMED;
543         }
544
545         msg->dn = winsdb_dn(msg, rec->name);
546         if (msg->dn == NULL) goto failed;
547         ret |= ldb_msg_add_fmt(msg, "type", "0x%02X", rec->name->type);
548         if (rec->name->name && *rec->name->name) {
549                 ret |= ldb_msg_add_string(msg, "name", rec->name->name);
550         }
551         if (rec->name->scope && *rec->name->scope) {
552                 ret |= ldb_msg_add_string(msg, "scope", rec->name->scope);
553         }
554         ret |= ldb_msg_add_fmt(msg, "objectClass", "winsRecord");
555         ret |= ldb_msg_add_fmt(msg, "recordType", "%u", rec->type);
556         ret |= ldb_msg_add_fmt(msg, "recordState", "%u", rec->state);
557         ret |= ldb_msg_add_fmt(msg, "nodeType", "%u", rec->node);
558         ret |= ldb_msg_add_fmt(msg, "isStatic", "%u", rec->is_static);
559         ret |= ldb_msg_add_string(msg, "expireTime", 
560                                   ldb_timestring(msg, rec->expire_time));
561         ret |= ldb_msg_add_fmt(msg, "versionID", "%llu", (long long)rec->version);
562         ret |= ldb_msg_add_string(msg, "winsOwner", rec->wins_owner);
563         ret |= ldb_msg_add_empty(msg, "address", 0);
564         for (i=0;rec->addresses[i];i++) {
565                 ret |= ldb_msg_add_winsdb_addr(msg, "address", rec->addresses[i]);
566         }
567         ret |= ldb_msg_add_empty(msg, "registeredBy", 0);
568         if (rec->registered_by) {
569                 ret |= ldb_msg_add_string(msg, "registeredBy", rec->registered_by);
570                 if (ret != 0) goto failed;
571         }
572         return msg;
573
574 failed:
575         talloc_free(msg);
576         return NULL;
577 }
578
579 /*
580   save a WINS record into the database
581 */
582 uint8_t winsdb_add(struct winsdb_handle *h, struct winsdb_record *rec, uint32_t flags)
583 {
584         struct ldb_message *msg;
585         struct ldb_context *wins_db = h->ldb;
586         TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
587         int trans = -1;
588         int ret = 0;
589
590         trans = ldb_transaction_start(wins_db);
591         if (trans != LDB_SUCCESS) goto failed;
592
593         if (flags & WINSDB_FLAG_ALLOC_VERSION) {
594                 rec->version = winsdb_allocate_version(h);
595                 if (rec->version == 0) goto failed;
596         }
597         if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
598                 rec->wins_owner = h->local_owner;
599         }
600
601         msg = winsdb_message(wins_db, rec, tmp_ctx);
602         if (msg == NULL) goto failed;
603         ret = ldb_add(wins_db, msg);
604         if (ret != 0) goto failed;
605
606         trans = ldb_transaction_commit(wins_db);
607         if (trans != LDB_SUCCESS) goto failed;
608
609         talloc_free(tmp_ctx);
610         return NBT_RCODE_OK;
611
612 failed:
613         if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
614         talloc_free(tmp_ctx);
615         return NBT_RCODE_SVR;
616 }
617
618
619 /*
620   modify a WINS record in the database
621 */
622 uint8_t winsdb_modify(struct winsdb_handle *h, struct winsdb_record *rec, uint32_t flags)
623 {
624         struct ldb_message *msg;
625         struct ldb_context *wins_db = h->ldb;
626         TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
627         int trans;
628         int ret;
629         int i;
630
631         trans = ldb_transaction_start(wins_db);
632         if (trans != LDB_SUCCESS) goto failed;
633
634         if (flags & WINSDB_FLAG_ALLOC_VERSION) {
635                 rec->version = winsdb_allocate_version(h);
636                 if (rec->version == 0) goto failed;
637         }
638         if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
639                 rec->wins_owner = h->local_owner;
640         }
641
642         msg = winsdb_message(wins_db, rec, tmp_ctx);
643         if (msg == NULL) goto failed;
644
645         for (i=0;i<msg->num_elements;i++) {
646                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
647         }
648
649         ret = ldb_modify(wins_db, msg);
650         if (ret != 0) goto failed;
651
652         trans = ldb_transaction_commit(wins_db);
653         if (trans != LDB_SUCCESS) goto failed;
654
655         talloc_free(tmp_ctx);
656         return NBT_RCODE_OK;
657
658 failed:
659         if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
660         talloc_free(tmp_ctx);
661         return NBT_RCODE_SVR;
662 }
663
664
665 /*
666   delete a WINS record from the database
667 */
668 uint8_t winsdb_delete(struct winsdb_handle *h, struct winsdb_record *rec)
669 {
670         struct ldb_context *wins_db = h->ldb;
671         TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
672         const struct ldb_dn *dn;
673         int trans;
674         int ret;
675
676         trans = ldb_transaction_start(wins_db);
677         if (trans != LDB_SUCCESS) goto failed;
678
679         dn = winsdb_dn(tmp_ctx, rec->name);
680         if (dn == NULL) goto failed;
681
682         ret = ldb_delete(wins_db, dn);
683         if (ret != 0) goto failed;
684
685         trans = ldb_transaction_commit(wins_db);
686         if (trans != LDB_SUCCESS) goto failed;
687
688         talloc_free(tmp_ctx);
689         return NBT_RCODE_OK;
690
691 failed:
692         if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
693         talloc_free(tmp_ctx);
694         return NBT_RCODE_SVR;
695 }
696
697 struct winsdb_handle *winsdb_connect(TALLOC_CTX *mem_ctx)
698 {
699         struct winsdb_handle *h = NULL;
700         const char *owner;
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         owner = lp_parm_string(-1, "winsdb", "local_owner");
710         if (!owner) {
711                 owner = iface_n_ip(0);
712         }
713
714         h->local_owner = talloc_strdup(h, owner);
715         if (!h->local_owner) goto failed;
716
717         return h;
718 failed:
719         talloc_free(h);
720         return NULL;
721 }