s3-rpc_server: Created a per connection spoolss pipe.
[Samba/ekacnet.git] / source3 / smbd / smb2_tcon.c
blobe1b6775ced89ce2d8f68f83ec8cf775259c90f14
1 /*
2 Unix SMB/CIFS implementation.
3 Core SMB2 server
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/>.
21 #include "includes.h"
22 #include "smbd/globals.h"
23 #include "../libcli/smb/smb_common.h"
25 static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
26 const char *in_path,
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;
37 uint8_t *outhdr;
38 DATA_BLOB outbody;
39 size_t expected_body_size = 0x09;
40 size_t body_size;
41 uint16_t in_path_offset;
42 uint16_t in_path_length;
43 DATA_BLOB in_path_buffer;
44 char *in_path_string;
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;
50 uint32_t out_tree_id;
51 NTSTATUS status;
52 bool ok;
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,
80 in_path_buffer.data,
81 in_path_buffer.length,
82 &in_path_string,
83 &in_path_string_size, false);
84 if (!ok) {
85 return smbd_smb2_request_error(req, NT_STATUS_ILLEGAL_CHARACTER);
88 status = smbd_smb2_tree_connect(req, in_path_string,
89 &out_share_type,
90 &out_share_flags,
91 &out_capabilities,
92 &out_maximal_access,
93 &out_tree_id);
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) {
124 return 0;
127 idr_remove(tcon->session->tcons.idtree, tcon->tid);
128 DLIST_REMOVE(tcon->session->tcons.list, tcon);
130 if (tcon->compat_conn) {
131 set_current_service(tcon->compat_conn, 0, true);
132 close_cnum(tcon->compat_conn, tcon->session->vuid);
135 tcon->compat_conn = NULL;
136 tcon->tid = 0;
137 tcon->session = NULL;
139 return 0;
142 static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
143 const char *in_path,
144 uint8_t *out_share_type,
145 uint32_t *out_share_flags,
146 uint32_t *out_capabilities,
147 uint32_t *out_maximal_access,
148 uint32_t *out_tree_id)
150 const char *share = in_path;
151 fstring service;
152 int snum = -1;
153 struct smbd_smb2_tcon *tcon;
154 connection_struct *compat_conn = NULL;
155 user_struct *compat_vuser = req->session->compat_vuser;
156 int id;
157 NTSTATUS status;
159 if (strncmp(share, "\\\\", 2) == 0) {
160 const char *p = strchr(share+2, '\\');
161 if (p) {
162 share = p + 1;
166 DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
167 in_path, share));
169 fstrcpy(service, share);
171 strlower_m(service);
173 /* TODO: do more things... */
174 if (strequal(service,HOMES_NAME)) {
175 if (compat_vuser->homes_snum == -1) {
176 DEBUG(2, ("[homes] share not available for "
177 "user %s because it was not found "
178 "or created at session setup "
179 "time\n",
180 compat_vuser->server_info->unix_name));
181 return NT_STATUS_BAD_NETWORK_NAME;
183 snum = compat_vuser->homes_snum;
184 } else if ((compat_vuser->homes_snum != -1)
185 && strequal(service,
186 lp_servicename(compat_vuser->homes_snum))) {
187 snum = compat_vuser->homes_snum;
188 } else {
189 snum = find_service(service);
192 if (snum < 0) {
193 DEBUG(3,("smbd_smb2_tree_connect: couldn't find service %s\n",
194 service));
195 return NT_STATUS_BAD_NETWORK_NAME;
198 /* create a new tcon as child of the session */
199 tcon = talloc_zero(req->session, struct smbd_smb2_tcon);
200 if (tcon == NULL) {
201 return NT_STATUS_NO_MEMORY;
203 id = idr_get_new_random(req->session->tcons.idtree,
204 tcon,
205 req->session->tcons.limit);
206 if (id == -1) {
207 TALLOC_FREE(tcon);
208 return NT_STATUS_INSUFFICIENT_RESOURCES;
210 tcon->tid = id;
211 tcon->snum = snum;
213 DLIST_ADD_END(req->session->tcons.list, tcon,
214 struct smbd_smb2_tcon *);
215 tcon->session = req->session;
216 talloc_set_destructor(tcon, smbd_smb2_tcon_destructor);
218 compat_conn = make_connection_snum(req->sconn,
219 snum, req->session->compat_vuser,
220 data_blob_null, "???",
221 &status);
222 if (compat_conn == NULL) {
223 TALLOC_FREE(tcon);
224 return status;
226 tcon->compat_conn = talloc_move(tcon, &compat_conn);
227 tcon->compat_conn->cnum = tcon->tid;
229 if (IS_PRINT(tcon->compat_conn)) {
230 *out_share_type = SMB2_SHARE_TYPE_PRINT;
231 } else if (IS_IPC(tcon->compat_conn)) {
232 *out_share_type = SMB2_SHARE_TYPE_PIPE;
233 } else {
234 *out_share_type = SMB2_SHARE_TYPE_DISK;
237 *out_share_flags = SMB2_SHAREFLAG_ALLOW_NAMESPACE_CACHING;
239 if (lp_msdfs_root(SNUM(tcon->compat_conn)) && lp_host_msdfs()) {
240 *out_share_flags |= (SMB2_SHAREFLAG_DFS|SMB2_SHAREFLAG_DFS_ROOT);
241 *out_capabilities = SMB2_SHARE_CAP_DFS;
242 } else {
243 *out_capabilities = 0;
246 switch(lp_csc_policy(SNUM(tcon->compat_conn))) {
247 case CSC_POLICY_MANUAL:
248 break;
249 case CSC_POLICY_DOCUMENTS:
250 *out_share_flags |= SMB2_SHAREFLAG_AUTO_CACHING;
251 break;
252 case CSC_POLICY_PROGRAMS:
253 *out_share_flags |= SMB2_SHAREFLAG_VDO_CACHING;
254 break;
255 case CSC_POLICY_DISABLE:
256 *out_share_flags |= SMB2_SHAREFLAG_NO_CACHING;
257 break;
258 default:
259 break;
262 *out_maximal_access = FILE_GENERIC_ALL;
264 *out_tree_id = tcon->tid;
265 return NT_STATUS_OK;
268 NTSTATUS smbd_smb2_request_check_tcon(struct smbd_smb2_request *req)
270 const uint8_t *inhdr;
271 const uint8_t *outhdr;
272 int i = req->current_idx;
273 uint32_t in_tid;
274 void *p;
275 struct smbd_smb2_tcon *tcon;
276 bool chained_fixup = false;
278 inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
280 in_tid = IVAL(inhdr, SMB2_HDR_TID);
282 if (in_tid == (0xFFFFFFFF)) {
283 if (req->async) {
285 * async request - fill in tid from
286 * already setup out.vector[].iov_base.
288 outhdr = (const uint8_t *)req->out.vector[i].iov_base;
289 in_tid = IVAL(outhdr, SMB2_HDR_TID);
290 } else if (i > 2) {
292 * Chained request - fill in tid from
293 * the previous request out.vector[].iov_base.
295 outhdr = (const uint8_t *)req->out.vector[i-3].iov_base;
296 in_tid = IVAL(outhdr, SMB2_HDR_TID);
297 chained_fixup = true;
301 /* lookup an existing session */
302 p = idr_find(req->session->tcons.idtree, in_tid);
303 if (p == NULL) {
304 return NT_STATUS_NETWORK_NAME_DELETED;
306 tcon = talloc_get_type_abort(p, struct smbd_smb2_tcon);
308 if (!change_to_user(tcon->compat_conn,req->session->vuid)) {
309 return NT_STATUS_ACCESS_DENIED;
312 /* should we pass FLAG_CASELESS_PATHNAMES here? */
313 if (!set_current_service(tcon->compat_conn, 0, true)) {
314 return NT_STATUS_ACCESS_DENIED;
317 req->tcon = tcon;
319 if (chained_fixup) {
320 /* Fix up our own outhdr. */
321 outhdr = (const uint8_t *)req->out.vector[i].iov_base;
322 SIVAL(outhdr, SMB2_HDR_TID, in_tid);
325 return NT_STATUS_OK;
328 NTSTATUS smbd_smb2_request_process_tdis(struct smbd_smb2_request *req)
330 const uint8_t *inbody;
331 int i = req->current_idx;
332 DATA_BLOB outbody;
333 size_t expected_body_size = 0x04;
334 size_t body_size;
336 if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
337 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
340 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
342 body_size = SVAL(inbody, 0x00);
343 if (body_size != expected_body_size) {
344 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
348 * TODO: cancel all outstanding requests on the tcon
349 * and delete all file handles.
351 TALLOC_FREE(req->tcon);
353 outbody = data_blob_talloc(req->out.vector, NULL, 0x04);
354 if (outbody.data == NULL) {
355 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
358 SSVAL(outbody.data, 0x00, 0x04); /* struct size */
359 SSVAL(outbody.data, 0x02, 0); /* reserved */
361 return smbd_smb2_request_done(req, outbody, NULL);