c0a637eb1a7efc3e70d8409ba1ab8210d9638def
[amitay/samba.git] / source4 / libcli / smb2 / ioctl.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    SMB2 client ioctl call
5
6    Copyright (C) Andrew Tridgell 2005
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 "libcli/smb2/smb2.h"
24 #include "libcli/smb2/smb2_calls.h"
25 #include "librpc/gen_ndr/ioctl.h"
26
27 /*
28   send a ioctl request
29 */
30 struct smb2_request *smb2_ioctl_send(struct smb2_tree *tree, struct smb2_ioctl *io)
31 {
32         NTSTATUS status;
33         struct smb2_request *req;
34
35         req = smb2_request_init_tree(tree, SMB2_OP_IOCTL, 0x38, true,
36                                      io->in.in.length+io->in.out.length);
37         if (req == NULL) return NULL;
38
39         SSVAL(req->out.body, 0x02, 0); /* pad */
40         SIVAL(req->out.body, 0x04, io->in.function);
41         smb2_push_handle(req->out.body+0x08, &io->in.file.handle);
42
43         status = smb2_push_o32s32_blob(&req->out, 0x18, io->in.out);
44         if (!NT_STATUS_IS_OK(status)) {
45                 talloc_free(req);
46                 return NULL;
47         }
48
49         SIVAL(req->out.body, 0x20, io->in.unknown2);
50
51         status = smb2_push_o32s32_blob(&req->out, 0x24, io->in.in);
52         if (!NT_STATUS_IS_OK(status)) {
53                 talloc_free(req);
54                 return NULL;
55         }
56
57         SIVAL(req->out.body, 0x2C, io->in.max_response_size);
58         SBVAL(req->out.body, 0x30, io->in.flags);
59
60         smb2_transport_send(req);
61
62         return req;
63 }
64
65 /*
66  * 3.3.4.4 Sending an Error Response
67  */
68 static bool smb2_ioctl_is_failure(uint32_t ctl_code, NTSTATUS status,
69                                   size_t data_size)
70 {
71         if (NT_STATUS_IS_OK(status)) {
72                 return false;
73         }
74
75         if (NT_STATUS_EQUAL(status, STATUS_BUFFER_OVERFLOW)
76          && ((ctl_code == FSCTL_PIPE_TRANSCEIVE)
77           || (ctl_code == FSCTL_PIPE_PEEK)
78           || (ctl_code == FSCTL_DFS_GET_REFERRALS))) {
79                 return false;
80         }
81
82         if (((ctl_code == FSCTL_SRV_COPYCHUNK)
83                                 || (ctl_code == FSCTL_SRV_COPYCHUNK_WRITE))
84          && (data_size == sizeof(struct srv_copychunk_rsp))) {
85                 /*
86                  * copychunk responses may come with copychunk data or error
87                  * response data, independent of status.
88                  */
89                 return false;
90         }
91
92         return true;
93 }
94
95 /*
96   recv a ioctl reply
97 */
98 NTSTATUS smb2_ioctl_recv(struct smb2_request *req,
99                          TALLOC_CTX *mem_ctx, struct smb2_ioctl *io)
100 {
101         NTSTATUS status;
102
103         if (!smb2_request_receive(req) ||
104             smb2_ioctl_is_failure(io->in.function, req->status,
105                                   req->in.bufinfo.data_size)) {
106                 return smb2_request_destroy(req);
107         }
108
109         SMB2_CHECK_PACKET_RECV(req, 0x30, true);
110
111         io->out._pad       = SVAL(req->in.body, 0x02);
112         io->out.function   = IVAL(req->in.body, 0x04);
113         smb2_pull_handle(req->in.body+0x08, &io->out.file.handle);
114
115         status = smb2_pull_o32s32_blob(&req->in, mem_ctx, req->in.body+0x18, &io->out.in);
116         if (!NT_STATUS_IS_OK(status)) {
117                 smb2_request_destroy(req);
118                 return status;
119         }
120
121         status = smb2_pull_o32s32_blob(&req->in, mem_ctx, req->in.body+0x20, &io->out.out);
122         if (!NT_STATUS_IS_OK(status)) {
123                 smb2_request_destroy(req);
124                 return status;
125         }
126
127         io->out.unknown2 = IVAL(req->in.body, 0x28);
128         io->out.unknown3 = IVAL(req->in.body, 0x2C);
129
130         return smb2_request_destroy(req);
131 }
132
133 /*
134   sync ioctl request
135 */
136 NTSTATUS smb2_ioctl(struct smb2_tree *tree, TALLOC_CTX *mem_ctx, struct smb2_ioctl *io)
137 {
138         struct smb2_request *req = smb2_ioctl_send(tree, io);
139         return smb2_ioctl_recv(req, mem_ctx, io);
140 }