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