346ccc949bb6bdb34f9c36cbcf14183461ee8203
[mat/samba.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 3 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, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "includes.h"
24 #include "nbt_server/nbt_server.h"
25 #include "nbt_server/wins/winsdb.h"
26 #include <ldb.h>
27 #include <ldb_errors.h>
28 #include "librpc/gen_ndr/ndr_nbt.h"
29 #include "system/time.h"
30 #include "ldb_wrap.h"
31 #include "system/network.h"
32 #include "lib/socket/netif.h"
33 #include "param/param.h"
34
35 uint64_t winsdb_get_maxVersion(struct winsdb_handle *h)
36 {
37         int ret;
38         struct ldb_context *ldb = h->ldb;
39         struct ldb_dn *dn;
40         struct ldb_result *res = NULL;
41         TALLOC_CTX *tmp_ctx = talloc_new(ldb);
42         uint64_t maxVersion = 0;
43
44         dn = ldb_dn_new(tmp_ctx, ldb, "CN=VERSION");
45         if (!dn) goto failed;
46
47         /* find the record in the WINS database */
48         ret = ldb_search(ldb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL);
49         if (ret != LDB_SUCCESS) goto failed;
50         if (res->count > 1) goto failed;
51
52         if (res->count == 1) {
53                 maxVersion = ldb_msg_find_attr_as_uint64(res->msgs[0], "maxVersion", 0);
54         }
55
56 failed:
57         talloc_free(tmp_ctx);
58         return maxVersion;
59 }
60
61 /*
62  if newVersion == 0 return the old maxVersion + 1 and save it
63  if newVersion > 0 return MAX(oldMaxVersion, newMaxVersion) and save it
64 */
65 uint64_t winsdb_set_maxVersion(struct winsdb_handle *h, uint64_t newMaxVersion)
66 {
67         int trans;
68         int ret;
69         struct ldb_dn *dn;
70         struct ldb_result *res = NULL;
71         struct ldb_message *msg = NULL;
72         struct ldb_context *wins_db = h->ldb;
73         TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
74         uint64_t oldMaxVersion = 0;
75
76         trans = ldb_transaction_start(wins_db);
77         if (trans != LDB_SUCCESS) goto failed;
78
79         dn = ldb_dn_new(tmp_ctx, wins_db, "CN=VERSION");
80         if (!dn) goto failed;
81
82         /* find the record in the WINS database */
83         ret = ldb_search(wins_db, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL);
84         if (ret != LDB_SUCCESS) goto failed;
85         if (res->count > 1) goto failed;
86
87         if (res->count == 1) {
88                 oldMaxVersion = ldb_msg_find_attr_as_uint64(res->msgs[0], "maxVersion", 0);
89         }
90
91         if (newMaxVersion == 0) {
92                 newMaxVersion = oldMaxVersion + 1;
93         } else {
94                 newMaxVersion = MAX(oldMaxVersion, newMaxVersion);
95         }
96
97         msg = ldb_msg_new(tmp_ctx);
98         if (!msg) goto failed;
99         msg->dn = dn;
100
101
102         ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE, NULL);
103         if (ret != LDB_SUCCESS) goto failed;
104         ret = ldb_msg_add_string(msg, "objectClass", "winsMaxVersion");
105         if (ret != LDB_SUCCESS) goto failed;
106         ret = ldb_msg_add_empty(msg, "maxVersion", LDB_FLAG_MOD_REPLACE, NULL);
107         if (ret != LDB_SUCCESS) goto failed;
108         ret = ldb_msg_add_fmt(msg, "maxVersion", "%llu", (long long)newMaxVersion);
109         if (ret != LDB_SUCCESS) goto failed;
110
111         ret = ldb_modify(wins_db, msg);
112         if (ret != LDB_SUCCESS) ret = ldb_add(wins_db, msg);
113         if (ret != LDB_SUCCESS) goto failed;
114
115         trans = ldb_transaction_commit(wins_db);
116         if (trans != LDB_SUCCESS) goto failed;
117
118         talloc_free(tmp_ctx);
119         return newMaxVersion;
120
121 failed:
122         if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
123         talloc_free(tmp_ctx);
124         return 0;
125 }
126
127 /*
128   return a DN for a nbt_name
129 */
130 static struct ldb_dn *winsdb_dn(TALLOC_CTX *mem_ctx, struct ldb_context *ldb,
131                                 const struct nbt_name *name)
132 {
133         struct ldb_dn *dn;
134
135         dn = ldb_dn_new_fmt(mem_ctx, ldb, "type=0x%02X", name->type);
136         if (ldb_dn_is_valid(dn) && name->name && *name->name) {
137                 ldb_dn_add_child_fmt(dn, "name=%s", name->name);
138         }
139         if (ldb_dn_is_valid(dn) && name->scope && *name->scope) {
140                 ldb_dn_add_child_fmt(dn, "scope=%s", name->scope);
141         }
142         return dn;
143 }
144
145 static NTSTATUS winsdb_nbt_name(TALLOC_CTX *mem_ctx, struct ldb_dn *dn, struct nbt_name **_name)
146 {
147         NTSTATUS status;
148         struct nbt_name *name;
149         unsigned int comp_num;
150         uint32_t cur = 0;
151
152         name = talloc(mem_ctx, struct nbt_name);
153         if (!name) {
154                 status = NT_STATUS_NO_MEMORY;
155                 goto failed;
156         }
157
158         comp_num = ldb_dn_get_comp_num(dn);
159
160         if (comp_num > 3) {
161                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
162                 goto failed;
163         }
164
165         if (comp_num > cur && strcasecmp("scope", ldb_dn_get_component_name(dn, cur)) == 0) {
166                 name->scope     = (const char *)talloc_strdup(name, (char *)ldb_dn_get_component_val(dn, cur)->data);
167                 cur++;
168         } else {
169                 name->scope     = NULL;
170         }
171
172         if (comp_num > cur && strcasecmp("name", ldb_dn_get_component_name(dn, cur)) == 0) {
173                 name->name      = (const char *)talloc_strdup(name, (char *)ldb_dn_get_component_val(dn, cur)->data);
174                 cur++;
175         } else {
176                 name->name      = talloc_strdup(name, "");
177                 if (!name->name) {
178                         status = NT_STATUS_NO_MEMORY;
179                         goto failed;
180                 }
181         }
182
183         if (comp_num > cur && strcasecmp("type", ldb_dn_get_component_name(dn, cur)) == 0) {
184                 name->type      = strtoul((char *)ldb_dn_get_component_val(dn, cur)->data, NULL, 0);
185                 cur++;
186         } else {
187                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
188                 goto failed;
189         }
190
191         *_name = name;
192         return NT_STATUS_OK;
193 failed:
194         talloc_free(name);
195         return status;
196 }
197
198 /*
199  decode the winsdb_addr("address") attribute:
200  "172.31.1.1" or 
201  "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
202  are valid records
203 */
204 static NTSTATUS winsdb_addr_decode(struct winsdb_handle *h, struct winsdb_record *rec, struct ldb_val *val,
205                                    TALLOC_CTX *mem_ctx, struct winsdb_addr **_addr)
206 {
207         NTSTATUS status;
208         struct winsdb_addr *addr;
209         const char *address;
210         const char *wins_owner;
211         const char *expire_time;
212         char *p;
213
214         addr = talloc(mem_ctx, struct winsdb_addr);
215         if (!addr) {
216                 status = NT_STATUS_NO_MEMORY;
217                 goto failed;
218         }
219
220         address = (char *)val->data;
221
222         p = strchr(address, ';');
223         if (!p) {
224                 /* support old entries, with only the address */
225                 addr->address           = (const char *)talloc_steal(addr, val->data);
226                 addr->wins_owner        = talloc_reference(addr, rec->wins_owner);
227                 if (!addr->wins_owner) {
228                         status = NT_STATUS_NO_MEMORY;
229                         goto failed;
230                 }
231                 addr->expire_time       = rec->expire_time;
232                 *_addr = addr;
233                 return NT_STATUS_OK;
234         }
235
236         *p = '\0'; p++;
237         addr->address = talloc_strdup(addr, address);
238         if (!addr->address) {
239                 status = NT_STATUS_NO_MEMORY;
240                 goto failed;
241         }
242
243         if (strncmp("winsOwner:", p, 10) != 0) {
244                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
245                 goto failed;
246         }
247         wins_owner = p + 10;
248         p = strchr(wins_owner, ';');
249         if (!p) {
250                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
251                 goto failed;
252         }
253
254         *p = '\0';p++;
255         if (strcmp(wins_owner, "0.0.0.0") == 0) {
256                 wins_owner = h->local_owner;
257         }
258         addr->wins_owner = talloc_strdup(addr, wins_owner);
259         if (!addr->wins_owner) {
260                 status = NT_STATUS_NO_MEMORY;
261                 goto failed;
262         }
263
264         if (strncmp("expireTime:", p, 11) != 0) {
265                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
266                 goto failed;
267         }
268
269         expire_time = p + 11;
270         p = strchr(expire_time, ';');
271         if (!p) {
272                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
273                 goto failed;
274         }
275
276         *p = '\0';p++;
277         addr->expire_time = ldb_string_to_time(expire_time);
278
279         *_addr = addr;
280         return NT_STATUS_OK;
281 failed:
282         talloc_free(addr);
283         return status;
284 }
285
286 /*
287  encode the winsdb_addr("address") attribute like this:
288  non-static record:
289  "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
290  static record:
291  "172.31.1.1"
292 */
293 static int ldb_msg_add_winsdb_addr(struct ldb_message *msg, struct winsdb_record *rec,
294                                    const char *attr_name, struct winsdb_addr *addr)
295 {
296         const char *str;
297
298         if (rec->is_static) {
299                 str = talloc_strdup(msg, addr->address);
300                 if (!str) return LDB_ERR_OPERATIONS_ERROR;
301         } else {
302                 char *expire_time;
303                 expire_time = ldb_timestring(msg, addr->expire_time);
304                 if (!expire_time) return LDB_ERR_OPERATIONS_ERROR;
305                 str = talloc_asprintf(msg, "%s;winsOwner:%s;expireTime:%s;",
306                                       addr->address, addr->wins_owner,
307                                       expire_time);
308                 talloc_free(expire_time);
309                 if (!str) return LDB_ERR_OPERATIONS_ERROR;
310         }
311
312         return ldb_msg_add_string(msg, attr_name, str);
313 }
314
315 struct winsdb_addr **winsdb_addr_list_make(TALLOC_CTX *mem_ctx)
316 {
317         struct winsdb_addr **addresses;
318
319         addresses = talloc_array(mem_ctx, struct winsdb_addr *, 1);
320         if (!addresses) return NULL;
321
322         addresses[0] = NULL;
323
324         return addresses;
325 }
326
327 static int winsdb_addr_sort_list (struct winsdb_addr **p1, struct winsdb_addr **p2, void *opaque)
328 {
329         struct winsdb_addr *a1 = talloc_get_type(*p1, struct winsdb_addr);
330         struct winsdb_addr *a2 = talloc_get_type(*p2, struct winsdb_addr);
331         struct winsdb_handle *h= talloc_get_type(opaque, struct winsdb_handle);
332         bool a1_owned = false;
333         bool a2_owned = false;
334
335         /*
336          * first the owned addresses with the newest to the oldest address
337          * then the replica addresses with the newest to the oldest address
338          */
339         if (a2->expire_time != a1->expire_time) {
340                 return a2->expire_time - a1->expire_time;
341         }
342
343         if (strcmp(a2->wins_owner, h->local_owner) == 0) {
344                 a2_owned = true;
345         }
346
347         if (strcmp(a1->wins_owner, h->local_owner) == 0) {
348                 a1_owned = true;
349         }
350
351         return a2_owned - a1_owned;
352 }
353
354 struct winsdb_addr **winsdb_addr_list_add(struct winsdb_handle *h, const struct winsdb_record *rec,
355                                           struct winsdb_addr **addresses, const char *address,
356                                           const char *wins_owner, time_t expire_time,
357                                           bool is_name_registration)
358 {
359         struct winsdb_addr *old_addr = NULL;
360         size_t len = 0;
361         size_t i;
362         bool found_old_replica = false;
363
364         /*
365          * count the addresses and maybe
366          * find an old entry for the new address
367          */
368         for (i=0; addresses[i]; i++) {
369                 if (old_addr) continue;
370                 if (strcmp(addresses[i]->address, address) == 0) {
371                         old_addr = addresses[i];
372                 }
373         }
374         len = i;
375
376         /*
377          * the address is already there
378          * and we can replace it
379          */
380         if (old_addr) {
381                 goto remove_old_addr;
382         }
383
384         /*
385          * if we don't have 25 addresses already,
386          * we can just add the new address
387          */
388         if (len < 25) {
389                 goto add_new_addr;
390         }
391
392         /*
393          * if we haven't found the address,
394          * and we have already have 25 addresses
395          * if so then we need to do the following:
396          * - if it isn't a name registration, then just ignore the new address
397          * - if it is a name registration, then first search for 
398          *   the oldest replica and if there's no replica address
399          *   search the oldest owned address
400          */
401         if (!is_name_registration) {
402                 return addresses;
403         }
404
405         /*
406          * find the oldest replica address, if there's no replica
407          * record at all, find the oldest owned address
408          */
409         for (i=0; addresses[i]; i++) {
410                 bool cur_is_replica = false;
411                 /* find out if the current address is a replica */
412                 if (strcmp(addresses[i]->wins_owner, h->local_owner) != 0) {
413                         cur_is_replica = true;
414                 }
415
416                 /*
417                  * if we already found a replica address and the current address
418                  * is not a replica, then skip it
419                  */
420                 if (found_old_replica && !cur_is_replica) continue;
421
422                 /*
423                  * if we found the first replica address, reset the address
424                  * that would be replaced
425                  */
426                 if (!found_old_replica && cur_is_replica) {
427                         found_old_replica = true;
428                         old_addr = addresses[i];
429                         continue;
430                 }
431
432                 /*
433                  * if the first address isn't a replica, just start with 
434                  * the first one
435                  */
436                 if (!old_addr) {
437                         old_addr = addresses[i];
438                         continue;
439                 }
440
441                 /*
442                  * see if we find an older address
443                  */
444                 if (addresses[i]->expire_time < old_addr->expire_time) {
445                         old_addr = addresses[i];
446                         continue;
447                 }
448         }
449
450 remove_old_addr:
451         winsdb_addr_list_remove(addresses, old_addr->address);
452         len --;
453
454 add_new_addr:
455         addresses = talloc_realloc(addresses, addresses, struct winsdb_addr *, len + 2);
456         if (!addresses) return NULL;
457
458         addresses[len] = talloc(addresses, struct winsdb_addr);
459         if (!addresses[len]) {
460                 talloc_free(addresses);
461                 return NULL;
462         }
463
464         addresses[len]->address = talloc_strdup(addresses[len], address);
465         if (!addresses[len]->address) {
466                 talloc_free(addresses);
467                 return NULL;
468         }
469
470         addresses[len]->wins_owner = talloc_strdup(addresses[len], wins_owner);
471         if (!addresses[len]->wins_owner) {
472                 talloc_free(addresses);
473                 return NULL;
474         }
475
476         addresses[len]->expire_time = expire_time;
477
478         addresses[len+1] = NULL;
479
480         LDB_TYPESAFE_QSORT(addresses, len+1, h, winsdb_addr_sort_list);
481
482         return addresses;
483 }
484
485 void winsdb_addr_list_remove(struct winsdb_addr **addresses, const char *address)
486 {
487         size_t i;
488
489         for (i=0; addresses[i]; i++) {
490                 if (strcmp(addresses[i]->address, address) == 0) {
491                         break;
492                 }
493         }
494
495         for (; addresses[i]; i++) {
496                 addresses[i] = addresses[i+1];
497         }
498
499         return;
500 }
501
502 struct winsdb_addr *winsdb_addr_list_check(struct winsdb_addr **addresses, const char *address)
503 {
504         size_t i;
505
506         for (i=0; addresses[i]; i++) {
507                 if (strcmp(addresses[i]->address, address) == 0) {
508                         return addresses[i];
509                 }
510         }
511
512         return NULL;
513 }
514
515 size_t winsdb_addr_list_length(struct winsdb_addr **addresses)
516 {
517         size_t i;
518         for (i=0; addresses[i]; i++);
519         return i;
520 }
521
522 const char **winsdb_addr_string_list(TALLOC_CTX *mem_ctx, struct winsdb_addr **addresses)
523 {
524         size_t len = winsdb_addr_list_length(addresses);
525         const char **str_list=NULL;
526         size_t i;
527
528         for (i=0; i < len; i++) {
529                 str_list = str_list_add(str_list, addresses[i]->address);
530                 if (!str_list[i]) {
531                         return NULL;
532                 }
533         }
534         talloc_steal(mem_ctx, str_list);
535         return str_list;
536 }
537
538 /*
539   load a WINS entry from the database
540 */
541 NTSTATUS winsdb_lookup(struct winsdb_handle *h, 
542                        const struct nbt_name *name,
543                        TALLOC_CTX *mem_ctx,
544                        struct winsdb_record **_rec)
545 {
546         NTSTATUS status;
547         struct ldb_result *res = NULL;
548         int ret;
549         struct winsdb_record *rec;
550         struct ldb_context *wins_db = h->ldb;
551         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
552         time_t now = time(NULL);
553
554         /* find the record in the WINS database */
555         ret = ldb_search(wins_db, tmp_ctx, &res,
556                          winsdb_dn(tmp_ctx, wins_db, name),
557                          LDB_SCOPE_BASE, NULL, NULL);
558
559         if (ret != LDB_SUCCESS || res->count > 1) {
560                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
561                 goto failed;
562         } else if (res->count== 0) {
563                 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
564                 goto failed;
565         }
566
567         status = winsdb_record(h, res->msgs[0], tmp_ctx, now, &rec);
568         if (!NT_STATUS_IS_OK(status)) goto failed;
569
570         talloc_steal(mem_ctx, rec);
571         talloc_free(tmp_ctx);
572         *_rec = rec;
573         return NT_STATUS_OK;
574
575 failed:
576         talloc_free(tmp_ctx);
577         return status;
578 }
579
580 NTSTATUS winsdb_record(struct winsdb_handle *h, struct ldb_message *msg, TALLOC_CTX *mem_ctx, time_t now, struct winsdb_record **_rec)
581 {
582         NTSTATUS status;
583         struct winsdb_record *rec;
584         struct ldb_message_element *el;
585         struct nbt_name *name;
586         uint32_t i, j, num_values;
587
588         rec = talloc(mem_ctx, struct winsdb_record);
589         if (rec == NULL) {
590                 status = NT_STATUS_NO_MEMORY;
591                 goto failed;
592         }
593
594         status = winsdb_nbt_name(rec, msg->dn, &name);
595         if (!NT_STATUS_IS_OK(status)) goto failed;
596
597         if (strlen(name->name) > 15) {
598                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
599                 goto failed;
600         }
601         if (name->scope && strlen(name->scope) > 238) {
602                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
603                 goto failed;
604         }
605
606         /* parse it into a more convenient winsdb_record structure */
607         rec->name               = name;
608         rec->type               = ldb_msg_find_attr_as_int(msg, "recordType", WREPL_TYPE_UNIQUE);
609         rec->state              = ldb_msg_find_attr_as_int(msg, "recordState", WREPL_STATE_RELEASED);
610         rec->node               = ldb_msg_find_attr_as_int(msg, "nodeType", WREPL_NODE_B);
611         rec->is_static          = ldb_msg_find_attr_as_int(msg, "isStatic", 0);
612         rec->expire_time        = ldb_string_to_time(ldb_msg_find_attr_as_string(msg, "expireTime", NULL));
613         rec->version            = ldb_msg_find_attr_as_uint64(msg, "versionID", 0);
614         rec->wins_owner         = ldb_msg_find_attr_as_string(msg, "winsOwner", NULL);
615         rec->registered_by      = ldb_msg_find_attr_as_string(msg, "registeredBy", NULL);
616         talloc_steal(rec, rec->wins_owner);
617         talloc_steal(rec, rec->registered_by);
618
619         if (!rec->wins_owner || strcmp(rec->wins_owner, "0.0.0.0") == 0) {
620                 rec->wins_owner = h->local_owner;
621         }
622
623         el = ldb_msg_find_element(msg, "address");
624         if (el) {
625                 num_values = el->num_values;
626         } else {
627                 num_values = 0;
628         }
629
630         if (rec->type == WREPL_TYPE_UNIQUE || rec->type == WREPL_TYPE_GROUP) {
631                 if (num_values != 1) {
632                         status = NT_STATUS_INTERNAL_DB_CORRUPTION;
633                         goto failed;
634                 }
635         }
636         if (rec->state == WREPL_STATE_ACTIVE) {
637                 if (num_values < 1) {
638                         status = NT_STATUS_INTERNAL_DB_CORRUPTION;
639                         goto failed;
640                 }
641         }
642         if (num_values > 25) {
643                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
644                 goto failed;
645         }
646
647         rec->addresses     = talloc_array(rec, struct winsdb_addr *, num_values+1);
648         if (rec->addresses == NULL) {
649                 status = NT_STATUS_NO_MEMORY;
650                 goto failed;
651         }
652
653         for (i=0,j=0;i<num_values;i++) {
654                 bool we_are_owner = false;
655
656                 status = winsdb_addr_decode(h, rec, &el->values[i], rec->addresses, &rec->addresses[j]);
657                 if (!NT_STATUS_IS_OK(status)) goto failed;
658
659                 if (strcmp(rec->addresses[j]->wins_owner, h->local_owner) == 0) {
660                         we_are_owner = true;
661                 }
662
663                 /*
664                  * the record isn't static and is active
665                  * then don't add the address if it's expired,
666                  * but only if we're the owner of the address
667                  *
668                  * This is important for SGROUP records,
669                  * because each server thinks he's the owner of the
670                  * record and the record isn't replicated on a
671                  * name_refresh. So addresses owned by another owner
672                  * could expire, but we still need to return them
673                  * (as windows does).
674                  */
675                 if (!rec->is_static &&
676                     rec->addresses[j]->expire_time <= now &&
677                     rec->state == WREPL_STATE_ACTIVE &&
678                     we_are_owner) {
679                         DEBUG(5,("WINS: expiring name addr %s of %s (expired at %s)\n", 
680                                  rec->addresses[j]->address, nbt_name_string(rec->addresses[j], rec->name),
681                                  timestring(rec->addresses[j], rec->addresses[j]->expire_time)));
682                         talloc_free(rec->addresses[j]);
683                         rec->addresses[j] = NULL;
684                         continue;
685                 }
686                 j++;
687         }
688         rec->addresses[j] = NULL;
689         num_values = j;
690
691         if (rec->is_static && rec->state == WREPL_STATE_ACTIVE) {
692                 rec->expire_time = get_time_t_max();
693                 for (i=0;rec->addresses[i];i++) {
694                         rec->addresses[i]->expire_time = rec->expire_time;
695                 }
696         }
697
698         if (rec->state == WREPL_STATE_ACTIVE) {
699                 if (num_values < 1) {
700                         DEBUG(5,("WINS: expiring name %s (because it has no active addresses)\n", 
701                                  nbt_name_string(mem_ctx, rec->name)));
702                         rec->state = WREPL_STATE_RELEASED;
703                 }
704         }
705
706         *_rec = rec;
707         return NT_STATUS_OK;
708 failed:
709         if (NT_STATUS_EQUAL(NT_STATUS_INTERNAL_DB_CORRUPTION, status)) {
710                 DEBUG(1,("winsdb_record: corrupted record: %s\n", ldb_dn_get_linearized(msg->dn)));
711         }
712         talloc_free(rec);
713         return status;
714 }
715
716 /*
717   form a ldb_message from a winsdb_record
718 */
719 static struct ldb_message *winsdb_message(struct ldb_context *ldb,
720                                           struct winsdb_record *rec,
721                                           TALLOC_CTX *mem_ctx)
722 {
723         int i, ret;
724         size_t addr_count;
725         const char *expire_time;
726         struct ldb_message *msg = ldb_msg_new(mem_ctx);
727         if (msg == NULL) goto failed;
728
729         /* make sure we don't put in corrupted records */
730         addr_count = winsdb_addr_list_length(rec->addresses);
731         if (rec->state == WREPL_STATE_ACTIVE && addr_count == 0) {
732                 rec->state = WREPL_STATE_RELEASED;
733         }
734         if (rec->type == WREPL_TYPE_UNIQUE && addr_count > 1) {
735                 rec->type = WREPL_TYPE_MHOMED;
736         }
737
738         expire_time = ldb_timestring(msg, rec->expire_time);
739         if (!expire_time) {
740                 goto failed;
741         }
742
743         msg->dn = winsdb_dn(msg, ldb, rec->name);
744         if (msg->dn == NULL) goto failed;
745         ret = ldb_msg_add_fmt(msg, "type", "0x%02X", rec->name->type);
746         if (rec->name->name && *rec->name->name) {
747                 ret |= ldb_msg_add_string(msg, "name", rec->name->name);
748         }
749         if (rec->name->scope && *rec->name->scope) {
750                 ret |= ldb_msg_add_string(msg, "scope", rec->name->scope);
751         }
752         ret |= ldb_msg_add_fmt(msg, "objectClass", "winsRecord");
753         ret |= ldb_msg_add_fmt(msg, "recordType", "%u", rec->type);
754         ret |= ldb_msg_add_fmt(msg, "recordState", "%u", rec->state);
755         ret |= ldb_msg_add_fmt(msg, "nodeType", "%u", rec->node);
756         ret |= ldb_msg_add_fmt(msg, "isStatic", "%u", rec->is_static);
757         ret |= ldb_msg_add_empty(msg, "expireTime", 0, NULL);
758         if (!(rec->is_static && rec->state == WREPL_STATE_ACTIVE)) {
759                 ret |= ldb_msg_add_string(msg, "expireTime", expire_time);
760         }
761         ret |= ldb_msg_add_fmt(msg, "versionID", "%llu", (long long)rec->version);
762         ret |= ldb_msg_add_string(msg, "winsOwner", rec->wins_owner);
763         ret |= ldb_msg_add_empty(msg, "address", 0, NULL);
764         for (i=0;rec->addresses[i];i++) {
765                 ret |= ldb_msg_add_winsdb_addr(msg, rec, "address", rec->addresses[i]);
766         }
767         if (rec->registered_by) {
768                 ret |= ldb_msg_add_empty(msg, "registeredBy", 0, NULL);
769                 ret |= ldb_msg_add_string(msg, "registeredBy", rec->registered_by);
770         }
771         if (ret != LDB_SUCCESS) goto failed;
772         return msg;
773
774 failed:
775         talloc_free(msg);
776         return NULL;
777 }
778
779 /*
780   save a WINS record into the database
781 */
782 uint8_t winsdb_add(struct winsdb_handle *h, struct winsdb_record *rec, uint32_t flags)
783 {
784         struct ldb_message *msg;
785         struct ldb_context *wins_db = h->ldb;
786         TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
787         int trans = -1;
788         int ret;
789
790         trans = ldb_transaction_start(wins_db);
791         if (trans != LDB_SUCCESS) goto failed;
792
793         if (flags & WINSDB_FLAG_ALLOC_VERSION) {
794                 /* passing '0' means auto-allocate a new one */
795                 rec->version = winsdb_set_maxVersion(h, 0);
796                 if (rec->version == 0) goto failed;
797         }
798         if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
799                 rec->wins_owner = h->local_owner;
800         }
801
802         msg = winsdb_message(wins_db, rec, tmp_ctx);
803         if (msg == NULL) goto failed;
804         ret = ldb_add(wins_db, msg);
805         if (ret != LDB_SUCCESS) goto failed;
806
807         trans = ldb_transaction_commit(wins_db);
808         if (trans != LDB_SUCCESS) goto failed;
809
810         wins_hook(h, rec, WINS_HOOK_ADD, h->hook_script);
811
812         talloc_free(tmp_ctx);
813         return NBT_RCODE_OK;
814
815 failed:
816         if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
817         talloc_free(tmp_ctx);
818         return NBT_RCODE_SVR;
819 }
820
821
822 /*
823   modify a WINS record in the database
824 */
825 uint8_t winsdb_modify(struct winsdb_handle *h, struct winsdb_record *rec, uint32_t flags)
826 {
827         struct ldb_message *msg;
828         struct ldb_context *wins_db = h->ldb;
829         TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
830         int trans;
831         int ret;
832         unsigned int i;
833
834         trans = ldb_transaction_start(wins_db);
835         if (trans != LDB_SUCCESS) goto failed;
836
837         if (flags & WINSDB_FLAG_ALLOC_VERSION) {
838                 /* passing '0' means auto-allocate a new one */
839                 rec->version = winsdb_set_maxVersion(h, 0);
840                 if (rec->version == 0) goto failed;
841         }
842         if (flags & WINSDB_FLAG_TAKE_OWNERSHIP) {
843                 rec->wins_owner = h->local_owner;
844         }
845
846         msg = winsdb_message(wins_db, rec, tmp_ctx);
847         if (msg == NULL) goto failed;
848
849         for (i=0;i<msg->num_elements;i++) {
850                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
851         }
852
853         ret = ldb_modify(wins_db, msg);
854         if (ret != LDB_SUCCESS) goto failed;
855
856         trans = ldb_transaction_commit(wins_db);
857         if (trans != LDB_SUCCESS) goto failed;
858
859         wins_hook(h, rec, WINS_HOOK_MODIFY, h->hook_script);
860
861         talloc_free(tmp_ctx);
862         return NBT_RCODE_OK;
863
864 failed:
865         if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
866         talloc_free(tmp_ctx);
867         return NBT_RCODE_SVR;
868 }
869
870
871 /*
872   delete a WINS record from the database
873 */
874 uint8_t winsdb_delete(struct winsdb_handle *h, struct winsdb_record *rec)
875 {
876         struct ldb_context *wins_db = h->ldb;
877         TALLOC_CTX *tmp_ctx = talloc_new(wins_db);
878         struct ldb_dn *dn;
879         int trans;
880         int ret;
881
882         trans = ldb_transaction_start(wins_db);
883         if (trans != LDB_SUCCESS) goto failed;
884
885         dn = winsdb_dn(tmp_ctx, wins_db, rec->name);
886         if (dn == NULL) goto failed;
887
888         ret = ldb_delete(wins_db, dn);
889         if (ret != LDB_SUCCESS) goto failed;
890
891         trans = ldb_transaction_commit(wins_db);
892         if (trans != LDB_SUCCESS) goto failed;
893
894         wins_hook(h, rec, WINS_HOOK_DELETE, h->hook_script);
895
896         talloc_free(tmp_ctx);
897         return NBT_RCODE_OK;
898
899 failed:
900         if (trans == LDB_SUCCESS) ldb_transaction_cancel(wins_db);
901         talloc_free(tmp_ctx);
902         return NBT_RCODE_SVR;
903 }
904
905 static bool winsdb_check_or_add_module_list(struct tevent_context *ev_ctx, 
906                                             struct loadparm_context *lp_ctx, struct winsdb_handle *h,
907                                             const char *wins_path)
908 {
909         int trans;
910         int ret;
911         struct ldb_dn *dn;
912         struct ldb_result *res = NULL;
913         struct ldb_message *msg = NULL;
914         TALLOC_CTX *tmp_ctx = talloc_new(h);
915         unsigned int flags = 0;
916
917         trans = ldb_transaction_start(h->ldb);
918         if (trans != LDB_SUCCESS) goto failed;
919
920         /* check if we have a special @MODULES record already */
921         dn = ldb_dn_new(tmp_ctx, h->ldb, "@MODULES");
922         if (!dn) goto failed;
923
924         /* find the record in the WINS database */
925         ret = ldb_search(h->ldb, tmp_ctx, &res, dn, LDB_SCOPE_BASE, NULL, NULL);
926         if (ret != LDB_SUCCESS) goto failed;
927
928         if (res->count > 0) goto skip;
929
930         /* if there's no record, add one */
931         msg = ldb_msg_new(tmp_ctx);
932         if (!msg) goto failed;
933         msg->dn = dn;
934
935         ret = ldb_msg_add_string(msg, "@LIST", "wins_ldb");
936         if (ret != LDB_SUCCESS) goto failed;
937
938         ret = ldb_add(h->ldb, msg);
939         if (ret != LDB_SUCCESS) goto failed;
940
941         trans = ldb_transaction_commit(h->ldb);
942         if (trans != LDB_SUCCESS) goto failed;
943
944         /* close and reopen the database, with the modules */
945         trans = LDB_ERR_OTHER;
946         talloc_free(h->ldb);
947         h->ldb = NULL;
948
949         if (lpcfg_parm_bool(lp_ctx, NULL,"winsdb", "nosync", false)) {
950                 flags |= LDB_FLG_NOSYNC;
951         }
952
953         h->ldb = ldb_wrap_connect(h, ev_ctx, lp_ctx, wins_path,
954                                   NULL, NULL, flags);
955         if (!h->ldb) goto failed;
956
957         talloc_free(tmp_ctx);
958         return true;
959
960 skip:
961         if (trans == LDB_SUCCESS) ldb_transaction_cancel(h->ldb);
962         talloc_free(tmp_ctx);
963         return true;
964
965 failed:
966         if (trans == LDB_SUCCESS) ldb_transaction_cancel(h->ldb);
967         talloc_free(tmp_ctx);
968         return false;
969 }
970
971 struct winsdb_handle *winsdb_connect(TALLOC_CTX *mem_ctx, 
972                                      struct tevent_context *ev_ctx,
973                                      struct loadparm_context *lp_ctx,
974                                      const char *owner,
975                                      enum winsdb_handle_caller caller)
976 {
977         struct winsdb_handle *h = NULL;
978         unsigned int flags = 0;
979         bool ret;
980         int ldb_err;
981         char *wins_path;
982
983         h = talloc_zero(mem_ctx, struct winsdb_handle);
984         if (!h) return NULL;
985
986         wins_path = lpcfg_state_path(h, lp_ctx, "wins.ldb");
987
988         if (lpcfg_parm_bool(lp_ctx, NULL,"winsdb", "nosync", false)) {
989                 flags |= LDB_FLG_NOSYNC;
990         }
991
992         h->ldb = ldb_wrap_connect(h, ev_ctx, lp_ctx, wins_path,
993                                   NULL, NULL, flags);
994         if (!h->ldb) goto failed;
995
996         h->caller = caller;
997         h->hook_script = lpcfg_wins_hook(lp_ctx);
998
999         h->local_owner = talloc_strdup(h, owner);
1000         if (!h->local_owner) goto failed;
1001
1002         /* make sure the module list is available and used */
1003         ret = winsdb_check_or_add_module_list(ev_ctx, lp_ctx, h, wins_path);
1004         if (!ret) goto failed;
1005
1006         ldb_err = ldb_set_opaque(h->ldb, "winsdb_handle", h);
1007         if (ldb_err != LDB_SUCCESS) goto failed;
1008
1009         return h;
1010 failed:
1011         talloc_free(h);
1012         return NULL;
1013 }
1014