summaryrefslogtreecommitdiff
path: root/lib/util/server_id_db.c
blob: e190f457526051d1637f5cb8f4f1b04f4cb1b16e (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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
 * Map names to server_ids
 *
 * Copyright Volker Lendecke <vl@samba.org> 2014
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

#include "replace.h"
#include "system/filesys.h"
#include "lib/util/server_id.h"
#include "lib/util/server_id_db.h"
#include "lib/tdb_wrap/tdb_wrap.h"
#include "lib/util/strv.h"
#include "lib/util/util_tdb.h"
#include "lib/util/samba_util.h"

static TDB_DATA talloc_tdb_data(void *ptr)
{
	return (TDB_DATA) { .dptr = ptr, .dsize = talloc_get_size(ptr) };
}

struct server_id_db {
	struct server_id pid;
	struct tdb_wrap *tdb;
	char *names;
};

static int server_id_db_destructor(struct server_id_db *db);

struct server_id_db *server_id_db_init(TALLOC_CTX *mem_ctx,
				       struct server_id pid,
				       const char *base_path,
				       int hash_size, int tdb_flags)
{
	struct server_id_db *db;
	size_t pathlen = strlen(base_path) + 11;
	char path[pathlen];

	db = talloc(mem_ctx, struct server_id_db);
	if (db == NULL) {
		return NULL;
	}
	db->pid = pid;
	db->names = NULL;

	snprintf(path, pathlen, "%s/names.tdb", base_path);

	db->tdb = tdb_wrap_open(db, path, hash_size, tdb_flags,
				O_RDWR|O_CREAT, 0660);
	if (db->tdb == NULL) {
		TALLOC_FREE(db);
		return NULL;
	}

	talloc_set_destructor(db, server_id_db_destructor);

	return db;
}

void server_id_db_reinit(struct server_id_db *db, struct server_id pid)
{
	db->pid = pid;
	TALLOC_FREE(db->names);
}

struct server_id server_id_db_pid(struct server_id_db *db)
{
	return db->pid;
}

static int server_id_db_destructor(struct server_id_db *db)
{
	char *name = NULL;

	while ((name = strv_next(db->names, name)) != NULL) {
		server_id_db_remove(db, name);
	}

	return 0;
}

int server_id_db_add(struct server_id_db *db, const char *name)
{
	struct tdb_context *tdb = db->tdb->tdb;
	TDB_DATA key;
	char *n;
	int ret;

	n = strv_find(db->names, name);
	if (n != NULL) {
		return EEXIST;
	}

	ret = strv_add(db, &db->names, name);
	if (ret != 0) {
		return ret;
	}

	key = string_term_tdb_data(name);

	{
		size_t idlen = server_id_str_buf_unique(db->pid, NULL, 0);
		char idbuf[idlen];

		server_id_str_buf_unique(db->pid, idbuf, idlen);

		ret = tdb_append(
			tdb, key,
			(TDB_DATA) { .dptr = (uint8_t *)idbuf, .dsize = idlen });
	}

	if (ret != 0) {
		enum TDB_ERROR err = tdb_error(tdb);
		strv_delete(&db->names, strv_find(db->names, name));
		return map_unix_error_from_tdb(err);
	}

	return 0;
}

int server_id_db_prune_name(struct server_id_db *db, const char *name,
			    struct server_id server)
{
	struct tdb_context *tdb = db->tdb->tdb;
	size_t idbuf_len = server_id_str_buf_unique(server, NULL, 0);
	char idbuf[idbuf_len];
	TDB_DATA key;
	uint8_t *data;
	size_t datalen;
	char *ids, *id;
	int ret;

	key = string_term_tdb_data(name);
	server_id_str_buf_unique(server, idbuf, idbuf_len);

	ret = tdb_chainlock(tdb, key);
	if (ret == -1) {
		enum TDB_ERROR err = tdb_error(tdb);
		return map_unix_error_from_tdb(err);
	}

	ret = tdb_fetch_talloc(tdb, key, db, &data);
	if (ret != 0) {
		tdb_chainunlock(tdb, key);
		return ret;
	}

	datalen = talloc_get_size(data);
	if ((datalen == 0) || (data[datalen-1] != '\0')) {
		tdb_chainunlock(tdb, key);
		TALLOC_FREE(data);
		return EINVAL;
	}

	ids = (char *)data;

	id = strv_find(ids, idbuf);
	if (id == NULL) {
		tdb_chainunlock(tdb, key);
		TALLOC_FREE(data);
		return ENOENT;
	}

	strv_delete(&ids, id);

	if (talloc_get_size(ids) == 0) {
		ret = tdb_delete(tdb, key);
	} else {
		ret = tdb_store(tdb, key, talloc_tdb_data(ids), TDB_MODIFY);
	}
	TALLOC_FREE(data);

	tdb_chainunlock(tdb, key);

	return 0;
}

int server_id_db_remove(struct server_id_db *db, const char *name)
{
	char *n;
	int ret;

	n = strv_find(db->names, name);
	if (n == NULL) {
		return ENOENT;
	}

	ret = server_id_db_prune_name(db, name, db->pid);
	if (ret != 0) {
		return ret;
	}

	strv_delete(&db->names, n);
	return 0;
}

int server_id_db_lookup(struct server_id_db *db, const char *name,
			TALLOC_CTX *mem_ctx, unsigned *pnum_servers,
			struct server_id **pservers)
{
	struct tdb_context *tdb = db->tdb->tdb;
	TDB_DATA key;
	uint8_t *data;
	size_t datalen;
	char *ids, *id;
	unsigned num_servers;
	struct server_id *servers;
	int i, ret;

	key = string_term_tdb_data(name);

	ret = tdb_fetch_talloc(tdb, key, mem_ctx, &data);
	if (ret != 0) {
		return ret;
	}

	datalen = talloc_get_size(data);
	if ((datalen == 0) || (data[datalen-1] != '\0')) {
		TALLOC_FREE(data);
		return EINVAL;
	}

	ids = (char *)data;
	num_servers = strv_count(ids);

	servers = talloc_array(mem_ctx, struct server_id, num_servers);
	if (servers == NULL) {
		TALLOC_FREE(data);
		return ENOMEM;
	}

	i = 0;

	for (id = ids; id != NULL; id = strv_next(ids, id)) {
		servers[i++] = server_id_from_string(NONCLUSTER_VNN, id);
	}

	TALLOC_FREE(data);

	*pnum_servers = num_servers;
	*pservers = servers;

	return 0;
}

bool server_id_db_lookup_one(struct server_id_db *db, const char *name,
			     struct server_id *server)
{
	int ret;
	unsigned num_servers;
	struct server_id *servers;

	ret = server_id_db_lookup(db, name, db, &num_servers, &servers);
	if (ret != 0) {
		return false;
	}
	if (num_servers == 0) {
		TALLOC_FREE(servers);
		return false;
	}
	*server = servers[0];
	TALLOC_FREE(servers);
	return true;
}

struct server_id_db_traverse_state {
	TALLOC_CTX *mem_ctx;
	int (*fn)(const char *name,
		  unsigned num_servers,
		  const struct server_id *servers,
		  void *private_data);
	void *private_data;
};

static int server_id_db_traverse_fn(struct tdb_context *tdb,
				    TDB_DATA key, TDB_DATA data,
				    void *private_data)
{
	struct server_id_db_traverse_state *state = private_data;
	const char *name;
	char *ids, *id;
	unsigned num_servers;
	struct server_id *servers;
	int i, ret;

	if (key.dsize == 0) {
		return 0;
	}
	if (key.dptr[key.dsize-1] != '\0') {
		return 0;
	}
	name = (const char *)key.dptr;

	ids = (char *)talloc_memdup(state->mem_ctx, data.dptr, data.dsize);
	if (ids == NULL) {
		return 0;
	}

	num_servers = strv_count(ids);
	servers = talloc_array(ids, struct server_id, num_servers);

	i = 0;

	for (id = ids; id != NULL; id = strv_next(ids, id)) {
		servers[i++] = server_id_from_string(NONCLUSTER_VNN, id);
	}

	ret = state->fn(name, num_servers, servers, state->private_data);

	TALLOC_FREE(ids);

	return ret;
}

int server_id_db_traverse_read(struct server_id_db *db,
			       int (*fn)(const char *name,
					 unsigned num_servers,
					 const struct server_id *servers,
					 void *private_data),
			       void *private_data)
{
	struct server_id_db_traverse_state state;
	int ret;

	state = (struct server_id_db_traverse_state) {
		.fn = fn, .private_data = private_data,
		.mem_ctx = talloc_new(db)
	};

	if (state.mem_ctx == NULL) {
		return ENOMEM;
	}

	ret = tdb_traverse_read(db->tdb->tdb, server_id_db_traverse_fn,
				&state);
	TALLOC_FREE(state.mem_ctx);
	return ret;
}