r8734: fixed the wins server for the new ldb DN restrictions.
[garming/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 "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   remove a version id
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 /*
88   return a DN for a nbt_name
89 */
90 static char *winsdb_dn(TALLOC_CTX *mem_ctx, struct nbt_name *name)
91 {
92         char *ret = talloc_asprintf(mem_ctx, "type=%02x", name->type);
93         if (ret == NULL) {
94                 return ret;
95         }
96         if (name->name && *name->name) {
97                 ret = talloc_asprintf_append(ret, ",name=%s", name->name);
98         }
99         if (ret == NULL) {
100                 return ret;
101         }
102         if (name->scope && *name->scope) {
103                 ret = talloc_asprintf_append(ret, ",scope=%s", name->scope);
104         }
105         if (ret == NULL) {
106                 return ret;
107         }
108         return ret;
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         const char *expr;
123         int i;
124
125         expr = talloc_asprintf(tmp_ctx, "dn=%s", winsdb_dn(tmp_ctx, name));
126         if (expr == NULL) goto failed;
127
128         /* find the record in the WINS database */
129         ret = ldb_search(winssrv->wins_db, NULL, LDB_SCOPE_ONELEVEL, expr, NULL, &res);
130         if (res != NULL) {
131                 talloc_steal(tmp_ctx, res);
132         }
133         if (ret != 1) goto failed;
134
135         rec = talloc(tmp_ctx, struct winsdb_record);
136         if (rec == NULL) goto failed;
137
138         /* parse it into a more convenient winsdb_record structure */
139         rec->name           = name;
140         rec->state          = ldb_msg_find_int(res[0], "active", WINS_REC_RELEASED);
141         rec->nb_flags       = ldb_msg_find_int(res[0], "nbFlags", 0);
142         rec->expire_time    = ldap_string_to_time(ldb_msg_find_string(res[0], "expires", NULL));
143         rec->registered_by  = ldb_msg_find_string(res[0], "registeredBy", NULL);
144         rec->version        = ldb_msg_find_uint64(res[0], "version", 0);
145         talloc_steal(rec, rec->registered_by);
146
147         el = ldb_msg_find_element(res[0], "address");
148         if (el == NULL) goto failed;
149
150         rec->addresses     = talloc_array(rec, const char *, el->num_values+1);
151         if (rec->addresses == NULL) goto failed;
152
153         for (i=0;i<el->num_values;i++) {
154                 rec->addresses[i] = talloc_steal(rec->addresses, el->values[i].data);
155         }
156         rec->addresses[i] = NULL;
157
158         /* see if it has already expired */
159         if (rec->state == WINS_REC_ACTIVE &&
160             rec->expire_time <= time(NULL)) {
161                 DEBUG(5,("WINS: expiring name %s (expired at %s)\n", 
162                          nbt_name_string(tmp_ctx, rec->name), timestring(tmp_ctx, rec->expire_time)));
163                 rec->state = WINS_REC_RELEASED;
164         }
165
166         talloc_steal(mem_ctx, rec);
167         talloc_free(tmp_ctx);
168         return rec;
169
170 failed:
171         talloc_free(tmp_ctx);
172         return NULL;
173 }
174
175
176 /*
177   form a ldb_message from a winsdb_record
178 */
179 static struct ldb_message *winsdb_message(struct wins_server *winssrv, 
180                                           struct winsdb_record *rec, TALLOC_CTX *mem_ctx)
181 {
182         int i, ret=0;
183         struct ldb_context *ldb = winssrv->wins_db;
184         struct ldb_message *msg = ldb_msg_new(mem_ctx);
185         if (msg == NULL) goto failed;
186
187         msg->dn = winsdb_dn(msg, rec->name);
188         if (msg->dn == NULL) goto failed;
189         ret |= ldb_msg_add_fmt(ldb, msg, "objectClass", "wins");
190         ret |= ldb_msg_add_fmt(ldb, msg, "active", "%u", rec->state);
191         ret |= ldb_msg_add_fmt(ldb, msg, "nbFlags", "0x%04x", rec->nb_flags);
192         ret |= ldb_msg_add_string(ldb, msg, "registeredBy", rec->registered_by);
193         ret |= ldb_msg_add_string(ldb, msg, "expires", 
194                                   ldap_timestring(msg, rec->expire_time));
195         ret |= ldb_msg_add_fmt(ldb, msg, "version", "%llu", rec->version);
196         for (i=0;rec->addresses[i];i++) {
197                 ret |= ldb_msg_add_string(ldb, msg, "address", rec->addresses[i]);
198         }
199         if (ret != 0) goto failed;
200         return msg;
201
202 failed:
203         talloc_free(msg);
204         return NULL;
205 }
206
207 /*
208   save a WINS record into the database
209 */
210 uint8_t winsdb_add(struct wins_server *winssrv, struct winsdb_record *rec)
211 {
212         struct ldb_context *ldb = winssrv->wins_db;
213         struct ldb_message *msg;
214         TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
215         int ret;
216
217         rec->version = winsdb_allocate_version(winssrv);
218         if (rec->version == 0) goto failed;
219
220         msg = winsdb_message(winssrv, rec, tmp_ctx);
221         if (msg == NULL) goto failed;
222         ret = ldb_add(ldb, msg);
223         if (ret != 0) goto failed;
224
225         talloc_free(tmp_ctx);
226         return NBT_RCODE_OK;
227
228 failed:
229         talloc_free(tmp_ctx);
230         return NBT_RCODE_SVR;
231 }
232
233
234 /*
235   modify a WINS record in the database
236 */
237 uint8_t winsdb_modify(struct wins_server *winssrv, struct winsdb_record *rec)
238 {
239         struct ldb_context *ldb = winssrv->wins_db;
240         struct ldb_message *msg;
241         TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
242         int ret;
243         int i;
244
245         rec->version = winsdb_allocate_version(winssrv);
246         if (rec->version == 0) goto failed;
247
248         msg = winsdb_message(winssrv, rec, tmp_ctx);
249         if (msg == NULL) goto failed;
250
251         for (i=0;i<msg->num_elements;i++) {
252                 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
253         }
254
255         ret = ldb_modify(ldb, msg);
256         if (ret != 0) goto failed;
257
258         talloc_free(tmp_ctx);
259         return NBT_RCODE_OK;
260
261 failed:
262         talloc_free(tmp_ctx);
263         return NBT_RCODE_SVR;
264 }
265
266
267 /*
268   delete a WINS record from the database
269 */
270 uint8_t winsdb_delete(struct wins_server *winssrv, struct winsdb_record *rec)
271 {
272         struct ldb_context *ldb = winssrv->wins_db;
273         TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
274         int ret;
275         const char *dn;
276
277         winsdb_remove_version(winssrv, rec->version);
278
279         dn = winsdb_dn(tmp_ctx, rec->name);
280         if (dn == NULL) goto failed;
281
282         ret = ldb_delete(ldb, dn);
283         if (ret != 0) goto failed;
284
285         talloc_free(tmp_ctx);
286         return NBT_RCODE_OK;
287
288 failed:
289         talloc_free(tmp_ctx);
290         return NBT_RCODE_SVR;
291 }
292
293
294 /*
295   connect to the WINS database
296 */
297 NTSTATUS winsdb_init(struct wins_server *winssrv)
298 {
299         winssrv->wins_db = ldb_wrap_connect(winssrv, lp_wins_url(), 0, NULL);
300         if (winssrv->wins_db == NULL) {
301                 return NT_STATUS_INTERNAL_DB_ERROR;
302         }
303
304         return NT_STATUS_OK;
305 }