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/>.
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "lib/util/bitmap.h"
27 static void conn_free_internal(connection_struct
*conn
);
29 /****************************************************************************
30 * Remove a conn struct from conn->sconn->connections
31 * if not already done.
32 ****************************************************************************/
34 static int conn_struct_destructor(connection_struct
*conn
)
36 if (conn
->sconn
!= NULL
) {
37 DLIST_REMOVE(conn
->sconn
->connections
, conn
);
38 SMB_ASSERT(conn
->sconn
->num_connections
> 0);
39 conn
->sconn
->num_connections
--;
42 conn_free_internal(conn
);
46 /****************************************************************************
47 Return the number of open connections.
48 ****************************************************************************/
50 int conn_num_open(struct smbd_server_connection
*sconn
)
52 return sconn
->num_connections
;
55 /****************************************************************************
56 Check if a snum is in use.
57 ****************************************************************************/
59 bool conn_snum_used(struct smbd_server_connection
*sconn
,
62 struct connection_struct
*conn
;
64 for (conn
=sconn
->connections
; conn
; conn
=conn
->next
) {
65 if (conn
->params
->service
== snum
) {
73 enum protocol_types
conn_protocol(struct smbd_server_connection
*sconn
)
75 if ((sconn
!= NULL
) &&
76 (sconn
->client
!= NULL
) &&
77 (sconn
->client
->connections
!= NULL
)) {
78 return sconn
->client
->connections
->protocol
;
81 * Default to what source3/lib/util.c has as default for the
82 * static Protocol variable to not change behaviour.
84 return PROTOCOL_COREPLUS
;
87 bool conn_using_smb2(struct smbd_server_connection
*sconn
)
89 enum protocol_types proto
= conn_protocol(sconn
);
90 return (proto
>= PROTOCOL_SMB2_02
);
93 /****************************************************************************
94 Find first available connection slot, starting from a random position.
95 The randomisation stops problems with the server dying and clients
96 thinking the server is still available.
97 ****************************************************************************/
99 connection_struct
*conn_new(struct smbd_server_connection
*sconn
)
101 connection_struct
*conn
= NULL
;
103 conn
= talloc_zero(NULL
, connection_struct
);
105 DBG_ERR("talloc_zero failed\n");
108 conn
->params
= talloc(conn
, struct share_params
);
109 if (conn
->params
== NULL
) {
110 DBG_ERR("talloc_zero failed\n");
114 conn
->vuid_cache
= talloc_zero(conn
, struct vuid_cache
);
115 if (conn
->vuid_cache
== NULL
) {
116 DBG_ERR("talloc_zero failed\n");
120 conn
->connectpath
= talloc_strdup(conn
, "");
121 if (conn
->connectpath
== NULL
) {
122 DBG_ERR("talloc_zero failed\n");
126 conn
->cwd_fsp
= talloc_zero(conn
, struct files_struct
);
127 if (conn
->cwd_fsp
== NULL
) {
128 DBG_ERR("talloc_zero failed\n");
132 conn
->cwd_fsp
->fsp_name
= synthetic_smb_fname(conn
->cwd_fsp
,
138 if (conn
->cwd_fsp
->fsp_name
== NULL
) {
142 conn
->cwd_fsp
->fh
= fd_handle_create(conn
->cwd_fsp
);
143 if (conn
->cwd_fsp
->fh
== NULL
) {
144 DBG_ERR("talloc_zero failed\n");
149 conn
->force_group_gid
= (gid_t
)-1;
150 fsp_set_fd(conn
->cwd_fsp
, -1);
151 conn
->cwd_fsp
->fnum
= FNUM_FIELD_INVALID
;
152 conn
->cwd_fsp
->conn
= conn
;
154 DLIST_ADD(sconn
->connections
, conn
);
155 sconn
->num_connections
++;
158 * Catches the case where someone forgets to call
161 talloc_set_destructor(conn
, conn_struct_destructor
);
165 /****************************************************************************
166 Clear a vuid out of the connection's vuid cache
167 ****************************************************************************/
169 static void conn_clear_vuid_cache(connection_struct
*conn
, uint64_t vuid
)
171 struct vuid_cache_entry
*ent
= NULL
;
174 for (i
=0; i
<VUID_CACHE_SIZE
; i
++) {
175 ent
= &conn
->vuid_cache
->array
[i
];
176 if (ent
->vuid
!= vuid
) {
180 if (i
== VUID_CACHE_SIZE
) {
184 ent
->vuid
= UID_FIELD_INVALID
;
187 * We need to keep conn->session_info around
188 * if it's equal to ent->session_info as a SMBulogoff
189 * is often followed by a SMBtdis (with an invalid
190 * vuid). The debug code (or regular code in
191 * vfs_full_audit) wants to refer to the
192 * conn->session_info pointer to print debug
193 * statements. Theoretically this is a bug,
194 * as once the vuid is gone the session_info
195 * on the conn struct isn't valid any more,
196 * but there's enough code that assumes
197 * conn->session_info is never null that
198 * it's easier to hold onto the old pointer
199 * until we get a new sessionsetupX.
200 * As everything is hung off the
201 * conn pointer as a talloc context we're not
202 * leaking memory here. See bug #6315. JRA.
204 if (conn
->session_info
== ent
->session_info
) {
205 ent
->session_info
= NULL
;
207 TALLOC_FREE(ent
->session_info
);
209 ent
->read_only
= False
;
210 ent
->share_access
= 0;
211 TALLOC_FREE(ent
->veto_list
);
212 TALLOC_FREE(ent
->hide_list
);
215 /****************************************************************************
216 Clear a vuid out of the validity cache, and as the 'owner' of a connection.
218 Called from invalidate_vuid()
219 ****************************************************************************/
221 void conn_clear_vuid_caches(struct smbd_server_connection
*sconn
, uint64_t vuid
)
223 connection_struct
*conn
;
225 for (conn
=sconn
->connections
; conn
;conn
=conn
->next
) {
226 if (conn
->vuid
== vuid
) {
227 conn
->vuid
= UID_FIELD_INVALID
;
229 conn_clear_vuid_cache(conn
, vuid
);
233 /****************************************************************************
234 Free a conn structure - internal part.
235 ****************************************************************************/
237 static void conn_free_internal(connection_struct
*conn
)
239 vfs_handle_struct
*handle
= NULL
, *thandle
= NULL
;
240 struct trans_state
*state
= NULL
;
242 /* Free vfs_connection_struct */
243 handle
= conn
->vfs_handles
;
245 thandle
= handle
->next
;
246 DLIST_REMOVE(conn
->vfs_handles
, handle
);
247 if (handle
->free_data
)
248 handle
->free_data(&handle
->data
);
252 /* Free any pending transactions stored on this conn. */
253 for (state
= conn
->pending_trans
; state
; state
= state
->next
) {
254 /* state->setup is a talloc child of state. */
255 SAFE_FREE(state
->param
);
256 SAFE_FREE(state
->data
);
262 /****************************************************************************
263 Free a conn structure.
264 ****************************************************************************/
266 void conn_free(connection_struct
*conn
)
272 * Correctly initialize a share with case options.
274 void conn_setup_case_options(connection_struct
*conn
)
276 int snum
= conn
->params
->service
;
278 if (lp_case_sensitive(snum
) == Auto
) {
279 /* We will be setting this per packet. Set to be case
280 * insensitive for now. */
281 conn
->case_sensitive
= false;
283 conn
->case_sensitive
= (bool)lp_case_sensitive(snum
);
286 conn
->case_preserve
= lp_preserve_case(snum
);
287 conn
->short_case_preserve
= lp_short_preserve_case(snum
);