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 PubSub_OnEmbedWindow(context->pubSub, context, &e);
481#ifdef WITH_PROGRESS_BAR
482 if (wfc->taskBarList)
484 ShowWindow(wfc->hwnd, SW_SHOWMINIMIZED);
485 wfc->taskBarList->lpVtbl->SetProgressState(wfc->taskBarList, wfc->hwnd, TBPF_INDETERMINATE);
488 UpdateWindow(wfc->hwnd);
489 context->update->BeginPaint = wf_begin_paint;
490 context->update->DesktopResize = wf_desktop_resize;
491 context->update->EndPaint = wf_end_paint;
492 context->update->SetKeyboardIndicators = wf_keyboard_set_indicators;
493 wf_register_pointer(context->graphics);
495 wfc->floatbar = wf_floatbar_new(wfc, wfc->hInstance,
498 wf_event_focus_in(wfc);
503static void wf_post_disconnect(freerdp* instance)
507 if (!instance || !instance->context)
510 wfc = (wfContext*)instance->context;
511 free(wfc->window_title);
514static CREDUI_INFOW wfUiInfo = {
sizeof(CREDUI_INFOW),
nullptr, L
"Enter your credentials",
515 L
"Remote Desktop Security",
nullptr };
517static BOOL wf_authenticate_ex(freerdp* instance,
char** username,
char** password,
char** domain,
518 rdp_auth_reason reason)
524 WCHAR UserNameW[CREDUI_MAX_USERNAME_LENGTH + 1] = WINPR_C_ARRAY_INIT;
525 WCHAR UserW[CREDUI_MAX_USERNAME_LENGTH + 1] = WINPR_C_ARRAY_INIT;
526 WCHAR DomainW[CREDUI_MAX_DOMAIN_TARGET_LENGTH + 1] = WINPR_C_ARRAY_INIT;
527 WCHAR PasswordW[CREDUI_MAX_PASSWORD_LENGTH + 1] = WINPR_C_ARRAY_INIT;
529 WINPR_ASSERT(instance);
530 WINPR_ASSERT(instance->context);
531 WINPR_ASSERT(instance->context->settings);
533 wfc = (wfContext*)instance->context;
536 WINPR_ASSERT(username);
537 WINPR_ASSERT(domain);
538 WINPR_ASSERT(password);
540 const WCHAR auth[] = L
"Target credentials requested";
541 const WCHAR authPin[] = L
"PIN requested";
542 const WCHAR gwAuth[] = L
"Gateway credentials requested";
543 const WCHAR* titleW = auth;
546 dwFlags = CREDUI_FLAGS_DO_NOT_PERSIST | CREDUI_FLAGS_EXCLUDE_CERTIFICATES |
547 CREDUI_FLAGS_USERNAME_TARGET_CREDENTIALS;
554 if ((*username) && (*password))
557 case AUTH_SMARTCARD_PIN:
558 dwFlags &= ~CREDUI_FLAGS_USERNAME_TARGET_CREDENTIALS;
559 dwFlags |= CREDUI_FLAGS_PASSWORD_ONLY_OK | CREDUI_FLAGS_KEEP_USERNAME;
564 *username = _strdup(
"PIN");
577 (void)ConvertUtf8ToWChar(*username, UserNameW, ARRAYSIZE(UserNameW));
578 (void)ConvertUtf8ToWChar(*username, UserW, ARRAYSIZE(UserW));
582 (void)ConvertUtf8ToWChar(*password, PasswordW, ARRAYSIZE(PasswordW));
585 (void)ConvertUtf8ToWChar(*domain, DomainW, ARRAYSIZE(DomainW));
587 if (_wcsnlen(PasswordW, ARRAYSIZE(PasswordW)) == 0)
589 if (!wfc->isConsole &&
591 WLog_ERR(TAG,
"Flag for stdin read present but stdin is redirected; using GUI");
592 if (wfc->isConsole &&
594 status = CredUICmdLinePromptForCredentialsW(titleW,
nullptr, 0, UserNameW,
595 ARRAYSIZE(UserNameW), PasswordW,
596 ARRAYSIZE(PasswordW), &fSave, dwFlags);
598 status = CredUIPromptForCredentialsW(&wfUiInfo, titleW,
nullptr, 0, UserNameW,
599 ARRAYSIZE(UserNameW), PasswordW,
600 ARRAYSIZE(PasswordW), &fSave, dwFlags);
601 if (status != NO_ERROR)
603 WLog_ERR(TAG,
"CredUIPromptForCredentials unexpected status: 0x%08lX", status);
607 if ((dwFlags & CREDUI_FLAGS_KEEP_USERNAME) == 0)
609 status = CredUIParseUserNameW(UserNameW, UserW, ARRAYSIZE(UserW), DomainW,
611 if (status != NO_ERROR)
613 CHAR User[CREDUI_MAX_USERNAME_LENGTH + 1] = WINPR_C_ARRAY_INIT;
614 CHAR UserName[CREDUI_MAX_USERNAME_LENGTH + 1] = WINPR_C_ARRAY_INIT;
615 CHAR Domain[CREDUI_MAX_DOMAIN_TARGET_LENGTH + 1] = WINPR_C_ARRAY_INIT;
617 (void)ConvertWCharNToUtf8(UserNameW, ARRAYSIZE(UserNameW), UserName,
618 ARRAYSIZE(UserName));
619 (void)ConvertWCharNToUtf8(UserW, ARRAYSIZE(UserW), User, ARRAYSIZE(User));
620 (void)ConvertWCharNToUtf8(DomainW, ARRAYSIZE(DomainW), Domain, ARRAYSIZE(Domain));
621 WLog_ERR(TAG,
"Failed to parse UserName: %s into User: %s Domain: %s", UserName,
628 *username = ConvertWCharNToUtf8Alloc(UserW, ARRAYSIZE(UserW),
nullptr);
631 WLog_ERR(TAG,
"ConvertWCharNToUtf8Alloc failed", status);
635 if (_wcsnlen(DomainW, ARRAYSIZE(DomainW)) > 0)
636 *domain = ConvertWCharNToUtf8Alloc(DomainW, ARRAYSIZE(DomainW),
nullptr);
638 *domain = _strdup(
"\0");
643 WLog_ERR(TAG,
"strdup failed", status);
647 *password = ConvertWCharNToUtf8Alloc(PasswordW, ARRAYSIZE(PasswordW),
nullptr);
658static WCHAR* wf_format_text(
const WCHAR* fmt, ...)
662 WCHAR* buffer =
nullptr;
666 WCHAR* tmp =
nullptr;
667 va_list ap = WINPR_C_ARRAY_INIT;
669 rc = _vsnwprintf(buffer, size, fmt, ap);
674 if ((
size_t)rc < size)
677 size = (size_t)rc + 1;
678 tmp = realloc(buffer, size *
sizeof(WCHAR));
690#ifdef WITH_WINDOWS_CERT_STORE
698static void wf_report_error(
char* wszMessage, DWORD dwErrCode)
700 LPSTR pwszMsgBuf =
nullptr;
702 if (
nullptr != wszMessage && 0 != *wszMessage)
704 WLog_ERR(TAG,
"%s", wszMessage);
707 FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
712 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
722 if (
nullptr != pwszMsgBuf)
724 WLog_ERR(TAG,
"Error: 0x%08x (%d) %s", dwErrCode, dwErrCode, pwszMsgBuf);
725 LocalFree(pwszMsgBuf);
729 WLog_ERR(TAG,
"Error: 0x%08x (%d)", dwErrCode, dwErrCode);
733static DWORD wf_is_x509_certificate_trusted(
const char* common_name,
const char* subject,
734 const char* issuer,
const char* fingerprint)
736 HRESULT hr = CRYPT_E_NOT_FOUND;
738 DWORD dwChainFlags = CERT_CHAIN_REVOCATION_CHECK_CHAIN_EXCLUDE_ROOT;
740 HCERTCHAINENGINE hChainEngine =
nullptr;
741 PCCERT_CHAIN_CONTEXT pChainContext =
nullptr;
743 CERT_ENHKEY_USAGE EnhkeyUsage = WINPR_C_ARRAY_INIT;
744 CERT_USAGE_MATCH CertUsage = WINPR_C_ARRAY_INIT;
745 CERT_CHAIN_PARA ChainPara = WINPR_C_ARRAY_INIT;
746 CERT_CHAIN_POLICY_PARA ChainPolicy = WINPR_C_ARRAY_INIT;
747 CERT_CHAIN_POLICY_STATUS PolicyStatus = WINPR_C_ARRAY_INIT;
748 CERT_CHAIN_ENGINE_CONFIG EngineConfig = WINPR_C_ARRAY_INIT;
750 DWORD derPubKeyLen = WINPR_ASSERTING_INT_CAST(uint32_t, strlen(fingerprint));
751 char* derPubKey = calloc(derPubKeyLen,
sizeof(
char));
752 if (
nullptr == derPubKey)
754 WLog_ERR(TAG,
"Could not allocate derPubKey");
761 if (!CryptStringToBinaryA(fingerprint, 0, CRYPT_STRING_BASE64HEADER, derPubKey, &derPubKeyLen,
764 WLog_ERR(TAG,
"CryptStringToBinary failed. Err: %d", GetLastError());
771 EnhkeyUsage.cUsageIdentifier = 0;
772 EnhkeyUsage.rgpszUsageIdentifier =
nullptr;
774 CertUsage.dwType = USAGE_MATCH_TYPE_AND;
775 CertUsage.Usage = EnhkeyUsage;
777 ChainPara.cbSize =
sizeof(ChainPara);
778 ChainPara.RequestedUsage = CertUsage;
780 ChainPolicy.cbSize =
sizeof(ChainPolicy);
782 PolicyStatus.cbSize =
sizeof(PolicyStatus);
784 EngineConfig.cbSize =
sizeof(EngineConfig);
785 EngineConfig.dwUrlRetrievalTimeout = 0;
787 pCert = CertCreateCertificateContext(X509_ASN_ENCODING, derPubKey, derPubKeyLen);
788 if (
nullptr == pCert)
790 WLog_ERR(TAG,
"FAILED: Certificate could not be parsed.");
794 dwChainFlags |= CERT_CHAIN_ENABLE_PEER_TRUST;
803 if (!CertCreateCertificateChainEngine(&EngineConfig, &hChainEngine))
805 hr = HRESULT_FROM_WIN32(GetLastError());
812 if (!CertGetCertificateChain(hChainEngine,
823 hr = HRESULT_FROM_WIN32(GetLastError());
830 if (!CertVerifyCertificateChainPolicy(CERT_CHAIN_POLICY_BASE,
835 hr = HRESULT_FROM_WIN32(GetLastError());
839 if (PolicyStatus.dwError != S_OK)
841 wf_report_error(
"CertVerifyCertificateChainPolicy: Chain Status", PolicyStatus.dwError);
842 hr = PolicyStatus.dwError;
848 if (PolicyStatus.dwError == CRYPT_E_NO_REVOCATION_CHECK ||
849 PolicyStatus.dwError == CRYPT_E_REVOCATION_OFFLINE)
857 WLog_INFO(TAG,
"CertVerifyCertificateChainPolicy succeeded for %s (%s) issued by %s",
858 common_name, subject, issuer);
865 WLog_INFO(TAG,
"CertVerifyCertificateChainPolicy failed for %s (%s) issued by %s",
866 common_name, subject, issuer);
867 wf_report_error(
nullptr, hr);
872 if (
nullptr != pChainContext)
874 CertFreeCertificateChain(pChainContext);
877 if (
nullptr != hChainEngine)
879 CertFreeCertificateChainEngine(hChainEngine);
882 if (
nullptr != pCert)
884 CertFreeCertificateContext(pCert);
891static DWORD wf_cli_verify_certificate_ex(freerdp* instance,
const char* host, UINT16 port,
892 const char* common_name,
const char* subject,
893 const char* issuer,
const char* fingerprint, DWORD flags)
895#ifdef WITH_WINDOWS_CERT_STORE
896 if (flags & VERIFY_CERT_FLAG_FP_IS_PEM && !(flags & VERIFY_CERT_FLAG_MISMATCH))
898 if (wf_is_x509_certificate_trusted(common_name, subject, issuer, fingerprint) == S_OK)
905 return client_cli_verify_certificate_ex(instance, host, port, common_name, subject, issuer,
909static DWORD wf_verify_certificate_ex(freerdp* instance,
const char* host, UINT16 port,
910 const char* common_name,
const char* subject,
911 const char* issuer,
const char* fingerprint, DWORD flags)
917#ifdef WITH_WINDOWS_CERT_STORE
918 if (flags & VERIFY_CERT_FLAG_FP_IS_PEM && !(flags & VERIFY_CERT_FLAG_MISMATCH))
920 if (wf_is_x509_certificate_trusted(common_name, subject, issuer, fingerprint) == S_OK)
927 buffer = wf_format_text(
928 L
"Certificate details:\n"
929 L
"\tCommonName: %S\n"
932 L
"\tThumbprint: %S\n"
933 L
"\tHostMismatch: %S\n"
935 L
"The above X.509 certificate could not be verified, possibly because you do not have "
936 L
"the CA certificate in your certificate store, or the certificate has expired. "
937 L
"Please look at the OpenSSL documentation on how to add a private CA to the store.\n"
939 L
"YES\tAccept permanently\n"
940 L
"NO\tAccept for this session only\n"
941 L
"CANCEL\tAbort connection\n",
942 common_name, subject, issuer, fingerprint,
943 flags & VERIFY_CERT_FLAG_MISMATCH ?
"Yes" :
"No");
944 caption = wf_format_text(L
"Verify certificate for %S:%hu", host, port);
946 WINPR_UNUSED(instance);
948 if (!buffer || !caption)
951 what = MessageBoxW(
nullptr, buffer, caption, MB_YESNOCANCEL);
969static DWORD wf_verify_changed_certificate_ex(freerdp* instance,
const char* host, UINT16 port,
970 const char* common_name,
const char* subject,
971 const char* issuer,
const char* new_fingerprint,
972 const char* old_subject,
const char* old_issuer,
973 const char* old_fingerprint, DWORD flags)
979 buffer = wf_format_text(
980 L
"New Certificate details:\n"
981 L
"\tCommonName: %S\n"
984 L
"\tThumbprint: %S\n"
985 L
"\tHostMismatch: %S\n"
987 L
"Old Certificate details:\n"
991 L
"The above X.509 certificate could not be verified, possibly because you do not have "
992 L
"the CA certificate in your certificate store, or the certificate has expired. "
993 L
"Please look at the OpenSSL documentation on how to add a private CA to the store.\n"
995 L
"YES\tAccept permanently\n"
996 L
"NO\tAccept for this session only\n"
997 L
"CANCEL\tAbort connection\n",
998 common_name, subject, issuer, new_fingerprint,
999 flags & VERIFY_CERT_FLAG_MISMATCH ?
"Yes" :
"No", old_subject, old_issuer, old_fingerprint);
1000 caption = wf_format_text(L
"Verify certificate change for %S:%hu", host, port);
1002 WINPR_UNUSED(instance);
1003 if (!buffer || !caption)
1006 what = MessageBoxW(
nullptr, buffer, caption, MB_YESNOCANCEL);
1024static BOOL wf_present_gateway_message(freerdp* instance, UINT32 type, BOOL isDisplayMandatory,
1025 BOOL isConsentMandatory,
size_t length,
const WCHAR* message)
1027 if (!isDisplayMandatory && !isConsentMandatory)
1031 if (type == GATEWAY_MESSAGE_CONSENT && isConsentMandatory)
1036 msg = wf_format_text(L
"%.*s\n\nI understand and agree to the terms of this policy", length,
1038 mbRes = MessageBoxW(
nullptr, msg, L
"Consent Message", MB_YESNO);
1045 return client_cli_present_gateway_message(instance, type, isDisplayMandatory,
1046 isConsentMandatory, length, message);
1051static DWORD WINAPI wf_client_thread(LPVOID lpParam)
1053 MSG msg = WINPR_C_ARRAY_INIT;
1056 BOOL msg_ret = FALSE;
1060 freerdp* instance = (freerdp*)lpParam;
1061 WINPR_ASSERT(instance);
1063 if (!freerdp_connect(instance))
1066 rdpContext* context = instance->context;
1067 WINPR_ASSERT(context);
1069 wfContext* wfc = (wfContext*)instance->context;
1072 rdpChannels* channels = context->channels;
1073 WINPR_ASSERT(channels);
1075 rdpSettings* settings = context->settings;
1076 WINPR_ASSERT(settings);
1078 while (!freerdp_shall_disconnect_context(instance->context))
1080 HANDLE handles[MAXIMUM_WAIT_OBJECTS] = WINPR_C_ARRAY_INIT;
1083 if (freerdp_focus_required(instance))
1085 wf_event_focus_in(wfc);
1086 wf_event_focus_in(wfc);
1090 DWORD tmp = freerdp_get_event_handles(context, &handles[nCount], 64 - nCount);
1094 WLog_ERR(TAG,
"freerdp_get_event_handles failed");
1101 DWORD status = MsgWaitForMultipleObjectsEx(nCount, handles, 5 * 1000, QS_ALLINPUT,
1102 MWMO_ALERTABLE | MWMO_INPUTAVAILABLE);
1103 if (status == WAIT_FAILED)
1105 WLog_ERR(TAG,
"wfreerdp_run: WaitForMultipleObjects failed: 0x%08lX", GetLastError());
1110 if (!freerdp_check_event_handles(context))
1112 if (client_auto_reconnect(instance))
1115 WLog_ERR(TAG,
"Failed to check FreeRDP file descriptor");
1120 if (freerdp_shall_disconnect_context(instance->context))
1125 while (PeekMessage(&msg,
nullptr, 0, 0, PM_NOREMOVE))
1127 msg_ret = GetMessage(&msg,
nullptr, 0, 0);
1131 if ((msg.message == WM_SETFOCUS) && (msg.lParam == 1))
1133 PostMessage(wfc->hwnd, WM_SETFOCUS, 0, 0);
1135 else if ((msg.message == WM_KILLFOCUS) && (msg.lParam == 1))
1137 PostMessage(wfc->hwnd, WM_KILLFOCUS, 0, 0);
1141 switch (msg.message)
1145 width = LOWORD(msg.lParam);
1146 height = HIWORD(msg.lParam);
1147 SetWindowPos(wfc->hwnd, HWND_TOP, 0, 0, width, height, SWP_FRAMECHANGED);
1150 case WM_FREERDP_SHOWWINDOW:
1152 ShowWindow(wfc->hwnd, SW_NORMAL);
1159 if ((msg_ret == 0) || (msg_ret == -1))
1165 TranslateMessage(&msg);
1166 DispatchMessage(&msg);
1174 freerdp_disconnect(instance);
1177 error = freerdp_get_last_error(instance->context);
1178 WLog_DBG(TAG,
"Main thread exited with %" PRIu32, error);
1183static DWORD WINAPI wf_keyboard_thread(LPVOID lpParam)
1189 wfc = (wfContext*)lpParam;
1190 WINPR_ASSERT(
nullptr != wfc);
1191 hook_handle = SetWindowsHookEx(WH_KEYBOARD_LL, wf_ll_kbd_proc, wfc->hInstance, 0);
1195 while ((status = GetMessage(&msg,
nullptr, 0, 0)) != 0)
1199 WLog_ERR(TAG,
"keyboard thread error getting message");
1204 TranslateMessage(&msg);
1205 DispatchMessage(&msg);
1209 UnhookWindowsHookEx(hook_handle);
1213 WLog_ERR(TAG,
"failed to install keyboard hook");
1216 WLog_DBG(TAG,
"Keyboard thread exited.");
1221int freerdp_client_set_window_size(wfContext* wfc,
int width,
int height)
1223 WLog_DBG(TAG,
"freerdp_client_set_window_size %d, %d", width, height);
1225 if ((width != wfc->client_width) || (height != wfc->client_height))
1227 PostThreadMessage(wfc->mainThreadId, WM_SIZE, SIZE_RESTORED,
1228 ((UINT)height << 16) | (UINT)width);
1234void wf_size_scrollbars(wfContext* wfc, UINT32 client_width, UINT32 client_height)
1236 const rdpSettings* settings;
1239 settings = wfc->common.context.settings;
1240 WINPR_ASSERT(settings);
1242 if (wfc->disablewindowtracking)
1246 wfc->disablewindowtracking = TRUE;
1251 wfc->xCurrentScroll = 0;
1252 wfc->yCurrentScroll = 0;
1254 if (wfc->xScrollVisible || wfc->yScrollVisible)
1256 if (ShowScrollBar(wfc->hwnd, SB_BOTH, FALSE))
1258 wfc->xScrollVisible = FALSE;
1259 wfc->yScrollVisible = FALSE;
1266 BOOL horiz = wfc->xScrollVisible;
1267 BOOL vert = wfc->yScrollVisible;
1276 settings, FreeRDP_DesktopWidth) )
1288 settings, FreeRDP_DesktopHeight) )
1293 if (horiz == vert && (horiz != wfc->xScrollVisible && vert != wfc->yScrollVisible))
1295 if (ShowScrollBar(wfc->hwnd, SB_BOTH, horiz))
1297 wfc->xScrollVisible = horiz;
1298 wfc->yScrollVisible = vert;
1302 if (horiz != wfc->xScrollVisible)
1304 if (ShowScrollBar(wfc->hwnd, SB_HORZ, horiz))
1306 wfc->xScrollVisible = horiz;
1310 if (vert != wfc->yScrollVisible)
1312 if (ShowScrollBar(wfc->hwnd, SB_VERT, vert))
1314 wfc->yScrollVisible = vert;
1325 wfc->xCurrentScroll = MIN(wfc->xCurrentScroll, wfc->xMaxScroll);
1326 si.cbSize =
sizeof(si);
1327 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
1328 si.nMin = wfc->xMinScroll;
1330 si.nPage = client_width;
1331 si.nPos = wfc->xCurrentScroll;
1332 SetScrollInfo(wfc->hwnd, SB_HORZ, &si, TRUE);
1340 wfc->yMaxScroll = MAX(
1342 wfc->yCurrentScroll = MIN(wfc->yCurrentScroll, wfc->yMaxScroll);
1343 si.cbSize =
sizeof(si);
1344 si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
1345 si.nMin = wfc->yMinScroll;
1347 si.nPage = client_height;
1348 si.nPos = wfc->yCurrentScroll;
1349 SetScrollInfo(wfc->hwnd, SB_VERT, &si, TRUE);
1353 wfc->disablewindowtracking = FALSE;
1354 wf_update_canvas_diff(wfc);
1357static BOOL wfreerdp_client_global_init(
void)
1361 WSAStartup(0x101, &wsaData);
1362 if (freerdp_handle_signals() != 0)
1365 if (freerdp_register_addin_provider(freerdp_channels_load_static_addin_entry, 0) !=
1372static void wfreerdp_client_global_uninit(
void)
1377static BOOL wfreerdp_client_new(freerdp* instance, rdpContext* context)
1379 wfContext* wfc = (wfContext*)context;
1385 wfc->isConsole = wf_has_console();
1387 if (!(wfreerdp_client_global_init()))
1390 WINPR_ASSERT(instance);
1391 instance->PreConnect = wf_pre_connect;
1392 instance->PostConnect = wf_post_connect;
1393 instance->PostDisconnect = wf_post_disconnect;
1394 instance->AuthenticateEx = wf_authenticate_ex;
1396#ifdef WITH_WINDOWS_CERT_STORE
1401 if (!wfc->isConsole)
1403 instance->VerifyCertificateEx = wf_verify_certificate_ex;
1404 instance->VerifyChangedCertificateEx = wf_verify_changed_certificate_ex;
1405 instance->PresentGatewayMessage = wf_present_gateway_message;
1408#ifdef WITH_PROGRESS_BAR
1409 CoInitializeEx(
nullptr, COINIT_APARTMENTTHREADED);
1410 CoCreateInstance(&CLSID_TaskbarList,
nullptr, CLSCTX_ALL, &IID_ITaskbarList3,
1411 (
void**)&wfc->taskBarList);
1417static void wfreerdp_client_free(freerdp* instance, rdpContext* context)
1419 WINPR_UNUSED(instance);
1423#ifdef WITH_PROGRESS_BAR
1428static int wfreerdp_client_start(rdpContext* context)
1430 wfContext* wfc = (wfContext*)context;
1432 WINPR_ASSERT(context);
1433 WINPR_ASSERT(context->settings);
1435 freerdp* instance = context->instance;
1436 WINPR_ASSERT(instance);
1438 rdpSettings* settings = context->settings;
1439 WINPR_ASSERT(settings);
1441 HINSTANCE hInstance = GetModuleHandle(
nullptr);
1446 wfc->hWndParent = hWndParent;
1450 typedef UINT(WINAPI * GetDpiForWindow_t)(HWND hwnd);
1451 typedef BOOL(WINAPI * SetProcessDPIAware_t)(void);
1453 HMODULE
module = GetModuleHandle(_T("User32"));
1456 GetDpiForWindow_t pGetDpiForWindow =
1457 GetProcAddressAs(module,
"GetDpiForWindow", GetDpiForWindow_t);
1458 SetProcessDPIAware_t pSetProcessDPIAware =
1459 GetProcAddressAs(module,
"SetProcessDPIAware", SetProcessDPIAware_t);
1460 if (pGetDpiForWindow && pSetProcessDPIAware)
1462 const UINT dpiAwareness = pGetDpiForWindow(hWndParent);
1463 if (dpiAwareness != USER_DEFAULT_SCREEN_DPI)
1464 pSetProcessDPIAware();
1466 FreeLibrary(module);
1474 wfc->systemMenuInsertPosition = 6;
1476 wfc->hInstance = hInstance;
1477 wfc->cursor = LoadCursor(
nullptr, IDC_ARROW);
1478 wfc->icon = LoadIcon(GetModuleHandle(
nullptr), MAKEINTRESOURCE(IDI_ICON1));
1479 wfc->wndClassName = _tcsdup(_T(
"FreeRDP"));
1480 wfc->wndClass.cbSize =
sizeof(WNDCLASSEX);
1481 wfc->wndClass.style = CS_HREDRAW | CS_VREDRAW;
1482 wfc->wndClass.lpfnWndProc = wf_event_proc;
1483 wfc->wndClass.cbClsExtra = 0;
1484 wfc->wndClass.cbWndExtra = 0;
1485 wfc->wndClass.hCursor =
nullptr;
1486 wfc->wndClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
1487 wfc->wndClass.lpszMenuName =
nullptr;
1488 wfc->wndClass.lpszClassName = wfc->wndClassName;
1489 wfc->wndClass.hInstance = hInstance;
1490 wfc->wndClass.hIcon = wfc->icon;
1491 wfc->wndClass.hIconSm = wfc->icon;
1492 RegisterClassEx(&(wfc->wndClass));
1493 wfc->keyboardThread =
1494 CreateThread(
nullptr, 0, wf_keyboard_thread, (
void*)wfc, 0, &wfc->keyboardThreadId);
1496 if (!wfc->keyboardThread)
1499 wfc->common.thread =
1500 CreateThread(
nullptr, 0, wf_client_thread, (
void*)instance, 0, &wfc->mainThreadId);
1502 if (!wfc->common.thread)
1508static int wfreerdp_client_stop(rdpContext* context)
1511 wfContext* wfc = (wfContext*)context;
1514 PostThreadMessage(wfc->mainThreadId, WM_QUIT, 0, 0);
1515 rc = freerdp_client_common_stop(context);
1516 wfc->mainThreadId = 0;
1518 if (wfc->keyboardThread)
1520 PostThreadMessage(wfc->keyboardThreadId, WM_QUIT, 0, 0);
1521 (void)WaitForSingleObject(wfc->keyboardThread, INFINITE);
1522 (void)CloseHandle(wfc->keyboardThread);
1523 wfc->keyboardThread =
nullptr;
1524 wfc->keyboardThreadId = 0;
1530int RdpClientEntry(RDP_CLIENT_ENTRY_POINTS* pEntryPoints)
1532 pEntryPoints->Version = 1;
1533 pEntryPoints->Size =
sizeof(RDP_CLIENT_ENTRY_POINTS_V1);
1534 pEntryPoints->GlobalInit = wfreerdp_client_global_init;
1535 pEntryPoints->GlobalUninit = wfreerdp_client_global_uninit;
1536 pEntryPoints->ContextSize =
sizeof(wfContext);
1537 pEntryPoints->ClientNew = wfreerdp_client_new;
1538 pEntryPoints->ClientFree = wfreerdp_client_free;
1539 pEntryPoints->ClientStart = wfreerdp_client_start;
1540 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.