22#include <freerdp/config.h>
24#include <winpr/windows.h>
25#include <winpr/library.h>
28#include <winpr/assert.h>
35#include <winpr/assert.h>
39#ifdef WITH_PROGRESS_BAR
43#ifdef WITH_WINDOWS_CERT_STORE
47#include <freerdp/log.h>
48#include <freerdp/freerdp.h>
49#include <freerdp/constants.h>
50#include <freerdp/settings.h>
52#include <freerdp/locale/locale.h>
53#include <freerdp/locale/keyboard.h>
54#include <freerdp/codec/region.h>
55#include <freerdp/client/cmdline.h>
56#include <freerdp/client/channels.h>
57#include <freerdp/channels/channels.h>
61#include "wf_channels.h"
62#include "wf_graphics.h"
64#include "resource/resource.h"
66#define TAG CLIENT_TAG("windows")
68#define WM_FREERDP_SHOWWINDOW (WM_USER + 100)
70static BOOL wf_has_console(
void)
72#ifdef WITH_WIN_CONSOLE
73 int file = _fileno(stdin);
74 int tty = _isatty(file);
80 WLog_INFO(TAG,
"Detected stdin=%d -> %s mode", file, tty ?
"console" :
"gui");
84static BOOL wf_end_paint(rdpContext* context)
86 RECT updateRect = { 0 };
91 WINPR_ASSERT(context);
93 wfContext* wfc = (wfContext*)context;
94 rdpGdi* gdi = context->gdi;
97 HGDI_DC hdc = gdi->primary->hdc;
103 WINPR_ASSERT(hwnd->invalid || (hwnd->ninvalid == 0));
105 if (hwnd->invalid->null)
108 const int ninvalid = hwnd->ninvalid;
109 const HGDI_RGN cinvalid = hwnd->cinvalid;
114 region16_init(&invalidRegion);
116 for (
int i = 0; i < ninvalid; i++)
118 invalidRect.left = cinvalid[i].x;
119 invalidRect.top = cinvalid[i].y;
120 invalidRect.right = cinvalid[i].x + cinvalid[i].w;
121 invalidRect.bottom = cinvalid[i].y + cinvalid[i].h;
122 region16_union_rect(&invalidRegion, &invalidRegion, &invalidRect);
125 if (!region16_is_empty(&invalidRegion))
127 extents = region16_extents(&invalidRegion);
128 updateRect.left = extents->left;
129 updateRect.top = extents->top;
130 updateRect.right = extents->right;
131 updateRect.bottom = extents->bottom;
133 wf_scale_rect(wfc, &updateRect);
135 InvalidateRect(wfc->hwnd, &updateRect, FALSE);
138 wf_rail_invalidate_region(wfc, &invalidRegion);
141 region16_uninit(&invalidRegion);
145 wfc->is_shown = TRUE;
147#ifdef WITH_PROGRESS_BAR
148 if (wfc->taskBarList)
150 wfc->taskBarList->lpVtbl->SetProgressState(wfc->taskBarList, wfc->hwnd,
155 PostMessage(wfc->hwnd, WM_FREERDP_SHOWWINDOW, 0, 0);
156 WLog_INFO(TAG,
"Window is shown!");
161static BOOL wf_begin_paint(rdpContext* context)
165 if (!context || !context->gdi || !context->gdi->primary || !context->gdi->primary->hdc)
168 hdc = context->gdi->primary->hdc;
170 if (!hdc || !hdc->hwnd || !hdc->hwnd->invalid)
173 hdc->hwnd->invalid->null = TRUE;
174 hdc->hwnd->ninvalid = 0;
178static BOOL wf_desktop_resize(rdpContext* context)
182 rdpSettings* settings;
183 wfContext* wfc = (wfContext*)context;
185 if (!context || !context->settings)
188 settings = context->settings;
192 same = (wfc->primary == wfc->drawing) ? TRUE : FALSE;
193 wf_image_free(wfc->primary);
197 context->gdi->dstFormat, NULL);
202 context->gdi->dstFormat, wfc->primary->pdata, NULL))
206 wfc->drawing = wfc->primary;
208 if (wfc->fullscreen != TRUE)
211 SetWindowPos(wfc->hwnd, HWND_TOP, -1, -1,
218 wf_update_offset(wfc);
219 GetWindowRect(wfc->hwnd, &rect);
220 InvalidateRect(wfc->hwnd, &rect, TRUE);
226static BOOL wf_pre_connect(freerdp* instance)
228 WINPR_ASSERT(instance);
229 WINPR_ASSERT(instance->context);
230 WINPR_ASSERT(instance->context->settings);
232 rdpContext* context = instance->context;
233 wfContext* wfc = (wfContext*)instance->context;
234 rdpSettings* settings = context->settings;
244 if (wfc->percentscreen > 0)
246 desktopWidth = (GetSystemMetrics(SM_CXSCREEN) * wfc->percentscreen) / 100;
249 desktopHeight = (GetSystemMetrics(SM_CYSCREEN) * wfc->percentscreen) / 100;
258 desktopWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN);
259 desktopHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN);
263 desktopWidth = GetSystemMetrics(SM_CXSCREEN);
264 desktopHeight = GetSystemMetrics(SM_CYSCREEN);
270 desktopWidth = (desktopWidth + 3) & (~3);
287 CHAR name[KL_NAMELENGTH + 1] = { 0 };
288 if (GetKeyboardLayoutNameA(name))
293 rc = strtoul(name, NULL, 16);
295 keyboardLayoutId = rc;
298 if (keyboardLayoutId == 0)
300 const HKL layout = GetKeyboardLayout(0);
301 const uint32_t masked = (uint32_t)(((uintptr_t)layout >> 16) & 0xFFFF);
302 keyboardLayoutId = masked;
306 if (keyboardLayoutId == 0)
307 freerdp_detect_keyboard_layout_from_system_locale(&keyboardLayoutId);
308 if (keyboardLayoutId == 0)
309 keyboardLayoutId = ENGLISH_UNITED_STATES;
312 PubSub_SubscribeChannelConnected(instance->context->pubSub, wf_OnChannelConnectedEventHandler);
313 PubSub_SubscribeChannelDisconnected(instance->context->pubSub,
314 wf_OnChannelDisconnectedEventHandler);
318static void wf_append_item_to_system_menu(HMENU hMenu, UINT fMask, UINT wID,
const wchar_t* text,
321 MENUITEMINFO item_info = { 0 };
322 item_info.fMask = fMask;
323 item_info.cbSize =
sizeof(MENUITEMINFO);
325 item_info.fType = MFT_STRING;
326 item_info.dwTypeData = _wcsdup(text);
327 item_info.cch = (UINT)_wcslen(text);
329 item_info.dwItemData = (ULONG_PTR)wfc;
330 InsertMenuItem(hMenu, wfc->systemMenuInsertPosition++, TRUE, &item_info);
333static void wf_add_system_menu(wfContext* wfc)
337 if (wfc->fullscreen && !wfc->fullscreen_toggle)
347 hMenu = GetSystemMenu(wfc->hwnd, FALSE);
349 wf_append_item_to_system_menu(hMenu,
350 MIIM_CHECKMARKS | MIIM_FTYPE | MIIM_ID | MIIM_STRING | MIIM_DATA,
351 SYSCOMMAND_ID_SMARTSIZING, L
"Smart sizing", wfc);
355 CheckMenuItem(hMenu, SYSCOMMAND_ID_SMARTSIZING, MF_CHECKED);
359 wf_append_item_to_system_menu(hMenu, MIIM_FTYPE | MIIM_ID | MIIM_STRING,
360 SYSCOMMAND_ID_REQUEST_CONTROL, L
"Request control", wfc);
363static WCHAR* wf_window_get_title(rdpSettings* settings)
366 WCHAR* windowTitle = NULL;
368 WCHAR prefix[] = L
"FreeRDP:";
380 size = strlen(name) + 16 + wcslen(prefix);
381 windowTitle = calloc(size,
sizeof(WCHAR));
387 _snwprintf_s(windowTitle, size, _TRUNCATE, L
"%s %S", prefix, name);
389 _snwprintf_s(windowTitle, size, _TRUNCATE, L
"%s %S:%u", prefix, name,
395static BOOL wf_post_connect(freerdp* instance)
402 rdpSettings* settings;
403 EmbedWindowEventArgs e;
404 const UINT32 format = PIXEL_FORMAT_BGRX32;
406 WINPR_ASSERT(instance);
408 context = instance->context;
409 WINPR_ASSERT(context);
411 settings = context->settings;
412 WINPR_ASSERT(settings);
414 wfc = (wfContext*)instance->context;
423 WLog_ERR(TAG,
"Failed to allocate primary surface");
427 if (!gdi_init_ex(instance, format, 0, wfc->primary->pdata, NULL))
430 cache = instance->context->cache;
433 gdi = instance->context->gdi;
437 wf_gdi_register_update_callbacks(context->update);
440 wfc->window_title = wf_window_get_title(settings);
442 if (!wfc->window_title)
454 dwStyle = WS_CHILD | WS_BORDER;
457 WS_CAPTION | WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX | WS_SIZEBOX | WS_MAXIMIZEBOX;
461 wfc->hwnd = CreateWindowEx(0, wfc->wndClassName, wfc->window_title, dwStyle, 0, 0, 0, 0,
462 wfc->hWndParent, NULL, wfc->hInstance, NULL);
465 WLog_ERR(TAG,
"CreateWindowEx failed with error: %lu", GetLastError());
468 SetWindowLongPtr(wfc->hwnd, GWLP_USERDATA, (LONG_PTR)wfc);
471 wf_resize_window(wfc);
472 wf_add_system_menu(wfc);
475 wfc->drawing = wfc->primary;
476 EventArgsInit(&e,
"wfreerdp");
478 e.handle = (
void*)wfc->hwnd;
479 PubSub_OnEmbedWindow(context->pubSub, context, &e);
480#ifdef WITH_PROGRESS_BAR
481 if (wfc->taskBarList)
483 ShowWindow(wfc->hwnd, SW_SHOWMINIMIZED);
484 wfc->taskBarList->lpVtbl->SetProgressState(wfc->taskBarList, wfc->hwnd, TBPF_INDETERMINATE);
487 UpdateWindow(wfc->hwnd);
488 context->update->BeginPaint = wf_begin_paint;
489 context->update->DesktopResize = wf_desktop_resize;
490 context->update->EndPaint = wf_end_paint;
491 context->update->SetKeyboardIndicators = wf_keyboard_set_indicators;
492 wf_register_pointer(context->graphics);
494 wfc->floatbar = wf_floatbar_new(wfc, wfc->hInstance,
497 wf_event_focus_in(wfc);
502static void wf_post_disconnect(freerdp* instance)
506 if (!instance || !instance->context)
509 wfc = (wfContext*)instance->context;
510 free(wfc->window_title);
513static CREDUI_INFOW wfUiInfo = {
sizeof(CREDUI_INFOW), NULL, L
"Enter your credentials",
514 L
"Remote Desktop Security", NULL };
516static BOOL wf_authenticate_ex(freerdp* instance,
char** username,
char** password,
char** domain,
517 rdp_auth_reason reason)
523 WCHAR UserNameW[CREDUI_MAX_USERNAME_LENGTH + 1] = { 0 };
524 WCHAR UserW[CREDUI_MAX_USERNAME_LENGTH + 1] = { 0 };
525 WCHAR DomainW[CREDUI_MAX_DOMAIN_TARGET_LENGTH + 1] = { 0 };
526 WCHAR PasswordW[CREDUI_MAX_PASSWORD_LENGTH + 1] = { 0 };
528 WINPR_ASSERT(instance);
529 WINPR_ASSERT(instance->context);
530 WINPR_ASSERT(instance->context->settings);
532 wfc = (wfContext*)instance->context;
535 WINPR_ASSERT(username);
536 WINPR_ASSERT(domain);
537 WINPR_ASSERT(password);
539 const WCHAR auth[] = L
"Target credentials requested";
540 const WCHAR authPin[] = L
"PIN requested";
541 const WCHAR gwAuth[] = L
"Gateway credentials requested";
542 const WCHAR* titleW = auth;
545 dwFlags = CREDUI_FLAGS_DO_NOT_PERSIST | CREDUI_FLAGS_EXCLUDE_CERTIFICATES |
546 CREDUI_FLAGS_USERNAME_TARGET_CREDENTIALS;
553 if ((*username) && (*password))
556 case AUTH_SMARTCARD_PIN:
557 dwFlags &= ~CREDUI_FLAGS_USERNAME_TARGET_CREDENTIALS;
558 dwFlags |= CREDUI_FLAGS_PASSWORD_ONLY_OK | CREDUI_FLAGS_KEEP_USERNAME;
563 *username = _strdup(
"PIN");
576 (void)ConvertUtf8ToWChar(*username, UserNameW, ARRAYSIZE(UserNameW));
577 (void)ConvertUtf8ToWChar(*username, UserW, ARRAYSIZE(UserW));
581 (void)ConvertUtf8ToWChar(*password, PasswordW, ARRAYSIZE(PasswordW));
584 (void)ConvertUtf8ToWChar(*domain, DomainW, ARRAYSIZE(DomainW));
586 if (_wcsnlen(PasswordW, ARRAYSIZE(PasswordW)) == 0)
588 if (!wfc->isConsole &&
590 WLog_ERR(TAG,
"Flag for stdin read present but stdin is redirected; using GUI");
591 if (wfc->isConsole &&
593 status = CredUICmdLinePromptForCredentialsW(titleW, NULL, 0, UserNameW,
594 ARRAYSIZE(UserNameW), PasswordW,
595 ARRAYSIZE(PasswordW), &fSave, dwFlags);
597 status = CredUIPromptForCredentialsW(&wfUiInfo, titleW, NULL, 0, UserNameW,
598 ARRAYSIZE(UserNameW), PasswordW,
599 ARRAYSIZE(PasswordW), &fSave, dwFlags);
600 if (status != NO_ERROR)
602 WLog_ERR(TAG,
"CredUIPromptForCredentials unexpected status: 0x%08lX", status);
606 if ((dwFlags & CREDUI_FLAGS_KEEP_USERNAME) == 0)
608 status = CredUIParseUserNameW(UserNameW, UserW, ARRAYSIZE(UserW), DomainW,
610 if (status != NO_ERROR)
612 CHAR User[CREDUI_MAX_USERNAME_LENGTH + 1] = { 0 };
613 CHAR UserName[CREDUI_MAX_USERNAME_LENGTH + 1] = { 0 };
614 CHAR Domain[CREDUI_MAX_DOMAIN_TARGET_LENGTH + 1] = { 0 };
616 (void)ConvertWCharNToUtf8(UserNameW, ARRAYSIZE(UserNameW), UserName,
617 ARRAYSIZE(UserName));
618 (void)ConvertWCharNToUtf8(UserW, ARRAYSIZE(UserW), User, ARRAYSIZE(User));
619 (void)ConvertWCharNToUtf8(DomainW, ARRAYSIZE(DomainW), Domain, ARRAYSIZE(Domain));
620 WLog_ERR(TAG,
"Failed to parse UserName: %s into User: %s Domain: %s", UserName,
627 *username = ConvertWCharNToUtf8Alloc(UserW, ARRAYSIZE(UserW), NULL);
630 WLog_ERR(TAG,
"ConvertWCharNToUtf8Alloc failed", status);
634 if (_wcsnlen(DomainW, ARRAYSIZE(DomainW)) > 0)
635 *domain = ConvertWCharNToUtf8Alloc(DomainW, ARRAYSIZE(DomainW), NULL);
637 *domain = _strdup(
"\0");
642 WLog_ERR(TAG,
"strdup failed", status);
646 *password = ConvertWCharNToUtf8Alloc(PasswordW, ARRAYSIZE(PasswordW), NULL);
657static WCHAR* wf_format_text(
const WCHAR* fmt, ...)
661 WCHAR* buffer = NULL;
668 rc = _vsnwprintf(buffer, size, fmt, ap);
673 if ((
size_t)rc < size)
676 size = (size_t)rc + 1;
677 tmp = realloc(buffer, size *
sizeof(WCHAR));
689#ifdef WITH_WINDOWS_CERT_STORE
697static void wf_report_error(
char* wszMessage, DWORD dwErrCode)
699 LPSTR pwszMsgBuf = NULL;
701 if (NULL != wszMessage && 0 != *wszMessage)
703 WLog_ERR(TAG,
"%s", wszMessage);
706 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
711 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
721 if (NULL != pwszMsgBuf)
723 WLog_ERR(TAG,
"Error: 0x%08x (%d) %s", dwErrCode, dwErrCode, pwszMsgBuf);
724 LocalFree(pwszMsgBuf);
728 WLog_ERR(TAG,
"Error: 0x%08x (%d)", dwErrCode, dwErrCode);
732static DWORD wf_is_x509_certificate_trusted(
const char* common_name,
const char* subject,
733 const char* issuer,
const char* fingerprint)
735 HRESULT hr = CRYPT_E_NOT_FOUND;
737 DWORD dwChainFlags = CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT;
739 HCERTCHAINENGINE hChainEngine = NULL;
740 PCCERT_CHAIN_CONTEXT pChainContext = NULL;
742 CERT_ENHKEY_USAGE EnhkeyUsage = { 0 };
743 CERT_USAGE_MATCH CertUsage = { 0 };
744 CERT_CHAIN_PARA ChainPara = { 0 };
745 CERT_CHAIN_POLICY_PARA ChainPolicy = { 0 };
746 CERT_CHAIN_POLICY_STATUS PolicyStatus = { 0 };
747 CERT_CHAIN_ENGINE_CONFIG EngineConfig = { 0 };
749 DWORD derPubKeyLen = WINPR_ASSERTING_INT_CAST(uint32_t, strlen(fingerprint));
750 char* derPubKey = calloc(derPubKeyLen,
sizeof(
char));
751 if (NULL == derPubKey)
753 WLog_ERR(TAG,
"Could not allocate derPubKey");
760 if (!CryptStringToBinaryA(fingerprint, 0, CRYPT_STRING_BASE64HEADER, derPubKey, &derPubKeyLen,
763 WLog_ERR(TAG,
"CryptStringToBinary failed. Err: %d", GetLastError());
770 EnhkeyUsage.cUsageIdentifier = 0;
771 EnhkeyUsage.rgpszUsageIdentifier = NULL;
773 CertUsage.dwType = USAGE_MATCH_TYPE_AND;
774 CertUsage.Usage = EnhkeyUsage;
776 ChainPara.cbSize =
sizeof(ChainPara);
777 ChainPara.RequestedUsage = CertUsage;
779 ChainPolicy.cbSize =
sizeof(ChainPolicy);
781 PolicyStatus.cbSize =
sizeof(PolicyStatus);
783 EngineConfig.cbSize =
sizeof(EngineConfig);
784 EngineConfig.dwUrlRetrievalTimeout = 0;
786 pCert = CertCreateCertificateContext(X509_ASN_ENCODING, derPubKey, derPubKeyLen);
789 WLog_ERR(TAG,
"FAILED: Certificate could not be parsed.");
793 dwChainFlags |= CERT_CHAIN_ENABLE_PEER_TRUST;
802 if (!CertCreateCertificateChainEngine(&EngineConfig, &hChainEngine))
804 hr = HRESULT_FROM_WIN32(GetLastError());
811 if (!CertGetCertificateChain(hChainEngine,
822 hr = HRESULT_FROM_WIN32(GetLastError());
829 if (!CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_BASE,
834 hr = HRESULT_FROM_WIN32(GetLastError());
838 if (PolicyStatus.dwError != S_OK)
840 wf_report_error(
"CertVerifyCertificateChainPolicy: Chain Status", PolicyStatus.dwError);
841 hr = PolicyStatus.dwError;
847 if (PolicyStatus.dwError == CRYPT_E_NO_REVOCATION_CHECK ||
848 PolicyStatus.dwError == CRYPT_E_REVOCATION_OFFLINE)
856 WLog_INFO(TAG,
"CertVerifyCertificateChainPolicy succeeded for %s (%s) issued by %s",
857 common_name, subject, issuer);
864 WLog_INFO(TAG,
"CertVerifyCertificateChainPolicy failed for %s (%s) issued by %s",
865 common_name, subject, issuer);
866 wf_report_error(NULL, hr);
871 if (NULL != pChainContext)
873 CertFreeCertificateChain(pChainContext);
876 if (NULL != hChainEngine)
878 CertFreeCertificateChainEngine(hChainEngine);
883 CertFreeCertificateContext(pCert);
890static DWORD wf_cli_verify_certificate_ex(freerdp* instance,
const char* host, UINT16 port,
891 const char* common_name,
const char* subject,
892 const char* issuer,
const char* fingerprint, DWORD flags)
894#ifdef WITH_WINDOWS_CERT_STORE
895 if (flags & VERIFY_CERT_FLAG_FP_IS_PEM && !(flags & VERIFY_CERT_FLAG_MISMATCH))
897 if (wf_is_x509_certificate_trusted(common_name, subject, issuer, fingerprint) == S_OK)
904 return client_cli_verify_certificate_ex(instance, host, port, common_name, subject, issuer,
908static DWORD wf_verify_certificate_ex(freerdp* instance,
const char* host, UINT16 port,
909 const char* common_name,
const char* subject,
910 const char* issuer,
const char* fingerprint, DWORD flags)
916#ifdef WITH_WINDOWS_CERT_STORE
917 if (flags & VERIFY_CERT_FLAG_FP_IS_PEM && !(flags & VERIFY_CERT_FLAG_MISMATCH))
919 if (wf_is_x509_certificate_trusted(common_name, subject, issuer, fingerprint) == S_OK)
926 buffer = wf_format_text(
927 L
"Certificate details:\n"
928 L
"\tCommonName: %S\n"
931 L
"\tThumbprint: %S\n"
932 L
"\tHostMismatch: %S\n"
934 L
"The above X.509 certificate could not be verified, possibly because you do not have "
935 L
"the CA certificate in your certificate store, or the certificate has expired. "
936 L
"Please look at the OpenSSL documentation on how to add a private CA to the store.\n"
938 L
"YES\tAccept permanently\n"
939 L
"NO\tAccept for this session only\n"
940 L
"CANCEL\tAbort connection\n",
941 common_name, subject, issuer, fingerprint,
942 flags & VERIFY_CERT_FLAG_MISMATCH ?
"Yes" :
"No");
943 caption = wf_format_text(L
"Verify certificate for %S:%hu", host, port);
945 WINPR_UNUSED(instance);
947 if (!buffer || !caption)
950 what = MessageBoxW(NULL, buffer, caption, MB_YESNOCANCEL);
968static DWORD wf_verify_changed_certificate_ex(freerdp* instance,
const char* host, UINT16 port,
969 const char* common_name,
const char* subject,
970 const char* issuer,
const char* new_fingerprint,
971 const char* old_subject,
const char* old_issuer,
972 const char* old_fingerprint, DWORD flags)
978 buffer = wf_format_text(
979 L
"New Certificate details:\n"
980 L
"\tCommonName: %S\n"
983 L
"\tThumbprint: %S\n"
984 L
"\tHostMismatch: %S\n"
986 L
"Old Certificate details:\n"
990 L
"The above X.509 certificate could not be verified, possibly because you do not have "
991 L
"the CA certificate in your certificate store, or the certificate has expired. "
992 L
"Please look at the OpenSSL documentation on how to add a private CA to the store.\n"
994 L
"YES\tAccept permanently\n"
995 L
"NO\tAccept for this session only\n"
996 L
"CANCEL\tAbort connection\n",
997 common_name, subject, issuer, new_fingerprint,
998 flags & VERIFY_CERT_FLAG_MISMATCH ?
"Yes" :
"No", old_subject, old_issuer, old_fingerprint);
999 caption = wf_format_text(L
"Verify certificate change for %S:%hu", host, port);
1001 WINPR_UNUSED(instance);
1002 if (!buffer || !caption)
1005 what = MessageBoxW(NULL, buffer, caption, MB_YESNOCANCEL);
1023static BOOL wf_present_gateway_message(freerdp* instance, UINT32 type, BOOL isDisplayMandatory,
1024 BOOL isConsentMandatory,
size_t length,
const WCHAR* message)
1026 if (!isDisplayMandatory && !isConsentMandatory)
1030 if (type == GATEWAY_MESSAGE_CONSENT && isConsentMandatory)
1035 msg = wf_format_text(L
"%.*s\n\nI understand and agree to the terms of this policy", length,
1037 mbRes = MessageBoxW(NULL, msg, L
"Consent Message", MB_YESNO);
1044 return client_cli_present_gateway_message(instance, type, isDisplayMandatory,
1045 isConsentMandatory, length, message);
1050static DWORD WINAPI wf_client_thread(LPVOID lpParam)
1055 BOOL msg_ret = FALSE;
1059 freerdp* instance = (freerdp*)lpParam;
1060 WINPR_ASSERT(instance);
1062 if (!freerdp_connect(instance))
1065 rdpContext* context = instance->context;
1066 WINPR_ASSERT(context);
1068 wfContext* wfc = (wfContext*)instance->context;
1071 rdpChannels* channels = context->channels;
1072 WINPR_ASSERT(channels);
1074 rdpSettings* settings = context->settings;
1075 WINPR_ASSERT(settings);
1077 while (!freerdp_shall_disconnect_context(instance->context))
1079 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = { 0 };
1082 if (freerdp_focus_required(instance))
1084 wf_event_focus_in(wfc);
1085 wf_event_focus_in(wfc);
1089 DWORD tmp = freerdp_get_event_handles(context, &handles[nCount], 64 - nCount);
1093 WLog_ERR(TAG,
"freerdp_get_event_handles failed");
1100 DWORD status = MsgWaitForMultipleObjectsEx(nCount, handles, 5 * 1000, QS_ALLINPUT,
1101 MWMO_ALERTABLE | MWMO_INPUTAVAILABLE);
1102 if (status == WAIT_FAILED)
1104 WLog_ERR(TAG,
"wfreerdp_run: WaitForMultipleObjects failed: 0x%08lX", GetLastError());
1109 if (!freerdp_check_event_handles(context))
1111 if (client_auto_reconnect(instance))
1114 WLog_ERR(TAG,
"Failed to check FreeRDP file descriptor");
1119 if (freerdp_shall_disconnect_context(instance->context))
1124 while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
1126 msg_ret = GetMessage(&msg, NULL, 0, 0);
1130 if ((msg.message == WM_SETFOCUS) && (msg.lParam == 1))
1132 PostMessage(wfc->hwnd, WM_SETFOCUS, 0, 0);
1134 else if ((msg.message == WM_KILLFOCUS) && (msg.lParam == 1))
1136 PostMessage(wfc->hwnd, WM_KILLFOCUS, 0, 0);
1140 switch (msg.message)
1144 width = LOWORD(msg.lParam);
1145 height = HIWORD(msg.lParam);
1146 SetWindowPos(wfc->hwnd, HWND_TOP, 0, 0, width, height, SWP_FRAMECHANGED);
1149 case WM_FREERDP_SHOWWINDOW:
1151 ShowWindow(wfc->hwnd, SW_NORMAL);
1158 if ((msg_ret == 0) || (msg_ret == -1))
1164 TranslateMessage(&msg);
1165 DispatchMessage(&msg);
1173 freerdp_disconnect(instance);
1176 error = freerdp_get_last_error(instance->context);
1177 WLog_DBG(TAG,
"Main thread exited with %" PRIu32, error);
1182static DWORD WINAPI wf_keyboard_thread(LPVOID lpParam)
1188 wfc = (wfContext*)lpParam;
1189 WINPR_ASSERT(NULL != wfc);
1190 hook_handle = SetWindowsHookEx(WH_KEYBOARD_LL, wf_ll_kbd_proc, wfc->hInstance, 0);
1194 while ((status = GetMessage(&msg, NULL, 0, 0)) != 0)
1198 WLog_ERR(TAG,
"keyboard thread error getting message");
1203 TranslateMessage(&msg);
1204 DispatchMessage(&msg);
1208 UnhookWindowsHookEx(hook_handle);
1212 WLog_ERR(TAG,
"failed to install keyboard hook");
1215 WLog_DBG(TAG,
"Keyboard thread exited.");
1220int freerdp_client_set_window_size(wfContext* wfc,
int width,
int height)
1222 WLog_DBG(TAG,
"freerdp_client_set_window_size %d, %d", width, height);
1224 if ((width != wfc->client_width) || (height != wfc->client_height))
1226 PostThreadMessage(wfc->mainThreadId, WM_SIZE, SIZE_RESTORED,
1227 ((UINT)height << 16) | (UINT)width);
1233void wf_size_scrollbars(wfContext* wfc, UINT32 client_width, UINT32 client_height)
1235 const rdpSettings* settings;
1238 settings = wfc->common.context.settings;
1239 WINPR_ASSERT(settings);
1241 if (wfc->disablewindowtracking)
1245 wfc->disablewindowtracking = TRUE;
1250 wfc->xCurrentScroll = 0;
1251 wfc->yCurrentScroll = 0;
1253 if (wfc->xScrollVisible || wfc->yScrollVisible)
1255 if (ShowScrollBar(wfc->hwnd, SB_BOTH, FALSE))
1257 wfc->xScrollVisible = FALSE;
1258 wfc->yScrollVisible = FALSE;
1265 BOOL horiz = wfc->xScrollVisible;
1266 BOOL vert = wfc->yScrollVisible;
1275 settings, FreeRDP_DesktopWidth) )
1287 settings, FreeRDP_DesktopHeight) )
1292 if (horiz == vert && (horiz != wfc->xScrollVisible && vert != wfc->yScrollVisible))
1294 if (ShowScrollBar(wfc->hwnd, SB_BOTH, horiz))
1296 wfc->xScrollVisible = horiz;
1297 wfc->yScrollVisible = vert;
1301 if (horiz != wfc->xScrollVisible)
1303 if (ShowScrollBar(wfc->hwnd, SB_HORZ, horiz))
1305 wfc->xScrollVisible = horiz;
1309 if (vert != wfc->yScrollVisible)
1311 if (ShowScrollBar(wfc->hwnd, SB_VERT, vert))
1313 wfc->yScrollVisible = vert;
1324 wfc->xCurrentScroll = MIN(wfc->xCurrentScroll, wfc->xMaxScroll);
1325 si.cbSize =
sizeof(si);
1326 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
1327 si.nMin = wfc->xMinScroll;
1329 si.nPage = client_width;
1330 si.nPos = wfc->xCurrentScroll;
1331 SetScrollInfo(wfc->hwnd, SB_HORZ, &si, TRUE);
1339 wfc->yMaxScroll = MAX(
1341 wfc->yCurrentScroll = MIN(wfc->yCurrentScroll, wfc->yMaxScroll);
1342 si.cbSize =
sizeof(si);
1343 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
1344 si.nMin = wfc->yMinScroll;
1346 si.nPage = client_height;
1347 si.nPos = wfc->yCurrentScroll;
1348 SetScrollInfo(wfc->hwnd, SB_VERT, &si, TRUE);
1352 wfc->disablewindowtracking = FALSE;
1353 wf_update_canvas_diff(wfc);
1356static BOOL wfreerdp_client_global_init(
void)
1360 WSAStartup(0x101, &wsaData);
1362 freerdp_register_addin_provider(freerdp_channels_load_static_addin_entry, 0);
1367static void wfreerdp_client_global_uninit(
void)
1372static BOOL wfreerdp_client_new(freerdp* instance, rdpContext* context)
1374 wfContext* wfc = (wfContext*)context;
1380 wfc->isConsole = wf_has_console();
1382 if (!(wfreerdp_client_global_init()))
1385 WINPR_ASSERT(instance);
1386 instance->PreConnect = wf_pre_connect;
1387 instance->PostConnect = wf_post_connect;
1388 instance->PostDisconnect = wf_post_disconnect;
1389 instance->AuthenticateEx = wf_authenticate_ex;
1391#ifdef WITH_WINDOWS_CERT_STORE
1396 if (!wfc->isConsole)
1398 instance->VerifyCertificateEx = wf_verify_certificate_ex;
1399 instance->VerifyChangedCertificateEx = wf_verify_changed_certificate_ex;
1400 instance->PresentGatewayMessage = wf_present_gateway_message;
1403#ifdef WITH_PROGRESS_BAR
1404 CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
1405 CoCreateInstance(&CLSID_TaskbarList, NULL, CLSCTX_ALL, &IID_ITaskbarList3,
1406 (
void**)&wfc->taskBarList);
1412static void wfreerdp_client_free(freerdp* instance, rdpContext* context)
1414 WINPR_UNUSED(instance);
1418#ifdef WITH_PROGRESS_BAR
1423static int wfreerdp_client_start(rdpContext* context)
1425 wfContext* wfc = (wfContext*)context;
1427 WINPR_ASSERT(context);
1428 WINPR_ASSERT(context->settings);
1430 freerdp* instance = context->instance;
1431 WINPR_ASSERT(instance);
1433 rdpSettings* settings = context->settings;
1434 WINPR_ASSERT(settings);
1436 HINSTANCE hInstance = GetModuleHandle(NULL);
1441 wfc->hWndParent = hWndParent;
1445 typedef UINT(WINAPI * GetDpiForWindow_t)(HWND hwnd);
1446 typedef BOOL(WINAPI * SetProcessDPIAware_t)(void);
1448 HMODULE
module = GetModuleHandle(_T("User32"));
1451 GetDpiForWindow_t pGetDpiForWindow =
1452 GetProcAddressAs(module,
"GetDpiForWindow", GetDpiForWindow_t);
1453 SetProcessDPIAware_t pSetProcessDPIAware =
1454 GetProcAddressAs(module,
"SetProcessDPIAware", SetProcessDPIAware_t);
1455 if (pGetDpiForWindow && pSetProcessDPIAware)
1457 const UINT dpiAwareness = pGetDpiForWindow(hWndParent);
1458 if (dpiAwareness != USER_DEFAULT_SCREEN_DPI)
1459 pSetProcessDPIAware();
1461 FreeLibrary(module);
1469 wfc->systemMenuInsertPosition = 6;
1471 wfc->hInstance = hInstance;
1472 wfc->cursor = LoadCursor(NULL, IDC_ARROW);
1473 wfc->icon = LoadIcon(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1));
1474 wfc->wndClassName = _tcsdup(_T(
"FreeRDP"));
1475 wfc->wndClass.cbSize =
sizeof(WNDCLASSEX);
1476 wfc->wndClass.style = CS_HREDRAW | CS_VREDRAW;
1477 wfc->wndClass.lpfnWndProc = wf_event_proc;
1478 wfc->wndClass.cbClsExtra = 0;
1479 wfc->wndClass.cbWndExtra = 0;
1480 wfc->wndClass.hCursor = NULL;
1481 wfc->wndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
1482 wfc->wndClass.lpszMenuName = NULL;
1483 wfc->wndClass.lpszClassName = wfc->wndClassName;
1484 wfc->wndClass.hInstance = hInstance;
1485 wfc->wndClass.hIcon = wfc->icon;
1486 wfc->wndClass.hIconSm = wfc->icon;
1487 RegisterClassEx(&(wfc->wndClass));
1488 wfc->keyboardThread =
1489 CreateThread(NULL, 0, wf_keyboard_thread, (
void*)wfc, 0, &wfc->keyboardThreadId);
1491 if (!wfc->keyboardThread)
1494 wfc->common.thread =
1495 CreateThread(NULL, 0, wf_client_thread, (
void*)instance, 0, &wfc->mainThreadId);
1497 if (!wfc->common.thread)
1503static int wfreerdp_client_stop(rdpContext* context)
1506 wfContext* wfc = (wfContext*)context;
1509 PostThreadMessage(wfc->mainThreadId, WM_QUIT, 0, 0);
1510 rc = freerdp_client_common_stop(context);
1511 wfc->mainThreadId = 0;
1513 if (wfc->keyboardThread)
1515 PostThreadMessage(wfc->keyboardThreadId, WM_QUIT, 0, 0);
1516 (void)WaitForSingleObject(wfc->keyboardThread, INFINITE);
1517 (void)CloseHandle(wfc->keyboardThread);
1518 wfc->keyboardThread = NULL;
1519 wfc->keyboardThreadId = 0;
1525int RdpClientEntry(RDP_CLIENT_ENTRY_POINTS* pEntryPoints)
1527 pEntryPoints->Version = 1;
1528 pEntryPoints->Size =
sizeof(RDP_CLIENT_ENTRY_POINTS_V1);
1529 pEntryPoints->GlobalInit = wfreerdp_client_global_init;
1530 pEntryPoints->GlobalUninit = wfreerdp_client_global_uninit;
1531 pEntryPoints->ContextSize =
sizeof(wfContext);
1532 pEntryPoints->ClientNew = wfreerdp_client_new;
1533 pEntryPoints->ClientFree = wfreerdp_client_free;
1534 pEntryPoints->ClientStart = wfreerdp_client_start;
1535 pEntryPoints->ClientStop = wfreerdp_client_stop;
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 UINT64 freerdp_settings_get_uint64(const rdpSettings *settings, FreeRDP_Settings_Keys_UInt64 id)
Returns a UINT64 settings value.
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 BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 param)
Sets a UINT32 settings value.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.
FREERDP_API BOOL freerdp_settings_set_bool(rdpSettings *settings, FreeRDP_Settings_Keys_Bool id, BOOL param)
Sets a BOOL settings value.