2022-09-28 17:28:58 +03:00
|
|
|
/**
|
|
|
|
* FreeRDP: A Remote Desktop Protocol Implementation
|
|
|
|
*
|
|
|
|
* Copyright 2022 Stefan Koell
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-10-04 17:06:22 +03:00
|
|
|
#include <windows.h>
|
2022-09-27 15:19:30 +03:00
|
|
|
#include <wincred.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <malloc.h>
|
2022-09-27 18:15:58 +03:00
|
|
|
#include <freerdp/settings.h>
|
2022-09-27 15:19:30 +03:00
|
|
|
|
2024-04-03 12:39:52 +03:00
|
|
|
#include "wf_defaults.h"
|
|
|
|
|
2022-09-28 09:20:24 +03:00
|
|
|
static PCWSTR ValidateString(const BYTE* pb, ULONG cb)
|
2022-09-27 18:15:58 +03:00
|
|
|
{
|
|
|
|
if (!pb || !cb)
|
|
|
|
return 0;
|
2022-09-27 15:19:30 +03:00
|
|
|
|
2022-09-28 18:28:51 +03:00
|
|
|
if (cb % sizeof(WCHAR) != 0)
|
2022-09-27 18:15:58 +03:00
|
|
|
return 0;
|
2022-09-27 15:19:30 +03:00
|
|
|
|
2022-09-27 18:15:58 +03:00
|
|
|
return (PCWSTR)pb;
|
2022-09-27 15:19:30 +03:00
|
|
|
}
|
|
|
|
|
2022-09-28 09:20:24 +03:00
|
|
|
static void AddDefaultSettings_I(rdpSettings* settings, size_t idHostname, size_t idUsername,
|
2023-09-12 16:56:14 +03:00
|
|
|
size_t idDomain, size_t idPassword)
|
2022-09-27 15:19:30 +03:00
|
|
|
{
|
2022-09-28 17:28:58 +03:00
|
|
|
static const PSTR TERMSRV = "TERMSRV/%s";
|
|
|
|
|
2023-07-28 09:38:08 +03:00
|
|
|
PSTR TargetName = NULL;
|
|
|
|
PSTR UserName = NULL;
|
|
|
|
PWSTR TargetNameW = NULL;
|
2023-09-12 16:56:14 +03:00
|
|
|
PWSTR ServerHostNameW = NULL;
|
|
|
|
PWSTR ParsedUserNameW = NULL;
|
|
|
|
PWSTR ParsedDomainW = NULL;
|
|
|
|
PWSTR PasswordNullTerminatedW = NULL;
|
2022-09-28 18:28:51 +03:00
|
|
|
PCREDENTIALW Credential = { 0 };
|
2022-09-28 17:28:58 +03:00
|
|
|
|
2022-09-27 15:19:30 +03:00
|
|
|
PCSTR ServerHostname = freerdp_settings_get_string(settings, idHostname);
|
|
|
|
|
|
|
|
if (!ServerHostname)
|
|
|
|
return;
|
|
|
|
|
2022-09-27 18:15:58 +03:00
|
|
|
BOOL bExistUserName = freerdp_settings_get_string(settings, idUsername) != 0;
|
|
|
|
BOOL bExistPassword = freerdp_settings_get_string(settings, idPassword) != 0;
|
2022-09-27 15:19:30 +03:00
|
|
|
|
|
|
|
if (bExistUserName && bExistPassword)
|
|
|
|
return;
|
|
|
|
|
2022-09-28 17:28:58 +03:00
|
|
|
int len = _snprintf(TargetName, 0, TERMSRV, ServerHostname);
|
2022-09-27 18:15:58 +03:00
|
|
|
len++;
|
|
|
|
TargetName = (PSTR)malloc(len);
|
2022-09-28 17:28:58 +03:00
|
|
|
|
|
|
|
if (!TargetName)
|
2022-09-28 18:28:51 +03:00
|
|
|
goto fail;
|
2022-09-28 17:28:58 +03:00
|
|
|
|
|
|
|
_snprintf(TargetName, len, TERMSRV, ServerHostname);
|
2022-09-27 18:15:58 +03:00
|
|
|
|
|
|
|
TargetName[len - 1] = 0;
|
2022-09-27 15:19:30 +03:00
|
|
|
|
2022-10-28 09:09:27 +03:00
|
|
|
TargetNameW = ConvertUtf8ToWCharAlloc(TargetName, NULL);
|
|
|
|
if (!TargetNameW)
|
2022-09-28 18:28:51 +03:00
|
|
|
goto fail;
|
2022-09-28 17:28:58 +03:00
|
|
|
|
|
|
|
if (!CredReadW(TargetNameW, CRED_TYPE_GENERIC, 0, &Credential))
|
2022-09-28 18:28:51 +03:00
|
|
|
goto fail;
|
2022-09-27 18:15:58 +03:00
|
|
|
|
|
|
|
if (!bExistPassword)
|
2022-09-27 15:19:30 +03:00
|
|
|
{
|
2023-07-28 09:38:08 +03:00
|
|
|
const WCHAR* PasswordW =
|
|
|
|
ValidateString(Credential->CredentialBlob, Credential->CredentialBlobSize);
|
2022-09-27 18:15:58 +03:00
|
|
|
|
2023-09-12 16:56:14 +03:00
|
|
|
PasswordNullTerminatedW = (PWSTR)calloc(Credential->CredentialBlobSize + 1, sizeof(WCHAR));
|
|
|
|
|
|
|
|
if (!PasswordNullTerminatedW)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
memcpy(PasswordNullTerminatedW, PasswordW, Credential->CredentialBlobSize * sizeof(WCHAR));
|
|
|
|
|
|
|
|
if (PasswordNullTerminatedW)
|
2022-09-27 15:19:30 +03:00
|
|
|
{
|
2023-09-13 11:18:13 +03:00
|
|
|
if (!freerdp_settings_set_string_from_utf16(settings, idPassword,
|
|
|
|
PasswordNullTerminatedW))
|
|
|
|
{
|
2022-10-28 09:09:27 +03:00
|
|
|
goto fail;
|
2023-09-13 11:18:13 +03:00
|
|
|
}
|
2022-09-27 15:19:30 +03:00
|
|
|
}
|
2022-09-27 18:15:58 +03:00
|
|
|
}
|
2022-09-27 15:19:30 +03:00
|
|
|
|
2022-09-27 18:15:58 +03:00
|
|
|
if (!bExistUserName)
|
|
|
|
{
|
2023-07-28 09:38:08 +03:00
|
|
|
const WCHAR* UserNameW = Credential->UserName;
|
2022-09-28 17:28:58 +03:00
|
|
|
|
|
|
|
if (UserNameW)
|
2022-09-27 18:15:58 +03:00
|
|
|
{
|
2023-09-12 16:56:14 +03:00
|
|
|
ParsedUserNameW = calloc(CREDUI_MAX_USERNAME_LENGTH + 1, sizeof(WCHAR));
|
|
|
|
if (!ParsedUserNameW)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
ParsedDomainW = calloc(CREDUI_MAX_DOMAIN_TARGET_LENGTH + 1, sizeof(WCHAR));
|
|
|
|
if (!ParsedDomainW)
|
2022-10-28 09:09:27 +03:00
|
|
|
goto fail;
|
2023-09-12 16:56:14 +03:00
|
|
|
|
|
|
|
DWORD ParseResult =
|
|
|
|
CredUIParseUserNameW(UserNameW, ParsedUserNameW, CREDUI_MAX_USERNAME_LENGTH + 1,
|
|
|
|
ParsedDomainW, CREDUI_MAX_DOMAIN_TARGET_LENGTH + 1);
|
|
|
|
|
|
|
|
if (ParseResult == NO_ERROR)
|
|
|
|
{
|
|
|
|
if (!freerdp_settings_set_string_from_utf16(settings, idUsername, ParsedUserNameW))
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
if (*ParsedDomainW != 0)
|
|
|
|
{
|
|
|
|
if (!freerdp_settings_set_string_from_utf16(settings, idDomain, ParsedDomainW))
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (ParseResult == ERROR_INVALID_ACCOUNT_NAME)
|
|
|
|
{
|
|
|
|
if (!freerdp_settings_set_string_from_utf16(settings, idUsername, UserNameW))
|
|
|
|
goto fail;
|
|
|
|
}
|
2022-09-27 18:15:58 +03:00
|
|
|
}
|
2022-09-27 15:19:30 +03:00
|
|
|
}
|
2022-09-27 18:15:58 +03:00
|
|
|
|
2022-09-28 18:28:51 +03:00
|
|
|
fail:
|
2023-09-12 16:56:14 +03:00
|
|
|
if (Credential)
|
|
|
|
{
|
|
|
|
CredFree(Credential);
|
|
|
|
}
|
2022-09-28 18:28:51 +03:00
|
|
|
free(TargetName);
|
|
|
|
free(UserName);
|
2023-09-12 16:56:14 +03:00
|
|
|
free(TargetNameW);
|
|
|
|
free(ServerHostNameW);
|
|
|
|
free(ParsedUserNameW);
|
|
|
|
free(ParsedDomainW);
|
|
|
|
free(PasswordNullTerminatedW);
|
2022-09-28 18:28:51 +03:00
|
|
|
return;
|
2022-09-27 15:19:30 +03:00
|
|
|
}
|
|
|
|
|
2022-09-28 17:28:58 +03:00
|
|
|
void WINAPI AddDefaultSettings(rdpSettings* settings)
|
2022-09-27 15:19:30 +03:00
|
|
|
{
|
2023-09-13 11:18:13 +03:00
|
|
|
AddDefaultSettings_I(settings, FreeRDP_ServerHostname, FreeRDP_Username, FreeRDP_Domain,
|
|
|
|
FreeRDP_Password);
|
|
|
|
AddDefaultSettings_I(settings, FreeRDP_GatewayHostname, FreeRDP_GatewayUsername,
|
|
|
|
FreeRDP_GatewayDomain, FreeRDP_GatewayPassword);
|
2022-09-28 17:28:58 +03:00
|
|
|
}
|