2 Unix SMB/CIFS implementation.
5 Copyright (C) Stefan Metzmacher 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "smbd/globals.h"
23 #include "../source4/libcli/smb2/smb2_constants.h"
25 static NTSTATUS
smbd_smb2_tree_connect(struct smbd_smb2_request
*req
,
27 uint8_t *out_share_type
,
28 uint32_t *out_share_flags
,
29 uint32_t *out_capabilities
,
30 uint32_t *out_maximal_access
,
31 uint32_t *out_tree_id
);
33 NTSTATUS
smbd_smb2_request_process_tcon(struct smbd_smb2_request
*req
)
35 const uint8_t *inbody
;
36 int i
= req
->current_idx
;
39 size_t expected_body_size
= 0x09;
41 uint16_t in_path_offset
;
42 uint16_t in_path_length
;
43 DATA_BLOB in_path_buffer
;
45 size_t in_path_string_size
;
46 uint8_t out_share_type
;
47 uint32_t out_share_flags
;
48 uint32_t out_capabilities
;
49 uint32_t out_maximal_access
;
54 if (req
->in
.vector
[i
+1].iov_len
!= (expected_body_size
& 0xFFFFFFFE)) {
55 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
58 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
60 body_size
= SVAL(inbody
, 0x00);
61 if (body_size
!= expected_body_size
) {
62 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
65 in_path_offset
= SVAL(inbody
, 0x04);
66 in_path_length
= SVAL(inbody
, 0x06);
68 if (in_path_offset
!= (SMB2_HDR_BODY
+ (body_size
& 0xFFFFFFFE))) {
69 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
72 if (in_path_length
> req
->in
.vector
[i
+2].iov_len
) {
73 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
76 in_path_buffer
.data
= (uint8_t *)req
->in
.vector
[i
+2].iov_base
;
77 in_path_buffer
.length
= in_path_length
;
79 ok
= convert_string_talloc(req
, CH_UTF16
, CH_UNIX
,
81 in_path_buffer
.length
,
83 &in_path_string_size
, false);
85 return smbd_smb2_request_error(req
, NT_STATUS_ILLEGAL_CHARACTER
);
88 status
= smbd_smb2_tree_connect(req
, in_path_string
,
94 if (!NT_STATUS_IS_OK(status
)) {
95 return smbd_smb2_request_error(req
, status
);
98 outhdr
= (uint8_t *)req
->out
.vector
[i
].iov_base
;
100 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x10);
101 if (outbody
.data
== NULL
) {
102 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
105 SIVAL(outhdr
, SMB2_HDR_TID
, out_tree_id
);
107 SSVAL(outbody
.data
, 0x00, 0x10); /* struct size */
108 SCVAL(outbody
.data
, 0x02,
109 out_share_type
); /* share type */
110 SCVAL(outbody
.data
, 0x03, 0); /* reserved */
111 SIVAL(outbody
.data
, 0x04,
112 out_share_flags
); /* share flags */
113 SIVAL(outbody
.data
, 0x08,
114 out_capabilities
); /* capabilities */
115 SIVAL(outbody
.data
, 0x0C,
116 out_maximal_access
); /* maximal access */
118 return smbd_smb2_request_done(req
, outbody
, NULL
);
121 static int smbd_smb2_tcon_destructor(struct smbd_smb2_tcon
*tcon
)
123 if (tcon
->session
== NULL
) {
127 idr_remove(tcon
->session
->tcons
.idtree
, tcon
->tid
);
128 DLIST_REMOVE(tcon
->session
->tcons
.list
, tcon
);
130 conn_free(tcon
->compat_conn
);
132 tcon
->compat_conn
= NULL
;
134 tcon
->session
= NULL
;
139 static NTSTATUS
smbd_smb2_tree_connect(struct smbd_smb2_request
*req
,
141 uint8_t *out_share_type
,
142 uint32_t *out_share_flags
,
143 uint32_t *out_capabilities
,
144 uint32_t *out_maximal_access
,
145 uint32_t *out_tree_id
)
147 const char *share
= in_path
;
150 struct smbd_smb2_tcon
*tcon
;
154 if (strncmp(share
, "\\\\", 2) == 0) {
155 const char *p
= strchr(share
+2, '\\');
161 DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
164 fstrcpy(service
, share
);
168 snum
= find_service(service
);
170 DEBUG(1,("smbd_smb2_tree_connect: couldn't find service %s\n",
172 return NT_STATUS_BAD_NETWORK_NAME
;
175 /* TODO: do more things... */
177 /* create a new tcon as child of the session */
178 tcon
= talloc_zero(req
->session
, struct smbd_smb2_tcon
);
180 return NT_STATUS_NO_MEMORY
;
182 id
= idr_get_new_random(req
->session
->tcons
.idtree
,
184 req
->session
->tcons
.limit
);
187 return NT_STATUS_INSUFFICIENT_RESOURCES
;
192 DLIST_ADD_END(req
->session
->tcons
.list
, tcon
,
193 struct smbd_smb2_tcon
*);
194 tcon
->session
= req
->session
;
195 talloc_set_destructor(tcon
, smbd_smb2_tcon_destructor
);
197 tcon
->compat_conn
= make_connection_snum(req
->sconn
,
198 snum
, req
->session
->compat_vuser
,
199 data_blob_null
, "???",
201 if (tcon
->compat_conn
== NULL
) {
205 tcon
->compat_conn
->cnum
= tcon
->tid
;
207 *out_share_type
= 0x01;
208 *out_share_flags
= SMB2_SHAREFLAG_ALL
;
209 *out_capabilities
= 0;
210 *out_maximal_access
= FILE_GENERIC_ALL
;
212 *out_tree_id
= tcon
->tid
;
216 NTSTATUS
smbd_smb2_request_check_tcon(struct smbd_smb2_request
*req
)
218 const uint8_t *inhdr
;
219 int i
= req
->current_idx
;
222 struct smbd_smb2_tcon
*tcon
;
224 inhdr
= (const uint8_t *)req
->in
.vector
[i
+0].iov_base
;
226 in_tid
= IVAL(inhdr
, SMB2_HDR_TID
);
228 /* lookup an existing session */
229 p
= idr_find(req
->session
->tcons
.idtree
, in_tid
);
231 return NT_STATUS_NETWORK_NAME_DELETED
;
233 tcon
= talloc_get_type_abort(p
, struct smbd_smb2_tcon
);
235 if (!change_to_user(tcon
->compat_conn
,req
->session
->vuid
)) {
236 return NT_STATUS_ACCESS_DENIED
;
239 /* should we pass FLAG_CASELESS_PATHNAMES here? */
240 if (!set_current_service(tcon
->compat_conn
, 0, true)) {
241 return NT_STATUS_ACCESS_DENIED
;
248 NTSTATUS
smbd_smb2_request_process_tdis(struct smbd_smb2_request
*req
)
250 const uint8_t *inbody
;
251 int i
= req
->current_idx
;
253 size_t expected_body_size
= 0x04;
256 if (req
->in
.vector
[i
+1].iov_len
!= (expected_body_size
& 0xFFFFFFFE)) {
257 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
260 inbody
= (const uint8_t *)req
->in
.vector
[i
+1].iov_base
;
262 body_size
= SVAL(inbody
, 0x00);
263 if (body_size
!= expected_body_size
) {
264 return smbd_smb2_request_error(req
, NT_STATUS_INVALID_PARAMETER
);
268 * TODO: cancel all outstanding requests on the tcon
269 * and delete all file handles.
271 TALLOC_FREE(req
->tcon
);
273 outbody
= data_blob_talloc(req
->out
.vector
, NULL
, 0x04);
274 if (outbody
.data
== NULL
) {
275 return smbd_smb2_request_error(req
, NT_STATUS_NO_MEMORY
);
278 SSVAL(outbody
.data
, 0x00, 0x04); /* struct size */
279 SSVAL(outbody
.data
, 0x02, 0); /* reserved */
281 return smbd_smb2_request_done(req
, outbody
, NULL
);