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;
76 allocate a new version id for a record
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);
87 load a WINS entry from the database
89 struct winsdb_record *winsdb_load(struct wins_server *winssrv,
90 struct nbt_name *name, TALLOC_CTX *mem_ctx)
92 struct ldb_message **res = NULL;
94 struct winsdb_record *rec;
95 struct ldb_message_element *el;
96 TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
100 expr = talloc_asprintf(tmp_ctx, "dn=NAME=%s", nbt_name_string(tmp_ctx, name));
101 if (expr == NULL) goto failed;
103 /* find the record in the WINS database */
104 ret = ldb_search(winssrv->wins_db, NULL, LDB_SCOPE_ONELEVEL, expr, NULL, &res);
106 talloc_steal(tmp_ctx, res);
108 if (ret != 1) goto failed;
110 rec = talloc(tmp_ctx, struct winsdb_record);
111 if (rec == NULL) goto failed;
113 /* parse it into a more convenient winsdb_record structure */
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);
122 el = ldb_msg_find_element(res[0], "address");
123 if (el == NULL) goto failed;
125 rec->addresses = talloc_array(rec, const char *, el->num_values+1);
126 if (rec->addresses == NULL) goto failed;
128 for (i=0;i<el->num_values;i++) {
129 rec->addresses[i] = talloc_steal(rec->addresses, el->values[i].data);
131 rec->addresses[i] = NULL;
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;
141 talloc_steal(mem_ctx, rec);
142 talloc_free(tmp_ctx);
146 talloc_free(tmp_ctx);
152 form a ldb_message from a winsdb_record
154 static struct ldb_message *winsdb_message(struct wins_server *winssrv,
155 struct winsdb_record *rec, TALLOC_CTX *mem_ctx)
158 struct ldb_context *ldb = winssrv->wins_db;
159 struct ldb_message *msg = ldb_msg_new(mem_ctx);
160 if (msg == NULL) goto failed;
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]);
173 if (ret != 0) goto failed;
182 save a WINS record into the database
184 uint8_t winsdb_add(struct wins_server *winssrv, struct winsdb_record *rec)
186 struct ldb_context *ldb = winssrv->wins_db;
187 struct ldb_message *msg;
188 TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
191 rec->version = winsdb_allocate_version(winssrv);
192 if (rec->version == 0) goto failed;
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;
199 talloc_free(tmp_ctx);
203 talloc_free(tmp_ctx);
204 return NBT_RCODE_SVR;
209 modify a WINS record in the database
211 uint8_t winsdb_modify(struct wins_server *winssrv, struct winsdb_record *rec)
213 struct ldb_context *ldb = winssrv->wins_db;
214 struct ldb_message *msg;
215 TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
219 rec->version = winsdb_allocate_version(winssrv);
220 if (rec->version == 0) goto failed;
222 msg = winsdb_message(winssrv, rec, tmp_ctx);
223 if (msg == NULL) goto failed;
225 for (i=0;i<msg->num_elements;i++) {
226 msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
229 ret = ldb_modify(ldb, msg);
230 if (ret != 0) goto failed;
232 talloc_free(tmp_ctx);
236 talloc_free(tmp_ctx);
237 return NBT_RCODE_SVR;
242 delete a WINS record from the database
244 uint8_t winsdb_delete(struct wins_server *winssrv, struct winsdb_record *rec)
246 struct ldb_context *ldb = winssrv->wins_db;
247 TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
251 winsdb_remove_version(winssrv, rec->version);
253 dn = talloc_asprintf(tmp_ctx, "NAME=%s", nbt_name_string(tmp_ctx, rec->name));
254 if (dn == NULL) goto failed;
256 ret = ldb_delete(ldb, dn);
257 if (ret != 0) goto failed;
259 talloc_free(tmp_ctx);
263 talloc_free(tmp_ctx);
264 return NBT_RCODE_SVR;
269 connect to the WINS database
271 NTSTATUS winsdb_init(struct wins_server *winssrv)
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;