lib: uid_wrapper: Fix setgroups and syscall detection on a system without native...
[Samba.git] / lib / dbwrap / dbwrap.c
blobd75c714ef1fe41577757be6fb37f56f4ba8b9fd4
1 /*
2 Unix SMB/CIFS implementation.
3 Database interface wrapper
4 Copyright (C) Jim McDonough <jmcd@us.ibm.com> 2006
6 Major code contributions from Aleksey Fedoseev (fedoseev@ru.ibm.com)
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "dbwrap/dbwrap.h"
24 #include "dbwrap/dbwrap_private.h"
25 #include "lib/util/util_tdb.h"
28 * Fall back using fetch if no genuine exists operation is provided
31 static int dbwrap_fallback_exists(struct db_context *db, TDB_DATA key)
33 NTSTATUS status = dbwrap_parse_record(db, key, NULL, NULL);
34 return NT_STATUS_IS_OK(status) ? 1 : 0;
37 static int delete_record(struct db_record *rec, void *data)
39 NTSTATUS status = dbwrap_record_delete(rec);
40 return NT_STATUS_IS_OK(status) ? 0 : -1;
44 * Fallback wipe implementation using traverse and delete if no genuine
45 * wipe operation is provided
47 static int dbwrap_fallback_wipe(struct db_context *db)
49 NTSTATUS status = dbwrap_trans_traverse(db, delete_record, NULL);
50 return NT_STATUS_IS_OK(status) ? 0 : -1;
53 static int do_nothing(struct db_record *rec, void *unused)
55 return 0;
59 * Fallback check operation: just traverse.
61 static int dbwrap_fallback_check(struct db_context *db)
63 NTSTATUS status = dbwrap_traverse_read(db, do_nothing, NULL, NULL);
64 return NT_STATUS_IS_OK(status) ? 0 : -1;
68 * Wrapper functions for the backend methods
71 TDB_DATA dbwrap_record_get_key(const struct db_record *rec)
73 return rec->key;
76 TDB_DATA dbwrap_record_get_value(const struct db_record *rec)
78 return rec->value;
81 NTSTATUS dbwrap_record_store(struct db_record *rec, TDB_DATA data, int flags)
83 NTSTATUS status;
84 struct db_context *db;
86 status = rec->store(rec, data, flags);
87 if (!NT_STATUS_IS_OK(status)) {
88 return status;
90 db = rec->db;
91 if (db->stored_callback != NULL) {
92 db->stored_callback(db, rec,
93 db->stored_callback_private_data);
95 return NT_STATUS_OK;
98 void dbwrap_set_stored_callback(
99 struct db_context *db,
100 void (*cb)(struct db_context *db, struct db_record *rec,
101 void *private_data),
102 void *private_data)
104 db->stored_callback = cb;
105 db->stored_callback_private_data = private_data;
108 NTSTATUS dbwrap_record_delete(struct db_record *rec)
110 NTSTATUS status;
111 struct db_context *db;
113 status = rec->delete_rec(rec);
114 if (!NT_STATUS_IS_OK(status)) {
115 return status;
117 db = rec->db;
118 if (db->stored_callback != NULL) {
119 db->stored_callback(db, rec,
120 db->stored_callback_private_data);
122 return NT_STATUS_OK;
125 struct dbwrap_lock_order_state {
126 struct db_context **locked_dbs;
127 struct db_context *db;
130 static void debug_lock_order(int level, struct db_context *dbs[])
132 int i;
133 DEBUG(level, ("lock order: "));
134 for (i=0; i<DBWRAP_LOCK_ORDER_MAX; i++) {
135 DEBUGADD(level, (" %d:%s", i + 1, dbs[i] ? dbs[i]->name : "<none>"));
137 DEBUGADD(level, ("\n"));
140 static int dbwrap_lock_order_state_destructor(
141 struct dbwrap_lock_order_state *s)
143 int idx = s->db->lock_order - 1;
145 DEBUG(5, ("release lock order %d for %s\n",
146 (int)s->db->lock_order, s->db->name));
148 if (s->locked_dbs[idx] != s->db) {
149 DEBUG(0, ("locked db at lock order %d is %s, expected %s\n",
150 idx + 1, s->locked_dbs[idx]->name, s->db->name));
151 debug_lock_order(0, s->locked_dbs);
152 smb_panic("inconsistent lock_order\n");
155 s->locked_dbs[idx] = NULL;
157 debug_lock_order(10, s->locked_dbs);
159 return 0;
163 static struct dbwrap_lock_order_state *dbwrap_check_lock_order(
164 struct db_context *db, TALLOC_CTX *mem_ctx)
166 int idx;
167 static struct db_context *locked_dbs[DBWRAP_LOCK_ORDER_MAX];
168 struct dbwrap_lock_order_state *state = NULL;
170 if (!DBWRAP_LOCK_ORDER_VALID(db->lock_order)) {
171 DEBUG(0,("Invalid lock order %d of %s\n",
172 (int)db->lock_order, db->name));
173 smb_panic("invalid lock_order\n");
174 return NULL;
177 DEBUG(5, ("check lock order %d for %s\n",
178 (int)db->lock_order, db->name));
181 for (idx=db->lock_order - 1; idx < DBWRAP_LOCK_ORDER_MAX; idx++) {
182 if (locked_dbs[idx] != NULL) {
183 DEBUG(0, ("Lock order violation: Trying %s at %d while %s at %d is locked\n",
184 db->name, (int)db->lock_order, locked_dbs[idx]->name, idx + 1));
185 debug_lock_order(0, locked_dbs);
186 smb_panic("invalid lock_order");
187 return NULL;
191 state = talloc(mem_ctx, struct dbwrap_lock_order_state);
192 if (state == NULL) {
193 DEBUG(1, ("talloc failed\n"));
194 return NULL;
196 state->db = db;
197 state->locked_dbs = locked_dbs;
198 talloc_set_destructor(state, dbwrap_lock_order_state_destructor);
200 locked_dbs[db->lock_order - 1] = db;
202 debug_lock_order(10, locked_dbs);
204 return state;
207 static struct db_record *dbwrap_fetch_locked_internal(
208 struct db_context *db, TALLOC_CTX *mem_ctx, TDB_DATA key,
209 struct db_record *(*db_fn)(struct db_context *db, TALLOC_CTX *mem_ctx,
210 TDB_DATA key))
212 struct db_record *rec;
213 struct dbwrap_lock_order_state *lock_order = NULL;
215 if (db->lock_order != DBWRAP_LOCK_ORDER_NONE) {
216 lock_order = dbwrap_check_lock_order(db, mem_ctx);
217 if (lock_order == NULL) {
218 return NULL;
221 rec = db_fn(db, mem_ctx, key);
222 if (rec == NULL) {
223 TALLOC_FREE(lock_order);
224 return NULL;
226 (void)talloc_steal(rec, lock_order);
227 rec->db = db;
228 return rec;
231 struct db_record *dbwrap_fetch_locked(struct db_context *db,
232 TALLOC_CTX *mem_ctx,
233 TDB_DATA key)
235 return dbwrap_fetch_locked_internal(db, mem_ctx, key,
236 db->fetch_locked);
239 struct db_record *dbwrap_try_fetch_locked(struct db_context *db,
240 TALLOC_CTX *mem_ctx,
241 TDB_DATA key)
243 return dbwrap_fetch_locked_internal(
244 db, mem_ctx, key,
245 db->try_fetch_locked
246 ? db->try_fetch_locked : db->fetch_locked);
249 struct db_context *dbwrap_record_get_db(struct db_record *rec)
251 return rec->db;
254 struct dbwrap_fetch_state {
255 TALLOC_CTX *mem_ctx;
256 TDB_DATA data;
259 static void dbwrap_fetch_parser(TDB_DATA key, TDB_DATA data,
260 void *private_data)
262 struct dbwrap_fetch_state *state =
263 (struct dbwrap_fetch_state *)private_data;
265 state->data.dsize = data.dsize;
266 state->data.dptr = (uint8_t *)talloc_memdup(state->mem_ctx, data.dptr,
267 data.dsize);
270 NTSTATUS dbwrap_fetch(struct db_context *db, TALLOC_CTX *mem_ctx,
271 TDB_DATA key, TDB_DATA *value)
273 struct dbwrap_fetch_state state;
274 NTSTATUS status;
276 if (value == NULL) {
277 return NT_STATUS_INVALID_PARAMETER;
280 state.mem_ctx = mem_ctx;
282 status = dbwrap_parse_record(db, key, dbwrap_fetch_parser, &state);
283 if (!NT_STATUS_IS_OK(status)) {
284 return status;
286 if ((state.data.dsize != 0) && (state.data.dptr == NULL)) {
287 return NT_STATUS_NO_MEMORY;
289 *value = state.data;
290 return NT_STATUS_OK;
293 bool dbwrap_exists(struct db_context *db, TDB_DATA key)
295 int result;
296 if (db->exists != NULL) {
297 result = db->exists(db, key);
298 } else {
299 result = dbwrap_fallback_exists(db,key);
301 return (result == 1);
304 NTSTATUS dbwrap_store(struct db_context *db, TDB_DATA key,
305 TDB_DATA data, int flags)
307 struct db_record *rec;
308 NTSTATUS status;
309 TALLOC_CTX *frame = talloc_stackframe();
311 rec = dbwrap_fetch_locked(db, frame, key);
312 if (rec == NULL) {
313 TALLOC_FREE(frame);
314 return NT_STATUS_NO_MEMORY;
317 status = dbwrap_record_store(rec, data, flags);
318 TALLOC_FREE(frame);
319 return status;
322 NTSTATUS dbwrap_delete(struct db_context *db, TDB_DATA key)
324 struct db_record *rec;
325 NTSTATUS status;
326 TALLOC_CTX *frame = talloc_stackframe();
328 rec = dbwrap_fetch_locked(db, frame, key);
329 if (rec == NULL) {
330 TALLOC_FREE(frame);
331 return NT_STATUS_NO_MEMORY;
333 status = dbwrap_record_delete(rec);
334 TALLOC_FREE(frame);
335 return status;
338 NTSTATUS dbwrap_traverse(struct db_context *db,
339 int (*f)(struct db_record*, void*),
340 void *private_data,
341 int *count)
343 int ret = db->traverse(db, f, private_data);
345 if (ret < 0) {
346 return NT_STATUS_INTERNAL_DB_CORRUPTION;
349 if (count != NULL) {
350 *count = ret;
353 return NT_STATUS_OK;
356 NTSTATUS dbwrap_traverse_read(struct db_context *db,
357 int (*f)(struct db_record*, void*),
358 void *private_data,
359 int *count)
361 int ret = db->traverse_read(db, f, private_data);
363 if (ret < 0) {
364 return NT_STATUS_INTERNAL_DB_CORRUPTION;
367 if (count != NULL) {
368 *count = ret;
371 return NT_STATUS_OK;
374 static void dbwrap_null_parser(TDB_DATA key, TDB_DATA val, void* data)
376 return;
379 NTSTATUS dbwrap_parse_record(struct db_context *db, TDB_DATA key,
380 void (*parser)(TDB_DATA key, TDB_DATA data,
381 void *private_data),
382 void *private_data)
384 if (parser == NULL) {
385 parser = dbwrap_null_parser;
387 return db->parse_record(db, key, parser, private_data);
390 int dbwrap_wipe(struct db_context *db)
392 if (db->wipe == NULL) {
393 return dbwrap_fallback_wipe(db);
395 return db->wipe(db);
398 int dbwrap_hash_size(struct db_context *db)
400 return db->hash_size;
403 int dbwrap_check(struct db_context *db)
405 if (db->check == NULL) {
406 return dbwrap_fallback_check(db);
408 return db->check(db);
411 int dbwrap_get_seqnum(struct db_context *db)
413 return db->get_seqnum(db);
416 int dbwrap_transaction_start(struct db_context *db)
418 if (!db->persistent) {
420 * dbwrap_ctdb has two different data models for persistent
421 * and non-persistent databases. Transactions are supported
422 * only for the persistent databases. This check is here to
423 * prevent breakages of the cluster case, autobuild at this
424 * point only tests non-clustered Samba. Before removing this
425 * check, please make sure that this facility has also been
426 * added to dbwrap_ctdb.
428 * Thanks, vl
430 DEBUG(1, ("transactions not supported on non-persistent "
431 "database %s\n", db->name));
432 return -1;
434 return db->transaction_start(db);
437 NTSTATUS dbwrap_transaction_start_nonblock(struct db_context *db)
439 if (db->transaction_start_nonblock) {
440 return db->transaction_start_nonblock(db);
441 } else {
442 return dbwrap_transaction_start(db) == 0 ? NT_STATUS_OK
443 : NT_STATUS_UNSUCCESSFUL;
447 int dbwrap_transaction_commit(struct db_context *db)
449 return db->transaction_commit(db);
452 int dbwrap_transaction_cancel(struct db_context *db)
454 return db->transaction_cancel(db);
457 void dbwrap_db_id(struct db_context *db, const uint8_t **id, size_t *idlen)
459 db->id(db, id, idlen);
462 bool dbwrap_is_persistent(struct db_context *db)
464 return db->persistent;
467 const char *dbwrap_name(struct db_context *db)
469 return db->name;