s3:smbd: make use of smbXsrv_tcon and smbXsrv_session for smb2
[Samba/gebeck_regimport.git] / source3 / lib / util_tdb.c
blobc6c6d262772c567260306fb5b2fa07474808e10d
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
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.
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.
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/>.
22 #include "includes.h"
23 #include "system/filesys.h"
24 #include "util_tdb.h"
26 #undef malloc
27 #undef realloc
28 #undef calloc
29 #undef strdup
31 /* these are little tdb utility functions that are meant to make
32 dealing with a tdb database a little less cumbersome in Samba */
34 int tdb_trans_store_bystring(TDB_CONTEXT *tdb, const char *keystr,
35 TDB_DATA data, int flags)
37 TDB_DATA key = string_term_tdb_data(keystr);
39 return tdb_trans_store(tdb, key, data, flags);
42 /****************************************************************************
43 Useful pair of routines for packing/unpacking data consisting of
44 integers and strings.
45 ****************************************************************************/
47 static size_t tdb_pack_va(uint8 *buf, int bufsize, const char *fmt, va_list ap)
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;
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);
110 break;
111 default:
112 DEBUG(0,("Unknown tdb_pack format %c in %s\n",
113 c, fmt));
114 len = 0;
115 break;
118 buf += len;
119 if (bufsize)
120 bufsize -= len;
121 if (bufsize < 0)
122 bufsize = 0;
125 DEBUG(18,("tdb_pack_va(%s, %d) -> %d\n",
126 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
128 return PTR_DIFF(buf, buf0);
131 size_t tdb_pack(uint8 *buf, int bufsize, const char *fmt, ...)
133 va_list ap;
134 size_t result;
136 va_start(ap, fmt);
137 result = tdb_pack_va(buf, bufsize, fmt, ap);
138 va_end(ap);
139 return result;
142 bool tdb_pack_append(TALLOC_CTX *mem_ctx, uint8 **buf, size_t *len,
143 const char *fmt, ...)
145 va_list ap;
146 size_t len1, len2;
148 va_start(ap, fmt);
149 len1 = tdb_pack_va(NULL, 0, fmt, ap);
150 va_end(ap);
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);
159 if (*buf == NULL) {
160 return False;
163 va_start(ap, fmt);
164 len2 = tdb_pack_va((*buf)+(*len), len1, fmt, ap);
165 va_end(ap);
167 if (len1 != len2) {
168 return False;
171 *len += len2;
173 return True;
176 /****************************************************************************
177 Useful pair of routines for packing/unpacking data consisting of
178 integers and strings.
179 ****************************************************************************/
181 int tdb_unpack(const uint8 *buf, int bufsize, const char *fmt, ...)
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;
196 va_start(ap, fmt);
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;
227 * This isn't a real pointer - only a token (1 or 0)
228 * to mark the fact a pointer is present.
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;
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));
271 len = 0;
272 break;
275 buf += len;
276 bufsize -= len;
279 va_end(ap);
281 DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
282 fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
284 return PTR_DIFF(buf, buf0);
286 no_space:
287 va_end(ap);
288 return -1;
292 /****************************************************************************
293 Log tdb messages via DEBUG().
294 ****************************************************************************/
296 static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, const char *format, ...)
298 va_list ap;
299 char *ptr = NULL;
300 int ret;
302 va_start(ap, format);
303 ret = vasprintf(&ptr, format, ap);
304 va_end(ap);
306 if ((ret == -1) || !*ptr)
307 return;
309 DEBUG((int)level, ("tdb(%s): %s", tdb_name(tdb) ? tdb_name(tdb) : "unnamed", ptr));
310 SAFE_FREE(ptr);
313 /****************************************************************************
314 Like tdb_open() but also setup a logging function that redirects to
315 the samba DEBUG() system.
316 ****************************************************************************/
318 TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
319 int open_flags, mode_t mode)
321 TDB_CONTEXT *tdb;
323 if (!lp_use_mmap())
324 tdb_flags |= TDB_NOMMAP;
326 if ((hash_size == 0) && (name != NULL)) {
327 const char *base = strrchr_m(name, '/');
328 if (base != NULL) {
329 base += 1;
331 else {
332 base = name;
334 hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
337 tdb = tdb_open_compat(name, hash_size, tdb_flags,
338 open_flags, mode, tdb_log, NULL);
339 if (!tdb)
340 return NULL;
342 return tdb;
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 ****************************************************************************/
350 int tdb_trans_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
351 int flag)
353 int res;
355 if ((res = tdb_transaction_start(tdb)) != 0) {
356 DEBUG(5, ("tdb_transaction_start failed\n"));
357 return res;
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;
366 if ((res = tdb_transaction_commit(tdb)) != 0) {
367 DEBUG(5, ("tdb_transaction_commit failed\n"));
370 return res;
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 ****************************************************************************/
378 int tdb_trans_delete(struct tdb_context *tdb, TDB_DATA key)
380 int res;
382 if ((res = tdb_transaction_start(tdb)) != 0) {
383 DEBUG(5, ("tdb_transaction_start failed\n"));
384 return res;
387 if ((res = tdb_delete(tdb, key)) != 0) {
388 DEBUG(10, ("tdb_delete failed\n"));
389 tdb_transaction_cancel(tdb);
390 return res;
393 if ((res = tdb_transaction_commit(tdb)) != 0) {
394 DEBUG(5, ("tdb_transaction_commit failed\n"));
397 return res;
400 int tdb_data_cmp(TDB_DATA t1, TDB_DATA t2)
402 int ret;
403 if (t1.dptr == NULL && t2.dptr != NULL) {
404 return -1;
406 if (t1.dptr != NULL && t2.dptr == NULL) {
407 return 1;
409 if (t1.dptr == t2.dptr) {
410 return t1.dsize - t2.dsize;
412 ret = memcmp(t1.dptr, t2.dptr, MIN(t1.dsize, t2.dsize));
413 if (ret == 0) {
414 return t1.dsize - t2.dsize;
416 return ret;