Add SMB2 paths to smbd/conn.c. Except for conn_idle_all(), to be cleaned up next.
[Samba/vl.git] / source3 / smbd / conn.c
blobe8f14fd9189cf91e6629eebf7b24cc5e6e7e3678
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/globals.h"
25 /* The connections bitmap is expanded in increments of BITMAP_BLOCK_SZ. The
26 * maximum size of the bitmap is the largest positive integer, but you will hit
27 * the "max connections" limit, looong before that.
30 #define BITMAP_BLOCK_SZ 128
32 /****************************************************************************
33 Init the conn structures.
34 ****************************************************************************/
36 void conn_init(struct smbd_server_connection *sconn)
38 sconn->smb1.tcons.Connections = NULL;
39 sconn->smb1.tcons.bmap = bitmap_talloc(sconn, BITMAP_BLOCK_SZ);
42 /****************************************************************************
43 Return the number of open connections.
44 ****************************************************************************/
46 int conn_num_open(struct smbd_server_connection *sconn)
48 return sconn->num_tcons_open;
51 /****************************************************************************
52 Check if a snum is in use.
53 ****************************************************************************/
55 bool conn_snum_used(int snum)
57 struct smbd_server_connection *sconn = smbd_server_conn;
59 if (sconn->using_smb2) {
60 /* SMB2 */
61 struct smbd_smb2_session *sess;
62 for (sess = sconn->smb2.sessions.list; sess; sess = sess->next) {
63 struct smbd_smb2_tcon *ptcon;
65 for (ptcon = sess->tcons.list; ptcon; ptcon = ptcon->next) {
66 if (ptcon->compat_conn &&
67 ptcon->compat_conn->params &&
68 (ptcon->compat_conn->params->service = snum)) {
69 return true;
73 } else {
74 /* SMB1 */
75 connection_struct *conn;
76 for (conn=sconn->smb1.tcons.Connections;conn;conn=conn->next) {
77 if (conn->params->service == snum) {
78 return true;
82 return false;
85 /****************************************************************************
86 Find a conn given a cnum.
87 ****************************************************************************/
89 connection_struct *conn_find(struct smbd_server_connection *sconn,unsigned cnum)
91 if (sconn->using_smb2) {
92 /* SMB2 */
93 struct smbd_smb2_session *sess;
94 for (sess = sconn->smb2.sessions.list; sess; sess = sess->next) {
95 struct smbd_smb2_tcon *ptcon;
97 for (ptcon = sess->tcons.list; ptcon; ptcon = ptcon->next) {
98 if (ptcon->compat_conn &&
99 ptcon->compat_conn->cnum == cnum) {
100 return ptcon->compat_conn;
104 } else {
105 /* SMB1 */
106 int count=0;
107 connection_struct *conn;
108 for (conn=sconn->smb1.tcons.Connections;conn;conn=conn->next,count++) {
109 if (conn->cnum == cnum) {
110 if (count > 10) {
111 DLIST_PROMOTE(sconn->smb1.tcons.Connections,
112 conn);
114 return conn;
119 return NULL;
122 /****************************************************************************
123 Find first available connection slot, starting from a random position.
124 The randomisation stops problems with the server dieing and clients
125 thinking the server is still available.
126 ****************************************************************************/
128 connection_struct *conn_new(struct smbd_server_connection *sconn)
130 connection_struct *conn;
131 int i;
132 int find_offset = 1;
134 if (sconn->using_smb2) {
135 /* SMB2 */
136 if (!(conn=TALLOC_ZERO_P(NULL, connection_struct)) ||
137 !(conn->params = TALLOC_P(conn, struct share_params))) {
138 DEBUG(0,("TALLOC_ZERO() failed!\n"));
139 TALLOC_FREE(conn);
140 return NULL;
142 conn->sconn = sconn;
143 return conn;
146 /* SMB1 */
147 find_again:
148 i = bitmap_find(sconn->smb1.tcons.bmap, find_offset);
150 if (i == -1) {
151 /* Expand the connections bitmap. */
152 int oldsz = sconn->smb1.tcons.bmap->n;
153 int newsz = sconn->smb1.tcons.bmap->n +
154 BITMAP_BLOCK_SZ;
155 struct bitmap * nbmap;
157 if (newsz <= oldsz) {
158 /* Integer wrap. */
159 DEBUG(0,("ERROR! Out of connection structures\n"));
160 return NULL;
163 DEBUG(4,("resizing connections bitmap from %d to %d\n",
164 oldsz, newsz));
166 nbmap = bitmap_talloc(sconn, newsz);
167 if (!nbmap) {
168 DEBUG(0,("ERROR! malloc fail.\n"));
169 return NULL;
172 bitmap_copy(nbmap, sconn->smb1.tcons.bmap);
173 TALLOC_FREE(sconn->smb1.tcons.bmap);
175 sconn->smb1.tcons.bmap = nbmap;
176 find_offset = oldsz; /* Start next search in the new portion. */
178 goto find_again;
181 /* The bitmap position is used below as the connection number
182 * conn->cnum). This ends up as the TID field in the SMB header,
183 * which is limited to 16 bits (we skip 0xffff which is the
184 * NULL TID).
186 if (i > 65534) {
187 DEBUG(0, ("Maximum connection limit reached\n"));
188 return NULL;
191 if (!(conn=TALLOC_ZERO_P(NULL, connection_struct)) ||
192 !(conn->params = TALLOC_P(conn, struct share_params))) {
193 DEBUG(0,("TALLOC_ZERO() failed!\n"));
194 TALLOC_FREE(conn);
195 return NULL;
197 conn->sconn = sconn;
198 conn->cnum = i;
199 conn->force_group_gid = (gid_t)-1;
201 bitmap_set(sconn->smb1.tcons.bmap, i);
203 sconn->num_tcons_open++;
205 string_set(&conn->connectpath,"");
206 string_set(&conn->origpath,"");
208 DLIST_ADD(sconn->smb1.tcons.Connections, conn);
210 return conn;
213 /****************************************************************************
214 Close all conn structures.
215 Return true if any were closed.
216 ****************************************************************************/
218 bool conn_close_all(struct smbd_server_connection *sconn)
220 bool ret = false;
221 if (sconn->using_smb2) {
222 /* SMB2 */
223 struct smbd_smb2_session *sess;
224 for (sess = sconn->smb2.sessions.list; sess; sess = sess->next) {
225 struct smbd_smb2_tcon *tcon, *tc_next;
227 for (tcon = sess->tcons.list; tcon; tcon = tc_next) {
228 tc_next = tcon->next;
229 TALLOC_FREE(tcon);
230 ret = true;
233 } else {
234 /* SMB1 */
235 connection_struct *conn, *next;
237 for (conn=sconn->smb1.tcons.Connections;conn;conn=next) {
238 next=conn->next;
239 set_current_service(conn, 0, True);
240 close_cnum(conn, conn->vuid);
241 ret = true;
244 return ret;
247 /****************************************************************************
248 Idle inactive connections.
249 ****************************************************************************/
251 bool conn_idle_all(struct smbd_server_connection *sconn,time_t t)
253 int deadtime = lp_deadtime()*60;
254 connection_struct *conn;
256 if (deadtime <= 0)
257 deadtime = DEFAULT_SMBD_TIMEOUT;
259 for (conn=sconn->smb1.tcons.Connections;conn;conn=conn->next) {
261 time_t age = t - conn->lastused;
263 /* Update if connection wasn't idle. */
264 if (conn->lastused != conn->lastused_count) {
265 conn->lastused = t;
266 conn->lastused_count = t;
269 /* close dirptrs on connections that are idle */
270 if (age > DPTR_IDLE_TIMEOUT) {
271 dptr_idlecnum(conn);
274 if (conn->num_files_open > 0 || age < deadtime) {
275 return False;
280 * Check all pipes for any open handles. We cannot
281 * idle with a handle open.
283 if (check_open_pipes()) {
284 return False;
287 return True;
290 /****************************************************************************
291 Clear a vuid out of the validity cache, and as the 'owner' of a connection.
292 ****************************************************************************/
294 void conn_clear_vuid_caches(struct smbd_server_connection *sconn,uint16_t vuid)
296 connection_struct *conn;
298 if (sconn->using_smb2) {
299 /* SMB2 */
300 struct smbd_smb2_session *sess;
301 for (sess = sconn->smb2.sessions.list; sess; sess = sess->next) {
302 struct smbd_smb2_tcon *ptcon;
304 for (ptcon = sess->tcons.list; ptcon; ptcon = ptcon->next) {
305 if (ptcon->compat_conn) {
306 if (ptcon->compat_conn->vuid == vuid) {
307 ptcon->compat_conn->vuid = UID_FIELD_INVALID;
309 conn_clear_vuid_cache(ptcon->compat_conn, vuid);
313 } else {
314 /* SMB1 */
315 for (conn=sconn->smb1.tcons.Connections;conn;conn=conn->next) {
316 if (conn->vuid == vuid) {
317 conn->vuid = UID_FIELD_INVALID;
319 conn_clear_vuid_cache(conn, vuid);
324 /****************************************************************************
325 Free a conn structure - internal part.
326 ****************************************************************************/
328 static void conn_free_internal(connection_struct *conn)
330 vfs_handle_struct *handle = NULL, *thandle = NULL;
331 struct trans_state *state = NULL;
333 /* Free vfs_connection_struct */
334 handle = conn->vfs_handles;
335 while(handle) {
336 thandle = handle->next;
337 DLIST_REMOVE(conn->vfs_handles, handle);
338 if (handle->free_data)
339 handle->free_data(&handle->data);
340 handle = thandle;
343 /* Free any pending transactions stored on this conn. */
344 for (state = conn->pending_trans; state; state = state->next) {
345 /* state->setup is a talloc child of state. */
346 SAFE_FREE(state->param);
347 SAFE_FREE(state->data);
350 free_namearray(conn->veto_list);
351 free_namearray(conn->hide_list);
352 free_namearray(conn->veto_oplock_list);
353 free_namearray(conn->aio_write_behind_list);
355 string_free(&conn->connectpath);
356 string_free(&conn->origpath);
358 ZERO_STRUCTP(conn);
359 talloc_destroy(conn);
362 /****************************************************************************
363 Free a conn structure.
364 ****************************************************************************/
366 void conn_free(connection_struct *conn)
368 if (conn->sconn == NULL) {
369 conn_free_internal(conn);
370 return;
373 if (conn->sconn->using_smb2) {
374 /* SMB2 */
375 conn_free_internal(conn);
376 return;
379 /* SMB1 */
380 DLIST_REMOVE(conn->sconn->smb1.tcons.Connections, conn);
382 if (conn->sconn->smb1.tcons.bmap != NULL) {
384 * Can be NULL for fake connections created by
385 * create_conn_struct()
387 bitmap_clear(conn->sconn->smb1.tcons.bmap, conn->cnum);
390 SMB_ASSERT(conn->sconn->num_tcons_open > 0);
391 conn->sconn->num_tcons_open--;
393 conn_free_internal(conn);
396 /****************************************************************************
397 Receive a smbcontrol message to forcibly unmount a share.
398 The message contains just a share name and all instances of that
399 share are unmounted.
400 The special sharename '*' forces unmount of all shares.
401 ****************************************************************************/
403 void msg_force_tdis(struct messaging_context *msg,
404 void *private_data,
405 uint32_t msg_type,
406 struct server_id server_id,
407 DATA_BLOB *data)
409 struct smbd_server_connection *sconn;
410 connection_struct *conn, *next;
411 fstring sharename;
413 sconn = msg_ctx_to_sconn(msg);
414 if (sconn == NULL) {
415 DEBUG(1, ("could not find sconn\n"));
416 return;
419 fstrcpy(sharename, (const char *)data->data);
421 if (strcmp(sharename, "*") == 0) {
422 DEBUG(1,("Forcing close of all shares\n"));
423 conn_close_all(sconn);
424 return;
427 if (sconn->using_smb2) {
428 /* SMB2 */
429 struct smbd_smb2_session *sess;
430 for (sess = sconn->smb2.sessions.list; sess; sess = sess->next) {
431 struct smbd_smb2_tcon *tcon, *tc_next;
433 for (tcon = sess->tcons.list; tcon; tcon = tc_next) {
434 tc_next = tcon->next;
435 if (tcon->compat_conn &&
436 strequal(lp_servicename(SNUM(tcon->compat_conn)),
437 sharename)) {
438 DEBUG(1,("Forcing close of share %s cnum=%d\n",
439 sharename, tcon->compat_conn->cnum));
440 TALLOC_FREE(tcon);
444 } else {
445 /* SMB1 */
446 for (conn=sconn->smb1.tcons.Connections;conn;conn=next) {
447 next=conn->next;
448 if (strequal(lp_servicename(SNUM(conn)), sharename)) {
449 DEBUG(1,("Forcing close of share %s cnum=%d\n",
450 sharename, conn->cnum));
451 close_cnum(conn, (uint16)-1);