1 /*
2    Unix SMB/CIFS implementation.
3 
4    RAW_QFS_* operations
5 
6    Copyright (C) Andrew Tridgell 2003
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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22 
23 #include "includes.h"
24 #include "libcli/raw/libcliraw.h"
25 #include "librpc/gen_ndr/ndr_misc.h"
26 
27 /****************************************************************************
28  Query FS Info - SMBdskattr call (async send)
29 ****************************************************************************/
smb_raw_dskattr_send(struct smbcli_tree * tree,union smb_fsinfo * fsinfo)30 static struct smbcli_request *smb_raw_dskattr_send(struct smbcli_tree *tree,
31 						union smb_fsinfo *fsinfo)
32 {
33 	struct smbcli_request *req;
34 
35 	req = smbcli_request_setup(tree, SMBdskattr, 0, 0);
36 
37 	if (!smbcli_request_send(req)) {
38 		smbcli_request_destroy(req);
39 		return NULL;
40 	}
41 
42 	return req;
43 }
44 
45 /****************************************************************************
46  Query FS Info - SMBdskattr call (async recv)
47 ****************************************************************************/
smb_raw_dskattr_recv(struct smbcli_request * req,union smb_fsinfo * fsinfo)48 static NTSTATUS smb_raw_dskattr_recv(struct smbcli_request *req,
49 				     union smb_fsinfo *fsinfo)
50 {
51 	if (!smbcli_request_receive(req) ||
52 	    smbcli_request_is_error(req)) {
53 		goto failed;
54 	}
55 
56 	SMBCLI_CHECK_WCT(req, 5);
57 	fsinfo->dskattr.out.units_total =     SVAL(req->in.vwv, VWV(0));
58 	fsinfo->dskattr.out.blocks_per_unit = SVAL(req->in.vwv, VWV(1));
59 	fsinfo->dskattr.out.block_size =      SVAL(req->in.vwv, VWV(2));
60 	fsinfo->dskattr.out.units_free =      SVAL(req->in.vwv, VWV(3));
61 
62 failed:
63 	return smbcli_request_destroy(req);
64 }
65 
66 
67 /****************************************************************************
68  RAW_QFS_ trans2 interface via blobs (async send)
69 ****************************************************************************/
smb_raw_qfsinfo_send(struct smbcli_tree * tree,TALLOC_CTX * mem_ctx,uint16_t info_level)70 static struct smbcli_request *smb_raw_qfsinfo_send(struct smbcli_tree *tree,
71 						TALLOC_CTX *mem_ctx,
72 						uint16_t info_level)
73 {
74 	struct smb_trans2 tp;
75 	uint16_t setup = TRANSACT2_QFSINFO;
76 
77 	tp.in.max_setup = 0;
78 	tp.in.flags = 0;
79 	tp.in.timeout = 0;
80 	tp.in.setup_count = 1;
81 	tp.in.max_param = 0;
82 	tp.in.max_data = 0xFFFF;
83 	tp.in.setup = &setup;
84 	tp.in.data = data_blob(NULL, 0);
85 	tp.in.timeout = 0;
86 
87 	tp.in.params = data_blob_talloc(mem_ctx, NULL, 2);
88 	if (!tp.in.params.data) {
89 		return NULL;
90 	}
91 	SSVAL(tp.in.params.data, 0, info_level);
92 
93 	return smb_raw_trans2_send(tree, &tp);
94 }
95 
96 /****************************************************************************
97  RAW_QFS_ trans2 interface via blobs (async recv)
98 ****************************************************************************/
smb_raw_qfsinfo_blob_recv(struct smbcli_request * req,TALLOC_CTX * mem_ctx,DATA_BLOB * blob)99 static NTSTATUS smb_raw_qfsinfo_blob_recv(struct smbcli_request *req,
100 					  TALLOC_CTX *mem_ctx,
101 					  DATA_BLOB *blob)
102 {
103 	struct smb_trans2 tp;
104 	NTSTATUS status;
105 
106 	status = smb_raw_trans2_recv(req, mem_ctx, &tp);
107 
108 	if (NT_STATUS_IS_OK(status)) {
109 		(*blob) = tp.out.data;
110 	}
111 
112 	return status;
113 }
114 
115 
116 /* local macros to make the code more readable */
117 #define QFS_CHECK_MIN_SIZE(size) if (blob.length < (size)) { \
118       DEBUG(1,("Unexpected QFS reply size %d for level %u - expected min of %d\n", \
119 	       (int)blob.length, fsinfo->generic.level, (size))); \
120       status = NT_STATUS_INFO_LENGTH_MISMATCH; \
121       goto failed; \
122 }
123 #define QFS_CHECK_SIZE(size) if (blob.length != (size)) { \
124       DEBUG(1,("Unexpected QFS reply size %d for level %u - expected %d\n", \
125 	       (int)blob.length, fsinfo->generic.level, (size))); \
126       status = NT_STATUS_INFO_LENGTH_MISMATCH; \
127       goto failed; \
128 }
129 
130 
131 /****************************************************************************
132  Query FSInfo raw interface (async send)
133 ****************************************************************************/
smb_raw_fsinfo_send(struct smbcli_tree * tree,TALLOC_CTX * mem_ctx,union smb_fsinfo * fsinfo)134 struct smbcli_request *smb_raw_fsinfo_send(struct smbcli_tree *tree,
135 					TALLOC_CTX *mem_ctx,
136 					union smb_fsinfo *fsinfo)
137 {
138 	uint16_t info_level;
139 
140 	/* handle the only non-trans2 call separately */
141 	if (fsinfo->generic.level == RAW_QFS_DSKATTR) {
142 		return smb_raw_dskattr_send(tree, fsinfo);
143 	}
144 	if (fsinfo->generic.level >= RAW_QFS_GENERIC) {
145 		return NULL;
146 	}
147 
148 	/* the headers map the trans2 levels direct to info levels */
149 	info_level = (uint16_t)fsinfo->generic.level;
150 
151 	return smb_raw_qfsinfo_send(tree, mem_ctx, info_level);
152 }
153 
154 /*
155   parse the fsinfo 'passthru' level replies
156 */
smb_raw_fsinfo_passthru_parse(DATA_BLOB blob,TALLOC_CTX * mem_ctx,enum smb_fsinfo_level level,union smb_fsinfo * fsinfo)157 NTSTATUS smb_raw_fsinfo_passthru_parse(DATA_BLOB blob, TALLOC_CTX *mem_ctx,
158 				       enum smb_fsinfo_level level,
159 				       union smb_fsinfo *fsinfo)
160 {
161 	NTSTATUS status = NT_STATUS_OK;
162 	int i;
163 
164 	/* parse the results */
165 	switch (level) {
166 	case RAW_QFS_VOLUME_INFORMATION:
167 		QFS_CHECK_MIN_SIZE(18);
168 		fsinfo->volume_info.out.create_time   = smbcli_pull_nttime(blob.data, 0);
169 		fsinfo->volume_info.out.serial_number = IVAL(blob.data, 8);
170 		smbcli_blob_pull_string(NULL, mem_ctx, &blob,
171 					&fsinfo->volume_info.out.volume_name,
172 					12, 18, STR_UNICODE);
173 		break;
174 
175 	case RAW_QFS_SIZE_INFORMATION:
176 		QFS_CHECK_SIZE(24);
177 		fsinfo->size_info.out.total_alloc_units = BVAL(blob.data,  0);
178 		fsinfo->size_info.out.avail_alloc_units = BVAL(blob.data,  8);
179 		fsinfo->size_info.out.sectors_per_unit =  IVAL(blob.data, 16);
180 		fsinfo->size_info.out.bytes_per_sector =  IVAL(blob.data, 20);
181 		break;
182 
183 	case RAW_QFS_DEVICE_INFORMATION:
184 		QFS_CHECK_SIZE(8);
185 		fsinfo->device_info.out.device_type     = IVAL(blob.data,  0);
186 		fsinfo->device_info.out.characteristics = IVAL(blob.data,  4);
187 		break;
188 
189 	case RAW_QFS_ATTRIBUTE_INFORMATION:
190 		QFS_CHECK_MIN_SIZE(12);
191 		fsinfo->attribute_info.out.fs_attr   =                 IVAL(blob.data, 0);
192 		fsinfo->attribute_info.out.max_file_component_length = IVAL(blob.data, 4);
193 		smbcli_blob_pull_string(NULL, mem_ctx, &blob,
194 					&fsinfo->attribute_info.out.fs_type,
195 					8, 12, STR_UNICODE);
196 		break;
197 
198 	case RAW_QFS_QUOTA_INFORMATION:
199 		QFS_CHECK_SIZE(48);
200 		fsinfo->quota_information.out.unknown[0] =  BVAL(blob.data,  0);
201 		fsinfo->quota_information.out.unknown[1] =  BVAL(blob.data,  8);
202 		fsinfo->quota_information.out.unknown[2] =  BVAL(blob.data, 16);
203 		fsinfo->quota_information.out.quota_soft =  BVAL(blob.data, 24);
204 		fsinfo->quota_information.out.quota_hard =  BVAL(blob.data, 32);
205 		fsinfo->quota_information.out.quota_flags = BVAL(blob.data, 40);
206 		break;
207 
208 	case RAW_QFS_FULL_SIZE_INFORMATION:
209 		QFS_CHECK_SIZE(32);
210 		fsinfo->full_size_information.out.total_alloc_units =        BVAL(blob.data,  0);
211 		fsinfo->full_size_information.out.call_avail_alloc_units =   BVAL(blob.data,  8);
212 		fsinfo->full_size_information.out.actual_avail_alloc_units = BVAL(blob.data, 16);
213 		fsinfo->full_size_information.out.sectors_per_unit =         IVAL(blob.data, 24);
214 		fsinfo->full_size_information.out.bytes_per_sector =         IVAL(blob.data, 28);
215 		break;
216 
217 	case RAW_QFS_OBJECTID_INFORMATION:
218 		QFS_CHECK_SIZE(64);
219 		status = ndr_pull_struct_blob(&blob, mem_ctx, &fsinfo->objectid_information.out.guid,
220 					      (ndr_pull_flags_fn_t)ndr_pull_GUID);
221 		for (i=0;i<6;i++) {
222 			fsinfo->objectid_information.out.unknown[i] = BVAL(blob.data, 16 + i*8);
223 		}
224 		break;
225 
226 	default:
227 		status = NT_STATUS_INVALID_INFO_CLASS;
228 	}
229 
230 failed:
231 	return status;
232 }
233 
234 
235 /****************************************************************************
236  Query FSInfo raw interface (async recv)
237 ****************************************************************************/
smb_raw_fsinfo_recv(struct smbcli_request * req,TALLOC_CTX * mem_ctx,union smb_fsinfo * fsinfo)238 NTSTATUS smb_raw_fsinfo_recv(struct smbcli_request *req,
239 			     TALLOC_CTX *mem_ctx,
240 			     union smb_fsinfo *fsinfo)
241 {
242 	DATA_BLOB blob;
243 	NTSTATUS status;
244 	struct smbcli_session *session = req?req->session:NULL;
245 
246 	if (fsinfo->generic.level == RAW_QFS_DSKATTR) {
247 		return smb_raw_dskattr_recv(req, fsinfo);
248 	}
249 
250 	status = smb_raw_qfsinfo_blob_recv(req, mem_ctx, &blob);
251 	if (!NT_STATUS_IS_OK(status)) {
252 		return status;
253 	}
254 
255 	/* parse the results */
256 	switch (fsinfo->generic.level) {
257 	case RAW_QFS_GENERIC:
258 	case RAW_QFS_DSKATTR:
259 		/* handled above */
260 		break;
261 
262 	case RAW_QFS_ALLOCATION:
263 		QFS_CHECK_SIZE(18);
264 		fsinfo->allocation.out.fs_id =             IVAL(blob.data,  0);
265 		fsinfo->allocation.out.sectors_per_unit =  IVAL(blob.data,  4);
266 		fsinfo->allocation.out.total_alloc_units = IVAL(blob.data,  8);
267 		fsinfo->allocation.out.avail_alloc_units = IVAL(blob.data, 12);
268 		fsinfo->allocation.out.bytes_per_sector =  SVAL(blob.data, 16);
269 		break;
270 
271 	case RAW_QFS_VOLUME:
272 		QFS_CHECK_MIN_SIZE(5);
273 		fsinfo->volume.out.serial_number = IVAL(blob.data, 0);
274 		smbcli_blob_pull_string(session, mem_ctx, &blob,
275 				     &fsinfo->volume.out.volume_name,
276 				     4, 5, STR_LEN8BIT | STR_NOALIGN);
277 		break;
278 
279 	case RAW_QFS_VOLUME_INFO:
280 	case RAW_QFS_VOLUME_INFORMATION:
281 		return smb_raw_fsinfo_passthru_parse(blob, mem_ctx,
282 						     RAW_QFS_VOLUME_INFORMATION, fsinfo);
283 
284 	case RAW_QFS_SIZE_INFO:
285 	case RAW_QFS_SIZE_INFORMATION:
286 		return smb_raw_fsinfo_passthru_parse(blob, mem_ctx,
287 						     RAW_QFS_SIZE_INFORMATION, fsinfo);
288 
289 	case RAW_QFS_DEVICE_INFO:
290 	case RAW_QFS_DEVICE_INFORMATION:
291 		return smb_raw_fsinfo_passthru_parse(blob, mem_ctx,
292 						     RAW_QFS_DEVICE_INFORMATION, fsinfo);
293 
294 	case RAW_QFS_ATTRIBUTE_INFO:
295 	case RAW_QFS_ATTRIBUTE_INFORMATION:
296 		return smb_raw_fsinfo_passthru_parse(blob, mem_ctx,
297 						     RAW_QFS_ATTRIBUTE_INFORMATION, fsinfo);
298 
299 	case RAW_QFS_UNIX_INFO:
300 		QFS_CHECK_SIZE(12);
301 		fsinfo->unix_info.out.major_version = SVAL(blob.data, 0);
302 		fsinfo->unix_info.out.minor_version = SVAL(blob.data, 2);
303 		fsinfo->unix_info.out.capability    = SVAL(blob.data, 4);
304 		break;
305 
306 	case RAW_QFS_QUOTA_INFORMATION:
307 		return smb_raw_fsinfo_passthru_parse(blob, mem_ctx,
308 						     RAW_QFS_QUOTA_INFORMATION, fsinfo);
309 
310 	case RAW_QFS_FULL_SIZE_INFORMATION:
311 		return smb_raw_fsinfo_passthru_parse(blob, mem_ctx,
312 						     RAW_QFS_FULL_SIZE_INFORMATION, fsinfo);
313 
314 	case RAW_QFS_OBJECTID_INFORMATION:
315 		return smb_raw_fsinfo_passthru_parse(blob, mem_ctx,
316 						     RAW_QFS_OBJECTID_INFORMATION, fsinfo);
317 	}
318 
319 failed:
320 	return status;
321 }
322 
323 /****************************************************************************
324  Query FSInfo raw interface (sync interface)
325 ****************************************************************************/
smb_raw_fsinfo(struct smbcli_tree * tree,TALLOC_CTX * mem_ctx,union smb_fsinfo * fsinfo)326 NTSTATUS smb_raw_fsinfo(struct smbcli_tree *tree,
327 			TALLOC_CTX *mem_ctx,
328 			union smb_fsinfo *fsinfo)
329 {
330 	struct smbcli_request *req = smb_raw_fsinfo_send(tree, mem_ctx, fsinfo);
331 	return smb_raw_fsinfo_recv(req, mem_ctx, fsinfo);
332 }
333