source3/libnet/libnet_join.h: fix licence/copyright
[Samba.git] / source3 / lib / conn_tdb.c
blob50f0d9fa1bb10a41c8632d1166a66c30134bcac7
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.h"
25 static struct db_context *connections_db_ctx(bool rw)
27 static struct db_context *db_ctx;
28 int open_flags;
30 if (db_ctx != NULL) {
31 return db_ctx;
34 open_flags = rw ? (O_RDWR|O_CREAT) : O_RDONLY;
36 db_ctx = db_open(NULL, lock_path("connections.tdb"), 0,
37 TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH|TDB_DEFAULT, open_flags, 0644);
38 return db_ctx;
41 static struct db_record *connections_fetch_record(TALLOC_CTX *mem_ctx,
42 TDB_DATA key)
44 struct db_context *ctx = connections_db_ctx(True);
46 if (ctx == NULL) {
47 return NULL;
50 return ctx->fetch_locked(ctx, mem_ctx, key);
53 struct db_record *connections_fetch_entry(TALLOC_CTX *mem_ctx,
54 connection_struct *conn,
55 const char *name)
57 struct connections_key ckey;
58 TDB_DATA key;
60 ZERO_STRUCT(ckey);
61 ckey.pid = sconn_server_id(conn->sconn);
62 ckey.cnum = conn->cnum;
63 strlcpy(ckey.name, name, sizeof(ckey.name));
65 key.dsize = sizeof(ckey);
66 key.dptr = (uint8 *)&ckey;
68 return connections_fetch_record(mem_ctx, key);
71 struct conn_traverse_state {
72 int (*fn)(struct db_record *rec,
73 const struct connections_key *key,
74 const struct connections_data *data,
75 void *private_data);
76 void *private_data;
79 static int conn_traverse_fn(struct db_record *rec, void *private_data)
81 struct conn_traverse_state *state =
82 (struct conn_traverse_state *)private_data;
84 if ((rec->key.dsize != sizeof(struct connections_key))
85 || (rec->value.dsize != sizeof(struct connections_data))) {
86 return 0;
89 return state->fn(rec, (const struct connections_key *)rec->key.dptr,
90 (const struct connections_data *)rec->value.dptr,
91 state->private_data);
94 int connections_traverse(int (*fn)(struct db_record *rec,
95 void *private_data),
96 void *private_data)
98 struct db_context *ctx = connections_db_ctx(False);
100 if (ctx == NULL) {
101 return -1;
104 return ctx->traverse(ctx, fn, private_data);
107 int connections_forall(int (*fn)(struct db_record *rec,
108 const struct connections_key *key,
109 const struct connections_data *data,
110 void *private_data),
111 void *private_data)
113 struct db_context *ctx;
114 struct conn_traverse_state state;
116 ctx = connections_db_ctx(true);
117 if (ctx == NULL) {
118 return -1;
121 state.fn = fn;
122 state.private_data = private_data;
124 return ctx->traverse(ctx, conn_traverse_fn, (void *)&state);
127 struct conn_traverse_read_state {
128 int (*fn)(const struct connections_key *key,
129 const struct connections_data *data,
130 void *private_data);
131 void *private_data;
134 static int connections_forall_read_fn(struct db_record *rec,
135 void *private_data)
137 struct conn_traverse_read_state *state =
138 (struct conn_traverse_read_state *)private_data;
140 if ((rec->key.dsize != sizeof(struct connections_key))
141 || (rec->value.dsize != sizeof(struct connections_data))) {
142 return 0;
144 return state->fn((const struct connections_key *)rec->key.dptr,
145 (const struct connections_data *)rec->value.dptr,
146 state->private_data);
149 int connections_forall_read(int (*fn)(const struct connections_key *key,
150 const struct connections_data *data,
151 void *private_data),
152 void *private_data)
154 struct db_context *ctx;
155 struct conn_traverse_read_state state;
157 ctx = connections_db_ctx(false);
158 if (ctx == NULL) {
159 return -1;
162 state.fn = fn;
163 state.private_data = private_data;
165 return ctx->traverse_read(ctx, connections_forall_read_fn,
166 (void *)&state);
169 bool connections_init(bool rw)
171 return (connections_db_ctx(rw) != NULL);