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