r25068: Older samba3 DCs will return DCERPC_FAULT_OP_RNG_ERROR for every opcode on the
[Samba.git] / source / lib / conn_tdb.c
blob979f202df7f441780dbb9b7ca18659869adb36be
1 /*
2 Unix SMB/CIFS implementation.
3 Low-level connections.tdb access functions
4 Copyright (C) Volker Lendecke 2007
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include "includes.h"
23 static struct db_context *connections_db_ctx(BOOL rw)
25 static struct db_context *db_ctx;
27 if (db_ctx != NULL) {
28 return db_ctx;
31 if (rw) {
32 db_ctx = db_open(NULL, lock_path("connections.tdb"), 0,
33 TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
34 O_RDWR | O_CREAT, 0644);
36 else {
37 db_ctx = db_open(NULL, lock_path("connections.tdb"), 0,
38 TDB_DEFAULT, O_RDONLY, 0);
41 return db_ctx;
44 struct db_record *connections_fetch_record(TALLOC_CTX *mem_ctx,
45 TDB_DATA key)
47 struct db_context *ctx = connections_db_ctx(True);
49 if (ctx == NULL) {
50 return NULL;
53 return ctx->fetch_locked(ctx, mem_ctx, key);
56 struct db_record *connections_fetch_entry(TALLOC_CTX *mem_ctx,
57 connection_struct *conn,
58 const char *name)
60 struct connections_key ckey;
61 TDB_DATA key;
63 ZERO_STRUCT(ckey);
64 ckey.pid = procid_self();
65 ckey.cnum = conn ? conn->cnum : -1;
66 strlcpy(ckey.name, name, sizeof(ckey.name));
68 key.dsize = sizeof(ckey);
69 key.dptr = (uint8 *)&ckey;
71 return connections_fetch_record(mem_ctx, key);
74 struct conn_traverse_state {
75 int (*fn)(struct db_record *rec,
76 const struct connections_key *key,
77 const struct connections_data *data,
78 void *private_data);
79 void *private_data;
82 static int conn_traverse_fn(struct db_record *rec, void *private_data)
84 struct conn_traverse_state *state =
85 (struct conn_traverse_state *)private_data;
87 if ((rec->key.dsize != sizeof(struct connections_key))
88 || (rec->value.dsize != sizeof(struct connections_data))) {
89 return 0;
92 return state->fn(rec, (const struct connections_key *)rec->key.dptr,
93 (const struct connections_data *)rec->value.dptr,
94 state->private_data);
97 int connections_traverse(int (*fn)(struct db_record *rec,
98 void *private_data),
99 void *private_data)
101 struct db_context *ctx = connections_db_ctx(False);
103 if (ctx == NULL) {
104 return -1;
107 return ctx->traverse(ctx, fn, private_data);
110 int connections_forall(int (*fn)(struct db_record *rec,
111 const struct connections_key *key,
112 const struct connections_data *data,
113 void *private_data),
114 void *private_data)
116 struct conn_traverse_state state;
118 state.fn = fn;
119 state.private_data = private_data;
121 return connections_traverse(conn_traverse_fn, (void *)&state);
124 BOOL connections_init(BOOL rw)
126 return (connections_db_ctx(rw) != NULL);