2 Unix SMB/CIFS implementation.
6 Copyright (C) Andrew Tridgell 1992-2006
7 Copyright (C) Volker Lendecke 2007-2011
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "libcli/util/ntstatus.h"
26 #include "lib/util/memory.h"
27 #include "lib/util/byteorder.h"
28 #include "system/filesys.h"
29 #include "../lib/tdb/include/tdb.h"
30 #include "../lib/util/util_tdb.h"
32 /* these are little tdb utility functions that are meant to make
33 dealing with a tdb database a little less cumbersome in Samba */
35 /***************************************************************
36 Make a TDB_DATA and keep the const warning in one place
37 ****************************************************************/
39 TDB_DATA make_tdb_data(const uint8_t *dptr, size_t dsize)
42 ret.dptr = discard_const_p(uint8_t, dptr);
47 bool tdb_data_equal(TDB_DATA t1, TDB_DATA t2)
49 if (t1.dsize != t2.dsize) {
52 return (memcmp(t1.dptr, t2.dptr, t1.dsize) == 0);
55 bool tdb_data_is_empty(TDB_DATA d)
57 return (d.dsize == 0) || (d.dptr == NULL);
60 TDB_DATA string_tdb_data(const char *string)
62 return make_tdb_data((const uint8_t *)string, string ? strlen(string) : 0 );
65 TDB_DATA string_term_tdb_data(const char *string)
67 return make_tdb_data((const uint8_t *)string, string ? strlen(string) + 1 : 0);
70 TDB_DATA tdb_data_talloc_copy(TALLOC_CTX* mem_ctx, TDB_DATA data) {
72 .dptr = (uint8_t *)talloc_size(mem_ctx, data.dsize+1),
75 if (ret.dptr == NULL) {
78 memcpy(ret.dptr, data.dptr, data.dsize);
79 ret.dptr[ret.dsize] = '\0';
85 /****************************************************************************
86 Lock a chain by string. Return non-zero if lock failed.
87 ****************************************************************************/
89 int tdb_lock_bystring(struct tdb_context *tdb, const char *keyval)
91 TDB_DATA key = string_term_tdb_data(keyval);
93 return tdb_chainlock(tdb, key);
96 /****************************************************************************
97 Unlock a chain by string.
98 ****************************************************************************/
100 void tdb_unlock_bystring(struct tdb_context *tdb, const char *keyval)
102 TDB_DATA key = string_term_tdb_data(keyval);
104 tdb_chainunlock(tdb, key);
107 /****************************************************************************
108 Read lock a chain by string. Return non-zero if lock failed.
109 ****************************************************************************/
111 int tdb_read_lock_bystring(struct tdb_context *tdb, const char *keyval)
113 TDB_DATA key = string_term_tdb_data(keyval);
115 return tdb_chainlock_read(tdb, key);
118 /****************************************************************************
119 Read unlock a chain by string.
120 ****************************************************************************/
122 void tdb_read_unlock_bystring(struct tdb_context *tdb, const char *keyval)
124 TDB_DATA key = string_term_tdb_data(keyval);
126 tdb_chainunlock_read(tdb, key);
130 /****************************************************************************
131 Fetch a int32_t value by a arbitrary blob key, return -1 if not found.
132 Output is int32_t in native byte order.
133 ****************************************************************************/
135 static int32_t tdb_fetch_int32_byblob(struct tdb_context *tdb, TDB_DATA key)
140 data = tdb_fetch(tdb, key);
141 if (!data.dptr || data.dsize != sizeof(int32_t)) {
142 SAFE_FREE(data.dptr);
146 ret = IVAL(data.dptr,0);
147 SAFE_FREE(data.dptr);
151 /****************************************************************************
152 Fetch a int32_t value by string key, return -1 if not found.
153 Output is int32_t in native byte order.
154 ****************************************************************************/
156 int32_t tdb_fetch_int32(struct tdb_context *tdb, const char *keystr)
158 return tdb_fetch_int32_byblob(tdb, string_term_tdb_data(keystr));
161 /****************************************************************************
162 Store a int32_t value by an arbitrary blob key, return 0 on success, -ve on failure.
163 Input is int32_t in native byte order. Output in tdb is in little-endian.
164 ****************************************************************************/
166 static int tdb_store_int32_byblob(struct tdb_context *tdb, TDB_DATA key,
173 data.dptr = (unsigned char *)&v_store;
174 data.dsize = sizeof(int32_t);
176 return tdb_store(tdb, key, data, TDB_REPLACE);
179 /****************************************************************************
180 Store a int32_t value by string key, return 0 on success, -ve on failure.
181 Input is int32_t in native byte order. Output in tdb is in little-endian.
182 ****************************************************************************/
184 int tdb_store_int32(struct tdb_context *tdb, const char *keystr, int32_t v)
186 return tdb_store_int32_byblob(tdb, string_term_tdb_data(keystr), v);
189 /****************************************************************************
190 Fetch a uint32_t value by a arbitrary blob key, return false if not found.
191 Output is uint32_t in native byte order.
192 ****************************************************************************/
194 static bool tdb_fetch_uint32_byblob(struct tdb_context *tdb, TDB_DATA key,
199 data = tdb_fetch(tdb, key);
200 if (!data.dptr || data.dsize != sizeof(uint32_t)) {
201 SAFE_FREE(data.dptr);
205 *value = IVAL(data.dptr,0);
206 SAFE_FREE(data.dptr);
210 /****************************************************************************
211 Fetch a uint32_t value by string key, return false if not found.
212 Output is uint32_t in native byte order.
213 ****************************************************************************/
215 bool tdb_fetch_uint32(struct tdb_context *tdb, const char *keystr, uint32_t *value)
217 return tdb_fetch_uint32_byblob(tdb, string_term_tdb_data(keystr), value);
220 /****************************************************************************
221 Store a uint32_t value by an arbitrary blob key, return true on success, false on failure.
222 Input is uint32_t in native byte order. Output in tdb is in little-endian.
223 ****************************************************************************/
225 static bool tdb_store_uint32_byblob(struct tdb_context *tdb, TDB_DATA key,
232 SIVAL(&v_store, 0, value);
233 data.dptr = (unsigned char *)&v_store;
234 data.dsize = sizeof(uint32_t);
236 if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
242 /****************************************************************************
243 Store a uint32_t value by string key, return true on success, false on failure.
244 Input is uint32_t in native byte order. Output in tdb is in little-endian.
245 ****************************************************************************/
247 bool tdb_store_uint32(struct tdb_context *tdb, const char *keystr, uint32_t value)
249 return tdb_store_uint32_byblob(tdb, string_term_tdb_data(keystr), value);
251 /****************************************************************************
252 Store a buffer by a null terminated string key. Return 0 on success, -ve
254 ****************************************************************************/
256 int tdb_store_bystring(struct tdb_context *tdb, const char *keystr, TDB_DATA data, int flags)
258 TDB_DATA key = string_term_tdb_data(keystr);
260 return tdb_store(tdb, key, data, flags);
263 /****************************************************************************
264 Fetch a buffer using a null terminated string key. Don't forget to call
265 free() on the result dptr.
266 ****************************************************************************/
268 TDB_DATA tdb_fetch_bystring(struct tdb_context *tdb, const char *keystr)
270 TDB_DATA key = string_term_tdb_data(keystr);
272 return tdb_fetch(tdb, key);
275 /****************************************************************************
276 Delete an entry using a null terminated string key.
277 ****************************************************************************/
279 int tdb_delete_bystring(struct tdb_context *tdb, const char *keystr)
281 TDB_DATA key = string_term_tdb_data(keystr);
283 return tdb_delete(tdb, key);
286 /****************************************************************************
287 Atomic integer change. Returns old value. To create, set initial value in *oldval.
288 ****************************************************************************/
290 int32_t tdb_change_int32_atomic(struct tdb_context *tdb, const char *keystr, int32_t *oldval, int32_t change_val)
295 if (tdb_lock_bystring(tdb, keystr) != 0)
298 if ((val = tdb_fetch_int32(tdb, keystr)) == -1) {
299 /* The lookup failed */
300 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
301 /* but not because it didn't exist */
305 /* Start with 'old' value */
309 /* It worked, set return value (oldval) to tdb data */
313 /* Increment value for storage and return next time */
316 if (tdb_store_int32(tdb, keystr, val) != 0)
323 tdb_unlock_bystring(tdb, keystr);
327 /****************************************************************************
328 Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval.
329 ****************************************************************************/
331 bool tdb_change_uint32_atomic(struct tdb_context *tdb, const char *keystr, uint32_t *oldval, uint32_t change_val)
336 if (tdb_lock_bystring(tdb, keystr) != 0)
339 if (!tdb_fetch_uint32(tdb, keystr, &val)) {
341 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
342 /* and not because it didn't exist */
346 /* Start with 'old' value */
350 /* it worked, set return value (oldval) to tdb data */
355 /* get a new value to store */
358 if (!tdb_store_uint32(tdb, keystr, val))
365 tdb_unlock_bystring(tdb, keystr);
369 /****************************************************************************
370 Allow tdb_delete to be used as a tdb_traversal_fn.
371 ****************************************************************************/
373 int tdb_traverse_delete_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf,
376 return tdb_delete(the_tdb, key);
379 /****************************************************************************
380 Return an NTSTATUS from a TDB_ERROR
381 ****************************************************************************/
383 NTSTATUS map_nt_error_from_tdb(enum TDB_ERROR err)
389 result = NT_STATUS_OK;
391 case TDB_ERR_CORRUPT:
392 result = NT_STATUS_INTERNAL_DB_CORRUPTION;
395 result = NT_STATUS_UNEXPECTED_IO_ERROR;
398 result = NT_STATUS_NO_MEMORY;
401 result = NT_STATUS_OBJECT_NAME_COLLISION;
406 * TDB_ERR_LOCK is very broad, we could for example
407 * distinguish between fcntl locks and invalid lock
408 * sequences. So NT_STATUS_FILE_LOCK_CONFLICT is a
411 result = NT_STATUS_FILE_LOCK_CONFLICT;
415 case TDB_ERR_LOCK_TIMEOUT:
417 * These two ones in the enum are not actually used
419 result = NT_STATUS_FILE_LOCK_CONFLICT;
421 case TDB_ERR_NOEXIST:
422 result = NT_STATUS_NOT_FOUND;
425 result = NT_STATUS_INVALID_PARAMETER;
428 result = NT_STATUS_ACCESS_DENIED;
430 case TDB_ERR_NESTING:
431 result = NT_STATUS_INTERNAL_ERROR;
434 result = NT_STATUS_INTERNAL_ERROR;
440 int map_unix_error_from_tdb(enum TDB_ERROR err)
448 case TDB_ERR_CORRUPT:
463 * TDB_ERR_LOCK is very broad, we could for example
464 * distinguish between fcntl locks and invalid lock
465 * sequences. EWOULDBLOCK is wrong, but there is no real
466 * generic lock error code in errno.h
468 result = EWOULDBLOCK;
472 case TDB_ERR_LOCK_TIMEOUT:
474 * These two ones in the enum are not actually used
478 case TDB_ERR_NOEXIST:
487 case TDB_ERR_NESTING:
489 * Well, this db is already busy...
497 struct tdb_fetch_talloc_state {
502 static int tdb_fetch_talloc_parser(TDB_DATA key, TDB_DATA data,
505 struct tdb_fetch_talloc_state *state = private_data;
506 state->buf = talloc_memdup(state->mem_ctx, data.dptr, data.dsize);
510 int tdb_fetch_talloc(struct tdb_context *tdb, TDB_DATA key,
511 TALLOC_CTX *mem_ctx, uint8_t **buf)
513 struct tdb_fetch_talloc_state state = { .mem_ctx = mem_ctx };
516 ret = tdb_parse_record(tdb, key, tdb_fetch_talloc_parser, &state);
518 enum TDB_ERROR err = tdb_error(tdb);
519 return map_unix_error_from_tdb(err);
522 if (state.buf == NULL) {