util_tdb: make the _byblob functions static - not currently used elsewhere.
[ddiss/samba.git] / source3 / lib / util_tdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3    tdb utility functions
4    Copyright (C) Andrew Tridgell   1992-1998
5    Copyright (C) Rafal Szczesniak  2002
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22 #undef malloc
23 #undef realloc
24 #undef calloc
25 #undef strdup
26
27 /* these are little tdb utility functions that are meant to make
28    dealing with a tdb database a little less cumbersome in Samba */
29
30 static SIG_ATOMIC_T gotalarm;
31
32 /***************************************************************
33  Signal function to tell us we timed out.
34 ****************************************************************/
35
36 static void gotalarm_sig(void)
37 {
38         gotalarm = 1;
39 }
40
41 /***************************************************************
42  Make a TDB_DATA and keep the const warning in one place
43 ****************************************************************/
44
45 TDB_DATA make_tdb_data(const uint8 *dptr, size_t dsize)
46 {
47         TDB_DATA ret;
48         ret.dptr = CONST_DISCARD(uint8 *, dptr);
49         ret.dsize = dsize;
50         return ret;
51 }
52
53 TDB_DATA string_tdb_data(const char *string)
54 {
55         return make_tdb_data((const uint8 *)string, string ? strlen(string) : 0 );
56 }
57
58 TDB_DATA string_term_tdb_data(const char *string)
59 {
60         return make_tdb_data((const uint8 *)string, string ? strlen(string) + 1 : 0);
61 }
62
63 /****************************************************************************
64  Lock a chain with timeout (in seconds).
65 ****************************************************************************/
66
67 static int tdb_chainlock_with_timeout_internal( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout, int rw_type)
68 {
69         /* Allow tdb_chainlock to be interrupted by an alarm. */
70         int ret;
71         gotalarm = 0;
72
73         if (timeout) {
74                 CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
75                 tdb_setalarm_sigptr(tdb, &gotalarm);
76                 alarm(timeout);
77         }
78
79         if (rw_type == F_RDLCK)
80                 ret = tdb_chainlock_read(tdb, key);
81         else
82                 ret = tdb_chainlock(tdb, key);
83
84         if (timeout) {
85                 alarm(0);
86                 tdb_setalarm_sigptr(tdb, NULL);
87                 CatchSignal(SIGALRM, SIGNAL_CAST SIG_IGN);
88                 if (gotalarm) {
89                         DEBUG(0,("tdb_chainlock_with_timeout_internal: alarm (%u) timed out for key %s in tdb %s\n",
90                                 timeout, key.dptr, tdb_name(tdb)));
91                         /* TODO: If we time out waiting for a lock, it might
92                          * be nice to use F_GETLK to get the pid of the
93                          * process currently holding the lock and print that
94                          * as part of the debugging message. -- mbp */
95                         return -1;
96                 }
97         }
98
99         return ret;
100 }
101
102 /****************************************************************************
103  Write lock a chain. Return -1 if timeout or lock failed.
104 ****************************************************************************/
105
106 int tdb_chainlock_with_timeout( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout)
107 {
108         return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_WRLCK);
109 }
110
111 /****************************************************************************
112  Lock a chain by string. Return -1 if timeout or lock failed.
113 ****************************************************************************/
114
115 int tdb_lock_bystring(TDB_CONTEXT *tdb, const char *keyval)
116 {
117         TDB_DATA key = string_term_tdb_data(keyval);
118         
119         return tdb_chainlock(tdb, key);
120 }
121
122 int tdb_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval,
123                                    int timeout)
124 {
125         TDB_DATA key = string_term_tdb_data(keyval);
126         
127         return tdb_chainlock_with_timeout(tdb, key, timeout);
128 }
129
130 /****************************************************************************
131  Unlock a chain by string.
132 ****************************************************************************/
133
134 void tdb_unlock_bystring(TDB_CONTEXT *tdb, const char *keyval)
135 {
136         TDB_DATA key = string_term_tdb_data(keyval);
137
138         tdb_chainunlock(tdb, key);
139 }
140
141 /****************************************************************************
142  Read lock a chain by string. Return -1 if timeout or lock failed.
143 ****************************************************************************/
144
145 int tdb_read_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval, unsigned int timeout)
146 {
147         TDB_DATA key = string_term_tdb_data(keyval);
148         
149         return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_RDLCK);
150 }
151
152 /****************************************************************************
153  Read unlock a chain by string.
154 ****************************************************************************/
155
156 void tdb_read_unlock_bystring(TDB_CONTEXT *tdb, const char *keyval)
157 {
158         TDB_DATA key = string_term_tdb_data(keyval);
159         
160         tdb_chainunlock_read(tdb, key);
161 }
162
163
164 /****************************************************************************
165  Fetch a int32 value by a arbitrary blob key, return -1 if not found.
166  Output is int32 in native byte order.
167 ****************************************************************************/
168
169 static int32 tdb_fetch_int32_byblob(TDB_CONTEXT *tdb, TDB_DATA key)
170 {
171         TDB_DATA data;
172         int32 ret;
173
174         data = tdb_fetch(tdb, key);
175         if (!data.dptr || data.dsize != sizeof(int32)) {
176                 SAFE_FREE(data.dptr);
177                 return -1;
178         }
179
180         ret = IVAL(data.dptr,0);
181         SAFE_FREE(data.dptr);
182         return ret;
183 }
184
185 /****************************************************************************
186  Fetch a int32 value by string key, return -1 if not found.
187  Output is int32 in native byte order.
188 ****************************************************************************/
189
190 int32 tdb_fetch_int32(TDB_CONTEXT *tdb, const char *keystr)
191 {
192         TDB_DATA key = string_term_tdb_data(keystr);
193
194         return tdb_fetch_int32_byblob(tdb, key);
195 }
196
197 /****************************************************************************
198  Store a int32 value by an arbitary blob key, return 0 on success, -1 on failure.
199  Input is int32 in native byte order. Output in tdb is in little-endian.
200 ****************************************************************************/
201
202 static int tdb_store_int32_byblob(TDB_CONTEXT *tdb, TDB_DATA key, int32 v)
203 {
204         TDB_DATA data;
205         int32 v_store;
206
207         SIVAL(&v_store,0,v);
208         data.dptr = (uint8 *)&v_store;
209         data.dsize = sizeof(int32);
210
211         return tdb_store(tdb, key, data, TDB_REPLACE);
212 }
213
214 /****************************************************************************
215  Store a int32 value by string key, return 0 on success, -1 on failure.
216  Input is int32 in native byte order. Output in tdb is in little-endian.
217 ****************************************************************************/
218
219 int tdb_store_int32(TDB_CONTEXT *tdb, const char *keystr, int32 v)
220 {
221         TDB_DATA key = string_term_tdb_data(keystr);
222
223         return tdb_store_int32_byblob(tdb, key, v);
224 }
225
226 /****************************************************************************
227  Fetch a uint32 value by a arbitrary blob key, return -1 if not found.
228  Output is uint32 in native byte order.
229 ****************************************************************************/
230
231 static bool tdb_fetch_uint32_byblob(TDB_CONTEXT *tdb, TDB_DATA key, uint32 *value)
232 {
233         TDB_DATA data;
234
235         data = tdb_fetch(tdb, key);
236         if (!data.dptr || data.dsize != sizeof(uint32)) {
237                 SAFE_FREE(data.dptr);
238                 return False;
239         }
240
241         *value = IVAL(data.dptr,0);
242         SAFE_FREE(data.dptr);
243         return True;
244 }
245
246 /****************************************************************************
247  Fetch a uint32 value by string key, return -1 if not found.
248  Output is uint32 in native byte order.
249 ****************************************************************************/
250
251 bool tdb_fetch_uint32(TDB_CONTEXT *tdb, const char *keystr, uint32 *value)
252 {
253         TDB_DATA key = string_term_tdb_data(keystr);
254
255         return tdb_fetch_uint32_byblob(tdb, key, value);
256 }
257
258 /****************************************************************************
259  Store a uint32 value by an arbitary blob key, return 0 on success, -1 on failure.
260  Input is uint32 in native byte order. Output in tdb is in little-endian.
261 ****************************************************************************/
262
263 static bool tdb_store_uint32_byblob(TDB_CONTEXT *tdb, TDB_DATA key, uint32 value)
264 {
265         TDB_DATA data;
266         uint32 v_store;
267         bool ret = True;
268
269         SIVAL(&v_store, 0, value);
270         data.dptr = (uint8 *)&v_store;
271         data.dsize = sizeof(uint32);
272
273         if (tdb_store(tdb, key, data, TDB_REPLACE) == -1)
274                 ret = False;
275
276         return ret;
277 }
278
279 /****************************************************************************
280  Store a uint32 value by string key, return 0 on success, -1 on failure.
281  Input is uint32 in native byte order. Output in tdb is in little-endian.
282 ****************************************************************************/
283
284 bool tdb_store_uint32(TDB_CONTEXT *tdb, const char *keystr, uint32 value)
285 {
286         TDB_DATA key = string_term_tdb_data(keystr);
287
288         return tdb_store_uint32_byblob(tdb, key, value);
289 }
290 /****************************************************************************
291  Store a buffer by a null terminated string key.  Return 0 on success, -1
292  on failure.
293 ****************************************************************************/
294
295 int tdb_store_bystring(TDB_CONTEXT *tdb, const char *keystr, TDB_DATA data, int flags)
296 {
297         TDB_DATA key = string_term_tdb_data(keystr);
298
299         return tdb_store(tdb, key, data, flags);
300 }
301
302 int tdb_trans_store_bystring(TDB_CONTEXT *tdb, const char *keystr,
303                              TDB_DATA data, int flags)
304 {
305         TDB_DATA key = string_term_tdb_data(keystr);
306         
307         return tdb_trans_store(tdb, key, data, flags);
308 }
309
310 /****************************************************************************
311  Fetch a buffer using a null terminated string key.  Don't forget to call
312  free() on the result dptr.
313 ****************************************************************************/
314
315 TDB_DATA tdb_fetch_bystring(TDB_CONTEXT *tdb, const char *keystr)
316 {
317         TDB_DATA key = string_term_tdb_data(keystr);
318
319         return tdb_fetch(tdb, key);
320 }
321
322 /****************************************************************************
323  Delete an entry using a null terminated string key. 
324 ****************************************************************************/
325
326 int tdb_delete_bystring(TDB_CONTEXT *tdb, const char *keystr)
327 {
328         TDB_DATA key = string_term_tdb_data(keystr);
329
330         return tdb_delete(tdb, key);
331 }
332
333 /****************************************************************************
334  Atomic integer change. Returns old value. To create, set initial value in *oldval. 
335 ****************************************************************************/
336
337 int32 tdb_change_int32_atomic(TDB_CONTEXT *tdb, const char *keystr, int32 *oldval, int32 change_val)
338 {
339         int32 val;
340         int32 ret = -1;
341
342         if (tdb_lock_bystring(tdb, keystr) == -1)
343                 return -1;
344
345         if ((val = tdb_fetch_int32(tdb, keystr)) == -1) {
346                 /* The lookup failed */
347                 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
348                         /* but not because it didn't exist */
349                         goto err_out;
350                 }
351                 
352                 /* Start with 'old' value */
353                 val = *oldval;
354
355         } else {
356                 /* It worked, set return value (oldval) to tdb data */
357                 *oldval = val;
358         }
359
360         /* Increment value for storage and return next time */
361         val += change_val;
362                 
363         if (tdb_store_int32(tdb, keystr, val) == -1)
364                 goto err_out;
365
366         ret = 0;
367
368   err_out:
369
370         tdb_unlock_bystring(tdb, keystr);
371         return ret;
372 }
373
374 /****************************************************************************
375  Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval. 
376 ****************************************************************************/
377
378 bool tdb_change_uint32_atomic(TDB_CONTEXT *tdb, const char *keystr, uint32 *oldval, uint32 change_val)
379 {
380         uint32 val;
381         bool ret = False;
382
383         if (tdb_lock_bystring(tdb, keystr) == -1)
384                 return False;
385
386         if (!tdb_fetch_uint32(tdb, keystr, &val)) {
387                 /* It failed */
388                 if (tdb_error(tdb) != TDB_ERR_NOEXIST) { 
389                         /* and not because it didn't exist */
390                         goto err_out;
391                 }
392
393                 /* Start with 'old' value */
394                 val = *oldval;
395
396         } else {
397                 /* it worked, set return value (oldval) to tdb data */
398                 *oldval = val;
399
400         }
401
402         /* get a new value to store */
403         val += change_val;
404                 
405         if (!tdb_store_uint32(tdb, keystr, val))
406                 goto err_out;
407
408         ret = True;
409
410   err_out:
411
412         tdb_unlock_bystring(tdb, keystr);
413         return ret;
414 }
415
416 /****************************************************************************
417  Useful pair of routines for packing/unpacking data consisting of
418  integers and strings.
419 ****************************************************************************/
420
421 static size_t tdb_pack_va(uint8 *buf, int bufsize, const char *fmt, va_list ap)
422 {
423         uint8 bt;
424         uint16 w;
425         uint32 d;
426         int i;
427         void *p;
428         int len;
429         char *s;
430         char c;
431         uint8 *buf0 = buf;
432         const char *fmt0 = fmt;
433         int bufsize0 = bufsize;
434
435         while (*fmt) {
436                 switch ((c = *fmt++)) {
437                 case 'b': /* unsigned 8-bit integer */
438                         len = 1;
439                         bt = (uint8)va_arg(ap, int);
440                         if (bufsize && bufsize >= len)
441                                 SSVAL(buf, 0, bt);
442                         break;
443                 case 'w': /* unsigned 16-bit integer */
444                         len = 2;
445                         w = (uint16)va_arg(ap, int);
446                         if (bufsize && bufsize >= len)
447                                 SSVAL(buf, 0, w);
448                         break;
449                 case 'd': /* signed 32-bit integer (standard int in most systems) */
450                         len = 4;
451                         d = va_arg(ap, uint32);
452                         if (bufsize && bufsize >= len)
453                                 SIVAL(buf, 0, d);
454                         break;
455                 case 'p': /* pointer */
456                         len = 4;
457                         p = va_arg(ap, void *);
458                         d = p?1:0;
459                         if (bufsize && bufsize >= len)
460                                 SIVAL(buf, 0, d);
461                         break;
462                 case 'P': /* null-terminated string */
463                         s = va_arg(ap,char *);
464                         w = strlen(s);
465                         len = w + 1;
466                         if (bufsize && bufsize >= len)
467                                 memcpy(buf, s, len);
468                         break;
469                 case 'f': /* null-terminated string */
470                         s = va_arg(ap,char *);
471                         w = strlen(s);
472                         len = w + 1;
473                         if (bufsize && bufsize >= len)
474                                 memcpy(buf, s, len);
475                         break;
476                 case 'B': /* fixed-length string */
477                         i = va_arg(ap, int);
478                         s = va_arg(ap, char *);
479                         len = 4+i;
480                         if (bufsize && bufsize >= len) {
481                                 SIVAL(buf, 0, i);
482                                 memcpy(buf+4, s, i);
483                         }
484                         break;
485                 default:
486                         DEBUG(0,("Unknown tdb_pack format %c in %s\n", 
487                                  c, fmt));
488                         len = 0;
489                         break;
490                 }
491
492                 buf += len;
493                 if (bufsize)
494                         bufsize -= len;
495                 if (bufsize < 0)
496                         bufsize = 0;
497         }
498
499         DEBUG(18,("tdb_pack_va(%s, %d) -> %d\n", 
500                  fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
501         
502         return PTR_DIFF(buf, buf0);
503 }
504
505 size_t tdb_pack(uint8 *buf, int bufsize, const char *fmt, ...)
506 {
507         va_list ap;
508         size_t result;
509
510         va_start(ap, fmt);
511         result = tdb_pack_va(buf, bufsize, fmt, ap);
512         va_end(ap);
513         return result;
514 }
515
516 bool tdb_pack_append(TALLOC_CTX *mem_ctx, uint8 **buf, size_t *len,
517                      const char *fmt, ...)
518 {
519         va_list ap;
520         size_t len1, len2;
521
522         va_start(ap, fmt);
523         len1 = tdb_pack_va(NULL, 0, fmt, ap);
524         va_end(ap);
525
526         if (mem_ctx != NULL) {
527                 *buf = TALLOC_REALLOC_ARRAY(mem_ctx, *buf, uint8,
528                                             (*len) + len1);
529         } else {
530                 *buf = SMB_REALLOC_ARRAY(*buf, uint8, (*len) + len1);
531         }
532
533         if (*buf == NULL) {
534                 return False;
535         }
536
537         va_start(ap, fmt);
538         len2 = tdb_pack_va((*buf)+(*len), len1, fmt, ap);
539         va_end(ap);
540
541         if (len1 != len2) {
542                 return False;
543         }
544
545         *len += len2;
546
547         return True;
548 }
549
550 /****************************************************************************
551  Useful pair of routines for packing/unpacking data consisting of
552  integers and strings.
553 ****************************************************************************/
554
555 int tdb_unpack(const uint8 *buf, int bufsize, const char *fmt, ...)
556 {
557         va_list ap;
558         uint8 *bt;
559         uint16 *w;
560         uint32 *d;
561         int len;
562         int *i;
563         void **p;
564         char *s, **b, **ps;
565         char c;
566         const uint8 *buf0 = buf;
567         const char *fmt0 = fmt;
568         int bufsize0 = bufsize;
569
570         va_start(ap, fmt);
571
572         while (*fmt) {
573                 switch ((c=*fmt++)) {
574                 case 'b':
575                         len = 1;
576                         bt = va_arg(ap, uint8 *);
577                         if (bufsize < len)
578                                 goto no_space;
579                         *bt = SVAL(buf, 0);
580                         break;
581                 case 'w':
582                         len = 2;
583                         w = va_arg(ap, uint16 *);
584                         if (bufsize < len)
585                                 goto no_space;
586                         *w = SVAL(buf, 0);
587                         break;
588                 case 'd':
589                         len = 4;
590                         d = va_arg(ap, uint32 *);
591                         if (bufsize < len)
592                                 goto no_space;
593                         *d = IVAL(buf, 0);
594                         break;
595                 case 'p':
596                         len = 4;
597                         p = va_arg(ap, void **);
598                         if (bufsize < len)
599                                 goto no_space;
600                         /*
601                          * This isn't a real pointer - only a token (1 or 0)
602                          * to mark the fact a pointer is present.
603                          */
604
605                         *p = (void *)(IVAL(buf, 0) ? (void *)1 : NULL);
606                         break;
607                 case 'P':
608                         /* Return malloc'ed string. */
609                         ps = va_arg(ap,char **);
610                         len = strlen((const char *)buf) + 1;
611                         *ps = SMB_STRDUP((const char *)buf);
612                         break;
613                 case 'f':
614                         s = va_arg(ap,char *);
615                         len = strlen((const char *)buf) + 1;
616                         if (bufsize < len || len > sizeof(fstring))
617                                 goto no_space;
618                         memcpy(s, buf, len);
619                         break;
620                 case 'B':
621                         i = va_arg(ap, int *);
622                         b = va_arg(ap, char **);
623                         len = 4;
624                         if (bufsize < len)
625                                 goto no_space;
626                         *i = IVAL(buf, 0);
627                         if (! *i) {
628                                 *b = NULL;
629                                 break;
630                         }
631                         len += *i;
632                         if (bufsize < len)
633                                 goto no_space;
634                         *b = (char *)SMB_MALLOC(*i);
635                         if (! *b)
636                                 goto no_space;
637                         memcpy(*b, buf+4, *i);
638                         break;
639                 default:
640                         DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
641                                  c, fmt));
642
643                         len = 0;
644                         break;
645                 }
646
647                 buf += len;
648                 bufsize -= len;
649         }
650
651         va_end(ap);
652
653         DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
654                  fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
655
656         return PTR_DIFF(buf, buf0);
657
658  no_space:
659         va_end(ap);
660         return -1;
661 }
662
663
664 /****************************************************************************
665  Log tdb messages via DEBUG().
666 ****************************************************************************/
667
668 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, const char *format, ...)
669 {
670         va_list ap;
671         char *ptr = NULL;
672         int ret;
673
674         va_start(ap, format);
675         ret = vasprintf(&ptr, format, ap);
676         va_end(ap);
677
678         if ((ret == -1) || !*ptr)
679                 return;
680
681         DEBUG((int)level, ("tdb(%s): %s", tdb_name(tdb) ? tdb_name(tdb) : "unnamed", ptr));
682         SAFE_FREE(ptr);
683 }
684
685 /****************************************************************************
686  Like tdb_open() but also setup a logging function that redirects to
687  the samba DEBUG() system.
688 ****************************************************************************/
689
690 TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
691                           int open_flags, mode_t mode)
692 {
693         TDB_CONTEXT *tdb;
694         struct tdb_logging_context log_ctx;
695
696         if (!lp_use_mmap())
697                 tdb_flags |= TDB_NOMMAP;
698
699         log_ctx.log_fn = tdb_log;
700         log_ctx.log_private = NULL;
701
702         if ((hash_size == 0) && (name != NULL)) {
703                 const char *base = strrchr_m(name, '/');
704                 if (base != NULL) {
705                         base += 1;
706                 }
707                 else {
708                         base = name;
709                 }
710                 hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
711         }
712
713         tdb = tdb_open_ex(name, hash_size, tdb_flags, 
714                           open_flags, mode, &log_ctx, NULL);
715         if (!tdb)
716                 return NULL;
717
718         return tdb;
719 }
720
721 /****************************************************************************
722  Allow tdb_delete to be used as a tdb_traversal_fn.
723 ****************************************************************************/
724
725 int tdb_traverse_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
726                      void *state)
727 {
728     return tdb_delete(the_tdb, key);
729 }
730
731
732
733 /**
734  * Search across the whole tdb for keys that match the given pattern
735  * return the result as a list of keys
736  *
737  * @param tdb pointer to opened tdb file context
738  * @param pattern searching pattern used by fnmatch(3) functions
739  *
740  * @return list of keys found by looking up with given pattern
741  **/
742 TDB_LIST_NODE *tdb_search_keys(TDB_CONTEXT *tdb, const char* pattern)
743 {
744         TDB_DATA key, next;
745         TDB_LIST_NODE *list = NULL;
746         TDB_LIST_NODE *rec = NULL;
747         
748         for (key = tdb_firstkey(tdb); key.dptr; key = next) {
749                 /* duplicate key string to ensure null-termination */
750                 char *key_str = SMB_STRNDUP((const char *)key.dptr, key.dsize);
751                 if (!key_str) {
752                         DEBUG(0, ("tdb_search_keys: strndup() failed!\n"));
753                         smb_panic("strndup failed!\n");
754                 }
755                 
756                 DEBUG(18, ("checking %s for match to pattern %s\n", key_str, pattern));
757                 
758                 next = tdb_nextkey(tdb, key);
759
760                 /* do the pattern checking */
761                 if (fnmatch(pattern, key_str, 0) == 0) {
762                         rec = SMB_MALLOC_P(TDB_LIST_NODE);
763                         ZERO_STRUCTP(rec);
764
765                         rec->node_key = key;
766         
767                         DLIST_ADD_END(list, rec, TDB_LIST_NODE *);
768                 
769                         DEBUG(18, ("checking %s matched pattern %s\n", key_str, pattern));
770                 } else {
771                         free(key.dptr);
772                 }
773                 
774                 /* free duplicated key string */
775                 free(key_str);
776         }
777         
778         return list;
779
780 }
781
782
783 /**
784  * Free the list returned by tdb_search_keys
785  *
786  * @param node list of results found by tdb_search_keys
787  **/
788 void tdb_search_list_free(TDB_LIST_NODE* node)
789 {
790         TDB_LIST_NODE *next_node;
791         
792         while (node) {
793                 next_node = node->next;
794                 SAFE_FREE(node->node_key.dptr);
795                 SAFE_FREE(node);
796                 node = next_node;
797         };
798 }
799
800 /****************************************************************************
801  tdb_store, wrapped in a transaction. This way we make sure that a process
802  that dies within writing does not leave a corrupt tdb behind.
803 ****************************************************************************/
804
805 int tdb_trans_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
806                     int flag)
807 {
808         int res;
809
810         if ((res = tdb_transaction_start(tdb)) != 0) {
811                 DEBUG(5, ("tdb_transaction_start failed\n"));
812                 return res;
813         }
814
815         if ((res = tdb_store(tdb, key, dbuf, flag)) != 0) {
816                 DEBUG(10, ("tdb_store failed\n"));
817                 if (tdb_transaction_cancel(tdb) != 0) {
818                         smb_panic("Cancelling transaction failed");
819                 }
820                 return res;
821         }
822
823         if ((res = tdb_transaction_commit(tdb)) != 0) {
824                 DEBUG(5, ("tdb_transaction_commit failed\n"));
825         }
826
827         return res;
828 }
829
830 /****************************************************************************
831  tdb_delete, wrapped in a transaction. This way we make sure that a process
832  that dies within deleting does not leave a corrupt tdb behind.
833 ****************************************************************************/
834
835 int tdb_trans_delete(struct tdb_context *tdb, TDB_DATA key)
836 {
837         int res;
838
839         if ((res = tdb_transaction_start(tdb)) != 0) {
840                 DEBUG(5, ("tdb_transaction_start failed\n"));
841                 return res;
842         }
843
844         if ((res = tdb_delete(tdb, key)) != 0) {
845                 DEBUG(10, ("tdb_delete failed\n"));
846                 if (tdb_transaction_cancel(tdb) != 0) {
847                         smb_panic("Cancelling transaction failed");
848                 }
849                 return res;
850         }
851
852         if ((res = tdb_transaction_commit(tdb)) != 0) {
853                 DEBUG(5, ("tdb_transaction_commit failed\n"));
854         }
855
856         return res;
857 }
858
859 /*
860  Log tdb messages via DEBUG().
861 */
862 static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, 
863                          const char *format, ...) PRINTF_ATTRIBUTE(3,4);
864
865 static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, 
866                          const char *format, ...)
867 {
868         va_list ap;
869         char *ptr = NULL;
870         int debuglevel = 0;
871         int ret;
872
873         switch (level) {
874         case TDB_DEBUG_FATAL:
875                 debug_level = 0;
876                 break;
877         case TDB_DEBUG_ERROR:
878                 debuglevel = 1;
879                 break;
880         case TDB_DEBUG_WARNING:
881                 debuglevel = 2;
882                 break;
883         case TDB_DEBUG_TRACE:
884                 debuglevel = 5;
885                 break;
886         default:
887                 debuglevel = 0;
888         }               
889
890         va_start(ap, format);
891         ret = vasprintf(&ptr, format, ap);
892         va_end(ap);
893
894         if (ret != -1) {
895                 const char *name = tdb_name(tdb);
896                 DEBUG(debuglevel, ("tdb(%s): %s", name ? name : "unnamed", ptr));
897                 free(ptr);
898         }
899 }
900
901 static struct tdb_wrap *tdb_list;
902
903 /* destroy the last connection to a tdb */
904 static int tdb_wrap_destructor(struct tdb_wrap *w)
905 {
906         tdb_close(w->tdb);
907         DLIST_REMOVE(tdb_list, w);
908         return 0;
909 }                                
910
911 /*
912   wrapped connection to a tdb database
913   to close just talloc_free() the tdb_wrap pointer
914  */
915 struct tdb_wrap *tdb_wrap_open(TALLOC_CTX *mem_ctx,
916                                const char *name, int hash_size, int tdb_flags,
917                                int open_flags, mode_t mode)
918 {
919         struct tdb_wrap *w;
920         struct tdb_logging_context log_ctx;
921         log_ctx.log_fn = tdb_wrap_log;
922
923         if (!lp_use_mmap())
924                 tdb_flags |= TDB_NOMMAP;
925
926         for (w=tdb_list;w;w=w->next) {
927                 if (strcmp(name, w->name) == 0) {
928                         /*
929                          * Yes, talloc_reference is exactly what we want
930                          * here. Otherwise we would have to implement our own
931                          * reference counting.
932                          */
933                         return talloc_reference(mem_ctx, w);
934                 }
935         }
936
937         w = talloc(mem_ctx, struct tdb_wrap);
938         if (w == NULL) {
939                 return NULL;
940         }
941
942         if (!(w->name = talloc_strdup(w, name))) {
943                 talloc_free(w);
944                 return NULL;
945         }
946
947         if ((hash_size == 0) && (name != NULL)) {
948                 const char *base = strrchr_m(name, '/');
949                 if (base != NULL) {
950                         base += 1;
951                 }
952                 else {
953                         base = name;
954                 }
955                 hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
956         }
957
958         w->tdb = tdb_open_ex(name, hash_size, tdb_flags, 
959                              open_flags, mode, &log_ctx, NULL);
960         if (w->tdb == NULL) {
961                 talloc_free(w);
962                 return NULL;
963         }
964
965         talloc_set_destructor(w, tdb_wrap_destructor);
966
967         DLIST_ADD(tdb_list, w);
968
969         return w;
970 }
971
972 NTSTATUS map_nt_error_from_tdb(enum TDB_ERROR err)
973 {
974         struct { enum TDB_ERROR err; NTSTATUS status; } map[] =
975                 { { TDB_SUCCESS,        NT_STATUS_OK },
976                   { TDB_ERR_CORRUPT,    NT_STATUS_INTERNAL_DB_CORRUPTION },
977                   { TDB_ERR_IO,         NT_STATUS_UNEXPECTED_IO_ERROR },
978                   { TDB_ERR_OOM,        NT_STATUS_NO_MEMORY },
979                   { TDB_ERR_EXISTS,     NT_STATUS_OBJECT_NAME_COLLISION },
980
981                   /*
982                    * TDB_ERR_LOCK is very broad, we could for example
983                    * distinguish between fcntl locks and invalid lock
984                    * sequences. So NT_STATUS_FILE_LOCK_CONFLICT is a
985                    * compromise.
986                    */
987                   { TDB_ERR_LOCK,       NT_STATUS_FILE_LOCK_CONFLICT },
988                   /*
989                    * The next two ones in the enum are not actually used
990                    */
991                   { TDB_ERR_NOLOCK,     NT_STATUS_FILE_LOCK_CONFLICT },
992                   { TDB_ERR_LOCK_TIMEOUT, NT_STATUS_FILE_LOCK_CONFLICT },
993                   { TDB_ERR_NOEXIST,    NT_STATUS_NOT_FOUND },
994                   { TDB_ERR_EINVAL,     NT_STATUS_INVALID_PARAMETER },
995                   { TDB_ERR_RDONLY,     NT_STATUS_ACCESS_DENIED }
996                 };
997
998         int i;
999
1000         for (i=0; i < sizeof(map) / sizeof(map[0]); i++) {
1001                 if (err == map[i].err) {
1002                         return map[i].status;
1003                 }
1004         }
1005
1006         return NT_STATUS_INTERNAL_ERROR;
1007 }
1008
1009
1010 /*********************************************************************
1011  * the following is a generic validation mechanism for tdbs.
1012  *********************************************************************/
1013
1014 /* 
1015  * internal validation function, executed by the child.  
1016  */
1017 static int tdb_validate_child(struct tdb_context *tdb,
1018                               tdb_validate_data_func validate_fn)
1019 {
1020         int ret = 1;
1021         int num_entries = 0;
1022         struct tdb_validation_status v_status;
1023
1024         v_status.tdb_error = False;
1025         v_status.bad_freelist = False;
1026         v_status.bad_entry = False;
1027         v_status.unknown_key = False;
1028         v_status.success = True;
1029
1030         if (!tdb) {
1031                 v_status.tdb_error = True;
1032                 v_status.success = False;
1033                 goto out;
1034         }
1035
1036         /* Check if the tdb's freelist is good. */
1037         if (tdb_validate_freelist(tdb, &num_entries) == -1) {
1038                 v_status.bad_freelist = True;
1039                 v_status.success = False;
1040                 goto out;
1041         }
1042
1043         DEBUG(10,("tdb_validate_child: tdb %s freelist has %d entries\n",
1044                   tdb_name(tdb), num_entries));
1045
1046         /* Now traverse the tdb to validate it. */
1047         num_entries = tdb_traverse(tdb, validate_fn, (void *)&v_status);
1048         if (!v_status.success) {
1049                 goto out;
1050         } else if (num_entries == -1) {
1051                 v_status.tdb_error = True;
1052                 v_status.success = False;
1053                 goto out;
1054         }
1055
1056         DEBUG(10,("tdb_validate_child: tdb %s is good with %d entries\n",
1057                   tdb_name(tdb), num_entries));
1058         ret = 0; /* Cache is good. */
1059
1060 out:
1061         DEBUG(10,   ("tdb_validate_child: summary of validation status:\n"));
1062         DEBUGADD(10,(" * tdb error: %s\n", v_status.tdb_error ? "yes" : "no"));
1063         DEBUGADD(10,(" * bad freelist: %s\n",v_status.bad_freelist?"yes":"no"));
1064         DEBUGADD(10,(" * bad entry: %s\n", v_status.bad_entry ? "yes" : "no"));
1065         DEBUGADD(10,(" * unknown key: %s\n", v_status.unknown_key?"yes":"no"));
1066         DEBUGADD(10,(" => overall success: %s\n", v_status.success?"yes":"no"));
1067
1068         return ret;
1069 }
1070
1071 /*
1072  * tdb validation function.
1073  * returns 0 if tdb is ok, != 0 if it isn't.
1074  * this function expects an opened tdb.
1075  */
1076 int tdb_validate(struct tdb_context *tdb, tdb_validate_data_func validate_fn)
1077 {
1078         pid_t child_pid = -1;
1079         int child_status = 0;
1080         int wait_pid = 0;
1081         int ret = 1;
1082
1083         if (tdb == NULL) {
1084                 DEBUG(1, ("Error: tdb_validate called with tdb == NULL\n"));
1085                 return ret;
1086         }
1087
1088         DEBUG(5, ("tdb_validate called for tdb '%s'\n", tdb_name(tdb)));
1089
1090         /* fork and let the child do the validation.
1091          * benefit: no need to twist signal handlers and panic functions.
1092          * just let the child panic. we catch the signal. */
1093
1094         DEBUG(10, ("tdb_validate: forking to let child do validation.\n"));
1095         child_pid = sys_fork();
1096         if (child_pid == 0) {
1097                 /* child code */
1098                 DEBUG(10, ("tdb_validate (validation child): created\n"));
1099                 DEBUG(10, ("tdb_validate (validation child): "
1100                            "calling tdb_validate_child\n"));
1101                 exit(tdb_validate_child(tdb, validate_fn));
1102         }
1103         else if (child_pid < 0) {
1104                 DEBUG(1, ("tdb_validate: fork for validation failed.\n"));
1105                 goto done;
1106         }
1107
1108         /* parent */
1109
1110         DEBUG(10, ("tdb_validate: fork succeeded, child PID = %d\n",child_pid));
1111
1112         DEBUG(10, ("tdb_validate: waiting for child to finish...\n"));
1113         while  ((wait_pid = sys_waitpid(child_pid, &child_status, 0)) < 0) {
1114                 if (errno == EINTR) {
1115                         DEBUG(10, ("tdb_validate: got signal during waitpid, "
1116                                    "retrying\n"));
1117                         errno = 0;
1118                         continue;
1119                 }
1120                 DEBUG(1, ("tdb_validate: waitpid failed with error '%s'.\n",
1121                           strerror(errno)));
1122                 goto done;
1123         }
1124         if (wait_pid != child_pid) {
1125                 DEBUG(1, ("tdb_validate: waitpid returned pid %d, "
1126                           "but %d was expected\n", wait_pid, child_pid));
1127                 goto done;
1128         }
1129
1130         DEBUG(10, ("tdb_validate: validating child returned.\n"));
1131         if (WIFEXITED(child_status)) {
1132                 DEBUG(10, ("tdb_validate: child exited, code %d.\n",
1133                            WEXITSTATUS(child_status)));
1134                 ret = WEXITSTATUS(child_status);
1135         }
1136         if (WIFSIGNALED(child_status)) {
1137                 DEBUG(10, ("tdb_validate: child terminated by signal %d\n",
1138                            WTERMSIG(child_status)));
1139 #ifdef WCOREDUMP
1140                 if (WCOREDUMP(child_status)) {
1141                         DEBUGADD(10, ("core dumped\n"));
1142                 }
1143 #endif
1144                 ret = WTERMSIG(child_status);
1145         }
1146         if (WIFSTOPPED(child_status)) {
1147                 DEBUG(10, ("tdb_validate: child was stopped by signal %d\n",
1148                            WSTOPSIG(child_status)));
1149                 ret = WSTOPSIG(child_status);
1150         }
1151
1152 done:
1153         DEBUG(5, ("tdb_validate returning code '%d' for tdb '%s'\n", ret,
1154                   tdb_name(tdb)));
1155
1156         return ret;
1157 }
1158
1159 /*
1160  * tdb validation function.
1161  * returns 0 if tdb is ok, != 0 if it isn't.
1162  * this is a wrapper around the actual validation function that opens and closes
1163  * the tdb.
1164  */
1165 int tdb_validate_open(const char *tdb_path, tdb_validate_data_func validate_fn)
1166 {
1167         TDB_CONTEXT *tdb = NULL;
1168         int ret = 1;
1169
1170         DEBUG(5, ("tdb_validate_open called for tdb '%s'\n", tdb_path));
1171
1172         tdb = tdb_open_log(tdb_path, 0, TDB_DEFAULT, O_RDONLY, 0);
1173         if (!tdb) {
1174                 DEBUG(1, ("Error opening tdb %s\n", tdb_path));
1175                 return ret;
1176         }
1177
1178         ret = tdb_validate(tdb, validate_fn);
1179         tdb_close(tdb);
1180         return ret;
1181 }
1182
1183 /*
1184  * tdb backup function and helpers for tdb_validate wrapper with backup
1185  * handling.
1186  */
1187
1188 /* this structure eliminates the need for a global overall status for
1189  * the traverse-copy */
1190 struct tdb_copy_data {
1191         struct tdb_context *dst;
1192         bool success;
1193 };
1194
1195 static int traverse_copy_fn(struct tdb_context *tdb, TDB_DATA key,
1196                             TDB_DATA dbuf, void *private_data)
1197 {
1198         struct tdb_copy_data *data = (struct tdb_copy_data *)private_data;
1199
1200         if (tdb_store(data->dst, key, dbuf, TDB_INSERT) != 0) {
1201                 DEBUG(4, ("Failed to insert into %s: %s\n", tdb_name(data->dst),
1202                           strerror(errno)));
1203                 data->success = False;
1204                 return 1;
1205         }
1206         return 0;
1207 }
1208
1209 static int tdb_copy(struct tdb_context *src, struct tdb_context *dst)
1210 {
1211         struct tdb_copy_data data;
1212         int count;
1213
1214         data.dst = dst;
1215         data.success = True;
1216
1217         count = tdb_traverse(src, traverse_copy_fn, (void *)(&data));
1218         if ((count < 0) || (data.success == False)) {
1219                 return -1;
1220         }
1221         return count;
1222 }
1223
1224 static int tdb_verify_basic(struct tdb_context *tdb)
1225 {
1226         return tdb_traverse(tdb, NULL, NULL);
1227 }
1228
1229 /* this backup function is essentially taken from lib/tdb/tools/tdbbackup.tdb
1230  */
1231 static int tdb_backup(TALLOC_CTX *ctx, const char *src_path,
1232                       const char *dst_path, int hash_size)
1233 {
1234         struct tdb_context *src_tdb = NULL;
1235         struct tdb_context *dst_tdb = NULL;
1236         char *tmp_path = NULL;
1237         struct stat st;
1238         int count1, count2;
1239         int saved_errno = 0;
1240         int ret = -1;
1241
1242         if (stat(src_path, &st) != 0) {
1243                 DEBUG(3, ("Could not stat '%s': %s\n", src_path,
1244                           strerror(errno)));
1245                 goto done;
1246         }
1247
1248         /* open old tdb RDWR - so we can lock it */
1249         src_tdb = tdb_open_log(src_path, 0, TDB_DEFAULT, O_RDWR, 0);
1250         if (src_tdb == NULL) {
1251                 DEBUG(3, ("Failed to open tdb '%s'\n", src_path));
1252                 goto done;
1253         }
1254
1255         if (tdb_lockall(src_tdb) != 0) {
1256                 DEBUG(3, ("Failed to lock tdb '%s'\n", src_path));
1257                 goto done;
1258         }
1259
1260         tmp_path = talloc_asprintf(ctx, "%s%s", dst_path, ".tmp");
1261         unlink(tmp_path);
1262         dst_tdb = tdb_open_log(tmp_path,
1263                                hash_size ? hash_size : tdb_hash_size(src_tdb),
1264                                TDB_DEFAULT, O_RDWR | O_CREAT | O_EXCL,
1265                                st.st_mode & 0777);
1266         if (dst_tdb == NULL) {
1267                 DEBUG(3, ("Error creating tdb '%s': %s\n", tmp_path,
1268                           strerror(errno)));
1269                 saved_errno = errno;
1270                 unlink(tmp_path);
1271                 goto done;
1272         }
1273
1274         count1 = tdb_copy(src_tdb, dst_tdb);
1275         if (count1 < 0) {
1276                 DEBUG(3, ("Failed to copy tdb '%s': %s\n", src_path,
1277                           strerror(errno)));
1278                 tdb_close(dst_tdb);
1279                 goto done;
1280         }
1281
1282         /* reopen ro and do basic verification */
1283         tdb_close(dst_tdb);
1284         dst_tdb = tdb_open_log(tmp_path, 0, TDB_DEFAULT, O_RDONLY, 0);
1285         if (!dst_tdb) {
1286                 DEBUG(3, ("Failed to reopen tdb '%s': %s\n", tmp_path,
1287                           strerror(errno)));
1288                 goto done;
1289         }
1290         count2 = tdb_verify_basic(dst_tdb);
1291         if (count2 != count1) {
1292                 DEBUG(3, ("Failed to verify result of copying tdb '%s'.\n",
1293                           src_path));
1294                 tdb_close(dst_tdb);
1295                 goto done;
1296         }
1297
1298         DEBUG(10, ("tdb_backup: successfully copied %d entries\n", count1));
1299
1300         /* make sure the new tdb has reached stable storage
1301          * then rename it to its destination */
1302         fsync(tdb_fd(dst_tdb));
1303         tdb_close(dst_tdb);
1304         unlink(dst_path);
1305         if (rename(tmp_path, dst_path) != 0) {
1306                 DEBUG(3, ("Failed to rename '%s' to '%s': %s\n",
1307                           tmp_path, dst_path, strerror(errno)));
1308                 goto done;
1309         }
1310
1311         /* success */
1312         ret = 0;
1313
1314 done:
1315         if (src_tdb != NULL) {
1316                 tdb_close(src_tdb);
1317         }
1318         if (tmp_path != NULL) {
1319                 unlink(tmp_path);
1320                 TALLOC_FREE(tmp_path);
1321         }
1322         if (saved_errno != 0) {
1323                 errno = saved_errno;
1324         }
1325         return ret;
1326 }
1327
1328 static int rename_file_with_suffix(TALLOC_CTX *ctx, const char *path,
1329                                    const char *suffix)
1330 {
1331         int ret = -1;
1332         char *dst_path;
1333
1334         dst_path = talloc_asprintf(ctx, "%s%s", path, suffix);
1335
1336         ret = (rename(path, dst_path) != 0);
1337
1338         if (ret == 0) {
1339                 DEBUG(5, ("moved '%s' to '%s'\n", path, dst_path));
1340         } else if (errno == ENOENT) {
1341                 DEBUG(3, ("file '%s' does not exist - so not moved\n", path));
1342                 ret = 0;
1343         } else {
1344                 DEBUG(3, ("error renaming %s to %s: %s\n", path, dst_path,
1345                           strerror(errno)));
1346         }
1347
1348         TALLOC_FREE(dst_path);
1349         return ret;
1350 }
1351
1352 /*
1353  * do a backup of a tdb, moving the destination out of the way first
1354  */
1355 static int tdb_backup_with_rotate(TALLOC_CTX *ctx, const char *src_path,
1356                                   const char *dst_path, int hash_size,
1357                                   const char *rotate_suffix,
1358                                   bool retry_norotate_if_nospc,
1359                                   bool rename_as_last_resort_if_nospc)
1360 {
1361         int ret;
1362
1363         rename_file_with_suffix(ctx, dst_path, rotate_suffix);
1364
1365         ret = tdb_backup(ctx, src_path, dst_path, hash_size);
1366
1367         if (ret != 0) {
1368                 DEBUG(10, ("backup of %s failed: %s\n", src_path, strerror(errno)));
1369         }
1370         if ((ret != 0) && (errno == ENOSPC) && retry_norotate_if_nospc)
1371         {
1372                 char *rotate_path = talloc_asprintf(ctx, "%s%s", dst_path,
1373                                                     rotate_suffix);
1374                 DEBUG(10, ("backup of %s failed due to lack of space\n",
1375                            src_path));
1376                 DEBUGADD(10, ("trying to free some space by removing rotated "
1377                               "dst %s\n", rotate_path));
1378                 if (unlink(rotate_path) == -1) {
1379                         DEBUG(10, ("unlink of %s failed: %s\n", rotate_path,
1380                                    strerror(errno)));
1381                 } else {
1382                         ret = tdb_backup(ctx, src_path, dst_path, hash_size);
1383                 }
1384                 TALLOC_FREE(rotate_path);
1385         }
1386
1387         if ((ret != 0) && (errno == ENOSPC) && rename_as_last_resort_if_nospc)
1388         {
1389                 DEBUG(10, ("backup of %s failed due to lack of space\n", 
1390                            src_path));
1391                 DEBUGADD(10, ("using 'rename' as a last resort\n"));
1392                 ret = rename(src_path, dst_path);
1393         }
1394
1395         return ret;
1396 }
1397
1398 /*
1399  * validation function with backup handling:
1400  *
1401  *  - calls tdb_validate
1402  *  - if the tdb is ok, create a backup "name.bak", possibly moving
1403  *    existing backup to name.bak.old,
1404  *    return 0 (success) even if the backup fails
1405  *  - if the tdb is corrupt:
1406  *    - move the tdb to "name.corrupt"
1407  *    - check if there is valid backup.
1408  *      if so, restore the backup.
1409  *      if restore is successful, return 0 (success),
1410  *    - otherwise return -1 (failure)
1411  */
1412 int tdb_validate_and_backup(const char *tdb_path,
1413                             tdb_validate_data_func validate_fn)
1414 {
1415         int ret = -1;
1416         const char *backup_suffix = ".bak";
1417         const char *corrupt_suffix = ".corrupt";
1418         const char *rotate_suffix = ".old";
1419         char *tdb_path_backup;
1420         struct stat st;
1421         TALLOC_CTX *ctx = NULL;
1422
1423         ctx = talloc_new(NULL);
1424         if (ctx == NULL) {
1425                 DEBUG(0, ("tdb_validate_and_backup: out of memory\n"));
1426                 goto done;
1427         }
1428
1429         tdb_path_backup = talloc_asprintf(ctx, "%s%s", tdb_path, backup_suffix);
1430
1431         ret = tdb_validate_open(tdb_path, validate_fn);
1432
1433         if (ret == 0) {
1434                 DEBUG(1, ("tdb '%s' is valid\n", tdb_path));
1435                 ret = tdb_backup_with_rotate(ctx, tdb_path, tdb_path_backup, 0,
1436                                              rotate_suffix, True, False);
1437                 if (ret != 0) {
1438                         DEBUG(1, ("Error creating backup of tdb '%s'\n",
1439                                   tdb_path));
1440                         /* the actual validation was successful: */
1441                         ret = 0;
1442                 } else {
1443                         DEBUG(1, ("Created backup '%s' of tdb '%s'\n",
1444                                   tdb_path_backup, tdb_path));
1445                 }
1446         } else {
1447                 DEBUG(1, ("tdb '%s' is invalid\n", tdb_path));
1448
1449                 ret =stat(tdb_path_backup, &st);
1450                 if (ret != 0) {
1451                         DEBUG(5, ("Could not stat '%s': %s\n", tdb_path_backup,
1452                                   strerror(errno)));
1453                         DEBUG(1, ("No backup found.\n"));
1454                 } else {
1455                         DEBUG(1, ("backup '%s' found.\n", tdb_path_backup));
1456                         ret = tdb_validate_open(tdb_path_backup, validate_fn);
1457                         if (ret != 0) {
1458                                 DEBUG(1, ("Backup '%s' is invalid.\n",
1459                                           tdb_path_backup));
1460                         }
1461                 }
1462
1463                 if (ret != 0) {
1464                         int renamed = rename_file_with_suffix(ctx, tdb_path,
1465                                                               corrupt_suffix);
1466                         if (renamed != 0) {
1467                                 DEBUG(1, ("Error moving tdb to '%s%s'\n",
1468                                           tdb_path, corrupt_suffix));
1469                         } else {
1470                                 DEBUG(1, ("Corrupt tdb stored as '%s%s'\n",
1471                                           tdb_path, corrupt_suffix));
1472                         }
1473                         goto done;
1474                 }
1475
1476                 DEBUG(1, ("valid backup '%s' found\n", tdb_path_backup));
1477                 ret = tdb_backup_with_rotate(ctx, tdb_path_backup, tdb_path, 0,
1478                                              corrupt_suffix, True, True);
1479                 if (ret != 0) {
1480                         DEBUG(1, ("Error restoring backup from '%s'\n",
1481                                   tdb_path_backup));
1482                 } else {
1483                         DEBUG(1, ("Restored tdb backup from '%s'\n",
1484                                   tdb_path_backup));
1485                 }
1486         }
1487
1488 done:
1489         TALLOC_FREE(ctx);
1490         return ret;
1491 }