r5585: LDB interfaces change:
[samba.git] / source / nbt_server / wins / winsdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    WINS database routines
5
6    Copyright (C) Andrew Tridgell        2005
7    
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 "db_wrap.h"
28 #include "system/time.h"
29
30 /*
31   save the min/max version IDs for the database
32 */
33 static BOOL winsdb_save_version(struct wins_server *winssrv)
34 {
35         int i, ret = 0;
36         struct ldb_context *ldb = winssrv->wins_db;
37         struct ldb_message *msg = ldb_msg_new(winssrv);
38         if (msg == NULL) goto failed;
39
40         msg->dn = talloc_strdup(msg, "CN=VERSION");
41         if (msg->dn == NULL) goto failed;
42
43         ret |= ldb_msg_add_fmt(ldb, msg, "minVersion", "%llu", winssrv->min_version);
44         ret |= ldb_msg_add_fmt(ldb, msg, "maxVersion", "%llu", winssrv->max_version);
45         if (ret != 0) goto failed;
46
47         for (i=0;i<msg->num_elements;i++) {
48                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
49         }
50
51         ret = ldb_modify(ldb, msg);
52         if (ret != 0) ret = ldb_add(ldb, msg);
53         if (ret != 0) goto failed;
54
55         talloc_free(msg);
56         return True;
57
58 failed:
59         talloc_free(msg);
60         return False;
61 }
62
63 /*
64   allocate a new version id for a record
65 */
66 static uint64_t winsdb_allocate_version(struct wins_server *winssrv)
67 {
68         winssrv->max_version++;
69         if (!winsdb_save_version(winssrv)) {
70                 return 0;
71         }
72         return winssrv->max_version;
73 }
74
75 /*
76   allocate a new version id for a record
77 */
78 static void winsdb_remove_version(struct wins_server *winssrv, uint64_t version)
79 {
80         if (version == winssrv->min_version) {
81                 winssrv->min_version++;
82                 winsdb_save_version(winssrv);
83         }
84 }
85
86 /*
87   load a WINS entry from the database
88 */
89 struct winsdb_record *winsdb_load(struct wins_server *winssrv, 
90                                   struct nbt_name *name, TALLOC_CTX *mem_ctx)
91 {
92         struct ldb_message **res = NULL;
93         int ret;
94         struct winsdb_record *rec;
95         struct ldb_message_element *el;
96         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
97         const char *expr;
98         int i;
99
100         expr = talloc_asprintf(tmp_ctx, "dn=NAME=%s", nbt_name_string(tmp_ctx, name));
101         if (expr == NULL) goto failed;
102
103         /* find the record in the WINS database */
104         ret = ldb_search(winssrv->wins_db, NULL, LDB_SCOPE_ONELEVEL, expr, NULL, &res);
105         if (res != NULL) {
106                 talloc_steal(tmp_ctx, res);
107         }
108         if (ret != 1) goto failed;
109
110         rec = talloc(tmp_ctx, struct winsdb_record);
111         if (rec == NULL) goto failed;
112
113         /* parse it into a more convenient winsdb_record structure */
114         rec->name           = name;
115         rec->state          = ldb_msg_find_int(res[0], "active", WINS_REC_RELEASED);
116         rec->nb_flags       = ldb_msg_find_int(res[0], "nbFlags", 0);
117         rec->expire_time    = ldap_string_to_time(ldb_msg_find_string(res[0], "expires", NULL));
118         rec->registered_by  = ldb_msg_find_string(res[0], "registeredBy", NULL);
119         rec->version        = ldb_msg_find_uint64(res[0], "version", 0);
120         talloc_steal(rec, rec->registered_by);
121
122         el = ldb_msg_find_element(res[0], "address");
123         if (el == NULL) goto failed;
124
125         rec->addresses     = talloc_array(rec, const char *, el->num_values+1);
126         if (rec->addresses == NULL) goto failed;
127
128         for (i=0;i<el->num_values;i++) {
129                 rec->addresses[i] = talloc_steal(rec->addresses, el->values[i].data);
130         }
131         rec->addresses[i] = NULL;
132
133         /* see if it has already expired */
134         if (rec->state == WINS_REC_ACTIVE &&
135             rec->expire_time <= time(NULL)) {
136                 DEBUG(5,("WINS: expiring name %s (expired at %s)\n", 
137                          nbt_name_string(tmp_ctx, rec->name), timestring(tmp_ctx, rec->expire_time)));
138                 rec->state = WINS_REC_RELEASED;
139         }
140
141         talloc_steal(mem_ctx, rec);
142         talloc_free(tmp_ctx);
143         return rec;
144
145 failed:
146         talloc_free(tmp_ctx);
147         return NULL;
148 }
149
150
151 /*
152   form a ldb_message from a winsdb_record
153 */
154 static struct ldb_message *winsdb_message(struct wins_server *winssrv, 
155                                           struct winsdb_record *rec, TALLOC_CTX *mem_ctx)
156 {
157         int i, ret=0;
158         struct ldb_context *ldb = winssrv->wins_db;
159         struct ldb_message *msg = ldb_msg_new(mem_ctx);
160         if (msg == NULL) goto failed;
161
162         msg->dn = talloc_asprintf(msg, "NAME=%s", nbt_name_string(msg, rec->name));
163         if (msg->dn == NULL) goto failed;
164         ret |= ldb_msg_add_fmt(ldb, msg, "active", "%u", rec->state);
165         ret |= ldb_msg_add_fmt(ldb, msg, "nbFlags", "0x%04x", rec->nb_flags);
166         ret |= ldb_msg_add_string(ldb, msg, "registeredBy", rec->registered_by);
167         ret |= ldb_msg_add_string(ldb, msg, "expires", 
168                                   ldap_timestring(msg, rec->expire_time));
169         ret |= ldb_msg_add_fmt(ldb, msg, "version", "%llu", rec->version);
170         for (i=0;rec->addresses[i];i++) {
171                 ret |= ldb_msg_add_string(ldb, msg, "address", rec->addresses[i]);
172         }
173         if (ret != 0) goto failed;
174         return msg;
175
176 failed:
177         talloc_free(msg);
178         return NULL;
179 }
180
181 /*
182   save a WINS record into the database
183 */
184 uint8_t winsdb_add(struct wins_server *winssrv, struct winsdb_record *rec)
185 {
186         struct ldb_context *ldb = winssrv->wins_db;
187         struct ldb_message *msg;
188         TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
189         int ret;
190
191         rec->version = winsdb_allocate_version(winssrv);
192         if (rec->version == 0) goto failed;
193
194         msg = winsdb_message(winssrv, rec, tmp_ctx);
195         if (msg == NULL) goto failed;
196         ret = ldb_add(ldb, msg);
197         if (ret != 0) goto failed;
198
199         talloc_free(tmp_ctx);
200         return NBT_RCODE_OK;
201
202 failed:
203         talloc_free(tmp_ctx);
204         return NBT_RCODE_SVR;
205 }
206
207
208 /*
209   modify a WINS record in the database
210 */
211 uint8_t winsdb_modify(struct wins_server *winssrv, struct winsdb_record *rec)
212 {
213         struct ldb_context *ldb = winssrv->wins_db;
214         struct ldb_message *msg;
215         TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
216         int ret;
217         int i;
218
219         rec->version = winsdb_allocate_version(winssrv);
220         if (rec->version == 0) goto failed;
221
222         msg = winsdb_message(winssrv, rec, tmp_ctx);
223         if (msg == NULL) goto failed;
224
225         for (i=0;i<msg->num_elements;i++) {
226                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
227         }
228
229         ret = ldb_modify(ldb, msg);
230         if (ret != 0) goto failed;
231
232         talloc_free(tmp_ctx);
233         return NBT_RCODE_OK;
234
235 failed:
236         talloc_free(tmp_ctx);
237         return NBT_RCODE_SVR;
238 }
239
240
241 /*
242   delete a WINS record from the database
243 */
244 uint8_t winsdb_delete(struct wins_server *winssrv, struct winsdb_record *rec)
245 {
246         struct ldb_context *ldb = winssrv->wins_db;
247         TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
248         int ret;
249         const char *dn;
250
251         winsdb_remove_version(winssrv, rec->version);
252
253         dn = talloc_asprintf(tmp_ctx, "NAME=%s", nbt_name_string(tmp_ctx, rec->name));
254         if (dn == NULL) goto failed;
255
256         ret = ldb_delete(ldb, dn);
257         if (ret != 0) goto failed;
258
259         talloc_free(tmp_ctx);
260         return NBT_RCODE_OK;
261
262 failed:
263         talloc_free(tmp_ctx);
264         return NBT_RCODE_SVR;
265 }
266
267
268 /*
269   connect to the WINS database
270 */
271 NTSTATUS winsdb_init(struct wins_server *winssrv)
272 {
273         winssrv->wins_db = ldb_wrap_connect(winssrv, lp_wins_url(), 0, NULL);
274         if (winssrv->wins_db == NULL) {
275                 return NT_STATUS_INTERNAL_DB_ERROR;
276         }
277
278         return NT_STATUS_OK;
279 }