s3: Make the difference between r/o and r/w in connections_db_ctx more obvious
[ira/wip.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
22 static struct db_context *connections_db_ctx(bool rw)
23 {
24         static struct db_context *db_ctx;
25         int open_flags;
26
27         if (db_ctx != NULL) {
28                 return db_ctx;
29         }
30
31         open_flags = rw ? (O_RDWR|O_CREAT) : O_RDONLY;
32
33         db_ctx = db_open(NULL, lock_path("connections.tdb"), 0,
34                          TDB_CLEAR_IF_FIRST|TDB_DEFAULT, open_flags, 0644);
35         return db_ctx;
36 }
37
38 static struct db_record *connections_fetch_record(TALLOC_CTX *mem_ctx,
39                                                   TDB_DATA key)
40 {
41         struct db_context *ctx = connections_db_ctx(True);
42
43         if (ctx == NULL) {
44                 return NULL;
45         }
46
47         return ctx->fetch_locked(ctx, mem_ctx, key);
48 }
49
50 struct db_record *connections_fetch_entry(TALLOC_CTX *mem_ctx,
51                                           connection_struct *conn,
52                                           const char *name)
53 {
54         struct connections_key ckey;
55         TDB_DATA key;
56
57         ZERO_STRUCT(ckey);
58         ckey.pid = procid_self();
59         ckey.cnum = conn ? conn->cnum : -1;
60         strlcpy(ckey.name, name, sizeof(ckey.name));
61
62         key.dsize = sizeof(ckey);
63         key.dptr = (uint8 *)&ckey;
64
65         return connections_fetch_record(mem_ctx, key);
66 }
67
68 struct conn_traverse_state {
69         int (*fn)(struct db_record *rec,
70                   const struct connections_key *key,
71                   const struct connections_data *data,
72                   void *private_data);
73         void *private_data;
74 };
75
76 static int conn_traverse_fn(struct db_record *rec, void *private_data)
77 {
78         struct conn_traverse_state *state =
79                 (struct conn_traverse_state *)private_data;
80
81         if ((rec->key.dsize != sizeof(struct connections_key))
82             || (rec->value.dsize != sizeof(struct connections_data))) {
83                 return 0;
84         }
85
86         return state->fn(rec, (const struct connections_key *)rec->key.dptr,
87                          (const struct connections_data *)rec->value.dptr,
88                          state->private_data);
89 }
90
91 int connections_traverse(int (*fn)(struct db_record *rec,
92                                    void *private_data),
93                          void *private_data)
94 {
95         struct db_context *ctx = connections_db_ctx(False);
96
97         if (ctx == NULL) {
98                 return -1;
99         }
100
101         return ctx->traverse(ctx, fn, private_data);
102 }
103
104 int connections_forall(int (*fn)(struct db_record *rec,
105                                  const struct connections_key *key,
106                                  const struct connections_data *data,
107                                  void *private_data),
108                        void *private_data)
109 {
110         struct db_context *ctx;
111         struct conn_traverse_state state;
112
113         ctx = connections_db_ctx(true);
114         if (ctx == NULL) {
115                 return -1;
116         }
117
118         state.fn = fn;
119         state.private_data = private_data;
120
121         return ctx->traverse(ctx, conn_traverse_fn, (void *)&state);
122 }
123
124 bool connections_init(bool rw)
125 {
126         return (connections_db_ctx(rw) != NULL);
127 }