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(void)
42 /***************************************************************
43 Make a TDB_DATA and keep the const warning in one place
44 ****************************************************************/
46 TDB_DATA make_tdb_data(const uint8 *dptr, size_t dsize)
49 ret.dptr = CONST_DISCARD(uint8 *, dptr);
54 TDB_DATA string_tdb_data(const char *string)
56 return make_tdb_data((const uint8 *)string, string ? strlen(string) : 0 );
59 TDB_DATA string_term_tdb_data(const char *string)
61 return make_tdb_data((const uint8 *)string, string ? strlen(string) + 1 : 0);
64 /****************************************************************************
65 Lock a chain with timeout (in seconds).
66 ****************************************************************************/
68 static int tdb_chainlock_with_timeout_internal( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout, int rw_type)
70 /* Allow tdb_chainlock to be interrupted by an alarm. */
75 CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
76 tdb_setalarm_sigptr(tdb, &gotalarm);
80 if (rw_type == F_RDLCK)
81 ret = tdb_chainlock_read(tdb, key);
83 ret = tdb_chainlock(tdb, key);
87 tdb_setalarm_sigptr(tdb, NULL);
88 CatchSignal(SIGALRM, SIGNAL_CAST SIG_IGN);
90 DEBUG(0,("tdb_chainlock_with_timeout_internal: alarm (%u) timed out for key %s in tdb %s\n",
91 timeout, key.dptr, tdb_name(tdb)));
92 /* TODO: If we time out waiting for a lock, it might
93 * be nice to use F_GETLK to get the pid of the
94 * process currently holding the lock and print that
95 * as part of the debugging message. -- mbp */
103 /****************************************************************************
104 Write lock a chain. Return -1 if timeout or lock failed.
105 ****************************************************************************/
107 int tdb_chainlock_with_timeout( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout)
109 return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_WRLCK);
112 /****************************************************************************
113 Lock a chain by string. Return -1 if timeout or lock failed.
114 ****************************************************************************/
116 int tdb_lock_bystring(TDB_CONTEXT *tdb, const char *keyval)
118 TDB_DATA key = string_term_tdb_data(keyval);
120 return tdb_chainlock(tdb, key);
123 int tdb_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval,
126 TDB_DATA key = string_term_tdb_data(keyval);
128 return tdb_chainlock_with_timeout(tdb, key, timeout);
131 /****************************************************************************
132 Unlock a chain by string.
133 ****************************************************************************/
135 void tdb_unlock_bystring(TDB_CONTEXT *tdb, const char *keyval)
137 TDB_DATA key = string_term_tdb_data(keyval);
139 tdb_chainunlock(tdb, key);
142 /****************************************************************************
143 Read lock a chain by string. Return -1 if timeout or lock failed.
144 ****************************************************************************/
146 int tdb_read_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval, unsigned int timeout)
148 TDB_DATA key = string_term_tdb_data(keyval);
150 return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_RDLCK);
153 /****************************************************************************
154 Read unlock a chain by string.
155 ****************************************************************************/
157 void tdb_read_unlock_bystring(TDB_CONTEXT *tdb, const char *keyval)
159 TDB_DATA key = string_term_tdb_data(keyval);
161 tdb_chainunlock_read(tdb, key);
165 /****************************************************************************
166 Fetch a int32 value by a arbitrary blob key, return -1 if not found.
167 Output is int32 in native byte order.
168 ****************************************************************************/
170 int32 tdb_fetch_int32_byblob(TDB_CONTEXT *tdb, TDB_DATA key)
175 data = tdb_fetch(tdb, key);
176 if (!data.dptr || data.dsize != sizeof(int32)) {
177 SAFE_FREE(data.dptr);
181 ret = IVAL(data.dptr,0);
182 SAFE_FREE(data.dptr);
186 /****************************************************************************
187 Fetch a int32 value by string key, return -1 if not found.
188 Output is int32 in native byte order.
189 ****************************************************************************/
191 int32 tdb_fetch_int32(TDB_CONTEXT *tdb, const char *keystr)
193 TDB_DATA key = string_term_tdb_data(keystr);
195 return tdb_fetch_int32_byblob(tdb, key);
198 /****************************************************************************
199 Store a int32 value by an arbitary blob key, return 0 on success, -1 on failure.
200 Input is int32 in native byte order. Output in tdb is in little-endian.
201 ****************************************************************************/
203 int tdb_store_int32_byblob(TDB_CONTEXT *tdb, TDB_DATA key, int32 v)
209 data.dptr = (uint8 *)&v_store;
210 data.dsize = sizeof(int32);
212 return tdb_store(tdb, key, data, TDB_REPLACE);
215 /****************************************************************************
216 Store a int32 value by string key, return 0 on success, -1 on failure.
217 Input is int32 in native byte order. Output in tdb is in little-endian.
218 ****************************************************************************/
220 int tdb_store_int32(TDB_CONTEXT *tdb, const char *keystr, int32 v)
222 TDB_DATA key = string_term_tdb_data(keystr);
224 return tdb_store_int32_byblob(tdb, key, v);
227 /****************************************************************************
228 Fetch a uint32 value by a arbitrary blob key, return -1 if not found.
229 Output is uint32 in native byte order.
230 ****************************************************************************/
232 bool tdb_fetch_uint32_byblob(TDB_CONTEXT *tdb, TDB_DATA key, uint32 *value)
236 data = tdb_fetch(tdb, key);
237 if (!data.dptr || data.dsize != sizeof(uint32)) {
238 SAFE_FREE(data.dptr);
242 *value = IVAL(data.dptr,0);
243 SAFE_FREE(data.dptr);
247 /****************************************************************************
248 Fetch a uint32 value by string key, return -1 if not found.
249 Output is uint32 in native byte order.
250 ****************************************************************************/
252 bool tdb_fetch_uint32(TDB_CONTEXT *tdb, const char *keystr, uint32 *value)
254 TDB_DATA key = string_term_tdb_data(keystr);
256 return tdb_fetch_uint32_byblob(tdb, key, value);
259 /****************************************************************************
260 Store a uint32 value by an arbitary blob key, return 0 on success, -1 on failure.
261 Input is uint32 in native byte order. Output in tdb is in little-endian.
262 ****************************************************************************/
264 bool tdb_store_uint32_byblob(TDB_CONTEXT *tdb, TDB_DATA key, uint32 value)
270 SIVAL(&v_store, 0, value);
271 data.dptr = (uint8 *)&v_store;
272 data.dsize = sizeof(uint32);
274 if (tdb_store(tdb, key, data, TDB_REPLACE) == -1)
280 /****************************************************************************
281 Store a uint32 value by string key, return 0 on success, -1 on failure.
282 Input is uint32 in native byte order. Output in tdb is in little-endian.
283 ****************************************************************************/
285 bool tdb_store_uint32(TDB_CONTEXT *tdb, const char *keystr, uint32 value)
287 TDB_DATA key = string_term_tdb_data(keystr);
289 return tdb_store_uint32_byblob(tdb, key, value);
291 /****************************************************************************
292 Store a buffer by a null terminated string key. Return 0 on success, -1
294 ****************************************************************************/
296 int tdb_store_bystring(TDB_CONTEXT *tdb, const char *keystr, TDB_DATA data, int flags)
298 TDB_DATA key = string_term_tdb_data(keystr);
300 return tdb_store(tdb, key, data, flags);
303 int tdb_trans_store_bystring(TDB_CONTEXT *tdb, const char *keystr,
304 TDB_DATA data, int flags)
306 TDB_DATA key = string_term_tdb_data(keystr);
308 return tdb_trans_store(tdb, key, data, flags);
311 /****************************************************************************
312 Fetch a buffer using a null terminated string key. Don't forget to call
313 free() on the result dptr.
314 ****************************************************************************/
316 TDB_DATA tdb_fetch_bystring(TDB_CONTEXT *tdb, const char *keystr)
318 TDB_DATA key = string_term_tdb_data(keystr);
320 return tdb_fetch(tdb, key);
323 /****************************************************************************
324 Delete an entry using a null terminated string key.
325 ****************************************************************************/
327 int tdb_delete_bystring(TDB_CONTEXT *tdb, const char *keystr)
329 TDB_DATA key = string_term_tdb_data(keystr);
331 return tdb_delete(tdb, key);
334 /****************************************************************************
335 Atomic integer change. Returns old value. To create, set initial value in *oldval.
336 ****************************************************************************/
338 int32 tdb_change_int32_atomic(TDB_CONTEXT *tdb, const char *keystr, int32 *oldval, int32 change_val)
343 if (tdb_lock_bystring(tdb, keystr) == -1)
346 if ((val = tdb_fetch_int32(tdb, keystr)) == -1) {
347 /* The lookup failed */
348 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
349 /* but not because it didn't exist */
353 /* Start with 'old' value */
357 /* It worked, set return value (oldval) to tdb data */
361 /* Increment value for storage and return next time */
364 if (tdb_store_int32(tdb, keystr, val) == -1)
371 tdb_unlock_bystring(tdb, keystr);
375 /****************************************************************************
376 Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval.
377 ****************************************************************************/
379 bool tdb_change_uint32_atomic(TDB_CONTEXT *tdb, const char *keystr, uint32 *oldval, uint32 change_val)
384 if (tdb_lock_bystring(tdb, keystr) == -1)
387 if (!tdb_fetch_uint32(tdb, keystr, &val)) {
389 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
390 /* and not because it didn't exist */
394 /* Start with 'old' value */
398 /* it worked, set return value (oldval) to tdb data */
403 /* get a new value to store */
406 if (!tdb_store_uint32(tdb, keystr, val))
413 tdb_unlock_bystring(tdb, keystr);
417 /****************************************************************************
418 Useful pair of routines for packing/unpacking data consisting of
419 integers and strings.
420 ****************************************************************************/
422 static size_t tdb_pack_va(uint8 *buf, int bufsize, const char *fmt, va_list ap)
433 const char *fmt0 = fmt;
434 int bufsize0 = bufsize;
437 switch ((c = *fmt++)) {
438 case 'b': /* unsigned 8-bit integer */
440 bt = (uint8)va_arg(ap, int);
441 if (bufsize && bufsize >= len)
444 case 'w': /* unsigned 16-bit integer */
446 w = (uint16)va_arg(ap, int);
447 if (bufsize && bufsize >= len)
450 case 'd': /* signed 32-bit integer (standard int in most systems) */
452 d = va_arg(ap, uint32);
453 if (bufsize && bufsize >= len)
456 case 'p': /* pointer */
458 p = va_arg(ap, void *);
460 if (bufsize && bufsize >= len)
463 case 'P': /* null-terminated string */
464 s = va_arg(ap,char *);
467 if (bufsize && bufsize >= len)
470 case 'f': /* null-terminated string */
471 s = va_arg(ap,char *);
474 if (bufsize && bufsize >= len)
477 case 'B': /* fixed-length string */
479 s = va_arg(ap, char *);
481 if (bufsize && bufsize >= len) {
487 DEBUG(0,("Unknown tdb_pack format %c in %s\n",
500 DEBUG(18,("tdb_pack_va(%s, %d) -> %d\n",
501 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
503 return PTR_DIFF(buf, buf0);
506 size_t tdb_pack(uint8 *buf, int bufsize, const char *fmt, ...)
512 result = tdb_pack_va(buf, bufsize, fmt, ap);
517 bool tdb_pack_append(TALLOC_CTX *mem_ctx, uint8 **buf, size_t *len,
518 const char *fmt, ...)
524 len1 = tdb_pack_va(NULL, 0, fmt, ap);
527 if (mem_ctx != NULL) {
528 *buf = TALLOC_REALLOC_ARRAY(mem_ctx, *buf, uint8,
531 *buf = SMB_REALLOC_ARRAY(*buf, uint8, (*len) + len1);
539 len2 = tdb_pack_va((*buf)+(*len), len1, fmt, ap);
551 /****************************************************************************
552 Useful pair of routines for packing/unpacking data consisting of
553 integers and strings.
554 ****************************************************************************/
556 int tdb_unpack(const uint8 *buf, int bufsize, const char *fmt, ...)
567 const uint8 *buf0 = buf;
568 const char *fmt0 = fmt;
569 int bufsize0 = bufsize;
574 switch ((c=*fmt++)) {
577 bt = va_arg(ap, uint8 *);
584 w = va_arg(ap, uint16 *);
591 d = va_arg(ap, uint32 *);
598 p = va_arg(ap, void **);
602 * This isn't a real pointer - only a token (1 or 0)
603 * to mark the fact a pointer is present.
606 *p = (void *)(IVAL(buf, 0) ? (void *)1 : NULL);
609 /* Return malloc'ed string. */
610 ps = va_arg(ap,char **);
611 len = strlen((const char *)buf) + 1;
612 *ps = SMB_STRDUP((const char *)buf);
615 s = va_arg(ap,char *);
616 len = strlen((const char *)buf) + 1;
617 if (bufsize < len || len > sizeof(fstring))
622 i = va_arg(ap, int *);
623 b = va_arg(ap, char **);
635 *b = (char *)SMB_MALLOC(*i);
638 memcpy(*b, buf+4, *i);
641 DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
654 DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
655 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
657 return PTR_DIFF(buf, buf0);
665 /****************************************************************************
666 Log tdb messages via DEBUG().
667 ****************************************************************************/
669 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, const char *format, ...)
675 va_start(ap, format);
676 ret = vasprintf(&ptr, format, ap);
679 if ((ret == -1) || !*ptr)
682 DEBUG((int)level, ("tdb(%s): %s", tdb_name(tdb) ? tdb_name(tdb) : "unnamed", ptr));
686 /****************************************************************************
687 Like tdb_open() but also setup a logging function that redirects to
688 the samba DEBUG() system.
689 ****************************************************************************/
691 TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
692 int open_flags, mode_t mode)
695 struct tdb_logging_context log_ctx;
698 tdb_flags |= TDB_NOMMAP;
700 log_ctx.log_fn = tdb_log;
701 log_ctx.log_private = NULL;
703 if ((hash_size == 0) && (name != NULL)) {
704 const char *base = strrchr_m(name, '/');
711 hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
714 tdb = tdb_open_ex(name, hash_size, tdb_flags,
715 open_flags, mode, &log_ctx, NULL);
722 /****************************************************************************
723 Allow tdb_delete to be used as a tdb_traversal_fn.
724 ****************************************************************************/
726 static int tdb_traverse_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key,
727 TDB_DATA dbuf, void *state)
729 return tdb_delete(the_tdb, key);
732 int tdb_wipe(TDB_CONTEXT *tdb)
734 return tdb_traverse(tdb, tdb_traverse_delete_fn, NULL);
741 * Search across the whole tdb for keys that match the given pattern
742 * return the result as a list of keys
744 * @param tdb pointer to opened tdb file context
745 * @param pattern searching pattern used by fnmatch(3) functions
747 * @return list of keys found by looking up with given pattern
749 TDB_LIST_NODE *tdb_search_keys(TDB_CONTEXT *tdb, const char* pattern)
752 TDB_LIST_NODE *list = NULL;
753 TDB_LIST_NODE *rec = NULL;
755 for (key = tdb_firstkey(tdb); key.dptr; key = next) {
756 /* duplicate key string to ensure null-termination */
757 char *key_str = SMB_STRNDUP((const char *)key.dptr, key.dsize);
759 DEBUG(0, ("tdb_search_keys: strndup() failed!\n"));
760 smb_panic("strndup failed!\n");
763 DEBUG(18, ("checking %s for match to pattern %s\n", key_str, pattern));
765 next = tdb_nextkey(tdb, key);
767 /* do the pattern checking */
768 if (fnmatch(pattern, key_str, 0) == 0) {
769 rec = SMB_MALLOC_P(TDB_LIST_NODE);
774 DLIST_ADD_END(list, rec, TDB_LIST_NODE *);
776 DEBUG(18, ("checking %s matched pattern %s\n", key_str, pattern));
781 /* free duplicated key string */
791 * Free the list returned by tdb_search_keys
793 * @param node list of results found by tdb_search_keys
795 void tdb_search_list_free(TDB_LIST_NODE* node)
797 TDB_LIST_NODE *next_node;
800 next_node = node->next;
801 SAFE_FREE(node->node_key.dptr);
807 /****************************************************************************
808 tdb_store, wrapped in a transaction. This way we make sure that a process
809 that dies within writing does not leave a corrupt tdb behind.
810 ****************************************************************************/
812 int tdb_trans_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
817 if ((res = tdb_transaction_start(tdb)) != 0) {
818 DEBUG(5, ("tdb_transaction_start failed\n"));
822 if ((res = tdb_store(tdb, key, dbuf, flag)) != 0) {
823 DEBUG(10, ("tdb_store failed\n"));
824 if (tdb_transaction_cancel(tdb) != 0) {
825 smb_panic("Cancelling transaction failed");
830 if ((res = tdb_transaction_commit(tdb)) != 0) {
831 DEBUG(5, ("tdb_transaction_commit failed\n"));
837 /****************************************************************************
838 tdb_delete, wrapped in a transaction. This way we make sure that a process
839 that dies within deleting does not leave a corrupt tdb behind.
840 ****************************************************************************/
842 int tdb_trans_delete(struct tdb_context *tdb, TDB_DATA key)
846 if ((res = tdb_transaction_start(tdb)) != 0) {
847 DEBUG(5, ("tdb_transaction_start failed\n"));
851 if ((res = tdb_delete(tdb, key)) != 0) {
852 DEBUG(10, ("tdb_delete failed\n"));
853 if (tdb_transaction_cancel(tdb) != 0) {
854 smb_panic("Cancelling transaction failed");
859 if ((res = tdb_transaction_commit(tdb)) != 0) {
860 DEBUG(5, ("tdb_transaction_commit failed\n"));
867 Log tdb messages via DEBUG().
869 static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
870 const char *format, ...) PRINTF_ATTRIBUTE(3,4);
872 static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
873 const char *format, ...)
881 case TDB_DEBUG_FATAL:
884 case TDB_DEBUG_ERROR:
887 case TDB_DEBUG_WARNING:
890 case TDB_DEBUG_TRACE:
897 va_start(ap, format);
898 ret = vasprintf(&ptr, format, ap);
902 const char *name = tdb_name(tdb);
903 DEBUG(debuglevel, ("tdb(%s): %s", name ? name : "unnamed", ptr));
908 static struct tdb_wrap *tdb_list;
910 /* destroy the last connection to a tdb */
911 static int tdb_wrap_destructor(struct tdb_wrap *w)
914 DLIST_REMOVE(tdb_list, w);
919 wrapped connection to a tdb database
920 to close just talloc_free() the tdb_wrap pointer
922 struct tdb_wrap *tdb_wrap_open(TALLOC_CTX *mem_ctx,
923 const char *name, int hash_size, int tdb_flags,
924 int open_flags, mode_t mode)
927 struct tdb_logging_context log_ctx;
928 log_ctx.log_fn = tdb_wrap_log;
931 tdb_flags |= TDB_NOMMAP;
933 for (w=tdb_list;w;w=w->next) {
934 if (strcmp(name, w->name) == 0) {
936 * Yes, talloc_reference is exactly what we want
937 * here. Otherwise we would have to implement our own
938 * reference counting.
940 return talloc_reference(mem_ctx, w);
944 w = talloc(mem_ctx, struct tdb_wrap);
949 if (!(w->name = talloc_strdup(w, name))) {
954 if ((hash_size == 0) && (name != NULL)) {
955 const char *base = strrchr_m(name, '/');
962 hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
965 w->tdb = tdb_open_ex(name, hash_size, tdb_flags,
966 open_flags, mode, &log_ctx, NULL);
967 if (w->tdb == NULL) {
972 talloc_set_destructor(w, tdb_wrap_destructor);
974 DLIST_ADD(tdb_list, w);
979 NTSTATUS map_nt_error_from_tdb(enum TDB_ERROR err)
981 struct { enum TDB_ERROR err; NTSTATUS status; } map[] =
982 { { TDB_SUCCESS, NT_STATUS_OK },
983 { TDB_ERR_CORRUPT, NT_STATUS_INTERNAL_DB_CORRUPTION },
984 { TDB_ERR_IO, NT_STATUS_UNEXPECTED_IO_ERROR },
985 { TDB_ERR_OOM, NT_STATUS_NO_MEMORY },
986 { TDB_ERR_EXISTS, NT_STATUS_OBJECT_NAME_COLLISION },
989 * TDB_ERR_LOCK is very broad, we could for example
990 * distinguish between fcntl locks and invalid lock
991 * sequences. So NT_STATUS_FILE_LOCK_CONFLICT is a
994 { TDB_ERR_LOCK, NT_STATUS_FILE_LOCK_CONFLICT },
996 * The next two ones in the enum are not actually used
998 { TDB_ERR_NOLOCK, NT_STATUS_FILE_LOCK_CONFLICT },
999 { TDB_ERR_LOCK_TIMEOUT, NT_STATUS_FILE_LOCK_CONFLICT },
1000 { TDB_ERR_NOEXIST, NT_STATUS_NOT_FOUND },
1001 { TDB_ERR_EINVAL, NT_STATUS_INVALID_PARAMETER },
1002 { TDB_ERR_RDONLY, NT_STATUS_ACCESS_DENIED }
1007 for (i=0; i < sizeof(map) / sizeof(map[0]); i++) {
1008 if (err == map[i].err) {
1009 return map[i].status;
1013 return NT_STATUS_INTERNAL_ERROR;
1017 /*********************************************************************
1018 * the following is a generic validation mechanism for tdbs.
1019 *********************************************************************/
1022 * internal validation function, executed by the child.
1024 static int tdb_validate_child(struct tdb_context *tdb,
1025 tdb_validate_data_func validate_fn)
1028 int num_entries = 0;
1029 struct tdb_validation_status v_status;
1031 v_status.tdb_error = False;
1032 v_status.bad_freelist = False;
1033 v_status.bad_entry = False;
1034 v_status.unknown_key = False;
1035 v_status.success = True;
1038 v_status.tdb_error = True;
1039 v_status.success = False;
1043 /* Check if the tdb's freelist is good. */
1044 if (tdb_validate_freelist(tdb, &num_entries) == -1) {
1045 v_status.bad_freelist = True;
1046 v_status.success = False;
1050 DEBUG(10,("tdb_validate_child: tdb %s freelist has %d entries\n",
1051 tdb_name(tdb), num_entries));
1053 /* Now traverse the tdb to validate it. */
1054 num_entries = tdb_traverse(tdb, validate_fn, (void *)&v_status);
1055 if (!v_status.success) {
1057 } else if (num_entries == -1) {
1058 v_status.tdb_error = True;
1059 v_status.success = False;
1063 DEBUG(10,("tdb_validate_child: tdb %s is good with %d entries\n",
1064 tdb_name(tdb), num_entries));
1065 ret = 0; /* Cache is good. */
1068 DEBUG(10, ("tdb_validate_child: summary of validation status:\n"));
1069 DEBUGADD(10,(" * tdb error: %s\n", v_status.tdb_error ? "yes" : "no"));
1070 DEBUGADD(10,(" * bad freelist: %s\n",v_status.bad_freelist?"yes":"no"));
1071 DEBUGADD(10,(" * bad entry: %s\n", v_status.bad_entry ? "yes" : "no"));
1072 DEBUGADD(10,(" * unknown key: %s\n", v_status.unknown_key?"yes":"no"));
1073 DEBUGADD(10,(" => overall success: %s\n", v_status.success?"yes":"no"));
1079 * tdb validation function.
1080 * returns 0 if tdb is ok, != 0 if it isn't.
1081 * this function expects an opened tdb.
1083 int tdb_validate(struct tdb_context *tdb, tdb_validate_data_func validate_fn)
1085 pid_t child_pid = -1;
1086 int child_status = 0;
1091 DEBUG(1, ("Error: tdb_validate called with tdb == NULL\n"));
1095 DEBUG(5, ("tdb_validate called for tdb '%s'\n", tdb_name(tdb)));
1097 /* fork and let the child do the validation.
1098 * benefit: no need to twist signal handlers and panic functions.
1099 * just let the child panic. we catch the signal. */
1101 DEBUG(10, ("tdb_validate: forking to let child do validation.\n"));
1102 child_pid = sys_fork();
1103 if (child_pid == 0) {
1105 DEBUG(10, ("tdb_validate (validation child): created\n"));
1106 DEBUG(10, ("tdb_validate (validation child): "
1107 "calling tdb_validate_child\n"));
1108 exit(tdb_validate_child(tdb, validate_fn));
1110 else if (child_pid < 0) {
1111 DEBUG(1, ("tdb_validate: fork for validation failed.\n"));
1117 DEBUG(10, ("tdb_validate: fork succeeded, child PID = %d\n",child_pid));
1119 DEBUG(10, ("tdb_validate: waiting for child to finish...\n"));
1120 while ((wait_pid = sys_waitpid(child_pid, &child_status, 0)) < 0) {
1121 if (errno == EINTR) {
1122 DEBUG(10, ("tdb_validate: got signal during waitpid, "
1127 DEBUG(1, ("tdb_validate: waitpid failed with error '%s'.\n",
1131 if (wait_pid != child_pid) {
1132 DEBUG(1, ("tdb_validate: waitpid returned pid %d, "
1133 "but %d was expected\n", wait_pid, child_pid));
1137 DEBUG(10, ("tdb_validate: validating child returned.\n"));
1138 if (WIFEXITED(child_status)) {
1139 DEBUG(10, ("tdb_validate: child exited, code %d.\n",
1140 WEXITSTATUS(child_status)));
1141 ret = WEXITSTATUS(child_status);
1143 if (WIFSIGNALED(child_status)) {
1144 DEBUG(10, ("tdb_validate: child terminated by signal %d\n",
1145 WTERMSIG(child_status)));
1147 if (WCOREDUMP(child_status)) {
1148 DEBUGADD(10, ("core dumped\n"));
1151 ret = WTERMSIG(child_status);
1153 if (WIFSTOPPED(child_status)) {
1154 DEBUG(10, ("tdb_validate: child was stopped by signal %d\n",
1155 WSTOPSIG(child_status)));
1156 ret = WSTOPSIG(child_status);
1160 DEBUG(5, ("tdb_validate returning code '%d' for tdb '%s'\n", ret,
1167 * tdb validation function.
1168 * returns 0 if tdb is ok, != 0 if it isn't.
1169 * this is a wrapper around the actual validation function that opens and closes
1172 int tdb_validate_open(const char *tdb_path, tdb_validate_data_func validate_fn)
1174 TDB_CONTEXT *tdb = NULL;
1177 DEBUG(5, ("tdb_validate_open called for tdb '%s'\n", tdb_path));
1179 tdb = tdb_open_log(tdb_path, 0, TDB_DEFAULT, O_RDONLY, 0);
1181 DEBUG(1, ("Error opening tdb %s\n", tdb_path));
1185 ret = tdb_validate(tdb, validate_fn);
1191 * tdb backup function and helpers for tdb_validate wrapper with backup
1195 /* this structure eliminates the need for a global overall status for
1196 * the traverse-copy */
1197 struct tdb_copy_data {
1198 struct tdb_context *dst;
1202 static int traverse_copy_fn(struct tdb_context *tdb, TDB_DATA key,
1203 TDB_DATA dbuf, void *private_data)
1205 struct tdb_copy_data *data = (struct tdb_copy_data *)private_data;
1207 if (tdb_store(data->dst, key, dbuf, TDB_INSERT) != 0) {
1208 DEBUG(4, ("Failed to insert into %s: %s\n", tdb_name(data->dst),
1210 data->success = False;
1216 static int tdb_copy(struct tdb_context *src, struct tdb_context *dst)
1218 struct tdb_copy_data data;
1222 data.success = True;
1224 count = tdb_traverse(src, traverse_copy_fn, (void *)(&data));
1225 if ((count < 0) || (data.success == False)) {
1231 static int tdb_verify_basic(struct tdb_context *tdb)
1233 return tdb_traverse(tdb, NULL, NULL);
1236 /* this backup function is essentially taken from lib/tdb/tools/tdbbackup.tdb
1238 static int tdb_backup(TALLOC_CTX *ctx, const char *src_path,
1239 const char *dst_path, int hash_size)
1241 struct tdb_context *src_tdb = NULL;
1242 struct tdb_context *dst_tdb = NULL;
1243 char *tmp_path = NULL;
1246 int saved_errno = 0;
1249 if (stat(src_path, &st) != 0) {
1250 DEBUG(3, ("Could not stat '%s': %s\n", src_path,
1255 /* open old tdb RDWR - so we can lock it */
1256 src_tdb = tdb_open_log(src_path, 0, TDB_DEFAULT, O_RDWR, 0);
1257 if (src_tdb == NULL) {
1258 DEBUG(3, ("Failed to open tdb '%s'\n", src_path));
1262 if (tdb_lockall(src_tdb) != 0) {
1263 DEBUG(3, ("Failed to lock tdb '%s'\n", src_path));
1267 tmp_path = talloc_asprintf(ctx, "%s%s", dst_path, ".tmp");
1269 dst_tdb = tdb_open_log(tmp_path,
1270 hash_size ? hash_size : tdb_hash_size(src_tdb),
1271 TDB_DEFAULT, O_RDWR | O_CREAT | O_EXCL,
1273 if (dst_tdb == NULL) {
1274 DEBUG(3, ("Error creating tdb '%s': %s\n", tmp_path,
1276 saved_errno = errno;
1281 count1 = tdb_copy(src_tdb, dst_tdb);
1283 DEBUG(3, ("Failed to copy tdb '%s': %s\n", src_path,
1289 /* reopen ro and do basic verification */
1291 dst_tdb = tdb_open_log(tmp_path, 0, TDB_DEFAULT, O_RDONLY, 0);
1293 DEBUG(3, ("Failed to reopen tdb '%s': %s\n", tmp_path,
1297 count2 = tdb_verify_basic(dst_tdb);
1298 if (count2 != count1) {
1299 DEBUG(3, ("Failed to verify result of copying tdb '%s'.\n",
1305 DEBUG(10, ("tdb_backup: successfully copied %d entries\n", count1));
1307 /* make sure the new tdb has reached stable storage
1308 * then rename it to its destination */
1309 fsync(tdb_fd(dst_tdb));
1312 if (rename(tmp_path, dst_path) != 0) {
1313 DEBUG(3, ("Failed to rename '%s' to '%s': %s\n",
1314 tmp_path, dst_path, strerror(errno)));
1322 if (src_tdb != NULL) {
1325 if (tmp_path != NULL) {
1327 TALLOC_FREE(tmp_path);
1329 if (saved_errno != 0) {
1330 errno = saved_errno;
1335 static int rename_file_with_suffix(TALLOC_CTX *ctx, const char *path,
1341 dst_path = talloc_asprintf(ctx, "%s%s", path, suffix);
1343 ret = (rename(path, dst_path) != 0);
1346 DEBUG(5, ("moved '%s' to '%s'\n", path, dst_path));
1347 } else if (errno == ENOENT) {
1348 DEBUG(3, ("file '%s' does not exist - so not moved\n", path));
1351 DEBUG(3, ("error renaming %s to %s: %s\n", path, dst_path,
1355 TALLOC_FREE(dst_path);
1360 * do a backup of a tdb, moving the destination out of the way first
1362 static int tdb_backup_with_rotate(TALLOC_CTX *ctx, const char *src_path,
1363 const char *dst_path, int hash_size,
1364 const char *rotate_suffix,
1365 bool retry_norotate_if_nospc,
1366 bool rename_as_last_resort_if_nospc)
1370 rename_file_with_suffix(ctx, dst_path, rotate_suffix);
1372 ret = tdb_backup(ctx, src_path, dst_path, hash_size);
1375 DEBUG(10, ("backup of %s failed: %s\n", src_path, strerror(errno)));
1377 if ((ret != 0) && (errno == ENOSPC) && retry_norotate_if_nospc)
1379 char *rotate_path = talloc_asprintf(ctx, "%s%s", dst_path,
1381 DEBUG(10, ("backup of %s failed due to lack of space\n",
1383 DEBUGADD(10, ("trying to free some space by removing rotated "
1384 "dst %s\n", rotate_path));
1385 if (unlink(rotate_path) == -1) {
1386 DEBUG(10, ("unlink of %s failed: %s\n", rotate_path,
1389 ret = tdb_backup(ctx, src_path, dst_path, hash_size);
1391 TALLOC_FREE(rotate_path);
1394 if ((ret != 0) && (errno == ENOSPC) && rename_as_last_resort_if_nospc)
1396 DEBUG(10, ("backup of %s failed due to lack of space\n",
1398 DEBUGADD(10, ("using 'rename' as a last resort\n"));
1399 ret = rename(src_path, dst_path);
1406 * validation function with backup handling:
1408 * - calls tdb_validate
1409 * - if the tdb is ok, create a backup "name.bak", possibly moving
1410 * existing backup to name.bak.old,
1411 * return 0 (success) even if the backup fails
1412 * - if the tdb is corrupt:
1413 * - move the tdb to "name.corrupt"
1414 * - check if there is valid backup.
1415 * if so, restore the backup.
1416 * if restore is successful, return 0 (success),
1417 * - otherwise return -1 (failure)
1419 int tdb_validate_and_backup(const char *tdb_path,
1420 tdb_validate_data_func validate_fn)
1423 const char *backup_suffix = ".bak";
1424 const char *corrupt_suffix = ".corrupt";
1425 const char *rotate_suffix = ".old";
1426 char *tdb_path_backup;
1428 TALLOC_CTX *ctx = NULL;
1430 ctx = talloc_new(NULL);
1432 DEBUG(0, ("tdb_validate_and_backup: out of memory\n"));
1436 tdb_path_backup = talloc_asprintf(ctx, "%s%s", tdb_path, backup_suffix);
1438 ret = tdb_validate_open(tdb_path, validate_fn);
1441 DEBUG(1, ("tdb '%s' is valid\n", tdb_path));
1442 ret = tdb_backup_with_rotate(ctx, tdb_path, tdb_path_backup, 0,
1443 rotate_suffix, True, False);
1445 DEBUG(1, ("Error creating backup of tdb '%s'\n",
1447 /* the actual validation was successful: */
1450 DEBUG(1, ("Created backup '%s' of tdb '%s'\n",
1451 tdb_path_backup, tdb_path));
1454 DEBUG(1, ("tdb '%s' is invalid\n", tdb_path));
1456 ret =stat(tdb_path_backup, &st);
1458 DEBUG(5, ("Could not stat '%s': %s\n", tdb_path_backup,
1460 DEBUG(1, ("No backup found.\n"));
1462 DEBUG(1, ("backup '%s' found.\n", tdb_path_backup));
1463 ret = tdb_validate_open(tdb_path_backup, validate_fn);
1465 DEBUG(1, ("Backup '%s' is invalid.\n",
1471 int renamed = rename_file_with_suffix(ctx, tdb_path,
1474 DEBUG(1, ("Error moving tdb to '%s%s'\n",
1475 tdb_path, corrupt_suffix));
1477 DEBUG(1, ("Corrupt tdb stored as '%s%s'\n",
1478 tdb_path, corrupt_suffix));
1483 DEBUG(1, ("valid backup '%s' found\n", tdb_path_backup));
1484 ret = tdb_backup_with_rotate(ctx, tdb_path_backup, tdb_path, 0,
1485 corrupt_suffix, True, True);
1487 DEBUG(1, ("Error restoring backup from '%s'\n",
1490 DEBUG(1, ("Restored tdb backup from '%s'\n",