python/samba/tests/krb5: Allow PkInitTests.test_pkinit_ntlm_from_pac_must_change_now...
[samba.git] / source4 / smb_server / tcon.c
blob04dc7fe9d86e055b15f24e2ae77661b0a18aaf58
1 /*
2 Unix SMB/CIFS implementation.
3 Manage smbsrv_tcon structures
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Alexander Bokovoy 2002
6 Copyright (C) Stefan Metzmacher 2005-2006
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 "smb_server/smb_server.h"
24 #include "samba/service_stream.h"
25 #include "lib/tsocket/tsocket.h"
26 #include "ntvfs/ntvfs.h"
27 #include "lib/util/idtree_random.h"
29 /****************************************************************************
30 init the tcon structures
31 ****************************************************************************/
32 static NTSTATUS smbsrv_init_tcons(struct smbsrv_tcons_context *tcons_ctx, TALLOC_CTX *mem_ctx, uint32_t limit)
34 /*
35 * the idr_* functions take 'int' as limit,
36 * and only work with a max limit 0x00FFFFFF
38 limit &= 0x00FFFFFF;
40 tcons_ctx->idtree_tid = idr_init(mem_ctx);
41 NT_STATUS_HAVE_NO_MEMORY(tcons_ctx->idtree_tid);
42 tcons_ctx->idtree_limit = limit;
43 tcons_ctx->list = NULL;
45 return NT_STATUS_OK;
48 NTSTATUS smbsrv_smb_init_tcons(struct smbsrv_connection *smb_conn)
50 return smbsrv_init_tcons(&smb_conn->smb_tcons, smb_conn, UINT16_MAX);
53 NTSTATUS smbsrv_smb2_init_tcons(struct smbsrv_session *smb_sess)
55 return smbsrv_init_tcons(&smb_sess->smb2_tcons, smb_sess, UINT32_MAX);
58 /****************************************************************************
59 find a tcon given a tid for SMB
60 ****************************************************************************/
61 static struct smbsrv_tcon *smbsrv_tcon_find(struct smbsrv_tcons_context *tcons_ctx,
62 uint32_t tid, struct timeval request_time)
64 void *p;
65 struct smbsrv_tcon *tcon;
67 if (tid == 0) return NULL;
69 if (tid > tcons_ctx->idtree_limit) return NULL;
71 p = idr_find(tcons_ctx->idtree_tid, tid);
72 if (!p) return NULL;
74 tcon = talloc_get_type(p, struct smbsrv_tcon);
75 if (!tcon) return NULL;
77 tcon->statistics.last_request_time = request_time;
79 return tcon;
82 struct smbsrv_tcon *smbsrv_smb_tcon_find(struct smbsrv_connection *smb_conn,
83 uint32_t tid, struct timeval request_time)
85 return smbsrv_tcon_find(&smb_conn->smb_tcons, tid, request_time);
88 struct smbsrv_tcon *smbsrv_smb2_tcon_find(struct smbsrv_session *smb_sess,
89 uint32_t tid, struct timeval request_time)
91 if (!smb_sess) return NULL;
92 return smbsrv_tcon_find(&smb_sess->smb2_tcons, tid, request_time);
96 destroy a connection structure
98 static int smbsrv_tcon_destructor(struct smbsrv_tcon *tcon)
100 struct smbsrv_tcons_context *tcons_ctx;
101 struct tsocket_address *client_addr;
103 client_addr = tcon->smb_conn->connection->remote_address;
105 DEBUG(3,("%s closed connection to service %s\n",
106 tsocket_address_string(client_addr, tcon),
107 tcon->share_name));
109 /* tell the ntvfs backend that we are disconnecting */
110 if (tcon->ntvfs) {
111 ntvfs_disconnect(tcon->ntvfs);
112 tcon->ntvfs = NULL;
115 if (tcon->smb2.session) {
116 tcons_ctx = &tcon->smb2.session->smb2_tcons;
117 } else {
118 tcons_ctx = &tcon->smb_conn->smb_tcons;
121 idr_remove(tcons_ctx->idtree_tid, tcon->tid);
122 DLIST_REMOVE(tcons_ctx->list, tcon);
123 return 0;
127 find first available connection slot
129 static struct smbsrv_tcon *smbsrv_tcon_new(struct smbsrv_connection *smb_conn,
130 struct smbsrv_session *smb_sess,
131 const char *share_name)
133 TALLOC_CTX *mem_ctx;
134 struct smbsrv_tcons_context *tcons_ctx;
135 uint32_t handle_uint_max;
136 struct smbsrv_tcon *tcon;
137 NTSTATUS status;
138 int i;
140 if (smb_sess) {
141 mem_ctx = smb_sess;
142 tcons_ctx = &smb_sess->smb2_tcons;
143 handle_uint_max = UINT32_MAX;
144 } else {
145 mem_ctx = smb_conn;
146 tcons_ctx = &smb_conn->smb_tcons;
147 handle_uint_max = UINT16_MAX;
150 tcon = talloc_zero(mem_ctx, struct smbsrv_tcon);
151 if (!tcon) return NULL;
152 tcon->smb_conn = smb_conn;
153 tcon->smb2.session = smb_sess;
154 tcon->share_name = talloc_strdup(tcon, share_name);
155 if (!tcon->share_name) goto failed;
158 * the use -1 here, because we don't want to give away the wildcard
159 * fnum used in SMBflush
161 status = smbsrv_init_handles(tcon, handle_uint_max - 1);
162 if (!NT_STATUS_IS_OK(status)) {
163 DEBUG(1,("ERROR! failed to init handles: %s\n", nt_errstr(status)));
164 goto failed;
167 i = idr_get_new_random(
168 tcons_ctx->idtree_tid, tcon, 1, tcons_ctx->idtree_limit);
169 if (i == -1) {
170 DEBUG(1,("ERROR! Out of connection structures\n"));
171 goto failed;
173 tcon->tid = i;
175 DLIST_ADD(tcons_ctx->list, tcon);
176 talloc_set_destructor(tcon, smbsrv_tcon_destructor);
178 /* now fill in some statistics */
179 tcon->statistics.connect_time = timeval_current();
181 return tcon;
183 failed:
184 talloc_free(tcon);
185 return NULL;
188 struct smbsrv_tcon *smbsrv_smb_tcon_new(struct smbsrv_connection *smb_conn, const char *share_name)
190 return smbsrv_tcon_new(smb_conn, NULL, share_name);
193 struct smbsrv_tcon *smbsrv_smb2_tcon_new(struct smbsrv_session *smb_sess, const char *share_name)
195 return smbsrv_tcon_new(smb_sess->smb_conn, smb_sess, share_name);