s3:lib: split things into a conn_tdb.h
[Samba/gebeck_regimport.git] / source3 / lib / conn_tdb.c
blob776f53ca79e6ee77e312078eb252d8f5ebf35ba5
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"
25 #include "messages.h"
26 #include "lib/conn_tdb.h"
28 static struct db_context *connections_db_ctx(bool rw)
30 static struct db_context *db_ctx;
31 int open_flags;
33 if (db_ctx != NULL) {
34 return 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);
42 return db_ctx;
45 static struct db_record *connections_fetch_record(TALLOC_CTX *mem_ctx,
46 TDB_DATA key)
48 struct db_context *ctx = connections_db_ctx(True);
50 if (ctx == NULL) {
51 return NULL;
54 return dbwrap_fetch_locked(ctx, mem_ctx, key);
57 struct db_record *connections_fetch_entry(TALLOC_CTX *mem_ctx,
58 connection_struct *conn,
59 const char *name)
61 struct connections_key ckey;
62 TDB_DATA key;
64 ZERO_STRUCT(ckey);
65 ckey.pid = messaging_server_id(conn->sconn->msg_ctx);
66 ckey.cnum = conn->cnum;
67 strlcpy(ckey.name, name, sizeof(ckey.name));
69 key.dsize = sizeof(ckey);
70 key.dptr = (uint8 *)&ckey;
72 return connections_fetch_record(mem_ctx, key);
75 struct conn_traverse_state {
76 int (*fn)(struct db_record *rec,
77 const struct connections_key *key,
78 const struct connections_data *data,
79 void *private_data);
80 void *private_data;
83 static int conn_traverse_fn(struct db_record *rec, void *private_data)
85 TDB_DATA key;
86 TDB_DATA value;
87 struct conn_traverse_state *state =
88 (struct conn_traverse_state *)private_data;
90 key = dbwrap_record_get_key(rec);
91 value = dbwrap_record_get_value(rec);
93 if ((key.dsize != sizeof(struct connections_key))
94 || (value.dsize != sizeof(struct connections_data))) {
95 return 0;
98 return state->fn(rec, (const struct connections_key *)key.dptr,
99 (const struct connections_data *)value.dptr,
100 state->private_data);
103 int connections_traverse(int (*fn)(struct db_record *rec,
104 void *private_data),
105 void *private_data)
107 NTSTATUS status;
108 int count;
109 struct db_context *ctx = connections_db_ctx(False);
111 if (ctx == NULL) {
112 return -1;
115 status = dbwrap_traverse(ctx, fn, private_data, &count);
116 if (!NT_STATUS_IS_OK(status)) {
117 return -1;
120 return count;
123 int connections_forall(int (*fn)(struct db_record *rec,
124 const struct connections_key *key,
125 const struct connections_data *data,
126 void *private_data),
127 void *private_data)
129 struct db_context *ctx;
130 struct conn_traverse_state state;
131 NTSTATUS status;
132 int count;
134 ctx = connections_db_ctx(true);
135 if (ctx == NULL) {
136 return -1;
139 state.fn = fn;
140 state.private_data = private_data;
142 status = dbwrap_traverse(ctx, conn_traverse_fn, (void *)&state, &count);
143 if (!NT_STATUS_IS_OK(status)) {
144 return -1;
147 return count;
150 struct conn_traverse_read_state {
151 int (*fn)(const struct connections_key *key,
152 const struct connections_data *data,
153 void *private_data);
154 void *private_data;
157 static int connections_forall_read_fn(struct db_record *rec,
158 void *private_data)
160 TDB_DATA key;
161 TDB_DATA value;
162 struct conn_traverse_read_state *state =
163 (struct conn_traverse_read_state *)private_data;
165 key = dbwrap_record_get_key(rec);
166 value = dbwrap_record_get_value(rec);
168 if ((key.dsize != sizeof(struct connections_key))
169 || (value.dsize != sizeof(struct connections_data))) {
170 return 0;
172 return state->fn((const struct connections_key *)key.dptr,
173 (const struct connections_data *)value.dptr,
174 state->private_data);
177 int connections_forall_read(int (*fn)(const struct connections_key *key,
178 const struct connections_data *data,
179 void *private_data),
180 void *private_data)
182 struct db_context *ctx;
183 struct conn_traverse_read_state state;
184 NTSTATUS status;
185 int count;
187 ctx = connections_db_ctx(false);
188 if (ctx == NULL) {
189 return -1;
192 state.fn = fn;
193 state.private_data = private_data;
195 status = dbwrap_traverse_read(ctx, connections_forall_read_fn,
196 (void *)&state, &count);
198 if (!NT_STATUS_IS_OK(status)) {
199 return -1;
202 return count;
205 bool connections_init(bool rw)
207 return (connections_db_ctx(rw) != NULL);