r23792: convert Samba4 to GPLv3
[bbaumbach/samba-autobuild/.git] / source4 / libcli / raw / rawioctl.c
1 /* 
2    Unix SMB/CIFS implementation.
3    client file operations
4    Copyright (C) Andrew Tridgell 2003
5    Copyright (C) James J Myers 2003 <myersjj@samba.org>
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 "libcli/raw/libcliraw.h"
23
24 #define SETUP_REQUEST(cmd, wct, buflen) do { \
25         req = smbcli_request_setup(tree, cmd, wct, buflen); \
26         if (!req) return NULL; \
27 } while (0)
28
29 /* 
30    send a raw smb ioctl - async send
31 */
32 static struct smbcli_request *smb_raw_smbioctl_send(struct smbcli_tree *tree, 
33                                                  union smb_ioctl *parms)
34 {
35         struct smbcli_request *req; 
36
37         SETUP_REQUEST(SMBioctl, 3, 0);
38
39         SSVAL(req->out.vwv, VWV(0), parms->ioctl.in.file.fnum);
40         SIVAL(req->out.vwv, VWV(1), parms->ioctl.in.request);
41
42         if (!smbcli_request_send(req)) {
43                 smbcli_request_destroy(req);
44                 return NULL;
45         }
46
47         return req;
48 }
49
50 /* 
51    send a raw smb ioctl - async recv
52 */
53 static NTSTATUS smb_raw_smbioctl_recv(struct smbcli_request *req, 
54                                       TALLOC_CTX *mem_ctx, 
55                                       union smb_ioctl *parms)
56 {
57         if (!smbcli_request_receive(req) ||
58             smbcli_request_is_error(req)) {
59                 return smbcli_request_destroy(req);
60         }
61
62         parms->ioctl.out.blob = smbcli_req_pull_blob(req, mem_ctx, req->in.data, -1);
63         return smbcli_request_destroy(req);
64 }
65
66
67
68 /****************************************************************************
69 NT ioctl (async send)
70 ****************************************************************************/
71 static struct smbcli_request *smb_raw_ntioctl_send(struct smbcli_tree *tree, 
72                                                 union smb_ioctl *parms)
73 {
74         struct smb_nttrans nt;
75         uint8_t setup[8];
76
77         nt.in.max_setup = 4;
78         nt.in.max_param = 0;
79         nt.in.max_data = parms->ntioctl.in.max_data;
80         nt.in.setup_count = 4;
81         nt.in.setup = setup;
82         SIVAL(setup, 0, parms->ntioctl.in.function);
83         SSVAL(setup, 4, parms->ntioctl.in.file.fnum);
84         SCVAL(setup, 6, parms->ntioctl.in.fsctl);
85         SCVAL(setup, 7, parms->ntioctl.in.filter);
86         nt.in.function = NT_TRANSACT_IOCTL;
87         nt.in.params = data_blob(NULL, 0);
88         nt.in.data = parms->ntioctl.in.blob;
89
90         return smb_raw_nttrans_send(tree, &nt);
91 }
92
93 /****************************************************************************
94 NT ioctl (async recv)
95 ****************************************************************************/
96 static NTSTATUS smb_raw_ntioctl_recv(struct smbcli_request *req, 
97                                      TALLOC_CTX *mem_ctx,
98                                      union smb_ioctl *parms)
99 {
100         NTSTATUS status;
101         struct smb_nttrans nt;
102         TALLOC_CTX *tmp_mem;
103
104         tmp_mem = talloc_new(mem_ctx);
105         NT_STATUS_HAVE_NO_MEMORY(tmp_mem);
106
107         status = smb_raw_nttrans_recv(req, tmp_mem, &nt);
108         if (!NT_STATUS_IS_OK(status)) goto fail;
109
110         parms->ntioctl.out.blob = nt.out.data;
111         talloc_steal(mem_ctx, parms->ntioctl.out.blob.data);
112
113 fail:
114         talloc_free(tmp_mem);
115         return status;
116 }
117
118
119 /* 
120    send a raw ioctl - async send
121 */
122 struct smbcli_request *smb_raw_ioctl_send(struct smbcli_tree *tree, union smb_ioctl *parms)
123 {
124         struct smbcli_request *req = NULL;
125         
126         switch (parms->generic.level) {
127         case RAW_IOCTL_IOCTL:
128                 req = smb_raw_smbioctl_send(tree, parms);
129                 break;
130                 
131         case RAW_IOCTL_NTIOCTL:
132                 req = smb_raw_ntioctl_send(tree, parms);
133                 break;
134
135         case RAW_IOCTL_SMB2:
136         case RAW_IOCTL_SMB2_NO_HANDLE:
137                 return NULL;
138         }
139
140         return req;
141 }
142
143 /* 
144    recv a raw ioctl - async recv
145 */
146 NTSTATUS smb_raw_ioctl_recv(struct smbcli_request *req,
147                             TALLOC_CTX *mem_ctx, union smb_ioctl *parms)
148 {
149         switch (parms->generic.level) {
150         case RAW_IOCTL_IOCTL:
151                 return smb_raw_smbioctl_recv(req, mem_ctx, parms);
152                 
153         case RAW_IOCTL_NTIOCTL:
154                 return smb_raw_ntioctl_recv(req, mem_ctx, parms);
155
156         case RAW_IOCTL_SMB2:
157         case RAW_IOCTL_SMB2_NO_HANDLE:
158                 break;
159         }
160         return NT_STATUS_INVALID_LEVEL;
161 }
162
163 /* 
164    send a raw ioctl - sync interface
165 */
166 _PUBLIC_ NTSTATUS smb_raw_ioctl(struct smbcli_tree *tree, 
167                 TALLOC_CTX *mem_ctx, union smb_ioctl *parms)
168 {
169         struct smbcli_request *req;
170         req = smb_raw_ioctl_send(tree, parms);
171         return smb_raw_ioctl_recv(req, mem_ctx, parms);
172 }