Reorder arguments to ldb_search() to match what is in Samba 4.
[Samba/ekacnet.git] / source3 / lib / util_tdb.c
blob2dbdd5794787df11ab76fec59aa8a94d1699a279
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 #undef malloc
24 #undef realloc
25 #undef calloc
26 #undef strdup
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)
39 gotalarm = 1;
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. */
49 int ret;
50 gotalarm = 0;
52 if (timeout) {
53 CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
54 tdb_setalarm_sigptr(tdb, &gotalarm);
55 alarm(timeout);
58 if (rw_type == F_RDLCK)
59 ret = tdb_chainlock_read(tdb, key);
60 else
61 ret = tdb_chainlock(tdb, key);
63 if (timeout) {
64 alarm(0);
65 tdb_setalarm_sigptr(tdb, NULL);
66 CatchSignal(SIGALRM, SIGNAL_CAST 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 */
74 return -1;
78 return ret;
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,
91 int timeout)
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)
127 uint8 bt;
128 uint16 w;
129 uint32 d;
130 int i;
131 void *p;
132 int len;
133 char *s;
134 char c;
135 uint8 *buf0 = buf;
136 const char *fmt0 = fmt;
137 int bufsize0 = bufsize;
139 while (*fmt) {
140 switch ((c = *fmt++)) {
141 case 'b': /* unsigned 8-bit integer */
142 len = 1;
143 bt = (uint8)va_arg(ap, int);
144 if (bufsize && bufsize >= len)
145 SSVAL(buf, 0, bt);
146 break;
147 case 'w': /* unsigned 16-bit integer */
148 len = 2;
149 w = (uint16)va_arg(ap, int);
150 if (bufsize && bufsize >= len)
151 SSVAL(buf, 0, w);
152 break;
153 case 'd': /* signed 32-bit integer (standard int in most systems) */
154 len = 4;
155 d = va_arg(ap, uint32);
156 if (bufsize && bufsize >= len)
157 SIVAL(buf, 0, d);
158 break;
159 case 'p': /* pointer */
160 len = 4;
161 p = va_arg(ap, void *);
162 d = p?1:0;
163 if (bufsize && bufsize >= len)
164 SIVAL(buf, 0, d);
165 break;
166 case 'P': /* null-terminated string */
167 s = va_arg(ap,char *);
168 w = strlen(s);
169 len = w + 1;
170 if (bufsize && bufsize >= len)
171 memcpy(buf, s, len);
172 break;
173 case 'f': /* null-terminated string */
174 s = va_arg(ap,char *);
175 w = strlen(s);
176 len = w + 1;
177 if (bufsize && bufsize >= len)
178 memcpy(buf, s, len);
179 break;
180 case 'B': /* fixed-length string */
181 i = va_arg(ap, int);
182 s = va_arg(ap, char *);
183 len = 4+i;
184 if (bufsize && bufsize >= len) {
185 SIVAL(buf, 0, i);
186 memcpy(buf+4, s, i);
188 break;
189 default:
190 DEBUG(0,("Unknown tdb_pack format %c in %s\n",
191 c, fmt));
192 len = 0;
193 break;
196 buf += len;
197 if (bufsize)
198 bufsize -= len;
199 if (bufsize < 0)
200 bufsize = 0;
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, ...)
211 va_list ap;
212 size_t result;
214 va_start(ap, fmt);
215 result = tdb_pack_va(buf, bufsize, fmt, ap);
216 va_end(ap);
217 return result;
220 bool tdb_pack_append(TALLOC_CTX *mem_ctx, uint8 **buf, size_t *len,
221 const char *fmt, ...)
223 va_list ap;
224 size_t len1, len2;
226 va_start(ap, fmt);
227 len1 = tdb_pack_va(NULL, 0, fmt, ap);
228 va_end(ap);
230 if (mem_ctx != NULL) {
231 *buf = TALLOC_REALLOC_ARRAY(mem_ctx, *buf, uint8,
232 (*len) + len1);
233 } else {
234 *buf = SMB_REALLOC_ARRAY(*buf, uint8, (*len) + len1);
237 if (*buf == NULL) {
238 return False;
241 va_start(ap, fmt);
242 len2 = tdb_pack_va((*buf)+(*len), len1, fmt, ap);
243 va_end(ap);
245 if (len1 != len2) {
246 return False;
249 *len += len2;
251 return True;
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, ...)
261 va_list ap;
262 uint8 *bt;
263 uint16 *w;
264 uint32 *d;
265 int len;
266 int *i;
267 void **p;
268 char *s, **b, **ps;
269 char c;
270 const uint8 *buf0 = buf;
271 const char *fmt0 = fmt;
272 int bufsize0 = bufsize;
274 va_start(ap, fmt);
276 while (*fmt) {
277 switch ((c=*fmt++)) {
278 case 'b': /* unsigned 8-bit integer */
279 len = 1;
280 bt = va_arg(ap, uint8 *);
281 if (bufsize < len)
282 goto no_space;
283 *bt = SVAL(buf, 0);
284 break;
285 case 'w': /* unsigned 16-bit integer */
286 len = 2;
287 w = va_arg(ap, uint16 *);
288 if (bufsize < len)
289 goto no_space;
290 *w = SVAL(buf, 0);
291 break;
292 case 'd': /* signed 32-bit integer (standard int in most systems) */
293 len = 4;
294 d = va_arg(ap, uint32 *);
295 if (bufsize < len)
296 goto no_space;
297 *d = IVAL(buf, 0);
298 break;
299 case 'p': /* pointer */
300 len = 4;
301 p = va_arg(ap, void **);
302 if (bufsize < len)
303 goto no_space;
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);
310 break;
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);
316 break;
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))
321 goto no_space;
322 memcpy(s, buf, len);
323 break;
324 case 'B': /* fixed-length string */
325 i = va_arg(ap, int *);
326 b = va_arg(ap, char **);
327 len = 4;
328 if (bufsize < len)
329 goto no_space;
330 *i = IVAL(buf, 0);
331 if (! *i) {
332 *b = NULL;
333 break;
335 len += *i;
336 if (bufsize < len)
337 goto no_space;
338 *b = (char *)SMB_MALLOC(*i);
339 if (! *b)
340 goto no_space;
341 memcpy(*b, buf+4, *i);
342 break;
343 default:
344 DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
345 c, fmt));
347 len = 0;
348 break;
351 buf += len;
352 bufsize -= len;
355 va_end(ap);
357 DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
358 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
360 return PTR_DIFF(buf, buf0);
362 no_space:
363 va_end(ap);
364 return -1;
368 /****************************************************************************
369 Log tdb messages via DEBUG().
370 ****************************************************************************/
372 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, const char *format, ...)
374 va_list ap;
375 char *ptr = NULL;
376 int ret;
378 va_start(ap, format);
379 ret = vasprintf(&ptr, format, ap);
380 va_end(ap);
382 if ((ret == -1) || !*ptr)
383 return;
385 DEBUG((int)level, ("tdb(%s): %s", tdb_name(tdb) ? tdb_name(tdb) : "unnamed", ptr));
386 SAFE_FREE(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)
397 TDB_CONTEXT *tdb;
398 struct tdb_logging_context log_ctx;
400 if (!lp_use_mmap())
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, '/');
408 if (base != NULL) {
409 base += 1;
411 else {
412 base = 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);
419 if (!tdb)
420 return NULL;
422 return tdb;
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,
431 int flag)
433 int res;
435 if ((res = tdb_transaction_start(tdb)) != 0) {
436 DEBUG(5, ("tdb_transaction_start failed\n"));
437 return res;
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");
445 return res;
448 if ((res = tdb_transaction_commit(tdb)) != 0) {
449 DEBUG(5, ("tdb_transaction_commit failed\n"));
452 return res;
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)
462 int res;
464 if ((res = tdb_transaction_start(tdb)) != 0) {
465 DEBUG(5, ("tdb_transaction_start failed\n"));
466 return res;
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");
474 return res;
477 if ((res = tdb_transaction_commit(tdb)) != 0) {
478 DEBUG(5, ("tdb_transaction_commit failed\n"));
481 return res;
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, ...)
493 va_list ap;
494 char *ptr = NULL;
495 int debuglevel = 0;
496 int ret;
498 switch (level) {
499 case TDB_DEBUG_FATAL:
500 debuglevel = 0;
501 break;
502 case TDB_DEBUG_ERROR:
503 debuglevel = 1;
504 break;
505 case TDB_DEBUG_WARNING:
506 debuglevel = 2;
507 break;
508 case TDB_DEBUG_TRACE:
509 debuglevel = 5;
510 break;
511 default:
512 debuglevel = 0;
515 va_start(ap, format);
516 ret = vasprintf(&ptr, format, ap);
517 va_end(ap);
519 if (ret != -1) {
520 const char *name = tdb_name(tdb);
521 DEBUG(debuglevel, ("tdb(%s): %s", name ? name : "unnamed", ptr));
522 free(ptr);
526 static struct tdb_wrap *tdb_list;
528 /* destroy the last connection to a tdb */
529 static int tdb_wrap_destructor(struct tdb_wrap *w)
531 tdb_close(w->tdb);
532 DLIST_REMOVE(tdb_list, w);
533 return 0;
537 wrapped connection to a tdb database
538 to close just talloc_free() the tdb_wrap pointer
540 struct tdb_wrap *tdb_wrap_open(TALLOC_CTX *mem_ctx,
541 const char *name, int hash_size, int tdb_flags,
542 int open_flags, mode_t mode)
544 struct tdb_wrap *w;
545 struct tdb_logging_context log_ctx;
546 log_ctx.log_fn = tdb_wrap_log;
548 if (!lp_use_mmap())
549 tdb_flags |= TDB_NOMMAP;
551 for (w=tdb_list;w;w=w->next) {
552 if (strcmp(name, w->name) == 0) {
554 * Yes, talloc_reference is exactly what we want
555 * here. Otherwise we would have to implement our own
556 * reference counting.
558 return talloc_reference(mem_ctx, w);
562 w = talloc(mem_ctx, struct tdb_wrap);
563 if (w == NULL) {
564 return NULL;
567 if (!(w->name = talloc_strdup(w, name))) {
568 talloc_free(w);
569 return NULL;
572 if ((hash_size == 0) && (name != NULL)) {
573 const char *base = strrchr_m(name, '/');
574 if (base != NULL) {
575 base += 1;
577 else {
578 base = name;
580 hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
583 w->tdb = tdb_open_ex(name, hash_size, tdb_flags,
584 open_flags, mode, &log_ctx, NULL);
585 if (w->tdb == NULL) {
586 talloc_free(w);
587 return NULL;
590 talloc_set_destructor(w, tdb_wrap_destructor);
592 DLIST_ADD(tdb_list, w);
594 return w;
597 NTSTATUS map_nt_error_from_tdb(enum TDB_ERROR err)
599 struct { enum TDB_ERROR err; NTSTATUS status; } map[] =
600 { { TDB_SUCCESS, NT_STATUS_OK },
601 { TDB_ERR_CORRUPT, NT_STATUS_INTERNAL_DB_CORRUPTION },
602 { TDB_ERR_IO, NT_STATUS_UNEXPECTED_IO_ERROR },
603 { TDB_ERR_OOM, NT_STATUS_NO_MEMORY },
604 { TDB_ERR_EXISTS, NT_STATUS_OBJECT_NAME_COLLISION },
607 * TDB_ERR_LOCK is very broad, we could for example
608 * distinguish between fcntl locks and invalid lock
609 * sequences. So NT_STATUS_FILE_LOCK_CONFLICT is a
610 * compromise.
612 { TDB_ERR_LOCK, NT_STATUS_FILE_LOCK_CONFLICT },
614 * The next two ones in the enum are not actually used
616 { TDB_ERR_NOLOCK, NT_STATUS_FILE_LOCK_CONFLICT },
617 { TDB_ERR_LOCK_TIMEOUT, NT_STATUS_FILE_LOCK_CONFLICT },
618 { TDB_ERR_NOEXIST, NT_STATUS_NOT_FOUND },
619 { TDB_ERR_EINVAL, NT_STATUS_INVALID_PARAMETER },
620 { TDB_ERR_RDONLY, NT_STATUS_ACCESS_DENIED }
623 int i;
625 for (i=0; i < sizeof(map) / sizeof(map[0]); i++) {
626 if (err == map[i].err) {
627 return map[i].status;
631 return NT_STATUS_INTERNAL_ERROR;
635 /*********************************************************************
636 * the following is a generic validation mechanism for tdbs.
637 *********************************************************************/
640 * internal validation function, executed by the child.
642 static int tdb_validate_child(struct tdb_context *tdb,
643 tdb_validate_data_func validate_fn)
645 int ret = 1;
646 int num_entries = 0;
647 struct tdb_validation_status v_status;
649 v_status.tdb_error = False;
650 v_status.bad_freelist = False;
651 v_status.bad_entry = False;
652 v_status.unknown_key = False;
653 v_status.success = True;
655 if (!tdb) {
656 v_status.tdb_error = True;
657 v_status.success = False;
658 goto out;
661 /* Check if the tdb's freelist is good. */
662 if (tdb_validate_freelist(tdb, &num_entries) == -1) {
663 v_status.bad_freelist = True;
664 v_status.success = False;
665 goto out;
668 DEBUG(10,("tdb_validate_child: tdb %s freelist has %d entries\n",
669 tdb_name(tdb), num_entries));
671 /* Now traverse the tdb to validate it. */
672 num_entries = tdb_traverse(tdb, validate_fn, (void *)&v_status);
673 if (!v_status.success) {
674 goto out;
675 } else if (num_entries == -1) {
676 v_status.tdb_error = True;
677 v_status.success = False;
678 goto out;
681 DEBUG(10,("tdb_validate_child: tdb %s is good with %d entries\n",
682 tdb_name(tdb), num_entries));
683 ret = 0; /* Cache is good. */
685 out:
686 DEBUG(10, ("tdb_validate_child: summary of validation status:\n"));
687 DEBUGADD(10,(" * tdb error: %s\n", v_status.tdb_error ? "yes" : "no"));
688 DEBUGADD(10,(" * bad freelist: %s\n",v_status.bad_freelist?"yes":"no"));
689 DEBUGADD(10,(" * bad entry: %s\n", v_status.bad_entry ? "yes" : "no"));
690 DEBUGADD(10,(" * unknown key: %s\n", v_status.unknown_key?"yes":"no"));
691 DEBUGADD(10,(" => overall success: %s\n", v_status.success?"yes":"no"));
693 return ret;
697 * tdb validation function.
698 * returns 0 if tdb is ok, != 0 if it isn't.
699 * this function expects an opened tdb.
701 int tdb_validate(struct tdb_context *tdb, tdb_validate_data_func validate_fn)
703 pid_t child_pid = -1;
704 int child_status = 0;
705 int wait_pid = 0;
706 int ret = 1;
708 if (tdb == NULL) {
709 DEBUG(1, ("Error: tdb_validate called with tdb == NULL\n"));
710 return ret;
713 DEBUG(5, ("tdb_validate called for tdb '%s'\n", tdb_name(tdb)));
715 /* fork and let the child do the validation.
716 * benefit: no need to twist signal handlers and panic functions.
717 * just let the child panic. we catch the signal. */
719 DEBUG(10, ("tdb_validate: forking to let child do validation.\n"));
720 child_pid = sys_fork();
721 if (child_pid == 0) {
722 /* child code */
723 DEBUG(10, ("tdb_validate (validation child): created\n"));
724 DEBUG(10, ("tdb_validate (validation child): "
725 "calling tdb_validate_child\n"));
726 exit(tdb_validate_child(tdb, validate_fn));
728 else if (child_pid < 0) {
729 DEBUG(1, ("tdb_validate: fork for validation failed.\n"));
730 goto done;
733 /* parent */
735 DEBUG(10, ("tdb_validate: fork succeeded, child PID = %d\n",child_pid));
737 DEBUG(10, ("tdb_validate: waiting for child to finish...\n"));
738 while ((wait_pid = sys_waitpid(child_pid, &child_status, 0)) < 0) {
739 if (errno == EINTR) {
740 DEBUG(10, ("tdb_validate: got signal during waitpid, "
741 "retrying\n"));
742 errno = 0;
743 continue;
745 DEBUG(1, ("tdb_validate: waitpid failed with error '%s'.\n",
746 strerror(errno)));
747 goto done;
749 if (wait_pid != child_pid) {
750 DEBUG(1, ("tdb_validate: waitpid returned pid %d, "
751 "but %d was expected\n", wait_pid, child_pid));
752 goto done;
755 DEBUG(10, ("tdb_validate: validating child returned.\n"));
756 if (WIFEXITED(child_status)) {
757 DEBUG(10, ("tdb_validate: child exited, code %d.\n",
758 WEXITSTATUS(child_status)));
759 ret = WEXITSTATUS(child_status);
761 if (WIFSIGNALED(child_status)) {
762 DEBUG(10, ("tdb_validate: child terminated by signal %d\n",
763 WTERMSIG(child_status)));
764 #ifdef WCOREDUMP
765 if (WCOREDUMP(child_status)) {
766 DEBUGADD(10, ("core dumped\n"));
768 #endif
769 ret = WTERMSIG(child_status);
771 if (WIFSTOPPED(child_status)) {
772 DEBUG(10, ("tdb_validate: child was stopped by signal %d\n",
773 WSTOPSIG(child_status)));
774 ret = WSTOPSIG(child_status);
777 done:
778 DEBUG(5, ("tdb_validate returning code '%d' for tdb '%s'\n", ret,
779 tdb_name(tdb)));
781 return ret;
785 * tdb validation function.
786 * returns 0 if tdb is ok, != 0 if it isn't.
787 * this is a wrapper around the actual validation function that opens and closes
788 * the tdb.
790 int tdb_validate_open(const char *tdb_path, tdb_validate_data_func validate_fn)
792 TDB_CONTEXT *tdb = NULL;
793 int ret = 1;
795 DEBUG(5, ("tdb_validate_open called for tdb '%s'\n", tdb_path));
797 tdb = tdb_open_log(tdb_path, 0, TDB_DEFAULT, O_RDONLY, 0);
798 if (!tdb) {
799 DEBUG(1, ("Error opening tdb %s\n", tdb_path));
800 return ret;
803 ret = tdb_validate(tdb, validate_fn);
804 tdb_close(tdb);
805 return ret;
809 * tdb backup function and helpers for tdb_validate wrapper with backup
810 * handling.
813 /* this structure eliminates the need for a global overall status for
814 * the traverse-copy */
815 struct tdb_copy_data {
816 struct tdb_context *dst;
817 bool success;
820 static int traverse_copy_fn(struct tdb_context *tdb, TDB_DATA key,
821 TDB_DATA dbuf, void *private_data)
823 struct tdb_copy_data *data = (struct tdb_copy_data *)private_data;
825 if (tdb_store(data->dst, key, dbuf, TDB_INSERT) != 0) {
826 DEBUG(4, ("Failed to insert into %s: %s\n", tdb_name(data->dst),
827 strerror(errno)));
828 data->success = False;
829 return 1;
831 return 0;
834 static int tdb_copy(struct tdb_context *src, struct tdb_context *dst)
836 struct tdb_copy_data data;
837 int count;
839 data.dst = dst;
840 data.success = True;
842 count = tdb_traverse(src, traverse_copy_fn, (void *)(&data));
843 if ((count < 0) || (data.success == False)) {
844 return -1;
846 return count;
849 static int tdb_verify_basic(struct tdb_context *tdb)
851 return tdb_traverse(tdb, NULL, NULL);
854 /* this backup function is essentially taken from lib/tdb/tools/tdbbackup.tdb
856 static int tdb_backup(TALLOC_CTX *ctx, const char *src_path,
857 const char *dst_path, int hash_size)
859 struct tdb_context *src_tdb = NULL;
860 struct tdb_context *dst_tdb = NULL;
861 char *tmp_path = NULL;
862 struct stat st;
863 int count1, count2;
864 int saved_errno = 0;
865 int ret = -1;
867 if (stat(src_path, &st) != 0) {
868 DEBUG(3, ("Could not stat '%s': %s\n", src_path,
869 strerror(errno)));
870 goto done;
873 /* open old tdb RDWR - so we can lock it */
874 src_tdb = tdb_open_log(src_path, 0, TDB_DEFAULT, O_RDWR, 0);
875 if (src_tdb == NULL) {
876 DEBUG(3, ("Failed to open tdb '%s'\n", src_path));
877 goto done;
880 if (tdb_lockall(src_tdb) != 0) {
881 DEBUG(3, ("Failed to lock tdb '%s'\n", src_path));
882 goto done;
885 tmp_path = talloc_asprintf(ctx, "%s%s", dst_path, ".tmp");
886 unlink(tmp_path);
887 dst_tdb = tdb_open_log(tmp_path,
888 hash_size ? hash_size : tdb_hash_size(src_tdb),
889 TDB_DEFAULT, O_RDWR | O_CREAT | O_EXCL,
890 st.st_mode & 0777);
891 if (dst_tdb == NULL) {
892 DEBUG(3, ("Error creating tdb '%s': %s\n", tmp_path,
893 strerror(errno)));
894 saved_errno = errno;
895 unlink(tmp_path);
896 goto done;
899 count1 = tdb_copy(src_tdb, dst_tdb);
900 if (count1 < 0) {
901 DEBUG(3, ("Failed to copy tdb '%s': %s\n", src_path,
902 strerror(errno)));
903 tdb_close(dst_tdb);
904 goto done;
907 /* reopen ro and do basic verification */
908 tdb_close(dst_tdb);
909 dst_tdb = tdb_open_log(tmp_path, 0, TDB_DEFAULT, O_RDONLY, 0);
910 if (!dst_tdb) {
911 DEBUG(3, ("Failed to reopen tdb '%s': %s\n", tmp_path,
912 strerror(errno)));
913 goto done;
915 count2 = tdb_verify_basic(dst_tdb);
916 if (count2 != count1) {
917 DEBUG(3, ("Failed to verify result of copying tdb '%s'.\n",
918 src_path));
919 tdb_close(dst_tdb);
920 goto done;
923 DEBUG(10, ("tdb_backup: successfully copied %d entries\n", count1));
925 /* make sure the new tdb has reached stable storage
926 * then rename it to its destination */
927 fsync(tdb_fd(dst_tdb));
928 tdb_close(dst_tdb);
929 unlink(dst_path);
930 if (rename(tmp_path, dst_path) != 0) {
931 DEBUG(3, ("Failed to rename '%s' to '%s': %s\n",
932 tmp_path, dst_path, strerror(errno)));
933 goto done;
936 /* success */
937 ret = 0;
939 done:
940 if (src_tdb != NULL) {
941 tdb_close(src_tdb);
943 if (tmp_path != NULL) {
944 unlink(tmp_path);
945 TALLOC_FREE(tmp_path);
947 if (saved_errno != 0) {
948 errno = saved_errno;
950 return ret;
953 static int rename_file_with_suffix(TALLOC_CTX *ctx, const char *path,
954 const char *suffix)
956 int ret = -1;
957 char *dst_path;
959 dst_path = talloc_asprintf(ctx, "%s%s", path, suffix);
961 ret = (rename(path, dst_path) != 0);
963 if (ret == 0) {
964 DEBUG(5, ("moved '%s' to '%s'\n", path, dst_path));
965 } else if (errno == ENOENT) {
966 DEBUG(3, ("file '%s' does not exist - so not moved\n", path));
967 ret = 0;
968 } else {
969 DEBUG(3, ("error renaming %s to %s: %s\n", path, dst_path,
970 strerror(errno)));
973 TALLOC_FREE(dst_path);
974 return ret;
978 * do a backup of a tdb, moving the destination out of the way first
980 static int tdb_backup_with_rotate(TALLOC_CTX *ctx, const char *src_path,
981 const char *dst_path, int hash_size,
982 const char *rotate_suffix,
983 bool retry_norotate_if_nospc,
984 bool rename_as_last_resort_if_nospc)
986 int ret;
988 rename_file_with_suffix(ctx, dst_path, rotate_suffix);
990 ret = tdb_backup(ctx, src_path, dst_path, hash_size);
992 if (ret != 0) {
993 DEBUG(10, ("backup of %s failed: %s\n", src_path, strerror(errno)));
995 if ((ret != 0) && (errno == ENOSPC) && retry_norotate_if_nospc)
997 char *rotate_path = talloc_asprintf(ctx, "%s%s", dst_path,
998 rotate_suffix);
999 DEBUG(10, ("backup of %s failed due to lack of space\n",
1000 src_path));
1001 DEBUGADD(10, ("trying to free some space by removing rotated "
1002 "dst %s\n", rotate_path));
1003 if (unlink(rotate_path) == -1) {
1004 DEBUG(10, ("unlink of %s failed: %s\n", rotate_path,
1005 strerror(errno)));
1006 } else {
1007 ret = tdb_backup(ctx, src_path, dst_path, hash_size);
1009 TALLOC_FREE(rotate_path);
1012 if ((ret != 0) && (errno == ENOSPC) && rename_as_last_resort_if_nospc)
1014 DEBUG(10, ("backup of %s failed due to lack of space\n",
1015 src_path));
1016 DEBUGADD(10, ("using 'rename' as a last resort\n"));
1017 ret = rename(src_path, dst_path);
1020 return ret;
1024 * validation function with backup handling:
1026 * - calls tdb_validate
1027 * - if the tdb is ok, create a backup "name.bak", possibly moving
1028 * existing backup to name.bak.old,
1029 * return 0 (success) even if the backup fails
1030 * - if the tdb is corrupt:
1031 * - move the tdb to "name.corrupt"
1032 * - check if there is valid backup.
1033 * if so, restore the backup.
1034 * if restore is successful, return 0 (success),
1035 * - otherwise return -1 (failure)
1037 int tdb_validate_and_backup(const char *tdb_path,
1038 tdb_validate_data_func validate_fn)
1040 int ret = -1;
1041 const char *backup_suffix = ".bak";
1042 const char *corrupt_suffix = ".corrupt";
1043 const char *rotate_suffix = ".old";
1044 char *tdb_path_backup;
1045 struct stat st;
1046 TALLOC_CTX *ctx = NULL;
1048 ctx = talloc_new(NULL);
1049 if (ctx == NULL) {
1050 DEBUG(0, ("tdb_validate_and_backup: out of memory\n"));
1051 goto done;
1054 tdb_path_backup = talloc_asprintf(ctx, "%s%s", tdb_path, backup_suffix);
1056 ret = tdb_validate_open(tdb_path, validate_fn);
1058 if (ret == 0) {
1059 DEBUG(1, ("tdb '%s' is valid\n", tdb_path));
1060 ret = tdb_backup_with_rotate(ctx, tdb_path, tdb_path_backup, 0,
1061 rotate_suffix, True, False);
1062 if (ret != 0) {
1063 DEBUG(1, ("Error creating backup of tdb '%s'\n",
1064 tdb_path));
1065 /* the actual validation was successful: */
1066 ret = 0;
1067 } else {
1068 DEBUG(1, ("Created backup '%s' of tdb '%s'\n",
1069 tdb_path_backup, tdb_path));
1071 } else {
1072 DEBUG(1, ("tdb '%s' is invalid\n", tdb_path));
1074 ret =stat(tdb_path_backup, &st);
1075 if (ret != 0) {
1076 DEBUG(5, ("Could not stat '%s': %s\n", tdb_path_backup,
1077 strerror(errno)));
1078 DEBUG(1, ("No backup found.\n"));
1079 } else {
1080 DEBUG(1, ("backup '%s' found.\n", tdb_path_backup));
1081 ret = tdb_validate_open(tdb_path_backup, validate_fn);
1082 if (ret != 0) {
1083 DEBUG(1, ("Backup '%s' is invalid.\n",
1084 tdb_path_backup));
1088 if (ret != 0) {
1089 int renamed = rename_file_with_suffix(ctx, tdb_path,
1090 corrupt_suffix);
1091 if (renamed != 0) {
1092 DEBUG(1, ("Error moving tdb to '%s%s'\n",
1093 tdb_path, corrupt_suffix));
1094 } else {
1095 DEBUG(1, ("Corrupt tdb stored as '%s%s'\n",
1096 tdb_path, corrupt_suffix));
1098 goto done;
1101 DEBUG(1, ("valid backup '%s' found\n", tdb_path_backup));
1102 ret = tdb_backup_with_rotate(ctx, tdb_path_backup, tdb_path, 0,
1103 corrupt_suffix, True, True);
1104 if (ret != 0) {
1105 DEBUG(1, ("Error restoring backup from '%s'\n",
1106 tdb_path_backup));
1107 } else {
1108 DEBUG(1, ("Restored tdb backup from '%s'\n",
1109 tdb_path_backup));
1113 done:
1114 TALLOC_FREE(ctx);
1115 return ret;