s3: Convert aio_linux to pread/pwrite_send/recv
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_tcon.c
blob4549d3aa8481bb22d08a6a68add4080a45f41cb3
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;
189 if (strncmp(share, "\\\\", 2) == 0) {
190 const char *p = strchr(share+2, '\\');
191 if (p) {
192 share = p + 1;
196 DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
197 in_path, share));
199 service = talloc_strdup(talloc_tos(), share);
200 if(!service) {
201 return NT_STATUS_NO_MEMORY;
204 strlower_m(service);
206 /* TODO: do more things... */
207 if (strequal(service,HOMES_NAME)) {
208 if (compat_vuser->homes_snum == -1) {
209 DEBUG(2, ("[homes] share not available for "
210 "user %s because it was not found "
211 "or created at session setup "
212 "time\n",
213 compat_vuser->session_info->unix_info->unix_name));
214 return NT_STATUS_BAD_NETWORK_NAME;
216 snum = compat_vuser->homes_snum;
217 } else if ((compat_vuser->homes_snum != -1)
218 && strequal(service,
219 lp_servicename(talloc_tos(), compat_vuser->homes_snum))) {
220 snum = compat_vuser->homes_snum;
221 } else {
222 snum = find_service(talloc_tos(), service, &service);
223 if (!service) {
224 return NT_STATUS_NO_MEMORY;
228 if (snum < 0) {
229 DEBUG(3,("smbd_smb2_tree_connect: couldn't find service %s\n",
230 service));
231 return NT_STATUS_BAD_NETWORK_NAME;
234 /* create a new tcon as child of the session */
235 status = smb2srv_tcon_create(req->session, now, &tcon);
236 if (!NT_STATUS_IS_OK(status)) {
237 return status;
240 compat_conn = make_connection_smb2(req->sconn,
241 tcon, snum,
242 req->session->compat,
243 "???",
244 &status);
245 if (compat_conn == NULL) {
246 TALLOC_FREE(tcon);
247 return status;
250 tcon->global->share_name = lp_servicename(tcon->global,
251 SNUM(compat_conn));
252 if (tcon->global->share_name == NULL) {
253 conn_free(compat_conn);
254 TALLOC_FREE(tcon);
255 return NT_STATUS_NO_MEMORY;
258 tcon->compat = talloc_move(tcon, &compat_conn);
260 tcon->status = NT_STATUS_OK;
262 status = smbXsrv_tcon_update(tcon);
263 if (!NT_STATUS_IS_OK(status)) {
264 TALLOC_FREE(tcon);
265 return status;
268 if (IS_PRINT(tcon->compat)) {
269 *out_share_type = SMB2_SHARE_TYPE_PRINT;
270 } else if (IS_IPC(tcon->compat)) {
271 *out_share_type = SMB2_SHARE_TYPE_PIPE;
272 } else {
273 *out_share_type = SMB2_SHARE_TYPE_DISK;
276 *out_share_flags = 0;
278 if (lp_msdfs_root(SNUM(tcon->compat)) && lp_host_msdfs()) {
279 *out_share_flags |= (SMB2_SHAREFLAG_DFS|SMB2_SHAREFLAG_DFS_ROOT);
280 *out_capabilities = SMB2_SHARE_CAP_DFS;
281 } else {
282 *out_capabilities = 0;
285 switch(lp_csc_policy(SNUM(tcon->compat))) {
286 case CSC_POLICY_MANUAL:
287 break;
288 case CSC_POLICY_DOCUMENTS:
289 *out_share_flags |= SMB2_SHAREFLAG_AUTO_CACHING;
290 break;
291 case CSC_POLICY_PROGRAMS:
292 *out_share_flags |= SMB2_SHAREFLAG_VDO_CACHING;
293 break;
294 case CSC_POLICY_DISABLE:
295 *out_share_flags |= SMB2_SHAREFLAG_NO_CACHING;
296 break;
297 default:
298 break;
301 if (lp_hideunreadable(SNUM(tcon->compat)) ||
302 lp_hideunwriteable_files(SNUM(tcon->compat))) {
303 *out_share_flags |= SMB2_SHAREFLAG_ACCESS_BASED_DIRECTORY_ENUM;
306 *out_maximal_access = tcon->compat->share_access;
308 *out_tree_id = tcon->global->tcon_wire_id;
309 return NT_STATUS_OK;
312 struct smbd_smb2_tree_connect_state {
313 const char *in_path;
314 uint8_t out_share_type;
315 uint32_t out_share_flags;
316 uint32_t out_capabilities;
317 uint32_t out_maximal_access;
318 uint32_t out_tree_id;
321 static struct tevent_req *smbd_smb2_tree_connect_send(TALLOC_CTX *mem_ctx,
322 struct tevent_context *ev,
323 struct smbd_smb2_request *smb2req,
324 const char *in_path)
326 struct tevent_req *req;
327 struct smbd_smb2_tree_connect_state *state;
328 NTSTATUS status;
330 req = tevent_req_create(mem_ctx, &state,
331 struct smbd_smb2_tree_connect_state);
332 if (req == NULL) {
333 return NULL;
335 state->in_path = in_path;
337 status = smbd_smb2_tree_connect(smb2req,
338 state->in_path,
339 &state->out_share_type,
340 &state->out_share_flags,
341 &state->out_capabilities,
342 &state->out_maximal_access,
343 &state->out_tree_id);
344 if (tevent_req_nterror(req, status)) {
345 return tevent_req_post(req, ev);
348 tevent_req_done(req);
349 return tevent_req_post(req, ev);
352 static NTSTATUS smbd_smb2_tree_connect_recv(struct tevent_req *req,
353 uint8_t *out_share_type,
354 uint32_t *out_share_flags,
355 uint32_t *out_capabilities,
356 uint32_t *out_maximal_access,
357 uint32_t *out_tree_id)
359 struct smbd_smb2_tree_connect_state *state =
360 tevent_req_data(req,
361 struct smbd_smb2_tree_connect_state);
362 NTSTATUS status;
364 if (tevent_req_is_nterror(req, &status)) {
365 tevent_req_received(req);
366 return status;
369 *out_share_type = state->out_share_type;
370 *out_share_flags = state->out_share_flags;
371 *out_capabilities = state->out_capabilities;
372 *out_maximal_access = state->out_maximal_access;
373 *out_tree_id = state->out_tree_id;
375 tevent_req_received(req);
376 return NT_STATUS_OK;
379 NTSTATUS smbd_smb2_request_process_tdis(struct smbd_smb2_request *req)
381 NTSTATUS status;
382 DATA_BLOB outbody;
384 status = smbd_smb2_request_verify_sizes(req, 0x04);
385 if (!NT_STATUS_IS_OK(status)) {
386 return smbd_smb2_request_error(req, status);
390 * TODO: cancel all outstanding requests on the tcon
392 status = smbXsrv_tcon_disconnect(req->tcon, req->tcon->compat->vuid);
393 if (!NT_STATUS_IS_OK(status)) {
394 DEBUG(0, ("smbd_smb2_request_process_tdis: "
395 "smbXsrv_tcon_disconnect() failed: %s\n",
396 nt_errstr(status)));
398 * If we hit this case, there is something completely
399 * wrong, so we better disconnect the transport connection.
401 return status;
404 TALLOC_FREE(req->tcon);
406 outbody = data_blob_talloc(req->out.vector, NULL, 0x04);
407 if (outbody.data == NULL) {
408 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
411 SSVAL(outbody.data, 0x00, 0x04); /* struct size */
412 SSVAL(outbody.data, 0x02, 0); /* reserved */
414 return smbd_smb2_request_done(req, outbody, NULL);