FreeRDP
Loading...
Searching...
No Matches
info.c
1
22#include <freerdp/config.h>
23
24#include "settings.h"
25
26#include <winpr/crt.h>
27#include <winpr/assert.h>
28
29#include <freerdp/crypto/crypto.h>
30#include <freerdp/log.h>
31#include <freerdp/session.h>
32#include <stdio.h>
33
34#include "timezone.h"
35
36#include "info.h"
37
38#define TAG FREERDP_TAG("core.info")
39
40#define logonInfoV2Size (2u + 4u + 4u + 4u + 4u)
41#define logonInfoV2ReservedSize 558u
42#define logonInfoV2TotalSize (logonInfoV2Size + logonInfoV2ReservedSize)
43
44static const char* INFO_TYPE_LOGON_STRINGS[4] = { "Logon Info V1", "Logon Info V2",
45 "Logon Plain Notify", "Logon Extended Info" };
46
47/* This define limits the length of the strings in the label field. */
48#define MAX_LABEL_LENGTH 40
49struct info_flags_t
50{
51 UINT32 flag;
52 const char* label;
53};
54
55static const struct info_flags_t info_flags[] = {
56 { INFO_MOUSE, "INFO_MOUSE" },
57 { INFO_DISABLECTRLALTDEL, "INFO_DISABLECTRLALTDEL" },
58 { INFO_AUTOLOGON, "INFO_AUTOLOGON" },
59 { INFO_UNICODE, "INFO_UNICODE" },
60 { INFO_MAXIMIZESHELL, "INFO_MAXIMIZESHELL" },
61 { INFO_LOGONNOTIFY, "INFO_LOGONNOTIFY" },
62 { INFO_COMPRESSION, "INFO_COMPRESSION" },
63 { INFO_ENABLEWINDOWSKEY, "INFO_ENABLEWINDOWSKEY" },
64 { INFO_REMOTECONSOLEAUDIO, "INFO_REMOTECONSOLEAUDIO" },
65 { INFO_FORCE_ENCRYPTED_CS_PDU, "INFO_FORCE_ENCRYPTED_CS_PDU" },
66 { INFO_RAIL, "INFO_RAIL" },
67 { INFO_LOGONERRORS, "INFO_LOGONERRORS" },
68 { INFO_MOUSE_HAS_WHEEL, "INFO_MOUSE_HAS_WHEEL" },
69 { INFO_PASSWORD_IS_SC_PIN, "INFO_PASSWORD_IS_SC_PIN" },
70 { INFO_NOAUDIOPLAYBACK, "INFO_NOAUDIOPLAYBACK" },
71 { INFO_USING_SAVED_CREDS, "INFO_USING_SAVED_CREDS" },
72 { INFO_AUDIOCAPTURE, "INFO_AUDIOCAPTURE" },
73 { INFO_VIDEO_DISABLE, "INFO_VIDEO_DISABLE" },
74 { INFO_HIDEF_RAIL_SUPPORTED, "INFO_HIDEF_RAIL_SUPPORTED" },
75};
76
77static BOOL rdp_read_info_null_string(rdpSettings* settings, FreeRDP_Settings_Keys_String id,
78 const char* what, UINT32 flags, wStream* s, size_t cbLen,
79 size_t max)
80{
81 const BOOL unicode = (flags & INFO_UNICODE) != 0;
82
83 if (!freerdp_settings_set_string(settings, id, nullptr))
84 return FALSE;
85
86 if (!Stream_CheckAndLogRequiredLength(TAG, s, (size_t)(cbLen)))
87 return FALSE;
88
89 if (cbLen > 0)
90 {
91 if ((cbLen > max) || (unicode && ((cbLen % 2) != 0)))
92 {
93 WLog_ERR(TAG, "protocol error: %s has invalid value: %" PRIuz "", what, cbLen);
94 return FALSE;
95 }
96
97 if (unicode)
98 {
99 const WCHAR* domain = Stream_PointerAs(s, WCHAR);
100 if (!freerdp_settings_set_string_from_utf16N(settings, id, domain,
101 cbLen / sizeof(WCHAR)))
102 {
103 WLog_ERR(TAG, "protocol error: no data to read for %s [expected %" PRIuz "]", what,
104 cbLen);
105 return FALSE;
106 }
107 }
108 else
109 {
110 const char* domain = Stream_ConstPointer(s);
111 if (!freerdp_settings_set_string_len(settings, id, domain, cbLen))
112 return FALSE;
113 }
114 }
115 Stream_Seek(s, cbLen);
116
117 return TRUE;
118}
119
120static char* rdp_info_package_flags_description(UINT32 flags)
121{
122 char* result = nullptr;
123 size_t maximum_size = 1 + MAX_LABEL_LENGTH * ARRAYSIZE(info_flags);
124
125 result = calloc(maximum_size, sizeof(char));
126
127 if (!result)
128 return nullptr;
129
130 for (size_t i = 0; i < ARRAYSIZE(info_flags); i++)
131 {
132 const struct info_flags_t* cur = &info_flags[i];
133 if (cur->flag & flags)
134 {
135 winpr_str_append(cur->label, result, maximum_size, "|");
136 }
137 }
138
139 return result;
140}
141
142static BOOL rdp_compute_client_auto_reconnect_cookie(rdpRdp* rdp)
143{
144 BYTE ClientRandom[CLIENT_RANDOM_LENGTH] = WINPR_C_ARRAY_INIT;
145 BYTE AutoReconnectRandom[32] = WINPR_C_ARRAY_INIT;
146 ARC_SC_PRIVATE_PACKET* serverCookie = nullptr;
147 ARC_CS_PRIVATE_PACKET* clientCookie = nullptr;
148
149 WINPR_ASSERT(rdp);
150 rdpSettings* settings = rdp->settings;
151 WINPR_ASSERT(settings);
152
153 serverCookie = settings->ServerAutoReconnectCookie;
154 clientCookie = settings->ClientAutoReconnectCookie;
155 clientCookie->cbLen = 28;
156 clientCookie->version = serverCookie->version;
157 clientCookie->logonId = serverCookie->logonId;
158 ZeroMemory(clientCookie->securityVerifier, sizeof(clientCookie->securityVerifier));
159 CopyMemory(AutoReconnectRandom, serverCookie->arcRandomBits,
160 sizeof(serverCookie->arcRandomBits));
161
162 if (settings->SelectedProtocol == PROTOCOL_RDP)
163 CopyMemory(ClientRandom, settings->ClientRandom, settings->ClientRandomLength);
164
165 /* SecurityVerifier = HMAC_MD5(AutoReconnectRandom, ClientRandom) */
166
167 if (!winpr_HMAC(WINPR_MD_MD5, AutoReconnectRandom, 16, ClientRandom, sizeof(ClientRandom),
168 clientCookie->securityVerifier, sizeof(clientCookie->securityVerifier)))
169 return FALSE;
170
171 return TRUE;
172}
173
179static BOOL rdp_read_server_auto_reconnect_cookie(rdpRdp* rdp, wStream* s, logon_info_ex* info)
180{
181 BYTE* p = nullptr;
182 ARC_SC_PRIVATE_PACKET* autoReconnectCookie = nullptr;
183 rdpSettings* settings = rdp->settings;
184 autoReconnectCookie = settings->ServerAutoReconnectCookie;
185
186 if (!Stream_CheckAndLogRequiredLength(TAG, s, 28))
187 return FALSE;
188
189 Stream_Read_UINT32(s, autoReconnectCookie->cbLen); /* cbLen (4 bytes) */
190
191 if (autoReconnectCookie->cbLen != 28)
192 {
193 WLog_ERR(TAG, "ServerAutoReconnectCookie.cbLen != 28");
194 return FALSE;
195 }
196
197 Stream_Read_UINT32(s, autoReconnectCookie->version); /* Version (4 bytes) */
198 Stream_Read_UINT32(s, autoReconnectCookie->logonId); /* LogonId (4 bytes) */
199 Stream_Read(s, autoReconnectCookie->arcRandomBits, 16); /* ArcRandomBits (16 bytes) */
200 p = autoReconnectCookie->arcRandomBits;
201 WLog_DBG(TAG,
202 "ServerAutoReconnectCookie: Version: %" PRIu32 " LogonId: %" PRIu32
203 " SecurityVerifier: "
204 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8
205 "%02" PRIX8 ""
206 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8
207 "%02" PRIX8 "",
208 autoReconnectCookie->version, autoReconnectCookie->logonId, p[0], p[1], p[2], p[3],
209 p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
210 info->LogonId = autoReconnectCookie->logonId;
211 CopyMemory(info->ArcRandomBits, p, 16);
212
213 if ((settings->PrintReconnectCookie))
214 {
215 char* base64 = nullptr;
216 base64 = crypto_base64_encode((BYTE*)autoReconnectCookie, sizeof(ARC_SC_PRIVATE_PACKET));
217 WLog_INFO(TAG, "Reconnect-cookie: %s", base64);
218 free(base64);
219 }
220
221 return TRUE;
222}
223
229static BOOL rdp_read_client_auto_reconnect_cookie(rdpRdp* rdp, wStream* s)
230{
231 ARC_CS_PRIVATE_PACKET* autoReconnectCookie = nullptr;
232 rdpSettings* settings = rdp->settings;
233 autoReconnectCookie = settings->ClientAutoReconnectCookie;
234
235 if (!Stream_CheckAndLogRequiredLength(TAG, s, 28))
236 return FALSE;
237
238 Stream_Read_UINT32(s, autoReconnectCookie->cbLen); /* cbLen (4 bytes) */
239 Stream_Read_UINT32(s, autoReconnectCookie->version); /* version (4 bytes) */
240 Stream_Read_UINT32(s, autoReconnectCookie->logonId); /* LogonId (4 bytes) */
241 Stream_Read(s, autoReconnectCookie->securityVerifier, 16); /* SecurityVerifier */
242 return TRUE;
243}
244
250static BOOL rdp_write_client_auto_reconnect_cookie(rdpRdp* rdp, wStream* s)
251{
252 BYTE* p = nullptr;
253 ARC_CS_PRIVATE_PACKET* autoReconnectCookie = nullptr;
254 rdpSettings* settings = nullptr;
255
256 WINPR_ASSERT(rdp);
257
258 settings = rdp->settings;
259 WINPR_ASSERT(settings);
260
261 autoReconnectCookie = settings->ClientAutoReconnectCookie;
262 WINPR_ASSERT(autoReconnectCookie);
263
264 p = autoReconnectCookie->securityVerifier;
265 WINPR_ASSERT(p);
266
267 WLog_DBG(TAG,
268 "ClientAutoReconnectCookie: Version: %" PRIu32 " LogonId: %" PRIu32 " ArcRandomBits: "
269 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8
270 "%02" PRIX8 ""
271 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8 "%02" PRIX8
272 "%02" PRIX8 "",
273 autoReconnectCookie->version, autoReconnectCookie->logonId, p[0], p[1], p[2], p[3],
274 p[4], p[5], p[6], p[7], p[8], p[9], p[10], p[11], p[12], p[13], p[14], p[15]);
275 if (!Stream_EnsureRemainingCapacity(s, 12ull + 16ull))
276 return FALSE;
277 Stream_Write_UINT32(s, autoReconnectCookie->cbLen); /* cbLen (4 bytes) */
278 Stream_Write_UINT32(s, autoReconnectCookie->version); /* version (4 bytes) */
279 Stream_Write_UINT32(s, autoReconnectCookie->logonId); /* LogonId (4 bytes) */
280 Stream_Write(s, autoReconnectCookie->securityVerifier, 16); /* SecurityVerifier (16 bytes) */
281 return TRUE;
282}
283
284/*
285 * Get the cbClientAddress size limit
286 * see [MS-RDPBCGR] 2.2.1.11.1.1.1 Extended Info Packet (TS_EXTENDED_INFO_PACKET)
287 */
288
289static size_t rdp_get_client_address_max_size(const rdpRdp* rdp)
290{
291 UINT32 version = 0;
292 rdpSettings* settings = nullptr;
293
294 WINPR_ASSERT(rdp);
295
296 settings = rdp->settings;
297 WINPR_ASSERT(settings);
298
299 version = freerdp_settings_get_uint32(settings, FreeRDP_RdpVersion);
300 if (version < RDP_VERSION_10_0)
301 return 64;
302 return 80;
303}
304
310static BOOL rdp_read_extended_info_packet(rdpRdp* rdp, wStream* s)
311{
312 UINT16 clientAddressFamily = 0;
313 UINT16 cbClientAddress = 0;
314 UINT16 cbClientDir = 0;
315 UINT16 cbAutoReconnectLen = 0;
316
317 WINPR_ASSERT(rdp);
318
319 rdpSettings* settings = rdp->settings;
320 WINPR_ASSERT(settings);
321
322 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
323 return FALSE;
324
325 Stream_Read_UINT16(s, clientAddressFamily); /* clientAddressFamily (2 bytes) */
326 Stream_Read_UINT16(s, cbClientAddress); /* cbClientAddress (2 bytes) */
327
328 settings->IPv6Enabled = ((clientAddressFamily == ADDRESS_FAMILY_INET6));
329
330 if (!rdp_read_info_null_string(settings, FreeRDP_ClientAddress, "cbClientAddress", INFO_UNICODE,
331 s, cbClientAddress, rdp_get_client_address_max_size(rdp)))
332 return FALSE;
333
334 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
335 return FALSE;
336
337 Stream_Read_UINT16(s, cbClientDir); /* cbClientDir (2 bytes) */
338
339 /* cbClientDir is the size in bytes of the character data in the clientDir field.
340 * This size includes the length of the mandatory null terminator.
341 * The maximum allowed value is 512 bytes.
342 * Note: Although according to [MS-RDPBCGR 2.2.1.11.1.1.1] the null terminator
343 * is mandatory the Microsoft Android client (starting with version 8.1.31.44)
344 * sets cbClientDir to 0.
345 */
346
347 if (!rdp_read_info_null_string(settings, FreeRDP_ClientDir, "cbClientDir", INFO_UNICODE, s,
348 cbClientDir, 512))
349 return FALSE;
350
356 /* optional: clientTimeZone (172 bytes) */
357 if (Stream_GetRemainingLength(s) == 0)
358 goto end;
359
360 if (!rdp_read_client_time_zone(s, settings))
361 return FALSE;
362
363 /* optional: clientSessionId (4 bytes), should be set to 0 */
364 if (Stream_GetRemainingLength(s) == 0)
365 goto end;
366 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
367 return FALSE;
368
369 Stream_Read_UINT32(s, settings->ClientSessionId);
370
371 /* optional: performanceFlags (4 bytes) */
372 if (Stream_GetRemainingLength(s) == 0)
373 goto end;
374
375 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
376 return FALSE;
377
378 Stream_Read_UINT32(s, settings->PerformanceFlags);
379 freerdp_performance_flags_split(settings);
380
381 /* optional: cbAutoReconnectLen (2 bytes) */
382 if (Stream_GetRemainingLength(s) == 0)
383 goto end;
384
385 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
386 return FALSE;
387
388 Stream_Read_UINT16(s, cbAutoReconnectLen);
389
390 /* optional: autoReconnectCookie (28 bytes) */
391 /* must be present if cbAutoReconnectLen is > 0 */
392 if (cbAutoReconnectLen > 0)
393 {
394 if (!rdp_read_client_auto_reconnect_cookie(rdp, s))
395 return FALSE;
396 }
397
398 /* skip reserved1 and reserved2 fields */
399 if (Stream_GetRemainingLength(s) == 0)
400 goto end;
401
402 if (!Stream_SafeSeek(s, 2))
403 return FALSE;
404
405 if (Stream_GetRemainingLength(s) == 0)
406 goto end;
407
408 if (!Stream_SafeSeek(s, 2))
409 return FALSE;
410
411 if (Stream_GetRemainingLength(s) == 0)
412 goto end;
413
414 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
415 return FALSE;
416
417 if (freerdp_settings_get_bool(settings, FreeRDP_SupportDynamicTimeZone))
418 {
419 UINT16 cbDynamicDSTTimeZoneKeyName = 0;
420
421 Stream_Read_UINT16(s, cbDynamicDSTTimeZoneKeyName);
422
423 if (!rdp_read_info_null_string(settings, FreeRDP_DynamicDSTTimeZoneKeyName,
424 "cbDynamicDSTTimeZoneKeyName", INFO_UNICODE, s,
425 cbDynamicDSTTimeZoneKeyName, 254))
426 return FALSE;
427
428 if (Stream_GetRemainingLength(s) == 0)
429 goto end;
430
431 if (!Stream_CheckAndLogRequiredLength(TAG, s, 2))
432 return FALSE;
433 UINT16 DynamicDaylightTimeDisabled = 0;
434 Stream_Read_UINT16(s, DynamicDaylightTimeDisabled);
435 if (DynamicDaylightTimeDisabled > 1)
436 {
437 WLog_WARN(TAG,
438 "[MS-RDPBCGR] 2.2.1.11.1.1.1 Extended Info Packet "
439 "(TS_EXTENDED_INFO_PACKET)::dynamicDaylightTimeDisabled value %d"
440 " not allowed in [0,1]",
441 settings->DynamicDaylightTimeDisabled);
442 return FALSE;
443 }
444 if (!freerdp_settings_set_bool(settings, FreeRDP_DynamicDaylightTimeDisabled,
445 DynamicDaylightTimeDisabled != 0))
446 return FALSE;
447 DEBUG_TIMEZONE("DynamicTimeZone=%s [%s]",
448 freerdp_settings_get_string(settings, FreeRDP_DynamicDSTTimeZoneKeyName),
449 freerdp_settings_get_bool(settings, FreeRDP_DynamicDaylightTimeDisabled)
450 ? "no-DST"
451 : "DST");
452 }
453
454end:
455 return TRUE;
456}
457
463static BOOL rdp_write_extended_info_packet(rdpRdp* rdp, wStream* s)
464{
465 BOOL ret = FALSE;
466 size_t cbClientAddress = 0;
467 const size_t cbClientAddressMax = rdp_get_client_address_max_size(rdp);
468 WCHAR* clientDir = nullptr;
469 size_t cbClientDir = 0;
470 const size_t cbClientDirMax = 512;
471 UINT16 cbAutoReconnectCookie = 0;
472
473 WINPR_ASSERT(rdp);
474
475 rdpSettings* settings = rdp->settings;
476 WINPR_ASSERT(settings);
477
478 UINT16 clientAddressFamily = ADDRESS_FAMILY_INET;
479 if (settings->ConnectChildSession)
480 clientAddressFamily = 0x0000;
481 else if (settings->IPv6Enabled)
482 clientAddressFamily = ADDRESS_FAMILY_INET6;
483
484 WCHAR* clientAddress = ConvertUtf8ToWCharAlloc(settings->ClientAddress, &cbClientAddress);
485
486 if (cbClientAddress > (UINT16_MAX / sizeof(WCHAR)))
487 {
488 WLog_ERR(TAG, "cbClientAddress > UINT16_MAX");
489 goto fail;
490 }
491
492 if (cbClientAddress > 0)
493 {
494 cbClientAddress = (UINT16)(cbClientAddress + 1) * sizeof(WCHAR);
495 if (cbClientAddress > cbClientAddressMax)
496 {
497 WLog_WARN(TAG,
498 "the client address %s [%" PRIuz "] exceeds the limit of %" PRIuz
499 ", truncating.",
500 settings->ClientAddress, cbClientAddress, cbClientAddressMax);
501
502 clientAddress[(cbClientAddressMax / sizeof(WCHAR)) - 1] = '\0';
503 cbClientAddress = cbClientAddressMax;
504 }
505 }
506
507 clientDir = ConvertUtf8ToWCharAlloc(settings->ClientDir, &cbClientDir);
508 if (cbClientDir > (UINT16_MAX / sizeof(WCHAR)))
509 {
510 WLog_ERR(TAG, "cbClientDir > UINT16_MAX");
511 goto fail;
512 }
513
514 if (cbClientDir > 0)
515 {
516 cbClientDir = (UINT16)(cbClientDir + 1) * sizeof(WCHAR);
517 if (cbClientDir > cbClientDirMax)
518 {
519 WLog_WARN(TAG,
520 "the client dir %s [%" PRIuz "] exceeds the limit of %" PRIuz ", truncating.",
521 settings->ClientDir, cbClientDir, cbClientDirMax);
522
523 clientDir[(cbClientDirMax / sizeof(WCHAR)) - 1] = '\0';
524 cbClientDir = cbClientDirMax;
525 }
526 }
527
528 if (settings->ServerAutoReconnectCookie->cbLen > UINT16_MAX)
529 {
530 WLog_ERR(TAG, "ServerAutoreconnectCookie::cbLen > UINT16_MAX");
531 goto fail;
532 }
533
534 cbAutoReconnectCookie = (UINT16)settings->ServerAutoReconnectCookie->cbLen;
535
536 if (!Stream_EnsureRemainingCapacity(s, 4ull + cbClientAddress + 2ull + cbClientDir))
537 goto fail;
538
539 Stream_Write_UINT16(s, clientAddressFamily); /* clientAddressFamily (2 bytes) */
540 Stream_Write_UINT16(s, (UINT16)cbClientAddress); /* cbClientAddress (2 bytes) */
541
542 Stream_Write(s, clientAddress, cbClientAddress); /* clientAddress */
543
544 Stream_Write_UINT16(s, (UINT16)cbClientDir); /* cbClientDir (2 bytes) */
545
546 Stream_Write(s, clientDir, cbClientDir); /* clientDir */
547
548 if (!rdp_write_client_time_zone(s, settings)) /* clientTimeZone (172 bytes) */
549 goto fail;
550
551 if (!Stream_EnsureRemainingCapacity(s, 10ull))
552 goto fail;
553
554 /* clientSessionId (4 bytes), should be set to 0 */
555 Stream_Write_UINT32(s, settings->ClientSessionId);
556 freerdp_performance_flags_make(settings);
557 Stream_Write_UINT32(s, settings->PerformanceFlags); /* performanceFlags (4 bytes) */
558 Stream_Write_UINT16(s, cbAutoReconnectCookie); /* cbAutoReconnectCookie (2 bytes) */
559
560 if (cbAutoReconnectCookie > 0)
561 {
562 if (!rdp_compute_client_auto_reconnect_cookie(rdp))
563 goto fail;
564 if (!rdp_write_client_auto_reconnect_cookie(rdp, s)) /* autoReconnectCookie */
565 goto fail;
566 }
567
568 if (freerdp_settings_get_bool(settings, FreeRDP_SupportDynamicTimeZone))
569 {
570 if (!Stream_EnsureRemainingCapacity(s, 8 + 254 * sizeof(WCHAR)))
571 goto fail;
572
573 Stream_Write_UINT16(s, 0); /* reserved1 (2 bytes) */
574 Stream_Write_UINT16(s, 0); /* reserved2 (2 bytes) */
575
576 size_t rlen = 0;
577 const char* tz = freerdp_settings_get_string(settings, FreeRDP_DynamicDSTTimeZoneKeyName);
578 if (tz)
579 rlen = strnlen(tz, 254);
580 Stream_Write_UINT16(s, (UINT16)rlen * sizeof(WCHAR));
581 if (Stream_Write_UTF16_String_From_UTF8(s, rlen, tz, rlen, FALSE) < 0)
582 goto fail;
583 Stream_Write_UINT16(s, settings->DynamicDaylightTimeDisabled ? 0x01 : 0x00);
584 }
585
586 ret = TRUE;
587fail:
588 free(clientAddress);
589 free(clientDir);
590 return ret;
591}
592
593static BOOL rdp_read_info_string(rdpSettings* settings, FreeRDP_Settings_Keys_String id,
594 UINT32 flags, wStream* s, size_t cbLenNonNull, size_t max)
595{
596 union
597 {
598 char c;
599 WCHAR w;
600 BYTE b[2];
601 } terminator;
602
603 const BOOL unicode = (flags & INFO_UNICODE) != 0;
604 const size_t nullSize = unicode ? sizeof(WCHAR) : sizeof(CHAR);
605
606 if (!freerdp_settings_set_string(settings, id, nullptr))
607 return FALSE;
608
609 if (!Stream_CheckAndLogRequiredLength(TAG, s, (size_t)(cbLenNonNull + nullSize)))
610 return FALSE;
611
612 if (cbLenNonNull > 0)
613 {
614 /* cbDomain is the size in bytes of the character data in the Domain field.
615 * This size excludes (!) the length of the mandatory null terminator.
616 * Maximum value including the mandatory null terminator: 512
617 */
618 if ((cbLenNonNull % 2) || (cbLenNonNull > (max - nullSize)))
619 {
620 WLog_ERR(TAG, "protocol error: invalid value: %" PRIuz "", cbLenNonNull);
621 return FALSE;
622 }
623
624 if (unicode)
625 {
626 const WCHAR* domain = Stream_PointerAs(s, WCHAR);
627 if (!freerdp_settings_set_string_from_utf16N(settings, id, domain,
628 cbLenNonNull / sizeof(WCHAR)))
629 return FALSE;
630 }
631 else
632 {
633 const char* domain = Stream_PointerAs(s, char);
634 if (!freerdp_settings_set_string_len(settings, id, domain, cbLenNonNull))
635 return FALSE;
636 }
637 }
638
639 Stream_Seek(s, cbLenNonNull);
640
641 terminator.w = L'\0';
642 Stream_Read(s, terminator.b, nullSize);
643
644 if (terminator.w != L'\0')
645 {
646 WLog_ERR(TAG, "protocol error: Domain must be null terminated");
647 if (!freerdp_settings_set_string(settings, id, nullptr))
648 WLog_ERR(TAG, "freerdp_settings_set_string(settings, id=%d, nullptr) failed", id);
649
650 return FALSE;
651 }
652
653 return TRUE;
654}
655
661static BOOL rdp_read_info_packet(rdpRdp* rdp, wStream* s, UINT16 tpktlength)
662{
663 BOOL smallsize = FALSE;
664 UINT32 flags = 0;
665 UINT16 cbDomain = 0;
666 UINT16 cbUserName = 0;
667 UINT16 cbPassword = 0;
668 UINT16 cbAlternateShell = 0;
669 UINT16 cbWorkingDir = 0;
670 UINT32 CompressionLevel = 0;
671 rdpSettings* settings = rdp->settings;
672
673 if (!Stream_CheckAndLogRequiredLengthWLog(rdp->log, s, 18))
674 return FALSE;
675
676 Stream_Read_UINT32(s, settings->KeyboardCodePage); /* CodePage (4 bytes ) */
677 Stream_Read_UINT32(s, flags); /* flags (4 bytes) */
678 settings->AudioCapture = ((flags & INFO_AUDIOCAPTURE) != 0);
679 settings->AudioPlayback = (!(flags & INFO_NOAUDIOPLAYBACK));
680 settings->AutoLogonEnabled = ((flags & INFO_AUTOLOGON) != 0);
681 settings->RemoteApplicationMode = ((flags & INFO_RAIL) != 0);
682 settings->HiDefRemoteApp = ((flags & INFO_HIDEF_RAIL_SUPPORTED) != 0);
683 settings->RemoteConsoleAudio = ((flags & INFO_REMOTECONSOLEAUDIO) != 0);
684 settings->CompressionEnabled = ((flags & INFO_COMPRESSION) != 0);
685 settings->LogonNotify = ((flags & INFO_LOGONNOTIFY) != 0);
686 settings->MouseHasWheel = ((flags & INFO_MOUSE_HAS_WHEEL) != 0);
687 settings->DisableCtrlAltDel = ((flags & INFO_DISABLECTRLALTDEL) != 0);
688 settings->ForceEncryptedCsPdu = ((flags & INFO_FORCE_ENCRYPTED_CS_PDU) != 0);
689 settings->PasswordIsSmartcardPin = ((flags & INFO_PASSWORD_IS_SC_PIN) != 0);
690
691 if (flags & INFO_COMPRESSION)
692 {
693 CompressionLevel = ((flags & 0x00001E00) >> 9);
694 settings->CompressionLevel = CompressionLevel;
695 }
696 else
697 {
698 settings->CompressionLevel = 0;
699 }
700
701 /* RDP 4 and 5 have smaller credential limits */
702 if (settings->RdpVersion < RDP_VERSION_5_PLUS)
703 smallsize = TRUE;
704
705 Stream_Read_UINT16(s, cbDomain); /* cbDomain (2 bytes) */
706 Stream_Read_UINT16(s, cbUserName); /* cbUserName (2 bytes) */
707 Stream_Read_UINT16(s, cbPassword); /* cbPassword (2 bytes) */
708 Stream_Read_UINT16(s, cbAlternateShell); /* cbAlternateShell (2 bytes) */
709 Stream_Read_UINT16(s, cbWorkingDir); /* cbWorkingDir (2 bytes) */
710
711 if (!rdp_read_info_string(settings, FreeRDP_Domain, flags, s, cbDomain, smallsize ? 52 : 512))
712 return FALSE;
713
714 if (!rdp_read_info_string(settings, FreeRDP_Username, flags, s, cbUserName,
715 smallsize ? 44 : 512))
716 return FALSE;
717
718 if (!rdp_read_info_string(settings, FreeRDP_Password, flags, s, cbPassword,
719 smallsize ? 32 : 512))
720 return FALSE;
721
722 if (!rdp_read_info_string(settings, FreeRDP_AlternateShell, flags, s, cbAlternateShell, 512))
723 return FALSE;
724
725 if (!rdp_read_info_string(settings, FreeRDP_ShellWorkingDirectory, flags, s, cbWorkingDir, 512))
726 return FALSE;
727
728 if (settings->RdpVersion >= RDP_VERSION_5_PLUS)
729 {
730 if (!rdp_read_extended_info_packet(rdp, s)) /* extraInfo */
731 return FALSE;
732 }
733
734 const size_t xrem = Stream_GetRemainingLength(s);
735 if (!tpkt_ensure_stream_consumed(rdp->log, s, tpktlength))
736 Stream_Seek(s, xrem);
737 return TRUE;
738}
739
745static BOOL rdp_write_info_packet(rdpRdp* rdp, wStream* s)
746{
747 BOOL ret = FALSE;
748 UINT32 flags = 0;
749 WCHAR* domainW = nullptr;
750 size_t cbDomain = 0;
751 WCHAR* userNameW = nullptr;
752 size_t cbUserName = 0;
753 WCHAR* passwordW = nullptr;
754 size_t cbPassword = 0;
755 WCHAR* alternateShellW = nullptr;
756 size_t cbAlternateShell = 0;
757 WCHAR* workingDirW = nullptr;
758 size_t cbWorkingDir = 0;
759 BOOL usedPasswordCookie = FALSE;
760 rdpSettings* settings = nullptr;
761
762 WINPR_ASSERT(rdp);
763 settings = rdp->settings;
764 WINPR_ASSERT(settings);
765
766 flags = INFO_MOUSE | INFO_UNICODE | INFO_LOGONERRORS | INFO_MAXIMIZESHELL |
767 INFO_ENABLEWINDOWSKEY | INFO_DISABLECTRLALTDEL | INFO_MOUSE_HAS_WHEEL |
768 INFO_FORCE_ENCRYPTED_CS_PDU;
769
770 if (settings->SmartcardLogon)
771 {
772 flags |= INFO_AUTOLOGON;
773 flags |= INFO_PASSWORD_IS_SC_PIN;
774 }
775
776 if (settings->AudioCapture)
777 flags |= INFO_AUDIOCAPTURE;
778
779 if (!settings->AudioPlayback)
780 flags |= INFO_NOAUDIOPLAYBACK;
781
782 if (settings->VideoDisable)
783 flags |= INFO_VIDEO_DISABLE;
784
785 if (settings->AutoLogonEnabled)
786 flags |= INFO_AUTOLOGON;
787
788 if (settings->RemoteApplicationMode)
789 {
790 if (settings->HiDefRemoteApp)
791 {
792 if (settings->RdpVersion >= RDP_VERSION_5_PLUS)
793 flags |= INFO_HIDEF_RAIL_SUPPORTED;
794 }
795
796 flags |= INFO_RAIL;
797 }
798
799 if (settings->RemoteConsoleAudio)
800 flags |= INFO_REMOTECONSOLEAUDIO;
801
802 if (settings->CompressionEnabled)
803 {
804 flags |= INFO_COMPRESSION;
805 flags |= ((settings->CompressionLevel << 9) & 0x00001E00);
806 }
807
808 if (settings->LogonNotify)
809 flags |= INFO_LOGONNOTIFY;
810
811 if (settings->PasswordIsSmartcardPin)
812 flags |= INFO_PASSWORD_IS_SC_PIN;
813
814 {
815 char* flags_description = rdp_info_package_flags_description(flags);
816
817 if (flags_description)
818 {
819 WLog_DBG(TAG, "Client Info Packet Flags = %s", flags_description);
820 free(flags_description);
821 }
822 }
823
824 domainW = freerdp_settings_get_string_as_utf16(settings, FreeRDP_Domain, &cbDomain);
825 if (cbDomain > UINT16_MAX / sizeof(WCHAR))
826 {
827 WLog_ERR(TAG, "cbDomain > UINT16_MAX");
828 goto fail;
829 }
830 cbDomain *= sizeof(WCHAR);
831
832 /* user name provided by the expert for connecting to the novice computer */
833 userNameW = freerdp_settings_get_string_as_utf16(settings, FreeRDP_Username, &cbUserName);
834 if (cbUserName > UINT16_MAX / sizeof(WCHAR))
835 {
836 WLog_ERR(TAG, "cbUserName > UINT16_MAX");
837 goto fail;
838 }
839 cbUserName *= sizeof(WCHAR);
840
841 {
842 const char* pin = "*";
843 if (!settings->RemoteAssistanceMode)
844 {
845 /* Ignore redirection password if we´re using smartcard and have the pin as password */
846 if (((flags & INFO_PASSWORD_IS_SC_PIN) == 0) && settings->RedirectionPassword &&
847 (settings->RedirectionPasswordLength > 0))
848 {
849 union
850 {
851 BYTE* bp;
852 WCHAR* wp;
853 } ptrconv;
854
855 if (settings->RedirectionPasswordLength > UINT16_MAX)
856 {
857 WLog_ERR(TAG, "RedirectionPasswordLength > UINT16_MAX");
858 goto fail;
859 }
860 usedPasswordCookie = TRUE;
861
862 ptrconv.bp = settings->RedirectionPassword;
863 passwordW = ptrconv.wp;
864 cbPassword = (UINT16)settings->RedirectionPasswordLength;
865 }
866 else
867 pin = freerdp_settings_get_string(settings, FreeRDP_Password);
868 }
869
870 if (!usedPasswordCookie && pin)
871 {
872 passwordW = ConvertUtf8ToWCharAlloc(pin, &cbPassword);
873 if (cbPassword > UINT16_MAX / sizeof(WCHAR))
874 {
875 WLog_ERR(TAG, "cbPassword > UINT16_MAX");
876 goto fail;
877 }
878 cbPassword = (UINT16)cbPassword * sizeof(WCHAR);
879 }
880 }
881
882 {
883 const char* altShell = nullptr;
884 if (!settings->RemoteAssistanceMode)
885 altShell = freerdp_settings_get_string(settings, FreeRDP_AlternateShell);
886 else if (settings->RemoteAssistancePassStub)
887 altShell = "*"; /* This field MUST be filled with "*" */
888 else
889 altShell = freerdp_settings_get_string(settings, FreeRDP_RemoteAssistancePassword);
890
891 if (altShell && strlen(altShell) > 0)
892 {
893 alternateShellW = ConvertUtf8ToWCharAlloc(altShell, &cbAlternateShell);
894 if (!alternateShellW)
895 {
896 WLog_ERR(TAG, "alternateShellW == nullptr");
897 goto fail;
898 }
899 if (cbAlternateShell > (UINT16_MAX / sizeof(WCHAR)))
900 {
901 WLog_ERR(TAG, "cbAlternateShell > UINT16_MAX");
902 goto fail;
903 }
904 cbAlternateShell = (UINT16)cbAlternateShell * sizeof(WCHAR);
905 }
906 }
907
908 {
909 FreeRDP_Settings_Keys_String inputId = FreeRDP_RemoteAssistanceSessionId;
910 if (!freerdp_settings_get_bool(settings, FreeRDP_RemoteAssistanceMode))
911 inputId = FreeRDP_ShellWorkingDirectory;
912
913 workingDirW = freerdp_settings_get_string_as_utf16(settings, inputId, &cbWorkingDir);
914 }
915 if (cbWorkingDir > (UINT16_MAX / sizeof(WCHAR)))
916 {
917 WLog_ERR(TAG, "cbWorkingDir > UINT16_MAX");
918 goto fail;
919 }
920 cbWorkingDir = (UINT16)cbWorkingDir * sizeof(WCHAR);
921
922 if (!Stream_EnsureRemainingCapacity(s, 18ull + cbDomain + cbUserName + cbPassword +
923 cbAlternateShell + cbWorkingDir + 5 * sizeof(WCHAR)))
924 goto fail;
925
926 Stream_Write_UINT32(s, settings->KeyboardCodePage); /* CodePage (4 bytes) */
927 Stream_Write_UINT32(s, flags); /* flags (4 bytes) */
928 Stream_Write_UINT16(s, (UINT16)cbDomain); /* cbDomain (2 bytes) */
929 Stream_Write_UINT16(s, (UINT16)cbUserName); /* cbUserName (2 bytes) */
930 Stream_Write_UINT16(s, (UINT16)cbPassword); /* cbPassword (2 bytes) */
931 Stream_Write_UINT16(s, (UINT16)cbAlternateShell); /* cbAlternateShell (2 bytes) */
932 Stream_Write_UINT16(s, (UINT16)cbWorkingDir); /* cbWorkingDir (2 bytes) */
933
934 Stream_Write(s, domainW, cbDomain);
935
936 /* the mandatory null terminator */
937 Stream_Write_UINT16(s, 0);
938
939 Stream_Write(s, userNameW, cbUserName);
940
941 /* the mandatory null terminator */
942 Stream_Write_UINT16(s, 0);
943
944 Stream_Write(s, passwordW, cbPassword);
945
946 /* the mandatory null terminator */
947 Stream_Write_UINT16(s, 0);
948
949 Stream_Write(s, alternateShellW, cbAlternateShell);
950
951 /* the mandatory null terminator */
952 Stream_Write_UINT16(s, 0);
953
954 Stream_Write(s, workingDirW, cbWorkingDir);
955
956 /* the mandatory null terminator */
957 Stream_Write_UINT16(s, 0);
958 ret = TRUE;
959fail:
960 free(domainW);
961 free(userNameW);
962 free(alternateShellW);
963 free(workingDirW);
964
965 if (!usedPasswordCookie)
966 free(passwordW);
967
968 if (!ret)
969 return FALSE;
970
971 if (settings->RdpVersion >= RDP_VERSION_5_PLUS)
972 ret = rdp_write_extended_info_packet(rdp, s); /* extraInfo */
973
974 return ret;
975}
976
984BOOL rdp_recv_client_info(rdpRdp* rdp, wStream* s)
985{
986 UINT16 length = 0;
987 UINT16 channelId = 0;
988 UINT16 securityFlags = 0;
989
990 WINPR_ASSERT(rdp_get_state(rdp) == CONNECTION_STATE_SECURE_SETTINGS_EXCHANGE);
991
992 if (!rdp_read_header(rdp, s, &length, &channelId))
993 return FALSE;
994
995 if (!rdp_read_security_header(rdp, s, &securityFlags, &length))
996 return FALSE;
997
998 if ((securityFlags & SEC_INFO_PKT) == 0)
999 return FALSE;
1000
1001 if (rdp->settings->UseRdpSecurityLayer)
1002 {
1003 if (securityFlags & SEC_REDIRECTION_PKT)
1004 {
1005 WLog_ERR(TAG, "Error: SEC_REDIRECTION_PKT unsupported");
1006 return FALSE;
1007 }
1008
1009 if (securityFlags & SEC_ENCRYPT)
1010 {
1011 if (!rdp_decrypt(rdp, s, &length, securityFlags))
1012 return FALSE;
1013 }
1014 }
1015
1016 return rdp_read_info_packet(rdp, s, length);
1017}
1018
1025BOOL rdp_send_client_info(rdpRdp* rdp)
1026{
1027 UINT16 sec_flags = SEC_INFO_PKT;
1028 wStream* s = nullptr;
1029 WINPR_ASSERT(rdp);
1030 s = rdp_send_stream_init(rdp, &sec_flags);
1031
1032 if (!s)
1033 {
1034 WLog_ERR(TAG, "Stream_New failed!");
1035 return FALSE;
1036 }
1037
1038 if (!rdp_write_info_packet(rdp, s))
1039 {
1040 Stream_Release(s);
1041 return FALSE;
1042 }
1043 return rdp_send(rdp, s, MCS_GLOBAL_CHANNEL_ID, sec_flags);
1044}
1045
1046static void rdp_free_logon_info(logon_info* info)
1047{
1048 if (!info)
1049 return;
1050 free(info->domain);
1051 free(info->username);
1052
1053 const logon_info empty = WINPR_C_ARRAY_INIT;
1054 *info = empty;
1055}
1056
1057static BOOL rdp_info_read_string(const char* what, wStream* s, size_t size, size_t max,
1058 BOOL skipMax, char** dst)
1059{
1060 WINPR_ASSERT(dst);
1061 *dst = nullptr;
1062
1063 if (size == 0)
1064 {
1065 if (skipMax)
1066 return Stream_SafeSeek(s, max);
1067 return TRUE;
1068 }
1069
1070 if (((size % sizeof(WCHAR)) != 0) || (size > max))
1071 {
1072 WLog_ERR(TAG, "protocol error: invalid %s value: %" PRIuz "", what, size);
1073 return FALSE;
1074 }
1075
1076 const WCHAR* str = Stream_ConstPointer(s);
1077 if (!Stream_SafeSeek(s, skipMax ? max : size))
1078 return FALSE;
1079
1080 if (str[size / sizeof(WCHAR) - 1])
1081 {
1082 WLog_ERR(TAG, "protocol error: %s must be null terminated", what);
1083 return FALSE;
1084 }
1085
1086 size_t len = 0;
1087 char* rc = ConvertWCharNToUtf8Alloc(str, size / sizeof(WCHAR), &len);
1088 if (!rc)
1089 {
1090 WLog_ERR(TAG, "failed to convert the %s string", what);
1091 free(rc);
1092 return FALSE;
1093 }
1094
1095 *dst = rc;
1096 return TRUE;
1097}
1098
1099static BOOL rdp_recv_logon_info_v1(rdpRdp* rdp, wStream* s, logon_info* info)
1100{
1101 UINT32 cbDomain = 0;
1102 UINT32 cbUserName = 0;
1103
1104 WINPR_UNUSED(rdp);
1105 WINPR_ASSERT(info);
1106
1107 if (!Stream_CheckAndLogRequiredLength(TAG, s, 576))
1108 return FALSE;
1109
1110 Stream_Read_UINT32(s, cbDomain); /* cbDomain (4 bytes) */
1111
1112 /* cbDomain is the size of the Unicode character data (including the mandatory
1113 * null terminator) in bytes present in the fixed-length (52 bytes) Domain field
1114 */
1115 if (!rdp_info_read_string("Domain", s, cbDomain, 52, TRUE, &info->domain))
1116 goto fail;
1117
1118 Stream_Read_UINT32(s, cbUserName); /* cbUserName (4 bytes) */
1119
1120 /* cbUserName is the size of the Unicode character data (including the mandatory
1121 * null terminator) in bytes present in the fixed-length (512 bytes) UserName field.
1122 */
1123 if (!rdp_info_read_string("UserName", s, cbUserName, 512, TRUE, &info->username))
1124 goto fail;
1125
1126 Stream_Read_UINT32(s, info->sessionId); /* SessionId (4 bytes) */
1127 WLog_DBG(TAG, "LogonInfoV1: SessionId: 0x%08" PRIX32 " UserName: [%s] Domain: [%s]",
1128 info->sessionId, info->username, info->domain);
1129 return TRUE;
1130fail:
1131 return FALSE;
1132}
1133
1134static BOOL rdp_recv_logon_info_v2(rdpRdp* rdp, wStream* s, logon_info* info)
1135{
1136 UINT16 Version = 0;
1137 UINT32 Size = 0;
1138 UINT32 cbDomain = 0;
1139 UINT32 cbUserName = 0;
1140
1141 WINPR_ASSERT(rdp);
1142 WINPR_ASSERT(s);
1143 WINPR_ASSERT(info);
1144
1145 WINPR_UNUSED(rdp);
1146
1147 if (!Stream_CheckAndLogRequiredLength(TAG, s, logonInfoV2TotalSize))
1148 return FALSE;
1149
1150 Stream_Read_UINT16(s, Version); /* Version (2 bytes) */
1151 if (Version != SAVE_SESSION_PDU_VERSION_ONE)
1152 {
1153 WLog_WARN(TAG, "LogonInfoV2::Version expected %d bytes, got %" PRIu16,
1154 SAVE_SESSION_PDU_VERSION_ONE, Version);
1155 return FALSE;
1156 }
1157
1158 Stream_Read_UINT32(s, Size); /* Size (4 bytes) */
1159
1160 /* [MS-RDPBCGR] 2.2.10.1.1.2 Logon Info Version 2 (TS_LOGON_INFO_VERSION_2)
1161 * should be logonInfoV2TotalSize
1162 * but even MS server 2019 sends logonInfoV2Size
1163 */
1164 if (Size != logonInfoV2TotalSize)
1165 {
1166 if (Size != logonInfoV2Size)
1167 {
1168 WLog_WARN(TAG, "LogonInfoV2::Size expected %" PRIu32 " bytes, got %" PRIu32,
1169 logonInfoV2TotalSize, Size);
1170 return FALSE;
1171 }
1172 }
1173
1174 Stream_Read_UINT32(s, info->sessionId); /* SessionId (4 bytes) */
1175 Stream_Read_UINT32(s, cbDomain); /* cbDomain (4 bytes) */
1176 Stream_Read_UINT32(s, cbUserName); /* cbUserName (4 bytes) */
1177 Stream_Seek(s, logonInfoV2ReservedSize); /* pad (558 bytes) */
1178
1179 /* cbDomain is the size in bytes of the Unicode character data in the Domain field.
1180 * The size of the mandatory null terminator is include in this value.
1181 * Note: Since MS-RDPBCGR 2.2.10.1.1.2 does not mention any size limits we assume
1182 * that the maximum value is 52 bytes, according to the fixed size of the
1183 * Domain field in the Logon Info Version 1 (TS_LOGON_INFO) structure.
1184 */
1185 if (!rdp_info_read_string("Domain", s, cbDomain, 52, FALSE, &info->domain))
1186 goto fail;
1187
1188 /* cbUserName is the size in bytes of the Unicode character data in the UserName field.
1189 * The size of the mandatory null terminator is include in this value.
1190 * Note: Since MS-RDPBCGR 2.2.10.1.1.2 does not mention any size limits we assume
1191 * that the maximum value is 512 bytes, according to the fixed size of the
1192 * Username field in the Logon Info Version 1 (TS_LOGON_INFO) structure.
1193 */
1194 if (!rdp_info_read_string("UserName", s, cbUserName, 512, FALSE, &info->username))
1195 goto fail;
1196
1197 /* We´ve seen undocumented padding with windows 11 here.
1198 * unless it has actual data in it ignore it.
1199 * if there is unexpected data, print a warning and dump the contents
1200 */
1201 {
1202 const size_t rem = Stream_GetRemainingLength(s);
1203 if (rem > 0)
1204 {
1205 BOOL warn = FALSE;
1206 const char* str = Stream_ConstPointer(s);
1207 for (size_t x = 0; x < rem; x++)
1208 {
1209 if (str[x] != '\0')
1210 warn = TRUE;
1211 }
1212 if (warn)
1213 {
1214 WLog_WARN(TAG, "unexpected padding of %" PRIuz " bytes, data not '\\0'", rem);
1215 winpr_HexDump(TAG, WLOG_TRACE, str, rem);
1216 }
1217
1218 if (!Stream_SafeSeek(s, rem))
1219 goto fail;
1220 }
1221 }
1222
1223 WLog_DBG(TAG, "LogonInfoV2: SessionId: 0x%08" PRIX32 " UserName: [%s] Domain: [%s]",
1224 info->sessionId, info->username, info->domain);
1225 return TRUE;
1226fail:
1227 return FALSE;
1228}
1229
1230static BOOL rdp_recv_logon_plain_notify(rdpRdp* rdp, wStream* s)
1231{
1232 WINPR_UNUSED(rdp);
1233 if (!Stream_CheckAndLogRequiredLength(TAG, s, 576))
1234 return FALSE;
1235
1236 Stream_Seek(s, 576); /* pad (576 bytes) */
1237 WLog_DBG(TAG, "LogonPlainNotify");
1238 return TRUE;
1239}
1240
1241static BOOL rdp_recv_logon_error_info(rdpRdp* rdp, wStream* s, logon_info_ex* info)
1242{
1243 freerdp* instance = nullptr;
1244 UINT32 errorNotificationType = 0;
1245 UINT32 errorNotificationData = 0;
1246
1247 WINPR_ASSERT(rdp);
1248 WINPR_ASSERT(rdp->context);
1249 WINPR_ASSERT(s);
1250 WINPR_ASSERT(info);
1251
1252 instance = rdp->context->instance;
1253 WINPR_ASSERT(instance);
1254
1255 if (!Stream_CheckAndLogRequiredLength(TAG, s, 8))
1256 return FALSE;
1257
1258 Stream_Read_UINT32(s, errorNotificationType); /* errorNotificationType (4 bytes) */
1259 Stream_Read_UINT32(s, errorNotificationData); /* errorNotificationData (4 bytes) */
1260 WLog_DBG(TAG, "LogonErrorInfo: Data: 0x%08" PRIX32 " Type: 0x%08" PRIX32 "",
1261 errorNotificationData, errorNotificationType);
1262 if (instance->LogonErrorInfo)
1263 {
1264 const int rc =
1265 instance->LogonErrorInfo(instance, errorNotificationData, errorNotificationType);
1266 if (rc < 0)
1267 return FALSE;
1268 }
1269 info->ErrorNotificationType = errorNotificationType;
1270 info->ErrorNotificationData = errorNotificationData;
1271 return TRUE;
1272}
1273
1274static BOOL rdp_recv_logon_info_extended(rdpRdp* rdp, wStream* s, logon_info_ex* info)
1275{
1276 UINT32 cbFieldData = 0;
1277 UINT32 fieldsPresent = 0;
1278 UINT16 Length = 0;
1279
1280 WINPR_ASSERT(rdp);
1281 WINPR_ASSERT(s);
1282 WINPR_ASSERT(info);
1283
1284 if (!Stream_CheckAndLogRequiredLength(TAG, s, 6))
1285 {
1286 WLog_WARN(TAG, "received short logon info extended, need 6 bytes, got %" PRIuz,
1287 Stream_GetRemainingLength(s));
1288 return FALSE;
1289 }
1290
1291 Stream_Read_UINT16(s, Length); /* Length (2 bytes) */
1292 Stream_Read_UINT32(s, fieldsPresent); /* fieldsPresent (4 bytes) */
1293
1294 if ((Length < 6) || (!Stream_CheckAndLogRequiredLength(TAG, s, (Length - 6U))))
1295 {
1296 WLog_WARN(TAG,
1297 "received short logon info extended, need %" PRIu16 " - 6 bytes, got %" PRIuz,
1298 Length, Stream_GetRemainingLength(s));
1299 return FALSE;
1300 }
1301
1302 WLog_DBG(TAG, "LogonInfoExtended: fieldsPresent: 0x%08" PRIX32 "", fieldsPresent);
1303
1304 /* logonFields */
1305
1306 if (fieldsPresent & LOGON_EX_AUTORECONNECTCOOKIE)
1307 {
1308 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1309 return FALSE;
1310
1311 info->haveCookie = TRUE;
1312 Stream_Read_UINT32(s, cbFieldData); /* cbFieldData (4 bytes) */
1313
1314 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbFieldData))
1315 return FALSE;
1316
1317 if (!rdp_read_server_auto_reconnect_cookie(rdp, s, info))
1318 return FALSE;
1319 }
1320
1321 if (fieldsPresent & LOGON_EX_LOGONERRORS)
1322 {
1323 info->haveErrorInfo = TRUE;
1324
1325 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1326 return FALSE;
1327
1328 Stream_Read_UINT32(s, cbFieldData); /* cbFieldData (4 bytes) */
1329
1330 if (!Stream_CheckAndLogRequiredLength(TAG, s, cbFieldData))
1331 return FALSE;
1332
1333 if (!rdp_recv_logon_error_info(rdp, s, info))
1334 return FALSE;
1335 }
1336
1337 if (!Stream_CheckAndLogRequiredLength(TAG, s, 570))
1338 return FALSE;
1339
1340 Stream_Seek(s, 570); /* pad (570 bytes) */
1341 return TRUE;
1342}
1343
1344BOOL rdp_recv_save_session_info(rdpRdp* rdp, wStream* s)
1345{
1346 UINT32 infoType = 0;
1347 BOOL status = 0;
1348 logon_info logonInfo = WINPR_C_ARRAY_INIT;
1349 logon_info_ex logonInfoEx = WINPR_C_ARRAY_INIT;
1350 rdpContext* context = rdp->context;
1351 rdpUpdate* update = rdp->context->update;
1352
1353 if (!Stream_CheckAndLogRequiredLength(TAG, s, 4))
1354 return FALSE;
1355
1356 Stream_Read_UINT32(s, infoType); /* infoType (4 bytes) */
1357
1358 switch (infoType)
1359 {
1360 case INFO_TYPE_LOGON:
1361 status = rdp_recv_logon_info_v1(rdp, s, &logonInfo);
1362
1363 if (status && update->SaveSessionInfo)
1364 status = update->SaveSessionInfo(context, infoType, &logonInfo);
1365
1366 rdp_free_logon_info(&logonInfo);
1367 break;
1368
1369 case INFO_TYPE_LOGON_LONG:
1370 status = rdp_recv_logon_info_v2(rdp, s, &logonInfo);
1371
1372 if (status && update->SaveSessionInfo)
1373 status = update->SaveSessionInfo(context, infoType, &logonInfo);
1374
1375 rdp_free_logon_info(&logonInfo);
1376 break;
1377
1378 case INFO_TYPE_LOGON_PLAIN_NOTIFY:
1379 status = rdp_recv_logon_plain_notify(rdp, s);
1380
1381 if (status && update->SaveSessionInfo)
1382 status = update->SaveSessionInfo(context, infoType, nullptr);
1383
1384 break;
1385
1386 case INFO_TYPE_LOGON_EXTENDED_INF:
1387 status = rdp_recv_logon_info_extended(rdp, s, &logonInfoEx);
1388
1389 if (status && update->SaveSessionInfo)
1390 status = update->SaveSessionInfo(context, infoType, &logonInfoEx);
1391
1392 break;
1393
1394 default:
1395 WLog_ERR(TAG, "Unhandled saveSessionInfo type 0x%" PRIx32 "", infoType);
1396 status = TRUE;
1397 break;
1398 }
1399
1400 if (!status)
1401 {
1402 WLog_DBG(TAG, "SaveSessionInfo error: infoType: %s (%" PRIu32 ")",
1403 infoType < 4 ? INFO_TYPE_LOGON_STRINGS[infoType % 4] : "Unknown", infoType);
1404 }
1405
1406 return status;
1407}
1408
1409static BOOL rdp_write_logon_info_v1(wStream* s, logon_info* info)
1410{
1411 const size_t charLen = 52 / sizeof(WCHAR);
1412 const size_t userCharLen = 512 / sizeof(WCHAR);
1413
1414 size_t sz = 4 + 52 + 4 + 512 + 4;
1415
1416 if (!Stream_EnsureRemainingCapacity(s, sz))
1417 return FALSE;
1418
1419 /* domain */
1420 {
1421 WINPR_ASSERT(info);
1422 if (!info->domain || !info->username)
1423 return FALSE;
1424 const size_t len = strnlen(info->domain, charLen + 1);
1425 if (len > charLen)
1426 return FALSE;
1427
1428 const size_t wlen = len * sizeof(WCHAR);
1429 if (wlen > UINT32_MAX)
1430 return FALSE;
1431
1432 Stream_Write_UINT32(s, (UINT32)wlen);
1433 if (Stream_Write_UTF16_String_From_UTF8(s, charLen, info->domain, len, TRUE) < 0)
1434 return FALSE;
1435 }
1436
1437 /* username */
1438 {
1439 const size_t len = strnlen(info->username, userCharLen + 1);
1440 if (len > userCharLen)
1441 return FALSE;
1442
1443 const size_t wlen = len * sizeof(WCHAR);
1444 if (wlen > UINT32_MAX)
1445 return FALSE;
1446
1447 Stream_Write_UINT32(s, (UINT32)wlen);
1448
1449 if (Stream_Write_UTF16_String_From_UTF8(s, userCharLen, info->username, len, TRUE) < 0)
1450 return FALSE;
1451 }
1452
1453 /* sessionId */
1454 Stream_Write_UINT32(s, info->sessionId);
1455 return TRUE;
1456}
1457
1458static BOOL rdp_write_logon_info_v2(wStream* s, const logon_info* info)
1459{
1460 size_t domainLen = 0;
1461 size_t usernameLen = 0;
1462
1463 if (!Stream_EnsureRemainingCapacity(s, logonInfoV2TotalSize))
1464 return FALSE;
1465
1466 Stream_Write_UINT16(s, SAVE_SESSION_PDU_VERSION_ONE);
1467 /* [MS-RDPBCGR] 2.2.10.1.1.2 Logon Info Version 2 (TS_LOGON_INFO_VERSION_2)
1468 * should be logonInfoV2TotalSize
1469 * but even MS server 2019 sends logonInfoV2Size
1470 */
1471 Stream_Write_UINT32(s, logonInfoV2Size);
1472 Stream_Write_UINT32(s, info->sessionId);
1473 if (info->domain)
1474 domainLen = strnlen(info->domain, 256); /* lmcons.h UNLEN */
1475 if (domainLen >= UINT32_MAX / sizeof(WCHAR))
1476 return FALSE;
1477 Stream_Write_UINT32(s, (UINT32)(domainLen + 1) * sizeof(WCHAR));
1478
1479 if (info->username)
1480 usernameLen = strnlen(info->username, 256); /* lmcons.h UNLEN */
1481 if (usernameLen >= UINT32_MAX / sizeof(WCHAR))
1482 return FALSE;
1483 Stream_Write_UINT32(s, (UINT32)(usernameLen + 1) * sizeof(WCHAR));
1484 Stream_Seek(s, logonInfoV2ReservedSize);
1485 if (Stream_Write_UTF16_String_From_UTF8(s, domainLen + 1, info->domain, domainLen, TRUE) < 0)
1486 return FALSE;
1487 if (Stream_Write_UTF16_String_From_UTF8(s, usernameLen + 1, info->username, usernameLen, TRUE) <
1488 0)
1489 return FALSE;
1490 return TRUE;
1491}
1492
1493static BOOL rdp_write_logon_info_plain(wStream* s)
1494{
1495 if (!Stream_EnsureRemainingCapacity(s, 576))
1496 return FALSE;
1497
1498 Stream_Seek(s, 576);
1499 return TRUE;
1500}
1501
1502static BOOL rdp_write_logon_info_ex(wStream* s, logon_info_ex* info)
1503{
1504 UINT32 FieldsPresent = 0;
1505 UINT16 Size = 2 + 4 + 570;
1506
1507 if (info->haveCookie)
1508 {
1509 FieldsPresent |= LOGON_EX_AUTORECONNECTCOOKIE;
1510 Size += 28;
1511 }
1512
1513 if (info->haveErrorInfo)
1514 {
1515 FieldsPresent |= LOGON_EX_LOGONERRORS;
1516 Size += 8;
1517 }
1518
1519 if (!Stream_EnsureRemainingCapacity(s, Size))
1520 return FALSE;
1521
1522 Stream_Write_UINT16(s, Size);
1523 Stream_Write_UINT32(s, FieldsPresent);
1524
1525 if (info->haveCookie)
1526 {
1527 Stream_Write_UINT32(s, 28); /* cbFieldData (4 bytes) */
1528 Stream_Write_UINT32(s, 28); /* cbLen (4 bytes) */
1529 Stream_Write_UINT32(s, AUTO_RECONNECT_VERSION_1); /* Version (4 bytes) */
1530 Stream_Write_UINT32(s, info->LogonId); /* LogonId (4 bytes) */
1531 Stream_Write(s, info->ArcRandomBits, 16); /* ArcRandomBits (16 bytes) */
1532 }
1533
1534 if (info->haveErrorInfo)
1535 {
1536 Stream_Write_UINT32(s, 8); /* cbFieldData (4 bytes) */
1537 Stream_Write_UINT32(s, info->ErrorNotificationType); /* ErrorNotificationType (4 bytes) */
1538 Stream_Write_UINT32(s, info->ErrorNotificationData); /* ErrorNotificationData (4 bytes) */
1539 }
1540
1541 Stream_Seek(s, 570);
1542 return TRUE;
1543}
1544
1545BOOL rdp_send_save_session_info(rdpContext* context, UINT32 type, void* data)
1546{
1547 UINT16 sec_flags = 0;
1548 BOOL status = 0;
1549
1550 WINPR_ASSERT(context);
1551 rdpRdp* rdp = context->rdp;
1552 wStream* s = rdp_data_pdu_init(rdp, &sec_flags);
1553
1554 if (!s)
1555 return FALSE;
1556
1557 Stream_Write_UINT32(s, type);
1558
1559 switch (type)
1560 {
1561 case INFO_TYPE_LOGON:
1562 status = rdp_write_logon_info_v1(s, (logon_info*)data);
1563 break;
1564
1565 case INFO_TYPE_LOGON_LONG:
1566 status = rdp_write_logon_info_v2(s, (logon_info*)data);
1567 break;
1568
1569 case INFO_TYPE_LOGON_PLAIN_NOTIFY:
1570 status = rdp_write_logon_info_plain(s);
1571 break;
1572
1573 case INFO_TYPE_LOGON_EXTENDED_INF:
1574 status = rdp_write_logon_info_ex(s, (logon_info_ex*)data);
1575 break;
1576
1577 default:
1578 WLog_ERR(TAG, "saveSessionInfo type 0x%" PRIx32 " not handled", type);
1579 status = FALSE;
1580 break;
1581 }
1582
1583 if (status)
1584 status =
1585 rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_SAVE_SESSION_INFO, rdp->mcs->userId, sec_flags);
1586 else
1587 Stream_Release(s);
1588
1589 return status;
1590}
1591
1592BOOL rdp_send_server_status_info(rdpContext* context, UINT32 status)
1593{
1594 UINT16 sec_flags = 0;
1595 wStream* s = nullptr;
1596 rdpRdp* rdp = context->rdp;
1597 s = rdp_data_pdu_init(rdp, &sec_flags);
1598
1599 if (!s)
1600 return FALSE;
1601
1602 Stream_Write_UINT32(s, status);
1603 return rdp_send_data_pdu(rdp, s, DATA_PDU_TYPE_STATUS_INFO, rdp->mcs->userId, sec_flags);
1604}
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_string_from_utf16N(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const WCHAR *param, size_t length)
Sets a string settings value. The param is converted to UTF-8 and the copy stored.
WINPR_ATTR_NODISCARD FREERDP_API const char * freerdp_settings_get_string(const rdpSettings *settings, FreeRDP_Settings_Keys_String id)
Returns a immutable string settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL val)
Sets a BOOL settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_string_len(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const char *val, size_t len)
Sets a string settings value. The val is copied.
WINPR_ATTR_NODISCARD FREERDP_API UINT32 freerdp_settings_get_uint32(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id)
Returns a UINT32 settings value.
FREERDP_API WCHAR * freerdp_settings_get_string_as_utf16(const rdpSettings *settings, FreeRDP_Settings_Keys_String id, size_t *pCharLen)
Return an allocated UTF16 string.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_set_string(rdpSettings *settings, FreeRDP_Settings_Keys_String id, const char *val)
Sets a string settings value. The param is copied.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.