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 int fetch_int32_parser(TDB_DATA key
, TDB_DATA data
, void *private_data
)
137 if (data
.dsize
== sizeof(int32_t)) {
138 *((int32_t *)private_data
) = PULL_LE_I32(data
.dptr
, 0);
143 static int32_t tdb_fetch_int32_byblob(struct tdb_context
*tdb
, TDB_DATA key
)
146 int32_t ret
= tdb_parse_record(tdb
, key
, fetch_int32_parser
, &v
);
153 /****************************************************************************
154 Fetch a int32_t value by string key, return -1 if not found.
155 Output is int32_t in native byte order.
156 ****************************************************************************/
158 int32_t tdb_fetch_int32(struct tdb_context
*tdb
, const char *keystr
)
160 return tdb_fetch_int32_byblob(tdb
, string_term_tdb_data(keystr
));
163 /****************************************************************************
164 Store a int32_t value by an arbitrary blob key, return 0 on success, -ve on failure.
165 Input is int32_t in native byte order. Output in tdb is in little-endian.
166 ****************************************************************************/
168 static int tdb_store_int32_byblob(struct tdb_context
*tdb
, TDB_DATA key
,
175 data
.dptr
= (unsigned char *)&v_store
;
176 data
.dsize
= sizeof(int32_t);
178 return tdb_store(tdb
, key
, data
, TDB_REPLACE
);
181 /****************************************************************************
182 Store a int32_t value by string key, return 0 on success, -ve on failure.
183 Input is int32_t in native byte order. Output in tdb is in little-endian.
184 ****************************************************************************/
186 int tdb_store_int32(struct tdb_context
*tdb
, const char *keystr
, int32_t v
)
188 return tdb_store_int32_byblob(tdb
, string_term_tdb_data(keystr
), v
);
191 /****************************************************************************
192 Fetch a uint32_t value by a arbitrary blob key, return false if not found.
193 Output is uint32_t in native byte order.
194 ****************************************************************************/
196 static int fetch_uint32_parser(TDB_DATA key
, TDB_DATA data
, void *private_data
)
198 if (data
.dsize
!= sizeof(uint32_t)) {
201 *((uint32_t *)private_data
) = PULL_LE_U32(data
.dptr
, 0);
205 static bool tdb_fetch_uint32_byblob(struct tdb_context
*tdb
, TDB_DATA key
,
208 int ret
= tdb_parse_record(tdb
, key
, fetch_uint32_parser
, value
);
217 /****************************************************************************
218 Fetch a uint32_t value by string key, return false if not found.
219 Output is uint32_t in native byte order.
220 ****************************************************************************/
222 bool tdb_fetch_uint32(struct tdb_context
*tdb
, const char *keystr
, uint32_t *value
)
224 return tdb_fetch_uint32_byblob(tdb
, string_term_tdb_data(keystr
), value
);
227 /****************************************************************************
228 Store a uint32_t value by an arbitrary blob key, return true on success, false on failure.
229 Input is uint32_t in native byte order. Output in tdb is in little-endian.
230 ****************************************************************************/
232 static bool tdb_store_uint32_byblob(struct tdb_context
*tdb
, TDB_DATA key
,
239 SIVAL(&v_store
, 0, value
);
240 data
.dptr
= (unsigned char *)&v_store
;
241 data
.dsize
= sizeof(uint32_t);
243 if (tdb_store(tdb
, key
, data
, TDB_REPLACE
) != 0)
249 /****************************************************************************
250 Store a uint32_t value by string key, return true on success, false on failure.
251 Input is uint32_t in native byte order. Output in tdb is in little-endian.
252 ****************************************************************************/
254 bool tdb_store_uint32(struct tdb_context
*tdb
, const char *keystr
, uint32_t value
)
256 return tdb_store_uint32_byblob(tdb
, string_term_tdb_data(keystr
), value
);
258 /****************************************************************************
259 Store a buffer by a null terminated string key. Return 0 on success, -ve
261 ****************************************************************************/
263 int tdb_store_bystring(struct tdb_context
*tdb
, const char *keystr
, TDB_DATA data
, int flags
)
265 TDB_DATA key
= string_term_tdb_data(keystr
);
267 return tdb_store(tdb
, key
, data
, flags
);
270 /****************************************************************************
271 Fetch a buffer using a null terminated string key. Don't forget to call
272 free() on the result dptr.
273 ****************************************************************************/
275 TDB_DATA
tdb_fetch_bystring(struct tdb_context
*tdb
, const char *keystr
)
277 TDB_DATA key
= string_term_tdb_data(keystr
);
279 return tdb_fetch(tdb
, key
);
282 /****************************************************************************
283 Delete an entry using a null terminated string key.
284 ****************************************************************************/
286 int tdb_delete_bystring(struct tdb_context
*tdb
, const char *keystr
)
288 TDB_DATA key
= string_term_tdb_data(keystr
);
290 return tdb_delete(tdb
, key
);
293 /****************************************************************************
294 Atomic integer change. Returns old value. To create, set initial value in *oldval.
295 ****************************************************************************/
297 int32_t tdb_change_int32_atomic(struct tdb_context
*tdb
, const char *keystr
, int32_t *oldval
, int32_t change_val
)
302 if (tdb_lock_bystring(tdb
, keystr
) != 0)
305 if ((val
= tdb_fetch_int32(tdb
, keystr
)) == -1) {
306 /* The lookup failed */
307 if (tdb_error(tdb
) != TDB_ERR_NOEXIST
) {
308 /* but not because it didn't exist */
312 /* Start with 'old' value */
316 /* It worked, set return value (oldval) to tdb data */
320 /* Increment value for storage and return next time */
323 if (tdb_store_int32(tdb
, keystr
, val
) != 0)
330 tdb_unlock_bystring(tdb
, keystr
);
334 /****************************************************************************
335 Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval.
336 ****************************************************************************/
338 bool tdb_change_uint32_atomic(struct tdb_context
*tdb
, const char *keystr
, uint32_t *oldval
, uint32_t change_val
)
343 if (tdb_lock_bystring(tdb
, keystr
) != 0)
346 if (!tdb_fetch_uint32(tdb
, keystr
, &val
)) {
348 if (tdb_error(tdb
) != TDB_ERR_NOEXIST
) {
349 /* and not because it didn't exist */
353 /* Start with 'old' value */
357 /* it worked, set return value (oldval) to tdb data */
362 /* get a new value to store */
365 if (!tdb_store_uint32(tdb
, keystr
, val
))
372 tdb_unlock_bystring(tdb
, keystr
);
376 /****************************************************************************
377 Return an NTSTATUS from a TDB_ERROR
378 ****************************************************************************/
380 NTSTATUS
map_nt_error_from_tdb(enum TDB_ERROR err
)
386 result
= NT_STATUS_OK
;
388 case TDB_ERR_CORRUPT
:
389 result
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
392 result
= NT_STATUS_UNEXPECTED_IO_ERROR
;
395 result
= NT_STATUS_NO_MEMORY
;
398 result
= NT_STATUS_OBJECT_NAME_COLLISION
;
403 * TDB_ERR_LOCK is very broad, we could for example
404 * distinguish between fcntl locks and invalid lock
405 * sequences. So NT_STATUS_FILE_LOCK_CONFLICT is a
408 result
= NT_STATUS_FILE_LOCK_CONFLICT
;
412 case TDB_ERR_LOCK_TIMEOUT
:
414 * These two ones in the enum are not actually used
416 result
= NT_STATUS_FILE_LOCK_CONFLICT
;
418 case TDB_ERR_NOEXIST
:
419 result
= NT_STATUS_NOT_FOUND
;
422 result
= NT_STATUS_INVALID_PARAMETER
;
425 result
= NT_STATUS_ACCESS_DENIED
;
427 case TDB_ERR_NESTING
:
428 result
= NT_STATUS_INTERNAL_ERROR
;
431 result
= NT_STATUS_INTERNAL_ERROR
;
437 int map_unix_error_from_tdb(enum TDB_ERROR err
)
445 case TDB_ERR_CORRUPT
:
460 * TDB_ERR_LOCK is very broad, we could for example
461 * distinguish between fcntl locks and invalid lock
462 * sequences. EWOULDBLOCK is wrong, but there is no real
463 * generic lock error code in errno.h
465 result
= EWOULDBLOCK
;
469 case TDB_ERR_LOCK_TIMEOUT
:
471 * These two ones in the enum are not actually used
475 case TDB_ERR_NOEXIST
:
484 case TDB_ERR_NESTING
:
486 * Well, this db is already busy...
494 struct tdb_fetch_talloc_state
{
499 static int tdb_fetch_talloc_parser(TDB_DATA key
, TDB_DATA data
,
502 struct tdb_fetch_talloc_state
*state
= private_data
;
503 state
->buf
= talloc_memdup(state
->mem_ctx
, data
.dptr
, data
.dsize
);
507 int tdb_fetch_talloc(struct tdb_context
*tdb
, TDB_DATA key
,
508 TALLOC_CTX
*mem_ctx
, uint8_t **buf
)
510 struct tdb_fetch_talloc_state state
= { .mem_ctx
= mem_ctx
};
513 ret
= tdb_parse_record(tdb
, key
, tdb_fetch_talloc_parser
, &state
);
515 enum TDB_ERROR err
= tdb_error(tdb
);
516 return map_unix_error_from_tdb(err
);
519 if (state
.buf
== NULL
) {