2 Unix SMB/CIFS implementation.
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/>.
28 /* these are little tdb utility functions that are meant to make
29 dealing with a tdb database a little less cumbersome in Samba */
31 static SIG_ATOMIC_T gotalarm
;
33 /***************************************************************
34 Signal function to tell us we timed out.
35 ****************************************************************/
37 static void gotalarm_sig(int signum
)
42 /****************************************************************************
43 Lock a chain with timeout (in seconds).
44 ****************************************************************************/
46 static int tdb_chainlock_with_timeout_internal( TDB_CONTEXT
*tdb
, TDB_DATA key
, unsigned int timeout
, int rw_type
)
48 /* Allow tdb_chainlock to be interrupted by an alarm. */
53 CatchSignal(SIGALRM
, gotalarm_sig
);
54 tdb_setalarm_sigptr(tdb
, &gotalarm
);
58 if (rw_type
== F_RDLCK
)
59 ret
= tdb_chainlock_read(tdb
, key
);
61 ret
= tdb_chainlock(tdb
, key
);
65 tdb_setalarm_sigptr(tdb
, NULL
);
66 CatchSignal(SIGALRM
, SIG_IGN
);
67 if (gotalarm
&& (ret
== -1)) {
68 DEBUG(0,("tdb_chainlock_with_timeout_internal: alarm (%u) timed out for key %s in tdb %s\n",
69 timeout
, key
.dptr
, tdb_name(tdb
)));
70 /* TODO: If we time out waiting for a lock, it might
71 * be nice to use F_GETLK to get the pid of the
72 * process currently holding the lock and print that
73 * as part of the debugging message. -- mbp */
81 /****************************************************************************
82 Write lock a chain. Return -1 if timeout or lock failed.
83 ****************************************************************************/
85 int tdb_chainlock_with_timeout( TDB_CONTEXT
*tdb
, TDB_DATA key
, unsigned int timeout
)
87 return tdb_chainlock_with_timeout_internal(tdb
, key
, timeout
, F_WRLCK
);
90 int tdb_lock_bystring_with_timeout(TDB_CONTEXT
*tdb
, const char *keyval
,
93 TDB_DATA key
= string_term_tdb_data(keyval
);
95 return tdb_chainlock_with_timeout(tdb
, key
, timeout
);
98 /****************************************************************************
99 Read lock a chain by string. Return -1 if timeout or lock failed.
100 ****************************************************************************/
102 int tdb_read_lock_bystring_with_timeout(TDB_CONTEXT
*tdb
, const char *keyval
, unsigned int timeout
)
104 TDB_DATA key
= string_term_tdb_data(keyval
);
106 return tdb_chainlock_with_timeout_internal(tdb
, key
, timeout
, F_RDLCK
);
112 int tdb_trans_store_bystring(TDB_CONTEXT
*tdb
, const char *keystr
,
113 TDB_DATA data
, int flags
)
115 TDB_DATA key
= string_term_tdb_data(keystr
);
117 return tdb_trans_store(tdb
, key
, data
, flags
);
120 /****************************************************************************
121 Useful pair of routines for packing/unpacking data consisting of
122 integers and strings.
123 ****************************************************************************/
125 static size_t tdb_pack_va(uint8
*buf
, int bufsize
, const char *fmt
, va_list ap
)
136 const char *fmt0
= fmt
;
137 int bufsize0
= bufsize
;
140 switch ((c
= *fmt
++)) {
141 case 'b': /* unsigned 8-bit integer */
143 bt
= (uint8
)va_arg(ap
, int);
144 if (bufsize
&& bufsize
>= len
)
147 case 'w': /* unsigned 16-bit integer */
149 w
= (uint16
)va_arg(ap
, int);
150 if (bufsize
&& bufsize
>= len
)
153 case 'd': /* signed 32-bit integer (standard int in most systems) */
155 d
= va_arg(ap
, uint32
);
156 if (bufsize
&& bufsize
>= len
)
159 case 'p': /* pointer */
161 p
= va_arg(ap
, void *);
163 if (bufsize
&& bufsize
>= len
)
166 case 'P': /* null-terminated string */
167 s
= va_arg(ap
,char *);
170 if (bufsize
&& bufsize
>= len
)
173 case 'f': /* null-terminated string */
174 s
= va_arg(ap
,char *);
177 if (bufsize
&& bufsize
>= len
)
180 case 'B': /* fixed-length string */
182 s
= va_arg(ap
, char *);
184 if (bufsize
&& bufsize
>= len
) {
190 DEBUG(0,("Unknown tdb_pack format %c in %s\n",
203 DEBUG(18,("tdb_pack_va(%s, %d) -> %d\n",
204 fmt0
, bufsize0
, (int)PTR_DIFF(buf
, buf0
)));
206 return PTR_DIFF(buf
, buf0
);
209 size_t tdb_pack(uint8
*buf
, int bufsize
, const char *fmt
, ...)
215 result
= tdb_pack_va(buf
, bufsize
, fmt
, ap
);
220 bool tdb_pack_append(TALLOC_CTX
*mem_ctx
, uint8
**buf
, size_t *len
,
221 const char *fmt
, ...)
227 len1
= tdb_pack_va(NULL
, 0, fmt
, ap
);
230 if (mem_ctx
!= NULL
) {
231 *buf
= TALLOC_REALLOC_ARRAY(mem_ctx
, *buf
, uint8
,
234 *buf
= SMB_REALLOC_ARRAY(*buf
, uint8
, (*len
) + len1
);
242 len2
= tdb_pack_va((*buf
)+(*len
), len1
, fmt
, ap
);
254 /****************************************************************************
255 Useful pair of routines for packing/unpacking data consisting of
256 integers and strings.
257 ****************************************************************************/
259 int tdb_unpack(const uint8
*buf
, int bufsize
, const char *fmt
, ...)
270 const uint8
*buf0
= buf
;
271 const char *fmt0
= fmt
;
272 int bufsize0
= bufsize
;
277 switch ((c
=*fmt
++)) {
278 case 'b': /* unsigned 8-bit integer */
280 bt
= va_arg(ap
, uint8
*);
285 case 'w': /* unsigned 16-bit integer */
287 w
= va_arg(ap
, uint16
*);
292 case 'd': /* signed 32-bit integer (standard int in most systems) */
294 d
= va_arg(ap
, uint32
*);
299 case 'p': /* pointer */
301 p
= va_arg(ap
, void **);
305 * This isn't a real pointer - only a token (1 or 0)
306 * to mark the fact a pointer is present.
309 *p
= (void *)(IVAL(buf
, 0) ? (void *)1 : NULL
);
311 case 'P': /* null-terminated string */
312 /* Return malloc'ed string. */
313 ps
= va_arg(ap
,char **);
314 len
= strlen((const char *)buf
) + 1;
315 *ps
= SMB_STRDUP((const char *)buf
);
317 case 'f': /* null-terminated string */
318 s
= va_arg(ap
,char *);
319 len
= strlen((const char *)buf
) + 1;
320 if (bufsize
< len
|| len
> sizeof(fstring
))
324 case 'B': /* fixed-length string */
325 i
= va_arg(ap
, int *);
326 b
= va_arg(ap
, char **);
338 *b
= (char *)SMB_MALLOC(*i
);
341 memcpy(*b
, buf
+4, *i
);
344 DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
357 DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
358 fmt0
, bufsize0
, (int)PTR_DIFF(buf
, buf0
)));
360 return PTR_DIFF(buf
, buf0
);
368 /****************************************************************************
369 Log tdb messages via DEBUG().
370 ****************************************************************************/
372 static void tdb_log(TDB_CONTEXT
*tdb
, enum tdb_debug_level level
, const char *format
, ...)
378 va_start(ap
, format
);
379 ret
= vasprintf(&ptr
, format
, ap
);
382 if ((ret
== -1) || !*ptr
)
385 DEBUG((int)level
, ("tdb(%s): %s", tdb_name(tdb
) ? tdb_name(tdb
) : "unnamed", ptr
));
389 /****************************************************************************
390 Like tdb_open() but also setup a logging function that redirects to
391 the samba DEBUG() system.
392 ****************************************************************************/
394 TDB_CONTEXT
*tdb_open_log(const char *name
, int hash_size
, int tdb_flags
,
395 int open_flags
, mode_t mode
)
398 struct tdb_logging_context log_ctx
;
401 tdb_flags
|= TDB_NOMMAP
;
403 log_ctx
.log_fn
= tdb_log
;
404 log_ctx
.log_private
= NULL
;
406 if ((hash_size
== 0) && (name
!= NULL
)) {
407 const char *base
= strrchr_m(name
, '/');
414 hash_size
= lp_parm_int(-1, "tdb_hashsize", base
, 0);
417 tdb
= tdb_open_ex(name
, hash_size
, tdb_flags
,
418 open_flags
, mode
, &log_ctx
, NULL
);
425 /****************************************************************************
426 tdb_store, wrapped in a transaction. This way we make sure that a process
427 that dies within writing does not leave a corrupt tdb behind.
428 ****************************************************************************/
430 int tdb_trans_store(struct tdb_context
*tdb
, TDB_DATA key
, TDB_DATA dbuf
,
435 if ((res
= tdb_transaction_start(tdb
)) != 0) {
436 DEBUG(5, ("tdb_transaction_start failed\n"));
440 if ((res
= tdb_store(tdb
, key
, dbuf
, flag
)) != 0) {
441 DEBUG(10, ("tdb_store failed\n"));
442 if (tdb_transaction_cancel(tdb
) != 0) {
443 smb_panic("Cancelling transaction failed");
448 if ((res
= tdb_transaction_commit(tdb
)) != 0) {
449 DEBUG(5, ("tdb_transaction_commit failed\n"));
455 /****************************************************************************
456 tdb_delete, wrapped in a transaction. This way we make sure that a process
457 that dies within deleting does not leave a corrupt tdb behind.
458 ****************************************************************************/
460 int tdb_trans_delete(struct tdb_context
*tdb
, TDB_DATA key
)
464 if ((res
= tdb_transaction_start(tdb
)) != 0) {
465 DEBUG(5, ("tdb_transaction_start failed\n"));
469 if ((res
= tdb_delete(tdb
, key
)) != 0) {
470 DEBUG(10, ("tdb_delete failed\n"));
471 if (tdb_transaction_cancel(tdb
) != 0) {
472 smb_panic("Cancelling transaction failed");
477 if ((res
= tdb_transaction_commit(tdb
)) != 0) {
478 DEBUG(5, ("tdb_transaction_commit failed\n"));
485 Log tdb messages via DEBUG().
487 static void tdb_wrap_log(TDB_CONTEXT
*tdb
, enum tdb_debug_level level
,
488 const char *format
, ...) PRINTF_ATTRIBUTE(3,4);
490 static void tdb_wrap_log(TDB_CONTEXT
*tdb
, enum tdb_debug_level level
,
491 const char *format
, ...)
499 case TDB_DEBUG_FATAL
:
502 case TDB_DEBUG_ERROR
:
505 case TDB_DEBUG_WARNING
:
508 case TDB_DEBUG_TRACE
:
515 va_start(ap
, format
);
516 ret
= vasprintf(&ptr
, format
, ap
);
520 const char *name
= tdb_name(tdb
);
521 DEBUG(debuglevel
, ("tdb(%s): %s", name
? name
: "unnamed", ptr
));
526 struct tdb_wrap_private
{
527 struct tdb_context
*tdb
;
529 struct tdb_wrap_private
*next
, *prev
;
532 static struct tdb_wrap_private
*tdb_list
;
534 /* destroy the last connection to a tdb */
535 static int tdb_wrap_private_destructor(struct tdb_wrap_private
*w
)
538 DLIST_REMOVE(tdb_list
, w
);
542 static struct tdb_wrap_private
*tdb_wrap_private_open(TALLOC_CTX
*mem_ctx
,
549 struct tdb_wrap_private
*result
;
550 struct tdb_logging_context log_ctx
;
552 result
= talloc(mem_ctx
, struct tdb_wrap_private
);
553 if (result
== NULL
) {
556 result
->name
= talloc_strdup(result
, name
);
557 if (result
->name
== NULL
) {
561 log_ctx
.log_fn
= tdb_wrap_log
;
563 if (!lp_use_mmap()) {
564 tdb_flags
|= TDB_NOMMAP
;
567 if ((hash_size
== 0) && (name
!= NULL
)) {
569 base
= strrchr_m(name
, '/');
576 hash_size
= lp_parm_int(-1, "tdb_hashsize", base
, 0);
579 result
->tdb
= tdb_open_ex(name
, hash_size
, tdb_flags
,
580 open_flags
, mode
, &log_ctx
, NULL
);
581 if (result
->tdb
== NULL
) {
584 talloc_set_destructor(result
, tdb_wrap_private_destructor
);
585 DLIST_ADD(tdb_list
, result
);
594 wrapped connection to a tdb database
595 to close just talloc_free() the tdb_wrap pointer
597 struct tdb_wrap
*tdb_wrap_open(TALLOC_CTX
*mem_ctx
,
598 const char *name
, int hash_size
, int tdb_flags
,
599 int open_flags
, mode_t mode
)
601 struct tdb_wrap
*result
;
602 struct tdb_wrap_private
*w
;
604 result
= talloc(mem_ctx
, struct tdb_wrap
);
605 if (result
== NULL
) {
609 for (w
=tdb_list
;w
;w
=w
->next
) {
610 if (strcmp(name
, w
->name
) == 0) {
616 w
= tdb_wrap_private_open(result
, name
, hash_size
, tdb_flags
,
620 * Correctly use talloc_reference: The tdb will be
621 * closed when "w" is being freed. The caller never
622 * sees "w", so an incorrect use of talloc_free(w)
623 * instead of calling talloc_unlink is not possible.
624 * To avoid having to refcount ourselves, "w" will
625 * have multiple parents that hang off all the
626 * tdb_wrap's being returned from here. Those parents
627 * can be freed without problem.
629 if (talloc_reference(result
, w
) == NULL
) {
636 result
->tdb
= w
->tdb
;
643 NTSTATUS
map_nt_error_from_tdb(enum TDB_ERROR err
)
645 NTSTATUS result
= NT_STATUS_INTERNAL_ERROR
;
649 result
= NT_STATUS_OK
;
651 case TDB_ERR_CORRUPT
:
652 result
= NT_STATUS_INTERNAL_DB_CORRUPTION
;
655 result
= NT_STATUS_UNEXPECTED_IO_ERROR
;
658 result
= NT_STATUS_NO_MEMORY
;
661 result
= NT_STATUS_OBJECT_NAME_COLLISION
;
666 * TDB_ERR_LOCK is very broad, we could for example
667 * distinguish between fcntl locks and invalid lock
668 * sequences. So NT_STATUS_FILE_LOCK_CONFLICT is a
671 result
= NT_STATUS_FILE_LOCK_CONFLICT
;
675 case TDB_ERR_LOCK_TIMEOUT
:
677 * These two ones in the enum are not actually used
679 result
= NT_STATUS_FILE_LOCK_CONFLICT
;
681 case TDB_ERR_NOEXIST
:
682 result
= NT_STATUS_NOT_FOUND
;
685 result
= NT_STATUS_INVALID_PARAMETER
;
688 result
= NT_STATUS_ACCESS_DENIED
;
690 case TDB_ERR_NESTING
:
691 result
= NT_STATUS_INTERNAL_ERROR
;
697 int tdb_data_cmp(TDB_DATA t1
, TDB_DATA t2
)
700 if (t1
.dptr
== NULL
&& t2
.dptr
!= NULL
) {
703 if (t1
.dptr
!= NULL
&& t2
.dptr
== NULL
) {
706 if (t1
.dptr
== t2
.dptr
) {
707 return t1
.dsize
- t2
.dsize
;
709 ret
= memcmp(t1
.dptr
, t2
.dptr
, MIN(t1
.dsize
, t2
.dsize
));
711 return t1
.dsize
- t2
.dsize
;