s3:smbd: use 'struct user_struct' instead of typedef'ed 'user_struct'
[Samba/gebeck_regimport.git] / source3 / smbd / smb2_tcon.c
blob14294aefaf44a8e8c8a9a0bbc7424ef89832ea65
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 int smbd_smb2_tcon_destructor(struct smbd_smb2_tcon *tcon)
174 if (tcon->session == NULL) {
175 return 0;
178 idr_remove(tcon->session->tcons.idtree, tcon->tid);
179 DLIST_REMOVE(tcon->session->tcons.list, tcon);
181 if (tcon->compat_conn) {
182 set_current_service(tcon->compat_conn, 0, true);
183 close_cnum(tcon->compat_conn, tcon->session->vuid);
186 tcon->compat_conn = NULL;
187 tcon->tid = 0;
188 tcon->session = NULL;
190 return 0;
193 static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
194 const char *in_path,
195 uint8_t *out_share_type,
196 uint32_t *out_share_flags,
197 uint32_t *out_capabilities,
198 uint32_t *out_maximal_access,
199 uint32_t *out_tree_id)
201 const char *share = in_path;
202 char *service = NULL;
203 int snum = -1;
204 struct smbd_smb2_tcon *tcon;
205 connection_struct *compat_conn = NULL;
206 struct user_struct *compat_vuser = req->session->compat_vuser;
207 int id;
208 NTSTATUS status;
210 if (strncmp(share, "\\\\", 2) == 0) {
211 const char *p = strchr(share+2, '\\');
212 if (p) {
213 share = p + 1;
217 DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
218 in_path, share));
220 service = talloc_strdup(talloc_tos(), share);
221 if(!service) {
222 return NT_STATUS_NO_MEMORY;
225 strlower_m(service);
227 /* TODO: do more things... */
228 if (strequal(service,HOMES_NAME)) {
229 if (compat_vuser->homes_snum == -1) {
230 DEBUG(2, ("[homes] share not available for "
231 "user %s because it was not found "
232 "or created at session setup "
233 "time\n",
234 compat_vuser->session_info->unix_info->unix_name));
235 return NT_STATUS_BAD_NETWORK_NAME;
237 snum = compat_vuser->homes_snum;
238 } else if ((compat_vuser->homes_snum != -1)
239 && strequal(service,
240 lp_servicename(compat_vuser->homes_snum))) {
241 snum = compat_vuser->homes_snum;
242 } else {
243 snum = find_service(talloc_tos(), service, &service);
244 if (!service) {
245 return NT_STATUS_NO_MEMORY;
249 if (snum < 0) {
250 DEBUG(3,("smbd_smb2_tree_connect: couldn't find service %s\n",
251 service));
252 return NT_STATUS_BAD_NETWORK_NAME;
255 /* create a new tcon as child of the session */
256 tcon = talloc_zero(req->session, struct smbd_smb2_tcon);
257 if (tcon == NULL) {
258 return NT_STATUS_NO_MEMORY;
260 id = idr_get_new_random(req->session->tcons.idtree,
261 tcon,
262 req->session->tcons.limit);
263 if (id == -1) {
264 TALLOC_FREE(tcon);
265 return NT_STATUS_INSUFFICIENT_RESOURCES;
267 tcon->tid = id;
268 tcon->snum = snum;
270 DLIST_ADD_END(req->session->tcons.list, tcon,
271 struct smbd_smb2_tcon *);
272 tcon->session = req->session;
273 talloc_set_destructor(tcon, smbd_smb2_tcon_destructor);
275 compat_conn = make_connection_smb2(req->sconn,
276 tcon,
277 req->session->compat_vuser,
278 "???",
279 &status);
280 if (compat_conn == NULL) {
281 TALLOC_FREE(tcon);
282 return status;
284 tcon->compat_conn = talloc_move(tcon, &compat_conn);
286 if (IS_PRINT(tcon->compat_conn)) {
287 *out_share_type = SMB2_SHARE_TYPE_PRINT;
288 } else if (IS_IPC(tcon->compat_conn)) {
289 *out_share_type = SMB2_SHARE_TYPE_PIPE;
290 } else {
291 *out_share_type = SMB2_SHARE_TYPE_DISK;
294 *out_share_flags = 0;
296 if (lp_msdfs_root(SNUM(tcon->compat_conn)) && lp_host_msdfs()) {
297 *out_share_flags |= (SMB2_SHAREFLAG_DFS|SMB2_SHAREFLAG_DFS_ROOT);
298 *out_capabilities = SMB2_SHARE_CAP_DFS;
299 } else {
300 *out_capabilities = 0;
303 switch(lp_csc_policy(SNUM(tcon->compat_conn))) {
304 case CSC_POLICY_MANUAL:
305 break;
306 case CSC_POLICY_DOCUMENTS:
307 *out_share_flags |= SMB2_SHAREFLAG_AUTO_CACHING;
308 break;
309 case CSC_POLICY_PROGRAMS:
310 *out_share_flags |= SMB2_SHAREFLAG_VDO_CACHING;
311 break;
312 case CSC_POLICY_DISABLE:
313 *out_share_flags |= SMB2_SHAREFLAG_NO_CACHING;
314 break;
315 default:
316 break;
319 if (lp_hideunreadable(SNUM(tcon->compat_conn)) ||
320 lp_hideunwriteable_files(SNUM(tcon->compat_conn))) {
321 *out_share_flags |= SMB2_SHAREFLAG_ACCESS_BASED_DIRECTORY_ENUM;
324 *out_maximal_access = tcon->compat_conn->share_access;
326 *out_tree_id = tcon->tid;
327 return NT_STATUS_OK;
330 struct smbd_smb2_tree_connect_state {
331 const char *in_path;
332 uint8_t out_share_type;
333 uint32_t out_share_flags;
334 uint32_t out_capabilities;
335 uint32_t out_maximal_access;
336 uint32_t out_tree_id;
339 static struct tevent_req *smbd_smb2_tree_connect_send(TALLOC_CTX *mem_ctx,
340 struct tevent_context *ev,
341 struct smbd_smb2_request *smb2req,
342 const char *in_path)
344 struct tevent_req *req;
345 struct smbd_smb2_tree_connect_state *state;
346 NTSTATUS status;
348 req = tevent_req_create(mem_ctx, &state,
349 struct smbd_smb2_tree_connect_state);
350 if (req == NULL) {
351 return NULL;
353 state->in_path = in_path;
355 status = smbd_smb2_tree_connect(smb2req,
356 state->in_path,
357 &state->out_share_type,
358 &state->out_share_flags,
359 &state->out_capabilities,
360 &state->out_maximal_access,
361 &state->out_tree_id);
362 if (tevent_req_nterror(req, status)) {
363 return tevent_req_post(req, ev);
366 tevent_req_done(req);
367 return tevent_req_post(req, ev);
370 static NTSTATUS smbd_smb2_tree_connect_recv(struct tevent_req *req,
371 uint8_t *out_share_type,
372 uint32_t *out_share_flags,
373 uint32_t *out_capabilities,
374 uint32_t *out_maximal_access,
375 uint32_t *out_tree_id)
377 struct smbd_smb2_tree_connect_state *state =
378 tevent_req_data(req,
379 struct smbd_smb2_tree_connect_state);
380 NTSTATUS status;
382 if (tevent_req_is_nterror(req, &status)) {
383 tevent_req_received(req);
384 return status;
387 *out_share_type = state->out_share_type;
388 *out_share_flags = state->out_share_flags;
389 *out_capabilities = state->out_capabilities;
390 *out_maximal_access = state->out_maximal_access;
391 *out_tree_id = state->out_tree_id;
393 tevent_req_received(req);
394 return NT_STATUS_OK;
397 NTSTATUS smbd_smb2_request_process_tdis(struct smbd_smb2_request *req)
399 NTSTATUS status;
400 DATA_BLOB outbody;
402 status = smbd_smb2_request_verify_sizes(req, 0x04);
403 if (!NT_STATUS_IS_OK(status)) {
404 return smbd_smb2_request_error(req, status);
408 * TODO: cancel all outstanding requests on the tcon
409 * and delete all file handles.
411 TALLOC_FREE(req->tcon);
413 outbody = data_blob_talloc(req->out.vector, NULL, 0x04);
414 if (outbody.data == NULL) {
415 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
418 SSVAL(outbody.data, 0x00, 0x04); /* struct size */
419 SSVAL(outbody.data, 0x02, 0); /* reserved */
421 return smbd_smb2_request_done(req, outbody, NULL);