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 | bool tdb_pack_append(TALLOC_CTX *mem_ctx, uint8_t **buf, size_t *len,
|
---|
144 | const char *fmt, ...)
|
---|
145 | {
|
---|
146 | va_list ap;
|
---|
147 | size_t len1, len2;
|
---|
148 |
|
---|
149 | va_start(ap, fmt);
|
---|
150 | len1 = tdb_pack_va(NULL, 0, fmt, ap);
|
---|
151 | va_end(ap);
|
---|
152 |
|
---|
153 | if (mem_ctx != NULL) {
|
---|
154 | *buf = talloc_realloc(mem_ctx, *buf, uint8_t,
|
---|
155 | (*len) + len1);
|
---|
156 | } else {
|
---|
157 | *buf = SMB_REALLOC_ARRAY(*buf, uint8_t, (*len) + len1);
|
---|
158 | }
|
---|
159 |
|
---|
160 | if (*buf == NULL) {
|
---|
161 | return False;
|
---|
162 | }
|
---|
163 |
|
---|
164 | va_start(ap, fmt);
|
---|
165 | len2 = tdb_pack_va((*buf)+(*len), len1, fmt, ap);
|
---|
166 | va_end(ap);
|
---|
167 |
|
---|
168 | if (len1 != len2) {
|
---|
169 | return False;
|
---|
170 | }
|
---|
171 |
|
---|
172 | *len += len2;
|
---|
173 |
|
---|
174 | return True;
|
---|
175 | }
|
---|
176 |
|
---|
177 | /****************************************************************************
|
---|
178 | Useful pair of routines for packing/unpacking data consisting of
|
---|
179 | integers and strings.
|
---|
180 | ****************************************************************************/
|
---|
181 |
|
---|
182 | int tdb_unpack(const uint8_t *buf, int bufsize, const char *fmt, ...)
|
---|
183 | {
|
---|
184 | va_list ap;
|
---|
185 | uint8_t *bt;
|
---|
186 | uint16_t *w;
|
---|
187 | uint32_t *d;
|
---|
188 | int len;
|
---|
189 | int *i;
|
---|
190 | void **p;
|
---|
191 | char *s, **b, **ps;
|
---|
192 | char c;
|
---|
193 | const uint8_t *buf0 = buf;
|
---|
194 | const char *fmt0 = fmt;
|
---|
195 | int bufsize0 = bufsize;
|
---|
196 |
|
---|
197 | va_start(ap, fmt);
|
---|
198 |
|
---|
199 | while (*fmt) {
|
---|
200 | switch ((c=*fmt++)) {
|
---|
201 | case 'b': /* unsigned 8-bit integer */
|
---|
202 | len = 1;
|
---|
203 | bt = va_arg(ap, uint8_t *);
|
---|
204 | if (bufsize < len)
|
---|
205 | goto no_space;
|
---|
206 | *bt = SVAL(buf, 0);
|
---|
207 | break;
|
---|
208 | case 'w': /* unsigned 16-bit integer */
|
---|
209 | len = 2;
|
---|
210 | w = va_arg(ap, uint16_t *);
|
---|
211 | if (bufsize < len)
|
---|
212 | goto no_space;
|
---|
213 | *w = SVAL(buf, 0);
|
---|
214 | break;
|
---|
215 | case 'd': /* unsigned 32-bit integer (standard int in most systems) */
|
---|
216 | len = 4;
|
---|
217 | d = va_arg(ap, uint32_t *);
|
---|
218 | if (bufsize < len)
|
---|
219 | goto no_space;
|
---|
220 | *d = IVAL(buf, 0);
|
---|
221 | break;
|
---|
222 | case 'p': /* pointer */
|
---|
223 | len = 4;
|
---|
224 | p = va_arg(ap, void **);
|
---|
225 | if (bufsize < len)
|
---|
226 | goto no_space;
|
---|
227 | /*
|
---|
228 | * This isn't a real pointer - only a token (1 or 0)
|
---|
229 | * to mark the fact a pointer is present.
|
---|
230 | */
|
---|
231 |
|
---|
232 | *p = (void *)(IVAL(buf, 0) ? (void *)1 : NULL);
|
---|
233 | break;
|
---|
234 | case 'P': /* null-terminated string */
|
---|
235 | /* Return malloc'ed string. */
|
---|
236 | ps = va_arg(ap,char **);
|
---|
237 | len = strnlen((const char *)buf, bufsize) + 1;
|
---|
238 | if (bufsize < len)
|
---|
239 | goto no_space;
|
---|
240 | *ps = SMB_STRDUP((const char *)buf);
|
---|
241 | if (*ps == NULL) {
|
---|
242 | goto no_space;
|
---|
243 | }
|
---|
244 | break;
|
---|
245 | case 'f': /* null-terminated string */
|
---|
246 | s = va_arg(ap,char *);
|
---|
247 | len = strnlen((const char *)buf, bufsize) + 1;
|
---|
248 | if (bufsize < len || len > sizeof(fstring))
|
---|
249 | goto no_space;
|
---|
250 | memcpy(s, buf, len);
|
---|
251 | break;
|
---|
252 | case 'B': /* fixed-length string */
|
---|
253 | i = va_arg(ap, int *);
|
---|
254 | b = va_arg(ap, char **);
|
---|
255 | len = 4;
|
---|
256 | if (bufsize < len)
|
---|
257 | goto no_space;
|
---|
258 | *i = IVAL(buf, 0);
|
---|
259 | if (! *i) {
|
---|
260 | *b = NULL;
|
---|
261 | break;
|
---|
262 | }
|
---|
263 | len += *i;
|
---|
264 | if (bufsize < len)
|
---|
265 | goto no_space;
|
---|
266 | *b = (char *)SMB_MALLOC(*i);
|
---|
267 | if (! *b)
|
---|
268 | goto no_space;
|
---|
269 | memcpy(*b, buf+4, *i);
|
---|
270 | break;
|
---|
271 | default:
|
---|
272 | DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
|
---|
273 | c, fmt));
|
---|
274 |
|
---|
275 | len = 0;
|
---|
276 | break;
|
---|
277 | }
|
---|
278 |
|
---|
279 | buf += len;
|
---|
280 | bufsize -= len;
|
---|
281 | }
|
---|
282 |
|
---|
283 | va_end(ap);
|
---|
284 |
|
---|
285 | DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
|
---|
286 | fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
|
---|
287 |
|
---|
288 | return PTR_DIFF(buf, buf0);
|
---|
289 |
|
---|
290 | no_space:
|
---|
291 | va_end(ap);
|
---|
292 | return -1;
|
---|
293 | }
|
---|
294 |
|
---|
295 |
|
---|
296 | /****************************************************************************
|
---|
297 | Log tdb messages via DEBUG().
|
---|
298 | ****************************************************************************/
|
---|
299 |
|
---|
300 | static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, const char *format, ...)
|
---|
301 | {
|
---|
302 | va_list ap;
|
---|
303 | char *ptr = NULL;
|
---|
304 | int ret;
|
---|
305 |
|
---|
306 | va_start(ap, format);
|
---|
307 | ret = vasprintf(&ptr, format, ap);
|
---|
308 | va_end(ap);
|
---|
309 |
|
---|
310 | if ((ret == -1) || !*ptr)
|
---|
311 | return;
|
---|
312 |
|
---|
313 | DEBUG((int)level, ("tdb(%s): %s", tdb_name(tdb) ? tdb_name(tdb) : "unnamed", ptr));
|
---|
314 | SAFE_FREE(ptr);
|
---|
315 | }
|
---|
316 |
|
---|
317 | /****************************************************************************
|
---|
318 | Like tdb_open() but also setup a logging function that redirects to
|
---|
319 | the samba DEBUG() system.
|
---|
320 | ****************************************************************************/
|
---|
321 |
|
---|
322 | TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
|
---|
323 | int open_flags, mode_t mode)
|
---|
324 | {
|
---|
325 | TDB_CONTEXT *tdb;
|
---|
326 | struct tdb_logging_context log_ctx = { .log_fn = tdb_log };
|
---|
327 |
|
---|
328 | if (!lp_use_mmap())
|
---|
329 | tdb_flags |= TDB_NOMMAP;
|
---|
330 |
|
---|
331 | if ((hash_size == 0) && (name != NULL)) {
|
---|
332 | const char *base = strrchr_m(name, '/');
|
---|
333 | if (base != NULL) {
|
---|
334 | base += 1;
|
---|
335 | }
|
---|
336 | else {
|
---|
337 | base = name;
|
---|
338 | }
|
---|
339 | hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
|
---|
340 | }
|
---|
341 |
|
---|
342 | tdb = tdb_open_ex(name, hash_size, tdb_flags,
|
---|
343 | open_flags, mode, &log_ctx, NULL);
|
---|
344 | if (!tdb)
|
---|
345 | return NULL;
|
---|
346 |
|
---|
347 | return tdb;
|
---|
348 | }
|
---|
349 |
|
---|
350 | /****************************************************************************
|
---|
351 | tdb_store, wrapped in a transaction. This way we make sure that a process
|
---|
352 | that dies within writing does not leave a corrupt tdb behind.
|
---|
353 | ****************************************************************************/
|
---|
354 |
|
---|
355 | int tdb_trans_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
|
---|
356 | int flag)
|
---|
357 | {
|
---|
358 | int res;
|
---|
359 |
|
---|
360 | if ((res = tdb_transaction_start(tdb)) != 0) {
|
---|
361 | DEBUG(5, ("tdb_transaction_start failed\n"));
|
---|
362 | return res;
|
---|
363 | }
|
---|
364 |
|
---|
365 | if ((res = tdb_store(tdb, key, dbuf, flag)) != 0) {
|
---|
366 | DEBUG(10, ("tdb_store failed\n"));
|
---|
367 | tdb_transaction_cancel(tdb);
|
---|
368 | return res;
|
---|
369 | }
|
---|
370 |
|
---|
371 | if ((res = tdb_transaction_commit(tdb)) != 0) {
|
---|
372 | DEBUG(5, ("tdb_transaction_commit failed\n"));
|
---|
373 | }
|
---|
374 |
|
---|
375 | return res;
|
---|
376 | }
|
---|
377 |
|
---|
378 | /****************************************************************************
|
---|
379 | tdb_delete, wrapped in a transaction. This way we make sure that a process
|
---|
380 | that dies within deleting does not leave a corrupt tdb behind.
|
---|
381 | ****************************************************************************/
|
---|
382 |
|
---|
383 | int tdb_trans_delete(struct tdb_context *tdb, TDB_DATA key)
|
---|
384 | {
|
---|
385 | int res;
|
---|
386 |
|
---|
387 | if ((res = tdb_transaction_start(tdb)) != 0) {
|
---|
388 | DEBUG(5, ("tdb_transaction_start failed\n"));
|
---|
389 | return res;
|
---|
390 | }
|
---|
391 |
|
---|
392 | if ((res = tdb_delete(tdb, key)) != 0) {
|
---|
393 | DEBUG(10, ("tdb_delete failed\n"));
|
---|
394 | tdb_transaction_cancel(tdb);
|
---|
395 | return res;
|
---|
396 | }
|
---|
397 |
|
---|
398 | if ((res = tdb_transaction_commit(tdb)) != 0) {
|
---|
399 | DEBUG(5, ("tdb_transaction_commit failed\n"));
|
---|
400 | }
|
---|
401 |
|
---|
402 | return res;
|
---|
403 | }
|
---|
404 |
|
---|
405 | int tdb_data_cmp(TDB_DATA t1, TDB_DATA t2)
|
---|
406 | {
|
---|
407 | int ret;
|
---|
408 | if (t1.dptr == NULL && t2.dptr != NULL) {
|
---|
409 | return -1;
|
---|
410 | }
|
---|
411 | if (t1.dptr != NULL && t2.dptr == NULL) {
|
---|
412 | return 1;
|
---|
413 | }
|
---|
414 | if (t1.dptr == t2.dptr) {
|
---|
415 | return t1.dsize - t2.dsize;
|
---|
416 | }
|
---|
417 | ret = memcmp(t1.dptr, t2.dptr, MIN(t1.dsize, t2.dsize));
|
---|
418 | if (ret == 0) {
|
---|
419 | return t1.dsize - t2.dsize;
|
---|
420 | }
|
---|
421 | return ret;
|
---|
422 | }
|
---|
423 |
|
---|
424 | char *tdb_data_string(TALLOC_CTX *mem_ctx, TDB_DATA d)
|
---|
425 | {
|
---|
426 | int len;
|
---|
427 | char *ret = NULL;
|
---|
428 | cbuf *ost = cbuf_new(mem_ctx);
|
---|
429 |
|
---|
430 | if (ost == NULL) {
|
---|
431 | return NULL;
|
---|
432 | }
|
---|
433 |
|
---|
434 | len = cbuf_printf(ost, "%d:");
|
---|
435 | if (len == -1) {
|
---|
436 | goto done;
|
---|
437 | }
|
---|
438 |
|
---|
439 | if (d.dptr == NULL) {
|
---|
440 | len = cbuf_puts(ost, "<NULL>", -1);
|
---|
441 | } else {
|
---|
442 | len = cbuf_print_quoted(ost, (const char*)d.dptr, d.dsize);
|
---|
443 | }
|
---|
444 | if (len == -1) {
|
---|
445 | goto done;
|
---|
446 | }
|
---|
447 |
|
---|
448 | cbuf_swapptr(ost, &ret, 0);
|
---|
449 | talloc_steal(mem_ctx, ret);
|
---|
450 |
|
---|
451 | done:
|
---|
452 | talloc_free(ost);
|
---|
453 | return ret;
|
---|
454 | }
|
---|
455 |
|
---|
456 | static sig_atomic_t gotalarm;
|
---|
457 |
|
---|
458 | /***************************************************************
|
---|
459 | Signal function to tell us we timed out.
|
---|
460 | ****************************************************************/
|
---|
461 |
|
---|
462 | static void gotalarm_sig(int signum)
|
---|
463 | {
|
---|
464 | gotalarm = 1;
|
---|
465 | }
|
---|
466 |
|
---|
467 | /****************************************************************************
|
---|
468 | Lock a chain with timeout (in seconds).
|
---|
469 | ****************************************************************************/
|
---|
470 |
|
---|
471 | static int tdb_chainlock_with_timeout_internal( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout, int rw_type)
|
---|
472 | {
|
---|
473 | /* Allow tdb_chainlock to be interrupted by an alarm. */
|
---|
474 | int ret;
|
---|
475 | gotalarm = 0;
|
---|
476 |
|
---|
477 | if (timeout) {
|
---|
478 | CatchSignal(SIGALRM, gotalarm_sig);
|
---|
479 | tdb_setalarm_sigptr(tdb, &gotalarm);
|
---|
480 | alarm(timeout);
|
---|
481 | }
|
---|
482 |
|
---|
483 | if (rw_type == F_RDLCK)
|
---|
484 | ret = tdb_chainlock_read(tdb, key);
|
---|
485 | else
|
---|
486 | ret = tdb_chainlock(tdb, key);
|
---|
487 |
|
---|
488 | if (timeout) {
|
---|
489 | alarm(0);
|
---|
490 | tdb_setalarm_sigptr(tdb, NULL);
|
---|
491 | CatchSignal(SIGALRM, SIG_IGN);
|
---|
492 | if (gotalarm && (ret != 0)) {
|
---|
493 | DEBUG(0,("tdb_chainlock_with_timeout_internal: alarm (%u) timed out for key %s in tdb %s\n",
|
---|
494 | timeout, key.dptr, tdb_name(tdb)));
|
---|
495 | /* TODO: If we time out waiting for a lock, it might
|
---|
496 | * be nice to use F_GETLK to get the pid of the
|
---|
497 | * process currently holding the lock and print that
|
---|
498 | * as part of the debugging message. -- mbp */
|
---|
499 | return -1;
|
---|
500 | }
|
---|
501 | }
|
---|
502 |
|
---|
503 | return ret == 0 ? 0 : -1;
|
---|
504 | }
|
---|
505 |
|
---|
506 | /****************************************************************************
|
---|
507 | Write lock a chain. Return non-zero if timeout or lock failed.
|
---|
508 | ****************************************************************************/
|
---|
509 |
|
---|
510 | int tdb_chainlock_with_timeout( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout)
|
---|
511 | {
|
---|
512 | return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_WRLCK);
|
---|
513 | }
|
---|
514 |
|
---|
515 | int tdb_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval,
|
---|
516 | int timeout)
|
---|
517 | {
|
---|
518 | TDB_DATA key = string_term_tdb_data(keyval);
|
---|
519 |
|
---|
520 | return tdb_chainlock_with_timeout(tdb, key, timeout);
|
---|
521 | }
|
---|
522 |
|
---|
523 | /****************************************************************************
|
---|
524 | Read lock a chain by string. Return non-zero if timeout or lock failed.
|
---|
525 | ****************************************************************************/
|
---|
526 |
|
---|
527 | int tdb_read_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval, unsigned int timeout)
|
---|
528 | {
|
---|
529 | TDB_DATA key = string_term_tdb_data(keyval);
|
---|
530 |
|
---|
531 | return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_RDLCK);
|
---|
532 | }
|
---|