2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 2005
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.
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.
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.
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"
29 #include "system/time.h"
32 save the min/max version IDs for the database
34 static BOOL winsdb_save_version(struct wins_server *winssrv)
37 struct ldb_context *ldb = winssrv->wins_db;
38 struct ldb_message *msg = ldb_msg_new(winssrv);
39 if (msg == NULL) goto failed;
41 msg->dn = ldb_dn_explode(msg, "CN=VERSION");
42 if (msg->dn == NULL) goto failed;
44 ret |= ldb_msg_add_fmt(ldb, msg, "minVersion", "%llu", winssrv->min_version);
45 ret |= ldb_msg_add_fmt(ldb, msg, "maxVersion", "%llu", winssrv->max_version);
46 if (ret != 0) goto failed;
48 for (i=0;i<msg->num_elements;i++) {
49 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
52 ret = ldb_modify(ldb, msg);
53 if (ret != 0) ret = ldb_add(ldb, msg);
54 if (ret != 0) goto failed;
65 allocate a new version id for a record
67 static uint64_t winsdb_allocate_version(struct wins_server *winssrv)
69 winssrv->max_version++;
70 if (!winsdb_save_version(winssrv)) {
73 return winssrv->max_version;
79 static BOOL winsdb_remove_version(struct wins_server *winssrv, uint64_t version)
81 if (version == winssrv->min_version) {
82 winssrv->min_version++;
83 return winsdb_save_version(winssrv);
91 return a DN for a nbt_name
93 static struct ldb_dn *winsdb_dn(TALLOC_CTX *mem_ctx, struct nbt_name *name)
97 dn = ldb_dn_string_compose(mem_ctx, NULL, "type=%02x", name->type);
101 if (dn && name->name && *name->name) {
102 dn = ldb_dn_string_compose(mem_ctx, dn, "name=%s", name->name);
104 if (dn && name->scope && *name->scope) {
105 dn = ldb_dn_string_compose(mem_ctx, dn, "scope=%s", name->scope);
111 load a WINS entry from the database
113 struct winsdb_record *winsdb_load(struct wins_server *winssrv,
114 struct nbt_name *name, TALLOC_CTX *mem_ctx)
116 struct ldb_message **res = NULL;
118 struct winsdb_record *rec;
119 struct ldb_message_element *el;
120 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
123 /* find the record in the WINS database */
124 ret = ldb_search(winssrv->wins_db, winsdb_dn(tmp_ctx, name), LDB_SCOPE_BASE,
127 talloc_steal(tmp_ctx, res);
129 if (ret != 1) goto failed;
131 rec = talloc(tmp_ctx, struct winsdb_record);
132 if (rec == NULL) goto failed;
134 /* parse it into a more convenient winsdb_record structure */
136 rec->state = ldb_msg_find_int(res[0], "active", WINS_REC_RELEASED);
137 rec->nb_flags = ldb_msg_find_int(res[0], "nbFlags", 0);
138 rec->expire_time = ldap_string_to_time(ldb_msg_find_string(res[0], "expires", NULL));
139 rec->registered_by = ldb_msg_find_string(res[0], "registeredBy", NULL);
140 rec->version = ldb_msg_find_uint64(res[0], "version", 0);
141 talloc_steal(rec, rec->registered_by);
143 el = ldb_msg_find_element(res[0], "address");
144 if (el == NULL) goto failed;
146 rec->addresses = talloc_array(rec, const char *, el->num_values+1);
147 if (rec->addresses == NULL) goto failed;
149 for (i=0;i<el->num_values;i++) {
150 rec->addresses[i] = talloc_steal(rec->addresses, el->values[i].data);
152 rec->addresses[i] = NULL;
154 /* see if it has already expired */
155 if (rec->state == WINS_REC_ACTIVE &&
156 rec->expire_time <= time(NULL)) {
157 DEBUG(5,("WINS: expiring name %s (expired at %s)\n",
158 nbt_name_string(tmp_ctx, rec->name), timestring(tmp_ctx, rec->expire_time)));
159 rec->state = WINS_REC_RELEASED;
162 talloc_steal(mem_ctx, rec);
163 talloc_free(tmp_ctx);
167 talloc_free(tmp_ctx);
173 form a ldb_message from a winsdb_record
175 static struct ldb_message *winsdb_message(struct wins_server *winssrv,
176 struct winsdb_record *rec, TALLOC_CTX *mem_ctx)
179 struct ldb_context *ldb = winssrv->wins_db;
180 struct ldb_message *msg = ldb_msg_new(mem_ctx);
181 if (msg == NULL) goto failed;
183 msg->dn = winsdb_dn(msg, rec->name);
184 if (msg->dn == NULL) goto failed;
185 ret |= ldb_msg_add_fmt(ldb, msg, "objectClass", "wins");
186 ret |= ldb_msg_add_fmt(ldb, msg, "active", "%u", rec->state);
187 ret |= ldb_msg_add_fmt(ldb, msg, "nbFlags", "0x%04x", rec->nb_flags);
188 ret |= ldb_msg_add_string(ldb, msg, "registeredBy", rec->registered_by);
189 ret |= ldb_msg_add_string(ldb, msg, "expires",
190 ldap_timestring(msg, rec->expire_time));
191 ret |= ldb_msg_add_fmt(ldb, msg, "version", "%llu", rec->version);
192 for (i=0;rec->addresses[i];i++) {
193 ret |= ldb_msg_add_string(ldb, msg, "address", rec->addresses[i]);
195 if (ret != 0) goto failed;
204 save a WINS record into the database
206 uint8_t winsdb_add(struct wins_server *winssrv, struct winsdb_record *rec)
208 struct ldb_context *ldb = winssrv->wins_db;
209 struct ldb_message *msg;
210 TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
215 trans = ldb_transaction_start(ldb);
216 if (trans != LDB_SUCCESS) goto failed;
218 rec->version = winsdb_allocate_version(winssrv);
219 if (rec->version == 0) goto failed;
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;
226 trans = ldb_transaction_commit(ldb);
227 if (trans != LDB_SUCCESS) goto failed;
229 talloc_free(tmp_ctx);
233 if (trans == LDB_SUCCESS) ldb_transaction_cancel(ldb);
234 talloc_free(tmp_ctx);
235 return NBT_RCODE_SVR;
240 modify a WINS record in the database
242 uint8_t winsdb_modify(struct wins_server *winssrv, struct winsdb_record *rec)
244 struct ldb_context *ldb = winssrv->wins_db;
245 struct ldb_message *msg;
246 TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
251 trans = ldb_transaction_start(ldb);
252 if (trans != LDB_SUCCESS) goto failed;
254 rec->version = winsdb_allocate_version(winssrv);
255 if (rec->version == 0) goto failed;
257 msg = winsdb_message(winssrv, rec, tmp_ctx);
258 if (msg == NULL) goto failed;
260 for (i=0;i<msg->num_elements;i++) {
261 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
264 ret = ldb_modify(ldb, msg);
265 if (ret != 0) goto failed;
267 trans = ldb_transaction_commit(ldb);
268 if (trans != LDB_SUCCESS) goto failed;
270 talloc_free(tmp_ctx);
274 if (trans == LDB_SUCCESS) ldb_transaction_cancel(ldb);
275 talloc_free(tmp_ctx);
276 return NBT_RCODE_SVR;
281 delete a WINS record from the database
283 uint8_t winsdb_delete(struct wins_server *winssrv, struct winsdb_record *rec)
285 struct ldb_context *ldb = winssrv->wins_db;
286 TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
287 const struct ldb_dn *dn;
291 trans = ldb_transaction_start(ldb);
292 if (trans != LDB_SUCCESS) goto failed;
294 if(!winsdb_remove_version(winssrv, rec->version))
297 dn = winsdb_dn(tmp_ctx, rec->name);
298 if (dn == NULL) goto failed;
300 ret = ldb_delete(ldb, dn);
301 if (ret != 0) goto failed;
303 trans = ldb_transaction_commit(ldb);
304 if (trans != LDB_SUCCESS) goto failed;
306 talloc_free(tmp_ctx);
310 if (trans == LDB_SUCCESS) ldb_transaction_cancel(ldb);
311 talloc_free(tmp_ctx);
312 return NBT_RCODE_SVR;
317 connect to the WINS database
319 NTSTATUS winsdb_init(struct wins_server *winssrv)
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;