Fix bug 7781 - Samba transforms ShareName to lowercase (sharename) when adding new...
[kai/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 #include "../libcli/security/security.h"
25
26 static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
27                                        const char *in_path,
28                                        uint8_t *out_share_type,
29                                        uint32_t *out_share_flags,
30                                        uint32_t *out_capabilities,
31                                        uint32_t *out_maximal_access,
32                                        uint32_t *out_tree_id);
33
34 NTSTATUS smbd_smb2_request_process_tcon(struct smbd_smb2_request *req)
35 {
36         const uint8_t *inbody;
37         int i = req->current_idx;
38         uint8_t *outhdr;
39         DATA_BLOB outbody;
40         size_t expected_body_size = 0x09;
41         size_t body_size;
42         uint16_t in_path_offset;
43         uint16_t in_path_length;
44         DATA_BLOB in_path_buffer;
45         char *in_path_string;
46         size_t in_path_string_size;
47         uint8_t out_share_type = 0;
48         uint32_t out_share_flags = 0;
49         uint32_t out_capabilities = 0;
50         uint32_t out_maximal_access = 0;
51         uint32_t out_tree_id = 0;
52         NTSTATUS status;
53         bool ok;
54
55         if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
56                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
57         }
58
59         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
60
61         body_size = SVAL(inbody, 0x00);
62         if (body_size != expected_body_size) {
63                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
64         }
65
66         in_path_offset = SVAL(inbody, 0x04);
67         in_path_length = SVAL(inbody, 0x06);
68
69         if (in_path_offset != (SMB2_HDR_BODY + (body_size & 0xFFFFFFFE))) {
70                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
71         }
72
73         if (in_path_length > req->in.vector[i+2].iov_len) {
74                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
75         }
76
77         in_path_buffer.data = (uint8_t *)req->in.vector[i+2].iov_base;
78         in_path_buffer.length = in_path_length;
79
80         ok = convert_string_talloc(req, CH_UTF16, CH_UNIX,
81                                    in_path_buffer.data,
82                                    in_path_buffer.length,
83                                    &in_path_string,
84                                    &in_path_string_size, false);
85         if (!ok) {
86                 return smbd_smb2_request_error(req, NT_STATUS_ILLEGAL_CHARACTER);
87         }
88
89         status = smbd_smb2_tree_connect(req, in_path_string,
90                                         &out_share_type,
91                                         &out_share_flags,
92                                         &out_capabilities,
93                                         &out_maximal_access,
94                                         &out_tree_id);
95         if (!NT_STATUS_IS_OK(status)) {
96                 return smbd_smb2_request_error(req, status);
97         }
98
99         outhdr = (uint8_t *)req->out.vector[i].iov_base;
100
101         outbody = data_blob_talloc(req->out.vector, NULL, 0x10);
102         if (outbody.data == NULL) {
103                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
104         }
105
106         SIVAL(outhdr, SMB2_HDR_TID, out_tree_id);
107
108         SSVAL(outbody.data, 0x00, 0x10);        /* struct size */
109         SCVAL(outbody.data, 0x02,
110               out_share_type);                  /* share type */
111         SCVAL(outbody.data, 0x03, 0);           /* reserved */
112         SIVAL(outbody.data, 0x04,
113               out_share_flags);                 /* share flags */
114         SIVAL(outbody.data, 0x08,
115               out_capabilities);                /* capabilities */
116         SIVAL(outbody.data, 0x0C,
117               out_maximal_access);              /* maximal access */
118
119         return smbd_smb2_request_done(req, outbody, NULL);
120 }
121
122 static int smbd_smb2_tcon_destructor(struct smbd_smb2_tcon *tcon)
123 {
124         if (tcon->session == NULL) {
125                 return 0;
126         }
127
128         idr_remove(tcon->session->tcons.idtree, tcon->tid);
129         DLIST_REMOVE(tcon->session->tcons.list, tcon);
130         SMB_ASSERT(tcon->session->sconn->num_tcons_open > 0);
131         tcon->session->sconn->num_tcons_open--;
132
133         if (tcon->compat_conn) {
134                 set_current_service(tcon->compat_conn, 0, true);
135                 close_cnum(tcon->compat_conn, tcon->session->vuid);
136         }
137
138         tcon->compat_conn = NULL;
139         tcon->tid = 0;
140         tcon->session = NULL;
141
142         return 0;
143 }
144
145 static NTSTATUS smbd_smb2_tree_connect(struct smbd_smb2_request *req,
146                                        const char *in_path,
147                                        uint8_t *out_share_type,
148                                        uint32_t *out_share_flags,
149                                        uint32_t *out_capabilities,
150                                        uint32_t *out_maximal_access,
151                                        uint32_t *out_tree_id)
152 {
153         const char *share = in_path;
154         char *service = NULL;
155         int snum = -1;
156         struct smbd_smb2_tcon *tcon;
157         connection_struct *compat_conn = NULL;
158         user_struct *compat_vuser = req->session->compat_vuser;
159         int id;
160         NTSTATUS status;
161
162         if (strncmp(share, "\\\\", 2) == 0) {
163                 const char *p = strchr(share+2, '\\');
164                 if (p) {
165                         share = p + 1;
166                 }
167         }
168
169         DEBUG(10,("smbd_smb2_tree_connect: path[%s] share[%s]\n",
170                   in_path, share));
171
172         service = talloc_strdup(talloc_tos(), share);
173         if(!service) {
174                 return NT_STATUS_NO_MEMORY;
175         }
176
177         strlower_m(service);
178
179         /* TODO: do more things... */
180         if (strequal(service,HOMES_NAME)) {
181                 if (compat_vuser->homes_snum == -1) {
182                         DEBUG(2, ("[homes] share not available for "
183                                 "user %s because it was not found "
184                                 "or created at session setup "
185                                 "time\n",
186                                 compat_vuser->server_info->unix_name));
187                         return NT_STATUS_BAD_NETWORK_NAME;
188                 }
189                 snum = compat_vuser->homes_snum;
190         } else if ((compat_vuser->homes_snum != -1)
191                    && strequal(service,
192                         lp_servicename(compat_vuser->homes_snum))) {
193                 snum = compat_vuser->homes_snum;
194         } else {
195                 snum = find_service(talloc_tos(), service, &service);
196                 if (!service) {
197                         return NT_STATUS_NO_MEMORY;
198                 }
199         }
200
201         if (snum < 0) {
202                 DEBUG(3,("smbd_smb2_tree_connect: couldn't find service %s\n",
203                          service));
204                 return NT_STATUS_BAD_NETWORK_NAME;
205         }
206
207         /* create a new tcon as child of the session */
208         tcon = talloc_zero(req->session, struct smbd_smb2_tcon);
209         if (tcon == NULL) {
210                 return NT_STATUS_NO_MEMORY;
211         }
212         id = idr_get_new_random(req->session->tcons.idtree,
213                                 tcon,
214                                 req->session->tcons.limit);
215         if (id == -1) {
216                 TALLOC_FREE(tcon);
217                 return NT_STATUS_INSUFFICIENT_RESOURCES;
218         }
219         tcon->tid = id;
220         tcon->snum = snum;
221
222         DLIST_ADD_END(req->session->tcons.list, tcon,
223                       struct smbd_smb2_tcon *);
224         tcon->session = req->session;
225         tcon->session->sconn->num_tcons_open++;
226         talloc_set_destructor(tcon, smbd_smb2_tcon_destructor);
227
228         compat_conn = make_connection_snum(req->sconn,
229                                         snum, req->session->compat_vuser,
230                                         data_blob_null, "???",
231                                         &status);
232         if (compat_conn == NULL) {
233                 TALLOC_FREE(tcon);
234                 return status;
235         }
236         tcon->compat_conn = talloc_move(tcon, &compat_conn);
237         tcon->compat_conn->cnum = tcon->tid;
238
239         if (IS_PRINT(tcon->compat_conn)) {
240                 *out_share_type = SMB2_SHARE_TYPE_PRINT;
241         } else if (IS_IPC(tcon->compat_conn)) {
242                 *out_share_type = SMB2_SHARE_TYPE_PIPE;
243         } else {
244                 *out_share_type = SMB2_SHARE_TYPE_DISK;
245         }
246
247         *out_share_flags = SMB2_SHAREFLAG_ALLOW_NAMESPACE_CACHING;
248
249         if (lp_msdfs_root(SNUM(tcon->compat_conn)) && lp_host_msdfs()) {
250                 *out_share_flags |= (SMB2_SHAREFLAG_DFS|SMB2_SHAREFLAG_DFS_ROOT);
251                 *out_capabilities = SMB2_SHARE_CAP_DFS;
252         } else {
253                 *out_capabilities = 0;
254         }
255
256         switch(lp_csc_policy(SNUM(tcon->compat_conn))) {
257         case CSC_POLICY_MANUAL:
258                 break;
259         case CSC_POLICY_DOCUMENTS:
260                 *out_share_flags |= SMB2_SHAREFLAG_AUTO_CACHING;
261                 break;
262         case CSC_POLICY_PROGRAMS:
263                 *out_share_flags |= SMB2_SHAREFLAG_VDO_CACHING;
264                 break;
265         case CSC_POLICY_DISABLE:
266                 *out_share_flags |= SMB2_SHAREFLAG_NO_CACHING;
267                 break;
268         default:
269                 break;
270         }
271
272         *out_maximal_access = FILE_GENERIC_ALL;
273
274         *out_tree_id = tcon->tid;
275         return NT_STATUS_OK;
276 }
277
278 NTSTATUS smbd_smb2_request_check_tcon(struct smbd_smb2_request *req)
279 {
280         const uint8_t *inhdr;
281         const uint8_t *outhdr;
282         int i = req->current_idx;
283         uint32_t in_tid;
284         void *p;
285         struct smbd_smb2_tcon *tcon;
286         bool chained_fixup = false;
287
288         inhdr = (const uint8_t *)req->in.vector[i+0].iov_base;
289
290         in_tid = IVAL(inhdr, SMB2_HDR_TID);
291
292         if (in_tid == (0xFFFFFFFF)) {
293                 if (req->async) {
294                         /*
295                          * async request - fill in tid from
296                          * already setup out.vector[].iov_base.
297                          */
298                         outhdr = (const uint8_t *)req->out.vector[i].iov_base;
299                         in_tid = IVAL(outhdr, SMB2_HDR_TID);
300                 } else if (i > 2) {
301                         /*
302                          * Chained request - fill in tid from
303                          * the previous request out.vector[].iov_base.
304                          */
305                         outhdr = (const uint8_t *)req->out.vector[i-3].iov_base;
306                         in_tid = IVAL(outhdr, SMB2_HDR_TID);
307                         chained_fixup = true;
308                 }
309         }
310
311         /* lookup an existing session */
312         p = idr_find(req->session->tcons.idtree, in_tid);
313         if (p == NULL) {
314                 return NT_STATUS_NETWORK_NAME_DELETED;
315         }
316         tcon = talloc_get_type_abort(p, struct smbd_smb2_tcon);
317
318         if (!change_to_user(tcon->compat_conn,req->session->vuid)) {
319                 return NT_STATUS_ACCESS_DENIED;
320         }
321
322         /* should we pass FLAG_CASELESS_PATHNAMES here? */
323         if (!set_current_service(tcon->compat_conn, 0, true)) {
324                 return NT_STATUS_ACCESS_DENIED;
325         }
326
327         req->tcon = tcon;
328
329         if (chained_fixup) {
330                 /* Fix up our own outhdr. */
331                 outhdr = (const uint8_t *)req->out.vector[i].iov_base;
332                 SIVAL(outhdr, SMB2_HDR_TID, in_tid);
333         }
334
335         return NT_STATUS_OK;
336 }
337
338 NTSTATUS smbd_smb2_request_process_tdis(struct smbd_smb2_request *req)
339 {
340         const uint8_t *inbody;
341         int i = req->current_idx;
342         DATA_BLOB outbody;
343         size_t expected_body_size = 0x04;
344         size_t body_size;
345
346         if (req->in.vector[i+1].iov_len != (expected_body_size & 0xFFFFFFFE)) {
347                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
348         }
349
350         inbody = (const uint8_t *)req->in.vector[i+1].iov_base;
351
352         body_size = SVAL(inbody, 0x00);
353         if (body_size != expected_body_size) {
354                 return smbd_smb2_request_error(req, NT_STATUS_INVALID_PARAMETER);
355         }
356
357         /*
358          * TODO: cancel all outstanding requests on the tcon
359          *       and delete all file handles.
360          */
361         TALLOC_FREE(req->tcon);
362
363         outbody = data_blob_talloc(req->out.vector, NULL, 0x04);
364         if (outbody.data == NULL) {
365                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
366         }
367
368         SSVAL(outbody.data, 0x00, 0x04);        /* struct size */
369         SSVAL(outbody.data, 0x02, 0);           /* reserved */
370
371         return smbd_smb2_request_done(req, outbody, NULL);
372 }