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