r11567: Ldb API change patch.
[kai/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    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "nbt_server/nbt_server.h"
25 #include "nbt_server/wins/winsdb.h"
26 #include "lib/ldb/include/ldb.h"
27 #include "lib/ldb/include/ldb_errors.h"
28 #include "db_wrap.h"
29 #include "system/time.h"
30
31 /*
32   return the new maxVersion and save it
33 */
34 static uint64_t winsdb_allocate_version(struct wins_server *winssrv)
35 {
36         int trans;
37         int ret;
38         struct ldb_context *ldb = winssrv->wins_db;
39         struct ldb_dn *dn;
40         struct ldb_result *res = NULL;
41         struct ldb_message *msg = NULL;
42         TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
43         uint64_t maxVersion = 0;
44
45         trans = ldb_transaction_start(ldb);
46         if (trans != LDB_SUCCESS) goto failed;
47
48         dn = ldb_dn_explode(tmp_ctx, "CN=VERSION");
49         if (!dn) goto failed;
50
51         /* find the record in the WINS database */
52         ret = ldb_search(ldb, dn, LDB_SCOPE_BASE, NULL, NULL, &res);
53
54         if (ret != LDB_SUCCESS) goto failed;
55         if (res->count > 1) goto failed;
56
57         talloc_steal(tmp_ctx, res);
58
59         if (res->count == 1) {
60                 maxVersion = ldb_msg_find_uint64(res->msgs[0], "maxVersion", 0);
61         }
62         maxVersion++;
63
64         msg = ldb_msg_new(tmp_ctx);
65         if (!msg) goto failed;
66         msg->dn = dn;
67
68
69         ret = ldb_msg_add_empty(msg, "objectClass", LDB_FLAG_MOD_REPLACE);
70         if (ret != 0) goto failed;
71         ret = ldb_msg_add_string(msg, "objectClass", "winsMaxVersion");
72         if (ret != 0) goto failed;
73         ret = ldb_msg_add_empty(msg, "maxVersion", LDB_FLAG_MOD_REPLACE);
74         if (ret != 0) goto failed;
75         ret = ldb_msg_add_fmt(msg, "maxVersion", "%llu", maxVersion);
76         if (ret != 0) goto failed;
77
78         ret = ldb_modify(ldb, msg);
79         if (ret != 0) ret = ldb_add(ldb, msg);
80         if (ret != 0) goto failed;
81
82         trans = ldb_transaction_commit(ldb);
83         if (trans != LDB_SUCCESS) goto failed;
84
85         talloc_free(tmp_ctx);
86         return maxVersion;
87
88 failed:
89         if (trans == LDB_SUCCESS) ldb_transaction_cancel(ldb);
90         talloc_free(tmp_ctx);
91         return 0;
92 }
93
94 /*
95   return a DN for a nbt_name
96 */
97 static struct ldb_dn *winsdb_dn(TALLOC_CTX *mem_ctx, struct nbt_name *name)
98 {
99         struct ldb_dn *dn;
100
101         dn = ldb_dn_string_compose(mem_ctx, NULL, "type=0x%02X", name->type);
102         if (dn && name->name && *name->name) {
103                 dn = ldb_dn_string_compose(mem_ctx, dn, "name=%s", name->name);
104         }
105         if (dn && name->scope && *name->scope) {
106                 dn = ldb_dn_string_compose(mem_ctx, dn, "scope=%s", name->scope);
107         }
108         return dn;
109 }
110
111 static NTSTATUS winsdb_nbt_name(TALLOC_CTX *mem_ctx, struct ldb_dn *dn, struct nbt_name **_name)
112 {
113         NTSTATUS status;
114         struct nbt_name *name;
115         uint32_t cur = 0;
116
117         name = talloc(mem_ctx, struct nbt_name);
118         if (!name) {
119                 status = NT_STATUS_NO_MEMORY;
120                 goto failed;
121         }
122
123         if (dn->comp_num > 3) {
124                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
125                 goto failed;
126         }
127
128         if (dn->comp_num > cur && strcasecmp("scope", dn->components[cur].name) == 0) {
129                 name->scope     = talloc_steal(name, dn->components[cur].value.data);
130                 cur++;
131         } else {
132                 name->scope     = NULL;
133         }
134
135         if (dn->comp_num > cur && strcasecmp("name", dn->components[cur].name) == 0) {
136                 name->name      = talloc_steal(name, dn->components[cur].value.data);
137                 cur++;
138         } else {
139                 name->name      = talloc_strdup(name, "");
140                 if (!name->name) {
141                         status = NT_STATUS_NO_MEMORY;
142                         goto failed;
143                 }
144         }
145
146         if (dn->comp_num > cur && strcasecmp("type", dn->components[cur].name) == 0) {
147                 name->type      = strtoul((char *)dn->components[cur].value.data, NULL, 0);
148                 cur++;
149         } else {
150                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
151                 goto failed;
152         }
153
154         *_name = name;
155         return NT_STATUS_OK;
156 failed:
157         talloc_free(name);
158         return status;
159 }
160
161 /*
162  decode the winsdb_addr("address") attribute:
163  "172.31.1.1" or 
164  "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
165  are valid records
166 */
167 static NTSTATUS winsdb_addr_decode(struct winsdb_record *rec, struct ldb_val *val,
168                                    TALLOC_CTX *mem_ctx, struct winsdb_addr **_addr)
169 {
170         NTSTATUS status;
171         struct winsdb_addr *addr;
172         char *address;
173         char *wins_owner;
174         char *expire_time;
175         char *p;
176
177         addr = talloc(mem_ctx, struct winsdb_addr);
178         if (!addr) {
179                 status = NT_STATUS_NO_MEMORY;
180                 goto failed;
181         }
182
183         address = (char *)val->data;
184
185         p = strchr(address, ';');
186         if (!p) {
187                 /* support old entries, with only the address */
188                 addr->address           = talloc_steal(addr, val->data);
189                 addr->wins_owner        = talloc_reference(addr, rec->wins_owner);
190                 if (!addr->wins_owner) {
191                         status = NT_STATUS_NO_MEMORY;
192                         goto failed;
193                 }
194                 addr->expire_time       = rec->expire_time;
195                 *_addr = addr;
196                 return NT_STATUS_OK;
197         }
198
199         *p = '\0';p++;
200         addr->address = talloc_strdup(addr, address);
201         if (!addr->address) {
202                 status = NT_STATUS_NO_MEMORY;
203                 goto failed;
204         }
205
206         if (strncmp("winsOwner:", p, 10) != 0) {
207                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
208                 goto failed;
209         }
210         wins_owner = p + 10;
211         p = strchr(wins_owner, ';');
212         if (!p) {
213                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
214                 goto failed;
215         }
216
217         *p = '\0';p++;
218         addr->wins_owner = talloc_strdup(addr, wins_owner);
219         if (!addr->wins_owner) {
220                 status = NT_STATUS_NO_MEMORY;
221                 goto failed;
222         }
223
224         if (strncmp("expireTime:", p, 11) != 0) {
225                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
226                 goto failed;
227         }
228
229         expire_time = p + 11;
230         p = strchr(expire_time, ';');
231         if (!p) {
232                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
233                 goto failed;
234         }
235
236         *p = '\0';p++;
237         addr->expire_time = ldb_string_to_time(expire_time);
238
239         *_addr = addr;
240         return NT_STATUS_OK;
241 failed:
242         talloc_free(addr);
243         return status;
244 }
245
246 /*
247  encode the winsdb_addr("address") attribute like this:
248  "172.31.1.1;winsOwner:172.31.9.202;expireTime:20050923032330.0Z;"
249 */
250 static int ldb_msg_add_winsdb_addr(struct ldb_message *msg, 
251                                    const char *attr_name, struct winsdb_addr *addr)
252 {
253         struct ldb_val val;
254         const char *str;
255
256         str = talloc_asprintf(msg, "%s;winsOwner:%s;expireTime:%s;",
257                               addr->address, addr->wins_owner,
258                               ldb_timestring(msg, addr->expire_time));
259         if (!str) return -1;
260
261         val.data = discard_const_p(uint8_t, str);
262         val.length = strlen(str);
263
264         return ldb_msg_add_value(msg, attr_name, &val);
265 }
266
267 struct winsdb_addr **winsdb_addr_list_make(TALLOC_CTX *mem_ctx)
268 {
269         struct winsdb_addr **addresses;
270
271         addresses = talloc_array(mem_ctx, struct winsdb_addr *, 1);
272         if (!addresses) return NULL;
273
274         addresses[0] = NULL;
275
276         return addresses;
277 }
278
279 struct winsdb_addr **winsdb_addr_list_add(struct winsdb_addr **addresses, const char *address,
280                                           const char *wins_owner, time_t expire_time)
281 {
282         size_t len = winsdb_addr_list_length(addresses);
283
284         addresses = talloc_realloc(addresses, addresses, struct winsdb_addr *, len + 2);
285         if (!addresses) return NULL;
286
287         addresses[len] = talloc(addresses, struct winsdb_addr);
288         if (!addresses[len]) {
289                 talloc_free(addresses);
290                 return NULL;
291         }
292
293         addresses[len]->address = talloc_strdup(addresses[len], address);
294         if (!addresses[len]->address) {
295                 talloc_free(addresses);
296                 return NULL;
297         }
298
299         addresses[len]->wins_owner = talloc_strdup(addresses[len], wins_owner);
300         if (!addresses[len]->wins_owner) {
301                 talloc_free(addresses);
302                 return NULL;
303         }
304
305         addresses[len]->expire_time = expire_time;
306
307         addresses[len+1] = NULL;
308
309         return addresses;
310 }
311
312 void winsdb_addr_list_remove(struct winsdb_addr **addresses, const char *address)
313 {
314         size_t i;
315
316         for (i=0; addresses[i]; i++) {
317                 if (strcmp(addresses[i]->address, address) == 0) {
318                         break;
319                 }
320         }
321         if (!addresses[i]) return;
322
323         for (; addresses[i]; i++) {
324                 addresses[i] = addresses[i+1];
325         }
326
327         return;
328 }
329
330 struct winsdb_addr *winsdb_addr_list_check(struct winsdb_addr **addresses, const char *address)
331 {
332         size_t i;
333
334         for (i=0; addresses[i]; i++) {
335                 if (strcmp(addresses[i]->address, address) == 0) {
336                         return addresses[i];
337                 }
338         }
339
340         return NULL;
341 }
342
343 size_t winsdb_addr_list_length(struct winsdb_addr **addresses)
344 {
345         size_t i;
346         for (i=0; addresses[i]; i++);
347         return i;
348 }
349
350 const char **winsdb_addr_string_list(TALLOC_CTX *mem_ctx, struct winsdb_addr **addresses)
351 {
352         size_t len = winsdb_addr_list_length(addresses);
353         const char **str_list;
354         size_t i;
355
356         str_list = talloc_array(mem_ctx, const char *, len + 1);
357         if (!str_list) return NULL;
358
359         for (i=0; i < len; i++) {
360                 str_list[i] = talloc_strdup(str_list, addresses[i]->address);
361                 if (!str_list[i]) {
362                         talloc_free(str_list);
363                         return NULL;
364                 }
365         }
366
367         str_list[len] = NULL;
368         return str_list;
369 }
370
371 /*
372   load a WINS entry from the database
373 */
374 NTSTATUS winsdb_lookup(struct ldb_context *wins_db, 
375                        struct nbt_name *name,
376                        TALLOC_CTX *mem_ctx,
377                        struct winsdb_record **_rec)
378 {
379         NTSTATUS status;
380         struct ldb_result *res = NULL;
381         int ret;
382         struct winsdb_record *rec;
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(res->msgs[0], name, 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 ldb_message *msg, struct nbt_name *name, TALLOC_CTX *mem_ctx, struct winsdb_record **_rec)
421 {
422         NTSTATUS status;
423         struct winsdb_record *rec;
424         struct ldb_message_element *el;
425         uint32_t i;
426
427         rec = talloc(mem_ctx, struct winsdb_record);
428         if (rec == NULL) {
429                 status = NT_STATUS_NO_MEMORY;
430                 goto failed;
431         }
432
433         if (!name) {
434                 status = winsdb_nbt_name(rec, msg->dn, &name);
435                 if (!NT_STATUS_IS_OK(status)) goto failed;
436         }
437
438         if (strlen(name->name) > 15) {
439                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
440                 goto failed;
441         }
442         if (name->scope && strlen(name->scope) > 238) {
443                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
444                 goto failed;
445         }
446
447         /* parse it into a more convenient winsdb_record structure */
448         rec->name               = name;
449         rec->type               = ldb_msg_find_int(msg, "recordType", WREPL_TYPE_UNIQUE);
450         rec->state              = ldb_msg_find_int(msg, "recordState", WREPL_STATE_RELEASED);
451         rec->node               = ldb_msg_find_int(msg, "nodeType", WREPL_NODE_B);
452         rec->is_static          = ldb_msg_find_int(msg, "isStatic", 0);
453         rec->expire_time        = ldb_string_to_time(ldb_msg_find_string(msg, "expireTime", NULL));
454         rec->version            = ldb_msg_find_uint64(msg, "versionID", 0);
455         rec->wins_owner         = ldb_msg_find_string(msg, "winsOwner", NULL);
456         rec->registered_by      = ldb_msg_find_string(msg, "registeredBy", NULL);
457         talloc_steal(rec, rec->wins_owner);
458         talloc_steal(rec, rec->registered_by);
459
460         if (!rec->wins_owner) {
461                 rec->wins_owner = talloc_strdup(rec, WINSDB_OWNER_LOCAL);
462                 if (rec->wins_owner == NULL) {
463                         status = NT_STATUS_NO_MEMORY;
464                         goto failed;
465                 }
466         }
467
468         el = ldb_msg_find_element(msg, "address");
469         if (el == NULL) {
470                 status = NT_STATUS_INTERNAL_DB_CORRUPTION;
471                 goto failed;
472         }
473
474         if (rec->type == WREPL_TYPE_UNIQUE || rec->type == WREPL_TYPE_GROUP) {
475                 if (el->num_values != 1) {
476                         status = NT_STATUS_INTERNAL_DB_CORRUPTION;
477                         goto failed;
478                 }
479         }
480
481         rec->addresses     = talloc_array(rec, struct winsdb_addr *, el->num_values+1);
482         if (rec->addresses == NULL) {
483                 status = NT_STATUS_NO_MEMORY;
484                 goto failed;
485         }
486
487         for (i=0;i<el->num_values;i++) {
488                 status = winsdb_addr_decode(rec, &el->values[i], rec->addresses, &rec->addresses[i]);
489                 if (!NT_STATUS_IS_OK(status)) goto failed;
490         }
491         rec->addresses[i] = NULL;
492
493         *_rec = rec;
494         return NT_STATUS_OK;
495 failed:
496         if (NT_STATUS_EQUAL(NT_STATUS_INTERNAL_DB_CORRUPTION, status)) {
497                 DEBUG(1,("winsdb_record: corrupted record: %s\n", ldb_dn_linearize(rec, msg->dn)));
498         }
499         talloc_free(rec);
500         return status;
501 }
502
503 /*
504   form a ldb_message from a winsdb_record
505 */
506 struct ldb_message *winsdb_message(struct ldb_context *ldb, 
507                                    struct winsdb_record *rec, TALLOC_CTX *mem_ctx)
508 {
509         int i, ret=0;
510         struct ldb_message *msg = ldb_msg_new(mem_ctx);
511         if (msg == NULL) goto failed;
512
513         msg->dn = winsdb_dn(msg, rec->name);
514         if (msg->dn == NULL) goto failed;
515         ret |= ldb_msg_add_fmt(msg, "type", "0x%02X", rec->name->type);
516         if (rec->name->name && *rec->name->name) {
517                 ret |= ldb_msg_add_string(msg, "name", rec->name->name);
518         }
519         if (rec->name->scope && *rec->name->scope) {
520                 ret |= ldb_msg_add_string(msg, "scope", rec->name->scope);
521         }
522         ret |= ldb_msg_add_fmt(msg, "objectClass", "winsRecord");
523         ret |= ldb_msg_add_fmt(msg, "recordType", "%u", rec->type);
524         ret |= ldb_msg_add_fmt(msg, "recordState", "%u", rec->state);
525         ret |= ldb_msg_add_fmt(msg, "nodeType", "%u", rec->node);
526         ret |= ldb_msg_add_fmt(msg, "isStatic", "%u", rec->is_static);
527         ret |= ldb_msg_add_string(msg, "expireTime", 
528                                   ldb_timestring(msg, rec->expire_time));
529         ret |= ldb_msg_add_fmt(msg, "versionID", "%llu", rec->version);
530         ret |= ldb_msg_add_string(msg, "winsOwner", rec->wins_owner);
531         for (i=0;rec->addresses[i];i++) {
532                 ret |= ldb_msg_add_winsdb_addr(msg, "address", rec->addresses[i]);
533         }
534         ret |= ldb_msg_add_string(msg, "registeredBy", rec->registered_by);
535         if (ret != 0) goto failed;
536         return msg;
537
538 failed:
539         talloc_free(msg);
540         return NULL;
541 }
542
543 /*
544   save a WINS record into the database
545 */
546 uint8_t winsdb_add(struct wins_server *winssrv, struct winsdb_record *rec)
547 {
548         struct ldb_context *ldb = winssrv->wins_db;
549         struct ldb_message *msg;
550         TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
551         int trans = -1;
552         int ret = 0;
553
554         trans = ldb_transaction_start(ldb);
555         if (trans != LDB_SUCCESS) goto failed;
556
557         rec->version = winsdb_allocate_version(winssrv);
558         if (rec->version == 0) goto failed;
559         rec->wins_owner = WINSDB_OWNER_LOCAL;
560
561         msg = winsdb_message(winssrv->wins_db, rec, tmp_ctx);
562         if (msg == NULL) goto failed;
563         ret = ldb_add(ldb, msg);
564         if (ret != 0) goto failed;
565
566         trans = ldb_transaction_commit(ldb);
567         if (trans != LDB_SUCCESS) goto failed;
568
569         talloc_free(tmp_ctx);
570         return NBT_RCODE_OK;
571
572 failed:
573         if (trans == LDB_SUCCESS) ldb_transaction_cancel(ldb);
574         talloc_free(tmp_ctx);
575         return NBT_RCODE_SVR;
576 }
577
578
579 /*
580   modify a WINS record in the database
581 */
582 uint8_t winsdb_modify(struct wins_server *winssrv, struct winsdb_record *rec)
583 {
584         struct ldb_context *ldb = winssrv->wins_db;
585         struct ldb_message *msg;
586         TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
587         int trans;
588         int ret;
589         int i;
590
591         trans = ldb_transaction_start(ldb);
592         if (trans != LDB_SUCCESS) goto failed;
593
594         rec->version = winsdb_allocate_version(winssrv);
595         if (rec->version == 0) goto failed;
596         rec->wins_owner = WINSDB_OWNER_LOCAL;
597
598         msg = winsdb_message(winssrv->wins_db, rec, tmp_ctx);
599         if (msg == NULL) goto failed;
600
601         for (i=0;i<msg->num_elements;i++) {
602                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
603         }
604
605         ret = ldb_modify(ldb, msg);
606         if (ret != 0) goto failed;
607
608         trans = ldb_transaction_commit(ldb);
609         if (trans != LDB_SUCCESS) goto failed;
610
611         talloc_free(tmp_ctx);
612         return NBT_RCODE_OK;
613
614 failed:
615         if (trans == LDB_SUCCESS) ldb_transaction_cancel(ldb);
616         talloc_free(tmp_ctx);
617         return NBT_RCODE_SVR;
618 }
619
620
621 /*
622   delete a WINS record from the database
623 */
624 uint8_t winsdb_delete(struct wins_server *winssrv, struct winsdb_record *rec)
625 {
626         struct ldb_context *ldb = winssrv->wins_db;
627         TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
628         const struct ldb_dn *dn;
629         int trans;
630         int ret;
631
632         trans = ldb_transaction_start(ldb);
633         if (trans != LDB_SUCCESS) goto failed;
634
635         dn = winsdb_dn(tmp_ctx, rec->name);
636         if (dn == NULL) goto failed;
637
638         ret = ldb_delete(ldb, dn);
639         if (ret != 0) goto failed;
640
641         trans = ldb_transaction_commit(ldb);
642         if (trans != LDB_SUCCESS) goto failed;
643
644         talloc_free(tmp_ctx);
645         return NBT_RCODE_OK;
646
647 failed:
648         if (trans == LDB_SUCCESS) ldb_transaction_cancel(ldb);
649         talloc_free(tmp_ctx);
650         return NBT_RCODE_SVR;
651 }
652
653 struct ldb_context *winsdb_connect(TALLOC_CTX *mem_ctx)
654 {
655         return ldb_wrap_connect(mem_ctx, lp_wins_url(), 0, NULL);
656 }