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