s3:smb2_*: make use of smb2req->xconn where possible
[samba.git] / source3 / smbd / smb2_ioctl_network_fs.c
1 /*
2    Unix SMB/CIFS implementation.
3    Core SMB2 server
4
5    Copyright (C) Stefan Metzmacher 2009
6    Copyright (C) David Disseldorp 2012
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "smbd/smbd.h"
24 #include "smbd/globals.h"
25 #include "../libcli/smb/smb_common.h"
26 #include "../libcli/security/security.h"
27 #include "../lib/util/tevent_ntstatus.h"
28 #include "../lib/ccan/build_assert/build_assert.h"
29 #include "include/ntioctl.h"
30 #include "../librpc/ndr/libndr.h"
31 #include "librpc/gen_ndr/ndr_ioctl.h"
32 #include "smb2_ioctl_private.h"
33
34 #define COPYCHUNK_MAX_CHUNKS    256             /* 2k8r2 & win8 = 256 */
35 #define COPYCHUNK_MAX_CHUNK_LEN 1048576         /* 2k8r2 & win8 = 1048576 */
36 #define COPYCHUNK_MAX_TOTAL_LEN 16777216        /* 2k8r2 & win8 = 16777216 */
37 static void copychunk_pack_limits(struct srv_copychunk_rsp *cc_rsp)
38 {
39         cc_rsp->chunks_written = COPYCHUNK_MAX_CHUNKS;
40         cc_rsp->chunk_bytes_written = COPYCHUNK_MAX_CHUNK_LEN;
41         cc_rsp->total_bytes_written = COPYCHUNK_MAX_TOTAL_LEN;
42 }
43
44 static NTSTATUS copychunk_check_limits(struct srv_copychunk_copy *cc_copy)
45 {
46         uint32_t i;
47         uint32_t total_len = 0;
48
49         /*
50          * [MS-SMB2] 3.3.5.15.6 Handling a Server-Side Data Copy Request
51          * Send and invalid parameter response if:
52          * - The ChunkCount value is greater than
53          *   ServerSideCopyMaxNumberofChunks
54          */
55         if (cc_copy->chunk_count > COPYCHUNK_MAX_CHUNKS) {
56                 return NT_STATUS_INVALID_PARAMETER;
57         }
58
59         for (i = 0; i < cc_copy->chunk_count; i++) {
60                 /*
61                  * - The Length value in a single chunk is greater than
62                  *   ServerSideCopyMaxChunkSize or equal to zero.
63                  */
64                 if ((cc_copy->chunks[i].length == 0)
65                  || (cc_copy->chunks[i].length > COPYCHUNK_MAX_CHUNK_LEN)) {
66                         return NT_STATUS_INVALID_PARAMETER;
67                 }
68                 total_len += cc_copy->chunks[i].length;
69         }
70         /*
71          * - Sum of Lengths in all chunks is greater than
72          *   ServerSideCopyMaxDataSize
73          */
74         if (total_len > COPYCHUNK_MAX_TOTAL_LEN) {
75                 return NT_STATUS_INVALID_PARAMETER;
76         }
77
78         return NT_STATUS_OK;
79 }
80
81 struct fsctl_srv_copychunk_state {
82         struct connection_struct *conn;
83         uint32_t dispatch_count;
84         uint32_t recv_count;
85         uint32_t bad_recv_count;
86         NTSTATUS status;
87         off_t total_written;
88         struct files_struct *src_fsp;
89         struct files_struct *dst_fsp;
90         enum {
91                 COPYCHUNK_OUT_EMPTY = 0,
92                 COPYCHUNK_OUT_LIMITS,
93                 COPYCHUNK_OUT_RSP,
94         } out_data;
95 };
96 static void fsctl_srv_copychunk_vfs_done(struct tevent_req *subreq);
97
98 static NTSTATUS copychunk_check_handles(uint32_t ctl_code,
99                                         struct files_struct *src_fsp,
100                                         struct files_struct *dst_fsp,
101                                         struct smb_request *smb1req)
102 {
103         /*
104          * [MS-SMB2] 3.3.5.15.6 Handling a Server-Side Data Copy Request
105          * The server MUST fail the request with STATUS_ACCESS_DENIED if any of
106          * the following are true:
107          * - The Open.GrantedAccess of the destination file does not include
108          *   FILE_WRITE_DATA or FILE_APPEND_DATA.
109          */
110         if (!CHECK_WRITE(dst_fsp)) {
111                 DEBUG(5, ("copy chunk no write on dest handle (%s).\n",
112                         smb_fname_str_dbg(dst_fsp->fsp_name) ));
113                 return NT_STATUS_ACCESS_DENIED;
114         }
115         /*
116          * - The Open.GrantedAccess of the destination file does not include
117          *   FILE_READ_DATA, and the CtlCode is FSCTL_SRV_COPYCHUNK.
118          */
119         if ((ctl_code == FSCTL_SRV_COPYCHUNK)
120           && !CHECK_READ(dst_fsp, smb1req)) {
121                 DEBUG(5, ("copy chunk no read on dest handle (%s).\n",
122                         smb_fname_str_dbg(dst_fsp->fsp_name) ));
123                 return NT_STATUS_ACCESS_DENIED;
124         }
125         /*
126          * - The Open.GrantedAccess of the source file does not include
127          *   FILE_READ_DATA access.
128          */
129         if (!CHECK_READ(src_fsp, smb1req)) {
130                 DEBUG(5, ("copy chunk no read on src handle (%s).\n",
131                         smb_fname_str_dbg(src_fsp->fsp_name) ));
132                 return NT_STATUS_ACCESS_DENIED;
133         }
134
135         if (src_fsp->is_directory) {
136                 DEBUG(5, ("copy chunk no read on src directory handle (%s).\n",
137                         smb_fname_str_dbg(src_fsp->fsp_name) ));
138                 return NT_STATUS_ACCESS_DENIED;
139         }
140
141         if (dst_fsp->is_directory) {
142                 DEBUG(5, ("copy chunk no read on dst directory handle (%s).\n",
143                         smb_fname_str_dbg(dst_fsp->fsp_name) ));
144                 return NT_STATUS_ACCESS_DENIED;
145         }
146
147         if (IS_IPC(src_fsp->conn) || IS_IPC(dst_fsp->conn)) {
148                 DEBUG(5, ("copy chunk no access on IPC$ handle.\n"));
149                 return NT_STATUS_ACCESS_DENIED;
150         }
151
152         if (IS_PRINT(src_fsp->conn) || IS_PRINT(dst_fsp->conn)) {
153                 DEBUG(5, ("copy chunk no access on PRINT handle.\n"));
154                 return NT_STATUS_ACCESS_DENIED;
155         }
156
157         return NT_STATUS_OK;
158 }
159
160 static struct tevent_req *fsctl_srv_copychunk_send(TALLOC_CTX *mem_ctx,
161                                                    struct tevent_context *ev,
162                                                    uint32_t ctl_code,
163                                                    struct files_struct *dst_fsp,
164                                                    DATA_BLOB *in_input,
165                                                    size_t in_max_output,
166                                                    struct smbd_smb2_request *smb2req)
167 {
168         struct tevent_req *req;
169         struct srv_copychunk_copy cc_copy;
170         enum ndr_err_code ndr_ret;
171         uint64_t src_persistent_h;
172         uint64_t src_volatile_h;
173         int i;
174         struct srv_copychunk *chunk;
175         struct fsctl_srv_copychunk_state *state;
176
177         /* handler for both copy-chunk variants */
178         SMB_ASSERT((ctl_code == FSCTL_SRV_COPYCHUNK)
179                 || (ctl_code == FSCTL_SRV_COPYCHUNK_WRITE));
180
181         req = tevent_req_create(mem_ctx, &state,
182                                 struct fsctl_srv_copychunk_state);
183         if (req == NULL) {
184                 return NULL;
185         }
186         state->conn = dst_fsp->conn;
187
188         if (in_max_output < sizeof(struct srv_copychunk_rsp)) {
189                 DEBUG(3, ("max output %d not large enough to hold copy chunk "
190                           "response %lu\n", (int)in_max_output,
191                           (unsigned long)sizeof(struct srv_copychunk_rsp)));
192                 state->status = NT_STATUS_INVALID_PARAMETER;
193                 tevent_req_nterror(req, state->status);
194                 return tevent_req_post(req, ev);
195         }
196
197         ndr_ret = ndr_pull_struct_blob(in_input, mem_ctx, &cc_copy,
198                         (ndr_pull_flags_fn_t)ndr_pull_srv_copychunk_copy);
199         if (ndr_ret != NDR_ERR_SUCCESS) {
200                 DEBUG(0, ("failed to unmarshall copy chunk req\n"));
201                 state->status = NT_STATUS_INVALID_PARAMETER;
202                 tevent_req_nterror(req, state->status);
203                 return tevent_req_post(req, ev);
204         }
205
206         /* persistent/volatile keys sent as the resume key */
207         src_persistent_h = BVAL(cc_copy.source_key, 0);
208         src_volatile_h = BVAL(cc_copy.source_key, 8);
209         state->src_fsp = file_fsp_get(smb2req, src_persistent_h, src_volatile_h);
210         if (state->src_fsp == NULL) {
211                 DEBUG(3, ("invalid resume key in copy chunk req\n"));
212                 state->status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
213                 tevent_req_nterror(req, state->status);
214                 return tevent_req_post(req, ev);
215         }
216
217         state->dst_fsp = dst_fsp;
218
219         state->status = copychunk_check_handles(ctl_code,
220                                                 state->src_fsp,
221                                                 state->dst_fsp,
222                                                 smb2req->smb1req);
223         if (!NT_STATUS_IS_OK(state->status)) {
224                 tevent_req_nterror(req, state->status);
225                 return tevent_req_post(req, ev);
226         }
227
228         state->status = copychunk_check_limits(&cc_copy);
229         if (tevent_req_nterror(req, state->status)) {
230                 DEBUG(3, ("copy chunk req exceeds limits\n"));
231                 state->out_data = COPYCHUNK_OUT_LIMITS;
232                 return tevent_req_post(req, ev);
233         }
234
235         /* any errors from here onwards should carry copychunk response data */
236         state->out_data = COPYCHUNK_OUT_RSP;
237
238         for (i = 0; i < cc_copy.chunk_count; i++) {
239                 struct tevent_req *vfs_subreq;
240                 chunk = &cc_copy.chunks[i];
241                 vfs_subreq = SMB_VFS_COPY_CHUNK_SEND(dst_fsp->conn,
242                                                      state, ev,
243                                                      state->src_fsp,
244                                                      chunk->source_off,
245                                                      state->dst_fsp,
246                                                      chunk->target_off,
247                                                      chunk->length);
248                 if (vfs_subreq == NULL) {
249                         DEBUG(0, ("VFS copy chunk send failed\n"));
250                         state->status = NT_STATUS_NO_MEMORY;
251                         if (state->dispatch_count == 0) {
252                                 /* nothing dispatched, return immediately */
253                                 tevent_req_nterror(req, state->status);
254                                 return tevent_req_post(req, ev);
255                         } else {
256                                 /*
257                                  * wait for dispatched to complete before
258                                  * returning error.
259                                  */
260                                 break;
261                         }
262                 }
263                 tevent_req_set_callback(vfs_subreq,
264                                         fsctl_srv_copychunk_vfs_done, req);
265                 state->dispatch_count++;
266         }
267
268         return req;
269 }
270
271 static void fsctl_srv_copychunk_vfs_done(struct tevent_req *subreq)
272 {
273         struct tevent_req *req = tevent_req_callback_data(
274                 subreq, struct tevent_req);
275         struct fsctl_srv_copychunk_state *state = tevent_req_data(req,
276                                         struct fsctl_srv_copychunk_state);
277         off_t chunk_nwritten;
278         NTSTATUS status;
279
280         state->recv_count++;
281         status = SMB_VFS_COPY_CHUNK_RECV(state->conn, subreq,
282                                          &chunk_nwritten);
283         TALLOC_FREE(subreq);
284         if (NT_STATUS_IS_OK(status)) {
285                 DEBUG(10, ("good copy chunk recv %u of %u\n",
286                            (unsigned int)state->recv_count,
287                            (unsigned int)state->dispatch_count));
288                 state->total_written += chunk_nwritten;
289         } else {
290                 DEBUG(0, ("bad status in copy chunk recv %u of %u: %s\n",
291                           (unsigned int)state->recv_count,
292                           (unsigned int)state->dispatch_count,
293                           nt_errstr(status)));
294                 state->bad_recv_count++;
295                 /* may overwrite previous failed status */
296                 state->status = status;
297         }
298
299         if (state->recv_count != state->dispatch_count) {
300                 /*
301                  * Wait for all VFS copy_chunk requests to complete, even
302                  * if an error is received for a specific chunk.
303                  */
304                 return;
305         }
306
307         if (!tevent_req_nterror(req, state->status)) {
308                 tevent_req_done(req);
309         }
310 }
311
312 static NTSTATUS fsctl_srv_copychunk_recv(struct tevent_req *req,
313                                          struct srv_copychunk_rsp *cc_rsp,
314                                          bool *pack_rsp)
315 {
316         struct fsctl_srv_copychunk_state *state = tevent_req_data(req,
317                                         struct fsctl_srv_copychunk_state);
318         NTSTATUS status;
319
320         switch (state->out_data) {
321         case COPYCHUNK_OUT_EMPTY:
322                 *pack_rsp = false;
323                 break;
324         case COPYCHUNK_OUT_LIMITS:
325                 /* 2.2.32.1 - send back our maximum transfer size limits */
326                 copychunk_pack_limits(cc_rsp);
327                 *pack_rsp = true;
328                 break;
329         case COPYCHUNK_OUT_RSP:
330                 cc_rsp->chunks_written = state->recv_count - state->bad_recv_count;
331                 cc_rsp->chunk_bytes_written = 0;
332                 cc_rsp->total_bytes_written = state->total_written;
333                 *pack_rsp = true;
334                 break;
335         default:        /* not reached */
336                 assert(1);
337                 break;
338         }
339         status = state->status;
340         tevent_req_received(req);
341
342         return status;
343 }
344
345 static NTSTATUS fsctl_validate_neg_info(TALLOC_CTX *mem_ctx,
346                                         struct tevent_context *ev,
347                                         struct smbXsrv_connection *conn,
348                                         DATA_BLOB *in_input,
349                                         uint32_t in_max_output,
350                                         DATA_BLOB *out_output,
351                                         bool *disconnect)
352 {
353         uint32_t in_capabilities;
354         DATA_BLOB in_guid_blob;
355         struct GUID in_guid;
356         uint16_t in_security_mode;
357         uint16_t in_num_dialects;
358         uint16_t dialect;
359         DATA_BLOB out_guid_blob;
360         NTSTATUS status;
361         enum protocol_types protocol = PROTOCOL_NONE;
362
363         if (in_input->length < 0x18) {
364                 return NT_STATUS_INVALID_PARAMETER;
365         }
366
367         in_capabilities = IVAL(in_input->data, 0x00);
368         in_guid_blob = data_blob_const(in_input->data + 0x04, 16);
369         in_security_mode = SVAL(in_input->data, 0x14);
370         in_num_dialects = SVAL(in_input->data, 0x16);
371
372         if (in_input->length < (0x18 + in_num_dialects*2)) {
373                 return NT_STATUS_INVALID_PARAMETER;
374         }
375
376         if (in_max_output < 0x18) {
377                 return NT_STATUS_BUFFER_TOO_SMALL;
378         }
379
380         status = GUID_from_ndr_blob(&in_guid_blob, &in_guid);
381         if (!NT_STATUS_IS_OK(status)) {
382                 return status;
383         }
384
385         /*
386          * From: [MS-SMB2]
387          * 3.3.5.15.12 Handling a Validate Negotiate Info Request
388          *
389          * The server MUST determine the greatest common dialect
390          * between the dialects it implements and the Dialects array
391          * of the VALIDATE_NEGOTIATE_INFO request. If no dialect is
392          * matched, or if the value is not equal to Connection.Dialect,
393          * the server MUST terminate the transport connection
394          * and free the Connection object.
395          */
396         protocol = smbd_smb2_protocol_dialect_match(in_input->data + 0x18,
397                                                     in_num_dialects,
398                                                     &dialect);
399         if (conn->protocol != protocol) {
400                 *disconnect = true;
401                 return NT_STATUS_ACCESS_DENIED;
402         }
403
404         if (!GUID_equal(&in_guid, &conn->smb2.client.guid)) {
405                 *disconnect = true;
406                 return NT_STATUS_ACCESS_DENIED;
407         }
408
409         if (in_security_mode != conn->smb2.client.security_mode) {
410                 *disconnect = true;
411                 return NT_STATUS_ACCESS_DENIED;
412         }
413
414         if (in_capabilities != conn->smb2.client.capabilities) {
415                 *disconnect = true;
416                 return NT_STATUS_ACCESS_DENIED;
417         }
418
419         status = GUID_to_ndr_blob(&conn->smb2.server.guid, mem_ctx,
420                                   &out_guid_blob);
421         if (!NT_STATUS_IS_OK(status)) {
422                 return status;
423         }
424
425         *out_output = data_blob_talloc(mem_ctx, NULL, 0x18);
426         if (out_output->data == NULL) {
427                 return NT_STATUS_NO_MEMORY;
428         }
429
430         SIVAL(out_output->data, 0x00, conn->smb2.server.capabilities);
431         memcpy(out_output->data+0x04, out_guid_blob.data, 16);
432         SSVAL(out_output->data, 0x14, conn->smb2.server.security_mode);
433         SSVAL(out_output->data, 0x16, conn->smb2.server.dialect);
434
435         return NT_STATUS_OK;
436 }
437
438 static NTSTATUS fsctl_srv_req_resume_key(TALLOC_CTX *mem_ctx,
439                                          struct tevent_context *ev,
440                                          struct files_struct *fsp,
441                                          uint32_t in_max_output,
442                                          DATA_BLOB *out_output)
443 {
444         struct req_resume_key_rsp rkey_rsp;
445         enum ndr_err_code ndr_ret;
446         DATA_BLOB output;
447
448         if (fsp == NULL) {
449                 return NT_STATUS_FILE_CLOSED;
450         }
451
452         ZERO_STRUCT(rkey_rsp);
453         /* combine persistent and volatile handles for the resume key */
454         SBVAL(rkey_rsp.resume_key, 0, fsp->op->global->open_persistent_id);
455         SBVAL(rkey_rsp.resume_key, 8, fsp->op->global->open_volatile_id);
456
457         ndr_ret = ndr_push_struct_blob(&output, mem_ctx, &rkey_rsp,
458                         (ndr_push_flags_fn_t)ndr_push_req_resume_key_rsp);
459         if (ndr_ret != NDR_ERR_SUCCESS) {
460                 return NT_STATUS_INTERNAL_ERROR;
461         }
462
463         if (in_max_output < output.length) {
464                 DEBUG(1, ("max output %u too small for resume key rsp %ld\n",
465                           (unsigned int)in_max_output, (long int)output.length));
466                 return NT_STATUS_INVALID_PARAMETER;
467         }
468         *out_output = output;
469
470         return NT_STATUS_OK;
471 }
472
473 static void smb2_ioctl_network_fs_copychunk_done(struct tevent_req *subreq);
474
475 struct tevent_req *smb2_ioctl_network_fs(uint32_t ctl_code,
476                                          struct tevent_context *ev,
477                                          struct tevent_req *req,
478                                          struct smbd_smb2_ioctl_state *state)
479 {
480         struct tevent_req *subreq;
481         NTSTATUS status;
482
483         switch (ctl_code) {
484         /*
485          * [MS-SMB2] 2.2.31
486          * FSCTL_SRV_COPYCHUNK is issued when a handle has
487          * FILE_READ_DATA and FILE_WRITE_DATA access to the file;
488          * FSCTL_SRV_COPYCHUNK_WRITE is issued when a handle only has
489          * FILE_WRITE_DATA access.
490          */
491         case FSCTL_SRV_COPYCHUNK_WRITE: /* FALL THROUGH */
492         case FSCTL_SRV_COPYCHUNK:
493                 subreq = fsctl_srv_copychunk_send(state, ev,
494                                                   ctl_code,
495                                                   state->fsp,
496                                                   &state->in_input,
497                                                   state->in_max_output,
498                                                   state->smb2req);
499                 if (tevent_req_nomem(subreq, req)) {
500                         return tevent_req_post(req, ev);
501                 }
502                 tevent_req_set_callback(subreq,
503                                         smb2_ioctl_network_fs_copychunk_done,
504                                         req);
505                 return req;
506                 break;
507         case FSCTL_VALIDATE_NEGOTIATE_INFO:
508                 status = fsctl_validate_neg_info(state, ev,
509                                                  state->smbreq->xconn,
510                                                  &state->in_input,
511                                                  state->in_max_output,
512                                                  &state->out_output,
513                                                  &state->disconnect);
514                 if (!tevent_req_nterror(req, status)) {
515                         tevent_req_done(req);
516                 }
517                 return tevent_req_post(req, ev);
518                 break;
519         case FSCTL_SRV_REQUEST_RESUME_KEY:
520                 status = fsctl_srv_req_resume_key(state, ev, state->fsp,
521                                                   state->in_max_output,
522                                                   &state->out_output);
523                 if (!tevent_req_nterror(req, status)) {
524                         tevent_req_done(req);
525                 }
526                 return tevent_req_post(req, ev);
527                 break;
528         default: {
529                 uint8_t *out_data = NULL;
530                 uint32_t out_data_len = 0;
531
532                 if (state->fsp == NULL) {
533                         status = NT_STATUS_NOT_SUPPORTED;
534                 } else {
535                         status = SMB_VFS_FSCTL(state->fsp,
536                                                state,
537                                                ctl_code,
538                                                state->smbreq->flags2,
539                                                state->in_input.data,
540                                                state->in_input.length,
541                                                &out_data,
542                                                state->in_max_output,
543                                                &out_data_len);
544                         state->out_output = data_blob_const(out_data, out_data_len);
545                         if (NT_STATUS_IS_OK(status)) {
546                                 tevent_req_done(req);
547                                 return tevent_req_post(req, ev);
548                         }
549                 }
550
551                 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
552                         if (IS_IPC(state->smbreq->conn)) {
553                                 status = NT_STATUS_FS_DRIVER_REQUIRED;
554                         } else {
555                                 status = NT_STATUS_INVALID_DEVICE_REQUEST;
556                         }
557                 }
558
559                 tevent_req_nterror(req, status);
560                 return tevent_req_post(req, ev);
561                 break;
562         }
563         }
564
565         tevent_req_nterror(req, NT_STATUS_INTERNAL_ERROR);
566         return tevent_req_post(req, ev);
567 }
568
569 static void smb2_ioctl_network_fs_copychunk_done(struct tevent_req *subreq)
570 {
571         struct tevent_req *req = tevent_req_callback_data(subreq,
572                                                           struct tevent_req);
573         struct smbd_smb2_ioctl_state *ioctl_state = tevent_req_data(req,
574                                                 struct smbd_smb2_ioctl_state);
575         struct srv_copychunk_rsp cc_rsp;
576         NTSTATUS status;
577         bool pack_rsp = false;
578
579         ZERO_STRUCT(cc_rsp);
580         status = fsctl_srv_copychunk_recv(subreq, &cc_rsp, &pack_rsp);
581         TALLOC_FREE(subreq);
582         if (pack_rsp == true) {
583                 enum ndr_err_code ndr_ret;
584                 ndr_ret = ndr_push_struct_blob(&ioctl_state->out_output,
585                                                ioctl_state,
586                                                &cc_rsp,
587                                 (ndr_push_flags_fn_t)ndr_push_srv_copychunk_rsp);
588                 if (ndr_ret != NDR_ERR_SUCCESS) {
589                         status = NT_STATUS_INTERNAL_ERROR;
590                 }
591         }
592
593         if (!tevent_req_nterror(req, status)) {
594                 tevent_req_done(req);
595         }
596 }