r10913: This patch isn't as big as it looks ...
[ira/wip.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   save the min/max version IDs for the database
33 */
34 static BOOL winsdb_save_version(struct wins_server *winssrv)
35 {
36         int i, ret = 0;
37         struct ldb_context *ldb = winssrv->wins_db;
38         struct ldb_message *msg = ldb_msg_new(winssrv);
39         if (msg == NULL) goto failed;
40
41         msg->dn = ldb_dn_explode(msg, "CN=VERSION");
42         if (msg->dn == NULL) goto failed;
43
44         ret |= ldb_msg_add_string(msg, "objectClass", "winsEntry");
45         ret |= ldb_msg_add_fmt(msg, "minVersion", "%llu", winssrv->min_version);
46         ret |= ldb_msg_add_fmt(msg, "maxVersion", "%llu", winssrv->max_version);
47         if (ret != 0) goto failed;
48
49         for (i=0;i<msg->num_elements;i++) {
50                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
51         }
52
53         ret = ldb_modify(ldb, msg);
54         if (ret != 0) ret = ldb_add(ldb, msg);
55         if (ret != 0) goto failed;
56
57         talloc_free(msg);
58         return True;
59
60 failed:
61         talloc_free(msg);
62         return False;
63 }
64
65 /*
66   allocate a new version id for a record
67 */
68 static uint64_t winsdb_allocate_version(struct wins_server *winssrv)
69 {
70         winssrv->max_version++;
71         if (!winsdb_save_version(winssrv)) {
72                 return 0;
73         }
74         return winssrv->max_version;
75 }
76
77 /*
78   remove a version id
79 */
80 static BOOL winsdb_remove_version(struct wins_server *winssrv, uint64_t version)
81 {
82         if (version == winssrv->min_version) {
83                 winssrv->min_version++;
84                 return winsdb_save_version(winssrv);
85         }
86
87         return True;
88 }
89
90
91 /*
92   return a DN for a nbt_name
93 */
94 static struct ldb_dn *winsdb_dn(TALLOC_CTX *mem_ctx, struct nbt_name *name)
95 {
96         struct ldb_dn *dn;
97
98         dn = ldb_dn_string_compose(mem_ctx, NULL, "type=%02x", name->type);
99         if (dn == NULL) {
100                 return NULL;
101         }
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 /*
112   load a WINS entry from the database
113 */
114 struct winsdb_record *winsdb_load(struct wins_server *winssrv, 
115                                   struct nbt_name *name, TALLOC_CTX *mem_ctx)
116 {
117         struct ldb_message **res = NULL;
118         int ret;
119         struct winsdb_record *rec;
120         struct ldb_message_element *el;
121         TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
122         int i;
123
124         /* find the record in the WINS database */
125         ret = ldb_search(winssrv->wins_db, winsdb_dn(tmp_ctx, name), LDB_SCOPE_BASE, 
126                          NULL, NULL, &res);
127         if (res != NULL) {
128                 talloc_steal(tmp_ctx, res);
129         }
130         if (ret != 1) goto failed;
131
132         rec = talloc(tmp_ctx, struct winsdb_record);
133         if (rec == NULL) goto failed;
134
135         /* parse it into a more convenient winsdb_record structure */
136         rec->name           = name;
137         rec->state          = ldb_msg_find_int(res[0], "active", WINS_REC_RELEASED);
138         rec->nb_flags       = ldb_msg_find_int(res[0], "nbFlags", 0);
139         rec->expire_time    = ldap_string_to_time(ldb_msg_find_string(res[0], "expires", NULL));
140         rec->registered_by  = ldb_msg_find_string(res[0], "registeredBy", NULL);
141         rec->version        = ldb_msg_find_uint64(res[0], "version", 0);
142         talloc_steal(rec, rec->registered_by);
143
144         el = ldb_msg_find_element(res[0], "address");
145         if (el == NULL) goto failed;
146
147         rec->addresses     = talloc_array(rec, const char *, el->num_values+1);
148         if (rec->addresses == NULL) goto failed;
149
150         for (i=0;i<el->num_values;i++) {
151                 rec->addresses[i] = talloc_steal(rec->addresses, el->values[i].data);
152         }
153         rec->addresses[i] = NULL;
154
155         /* see if it has already expired */
156         if (rec->state == WINS_REC_ACTIVE &&
157             rec->expire_time <= time(NULL)) {
158                 DEBUG(5,("WINS: expiring name %s (expired at %s)\n", 
159                          nbt_name_string(tmp_ctx, rec->name), timestring(tmp_ctx, rec->expire_time)));
160                 rec->state = WINS_REC_RELEASED;
161         }
162
163         talloc_steal(mem_ctx, rec);
164         talloc_free(tmp_ctx);
165         return rec;
166
167 failed:
168         talloc_free(tmp_ctx);
169         return NULL;
170 }
171
172
173 /*
174   form a ldb_message from a winsdb_record
175 */
176 static struct ldb_message *winsdb_message(struct wins_server *winssrv, 
177                                           struct winsdb_record *rec, TALLOC_CTX *mem_ctx)
178 {
179         int i, ret=0;
180         struct ldb_message *msg = ldb_msg_new(mem_ctx);
181         if (msg == NULL) goto failed;
182
183         msg->dn = winsdb_dn(msg, rec->name);
184         if (msg->dn == NULL) goto failed;
185         ret |= ldb_msg_add_fmt(msg, "objectClass", "wins");
186         ret |= ldb_msg_add_fmt(msg, "active", "%u", rec->state);
187         ret |= ldb_msg_add_fmt(msg, "nbFlags", "0x%04x", rec->nb_flags);
188         ret |= ldb_msg_add_string(msg, "registeredBy", rec->registered_by);
189         ret |= ldb_msg_add_string(msg, "expires", 
190                                   ldap_timestring(msg, rec->expire_time));
191         ret |= ldb_msg_add_fmt(msg, "version", "%llu", rec->version);
192         for (i=0;rec->addresses[i];i++) {
193                 ret |= ldb_msg_add_string(msg, "address", rec->addresses[i]);
194         }
195         if (ret != 0) goto failed;
196         return msg;
197
198 failed:
199         talloc_free(msg);
200         return NULL;
201 }
202
203 /*
204   save a WINS record into the database
205 */
206 uint8_t winsdb_add(struct wins_server *winssrv, struct winsdb_record *rec)
207 {
208         struct ldb_context *ldb = winssrv->wins_db;
209         struct ldb_message *msg;
210         TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
211         int trans = -1;
212         int ret = 0;
213
214
215         trans = ldb_transaction_start(ldb);
216         if (trans != LDB_SUCCESS) goto failed;
217
218         rec->version = winsdb_allocate_version(winssrv);
219         if (rec->version == 0) goto failed;
220
221         msg = winsdb_message(winssrv, rec, tmp_ctx);
222         if (msg == NULL) goto failed;
223         ret = ldb_add(ldb, msg);
224         if (ret != 0) goto failed;
225
226         trans = ldb_transaction_commit(ldb);
227         if (trans != LDB_SUCCESS) goto failed;
228
229         talloc_free(tmp_ctx);
230         return NBT_RCODE_OK;
231
232 failed:
233         if (trans == LDB_SUCCESS) ldb_transaction_cancel(ldb);
234         talloc_free(tmp_ctx);
235         return NBT_RCODE_SVR;
236 }
237
238
239 /*
240   modify a WINS record in the database
241 */
242 uint8_t winsdb_modify(struct wins_server *winssrv, struct winsdb_record *rec)
243 {
244         struct ldb_context *ldb = winssrv->wins_db;
245         struct ldb_message *msg;
246         TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
247         int trans;
248         int ret;
249         int i;
250
251         trans = ldb_transaction_start(ldb);
252         if (trans != LDB_SUCCESS) goto failed;
253
254         rec->version = winsdb_allocate_version(winssrv);
255         if (rec->version == 0) goto failed;
256
257         msg = winsdb_message(winssrv, rec, tmp_ctx);
258         if (msg == NULL) goto failed;
259
260         for (i=0;i<msg->num_elements;i++) {
261                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
262         }
263
264         ret = ldb_modify(ldb, msg);
265         if (ret != 0) goto failed;
266
267         trans = ldb_transaction_commit(ldb);
268         if (trans != LDB_SUCCESS) goto failed;
269
270         talloc_free(tmp_ctx);
271         return NBT_RCODE_OK;
272
273 failed:
274         if (trans == LDB_SUCCESS) ldb_transaction_cancel(ldb);
275         talloc_free(tmp_ctx);
276         return NBT_RCODE_SVR;
277 }
278
279
280 /*
281   delete a WINS record from the database
282 */
283 uint8_t winsdb_delete(struct wins_server *winssrv, struct winsdb_record *rec)
284 {
285         struct ldb_context *ldb = winssrv->wins_db;
286         TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
287         const struct ldb_dn *dn;
288         int trans;
289         int ret;
290
291         trans = ldb_transaction_start(ldb);
292         if (trans != LDB_SUCCESS) goto failed;
293
294         if(!winsdb_remove_version(winssrv, rec->version))
295                 goto failed;
296
297         dn = winsdb_dn(tmp_ctx, rec->name);
298         if (dn == NULL) goto failed;
299
300         ret = ldb_delete(ldb, dn);
301         if (ret != 0) goto failed;
302
303         trans = ldb_transaction_commit(ldb);
304         if (trans != LDB_SUCCESS) goto failed;
305
306         talloc_free(tmp_ctx);
307         return NBT_RCODE_OK;
308
309 failed:
310         if (trans == LDB_SUCCESS) ldb_transaction_cancel(ldb);
311         talloc_free(tmp_ctx);
312         return NBT_RCODE_SVR;
313 }
314
315
316 /*
317   connect to the WINS database
318 */
319 NTSTATUS winsdb_init(struct wins_server *winssrv)
320 {
321         winssrv->wins_db = ldb_wrap_connect(winssrv, lp_wins_url(), 0, NULL);
322         if (winssrv->wins_db == NULL) {
323                 return NT_STATUS_INTERNAL_DB_ERROR;
324         }
325
326         return NT_STATUS_OK;
327 }