r17930: Merge noinclude branch:
[kai/samba-autobuild/.git] / source4 / smb_server / tcon.c
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
7    
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 2 of the License, or
11    (at your option) any later version.
12    
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.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "lib/util/dlinklist.h"
25 #include "smb_server/smb_server.h"
26 #include "smbd/service_stream.h"
27 #include "ntvfs/ntvfs.h"
28
29 struct socket_address *smbsrv_get_my_addr(void *p, TALLOC_CTX *mem_ctx)
30 {
31         struct smbsrv_connection *smb_conn = talloc_get_type(p,
32                                              struct smbsrv_connection);
33
34         return socket_get_my_addr(smb_conn->connection->socket, mem_ctx);
35 }
36
37 struct socket_address *smbsrv_get_peer_addr(void *p, TALLOC_CTX *mem_ctx)
38 {
39         struct smbsrv_connection *smb_conn = talloc_get_type(p,
40                                              struct smbsrv_connection);
41
42         return socket_get_peer_addr(smb_conn->connection->socket, mem_ctx);
43 }
44
45 /****************************************************************************
46 init the tcon structures
47 ****************************************************************************/
48 static NTSTATUS smbsrv_init_tcons(struct smbsrv_tcons_context *tcons_ctx, TALLOC_CTX *mem_ctx, uint32_t limit)
49 {
50         /* 
51          * the idr_* functions take 'int' as limit,
52          * and only work with a max limit 0x00FFFFFF
53          */
54         limit &= 0x00FFFFFF;
55
56         tcons_ctx->idtree_tid   = idr_init(mem_ctx);
57         NT_STATUS_HAVE_NO_MEMORY(tcons_ctx->idtree_tid);
58         tcons_ctx->idtree_limit = limit;
59         tcons_ctx->list         = NULL;
60
61         return NT_STATUS_OK;
62 }
63
64 NTSTATUS smbsrv_smb_init_tcons(struct smbsrv_connection *smb_conn)
65 {
66         return smbsrv_init_tcons(&smb_conn->smb_tcons, smb_conn, UINT16_MAX);
67 }
68
69 NTSTATUS smbsrv_smb2_init_tcons(struct smbsrv_session *smb_sess)
70 {
71         return smbsrv_init_tcons(&smb_sess->smb2_tcons, smb_sess, UINT32_MAX);
72 }
73
74 /****************************************************************************
75 find a tcon given a tid for SMB
76 ****************************************************************************/
77 static struct smbsrv_tcon *smbsrv_tcon_find(struct smbsrv_tcons_context *tcons_ctx,
78                                             uint32_t tid, struct timeval request_time)
79 {
80         void *p;
81         struct smbsrv_tcon *tcon;
82
83         if (tid == 0) return NULL;
84
85         if (tid > tcons_ctx->idtree_limit) return NULL;
86
87         p = idr_find(tcons_ctx->idtree_tid, tid);
88         if (!p) return NULL;
89
90         tcon = talloc_get_type(p, struct smbsrv_tcon);
91         if (!tcon) return NULL;
92
93         tcon->statistics.last_request_time = request_time;
94
95         return tcon;
96 }
97
98 struct smbsrv_tcon *smbsrv_smb_tcon_find(struct smbsrv_connection *smb_conn,
99                                          uint32_t tid, struct timeval request_time)
100 {
101         return smbsrv_tcon_find(&smb_conn->smb_tcons, tid, request_time);
102 }
103
104 struct smbsrv_tcon *smbsrv_smb2_tcon_find(struct smbsrv_session *smb_sess,
105                                           uint32_t tid, struct timeval request_time)
106 {
107         if (!smb_sess) return NULL;
108         return smbsrv_tcon_find(&smb_sess->smb2_tcons, tid, request_time);
109 }
110
111 /*
112   destroy a connection structure
113 */
114 static int smbsrv_tcon_destructor(struct smbsrv_tcon *tcon)
115 {
116         struct smbsrv_tcons_context *tcons_ctx;
117         struct socket_address *client_addr;
118
119         client_addr = socket_get_peer_addr(tcon->smb_conn->connection->socket, tcon);
120         DEBUG(3,("%s closed connection to service %s\n",
121                  client_addr ? client_addr->addr : "(unknown)",
122                  tcon->share_name));
123
124         /* tell the ntvfs backend that we are disconnecting */
125         if (tcon->ntvfs) {
126                 ntvfs_disconnect(tcon->ntvfs);
127                 tcon->ntvfs = NULL;
128         }
129
130         if (tcon->smb2.session) {
131                 tcons_ctx = &tcon->smb2.session->smb2_tcons;
132         } else {
133                 tcons_ctx = &tcon->smb_conn->smb_tcons;
134         }
135
136         idr_remove(tcons_ctx->idtree_tid, tcon->tid);
137         DLIST_REMOVE(tcons_ctx->list, tcon);
138         return 0;
139 }
140
141 /*
142   find first available connection slot
143 */
144 static struct smbsrv_tcon *smbsrv_tcon_new(struct smbsrv_connection *smb_conn,
145                                            struct smbsrv_session *smb_sess,
146                                            const char *share_name)
147 {
148         TALLOC_CTX *mem_ctx;
149         struct smbsrv_tcons_context *tcons_ctx;
150         struct smbsrv_tcon *tcon;
151         NTSTATUS status;
152         int i;
153
154         if (smb_sess) {
155                 mem_ctx = smb_sess;
156                 tcons_ctx = &smb_sess->smb2_tcons;
157         } else {
158                 mem_ctx = smb_conn;
159                 tcons_ctx = &smb_conn->smb_tcons;
160         }
161
162         tcon = talloc_zero(mem_ctx, struct smbsrv_tcon);
163         if (!tcon) return NULL;
164         tcon->smb_conn          = smb_conn;
165         tcon->smb2.session      = smb_sess;
166         tcon->share_name        = talloc_strdup(tcon, share_name);
167         if (!tcon->share_name) goto failed;
168
169         /*
170          * the use -1 here, because we don't want to give away the wildcard
171          * fnum used in SMBflush
172          */
173         status = smbsrv_init_handles(tcon, UINT16_MAX - 1);
174         if (!NT_STATUS_IS_OK(status)) {
175                 DEBUG(1,("ERROR! failed to init handles: %s\n", nt_errstr(status)));
176                 goto failed;
177         }
178
179         i = idr_get_new_random(tcons_ctx->idtree_tid, tcon, tcons_ctx->idtree_limit);
180         if (i == -1) {
181                 DEBUG(1,("ERROR! Out of connection structures\n"));
182                 goto failed;
183         }
184         tcon->tid = i;
185
186         DLIST_ADD(tcons_ctx->list, tcon);
187         talloc_set_destructor(tcon, smbsrv_tcon_destructor);
188
189         /* now fill in some statistics */
190         tcon->statistics.connect_time = timeval_current();
191
192         return tcon;
193
194 failed:
195         talloc_free(tcon);
196         return NULL;
197 }
198
199 struct smbsrv_tcon *smbsrv_smb_tcon_new(struct smbsrv_connection *smb_conn, const char *share_name)
200 {
201         return smbsrv_tcon_new(smb_conn, NULL, share_name);
202 }
203
204 struct smbsrv_tcon *smbsrv_smb2_tcon_new(struct smbsrv_session *smb_sess, const char *share_name)
205 {
206         return smbsrv_tcon_new(smb_sess->smb_conn, smb_sess, share_name);
207 }