s3: in sys_popen(), fix a debug message
[kai/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
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                         if (*ps == NULL) {
241                                 goto no_space;
242                         }
243                         break;
244                 case 'f': /* null-terminated string */
245                         s = va_arg(ap,char *);
246                         len = strnlen((const char *)buf, bufsize) + 1;
247                         if (bufsize < len || len > sizeof(fstring))
248                                 goto no_space;
249                         memcpy(s, buf, len);
250                         break;
251                 case 'B': /* fixed-length string */
252                         i = va_arg(ap, int *);
253                         b = va_arg(ap, char **);
254                         len = 4;
255                         if (bufsize < len)
256                                 goto no_space;
257                         *i = IVAL(buf, 0);
258                         if (! *i) {
259                                 *b = NULL;
260                                 break;
261                         }
262                         len += *i;
263                         if (bufsize < len)
264                                 goto no_space;
265                         *b = (char *)SMB_MALLOC(*i);
266                         if (! *b)
267                                 goto no_space;
268                         memcpy(*b, buf+4, *i);
269                         break;
270                 default:
271                         DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
272                                  c, fmt));
273
274                         len = 0;
275                         break;
276                 }
277
278                 buf += len;
279                 bufsize -= len;
280         }
281
282         va_end(ap);
283
284         DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
285                  fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
286
287         return PTR_DIFF(buf, buf0);
288
289  no_space:
290         va_end(ap);
291         return -1;
292 }
293
294
295 /****************************************************************************
296  Log tdb messages via DEBUG().
297 ****************************************************************************/
298
299 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, const char *format, ...)
300 {
301         va_list ap;
302         char *ptr = NULL;
303         int ret;
304
305         va_start(ap, format);
306         ret = vasprintf(&ptr, format, ap);
307         va_end(ap);
308
309         if ((ret == -1) || !*ptr)
310                 return;
311
312         DEBUG((int)level, ("tdb(%s): %s", tdb_name(tdb) ? tdb_name(tdb) : "unnamed", ptr));
313         SAFE_FREE(ptr);
314 }
315
316 /****************************************************************************
317  Like tdb_open() but also setup a logging function that redirects to
318  the samba DEBUG() system.
319 ****************************************************************************/
320
321 TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
322                           int open_flags, mode_t mode)
323 {
324         TDB_CONTEXT *tdb;
325
326         if (!lp_use_mmap())
327                 tdb_flags |= TDB_NOMMAP;
328
329         if ((hash_size == 0) && (name != NULL)) {
330                 const char *base = strrchr_m(name, '/');
331                 if (base != NULL) {
332                         base += 1;
333                 }
334                 else {
335                         base = name;
336                 }
337                 hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
338         }
339
340         tdb = tdb_open_compat(name, hash_size, tdb_flags,
341                               open_flags, mode, tdb_log, NULL);
342         if (!tdb)
343                 return NULL;
344
345         return tdb;
346 }
347
348 /****************************************************************************
349  tdb_store, wrapped in a transaction. This way we make sure that a process
350  that dies within writing does not leave a corrupt tdb behind.
351 ****************************************************************************/
352
353 int tdb_trans_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
354                     int flag)
355 {
356         int res;
357
358         if ((res = tdb_transaction_start(tdb)) != 0) {
359                 DEBUG(5, ("tdb_transaction_start failed\n"));
360                 return res;
361         }
362
363         if ((res = tdb_store(tdb, key, dbuf, flag)) != 0) {
364                 DEBUG(10, ("tdb_store failed\n"));
365                 tdb_transaction_cancel(tdb);
366                 return res;
367         }
368
369         if ((res = tdb_transaction_commit(tdb)) != 0) {
370                 DEBUG(5, ("tdb_transaction_commit failed\n"));
371         }
372
373         return res;
374 }
375
376 /****************************************************************************
377  tdb_delete, wrapped in a transaction. This way we make sure that a process
378  that dies within deleting does not leave a corrupt tdb behind.
379 ****************************************************************************/
380
381 int tdb_trans_delete(struct tdb_context *tdb, TDB_DATA key)
382 {
383         int res;
384
385         if ((res = tdb_transaction_start(tdb)) != 0) {
386                 DEBUG(5, ("tdb_transaction_start failed\n"));
387                 return res;
388         }
389
390         if ((res = tdb_delete(tdb, key)) != 0) {
391                 DEBUG(10, ("tdb_delete failed\n"));
392                 tdb_transaction_cancel(tdb);
393                 return res;
394         }
395
396         if ((res = tdb_transaction_commit(tdb)) != 0) {
397                 DEBUG(5, ("tdb_transaction_commit failed\n"));
398         }
399
400         return res;
401 }
402
403 int tdb_data_cmp(TDB_DATA t1, TDB_DATA t2)
404 {
405         int ret;
406         if (t1.dptr == NULL && t2.dptr != NULL) {
407                 return -1;
408         }
409         if (t1.dptr != NULL && t2.dptr == NULL) {
410                 return 1;
411         }
412         if (t1.dptr == t2.dptr) {
413                 return t1.dsize - t2.dsize;
414         }
415         ret = memcmp(t1.dptr, t2.dptr, MIN(t1.dsize, t2.dsize));
416         if (ret == 0) {
417                 return t1.dsize - t2.dsize;
418         }
419         return ret;
420 }