Merge branch 'selftest' of git://git.samba.org/jelmer/samba
[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    Copyright (C) Michael Adam      2007
7    
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.
12    
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.
17    
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/>.
20 */
21
22 #include "includes.h"
23 #undef malloc
24 #undef realloc
25 #undef calloc
26 #undef strdup
27
28 /* these are little tdb utility functions that are meant to make
29    dealing with a tdb database a little less cumbersome in Samba */
30
31 static SIG_ATOMIC_T gotalarm;
32
33 /***************************************************************
34  Signal function to tell us we timed out.
35 ****************************************************************/
36
37 static void gotalarm_sig(void)
38 {
39         gotalarm = 1;
40 }
41
42 /****************************************************************************
43  Lock a chain with timeout (in seconds).
44 ****************************************************************************/
45
46 static int tdb_chainlock_with_timeout_internal( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout, int rw_type)
47 {
48         /* Allow tdb_chainlock to be interrupted by an alarm. */
49         int ret;
50         gotalarm = 0;
51
52         if (timeout) {
53                 CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
54                 tdb_setalarm_sigptr(tdb, &gotalarm);
55                 alarm(timeout);
56         }
57
58         if (rw_type == F_RDLCK)
59                 ret = tdb_chainlock_read(tdb, key);
60         else
61                 ret = tdb_chainlock(tdb, key);
62
63         if (timeout) {
64                 alarm(0);
65                 tdb_setalarm_sigptr(tdb, NULL);
66                 CatchSignal(SIGALRM, SIGNAL_CAST SIG_IGN);
67                 if (gotalarm) {
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;
75                 }
76         }
77
78         return ret;
79 }
80
81 /****************************************************************************
82  Write lock a chain. Return -1 if timeout or lock failed.
83 ****************************************************************************/
84
85 int tdb_chainlock_with_timeout( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout)
86 {
87         return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_WRLCK);
88 }
89
90 int tdb_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval,
91                                    int timeout)
92 {
93         TDB_DATA key = string_term_tdb_data(keyval);
94         
95         return tdb_chainlock_with_timeout(tdb, key, timeout);
96 }
97
98 /****************************************************************************
99  Read lock a chain by string. Return -1 if timeout or lock failed.
100 ****************************************************************************/
101
102 int tdb_read_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval, unsigned int timeout)
103 {
104         TDB_DATA key = string_term_tdb_data(keyval);
105         
106         return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_RDLCK);
107 }
108
109
110
111
112 int tdb_trans_store_bystring(TDB_CONTEXT *tdb, const char *keystr,
113                              TDB_DATA data, int flags)
114 {
115         TDB_DATA key = string_term_tdb_data(keystr);
116         
117         return tdb_trans_store(tdb, key, data, flags);
118 }
119
120 /****************************************************************************
121  Useful pair of routines for packing/unpacking data consisting of
122  integers and strings.
123 ****************************************************************************/
124
125 static size_t tdb_pack_va(uint8 *buf, int bufsize, const char *fmt, va_list ap)
126 {
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;
138
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);
187                         }
188                         break;
189                 default:
190                         DEBUG(0,("Unknown tdb_pack format %c in %s\n", 
191                                  c, fmt));
192                         len = 0;
193                         break;
194                 }
195
196                 buf += len;
197                 if (bufsize)
198                         bufsize -= len;
199                 if (bufsize < 0)
200                         bufsize = 0;
201         }
202
203         DEBUG(18,("tdb_pack_va(%s, %d) -> %d\n", 
204                  fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
205         
206         return PTR_DIFF(buf, buf0);
207 }
208
209 size_t tdb_pack(uint8 *buf, int bufsize, const char *fmt, ...)
210 {
211         va_list ap;
212         size_t result;
213
214         va_start(ap, fmt);
215         result = tdb_pack_va(buf, bufsize, fmt, ap);
216         va_end(ap);
217         return result;
218 }
219
220 bool tdb_pack_append(TALLOC_CTX *mem_ctx, uint8 **buf, size_t *len,
221                      const char *fmt, ...)
222 {
223         va_list ap;
224         size_t len1, len2;
225
226         va_start(ap, fmt);
227         len1 = tdb_pack_va(NULL, 0, fmt, ap);
228         va_end(ap);
229
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);
235         }
236
237         if (*buf == NULL) {
238                 return False;
239         }
240
241         va_start(ap, fmt);
242         len2 = tdb_pack_va((*buf)+(*len), len1, fmt, ap);
243         va_end(ap);
244
245         if (len1 != len2) {
246                 return False;
247         }
248
249         *len += len2;
250
251         return True;
252 }
253
254 /****************************************************************************
255  Useful pair of routines for packing/unpacking data consisting of
256  integers and strings.
257 ****************************************************************************/
258
259 int tdb_unpack(const uint8 *buf, int bufsize, const char *fmt, ...)
260 {
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;
273
274         va_start(ap, fmt);
275
276         while (*fmt) {
277                 switch ((c=*fmt++)) {
278                 case 'b':
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':
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':
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':
300                         len = 4;
301                         p = va_arg(ap, void **);
302                         if (bufsize < len)
303                                 goto no_space;
304                         /*
305                          * This isn't a real pointer - only a token (1 or 0)
306                          * to mark the fact a pointer is present.
307                          */
308
309                         *p = (void *)(IVAL(buf, 0) ? (void *)1 : NULL);
310                         break;
311                 case 'P':
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':
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':
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;
334                         }
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));
346
347                         len = 0;
348                         break;
349                 }
350
351                 buf += len;
352                 bufsize -= len;
353         }
354
355         va_end(ap);
356
357         DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
358                  fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
359
360         return PTR_DIFF(buf, buf0);
361
362  no_space:
363         va_end(ap);
364         return -1;
365 }
366
367
368 /****************************************************************************
369  Log tdb messages via DEBUG().
370 ****************************************************************************/
371
372 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, const char *format, ...)
373 {
374         va_list ap;
375         char *ptr = NULL;
376         int ret;
377
378         va_start(ap, format);
379         ret = vasprintf(&ptr, format, ap);
380         va_end(ap);
381
382         if ((ret == -1) || !*ptr)
383                 return;
384
385         DEBUG((int)level, ("tdb(%s): %s", tdb_name(tdb) ? tdb_name(tdb) : "unnamed", ptr));
386         SAFE_FREE(ptr);
387 }
388
389 /****************************************************************************
390  Like tdb_open() but also setup a logging function that redirects to
391  the samba DEBUG() system.
392 ****************************************************************************/
393
394 TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
395                           int open_flags, mode_t mode)
396 {
397         TDB_CONTEXT *tdb;
398         struct tdb_logging_context log_ctx;
399
400         if (!lp_use_mmap())
401                 tdb_flags |= TDB_NOMMAP;
402
403         log_ctx.log_fn = tdb_log;
404         log_ctx.log_private = NULL;
405
406         if ((hash_size == 0) && (name != NULL)) {
407                 const char *base = strrchr_m(name, '/');
408                 if (base != NULL) {
409                         base += 1;
410                 }
411                 else {
412                         base = name;
413                 }
414                 hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
415         }
416
417         tdb = tdb_open_ex(name, hash_size, tdb_flags, 
418                           open_flags, mode, &log_ctx, NULL);
419         if (!tdb)
420                 return NULL;
421
422         return tdb;
423 }
424
425
426 /**
427  * Search across the whole tdb for keys that match the given pattern
428  * return the result as a list of keys
429  *
430  * @param tdb pointer to opened tdb file context
431  * @param pattern searching pattern used by fnmatch(3) functions
432  *
433  * @return list of keys found by looking up with given pattern
434  **/
435 TDB_LIST_NODE *tdb_search_keys(TDB_CONTEXT *tdb, const char* pattern)
436 {
437         TDB_DATA key, next;
438         TDB_LIST_NODE *list = NULL;
439         TDB_LIST_NODE *rec = NULL;
440         
441         for (key = tdb_firstkey(tdb); key.dptr; key = next) {
442                 /* duplicate key string to ensure null-termination */
443                 char *key_str = SMB_STRNDUP((const char *)key.dptr, key.dsize);
444                 if (!key_str) {
445                         DEBUG(0, ("tdb_search_keys: strndup() failed!\n"));
446                         smb_panic("strndup failed!\n");
447                 }
448                 
449                 DEBUG(18, ("checking %s for match to pattern %s\n", key_str, pattern));
450                 
451                 next = tdb_nextkey(tdb, key);
452
453                 /* do the pattern checking */
454                 if (fnmatch(pattern, key_str, 0) == 0) {
455                         rec = SMB_MALLOC_P(TDB_LIST_NODE);
456                         ZERO_STRUCTP(rec);
457
458                         rec->node_key = key;
459         
460                         DLIST_ADD_END(list, rec, TDB_LIST_NODE *);
461                 
462                         DEBUG(18, ("checking %s matched pattern %s\n", key_str, pattern));
463                 } else {
464                         free(key.dptr);
465                 }
466                 
467                 /* free duplicated key string */
468                 free(key_str);
469         }
470         
471         return list;
472
473 }
474
475
476 /**
477  * Free the list returned by tdb_search_keys
478  *
479  * @param node list of results found by tdb_search_keys
480  **/
481 void tdb_search_list_free(TDB_LIST_NODE* node)
482 {
483         TDB_LIST_NODE *next_node;
484         
485         while (node) {
486                 next_node = node->next;
487                 SAFE_FREE(node->node_key.dptr);
488                 SAFE_FREE(node);
489                 node = next_node;
490         };
491 }
492
493 /****************************************************************************
494  tdb_store, wrapped in a transaction. This way we make sure that a process
495  that dies within writing does not leave a corrupt tdb behind.
496 ****************************************************************************/
497
498 int tdb_trans_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
499                     int flag)
500 {
501         int res;
502
503         if ((res = tdb_transaction_start(tdb)) != 0) {
504                 DEBUG(5, ("tdb_transaction_start failed\n"));
505                 return res;
506         }
507
508         if ((res = tdb_store(tdb, key, dbuf, flag)) != 0) {
509                 DEBUG(10, ("tdb_store failed\n"));
510                 if (tdb_transaction_cancel(tdb) != 0) {
511                         smb_panic("Cancelling transaction failed");
512                 }
513                 return res;
514         }
515
516         if ((res = tdb_transaction_commit(tdb)) != 0) {
517                 DEBUG(5, ("tdb_transaction_commit failed\n"));
518         }
519
520         return res;
521 }
522
523 /****************************************************************************
524  tdb_delete, wrapped in a transaction. This way we make sure that a process
525  that dies within deleting does not leave a corrupt tdb behind.
526 ****************************************************************************/
527
528 int tdb_trans_delete(struct tdb_context *tdb, TDB_DATA key)
529 {
530         int res;
531
532         if ((res = tdb_transaction_start(tdb)) != 0) {
533                 DEBUG(5, ("tdb_transaction_start failed\n"));
534                 return res;
535         }
536
537         if ((res = tdb_delete(tdb, key)) != 0) {
538                 DEBUG(10, ("tdb_delete failed\n"));
539                 if (tdb_transaction_cancel(tdb) != 0) {
540                         smb_panic("Cancelling transaction failed");
541                 }
542                 return res;
543         }
544
545         if ((res = tdb_transaction_commit(tdb)) != 0) {
546                 DEBUG(5, ("tdb_transaction_commit failed\n"));
547         }
548
549         return res;
550 }
551
552 /*
553  Log tdb messages via DEBUG().
554 */
555 static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, 
556                          const char *format, ...) PRINTF_ATTRIBUTE(3,4);
557
558 static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, 
559                          const char *format, ...)
560 {
561         va_list ap;
562         char *ptr = NULL;
563         int debuglevel = 0;
564         int ret;
565
566         switch (level) {
567         case TDB_DEBUG_FATAL:
568                 debuglevel = 0;
569                 break;
570         case TDB_DEBUG_ERROR:
571                 debuglevel = 1;
572                 break;
573         case TDB_DEBUG_WARNING:
574                 debuglevel = 2;
575                 break;
576         case TDB_DEBUG_TRACE:
577                 debuglevel = 5;
578                 break;
579         default:
580                 debuglevel = 0;
581         }               
582
583         va_start(ap, format);
584         ret = vasprintf(&ptr, format, ap);
585         va_end(ap);
586
587         if (ret != -1) {
588                 const char *name = tdb_name(tdb);
589                 DEBUG(debuglevel, ("tdb(%s): %s", name ? name : "unnamed", ptr));
590                 free(ptr);
591         }
592 }
593
594 static struct tdb_wrap *tdb_list;
595
596 /* destroy the last connection to a tdb */
597 static int tdb_wrap_destructor(struct tdb_wrap *w)
598 {
599         tdb_close(w->tdb);
600         DLIST_REMOVE(tdb_list, w);
601         return 0;
602 }                                
603
604 /*
605   wrapped connection to a tdb database
606   to close just talloc_free() the tdb_wrap pointer
607  */
608 struct tdb_wrap *tdb_wrap_open(TALLOC_CTX *mem_ctx,
609                                const char *name, int hash_size, int tdb_flags,
610                                int open_flags, mode_t mode)
611 {
612         struct tdb_wrap *w;
613         struct tdb_logging_context log_ctx;
614         log_ctx.log_fn = tdb_wrap_log;
615
616         if (!lp_use_mmap())
617                 tdb_flags |= TDB_NOMMAP;
618
619         for (w=tdb_list;w;w=w->next) {
620                 if (strcmp(name, w->name) == 0) {
621                         /*
622                          * Yes, talloc_reference is exactly what we want
623                          * here. Otherwise we would have to implement our own
624                          * reference counting.
625                          */
626                         return talloc_reference(mem_ctx, w);
627                 }
628         }
629
630         w = talloc(mem_ctx, struct tdb_wrap);
631         if (w == NULL) {
632                 return NULL;
633         }
634
635         if (!(w->name = talloc_strdup(w, name))) {
636                 talloc_free(w);
637                 return NULL;
638         }
639
640         if ((hash_size == 0) && (name != NULL)) {
641                 const char *base = strrchr_m(name, '/');
642                 if (base != NULL) {
643                         base += 1;
644                 }
645                 else {
646                         base = name;
647                 }
648                 hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
649         }
650
651         w->tdb = tdb_open_ex(name, hash_size, tdb_flags, 
652                              open_flags, mode, &log_ctx, NULL);
653         if (w->tdb == NULL) {
654                 talloc_free(w);
655                 return NULL;
656         }
657
658         talloc_set_destructor(w, tdb_wrap_destructor);
659
660         DLIST_ADD(tdb_list, w);
661
662         return w;
663 }
664
665 NTSTATUS map_nt_error_from_tdb(enum TDB_ERROR err)
666 {
667         struct { enum TDB_ERROR err; NTSTATUS status; } map[] =
668                 { { TDB_SUCCESS,        NT_STATUS_OK },
669                   { TDB_ERR_CORRUPT,    NT_STATUS_INTERNAL_DB_CORRUPTION },
670                   { TDB_ERR_IO,         NT_STATUS_UNEXPECTED_IO_ERROR },
671                   { TDB_ERR_OOM,        NT_STATUS_NO_MEMORY },
672                   { TDB_ERR_EXISTS,     NT_STATUS_OBJECT_NAME_COLLISION },
673
674                   /*
675                    * TDB_ERR_LOCK is very broad, we could for example
676                    * distinguish between fcntl locks and invalid lock
677                    * sequences. So NT_STATUS_FILE_LOCK_CONFLICT is a
678                    * compromise.
679                    */
680                   { TDB_ERR_LOCK,       NT_STATUS_FILE_LOCK_CONFLICT },
681                   /*
682                    * The next two ones in the enum are not actually used
683                    */
684                   { TDB_ERR_NOLOCK,     NT_STATUS_FILE_LOCK_CONFLICT },
685                   { TDB_ERR_LOCK_TIMEOUT, NT_STATUS_FILE_LOCK_CONFLICT },
686                   { TDB_ERR_NOEXIST,    NT_STATUS_NOT_FOUND },
687                   { TDB_ERR_EINVAL,     NT_STATUS_INVALID_PARAMETER },
688                   { TDB_ERR_RDONLY,     NT_STATUS_ACCESS_DENIED }
689                 };
690
691         int i;
692
693         for (i=0; i < sizeof(map) / sizeof(map[0]); i++) {
694                 if (err == map[i].err) {
695                         return map[i].status;
696                 }
697         }
698
699         return NT_STATUS_INTERNAL_ERROR;
700 }
701
702
703 /*********************************************************************
704  * the following is a generic validation mechanism for tdbs.
705  *********************************************************************/
706
707 /* 
708  * internal validation function, executed by the child.  
709  */
710 static int tdb_validate_child(struct tdb_context *tdb,
711                               tdb_validate_data_func validate_fn)
712 {
713         int ret = 1;
714         int num_entries = 0;
715         struct tdb_validation_status v_status;
716
717         v_status.tdb_error = False;
718         v_status.bad_freelist = False;
719         v_status.bad_entry = False;
720         v_status.unknown_key = False;
721         v_status.success = True;
722
723         if (!tdb) {
724                 v_status.tdb_error = True;
725                 v_status.success = False;
726                 goto out;
727         }
728
729         /* Check if the tdb's freelist is good. */
730         if (tdb_validate_freelist(tdb, &num_entries) == -1) {
731                 v_status.bad_freelist = True;
732                 v_status.success = False;
733                 goto out;
734         }
735
736         DEBUG(10,("tdb_validate_child: tdb %s freelist has %d entries\n",
737                   tdb_name(tdb), num_entries));
738
739         /* Now traverse the tdb to validate it. */
740         num_entries = tdb_traverse(tdb, validate_fn, (void *)&v_status);
741         if (!v_status.success) {
742                 goto out;
743         } else if (num_entries == -1) {
744                 v_status.tdb_error = True;
745                 v_status.success = False;
746                 goto out;
747         }
748
749         DEBUG(10,("tdb_validate_child: tdb %s is good with %d entries\n",
750                   tdb_name(tdb), num_entries));
751         ret = 0; /* Cache is good. */
752
753 out:
754         DEBUG(10,   ("tdb_validate_child: summary of validation status:\n"));
755         DEBUGADD(10,(" * tdb error: %s\n", v_status.tdb_error ? "yes" : "no"));
756         DEBUGADD(10,(" * bad freelist: %s\n",v_status.bad_freelist?"yes":"no"));
757         DEBUGADD(10,(" * bad entry: %s\n", v_status.bad_entry ? "yes" : "no"));
758         DEBUGADD(10,(" * unknown key: %s\n", v_status.unknown_key?"yes":"no"));
759         DEBUGADD(10,(" => overall success: %s\n", v_status.success?"yes":"no"));
760
761         return ret;
762 }
763
764 /*
765  * tdb validation function.
766  * returns 0 if tdb is ok, != 0 if it isn't.
767  * this function expects an opened tdb.
768  */
769 int tdb_validate(struct tdb_context *tdb, tdb_validate_data_func validate_fn)
770 {
771         pid_t child_pid = -1;
772         int child_status = 0;
773         int wait_pid = 0;
774         int ret = 1;
775
776         if (tdb == NULL) {
777                 DEBUG(1, ("Error: tdb_validate called with tdb == NULL\n"));
778                 return ret;
779         }
780
781         DEBUG(5, ("tdb_validate called for tdb '%s'\n", tdb_name(tdb)));
782
783         /* fork and let the child do the validation.
784          * benefit: no need to twist signal handlers and panic functions.
785          * just let the child panic. we catch the signal. */
786
787         DEBUG(10, ("tdb_validate: forking to let child do validation.\n"));
788         child_pid = sys_fork();
789         if (child_pid == 0) {
790                 /* child code */
791                 DEBUG(10, ("tdb_validate (validation child): created\n"));
792                 DEBUG(10, ("tdb_validate (validation child): "
793                            "calling tdb_validate_child\n"));
794                 exit(tdb_validate_child(tdb, validate_fn));
795         }
796         else if (child_pid < 0) {
797                 DEBUG(1, ("tdb_validate: fork for validation failed.\n"));
798                 goto done;
799         }
800
801         /* parent */
802
803         DEBUG(10, ("tdb_validate: fork succeeded, child PID = %d\n",child_pid));
804
805         DEBUG(10, ("tdb_validate: waiting for child to finish...\n"));
806         while  ((wait_pid = sys_waitpid(child_pid, &child_status, 0)) < 0) {
807                 if (errno == EINTR) {
808                         DEBUG(10, ("tdb_validate: got signal during waitpid, "
809                                    "retrying\n"));
810                         errno = 0;
811                         continue;
812                 }
813                 DEBUG(1, ("tdb_validate: waitpid failed with error '%s'.\n",
814                           strerror(errno)));
815                 goto done;
816         }
817         if (wait_pid != child_pid) {
818                 DEBUG(1, ("tdb_validate: waitpid returned pid %d, "
819                           "but %d was expected\n", wait_pid, child_pid));
820                 goto done;
821         }
822
823         DEBUG(10, ("tdb_validate: validating child returned.\n"));
824         if (WIFEXITED(child_status)) {
825                 DEBUG(10, ("tdb_validate: child exited, code %d.\n",
826                            WEXITSTATUS(child_status)));
827                 ret = WEXITSTATUS(child_status);
828         }
829         if (WIFSIGNALED(child_status)) {
830                 DEBUG(10, ("tdb_validate: child terminated by signal %d\n",
831                            WTERMSIG(child_status)));
832 #ifdef WCOREDUMP
833                 if (WCOREDUMP(child_status)) {
834                         DEBUGADD(10, ("core dumped\n"));
835                 }
836 #endif
837                 ret = WTERMSIG(child_status);
838         }
839         if (WIFSTOPPED(child_status)) {
840                 DEBUG(10, ("tdb_validate: child was stopped by signal %d\n",
841                            WSTOPSIG(child_status)));
842                 ret = WSTOPSIG(child_status);
843         }
844
845 done:
846         DEBUG(5, ("tdb_validate returning code '%d' for tdb '%s'\n", ret,
847                   tdb_name(tdb)));
848
849         return ret;
850 }
851
852 /*
853  * tdb validation function.
854  * returns 0 if tdb is ok, != 0 if it isn't.
855  * this is a wrapper around the actual validation function that opens and closes
856  * the tdb.
857  */
858 int tdb_validate_open(const char *tdb_path, tdb_validate_data_func validate_fn)
859 {
860         TDB_CONTEXT *tdb = NULL;
861         int ret = 1;
862
863         DEBUG(5, ("tdb_validate_open called for tdb '%s'\n", tdb_path));
864
865         tdb = tdb_open_log(tdb_path, 0, TDB_DEFAULT, O_RDONLY, 0);
866         if (!tdb) {
867                 DEBUG(1, ("Error opening tdb %s\n", tdb_path));
868                 return ret;
869         }
870
871         ret = tdb_validate(tdb, validate_fn);
872         tdb_close(tdb);
873         return ret;
874 }
875
876 /*
877  * tdb backup function and helpers for tdb_validate wrapper with backup
878  * handling.
879  */
880
881 /* this structure eliminates the need for a global overall status for
882  * the traverse-copy */
883 struct tdb_copy_data {
884         struct tdb_context *dst;
885         bool success;
886 };
887
888 static int traverse_copy_fn(struct tdb_context *tdb, TDB_DATA key,
889                             TDB_DATA dbuf, void *private_data)
890 {
891         struct tdb_copy_data *data = (struct tdb_copy_data *)private_data;
892
893         if (tdb_store(data->dst, key, dbuf, TDB_INSERT) != 0) {
894                 DEBUG(4, ("Failed to insert into %s: %s\n", tdb_name(data->dst),
895                           strerror(errno)));
896                 data->success = False;
897                 return 1;
898         }
899         return 0;
900 }
901
902 static int tdb_copy(struct tdb_context *src, struct tdb_context *dst)
903 {
904         struct tdb_copy_data data;
905         int count;
906
907         data.dst = dst;
908         data.success = True;
909
910         count = tdb_traverse(src, traverse_copy_fn, (void *)(&data));
911         if ((count < 0) || (data.success == False)) {
912                 return -1;
913         }
914         return count;
915 }
916
917 static int tdb_verify_basic(struct tdb_context *tdb)
918 {
919         return tdb_traverse(tdb, NULL, NULL);
920 }
921
922 /* this backup function is essentially taken from lib/tdb/tools/tdbbackup.tdb
923  */
924 static int tdb_backup(TALLOC_CTX *ctx, const char *src_path,
925                       const char *dst_path, int hash_size)
926 {
927         struct tdb_context *src_tdb = NULL;
928         struct tdb_context *dst_tdb = NULL;
929         char *tmp_path = NULL;
930         struct stat st;
931         int count1, count2;
932         int saved_errno = 0;
933         int ret = -1;
934
935         if (stat(src_path, &st) != 0) {
936                 DEBUG(3, ("Could not stat '%s': %s\n", src_path,
937                           strerror(errno)));
938                 goto done;
939         }
940
941         /* open old tdb RDWR - so we can lock it */
942         src_tdb = tdb_open_log(src_path, 0, TDB_DEFAULT, O_RDWR, 0);
943         if (src_tdb == NULL) {
944                 DEBUG(3, ("Failed to open tdb '%s'\n", src_path));
945                 goto done;
946         }
947
948         if (tdb_lockall(src_tdb) != 0) {
949                 DEBUG(3, ("Failed to lock tdb '%s'\n", src_path));
950                 goto done;
951         }
952
953         tmp_path = talloc_asprintf(ctx, "%s%s", dst_path, ".tmp");
954         unlink(tmp_path);
955         dst_tdb = tdb_open_log(tmp_path,
956                                hash_size ? hash_size : tdb_hash_size(src_tdb),
957                                TDB_DEFAULT, O_RDWR | O_CREAT | O_EXCL,
958                                st.st_mode & 0777);
959         if (dst_tdb == NULL) {
960                 DEBUG(3, ("Error creating tdb '%s': %s\n", tmp_path,
961                           strerror(errno)));
962                 saved_errno = errno;
963                 unlink(tmp_path);
964                 goto done;
965         }
966
967         count1 = tdb_copy(src_tdb, dst_tdb);
968         if (count1 < 0) {
969                 DEBUG(3, ("Failed to copy tdb '%s': %s\n", src_path,
970                           strerror(errno)));
971                 tdb_close(dst_tdb);
972                 goto done;
973         }
974
975         /* reopen ro and do basic verification */
976         tdb_close(dst_tdb);
977         dst_tdb = tdb_open_log(tmp_path, 0, TDB_DEFAULT, O_RDONLY, 0);
978         if (!dst_tdb) {
979                 DEBUG(3, ("Failed to reopen tdb '%s': %s\n", tmp_path,
980                           strerror(errno)));
981                 goto done;
982         }
983         count2 = tdb_verify_basic(dst_tdb);
984         if (count2 != count1) {
985                 DEBUG(3, ("Failed to verify result of copying tdb '%s'.\n",
986                           src_path));
987                 tdb_close(dst_tdb);
988                 goto done;
989         }
990
991         DEBUG(10, ("tdb_backup: successfully copied %d entries\n", count1));
992
993         /* make sure the new tdb has reached stable storage
994          * then rename it to its destination */
995         fsync(tdb_fd(dst_tdb));
996         tdb_close(dst_tdb);
997         unlink(dst_path);
998         if (rename(tmp_path, dst_path) != 0) {
999                 DEBUG(3, ("Failed to rename '%s' to '%s': %s\n",
1000                           tmp_path, dst_path, strerror(errno)));
1001                 goto done;
1002         }
1003
1004         /* success */
1005         ret = 0;
1006
1007 done:
1008         if (src_tdb != NULL) {
1009                 tdb_close(src_tdb);
1010         }
1011         if (tmp_path != NULL) {
1012                 unlink(tmp_path);
1013                 TALLOC_FREE(tmp_path);
1014         }
1015         if (saved_errno != 0) {
1016                 errno = saved_errno;
1017         }
1018         return ret;
1019 }
1020
1021 static int rename_file_with_suffix(TALLOC_CTX *ctx, const char *path,
1022                                    const char *suffix)
1023 {
1024         int ret = -1;
1025         char *dst_path;
1026
1027         dst_path = talloc_asprintf(ctx, "%s%s", path, suffix);
1028
1029         ret = (rename(path, dst_path) != 0);
1030
1031         if (ret == 0) {
1032                 DEBUG(5, ("moved '%s' to '%s'\n", path, dst_path));
1033         } else if (errno == ENOENT) {
1034                 DEBUG(3, ("file '%s' does not exist - so not moved\n", path));
1035                 ret = 0;
1036         } else {
1037                 DEBUG(3, ("error renaming %s to %s: %s\n", path, dst_path,
1038                           strerror(errno)));
1039         }
1040
1041         TALLOC_FREE(dst_path);
1042         return ret;
1043 }
1044
1045 /*
1046  * do a backup of a tdb, moving the destination out of the way first
1047  */
1048 static int tdb_backup_with_rotate(TALLOC_CTX *ctx, const char *src_path,
1049                                   const char *dst_path, int hash_size,
1050                                   const char *rotate_suffix,
1051                                   bool retry_norotate_if_nospc,
1052                                   bool rename_as_last_resort_if_nospc)
1053 {
1054         int ret;
1055
1056         rename_file_with_suffix(ctx, dst_path, rotate_suffix);
1057
1058         ret = tdb_backup(ctx, src_path, dst_path, hash_size);
1059
1060         if (ret != 0) {
1061                 DEBUG(10, ("backup of %s failed: %s\n", src_path, strerror(errno)));
1062         }
1063         if ((ret != 0) && (errno == ENOSPC) && retry_norotate_if_nospc)
1064         {
1065                 char *rotate_path = talloc_asprintf(ctx, "%s%s", dst_path,
1066                                                     rotate_suffix);
1067                 DEBUG(10, ("backup of %s failed due to lack of space\n",
1068                            src_path));
1069                 DEBUGADD(10, ("trying to free some space by removing rotated "
1070                               "dst %s\n", rotate_path));
1071                 if (unlink(rotate_path) == -1) {
1072                         DEBUG(10, ("unlink of %s failed: %s\n", rotate_path,
1073                                    strerror(errno)));
1074                 } else {
1075                         ret = tdb_backup(ctx, src_path, dst_path, hash_size);
1076                 }
1077                 TALLOC_FREE(rotate_path);
1078         }
1079
1080         if ((ret != 0) && (errno == ENOSPC) && rename_as_last_resort_if_nospc)
1081         {
1082                 DEBUG(10, ("backup of %s failed due to lack of space\n", 
1083                            src_path));
1084                 DEBUGADD(10, ("using 'rename' as a last resort\n"));
1085                 ret = rename(src_path, dst_path);
1086         }
1087
1088         return ret;
1089 }
1090
1091 /*
1092  * validation function with backup handling:
1093  *
1094  *  - calls tdb_validate
1095  *  - if the tdb is ok, create a backup "name.bak", possibly moving
1096  *    existing backup to name.bak.old,
1097  *    return 0 (success) even if the backup fails
1098  *  - if the tdb is corrupt:
1099  *    - move the tdb to "name.corrupt"
1100  *    - check if there is valid backup.
1101  *      if so, restore the backup.
1102  *      if restore is successful, return 0 (success),
1103  *    - otherwise return -1 (failure)
1104  */
1105 int tdb_validate_and_backup(const char *tdb_path,
1106                             tdb_validate_data_func validate_fn)
1107 {
1108         int ret = -1;
1109         const char *backup_suffix = ".bak";
1110         const char *corrupt_suffix = ".corrupt";
1111         const char *rotate_suffix = ".old";
1112         char *tdb_path_backup;
1113         struct stat st;
1114         TALLOC_CTX *ctx = NULL;
1115
1116         ctx = talloc_new(NULL);
1117         if (ctx == NULL) {
1118                 DEBUG(0, ("tdb_validate_and_backup: out of memory\n"));
1119                 goto done;
1120         }
1121
1122         tdb_path_backup = talloc_asprintf(ctx, "%s%s", tdb_path, backup_suffix);
1123
1124         ret = tdb_validate_open(tdb_path, validate_fn);
1125
1126         if (ret == 0) {
1127                 DEBUG(1, ("tdb '%s' is valid\n", tdb_path));
1128                 ret = tdb_backup_with_rotate(ctx, tdb_path, tdb_path_backup, 0,
1129                                              rotate_suffix, True, False);
1130                 if (ret != 0) {
1131                         DEBUG(1, ("Error creating backup of tdb '%s'\n",
1132                                   tdb_path));
1133                         /* the actual validation was successful: */
1134                         ret = 0;
1135                 } else {
1136                         DEBUG(1, ("Created backup '%s' of tdb '%s'\n",
1137                                   tdb_path_backup, tdb_path));
1138                 }
1139         } else {
1140                 DEBUG(1, ("tdb '%s' is invalid\n", tdb_path));
1141
1142                 ret =stat(tdb_path_backup, &st);
1143                 if (ret != 0) {
1144                         DEBUG(5, ("Could not stat '%s': %s\n", tdb_path_backup,
1145                                   strerror(errno)));
1146                         DEBUG(1, ("No backup found.\n"));
1147                 } else {
1148                         DEBUG(1, ("backup '%s' found.\n", tdb_path_backup));
1149                         ret = tdb_validate_open(tdb_path_backup, validate_fn);
1150                         if (ret != 0) {
1151                                 DEBUG(1, ("Backup '%s' is invalid.\n",
1152                                           tdb_path_backup));
1153                         }
1154                 }
1155
1156                 if (ret != 0) {
1157                         int renamed = rename_file_with_suffix(ctx, tdb_path,
1158                                                               corrupt_suffix);
1159                         if (renamed != 0) {
1160                                 DEBUG(1, ("Error moving tdb to '%s%s'\n",
1161                                           tdb_path, corrupt_suffix));
1162                         } else {
1163                                 DEBUG(1, ("Corrupt tdb stored as '%s%s'\n",
1164                                           tdb_path, corrupt_suffix));
1165                         }
1166                         goto done;
1167                 }
1168
1169                 DEBUG(1, ("valid backup '%s' found\n", tdb_path_backup));
1170                 ret = tdb_backup_with_rotate(ctx, tdb_path_backup, tdb_path, 0,
1171                                              corrupt_suffix, True, True);
1172                 if (ret != 0) {
1173                         DEBUG(1, ("Error restoring backup from '%s'\n",
1174                                   tdb_path_backup));
1175                 } else {
1176                         DEBUG(1, ("Restored tdb backup from '%s'\n",
1177                                   tdb_path_backup));
1178                 }
1179         }
1180
1181 done:
1182         TALLOC_FREE(ctx);
1183         return ret;
1184 }