scripting: Make tdb_copy use the python subprocess module
[Samba.git] / source3 / smbd / conn.c
blobe6f81a9b10bc66ea4e76a85dd81f6da35cd7f90e
1 /*
2 Unix SMB/CIFS implementation.
3 Manage connections_struct structures
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Alexander Bokovoy 2002
6 Copyright (C) Jeremy Allison 2010
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "includes.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "lib/util/bitmap.h"
27 /*******************************************************************
28 Static cache for storing per-user share access value. This really
29 belongs inside the vuid_cache.array struct but we can't change the
30 VFS ABI for 4.0.x. This is fixed in 4.1.x. JRA.
31 ********************************************************************/
33 struct connection_share_access_list {
34 struct connection_share_access_list *next, *prev;
35 connection_struct *conn;
36 uint32_t vuid_cache_share_access_array[VUID_CACHE_SIZE];
39 static struct connection_share_access_list *conn_share_access_list;
41 /*******************************************************************
42 Destructor function for per-user share access value.
43 ********************************************************************/
45 static int free_csal_entry(struct connection_share_access_list *csal)
47 DLIST_REMOVE(conn_share_access_list, csal);
48 return 0;
51 /*******************************************************************
52 Utility function to find a per-user share access value struct.
53 ********************************************************************/
55 static struct connection_share_access_list *find_csal_entry(connection_struct *conn)
57 struct connection_share_access_list *csal;
59 for (csal = conn_share_access_list; csal; csal = csal->next) {
60 if (csal->conn == conn) {
61 DLIST_PROMOTE(conn_share_access_list, csal);
62 return csal;
65 return NULL;
68 /*******************************************************************
69 Accessor functions for per-user share access value.
70 These are the only two functions exposed externally.
71 ********************************************************************/
73 uint32_t get_connection_share_access_list_entry(connection_struct *conn,
74 unsigned int i)
76 struct connection_share_access_list *csal =
77 find_csal_entry(conn);
79 if (csal == NULL) {
81 * This is a faked up connection struct
82 * for internal purposes.
83 * Return full access.
85 return SEC_RIGHTS_FILE_ALL;
88 return csal->vuid_cache_share_access_array[i];
91 void set_connection_share_access_list_entry(connection_struct *conn,
92 unsigned int i,
93 uint32_t val)
95 struct connection_share_access_list *csal =
96 find_csal_entry(conn);
98 if (csal == NULL) {
99 return;
102 csal->vuid_cache_share_access_array[i] = val;
105 /****************************************************************************
106 Return the number of open connections.
107 ****************************************************************************/
109 int conn_num_open(struct smbd_server_connection *sconn)
111 return sconn->num_connections;
114 /****************************************************************************
115 Check if a snum is in use.
116 ****************************************************************************/
118 bool conn_snum_used(struct smbd_server_connection *sconn,
119 int snum)
121 struct connection_struct *conn;
123 for (conn=sconn->connections; conn; conn=conn->next) {
124 if (conn->params->service == snum) {
125 return true;
129 return false;
132 /****************************************************************************
133 Find first available connection slot, starting from a random position.
134 The randomisation stops problems with the server dieing and clients
135 thinking the server is still available.
136 ****************************************************************************/
138 connection_struct *conn_new(struct smbd_server_connection *sconn)
140 connection_struct *conn;
141 struct connection_share_access_list *csal;
143 if (!(conn=talloc_zero(NULL, connection_struct)) ||
144 !(conn->params = talloc(conn, struct share_params)) ||
145 !(conn->connectpath = talloc_strdup(conn, "")) ||
146 !(conn->origpath = talloc_strdup(conn, "")) ||
147 !(csal = talloc_zero(conn, struct connection_share_access_list))) {
148 DEBUG(0,("TALLOC_ZERO() failed!\n"));
149 TALLOC_FREE(conn);
150 return NULL;
152 talloc_set_destructor(csal, free_csal_entry);
154 conn->sconn = sconn;
155 conn->force_group_gid = (gid_t)-1;
157 DLIST_ADD(sconn->connections, conn);
158 DLIST_ADD(conn_share_access_list, csal);
159 sconn->num_connections++;
161 return conn;
164 /****************************************************************************
165 Clear a vuid out of the connection's vuid cache
166 ****************************************************************************/
168 static void conn_clear_vuid_cache(connection_struct *conn, uint64_t vuid)
170 int i;
172 for (i=0; i<VUID_CACHE_SIZE; i++) {
173 struct vuid_cache_entry *ent;
175 ent = &conn->vuid_cache.array[i];
177 if (ent->vuid == vuid) {
178 ent->vuid = UID_FIELD_INVALID;
180 * We need to keep conn->session_info around
181 * if it's equal to ent->session_info as a SMBulogoff
182 * is often followed by a SMBtdis (with an invalid
183 * vuid). The debug code (or regular code in
184 * vfs_full_audit) wants to refer to the
185 * conn->session_info pointer to print debug
186 * statements. Theoretically this is a bug,
187 * as once the vuid is gone the session_info
188 * on the conn struct isn't valid any more,
189 * but there's enough code that assumes
190 * conn->session_info is never null that
191 * it's easier to hold onto the old pointer
192 * until we get a new sessionsetupX.
193 * As everything is hung off the
194 * conn pointer as a talloc context we're not
195 * leaking memory here. See bug #6315. JRA.
197 if (conn->session_info == ent->session_info) {
198 ent->session_info = NULL;
199 } else {
200 TALLOC_FREE(ent->session_info);
202 ent->read_only = False;
207 /****************************************************************************
208 Clear a vuid out of the validity cache, and as the 'owner' of a connection.
210 Called from invalidate_vuid()
211 ****************************************************************************/
213 void conn_clear_vuid_caches(struct smbd_server_connection *sconn, uint64_t vuid)
215 connection_struct *conn;
217 for (conn=sconn->connections; conn;conn=conn->next) {
218 if (conn->vuid == vuid) {
219 conn->vuid = UID_FIELD_INVALID;
221 conn_clear_vuid_cache(conn, vuid);
225 /****************************************************************************
226 Free a conn structure - internal part.
227 ****************************************************************************/
229 static void conn_free_internal(connection_struct *conn)
231 vfs_handle_struct *handle = NULL, *thandle = NULL;
232 struct trans_state *state = NULL;
234 /* Free vfs_connection_struct */
235 handle = conn->vfs_handles;
236 while(handle) {
237 thandle = handle->next;
238 DLIST_REMOVE(conn->vfs_handles, handle);
239 if (handle->free_data)
240 handle->free_data(&handle->data);
241 handle = thandle;
244 /* Free any pending transactions stored on this conn. */
245 for (state = conn->pending_trans; state; state = state->next) {
246 /* state->setup is a talloc child of state. */
247 SAFE_FREE(state->param);
248 SAFE_FREE(state->data);
251 free_namearray(conn->veto_list);
252 free_namearray(conn->hide_list);
253 free_namearray(conn->veto_oplock_list);
254 free_namearray(conn->aio_write_behind_list);
256 ZERO_STRUCTP(conn);
257 talloc_destroy(conn);
260 /****************************************************************************
261 Free a conn structure.
262 ****************************************************************************/
264 void conn_free(connection_struct *conn)
266 if (conn->sconn == NULL) {
267 conn_free_internal(conn);
268 return;
271 DLIST_REMOVE(conn->sconn->connections, conn);
272 SMB_ASSERT(conn->sconn->num_connections > 0);
273 conn->sconn->num_connections--;
275 conn_free_internal(conn);