s3:smbd: change blocking.c to use fsp_fnum_dbg() for fsp->fnum logging.
[Samba/gebeck_regimport.git] / source3 / smbd / conn.c
blob399935a96451215eaa108d84a144393dd2fa4d77
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 /* The connections bitmap is expanded in increments of BITMAP_BLOCK_SZ. The
28 * maximum size of the bitmap is the largest positive integer, but you will hit
29 * the "max connections" limit, looong before that.
32 #define BITMAP_BLOCK_SZ 128
34 /****************************************************************************
35 Init the conn structures.
36 ****************************************************************************/
38 void conn_init(struct smbd_server_connection *sconn)
40 sconn->smb1.tcons.bmap = bitmap_talloc(sconn, BITMAP_BLOCK_SZ);
43 /****************************************************************************
44 Return the number of open connections.
45 ****************************************************************************/
47 int conn_num_open(struct smbd_server_connection *sconn)
49 return sconn->num_connections;
52 /****************************************************************************
53 Check if a snum is in use.
54 ****************************************************************************/
56 bool conn_snum_used(struct smbd_server_connection *sconn,
57 int snum)
59 struct connection_struct *conn;
61 for (conn=sconn->connections; conn; conn=conn->next) {
62 if (conn->params->service == snum) {
63 return true;
67 return false;
70 /****************************************************************************
71 Find a conn given a cnum.
72 ****************************************************************************/
74 connection_struct *conn_find(struct smbd_server_connection *sconn, uint32_t cnum)
76 size_t count=0;
77 struct connection_struct *conn;
79 for (conn=sconn->connections; conn; conn=conn->next,count++) {
80 if (conn->cnum == cnum) {
81 if (count > 10) {
82 DLIST_PROMOTE(sconn->connections, conn);
84 return conn;
88 return NULL;
91 /****************************************************************************
92 Find first available connection slot, starting from a random position.
93 The randomisation stops problems with the server dieing and clients
94 thinking the server is still available.
95 ****************************************************************************/
97 connection_struct *conn_new(struct smbd_server_connection *sconn)
99 connection_struct *conn;
100 int i;
101 uint32_t cnum;
102 int find_offset = 1;
104 if (sconn->using_smb2) {
105 /* SMB2 */
106 if (!(conn=talloc_zero(NULL, connection_struct)) ||
107 !(conn->params = talloc(conn, struct share_params))) {
108 DEBUG(0,("TALLOC_ZERO() failed!\n"));
109 TALLOC_FREE(conn);
110 return NULL;
112 conn->sconn = sconn;
114 DLIST_ADD(sconn->connections, conn);
115 sconn->num_connections++;
117 return conn;
120 /* SMB1 */
121 find_again:
122 i = bitmap_find(sconn->smb1.tcons.bmap, find_offset);
124 if (i == -1) {
125 /* Expand the connections bitmap. */
126 int oldsz = sconn->smb1.tcons.bmap->n;
127 int newsz = sconn->smb1.tcons.bmap->n +
128 BITMAP_BLOCK_SZ;
129 struct bitmap * nbmap;
131 if (newsz <= oldsz) {
132 /* Integer wrap. */
133 DEBUG(0,("ERROR! Out of connection structures\n"));
134 return NULL;
137 DEBUG(4,("resizing connections bitmap from %d to %d\n",
138 oldsz, newsz));
140 nbmap = bitmap_talloc(sconn, newsz);
141 if (!nbmap) {
142 DEBUG(0,("ERROR! malloc fail.\n"));
143 return NULL;
146 bitmap_copy(nbmap, sconn->smb1.tcons.bmap);
147 TALLOC_FREE(sconn->smb1.tcons.bmap);
149 sconn->smb1.tcons.bmap = nbmap;
150 find_offset = oldsz; /* Start next search in the new portion. */
152 goto find_again;
155 /* The bitmap position is used below as the connection number
156 * conn->cnum). This ends up as the TID field in the SMB header,
157 * which is limited to 16 bits (we skip 0xffff which is the
158 * NULL TID).
160 cnum = i + CNUM_OFFSET;
161 if (cnum >= 0xFFFF) {
162 DEBUG(0, ("Maximum connection limit reached\n"));
163 return NULL;
166 if (!(conn=talloc_zero(NULL, connection_struct)) ||
167 !(conn->params = talloc(conn, struct share_params))) {
168 DEBUG(0,("TALLOC_ZERO() failed!\n"));
169 TALLOC_FREE(conn);
170 return NULL;
172 conn->sconn = sconn;
173 conn->cnum = cnum;
174 conn->force_group_gid = (gid_t)-1;
176 bitmap_set(sconn->smb1.tcons.bmap, i);
178 string_set(&conn->connectpath,"");
179 string_set(&conn->origpath,"");
181 DLIST_ADD(sconn->connections, conn);
182 sconn->num_connections++;
184 return conn;
187 /****************************************************************************
188 Clear a vuid out of the connection's vuid cache
189 ****************************************************************************/
191 static void conn_clear_vuid_cache(connection_struct *conn, uint64_t vuid)
193 int i;
195 for (i=0; i<VUID_CACHE_SIZE; i++) {
196 struct vuid_cache_entry *ent;
198 ent = &conn->vuid_cache.array[i];
200 if (ent->vuid == vuid) {
201 ent->vuid = UID_FIELD_INVALID;
203 * We need to keep conn->session_info around
204 * if it's equal to ent->session_info as a SMBulogoff
205 * is often followed by a SMBtdis (with an invalid
206 * vuid). The debug code (or regular code in
207 * vfs_full_audit) wants to refer to the
208 * conn->session_info pointer to print debug
209 * statements. Theoretically this is a bug,
210 * as once the vuid is gone the session_info
211 * on the conn struct isn't valid any more,
212 * but there's enough code that assumes
213 * conn->session_info is never null that
214 * it's easier to hold onto the old pointer
215 * until we get a new sessionsetupX.
216 * As everything is hung off the
217 * conn pointer as a talloc context we're not
218 * leaking memory here. See bug #6315. JRA.
220 if (conn->session_info == ent->session_info) {
221 ent->session_info = NULL;
222 } else {
223 TALLOC_FREE(ent->session_info);
225 ent->read_only = False;
230 /****************************************************************************
231 Clear a vuid out of the validity cache, and as the 'owner' of a connection.
233 Called from invalidate_vuid()
234 ****************************************************************************/
236 void conn_clear_vuid_caches(struct smbd_server_connection *sconn, uint64_t vuid)
238 connection_struct *conn;
240 for (conn=sconn->connections; conn;conn=conn->next) {
241 if (conn->vuid == vuid) {
242 conn->vuid = UID_FIELD_INVALID;
244 conn_clear_vuid_cache(conn, vuid);
248 /****************************************************************************
249 Free a conn structure - internal part.
250 ****************************************************************************/
252 static void conn_free_internal(connection_struct *conn)
254 vfs_handle_struct *handle = NULL, *thandle = NULL;
255 struct trans_state *state = NULL;
257 /* Free vfs_connection_struct */
258 handle = conn->vfs_handles;
259 while(handle) {
260 thandle = handle->next;
261 DLIST_REMOVE(conn->vfs_handles, handle);
262 if (handle->free_data)
263 handle->free_data(&handle->data);
264 handle = thandle;
267 /* Free any pending transactions stored on this conn. */
268 for (state = conn->pending_trans; state; state = state->next) {
269 /* state->setup is a talloc child of state. */
270 SAFE_FREE(state->param);
271 SAFE_FREE(state->data);
274 free_namearray(conn->veto_list);
275 free_namearray(conn->hide_list);
276 free_namearray(conn->veto_oplock_list);
277 free_namearray(conn->aio_write_behind_list);
279 string_free(&conn->connectpath);
280 string_free(&conn->origpath);
282 ZERO_STRUCTP(conn);
283 talloc_destroy(conn);
286 /****************************************************************************
287 Free a conn structure.
288 ****************************************************************************/
290 void conn_free(connection_struct *conn)
292 if (conn->sconn == NULL) {
293 conn_free_internal(conn);
294 return;
297 if (!conn->sconn->using_smb2 &&
298 conn->sconn->smb1.tcons.bmap != NULL &&
299 conn->cnum >= CNUM_OFFSET &&
300 conn->cnum < 0xFFFF)
302 int i = conn->cnum - CNUM_OFFSET;
304 * Can be NULL for fake connections created by
305 * create_conn_struct()
307 bitmap_clear(conn->sconn->smb1.tcons.bmap, i);
310 DLIST_REMOVE(conn->sconn->connections, conn);
311 SMB_ASSERT(conn->sconn->num_connections > 0);
312 conn->sconn->num_connections--;
314 conn_free_internal(conn);