TODO destructors ... 2step:smb2 use smbXsrv_tcon: make use of smbXsrv_tcon smb2 remov...
[metze/samba/wip.git] / source3 / smbd / smb2_tcon.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6
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.
11
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.
16
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/>.
19 */
20
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"
29
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);
40
41 static void smbd_smb2_request_tcon_done(struct tevent_req *subreq);
42
43 NTSTATUS smbd_smb2_request_process_tcon(struct smbd_smb2_request *req)
44 {
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;
55
56         status = smbd_smb2_request_verify_sizes(req, 0x09);
57         if (!NT_STATUS_IS_OK(status)) {
58                 return smbd_smb2_request_error(req, status);
59         }
60         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
61
62         in_path_offset = SVAL(inbody, 0x04);
63         in_path_length = SVAL(inbody, 0x06);
64
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);
67         }
68
69         if (in_path_length > req->in.vector[i+2].iov_len) {
70                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
71         }
72
73         in_path_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
74         in_path_buffer.length = in_path_length;
75
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);
83         }
84
85         if (in_path_buffer.length == 0) {
86                 in_path_string_size = 0;
87         }
88
89         if (strlen(in_path_string) != in_path_string_size) {
90                 return smbd_smb2_request_error(req, NT_STATUS_BAD_NETWORK_NAME);
91         }
92
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);
99         }
100         tevent_req_set_callback(subreq, smbd_smb2_request_tcon_done, req);
101
102         return smbd_smb2_request_pending_queue(req, subreq, 500);
103 }
104
105 static void smbd_smb2_request_tcon_done(struct tevent_req *subreq)
106 {
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;
120
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;
134                 }
135                 return;
136         }
137
138         outhdr = (uint8_t *)req->out.vector[i].iov_base;
139
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;
147                 }
148                 return;
149         }
150
151         SIVAL(outhdr, SMB2_HDR_TID, out_tree_id);
152
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 */
163
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;
169         }
170 }
171
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)
179 {
180         const char *share = in_path;
181         char *service = NULL;
182         int snum = -1;
183         struct smbXsrv_tcon *tcon;
184         NTTIME now = 0;
185         connection_struct *compat_conn = NULL;
186         user_struct *compat_vuser = req->session->compat;
187         NTSTATUS status;
188
189         if (strncmp(share, "\\\\", 2) == 0) {
190                 const char *p = strchr(share+2, '\\');
191                 if (p) {
192                         share = p + 1;
193                 }
194         }
195
196         DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
197                   in_path, share));
198
199         service = talloc_strdup(talloc_tos(), share);
200         if(!service) {
201                 return NT_STATUS_NO_MEMORY;
202         }
203
204         strlower_m(service);
205
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;
215                 }
216                 snum = compat_vuser->homes_snum;
217         } else if ((compat_vuser->homes_snum != -1)
218                    && strequal(service,
219                         lp_servicename(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;
225                 }
226         }
227
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;
232         }
233
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;
238         }
239
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;
248         }
249         tcon->compat = talloc_move(tcon, &compat_conn);
250
251         tcon->status = NT_STATUS_OK;
252
253         status = smbXsrv_tcon_update(tcon);
254         if (!NT_STATUS_IS_OK(status)) {
255                 close_cnum(tcon->compat, tcon->compat->vuid);
256                 TALLOC_FREE(tcon);
257                 return status;
258         }
259
260         if (IS_PRINT(tcon->compat)) {
261                 *out_share_type = SMB2_SHARE_TYPE_PRINT;
262         } else if (IS_IPC(tcon->compat)) {
263                 *out_share_type = SMB2_SHARE_TYPE_PIPE;
264         } else {
265                 *out_share_type = SMB2_SHARE_TYPE_DISK;
266         }
267
268         *out_share_flags = 0;
269
270         if (lp_msdfs_root(SNUM(tcon->compat)) && lp_host_msdfs()) {
271                 *out_share_flags |= (SMB2_SHAREFLAG_DFS|SMB2_SHAREFLAG_DFS_ROOT);
272                 *out_capabilities = SMB2_SHARE_CAP_DFS;
273         } else {
274                 *out_capabilities = 0;
275         }
276
277         switch(lp_csc_policy(SNUM(tcon->compat))) {
278         case CSC_POLICY_MANUAL:
279                 break;
280         case CSC_POLICY_DOCUMENTS:
281                 *out_share_flags |= SMB2_SHAREFLAG_AUTO_CACHING;
282                 break;
283         case CSC_POLICY_PROGRAMS:
284                 *out_share_flags |= SMB2_SHAREFLAG_VDO_CACHING;
285                 break;
286         case CSC_POLICY_DISABLE:
287                 *out_share_flags |= SMB2_SHAREFLAG_NO_CACHING;
288                 break;
289         default:
290                 break;
291         }
292
293         if (lp_hideunreadable(SNUM(tcon->compat)) ||
294             lp_hideunwriteable_files(SNUM(tcon->compat))) {
295                 *out_share_flags |= SMB2_SHAREFLAG_ACCESS_BASED_DIRECTORY_ENUM;
296         }
297
298         *out_maximal_access = tcon->compat->share_access;
299
300         *out_tree_id = tcon->global->tcon_wire_id;
301         return NT_STATUS_OK;
302 }
303
304 struct smbd_smb2_tree_connect_state {
305         const char *in_path;
306         uint8_t out_share_type;
307         uint32_t out_share_flags;
308         uint32_t out_capabilities;
309         uint32_t out_maximal_access;
310         uint32_t out_tree_id;
311 };
312
313 static struct tevent_req *smbd_smb2_tree_connect_send(TALLOC_CTX *mem_ctx,
314                                         struct tevent_context *ev,
315                                         struct smbd_smb2_request *smb2req,
316                                         const char *in_path)
317 {
318         struct tevent_req *req;
319         struct smbd_smb2_tree_connect_state *state;
320         NTSTATUS status;
321
322         req = tevent_req_create(mem_ctx, &state,
323                                 struct smbd_smb2_tree_connect_state);
324         if (req == NULL) {
325                 return NULL;
326         }
327         state->in_path = in_path;
328
329         status = smbd_smb2_tree_connect(smb2req,
330                                         state->in_path,
331                                         &state->out_share_type,
332                                         &state->out_share_flags,
333                                         &state->out_capabilities,
334                                         &state->out_maximal_access,
335                                         &state->out_tree_id);
336         if (tevent_req_nterror(req, status)) {
337                 return tevent_req_post(req, ev);
338         }
339
340         tevent_req_done(req);
341         return tevent_req_post(req, ev);
342 }
343
344 static NTSTATUS smbd_smb2_tree_connect_recv(struct tevent_req *req,
345                                             uint8_t *out_share_type,
346                                             uint32_t *out_share_flags,
347                                             uint32_t *out_capabilities,
348                                             uint32_t *out_maximal_access,
349                                             uint32_t *out_tree_id)
350 {
351         struct smbd_smb2_tree_connect_state *state =
352                 tevent_req_data(req,
353                 struct smbd_smb2_tree_connect_state);
354         NTSTATUS status;
355
356         if (tevent_req_is_nterror(req, &status)) {
357                 tevent_req_received(req);
358                 return status;
359         }
360
361         *out_share_type = state->out_share_type;
362         *out_share_flags = state->out_share_flags;
363         *out_capabilities = state->out_capabilities;
364         *out_maximal_access = state->out_maximal_access;
365         *out_tree_id = state->out_tree_id;
366
367         tevent_req_received(req);
368         return NT_STATUS_OK;
369 }
370
371 NTSTATUS smbd_smb2_request_process_tdis(struct smbd_smb2_request *req)
372 {
373         NTSTATUS status;
374         DATA_BLOB outbody;
375         struct connection_struct *compat_conn = req->tcon->compat;
376
377         status = smbd_smb2_request_verify_sizes(req, 0x04);
378         if (!NT_STATUS_IS_OK(status)) {
379                 return smbd_smb2_request_error(req, status);
380         }
381
382         /*
383          * TODO: cancel all outstanding requests on the tcon
384          *       and delete all file handles.
385          */
386         set_current_service(compat_conn, 0, true);
387         close_cnum(compat_conn, compat_conn->vuid);
388         TALLOC_FREE(req->tcon);
389
390         outbody = data_blob_talloc(req->out.vector, NULL, 0x04);
391         if (outbody.data == NULL) {
392                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
393         }
394
395         SSVAL(outbody.data, 0x00, 0x04);        /* struct size */
396         SSVAL(outbody.data, 0x02, 0);           /* reserved */
397
398         return smbd_smb2_request_done(req, outbody, NULL);
399 }