0f765d662a79dbdba22ca4ab617a81d82e751385
[samba.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/globals.h"
23 #include "../libcli/smb/smb_common.h"
24
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);
32
33 NTSTATUS smbd_smb2_request_process_tcon(struct smbd_smb2_request *req)
34 {
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;
53
54         if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
55                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
56         }
57
58         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
59
60         body_size = SVAL(inbody, 0x00);
61         if (body_size != expected_body_size) {
62                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
63         }
64
65         in_path_offset = SVAL(inbody, 0x04);
66         in_path_length = SVAL(inbody, 0x06);
67
68         if (in_path_offset != (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
69                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
70         }
71
72         if (in_path_length > req->in.vector[i+2].iov_len) {
73                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
74         }
75
76         in_path_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
77         in_path_buffer.length = in_path_length;
78
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);
86         }
87
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);
96         }
97
98         outhdr = (uint8_t *)req->out.vector[i].iov_base;
99
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);
103         }
104
105         SIVAL(outhdr, SMB2_HDR_TID, out_tree_id);
106
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 */
117
118         return smbd_smb2_request_done(req, outbody, NULL);
119 }
120
121 static int smbd_smb2_tcon_destructor(struct smbd_smb2_tcon *tcon)
122 {
123         if (tcon->session == NULL) {
124                 return 0;
125         }
126
127         idr_remove(tcon->session->tcons.idtree, tcon->tid);
128         DLIST_REMOVE(tcon->session->tcons.list, tcon);
129
130         if (tcon->compat_conn) {
131                 close_cnum(tcon->compat_conn, tcon->session->vuid);
132         }
133
134         tcon->compat_conn = NULL;
135         tcon->tid = 0;
136         tcon->session = NULL;
137
138         return 0;
139 }
140
141 static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
142                                        const char *in_path,
143                                        uint8_t *out_share_type,
144                                        uint32_t *out_share_flags,
145                                        uint32_t *out_capabilities,
146                                        uint32_t *out_maximal_access,
147                                        uint32_t *out_tree_id)
148 {
149         const char *share = in_path;
150         fstring service;
151         int snum = -1;
152         struct smbd_smb2_tcon *tcon;
153         connection_struct *compat_conn = NULL;
154         int id;
155         NTSTATUS status;
156
157         if (strncmp(share, "\\\\", 2) == 0) {
158                 const char *p = strchr(share+2, '\\');
159                 if (p) {
160                         share = p + 1;
161                 }
162         }
163
164         DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
165                   in_path, share));
166
167         fstrcpy(service, share);
168
169         strlower_m(service);
170
171         snum = find_service(service);
172         if (snum < 0) {
173                 DEBUG(3,("smbd_smb2_tree_connect: couldn't find service %s\n",
174                          service));
175                 return NT_STATUS_BAD_NETWORK_NAME;
176         }
177
178         /* TODO: do more things... */
179
180         /* create a new tcon as child of the session */
181         tcon = talloc_zero(req->session, struct smbd_smb2_tcon);
182         if (tcon == NULL) {
183                 return NT_STATUS_NO_MEMORY;
184         }
185         id = idr_get_new_random(req->session->tcons.idtree,
186                                 tcon,
187                                 req->session->tcons.limit);
188         if (id == -1) {
189                 TALLOC_FREE(tcon);
190                 return NT_STATUS_INSUFFICIENT_RESOURCES;
191         }
192         tcon->tid = id;
193         tcon->snum = snum;
194
195         DLIST_ADD_END(req->session->tcons.list, tcon,
196                       struct smbd_smb2_tcon *);
197         tcon->session = req->session;
198         talloc_set_destructor(tcon, smbd_smb2_tcon_destructor);
199
200         compat_conn = make_connection_snum(req->sconn,
201                                         snum, req->session->compat_vuser,
202                                         data_blob_null, "???",
203                                         &status);
204         if (compat_conn == NULL) {
205                 TALLOC_FREE(tcon);
206                 return status;
207         }
208         tcon->compat_conn = talloc_move(tcon, &compat_conn);
209         tcon->compat_conn->cnum = tcon->tid;
210
211         if (IS_PRINT(tcon->compat_conn)) {
212                 *out_share_type = 0x03;
213         } else if (IS_IPC(tcon->compat_conn)) {
214                 *out_share_type = 0x02;
215         } else {
216                 *out_share_type = 0x01;
217         }
218         *out_share_flags = SMB2_SHAREFLAG_ALL;
219         *out_capabilities = 0;
220         *out_maximal_access = FILE_GENERIC_ALL;
221
222         *out_tree_id = tcon->tid;
223         return NT_STATUS_OK;
224 }
225
226 NTSTATUS smbd_smb2_request_check_tcon(struct smbd_smb2_request *req)
227 {
228         const uint8_t *inhdr;
229         const uint8_t *outhdr;
230         int i = req->current_idx;
231         uint32_t in_tid;
232         void *p;
233         struct smbd_smb2_tcon *tcon;
234         bool chained_fixup = false;
235
236         inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
237
238         in_tid = IVAL(inhdr, SMB2_HDR_TID);
239
240         if (in_tid == (0xFFFFFFFF)) {
241                 if (req->async) {
242                         /*
243                          * async request - fill in tid from
244                          * already setup out.vector[].iov_base.
245                          */
246                         outhdr = (const uint8_t *)req->out.vector[i].iov_base;
247                         in_tid = IVAL(outhdr, SMB2_HDR_TID);
248                 } else if (i > 2) {
249                         /*
250                          * Chained request - fill in tid from
251                          * the previous request out.vector[].iov_base.
252                          */
253                         outhdr = (const uint8_t *)req->out.vector[i-3].iov_base;
254                         in_tid = IVAL(outhdr, SMB2_HDR_TID);
255                         chained_fixup = true;
256                 }
257         }
258
259         /* lookup an existing session */
260         p = idr_find(req->session->tcons.idtree, in_tid);
261         if (p == NULL) {
262                 return NT_STATUS_NETWORK_NAME_DELETED;
263         }
264         tcon = talloc_get_type_abort(p, struct smbd_smb2_tcon);
265
266         if (!change_to_user(tcon->compat_conn,req->session->vuid)) {
267                 return NT_STATUS_ACCESS_DENIED;
268         }
269
270         /* should we pass FLAG_CASELESS_PATHNAMES here? */
271         if (!set_current_service(tcon->compat_conn, 0, true)) {
272                 return NT_STATUS_ACCESS_DENIED;
273         }
274
275         req->tcon = tcon;
276
277         if (chained_fixup) {
278                 /* Fix up our own outhdr. */
279                 outhdr = (const uint8_t *)req->out.vector[i].iov_base;
280                 SIVAL(outhdr, SMB2_HDR_TID, in_tid);
281         }
282
283         return NT_STATUS_OK;
284 }
285
286 NTSTATUS smbd_smb2_request_process_tdis(struct smbd_smb2_request *req)
287 {
288         const uint8_t *inbody;
289         int i = req->current_idx;
290         DATA_BLOB outbody;
291         size_t expected_body_size = 0x04;
292         size_t body_size;
293
294         if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
295                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
296         }
297
298         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
299
300         body_size = SVAL(inbody, 0x00);
301         if (body_size != expected_body_size) {
302                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
303         }
304
305         /*
306          * TODO: cancel all outstanding requests on the tcon
307          *       and delete all file handles.
308          */
309         TALLOC_FREE(req->tcon);
310
311         outbody = data_blob_talloc(req->out.vector, NULL, 0x04);
312         if (outbody.data == NULL) {
313                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
314         }
315
316         SSVAL(outbody.data, 0x00, 0x04);        /* struct size */
317         SSVAL(outbody.data, 0x02, 0);           /* reserved */
318
319         return smbd_smb2_request_done(req, outbody, NULL);
320 }