r6149: Fixes bugs #2498 and 2484.
[vlendec/samba-autobuild/.git] / source3 / tdb / tdbutil.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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include <fnmatch.h>
24
25 /* these are little tdb utility functions that are meant to make
26    dealing with a tdb database a little less cumbersome in Samba */
27
28 static SIG_ATOMIC_T gotalarm;
29
30 /***************************************************************
31  Signal function to tell us we timed out.
32 ****************************************************************/
33
34 static void gotalarm_sig(void)
35 {
36         gotalarm = 1;
37 }
38
39 /***************************************************************
40  Make a TDB_DATA and keep the const warning in one place
41 ****************************************************************/
42
43 TDB_DATA make_tdb_data(const char *dptr, size_t dsize)
44 {
45         TDB_DATA ret;
46         ret.dptr = CONST_DISCARD(char *, dptr);
47         ret.dsize = dsize;
48         return ret;
49 }
50
51 TDB_DATA string_tdb_data(const char *string)
52 {
53         return make_tdb_data(string, strlen(string));
54 }
55
56 /****************************************************************************
57  Lock a chain with timeout (in seconds).
58 ****************************************************************************/
59
60 static int tdb_chainlock_with_timeout_internal( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout, int rw_type)
61 {
62         /* Allow tdb_chainlock to be interrupted by an alarm. */
63         int ret;
64         gotalarm = 0;
65         tdb_set_lock_alarm(CONST_DISCARD(sig_atomic_t *, &gotalarm));
66
67         if (timeout) {
68                 CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
69                 alarm(timeout);
70         }
71
72         if (rw_type == F_RDLCK)
73                 ret = tdb_chainlock_read(tdb, key);
74         else
75                 ret = tdb_chainlock(tdb, key);
76
77         if (timeout) {
78                 alarm(0);
79                 CatchSignal(SIGALRM, SIGNAL_CAST SIG_IGN);
80                 if (gotalarm) {
81                         DEBUG(0,("tdb_chainlock_with_timeout_internal: alarm (%u) timed out for key %s in tdb %s\n",
82                                 timeout, key.dptr, tdb->name ));
83                         /* TODO: If we time out waiting for a lock, it might
84                          * be nice to use F_GETLK to get the pid of the
85                          * process currently holding the lock and print that
86                          * as part of the debugging message. -- mbp */
87                         return -1;
88                 }
89         }
90
91         return ret;
92 }
93
94 /****************************************************************************
95  Write lock a chain. Return -1 if timeout or lock failed.
96 ****************************************************************************/
97
98 int tdb_chainlock_with_timeout( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout)
99 {
100         return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_WRLCK);
101 }
102
103 /****************************************************************************
104  Lock a chain by string. Return -1 if timeout or lock failed.
105 ****************************************************************************/
106
107 int tdb_lock_bystring(TDB_CONTEXT *tdb, const char *keyval, unsigned int timeout)
108 {
109         TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
110         
111         return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_WRLCK);
112 }
113
114 /****************************************************************************
115  Unlock a chain by string.
116 ****************************************************************************/
117
118 void tdb_unlock_bystring(TDB_CONTEXT *tdb, const char *keyval)
119 {
120         TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
121
122         tdb_chainunlock(tdb, key);
123 }
124
125 /****************************************************************************
126  Read lock a chain by string. Return -1 if timeout or lock failed.
127 ****************************************************************************/
128
129 int tdb_read_lock_bystring(TDB_CONTEXT *tdb, const char *keyval, unsigned int timeout)
130 {
131         TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
132         
133         return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_RDLCK);
134 }
135
136 /****************************************************************************
137  Read unlock a chain by string.
138 ****************************************************************************/
139
140 void tdb_read_unlock_bystring(TDB_CONTEXT *tdb, const char *keyval)
141 {
142         TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
143         
144         tdb_chainunlock_read(tdb, key);
145 }
146
147
148 /****************************************************************************
149  Fetch a int32 value by a arbitrary blob key, return -1 if not found.
150  Output is int32 in native byte order.
151 ****************************************************************************/
152
153 int32 tdb_fetch_int32_byblob(TDB_CONTEXT *tdb, const char *keyval, size_t len)
154 {
155         TDB_DATA key = make_tdb_data(keyval, len);
156         TDB_DATA data;
157         int32 ret;
158
159         data = tdb_fetch(tdb, key);
160         if (!data.dptr || data.dsize != sizeof(int32)) {
161                 SAFE_FREE(data.dptr);
162                 return -1;
163         }
164
165         ret = IVAL(data.dptr,0);
166         SAFE_FREE(data.dptr);
167         return ret;
168 }
169
170 /****************************************************************************
171  Fetch a int32 value by string key, return -1 if not found.
172  Output is int32 in native byte order.
173 ****************************************************************************/
174
175 int32 tdb_fetch_int32(TDB_CONTEXT *tdb, const char *keystr)
176 {
177         return tdb_fetch_int32_byblob(tdb, keystr, strlen(keystr) + 1);
178 }
179
180 /****************************************************************************
181  Store a int32 value by an arbitary blob key, return 0 on success, -1 on failure.
182  Input is int32 in native byte order. Output in tdb is in little-endian.
183 ****************************************************************************/
184
185 int tdb_store_int32_byblob(TDB_CONTEXT *tdb, const char *keystr, size_t len, int32 v)
186 {
187         TDB_DATA key = make_tdb_data(keystr, len);
188         TDB_DATA data;
189         int32 v_store;
190
191         SIVAL(&v_store,0,v);
192         data.dptr = (void *)&v_store;
193         data.dsize = sizeof(int32);
194
195         return tdb_store(tdb, key, data, TDB_REPLACE);
196 }
197
198 /****************************************************************************
199  Store a int32 value by string key, return 0 on success, -1 on failure.
200  Input is int32 in native byte order. Output in tdb is in little-endian.
201 ****************************************************************************/
202
203 int tdb_store_int32(TDB_CONTEXT *tdb, const char *keystr, int32 v)
204 {
205         return tdb_store_int32_byblob(tdb, keystr, strlen(keystr) + 1, v);
206 }
207
208 /****************************************************************************
209  Fetch a uint32 value by a arbitrary blob key, return -1 if not found.
210  Output is uint32 in native byte order.
211 ****************************************************************************/
212
213 BOOL tdb_fetch_uint32_byblob(TDB_CONTEXT *tdb, const char *keyval, size_t len, uint32 *value)
214 {
215         TDB_DATA key = make_tdb_data(keyval, len);
216         TDB_DATA data;
217
218         data = tdb_fetch(tdb, key);
219         if (!data.dptr || data.dsize != sizeof(uint32)) {
220                 SAFE_FREE(data.dptr);
221                 return False;
222         }
223
224         *value = IVAL(data.dptr,0);
225         SAFE_FREE(data.dptr);
226         return True;
227 }
228
229 /****************************************************************************
230  Fetch a uint32 value by string key, return -1 if not found.
231  Output is uint32 in native byte order.
232 ****************************************************************************/
233
234 BOOL tdb_fetch_uint32(TDB_CONTEXT *tdb, const char *keystr, uint32 *value)
235 {
236         return tdb_fetch_uint32_byblob(tdb, keystr, strlen(keystr) + 1, value);
237 }
238
239 /****************************************************************************
240  Store a uint32 value by an arbitary blob key, return 0 on success, -1 on failure.
241  Input is uint32 in native byte order. Output in tdb is in little-endian.
242 ****************************************************************************/
243
244 BOOL tdb_store_uint32_byblob(TDB_CONTEXT *tdb, const char *keystr, size_t len, uint32 value)
245 {
246         TDB_DATA key = make_tdb_data(keystr, len);
247         TDB_DATA data;
248         uint32 v_store;
249         BOOL ret = True;
250
251         SIVAL(&v_store, 0, value);
252         data.dptr = (void *)&v_store;
253         data.dsize = sizeof(uint32);
254
255         if (tdb_store(tdb, key, data, TDB_REPLACE) == -1)
256                 ret = False;
257
258         return ret;
259 }
260
261 /****************************************************************************
262  Store a uint32 value by string key, return 0 on success, -1 on failure.
263  Input is uint32 in native byte order. Output in tdb is in little-endian.
264 ****************************************************************************/
265
266 BOOL tdb_store_uint32(TDB_CONTEXT *tdb, const char *keystr, uint32 value)
267 {
268         return tdb_store_uint32_byblob(tdb, keystr, strlen(keystr) + 1, value);
269 }
270 /****************************************************************************
271  Store a buffer by a null terminated string key.  Return 0 on success, -1
272  on failure.
273 ****************************************************************************/
274
275 int tdb_store_bystring(TDB_CONTEXT *tdb, const char *keystr, TDB_DATA data, int flags)
276 {
277         TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
278         
279         return tdb_store(tdb, key, data, flags);
280 }
281
282 /****************************************************************************
283  Fetch a buffer using a null terminated string key.  Don't forget to call
284  free() on the result dptr.
285 ****************************************************************************/
286
287 TDB_DATA tdb_fetch_bystring(TDB_CONTEXT *tdb, const char *keystr)
288 {
289         TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
290
291         return tdb_fetch(tdb, key);
292 }
293
294 /****************************************************************************
295  Delete an entry using a null terminated string key. 
296 ****************************************************************************/
297
298 int tdb_delete_bystring(TDB_CONTEXT *tdb, const char *keystr)
299 {
300         TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
301
302         return tdb_delete(tdb, key);
303 }
304
305 /****************************************************************************
306  Atomic integer change. Returns old value. To create, set initial value in *oldval. 
307 ****************************************************************************/
308
309 int32 tdb_change_int32_atomic(TDB_CONTEXT *tdb, const char *keystr, int32 *oldval, int32 change_val)
310 {
311         int32 val;
312         int32 ret = -1;
313
314         if (tdb_lock_bystring(tdb, keystr,0) == -1)
315                 return -1;
316
317         if ((val = tdb_fetch_int32(tdb, keystr)) == -1) {
318                 /* The lookup failed */
319                 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
320                         /* but not because it didn't exist */
321                         goto err_out;
322                 }
323                 
324                 /* Start with 'old' value */
325                 val = *oldval;
326
327         } else {
328                 /* It worked, set return value (oldval) to tdb data */
329                 *oldval = val;
330         }
331
332         /* Increment value for storage and return next time */
333         val += change_val;
334                 
335         if (tdb_store_int32(tdb, keystr, val) == -1)
336                 goto err_out;
337
338         ret = 0;
339
340   err_out:
341
342         tdb_unlock_bystring(tdb, keystr);
343         return ret;
344 }
345
346 /****************************************************************************
347  Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval. 
348 ****************************************************************************/
349
350 BOOL tdb_change_uint32_atomic(TDB_CONTEXT *tdb, const char *keystr, uint32 *oldval, uint32 change_val)
351 {
352         uint32 val;
353         BOOL ret = False;
354
355         if (tdb_lock_bystring(tdb, keystr,0) == -1)
356                 return False;
357
358         if (!tdb_fetch_uint32(tdb, keystr, &val)) {
359                 /* It failed */
360                 if (tdb_error(tdb) != TDB_ERR_NOEXIST) { 
361                         /* and not because it didn't exist */
362                         goto err_out;
363                 }
364
365                 /* Start with 'old' value */
366                 val = *oldval;
367
368         } else {
369                 /* it worked, set return value (oldval) to tdb data */
370                 *oldval = val;
371
372         }
373
374         /* get a new value to store */
375         val += change_val;
376                 
377         if (!tdb_store_uint32(tdb, keystr, val))
378                 goto err_out;
379
380         ret = True;
381
382   err_out:
383
384         tdb_unlock_bystring(tdb, keystr);
385         return ret;
386 }
387
388 /****************************************************************************
389  Useful pair of routines for packing/unpacking data consisting of
390  integers and strings.
391 ****************************************************************************/
392
393 size_t tdb_pack(char *buf, int bufsize, const char *fmt, ...)
394 {
395         va_list ap;
396         uint8 bt;
397         uint16 w;
398         uint32 d;
399         int i;
400         void *p;
401         int len;
402         char *s;
403         char c;
404         char *buf0 = buf;
405         const char *fmt0 = fmt;
406         int bufsize0 = bufsize;
407
408         va_start(ap, fmt);
409
410         while (*fmt) {
411                 switch ((c = *fmt++)) {
412                 case 'b': /* unsigned 8-bit integer */
413                         len = 1;
414                         bt = (uint8)va_arg(ap, int);
415                         if (bufsize && bufsize >= len)
416                                 SSVAL(buf, 0, bt);
417                         break;
418                 case 'w': /* unsigned 16-bit integer */
419                         len = 2;
420                         w = (uint16)va_arg(ap, int);
421                         if (bufsize && bufsize >= len)
422                                 SSVAL(buf, 0, w);
423                         break;
424                 case 'd': /* signed 32-bit integer (standard int in most systems) */
425                         len = 4;
426                         d = va_arg(ap, uint32);
427                         if (bufsize && bufsize >= len)
428                                 SIVAL(buf, 0, d);
429                         break;
430                 case 'p': /* pointer */
431                         len = 4;
432                         p = va_arg(ap, void *);
433                         d = p?1:0;
434                         if (bufsize && bufsize >= len)
435                                 SIVAL(buf, 0, d);
436                         break;
437                 case 'P': /* null-terminated string */
438                         s = va_arg(ap,char *);
439                         w = strlen(s);
440                         len = w + 1;
441                         if (bufsize && bufsize >= len)
442                                 memcpy(buf, s, len);
443                         break;
444                 case 'f': /* null-terminated string */
445                         s = va_arg(ap,char *);
446                         w = strlen(s);
447                         len = w + 1;
448                         if (bufsize && bufsize >= len)
449                                 memcpy(buf, s, len);
450                         break;
451                 case 'B': /* fixed-length string */
452                         i = va_arg(ap, int);
453                         s = va_arg(ap, char *);
454                         len = 4+i;
455                         if (bufsize && bufsize >= len) {
456                                 SIVAL(buf, 0, i);
457                                 memcpy(buf+4, s, i);
458                         }
459                         break;
460                 default:
461                         DEBUG(0,("Unknown tdb_pack format %c in %s\n", 
462                                  c, fmt));
463                         len = 0;
464                         break;
465                 }
466
467                 buf += len;
468                 if (bufsize)
469                         bufsize -= len;
470                 if (bufsize < 0)
471                         bufsize = 0;
472         }
473
474         va_end(ap);
475
476         DEBUG(18,("tdb_pack(%s, %d) -> %d\n", 
477                  fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
478         
479         return PTR_DIFF(buf, buf0);
480 }
481
482 /****************************************************************************
483  Useful pair of routines for packing/unpacking data consisting of
484  integers and strings.
485 ****************************************************************************/
486
487 int tdb_unpack(char *buf, int bufsize, const char *fmt, ...)
488 {
489         va_list ap;
490         uint8 *bt;
491         uint16 *w;
492         uint32 *d;
493         int len;
494         int *i;
495         void **p;
496         char *s, **b;
497         char c;
498         char *buf0 = buf;
499         const char *fmt0 = fmt;
500         int bufsize0 = bufsize;
501
502         va_start(ap, fmt);
503         
504         while (*fmt) {
505                 switch ((c=*fmt++)) {
506                 case 'b':
507                         len = 1;
508                         bt = va_arg(ap, uint8 *);
509                         if (bufsize < len)
510                                 goto no_space;
511                         *bt = SVAL(buf, 0);
512                         break;
513                 case 'w':
514                         len = 2;
515                         w = va_arg(ap, uint16 *);
516                         if (bufsize < len)
517                                 goto no_space;
518                         *w = SVAL(buf, 0);
519                         break;
520                 case 'd':
521                         len = 4;
522                         d = va_arg(ap, uint32 *);
523                         if (bufsize < len)
524                                 goto no_space;
525                         *d = IVAL(buf, 0);
526                         break;
527                 case 'p':
528                         len = 4;
529                         p = va_arg(ap, void **);
530                         if (bufsize < len)
531                                 goto no_space;
532                         *p = (void *)IVAL(buf, 0);
533                         break;
534                 case 'P':
535                         s = va_arg(ap,char *);
536                         len = strlen(buf) + 1;
537                         if (bufsize < len || len > sizeof(pstring))
538                                 goto no_space;
539                         memcpy(s, buf, len);
540                         break;
541                 case 'f':
542                         s = va_arg(ap,char *);
543                         len = strlen(buf) + 1;
544                         if (bufsize < len || len > sizeof(fstring))
545                                 goto no_space;
546                         memcpy(s, buf, len);
547                         break;
548                 case 'B':
549                         i = va_arg(ap, int *);
550                         b = va_arg(ap, char **);
551                         len = 4;
552                         if (bufsize < len)
553                                 goto no_space;
554                         *i = IVAL(buf, 0);
555                         if (! *i) {
556                                 *b = NULL;
557                                 break;
558                         }
559                         len += *i;
560                         if (bufsize < len)
561                                 goto no_space;
562                         *b = (char *)SMB_MALLOC(*i);
563                         if (! *b)
564                                 goto no_space;
565                         memcpy(*b, buf+4, *i);
566                         break;
567                 default:
568                         DEBUG(0,("Unknown tdb_unpack format %c in %s\n", 
569                                  c, fmt));
570
571                         len = 0;
572                         break;
573                 }
574
575                 buf += len;
576                 bufsize -= len;
577         }
578
579         va_end(ap);
580
581         DEBUG(18,("tdb_unpack(%s, %d) -> %d\n", 
582                  fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
583
584         return PTR_DIFF(buf, buf0);
585
586  no_space:
587         return -1;
588 }
589
590
591 /**
592  * Pack SID passed by pointer
593  *
594  * @param pack_buf pointer to buffer which is to be filled with packed data
595  * @param bufsize size of packing buffer
596  * @param sid pointer to sid to be packed
597  *
598  * @return length of the packed representation of the whole structure
599  **/
600 size_t tdb_sid_pack(char* pack_buf, int bufsize, DOM_SID* sid)
601 {
602         int idx;
603         size_t len = 0;
604         
605         if (!sid || !pack_buf) return -1;
606         
607         len += tdb_pack(pack_buf + len, bufsize - len, "bb", sid->sid_rev_num,
608                         sid->num_auths);
609         
610         for (idx = 0; idx < 6; idx++) {
611                 len += tdb_pack(pack_buf + len, bufsize - len, "b", sid->id_auth[idx]);
612         }
613         
614         for (idx = 0; idx < MAXSUBAUTHS; idx++) {
615                 len += tdb_pack(pack_buf + len, bufsize - len, "d", sid->sub_auths[idx]);
616         }
617         
618         return len;
619 }
620
621
622 /**
623  * Unpack SID into a pointer
624  *
625  * @param pack_buf pointer to buffer with packed representation
626  * @param bufsize size of the buffer
627  * @param sid pointer to sid structure to be filled with unpacked data
628  *
629  * @return size of structure unpacked from buffer
630  **/
631 size_t tdb_sid_unpack(char* pack_buf, int bufsize, DOM_SID* sid)
632 {
633         int idx, len = 0;
634         
635         if (!sid || !pack_buf) return -1;
636
637         len += tdb_unpack(pack_buf + len, bufsize - len, "bb",
638                           &sid->sid_rev_num, &sid->num_auths);
639                           
640         for (idx = 0; idx < 6; idx++) {
641                 len += tdb_unpack(pack_buf + len, bufsize - len, "b", &sid->id_auth[idx]);
642         }
643         
644         for (idx = 0; idx < MAXSUBAUTHS; idx++) {
645                 len += tdb_unpack(pack_buf + len, bufsize - len, "d", &sid->sub_auths[idx]);
646         }
647         
648         return len;
649 }
650
651
652 /**
653  * Pack TRUSTED_DOM_PASS passed by pointer
654  *
655  * @param pack_buf pointer to buffer which is to be filled with packed data
656  * @param bufsize size of the buffer
657  * @param pass pointer to trusted domain password to be packed
658  *
659  * @return length of the packed representation of the whole structure
660  **/
661 size_t tdb_trusted_dom_pass_pack(char* pack_buf, int bufsize, TRUSTED_DOM_PASS* pass)
662 {
663         int idx, len = 0;
664         
665         if (!pack_buf || !pass) return -1;
666         
667         /* packing unicode domain name and password */
668         len += tdb_pack(pack_buf + len, bufsize - len, "d", pass->uni_name_len);
669         
670         for (idx = 0; idx < 32; idx++)
671                 len +=  tdb_pack(pack_buf + len, bufsize - len, "w", pass->uni_name[idx]);
672         
673         len += tdb_pack(pack_buf + len, bufsize - len, "dPd", pass->pass_len,
674                              pass->pass, pass->mod_time);
675
676         /* packing SID structure */
677         len += tdb_sid_pack(pack_buf + len, bufsize - len, &pass->domain_sid);
678
679         return len;
680 }
681
682
683 /**
684  * Unpack TRUSTED_DOM_PASS passed by pointer
685  *
686  * @param pack_buf pointer to buffer with packed representation
687  * @param bufsize size of the buffer
688  * @param pass pointer to trusted domain password to be filled with unpacked data
689  *
690  * @return size of structure unpacked from buffer
691  **/
692 size_t tdb_trusted_dom_pass_unpack(char* pack_buf, int bufsize, TRUSTED_DOM_PASS* pass)
693 {
694         int idx, len = 0;
695         
696         if (!pack_buf || !pass) return -1;
697
698         /* unpack unicode domain name and plaintext password */
699         len += tdb_unpack(pack_buf, bufsize - len, "d", &pass->uni_name_len);
700         
701         for (idx = 0; idx < 32; idx++)
702                 len +=  tdb_unpack(pack_buf + len, bufsize - len, "w", &pass->uni_name[idx]);
703
704         len += tdb_unpack(pack_buf + len, bufsize - len, "dPd", &pass->pass_len, &pass->pass,
705                           &pass->mod_time);
706         
707         /* unpack domain sid */
708         len += tdb_sid_unpack(pack_buf + len, bufsize - len, &pass->domain_sid);
709         
710         return len;     
711 }
712
713
714 /****************************************************************************
715  Log tdb messages via DEBUG().
716 ****************************************************************************/
717
718 static void tdb_log(TDB_CONTEXT *tdb, int level, const char *format, ...)
719 {
720         va_list ap;
721         char *ptr = NULL;
722
723         va_start(ap, format);
724         vasprintf(&ptr, format, ap);
725         va_end(ap);
726         
727         if (!ptr || !*ptr)
728                 return;
729
730         DEBUG(level, ("tdb(%s): %s", tdb->name ? tdb->name : "unnamed", ptr));
731         SAFE_FREE(ptr);
732 }
733
734 /****************************************************************************
735  Like tdb_open() but also setup a logging function that redirects to
736  the samba DEBUG() system.
737 ****************************************************************************/
738
739 TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
740                           int open_flags, mode_t mode)
741 {
742         TDB_CONTEXT *tdb;
743
744         if (!lp_use_mmap())
745                 tdb_flags |= TDB_NOMMAP;
746
747         tdb = tdb_open_ex(name, hash_size, tdb_flags, 
748                                     open_flags, mode, tdb_log, NULL);
749         if (!tdb)
750                 return NULL;
751
752         return tdb;
753 }
754
755
756 /****************************************************************************
757  Allow tdb_delete to be used as a tdb_traversal_fn.
758 ****************************************************************************/
759
760 int tdb_traverse_delete_fn(TDB_CONTEXT *the_tdb, TDB_DATA key, TDB_DATA dbuf,
761                      void *state)
762 {
763     return tdb_delete(the_tdb, key);
764 }
765
766
767
768 /**
769  * Search across the whole tdb for keys that match the given pattern
770  * return the result as a list of keys
771  *
772  * @param tdb pointer to opened tdb file context
773  * @param pattern searching pattern used by fnmatch(3) functions
774  *
775  * @return list of keys found by looking up with given pattern
776  **/
777 TDB_LIST_NODE *tdb_search_keys(TDB_CONTEXT *tdb, const char* pattern)
778 {
779         TDB_DATA key, next;
780         TDB_LIST_NODE *list = NULL;
781         TDB_LIST_NODE *rec = NULL;
782         TDB_LIST_NODE *tmp = NULL;
783         
784         for (key = tdb_firstkey(tdb); key.dptr; key = next) {
785                 /* duplicate key string to ensure null-termination */
786                 char *key_str = (char*) SMB_STRNDUP(key.dptr, key.dsize);
787                 if (!key_str) {
788                         DEBUG(0, ("tdb_search_keys: strndup() failed!\n"));
789                         smb_panic("strndup failed!\n");
790                 }
791                 
792                 DEBUG(18, ("checking %s for match to pattern %s\n", key_str, pattern));
793                 
794                 next = tdb_nextkey(tdb, key);
795
796                 /* do the pattern checking */
797                 if (fnmatch(pattern, key_str, 0) == 0) {
798                         rec = SMB_MALLOC_P(TDB_LIST_NODE);
799                         ZERO_STRUCTP(rec);
800
801                         rec->node_key = key;
802         
803                         DLIST_ADD_END(list, rec, tmp);
804                 
805                         DEBUG(18, ("checking %s matched pattern %s\n", key_str, pattern));
806                 } else {
807                         free(key.dptr);
808                 }
809                 
810                 /* free duplicated key string */
811                 free(key_str);
812         }
813         
814         return list;
815
816 }
817
818
819 /**
820  * Free the list returned by tdb_search_keys
821  *
822  * @param node list of results found by tdb_search_keys
823  **/
824 void tdb_search_list_free(TDB_LIST_NODE* node)
825 {
826         TDB_LIST_NODE *next_node;
827         
828         while (node) {
829                 next_node = node->next;
830                 SAFE_FREE(node->node_key.dptr);
831                 SAFE_FREE(node);
832                 node = next_node;
833         };
834 }