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 3 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, see <http://www.gnu.org/licenses/>.
21 #include "system/filesys.h"
22 #include "smbd/globals.h"
23 #include "dbwrap/dbwrap.h"
24 #include "dbwrap/dbwrap_open.h"
26 #include "lib/conn_tdb.h"
28 static struct db_context *connections_db_ctx(bool rw)
30 static struct db_context *db_ctx;
37 open_flags = rw ? (O_RDWR|O_CREAT) : O_RDONLY;
39 db_ctx = db_open(NULL, lock_path("connections.tdb"), 0,
40 TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH|TDB_DEFAULT,
41 open_flags, 0644, DBWRAP_LOCK_ORDER_1);
45 static struct db_record *connections_fetch_record(TALLOC_CTX *mem_ctx,
48 struct db_context *ctx = connections_db_ctx(True);
54 return dbwrap_fetch_locked(ctx, mem_ctx, key);
57 struct db_record *connections_fetch_entry_ext(TALLOC_CTX *mem_ctx,
62 struct connections_key ckey;
68 strlcpy(ckey.name, name, sizeof(ckey.name));
70 key.dsize = sizeof(ckey);
71 key.dptr = (uint8 *)&ckey;
73 return connections_fetch_record(mem_ctx, key);
76 struct db_record *connections_fetch_entry(TALLOC_CTX *mem_ctx,
77 connection_struct *conn,
80 struct server_id id = messaging_server_id(conn->sconn->msg_ctx);
81 return connections_fetch_entry_ext(mem_ctx, id, conn->cnum, name);
85 struct conn_traverse_state {
86 int (*fn)(struct db_record *rec,
87 const struct connections_key *key,
88 const struct connections_data *data,
93 static int conn_traverse_fn(struct db_record *rec, void *private_data)
97 struct conn_traverse_state *state =
98 (struct conn_traverse_state *)private_data;
100 key = dbwrap_record_get_key(rec);
101 value = dbwrap_record_get_value(rec);
103 if ((key.dsize != sizeof(struct connections_key))
104 || (value.dsize != sizeof(struct connections_data))) {
108 return state->fn(rec, (const struct connections_key *)key.dptr,
109 (const struct connections_data *)value.dptr,
110 state->private_data);
113 int connections_forall(int (*fn)(struct db_record *rec,
114 const struct connections_key *key,
115 const struct connections_data *data,
119 struct db_context *ctx;
120 struct conn_traverse_state state;
124 ctx = connections_db_ctx(true);
130 state.private_data = private_data;
132 status = dbwrap_traverse(ctx, conn_traverse_fn, (void *)&state, &count);
133 if (!NT_STATUS_IS_OK(status)) {
140 struct conn_traverse_read_state {
141 int (*fn)(const struct connections_key *key,
142 const struct connections_data *data,
147 static int connections_forall_read_fn(struct db_record *rec,
152 struct conn_traverse_read_state *state =
153 (struct conn_traverse_read_state *)private_data;
155 key = dbwrap_record_get_key(rec);
156 value = dbwrap_record_get_value(rec);
158 if ((key.dsize != sizeof(struct connections_key))
159 || (value.dsize != sizeof(struct connections_data))) {
162 return state->fn((const struct connections_key *)key.dptr,
163 (const struct connections_data *)value.dptr,
164 state->private_data);
167 int connections_forall_read(int (*fn)(const struct connections_key *key,
168 const struct connections_data *data,
172 struct db_context *ctx;
173 struct conn_traverse_read_state state;
177 ctx = connections_db_ctx(false);
183 state.private_data = private_data;
185 status = dbwrap_traverse_read(ctx, connections_forall_read_fn,
186 (void *)&state, &count);
188 if (!NT_STATUS_IS_OK(status)) {
195 bool connections_init(bool rw)
197 return (connections_db_ctx(rw) != NULL);