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>
58#include <freerdp/utils/signal.h>
62#include "wf_channels.h"
63#include "wf_graphics.h"
65#include "resource/resource.h"
67#define TAG CLIENT_TAG("windows")
69#define WM_FREERDP_SHOWWINDOW (WM_USER + 100)
71static BOOL wf_has_console(
void)
73#ifdef WITH_WIN_CONSOLE
74 int file = _fileno(stdin);
75 int tty = _isatty(file);
81 WLog_INFO(TAG,
"Detected stdin=%d -> %s mode", file, tty ?
"console" :
"gui");
85static BOOL wf_end_paint(rdpContext* context)
87 RECT updateRect = WINPR_C_ARRAY_INIT;
88 REGION16 invalidRegion = WINPR_C_ARRAY_INIT;
92 WINPR_ASSERT(context);
94 wfContext* wfc = (wfContext*)context;
95 rdpGdi* gdi = context->gdi;
98 HGDI_DC hdc = gdi->primary->hdc;
104 WINPR_ASSERT(hwnd->invalid || (hwnd->ninvalid == 0));
106 if (hwnd->invalid->null)
109 const int ninvalid = hwnd->ninvalid;
110 const HGDI_RGN cinvalid = hwnd->cinvalid;
115 region16_init(&invalidRegion);
117 for (
int i = 0; i < ninvalid; i++)
119 invalidRect.left = cinvalid[i].x;
120 invalidRect.top = cinvalid[i].y;
121 invalidRect.right = cinvalid[i].x + cinvalid[i].w;
122 invalidRect.bottom = cinvalid[i].y + cinvalid[i].h;
123 region16_union_rect(&invalidRegion, &invalidRegion, &invalidRect);
126 if (!region16_is_empty(&invalidRegion))
128 extents = region16_extents(&invalidRegion);
129 updateRect.left = extents->left;
130 updateRect.top = extents->top;
131 updateRect.right = extents->right;
132 updateRect.bottom = extents->bottom;
134 wf_scale_rect(wfc, &updateRect);
136 InvalidateRect(wfc->hwnd, &updateRect, FALSE);
139 wf_rail_invalidate_region(wfc, &invalidRegion);
142 region16_uninit(&invalidRegion);
146 wfc->is_shown = TRUE;
148#ifdef WITH_PROGRESS_BAR
149 if (wfc->taskBarList)
151 wfc->taskBarList->lpVtbl->SetProgressState(wfc->taskBarList, wfc->hwnd,
156 PostMessage(wfc->hwnd, WM_FREERDP_SHOWWINDOW, 0, 0);
157 WLog_INFO(TAG,
"Window is shown!");
162static BOOL wf_begin_paint(rdpContext* context)
166 if (!context || !context->gdi || !context->gdi->primary || !context->gdi->primary->hdc)
169 hdc = context->gdi->primary->hdc;
171 if (!hdc || !hdc->hwnd || !hdc->hwnd->invalid)
174 hdc->hwnd->invalid->null = TRUE;
175 hdc->hwnd->ninvalid = 0;
179static BOOL wf_desktop_resize(rdpContext* context)
183 rdpSettings* settings;
184 wfContext* wfc = (wfContext*)context;
186 if (!context || !context->settings)
189 settings = context->settings;
193 same = (wfc->primary == wfc->drawing) ? TRUE : FALSE;
194 wf_image_free(wfc->primary);
198 context->gdi->dstFormat,
nullptr);
203 context->gdi->dstFormat, wfc->primary->pdata,
nullptr))
207 wfc->drawing = wfc->primary;
209 if (wfc->fullscreen != TRUE)
212 SetWindowPos(wfc->hwnd, HWND_TOP, -1, -1,
219 wf_update_offset(wfc);
220 GetWindowRect(wfc->hwnd, &rect);
221 InvalidateRect(wfc->hwnd, &rect, TRUE);
227static BOOL wf_pre_connect(freerdp* instance)
229 WINPR_ASSERT(instance);
230 WINPR_ASSERT(instance->context);
231 WINPR_ASSERT(instance->context->settings);
233 rdpContext* context = instance->context;
234 wfContext* wfc = (wfContext*)instance->context;
235 rdpSettings* settings = context->settings;
245 if (wfc->percentscreen > 0)
247 desktopWidth = (GetSystemMetrics(SM_CXSCREEN) * wfc->percentscreen) / 100;
250 desktopHeight = (GetSystemMetrics(SM_CYSCREEN) * wfc->percentscreen) / 100;
259 desktopWidth = GetSystemMetrics(SM_CXVIRTUALSCREEN);
260 desktopHeight = GetSystemMetrics(SM_CYVIRTUALSCREEN);
264 desktopWidth = GetSystemMetrics(SM_CXSCREEN);
265 desktopHeight = GetSystemMetrics(SM_CYSCREEN);
271 desktopWidth = (desktopWidth + 3) & (~3);
288 CHAR name[KL_NAMELENGTH + 1] = WINPR_C_ARRAY_INIT;
289 if (GetKeyboardLayoutNameA(name))
294 rc = strtoul(name,
nullptr, 16);
296 keyboardLayoutId = rc;
299 if (keyboardLayoutId == 0)
301 const HKL layout = GetKeyboardLayout(0);
302 const uint32_t masked = (uint32_t)(((uintptr_t)layout >> 16) & 0xFFFF);
303 keyboardLayoutId = masked;
307 if (keyboardLayoutId == 0)
308 freerdp_detect_keyboard_layout_from_system_locale(&keyboardLayoutId);
309 if (keyboardLayoutId == 0)
310 keyboardLayoutId = ENGLISH_UNITED_STATES;
313 PubSub_SubscribeChannelConnected(instance->context->pubSub, wf_OnChannelConnectedEventHandler);
314 PubSub_SubscribeChannelDisconnected(instance->context->pubSub,
315 wf_OnChannelDisconnectedEventHandler);
319static void wf_append_item_to_system_menu(HMENU hMenu, UINT fMask, UINT wID,
const wchar_t* text,
322 MENUITEMINFO item_info = WINPR_C_ARRAY_INIT;
323 item_info.fMask = fMask;
324 item_info.cbSize =
sizeof(MENUITEMINFO);
326 item_info.fType = MFT_STRING;
327 item_info.dwTypeData = _wcsdup(text);
328 item_info.cch = (UINT)_wcslen(text);
330 item_info.dwItemData = (ULONG_PTR)wfc;
331 InsertMenuItem(hMenu, wfc->systemMenuInsertPosition++, TRUE, &item_info);
334static void wf_add_system_menu(wfContext* wfc)
338 if (wfc->fullscreen && !wfc->fullscreen_toggle)
348 hMenu = GetSystemMenu(wfc->hwnd, FALSE);
350 wf_append_item_to_system_menu(hMenu,
351 MIIM_CHECKMARKS | MIIM_FTYPE | MIIM_ID | MIIM_STRING | MIIM_DATA,
352 SYSCOMMAND_ID_SMARTSIZING, L
"Smart sizing", wfc);
356 CheckMenuItem(hMenu, SYSCOMMAND_ID_SMARTSIZING, MF_CHECKED);
360 wf_append_item_to_system_menu(hMenu, MIIM_FTYPE | MIIM_ID | MIIM_STRING,
361 SYSCOMMAND_ID_REQUEST_CONTROL, L
"Request control", wfc);
364static WCHAR* wf_window_get_title(rdpSettings* settings)
367 WCHAR* windowTitle =
nullptr;
369 WCHAR prefix[] = L
"FreeRDP:";
381 size = strlen(name) + 16 + wcslen(prefix);
382 windowTitle = calloc(size,
sizeof(WCHAR));
388 _snwprintf_s(windowTitle, size, _TRUNCATE, L
"%s %S", prefix, name);
390 _snwprintf_s(windowTitle, size, _TRUNCATE, L
"%s %S:%u", prefix, name,
396static BOOL wf_post_connect(freerdp* instance)
403 rdpSettings* settings;
404 EmbedWindowEventArgs e;
405 const UINT32 format = PIXEL_FORMAT_BGRX32;
407 WINPR_ASSERT(instance);
409 context = instance->context;
410 WINPR_ASSERT(context);
412 settings = context->settings;
413 WINPR_ASSERT(settings);
415 wfc = (wfContext*)instance->context;
424 WLog_ERR(TAG,
"Failed to allocate primary surface");
428 if (!gdi_init_ex(instance, format, 0, wfc->primary->pdata,
nullptr))
431 cache = instance->context->cache;
434 gdi = instance->context->gdi;
438 wf_gdi_register_update_callbacks(context->update);
441 wfc->window_title = wf_window_get_title(settings);
443 if (!wfc->window_title)
455 dwStyle = WS_CHILD | WS_BORDER;
458 WS_CAPTION | WS_OVERLAPPED | WS_SYSMENU | WS_MINIMIZEBOX | WS_SIZEBOX | WS_MAXIMIZEBOX;
462 wfc->hwnd = CreateWindowEx(0, wfc->wndClassName, wfc->window_title, dwStyle, 0, 0, 0, 0,
463 wfc->hWndParent,
nullptr, wfc->hInstance,
nullptr);
466 WLog_ERR(TAG,
"CreateWindowEx failed with error: %lu", GetLastError());
469 SetWindowLongPtr(wfc->hwnd, GWLP_USERDATA, (LONG_PTR)wfc);
472 wf_resize_window(wfc);
473 wf_add_system_menu(wfc);
476 wfc->drawing = wfc->primary;
477 EventArgsInit(&e,
"wfreerdp");
479 e.handle = (
void*)wfc->hwnd;
480 if (PubSub_OnEmbedWindow(context->pubSub, context, &e) < 0)
483#ifdef WITH_PROGRESS_BAR
484 if (wfc->taskBarList)
486 ShowWindow(wfc->hwnd, SW_SHOWMINIMIZED);
487 wfc->taskBarList->lpVtbl->SetProgressState(wfc->taskBarList, wfc->hwnd, TBPF_INDETERMINATE);
490 UpdateWindow(wfc->hwnd);
491 context->update->BeginPaint = wf_begin_paint;
492 context->update->DesktopResize = wf_desktop_resize;
493 context->update->EndPaint = wf_end_paint;
494 context->update->SetKeyboardIndicators = wf_keyboard_set_indicators;
495 wf_register_pointer(context->graphics);
497 wfc->floatbar = wf_floatbar_new(wfc, wfc->hInstance,
500 wf_event_focus_in(wfc);
505static void wf_post_disconnect(freerdp* instance)
509 if (!instance || !instance->context)
512 wfc = (wfContext*)instance->context;
513 free(wfc->window_title);
516static CREDUI_INFOW wfUiInfo = {
sizeof(CREDUI_INFOW),
nullptr, L
"Enter your credentials",
517 L
"Remote Desktop Security",
nullptr };
519static BOOL wf_authenticate_ex(freerdp* instance,
char** username,
char** password,
char** domain,
520 rdp_auth_reason reason)
526 WCHAR UserNameW[CREDUI_MAX_USERNAME_LENGTH + 1] = WINPR_C_ARRAY_INIT;
527 WCHAR UserW[CREDUI_MAX_USERNAME_LENGTH + 1] = WINPR_C_ARRAY_INIT;
528 WCHAR DomainW[CREDUI_MAX_DOMAIN_TARGET_LENGTH + 1] = WINPR_C_ARRAY_INIT;
529 WCHAR PasswordW[CREDUI_MAX_PASSWORD_LENGTH + 1] = WINPR_C_ARRAY_INIT;
531 WINPR_ASSERT(instance);
532 WINPR_ASSERT(instance->context);
533 WINPR_ASSERT(instance->context->settings);
535 wfc = (wfContext*)instance->context;
538 WINPR_ASSERT(username);
539 WINPR_ASSERT(domain);
540 WINPR_ASSERT(password);
542 const WCHAR auth[] = L
"Target credentials requested";
543 const WCHAR authPin[] = L
"PIN requested";
544 const WCHAR gwAuth[] = L
"Gateway credentials requested";
545 const WCHAR* titleW = auth;
548 dwFlags = CREDUI_FLAGS_DO_NOT_PERSIST | CREDUI_FLAGS_EXCLUDE_CERTIFICATES |
549 CREDUI_FLAGS_USERNAME_TARGET_CREDENTIALS;
556 if ((*username) && (*password))
559 case AUTH_SMARTCARD_PIN:
560 dwFlags &= ~CREDUI_FLAGS_USERNAME_TARGET_CREDENTIALS;
561 dwFlags |= CREDUI_FLAGS_PASSWORD_ONLY_OK | CREDUI_FLAGS_KEEP_USERNAME;
566 *username = _strdup(
"PIN");
579 (void)ConvertUtf8ToWChar(*username, UserNameW, ARRAYSIZE(UserNameW));
580 (void)ConvertUtf8ToWChar(*username, UserW, ARRAYSIZE(UserW));
584 (void)ConvertUtf8ToWChar(*password, PasswordW, ARRAYSIZE(PasswordW));
587 (void)ConvertUtf8ToWChar(*domain, DomainW, ARRAYSIZE(DomainW));
589 if (_wcsnlen(PasswordW, ARRAYSIZE(PasswordW)) == 0)
591 if (!wfc->isConsole &&
593 WLog_ERR(TAG,
"Flag for stdin read present but stdin is redirected; using GUI");
594 if (wfc->isConsole &&
596 status = CredUICmdLinePromptForCredentialsW(titleW,
nullptr, 0, UserNameW,
597 ARRAYSIZE(UserNameW), PasswordW,
598 ARRAYSIZE(PasswordW), &fSave, dwFlags);
600 status = CredUIPromptForCredentialsW(&wfUiInfo, titleW,
nullptr, 0, UserNameW,
601 ARRAYSIZE(UserNameW), PasswordW,
602 ARRAYSIZE(PasswordW), &fSave, dwFlags);
603 if (status != NO_ERROR)
605 WLog_ERR(TAG,
"CredUIPromptForCredentials unexpected status: 0x%08lX", status);
609 if ((dwFlags & CREDUI_FLAGS_KEEP_USERNAME) == 0)
611 status = CredUIParseUserNameW(UserNameW, UserW, ARRAYSIZE(UserW), DomainW,
613 if (status != NO_ERROR)
615 CHAR User[CREDUI_MAX_USERNAME_LENGTH + 1] = WINPR_C_ARRAY_INIT;
616 CHAR UserName[CREDUI_MAX_USERNAME_LENGTH + 1] = WINPR_C_ARRAY_INIT;
617 CHAR Domain[CREDUI_MAX_DOMAIN_TARGET_LENGTH + 1] = WINPR_C_ARRAY_INIT;
619 (void)ConvertWCharNToUtf8(UserNameW, ARRAYSIZE(UserNameW), UserName,
620 ARRAYSIZE(UserName));
621 (void)ConvertWCharNToUtf8(UserW, ARRAYSIZE(UserW), User, ARRAYSIZE(User));
622 (void)ConvertWCharNToUtf8(DomainW, ARRAYSIZE(DomainW), Domain, ARRAYSIZE(Domain));
623 WLog_ERR(TAG,
"Failed to parse UserName: %s into User: %s Domain: %s", UserName,
630 *username = ConvertWCharNToUtf8Alloc(UserW, ARRAYSIZE(UserW),
nullptr);
633 WLog_ERR(TAG,
"ConvertWCharNToUtf8Alloc failed", status);
637 if (_wcsnlen(DomainW, ARRAYSIZE(DomainW)) > 0)
638 *domain = ConvertWCharNToUtf8Alloc(DomainW, ARRAYSIZE(DomainW),
nullptr);
640 *domain = _strdup(
"\0");
645 WLog_ERR(TAG,
"strdup failed", status);
649 *password = ConvertWCharNToUtf8Alloc(PasswordW, ARRAYSIZE(PasswordW),
nullptr);
660static WCHAR* wf_format_text(
const WCHAR* fmt, ...)
664 WCHAR* buffer =
nullptr;
668 WCHAR* tmp =
nullptr;
669 va_list ap = WINPR_C_ARRAY_INIT;
671 rc = _vsnwprintf(buffer, size, fmt, ap);
676 if ((
size_t)rc < size)
679 size = (size_t)rc + 1;
680 tmp = realloc(buffer, size *
sizeof(WCHAR));
692#ifdef WITH_WINDOWS_CERT_STORE
700static void wf_report_error(
char* wszMessage, DWORD dwErrCode)
702 LPSTR pwszMsgBuf =
nullptr;
704 if (
nullptr != wszMessage && 0 != *wszMessage)
706 WLog_ERR(TAG,
"%s", wszMessage);
709 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
714 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
724 if (
nullptr != pwszMsgBuf)
726 WLog_ERR(TAG,
"Error: 0x%08x (%d) %s", dwErrCode, dwErrCode, pwszMsgBuf);
727 LocalFree(pwszMsgBuf);
731 WLog_ERR(TAG,
"Error: 0x%08x (%d)", dwErrCode, dwErrCode);
735static DWORD wf_is_x509_certificate_trusted(
const char* common_name,
const char* subject,
736 const char* issuer,
const char* fingerprint)
738 HRESULT hr = CRYPT_E_NOT_FOUND;
740 DWORD dwChainFlags = CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT;
742 HCERTCHAINENGINE hChainEngine =
nullptr;
743 PCCERT_CHAIN_CONTEXT pChainContext =
nullptr;
745 CERT_ENHKEY_USAGE EnhkeyUsage = WINPR_C_ARRAY_INIT;
746 CERT_USAGE_MATCH CertUsage = WINPR_C_ARRAY_INIT;
747 CERT_CHAIN_PARA ChainPara = WINPR_C_ARRAY_INIT;
748 CERT_CHAIN_POLICY_PARA ChainPolicy = WINPR_C_ARRAY_INIT;
749 CERT_CHAIN_POLICY_STATUS PolicyStatus = WINPR_C_ARRAY_INIT;
750 CERT_CHAIN_ENGINE_CONFIG EngineConfig = WINPR_C_ARRAY_INIT;
752 DWORD derPubKeyLen = WINPR_ASSERTING_INT_CAST(uint32_t, strlen(fingerprint));
753 char* derPubKey = calloc(derPubKeyLen,
sizeof(
char));
754 if (
nullptr == derPubKey)
756 WLog_ERR(TAG,
"Could not allocate derPubKey");
763 if (!CryptStringToBinaryA(fingerprint, 0, CRYPT_STRING_BASE64HEADER, derPubKey, &derPubKeyLen,
766 WLog_ERR(TAG,
"CryptStringToBinary failed. Err: %d", GetLastError());
773 EnhkeyUsage.cUsageIdentifier = 0;
774 EnhkeyUsage.rgpszUsageIdentifier =
nullptr;
776 CertUsage.dwType = USAGE_MATCH_TYPE_AND;
777 CertUsage.Usage = EnhkeyUsage;
779 ChainPara.cbSize =
sizeof(ChainPara);
780 ChainPara.RequestedUsage = CertUsage;
782 ChainPolicy.cbSize =
sizeof(ChainPolicy);
784 PolicyStatus.cbSize =
sizeof(PolicyStatus);
786 EngineConfig.cbSize =
sizeof(EngineConfig);
787 EngineConfig.dwUrlRetrievalTimeout = 0;
789 pCert = CertCreateCertificateContext(X509_ASN_ENCODING, derPubKey, derPubKeyLen);
790 if (
nullptr == pCert)
792 WLog_ERR(TAG,
"FAILED: Certificate could not be parsed.");
796 dwChainFlags |= CERT_CHAIN_ENABLE_PEER_TRUST;
805 if (!CertCreateCertificateChainEngine(&EngineConfig, &hChainEngine))
807 hr = HRESULT_FROM_WIN32(GetLastError());
814 if (!CertGetCertificateChain(hChainEngine,
825 hr = HRESULT_FROM_WIN32(GetLastError());
832 if (!CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_BASE,
837 hr = HRESULT_FROM_WIN32(GetLastError());
841 if (PolicyStatus.dwError != S_OK)
843 wf_report_error(
"CertVerifyCertificateChainPolicy: Chain Status", PolicyStatus.dwError);
844 hr = PolicyStatus.dwError;
850 if (PolicyStatus.dwError == CRYPT_E_NO_REVOCATION_CHECK ||
851 PolicyStatus.dwError == CRYPT_E_REVOCATION_OFFLINE)
859 WLog_INFO(TAG,
"CertVerifyCertificateChainPolicy succeeded for %s (%s) issued by %s",
860 common_name, subject, issuer);
867 WLog_INFO(TAG,
"CertVerifyCertificateChainPolicy failed for %s (%s) issued by %s",
868 common_name, subject, issuer);
869 wf_report_error(
nullptr, hr);
874 if (
nullptr != pChainContext)
876 CertFreeCertificateChain(pChainContext);
879 if (
nullptr != hChainEngine)
881 CertFreeCertificateChainEngine(hChainEngine);
884 if (
nullptr != pCert)
886 CertFreeCertificateContext(pCert);
893static DWORD wf_cli_verify_certificate_ex(freerdp* instance,
const char* host, UINT16 port,
894 const char* common_name,
const char* subject,
895 const char* issuer,
const char* fingerprint, DWORD flags)
897#ifdef WITH_WINDOWS_CERT_STORE
898 if (flags & VERIFY_CERT_FLAG_FP_IS_PEM && !(flags & VERIFY_CERT_FLAG_MISMATCH))
900 if (wf_is_x509_certificate_trusted(common_name, subject, issuer, fingerprint) == S_OK)
907 return client_cli_verify_certificate_ex(instance, host, port, common_name, subject, issuer,
911static DWORD wf_verify_certificate_ex(freerdp* instance,
const char* host, UINT16 port,
912 const char* common_name,
const char* subject,
913 const char* issuer,
const char* fingerprint, DWORD flags)
919#ifdef WITH_WINDOWS_CERT_STORE
920 if (flags & VERIFY_CERT_FLAG_FP_IS_PEM && !(flags & VERIFY_CERT_FLAG_MISMATCH))
922 if (wf_is_x509_certificate_trusted(common_name, subject, issuer, fingerprint) == S_OK)
929 buffer = wf_format_text(
930 L
"Certificate details:\n"
931 L
"\tCommonName: %S\n"
934 L
"\tThumbprint: %S\n"
935 L
"\tHostMismatch: %S\n"
937 L
"The above X.509 certificate could not be verified, possibly because you do not have "
938 L
"the CA certificate in your certificate store, or the certificate has expired. "
939 L
"Please look at the OpenSSL documentation on how to add a private CA to the store.\n"
941 L
"YES\tAccept permanently\n"
942 L
"NO\tAccept for this session only\n"
943 L
"CANCEL\tAbort connection\n",
944 common_name, subject, issuer, fingerprint,
945 flags & VERIFY_CERT_FLAG_MISMATCH ?
"Yes" :
"No");
946 caption = wf_format_text(L
"Verify certificate for %S:%hu", host, port);
948 WINPR_UNUSED(instance);
950 if (!buffer || !caption)
953 what = MessageBoxW(
nullptr, buffer, caption, MB_YESNOCANCEL);
971static DWORD wf_verify_changed_certificate_ex(freerdp* instance,
const char* host, UINT16 port,
972 const char* common_name,
const char* subject,
973 const char* issuer,
const char* new_fingerprint,
974 const char* old_subject,
const char* old_issuer,
975 const char* old_fingerprint, DWORD flags)
981 buffer = wf_format_text(
982 L
"New Certificate details:\n"
983 L
"\tCommonName: %S\n"
986 L
"\tThumbprint: %S\n"
987 L
"\tHostMismatch: %S\n"
989 L
"Old Certificate details:\n"
993 L
"The above X.509 certificate could not be verified, possibly because you do not have "
994 L
"the CA certificate in your certificate store, or the certificate has expired. "
995 L
"Please look at the OpenSSL documentation on how to add a private CA to the store.\n"
997 L
"YES\tAccept permanently\n"
998 L
"NO\tAccept for this session only\n"
999 L
"CANCEL\tAbort connection\n",
1000 common_name, subject, issuer, new_fingerprint,
1001 flags & VERIFY_CERT_FLAG_MISMATCH ?
"Yes" :
"No", old_subject, old_issuer, old_fingerprint);
1002 caption = wf_format_text(L
"Verify certificate change for %S:%hu", host, port);
1004 WINPR_UNUSED(instance);
1005 if (!buffer || !caption)
1008 what = MessageBoxW(
nullptr, buffer, caption, MB_YESNOCANCEL);
1026static BOOL wf_present_gateway_message(freerdp* instance, UINT32 type, BOOL isDisplayMandatory,
1027 BOOL isConsentMandatory,
size_t length,
const WCHAR* message)
1029 if (!isDisplayMandatory && !isConsentMandatory)
1033 if (type == GATEWAY_MESSAGE_CONSENT && isConsentMandatory)
1038 msg = wf_format_text(L
"%.*s\n\nI understand and agree to the terms of this policy", length,
1040 mbRes = MessageBoxW(
nullptr, msg, L
"Consent Message", MB_YESNO);
1047 return client_cli_present_gateway_message(instance, type, isDisplayMandatory,
1048 isConsentMandatory, length, message);
1053static DWORD WINAPI wf_client_thread(LPVOID lpParam)
1055 MSG msg = WINPR_C_ARRAY_INIT;
1058 BOOL msg_ret = FALSE;
1062 freerdp* instance = (freerdp*)lpParam;
1063 WINPR_ASSERT(instance);
1065 if (!freerdp_connect(instance))
1068 rdpContext* context = instance->context;
1069 WINPR_ASSERT(context);
1071 wfContext* wfc = (wfContext*)instance->context;
1074 rdpChannels* channels = context->channels;
1075 WINPR_ASSERT(channels);
1077 rdpSettings* settings = context->settings;
1078 WINPR_ASSERT(settings);
1080 while (!freerdp_shall_disconnect_context(instance->context))
1082 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
1085 if (freerdp_focus_required(instance))
1087 wf_event_focus_in(wfc);
1088 wf_event_focus_in(wfc);
1092 DWORD tmp = freerdp_get_event_handles(context, &handles[nCount], 64 - nCount);
1096 WLog_ERR(TAG,
"freerdp_get_event_handles failed");
1103 DWORD status = MsgWaitForMultipleObjectsEx(nCount, handles, 5 * 1000, QS_ALLINPUT,
1104 MWMO_ALERTABLE | MWMO_INPUTAVAILABLE);
1105 if (status == WAIT_FAILED)
1107 WLog_ERR(TAG,
"wfreerdp_run: WaitForMultipleObjects failed: 0x%08lX", GetLastError());
1112 if (!freerdp_check_event_handles(context))
1114 if (client_auto_reconnect(instance))
1117 WLog_ERR(TAG,
"Failed to check FreeRDP file descriptor");
1122 if (freerdp_shall_disconnect_context(instance->context))
1127 while (PeekMessage(&msg,
nullptr, 0, 0, PM_NOREMOVE))
1129 msg_ret = GetMessage(&msg,
nullptr, 0, 0);
1133 if ((msg.message == WM_SETFOCUS) && (msg.lParam == 1))
1135 PostMessage(wfc->hwnd, WM_SETFOCUS, 0, 0);
1137 else if ((msg.message == WM_KILLFOCUS) && (msg.lParam == 1))
1139 PostMessage(wfc->hwnd, WM_KILLFOCUS, 0, 0);
1143 switch (msg.message)
1147 width = LOWORD(msg.lParam);
1148 height = HIWORD(msg.lParam);
1149 SetWindowPos(wfc->hwnd, HWND_TOP, 0, 0, width, height, SWP_FRAMECHANGED);
1152 case WM_FREERDP_SHOWWINDOW:
1154 ShowWindow(wfc->hwnd, SW_NORMAL);
1161 if ((msg_ret == 0) || (msg_ret == -1))
1167 TranslateMessage(&msg);
1168 DispatchMessage(&msg);
1176 freerdp_disconnect(instance);
1179 error = freerdp_get_last_error(instance->context);
1180 WLog_DBG(TAG,
"Main thread exited with %" PRIu32, error);
1185static DWORD WINAPI wf_keyboard_thread(LPVOID lpParam)
1191 wfc = (wfContext*)lpParam;
1192 WINPR_ASSERT(
nullptr != wfc);
1193 hook_handle = SetWindowsHookEx(WH_KEYBOARD_LL, wf_ll_kbd_proc, wfc->hInstance, 0);
1197 while ((status = GetMessage(&msg,
nullptr, 0, 0)) != 0)
1201 WLog_ERR(TAG,
"keyboard thread error getting message");
1206 TranslateMessage(&msg);
1207 DispatchMessage(&msg);
1211 UnhookWindowsHookEx(hook_handle);
1215 WLog_ERR(TAG,
"failed to install keyboard hook");
1218 WLog_DBG(TAG,
"Keyboard thread exited.");
1223int freerdp_client_set_window_size(wfContext* wfc,
int width,
int height)
1225 WLog_DBG(TAG,
"freerdp_client_set_window_size %d, %d", width, height);
1227 if ((width != wfc->client_width) || (height != wfc->client_height))
1229 PostThreadMessage(wfc->mainThreadId, WM_SIZE, SIZE_RESTORED,
1230 ((UINT)height << 16) | (UINT)width);
1236void wf_size_scrollbars(wfContext* wfc, UINT32 client_width, UINT32 client_height)
1238 const rdpSettings* settings;
1241 settings = wfc->common.context.settings;
1242 WINPR_ASSERT(settings);
1244 if (wfc->disablewindowtracking)
1248 wfc->disablewindowtracking = TRUE;
1253 wfc->xCurrentScroll = 0;
1254 wfc->yCurrentScroll = 0;
1256 if (wfc->xScrollVisible || wfc->yScrollVisible)
1258 if (ShowScrollBar(wfc->hwnd, SB_BOTH, FALSE))
1260 wfc->xScrollVisible = FALSE;
1261 wfc->yScrollVisible = FALSE;
1268 BOOL horiz = wfc->xScrollVisible;
1269 BOOL vert = wfc->yScrollVisible;
1278 settings, FreeRDP_DesktopWidth) )
1290 settings, FreeRDP_DesktopHeight) )
1295 if (horiz == vert && (horiz != wfc->xScrollVisible && vert != wfc->yScrollVisible))
1297 if (ShowScrollBar(wfc->hwnd, SB_BOTH, horiz))
1299 wfc->xScrollVisible = horiz;
1300 wfc->yScrollVisible = vert;
1304 if (horiz != wfc->xScrollVisible)
1306 if (ShowScrollBar(wfc->hwnd, SB_HORZ, horiz))
1308 wfc->xScrollVisible = horiz;
1312 if (vert != wfc->yScrollVisible)
1314 if (ShowScrollBar(wfc->hwnd, SB_VERT, vert))
1316 wfc->yScrollVisible = vert;
1327 wfc->xCurrentScroll = MIN(wfc->xCurrentScroll, wfc->xMaxScroll);
1328 si.cbSize =
sizeof(si);
1329 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
1330 si.nMin = wfc->xMinScroll;
1332 si.nPage = client_width;
1333 si.nPos = wfc->xCurrentScroll;
1334 SetScrollInfo(wfc->hwnd, SB_HORZ, &si, TRUE);
1342 wfc->yMaxScroll = MAX(
1344 wfc->yCurrentScroll = MIN(wfc->yCurrentScroll, wfc->yMaxScroll);
1345 si.cbSize =
sizeof(si);
1346 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
1347 si.nMin = wfc->yMinScroll;
1349 si.nPage = client_height;
1350 si.nPos = wfc->yCurrentScroll;
1351 SetScrollInfo(wfc->hwnd, SB_VERT, &si, TRUE);
1355 wfc->disablewindowtracking = FALSE;
1356 wf_update_canvas_diff(wfc);
1359static BOOL wfreerdp_client_global_init(
void)
1363 WSAStartup(0x101, &wsaData);
1364 if (freerdp_handle_signals() != 0)
1367 if (freerdp_register_addin_provider(freerdp_channels_load_static_addin_entry, 0) !=
1374static void wfreerdp_client_global_uninit(
void)
1379static BOOL wfreerdp_client_new(freerdp* instance, rdpContext* context)
1381 wfContext* wfc = (wfContext*)context;
1387 wfc->isConsole = wf_has_console();
1389 if (!(wfreerdp_client_global_init()))
1392 WINPR_ASSERT(instance);
1393 instance->PreConnect = wf_pre_connect;
1394 instance->PostConnect = wf_post_connect;
1395 instance->PostDisconnect = wf_post_disconnect;
1396 instance->AuthenticateEx = wf_authenticate_ex;
1398#ifdef WITH_WINDOWS_CERT_STORE
1403 if (!wfc->isConsole)
1405 instance->VerifyCertificateEx = wf_verify_certificate_ex;
1406 instance->VerifyChangedCertificateEx = wf_verify_changed_certificate_ex;
1407 instance->PresentGatewayMessage = wf_present_gateway_message;
1410#ifdef WITH_PROGRESS_BAR
1411 CoInitializeEx(
nullptr, COINIT_APARTMENTTHREADED);
1412 CoCreateInstance(&CLSID_TaskbarList,
nullptr, CLSCTX_ALL, &IID_ITaskbarList3,
1413 (
void**)&wfc->taskBarList);
1419static void wfreerdp_client_free(freerdp* instance, rdpContext* context)
1421 WINPR_UNUSED(instance);
1425#ifdef WITH_PROGRESS_BAR
1430static int wfreerdp_client_start(rdpContext* context)
1432 wfContext* wfc = (wfContext*)context;
1434 WINPR_ASSERT(context);
1435 WINPR_ASSERT(context->settings);
1437 freerdp* instance = context->instance;
1438 WINPR_ASSERT(instance);
1440 rdpSettings* settings = context->settings;
1441 WINPR_ASSERT(settings);
1443 HINSTANCE hInstance = GetModuleHandle(
nullptr);
1448 wfc->hWndParent = hWndParent;
1452 typedef UINT(WINAPI * GetDpiForWindow_t)(HWND hwnd);
1453 typedef BOOL(WINAPI * SetProcessDPIAware_t)(void);
1455 HMODULE
module = GetModuleHandle(_T("User32"));
1458 GetDpiForWindow_t pGetDpiForWindow =
1459 GetProcAddressAs(module,
"GetDpiForWindow", GetDpiForWindow_t);
1460 SetProcessDPIAware_t pSetProcessDPIAware =
1461 GetProcAddressAs(module,
"SetProcessDPIAware", SetProcessDPIAware_t);
1462 if (pGetDpiForWindow && pSetProcessDPIAware)
1464 const UINT dpiAwareness = pGetDpiForWindow(hWndParent);
1465 if (dpiAwareness != USER_DEFAULT_SCREEN_DPI)
1466 pSetProcessDPIAware();
1468 FreeLibrary(module);
1476 wfc->systemMenuInsertPosition = 6;
1478 wfc->hInstance = hInstance;
1479 wfc->cursor = LoadCursor(
nullptr, IDC_ARROW);
1480 wfc->icon = LoadIcon(GetModuleHandle(
nullptr), MAKEINTRESOURCE(IDI_ICON1));
1481 wfc->wndClassName = _tcsdup(_T(
"FreeRDP"));
1482 wfc->wndClass.cbSize =
sizeof(WNDCLASSEX);
1483 wfc->wndClass.style = CS_HREDRAW | CS_VREDRAW;
1484 wfc->wndClass.lpfnWndProc = wf_event_proc;
1485 wfc->wndClass.cbClsExtra = 0;
1486 wfc->wndClass.cbWndExtra = 0;
1487 wfc->wndClass.hCursor =
nullptr;
1488 wfc->wndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
1489 wfc->wndClass.lpszMenuName =
nullptr;
1490 wfc->wndClass.lpszClassName = wfc->wndClassName;
1491 wfc->wndClass.hInstance = hInstance;
1492 wfc->wndClass.hIcon = wfc->icon;
1493 wfc->wndClass.hIconSm = wfc->icon;
1494 RegisterClassEx(&(wfc->wndClass));
1495 wfc->keyboardThread =
1496 CreateThread(
nullptr, 0, wf_keyboard_thread, (
void*)wfc, 0, &wfc->keyboardThreadId);
1498 if (!wfc->keyboardThread)
1501 wfc->common.thread =
1502 CreateThread(
nullptr, 0, wf_client_thread, (
void*)instance, 0, &wfc->mainThreadId);
1504 if (!wfc->common.thread)
1510static int wfreerdp_client_stop(rdpContext* context)
1513 wfContext* wfc = (wfContext*)context;
1516 PostThreadMessage(wfc->mainThreadId, WM_QUIT, 0, 0);
1517 rc = freerdp_client_common_stop(context);
1518 wfc->mainThreadId = 0;
1520 if (wfc->keyboardThread)
1522 PostThreadMessage(wfc->keyboardThreadId, WM_QUIT, 0, 0);
1523 (void)WaitForSingleObject(wfc->keyboardThread, INFINITE);
1524 (void)CloseHandle(wfc->keyboardThread);
1525 wfc->keyboardThread =
nullptr;
1526 wfc->keyboardThreadId = 0;
1532int RdpClientEntry(RDP_CLIENT_ENTRY_POINTS* pEntryPoints)
1534 pEntryPoints->Version = 1;
1535 pEntryPoints->Size =
sizeof(RDP_CLIENT_ENTRY_POINTS_V1);
1536 pEntryPoints->GlobalInit = wfreerdp_client_global_init;
1537 pEntryPoints->GlobalUninit = wfreerdp_client_global_uninit;
1538 pEntryPoints->ContextSize =
sizeof(wfContext);
1539 pEntryPoints->ClientNew = wfreerdp_client_new;
1540 pEntryPoints->ClientFree = wfreerdp_client_free;
1541 pEntryPoints->ClientStart = wfreerdp_client_start;
1542 pEntryPoints->ClientStop = wfreerdp_client_stop;
FREERDP_API BOOL freerdp_settings_set_uint32(rdpSettings *settings, FreeRDP_Settings_Keys_UInt32 id, UINT32 val)
Sets a UINT32 settings value.
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 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.
WINPR_ATTR_NODISCARD FREERDP_API BOOL freerdp_settings_get_bool(const rdpSettings *settings, FreeRDP_Settings_Keys_Bool id)
Returns a boolean settings value.