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