s3:conn_tdb: convert connections tdb to only use dbwrap wrapper functions
[samba.git] / source3 / lib / conn_tdb.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Low-level connections.tdb access functions
4    Copyright (C) Volker Lendecke 2007
5
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.
10
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.
15
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/>.
18 */
19
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
26 static struct db_context *connections_db_ctx(bool rw)
27 {
28         static struct db_context *db_ctx;
29         int open_flags;
30
31         if (db_ctx != NULL) {
32                 return db_ctx;
33         }
34
35         open_flags = rw ? (O_RDWR|O_CREAT) : O_RDONLY;
36
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;
40 }
41
42 static struct db_record *connections_fetch_record(TALLOC_CTX *mem_ctx,
43                                                   TDB_DATA key)
44 {
45         struct db_context *ctx = connections_db_ctx(True);
46
47         if (ctx == NULL) {
48                 return NULL;
49         }
50
51         return dbwrap_fetch_locked(ctx, mem_ctx, key);
52 }
53
54 struct db_record *connections_fetch_entry(TALLOC_CTX *mem_ctx,
55                                           connection_struct *conn,
56                                           const char *name)
57 {
58         struct connections_key ckey;
59         TDB_DATA key;
60
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));
65
66         key.dsize = sizeof(ckey);
67         key.dptr = (uint8 *)&ckey;
68
69         return connections_fetch_record(mem_ctx, key);
70 }
71
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;
78 };
79
80 static int conn_traverse_fn(struct db_record *rec, void *private_data)
81 {
82         TDB_DATA key;
83         TDB_DATA value;
84         struct conn_traverse_state *state =
85                 (struct conn_traverse_state *)private_data;
86
87         key = dbwrap_record_get_key(rec);
88         value = dbwrap_record_get_value(rec);
89
90         if ((key.dsize != sizeof(struct connections_key))
91             || (value.dsize != sizeof(struct connections_data))) {
92                 return 0;
93         }
94
95         return state->fn(rec, (const struct connections_key *)key.dptr,
96                          (const struct connections_data *)value.dptr,
97                          state->private_data);
98 }
99
100 int connections_traverse(int (*fn)(struct db_record *rec,
101                                    void *private_data),
102                          void *private_data)
103 {
104         NTSTATUS status;
105         int count;
106         struct db_context *ctx = connections_db_ctx(False);
107
108         if (ctx == NULL) {
109                 return -1;
110         }
111
112         status = dbwrap_traverse(ctx, fn, private_data, &count);
113         if (!NT_STATUS_IS_OK(status)) {
114                 return -1;
115         }
116
117         return count;
118 }
119
120 int connections_forall(int (*fn)(struct db_record *rec,
121                                  const struct connections_key *key,
122                                  const struct connections_data *data,
123                                  void *private_data),
124                        void *private_data)
125 {
126         struct db_context *ctx;
127         struct conn_traverse_state state;
128         NTSTATUS status;
129         int count;
130
131         ctx = connections_db_ctx(true);
132         if (ctx == NULL) {
133                 return -1;
134         }
135
136         state.fn = fn;
137         state.private_data = private_data;
138
139         status = dbwrap_traverse(ctx, conn_traverse_fn, (void *)&state, &count);
140         if (!NT_STATUS_IS_OK(status)) {
141                 return -1;
142         }
143
144         return count;
145 }
146
147 struct conn_traverse_read_state {
148         int (*fn)(const struct connections_key *key,
149                   const struct connections_data *data,
150                   void *private_data);
151         void *private_data;
152 };
153
154 static int connections_forall_read_fn(struct db_record *rec,
155                                       void *private_data)
156 {
157         TDB_DATA key;
158         TDB_DATA value;
159         struct conn_traverse_read_state *state =
160                 (struct conn_traverse_read_state *)private_data;
161
162         key = dbwrap_record_get_key(rec);
163         value = dbwrap_record_get_value(rec);
164
165         if ((key.dsize != sizeof(struct connections_key))
166             || (value.dsize != sizeof(struct connections_data))) {
167                 return 0;
168         }
169         return state->fn((const struct connections_key *)key.dptr,
170                          (const struct connections_data *)value.dptr,
171                          state->private_data);
172 }
173
174 int connections_forall_read(int (*fn)(const struct connections_key *key,
175                                       const struct connections_data *data,
176                                       void *private_data),
177                             void *private_data)
178 {
179         struct db_context *ctx;
180         struct conn_traverse_read_state state;
181         NTSTATUS status;
182         int count;
183
184         ctx = connections_db_ctx(false);
185         if (ctx == NULL) {
186                 return -1;
187         }
188
189         state.fn = fn;
190         state.private_data = private_data;
191
192         status = dbwrap_traverse_read(ctx, connections_forall_read_fn,
193                                       (void *)&state, &count);
194
195         if (!NT_STATUS_IS_OK(status)) {
196                 return -1;
197         }
198
199         return count;
200 }
201
202 bool connections_init(bool rw)
203 {
204         return (connections_db_ctx(rw) != NULL);
205 }