r23792: convert Samba4 to GPLv3
[samba.git] / source4 / lib / util / util_tdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    tdb utility functions
5
6    Copyright (C) Andrew Tridgell 1992-2006
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 "lib/tdb/include/tdb.h"
24 #include "pstring.h"
25
26 /* these are little tdb utility functions that are meant to make
27    dealing with a tdb database a little less cumbersome in Samba */
28
29 /***************************************************************
30  Make a TDB_DATA and keep the const warning in one place
31 ****************************************************************/
32
33 static TDB_DATA make_tdb_data(const char *dptr, size_t dsize)
34 {
35         TDB_DATA ret;
36         ret.dptr = discard_const_p(unsigned char, dptr);
37         ret.dsize = dsize;
38         return ret;
39 }
40
41 /****************************************************************************
42  Lock a chain by string. Return -1 if lock failed.
43 ****************************************************************************/
44
45 int tdb_lock_bystring(struct tdb_context *tdb, const char *keyval)
46 {
47         TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
48         
49         return tdb_chainlock(tdb, key);
50 }
51
52 /****************************************************************************
53  Unlock a chain by string.
54 ****************************************************************************/
55
56 void tdb_unlock_bystring(struct tdb_context *tdb, const char *keyval)
57 {
58         TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
59
60         tdb_chainunlock(tdb, key);
61 }
62
63 /****************************************************************************
64  Read lock a chain by string. Return -1 if lock failed.
65 ****************************************************************************/
66
67 int tdb_read_lock_bystring(struct tdb_context *tdb, const char *keyval)
68 {
69         TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
70         
71         return tdb_chainlock_read(tdb, key);
72 }
73
74 /****************************************************************************
75  Read unlock a chain by string.
76 ****************************************************************************/
77
78 void tdb_read_unlock_bystring(struct tdb_context *tdb, const char *keyval)
79 {
80         TDB_DATA key = make_tdb_data(keyval, strlen(keyval)+1);
81         
82         tdb_chainunlock_read(tdb, key);
83 }
84
85
86 /****************************************************************************
87  Fetch a int32_t value by a arbitrary blob key, return -1 if not found.
88  Output is int32_t in native byte order.
89 ****************************************************************************/
90
91 int32_t tdb_fetch_int32_byblob(struct tdb_context *tdb, const char *keyval, size_t len)
92 {
93         TDB_DATA key = make_tdb_data(keyval, len);
94         TDB_DATA data;
95         int32_t ret;
96
97         data = tdb_fetch(tdb, key);
98         if (!data.dptr || data.dsize != sizeof(int32_t)) {
99                 SAFE_FREE(data.dptr);
100                 return -1;
101         }
102
103         ret = IVAL(data.dptr,0);
104         SAFE_FREE(data.dptr);
105         return ret;
106 }
107
108 /****************************************************************************
109  Fetch a int32_t value by string key, return -1 if not found.
110  Output is int32_t in native byte order.
111 ****************************************************************************/
112
113 int32_t tdb_fetch_int32(struct tdb_context *tdb, const char *keystr)
114 {
115         return tdb_fetch_int32_byblob(tdb, keystr, strlen(keystr) + 1);
116 }
117
118 /****************************************************************************
119  Store a int32_t value by an arbitary blob key, return 0 on success, -1 on failure.
120  Input is int32_t in native byte order. Output in tdb is in little-endian.
121 ****************************************************************************/
122
123 int tdb_store_int32_byblob(struct tdb_context *tdb, const char *keystr, size_t len, int32_t v)
124 {
125         TDB_DATA key = make_tdb_data(keystr, len);
126         TDB_DATA data;
127         int32_t v_store;
128
129         SIVAL(&v_store,0,v);
130         data.dptr = (void *)&v_store;
131         data.dsize = sizeof(int32_t);
132
133         return tdb_store(tdb, key, data, TDB_REPLACE);
134 }
135
136 /****************************************************************************
137  Store a int32_t value by string key, return 0 on success, -1 on failure.
138  Input is int32_t in native byte order. Output in tdb is in little-endian.
139 ****************************************************************************/
140
141 int tdb_store_int32(struct tdb_context *tdb, const char *keystr, int32_t v)
142 {
143         return tdb_store_int32_byblob(tdb, keystr, strlen(keystr) + 1, v);
144 }
145
146 /****************************************************************************
147  Fetch a uint32_t value by a arbitrary blob key, return -1 if not found.
148  Output is uint32_t in native byte order.
149 ****************************************************************************/
150
151 BOOL tdb_fetch_uint32_byblob(struct tdb_context *tdb, const char *keyval, size_t len, uint32_t *value)
152 {
153         TDB_DATA key = make_tdb_data(keyval, len);
154         TDB_DATA data;
155
156         data = tdb_fetch(tdb, key);
157         if (!data.dptr || data.dsize != sizeof(uint32_t)) {
158                 SAFE_FREE(data.dptr);
159                 return False;
160         }
161
162         *value = IVAL(data.dptr,0);
163         SAFE_FREE(data.dptr);
164         return True;
165 }
166
167 /****************************************************************************
168  Fetch a uint32_t value by string key, return -1 if not found.
169  Output is uint32_t in native byte order.
170 ****************************************************************************/
171
172 BOOL tdb_fetch_uint32(struct tdb_context *tdb, const char *keystr, uint32_t *value)
173 {
174         return tdb_fetch_uint32_byblob(tdb, keystr, strlen(keystr) + 1, value);
175 }
176
177 /****************************************************************************
178  Store a uint32_t value by an arbitary blob key, return 0 on success, -1 on failure.
179  Input is uint32_t in native byte order. Output in tdb is in little-endian.
180 ****************************************************************************/
181
182 BOOL tdb_store_uint32_byblob(struct tdb_context *tdb, const char *keystr, size_t len, uint32_t value)
183 {
184         TDB_DATA key = make_tdb_data(keystr, len);
185         TDB_DATA data;
186         uint32_t v_store;
187         BOOL ret = True;
188
189         SIVAL(&v_store, 0, value);
190         data.dptr = (void *)&v_store;
191         data.dsize = sizeof(uint32_t);
192
193         if (tdb_store(tdb, key, data, TDB_REPLACE) == -1)
194                 ret = False;
195
196         return ret;
197 }
198
199 /****************************************************************************
200  Store a uint32_t value by string key, return 0 on success, -1 on failure.
201  Input is uint32_t in native byte order. Output in tdb is in little-endian.
202 ****************************************************************************/
203
204 BOOL tdb_store_uint32(struct tdb_context *tdb, const char *keystr, uint32_t value)
205 {
206         return tdb_store_uint32_byblob(tdb, keystr, strlen(keystr) + 1, value);
207 }
208 /****************************************************************************
209  Store a buffer by a null terminated string key.  Return 0 on success, -1
210  on failure.
211 ****************************************************************************/
212
213 int tdb_store_bystring(struct tdb_context *tdb, const char *keystr, TDB_DATA data, int flags)
214 {
215         TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
216         
217         return tdb_store(tdb, key, data, flags);
218 }
219
220 /****************************************************************************
221  Fetch a buffer using a null terminated string key.  Don't forget to call
222  free() on the result dptr.
223 ****************************************************************************/
224
225 TDB_DATA tdb_fetch_bystring(struct tdb_context *tdb, const char *keystr)
226 {
227         TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
228
229         return tdb_fetch(tdb, key);
230 }
231
232 /****************************************************************************
233  Delete an entry using a null terminated string key. 
234 ****************************************************************************/
235
236 int tdb_delete_bystring(struct tdb_context *tdb, const char *keystr)
237 {
238         TDB_DATA key = make_tdb_data(keystr, strlen(keystr)+1);
239
240         return tdb_delete(tdb, key);
241 }
242
243 /****************************************************************************
244  Atomic integer change. Returns old value. To create, set initial value in *oldval. 
245 ****************************************************************************/
246
247 int32_t tdb_change_int32_atomic(struct tdb_context *tdb, const char *keystr, int32_t *oldval, int32_t change_val)
248 {
249         int32_t val;
250         int32_t ret = -1;
251
252         if (tdb_lock_bystring(tdb, keystr) == -1)
253                 return -1;
254
255         if ((val = tdb_fetch_int32(tdb, keystr)) == -1) {
256                 /* The lookup failed */
257                 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
258                         /* but not because it didn't exist */
259                         goto err_out;
260                 }
261                 
262                 /* Start with 'old' value */
263                 val = *oldval;
264
265         } else {
266                 /* It worked, set return value (oldval) to tdb data */
267                 *oldval = val;
268         }
269
270         /* Increment value for storage and return next time */
271         val += change_val;
272                 
273         if (tdb_store_int32(tdb, keystr, val) == -1)
274                 goto err_out;
275
276         ret = 0;
277
278   err_out:
279
280         tdb_unlock_bystring(tdb, keystr);
281         return ret;
282 }
283
284 /****************************************************************************
285  Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval. 
286 ****************************************************************************/
287
288 BOOL tdb_change_uint32_atomic(struct tdb_context *tdb, const char *keystr, uint32_t *oldval, uint32_t change_val)
289 {
290         uint32_t val;
291         BOOL ret = False;
292
293         if (tdb_lock_bystring(tdb, keystr) == -1)
294                 return False;
295
296         if (!tdb_fetch_uint32(tdb, keystr, &val)) {
297                 /* It failed */
298                 if (tdb_error(tdb) != TDB_ERR_NOEXIST) { 
299                         /* and not because it didn't exist */
300                         goto err_out;
301                 }
302
303                 /* Start with 'old' value */
304                 val = *oldval;
305
306         } else {
307                 /* it worked, set return value (oldval) to tdb data */
308                 *oldval = val;
309
310         }
311
312         /* get a new value to store */
313         val += change_val;
314                 
315         if (!tdb_store_uint32(tdb, keystr, val))
316                 goto err_out;
317
318         ret = True;
319
320   err_out:
321
322         tdb_unlock_bystring(tdb, keystr);
323         return ret;
324 }
325
326 /****************************************************************************
327  Allow tdb_delete to be used as a tdb_traversal_fn.
328 ****************************************************************************/
329
330 int tdb_traverse_delete_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf,
331                      void *state)
332 {
333     return tdb_delete(the_tdb, key);
334 }
335
336
337
338 /****************************************************************************
339  Useful pair of routines for packing/unpacking data consisting of
340  integers and strings.
341 ****************************************************************************/
342
343 size_t tdb_pack(TDB_CONTEXT *tdb, char *buf, int bufsize, const char *fmt, ...)
344 {
345         va_list ap;
346         uint8_t bt;
347         uint16_t w;
348         uint32_t d;
349         int i;
350         void *p;
351         int len;
352         char *s;
353         char c;
354         char *buf0 = buf;
355         const char *fmt0 = fmt;
356         int bufsize0 = bufsize;
357         tdb_log_func log_fn = tdb_log_fn(tdb);
358
359         va_start(ap, fmt);
360
361         while (*fmt) {
362                 switch ((c = *fmt++)) {
363                 case 'b': /* unsigned 8-bit integer */
364                         len = 1;
365                         bt = (uint8_t)va_arg(ap, int);
366                         if (bufsize && bufsize >= len)
367                                 SSVAL(buf, 0, bt);
368                         break;
369                 case 'w': /* unsigned 16-bit integer */
370                         len = 2;
371                         w = (uint16_t)va_arg(ap, int);
372                         if (bufsize && bufsize >= len)
373                                 SSVAL(buf, 0, w);
374                         break;
375                 case 'd': /* signed 32-bit integer (standard int in most systems) */
376                         len = 4;
377                         d = va_arg(ap, uint32_t);
378                         if (bufsize && bufsize >= len)
379                                 SIVAL(buf, 0, d);
380                         break;
381                 case 'p': /* pointer */
382                         len = 4;
383                         p = va_arg(ap, void *);
384                         d = p?1:0;
385                         if (bufsize && bufsize >= len)
386                                 SIVAL(buf, 0, d);
387                         break;
388                 case 'P': /* null-terminated string */
389                         s = va_arg(ap,char *);
390                         w = strlen(s);
391                         len = w + 1;
392                         if (bufsize && bufsize >= len)
393                                 memcpy(buf, s, len);
394                         break;
395                 case 'f': /* null-terminated string */
396                         s = va_arg(ap,char *);
397                         w = strlen(s);
398                         len = w + 1;
399                         if (bufsize && bufsize >= len)
400                                 memcpy(buf, s, len);
401                         break;
402                 case 'B': /* fixed-length string */
403                         i = va_arg(ap, int);
404                         s = va_arg(ap, char *);
405                         len = 4+i;
406                         if (bufsize && bufsize >= len) {
407                                 SIVAL(buf, 0, i);
408                                 memcpy(buf+4, s, i);
409                         }
410                         break;
411                 default:
412                         log_fn(tdb, 0,"Unknown tdb_pack format %c in %s\n", 
413                                c, fmt);
414                         len = 0;
415                         break;
416                 }
417
418                 buf += len;
419                 if (bufsize)
420                         bufsize -= len;
421                 if (bufsize < 0)
422                         bufsize = 0;
423         }
424
425         va_end(ap);
426
427         log_fn(tdb, 18,"tdb_pack(%s, %d) -> %d\n", 
428                fmt0, bufsize0, (int)PTR_DIFF(buf, buf0));
429         
430         return PTR_DIFF(buf, buf0);
431 }
432
433 /****************************************************************************
434  Useful pair of routines for packing/unpacking data consisting of
435  integers and strings.
436 ****************************************************************************/
437
438 int tdb_unpack(TDB_CONTEXT *tdb, char *buf, int bufsize, const char *fmt, ...)
439 {
440         va_list ap;
441         uint8_t *bt;
442         uint16_t *w;
443         uint32_t *d;
444         int len;
445         int *i;
446         void **p;
447         char *s, **b;
448         char c;
449         char *buf0 = buf;
450         const char *fmt0 = fmt;
451         int bufsize0 = bufsize;
452         tdb_log_func log_fn = tdb_log_fn(tdb);
453
454         va_start(ap, fmt);
455         
456         while (*fmt) {
457                 switch ((c=*fmt++)) {
458                 case 'b':
459                         len = 1;
460                         bt = va_arg(ap, uint8_t *);
461                         if (bufsize < len)
462                                 goto no_space;
463                         *bt = SVAL(buf, 0);
464                         break;
465                 case 'w':
466                         len = 2;
467                         w = va_arg(ap, uint16_t *);
468                         if (bufsize < len)
469                                 goto no_space;
470                         *w = SVAL(buf, 0);
471                         break;
472                 case 'd':
473                         len = 4;
474                         d = va_arg(ap, uint32_t *);
475                         if (bufsize < len)
476                                 goto no_space;
477                         *d = IVAL(buf, 0);
478                         break;
479                 case 'p':
480                         len = 4;
481                         p = va_arg(ap, void **);
482                         if (bufsize < len)
483                                 goto no_space;
484                         *p = (void *)IVAL(buf, 0);
485                         break;
486                 case 'P':
487                         s = va_arg(ap,char *);
488                         len = strlen(buf) + 1;
489                         if (bufsize < len || len > sizeof(pstring))
490                                 goto no_space;
491                         memcpy(s, buf, len);
492                         break;
493                 case 'f':
494                         s = va_arg(ap,char *);
495                         len = strlen(buf) + 1;
496                         if (bufsize < len || len > sizeof(fstring))
497                                 goto no_space;
498                         memcpy(s, buf, len);
499                         break;
500                 case 'B':
501                         i = va_arg(ap, int *);
502                         b = va_arg(ap, char **);
503                         len = 4;
504                         if (bufsize < len)
505                                 goto no_space;
506                         *i = IVAL(buf, 0);
507                         if (! *i) {
508                                 *b = NULL;
509                                 break;
510                         }
511                         len += *i;
512                         if (bufsize < len)
513                                 goto no_space;
514                         *b = (char *)malloc(*i);
515                         if (! *b)
516                                 goto no_space;
517                         memcpy(*b, buf+4, *i);
518                         break;
519                 default:
520                         log_fn(tdb, 0, "Unknown tdb_unpack format %c in %s\n", 
521                                c, fmt);
522
523                         len = 0;
524                         break;
525                 }
526
527                 buf += len;
528                 bufsize -= len;
529         }
530
531         va_end(ap);
532
533         log_fn(tdb, 18, "tdb_unpack(%s, %d) -> %d\n", 
534                fmt0, bufsize0, (int)PTR_DIFF(buf, buf0));
535
536         return PTR_DIFF(buf, buf0);
537
538  no_space:
539         return -1;
540 }