s3: Pass smbd_server_connection to (unused) server_encryption_shutdown
[Samba/vl.git] / source3 / lib / conn_tdb.c
blobf4584806733263908bbcd678a073693084e99308
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 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/>.
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "smbd/globals.h"
23 #include "dbwrap/dbwrap.h"
24 #include "dbwrap/dbwrap_open.h"
26 static struct db_context *connections_db_ctx(bool rw)
28 static struct db_context *db_ctx;
29 int open_flags;
31 if (db_ctx != NULL) {
32 return db_ctx;
35 open_flags = rw ? (O_RDWR|O_CREAT) : O_RDONLY;
37 db_ctx = db_open(NULL, lock_path("connections.tdb"), 0,
38 TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH|TDB_DEFAULT, open_flags, 0644);
39 return db_ctx;
42 static struct db_record *connections_fetch_record(TALLOC_CTX *mem_ctx,
43 TDB_DATA key)
45 struct db_context *ctx = connections_db_ctx(True);
47 if (ctx == NULL) {
48 return NULL;
51 return ctx->fetch_locked(ctx, mem_ctx, key);
54 struct db_record *connections_fetch_entry(TALLOC_CTX *mem_ctx,
55 connection_struct *conn,
56 const char *name)
58 struct connections_key ckey;
59 TDB_DATA key;
61 ZERO_STRUCT(ckey);
62 ckey.pid = sconn_server_id(conn->sconn);
63 ckey.cnum = conn->cnum;
64 strlcpy(ckey.name, name, sizeof(ckey.name));
66 key.dsize = sizeof(ckey);
67 key.dptr = (uint8 *)&ckey;
69 return connections_fetch_record(mem_ctx, key);
72 struct conn_traverse_state {
73 int (*fn)(struct db_record *rec,
74 const struct connections_key *key,
75 const struct connections_data *data,
76 void *private_data);
77 void *private_data;
80 static int conn_traverse_fn(struct db_record *rec, void *private_data)
82 struct conn_traverse_state *state =
83 (struct conn_traverse_state *)private_data;
85 if ((rec->key.dsize != sizeof(struct connections_key))
86 || (rec->value.dsize != sizeof(struct connections_data))) {
87 return 0;
90 return state->fn(rec, (const struct connections_key *)rec->key.dptr,
91 (const struct connections_data *)rec->value.dptr,
92 state->private_data);
95 int connections_traverse(int (*fn)(struct db_record *rec,
96 void *private_data),
97 void *private_data)
99 struct db_context *ctx = connections_db_ctx(False);
101 if (ctx == NULL) {
102 return -1;
105 return ctx->traverse(ctx, fn, private_data);
108 int connections_forall(int (*fn)(struct db_record *rec,
109 const struct connections_key *key,
110 const struct connections_data *data,
111 void *private_data),
112 void *private_data)
114 struct db_context *ctx;
115 struct conn_traverse_state state;
117 ctx = connections_db_ctx(true);
118 if (ctx == NULL) {
119 return -1;
122 state.fn = fn;
123 state.private_data = private_data;
125 return ctx->traverse(ctx, conn_traverse_fn, (void *)&state);
128 struct conn_traverse_read_state {
129 int (*fn)(const struct connections_key *key,
130 const struct connections_data *data,
131 void *private_data);
132 void *private_data;
135 static int connections_forall_read_fn(struct db_record *rec,
136 void *private_data)
138 struct conn_traverse_read_state *state =
139 (struct conn_traverse_read_state *)private_data;
141 if ((rec->key.dsize != sizeof(struct connections_key))
142 || (rec->value.dsize != sizeof(struct connections_data))) {
143 return 0;
145 return state->fn((const struct connections_key *)rec->key.dptr,
146 (const struct connections_data *)rec->value.dptr,
147 state->private_data);
150 int connections_forall_read(int (*fn)(const struct connections_key *key,
151 const struct connections_data *data,
152 void *private_data),
153 void *private_data)
155 struct db_context *ctx;
156 struct conn_traverse_read_state state;
158 ctx = connections_db_ctx(false);
159 if (ctx == NULL) {
160 return -1;
163 state.fn = fn;
164 state.private_data = private_data;
166 return ctx->traverse_read(ctx, connections_forall_read_fn,
167 (void *)&state);
170 bool connections_init(bool rw)
172 return (connections_db_ctx(rw) != NULL);