s3:lib/conn_tdb: implement connections_forall_read() based on smbXsrv_*_global_traverse()
[Samba.git] / source3 / lib / conn_tdb.c
blob3930e2736a5593a22142a2991b2bdc1d357f9682
1 /*
2 Unix SMB/CIFS implementation.
3 Low-level connections.tdb access functions
4 Copyright (C) Volker Lendecke 2007
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.
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.
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/>.
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 "dbwrap/dbwrap_rbt.h"
26 #include "messages.h"
27 #include "lib/conn_tdb.h"
28 #include "util_tdb.h"
30 static struct db_context *connections_db_ctx(bool rw)
32 static struct db_context *db_ctx;
33 int open_flags;
35 if (db_ctx != NULL) {
36 return db_ctx;
39 open_flags = rw ? (O_RDWR|O_CREAT) : O_RDONLY;
41 db_ctx = db_open(NULL, lock_path("connections.tdb"), 0,
42 TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH|TDB_DEFAULT,
43 open_flags, 0644, DBWRAP_LOCK_ORDER_1);
44 return db_ctx;
47 static struct db_record *connections_fetch_record(TALLOC_CTX *mem_ctx,
48 TDB_DATA key)
50 struct db_context *ctx = connections_db_ctx(True);
52 if (ctx == NULL) {
53 return NULL;
56 return dbwrap_fetch_locked(ctx, mem_ctx, key);
59 struct db_record *connections_fetch_entry_ext(TALLOC_CTX *mem_ctx,
60 struct server_id id,
61 int cnum,
62 const char *name)
64 struct connections_key ckey;
65 TDB_DATA key;
67 ZERO_STRUCT(ckey);
68 ckey.pid = id;
69 ckey.cnum = cnum;
70 strlcpy(ckey.name, name, sizeof(ckey.name));
72 key.dsize = sizeof(ckey);
73 key.dptr = (uint8 *)&ckey;
75 return connections_fetch_record(mem_ctx, key);
78 struct db_record *connections_fetch_entry(TALLOC_CTX *mem_ctx,
79 connection_struct *conn,
80 const char *name)
82 struct server_id id = messaging_server_id(conn->sconn->msg_ctx);
83 return connections_fetch_entry_ext(mem_ctx, id, conn->cnum, name);
86 struct connections_forall_state {
87 struct db_context *session_by_pid;
88 int (*fn)(const struct connections_key *key,
89 const struct connections_data *data,
90 void *private_data);
91 void *private_data;
92 int count;
95 struct connections_forall_session {
96 uid_t uid;
97 gid_t gid;
98 char machine[FSTRING_LEN];
99 char addr[FSTRING_LEN];
102 static int collect_sessions_fn(struct smbXsrv_session_global0 *global,
103 void *connections_forall_state)
105 NTSTATUS status;
106 struct connections_forall_state *state =
107 (struct connections_forall_state*)connections_forall_state;
109 uint32_t id = global->session_global_id;
110 struct connections_forall_session sess;
112 sess.uid = global->auth_session_info->unix_token->uid;
113 sess.gid = global->auth_session_info->unix_token->gid;
114 strncpy(sess.machine, global->channels[0].remote_name, sizeof(sess.machine));
115 strncpy(sess.addr, global->channels[0].remote_address, sizeof(sess.addr));
117 status = dbwrap_store(state->session_by_pid,
118 make_tdb_data((void*)&id, sizeof(id)),
119 make_tdb_data((void*)&sess, sizeof(sess)),
120 TDB_INSERT);
121 if (!NT_STATUS_IS_OK(status)) {
122 DEBUG(0, ("Failed to store record: %s\n", nt_errstr(status)));
124 return 0;
127 static int traverse_tcon_fn(struct smbXsrv_tcon_global0 *global,
128 void *connections_forall_state)
130 NTSTATUS status;
131 struct connections_forall_state *state =
132 (struct connections_forall_state*)connections_forall_state;
134 struct connections_key key;
135 struct connections_data data;
137 uint32_t sess_id = global->session_global_id;
138 struct connections_forall_session sess = {
139 .uid = -1,
140 .gid = -1,
143 TDB_DATA val = tdb_null;
145 status = dbwrap_fetch(state->session_by_pid, state,
146 make_tdb_data((void*)&sess_id, sizeof(sess_id)),
147 &val);
148 if (NT_STATUS_IS_OK(status)) {
149 memcpy((uint8_t *)&sess, val.dptr, val.dsize);
152 ZERO_STRUCT(key);
153 ZERO_STRUCT(data);
155 key.pid = data.pid = global->server_id;
156 key.cnum = data.cnum = global->tcon_global_id;
157 strncpy(key.name, global->share_name, sizeof(key.name));
158 strncpy(data.servicename, global->share_name, sizeof(data.servicename));
159 data.uid = sess.uid;
160 data.gid = sess.gid;
161 strncpy(data.addr, sess.addr, sizeof(data.addr));
162 strncpy(data.machine, sess.machine, sizeof(data.machine));
163 data.start = nt_time_to_unix(global->creation_time);
165 state->count++;
167 return state->fn(&key, &data, state->private_data);
170 int connections_forall_read(int (*fn)(const struct connections_key *key,
171 const struct connections_data *data,
172 void *private_data),
173 void *private_data)
175 TALLOC_CTX *frame = talloc_stackframe();
176 struct connections_forall_state *state =
177 talloc_zero(talloc_tos(), struct connections_forall_state);
178 NTSTATUS status;
179 int ret = -1;
181 state->session_by_pid = db_open_rbt(state);
182 state->fn = fn;
183 state->private_data = private_data;
184 status = smbXsrv_session_global_traverse(collect_sessions_fn, state);
185 if (!NT_STATUS_IS_OK(status)) {
186 DEBUG(0, ("Failed to traverse sessions: %s\n",
187 nt_errstr(status)));
188 goto done;
191 status = smbXsrv_tcon_global_traverse(traverse_tcon_fn, state);
192 if (!NT_STATUS_IS_OK(status)) {
193 DEBUG(0, ("Failed to traverse tree connects: %s\n",
194 nt_errstr(status)));
195 goto done;
197 ret = state->count;
198 done:
199 talloc_free(frame);
200 return ret;
203 bool connections_init(bool rw)
205 return (connections_db_ctx(rw) != NULL);