s3:smbd: make use of smbXsrv_tcon and smbXsrv_session for smb2
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_tcon.c
blobceb145ff63a5a15c6fdb5f3e7bc898e2e158b5c4
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/smbd.h"
23 #include "smbd/globals.h"
24 #include "../libcli/smb/smb_common.h"
25 #include "../libcli/security/security.h"
26 #include "auth.h"
27 #include "lib/param/loadparm.h"
28 #include "../lib/util/tevent_ntstatus.h"
30 static struct tevent_req *smbd_smb2_tree_connect_send(TALLOC_CTX *mem_ctx,
31 struct tevent_context *ev,
32 struct smbd_smb2_request *smb2req,
33 const char *in_path);
34 static NTSTATUS smbd_smb2_tree_connect_recv(struct tevent_req *req,
35 uint8_t *out_share_type,
36 uint32_t *out_share_flags,
37 uint32_t *out_capabilities,
38 uint32_t *out_maximal_access,
39 uint32_t *out_tree_id);
41 static void smbd_smb2_request_tcon_done(struct tevent_req *subreq);
43 NTSTATUS smbd_smb2_request_process_tcon(struct smbd_smb2_request *req)
45 const uint8_t *inbody;
46 int i = req->current_idx;
47 uint16_t in_path_offset;
48 uint16_t in_path_length;
49 DATA_BLOB in_path_buffer;
50 char *in_path_string;
51 size_t in_path_string_size;
52 NTSTATUS status;
53 bool ok;
54 struct tevent_req *subreq;
56 status = smbd_smb2_request_verify_sizes(req, 0x09);
57 if (!NT_STATUS_IS_OK(status)) {
58 return smbd_smb2_request_error(req, status);
60 inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
62 in_path_offset = SVAL(inbody, 0x04);
63 in_path_length = SVAL(inbody, 0x06);
65 if (in_path_offset != (SMB2_HDR_BODY + req->in.vector[i+1].iov_len)) {
66 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
69 if (in_path_length > req->in.vector[i+2].iov_len) {
70 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
73 in_path_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
74 in_path_buffer.length = in_path_length;
76 ok = convert_string_talloc(req, CH_UTF16, CH_UNIX,
77 in_path_buffer.data,
78 in_path_buffer.length,
79 &in_path_string,
80 &in_path_string_size);
81 if (!ok) {
82 return smbd_smb2_request_error(req, NT_STATUS_ILLEGAL_CHARACTER);
85 if (in_path_buffer.length == 0) {
86 in_path_string_size = 0;
89 if (strlen(in_path_string) != in_path_string_size) {
90 return smbd_smb2_request_error(req, NT_STATUS_BAD_NETWORK_NAME);
93 subreq = smbd_smb2_tree_connect_send(req,
94 req->sconn->ev_ctx,
95 req,
96 in_path_string);
97 if (subreq == NULL) {
98 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
100 tevent_req_set_callback(subreq, smbd_smb2_request_tcon_done, req);
102 return smbd_smb2_request_pending_queue(req, subreq, 500);
105 static void smbd_smb2_request_tcon_done(struct tevent_req *subreq)
107 struct smbd_smb2_request *req =
108 tevent_req_callback_data(subreq,
109 struct smbd_smb2_request);
110 int i = req->current_idx;
111 uint8_t *outhdr;
112 DATA_BLOB outbody;
113 uint8_t out_share_type = 0;
114 uint32_t out_share_flags = 0;
115 uint32_t out_capabilities = 0;
116 uint32_t out_maximal_access = 0;
117 uint32_t out_tree_id = 0;
118 NTSTATUS status;
119 NTSTATUS error;
121 status = smbd_smb2_tree_connect_recv(subreq,
122 &out_share_type,
123 &out_share_flags,
124 &out_capabilities,
125 &out_maximal_access,
126 &out_tree_id);
127 TALLOC_FREE(subreq);
128 if (!NT_STATUS_IS_OK(status)) {
129 error = smbd_smb2_request_error(req, status);
130 if (!NT_STATUS_IS_OK(error)) {
131 smbd_server_connection_terminate(req->sconn,
132 nt_errstr(error));
133 return;
135 return;
138 outhdr = (uint8_t *)req->out.vector[i].iov_base;
140 outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
141 if (outbody.data == NULL) {
142 error = smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
143 if (!NT_STATUS_IS_OK(error)) {
144 smbd_server_connection_terminate(req->sconn,
145 nt_errstr(error));
146 return;
148 return;
151 SIVAL(outhdr, SMB2_HDR_TID, out_tree_id);
153 SSVAL(outbody.data, 0x00, 0x10); /* struct size */
154 SCVAL(outbody.data, 0x02,
155 out_share_type); /* share type */
156 SCVAL(outbody.data, 0x03, 0); /* reserved */
157 SIVAL(outbody.data, 0x04,
158 out_share_flags); /* share flags */
159 SIVAL(outbody.data, 0x08,
160 out_capabilities); /* capabilities */
161 SIVAL(outbody.data, 0x0C,
162 out_maximal_access); /* maximal access */
164 error = smbd_smb2_request_done(req, outbody, NULL);
165 if (!NT_STATUS_IS_OK(error)) {
166 smbd_server_connection_terminate(req->sconn,
167 nt_errstr(error));
168 return;
172 static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
173 const char *in_path,
174 uint8_t *out_share_type,
175 uint32_t *out_share_flags,
176 uint32_t *out_capabilities,
177 uint32_t *out_maximal_access,
178 uint32_t *out_tree_id)
180 const char *share = in_path;
181 char *service = NULL;
182 int snum = -1;
183 struct smbXsrv_tcon *tcon;
184 NTTIME now = timeval_to_nttime(&req->request_time);
185 connection_struct *compat_conn = NULL;
186 struct user_struct *compat_vuser = req->session->compat;
187 NTSTATUS status;
188 const char *share_name = NULL;
190 if (strncmp(share, "\\\\", 2) == 0) {
191 const char *p = strchr(share+2, '\\');
192 if (p) {
193 share = p + 1;
197 DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
198 in_path, share));
200 service = talloc_strdup(talloc_tos(), share);
201 if(!service) {
202 return NT_STATUS_NO_MEMORY;
205 strlower_m(service);
207 /* TODO: do more things... */
208 if (strequal(service,HOMES_NAME)) {
209 if (compat_vuser->homes_snum == -1) {
210 DEBUG(2, ("[homes] share not available for "
211 "user %s because it was not found "
212 "or created at session setup "
213 "time\n",
214 compat_vuser->session_info->unix_info->unix_name));
215 return NT_STATUS_BAD_NETWORK_NAME;
217 snum = compat_vuser->homes_snum;
218 } else if ((compat_vuser->homes_snum != -1)
219 && strequal(service,
220 lp_servicename(compat_vuser->homes_snum))) {
221 snum = compat_vuser->homes_snum;
222 } else {
223 snum = find_service(talloc_tos(), service, &service);
224 if (!service) {
225 return NT_STATUS_NO_MEMORY;
229 if (snum < 0) {
230 DEBUG(3,("smbd_smb2_tree_connect: couldn't find service %s\n",
231 service));
232 return NT_STATUS_BAD_NETWORK_NAME;
235 /* create a new tcon as child of the session */
236 status = smb2srv_tcon_create(req->session, now, &tcon);
237 if (!NT_STATUS_IS_OK(status)) {
238 return status;
241 compat_conn = make_connection_smb2(req->sconn,
242 tcon, snum,
243 req->session->compat,
244 "???",
245 &status);
246 if (compat_conn == NULL) {
247 TALLOC_FREE(tcon);
248 return status;
251 share_name = lp_servicename(SNUM(compat_conn));
252 tcon->global->share_name = talloc_strdup(tcon->global, share_name);
253 if (tcon->global->share_name == NULL) {
254 conn_free(compat_conn);
255 TALLOC_FREE(tcon);
256 return NT_STATUS_NO_MEMORY;
259 tcon->compat = talloc_move(tcon, &compat_conn);
261 tcon->status = NT_STATUS_OK;
263 status = smbXsrv_tcon_update(tcon);
264 if (!NT_STATUS_IS_OK(status)) {
265 TALLOC_FREE(tcon);
266 return status;
269 if (IS_PRINT(tcon->compat)) {
270 *out_share_type = SMB2_SHARE_TYPE_PRINT;
271 } else if (IS_IPC(tcon->compat)) {
272 *out_share_type = SMB2_SHARE_TYPE_PIPE;
273 } else {
274 *out_share_type = SMB2_SHARE_TYPE_DISK;
277 *out_share_flags = 0;
279 if (lp_msdfs_root(SNUM(tcon->compat)) && lp_host_msdfs()) {
280 *out_share_flags |= (SMB2_SHAREFLAG_DFS|SMB2_SHAREFLAG_DFS_ROOT);
281 *out_capabilities = SMB2_SHARE_CAP_DFS;
282 } else {
283 *out_capabilities = 0;
286 switch(lp_csc_policy(SNUM(tcon->compat))) {
287 case CSC_POLICY_MANUAL:
288 break;
289 case CSC_POLICY_DOCUMENTS:
290 *out_share_flags |= SMB2_SHAREFLAG_AUTO_CACHING;
291 break;
292 case CSC_POLICY_PROGRAMS:
293 *out_share_flags |= SMB2_SHAREFLAG_VDO_CACHING;
294 break;
295 case CSC_POLICY_DISABLE:
296 *out_share_flags |= SMB2_SHAREFLAG_NO_CACHING;
297 break;
298 default:
299 break;
302 if (lp_hideunreadable(SNUM(tcon->compat)) ||
303 lp_hideunwriteable_files(SNUM(tcon->compat))) {
304 *out_share_flags |= SMB2_SHAREFLAG_ACCESS_BASED_DIRECTORY_ENUM;
307 *out_maximal_access = tcon->compat->share_access;
309 *out_tree_id = tcon->global->tcon_wire_id;
310 return NT_STATUS_OK;
313 struct smbd_smb2_tree_connect_state {
314 const char *in_path;
315 uint8_t out_share_type;
316 uint32_t out_share_flags;
317 uint32_t out_capabilities;
318 uint32_t out_maximal_access;
319 uint32_t out_tree_id;
322 static struct tevent_req *smbd_smb2_tree_connect_send(TALLOC_CTX *mem_ctx,
323 struct tevent_context *ev,
324 struct smbd_smb2_request *smb2req,
325 const char *in_path)
327 struct tevent_req *req;
328 struct smbd_smb2_tree_connect_state *state;
329 NTSTATUS status;
331 req = tevent_req_create(mem_ctx, &state,
332 struct smbd_smb2_tree_connect_state);
333 if (req == NULL) {
334 return NULL;
336 state->in_path = in_path;
338 status = smbd_smb2_tree_connect(smb2req,
339 state->in_path,
340 &state->out_share_type,
341 &state->out_share_flags,
342 &state->out_capabilities,
343 &state->out_maximal_access,
344 &state->out_tree_id);
345 if (tevent_req_nterror(req, status)) {
346 return tevent_req_post(req, ev);
349 tevent_req_done(req);
350 return tevent_req_post(req, ev);
353 static NTSTATUS smbd_smb2_tree_connect_recv(struct tevent_req *req,
354 uint8_t *out_share_type,
355 uint32_t *out_share_flags,
356 uint32_t *out_capabilities,
357 uint32_t *out_maximal_access,
358 uint32_t *out_tree_id)
360 struct smbd_smb2_tree_connect_state *state =
361 tevent_req_data(req,
362 struct smbd_smb2_tree_connect_state);
363 NTSTATUS status;
365 if (tevent_req_is_nterror(req, &status)) {
366 tevent_req_received(req);
367 return status;
370 *out_share_type = state->out_share_type;
371 *out_share_flags = state->out_share_flags;
372 *out_capabilities = state->out_capabilities;
373 *out_maximal_access = state->out_maximal_access;
374 *out_tree_id = state->out_tree_id;
376 tevent_req_received(req);
377 return NT_STATUS_OK;
380 NTSTATUS smbd_smb2_request_process_tdis(struct smbd_smb2_request *req)
382 NTSTATUS status;
383 DATA_BLOB outbody;
385 status = smbd_smb2_request_verify_sizes(req, 0x04);
386 if (!NT_STATUS_IS_OK(status)) {
387 return smbd_smb2_request_error(req, status);
391 * TODO: cancel all outstanding requests on the tcon
393 status = smbXsrv_tcon_disconnect(req->tcon, req->tcon->compat->vuid);
394 if (!NT_STATUS_IS_OK(status)) {
395 DEBUG(0, ("smbd_smb2_request_process_tdis: "
396 "smbXsrv_tcon_disconnect() failed: %s\n",
397 nt_errstr(status)));
399 * If we hit this case, there is something completely
400 * wrong, so we better disconnect the transport connection.
402 return status;
405 TALLOC_FREE(req->tcon);
407 outbody = data_blob_talloc(req->out.vector, NULL, 0x04);
408 if (outbody.data == NULL) {
409 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
412 SSVAL(outbody.data, 0x00, 0x04); /* struct size */
413 SSVAL(outbody.data, 0x02, 0); /* reserved */
415 return smbd_smb2_request_done(req, outbody, NULL);