s3:lib: remove unused function connections_traverse()
[mat/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 #include "messages.h"
26 #include "lib/conn_tdb.h"
27
28 static struct db_context *connections_db_ctx(bool rw)
29 {
30         static struct db_context *db_ctx;
31         int open_flags;
32
33         if (db_ctx != NULL) {
34                 return db_ctx;
35         }
36
37         open_flags = rw ? (O_RDWR|O_CREAT) : O_RDONLY;
38
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;
43 }
44
45 static struct db_record *connections_fetch_record(TALLOC_CTX *mem_ctx,
46                                                   TDB_DATA key)
47 {
48         struct db_context *ctx = connections_db_ctx(True);
49
50         if (ctx == NULL) {
51                 return NULL;
52         }
53
54         return dbwrap_fetch_locked(ctx, mem_ctx, key);
55 }
56
57 struct db_record *connections_fetch_entry_ext(TALLOC_CTX *mem_ctx,
58                                               struct server_id id,
59                                               int cnum,
60                                               const char *name)
61 {
62         struct connections_key ckey;
63         TDB_DATA key;
64
65         ZERO_STRUCT(ckey);
66         ckey.pid = id;
67         ckey.cnum = cnum;
68         strlcpy(ckey.name, name, sizeof(ckey.name));
69
70         key.dsize = sizeof(ckey);
71         key.dptr = (uint8 *)&ckey;
72
73         return connections_fetch_record(mem_ctx, key);
74 }
75
76 struct db_record *connections_fetch_entry(TALLOC_CTX *mem_ctx,
77                                           connection_struct *conn,
78                                           const char *name)
79 {
80         struct server_id id = messaging_server_id(conn->sconn->msg_ctx);
81         return connections_fetch_entry_ext(mem_ctx, id, conn->cnum, name);
82 }
83
84
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;
91 };
92
93 static int conn_traverse_fn(struct db_record *rec, void *private_data)
94 {
95         TDB_DATA key;
96         TDB_DATA value;
97         struct conn_traverse_state *state =
98                 (struct conn_traverse_state *)private_data;
99
100         key = dbwrap_record_get_key(rec);
101         value = dbwrap_record_get_value(rec);
102
103         if ((key.dsize != sizeof(struct connections_key))
104             || (value.dsize != sizeof(struct connections_data))) {
105                 return 0;
106         }
107
108         return state->fn(rec, (const struct connections_key *)key.dptr,
109                          (const struct connections_data *)value.dptr,
110                          state->private_data);
111 }
112
113 int connections_forall(int (*fn)(struct db_record *rec,
114                                  const struct connections_key *key,
115                                  const struct connections_data *data,
116                                  void *private_data),
117                        void *private_data)
118 {
119         struct db_context *ctx;
120         struct conn_traverse_state state;
121         NTSTATUS status;
122         int count;
123
124         ctx = connections_db_ctx(true);
125         if (ctx == NULL) {
126                 return -1;
127         }
128
129         state.fn = fn;
130         state.private_data = private_data;
131
132         status = dbwrap_traverse(ctx, conn_traverse_fn, (void *)&state, &count);
133         if (!NT_STATUS_IS_OK(status)) {
134                 return -1;
135         }
136
137         return count;
138 }
139
140 struct conn_traverse_read_state {
141         int (*fn)(const struct connections_key *key,
142                   const struct connections_data *data,
143                   void *private_data);
144         void *private_data;
145 };
146
147 static int connections_forall_read_fn(struct db_record *rec,
148                                       void *private_data)
149 {
150         TDB_DATA key;
151         TDB_DATA value;
152         struct conn_traverse_read_state *state =
153                 (struct conn_traverse_read_state *)private_data;
154
155         key = dbwrap_record_get_key(rec);
156         value = dbwrap_record_get_value(rec);
157
158         if ((key.dsize != sizeof(struct connections_key))
159             || (value.dsize != sizeof(struct connections_data))) {
160                 return 0;
161         }
162         return state->fn((const struct connections_key *)key.dptr,
163                          (const struct connections_data *)value.dptr,
164                          state->private_data);
165 }
166
167 int connections_forall_read(int (*fn)(const struct connections_key *key,
168                                       const struct connections_data *data,
169                                       void *private_data),
170                             void *private_data)
171 {
172         struct db_context *ctx;
173         struct conn_traverse_read_state state;
174         NTSTATUS status;
175         int count;
176
177         ctx = connections_db_ctx(false);
178         if (ctx == NULL) {
179                 return -1;
180         }
181
182         state.fn = fn;
183         state.private_data = private_data;
184
185         status = dbwrap_traverse_read(ctx, connections_forall_read_fn,
186                                       (void *)&state, &count);
187
188         if (!NT_STATUS_IS_OK(status)) {
189                 return -1;
190         }
191
192         return count;
193 }
194
195 bool connections_init(bool rw)
196 {
197         return (connections_db_ctx(rw) != NULL);
198 }