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 | #undef malloc
|
---|
24 | #undef realloc
|
---|
25 | #undef calloc
|
---|
26 | #undef strdup
|
---|
27 |
|
---|
28 | /* these are little tdb utility functions that are meant to make
|
---|
29 | dealing with a tdb database a little less cumbersome in Samba */
|
---|
30 |
|
---|
31 | static SIG_ATOMIC_T gotalarm;
|
---|
32 |
|
---|
33 | /***************************************************************
|
---|
34 | Signal function to tell us we timed out.
|
---|
35 | ****************************************************************/
|
---|
36 |
|
---|
37 | static void gotalarm_sig(void)
|
---|
38 | {
|
---|
39 | gotalarm = 1;
|
---|
40 | }
|
---|
41 |
|
---|
42 | /***************************************************************
|
---|
43 | Make a TDB_DATA and keep the const warning in one place
|
---|
44 | ****************************************************************/
|
---|
45 |
|
---|
46 | TDB_DATA make_tdb_data(const uint8 *dptr, size_t dsize)
|
---|
47 | {
|
---|
48 | TDB_DATA ret;
|
---|
49 | ret.dptr = CONST_DISCARD(uint8 *, dptr);
|
---|
50 | ret.dsize = dsize;
|
---|
51 | return ret;
|
---|
52 | }
|
---|
53 |
|
---|
54 | TDB_DATA string_tdb_data(const char *string)
|
---|
55 | {
|
---|
56 | return make_tdb_data((const uint8 *)string, string ? strlen(string) : 0 );
|
---|
57 | }
|
---|
58 |
|
---|
59 | TDB_DATA string_term_tdb_data(const char *string)
|
---|
60 | {
|
---|
61 | return make_tdb_data((const uint8 *)string, string ? strlen(string) + 1 : 0);
|
---|
62 | }
|
---|
63 |
|
---|
64 | /****************************************************************************
|
---|
65 | Lock a chain with timeout (in seconds).
|
---|
66 | ****************************************************************************/
|
---|
67 |
|
---|
68 | static int tdb_chainlock_with_timeout_internal( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout, int rw_type)
|
---|
69 | {
|
---|
70 | /* Allow tdb_chainlock to be interrupted by an alarm. */
|
---|
71 | int ret;
|
---|
72 | gotalarm = 0;
|
---|
73 |
|
---|
74 | if (timeout) {
|
---|
75 | CatchSignal(SIGALRM, SIGNAL_CAST gotalarm_sig);
|
---|
76 | tdb_setalarm_sigptr(tdb, &gotalarm);
|
---|
77 | alarm(timeout);
|
---|
78 | }
|
---|
79 |
|
---|
80 | if (rw_type == F_RDLCK)
|
---|
81 | ret = tdb_chainlock_read(tdb, key);
|
---|
82 | else
|
---|
83 | ret = tdb_chainlock(tdb, key);
|
---|
84 |
|
---|
85 | if (timeout) {
|
---|
86 | alarm(0);
|
---|
87 | tdb_setalarm_sigptr(tdb, NULL);
|
---|
88 | CatchSignal(SIGALRM, SIGNAL_CAST SIG_IGN);
|
---|
89 | if (gotalarm && (ret == -1)) {
|
---|
90 | DEBUG(0,("tdb_chainlock_with_timeout_internal: alarm (%u) timed out for key %s in tdb %s\n",
|
---|
91 | timeout, key.dptr, tdb_name(tdb)));
|
---|
92 | /* TODO: If we time out waiting for a lock, it might
|
---|
93 | * be nice to use F_GETLK to get the pid of the
|
---|
94 | * process currently holding the lock and print that
|
---|
95 | * as part of the debugging message. -- mbp */
|
---|
96 | return -1;
|
---|
97 | }
|
---|
98 | }
|
---|
99 |
|
---|
100 | return ret;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /****************************************************************************
|
---|
104 | Write lock a chain. Return -1 if timeout or lock failed.
|
---|
105 | ****************************************************************************/
|
---|
106 |
|
---|
107 | int tdb_chainlock_with_timeout( TDB_CONTEXT *tdb, TDB_DATA key, unsigned int timeout)
|
---|
108 | {
|
---|
109 | return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_WRLCK);
|
---|
110 | }
|
---|
111 |
|
---|
112 | /****************************************************************************
|
---|
113 | Lock a chain by string. Return -1 if timeout or lock failed.
|
---|
114 | ****************************************************************************/
|
---|
115 |
|
---|
116 | int tdb_lock_bystring(TDB_CONTEXT *tdb, const char *keyval)
|
---|
117 | {
|
---|
118 | TDB_DATA key = string_term_tdb_data(keyval);
|
---|
119 |
|
---|
120 | return tdb_chainlock(tdb, key);
|
---|
121 | }
|
---|
122 |
|
---|
123 | int tdb_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval,
|
---|
124 | int timeout)
|
---|
125 | {
|
---|
126 | TDB_DATA key = string_term_tdb_data(keyval);
|
---|
127 |
|
---|
128 | return tdb_chainlock_with_timeout(tdb, key, timeout);
|
---|
129 | }
|
---|
130 |
|
---|
131 | /****************************************************************************
|
---|
132 | Unlock a chain by string.
|
---|
133 | ****************************************************************************/
|
---|
134 |
|
---|
135 | void tdb_unlock_bystring(TDB_CONTEXT *tdb, const char *keyval)
|
---|
136 | {
|
---|
137 | TDB_DATA key = string_term_tdb_data(keyval);
|
---|
138 |
|
---|
139 | tdb_chainunlock(tdb, key);
|
---|
140 | }
|
---|
141 |
|
---|
142 | /****************************************************************************
|
---|
143 | Read lock a chain by string. Return -1 if timeout or lock failed.
|
---|
144 | ****************************************************************************/
|
---|
145 |
|
---|
146 | int tdb_read_lock_bystring_with_timeout(TDB_CONTEXT *tdb, const char *keyval, unsigned int timeout)
|
---|
147 | {
|
---|
148 | TDB_DATA key = string_term_tdb_data(keyval);
|
---|
149 |
|
---|
150 | return tdb_chainlock_with_timeout_internal(tdb, key, timeout, F_RDLCK);
|
---|
151 | }
|
---|
152 |
|
---|
153 | /****************************************************************************
|
---|
154 | Read unlock a chain by string.
|
---|
155 | ****************************************************************************/
|
---|
156 |
|
---|
157 | void tdb_read_unlock_bystring(TDB_CONTEXT *tdb, const char *keyval)
|
---|
158 | {
|
---|
159 | TDB_DATA key = string_term_tdb_data(keyval);
|
---|
160 |
|
---|
161 | tdb_chainunlock_read(tdb, key);
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | /****************************************************************************
|
---|
166 | Fetch a int32 value by a arbitrary blob key, return -1 if not found.
|
---|
167 | Output is int32 in native byte order.
|
---|
168 | ****************************************************************************/
|
---|
169 |
|
---|
170 | int32 tdb_fetch_int32_byblob(TDB_CONTEXT *tdb, TDB_DATA key)
|
---|
171 | {
|
---|
172 | TDB_DATA data;
|
---|
173 | int32 ret;
|
---|
174 |
|
---|
175 | data = tdb_fetch(tdb, key);
|
---|
176 | if (!data.dptr || data.dsize != sizeof(int32)) {
|
---|
177 | SAFE_FREE(data.dptr);
|
---|
178 | return -1;
|
---|
179 | }
|
---|
180 |
|
---|
181 | ret = IVAL(data.dptr,0);
|
---|
182 | SAFE_FREE(data.dptr);
|
---|
183 | return ret;
|
---|
184 | }
|
---|
185 |
|
---|
186 | /****************************************************************************
|
---|
187 | Fetch a int32 value by string key, return -1 if not found.
|
---|
188 | Output is int32 in native byte order.
|
---|
189 | ****************************************************************************/
|
---|
190 |
|
---|
191 | int32 tdb_fetch_int32(TDB_CONTEXT *tdb, const char *keystr)
|
---|
192 | {
|
---|
193 | TDB_DATA key = string_term_tdb_data(keystr);
|
---|
194 |
|
---|
195 | return tdb_fetch_int32_byblob(tdb, key);
|
---|
196 | }
|
---|
197 |
|
---|
198 | /****************************************************************************
|
---|
199 | Store a int32 value by an arbitary blob key, return 0 on success, -1 on failure.
|
---|
200 | Input is int32 in native byte order. Output in tdb is in little-endian.
|
---|
201 | ****************************************************************************/
|
---|
202 |
|
---|
203 | int tdb_store_int32_byblob(TDB_CONTEXT *tdb, TDB_DATA key, int32 v)
|
---|
204 | {
|
---|
205 | TDB_DATA data;
|
---|
206 | int32 v_store;
|
---|
207 |
|
---|
208 | SIVAL(&v_store,0,v);
|
---|
209 | data.dptr = (uint8 *)&v_store;
|
---|
210 | data.dsize = sizeof(int32);
|
---|
211 |
|
---|
212 | return tdb_store(tdb, key, data, TDB_REPLACE);
|
---|
213 | }
|
---|
214 |
|
---|
215 | /****************************************************************************
|
---|
216 | Store a int32 value by string key, return 0 on success, -1 on failure.
|
---|
217 | Input is int32 in native byte order. Output in tdb is in little-endian.
|
---|
218 | ****************************************************************************/
|
---|
219 |
|
---|
220 | int tdb_store_int32(TDB_CONTEXT *tdb, const char *keystr, int32 v)
|
---|
221 | {
|
---|
222 | TDB_DATA key = string_term_tdb_data(keystr);
|
---|
223 |
|
---|
224 | return tdb_store_int32_byblob(tdb, key, v);
|
---|
225 | }
|
---|
226 |
|
---|
227 | /****************************************************************************
|
---|
228 | Fetch a uint32 value by a arbitrary blob key, return -1 if not found.
|
---|
229 | Output is uint32 in native byte order.
|
---|
230 | ****************************************************************************/
|
---|
231 |
|
---|
232 | bool tdb_fetch_uint32_byblob(TDB_CONTEXT *tdb, TDB_DATA key, uint32 *value)
|
---|
233 | {
|
---|
234 | TDB_DATA data;
|
---|
235 |
|
---|
236 | data = tdb_fetch(tdb, key);
|
---|
237 | if (!data.dptr || data.dsize != sizeof(uint32)) {
|
---|
238 | SAFE_FREE(data.dptr);
|
---|
239 | return False;
|
---|
240 | }
|
---|
241 |
|
---|
242 | *value = IVAL(data.dptr,0);
|
---|
243 | SAFE_FREE(data.dptr);
|
---|
244 | return True;
|
---|
245 | }
|
---|
246 |
|
---|
247 | /****************************************************************************
|
---|
248 | Fetch a uint32 value by string key, return -1 if not found.
|
---|
249 | Output is uint32 in native byte order.
|
---|
250 | ****************************************************************************/
|
---|
251 |
|
---|
252 | bool tdb_fetch_uint32(TDB_CONTEXT *tdb, const char *keystr, uint32 *value)
|
---|
253 | {
|
---|
254 | TDB_DATA key = string_term_tdb_data(keystr);
|
---|
255 |
|
---|
256 | return tdb_fetch_uint32_byblob(tdb, key, value);
|
---|
257 | }
|
---|
258 |
|
---|
259 | /****************************************************************************
|
---|
260 | Store a uint32 value by an arbitary blob key, return 0 on success, -1 on failure.
|
---|
261 | Input is uint32 in native byte order. Output in tdb is in little-endian.
|
---|
262 | ****************************************************************************/
|
---|
263 |
|
---|
264 | bool tdb_store_uint32_byblob(TDB_CONTEXT *tdb, TDB_DATA key, uint32 value)
|
---|
265 | {
|
---|
266 | TDB_DATA data;
|
---|
267 | uint32 v_store;
|
---|
268 | bool ret = True;
|
---|
269 |
|
---|
270 | SIVAL(&v_store, 0, value);
|
---|
271 | data.dptr = (uint8 *)&v_store;
|
---|
272 | data.dsize = sizeof(uint32);
|
---|
273 |
|
---|
274 | if (tdb_store(tdb, key, data, TDB_REPLACE) == -1)
|
---|
275 | ret = False;
|
---|
276 |
|
---|
277 | return ret;
|
---|
278 | }
|
---|
279 |
|
---|
280 | /****************************************************************************
|
---|
281 | Store a uint32 value by string key, return 0 on success, -1 on failure.
|
---|
282 | Input is uint32 in native byte order. Output in tdb is in little-endian.
|
---|
283 | ****************************************************************************/
|
---|
284 |
|
---|
285 | bool tdb_store_uint32(TDB_CONTEXT *tdb, const char *keystr, uint32 value)
|
---|
286 | {
|
---|
287 | TDB_DATA key = string_term_tdb_data(keystr);
|
---|
288 |
|
---|
289 | return tdb_store_uint32_byblob(tdb, key, value);
|
---|
290 | }
|
---|
291 | /****************************************************************************
|
---|
292 | Store a buffer by a null terminated string key. Return 0 on success, -1
|
---|
293 | on failure.
|
---|
294 | ****************************************************************************/
|
---|
295 |
|
---|
296 | int tdb_store_bystring(TDB_CONTEXT *tdb, const char *keystr, TDB_DATA data, int flags)
|
---|
297 | {
|
---|
298 | TDB_DATA key = string_term_tdb_data(keystr);
|
---|
299 |
|
---|
300 | return tdb_store(tdb, key, data, flags);
|
---|
301 | }
|
---|
302 |
|
---|
303 | int tdb_trans_store_bystring(TDB_CONTEXT *tdb, const char *keystr,
|
---|
304 | TDB_DATA data, int flags)
|
---|
305 | {
|
---|
306 | TDB_DATA key = string_term_tdb_data(keystr);
|
---|
307 |
|
---|
308 | return tdb_trans_store(tdb, key, data, flags);
|
---|
309 | }
|
---|
310 |
|
---|
311 | /****************************************************************************
|
---|
312 | Fetch a buffer using a null terminated string key. Don't forget to call
|
---|
313 | free() on the result dptr.
|
---|
314 | ****************************************************************************/
|
---|
315 |
|
---|
316 | TDB_DATA tdb_fetch_bystring(TDB_CONTEXT *tdb, const char *keystr)
|
---|
317 | {
|
---|
318 | TDB_DATA key = string_term_tdb_data(keystr);
|
---|
319 |
|
---|
320 | return tdb_fetch(tdb, key);
|
---|
321 | }
|
---|
322 |
|
---|
323 | /****************************************************************************
|
---|
324 | Delete an entry using a null terminated string key.
|
---|
325 | ****************************************************************************/
|
---|
326 |
|
---|
327 | int tdb_delete_bystring(TDB_CONTEXT *tdb, const char *keystr)
|
---|
328 | {
|
---|
329 | TDB_DATA key = string_term_tdb_data(keystr);
|
---|
330 |
|
---|
331 | return tdb_delete(tdb, key);
|
---|
332 | }
|
---|
333 |
|
---|
334 | /****************************************************************************
|
---|
335 | Atomic integer change. Returns old value. To create, set initial value in *oldval.
|
---|
336 | ****************************************************************************/
|
---|
337 |
|
---|
338 | int32 tdb_change_int32_atomic(TDB_CONTEXT *tdb, const char *keystr, int32 *oldval, int32 change_val)
|
---|
339 | {
|
---|
340 | int32 val;
|
---|
341 | int32 ret = -1;
|
---|
342 |
|
---|
343 | if (tdb_lock_bystring(tdb, keystr) == -1)
|
---|
344 | return -1;
|
---|
345 |
|
---|
346 | if ((val = tdb_fetch_int32(tdb, keystr)) == -1) {
|
---|
347 | /* The lookup failed */
|
---|
348 | if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
|
---|
349 | /* but not because it didn't exist */
|
---|
350 | goto err_out;
|
---|
351 | }
|
---|
352 |
|
---|
353 | /* Start with 'old' value */
|
---|
354 | val = *oldval;
|
---|
355 |
|
---|
356 | } else {
|
---|
357 | /* It worked, set return value (oldval) to tdb data */
|
---|
358 | *oldval = val;
|
---|
359 | }
|
---|
360 |
|
---|
361 | /* Increment value for storage and return next time */
|
---|
362 | val += change_val;
|
---|
363 |
|
---|
364 | if (tdb_store_int32(tdb, keystr, val) == -1)
|
---|
365 | goto err_out;
|
---|
366 |
|
---|
367 | ret = 0;
|
---|
368 |
|
---|
369 | err_out:
|
---|
370 |
|
---|
371 | tdb_unlock_bystring(tdb, keystr);
|
---|
372 | return ret;
|
---|
373 | }
|
---|
374 |
|
---|
375 | /****************************************************************************
|
---|
376 | Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval.
|
---|
377 | ****************************************************************************/
|
---|
378 |
|
---|
379 | bool tdb_change_uint32_atomic(TDB_CONTEXT *tdb, const char *keystr, uint32 *oldval, uint32 change_val)
|
---|
380 | {
|
---|
381 | uint32 val;
|
---|
382 | bool ret = False;
|
---|
383 |
|
---|
384 | if (tdb_lock_bystring(tdb, keystr) == -1)
|
---|
385 | return False;
|
---|
386 |
|
---|
387 | if (!tdb_fetch_uint32(tdb, keystr, &val)) {
|
---|
388 | /* It failed */
|
---|
389 | if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
|
---|
390 | /* and not because it didn't exist */
|
---|
391 | goto err_out;
|
---|
392 | }
|
---|
393 |
|
---|
394 | /* Start with 'old' value */
|
---|
395 | val = *oldval;
|
---|
396 |
|
---|
397 | } else {
|
---|
398 | /* it worked, set return value (oldval) to tdb data */
|
---|
399 | *oldval = val;
|
---|
400 |
|
---|
401 | }
|
---|
402 |
|
---|
403 | /* get a new value to store */
|
---|
404 | val += change_val;
|
---|
405 |
|
---|
406 | if (!tdb_store_uint32(tdb, keystr, val))
|
---|
407 | goto err_out;
|
---|
408 |
|
---|
409 | ret = True;
|
---|
410 |
|
---|
411 | err_out:
|
---|
412 |
|
---|
413 | tdb_unlock_bystring(tdb, keystr);
|
---|
414 | return ret;
|
---|
415 | }
|
---|
416 |
|
---|
417 | /****************************************************************************
|
---|
418 | Useful pair of routines for packing/unpacking data consisting of
|
---|
419 | integers and strings.
|
---|
420 | ****************************************************************************/
|
---|
421 |
|
---|
422 | static size_t tdb_pack_va(uint8 *buf, int bufsize, const char *fmt, va_list ap)
|
---|
423 | {
|
---|
424 | uint8 bt;
|
---|
425 | uint16 w;
|
---|
426 | uint32 d;
|
---|
427 | int i;
|
---|
428 | void *p;
|
---|
429 | int len;
|
---|
430 | char *s;
|
---|
431 | char c;
|
---|
432 | uint8 *buf0 = buf;
|
---|
433 | const char *fmt0 = fmt;
|
---|
434 | int bufsize0 = bufsize;
|
---|
435 |
|
---|
436 | while (*fmt) {
|
---|
437 | switch ((c = *fmt++)) {
|
---|
438 | case 'b': /* unsigned 8-bit integer */
|
---|
439 | len = 1;
|
---|
440 | bt = (uint8)va_arg(ap, int);
|
---|
441 | if (bufsize && bufsize >= len)
|
---|
442 | SSVAL(buf, 0, bt);
|
---|
443 | break;
|
---|
444 | case 'w': /* unsigned 16-bit integer */
|
---|
445 | len = 2;
|
---|
446 | w = (uint16)va_arg(ap, int);
|
---|
447 | if (bufsize && bufsize >= len)
|
---|
448 | SSVAL(buf, 0, w);
|
---|
449 | break;
|
---|
450 | case 'd': /* signed 32-bit integer (standard int in most systems) */
|
---|
451 | len = 4;
|
---|
452 | d = va_arg(ap, uint32);
|
---|
453 | if (bufsize && bufsize >= len)
|
---|
454 | SIVAL(buf, 0, d);
|
---|
455 | break;
|
---|
456 | case 'p': /* pointer */
|
---|
457 | len = 4;
|
---|
458 | p = va_arg(ap, void *);
|
---|
459 | d = p?1:0;
|
---|
460 | if (bufsize && bufsize >= len)
|
---|
461 | SIVAL(buf, 0, d);
|
---|
462 | break;
|
---|
463 | case 'P': /* null-terminated string */
|
---|
464 | s = va_arg(ap,char *);
|
---|
465 | w = strlen(s);
|
---|
466 | len = w + 1;
|
---|
467 | if (bufsize && bufsize >= len)
|
---|
468 | memcpy(buf, s, len);
|
---|
469 | break;
|
---|
470 | case 'f': /* null-terminated string */
|
---|
471 | s = va_arg(ap,char *);
|
---|
472 | w = strlen(s);
|
---|
473 | len = w + 1;
|
---|
474 | if (bufsize && bufsize >= len)
|
---|
475 | memcpy(buf, s, len);
|
---|
476 | break;
|
---|
477 | case 'B': /* fixed-length string */
|
---|
478 | i = va_arg(ap, int);
|
---|
479 | s = va_arg(ap, char *);
|
---|
480 | len = 4+i;
|
---|
481 | if (bufsize && bufsize >= len) {
|
---|
482 | SIVAL(buf, 0, i);
|
---|
483 | memcpy(buf+4, s, i);
|
---|
484 | }
|
---|
485 | break;
|
---|
486 | default:
|
---|
487 | DEBUG(0,("Unknown tdb_pack format %c in %s\n",
|
---|
488 | c, fmt));
|
---|
489 | len = 0;
|
---|
490 | break;
|
---|
491 | }
|
---|
492 |
|
---|
493 | buf += len;
|
---|
494 | if (bufsize)
|
---|
495 | bufsize -= len;
|
---|
496 | if (bufsize < 0)
|
---|
497 | bufsize = 0;
|
---|
498 | }
|
---|
499 |
|
---|
500 | DEBUG(18,("tdb_pack_va(%s, %d) -> %d\n",
|
---|
501 | fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
|
---|
502 |
|
---|
503 | return PTR_DIFF(buf, buf0);
|
---|
504 | }
|
---|
505 |
|
---|
506 | size_t tdb_pack(uint8 *buf, int bufsize, const char *fmt, ...)
|
---|
507 | {
|
---|
508 | va_list ap;
|
---|
509 | size_t result;
|
---|
510 |
|
---|
511 | va_start(ap, fmt);
|
---|
512 | result = tdb_pack_va(buf, bufsize, fmt, ap);
|
---|
513 | va_end(ap);
|
---|
514 | return result;
|
---|
515 | }
|
---|
516 |
|
---|
517 | bool tdb_pack_append(TALLOC_CTX *mem_ctx, uint8 **buf, size_t *len,
|
---|
518 | const char *fmt, ...)
|
---|
519 | {
|
---|
520 | va_list ap;
|
---|
521 | size_t len1, len2;
|
---|
522 |
|
---|
523 | va_start(ap, fmt);
|
---|
524 | len1 = tdb_pack_va(NULL, 0, fmt, ap);
|
---|
525 | va_end(ap);
|
---|
526 |
|
---|
527 | if (mem_ctx != NULL) {
|
---|
528 | *buf = TALLOC_REALLOC_ARRAY(mem_ctx, *buf, uint8,
|
---|
529 | (*len) + len1);
|
---|
530 | } else {
|
---|
531 | *buf = SMB_REALLOC_ARRAY(*buf, uint8, (*len) + len1);
|
---|
532 | }
|
---|
533 |
|
---|
534 | if (*buf == NULL) {
|
---|
535 | return False;
|
---|
536 | }
|
---|
537 |
|
---|
538 | va_start(ap, fmt);
|
---|
539 | len2 = tdb_pack_va((*buf)+(*len), len1, fmt, ap);
|
---|
540 | va_end(ap);
|
---|
541 |
|
---|
542 | if (len1 != len2) {
|
---|
543 | return False;
|
---|
544 | }
|
---|
545 |
|
---|
546 | *len += len2;
|
---|
547 |
|
---|
548 | return True;
|
---|
549 | }
|
---|
550 |
|
---|
551 | /****************************************************************************
|
---|
552 | Useful pair of routines for packing/unpacking data consisting of
|
---|
553 | integers and strings.
|
---|
554 | ****************************************************************************/
|
---|
555 |
|
---|
556 | int tdb_unpack(const uint8 *buf, int bufsize, const char *fmt, ...)
|
---|
557 | {
|
---|
558 | va_list ap;
|
---|
559 | uint8 *bt;
|
---|
560 | uint16 *w;
|
---|
561 | uint32 *d;
|
---|
562 | int len;
|
---|
563 | int *i;
|
---|
564 | void **p;
|
---|
565 | char *s, **b, **ps;
|
---|
566 | char c;
|
---|
567 | const uint8 *buf0 = buf;
|
---|
568 | const char *fmt0 = fmt;
|
---|
569 | int bufsize0 = bufsize;
|
---|
570 |
|
---|
571 | va_start(ap, fmt);
|
---|
572 |
|
---|
573 | while (*fmt) {
|
---|
574 | switch ((c=*fmt++)) {
|
---|
575 | case 'b':
|
---|
576 | len = 1;
|
---|
577 | bt = va_arg(ap, uint8 *);
|
---|
578 | if (bufsize < len)
|
---|
579 | goto no_space;
|
---|
580 | *bt = SVAL(buf, 0);
|
---|
581 | break;
|
---|
582 | case 'w':
|
---|
583 | len = 2;
|
---|
584 | w = va_arg(ap, uint16 *);
|
---|
585 | if (bufsize < len)
|
---|
586 | goto no_space;
|
---|
587 | *w = SVAL(buf, 0);
|
---|
588 | break;
|
---|
589 | case 'd':
|
---|
590 | len = 4;
|
---|
591 | d = va_arg(ap, uint32 *);
|
---|
592 | if (bufsize < len)
|
---|
593 | goto no_space;
|
---|
594 | *d = IVAL(buf, 0);
|
---|
595 | break;
|
---|
596 | case 'p':
|
---|
597 | len = 4;
|
---|
598 | p = va_arg(ap, void **);
|
---|
599 | if (bufsize < len)
|
---|
600 | goto no_space;
|
---|
601 | /*
|
---|
602 | * This isn't a real pointer - only a token (1 or 0)
|
---|
603 | * to mark the fact a pointer is present.
|
---|
604 | */
|
---|
605 |
|
---|
606 | *p = (void *)(IVAL(buf, 0) ? (void *)1 : NULL);
|
---|
607 | break;
|
---|
608 | case 'P':
|
---|
609 | /* Return malloc'ed string. */
|
---|
610 | ps = va_arg(ap,char **);
|
---|
611 | len = strlen((const char *)buf) + 1;
|
---|
612 | *ps = SMB_STRDUP((const char *)buf);
|
---|
613 | break;
|
---|
614 | case 'f':
|
---|
615 | s = va_arg(ap,char *);
|
---|
616 | len = strlen((const char *)buf) + 1;
|
---|
617 | if (bufsize < len || len > sizeof(fstring))
|
---|
618 | goto no_space;
|
---|
619 | memcpy(s, buf, len);
|
---|
620 | break;
|
---|
621 | case 'B':
|
---|
622 | i = va_arg(ap, int *);
|
---|
623 | b = va_arg(ap, char **);
|
---|
624 | len = 4;
|
---|
625 | if (bufsize < len)
|
---|
626 | goto no_space;
|
---|
627 | *i = IVAL(buf, 0);
|
---|
628 | if (! *i) {
|
---|
629 | *b = NULL;
|
---|
630 | break;
|
---|
631 | }
|
---|
632 | len += *i;
|
---|
633 | if (bufsize < len)
|
---|
634 | goto no_space;
|
---|
635 | *b = (char *)SMB_MALLOC(*i);
|
---|
636 | if (! *b)
|
---|
637 | goto no_space;
|
---|
638 | memcpy(*b, buf+4, *i);
|
---|
639 | break;
|
---|
640 | default:
|
---|
641 | DEBUG(0,("Unknown tdb_unpack format %c in %s\n",
|
---|
642 | c, fmt));
|
---|
643 |
|
---|
644 | len = 0;
|
---|
645 | break;
|
---|
646 | }
|
---|
647 |
|
---|
648 | buf += len;
|
---|
649 | bufsize -= len;
|
---|
650 | }
|
---|
651 |
|
---|
652 | va_end(ap);
|
---|
653 |
|
---|
654 | DEBUG(18,("tdb_unpack(%s, %d) -> %d\n",
|
---|
655 | fmt0, bufsize0, (int)PTR_DIFF(buf, buf0)));
|
---|
656 |
|
---|
657 | return PTR_DIFF(buf, buf0);
|
---|
658 |
|
---|
659 | no_space:
|
---|
660 | va_end(ap);
|
---|
661 | return -1;
|
---|
662 | }
|
---|
663 |
|
---|
664 |
|
---|
665 | /****************************************************************************
|
---|
666 | Log tdb messages via DEBUG().
|
---|
667 | ****************************************************************************/
|
---|
668 |
|
---|
669 | static void tdb_log(TDB_CONTEXT *tdb, enum tdb_debug_level level, const char *format, ...)
|
---|
670 | {
|
---|
671 | va_list ap;
|
---|
672 | char *ptr = NULL;
|
---|
673 | int ret;
|
---|
674 |
|
---|
675 | va_start(ap, format);
|
---|
676 | ret = vasprintf(&ptr, format, ap);
|
---|
677 | va_end(ap);
|
---|
678 |
|
---|
679 | if ((ret == -1) || !*ptr)
|
---|
680 | return;
|
---|
681 |
|
---|
682 | DEBUG((int)level, ("tdb(%s): %s", tdb_name(tdb) ? tdb_name(tdb) : "unnamed", ptr));
|
---|
683 | SAFE_FREE(ptr);
|
---|
684 | }
|
---|
685 |
|
---|
686 | /****************************************************************************
|
---|
687 | Like tdb_open() but also setup a logging function that redirects to
|
---|
688 | the samba DEBUG() system.
|
---|
689 | ****************************************************************************/
|
---|
690 |
|
---|
691 | TDB_CONTEXT *tdb_open_log(const char *name, int hash_size, int tdb_flags,
|
---|
692 | int open_flags, mode_t mode)
|
---|
693 | {
|
---|
694 | TDB_CONTEXT *tdb;
|
---|
695 | struct tdb_logging_context log_ctx;
|
---|
696 |
|
---|
697 | if (!lp_use_mmap())
|
---|
698 | tdb_flags |= TDB_NOMMAP;
|
---|
699 |
|
---|
700 | log_ctx.log_fn = tdb_log;
|
---|
701 | log_ctx.log_private = NULL;
|
---|
702 |
|
---|
703 | if ((hash_size == 0) && (name != NULL)) {
|
---|
704 | const char *base = strrchr_m(name, '/');
|
---|
705 | if (base != NULL) {
|
---|
706 | base += 1;
|
---|
707 | }
|
---|
708 | else {
|
---|
709 | base = name;
|
---|
710 | }
|
---|
711 | hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
|
---|
712 | }
|
---|
713 |
|
---|
714 | tdb = tdb_open_ex(name, hash_size, tdb_flags,
|
---|
715 | open_flags, mode, &log_ctx, NULL);
|
---|
716 | if (!tdb)
|
---|
717 | return NULL;
|
---|
718 |
|
---|
719 | return tdb;
|
---|
720 | }
|
---|
721 |
|
---|
722 |
|
---|
723 | /**
|
---|
724 | * Search across the whole tdb for keys that match the given pattern
|
---|
725 | * return the result as a list of keys
|
---|
726 | *
|
---|
727 | * @param tdb pointer to opened tdb file context
|
---|
728 | * @param pattern searching pattern used by fnmatch(3) functions
|
---|
729 | *
|
---|
730 | * @return list of keys found by looking up with given pattern
|
---|
731 | **/
|
---|
732 | TDB_LIST_NODE *tdb_search_keys(TDB_CONTEXT *tdb, const char* pattern)
|
---|
733 | {
|
---|
734 | TDB_DATA key, next;
|
---|
735 | TDB_LIST_NODE *list = NULL;
|
---|
736 | TDB_LIST_NODE *rec = NULL;
|
---|
737 |
|
---|
738 | for (key = tdb_firstkey(tdb); key.dptr; key = next) {
|
---|
739 | /* duplicate key string to ensure null-termination */
|
---|
740 | char *key_str = SMB_STRNDUP((const char *)key.dptr, key.dsize);
|
---|
741 | if (!key_str) {
|
---|
742 | DEBUG(0, ("tdb_search_keys: strndup() failed!\n"));
|
---|
743 | smb_panic("strndup failed!\n");
|
---|
744 | }
|
---|
745 |
|
---|
746 | DEBUG(18, ("checking %s for match to pattern %s\n", key_str, pattern));
|
---|
747 |
|
---|
748 | next = tdb_nextkey(tdb, key);
|
---|
749 |
|
---|
750 | /* do the pattern checking */
|
---|
751 | if (fnmatch(pattern, key_str, 0) == 0) {
|
---|
752 | rec = SMB_MALLOC_P(TDB_LIST_NODE);
|
---|
753 | ZERO_STRUCTP(rec);
|
---|
754 |
|
---|
755 | rec->node_key = key;
|
---|
756 |
|
---|
757 | DLIST_ADD_END(list, rec, TDB_LIST_NODE *);
|
---|
758 |
|
---|
759 | DEBUG(18, ("checking %s matched pattern %s\n", key_str, pattern));
|
---|
760 | } else {
|
---|
761 | free(key.dptr);
|
---|
762 | }
|
---|
763 |
|
---|
764 | /* free duplicated key string */
|
---|
765 | free(key_str);
|
---|
766 | }
|
---|
767 |
|
---|
768 | return list;
|
---|
769 |
|
---|
770 | }
|
---|
771 |
|
---|
772 |
|
---|
773 | /**
|
---|
774 | * Free the list returned by tdb_search_keys
|
---|
775 | *
|
---|
776 | * @param node list of results found by tdb_search_keys
|
---|
777 | **/
|
---|
778 | void tdb_search_list_free(TDB_LIST_NODE* node)
|
---|
779 | {
|
---|
780 | TDB_LIST_NODE *next_node;
|
---|
781 |
|
---|
782 | while (node) {
|
---|
783 | next_node = node->next;
|
---|
784 | SAFE_FREE(node->node_key.dptr);
|
---|
785 | SAFE_FREE(node);
|
---|
786 | node = next_node;
|
---|
787 | };
|
---|
788 | }
|
---|
789 |
|
---|
790 | /****************************************************************************
|
---|
791 | tdb_store, wrapped in a transaction. This way we make sure that a process
|
---|
792 | that dies within writing does not leave a corrupt tdb behind.
|
---|
793 | ****************************************************************************/
|
---|
794 |
|
---|
795 | int tdb_trans_store(struct tdb_context *tdb, TDB_DATA key, TDB_DATA dbuf,
|
---|
796 | int flag)
|
---|
797 | {
|
---|
798 | int res;
|
---|
799 |
|
---|
800 | if ((res = tdb_transaction_start(tdb)) != 0) {
|
---|
801 | DEBUG(5, ("tdb_transaction_start failed\n"));
|
---|
802 | return res;
|
---|
803 | }
|
---|
804 |
|
---|
805 | if ((res = tdb_store(tdb, key, dbuf, flag)) != 0) {
|
---|
806 | DEBUG(10, ("tdb_store failed\n"));
|
---|
807 | if (tdb_transaction_cancel(tdb) != 0) {
|
---|
808 | smb_panic("Cancelling transaction failed");
|
---|
809 | }
|
---|
810 | return res;
|
---|
811 | }
|
---|
812 |
|
---|
813 | if ((res = tdb_transaction_commit(tdb)) != 0) {
|
---|
814 | DEBUG(5, ("tdb_transaction_commit failed\n"));
|
---|
815 | }
|
---|
816 |
|
---|
817 | return res;
|
---|
818 | }
|
---|
819 |
|
---|
820 | /****************************************************************************
|
---|
821 | tdb_delete, wrapped in a transaction. This way we make sure that a process
|
---|
822 | that dies within deleting does not leave a corrupt tdb behind.
|
---|
823 | ****************************************************************************/
|
---|
824 |
|
---|
825 | int tdb_trans_delete(struct tdb_context *tdb, TDB_DATA key)
|
---|
826 | {
|
---|
827 | int res;
|
---|
828 |
|
---|
829 | if ((res = tdb_transaction_start(tdb)) != 0) {
|
---|
830 | DEBUG(5, ("tdb_transaction_start failed\n"));
|
---|
831 | return res;
|
---|
832 | }
|
---|
833 |
|
---|
834 | if ((res = tdb_delete(tdb, key)) != 0) {
|
---|
835 | DEBUG(10, ("tdb_delete failed\n"));
|
---|
836 | if (tdb_transaction_cancel(tdb) != 0) {
|
---|
837 | smb_panic("Cancelling transaction failed");
|
---|
838 | }
|
---|
839 | return res;
|
---|
840 | }
|
---|
841 |
|
---|
842 | if ((res = tdb_transaction_commit(tdb)) != 0) {
|
---|
843 | DEBUG(5, ("tdb_transaction_commit failed\n"));
|
---|
844 | }
|
---|
845 |
|
---|
846 | return res;
|
---|
847 | }
|
---|
848 |
|
---|
849 | /*
|
---|
850 | Log tdb messages via DEBUG().
|
---|
851 | */
|
---|
852 | static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
|
---|
853 | const char *format, ...) PRINTF_ATTRIBUTE(3,4);
|
---|
854 |
|
---|
855 | static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
|
---|
856 | const char *format, ...)
|
---|
857 | {
|
---|
858 | va_list ap;
|
---|
859 | char *ptr = NULL;
|
---|
860 | int debuglevel = 0;
|
---|
861 | int ret;
|
---|
862 |
|
---|
863 | switch (level) {
|
---|
864 | case TDB_DEBUG_FATAL:
|
---|
865 | debuglevel = 0;
|
---|
866 | break;
|
---|
867 | case TDB_DEBUG_ERROR:
|
---|
868 | debuglevel = 1;
|
---|
869 | break;
|
---|
870 | case TDB_DEBUG_WARNING:
|
---|
871 | debuglevel = 2;
|
---|
872 | break;
|
---|
873 | case TDB_DEBUG_TRACE:
|
---|
874 | debuglevel = 5;
|
---|
875 | break;
|
---|
876 | default:
|
---|
877 | debuglevel = 0;
|
---|
878 | }
|
---|
879 |
|
---|
880 | va_start(ap, format);
|
---|
881 | ret = vasprintf(&ptr, format, ap);
|
---|
882 | va_end(ap);
|
---|
883 |
|
---|
884 | if (ret != -1) {
|
---|
885 | const char *name = tdb_name(tdb);
|
---|
886 | DEBUG(debuglevel, ("tdb(%s): %s", name ? name : "unnamed", ptr));
|
---|
887 | free(ptr);
|
---|
888 | }
|
---|
889 | }
|
---|
890 |
|
---|
891 | static struct tdb_wrap *tdb_list;
|
---|
892 |
|
---|
893 | /* destroy the last connection to a tdb */
|
---|
894 | static int tdb_wrap_destructor(struct tdb_wrap *w)
|
---|
895 | {
|
---|
896 | tdb_close(w->tdb);
|
---|
897 | DLIST_REMOVE(tdb_list, w);
|
---|
898 | return 0;
|
---|
899 | }
|
---|
900 |
|
---|
901 | /*
|
---|
902 | wrapped connection to a tdb database
|
---|
903 | to close just talloc_free() the tdb_wrap pointer
|
---|
904 | */
|
---|
905 | struct tdb_wrap *tdb_wrap_open(TALLOC_CTX *mem_ctx,
|
---|
906 | const char *name, int hash_size, int tdb_flags,
|
---|
907 | int open_flags, mode_t mode)
|
---|
908 | {
|
---|
909 | struct tdb_wrap *w;
|
---|
910 | struct tdb_logging_context log_ctx;
|
---|
911 | log_ctx.log_fn = tdb_wrap_log;
|
---|
912 |
|
---|
913 | if (!lp_use_mmap())
|
---|
914 | tdb_flags |= TDB_NOMMAP;
|
---|
915 |
|
---|
916 | for (w=tdb_list;w;w=w->next) {
|
---|
917 | if (strcmp(name, w->name) == 0) {
|
---|
918 | /*
|
---|
919 | * Yes, talloc_reference is exactly what we want
|
---|
920 | * here. Otherwise we would have to implement our own
|
---|
921 | * reference counting.
|
---|
922 | */
|
---|
923 | return talloc_reference(mem_ctx, w);
|
---|
924 | }
|
---|
925 | }
|
---|
926 |
|
---|
927 | w = talloc(mem_ctx, struct tdb_wrap);
|
---|
928 | if (w == NULL) {
|
---|
929 | return NULL;
|
---|
930 | }
|
---|
931 |
|
---|
932 | if (!(w->name = talloc_strdup(w, name))) {
|
---|
933 | talloc_free(w);
|
---|
934 | return NULL;
|
---|
935 | }
|
---|
936 |
|
---|
937 | if ((hash_size == 0) && (name != NULL)) {
|
---|
938 | const char *base = strrchr_m(name, '/');
|
---|
939 | if (base != NULL) {
|
---|
940 | base += 1;
|
---|
941 | }
|
---|
942 | else {
|
---|
943 | base = name;
|
---|
944 | }
|
---|
945 | hash_size = lp_parm_int(-1, "tdb_hashsize", base, 0);
|
---|
946 | }
|
---|
947 |
|
---|
948 | w->tdb = tdb_open_ex(name, hash_size, tdb_flags,
|
---|
949 | open_flags, mode, &log_ctx, NULL);
|
---|
950 | if (w->tdb == NULL) {
|
---|
951 | talloc_free(w);
|
---|
952 | return NULL;
|
---|
953 | }
|
---|
954 |
|
---|
955 | talloc_set_destructor(w, tdb_wrap_destructor);
|
---|
956 |
|
---|
957 | DLIST_ADD(tdb_list, w);
|
---|
958 |
|
---|
959 | return w;
|
---|
960 | }
|
---|
961 |
|
---|
962 | NTSTATUS map_nt_error_from_tdb(enum TDB_ERROR err)
|
---|
963 | {
|
---|
964 | struct { enum TDB_ERROR err; NTSTATUS status; } map[] =
|
---|
965 | { { TDB_SUCCESS, NT_STATUS_OK },
|
---|
966 | { TDB_ERR_CORRUPT, NT_STATUS_INTERNAL_DB_CORRUPTION },
|
---|
967 | { TDB_ERR_IO, NT_STATUS_UNEXPECTED_IO_ERROR },
|
---|
968 | { TDB_ERR_OOM, NT_STATUS_NO_MEMORY },
|
---|
969 | { TDB_ERR_EXISTS, NT_STATUS_OBJECT_NAME_COLLISION },
|
---|
970 |
|
---|
971 | /*
|
---|
972 | * TDB_ERR_LOCK is very broad, we could for example
|
---|
973 | * distinguish between fcntl locks and invalid lock
|
---|
974 | * sequences. So NT_STATUS_FILE_LOCK_CONFLICT is a
|
---|
975 | * compromise.
|
---|
976 | */
|
---|
977 | { TDB_ERR_LOCK, NT_STATUS_FILE_LOCK_CONFLICT },
|
---|
978 | /*
|
---|
979 | * The next two ones in the enum are not actually used
|
---|
980 | */
|
---|
981 | { TDB_ERR_NOLOCK, NT_STATUS_FILE_LOCK_CONFLICT },
|
---|
982 | { TDB_ERR_LOCK_TIMEOUT, NT_STATUS_FILE_LOCK_CONFLICT },
|
---|
983 | { TDB_ERR_NOEXIST, NT_STATUS_NOT_FOUND },
|
---|
984 | { TDB_ERR_EINVAL, NT_STATUS_INVALID_PARAMETER },
|
---|
985 | { TDB_ERR_RDONLY, NT_STATUS_ACCESS_DENIED }
|
---|
986 | };
|
---|
987 |
|
---|
988 | int i;
|
---|
989 |
|
---|
990 | for (i=0; i < sizeof(map) / sizeof(map[0]); i++) {
|
---|
991 | if (err == map[i].err) {
|
---|
992 | return map[i].status;
|
---|
993 | }
|
---|
994 | }
|
---|
995 |
|
---|
996 | return NT_STATUS_INTERNAL_ERROR;
|
---|
997 | }
|
---|
998 |
|
---|
999 |
|
---|
1000 | /*********************************************************************
|
---|
1001 | * the following is a generic validation mechanism for tdbs.
|
---|
1002 | *********************************************************************/
|
---|
1003 |
|
---|
1004 | /*
|
---|
1005 | * internal validation function, executed by the child.
|
---|
1006 | */
|
---|
1007 | static int tdb_validate_child(struct tdb_context *tdb,
|
---|
1008 | tdb_validate_data_func validate_fn)
|
---|
1009 | {
|
---|
1010 | int ret = 1;
|
---|
1011 | int num_entries = 0;
|
---|
1012 | struct tdb_validation_status v_status;
|
---|
1013 |
|
---|
1014 | v_status.tdb_error = False;
|
---|
1015 | v_status.bad_freelist = False;
|
---|
1016 | v_status.bad_entry = False;
|
---|
1017 | v_status.unknown_key = False;
|
---|
1018 | v_status.success = True;
|
---|
1019 |
|
---|
1020 | if (!tdb) {
|
---|
1021 | v_status.tdb_error = True;
|
---|
1022 | v_status.success = False;
|
---|
1023 | goto out;
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | /* Check if the tdb's freelist is good. */
|
---|
1027 | if (tdb_validate_freelist(tdb, &num_entries) == -1) {
|
---|
1028 | v_status.bad_freelist = True;
|
---|
1029 | v_status.success = False;
|
---|
1030 | goto out;
|
---|
1031 | }
|
---|
1032 |
|
---|
1033 | DEBUG(10,("tdb_validate_child: tdb %s freelist has %d entries\n",
|
---|
1034 | tdb_name(tdb), num_entries));
|
---|
1035 |
|
---|
1036 | /* Now traverse the tdb to validate it. */
|
---|
1037 | num_entries = tdb_traverse(tdb, validate_fn, (void *)&v_status);
|
---|
1038 | if (!v_status.success) {
|
---|
1039 | goto out;
|
---|
1040 | } else if (num_entries == -1) {
|
---|
1041 | v_status.tdb_error = True;
|
---|
1042 | v_status.success = False;
|
---|
1043 | goto out;
|
---|
1044 | }
|
---|
1045 |
|
---|
1046 | DEBUG(10,("tdb_validate_child: tdb %s is good with %d entries\n",
|
---|
1047 | tdb_name(tdb), num_entries));
|
---|
1048 | ret = 0; /* Cache is good. */
|
---|
1049 |
|
---|
1050 | out:
|
---|
1051 | DEBUG(10, ("tdb_validate_child: summary of validation status:\n"));
|
---|
1052 | DEBUGADD(10,(" * tdb error: %s\n", v_status.tdb_error ? "yes" : "no"));
|
---|
1053 | DEBUGADD(10,(" * bad freelist: %s\n",v_status.bad_freelist?"yes":"no"));
|
---|
1054 | DEBUGADD(10,(" * bad entry: %s\n", v_status.bad_entry ? "yes" : "no"));
|
---|
1055 | DEBUGADD(10,(" * unknown key: %s\n", v_status.unknown_key?"yes":"no"));
|
---|
1056 | DEBUGADD(10,(" => overall success: %s\n", v_status.success?"yes":"no"));
|
---|
1057 |
|
---|
1058 | return ret;
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | /*
|
---|
1062 | * tdb validation function.
|
---|
1063 | * returns 0 if tdb is ok, != 0 if it isn't.
|
---|
1064 | * this function expects an opened tdb.
|
---|
1065 | */
|
---|
1066 | int tdb_validate(struct tdb_context *tdb, tdb_validate_data_func validate_fn)
|
---|
1067 | {
|
---|
1068 | pid_t child_pid = -1;
|
---|
1069 | int child_status = 0;
|
---|
1070 | int wait_pid = 0;
|
---|
1071 | int ret = 1;
|
---|
1072 |
|
---|
1073 | if (tdb == NULL) {
|
---|
1074 | DEBUG(1, ("Error: tdb_validate called with tdb == NULL\n"));
|
---|
1075 | return ret;
|
---|
1076 | }
|
---|
1077 |
|
---|
1078 | DEBUG(5, ("tdb_validate called for tdb '%s'\n", tdb_name(tdb)));
|
---|
1079 |
|
---|
1080 | /* fork and let the child do the validation.
|
---|
1081 | * benefit: no need to twist signal handlers and panic functions.
|
---|
1082 | * just let the child panic. we catch the signal. */
|
---|
1083 |
|
---|
1084 | DEBUG(10, ("tdb_validate: forking to let child do validation.\n"));
|
---|
1085 | child_pid = sys_fork();
|
---|
1086 | if (child_pid == 0) {
|
---|
1087 | /* child code */
|
---|
1088 | DEBUG(10, ("tdb_validate (validation child): created\n"));
|
---|
1089 | DEBUG(10, ("tdb_validate (validation child): "
|
---|
1090 | "calling tdb_validate_child\n"));
|
---|
1091 | exit(tdb_validate_child(tdb, validate_fn));
|
---|
1092 | }
|
---|
1093 | else if (child_pid < 0) {
|
---|
1094 | DEBUG(1, ("tdb_validate: fork for validation failed.\n"));
|
---|
1095 | goto done;
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | /* parent */
|
---|
1099 |
|
---|
1100 | DEBUG(10, ("tdb_validate: fork succeeded, child PID = %d\n",child_pid));
|
---|
1101 |
|
---|
1102 | DEBUG(10, ("tdb_validate: waiting for child to finish...\n"));
|
---|
1103 | while ((wait_pid = sys_waitpid(child_pid, &child_status, 0)) < 0) {
|
---|
1104 | if (errno == EINTR) {
|
---|
1105 | DEBUG(10, ("tdb_validate: got signal during waitpid, "
|
---|
1106 | "retrying\n"));
|
---|
1107 | errno = 0;
|
---|
1108 | continue;
|
---|
1109 | }
|
---|
1110 | DEBUG(1, ("tdb_validate: waitpid failed with error '%s'.\n",
|
---|
1111 | strerror(errno)));
|
---|
1112 | goto done;
|
---|
1113 | }
|
---|
1114 | if (wait_pid != child_pid) {
|
---|
1115 | DEBUG(1, ("tdb_validate: waitpid returned pid %d, "
|
---|
1116 | "but %d was expected\n", wait_pid, child_pid));
|
---|
1117 | goto done;
|
---|
1118 | }
|
---|
1119 |
|
---|
1120 | DEBUG(10, ("tdb_validate: validating child returned.\n"));
|
---|
1121 | if (WIFEXITED(child_status)) {
|
---|
1122 | DEBUG(10, ("tdb_validate: child exited, code %d.\n",
|
---|
1123 | WEXITSTATUS(child_status)));
|
---|
1124 | ret = WEXITSTATUS(child_status);
|
---|
1125 | }
|
---|
1126 | if (WIFSIGNALED(child_status)) {
|
---|
1127 | DEBUG(10, ("tdb_validate: child terminated by signal %d\n",
|
---|
1128 | WTERMSIG(child_status)));
|
---|
1129 | #ifdef WCOREDUMP
|
---|
1130 | if (WCOREDUMP(child_status)) {
|
---|
1131 | DEBUGADD(10, ("core dumped\n"));
|
---|
1132 | }
|
---|
1133 | #endif
|
---|
1134 | ret = WTERMSIG(child_status);
|
---|
1135 | }
|
---|
1136 | if (WIFSTOPPED(child_status)) {
|
---|
1137 | DEBUG(10, ("tdb_validate: child was stopped by signal %d\n",
|
---|
1138 | WSTOPSIG(child_status)));
|
---|
1139 | ret = WSTOPSIG(child_status);
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | done:
|
---|
1143 | DEBUG(5, ("tdb_validate returning code '%d' for tdb '%s'\n", ret,
|
---|
1144 | tdb_name(tdb)));
|
---|
1145 |
|
---|
1146 | return ret;
|
---|
1147 | }
|
---|
1148 |
|
---|
1149 | /*
|
---|
1150 | * tdb validation function.
|
---|
1151 | * returns 0 if tdb is ok, != 0 if it isn't.
|
---|
1152 | * this is a wrapper around the actual validation function that opens and closes
|
---|
1153 | * the tdb.
|
---|
1154 | */
|
---|
1155 | int tdb_validate_open(const char *tdb_path, tdb_validate_data_func validate_fn)
|
---|
1156 | {
|
---|
1157 | TDB_CONTEXT *tdb = NULL;
|
---|
1158 | int ret = 1;
|
---|
1159 |
|
---|
1160 | DEBUG(5, ("tdb_validate_open called for tdb '%s'\n", tdb_path));
|
---|
1161 |
|
---|
1162 | tdb = tdb_open_log(tdb_path, 0, TDB_DEFAULT, O_RDONLY, 0);
|
---|
1163 | if (!tdb) {
|
---|
1164 | DEBUG(1, ("Error opening tdb %s\n", tdb_path));
|
---|
1165 | return ret;
|
---|
1166 | }
|
---|
1167 |
|
---|
1168 | ret = tdb_validate(tdb, validate_fn);
|
---|
1169 | tdb_close(tdb);
|
---|
1170 | return ret;
|
---|
1171 | }
|
---|
1172 |
|
---|
1173 | /*
|
---|
1174 | * tdb backup function and helpers for tdb_validate wrapper with backup
|
---|
1175 | * handling.
|
---|
1176 | */
|
---|
1177 |
|
---|
1178 | /* this structure eliminates the need for a global overall status for
|
---|
1179 | * the traverse-copy */
|
---|
1180 | struct tdb_copy_data {
|
---|
1181 | struct tdb_context *dst;
|
---|
1182 | bool success;
|
---|
1183 | };
|
---|
1184 |
|
---|
1185 | static int traverse_copy_fn(struct tdb_context *tdb, TDB_DATA key,
|
---|
1186 | TDB_DATA dbuf, void *private_data)
|
---|
1187 | {
|
---|
1188 | struct tdb_copy_data *data = (struct tdb_copy_data *)private_data;
|
---|
1189 |
|
---|
1190 | if (tdb_store(data->dst, key, dbuf, TDB_INSERT) != 0) {
|
---|
1191 | DEBUG(4, ("Failed to insert into %s: %s\n", tdb_name(data->dst),
|
---|
1192 | strerror(errno)));
|
---|
1193 | data->success = False;
|
---|
1194 | return 1;
|
---|
1195 | }
|
---|
1196 | return 0;
|
---|
1197 | }
|
---|
1198 |
|
---|
1199 | static int tdb_copy(struct tdb_context *src, struct tdb_context *dst)
|
---|
1200 | {
|
---|
1201 | struct tdb_copy_data data;
|
---|
1202 | int count;
|
---|
1203 |
|
---|
1204 | data.dst = dst;
|
---|
1205 | data.success = True;
|
---|
1206 |
|
---|
1207 | count = tdb_traverse(src, traverse_copy_fn, (void *)(&data));
|
---|
1208 | if ((count < 0) || (data.success == False)) {
|
---|
1209 | return -1;
|
---|
1210 | }
|
---|
1211 | return count;
|
---|
1212 | }
|
---|
1213 |
|
---|
1214 | static int tdb_verify_basic(struct tdb_context *tdb)
|
---|
1215 | {
|
---|
1216 | return tdb_traverse(tdb, NULL, NULL);
|
---|
1217 | }
|
---|
1218 |
|
---|
1219 | /* this backup function is essentially taken from lib/tdb/tools/tdbbackup.tdb
|
---|
1220 | */
|
---|
1221 | static int tdb_backup(TALLOC_CTX *ctx, const char *src_path,
|
---|
1222 | const char *dst_path, int hash_size)
|
---|
1223 | {
|
---|
1224 | struct tdb_context *src_tdb = NULL;
|
---|
1225 | struct tdb_context *dst_tdb = NULL;
|
---|
1226 | char *tmp_path = NULL;
|
---|
1227 | struct stat st;
|
---|
1228 | int count1, count2;
|
---|
1229 | int saved_errno = 0;
|
---|
1230 | int ret = -1;
|
---|
1231 |
|
---|
1232 | if (stat(src_path, &st) != 0) {
|
---|
1233 | DEBUG(3, ("Could not stat '%s': %s\n", src_path,
|
---|
1234 | strerror(errno)));
|
---|
1235 | goto done;
|
---|
1236 | }
|
---|
1237 |
|
---|
1238 | /* open old tdb RDWR - so we can lock it */
|
---|
1239 | src_tdb = tdb_open_log(src_path, 0, TDB_DEFAULT, O_RDWR, 0);
|
---|
1240 | if (src_tdb == NULL) {
|
---|
1241 | DEBUG(3, ("Failed to open tdb '%s'\n", src_path));
|
---|
1242 | goto done;
|
---|
1243 | }
|
---|
1244 |
|
---|
1245 | if (tdb_lockall(src_tdb) != 0) {
|
---|
1246 | DEBUG(3, ("Failed to lock tdb '%s'\n", src_path));
|
---|
1247 | goto done;
|
---|
1248 | }
|
---|
1249 |
|
---|
1250 | tmp_path = talloc_asprintf(ctx, "%s%s", dst_path, ".tmp");
|
---|
1251 | unlink(tmp_path);
|
---|
1252 | dst_tdb = tdb_open_log(tmp_path,
|
---|
1253 | hash_size ? hash_size : tdb_hash_size(src_tdb),
|
---|
1254 | TDB_DEFAULT, O_RDWR | O_CREAT | O_EXCL,
|
---|
1255 | st.st_mode & 0777);
|
---|
1256 | if (dst_tdb == NULL) {
|
---|
1257 | DEBUG(3, ("Error creating tdb '%s': %s\n", tmp_path,
|
---|
1258 | strerror(errno)));
|
---|
1259 | saved_errno = errno;
|
---|
1260 | unlink(tmp_path);
|
---|
1261 | goto done;
|
---|
1262 | }
|
---|
1263 |
|
---|
1264 | count1 = tdb_copy(src_tdb, dst_tdb);
|
---|
1265 | if (count1 < 0) {
|
---|
1266 | DEBUG(3, ("Failed to copy tdb '%s': %s\n", src_path,
|
---|
1267 | strerror(errno)));
|
---|
1268 | tdb_close(dst_tdb);
|
---|
1269 | goto done;
|
---|
1270 | }
|
---|
1271 |
|
---|
1272 | /* reopen ro and do basic verification */
|
---|
1273 | tdb_close(dst_tdb);
|
---|
1274 | dst_tdb = tdb_open_log(tmp_path, 0, TDB_DEFAULT, O_RDONLY, 0);
|
---|
1275 | if (!dst_tdb) {
|
---|
1276 | DEBUG(3, ("Failed to reopen tdb '%s': %s\n", tmp_path,
|
---|
1277 | strerror(errno)));
|
---|
1278 | goto done;
|
---|
1279 | }
|
---|
1280 | count2 = tdb_verify_basic(dst_tdb);
|
---|
1281 | if (count2 != count1) {
|
---|
1282 | DEBUG(3, ("Failed to verify result of copying tdb '%s'.\n",
|
---|
1283 | src_path));
|
---|
1284 | tdb_close(dst_tdb);
|
---|
1285 | goto done;
|
---|
1286 | }
|
---|
1287 |
|
---|
1288 | DEBUG(10, ("tdb_backup: successfully copied %d entries\n", count1));
|
---|
1289 |
|
---|
1290 | /* make sure the new tdb has reached stable storage
|
---|
1291 | * then rename it to its destination */
|
---|
1292 | fsync(tdb_fd(dst_tdb));
|
---|
1293 | tdb_close(dst_tdb);
|
---|
1294 | unlink(dst_path);
|
---|
1295 | if (rename(tmp_path, dst_path) != 0) {
|
---|
1296 | DEBUG(3, ("Failed to rename '%s' to '%s': %s\n",
|
---|
1297 | tmp_path, dst_path, strerror(errno)));
|
---|
1298 | goto done;
|
---|
1299 | }
|
---|
1300 |
|
---|
1301 | /* success */
|
---|
1302 | ret = 0;
|
---|
1303 |
|
---|
1304 | done:
|
---|
1305 | if (src_tdb != NULL) {
|
---|
1306 | tdb_close(src_tdb);
|
---|
1307 | }
|
---|
1308 | if (tmp_path != NULL) {
|
---|
1309 | unlink(tmp_path);
|
---|
1310 | TALLOC_FREE(tmp_path);
|
---|
1311 | }
|
---|
1312 | if (saved_errno != 0) {
|
---|
1313 | errno = saved_errno;
|
---|
1314 | }
|
---|
1315 | return ret;
|
---|
1316 | }
|
---|
1317 |
|
---|
1318 | static int rename_file_with_suffix(TALLOC_CTX *ctx, const char *path,
|
---|
1319 | const char *suffix)
|
---|
1320 | {
|
---|
1321 | int ret = -1;
|
---|
1322 | char *dst_path;
|
---|
1323 |
|
---|
1324 | dst_path = talloc_asprintf(ctx, "%s%s", path, suffix);
|
---|
1325 |
|
---|
1326 | ret = (rename(path, dst_path) != 0);
|
---|
1327 |
|
---|
1328 | if (ret == 0) {
|
---|
1329 | DEBUG(5, ("moved '%s' to '%s'\n", path, dst_path));
|
---|
1330 | } else if (errno == ENOENT) {
|
---|
1331 | DEBUG(3, ("file '%s' does not exist - so not moved\n", path));
|
---|
1332 | ret = 0;
|
---|
1333 | } else {
|
---|
1334 | DEBUG(3, ("error renaming %s to %s: %s\n", path, dst_path,
|
---|
1335 | strerror(errno)));
|
---|
1336 | }
|
---|
1337 |
|
---|
1338 | TALLOC_FREE(dst_path);
|
---|
1339 | return ret;
|
---|
1340 | }
|
---|
1341 |
|
---|
1342 | /*
|
---|
1343 | * do a backup of a tdb, moving the destination out of the way first
|
---|
1344 | */
|
---|
1345 | static int tdb_backup_with_rotate(TALLOC_CTX *ctx, const char *src_path,
|
---|
1346 | const char *dst_path, int hash_size,
|
---|
1347 | const char *rotate_suffix,
|
---|
1348 | bool retry_norotate_if_nospc,
|
---|
1349 | bool rename_as_last_resort_if_nospc)
|
---|
1350 | {
|
---|
1351 | int ret;
|
---|
1352 |
|
---|
1353 | rename_file_with_suffix(ctx, dst_path, rotate_suffix);
|
---|
1354 |
|
---|
1355 | ret = tdb_backup(ctx, src_path, dst_path, hash_size);
|
---|
1356 |
|
---|
1357 | if (ret != 0) {
|
---|
1358 | DEBUG(10, ("backup of %s failed: %s\n", src_path, strerror(errno)));
|
---|
1359 | }
|
---|
1360 | if ((ret != 0) && (errno == ENOSPC) && retry_norotate_if_nospc)
|
---|
1361 | {
|
---|
1362 | char *rotate_path = talloc_asprintf(ctx, "%s%s", dst_path,
|
---|
1363 | rotate_suffix);
|
---|
1364 | DEBUG(10, ("backup of %s failed due to lack of space\n",
|
---|
1365 | src_path));
|
---|
1366 | DEBUGADD(10, ("trying to free some space by removing rotated "
|
---|
1367 | "dst %s\n", rotate_path));
|
---|
1368 | if (unlink(rotate_path) == -1) {
|
---|
1369 | DEBUG(10, ("unlink of %s failed: %s\n", rotate_path,
|
---|
1370 | strerror(errno)));
|
---|
1371 | } else {
|
---|
1372 | ret = tdb_backup(ctx, src_path, dst_path, hash_size);
|
---|
1373 | }
|
---|
1374 | TALLOC_FREE(rotate_path);
|
---|
1375 | }
|
---|
1376 |
|
---|
1377 | if ((ret != 0) && (errno == ENOSPC) && rename_as_last_resort_if_nospc)
|
---|
1378 | {
|
---|
1379 | DEBUG(10, ("backup of %s failed due to lack of space\n",
|
---|
1380 | src_path));
|
---|
1381 | DEBUGADD(10, ("using 'rename' as a last resort\n"));
|
---|
1382 | ret = rename(src_path, dst_path);
|
---|
1383 | }
|
---|
1384 |
|
---|
1385 | return ret;
|
---|
1386 | }
|
---|
1387 |
|
---|
1388 | /*
|
---|
1389 | * validation function with backup handling:
|
---|
1390 | *
|
---|
1391 | * - calls tdb_validate
|
---|
1392 | * - if the tdb is ok, create a backup "name.bak", possibly moving
|
---|
1393 | * existing backup to name.bak.old,
|
---|
1394 | * return 0 (success) even if the backup fails
|
---|
1395 | * - if the tdb is corrupt:
|
---|
1396 | * - move the tdb to "name.corrupt"
|
---|
1397 | * - check if there is valid backup.
|
---|
1398 | * if so, restore the backup.
|
---|
1399 | * if restore is successful, return 0 (success),
|
---|
1400 | * - otherwise return -1 (failure)
|
---|
1401 | */
|
---|
1402 | int tdb_validate_and_backup(const char *tdb_path,
|
---|
1403 | tdb_validate_data_func validate_fn)
|
---|
1404 | {
|
---|
1405 | int ret = -1;
|
---|
1406 | const char *backup_suffix = ".bak";
|
---|
1407 | const char *corrupt_suffix = ".corrupt";
|
---|
1408 | const char *rotate_suffix = ".old";
|
---|
1409 | char *tdb_path_backup;
|
---|
1410 | struct stat st;
|
---|
1411 | TALLOC_CTX *ctx = NULL;
|
---|
1412 |
|
---|
1413 | ctx = talloc_new(NULL);
|
---|
1414 | if (ctx == NULL) {
|
---|
1415 | DEBUG(0, ("tdb_validate_and_backup: out of memory\n"));
|
---|
1416 | goto done;
|
---|
1417 | }
|
---|
1418 |
|
---|
1419 | tdb_path_backup = talloc_asprintf(ctx, "%s%s", tdb_path, backup_suffix);
|
---|
1420 |
|
---|
1421 | ret = tdb_validate_open(tdb_path, validate_fn);
|
---|
1422 |
|
---|
1423 | if (ret == 0) {
|
---|
1424 | DEBUG(1, ("tdb '%s' is valid\n", tdb_path));
|
---|
1425 | ret = tdb_backup_with_rotate(ctx, tdb_path, tdb_path_backup, 0,
|
---|
1426 | rotate_suffix, True, False);
|
---|
1427 | if (ret != 0) {
|
---|
1428 | DEBUG(1, ("Error creating backup of tdb '%s'\n",
|
---|
1429 | tdb_path));
|
---|
1430 | /* the actual validation was successful: */
|
---|
1431 | ret = 0;
|
---|
1432 | } else {
|
---|
1433 | DEBUG(1, ("Created backup '%s' of tdb '%s'\n",
|
---|
1434 | tdb_path_backup, tdb_path));
|
---|
1435 | }
|
---|
1436 | } else {
|
---|
1437 | DEBUG(1, ("tdb '%s' is invalid\n", tdb_path));
|
---|
1438 |
|
---|
1439 | ret =stat(tdb_path_backup, &st);
|
---|
1440 | if (ret != 0) {
|
---|
1441 | DEBUG(5, ("Could not stat '%s': %s\n", tdb_path_backup,
|
---|
1442 | strerror(errno)));
|
---|
1443 | DEBUG(1, ("No backup found.\n"));
|
---|
1444 | } else {
|
---|
1445 | DEBUG(1, ("backup '%s' found.\n", tdb_path_backup));
|
---|
1446 | ret = tdb_validate_open(tdb_path_backup, validate_fn);
|
---|
1447 | if (ret != 0) {
|
---|
1448 | DEBUG(1, ("Backup '%s' is invalid.\n",
|
---|
1449 | tdb_path_backup));
|
---|
1450 | }
|
---|
1451 | }
|
---|
1452 |
|
---|
1453 | if (ret != 0) {
|
---|
1454 | int renamed = rename_file_with_suffix(ctx, tdb_path,
|
---|
1455 | corrupt_suffix);
|
---|
1456 | if (renamed != 0) {
|
---|
1457 | DEBUG(1, ("Error moving tdb to '%s%s'\n",
|
---|
1458 | tdb_path, corrupt_suffix));
|
---|
1459 | } else {
|
---|
1460 | DEBUG(1, ("Corrupt tdb stored as '%s%s'\n",
|
---|
1461 | tdb_path, corrupt_suffix));
|
---|
1462 | }
|
---|
1463 | goto done;
|
---|
1464 | }
|
---|
1465 |
|
---|
1466 | DEBUG(1, ("valid backup '%s' found\n", tdb_path_backup));
|
---|
1467 | ret = tdb_backup_with_rotate(ctx, tdb_path_backup, tdb_path, 0,
|
---|
1468 | corrupt_suffix, True, True);
|
---|
1469 | if (ret != 0) {
|
---|
1470 | DEBUG(1, ("Error restoring backup from '%s'\n",
|
---|
1471 | tdb_path_backup));
|
---|
1472 | } else {
|
---|
1473 | DEBUG(1, ("Restored tdb backup from '%s'\n",
|
---|
1474 | tdb_path_backup));
|
---|
1475 | }
|
---|
1476 | }
|
---|
1477 |
|
---|
1478 | done:
|
---|
1479 | TALLOC_FREE(ctx);
|
---|
1480 | return ret;
|
---|
1481 | }
|
---|