s4:gensec/gssapi: use gensec_gssapi_max_{input,wrapped}_size() for all backends
[Samba.git] / lib / tdb_wrap / tdb_wrap.c
bloba1bddf394869b214f71f1415b6f387ab53d34484
1 /*
2 Unix SMB/CIFS implementation.
3 TDB wrap functions
5 Copyright (C) Andrew Tridgell 2004
6 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2007
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "replace.h"
23 #include "lib/util/dlinklist.h"
24 #include "lib/util/debug.h"
25 #include "tdb_wrap.h"
28 Log tdb messages via DEBUG().
30 static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
31 const char *format, ...) PRINTF_ATTRIBUTE(3,4);
33 static void tdb_wrap_log(TDB_CONTEXT *tdb, enum tdb_debug_level level,
34 const char *format, ...)
36 va_list ap;
37 char *ptr = NULL;
38 int debuglevel = 0;
39 int ret;
41 switch (level) {
42 case TDB_DEBUG_FATAL:
43 debuglevel = 0;
44 break;
45 case TDB_DEBUG_ERROR:
46 debuglevel = 1;
47 break;
48 case TDB_DEBUG_WARNING:
49 debuglevel = 2;
50 break;
51 case TDB_DEBUG_TRACE:
52 debuglevel = 5;
53 break;
54 default:
55 debuglevel = 0;
58 va_start(ap, format);
59 ret = vasprintf(&ptr, format, ap);
60 va_end(ap);
62 if (ret != -1) {
63 const char *name = tdb_name(tdb);
64 DEBUG(debuglevel, ("tdb(%s): %s", name ? name : "unnamed", ptr));
65 free(ptr);
69 struct tdb_wrap_private {
70 struct tdb_context *tdb;
71 const char *name;
72 struct tdb_wrap_private *next, *prev;
75 static struct tdb_wrap_private *tdb_list;
77 /* destroy the last connection to a tdb */
78 static int tdb_wrap_private_destructor(struct tdb_wrap_private *w)
80 tdb_close(w->tdb);
81 DLIST_REMOVE(tdb_list, w);
82 return 0;
85 static struct tdb_wrap_private *tdb_wrap_private_open(TALLOC_CTX *mem_ctx,
86 const char *name,
87 int hash_size,
88 int tdb_flags,
89 int open_flags,
90 mode_t mode)
92 struct tdb_wrap_private *result;
93 struct tdb_logging_context lctx;
95 result = talloc(mem_ctx, struct tdb_wrap_private);
96 if (result == NULL) {
97 return NULL;
99 result->name = talloc_strdup(result, name);
100 if (result->name == NULL) {
101 goto fail;
104 lctx.log_fn = tdb_wrap_log;
105 lctx.log_private = NULL;
107 result->tdb = tdb_open_ex(name, hash_size, tdb_flags,
108 open_flags, mode, &lctx, NULL);
109 if (result->tdb == NULL) {
110 goto fail;
112 talloc_set_destructor(result, tdb_wrap_private_destructor);
113 DLIST_ADD(tdb_list, result);
114 return result;
116 fail:
117 TALLOC_FREE(result);
118 return NULL;
122 wrapped connection to a tdb database
123 to close just talloc_free() the tdb_wrap pointer
125 struct tdb_wrap *tdb_wrap_open(TALLOC_CTX *mem_ctx,
126 const char *name, int hash_size, int tdb_flags,
127 int open_flags, mode_t mode)
129 struct tdb_wrap *result;
130 struct tdb_wrap_private *w;
132 if (name == NULL) {
133 errno = EINVAL;
134 return NULL;
137 result = talloc(mem_ctx, struct tdb_wrap);
138 if (result == NULL) {
139 return NULL;
142 for (w=tdb_list;w;w=w->next) {
143 if (strcmp(name, w->name) == 0) {
144 break;
148 if (w == NULL) {
150 if (tdb_flags & TDB_MUTEX_LOCKING) {
151 if (!tdb_runtime_check_for_robust_mutexes()) {
152 tdb_flags &= ~TDB_MUTEX_LOCKING;
156 w = tdb_wrap_private_open(result, name, hash_size, tdb_flags,
157 open_flags, mode);
158 } else {
160 * Correctly use talloc_reference: The tdb will be
161 * closed when "w" is being freed. The caller never
162 * sees "w", so an incorrect use of talloc_free(w)
163 * instead of calling talloc_unlink is not possible.
164 * To avoid having to refcount ourselves, "w" will
165 * have multiple parents that hang off all the
166 * tdb_wrap's being returned from here. Those parents
167 * can be freed without problem.
169 if (talloc_reference(result, w) == NULL) {
170 goto fail;
173 if (w == NULL) {
174 goto fail;
176 result->tdb = w->tdb;
177 return result;
178 fail:
179 TALLOC_FREE(result);
180 return NULL;