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 "smbd/globals.h"
23 static struct db_context *connections_db_ctx(bool rw)
25 static struct db_context *db_ctx;
32 open_flags = rw ? (O_RDWR|O_CREAT) : O_RDONLY;
34 db_ctx = db_open(NULL, lock_path("connections.tdb"), 0,
35 TDB_CLEAR_IF_FIRST|TDB_DEFAULT, open_flags, 0644);
39 static struct db_record *connections_fetch_record(TALLOC_CTX *mem_ctx,
42 struct db_context *ctx = connections_db_ctx(True);
48 return ctx->fetch_locked(ctx, mem_ctx, key);
51 struct db_record *connections_fetch_entry(TALLOC_CTX *mem_ctx,
52 connection_struct *conn,
55 struct connections_key ckey;
59 ckey.pid = sconn_server_id(conn->sconn);
60 ckey.cnum = conn->cnum;
61 strlcpy(ckey.name, name, sizeof(ckey.name));
63 key.dsize = sizeof(ckey);
64 key.dptr = (uint8 *)&ckey;
66 return connections_fetch_record(mem_ctx, key);
69 struct conn_traverse_state {
70 int (*fn)(struct db_record *rec,
71 const struct connections_key *key,
72 const struct connections_data *data,
77 static int conn_traverse_fn(struct db_record *rec, void *private_data)
79 struct conn_traverse_state *state =
80 (struct conn_traverse_state *)private_data;
82 if ((rec->key.dsize != sizeof(struct connections_key))
83 || (rec->value.dsize != sizeof(struct connections_data))) {
87 return state->fn(rec, (const struct connections_key *)rec->key.dptr,
88 (const struct connections_data *)rec->value.dptr,
92 int connections_traverse(int (*fn)(struct db_record *rec,
96 struct db_context *ctx = connections_db_ctx(False);
102 return ctx->traverse(ctx, fn, private_data);
105 int connections_forall(int (*fn)(struct db_record *rec,
106 const struct connections_key *key,
107 const struct connections_data *data,
111 struct db_context *ctx;
112 struct conn_traverse_state state;
114 ctx = connections_db_ctx(true);
120 state.private_data = private_data;
122 return ctx->traverse(ctx, conn_traverse_fn, (void *)&state);
125 struct conn_traverse_read_state {
126 int (*fn)(const struct connections_key *key,
127 const struct connections_data *data,
132 static int connections_forall_read_fn(struct db_record *rec,
135 struct conn_traverse_read_state *state =
136 (struct conn_traverse_read_state *)private_data;
138 if ((rec->key.dsize != sizeof(struct connections_key))
139 || (rec->value.dsize != sizeof(struct connections_data))) {
142 return state->fn((const struct connections_key *)rec->key.dptr,
143 (const struct connections_data *)rec->value.dptr,
144 state->private_data);
147 int connections_forall_read(int (*fn)(const struct connections_key *key,
148 const struct connections_data *data,
152 struct db_context *ctx;
153 struct conn_traverse_read_state state;
155 ctx = connections_db_ctx(false);
161 state.private_data = private_data;
163 return ctx->traverse_read(ctx, connections_forall_read_fn,
167 bool connections_init(bool rw)
169 return (connections_db_ctx(rw) != NULL);