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"
28 #include "system/time.h"
31 save the min/max version IDs for the database
33 static BOOL winsdb_save_version(struct wins_server *winssrv)
36 struct ldb_context *ldb = winssrv->wins_db;
37 struct ldb_message *msg = ldb_msg_new(winssrv);
38 if (msg == NULL) goto failed;
40 msg->dn = talloc_strdup(msg, "CN=VERSION");
41 if (msg->dn == NULL) goto failed;
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;
47 for (i=0;i<msg->num_elements;i++) {
48 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
51 ret = ldb_modify(ldb, msg);
52 if (ret != 0) ret = ldb_add(ldb, msg);
53 if (ret != 0) goto failed;
64 allocate a new version id for a record
66 static uint64_t winsdb_allocate_version(struct wins_server *winssrv)
68 winssrv->max_version++;
69 if (!winsdb_save_version(winssrv)) {
72 return winssrv->max_version;
78 static void winsdb_remove_version(struct wins_server *winssrv, uint64_t version)
80 if (version == winssrv->min_version) {
81 winssrv->min_version++;
82 winsdb_save_version(winssrv);
88 return a DN for a nbt_name
90 static char *winsdb_dn(TALLOC_CTX *mem_ctx, struct nbt_name *name)
92 char *ret = talloc_asprintf(mem_ctx, "type=%02x", name->type);
96 if (name->name && *name->name) {
97 ret = talloc_asprintf_append(ret, ",name=%s", name->name);
102 if (name->scope && *name->scope) {
103 ret = talloc_asprintf_append(ret, ",scope=%s", name->scope);
112 load a WINS entry from the database
114 struct winsdb_record *winsdb_load(struct wins_server *winssrv,
115 struct nbt_name *name, TALLOC_CTX *mem_ctx)
117 struct ldb_message **res = NULL;
119 struct winsdb_record *rec;
120 struct ldb_message_element *el;
121 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
125 expr = talloc_asprintf(tmp_ctx, "dn=%s", winsdb_dn(tmp_ctx, name));
126 if (expr == NULL) goto failed;
128 /* find the record in the WINS database */
129 ret = ldb_search(winssrv->wins_db, NULL, LDB_SCOPE_ONELEVEL, expr, NULL, &res);
131 talloc_steal(tmp_ctx, res);
133 if (ret != 1) goto failed;
135 rec = talloc(tmp_ctx, struct winsdb_record);
136 if (rec == NULL) goto failed;
138 /* parse it into a more convenient winsdb_record structure */
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);
147 el = ldb_msg_find_element(res[0], "address");
148 if (el == NULL) goto failed;
150 rec->addresses = talloc_array(rec, const char *, el->num_values+1);
151 if (rec->addresses == NULL) goto failed;
153 for (i=0;i<el->num_values;i++) {
154 rec->addresses[i] = talloc_steal(rec->addresses, el->values[i].data);
156 rec->addresses[i] = NULL;
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;
166 talloc_steal(mem_ctx, rec);
167 talloc_free(tmp_ctx);
171 talloc_free(tmp_ctx);
177 form a ldb_message from a winsdb_record
179 static struct ldb_message *winsdb_message(struct wins_server *winssrv,
180 struct winsdb_record *rec, TALLOC_CTX *mem_ctx)
183 struct ldb_context *ldb = winssrv->wins_db;
184 struct ldb_message *msg = ldb_msg_new(mem_ctx);
185 if (msg == NULL) goto failed;
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]);
199 if (ret != 0) goto failed;
208 save a WINS record into the database
210 uint8_t winsdb_add(struct wins_server *winssrv, struct winsdb_record *rec)
212 struct ldb_context *ldb = winssrv->wins_db;
213 struct ldb_message *msg;
214 TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
217 rec->version = winsdb_allocate_version(winssrv);
218 if (rec->version == 0) goto failed;
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;
225 talloc_free(tmp_ctx);
229 talloc_free(tmp_ctx);
230 return NBT_RCODE_SVR;
235 modify a WINS record in the database
237 uint8_t winsdb_modify(struct wins_server *winssrv, struct winsdb_record *rec)
239 struct ldb_context *ldb = winssrv->wins_db;
240 struct ldb_message *msg;
241 TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
245 rec->version = winsdb_allocate_version(winssrv);
246 if (rec->version == 0) goto failed;
248 msg = winsdb_message(winssrv, rec, tmp_ctx);
249 if (msg == NULL) goto failed;
251 for (i=0;i<msg->num_elements;i++) {
252 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
255 ret = ldb_modify(ldb, msg);
256 if (ret != 0) goto failed;
258 talloc_free(tmp_ctx);
262 talloc_free(tmp_ctx);
263 return NBT_RCODE_SVR;
268 delete a WINS record from the database
270 uint8_t winsdb_delete(struct wins_server *winssrv, struct winsdb_record *rec)
272 struct ldb_context *ldb = winssrv->wins_db;
273 TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
277 winsdb_remove_version(winssrv, rec->version);
279 dn = winsdb_dn(tmp_ctx, rec->name);
280 if (dn == NULL) goto failed;
282 ret = ldb_delete(ldb, dn);
283 if (ret != 0) goto failed;
285 talloc_free(tmp_ctx);
289 talloc_free(tmp_ctx);
290 return NBT_RCODE_SVR;
295 connect to the WINS database
297 NTSTATUS winsdb_init(struct wins_server *winssrv)
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;