build:dist: call build-manpages-nogit for make dist and package generated files
[Samba/gebeck_regimport.git] / source3 / lib / conn_tdb.c
blobfb605e12ba11fb100faf731a9ffcf8850f6eaa73
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_ext(TALLOC_CTX *mem_ctx,
58 struct server_id id,
59 int cnum,
60 const char *name)
62 struct connections_key ckey;
63 TDB_DATA key;
65 ZERO_STRUCT(ckey);
66 ckey.pid = id;
67 ckey.cnum = cnum;
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,
78 const char *name)
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,
89 void *private_data);
90 void *private_data;
93 static int conn_traverse_fn(struct db_record *rec, void *private_data)
95 TDB_DATA key;
96 TDB_DATA value;
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))) {
105 return 0;
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_traverse(int (*fn)(struct db_record *rec,
114 void *private_data),
115 void *private_data)
117 NTSTATUS status;
118 int count;
119 struct db_context *ctx = connections_db_ctx(False);
121 if (ctx == NULL) {
122 return -1;
125 status = dbwrap_traverse(ctx, fn, private_data, &count);
126 if (!NT_STATUS_IS_OK(status)) {
127 return -1;
130 return count;
133 int connections_forall(int (*fn)(struct db_record *rec,
134 const struct connections_key *key,
135 const struct connections_data *data,
136 void *private_data),
137 void *private_data)
139 struct db_context *ctx;
140 struct conn_traverse_state state;
141 NTSTATUS status;
142 int count;
144 ctx = connections_db_ctx(true);
145 if (ctx == NULL) {
146 return -1;
149 state.fn = fn;
150 state.private_data = private_data;
152 status = dbwrap_traverse(ctx, conn_traverse_fn, (void *)&state, &count);
153 if (!NT_STATUS_IS_OK(status)) {
154 return -1;
157 return count;
160 struct conn_traverse_read_state {
161 int (*fn)(const struct connections_key *key,
162 const struct connections_data *data,
163 void *private_data);
164 void *private_data;
167 static int connections_forall_read_fn(struct db_record *rec,
168 void *private_data)
170 TDB_DATA key;
171 TDB_DATA value;
172 struct conn_traverse_read_state *state =
173 (struct conn_traverse_read_state *)private_data;
175 key = dbwrap_record_get_key(rec);
176 value = dbwrap_record_get_value(rec);
178 if ((key.dsize != sizeof(struct connections_key))
179 || (value.dsize != sizeof(struct connections_data))) {
180 return 0;
182 return state->fn((const struct connections_key *)key.dptr,
183 (const struct connections_data *)value.dptr,
184 state->private_data);
187 int connections_forall_read(int (*fn)(const struct connections_key *key,
188 const struct connections_data *data,
189 void *private_data),
190 void *private_data)
192 struct db_context *ctx;
193 struct conn_traverse_read_state state;
194 NTSTATUS status;
195 int count;
197 ctx = connections_db_ctx(false);
198 if (ctx == NULL) {
199 return -1;
202 state.fn = fn;
203 state.private_data = private_data;
205 status = dbwrap_traverse_read(ctx, connections_forall_read_fn,
206 (void *)&state, &count);
208 if (!NT_STATUS_IS_OK(status)) {
209 return -1;
212 return count;
215 bool connections_init(bool rw)
217 return (connections_db_ctx(rw) != NULL);