Add two flags to allow for handling of Extended Signatures (Session Key Protection...
[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/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 = timeval_to_nttime(&req->request_time);
185         connection_struct *compat_conn = NULL;
186         struct 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(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;
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         if (lp_smb_encrypt(snum) == SMB_SIGNING_REQUIRED) {
235                 status = NT_STATUS_ACCESS_DENIED;
236                 DEBUG(3,("smbd_smb2_tree_connect: "
237                          "service %s needs encryption - %s\n",
238                          service, nt_errstr(status)));
239                 return status;
240         }
241
242         /* create a new tcon as child of the session */
243         status = smb2srv_tcon_create(req->session, now, &tcon);
244         if (!NT_STATUS_IS_OK(status)) {
245                 return status;
246         }
247
248         compat_conn = make_connection_smb2(req->sconn,
249                                         tcon, snum,
250                                         req->session->compat,
251                                         "???",
252                                         &status);
253         if (compat_conn == NULL) {
254                 TALLOC_FREE(tcon);
255                 return status;
256         }
257
258         tcon->global->share_name = lp_servicename(tcon->global,
259                                                   SNUM(compat_conn));
260         if (tcon->global->share_name == NULL) {
261                 conn_free(compat_conn);
262                 TALLOC_FREE(tcon);
263                 return NT_STATUS_NO_MEMORY;
264         }
265
266         tcon->compat = talloc_move(tcon, &compat_conn);
267
268         tcon->status = NT_STATUS_OK;
269
270         status = smbXsrv_tcon_update(tcon);
271         if (!NT_STATUS_IS_OK(status)) {
272                 TALLOC_FREE(tcon);
273                 return status;
274         }
275
276         if (IS_PRINT(tcon->compat)) {
277                 *out_share_type = SMB2_SHARE_TYPE_PRINT;
278         } else if (IS_IPC(tcon->compat)) {
279                 *out_share_type = SMB2_SHARE_TYPE_PIPE;
280         } else {
281                 *out_share_type = SMB2_SHARE_TYPE_DISK;
282         }
283
284         *out_share_flags = 0;
285
286         if (lp_msdfs_root(SNUM(tcon->compat)) && lp_host_msdfs()) {
287                 *out_share_flags |= (SMB2_SHAREFLAG_DFS|SMB2_SHAREFLAG_DFS_ROOT);
288                 *out_capabilities = SMB2_SHARE_CAP_DFS;
289         } else {
290                 *out_capabilities = 0;
291         }
292
293         switch(lp_csc_policy(SNUM(tcon->compat))) {
294         case CSC_POLICY_MANUAL:
295                 break;
296         case CSC_POLICY_DOCUMENTS:
297                 *out_share_flags |= SMB2_SHAREFLAG_AUTO_CACHING;
298                 break;
299         case CSC_POLICY_PROGRAMS:
300                 *out_share_flags |= SMB2_SHAREFLAG_VDO_CACHING;
301                 break;
302         case CSC_POLICY_DISABLE:
303                 *out_share_flags |= SMB2_SHAREFLAG_NO_CACHING;
304                 break;
305         default:
306                 break;
307         }
308
309         if (lp_hideunreadable(SNUM(tcon->compat)) ||
310             lp_hideunwriteable_files(SNUM(tcon->compat))) {
311                 *out_share_flags |= SMB2_SHAREFLAG_ACCESS_BASED_DIRECTORY_ENUM;
312         }
313
314         *out_maximal_access = tcon->compat->share_access;
315
316         *out_tree_id = tcon->global->tcon_wire_id;
317         return NT_STATUS_OK;
318 }
319
320 struct smbd_smb2_tree_connect_state {
321         const char *in_path;
322         uint8_t out_share_type;
323         uint32_t out_share_flags;
324         uint32_t out_capabilities;
325         uint32_t out_maximal_access;
326         uint32_t out_tree_id;
327 };
328
329 static struct tevent_req *smbd_smb2_tree_connect_send(TALLOC_CTX *mem_ctx,
330                                         struct tevent_context *ev,
331                                         struct smbd_smb2_request *smb2req,
332                                         const char *in_path)
333 {
334         struct tevent_req *req;
335         struct smbd_smb2_tree_connect_state *state;
336         NTSTATUS status;
337
338         req = tevent_req_create(mem_ctx, &state,
339                                 struct smbd_smb2_tree_connect_state);
340         if (req == NULL) {
341                 return NULL;
342         }
343         state->in_path = in_path;
344
345         status = smbd_smb2_tree_connect(smb2req,
346                                         state->in_path,
347                                         &state->out_share_type,
348                                         &state->out_share_flags,
349                                         &state->out_capabilities,
350                                         &state->out_maximal_access,
351                                         &state->out_tree_id);
352         if (tevent_req_nterror(req, status)) {
353                 return tevent_req_post(req, ev);
354         }
355
356         tevent_req_done(req);
357         return tevent_req_post(req, ev);
358 }
359
360 static NTSTATUS smbd_smb2_tree_connect_recv(struct tevent_req *req,
361                                             uint8_t *out_share_type,
362                                             uint32_t *out_share_flags,
363                                             uint32_t *out_capabilities,
364                                             uint32_t *out_maximal_access,
365                                             uint32_t *out_tree_id)
366 {
367         struct smbd_smb2_tree_connect_state *state =
368                 tevent_req_data(req,
369                 struct smbd_smb2_tree_connect_state);
370         NTSTATUS status;
371
372         if (tevent_req_is_nterror(req, &status)) {
373                 tevent_req_received(req);
374                 return status;
375         }
376
377         *out_share_type = state->out_share_type;
378         *out_share_flags = state->out_share_flags;
379         *out_capabilities = state->out_capabilities;
380         *out_maximal_access = state->out_maximal_access;
381         *out_tree_id = state->out_tree_id;
382
383         tevent_req_received(req);
384         return NT_STATUS_OK;
385 }
386
387 NTSTATUS smbd_smb2_request_process_tdis(struct smbd_smb2_request *req)
388 {
389         NTSTATUS status;
390         DATA_BLOB outbody;
391
392         status = smbd_smb2_request_verify_sizes(req, 0x04);
393         if (!NT_STATUS_IS_OK(status)) {
394                 return smbd_smb2_request_error(req, status);
395         }
396
397         /*
398          * TODO: cancel all outstanding requests on the tcon
399          */
400         status = smbXsrv_tcon_disconnect(req->tcon, req->tcon->compat->vuid);
401         if (!NT_STATUS_IS_OK(status)) {
402                 DEBUG(0, ("smbd_smb2_request_process_tdis: "
403                           "smbXsrv_tcon_disconnect() failed: %s\n",
404                           nt_errstr(status)));
405                 /*
406                  * If we hit this case, there is something completely
407                  * wrong, so we better disconnect the transport connection.
408                  */
409                 return status;
410         }
411
412         TALLOC_FREE(req->tcon);
413
414         outbody = data_blob_talloc(req->out.vector, NULL, 0x04);
415         if (outbody.data == NULL) {
416                 return smbd_smb2_request_error(req, NT_STATUS_NO_MEMORY);
417         }
418
419         SSVAL(outbody.data, 0x00, 0x04);        /* struct size */
420         SSVAL(outbody.data, 0x02, 0);           /* reserved */
421
422         return smbd_smb2_request_done(req, outbody, NULL);
423 }