summaryrefslogtreecommitdiff
path: root/source4/nbt_server/wins/winsdb.c
blob: c46c4c571e4a6af680d5bb0bb93bae9247be1142 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/* 
   Unix SMB/CIFS implementation.

   WINS database routines

   Copyright (C) Andrew Tridgell	2005
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "includes.h"
#include "nbt_server/nbt_server.h"
#include "nbt_server/wins/winsdb.h"
#include "lib/ldb/include/ldb.h"
#include "db_wrap.h"
#include "system/time.h"

/*
  save the min/max version IDs for the database
*/
static BOOL winsdb_save_version(struct wins_server *winssrv)
{
	int i, ret = 0;
	struct ldb_context *ldb = winssrv->wins_db;
	struct ldb_message *msg = ldb_msg_new(winssrv);
	if (msg == NULL) goto failed;

	msg->dn = talloc_strdup(msg, "CN=VERSION");
	if (msg->dn == NULL) goto failed;

	ret |= ldb_msg_add_fmt(ldb, msg, "minVersion", "%llu", winssrv->min_version);
	ret |= ldb_msg_add_fmt(ldb, msg, "maxVersion", "%llu", winssrv->max_version);
	if (ret != 0) goto failed;

	for (i=0;i<msg->num_elements;i++) {
		msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
	}

	ret = ldb_modify(ldb, msg);
	if (ret != 0) ret = ldb_add(ldb, msg);
	if (ret != 0) goto failed;

	talloc_free(msg);
	return True;

failed:
	talloc_free(msg);
	return False;
}

/*
  allocate a new version id for a record
*/
static uint64_t winsdb_allocate_version(struct wins_server *winssrv)
{
	winssrv->max_version++;
	if (!winsdb_save_version(winssrv)) {
		return 0;
	}
	return winssrv->max_version;
}

/*
  remove a version id
*/
static void winsdb_remove_version(struct wins_server *winssrv, uint64_t version)
{
	if (version == winssrv->min_version) {
		winssrv->min_version++;
		winsdb_save_version(winssrv);
	}
}


/*
  return a DN for a nbt_name
*/
static char *winsdb_dn(TALLOC_CTX *mem_ctx, struct nbt_name *name)
{
	char *ret = talloc_asprintf(mem_ctx, "type=%02x", name->type);
	if (ret == NULL) {
		return ret;
	}
	if (name->name && *name->name) {
		ret = talloc_asprintf_append(ret, ",name=%s", name->name);
	}
	if (ret == NULL) {
		return ret;
	}
	if (name->scope && *name->scope) {
		ret = talloc_asprintf_append(ret, ",scope=%s", name->scope);
	}
	if (ret == NULL) {
		return ret;
	}
	return ret;
}

/*
  load a WINS entry from the database
*/
struct winsdb_record *winsdb_load(struct wins_server *winssrv, 
				  struct nbt_name *name, TALLOC_CTX *mem_ctx)
{
	struct ldb_message **res = NULL;
	int ret;
	struct winsdb_record *rec;
	struct ldb_message_element *el;
	TALLOC_CTX *tmp_ctx = talloc_new(mem_ctx);
	const char *expr;
	int i;

	expr = talloc_asprintf(tmp_ctx, "dn=%s", winsdb_dn(tmp_ctx, name));
	if (expr == NULL) goto failed;

	/* find the record in the WINS database */
	ret = ldb_search(winssrv->wins_db, NULL, LDB_SCOPE_ONELEVEL, expr, NULL, &res);
	if (res != NULL) {
		talloc_steal(tmp_ctx, res);
	}
	if (ret != 1) goto failed;

	rec = talloc(tmp_ctx, struct winsdb_record);
	if (rec == NULL) goto failed;

	/* parse it into a more convenient winsdb_record structure */
	rec->name           = name;
	rec->state          = ldb_msg_find_int(res[0], "active", WINS_REC_RELEASED);
	rec->nb_flags       = ldb_msg_find_int(res[0], "nbFlags", 0);
	rec->expire_time    = ldap_string_to_time(ldb_msg_find_string(res[0], "expires", NULL));
	rec->registered_by  = ldb_msg_find_string(res[0], "registeredBy", NULL);
	rec->version        = ldb_msg_find_uint64(res[0], "version", 0);
	talloc_steal(rec, rec->registered_by);

	el = ldb_msg_find_element(res[0], "address");
	if (el == NULL) goto failed;

	rec->addresses     = talloc_array(rec, const char *, el->num_values+1);
	if (rec->addresses == NULL) goto failed;

	for (i=0;i<el->num_values;i++) {
		rec->addresses[i] = talloc_steal(rec->addresses, el->values[i].data);
	}
	rec->addresses[i] = NULL;

	/* see if it has already expired */
	if (rec->state == WINS_REC_ACTIVE &&
	    rec->expire_time <= time(NULL)) {
		DEBUG(5,("WINS: expiring name %s (expired at %s)\n", 
			 nbt_name_string(tmp_ctx, rec->name), timestring(tmp_ctx, rec->expire_time)));
		rec->state = WINS_REC_RELEASED;
	}

	talloc_steal(mem_ctx, rec);
	talloc_free(tmp_ctx);
	return rec;

failed:
	talloc_free(tmp_ctx);
	return NULL;
}


/*
  form a ldb_message from a winsdb_record
*/
static struct ldb_message *winsdb_message(struct wins_server *winssrv, 
					  struct winsdb_record *rec, TALLOC_CTX *mem_ctx)
{
	int i, ret=0;
	struct ldb_context *ldb = winssrv->wins_db;
	struct ldb_message *msg = ldb_msg_new(mem_ctx);
	if (msg == NULL) goto failed;

	msg->dn = winsdb_dn(msg, rec->name);
	if (msg->dn == NULL) goto failed;
	ret |= ldb_msg_add_fmt(ldb, msg, "objectClass", "wins");
	ret |= ldb_msg_add_fmt(ldb, msg, "active", "%u", rec->state);
	ret |= ldb_msg_add_fmt(ldb, msg, "nbFlags", "0x%04x", rec->nb_flags);
	ret |= ldb_msg_add_string(ldb, msg, "registeredBy", rec->registered_by);
	ret |= ldb_msg_add_string(ldb, msg, "expires", 
				  ldap_timestring(msg, rec->expire_time));
	ret |= ldb_msg_add_fmt(ldb, msg, "version", "%llu", rec->version);
	for (i=0;rec->addresses[i];i++) {
		ret |= ldb_msg_add_string(ldb, msg, "address", rec->addresses[i]);
	}
	if (ret != 0) goto failed;
	return msg;

failed:
	talloc_free(msg);
	return NULL;
}

/*
  save a WINS record into the database
*/
uint8_t winsdb_add(struct wins_server *winssrv, struct winsdb_record *rec)
{
	struct ldb_context *ldb = winssrv->wins_db;
	struct ldb_message *msg;
	TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
	int ret;

	rec->version = winsdb_allocate_version(winssrv);
	if (rec->version == 0) goto failed;

	msg = winsdb_message(winssrv, rec, tmp_ctx);
	if (msg == NULL) goto failed;
	ret = ldb_add(ldb, msg);
	if (ret != 0) goto failed;

	talloc_free(tmp_ctx);
	return NBT_RCODE_OK;

failed:
	talloc_free(tmp_ctx);
	return NBT_RCODE_SVR;
}


/*
  modify a WINS record in the database
*/
uint8_t winsdb_modify(struct wins_server *winssrv, struct winsdb_record *rec)
{
	struct ldb_context *ldb = winssrv->wins_db;
	struct ldb_message *msg;
	TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
	int ret;
	int i;

	rec->version = winsdb_allocate_version(winssrv);
	if (rec->version == 0) goto failed;

	msg = winsdb_message(winssrv, rec, tmp_ctx);
	if (msg == NULL) goto failed;

	for (i=0;i<msg->num_elements;i++) {
		msg->elements[i].flags = LDB_FLAG_MOD_REPLACE;
	}

	ret = ldb_modify(ldb, msg);
	if (ret != 0) goto failed;

	talloc_free(tmp_ctx);
	return NBT_RCODE_OK;

failed:
	talloc_free(tmp_ctx);
	return NBT_RCODE_SVR;
}


/*
  delete a WINS record from the database
*/
uint8_t winsdb_delete(struct wins_server *winssrv, struct winsdb_record *rec)
{
	struct ldb_context *ldb = winssrv->wins_db;
	TALLOC_CTX *tmp_ctx = talloc_new(winssrv);
	int ret;
	const char *dn;

	winsdb_remove_version(winssrv, rec->version);

	dn = winsdb_dn(tmp_ctx, rec->name);
	if (dn == NULL) goto failed;

	ret = ldb_delete(ldb, dn);
	if (ret != 0) goto failed;

	talloc_free(tmp_ctx);
	return NBT_RCODE_OK;

failed:
	talloc_free(tmp_ctx);
	return NBT_RCODE_SVR;
}


/*
  connect to the WINS database
*/
NTSTATUS winsdb_init(struct wins_server *winssrv)
{
	winssrv->wins_db = ldb_wrap_connect(winssrv, lp_wins_url(), 0, NULL);
	if (winssrv->wins_db == NULL) {
		return NT_STATUS_INTERNAL_DB_ERROR;
	}

	return NT_STATUS_OK;
}