summaryrefslogtreecommitdiff
path: root/source3/lib/tdb_multikey.c
blob: 77e63c5aaa48f82553c250201447600c5608199a (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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/* 
 *  Unix SMB/CIFS implementation.
 *  TDB multi-key wrapper
 *  Copyright (C) Volker Lendecke 2006
 *  
 *  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"

static struct { enum TDB_ERROR t; NTSTATUS n; } tdb_to_ntstatus_map[] = {
	{ TDB_ERR_CORRUPT, NT_STATUS_INTERNAL_DB_CORRUPTION },
	{ TDB_ERR_IO, NT_STATUS_UNEXPECTED_IO_ERROR },
	{ TDB_ERR_LOCK, NT_STATUS_FILE_LOCK_CONFLICT },
	{ TDB_ERR_OOM, NT_STATUS_NO_MEMORY },
	{ TDB_ERR_EXISTS, NT_STATUS_OBJECTID_EXISTS },
	{ TDB_ERR_NOLOCK, NT_STATUS_NOT_LOCKED },
	{ TDB_ERR_LOCK_TIMEOUT, NT_STATUS_IO_TIMEOUT },
	{ TDB_ERR_NOEXIST, NT_STATUS_NOT_FOUND },
	{ TDB_ERR_EINVAL, NT_STATUS_INVALID_PARAMETER },
	{ TDB_ERR_RDONLY, NT_STATUS_ACCESS_DENIED },
	{ 0, NT_STATUS_OK },
};	

static NTSTATUS map_ntstatus_from_tdb(struct tdb_context *t)
{
	enum TDB_ERROR err = tdb_error(t);
	int i = 0;

	while (tdb_to_ntstatus_map[i].t != 0) {
		if (tdb_to_ntstatus_map[i].t == err) {
			return tdb_to_ntstatus_map[i].n;
		}
		i += 1;
	}

	return NT_STATUS_INTERNAL_ERROR;
}

#define KEY_VERSION (1)
#define PRIMARY_KEY_LENGTH (24)

/*
 * Check that the keying version is acceptable. Change operations are very
 * expensive under transactions anyway, so we do this upon every change to
 * avoid damage when someone changes the key format while we have the db open.
 *
 * To be called only within a transaction, we don't do locking here.
 */

static BOOL tdb_check_keyversion(struct tdb_context *tdb)
{
	const char *versionkey = "KEYVERSION";
	TDB_DATA key, data;
	NTSTATUS status;
	unsigned long version;
	char *endptr;

	key.dptr = CONST_DISCARD(char *, versionkey);
	key.dsize = strlen(versionkey)+1;

	data = tdb_fetch(tdb, key);
	if (data.dptr == NULL) {
		char *vstr;
		int res;

		asprintf(&vstr, "%d", KEY_VERSION);
		if (vstr == NULL) {
			DEBUG(0, ("asprintf failed\n"));
			return False;
		}
		data.dptr = vstr;
		data.dsize = strlen(vstr)+1;

		res = tdb_store(tdb, key, data, TDB_INSERT);
		SAFE_FREE(vstr);

		if (res < 0) {
			status = map_ntstatus_from_tdb(tdb);
			DEBUG(5, ("Could not store key: %s\n",
				  nt_errstr(status)));
			return False;
		}

		return True;
	}

	/*
	 * We have a key, check it
	 */

	SMB_ASSERT(data.dsize > 0);
	if (data.dptr[data.dsize-1] != '\0') {
		DEBUG(1, ("Key field not NUL terminated\n"));
		SAFE_FREE(data.dptr);
		return False;
	}

	version = strtoul(data.dptr, &endptr, 10);
	if (endptr != data.dptr+data.dsize-1) {
		DEBUG(1, ("Invalid version string\n"));
		SAFE_FREE(data.dptr);
		return False;
	}
	SAFE_FREE(data.dptr);

	if (version != KEY_VERSION) {
		DEBUG(1, ("Wrong key version: %ld, expected %d\n",
			  version, KEY_VERSION));
		return False;
	}

	return True;
}

/*
 * Find a record according to a key and value expected in that key. The
 * primary_key is returned for later reference in tdb_idx_update or
 * tdb_idx_delete.
 */

NTSTATUS tdb_find_keyed(TALLOC_CTX *ctx, struct tdb_context *tdb,
			int keynumber, const char *value,
			TDB_DATA *result, char **primary_key)
{
	TDB_DATA key, prim, data;
	NTSTATUS status;

	prim.dptr = data.dptr = NULL;

	key.dptr = talloc_asprintf(ctx, "KEY/%d/%s", keynumber, value);
	if (key.dptr == NULL) {
		DEBUG(0, ("talloc_asprintf failed\n"));
		status = NT_STATUS_NO_MEMORY;
		goto fail;
	}
	key.dsize = strlen(key.dptr)+1;

	prim = tdb_fetch(tdb, key);
	if (prim.dptr == NULL) {
		status = NT_STATUS_NOT_FOUND;
		goto fail;
	}

	data = tdb_fetch(tdb, prim);
	if (data.dptr == NULL) {
		DEBUG(1, ("Did not find record %s for key %s\n",
			  prim.dptr, key.dptr));
		status = NT_STATUS_INTERNAL_DB_CORRUPTION;
		goto fail;
	}

	if (primary_key != NULL) {
		*primary_key = talloc_strndup(ctx, prim.dptr, prim.dsize);
		if (*primary_key == NULL) {
			status = NT_STATUS_NO_MEMORY;
			goto fail;
		}
	}
	
	/*
	 * The following copy will be removed when tdb_fetch takes a
	 * TALLOC_CTX as parameter.
	 */

	result->dptr = (char *)talloc_memdup(ctx, data.dptr, data.dsize);
	if (result->dptr == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto fail;
	}
	result->dsize = data.dsize;

	status = NT_STATUS_OK;

 fail:
	TALLOC_FREE(key.dptr);
	SAFE_FREE(prim.dptr);
	SAFE_FREE(data.dptr);
	return status;
}

/*
 * Store all the key entries for a data entry. Best called within a tdb
 * transaction.
 */

static NTSTATUS set_keys(struct tdb_context *tdb,
			 char **(*getkeys)(TALLOC_CTX *mem_ctx, TDB_DATA data,
					   void *private_data),
			 TDB_DATA primary_key, TDB_DATA user_data,
			 void *private_data)
{
	int i;
	char **keys = getkeys(NULL, user_data, private_data);

	if (keys == NULL) {
		DEBUG(5, ("Could not get keys\n"));
		return NT_STATUS_NO_MEMORY;
	}

	for (i=0; keys[i] != NULL; i++) {
		NTSTATUS status;
		TDB_DATA key;

		key.dptr = talloc_asprintf(keys, "KEY/%d/%s", i, keys[i]);
		if (key.dptr == NULL) {
			DEBUG(0, ("talloc_asprintf failed\n"));
			TALLOC_FREE(keys);
			return NT_STATUS_NO_MEMORY;
		}
		key.dsize = strlen(key.dptr)+1;

		if (tdb_store(tdb, key, primary_key, TDB_INSERT) < 0) {
			status = map_ntstatus_from_tdb(tdb);
			DEBUG(5, ("Could not store key %d: %s\n", i,
				  nt_errstr(status)));
			TALLOC_FREE(keys);
			return status;
		}
	}

	TALLOC_FREE(keys);
	return NT_STATUS_OK;
}

/*
 * Delete all the key entries for a data entry. Best called within a tdb
 * transaction.
 */

static NTSTATUS del_keys(struct tdb_context *tdb,
			 char **(*getkeys)(TALLOC_CTX *mem_ctx, TDB_DATA data,
					   void *private_data),
			 TDB_DATA primary_key, void *private_data)
{
	TDB_DATA data;
	int i;
	char **keys;

	/*
	 * We need the data record to be able to fetch all the keys, so pull
	 * the user data
	 */

	data = tdb_fetch(tdb, primary_key);
	if (data.dptr == NULL) {
		DEBUG(5, ("Could not find record for key %s\n",
			  primary_key.dptr));
		return NT_STATUS_NOT_FOUND;
	}

	keys = getkeys(NULL, data, private_data);
	if (keys == NULL) {
		DEBUG(5, ("Could not get keys\n"));
		return NT_STATUS_NO_MEMORY;
	}

	SAFE_FREE(data.dptr);

	for (i=0; keys[i] != NULL; i++) {
		NTSTATUS status;
		TDB_DATA key;

		key.dptr = talloc_asprintf(keys, "KEY/%d/%s", i, keys[i]);
		if (key.dptr == NULL) {
			DEBUG(0, ("talloc_asprintf failed\n"));
			TALLOC_FREE(keys);
			return NT_STATUS_NO_MEMORY;
		}
		key.dsize = strlen(key.dptr)+1;

		if (tdb_delete(tdb, key) < 0) {
			status = map_ntstatus_from_tdb(tdb);
			DEBUG(5, ("Could not delete key %d: %s\n", i,
				  nt_errstr(status)));
			TALLOC_FREE(keys);
			return status;
		}
	}

	TALLOC_FREE(keys);
	return NT_STATUS_OK;
}

/*
 * Generate a unique primary key
 */

static TDB_DATA new_primary_key(struct tdb_context *tdb)
{
	TDB_DATA key;
	int i;

	/*
	 * Generate a new primary key, the for loop is for the very unlikely
	 * collisions.
	 */

	for (i=0; i<20; i++) {
		TDB_DATA data;
		asprintf(&key.dptr, "KEYPRIM/%s", generate_random_str(16));
		if (key.dptr == NULL) {
			DEBUG(0, ("talloc_asprintf failed\n"));
			return key;
		}

#ifdef DEVELOPER
		SMB_ASSERT(strlen(key.dptr) == PRIMARY_KEY_LENGTH);
#endif
		key.dsize = PRIMARY_KEY_LENGTH+1;

		data = tdb_fetch(tdb, key);
		if (data.dptr == NULL) {
			return key;
		}
		SAFE_FREE(key.dptr);
		SAFE_FREE(data.dptr);
	}

	DEBUG(0, ("Did not find a unique key string!\n"));
	key.dptr = NULL;
	key.dsize = 0;
	return key;
}

/*
 * Add a new record to the database
 */

NTSTATUS tdb_add_keyed(struct tdb_context *tdb,
		       char **(*getkeys)(TALLOC_CTX *mem_ctx, TDB_DATA data,
					 void *private_data),
		       TDB_DATA data, void *private_data)
{
	NTSTATUS status = NT_STATUS_OK;
	TDB_DATA key;

	key.dptr = NULL;

	if (tdb_transaction_start(tdb) < 0) {
		status = map_ntstatus_from_tdb(tdb);
		DEBUG(5, ("Could not start transaction: %s\n",
			  nt_errstr(status)));
		return status;
	}

	if (!tdb_check_keyversion(tdb)) {
		status = NT_STATUS_INTERNAL_DB_CORRUPTION;
		goto fail;
	}

	key = new_primary_key(tdb);
	if (key.dptr == NULL) {
		status = NT_STATUS_NO_MEMORY;
		goto fail;
	}

	if (tdb_store(tdb, key, data, TDB_INSERT) < 0) {
		status = map_ntstatus_from_tdb(tdb);
		DEBUG(5, ("Could not store record: %s\n", nt_errstr(status)));
		goto fail;
	}

	status = set_keys(tdb, getkeys, key, data, private_data);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(5, ("set_keys failed: %s\n", nt_errstr(status)));
		goto fail;
	}

	if (tdb_transaction_commit(tdb) < 0) {
		status = map_ntstatus_from_tdb(tdb);
		DEBUG(5, ("tdb_transaction_commit failed: %s\n",
			  nt_errstr(status)));
		goto fail;
	}

	SAFE_FREE(key.dptr);
	return NT_STATUS_OK;

 fail:
	if (tdb_transaction_cancel(tdb) < 0) {
		smb_panic("tdb_cancel_transaction failed\n");
	}

	SAFE_FREE(key.dptr);
	return status;
}

/*
 * Delete a record from the database, given its primary key
 */

NTSTATUS tdb_del_keyed(struct tdb_context *tdb,
		       char **(*getkeys)(TALLOC_CTX *mem_ctx, TDB_DATA data,
					 void *private_data),
		       const char *primary_key, void *private_data)
{
	NTSTATUS status = NT_STATUS_OK;
	TDB_DATA key;

	if ((primary_key == NULL) ||
	    (strlen(primary_key) != PRIMARY_KEY_LENGTH) ||
	    (strncmp(primary_key, "KEYPRIM/", 7) != 0)) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	if (tdb_transaction_start(tdb) < 0) {
		status = map_ntstatus_from_tdb(tdb);
		DEBUG(5, ("Could not start transaction: %s\n",
			  nt_errstr(status)));
		return status;
	}

	if (!tdb_check_keyversion(tdb)) {
		status = NT_STATUS_INTERNAL_DB_CORRUPTION;
		goto fail;
	}

	key.dptr = CONST_DISCARD(char *, primary_key);
	key.dsize = PRIMARY_KEY_LENGTH+1;

	status = del_keys(tdb, getkeys, key, private_data);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(0, ("del_keys failed: %s\n", nt_errstr(status)));
		goto fail;
	}

	if (tdb_delete(tdb, key) < 0) {
		DEBUG(5, ("Could not delete record %s\n", primary_key));
		status = NT_STATUS_INTERNAL_DB_CORRUPTION;
		goto fail;
	}

	if (tdb_transaction_commit(tdb) < 0) {
		status = map_ntstatus_from_tdb(tdb);
		DEBUG(5, ("tdb_transaction_commit failed: %s\n",
			  nt_errstr(status)));
		goto fail;
	}

	return NT_STATUS_OK;

 fail:
	if (tdb_transaction_cancel(tdb) < 0) {
		smb_panic("tdb_cancel_transaction failed\n");
	}

	return status;
}

/*
 * Update a record that has previously been fetched and then changed.
 */

NTSTATUS tdb_update_keyed(struct tdb_context *tdb, const char *primary_key,
			  char **(*getkeys)(TALLOC_CTX *mem_ctx,
					    TDB_DATA data, void *private_data),
			  TDB_DATA data, void *private_data)
{
	NTSTATUS status = NT_STATUS_OK;
	TDB_DATA key;

	if ((primary_key == NULL) ||
	    (strlen(primary_key) != PRIMARY_KEY_LENGTH) ||
	    (strncmp(primary_key, "KEYPRIM/", 7) != 0)) {
		return NT_STATUS_INVALID_PARAMETER;
	}

	if (tdb_transaction_start(tdb) < 0) {
		status = map_ntstatus_from_tdb(tdb);
		DEBUG(5, ("Could not start transaction: %s\n",
			  nt_errstr(status)));
		return status;
	}

	if (!tdb_check_keyversion(tdb)) {
		status = NT_STATUS_INTERNAL_DB_CORRUPTION;
		goto fail;
	}

	key.dptr = CONST_DISCARD(char *, primary_key);
	key.dsize = PRIMARY_KEY_LENGTH+1;

	status = del_keys(tdb, getkeys, key, private_data);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(5, ("del_keys failed: %s\n", nt_errstr(status)));
		goto fail;
	}

	if (tdb_store(tdb, key, data, TDB_REPLACE) < 0) {
		status = map_ntstatus_from_tdb(tdb);
		DEBUG(5, ("Could not store new record: %s\n",
			  nt_errstr(status)));
		goto fail;
	}

	status = set_keys(tdb, getkeys, key, data, private_data);
	if (!NT_STATUS_IS_OK(status)) {
		DEBUG(5, ("set_keys failed: %s\n", nt_errstr(status)));
		goto fail;
	}

	if (tdb_transaction_commit(tdb) < 0) {
		status = map_ntstatus_from_tdb(tdb);
		DEBUG(5, ("tdb_transaction_commit failed: %s\n",
			  nt_errstr(status)));
		goto fail;
	}

	return NT_STATUS_OK;

 fail:
	if (tdb_transaction_cancel(tdb) < 0) {
		smb_panic("tdb_cancel_transaction failed\n");
	}

	return status;
}