c6c6d262772c567260306fb5b2fa07474808e10d
[metze/samba/wip.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
26 #undef malloc
27 #undef realloc
28 #undef calloc
29 #undef strdup
30
31 /* these are little tdb utility functions that are meant to make
32    dealing with a tdb database a little less cumbersome in Samba */
33
34 int tdb_trans_store_bystring(TDB_CONTEXT *tdb, const char *keystr,
35                              TDB_DATA data, int flags)
36 {
37         TDB_DATA key = string_term_tdb_data(keystr);
38
39         return tdb_trans_store(tdb, key, data, flags);
40 }
41
42 /****************************************************************************
43  Useful pair of routines for packing/unpacking data consisting of
44  integers and strings.
45 ****************************************************************************/
46
47 static size_t tdb_pack_va(uint8 *buf, int bufsize, const char *fmt, va_list ap)
48 {
49         uint8 bt;
50         uint16 w;
51         uint32 d;
52         int i;
53         void *p;
54         int len;
55         char *s;
56         char c;
57         uint8 *buf0 = buf;
58         const char *fmt0 = fmt;
59         int bufsize0 = bufsize;
60
61         while (*fmt) {
62                 switch ((c = *fmt++)) {
63                 case 'b': /* unsigned 8-bit integer */
64                         len = 1;
65                         bt = (uint8)va_arg(ap, int);
66                         if (bufsize && bufsize >= len)
67                                 SSVAL(buf, 0, bt);
68                         break;
69                 case 'w': /* unsigned 16-bit integer */
70                         len = 2;
71                         w = (uint16)va_arg(ap, int);
72                         if (bufsize && bufsize >= len)
73                                 SSVAL(buf, 0, w);
74                         break;
75                 case 'd': /* signed 32-bit integer (standard int in most systems) */
76                         len = 4;
77                         d = va_arg(ap, uint32);
78                         if (bufsize && bufsize >= len)
79                                 SIVAL(buf, 0, d);
80                         break;
81                 case 'p': /* pointer */
82                         len = 4;
83                         p = va_arg(ap, void *);
84                         d = p?1:0;
85                         if (bufsize && bufsize >= len)
86                                 SIVAL(buf, 0, d);
87                         break;
88                 case 'P': /* null-terminated string */
89                         s = va_arg(ap,char *);
90                         w = strlen(s);
91                         len = w + 1;
92                         if (bufsize && bufsize >= len)
93                                 memcpy(buf, s, len);
94                         break;
95                 case 'f': /* null-terminated string */
96                         s = va_arg(ap,char *);
97                         w = strlen(s);
98                         len = w + 1;
99                         if (bufsize && bufsize >= len)
100                                 memcpy(buf, s, len);
101                         break;
102                 case 'B': /* fixed-length string */
103                         i = va_arg(ap, int);
104                         s = va_arg(ap, char *);
105                         len = 4+i;
106                         if (bufsize && bufsize >= len) {
107                                 SIVAL(buf, 0, i);
108                                 memcpy(buf+4, s, i);
109                         }
110                         break;
111                 default:
112                         DEBUG(0,("Unknown tdb_pack format %c in %s\n", 
113                                  c, fmt));
114                         len = 0;
115                         break;
116                 }
117
118                 buf += len;
119                 if (bufsize)
120                         bufsize -= len;
121                 if (bufsize < 0)
122                         bufsize = 0;
123         }
124
125         DEBUG(18,("tdb_pack_va(%s, %d) -> %d\n", 
126                  fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
127
128         return PTR_DIFF(buf, buf0);
129 }
130
131 size_t tdb_pack(uint8 *buf, int bufsize, const char *fmt, ...)
132 {
133         va_list ap;
134         size_t result;
135
136         va_start(ap, fmt);
137         result = tdb_pack_va(buf, bufsize, fmt, ap);
138         va_end(ap);
139         return result;
140 }
141
142 bool tdb_pack_append(TALLOC_CTX *mem_ctx, uint8 **buf, size_t *len,
143                      const char *fmt, ...)
144 {
145         va_list ap;
146         size_t len1, len2;
147
148         va_start(ap, fmt);
149         len1 = tdb_pack_va(NULL, 0, fmt, ap);
150         va_end(ap);
151
152         if (mem_ctx != NULL) {
153                 *buf = talloc_realloc(mem_ctx, *buf, uint8,
154                                             (*len) + len1);
155         } else {
156                 *buf = SMB_REALLOC_ARRAY(*buf, uint8, (*len) + len1);
157         }
158
159         if (*buf == NULL) {
160                 return False;
161         }
162
163         va_start(ap, fmt);
164         len2 = tdb_pack_va((*buf)+(*len), len1, fmt, ap);
165         va_end(ap);
166
167         if (len1 != len2) {
168                 return False;
169         }
170
171         *len += len2;
172
173         return True;
174 }
175
176 /****************************************************************************
177  Useful pair of routines for packing/unpacking data consisting of
178  integers and strings.
179 ****************************************************************************/
180
181 int tdb_unpack(const uint8 *buf, int bufsize, const char *fmt, ...)
182 {
183         va_list ap;
184         uint8 *bt;
185         uint16 *w;
186         uint32 *d;
187         int len;
188         int *i;
189         void **p;
190         char *s, **b, **ps;
191         char c;
192         const uint8 *buf0 = buf;
193         const char *fmt0 = fmt;
194         int bufsize0 = bufsize;
195
196         va_start(ap, fmt);
197
198         while (*fmt) {
199                 switch ((c=*fmt++)) {
200                 case 'b': /* unsigned 8-bit integer */
201                         len = 1;
202                         bt = va_arg(ap, uint8 *);
203                         if (bufsize < len)
204                                 goto no_space;
205                         *bt = SVAL(buf, 0);
206                         break;
207                 case 'w': /* unsigned 16-bit integer */
208                         len = 2;
209                         w = va_arg(ap, uint16 *);
210                         if (bufsize < len)
211                                 goto no_space;
212                         *w = SVAL(buf, 0);
213                         break;
214                 case 'd': /* unsigned 32-bit integer (standard int in most systems) */
215                         len = 4;
216                         d = va_arg(ap, uint32 *);
217                         if (bufsize < len)
218                                 goto no_space;
219                         *d = IVAL(buf, 0);
220                         break;
221                 case 'p': /* pointer */
222                         len = 4;
223                         p = va_arg(ap, void **);
224                         if (bufsize < len)
225                                 goto no_space;
226                         /*
227                          * This isn't a real pointer - only a token (1 or 0)
228                          * to mark the fact a pointer is present.
229                          */
230
231                         *p = (void *)(IVAL(buf, 0) ? (void *)1 : NULL);
232                         break;
233                 case 'P': /* null-terminated string */
234                         /* Return malloc'ed string. */
235                         ps = va_arg(ap,char **);
236                         len = strnlen((const char *)buf, bufsize) + 1;
237                         if (bufsize < len)
238                                 goto no_space;
239                         *ps = SMB_STRDUP((const char *)buf);
240                         break;
241                 case 'f': /* null-terminated string */
242                         s = va_arg(ap,char *);
243                         len = strnlen((const char *)buf, bufsize) + 1;
244                         if (bufsize < len || len > sizeof(fstring))
245                                 goto no_space;
246                         memcpy(s, buf, len);
247                         break;
248                 case 'B': /* fixed-length string */
249                         i = va_arg(ap, int *);
250                         b = va_arg(ap, char **);
251                         len = 4;
252                         if (bufsize < len)
253                                 goto no_space;
254                         *i = IVAL(buf, 0);
255                         if (! *i) {
256                                 *b = NULL;
257                                 break;
258                         }
259                         len += *i;
260                         if (bufsize < len)
261                                 goto no_space;
262                         *b = (char *)SMB_MALLOC(*i);
263                         if (! *b)
264                                 goto no_space;
265                         memcpy(*b, buf+4, *i);
266                         break;
267                 default:
268                         DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
269                                  c, fmt));
270
271                         len = 0;
272                         break;
273                 }
274
275                 buf += len;
276                 bufsize -= len;
277         }
278
279         va_end(ap);
280
281         DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
282                  fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
283
284         return PTR_DIFF(buf, buf0);
285
286  no_space:
287         va_end(ap);
288         return -1;
289 }
290
291
292 /****************************************************************************
293  Log tdb messages via DEBUG().
294 ****************************************************************************/
295
296 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, const char *format, ...)
297 {
298         va_list ap;
299         char *ptr = NULL;
300         int ret;
301
302         va_start(ap, format);
303         ret = vasprintf(&ptr, format, ap);
304         va_end(ap);
305
306         if ((ret == -1) || !*ptr)
307                 return;
308
309         DEBUG((int)level, ("tdb(%s): %s", tdb_name(tdb) ? tdb_name(tdb) : "unnamed", ptr));
310         SAFE_FREE(ptr);
311 }
312
313 /****************************************************************************
314  Like tdb_open() but also setup a logging function that redirects to
315  the samba DEBUG() system.
316 ****************************************************************************/
317
318 TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
319                           int open_flags, mode_t mode)
320 {
321         TDB_CONTEXT *tdb;
322
323         if (!lp_use_mmap())
324                 tdb_flags |= TDB_NOMMAP;
325
326         if ((hash_size == 0) && (name != NULL)) {
327                 const char *base = strrchr_m(name, '/');
328                 if (base != NULL) {
329                         base += 1;
330                 }
331                 else {
332                         base = name;
333                 }
334                 hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
335         }
336
337         tdb = tdb_open_compat(name, hash_size, tdb_flags,
338                               open_flags, mode, tdb_log, NULL);
339         if (!tdb)
340                 return NULL;
341
342         return tdb;
343 }
344
345 /****************************************************************************
346  tdb_store, wrapped in a transaction. This way we make sure that a process
347  that dies within writing does not leave a corrupt tdb behind.
348 ****************************************************************************/
349
350 int tdb_trans_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
351                     int flag)
352 {
353         int res;
354
355         if ((res = tdb_transaction_start(tdb)) != 0) {
356                 DEBUG(5, ("tdb_transaction_start failed\n"));
357                 return res;
358         }
359
360         if ((res = tdb_store(tdb, key, dbuf, flag)) != 0) {
361                 DEBUG(10, ("tdb_store failed\n"));
362                 tdb_transaction_cancel(tdb);
363                 return res;
364         }
365
366         if ((res = tdb_transaction_commit(tdb)) != 0) {
367                 DEBUG(5, ("tdb_transaction_commit failed\n"));
368         }
369
370         return res;
371 }
372
373 /****************************************************************************
374  tdb_delete, wrapped in a transaction. This way we make sure that a process
375  that dies within deleting does not leave a corrupt tdb behind.
376 ****************************************************************************/
377
378 int tdb_trans_delete(struct tdb_context *tdb, TDB_DATA key)
379 {
380         int res;
381
382         if ((res = tdb_transaction_start(tdb)) != 0) {
383                 DEBUG(5, ("tdb_transaction_start failed\n"));
384                 return res;
385         }
386
387         if ((res = tdb_delete(tdb, key)) != 0) {
388                 DEBUG(10, ("tdb_delete failed\n"));
389                 tdb_transaction_cancel(tdb);
390                 return res;
391         }
392
393         if ((res = tdb_transaction_commit(tdb)) != 0) {
394                 DEBUG(5, ("tdb_transaction_commit failed\n"));
395         }
396
397         return res;
398 }
399
400 int tdb_data_cmp(TDB_DATA t1, TDB_DATA t2)
401 {
402         int ret;
403         if (t1.dptr == NULL && t2.dptr != NULL) {
404                 return -1;
405         }
406         if (t1.dptr != NULL && t2.dptr == NULL) {
407                 return 1;
408         }
409         if (t1.dptr == t2.dptr) {
410                 return t1.dsize - t2.dsize;
411         }
412         ret = memcmp(t1.dptr, t2.dptr, MIN(t1.dsize, t2.dsize));
413         if (ret == 0) {
414                 return t1.dsize - t2.dsize;
415         }
416         return ret;
417 }