s3:lib: Fix uninitialized variable
[samba.git] / source3 / lib / util_tdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3    tdb utility functions
4    Copyright (C) Andrew Tridgell   1992-1998
5    Copyright (C) Rafal Szczesniak  2002
6    Copyright (C) Michael Adam      2007
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "util_tdb.h"
25 #include "cbuf.h"
26
27 #undef malloc
28 #undef realloc
29 #undef calloc
30 #undef strdup
31
32 /****************************************************************************
33  Useful pair of routines for packing/unpacking data consisting of
34  integers and strings.
35 ****************************************************************************/
36
37 static size_t tdb_pack_va(uint8_t *buf, int bufsize, const char *fmt, va_list ap)
38 {
39         uint8_t bt;
40         uint16_t w;
41         uint32_t d;
42         int i;
43         void *p;
44         int len = 0;
45         char *s;
46         char c;
47         uint8_t *buf0 = buf;
48         const char *fmt0 = fmt;
49         int bufsize0 = bufsize;
50
51         while (*fmt) {
52                 switch ((c = *fmt++)) {
53                 case 'b': /* unsigned 8-bit integer */
54                         len = 1;
55                         bt = (uint8_t)va_arg(ap, int);
56                         if (bufsize && bufsize >= len)
57                                 SSVAL(buf, 0, bt);
58                         break;
59                 case 'w': /* unsigned 16-bit integer */
60                         len = 2;
61                         w = (uint16_t)va_arg(ap, int);
62                         if (bufsize && bufsize >= len)
63                                 SSVAL(buf, 0, w);
64                         break;
65                 case 'd': /* signed 32-bit integer (standard int in most systems) */
66                         len = 4;
67                         d = va_arg(ap, uint32_t);
68                         if (bufsize && bufsize >= len)
69                                 SIVAL(buf, 0, d);
70                         break;
71                 case 'p': /* pointer */
72                         len = 4;
73                         p = va_arg(ap, void *);
74                         d = p?1:0;
75                         if (bufsize && bufsize >= len)
76                                 SIVAL(buf, 0, d);
77                         break;
78                 case 'P': /* null-terminated string */
79                         s = va_arg(ap,char *);
80                         w = strlen(s);
81                         len = w + 1;
82                         if (bufsize && bufsize >= len)
83                                 memcpy(buf, s, len);
84                         break;
85                 case 'f': /* null-terminated string */
86                         s = va_arg(ap,char *);
87                         w = strlen(s);
88                         len = w + 1;
89                         if (bufsize && bufsize >= len)
90                                 memcpy(buf, s, len);
91                         break;
92                 case 'B': /* fixed-length string */
93                         i = va_arg(ap, int);
94                         s = va_arg(ap, char *);
95                         len = 4+i;
96                         if (bufsize && bufsize >= len) {
97                                 SIVAL(buf, 0, i);
98                                 memcpy(buf+4, s, i);
99                         }
100                         break;
101                 default:
102                         DEBUG(0,("Unknown tdb_pack format %c in %s\n", 
103                                  c, fmt));
104                         len = 0;
105                         break;
106                 }
107
108                 buf += len;
109                 if (bufsize)
110                         bufsize -= len;
111                 if (bufsize < 0)
112                         bufsize = 0;
113         }
114
115         DEBUG(18,("tdb_pack_va(%s, %d) -> %d\n", 
116                  fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
117
118         return PTR_DIFF(buf, buf0);
119 }
120
121 size_t tdb_pack(uint8_t *buf, int bufsize, const char *fmt, ...)
122 {
123         va_list ap;
124         size_t result;
125
126         va_start(ap, fmt);
127         result = tdb_pack_va(buf, bufsize, fmt, ap);
128         va_end(ap);
129         return result;
130 }
131
132 /****************************************************************************
133  Useful pair of routines for packing/unpacking data consisting of
134  integers and strings.
135 ****************************************************************************/
136
137 int tdb_unpack(const uint8_t *buf, int in_bufsize, const char *fmt, ...)
138 {
139         va_list ap;
140         uint8_t *bt;
141         uint16_t *w;
142         uint32_t *d;
143         size_t bufsize = in_bufsize;
144         size_t len;
145         uint32_t *i;
146         void **p;
147         char *s, **b, **ps;
148         char c;
149         const uint8_t *buf0 = buf;
150         const char *fmt0 = fmt;
151
152         va_start(ap, fmt);
153
154         while (*fmt) {
155                 switch ((c=*fmt++)) {
156                 case 'b': /* unsigned 8-bit integer */
157                         len = 1;
158                         bt = va_arg(ap, uint8_t *);
159                         if (bufsize < len)
160                                 goto no_space;
161                         *bt = SVAL(buf, 0);
162                         break;
163                 case 'w': /* unsigned 16-bit integer */
164                         len = 2;
165                         w = va_arg(ap, uint16_t *);
166                         if (bufsize < len)
167                                 goto no_space;
168                         *w = SVAL(buf, 0);
169                         break;
170                 case 'd': /* unsigned 32-bit integer (standard int in most systems) */
171                         len = 4;
172                         d = va_arg(ap, uint32_t *);
173                         if (bufsize < len)
174                                 goto no_space;
175                         *d = IVAL(buf, 0);
176                         break;
177                 case 'p': /* pointer */
178                         len = 4;
179                         p = va_arg(ap, void **);
180                         if (bufsize < len)
181                                 goto no_space;
182                         /*
183                          * This isn't a real pointer - only a token (1 or 0)
184                          * to mark the fact a pointer is present.
185                          */
186
187                         *p = (void *)(IVAL(buf, 0) ? (void *)1 : NULL);
188                         break;
189                 case 'P': /* null-terminated string */
190                         /* Return malloc'ed string. */
191                         ps = va_arg(ap,char **);
192                         len = strnlen((const char *)buf, bufsize) + 1;
193                         if (bufsize < len)
194                                 goto no_space;
195                         *ps = SMB_STRDUP((const char *)buf);
196                         if (*ps == NULL) {
197                                 goto no_space;
198                         }
199                         break;
200                 case 'f': /* null-terminated string */
201                         s = va_arg(ap,char *);
202                         len = strnlen((const char *)buf, bufsize) + 1;
203                         if (bufsize < len || len > sizeof(fstring))
204                                 goto no_space;
205                         memcpy(s, buf, len);
206                         break;
207                 case 'B': /* fixed-length string */
208                         i = va_arg(ap, uint32_t *);
209                         b = va_arg(ap, char **);
210                         len = 4;
211                         if (bufsize < len)
212                                 goto no_space;
213                         *i = IVAL(buf, 0);
214                         if (! *i) {
215                                 *b = NULL;
216                                 break;
217                         }
218                         len += *i;
219                         if (len < *i) {
220                                 goto no_space;
221                         }
222                         if (bufsize < len)
223                                 goto no_space;
224                         *b = (char *)SMB_MALLOC(*i);
225                         if (! *b)
226                                 goto no_space;
227                         memcpy(*b, buf+4, *i);
228                         break;
229                 default:
230                         DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
231                                  c, fmt));
232
233                         len = 0;
234                         break;
235                 }
236
237                 buf += len;
238                 bufsize -= len;
239         }
240
241         va_end(ap);
242
243         DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
244                  fmt0, in_bufsize, (int)PTR_DIFF(buf, buf0)));
245
246         return PTR_DIFF(buf, buf0);
247
248  no_space:
249         va_end(ap);
250         return -1;
251 }
252
253
254 /****************************************************************************
255  Log tdb messages via DEBUG().
256 ****************************************************************************/
257
258 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
259                     const char *format, ...) PRINTF_ATTRIBUTE(3,4);
260
261 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, const char *format, ...)
262 {
263         va_list ap;
264         char *ptr = NULL;
265         int ret;
266
267         va_start(ap, format);
268         ret = vasprintf(&ptr, format, ap);
269         va_end(ap);
270
271         if ((ret == -1) || !*ptr)
272                 return;
273
274         DEBUG((int)level, ("tdb(%s): %s", tdb_name(tdb) ? tdb_name(tdb) : "unnamed", ptr));
275         SAFE_FREE(ptr);
276 }
277
278 /****************************************************************************
279  Like tdb_open() but also setup a logging function that redirects to
280  the samba DEBUG() system.
281 ****************************************************************************/
282
283 TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
284                           int open_flags, mode_t mode)
285 {
286         TDB_CONTEXT *tdb;
287         struct tdb_logging_context log_ctx = { .log_fn = tdb_log };
288
289         if (!lp_use_mmap())
290                 tdb_flags |= TDB_NOMMAP;
291
292         if ((hash_size == 0) && (name != NULL)) {
293                 const char *base = strrchr_m(name, '/');
294                 if (base != NULL) {
295                         base += 1;
296                 }
297                 else {
298                         base = name;
299                 }
300                 hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
301         }
302
303         tdb = tdb_open_ex(name, hash_size, tdb_flags,
304                           open_flags, mode, &log_ctx, NULL);
305         if (!tdb)
306                 return NULL;
307
308         return tdb;
309 }
310
311 int tdb_data_cmp(TDB_DATA t1, TDB_DATA t2)
312 {
313         int ret;
314         if (t1.dptr == NULL && t2.dptr != NULL) {
315                 return -1;
316         }
317         if (t1.dptr != NULL && t2.dptr == NULL) {
318                 return 1;
319         }
320         if (t1.dptr == t2.dptr) {
321                 return t1.dsize - t2.dsize;
322         }
323         ret = memcmp(t1.dptr, t2.dptr, MIN(t1.dsize, t2.dsize));
324         if (ret == 0) {
325                 return t1.dsize - t2.dsize;
326         }
327         return ret;
328 }
329
330 char *tdb_data_string(TALLOC_CTX *mem_ctx, TDB_DATA d)
331 {
332         int len;
333         char *ret = NULL;
334         cbuf *ost = cbuf_new(mem_ctx);
335
336         if (ost == NULL) {
337                 return NULL;
338         }
339
340         len = cbuf_printf(ost, "%zu:", d.dsize);
341         if (len == -1) {
342                 goto done;
343         }
344
345         if (d.dptr == NULL) {
346                 len = cbuf_puts(ost, "<NULL>", -1);
347         } else {
348                 len = cbuf_print_quoted(ost, (const char*)d.dptr, d.dsize);
349         }
350         if (len == -1) {
351                 goto done;
352         }
353
354         cbuf_swapptr(ost, &ret, 0);
355         talloc_steal(mem_ctx, ret);
356
357 done:
358         talloc_free(ost);
359         return ret;
360 }
361
362 static sig_atomic_t gotalarm;
363
364 /***************************************************************
365  Signal function to tell us we timed out.
366 ****************************************************************/
367
368 static void gotalarm_sig(int signum)
369 {
370         gotalarm = 1;
371 }
372
373 /****************************************************************************
374  Lock a chain with timeout (in seconds).
375 ****************************************************************************/
376
377 static int tdb_chainlock_with_timeout_internal( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout, int rw_type)
378 {
379         /* Allow tdb_chainlock to be interrupted by an alarm. */
380         int ret;
381         gotalarm = 0;
382
383         if (timeout) {
384                 CatchSignal(SIGALRM, gotalarm_sig);
385                 tdb_setalarm_sigptr(tdb, &gotalarm);
386                 alarm(timeout);
387         }
388
389         if (rw_type == F_RDLCK)
390                 ret = tdb_chainlock_read(tdb, key);
391         else
392                 ret = tdb_chainlock(tdb, key);
393
394         if (timeout) {
395                 alarm(0);
396                 tdb_setalarm_sigptr(tdb, NULL);
397                 CatchSignal(SIGALRM, SIG_IGN);
398                 if (gotalarm && (ret != 0)) {
399                         DEBUG(0,("tdb_chainlock_with_timeout_internal: alarm (%u) timed out for key %s in tdb %s\n",
400                                 timeout, key.dptr, tdb_name(tdb)));
401                         /* TODO: If we time out waiting for a lock, it might
402                          * be nice to use F_GETLK to get the pid of the
403                          * process currently holding the lock and print that
404                          * as part of the debugging message. -- mbp */
405                         return -1;
406                 }
407         }
408
409         return ret == 0 ? 0 : -1;
410 }
411
412 /****************************************************************************
413  Write lock a chain. Return non-zero if timeout or lock failed.
414 ****************************************************************************/
415
416 int tdb_chainlock_with_timeout( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout)
417 {
418         return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_WRLCK);
419 }
420
421 int tdb_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval,
422                                    int timeout)
423 {
424         TDB_DATA key = string_term_tdb_data(keyval);
425
426         return tdb_chainlock_with_timeout(tdb, key, timeout);
427 }
428
429 /****************************************************************************
430  Read lock a chain by string. Return non-zero if timeout or lock failed.
431 ****************************************************************************/
432
433 int tdb_read_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval, unsigned int timeout)
434 {
435         TDB_DATA key = string_term_tdb_data(keyval);
436
437         return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_RDLCK);
438 }