s3: Fix Coverity ID 2473, UNINIT
[Samba/gbeck.git] / source3 / lib / util_tdb.c
bloba9290ab94aee7c0826684242b8531ebb50b2a856
1 /*
2 Unix SMB/CIFS implementation.
3 tdb utility functions
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Rafal Szczesniak 2002
6 Copyright (C) Michael Adam 2007
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "system/filesys.h"
24 #undef malloc
25 #undef realloc
26 #undef calloc
27 #undef strdup
29 /* these are little tdb utility functions that are meant to make
30 dealing with a tdb database a little less cumbersome in Samba */
32 static SIG_ATOMIC_T gotalarm;
34 /***************************************************************
35 Signal function to tell us we timed out.
36 ****************************************************************/
38 static void gotalarm_sig(int signum)
40 gotalarm = 1;
43 /****************************************************************************
44 Lock a chain with timeout (in seconds).
45 ****************************************************************************/
47 static int tdb_chainlock_with_timeout_internal( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout, int rw_type)
49 /* Allow tdb_chainlock to be interrupted by an alarm. */
50 int ret;
51 gotalarm = 0;
53 if (timeout) {
54 CatchSignal(SIGALRM, gotalarm_sig);
55 tdb_setalarm_sigptr(tdb, &gotalarm);
56 alarm(timeout);
59 if (rw_type == F_RDLCK)
60 ret = tdb_chainlock_read(tdb, key);
61 else
62 ret = tdb_chainlock(tdb, key);
64 if (timeout) {
65 alarm(0);
66 tdb_setalarm_sigptr(tdb, NULL);
67 CatchSignal(SIGALRM, SIG_IGN);
68 if (gotalarm && (ret == -1)) {
69 DEBUG(0,("tdb_chainlock_with_timeout_internal: alarm (%u) timed out for key %s in tdb %s\n",
70 timeout, key.dptr, tdb_name(tdb)));
71 /* TODO: If we time out waiting for a lock, it might
72 * be nice to use F_GETLK to get the pid of the
73 * process currently holding the lock and print that
74 * as part of the debugging message. -- mbp */
75 return -1;
79 return ret;
82 /****************************************************************************
83 Write lock a chain. Return -1 if timeout or lock failed.
84 ****************************************************************************/
86 int tdb_chainlock_with_timeout( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout)
88 return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_WRLCK);
91 int tdb_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval,
92 int timeout)
94 TDB_DATA key = string_term_tdb_data(keyval);
96 return tdb_chainlock_with_timeout(tdb, key, timeout);
99 /****************************************************************************
100 Read lock a chain by string. Return -1 if timeout or lock failed.
101 ****************************************************************************/
103 int tdb_read_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval, unsigned int timeout)
105 TDB_DATA key = string_term_tdb_data(keyval);
107 return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_RDLCK);
113 int tdb_trans_store_bystring(TDB_CONTEXT *tdb, const char *keystr,
114 TDB_DATA data, int flags)
116 TDB_DATA key = string_term_tdb_data(keystr);
118 return tdb_trans_store(tdb, key, data, flags);
121 /****************************************************************************
122 Useful pair of routines for packing/unpacking data consisting of
123 integers and strings.
124 ****************************************************************************/
126 static size_t tdb_pack_va(uint8 *buf, int bufsize, const char *fmt, va_list ap)
128 uint8 bt;
129 uint16 w;
130 uint32 d;
131 int i;
132 void *p;
133 int len;
134 char *s;
135 char c;
136 uint8 *buf0 = buf;
137 const char *fmt0 = fmt;
138 int bufsize0 = bufsize;
140 while (*fmt) {
141 switch ((c = *fmt++)) {
142 case 'b': /* unsigned 8-bit integer */
143 len = 1;
144 bt = (uint8)va_arg(ap, int);
145 if (bufsize && bufsize >= len)
146 SSVAL(buf, 0, bt);
147 break;
148 case 'w': /* unsigned 16-bit integer */
149 len = 2;
150 w = (uint16)va_arg(ap, int);
151 if (bufsize && bufsize >= len)
152 SSVAL(buf, 0, w);
153 break;
154 case 'd': /* signed 32-bit integer (standard int in most systems) */
155 len = 4;
156 d = va_arg(ap, uint32);
157 if (bufsize && bufsize >= len)
158 SIVAL(buf, 0, d);
159 break;
160 case 'p': /* pointer */
161 len = 4;
162 p = va_arg(ap, void *);
163 d = p?1:0;
164 if (bufsize && bufsize >= len)
165 SIVAL(buf, 0, d);
166 break;
167 case 'P': /* null-terminated string */
168 s = va_arg(ap,char *);
169 w = strlen(s);
170 len = w + 1;
171 if (bufsize && bufsize >= len)
172 memcpy(buf, s, len);
173 break;
174 case 'f': /* null-terminated string */
175 s = va_arg(ap,char *);
176 w = strlen(s);
177 len = w + 1;
178 if (bufsize && bufsize >= len)
179 memcpy(buf, s, len);
180 break;
181 case 'B': /* fixed-length string */
182 i = va_arg(ap, int);
183 s = va_arg(ap, char *);
184 len = 4+i;
185 if (bufsize && bufsize >= len) {
186 SIVAL(buf, 0, i);
187 memcpy(buf+4, s, i);
189 break;
190 default:
191 DEBUG(0,("Unknown tdb_pack format %c in %s\n",
192 c, fmt));
193 len = 0;
194 break;
197 buf += len;
198 if (bufsize)
199 bufsize -= len;
200 if (bufsize < 0)
201 bufsize = 0;
204 DEBUG(18,("tdb_pack_va(%s, %d) -> %d\n",
205 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
207 return PTR_DIFF(buf, buf0);
210 size_t tdb_pack(uint8 *buf, int bufsize, const char *fmt, ...)
212 va_list ap;
213 size_t result;
215 va_start(ap, fmt);
216 result = tdb_pack_va(buf, bufsize, fmt, ap);
217 va_end(ap);
218 return result;
221 bool tdb_pack_append(TALLOC_CTX *mem_ctx, uint8 **buf, size_t *len,
222 const char *fmt, ...)
224 va_list ap;
225 size_t len1, len2;
227 va_start(ap, fmt);
228 len1 = tdb_pack_va(NULL, 0, fmt, ap);
229 va_end(ap);
231 if (mem_ctx != NULL) {
232 *buf = TALLOC_REALLOC_ARRAY(mem_ctx, *buf, uint8,
233 (*len) + len1);
234 } else {
235 *buf = SMB_REALLOC_ARRAY(*buf, uint8, (*len) + len1);
238 if (*buf == NULL) {
239 return False;
242 va_start(ap, fmt);
243 len2 = tdb_pack_va((*buf)+(*len), len1, fmt, ap);
244 va_end(ap);
246 if (len1 != len2) {
247 return False;
250 *len += len2;
252 return True;
255 /****************************************************************************
256 Useful pair of routines for packing/unpacking data consisting of
257 integers and strings.
258 ****************************************************************************/
260 int tdb_unpack(const uint8 *buf, int bufsize, const char *fmt, ...)
262 va_list ap;
263 uint8 *bt;
264 uint16 *w;
265 uint32 *d;
266 int len;
267 int *i;
268 void **p;
269 char *s, **b, **ps;
270 char c;
271 const uint8 *buf0 = buf;
272 const char *fmt0 = fmt;
273 int bufsize0 = bufsize;
275 va_start(ap, fmt);
277 while (*fmt) {
278 switch ((c=*fmt++)) {
279 case 'b': /* unsigned 8-bit integer */
280 len = 1;
281 bt = va_arg(ap, uint8 *);
282 if (bufsize < len)
283 goto no_space;
284 *bt = SVAL(buf, 0);
285 break;
286 case 'w': /* unsigned 16-bit integer */
287 len = 2;
288 w = va_arg(ap, uint16 *);
289 if (bufsize < len)
290 goto no_space;
291 *w = SVAL(buf, 0);
292 break;
293 case 'd': /* signed 32-bit integer (standard int in most systems) */
294 len = 4;
295 d = va_arg(ap, uint32 *);
296 if (bufsize < len)
297 goto no_space;
298 *d = IVAL(buf, 0);
299 break;
300 case 'p': /* pointer */
301 len = 4;
302 p = va_arg(ap, void **);
303 if (bufsize < len)
304 goto no_space;
306 * This isn't a real pointer - only a token (1 or 0)
307 * to mark the fact a pointer is present.
310 *p = (void *)(IVAL(buf, 0) ? (void *)1 : NULL);
311 break;
312 case 'P': /* null-terminated string */
313 /* Return malloc'ed string. */
314 ps = va_arg(ap,char **);
315 len = strlen((const char *)buf) + 1;
316 *ps = SMB_STRDUP((const char *)buf);
317 break;
318 case 'f': /* null-terminated string */
319 s = va_arg(ap,char *);
320 len = strlen((const char *)buf) + 1;
321 if (bufsize < len || len > sizeof(fstring))
322 goto no_space;
323 memcpy(s, buf, len);
324 break;
325 case 'B': /* fixed-length string */
326 i = va_arg(ap, int *);
327 b = va_arg(ap, char **);
328 len = 4;
329 if (bufsize < len)
330 goto no_space;
331 *i = IVAL(buf, 0);
332 if (! *i) {
333 *b = NULL;
334 break;
336 len += *i;
337 if (bufsize < len)
338 goto no_space;
339 *b = (char *)SMB_MALLOC(*i);
340 if (! *b)
341 goto no_space;
342 memcpy(*b, buf+4, *i);
343 break;
344 default:
345 DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
346 c, fmt));
348 len = 0;
349 break;
352 buf += len;
353 bufsize -= len;
356 va_end(ap);
358 DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
359 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
361 return PTR_DIFF(buf, buf0);
363 no_space:
364 va_end(ap);
365 return -1;
369 /****************************************************************************
370 Log tdb messages via DEBUG().
371 ****************************************************************************/
373 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, const char *format, ...)
375 va_list ap;
376 char *ptr = NULL;
377 int ret;
379 va_start(ap, format);
380 ret = vasprintf(&ptr, format, ap);
381 va_end(ap);
383 if ((ret == -1) || !*ptr)
384 return;
386 DEBUG((int)level, ("tdb(%s): %s", tdb_name(tdb) ? tdb_name(tdb) : "unnamed", ptr));
387 SAFE_FREE(ptr);
390 /****************************************************************************
391 Like tdb_open() but also setup a logging function that redirects to
392 the samba DEBUG() system.
393 ****************************************************************************/
395 TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
396 int open_flags, mode_t mode)
398 TDB_CONTEXT *tdb;
399 struct tdb_logging_context log_ctx;
401 if (!lp_use_mmap())
402 tdb_flags |= TDB_NOMMAP;
404 log_ctx.log_fn = tdb_log;
405 log_ctx.log_private = NULL;
407 if ((hash_size == 0) && (name != NULL)) {
408 const char *base = strrchr_m(name, '/');
409 if (base != NULL) {
410 base += 1;
412 else {
413 base = name;
415 hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
418 tdb = tdb_open_ex(name, hash_size, tdb_flags,
419 open_flags, mode, &log_ctx, NULL);
420 if (!tdb)
421 return NULL;
423 return tdb;
426 /****************************************************************************
427 tdb_store, wrapped in a transaction. This way we make sure that a process
428 that dies within writing does not leave a corrupt tdb behind.
429 ****************************************************************************/
431 int tdb_trans_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
432 int flag)
434 int res;
436 if ((res = tdb_transaction_start(tdb)) != 0) {
437 DEBUG(5, ("tdb_transaction_start failed\n"));
438 return res;
441 if ((res = tdb_store(tdb, key, dbuf, flag)) != 0) {
442 DEBUG(10, ("tdb_store failed\n"));
443 if (tdb_transaction_cancel(tdb) != 0) {
444 smb_panic("Cancelling transaction failed");
446 return res;
449 if ((res = tdb_transaction_commit(tdb)) != 0) {
450 DEBUG(5, ("tdb_transaction_commit failed\n"));
453 return res;
456 /****************************************************************************
457 tdb_delete, wrapped in a transaction. This way we make sure that a process
458 that dies within deleting does not leave a corrupt tdb behind.
459 ****************************************************************************/
461 int tdb_trans_delete(struct tdb_context *tdb, TDB_DATA key)
463 int res;
465 if ((res = tdb_transaction_start(tdb)) != 0) {
466 DEBUG(5, ("tdb_transaction_start failed\n"));
467 return res;
470 if ((res = tdb_delete(tdb, key)) != 0) {
471 DEBUG(10, ("tdb_delete failed\n"));
472 if (tdb_transaction_cancel(tdb) != 0) {
473 smb_panic("Cancelling transaction failed");
475 return res;
478 if ((res = tdb_transaction_commit(tdb)) != 0) {
479 DEBUG(5, ("tdb_transaction_commit failed\n"));
482 return res;
486 Log tdb messages via DEBUG().
488 static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
489 const char *format, ...) PRINTF_ATTRIBUTE(3,4);
491 static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
492 const char *format, ...)
494 va_list ap;
495 char *ptr = NULL;
496 int debuglevel = 0;
497 int ret;
499 switch (level) {
500 case TDB_DEBUG_FATAL:
501 debuglevel = 0;
502 break;
503 case TDB_DEBUG_ERROR:
504 debuglevel = 1;
505 break;
506 case TDB_DEBUG_WARNING:
507 debuglevel = 2;
508 break;
509 case TDB_DEBUG_TRACE:
510 debuglevel = 5;
511 break;
512 default:
513 debuglevel = 0;
516 va_start(ap, format);
517 ret = vasprintf(&ptr, format, ap);
518 va_end(ap);
520 if (ret != -1) {
521 const char *name = tdb_name(tdb);
522 DEBUG(debuglevel, ("tdb(%s): %s", name ? name : "unnamed", ptr));
523 free(ptr);
527 struct tdb_wrap_private {
528 struct tdb_context *tdb;
529 const char *name;
530 struct tdb_wrap_private *next, *prev;
533 static struct tdb_wrap_private *tdb_list;
535 /* destroy the last connection to a tdb */
536 static int tdb_wrap_private_destructor(struct tdb_wrap_private *w)
538 tdb_close(w->tdb);
539 DLIST_REMOVE(tdb_list, w);
540 return 0;
543 static struct tdb_wrap_private *tdb_wrap_private_open(TALLOC_CTX *mem_ctx,
544 const char *name,
545 int hash_size,
546 int tdb_flags,
547 int open_flags,
548 mode_t mode)
550 struct tdb_wrap_private *result;
551 struct tdb_logging_context log_ctx;
553 result = talloc(mem_ctx, struct tdb_wrap_private);
554 if (result == NULL) {
555 return NULL;
557 result->name = talloc_strdup(result, name);
558 if (result->name == NULL) {
559 goto fail;
562 log_ctx.log_fn = tdb_wrap_log;
564 if (!lp_use_mmap()) {
565 tdb_flags |= TDB_NOMMAP;
568 if ((hash_size == 0) && (name != NULL)) {
569 const char *base;
570 base = strrchr_m(name, '/');
572 if (base != NULL) {
573 base += 1;
574 } else {
575 base = name;
577 hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
580 result->tdb = tdb_open_ex(name, hash_size, tdb_flags,
581 open_flags, mode, &log_ctx, NULL);
582 if (result->tdb == NULL) {
583 goto fail;
585 talloc_set_destructor(result, tdb_wrap_private_destructor);
586 DLIST_ADD(tdb_list, result);
587 return result;
589 fail:
590 TALLOC_FREE(result);
591 return NULL;
595 wrapped connection to a tdb database
596 to close just talloc_free() the tdb_wrap pointer
598 struct tdb_wrap *tdb_wrap_open(TALLOC_CTX *mem_ctx,
599 const char *name, int hash_size, int tdb_flags,
600 int open_flags, mode_t mode)
602 struct tdb_wrap *result;
603 struct tdb_wrap_private *w;
605 result = talloc(mem_ctx, struct tdb_wrap);
606 if (result == NULL) {
607 return NULL;
610 for (w=tdb_list;w;w=w->next) {
611 if (strcmp(name, w->name) == 0) {
612 break;
616 if (w == NULL) {
617 w = tdb_wrap_private_open(result, name, hash_size, tdb_flags,
618 open_flags, mode);
619 } else {
621 * Correctly use talloc_reference: The tdb will be
622 * closed when "w" is being freed. The caller never
623 * sees "w", so an incorrect use of talloc_free(w)
624 * instead of calling talloc_unlink is not possible.
625 * To avoid having to refcount ourselves, "w" will
626 * have multiple parents that hang off all the
627 * tdb_wrap's being returned from here. Those parents
628 * can be freed without problem.
630 if (talloc_reference(result, w) == NULL) {
631 goto fail;
634 if (w == NULL) {
635 goto fail;
637 result->tdb = w->tdb;
638 return result;
639 fail:
640 TALLOC_FREE(result);
641 return NULL;
644 NTSTATUS map_nt_error_from_tdb(enum TDB_ERROR err)
646 NTSTATUS result = NT_STATUS_INTERNAL_ERROR;
648 switch (err) {
649 case TDB_SUCCESS:
650 result = NT_STATUS_OK;
651 break;
652 case TDB_ERR_CORRUPT:
653 result = NT_STATUS_INTERNAL_DB_CORRUPTION;
654 break;
655 case TDB_ERR_IO:
656 result = NT_STATUS_UNEXPECTED_IO_ERROR;
657 break;
658 case TDB_ERR_OOM:
659 result = NT_STATUS_NO_MEMORY;
660 break;
661 case TDB_ERR_EXISTS:
662 result = NT_STATUS_OBJECT_NAME_COLLISION;
663 break;
665 case TDB_ERR_LOCK:
667 * TDB_ERR_LOCK is very broad, we could for example
668 * distinguish between fcntl locks and invalid lock
669 * sequences. So NT_STATUS_FILE_LOCK_CONFLICT is a
670 * compromise.
672 result = NT_STATUS_FILE_LOCK_CONFLICT;
673 break;
675 case TDB_ERR_NOLOCK:
676 case TDB_ERR_LOCK_TIMEOUT:
678 * These two ones in the enum are not actually used
680 result = NT_STATUS_FILE_LOCK_CONFLICT;
681 break;
682 case TDB_ERR_NOEXIST:
683 result = NT_STATUS_NOT_FOUND;
684 break;
685 case TDB_ERR_EINVAL:
686 result = NT_STATUS_INVALID_PARAMETER;
687 break;
688 case TDB_ERR_RDONLY:
689 result = NT_STATUS_ACCESS_DENIED;
690 break;
691 case TDB_ERR_NESTING:
692 result = NT_STATUS_INTERNAL_ERROR;
693 break;
695 return result;
698 int tdb_data_cmp(TDB_DATA t1, TDB_DATA t2)
700 int ret;
701 if (t1.dptr == NULL && t2.dptr != NULL) {
702 return -1;
704 if (t1.dptr != NULL && t2.dptr == NULL) {
705 return 1;
707 if (t1.dptr == t2.dptr) {
708 return t1.dsize - t2.dsize;
710 ret = memcmp(t1.dptr, t2.dptr, MIN(t1.dsize, t2.dsize));
711 if (ret == 0) {
712 return t1.dsize - t2.dsize;
714 return ret;