2020-01-30 01:31:45 +00:00
|
|
|
/*
|
|
|
|
* This program source code file is part of KiCad, a free EDA CAD application.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2020 Ian McInerney <Ian.S.McInerney at ieee.org>
|
2022-02-09 14:01:30 +00:00
|
|
|
* Copyright (C) 2020-2022 KiCad Developers, see AUTHORS.txt for contributors.
|
2020-01-30 01:31:45 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation, either version 3 of the License, or (at your
|
|
|
|
* option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <kiplatform/environment.h>
|
|
|
|
#include <wx/intl.h>
|
2021-01-23 02:23:37 +00:00
|
|
|
#include <wx/filename.h>
|
|
|
|
#include <wx/stdpaths.h>
|
2020-01-30 01:31:45 +00:00
|
|
|
#include <wx/string.h>
|
2021-11-11 14:29:25 +00:00
|
|
|
#include <wx/tokenzr.h>
|
2021-06-16 17:57:51 +00:00
|
|
|
#include <wx/app.h>
|
2024-04-26 01:24:34 +00:00
|
|
|
#include <wx/uri.h>
|
2020-01-30 01:31:45 +00:00
|
|
|
|
|
|
|
#include <Windows.h>
|
|
|
|
#include <shellapi.h>
|
2020-12-16 00:28:58 +00:00
|
|
|
#include <shlwapi.h>
|
2021-11-11 14:29:25 +00:00
|
|
|
#include <winhttp.h>
|
2020-01-30 01:31:45 +00:00
|
|
|
|
2022-03-13 00:12:34 +00:00
|
|
|
#include <Softpub.h>
|
|
|
|
#include <wincrypt.h>
|
|
|
|
#include <wintrust.h>
|
|
|
|
|
2021-03-23 19:09:57 +00:00
|
|
|
|
|
|
|
void KIPLATFORM::ENV::Init()
|
|
|
|
{
|
|
|
|
// No tasks for this platform
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-30 01:31:45 +00:00
|
|
|
bool KIPLATFORM::ENV::MoveToTrash( const wxString& aPath, wxString& aError )
|
|
|
|
{
|
|
|
|
// The filename field must be a double-null terminated string
|
|
|
|
wxString temp = aPath + '\0';
|
|
|
|
|
|
|
|
SHFILEOPSTRUCT fileOp;
|
|
|
|
::ZeroMemory( &fileOp, sizeof( fileOp ) );
|
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
fileOp.hwnd = nullptr; // Set to null since there is no progress dialog
|
2020-01-30 01:31:45 +00:00
|
|
|
fileOp.wFunc = FO_DELETE;
|
|
|
|
fileOp.pFrom = temp.c_str();
|
2021-07-18 14:06:48 +00:00
|
|
|
fileOp.pTo = nullptr; // Set to to NULL since we aren't moving the file
|
2020-01-30 01:31:45 +00:00
|
|
|
fileOp.fFlags = FOF_ALLOWUNDO | FOF_NOERRORUI | FOF_NOCONFIRMATION | FOF_SILENT;
|
|
|
|
|
|
|
|
int eVal = SHFileOperation( &fileOp );
|
|
|
|
|
|
|
|
if( eVal != 0 )
|
|
|
|
{
|
|
|
|
aError = wxString::Format( _( "Error code: %d" ), eVal );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2020-12-16 00:28:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool KIPLATFORM::ENV::IsNetworkPath( const wxString& aPath )
|
|
|
|
{
|
|
|
|
return ::PathIsNetworkPathW( aPath.wc_str() );
|
2021-01-23 02:23:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-28 00:11:39 +00:00
|
|
|
wxString KIPLATFORM::ENV::GetDocumentsPath()
|
2021-01-23 02:23:37 +00:00
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
// If called by a python script in stand-alone (outside KiCad), wxStandardPaths::Get()
|
2021-06-16 08:18:39 +00:00
|
|
|
// complains about not existing app. so use a dummy app
|
|
|
|
if( wxTheApp == nullptr )
|
|
|
|
{
|
|
|
|
wxApp dummy;
|
|
|
|
return wxStandardPaths::Get().GetDocumentsDir();
|
|
|
|
}
|
|
|
|
|
2021-01-23 02:23:37 +00:00
|
|
|
return wxStandardPaths::Get().GetDocumentsDir();
|
2021-01-23 04:35:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-28 00:11:39 +00:00
|
|
|
wxString KIPLATFORM::ENV::GetUserConfigPath()
|
2021-01-23 04:35:09 +00:00
|
|
|
{
|
2021-07-18 14:06:48 +00:00
|
|
|
// If called by a python script in stand-alone (outside KiCad), wxStandardPaths::Get()
|
2021-06-16 08:18:39 +00:00
|
|
|
// complains about not existing app. so use a dummy app
|
|
|
|
if( wxTheApp == nullptr )
|
|
|
|
{
|
|
|
|
wxApp dummy;
|
|
|
|
return wxStandardPaths::Get().GetUserConfigDir();
|
|
|
|
}
|
|
|
|
|
2021-01-23 04:35:09 +00:00
|
|
|
return wxStandardPaths::Get().GetUserConfigDir();
|
2021-01-23 16:59:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-06-26 19:51:16 +00:00
|
|
|
wxString KIPLATFORM::ENV::GetUserDataPath()
|
|
|
|
{
|
|
|
|
// If called by a python script in stand-alone (outside KiCad), wxStandardPaths::Get()
|
|
|
|
// complains about not existing app. so use a dummy app
|
|
|
|
if( wxTheApp == nullptr )
|
|
|
|
{
|
|
|
|
wxApp dummy;
|
|
|
|
return wxStandardPaths::Get().GetUserDataDir();
|
|
|
|
}
|
|
|
|
|
|
|
|
return wxStandardPaths::Get().GetUserDataDir();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-07-12 05:57:30 +00:00
|
|
|
wxString KIPLATFORM::ENV::GetUserLocalDataPath()
|
|
|
|
{
|
|
|
|
// If called by a python script in stand-alone (outside KiCad), wxStandardPaths::Get()
|
|
|
|
// complains about not existing app. so use a dummy app
|
|
|
|
if( wxTheApp == nullptr )
|
|
|
|
{
|
|
|
|
wxApp dummy;
|
|
|
|
return wxStandardPaths::Get().GetUserLocalDataDir();
|
|
|
|
}
|
|
|
|
|
|
|
|
return wxStandardPaths::Get().GetUserLocalDataDir();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-01-28 00:11:39 +00:00
|
|
|
wxString KIPLATFORM::ENV::GetUserCachePath()
|
2021-01-23 16:59:19 +00:00
|
|
|
{
|
2021-06-09 19:32:58 +00:00
|
|
|
// Unfortunately AppData/Local is the closest analog to "Cache" directories of other platforms
|
2021-01-23 16:59:19 +00:00
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
// Make sure we don't include the "appinfo" (appended app name)
|
2021-06-16 08:18:39 +00:00
|
|
|
|
2021-07-18 14:06:48 +00:00
|
|
|
// If called by a python script in stand-alone (outside KiCad), wxStandardPaths::Get()
|
2021-06-16 08:18:39 +00:00
|
|
|
// complains about not existing app. so use a dummy app
|
|
|
|
if( wxTheApp == nullptr )
|
|
|
|
{
|
|
|
|
wxApp dummy;
|
|
|
|
wxStandardPaths::Get().UseAppInfo( wxStandardPaths::AppInfo_None );
|
|
|
|
|
|
|
|
return wxStandardPaths::Get().GetUserLocalDataDir();
|
|
|
|
}
|
|
|
|
|
2021-01-23 16:59:19 +00:00
|
|
|
wxStandardPaths::Get().UseAppInfo( wxStandardPaths::AppInfo_None );
|
|
|
|
|
|
|
|
return wxStandardPaths::Get().GetUserLocalDataDir();
|
2021-03-23 19:09:57 +00:00
|
|
|
}
|
2021-11-11 14:29:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool KIPLATFORM::ENV::GetSystemProxyConfig( const wxString& aURL, PROXY_CONFIG& aCfg )
|
|
|
|
{
|
2021-11-12 03:47:12 +00:00
|
|
|
// Original source from Microsoft sample (public domain)
|
|
|
|
// https://github.com/microsoft/Windows-classic-samples/blob/main/Samples/WinhttpProxy/cpp/GetProxy.cpp#L844
|
|
|
|
bool autoProxyDetect = false;
|
2021-11-11 14:29:25 +00:00
|
|
|
WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ieProxyConfig = { 0 };
|
|
|
|
WINHTTP_AUTOPROXY_OPTIONS autoProxyOptions = { 0 };
|
|
|
|
WINHTTP_PROXY_INFO autoProxyInfo = { 0 };
|
2021-11-12 03:56:45 +00:00
|
|
|
HINTERNET proxyResolveSession = NULL;
|
2021-11-12 03:47:12 +00:00
|
|
|
bool success = false;
|
2021-11-11 14:29:25 +00:00
|
|
|
|
2024-04-26 01:24:34 +00:00
|
|
|
wxURI uri( aURL );
|
|
|
|
|
|
|
|
LPWSTR proxyStr = NULL;
|
|
|
|
LPWSTR bypassProxyStr = NULL;
|
|
|
|
|
2021-11-11 14:29:25 +00:00
|
|
|
if( WinHttpGetIEProxyConfigForCurrentUser( &ieProxyConfig ) )
|
|
|
|
{
|
|
|
|
// welcome to the wonderful world of IE
|
|
|
|
// we use the ie config simply to handle it off to the other win32 api
|
|
|
|
if( ieProxyConfig.fAutoDetect )
|
|
|
|
{
|
2021-11-12 03:47:12 +00:00
|
|
|
autoProxyDetect = true;
|
2021-11-11 14:29:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if( ieProxyConfig.lpszAutoConfigUrl != NULL )
|
|
|
|
{
|
2021-11-12 03:47:12 +00:00
|
|
|
autoProxyDetect = true;
|
2021-11-11 14:29:25 +00:00
|
|
|
autoProxyOptions.lpszAutoConfigUrl = ieProxyConfig.lpszAutoConfigUrl;
|
|
|
|
}
|
|
|
|
}
|
2021-11-14 02:23:32 +00:00
|
|
|
else if( GetLastError() == ERROR_FILE_NOT_FOUND )
|
2021-11-11 14:29:25 +00:00
|
|
|
{
|
2021-11-14 02:23:32 +00:00
|
|
|
// this is the only error code where we want to continue attempting to find a proxy
|
2021-11-12 03:47:12 +00:00
|
|
|
autoProxyDetect = true;
|
2021-11-11 14:29:25 +00:00
|
|
|
}
|
|
|
|
|
2021-11-12 03:47:12 +00:00
|
|
|
if( autoProxyDetect )
|
2021-11-11 14:29:25 +00:00
|
|
|
{
|
2021-11-12 03:56:45 +00:00
|
|
|
proxyResolveSession =
|
2021-11-12 04:58:34 +00:00
|
|
|
WinHttpOpen( NULL, WINHTTP_ACCESS_TYPE_AUTOMATIC_PROXY, WINHTTP_NO_PROXY_NAME,
|
2021-11-12 03:56:45 +00:00
|
|
|
WINHTTP_NO_PROXY_BYPASS, WINHTTP_FLAG_ASYNC );
|
|
|
|
|
2021-11-12 04:58:34 +00:00
|
|
|
if( proxyResolveSession )
|
2021-11-11 14:29:25 +00:00
|
|
|
{
|
2021-11-12 04:58:34 +00:00
|
|
|
// either we use the ie url or we set the auto detect mode
|
|
|
|
if( autoProxyOptions.lpszAutoConfigUrl != NULL )
|
|
|
|
{
|
|
|
|
autoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_CONFIG_URL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
autoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;
|
|
|
|
autoProxyOptions.dwAutoDetectFlags =
|
|
|
|
WINHTTP_AUTO_DETECT_TYPE_DHCP | WINHTTP_AUTO_DETECT_TYPE_DNS_A;
|
|
|
|
}
|
2021-11-11 14:29:25 +00:00
|
|
|
|
2021-11-14 02:23:32 +00:00
|
|
|
// dont do auto logon at first, this allows windows to use an cache
|
|
|
|
// per https://docs.microsoft.com/en-us/windows/win32/winhttp/autoproxy-cache
|
|
|
|
autoProxyOptions.fAutoLogonIfChallenged = FALSE;
|
2021-11-11 14:29:25 +00:00
|
|
|
|
2021-11-12 04:58:34 +00:00
|
|
|
autoProxyDetect = WinHttpGetProxyForUrl( proxyResolveSession, aURL.c_str(),
|
|
|
|
&autoProxyOptions, &autoProxyInfo );
|
2021-11-11 14:29:25 +00:00
|
|
|
|
2021-11-14 02:23:32 +00:00
|
|
|
if( !autoProxyDetect && GetLastError() == ERROR_WINHTTP_LOGIN_FAILURE )
|
|
|
|
{
|
|
|
|
autoProxyOptions.fAutoLogonIfChallenged = TRUE;
|
|
|
|
|
|
|
|
// try again with auto login now
|
|
|
|
autoProxyDetect = WinHttpGetProxyForUrl( proxyResolveSession, aURL.c_str(),
|
|
|
|
&autoProxyOptions, &autoProxyInfo );
|
|
|
|
}
|
|
|
|
|
2024-04-26 01:24:34 +00:00
|
|
|
if( autoProxyDetect )
|
|
|
|
{
|
|
|
|
if( autoProxyInfo.dwAccessType == WINHTTP_ACCESS_TYPE_NAMED_PROXY )
|
|
|
|
{
|
|
|
|
proxyStr = autoProxyInfo.lpszProxy;
|
|
|
|
bypassProxyStr = autoProxyInfo.lpszProxyBypass;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-12 03:56:45 +00:00
|
|
|
WinHttpCloseHandle( proxyResolveSession );
|
2021-11-11 14:29:25 +00:00
|
|
|
}
|
2024-04-26 01:24:34 +00:00
|
|
|
}
|
2021-11-12 04:58:34 +00:00
|
|
|
|
2024-04-26 01:24:34 +00:00
|
|
|
if( !autoProxyDetect && ieProxyConfig.lpszProxy != NULL )
|
|
|
|
{
|
|
|
|
proxyStr = ieProxyConfig.lpszProxy;
|
|
|
|
bypassProxyStr = ieProxyConfig.lpszProxyBypass;
|
2021-11-11 14:29:25 +00:00
|
|
|
}
|
|
|
|
|
2024-04-26 01:24:34 +00:00
|
|
|
bool bypassed = false;
|
|
|
|
if( bypassProxyStr != NULL )
|
2021-11-11 14:29:25 +00:00
|
|
|
{
|
2024-04-26 01:24:34 +00:00
|
|
|
wxStringTokenizer tokenizer( bypassProxyStr, wxT( ";" ) );
|
|
|
|
|
|
|
|
while( tokenizer.HasMoreTokens() )
|
2021-11-11 14:29:25 +00:00
|
|
|
{
|
2024-04-26 01:24:34 +00:00
|
|
|
wxString host = tokenizer.GetNextToken();
|
2021-11-11 14:29:25 +00:00
|
|
|
|
2024-04-26 01:24:34 +00:00
|
|
|
if( host == uri.GetServer() )
|
2021-11-11 14:29:25 +00:00
|
|
|
{
|
2024-04-26 01:24:34 +00:00
|
|
|
// the given url has a host in the proxy bypass list
|
|
|
|
return false;
|
2021-11-11 14:29:25 +00:00
|
|
|
}
|
|
|
|
|
2024-04-26 01:24:34 +00:00
|
|
|
// <local> is a special case that says all local sites bypass
|
|
|
|
// the windows way for considering local is any host without periods in the name that would imply
|
|
|
|
// some non-internal dns resolution
|
|
|
|
if( host == "<local>" )
|
|
|
|
{
|
|
|
|
if( !uri.GetServer().Contains( "." ) )
|
|
|
|
{
|
|
|
|
// great its a local uri that is bypassed
|
|
|
|
bypassed = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-11-11 14:29:25 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-26 01:24:34 +00:00
|
|
|
|
|
|
|
if( !bypassed && proxyStr != NULL )
|
2021-11-11 14:29:25 +00:00
|
|
|
{
|
2024-04-26 01:24:34 +00:00
|
|
|
// proxyStr can be in the following format per MSDN
|
|
|
|
//([<scheme>=][<scheme>"://"]<server>[":"<port>])
|
|
|
|
//and separated by semicolons or whitespace
|
|
|
|
wxStringTokenizer tokenizer( proxyStr, wxT( "; \t" ) );
|
|
|
|
|
|
|
|
while( tokenizer.HasMoreTokens() )
|
2021-11-11 14:29:25 +00:00
|
|
|
{
|
2024-04-26 01:24:34 +00:00
|
|
|
wxString entry = tokenizer.GetNextToken();
|
2021-11-11 14:29:25 +00:00
|
|
|
|
2024-04-26 01:24:34 +00:00
|
|
|
// deal with the [<scheme>=] part, which may or may not exist
|
|
|
|
if( entry.Contains( "=" ) )
|
|
|
|
{
|
|
|
|
wxString scheme = entry.BeforeFirst( '=' ).Lower();
|
|
|
|
entry = entry.AfterFirst( '=' );
|
2021-11-11 14:29:25 +00:00
|
|
|
|
2024-04-26 01:24:34 +00:00
|
|
|
// skip processing if the scheme doesnt match
|
|
|
|
if( scheme != uri.GetScheme().Lower() )
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we continue with the [<scheme>=] stripped off if we matched
|
|
|
|
}
|
|
|
|
|
|
|
|
// is the entry left not empty? we just take the first result
|
|
|
|
// : and :: are also special cases we want to ignore
|
|
|
|
if( entry != "" && entry != ":" && entry != "::" )
|
2021-11-11 14:29:25 +00:00
|
|
|
{
|
2024-04-26 01:24:34 +00:00
|
|
|
aCfg.host = entry;
|
2021-11-12 03:47:12 +00:00
|
|
|
success = true;
|
2024-04-26 01:24:34 +00:00
|
|
|
break;
|
2021-11-11 14:29:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-26 01:24:34 +00:00
|
|
|
|
2021-11-12 03:47:12 +00:00
|
|
|
// We have to clean up the strings the win32 api returned
|
|
|
|
if( autoProxyInfo.lpszProxy )
|
|
|
|
{
|
|
|
|
GlobalFree( autoProxyInfo.lpszProxy );
|
|
|
|
autoProxyInfo.lpszProxy = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( autoProxyInfo.lpszProxyBypass )
|
|
|
|
{
|
|
|
|
GlobalFree( autoProxyInfo.lpszProxyBypass );
|
|
|
|
autoProxyInfo.lpszProxyBypass = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ieProxyConfig.lpszAutoConfigUrl != NULL )
|
|
|
|
{
|
|
|
|
GlobalFree( ieProxyConfig.lpszAutoConfigUrl );
|
|
|
|
ieProxyConfig.lpszAutoConfigUrl = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ieProxyConfig.lpszProxy != NULL )
|
|
|
|
{
|
|
|
|
GlobalFree( ieProxyConfig.lpszProxy );
|
|
|
|
ieProxyConfig.lpszProxy = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if( ieProxyConfig.lpszProxyBypass != NULL )
|
|
|
|
{
|
|
|
|
GlobalFree( ieProxyConfig.lpszProxyBypass );
|
|
|
|
ieProxyConfig.lpszProxyBypass = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
2021-11-11 14:29:25 +00:00
|
|
|
}
|
2022-03-13 00:12:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
bool KIPLATFORM::ENV::VerifyFileSignature( const wxString& aPath )
|
|
|
|
{
|
|
|
|
WINTRUST_FILE_INFO fileData;
|
|
|
|
memset( &fileData, 0, sizeof( fileData ) );
|
|
|
|
fileData.cbStruct = sizeof( WINTRUST_FILE_INFO );
|
|
|
|
fileData.pcwszFilePath = aPath.wc_str();
|
|
|
|
|
|
|
|
// verifies entire certificate chain
|
|
|
|
GUID policy = WINTRUST_ACTION_GENERIC_VERIFY_V2;
|
|
|
|
|
|
|
|
WINTRUST_DATA trustData;
|
|
|
|
memset( &trustData, 0, sizeof( trustData ) );
|
|
|
|
|
|
|
|
trustData.cbStruct = sizeof( trustData );
|
|
|
|
trustData.dwUIChoice = WTD_UI_NONE;
|
|
|
|
// revocation checking incurs latency penalities due to need for online queries
|
|
|
|
trustData.fdwRevocationChecks = WTD_REVOKE_NONE;
|
|
|
|
trustData.dwUnionChoice = WTD_CHOICE_FILE;
|
|
|
|
trustData.dwStateAction = WTD_STATEACTION_VERIFY;
|
|
|
|
trustData.pFile = &fileData;
|
|
|
|
|
|
|
|
|
|
|
|
bool verified = false;
|
|
|
|
LONG status = WinVerifyTrust( NULL, &policy, &trustData );
|
|
|
|
|
|
|
|
verified = ( status == ERROR_SUCCESS );
|
|
|
|
|
|
|
|
// Cleanup/release (yes its weird looking)
|
|
|
|
trustData.dwStateAction = WTD_STATEACTION_CLOSE;
|
|
|
|
WinVerifyTrust( NULL, &policy, &trustData );
|
|
|
|
|
|
|
|
return verified;
|
|
|
|
}
|