tdb_unpack: Convert to size_t for internal calculations
[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 /* these are little tdb utility functions that are meant to make
33    dealing with a tdb database a little less cumbersome in Samba */
34
35 int tdb_trans_store_bystring(TDB_CONTEXT *tdb, const char *keystr,
36                              TDB_DATA data, int flags)
37 {
38         TDB_DATA key = string_term_tdb_data(keystr);
39
40         return tdb_trans_store(tdb, key, data, flags);
41 }
42
43 /****************************************************************************
44  Useful pair of routines for packing/unpacking data consisting of
45  integers and strings.
46 ****************************************************************************/
47
48 static size_t tdb_pack_va(uint8_t *buf, int bufsize, const char *fmt, va_list ap)
49 {
50         uint8_t bt;
51         uint16_t w;
52         uint32_t d;
53         int i;
54         void *p;
55         int len;
56         char *s;
57         char c;
58         uint8_t *buf0 = buf;
59         const char *fmt0 = fmt;
60         int bufsize0 = bufsize;
61
62         while (*fmt) {
63                 switch ((c = *fmt++)) {
64                 case 'b': /* unsigned 8-bit integer */
65                         len = 1;
66                         bt = (uint8_t)va_arg(ap, int);
67                         if (bufsize && bufsize >= len)
68                                 SSVAL(buf, 0, bt);
69                         break;
70                 case 'w': /* unsigned 16-bit integer */
71                         len = 2;
72                         w = (uint16_t)va_arg(ap, int);
73                         if (bufsize && bufsize >= len)
74                                 SSVAL(buf, 0, w);
75                         break;
76                 case 'd': /* signed 32-bit integer (standard int in most systems) */
77                         len = 4;
78                         d = va_arg(ap, uint32_t);
79                         if (bufsize && bufsize >= len)
80                                 SIVAL(buf, 0, d);
81                         break;
82                 case 'p': /* pointer */
83                         len = 4;
84                         p = va_arg(ap, void *);
85                         d = p?1:0;
86                         if (bufsize && bufsize >= len)
87                                 SIVAL(buf, 0, d);
88                         break;
89                 case 'P': /* null-terminated string */
90                         s = va_arg(ap,char *);
91                         w = strlen(s);
92                         len = w + 1;
93                         if (bufsize && bufsize >= len)
94                                 memcpy(buf, s, len);
95                         break;
96                 case 'f': /* null-terminated string */
97                         s = va_arg(ap,char *);
98                         w = strlen(s);
99                         len = w + 1;
100                         if (bufsize && bufsize >= len)
101                                 memcpy(buf, s, len);
102                         break;
103                 case 'B': /* fixed-length string */
104                         i = va_arg(ap, int);
105                         s = va_arg(ap, char *);
106                         len = 4+i;
107                         if (bufsize && bufsize >= len) {
108                                 SIVAL(buf, 0, i);
109                                 memcpy(buf+4, s, i);
110                         }
111                         break;
112                 default:
113                         DEBUG(0,("Unknown tdb_pack format %c in %s\n", 
114                                  c, fmt));
115                         len = 0;
116                         break;
117                 }
118
119                 buf += len;
120                 if (bufsize)
121                         bufsize -= len;
122                 if (bufsize < 0)
123                         bufsize = 0;
124         }
125
126         DEBUG(18,("tdb_pack_va(%s, %d) -> %d\n", 
127                  fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
128
129         return PTR_DIFF(buf, buf0);
130 }
131
132 size_t tdb_pack(uint8_t *buf, int bufsize, const char *fmt, ...)
133 {
134         va_list ap;
135         size_t result;
136
137         va_start(ap, fmt);
138         result = tdb_pack_va(buf, bufsize, fmt, ap);
139         va_end(ap);
140         return result;
141 }
142
143 /****************************************************************************
144  Useful pair of routines for packing/unpacking data consisting of
145  integers and strings.
146 ****************************************************************************/
147
148 int tdb_unpack(const uint8_t *buf, int in_bufsize, const char *fmt, ...)
149 {
150         va_list ap;
151         uint8_t *bt;
152         uint16_t *w;
153         uint32_t *d;
154         size_t bufsize = in_bufsize;
155         size_t len;
156         int *i;
157         void **p;
158         char *s, **b, **ps;
159         char c;
160         const uint8_t *buf0 = buf;
161         const char *fmt0 = fmt;
162
163         va_start(ap, fmt);
164
165         while (*fmt) {
166                 switch ((c=*fmt++)) {
167                 case 'b': /* unsigned 8-bit integer */
168                         len = 1;
169                         bt = va_arg(ap, uint8_t *);
170                         if (bufsize < len)
171                                 goto no_space;
172                         *bt = SVAL(buf, 0);
173                         break;
174                 case 'w': /* unsigned 16-bit integer */
175                         len = 2;
176                         w = va_arg(ap, uint16_t *);
177                         if (bufsize < len)
178                                 goto no_space;
179                         *w = SVAL(buf, 0);
180                         break;
181                 case 'd': /* unsigned 32-bit integer (standard int in most systems) */
182                         len = 4;
183                         d = va_arg(ap, uint32_t *);
184                         if (bufsize < len)
185                                 goto no_space;
186                         *d = IVAL(buf, 0);
187                         break;
188                 case 'p': /* pointer */
189                         len = 4;
190                         p = va_arg(ap, void **);
191                         if (bufsize < len)
192                                 goto no_space;
193                         /*
194                          * This isn't a real pointer - only a token (1 or 0)
195                          * to mark the fact a pointer is present.
196                          */
197
198                         *p = (void *)(IVAL(buf, 0) ? (void *)1 : NULL);
199                         break;
200                 case 'P': /* null-terminated string */
201                         /* Return malloc'ed string. */
202                         ps = va_arg(ap,char **);
203                         len = strnlen((const char *)buf, bufsize) + 1;
204                         if (bufsize < len)
205                                 goto no_space;
206                         *ps = SMB_STRDUP((const char *)buf);
207                         if (*ps == NULL) {
208                                 goto no_space;
209                         }
210                         break;
211                 case 'f': /* null-terminated string */
212                         s = va_arg(ap,char *);
213                         len = strnlen((const char *)buf, bufsize) + 1;
214                         if (bufsize < len || len > sizeof(fstring))
215                                 goto no_space;
216                         memcpy(s, buf, len);
217                         break;
218                 case 'B': /* fixed-length string */
219                         i = va_arg(ap, int *);
220                         b = va_arg(ap, char **);
221                         len = 4;
222                         if (bufsize < len)
223                                 goto no_space;
224                         *i = IVAL(buf, 0);
225                         if (! *i) {
226                                 *b = NULL;
227                                 break;
228                         }
229                         len += *i;
230                         if (bufsize < len)
231                                 goto no_space;
232                         *b = (char *)SMB_MALLOC(*i);
233                         if (! *b)
234                                 goto no_space;
235                         memcpy(*b, buf+4, *i);
236                         break;
237                 default:
238                         DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
239                                  c, fmt));
240
241                         len = 0;
242                         break;
243                 }
244
245                 buf += len;
246                 bufsize -= len;
247         }
248
249         va_end(ap);
250
251         DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
252                  fmt0, in_bufsize, (int)PTR_DIFF(buf, buf0)));
253
254         return PTR_DIFF(buf, buf0);
255
256  no_space:
257         va_end(ap);
258         return -1;
259 }
260
261
262 /****************************************************************************
263  Log tdb messages via DEBUG().
264 ****************************************************************************/
265
266 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
267                     const char *format, ...) PRINTF_ATTRIBUTE(3,4);
268
269 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, const char *format, ...)
270 {
271         va_list ap;
272         char *ptr = NULL;
273         int ret;
274
275         va_start(ap, format);
276         ret = vasprintf(&ptr, format, ap);
277         va_end(ap);
278
279         if ((ret == -1) || !*ptr)
280                 return;
281
282         DEBUG((int)level, ("tdb(%s): %s", tdb_name(tdb) ? tdb_name(tdb) : "unnamed", ptr));
283         SAFE_FREE(ptr);
284 }
285
286 /****************************************************************************
287  Like tdb_open() but also setup a logging function that redirects to
288  the samba DEBUG() system.
289 ****************************************************************************/
290
291 TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
292                           int open_flags, mode_t mode)
293 {
294         TDB_CONTEXT *tdb;
295         struct tdb_logging_context log_ctx = { .log_fn = tdb_log };
296
297         if (!lp_use_mmap())
298                 tdb_flags |= TDB_NOMMAP;
299
300         if ((hash_size == 0) && (name != NULL)) {
301                 const char *base = strrchr_m(name, '/');
302                 if (base != NULL) {
303                         base += 1;
304                 }
305                 else {
306                         base = name;
307                 }
308                 hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
309         }
310
311         tdb = tdb_open_ex(name, hash_size, tdb_flags,
312                           open_flags, mode, &log_ctx, NULL);
313         if (!tdb)
314                 return NULL;
315
316         return tdb;
317 }
318
319 /****************************************************************************
320  tdb_store, wrapped in a transaction. This way we make sure that a process
321  that dies within writing does not leave a corrupt tdb behind.
322 ****************************************************************************/
323
324 int tdb_trans_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
325                     int flag)
326 {
327         int res;
328
329         if ((res = tdb_transaction_start(tdb)) != 0) {
330                 DEBUG(5, ("tdb_transaction_start failed\n"));
331                 return res;
332         }
333
334         if ((res = tdb_store(tdb, key, dbuf, flag)) != 0) {
335                 DEBUG(10, ("tdb_store failed\n"));
336                 tdb_transaction_cancel(tdb);
337                 return res;
338         }
339
340         if ((res = tdb_transaction_commit(tdb)) != 0) {
341                 DEBUG(5, ("tdb_transaction_commit failed\n"));
342         }
343
344         return res;
345 }
346
347 /****************************************************************************
348  tdb_delete, wrapped in a transaction. This way we make sure that a process
349  that dies within deleting does not leave a corrupt tdb behind.
350 ****************************************************************************/
351
352 int tdb_trans_delete(struct tdb_context *tdb, TDB_DATA key)
353 {
354         int res;
355
356         if ((res = tdb_transaction_start(tdb)) != 0) {
357                 DEBUG(5, ("tdb_transaction_start failed\n"));
358                 return res;
359         }
360
361         if ((res = tdb_delete(tdb, key)) != 0) {
362                 DEBUG(10, ("tdb_delete failed\n"));
363                 tdb_transaction_cancel(tdb);
364                 return res;
365         }
366
367         if ((res = tdb_transaction_commit(tdb)) != 0) {
368                 DEBUG(5, ("tdb_transaction_commit failed\n"));
369         }
370
371         return res;
372 }
373
374 int tdb_data_cmp(TDB_DATA t1, TDB_DATA t2)
375 {
376         int ret;
377         if (t1.dptr == NULL && t2.dptr != NULL) {
378                 return -1;
379         }
380         if (t1.dptr != NULL && t2.dptr == NULL) {
381                 return 1;
382         }
383         if (t1.dptr == t2.dptr) {
384                 return t1.dsize - t2.dsize;
385         }
386         ret = memcmp(t1.dptr, t2.dptr, MIN(t1.dsize, t2.dsize));
387         if (ret == 0) {
388                 return t1.dsize - t2.dsize;
389         }
390         return ret;
391 }
392
393 char *tdb_data_string(TALLOC_CTX *mem_ctx, TDB_DATA d)
394 {
395         int len;
396         char *ret = NULL;
397         cbuf *ost = cbuf_new(mem_ctx);
398
399         if (ost == NULL) {
400                 return NULL;
401         }
402
403         len = cbuf_printf(ost, "%zu:", d.dsize);
404         if (len == -1) {
405                 goto done;
406         }
407
408         if (d.dptr == NULL) {
409                 len = cbuf_puts(ost, "<NULL>", -1);
410         } else {
411                 len = cbuf_print_quoted(ost, (const char*)d.dptr, d.dsize);
412         }
413         if (len == -1) {
414                 goto done;
415         }
416
417         cbuf_swapptr(ost, &ret, 0);
418         talloc_steal(mem_ctx, ret);
419
420 done:
421         talloc_free(ost);
422         return ret;
423 }
424
425 static sig_atomic_t gotalarm;
426
427 /***************************************************************
428  Signal function to tell us we timed out.
429 ****************************************************************/
430
431 static void gotalarm_sig(int signum)
432 {
433         gotalarm = 1;
434 }
435
436 /****************************************************************************
437  Lock a chain with timeout (in seconds).
438 ****************************************************************************/
439
440 static int tdb_chainlock_with_timeout_internal( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout, int rw_type)
441 {
442         /* Allow tdb_chainlock to be interrupted by an alarm. */
443         int ret;
444         gotalarm = 0;
445
446         if (timeout) {
447                 CatchSignal(SIGALRM, gotalarm_sig);
448                 tdb_setalarm_sigptr(tdb, &gotalarm);
449                 alarm(timeout);
450         }
451
452         if (rw_type == F_RDLCK)
453                 ret = tdb_chainlock_read(tdb, key);
454         else
455                 ret = tdb_chainlock(tdb, key);
456
457         if (timeout) {
458                 alarm(0);
459                 tdb_setalarm_sigptr(tdb, NULL);
460                 CatchSignal(SIGALRM, SIG_IGN);
461                 if (gotalarm && (ret != 0)) {
462                         DEBUG(0,("tdb_chainlock_with_timeout_internal: alarm (%u) timed out for key %s in tdb %s\n",
463                                 timeout, key.dptr, tdb_name(tdb)));
464                         /* TODO: If we time out waiting for a lock, it might
465                          * be nice to use F_GETLK to get the pid of the
466                          * process currently holding the lock and print that
467                          * as part of the debugging message. -- mbp */
468                         return -1;
469                 }
470         }
471
472         return ret == 0 ? 0 : -1;
473 }
474
475 /****************************************************************************
476  Write lock a chain. Return non-zero if timeout or lock failed.
477 ****************************************************************************/
478
479 int tdb_chainlock_with_timeout( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout)
480 {
481         return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_WRLCK);
482 }
483
484 int tdb_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval,
485                                    int timeout)
486 {
487         TDB_DATA key = string_term_tdb_data(keyval);
488
489         return tdb_chainlock_with_timeout(tdb, key, timeout);
490 }
491
492 /****************************************************************************
493  Read lock a chain by string. Return non-zero if timeout or lock failed.
494 ****************************************************************************/
495
496 int tdb_read_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval, unsigned int timeout)
497 {
498         TDB_DATA key = string_term_tdb_data(keyval);
499
500         return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_RDLCK);
501 }