r23779: Change from v2 or later to v3 or later.
[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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 static struct db_context *connections_db_ctx(BOOL rw)
24 {
25         static struct db_context *db_ctx;
26
27         if (db_ctx != NULL) {
28                 return db_ctx;
29         }
30
31         if (rw) {
32                 db_ctx = db_open(NULL, lock_path("connections.tdb"), 0,
33                                  TDB_CLEAR_IF_FIRST|TDB_DEFAULT, 
34                                  O_RDWR | O_CREAT, 0644);
35         }
36         else {
37                 db_ctx = db_open(NULL, lock_path("connections.tdb"), 0,
38                                  TDB_DEFAULT, O_RDONLY, 0);
39         }
40
41         return db_ctx;
42 }
43
44 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 ctx->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 = procid_self();
65         ckey.cnum = conn ? conn->cnum : -1;
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         struct conn_traverse_state *state =
85                 (struct conn_traverse_state *)private_data;
86
87         if ((rec->key.dsize != sizeof(struct connections_key))
88             || (rec->value.dsize != sizeof(struct connections_data))) {
89                 return 0;
90         }
91
92         return state->fn(rec, (const struct connections_key *)rec->key.dptr,
93                          (const struct connections_data *)rec->value.dptr,
94                          state->private_data);
95 }
96
97 int connections_traverse(int (*fn)(struct db_record *rec,
98                                    void *private_data),
99                          void *private_data)
100 {
101         struct db_context *ctx = connections_db_ctx(False);
102
103         if (ctx == NULL) {
104                 return -1;
105         }
106
107         return ctx->traverse(ctx, fn, private_data);
108 }
109
110 int connections_forall(int (*fn)(struct db_record *rec,
111                                  const struct connections_key *key,
112                                  const struct connections_data *data,
113                                  void *private_data),
114                        void *private_data)
115 {
116         struct conn_traverse_state state;
117
118         state.fn = fn;
119         state.private_data = private_data;
120
121         return connections_traverse(conn_traverse_fn, (void *)&state);
122 }
123
124 BOOL connections_init(BOOL rw)
125 {
126         return (connections_db_ctx(rw) != NULL);
127 }