Change to using TDB_INCOMPATIBLE_HASH (the jenkins hash) on all
[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 "smbd/globals.h"
22 #include "dbwrap.h"
23
24 static struct db_context *connections_db_ctx(bool rw)
25 {
26         static struct db_context *db_ctx;
27         int open_flags;
28
29         if (db_ctx != NULL) {
30                 return db_ctx;
31         }
32
33         open_flags = rw ? (O_RDWR|O_CREAT) : O_RDONLY;
34
35         db_ctx = db_open(NULL, lock_path("connections.tdb"), 0,
36                          TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH|TDB_DEFAULT, open_flags, 0644);
37         return db_ctx;
38 }
39
40 static struct db_record *connections_fetch_record(TALLOC_CTX *mem_ctx,
41                                                   TDB_DATA key)
42 {
43         struct db_context *ctx = connections_db_ctx(True);
44
45         if (ctx == NULL) {
46                 return NULL;
47         }
48
49         return ctx->fetch_locked(ctx, mem_ctx, key);
50 }
51
52 struct db_record *connections_fetch_entry(TALLOC_CTX *mem_ctx,
53                                           connection_struct *conn,
54                                           const char *name)
55 {
56         struct connections_key ckey;
57         TDB_DATA key;
58
59         ZERO_STRUCT(ckey);
60         ckey.pid = sconn_server_id(conn->sconn);
61         ckey.cnum = conn->cnum;
62         strlcpy(ckey.name, name, sizeof(ckey.name));
63
64         key.dsize = sizeof(ckey);
65         key.dptr = (uint8 *)&ckey;
66
67         return connections_fetch_record(mem_ctx, key);
68 }
69
70 struct conn_traverse_state {
71         int (*fn)(struct db_record *rec,
72                   const struct connections_key *key,
73                   const struct connections_data *data,
74                   void *private_data);
75         void *private_data;
76 };
77
78 static int conn_traverse_fn(struct db_record *rec, void *private_data)
79 {
80         struct conn_traverse_state *state =
81                 (struct conn_traverse_state *)private_data;
82
83         if ((rec->key.dsize != sizeof(struct connections_key))
84             || (rec->value.dsize != sizeof(struct connections_data))) {
85                 return 0;
86         }
87
88         return state->fn(rec, (const struct connections_key *)rec->key.dptr,
89                          (const struct connections_data *)rec->value.dptr,
90                          state->private_data);
91 }
92
93 int connections_traverse(int (*fn)(struct db_record *rec,
94                                    void *private_data),
95                          void *private_data)
96 {
97         struct db_context *ctx = connections_db_ctx(False);
98
99         if (ctx == NULL) {
100                 return -1;
101         }
102
103         return ctx->traverse(ctx, fn, private_data);
104 }
105
106 int connections_forall(int (*fn)(struct db_record *rec,
107                                  const struct connections_key *key,
108                                  const struct connections_data *data,
109                                  void *private_data),
110                        void *private_data)
111 {
112         struct db_context *ctx;
113         struct conn_traverse_state state;
114
115         ctx = connections_db_ctx(true);
116         if (ctx == NULL) {
117                 return -1;
118         }
119
120         state.fn = fn;
121         state.private_data = private_data;
122
123         return ctx->traverse(ctx, conn_traverse_fn, (void *)&state);
124 }
125
126 struct conn_traverse_read_state {
127         int (*fn)(const struct connections_key *key,
128                   const struct connections_data *data,
129                   void *private_data);
130         void *private_data;
131 };
132
133 static int connections_forall_read_fn(struct db_record *rec,
134                                       void *private_data)
135 {
136         struct conn_traverse_read_state *state =
137                 (struct conn_traverse_read_state *)private_data;
138
139         if ((rec->key.dsize != sizeof(struct connections_key))
140             || (rec->value.dsize != sizeof(struct connections_data))) {
141                 return 0;
142         }
143         return state->fn((const struct connections_key *)rec->key.dptr,
144                          (const struct connections_data *)rec->value.dptr,
145                          state->private_data);
146 }
147
148 int connections_forall_read(int (*fn)(const struct connections_key *key,
149                                       const struct connections_data *data,
150                                       void *private_data),
151                             void *private_data)
152 {
153         struct db_context *ctx;
154         struct conn_traverse_read_state state;
155
156         ctx = connections_db_ctx(false);
157         if (ctx == NULL) {
158                 return -1;
159         }
160
161         state.fn = fn;
162         state.private_data = private_data;
163
164         return ctx->traverse_read(ctx, connections_forall_read_fn,
165                                   (void *)&state);
166 }
167
168 bool connections_init(bool rw)
169 {
170         return (connections_db_ctx(rw) != NULL);
171 }