lib: Avoid an includes.h
[sfrench/samba-autobuild/.git] / lib / util / util_tdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    tdb utility functions
5
6    Copyright (C) Andrew Tridgell 1992-2006
7    Copyright (C) Volker Lendecke 2007-2011
8
9    This program is free software; you can redistribute it and/or modify
10    it under the terms of the GNU General Public License as published by
11    the Free Software Foundation; either version 3 of the License, or
12    (at your option) any later version.
13    
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17    GNU General Public License for more details.
18    
19    You should have received a copy of the GNU General Public License
20    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 */
22
23 #include "replace.h"
24 #include <talloc.h>
25 #include "libcli/util/ntstatus.h"
26 #include "lib/util/memory.h"
27 #include "lib/util/byteorder.h"
28 #include "system/filesys.h"
29 #include "../lib/tdb/include/tdb.h"
30 #include "../lib/util/util_tdb.h"
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 /***************************************************************
36  Make a TDB_DATA and keep the const warning in one place
37 ****************************************************************/
38
39 TDB_DATA make_tdb_data(const uint8_t *dptr, size_t dsize)
40 {
41         TDB_DATA ret;
42         ret.dptr = discard_const_p(uint8_t, dptr);
43         ret.dsize = dsize;
44         return ret;
45 }
46
47 bool tdb_data_equal(TDB_DATA t1, TDB_DATA t2)
48 {
49         if (t1.dsize != t2.dsize) {
50                 return false;
51         }
52         return (memcmp(t1.dptr, t2.dptr, t1.dsize) == 0);
53 }
54
55 bool tdb_data_is_empty(TDB_DATA d)
56 {
57         return (d.dsize == 0) || (d.dptr == NULL);
58 }
59
60 TDB_DATA string_tdb_data(const char *string)
61 {
62         return make_tdb_data((const uint8_t *)string, string ? strlen(string) : 0 );
63 }
64
65 TDB_DATA string_term_tdb_data(const char *string)
66 {
67         return make_tdb_data((const uint8_t *)string, string ? strlen(string) + 1 : 0);
68 }
69
70 TDB_DATA tdb_data_talloc_copy(TALLOC_CTX* mem_ctx, TDB_DATA data) {
71         TDB_DATA ret = {
72                 .dptr  = (uint8_t *)talloc_size(mem_ctx, data.dsize+1),
73                 .dsize = data.dsize
74         };
75         if (ret.dptr == NULL) {
76                 ret.dsize = 0;
77         } else {
78                 memcpy(ret.dptr, data.dptr, data.dsize);
79                 ret.dptr[ret.dsize] = '\0';
80         }
81         return ret;
82 }
83
84
85 /****************************************************************************
86  Lock a chain by string. Return non-zero if lock failed.
87 ****************************************************************************/
88
89 int tdb_lock_bystring(struct tdb_context *tdb, const char *keyval)
90 {
91         TDB_DATA key = string_term_tdb_data(keyval);
92         
93         return tdb_chainlock(tdb, key);
94 }
95
96 /****************************************************************************
97  Unlock a chain by string.
98 ****************************************************************************/
99
100 void tdb_unlock_bystring(struct tdb_context *tdb, const char *keyval)
101 {
102         TDB_DATA key = string_term_tdb_data(keyval);
103
104         tdb_chainunlock(tdb, key);
105 }
106
107 /****************************************************************************
108  Read lock a chain by string. Return non-zero if lock failed.
109 ****************************************************************************/
110
111 int tdb_read_lock_bystring(struct tdb_context *tdb, const char *keyval)
112 {
113         TDB_DATA key = string_term_tdb_data(keyval);
114         
115         return tdb_chainlock_read(tdb, key);
116 }
117
118 /****************************************************************************
119  Read unlock a chain by string.
120 ****************************************************************************/
121
122 void tdb_read_unlock_bystring(struct tdb_context *tdb, const char *keyval)
123 {
124         TDB_DATA key = string_term_tdb_data(keyval);
125         
126         tdb_chainunlock_read(tdb, key);
127 }
128
129
130 /****************************************************************************
131  Fetch a int32_t value by a arbitrary blob key, return -1 if not found.
132  Output is int32_t in native byte order.
133 ****************************************************************************/
134
135 int32_t tdb_fetch_int32_byblob(struct tdb_context *tdb, TDB_DATA key)
136 {
137         TDB_DATA data;
138         int32_t ret;
139
140         data = tdb_fetch(tdb, key);
141         if (!data.dptr || data.dsize != sizeof(int32_t)) {
142                 SAFE_FREE(data.dptr);
143                 return -1;
144         }
145
146         ret = IVAL(data.dptr,0);
147         SAFE_FREE(data.dptr);
148         return ret;
149 }
150
151 /****************************************************************************
152  Fetch a int32_t value by string key, return -1 if not found.
153  Output is int32_t in native byte order.
154 ****************************************************************************/
155
156 int32_t tdb_fetch_int32(struct tdb_context *tdb, const char *keystr)
157 {
158         return tdb_fetch_int32_byblob(tdb, string_term_tdb_data(keystr));
159 }
160
161 /****************************************************************************
162  Store a int32_t value by an arbitrary blob key, return 0 on success, -ve on failure.
163  Input is int32_t in native byte order. Output in tdb is in little-endian.
164 ****************************************************************************/
165
166 int tdb_store_int32_byblob(struct tdb_context *tdb, TDB_DATA key, int32_t v)
167 {
168         TDB_DATA data;
169         int32_t v_store;
170
171         SIVAL(&v_store,0,v);
172         data.dptr = (unsigned char *)&v_store;
173         data.dsize = sizeof(int32_t);
174
175         return tdb_store(tdb, key, data, TDB_REPLACE);
176 }
177
178 /****************************************************************************
179  Store a int32_t value by string key, return 0 on success, -ve on failure.
180  Input is int32_t in native byte order. Output in tdb is in little-endian.
181 ****************************************************************************/
182
183 int tdb_store_int32(struct tdb_context *tdb, const char *keystr, int32_t v)
184 {
185         return tdb_store_int32_byblob(tdb, string_term_tdb_data(keystr), v);
186 }
187
188 /****************************************************************************
189  Fetch a uint32_t value by a arbitrary blob key, return false if not found.
190  Output is uint32_t in native byte order.
191 ****************************************************************************/
192
193 bool tdb_fetch_uint32_byblob(struct tdb_context *tdb, TDB_DATA key, uint32_t *value)
194 {
195         TDB_DATA data;
196
197         data = tdb_fetch(tdb, key);
198         if (!data.dptr || data.dsize != sizeof(uint32_t)) {
199                 SAFE_FREE(data.dptr);
200                 return false;
201         }
202
203         *value = IVAL(data.dptr,0);
204         SAFE_FREE(data.dptr);
205         return true;
206 }
207
208 /****************************************************************************
209  Fetch a uint32_t value by string key, return false if not found.
210  Output is uint32_t in native byte order.
211 ****************************************************************************/
212
213 bool tdb_fetch_uint32(struct tdb_context *tdb, const char *keystr, uint32_t *value)
214 {
215         return tdb_fetch_uint32_byblob(tdb, string_term_tdb_data(keystr), value);
216 }
217
218 /****************************************************************************
219  Store a uint32_t value by an arbitrary blob key, return true on success, false on failure.
220  Input is uint32_t in native byte order. Output in tdb is in little-endian.
221 ****************************************************************************/
222
223 bool tdb_store_uint32_byblob(struct tdb_context *tdb, TDB_DATA key, uint32_t value)
224 {
225         TDB_DATA data;
226         uint32_t v_store;
227         bool ret = true;
228
229         SIVAL(&v_store, 0, value);
230         data.dptr = (unsigned char *)&v_store;
231         data.dsize = sizeof(uint32_t);
232
233         if (tdb_store(tdb, key, data, TDB_REPLACE) != 0)
234                 ret = false;
235
236         return ret;
237 }
238
239 /****************************************************************************
240  Store a uint32_t value by string key, return true on success, false on failure.
241  Input is uint32_t in native byte order. Output in tdb is in little-endian.
242 ****************************************************************************/
243
244 bool tdb_store_uint32(struct tdb_context *tdb, const char *keystr, uint32_t value)
245 {
246         return tdb_store_uint32_byblob(tdb, string_term_tdb_data(keystr), value);
247 }
248 /****************************************************************************
249  Store a buffer by a null terminated string key.  Return 0 on success, -ve
250  on failure.
251 ****************************************************************************/
252
253 int tdb_store_bystring(struct tdb_context *tdb, const char *keystr, TDB_DATA data, int flags)
254 {
255         TDB_DATA key = string_term_tdb_data(keystr);
256         
257         return tdb_store(tdb, key, data, flags);
258 }
259
260 /****************************************************************************
261  Fetch a buffer using a null terminated string key.  Don't forget to call
262  free() on the result dptr.
263 ****************************************************************************/
264
265 TDB_DATA tdb_fetch_bystring(struct tdb_context *tdb, const char *keystr)
266 {
267         TDB_DATA key = string_term_tdb_data(keystr);
268
269         return tdb_fetch(tdb, key);
270 }
271
272 /****************************************************************************
273  Delete an entry using a null terminated string key. 
274 ****************************************************************************/
275
276 int tdb_delete_bystring(struct tdb_context *tdb, const char *keystr)
277 {
278         TDB_DATA key = string_term_tdb_data(keystr);
279
280         return tdb_delete(tdb, key);
281 }
282
283 /****************************************************************************
284  Atomic integer change. Returns old value. To create, set initial value in *oldval. 
285 ****************************************************************************/
286
287 int32_t tdb_change_int32_atomic(struct tdb_context *tdb, const char *keystr, int32_t *oldval, int32_t change_val)
288 {
289         int32_t val;
290         int32_t ret = -1;
291
292         if (tdb_lock_bystring(tdb, keystr) != 0)
293                 return -1;
294
295         if ((val = tdb_fetch_int32(tdb, keystr)) == -1) {
296                 /* The lookup failed */
297                 if (tdb_error(tdb) != TDB_ERR_NOEXIST) {
298                         /* but not because it didn't exist */
299                         goto err_out;
300                 }
301                 
302                 /* Start with 'old' value */
303                 val = *oldval;
304
305         } else {
306                 /* It worked, set return value (oldval) to tdb data */
307                 *oldval = val;
308         }
309
310         /* Increment value for storage and return next time */
311         val += change_val;
312                 
313         if (tdb_store_int32(tdb, keystr, val) != 0)
314                 goto err_out;
315
316         ret = 0;
317
318   err_out:
319
320         tdb_unlock_bystring(tdb, keystr);
321         return ret;
322 }
323
324 /****************************************************************************
325  Atomic unsigned integer change. Returns old value. To create, set initial value in *oldval. 
326 ****************************************************************************/
327
328 bool tdb_change_uint32_atomic(struct tdb_context *tdb, const char *keystr, uint32_t *oldval, uint32_t change_val)
329 {
330         uint32_t val;
331         bool ret = false;
332
333         if (tdb_lock_bystring(tdb, keystr) != 0)
334                 return false;
335
336         if (!tdb_fetch_uint32(tdb, keystr, &val)) {
337                 /* It failed */
338                 if (tdb_error(tdb) != TDB_ERR_NOEXIST) { 
339                         /* and not because it didn't exist */
340                         goto err_out;
341                 }
342
343                 /* Start with 'old' value */
344                 val = *oldval;
345
346         } else {
347                 /* it worked, set return value (oldval) to tdb data */
348                 *oldval = val;
349
350         }
351
352         /* get a new value to store */
353         val += change_val;
354                 
355         if (!tdb_store_uint32(tdb, keystr, val))
356                 goto err_out;
357
358         ret = true;
359
360   err_out:
361
362         tdb_unlock_bystring(tdb, keystr);
363         return ret;
364 }
365
366 /****************************************************************************
367  Allow tdb_delete to be used as a tdb_traversal_fn.
368 ****************************************************************************/
369
370 int tdb_traverse_delete_fn(struct tdb_context *the_tdb, TDB_DATA key, TDB_DATA dbuf,
371                      void *state)
372 {
373     return tdb_delete(the_tdb, key);
374 }
375
376 /****************************************************************************
377  Return an NTSTATUS from a TDB_ERROR
378 ****************************************************************************/
379
380 NTSTATUS map_nt_error_from_tdb(enum TDB_ERROR err)
381 {
382         NTSTATUS result = NT_STATUS_INTERNAL_ERROR;
383
384         switch (err) {
385         case TDB_SUCCESS:
386                 result = NT_STATUS_OK;
387                 break;
388         case TDB_ERR_CORRUPT:
389                 result = NT_STATUS_INTERNAL_DB_CORRUPTION;
390                 break;
391         case TDB_ERR_IO:
392                 result = NT_STATUS_UNEXPECTED_IO_ERROR;
393                 break;
394         case TDB_ERR_OOM:
395                 result = NT_STATUS_NO_MEMORY;
396                 break;
397         case TDB_ERR_EXISTS:
398                 result = NT_STATUS_OBJECT_NAME_COLLISION;
399                 break;
400
401         case TDB_ERR_LOCK:
402                 /*
403                  * TDB_ERR_LOCK is very broad, we could for example
404                  * distinguish between fcntl locks and invalid lock
405                  * sequences. So NT_STATUS_FILE_LOCK_CONFLICT is a
406                  * compromise.
407                  */
408                 result = NT_STATUS_FILE_LOCK_CONFLICT;
409                 break;
410
411         case TDB_ERR_NOLOCK:
412         case TDB_ERR_LOCK_TIMEOUT:
413                 /*
414                  * These two ones in the enum are not actually used
415                  */
416                 result = NT_STATUS_FILE_LOCK_CONFLICT;
417                 break;
418         case TDB_ERR_NOEXIST:
419                 result = NT_STATUS_NOT_FOUND;
420                 break;
421         case TDB_ERR_EINVAL:
422                 result = NT_STATUS_INVALID_PARAMETER;
423                 break;
424         case TDB_ERR_RDONLY:
425                 result = NT_STATUS_ACCESS_DENIED;
426                 break;
427         case TDB_ERR_NESTING:
428                 result = NT_STATUS_INTERNAL_ERROR;
429                 break;
430         };
431         return result;
432 }
433
434 int map_unix_error_from_tdb(enum TDB_ERROR err)
435 {
436         int result = EINVAL;
437
438         switch (err) {
439         case TDB_SUCCESS:
440                 result = 0;
441                 break;
442         case TDB_ERR_CORRUPT:
443                 result = EILSEQ;
444                 break;
445         case TDB_ERR_IO:
446                 result = EIO;
447                 break;
448         case TDB_ERR_OOM:
449                 result = ENOMEM;
450                 break;
451         case TDB_ERR_EXISTS:
452                 result = EEXIST;
453                 break;
454
455         case TDB_ERR_LOCK:
456                 /*
457                  * TDB_ERR_LOCK is very broad, we could for example
458                  * distinguish between fcntl locks and invalid lock
459                  * sequences. EWOULDBLOCK is wrong, but there is no real
460                  * generic lock error code in errno.h
461                  */
462                 result = EWOULDBLOCK;
463                 break;
464
465         case TDB_ERR_NOLOCK:
466         case TDB_ERR_LOCK_TIMEOUT:
467                 /*
468                  * These two ones in the enum are not actually used
469                  */
470                 result = ENOLCK;
471                 break;
472         case TDB_ERR_NOEXIST:
473                 result = ENOENT;
474                 break;
475         case TDB_ERR_EINVAL:
476                 result = EINVAL;
477                 break;
478         case TDB_ERR_RDONLY:
479                 result = EROFS;
480                 break;
481         case TDB_ERR_NESTING:
482                 /*
483                  * Well, this db is already busy...
484                  */
485                 result = EBUSY;
486                 break;
487         };
488         return result;
489 }
490
491 struct tdb_fetch_talloc_state {
492         TALLOC_CTX *mem_ctx;
493         uint8_t *buf;
494 };
495
496 static int tdb_fetch_talloc_parser(TDB_DATA key, TDB_DATA data,
497                                    void *private_data)
498 {
499         struct tdb_fetch_talloc_state *state = private_data;
500         state->buf = talloc_memdup(state->mem_ctx, data.dptr, data.dsize);
501         return 0;
502 }
503
504 int tdb_fetch_talloc(struct tdb_context *tdb, TDB_DATA key,
505                      TALLOC_CTX *mem_ctx, uint8_t **buf)
506 {
507         struct tdb_fetch_talloc_state state = { .mem_ctx = mem_ctx };
508         int ret;
509
510         ret = tdb_parse_record(tdb, key, tdb_fetch_talloc_parser, &state);
511         if (ret == -1) {
512                 enum TDB_ERROR err = tdb_error(tdb);
513                 return map_unix_error_from_tdb(err);
514         }
515
516         if (state.buf == NULL) {
517                 return ENOMEM;
518         }
519
520         *buf = state.buf;
521         return 0;
522 }