BIG patch...
[tprouty/samba.git] / source / tdb / tdbutil.c
1 /* 
2    Unix SMB/CIFS implementation.
3    tdb utility functions
4    Copyright (C) Andrew Tridgell 1992-1998
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22 #include <fnmatch.h>
23
24 /* these are little tdb utility functions that are meant to make
25    dealing with a tdb database a little less cumbersome in Samba */
26
27 static SIG_ATOMIC_T gotalarm;
28
29 /***************************************************************
30  Signal function to tell us we timed out.
31 ****************************************************************/
32
33 static void gotalarm_sig(void)
34 {
35         gotalarm = 1;
36 }
37
38 /****************************************************************************
39  Lock a chain with timeout (in seconds).
40 ****************************************************************************/
41
42 static int tdb_chainlock_with_timeout( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout, int rw_type)
43 {
44         /* Allow tdb_chainlock to be interrupted by an alarm. */
45         int ret;
46         gotalarm = 0;
47         tdb_set_lock_alarm(&gotalarm);
48
49         if (timeout) {
50                 CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
51                 alarm(timeout);
52         }
53
54         if (rw_type == F_RDLCK)
55                 ret = tdb_chainlock_read(tdb, key);
56         else
57                 ret = tdb_chainlock(tdb, key);
58
59         if (timeout) {
60                 alarm(0);
61                 CatchSignal(SIGALRM, SIGNAL_CAST SIG_IGN);
62                 if (gotalarm) {
63                         DEBUG(0,("tdb_chainlock_with_timeout: alarm (%u) timed out for key %s in tdb %s\n",
64                                 timeout, key.dptr, tdb->name ));
65                         return -1;
66                 }
67         }
68
69         return ret;
70 }
71
72 /****************************************************************************
73  Lock a chain by string. Return -1 if timeout or lock failed.
74 ****************************************************************************/
75
76 int tdb_lock_bystring(TDB_CONTEXT *tdb, const char *keyval, unsigned int timeout)
77 {
78         TDB_DATA key;
79
80         key.dptr = keyval;
81         key.dsize = strlen(keyval)+1;
82         
83         return tdb_chainlock_with_timeout(tdb, key, timeout, F_WRLCK);
84 }
85
86 /****************************************************************************
87  Unlock a chain by string.
88 ****************************************************************************/
89
90 void tdb_unlock_bystring(TDB_CONTEXT *tdb, const char *keyval)
91 {
92         TDB_DATA key;
93
94         key.dptr = keyval;
95         key.dsize = strlen(keyval)+1;
96         
97         tdb_chainunlock(tdb, key);
98 }
99
100 /****************************************************************************
101  Read lock a chain by string. Return -1 if timeout or lock failed.
102 ****************************************************************************/
103
104 int tdb_read_lock_bystring(TDB_CONTEXT *tdb, const char *keyval, unsigned int timeout)
105 {
106         TDB_DATA key;
107
108         key.dptr = keyval;
109         key.dsize = strlen(keyval)+1;
110         
111         return tdb_chainlock_with_timeout(tdb, key, timeout, F_RDLCK);
112 }
113
114 /****************************************************************************
115  Read unlock a chain by string.
116 ****************************************************************************/
117
118 void tdb_read_unlock_bystring(TDB_CONTEXT *tdb, const char *keyval)
119 {
120         TDB_DATA key;
121
122         key.dptr = keyval;
123         key.dsize = strlen(keyval)+1;
124         
125         tdb_chainunlock_read(tdb, key);
126 }
127
128
129 /****************************************************************************
130  Fetch a int32 value by a arbitrary blob key, return -1 if not found.
131  Output is int32 in native byte order.
132 ****************************************************************************/
133
134 int32 tdb_fetch_int32_byblob(TDB_CONTEXT *tdb, const char *keyval, size_t len)
135 {
136         TDB_DATA key, data;
137         int32 ret;
138
139         key.dptr = keyval;
140         key.dsize = len;
141         data = tdb_fetch(tdb, key);
142         if (!data.dptr || data.dsize != sizeof(int32)) {
143                 SAFE_FREE(data.dptr);
144                 return -1;
145         }
146
147         ret = IVAL(data.dptr,0);
148         SAFE_FREE(data.dptr);
149         return ret;
150 }
151
152 /****************************************************************************
153  Fetch a int32 value by string key, return -1 if not found.
154  Output is int32 in native byte order.
155 ****************************************************************************/
156
157 int32 tdb_fetch_int32(TDB_CONTEXT *tdb, const char *keystr)
158 {
159         return tdb_fetch_int32_byblob(tdb, keystr, strlen(keystr) + 1);
160 }
161
162 /****************************************************************************
163  Store a int32 value by an arbitary blob key, return 0 on success, -1 on failure.
164  Input is int32 in native byte order. Output in tdb is in little-endian.
165 ****************************************************************************/
166
167 int tdb_store_int32_byblob(TDB_CONTEXT *tdb, const char *keystr, size_t len, int32 v)
168 {
169         TDB_DATA key, data;
170         int32 v_store;
171
172         key.dptr = keystr;
173         key.dsize = len;
174         SIVAL(&v_store,0,v);
175         data.dptr = (void *)&v_store;
176         data.dsize = sizeof(int32);
177
178         return tdb_store(tdb, key, data, TDB_REPLACE);
179 }
180
181 /****************************************************************************
182  Store a int32 value by string key, return 0 on success, -1 on failure.
183  Input is int32 in native byte order. Output in tdb is in little-endian.
184 ****************************************************************************/
185
186 int tdb_store_int32(TDB_CONTEXT *tdb, const char *keystr, int32 v)
187 {
188         return tdb_store_int32_byblob(tdb, keystr, strlen(keystr) + 1, v);
189 }
190
191 /****************************************************************************
192  Fetch a uint32 value by a arbitrary blob key, return -1 if not found.
193  Output is uint32 in native byte order.
194 ****************************************************************************/
195
196 BOOL tdb_fetch_uint32_byblob(TDB_CONTEXT *tdb, const char *keyval, size_t len, uint32 *value)
197 {
198         TDB_DATA key, data;
199
200         key.dptr = keyval;
201         key.dsize = len;
202         data = tdb_fetch(tdb, key);
203         if (!data.dptr || data.dsize != sizeof(uint32)) {
204                 SAFE_FREE(data.dptr);
205                 return False;
206         }
207
208         *value = IVAL(data.dptr,0);
209         SAFE_FREE(data.dptr);
210         return True;
211 }
212
213 /****************************************************************************
214  Fetch a uint32 value by string key, return -1 if not found.
215  Output is uint32 in native byte order.
216 ****************************************************************************/
217
218 BOOL tdb_fetch_uint32(TDB_CONTEXT *tdb, const char *keystr, uint32 *value)
219 {
220         return tdb_fetch_uint32_byblob(tdb, keystr, strlen(keystr) + 1, value);
221 }
222
223 /****************************************************************************
224  Store a uint32 value by an arbitary blob key, return 0 on success, -1 on failure.
225  Input is uint32 in native byte order. Output in tdb is in little-endian.
226 ****************************************************************************/
227
228 BOOL tdb_store_uint32_byblob(TDB_CONTEXT *tdb, const char *keystr, size_t len, uint32 value)
229 {
230         TDB_DATA key, data;
231         uint32 v_store;
232         BOOL ret = True;
233
234         key.dptr = keystr;
235         key.dsize = len;
236         SIVAL(&v_store, 0, value);
237         data.dptr = (void *)&v_store;
238         data.dsize = sizeof(uint32);
239
240         if (tdb_store(tdb, key, data, TDB_REPLACE) == -1)
241                 ret = False;
242
243         return ret;
244 }
245
246 /****************************************************************************
247  Store a uint32 value by string key, return 0 on success, -1 on failure.
248  Input is uint32 in native byte order. Output in tdb is in little-endian.
249 ****************************************************************************/
250
251 BOOL tdb_store_uint32(TDB_CONTEXT *tdb, const char *keystr, uint32 value)
252 {
253         return tdb_store_uint32_byblob(tdb, keystr, strlen(keystr) + 1, value);
254 }
255 /****************************************************************************
256  Store a buffer by a null terminated string key.  Return 0 on success, -1
257  on failure.
258 ****************************************************************************/
259
260 int tdb_store_by_string(TDB_CONTEXT *tdb, const char *keystr, TDB_DATA data, int flags)
261 {
262     TDB_DATA key;
263
264     key.dptr = keystr;
265     key.dsize = strlen(keystr) + 1;
266
267     return tdb_store(tdb, key, data, flags);
268 }
269
270 /****************************************************************************
271  Fetch a buffer using a null terminated string key.  Don't forget to call
272  free() on the result dptr.
273 ****************************************************************************/
274
275 TDB_DATA tdb_fetch_by_string(TDB_CONTEXT *tdb, const char *keystr)
276 {
277     TDB_DATA key;
278
279     key.dptr = keystr;
280     key.dsize = strlen(keystr) + 1;
281
282     return tdb_fetch(tdb, key);
283 }
284
285 /****************************************************************************
286  Delete an entry using a null terminated string key. 
287 ****************************************************************************/
288
289 int tdb_delete_by_string(TDB_CONTEXT *tdb, const char *keystr)
290 {
291     TDB_DATA key;
292
293     key.dptr = keystr;
294     key.dsize = strlen(keystr) + 1;
295
296     return tdb_delete(tdb, key);
297 }
298
299 /****************************************************************************
300  Atomic integer change. Returns old value. To create, set initial value in *oldval. 
301 ****************************************************************************/
302
303 int32 tdb_change_int32_atomic(TDB_CONTEXT *tdb, const char *keystr, int32 *oldval, int32 change_val)
304 {
305         int32 val;
306         int32 ret = -1;
307
308         if (tdb_lock_bystring(tdb, keystr,0) == -1)
309                 return -1;
310
311         if ((val = tdb_fetch_int32(tdb, keystr)) == -1) {
312                 /* The lookup failed */
313                 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
314                         /* but not becouse it didn't exist */
315                         goto err_out;
316                 }
317                 
318                 /* Start with 'old' value */
319                 val = *oldval;
320
321         } else {
322                 /* It worked, set return value (oldval) to tdb data */
323                 *oldval = val;
324         }
325
326         /* Increment value for storage and return next time */
327         val += change_val;
328                 
329         if (tdb_store_int32(tdb, keystr, val) == -1)
330                 goto err_out;
331
332         ret = 0;
333
334   err_out:
335
336         tdb_unlock_bystring(tdb, keystr);
337         return ret;
338 }
339
340 /****************************************************************************
341  Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval. 
342 ****************************************************************************/
343
344 BOOL tdb_change_uint32_atomic(TDB_CONTEXT *tdb, const char *keystr, uint32 *oldval, uint32 change_val)
345 {
346         uint32 val;
347         BOOL ret = False;
348
349         if (tdb_lock_bystring(tdb, keystr,0) == -1)
350                 return False;
351
352         if (!tdb_fetch_uint32(tdb, keystr, &val)) {
353                 /* It failed */
354                 if (tdb_error(tdb) != TDB_ERR_NOEXIST) { 
355                         /* and not becouse it didn't exist */
356                         goto err_out;
357                 }
358
359                 /* Start with 'old' value */
360                 val = *oldval;
361
362         } else {
363                 /* it worked, set return value (oldval) to tdb data */
364                 *oldval = val;
365
366         }
367
368         /* get a new value to store */
369         val += change_val;
370                 
371         if (!tdb_store_uint32(tdb, keystr, val))
372                 goto err_out;
373
374         ret = True;
375
376   err_out:
377
378         tdb_unlock_bystring(tdb, keystr);
379         return ret;
380 }
381
382 /****************************************************************************
383  Useful pair of routines for packing/unpacking data consisting of
384  integers and strings.
385 ****************************************************************************/
386
387 size_t tdb_pack(char *buf, int bufsize, const char *fmt, ...)
388 {
389         va_list ap;
390         uint16 w;
391         uint32 d;
392         int i;
393         void *p;
394         int len;
395         char *s;
396         char c;
397         char *buf0 = buf;
398         const char *fmt0 = fmt;
399         int bufsize0 = bufsize;
400
401         va_start(ap, fmt);
402
403         while (*fmt) {
404                 switch ((c = *fmt++)) {
405                 case 'w':
406                         len = 2;
407                         w = (uint16)va_arg(ap, int);
408                         if (bufsize >= len)
409                                 SSVAL(buf, 0, w);
410                         break;
411                 case 'd':
412                         len = 4;
413                         d = va_arg(ap, uint32);
414                         if (bufsize >= len)
415                                 SIVAL(buf, 0, d);
416                         break;
417                 case 'p':
418                         len = 4;
419                         p = va_arg(ap, void *);
420                         d = p?1:0;
421                         if (bufsize >= len)
422                                 SIVAL(buf, 0, d);
423                         break;
424                 case 'P':
425                         s = va_arg(ap,char *);
426                         w = strlen(s);
427                         len = w + 1;
428                         if (bufsize >= len)
429                                 memcpy(buf, s, len);
430                         break;
431                 case 'f':
432                         s = va_arg(ap,char *);
433                         w = strlen(s);
434                         len = w + 1;
435                         if (bufsize >= len)
436                                 memcpy(buf, s, len);
437                         break;
438                 case 'B':
439                         i = va_arg(ap, int);
440                         s = va_arg(ap, char *);
441                         len = 4+i;
442                         if (bufsize >= len) {
443                                 SIVAL(buf, 0, i);
444                                 memcpy(buf+4, s, i);
445                         }
446                         break;
447                 default:
448                         DEBUG(0,("Unknown tdb_pack format %c in %s\n", 
449                                  c, fmt));
450                         len = 0;
451                         break;
452                 }
453
454                 buf += len;
455                 bufsize -= len;
456         }
457
458         va_end(ap);
459
460         DEBUG(18,("tdb_pack(%s, %d) -> %d\n", 
461                  fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
462         
463         return PTR_DIFF(buf, buf0);
464 }
465
466 /****************************************************************************
467  Useful pair of routines for packing/unpacking data consisting of
468  integers and strings.
469 ****************************************************************************/
470
471 int tdb_unpack(char *buf, int bufsize, const char *fmt, ...)
472 {
473         va_list ap;
474         uint16 *w;
475         uint32 *d;
476         int len;
477         int *i;
478         void **p;
479         char *s, **b;
480         char c;
481         char *buf0 = buf;
482         const char *fmt0 = fmt;
483         int bufsize0 = bufsize;
484
485         va_start(ap, fmt);
486         
487         while (*fmt) {
488                 switch ((c=*fmt++)) {
489                 case 'w':
490                         len = 2;
491                         w = va_arg(ap, uint16 *);
492                         if (bufsize < len)
493                                 goto no_space;
494                         *w = SVAL(buf, 0);
495                         break;
496                 case 'd':
497                         len = 4;
498                         d = va_arg(ap, uint32 *);
499                         if (bufsize < len)
500                                 goto no_space;
501                         *d = IVAL(buf, 0);
502                         break;
503                 case 'p':
504                         len = 4;
505                         p = va_arg(ap, void **);
506                         if (bufsize < len)
507                                 goto no_space;
508                         *p = (void *)IVAL(buf, 0);
509                         break;
510                 case 'P':
511                         s = va_arg(ap,char *);
512                         len = strlen(buf) + 1;
513                         if (bufsize < len || len > sizeof(pstring))
514                                 goto no_space;
515                         memcpy(s, buf, len);
516                         break;
517                 case 'f':
518                         s = va_arg(ap,char *);
519                         len = strlen(buf) + 1;
520                         if (bufsize < len || len > sizeof(fstring))
521                                 goto no_space;
522                         memcpy(s, buf, len);
523                         break;
524                 case 'B':
525                         i = va_arg(ap, int *);
526                         b = va_arg(ap, char **);
527                         len = 4;
528                         if (bufsize < len)
529                                 goto no_space;
530                         *i = IVAL(buf, 0);
531                         if (! *i) {
532                                 *b = NULL;
533                                 break;
534                         }
535                         len += *i;
536                         if (bufsize < len)
537                                 goto no_space;
538                         *b = (char *)malloc(*i);
539                         if (! *b)
540                                 goto no_space;
541                         memcpy(*b, buf+4, *i);
542                         break;
543                 default:
544                         DEBUG(0,("Unknown tdb_unpack format %c in %s\n", 
545                                  c, fmt));
546
547                         len = 0;
548                         break;
549                 }
550
551                 buf += len;
552                 bufsize -= len;
553         }
554
555         va_end(ap);
556
557         DEBUG(18,("tdb_unpack(%s, %d) -> %d\n", 
558                  fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
559
560         return PTR_DIFF(buf, buf0);
561
562  no_space:
563         return -1;
564 }
565
566 /****************************************************************************
567  Log tdb messages via DEBUG().
568 ****************************************************************************/
569
570 static void tdb_log(TDB_CONTEXT *tdb, int level, const char *format, ...)
571 {
572         va_list ap;
573         char *ptr = NULL;
574
575         va_start(ap, format);
576         vasprintf(&ptr, format, ap);
577         va_end(ap);
578         
579         if (!ptr || !*ptr)
580                 return;
581
582         DEBUG(level, ("tdb(%s): %s", tdb->name ? tdb->name : "unnamed", ptr));
583         SAFE_FREE(ptr);
584 }
585
586 /****************************************************************************
587  Like tdb_open() but also setup a logging function that redirects to
588  the samba DEBUG() system.
589 ****************************************************************************/
590
591 TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
592                           int open_flags, mode_t mode)
593 {
594         TDB_CONTEXT *tdb;
595
596         if (!lp_use_mmap())
597                 tdb_flags |= TDB_NOMMAP;
598
599         tdb = tdb_open_ex(name, hash_size, tdb_flags, 
600                                     open_flags, mode, tdb_log);
601         if (!tdb)
602                 return NULL;
603
604         return tdb;
605 }
606
607
608 /****************************************************************************
609  Allow tdb_delete to be used as a tdb_traversal_fn.
610 ****************************************************************************/
611
612 int tdb_traverse_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
613                      void *state)
614 {
615     return tdb_delete(the_tdb, key);
616 }
617
618
619
620 /**
621  * Search across the whole tdb for keys that match the given pattern
622  * return the result as a list of keys
623  *
624  * @param tdb pointer to opened tdb file context
625  * @param pattern searching pattern used by fnmatch(3) functions
626  *
627  * @return list of keys found by looking up with given pattern
628  **/
629 TDB_LIST_NODE *tdb_search_keys(TDB_CONTEXT *tdb, const char* pattern)
630 {
631         TDB_DATA key, next;
632         TDB_LIST_NODE *list = NULL;
633         TDB_LIST_NODE *rec = NULL;
634         TDB_LIST_NODE *tmp = NULL;
635         
636         for (key = tdb_firstkey(tdb); key.dptr; key = next) {
637                 /* duplicate key string to ensure null-termination */
638                 char *key_str = (char*) strndup(key.dptr, key.dsize);
639                 if (!key_str) {
640                         DEBUG(0, ("tdb_search_keys: strndup() failed!\n"));
641                         smb_panic("strndup failed!\n");
642                 }
643                 
644                 DEBUG(18, ("checking %s for match to pattern %s\n", key_str, pattern));
645                 
646                 next = tdb_nextkey(tdb, key);
647
648                 /* do the pattern checking */
649                 if (fnmatch(pattern, key_str, 0) == 0) {
650                         rec = (TDB_LIST_NODE*) malloc(sizeof(*rec));
651                         ZERO_STRUCTP(rec);
652
653                         rec->node_key = key;
654         
655                         DLIST_ADD_END(list, rec, tmp);
656                 
657                         DEBUG(18, ("checking %s matched pattern %s\n", key_str, pattern));
658                 } else {
659                         free(key.dptr);
660                 }
661                 
662                 /* free duplicated key string */
663                 free(key_str);
664         }
665         
666         return list;
667
668 };
669
670
671 /**
672  * Free the list returned by tdb_search_keys
673  *
674  * @param node list of results found by tdb_search_keys
675  **/
676 void tdb_search_list_free(TDB_LIST_NODE* node)
677 {
678         TDB_LIST_NODE *next_node;
679         
680         while (node) {
681                 next_node = node->next;
682                 SAFE_FREE(node);
683                 node = next_node;
684         };
685 };
686
687