1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 |
|
---|
4 | tdb utility functions
|
---|
5 |
|
---|
6 | Copyright (C) Andrew Tridgell 1992-2006
|
---|
7 | Copyright (C) Volker Lendecke 2007-2011
|
---|
8 |
|
---|
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.
|
---|
13 |
|
---|
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.
|
---|
18 |
|
---|
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/>.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include "includes.h"
|
---|
24 | #include "system/filesys.h"
|
---|
25 | #include "../lib/tdb/include/tdb.h"
|
---|
26 | #include "../lib/util/util_tdb.h"
|
---|
27 |
|
---|
28 | /* these are little tdb utility functions that are meant to make
|
---|
29 | dealing with a tdb database a little less cumbersome in Samba */
|
---|
30 |
|
---|
31 | /***************************************************************
|
---|
32 | Make a TDB_DATA and keep the const warning in one place
|
---|
33 | ****************************************************************/
|
---|
34 |
|
---|
35 | TDB_DATA make_tdb_data(const uint8_t *dptr, size_t dsize)
|
---|
36 | {
|
---|
37 | TDB_DATA ret;
|
---|
38 | ret.dptr = discard_const_p(uint8_t, dptr);
|
---|
39 | ret.dsize = dsize;
|
---|
40 | return ret;
|
---|
41 | }
|
---|
42 |
|
---|
43 | bool tdb_data_equal(TDB_DATA t1, TDB_DATA t2)
|
---|
44 | {
|
---|
45 | if (t1.dsize != t2.dsize) {
|
---|
46 | return false;
|
---|
47 | }
|
---|
48 | return (memcmp(t1.dptr, t2.dptr, t1.dsize) == 0);
|
---|
49 | }
|
---|
50 |
|
---|
51 | bool tdb_data_is_empty(TDB_DATA d)
|
---|
52 | {
|
---|
53 | return (d.dsize == 0) || (d.dptr == NULL);
|
---|
54 | }
|
---|
55 |
|
---|
56 | TDB_DATA string_tdb_data(const char *string)
|
---|
57 | {
|
---|
58 | return make_tdb_data((const uint8_t *)string, string ? strlen(string) : 0 );
|
---|
59 | }
|
---|
60 |
|
---|
61 | TDB_DATA string_term_tdb_data(const char *string)
|
---|
62 | {
|
---|
63 | return make_tdb_data((const uint8_t *)string, string ? strlen(string) + 1 : 0);
|
---|
64 | }
|
---|
65 |
|
---|
66 | TDB_DATA tdb_data_talloc_copy(TALLOC_CTX* mem_ctx, TDB_DATA data) {
|
---|
67 | TDB_DATA ret = {
|
---|
68 | .dptr = (uint8_t *)talloc_size(mem_ctx, data.dsize+1),
|
---|
69 | .dsize = data.dsize
|
---|
70 | };
|
---|
71 | if (ret.dptr == NULL) {
|
---|
72 | ret.dsize = 0;
|
---|
73 | } else {
|
---|
74 | memcpy(ret.dptr, data.dptr, data.dsize);
|
---|
75 | ret.dptr[ret.dsize] = '\0';
|
---|
76 | }
|
---|
77 | return ret;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | /****************************************************************************
|
---|
82 | Lock a chain by string. Return non-zero if lock failed.
|
---|
83 | ****************************************************************************/
|
---|
84 |
|
---|
85 | int tdb_lock_bystring(struct tdb_context *tdb, const char *keyval)
|
---|
86 | {
|
---|
87 | TDB_DATA key = string_term_tdb_data(keyval);
|
---|
88 |
|
---|
89 | return tdb_chainlock(tdb, key);
|
---|
90 | }
|
---|
91 |
|
---|
92 | /****************************************************************************
|
---|
93 | Unlock a chain by string.
|
---|
94 | ****************************************************************************/
|
---|
95 |
|
---|
96 | void tdb_unlock_bystring(struct tdb_context *tdb, const char *keyval)
|
---|
97 | {
|
---|
98 | TDB_DATA key = string_term_tdb_data(keyval);
|
---|
99 |
|
---|
100 | tdb_chainunlock(tdb, key);
|
---|
101 | }
|
---|
102 |
|
---|
103 | /****************************************************************************
|
---|
104 | Read lock a chain by string. Return non-zero if lock failed.
|
---|
105 | ****************************************************************************/
|
---|
106 |
|
---|
107 | int tdb_read_lock_bystring(struct tdb_context *tdb, const char *keyval)
|
---|
108 | {
|
---|
109 | TDB_DATA key = string_term_tdb_data(keyval);
|
---|
110 |
|
---|
111 | return tdb_chainlock_read(tdb, key);
|
---|
112 | }
|
---|
113 |
|
---|
114 | /****************************************************************************
|
---|
115 | Read unlock a chain by string.
|
---|
116 | ****************************************************************************/
|
---|
117 |
|
---|
118 | void tdb_read_unlock_bystring(struct tdb_context *tdb, const char *keyval)
|
---|
119 | {
|
---|
120 | TDB_DATA key = string_term_tdb_data(keyval);
|
---|
121 |
|
---|
122 | tdb_chainunlock_read(tdb, key);
|
---|
123 | }
|
---|
124 |
|
---|
125 |
|
---|
126 | /****************************************************************************
|
---|
127 | Fetch a int32_t value by a arbitrary blob key, return -1 if not found.
|
---|
128 | Output is int32_t in native byte order.
|
---|
129 | ****************************************************************************/
|
---|
130 |
|
---|
131 | int32_t tdb_fetch_int32_byblob(struct tdb_context *tdb, TDB_DATA key)
|
---|
132 | {
|
---|
133 | TDB_DATA data;
|
---|
134 | int32_t ret;
|
---|
135 |
|
---|
136 | data = tdb_fetch(tdb, key);
|
---|
137 | if (!data.dptr || data.dsize != sizeof(int32_t)) {
|
---|
138 | SAFE_FREE(data.dptr);
|
---|
139 | return -1;
|
---|
140 | }
|
---|
141 |
|
---|
142 | ret = IVAL(data.dptr,0);
|
---|
143 | SAFE_FREE(data.dptr);
|
---|
144 | return ret;
|
---|
145 | }
|
---|
146 |
|
---|
147 | /****************************************************************************
|
---|
148 | Fetch a int32_t value by string key, return -1 if not found.
|
---|
149 | Output is int32_t in native byte order.
|
---|
150 | ****************************************************************************/
|
---|
151 |
|
---|
152 | int32_t tdb_fetch_int32(struct tdb_context *tdb, const char *keystr)
|
---|
153 | {
|
---|
154 | return tdb_fetch_int32_byblob(tdb, string_term_tdb_data(keystr));
|
---|
155 | }
|
---|
156 |
|
---|
157 | /****************************************************************************
|
---|
158 | Store a int32_t value by an arbitrary blob key, return 0 on success, -ve on failure.
|
---|
159 | Input is int32_t in native byte order. Output in tdb is in little-endian.
|
---|
160 | ****************************************************************************/
|
---|
161 |
|
---|
162 | int tdb_store_int32_byblob(struct tdb_context *tdb, TDB_DATA key, int32_t v)
|
---|
163 | {
|
---|
164 | TDB_DATA data;
|
---|
165 | int32_t v_store;
|
---|
166 |
|
---|
167 | SIVAL(&v_store,0,v);
|
---|
168 | data.dptr = (unsigned char *)&v_store;
|
---|
169 | data.dsize = sizeof(int32_t);
|
---|
170 |
|
---|
171 | return tdb_store(tdb, key, data, TDB_REPLACE);
|
---|
172 | }
|
---|
173 |
|
---|
174 | /****************************************************************************
|
---|
175 | Store a int32_t value by string key, return 0 on success, -ve on failure.
|
---|
176 | Input is int32_t in native byte order. Output in tdb is in little-endian.
|
---|
177 | ****************************************************************************/
|
---|
178 |
|
---|
179 | int tdb_store_int32(struct tdb_context *tdb, const char *keystr, int32_t v)
|
---|
180 | {
|
---|
181 | return tdb_store_int32_byblob(tdb, string_term_tdb_data(keystr), v);
|
---|
182 | }
|
---|
183 |
|
---|
184 | /****************************************************************************
|
---|
185 | Fetch a uint32_t value by a arbitrary blob key, return false if not found.
|
---|
186 | Output is uint32_t in native byte order.
|
---|
187 | ****************************************************************************/
|
---|
188 |
|
---|
189 | bool tdb_fetch_uint32_byblob(struct tdb_context *tdb, TDB_DATA key, uint32_t *value)
|
---|
190 | {
|
---|
191 | TDB_DATA data;
|
---|
192 |
|
---|
193 | data = tdb_fetch(tdb, key);
|
---|
194 | if (!data.dptr || data.dsize != sizeof(uint32_t)) {
|
---|
195 | SAFE_FREE(data.dptr);
|
---|
196 | return false;
|
---|
197 | }
|
---|
198 |
|
---|
199 | *value = IVAL(data.dptr,0);
|
---|
200 | SAFE_FREE(data.dptr);
|
---|
201 | return true;
|
---|
202 | }
|
---|
203 |
|
---|
204 | /****************************************************************************
|
---|
205 | Fetch a uint32_t value by string key, return false if not found.
|
---|
206 | Output is uint32_t in native byte order.
|
---|
207 | ****************************************************************************/
|
---|
208 |
|
---|
209 | bool tdb_fetch_uint32(struct tdb_context *tdb, const char *keystr, uint32_t *value)
|
---|
210 | {
|
---|
211 | return tdb_fetch_uint32_byblob(tdb, string_term_tdb_data(keystr), value);
|
---|
212 | }
|
---|
213 |
|
---|
214 | /****************************************************************************
|
---|
215 | Store a uint32_t value by an arbitrary blob key, return true on success, false on failure.
|
---|
216 | Input is uint32_t in native byte order. Output in tdb is in little-endian.
|
---|
217 | ****************************************************************************/
|
---|
218 |
|
---|
219 | bool tdb_store_uint32_byblob(struct tdb_context *tdb, TDB_DATA key, uint32_t value)
|
---|
220 | {
|
---|
221 | TDB_DATA data;
|
---|
222 | uint32_t v_store;
|
---|
223 | bool ret = true;
|
---|
224 |
|
---|
225 | SIVAL(&v_store, 0, value);
|
---|
226 | data.dptr = (unsigned char *)&v_store;
|
---|
227 | data.dsize = sizeof(uint32_t);
|
---|
228 |
|
---|
229 | if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
|
---|
230 | ret = false;
|
---|
231 |
|
---|
232 | return ret;
|
---|
233 | }
|
---|
234 |
|
---|
235 | /****************************************************************************
|
---|
236 | Store a uint32_t value by string key, return true on success, false on failure.
|
---|
237 | Input is uint32_t in native byte order. Output in tdb is in little-endian.
|
---|
238 | ****************************************************************************/
|
---|
239 |
|
---|
240 | bool tdb_store_uint32(struct tdb_context *tdb, const char *keystr, uint32_t value)
|
---|
241 | {
|
---|
242 | return tdb_store_uint32_byblob(tdb, string_term_tdb_data(keystr), value);
|
---|
243 | }
|
---|
244 | /****************************************************************************
|
---|
245 | Store a buffer by a null terminated string key. Return 0 on success, -ve
|
---|
246 | on failure.
|
---|
247 | ****************************************************************************/
|
---|
248 |
|
---|
249 | int tdb_store_bystring(struct tdb_context *tdb, const char *keystr, TDB_DATA data, int flags)
|
---|
250 | {
|
---|
251 | TDB_DATA key = string_term_tdb_data(keystr);
|
---|
252 |
|
---|
253 | return tdb_store(tdb, key, data, flags);
|
---|
254 | }
|
---|
255 |
|
---|
256 | /****************************************************************************
|
---|
257 | Fetch a buffer using a null terminated string key. Don't forget to call
|
---|
258 | free() on the result dptr.
|
---|
259 | ****************************************************************************/
|
---|
260 |
|
---|
261 | TDB_DATA tdb_fetch_bystring(struct tdb_context *tdb, const char *keystr)
|
---|
262 | {
|
---|
263 | TDB_DATA key = string_term_tdb_data(keystr);
|
---|
264 |
|
---|
265 | return tdb_fetch(tdb, key);
|
---|
266 | }
|
---|
267 |
|
---|
268 | /****************************************************************************
|
---|
269 | Delete an entry using a null terminated string key.
|
---|
270 | ****************************************************************************/
|
---|
271 |
|
---|
272 | int tdb_delete_bystring(struct tdb_context *tdb, const char *keystr)
|
---|
273 | {
|
---|
274 | TDB_DATA key = string_term_tdb_data(keystr);
|
---|
275 |
|
---|
276 | return tdb_delete(tdb, key);
|
---|
277 | }
|
---|
278 |
|
---|
279 | /****************************************************************************
|
---|
280 | Atomic integer change. Returns old value. To create, set initial value in *oldval.
|
---|
281 | ****************************************************************************/
|
---|
282 |
|
---|
283 | int32_t tdb_change_int32_atomic(struct tdb_context *tdb, const char *keystr, int32_t *oldval, int32_t change_val)
|
---|
284 | {
|
---|
285 | int32_t val;
|
---|
286 | int32_t ret = -1;
|
---|
287 |
|
---|
288 | if (tdb_lock_bystring(tdb, keystr) != 0)
|
---|
289 | return -1;
|
---|
290 |
|
---|
291 | if ((val = tdb_fetch_int32(tdb, keystr)) == -1) {
|
---|
292 | /* The lookup failed */
|
---|
293 | if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
|
---|
294 | /* but not because it didn't exist */
|
---|
295 | goto err_out;
|
---|
296 | }
|
---|
297 |
|
---|
298 | /* Start with 'old' value */
|
---|
299 | val = *oldval;
|
---|
300 |
|
---|
301 | } else {
|
---|
302 | /* It worked, set return value (oldval) to tdb data */
|
---|
303 | *oldval = val;
|
---|
304 | }
|
---|
305 |
|
---|
306 | /* Increment value for storage and return next time */
|
---|
307 | val += change_val;
|
---|
308 |
|
---|
309 | if (tdb_store_int32(tdb, keystr, val) != 0)
|
---|
310 | goto err_out;
|
---|
311 |
|
---|
312 | ret = 0;
|
---|
313 |
|
---|
314 | err_out:
|
---|
315 |
|
---|
316 | tdb_unlock_bystring(tdb, keystr);
|
---|
317 | return ret;
|
---|
318 | }
|
---|
319 |
|
---|
320 | /****************************************************************************
|
---|
321 | Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval.
|
---|
322 | ****************************************************************************/
|
---|
323 |
|
---|
324 | bool tdb_change_uint32_atomic(struct tdb_context *tdb, const char *keystr, uint32_t *oldval, uint32_t change_val)
|
---|
325 | {
|
---|
326 | uint32_t val;
|
---|
327 | bool ret = false;
|
---|
328 |
|
---|
329 | if (tdb_lock_bystring(tdb, keystr) != 0)
|
---|
330 | return false;
|
---|
331 |
|
---|
332 | if (!tdb_fetch_uint32(tdb, keystr, &val)) {
|
---|
333 | /* It failed */
|
---|
334 | if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
|
---|
335 | /* and not because it didn't exist */
|
---|
336 | goto err_out;
|
---|
337 | }
|
---|
338 |
|
---|
339 | /* Start with 'old' value */
|
---|
340 | val = *oldval;
|
---|
341 |
|
---|
342 | } else {
|
---|
343 | /* it worked, set return value (oldval) to tdb data */
|
---|
344 | *oldval = val;
|
---|
345 |
|
---|
346 | }
|
---|
347 |
|
---|
348 | /* get a new value to store */
|
---|
349 | val += change_val;
|
---|
350 |
|
---|
351 | if (!tdb_store_uint32(tdb, keystr, val))
|
---|
352 | goto err_out;
|
---|
353 |
|
---|
354 | ret = true;
|
---|
355 |
|
---|
356 | err_out:
|
---|
357 |
|
---|
358 | tdb_unlock_bystring(tdb, keystr);
|
---|
359 | return ret;
|
---|
360 | }
|
---|
361 |
|
---|
362 | /****************************************************************************
|
---|
363 | Allow tdb_delete to be used as a tdb_traversal_fn.
|
---|
364 | ****************************************************************************/
|
---|
365 |
|
---|
366 | int tdb_traverse_delete_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf,
|
---|
367 | void *state)
|
---|
368 | {
|
---|
369 | return tdb_delete(the_tdb, key);
|
---|
370 | }
|
---|
371 |
|
---|
372 | /****************************************************************************
|
---|
373 | Return an NTSTATUS from a TDB_ERROR
|
---|
374 | ****************************************************************************/
|
---|
375 |
|
---|
376 | NTSTATUS map_nt_error_from_tdb(enum TDB_ERROR err)
|
---|
377 | {
|
---|
378 | NTSTATUS result = NT_STATUS_INTERNAL_ERROR;
|
---|
379 |
|
---|
380 | switch (err) {
|
---|
381 | case TDB_SUCCESS:
|
---|
382 | result = NT_STATUS_OK;
|
---|
383 | break;
|
---|
384 | case TDB_ERR_CORRUPT:
|
---|
385 | result = NT_STATUS_INTERNAL_DB_CORRUPTION;
|
---|
386 | break;
|
---|
387 | case TDB_ERR_IO:
|
---|
388 | result = NT_STATUS_UNEXPECTED_IO_ERROR;
|
---|
389 | break;
|
---|
390 | case TDB_ERR_OOM:
|
---|
391 | result = NT_STATUS_NO_MEMORY;
|
---|
392 | break;
|
---|
393 | case TDB_ERR_EXISTS:
|
---|
394 | result = NT_STATUS_OBJECT_NAME_COLLISION;
|
---|
395 | break;
|
---|
396 |
|
---|
397 | case TDB_ERR_LOCK:
|
---|
398 | /*
|
---|
399 | * TDB_ERR_LOCK is very broad, we could for example
|
---|
400 | * distinguish between fcntl locks and invalid lock
|
---|
401 | * sequences. So NT_STATUS_FILE_LOCK_CONFLICT is a
|
---|
402 | * compromise.
|
---|
403 | */
|
---|
404 | result = NT_STATUS_FILE_LOCK_CONFLICT;
|
---|
405 | break;
|
---|
406 |
|
---|
407 | case TDB_ERR_NOLOCK:
|
---|
408 | case TDB_ERR_LOCK_TIMEOUT:
|
---|
409 | /*
|
---|
410 | * These two ones in the enum are not actually used
|
---|
411 | */
|
---|
412 | result = NT_STATUS_FILE_LOCK_CONFLICT;
|
---|
413 | break;
|
---|
414 | case TDB_ERR_NOEXIST:
|
---|
415 | result = NT_STATUS_NOT_FOUND;
|
---|
416 | break;
|
---|
417 | case TDB_ERR_EINVAL:
|
---|
418 | result = NT_STATUS_INVALID_PARAMETER;
|
---|
419 | break;
|
---|
420 | case TDB_ERR_RDONLY:
|
---|
421 | result = NT_STATUS_ACCESS_DENIED;
|
---|
422 | break;
|
---|
423 | case TDB_ERR_NESTING:
|
---|
424 | result = NT_STATUS_INTERNAL_ERROR;
|
---|
425 | break;
|
---|
426 | };
|
---|
427 | return result;
|
---|
428 | }
|
---|
429 |
|
---|
430 | int map_unix_error_from_tdb(enum TDB_ERROR err)
|
---|
431 | {
|
---|
432 | int result = EINVAL;
|
---|
433 |
|
---|
434 | switch (err) {
|
---|
435 | case TDB_SUCCESS:
|
---|
436 | result = 0;
|
---|
437 | break;
|
---|
438 | case TDB_ERR_CORRUPT:
|
---|
439 | result = EILSEQ;
|
---|
440 | break;
|
---|
441 | case TDB_ERR_IO:
|
---|
442 | result = EIO;
|
---|
443 | break;
|
---|
444 | case TDB_ERR_OOM:
|
---|
445 | result = ENOMEM;
|
---|
446 | break;
|
---|
447 | case TDB_ERR_EXISTS:
|
---|
448 | result = EEXIST;
|
---|
449 | break;
|
---|
450 |
|
---|
451 | case TDB_ERR_LOCK:
|
---|
452 | /*
|
---|
453 | * TDB_ERR_LOCK is very broad, we could for example
|
---|
454 | * distinguish between fcntl locks and invalid lock
|
---|
455 | * sequences. EWOULDBLOCK is wrong, but there is no real
|
---|
456 | * generic lock error code in errno.h
|
---|
457 | */
|
---|
458 | result = EWOULDBLOCK;
|
---|
459 | break;
|
---|
460 |
|
---|
461 | case TDB_ERR_NOLOCK:
|
---|
462 | case TDB_ERR_LOCK_TIMEOUT:
|
---|
463 | /*
|
---|
464 | * These two ones in the enum are not actually used
|
---|
465 | */
|
---|
466 | result = ENOLCK;
|
---|
467 | break;
|
---|
468 | case TDB_ERR_NOEXIST:
|
---|
469 | result = ENOENT;
|
---|
470 | break;
|
---|
471 | case TDB_ERR_EINVAL:
|
---|
472 | result = EINVAL;
|
---|
473 | break;
|
---|
474 | case TDB_ERR_RDONLY:
|
---|
475 | result = EROFS;
|
---|
476 | break;
|
---|
477 | case TDB_ERR_NESTING:
|
---|
478 | /*
|
---|
479 | * Well, this db is already busy...
|
---|
480 | */
|
---|
481 | result = EBUSY;
|
---|
482 | break;
|
---|
483 | };
|
---|
484 | return result;
|
---|
485 | }
|
---|
486 |
|
---|
487 | struct tdb_fetch_talloc_state {
|
---|
488 | TALLOC_CTX *mem_ctx;
|
---|
489 | uint8_t *buf;
|
---|
490 | };
|
---|
491 |
|
---|
492 | static int tdb_fetch_talloc_parser(TDB_DATA key, TDB_DATA data,
|
---|
493 | void *private_data)
|
---|
494 | {
|
---|
495 | struct tdb_fetch_talloc_state *state = private_data;
|
---|
496 | state->buf = talloc_memdup(state->mem_ctx, data.dptr, data.dsize);
|
---|
497 | return 0;
|
---|
498 | }
|
---|
499 |
|
---|
500 | int tdb_fetch_talloc(struct tdb_context *tdb, TDB_DATA key,
|
---|
501 | TALLOC_CTX *mem_ctx, uint8_t **buf)
|
---|
502 | {
|
---|
503 | struct tdb_fetch_talloc_state state = { .mem_ctx = mem_ctx };
|
---|
504 | int ret;
|
---|
505 |
|
---|
506 | ret = tdb_parse_record(tdb, key, tdb_fetch_talloc_parser, &state);
|
---|
507 | if (ret == -1) {
|
---|
508 | enum TDB_ERROR err = tdb_error(tdb);
|
---|
509 | return map_unix_error_from_tdb(err);
|
---|
510 | }
|
---|
511 |
|
---|
512 | if (state.buf == NULL) {
|
---|
513 | return ENOMEM;
|
---|
514 | }
|
---|
515 |
|
---|
516 | *buf = state.buf;
|
---|
517 | return 0;
|
---|
518 | }
|
---|