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