/src/FreeRDP/channels/rdpdr/client/rdpdr_capabilities.c
Line | Count | Source |
1 | | /** |
2 | | * FreeRDP: A Remote Desktop Protocol Implementation |
3 | | * Device Redirection Virtual Channel |
4 | | * |
5 | | * Copyright 2010-2011 Vic Lee |
6 | | * Copyright 2010-2012 Marc-Andre Moreau <marcandre.moreau@gmail.com> |
7 | | * Copyright 2015-2016 Thincast Technologies GmbH |
8 | | * Copyright 2015 DI (FH) Martin Haimberger <martin.haimberger@thincast.com> |
9 | | * Copyright 2016 Armin Novak <armin.novak@thincast.com> |
10 | | * |
11 | | * Licensed under the Apache License, Version 2.0 (the "License"); |
12 | | * you may not use this file except in compliance with the License. |
13 | | * You may obtain a copy of the License at |
14 | | * |
15 | | * http://www.apache.org/licenses/LICENSE-2.0 |
16 | | * |
17 | | * Unless required by applicable law or agreed to in writing, software |
18 | | * distributed under the License is distributed on an "AS IS" BASIS, |
19 | | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
20 | | * See the License for the specific language governing permissions and |
21 | | * limitations under the License. |
22 | | */ |
23 | | |
24 | | #include <freerdp/config.h> |
25 | | |
26 | | #include <stdio.h> |
27 | | #include <stdlib.h> |
28 | | #include <string.h> |
29 | | |
30 | | #include <winpr/crt.h> |
31 | | #include <winpr/stream.h> |
32 | | |
33 | | #include <freerdp/freerdp.h> |
34 | | #include <freerdp/utils/rdpdr_utils.h> |
35 | | |
36 | | #include "rdpdr_main.h" |
37 | | #include "rdpdr_capabilities.h" |
38 | | |
39 | 0 | #define RDPDR_CAPABILITY_HEADER_LENGTH 8 |
40 | | |
41 | | /* Output device direction general capability set */ |
42 | | static BOOL rdpdr_write_general_capset(rdpdrPlugin* rdpdr, wStream* s) |
43 | 0 | { |
44 | 0 | WINPR_ASSERT(rdpdr); |
45 | 0 | const RDPDR_CAPABILITY_HEADER header = { CAP_GENERAL_TYPE, RDPDR_CAPABILITY_HEADER_LENGTH + 36, |
46 | 0 | GENERAL_CAPABILITY_VERSION_02 }; |
47 | |
|
48 | 0 | const UINT32 ioCode1 = rdpdr->clientIOCode1 & rdpdr->serverIOCode1; |
49 | 0 | const UINT32 ioCode2 = rdpdr->clientIOCode2 & rdpdr->serverIOCode2; |
50 | |
|
51 | 0 | if (rdpdr_write_capset_header(rdpdr->log, s, &header) != CHANNEL_RC_OK) |
52 | 0 | return FALSE; |
53 | | |
54 | 0 | if (!Stream_EnsureRemainingCapacity(s, 36)) |
55 | 0 | return FALSE; |
56 | | |
57 | 0 | Stream_Write_UINT32(s, rdpdr->clientOsType); /* osType, ignored on receipt */ |
58 | 0 | Stream_Write_UINT32(s, rdpdr->clientOsVersion); /* osVersion, unused and must be set to zero */ |
59 | 0 | Stream_Write_UINT16(s, rdpdr->clientVersionMajor); /* protocolMajorVersion, must be set to 1 */ |
60 | 0 | Stream_Write_UINT16(s, rdpdr->clientVersionMinor); /* protocolMinorVersion */ |
61 | 0 | Stream_Write_UINT32(s, ioCode1); /* ioCode1 */ |
62 | 0 | Stream_Write_UINT32(s, ioCode2); /* ioCode2, must be set to zero, reserved for future use */ |
63 | 0 | Stream_Write_UINT32(s, rdpdr->clientExtendedPDU); /* extendedPDU */ |
64 | 0 | Stream_Write_UINT32(s, rdpdr->clientExtraFlags1); /* extraFlags1 */ |
65 | 0 | Stream_Write_UINT32( |
66 | 0 | s, |
67 | 0 | rdpdr->clientExtraFlags2); /* extraFlags2, must be set to zero, reserved for future use */ |
68 | 0 | Stream_Write_UINT32( |
69 | 0 | s, rdpdr->clientSpecialTypeDeviceCap); /* SpecialTypeDeviceCap, number of special devices to |
70 | | be redirected before logon */ |
71 | 0 | return TRUE; |
72 | 0 | } |
73 | | |
74 | | /* Process device direction general capability set */ |
75 | | static UINT rdpdr_process_general_capset(rdpdrPlugin* rdpdr, wStream* s, |
76 | | const RDPDR_CAPABILITY_HEADER* header) |
77 | 0 | { |
78 | 0 | WINPR_ASSERT(header); |
79 | |
|
80 | 0 | const BOOL gotV1 = header->Version == GENERAL_CAPABILITY_VERSION_01; |
81 | 0 | const size_t expect = gotV1 ? 32 : 36; |
82 | 0 | if (header->CapabilityLength != expect) |
83 | 0 | { |
84 | 0 | WLog_Print(rdpdr->log, WLOG_ERROR, |
85 | 0 | "CAP_GENERAL_TYPE::CapabilityLength expected %" PRIuz ", got %" PRIu32, expect, |
86 | 0 | header->CapabilityLength); |
87 | 0 | return ERROR_INVALID_DATA; |
88 | 0 | } |
89 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(rdpdr->log, s, expect)) |
90 | 0 | return ERROR_INVALID_DATA; |
91 | | |
92 | 0 | rdpdr->serverOsType = Stream_Get_UINT32(s); /* osType, ignored on receipt */ |
93 | 0 | rdpdr->serverOsVersion = Stream_Get_UINT32(s); /* osVersion, unused and must be set to zero */ |
94 | 0 | rdpdr->serverVersionMajor = Stream_Get_UINT16(s); /* protocolMajorVersion, must be set to 1 */ |
95 | 0 | rdpdr->serverVersionMinor = Stream_Get_UINT16(s); /* protocolMinorVersion */ |
96 | 0 | rdpdr->serverIOCode1 = Stream_Get_UINT32(s); /* ioCode1 */ |
97 | 0 | rdpdr->serverIOCode2 = |
98 | 0 | Stream_Get_UINT32(s); /* ioCode2, must be set to zero, reserved for future use */ |
99 | 0 | rdpdr->serverExtendedPDU = Stream_Get_UINT32(s); /* extendedPDU */ |
100 | 0 | rdpdr->serverExtraFlags1 = Stream_Get_UINT32(s); /* extraFlags1 */ |
101 | 0 | rdpdr->serverExtraFlags2 = |
102 | 0 | Stream_Get_UINT32(s); /* extraFlags2, must be set to zero, reserved for future use */ |
103 | 0 | if (gotV1) |
104 | 0 | rdpdr->serverSpecialTypeDeviceCap = 0; |
105 | 0 | else |
106 | 0 | rdpdr->serverSpecialTypeDeviceCap = Stream_Get_UINT32( |
107 | 0 | s); /* SpecialTypeDeviceCap, number of special devices to |
108 | | be redirected before logon */ |
109 | 0 | return CHANNEL_RC_OK; |
110 | 0 | } |
111 | | |
112 | | /* Output printer direction capability set */ |
113 | | static BOOL rdpdr_write_printer_capset(rdpdrPlugin* rdpdr, wStream* s) |
114 | 0 | { |
115 | 0 | WINPR_UNUSED(rdpdr); |
116 | 0 | const RDPDR_CAPABILITY_HEADER header = { CAP_PRINTER_TYPE, RDPDR_CAPABILITY_HEADER_LENGTH, |
117 | 0 | PRINT_CAPABILITY_VERSION_01 }; |
118 | 0 | return rdpdr_write_capset_header(rdpdr->log, s, &header) == CHANNEL_RC_OK; |
119 | 0 | } |
120 | | |
121 | | /* Process printer direction capability set */ |
122 | | static UINT rdpdr_process_printer_capset(WINPR_ATTR_UNUSED rdpdrPlugin* rdpdr, wStream* s, |
123 | | const RDPDR_CAPABILITY_HEADER* header) |
124 | 0 | { |
125 | 0 | WINPR_ASSERT(header); |
126 | 0 | if (!Stream_SafeSeek(s, header->CapabilityLength)) |
127 | 0 | return ERROR_BAD_LENGTH; |
128 | 0 | return CHANNEL_RC_OK; |
129 | 0 | } |
130 | | |
131 | | /* Output port redirection capability set */ |
132 | | static BOOL rdpdr_write_port_capset(rdpdrPlugin* rdpdr, wStream* s) |
133 | 0 | { |
134 | 0 | WINPR_UNUSED(rdpdr); |
135 | 0 | const RDPDR_CAPABILITY_HEADER header = { CAP_PORT_TYPE, RDPDR_CAPABILITY_HEADER_LENGTH, |
136 | 0 | PORT_CAPABILITY_VERSION_01 }; |
137 | 0 | return rdpdr_write_capset_header(rdpdr->log, s, &header) == CHANNEL_RC_OK; |
138 | 0 | } |
139 | | |
140 | | /* Process port redirection capability set */ |
141 | | static UINT rdpdr_process_port_capset(WINPR_ATTR_UNUSED rdpdrPlugin* rdpdr, wStream* s, |
142 | | const RDPDR_CAPABILITY_HEADER* header) |
143 | 0 | { |
144 | 0 | WINPR_ASSERT(header); |
145 | 0 | Stream_Seek(s, header->CapabilityLength); |
146 | 0 | return CHANNEL_RC_OK; |
147 | 0 | } |
148 | | |
149 | | /* Output drive redirection capability set */ |
150 | | static BOOL rdpdr_write_drive_capset(rdpdrPlugin* rdpdr, wStream* s) |
151 | 0 | { |
152 | 0 | WINPR_UNUSED(rdpdr); |
153 | 0 | const RDPDR_CAPABILITY_HEADER header = { CAP_DRIVE_TYPE, RDPDR_CAPABILITY_HEADER_LENGTH, |
154 | 0 | DRIVE_CAPABILITY_VERSION_02 }; |
155 | 0 | return rdpdr_write_capset_header(rdpdr->log, s, &header) == CHANNEL_RC_OK; |
156 | 0 | } |
157 | | |
158 | | /* Process drive redirection capability set */ |
159 | | static UINT rdpdr_process_drive_capset(WINPR_ATTR_UNUSED rdpdrPlugin* rdpdr, wStream* s, |
160 | | const RDPDR_CAPABILITY_HEADER* header) |
161 | 0 | { |
162 | 0 | WINPR_ASSERT(header); |
163 | 0 | Stream_Seek(s, header->CapabilityLength); |
164 | 0 | return CHANNEL_RC_OK; |
165 | 0 | } |
166 | | |
167 | | /* Output smart card redirection capability set */ |
168 | | static BOOL rdpdr_write_smartcard_capset(rdpdrPlugin* rdpdr, wStream* s) |
169 | 0 | { |
170 | 0 | WINPR_UNUSED(rdpdr); |
171 | 0 | const RDPDR_CAPABILITY_HEADER header = { CAP_SMARTCARD_TYPE, RDPDR_CAPABILITY_HEADER_LENGTH, |
172 | 0 | SMARTCARD_CAPABILITY_VERSION_01 }; |
173 | 0 | return rdpdr_write_capset_header(rdpdr->log, s, &header) == CHANNEL_RC_OK; |
174 | 0 | } |
175 | | |
176 | | /* Process smartcard redirection capability set */ |
177 | | static UINT rdpdr_process_smartcard_capset(WINPR_ATTR_UNUSED rdpdrPlugin* rdpdr, wStream* s, |
178 | | const RDPDR_CAPABILITY_HEADER* header) |
179 | 0 | { |
180 | 0 | WINPR_ASSERT(header); |
181 | 0 | Stream_Seek(s, header->CapabilityLength); |
182 | 0 | return CHANNEL_RC_OK; |
183 | 0 | } |
184 | | |
185 | | UINT rdpdr_process_capability_request(rdpdrPlugin* rdpdr, wStream* s) |
186 | 0 | { |
187 | 0 | UINT status = CHANNEL_RC_OK; |
188 | 0 | UINT16 numCapabilities = 0; |
189 | |
|
190 | 0 | if (!rdpdr || !s) |
191 | 0 | return CHANNEL_RC_NULL_DATA; |
192 | | |
193 | 0 | if (!rdpdr_state_advance(rdpdr, RDPDR_CHANNEL_STATE_CLIENT_CAPS)) |
194 | 0 | return ERROR_INVALID_STATE; |
195 | | |
196 | 0 | if (!Stream_CheckAndLogRequiredLengthWLog(rdpdr->log, s, 4)) |
197 | 0 | return ERROR_INVALID_DATA; |
198 | | |
199 | 0 | Stream_Read_UINT16(s, numCapabilities); |
200 | 0 | Stream_Seek(s, 2); /* pad (2 bytes) */ |
201 | |
|
202 | 0 | memset(rdpdr->capabilities, 0, sizeof(rdpdr->capabilities)); |
203 | 0 | for (UINT16 i = 0; i < numCapabilities; i++) |
204 | 0 | { |
205 | 0 | RDPDR_CAPABILITY_HEADER header = WINPR_C_ARRAY_INIT; |
206 | 0 | UINT error = rdpdr_read_capset_header(rdpdr->log, s, &header); |
207 | 0 | if (error != CHANNEL_RC_OK) |
208 | 0 | return error; |
209 | | |
210 | 0 | switch (header.CapabilityType) |
211 | 0 | { |
212 | 0 | case CAP_GENERAL_TYPE: |
213 | 0 | rdpdr->capabilities[header.CapabilityType] = TRUE; |
214 | 0 | status = rdpdr_process_general_capset(rdpdr, s, &header); |
215 | 0 | break; |
216 | | |
217 | 0 | case CAP_PRINTER_TYPE: |
218 | 0 | rdpdr->capabilities[header.CapabilityType] = TRUE; |
219 | 0 | status = rdpdr_process_printer_capset(rdpdr, s, &header); |
220 | 0 | break; |
221 | | |
222 | 0 | case CAP_PORT_TYPE: |
223 | 0 | rdpdr->capabilities[header.CapabilityType] = TRUE; |
224 | 0 | status = rdpdr_process_port_capset(rdpdr, s, &header); |
225 | 0 | break; |
226 | | |
227 | 0 | case CAP_DRIVE_TYPE: |
228 | 0 | rdpdr->capabilities[header.CapabilityType] = TRUE; |
229 | 0 | status = rdpdr_process_drive_capset(rdpdr, s, &header); |
230 | 0 | break; |
231 | | |
232 | 0 | case CAP_SMARTCARD_TYPE: |
233 | 0 | rdpdr->capabilities[header.CapabilityType] = TRUE; |
234 | 0 | status = rdpdr_process_smartcard_capset(rdpdr, s, &header); |
235 | 0 | break; |
236 | | |
237 | 0 | default: |
238 | 0 | break; |
239 | 0 | } |
240 | | |
241 | 0 | if (status != CHANNEL_RC_OK) |
242 | 0 | return status; |
243 | 0 | } |
244 | | |
245 | 0 | return CHANNEL_RC_OK; |
246 | 0 | } |
247 | | |
248 | | /** |
249 | | * Function description |
250 | | * |
251 | | * @return 0 on success, otherwise a Win32 error code |
252 | | */ |
253 | | UINT rdpdr_send_capability_response(rdpdrPlugin* rdpdr) |
254 | 0 | { |
255 | 0 | WINPR_ASSERT(rdpdr); |
256 | 0 | WINPR_ASSERT(rdpdr->rdpcontext); |
257 | |
|
258 | 0 | rdpSettings* settings = rdpdr->rdpcontext->settings; |
259 | 0 | WINPR_ASSERT(settings); |
260 | |
|
261 | 0 | wStream* s = StreamPool_Take(rdpdr->pool, 256); |
262 | |
|
263 | 0 | if (!s) |
264 | 0 | { |
265 | 0 | WLog_Print(rdpdr->log, WLOG_ERROR, "Stream_New failed!"); |
266 | 0 | return CHANNEL_RC_NO_MEMORY; |
267 | 0 | } |
268 | | |
269 | 0 | const RDPDR_DEVICE* cdrives = |
270 | 0 | freerdp_device_collection_find_type(settings, RDPDR_DTYP_FILESYSTEM); |
271 | 0 | const RDPDR_DEVICE* cserial = freerdp_device_collection_find_type(settings, RDPDR_DTYP_SERIAL); |
272 | 0 | const RDPDR_DEVICE* cparallel = |
273 | 0 | freerdp_device_collection_find_type(settings, RDPDR_DTYP_PARALLEL); |
274 | 0 | const RDPDR_DEVICE* csmart = |
275 | 0 | freerdp_device_collection_find_type(settings, RDPDR_DTYP_SMARTCARD); |
276 | 0 | const RDPDR_DEVICE* cprinter = freerdp_device_collection_find_type(settings, RDPDR_DTYP_PRINT); |
277 | | |
278 | | /* Only send capabilities the server announced */ |
279 | 0 | const BOOL drives = cdrives && rdpdr->capabilities[CAP_DRIVE_TYPE]; |
280 | 0 | const BOOL serial = cserial && rdpdr->capabilities[CAP_PORT_TYPE]; |
281 | 0 | const BOOL parallel = cparallel && rdpdr->capabilities[CAP_PORT_TYPE]; |
282 | 0 | const BOOL smart = csmart && rdpdr->capabilities[CAP_SMARTCARD_TYPE]; |
283 | 0 | const BOOL printer = cprinter && rdpdr->capabilities[CAP_PRINTER_TYPE]; |
284 | 0 | UINT16 count = 1; |
285 | 0 | if (drives) |
286 | 0 | count++; |
287 | 0 | if (serial || parallel) |
288 | 0 | count++; |
289 | 0 | if (smart) |
290 | 0 | count++; |
291 | 0 | if (printer) |
292 | 0 | count++; |
293 | |
|
294 | 0 | Stream_Write_UINT16(s, RDPDR_CTYP_CORE); |
295 | 0 | Stream_Write_UINT16(s, PAKID_CORE_CLIENT_CAPABILITY); |
296 | 0 | Stream_Write_UINT16(s, count); /* numCapabilities */ |
297 | 0 | Stream_Write_UINT16(s, 0); /* pad */ |
298 | |
|
299 | 0 | if (!rdpdr_write_general_capset(rdpdr, s)) |
300 | 0 | goto fail; |
301 | | |
302 | 0 | if (printer) |
303 | 0 | { |
304 | 0 | if (!rdpdr_write_printer_capset(rdpdr, s)) |
305 | 0 | goto fail; |
306 | 0 | } |
307 | 0 | if (serial || parallel) |
308 | 0 | { |
309 | 0 | if (!rdpdr_write_port_capset(rdpdr, s)) |
310 | 0 | goto fail; |
311 | 0 | } |
312 | 0 | if (drives) |
313 | 0 | { |
314 | 0 | if (!rdpdr_write_drive_capset(rdpdr, s)) |
315 | 0 | goto fail; |
316 | 0 | } |
317 | 0 | if (smart) |
318 | 0 | { |
319 | 0 | if (!rdpdr_write_smartcard_capset(rdpdr, s)) |
320 | 0 | goto fail; |
321 | 0 | } |
322 | 0 | return rdpdr_send(rdpdr, s); |
323 | | |
324 | 0 | fail: |
325 | 0 | Stream_Release(s); |
326 | 0 | return ERROR_OUTOFMEMORY; |
327 | 0 | } |