1
0
mirror of https://github.com/Ylianst/MeshAgent synced 2025-12-11 13:53:37 +00:00
This commit is contained in:
Bryan Roe
2020-04-29 03:01:44 -07:00
parent c7959a8332
commit 71bd469551
11 changed files with 127 additions and 376 deletions

View File

@@ -2341,7 +2341,7 @@ int GenerateSHA384FileHash(char *filePath, char *fileHash)
#ifdef WIN32
int retVal = 1;
fopen_s(&tmpFile, filePath, "rb");
_wfopen_s(&tmpFile, ILibUTF8ToWide(filePath, -1), L"rb");
#else
tmpFile = fopen(filePath, "rb");
#endif
@@ -3587,7 +3587,7 @@ void checkForEmbeddedMSH(MeshAgentHostContainer *agent)
int mshLen;
#ifdef WIN32
fopen_s(&tmpFile, agent->exePath, "rb");
_wfopen_s(&tmpFile, ILibUTF8ToWide(agent->exePath, -1), L"rb");
#else
tmpFile = fopen(agent->exePath, "rb");
#endif
@@ -3608,7 +3608,7 @@ void checkForEmbeddedMSH(MeshAgentHostContainer *agent)
{
FILE *msh = NULL;
#ifdef WIN32
fopen_s(&msh, MeshAgent_MakeAbsolutePath(agent->exePath, ".msh"), "wb");
_wfopen_s(&msh, ILibUTF8ToWide(MeshAgent_MakeAbsolutePath(agent->exePath, ".msh"), -1), L"wb");
#else
msh = fopen(MeshAgent_MakeAbsolutePath(agent->exePath, ".msh"), "wb");
#endif
@@ -4779,7 +4779,9 @@ int MeshAgent_Start(MeshAgentHostContainer *agentHost, int paramLen, char **para
exePath[0] = 0;
#ifdef WIN32
GetModuleFileName(NULL, exePath, sizeof(exePath));
WCHAR tmpExePath[2048];
GetModuleFileNameW(NULL, tmpExePath, sizeof(tmpExePath)/2);
WideCharToMultiByte(CP_UTF8, 0, (LPCWCH)tmpExePath, -1, (LPSTR)exePath, (int)sizeof(exePath), NULL, NULL);
#elif defined(__APPLE__)
if (_NSGetExecutablePath(exePath, &len) != 0) ILIBCRITICALEXIT(247);
exePath[(int)len] = 0;
@@ -4831,7 +4833,7 @@ int MeshAgent_Start(MeshAgentHostContainer *agentHost, int paramLen, char **para
{
strncpy_s(ILibScratchPad2, sizeof(ILibScratchPad2), param[0], x);
ILibScratchPad2[x] = 0;
SetCurrentDirectory(ILibScratchPad2);
SetCurrentDirectoryW(ILibUTF8ToWide(ILibScratchPad2, -1));
}
#endif
@@ -4863,7 +4865,7 @@ int MeshAgent_Start(MeshAgentHostContainer *agentHost, int paramLen, char **para
{
int i, ptr = 0;
#ifdef WIN32
STARTUPINFOA info = { sizeof(info) };
STARTUPINFOW info = { sizeof(info) };
PROCESS_INFORMATION processInfo;
#endif
// Get the update executable path
@@ -4883,14 +4885,14 @@ int MeshAgent_Start(MeshAgentHostContainer *agentHost, int paramLen, char **para
#ifdef WIN32
// Windows version
sprintf_s(ILibScratchPad, sizeof(ILibScratchPad), "%s -update:\"%s\"%s", updateFilePath, agentHost->exePath, str);
if (!CreateProcessA(NULL, ILibScratchPad, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &info, &processInfo))
if (!CreateProcessW(NULL, ILibUTF8ToWide(ILibScratchPad, -1), NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &info, &processInfo))
{
// We tried to execute a bad executable... not good. Lets try to recover.
if (agentHost->logUpdate != 0) { ILIBLOGMESSSAGE("SelfUpdate -> FAILED..."); }
if (updateFilePath != NULL && agentHost->exePath != NULL)
{
while (util_CopyFile(agentHost->exePath, updateFilePath, FALSE) == FALSE) Sleep(5000);
if (CreateProcessA(NULL, ILibScratchPad, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &info, &processInfo))
if (CreateProcessW(NULL, ILibUTF8ToWide(ILibScratchPad, -1), NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &info, &processInfo))
{
CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);
@@ -5008,7 +5010,7 @@ void MeshAgent_Stop(MeshAgentHostContainer *agent)
void MeshAgent_PerformSelfUpdate(char* selfpath, char* exepath, int argc, char **argv)
{
int i, ptr = 0;
STARTUPINFOA info = { sizeof(info) };
STARTUPINFOW info = { sizeof(info) };
PROCESS_INFORMATION processInfo;
// Sleep for 5 seconds, this will give some time for the calling process to get going.
@@ -5023,7 +5025,7 @@ void MeshAgent_PerformSelfUpdate(char* selfpath, char* exepath, int argc, char *
while (util_CopyFile(selfpath, exepath, FALSE) == FALSE) Sleep(5000);
// Now run the process
if (!CreateProcessA(NULL, ILibScratchPad2, NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo))
if (!CreateProcessW(NULL, ILibUTF8ToWide(ILibScratchPad2, -1), NULL, NULL, TRUE, 0, NULL, NULL, &info, &processInfo))
{
// TODO: Failed to run update.
}

View File

@@ -152,7 +152,7 @@ int signcheck_verifysign(char* filename, int upgrade)
cert.x509 = NULL;
#ifdef WIN32
fopen_s(&pFile, filename, "rb");
_wfopen_s(&pFile, ILibUTF8ToWide(filename, -1), L"rb");
#else
pFile = fopen(filename, "rb");
#endif

View File

@@ -95,17 +95,18 @@ BOOL IsAdmin()
return admin;
}
BOOL RunAsAdmin(char* args) {
char szPath[_MAX_PATH + 100];
if (GetModuleFileNameA(NULL, szPath, _MAX_PATH))
BOOL RunAsAdmin(char* args, int isAdmin)
{
WCHAR szPath[_MAX_PATH + 100];
if (GetModuleFileNameW(NULL, szPath, sizeof(szPath)/2))
{
SHELLEXECUTEINFO sei = { sizeof(sei) };
SHELLEXECUTEINFOW sei = { sizeof(sei) };
sei.hwnd = NULL;
sei.nShow = SW_NORMAL;
sei.lpVerb = "runas";
sei.lpVerb = isAdmin?L"open":L"runas";
sei.lpFile = szPath;
sei.lpParameters = args;
return ShellExecuteExA(&sei);
sei.lpParameters = ILibUTF8ToWide(args, -1);
return ShellExecuteExW(&sei);
}
return FALSE;
}
@@ -596,27 +597,6 @@ int StopService(LPCSTR servicename)
return r;
}
int RunProcess(char* exe, int waitForExit)
{
BOOL r = TRUE;
int count = 50;
DWORD exitcode;
STARTUPINFOA info = {sizeof(info)};
PROCESS_INFORMATION processInfo;
if (CreateProcessA(NULL, exe, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &info, &processInfo) == 0) return 0;
if (waitForExit != 0) {
do
{
Sleep(100);
r = GetExitCodeProcess(processInfo.hProcess, &exitcode);
if (exitcode == STILL_ACTIVE) r = 0;
} while (r == 0 && count-- > 0);
}
CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);
return r;
}
/*
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
@@ -630,287 +610,6 @@ int APIENTRY _tWinMain(HINSTANCE hInstance,
}
*/
#ifndef _MINCORE
void fullinstall(int uninstallonly, char* proxy, int proxylen, char* tag, int taglen)
{
int r = 0;
int loops = 0;
char targetexe2[_MAX_PATH + 40];
char *targetexe = targetexe2 + 1;
size_t targetexelen = 0;
char selfexe[_MAX_PATH];
size_t selfexelen = 0;
char setup1[_MAX_PATH];
char setup2[_MAX_PATH];
int setup1len;
int setup2len;
if (IsAdmin() == FALSE) { printf("Requires administrator permissions.\r\n"); return; }
if (uninstallonly != 0) { printf("Performing uninstall...\r\n"); } else { printf("Performing install...\r\n"); }
HKEY hKey;
DWORD len = 0;
if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Services\\Mesh Agent", 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
{
if (RegQueryValueExA(hKey, "ImagePath", NULL, NULL, NULL, &len) == ERROR_SUCCESS && len > 0)
{
char *ipath = ILibMemory_Allocate(len, 0, NULL, NULL);
RegQueryValueExA(hKey, "ImagePath", NULL, NULL, ipath, &len);
STARTUPINFOA info = { sizeof(info) };
PROCESS_INFORMATION processInfo;
sprintf_s(ILibScratchPad, sizeof(ILibScratchPad), "%s -exec \"try { require('service-manager').manager.uninstallService('meshagentDiagnostic'); require('task-scheduler').delete('meshagentDiagnostic/periodicStart').then(function(){process.exit();}, function(){process.exit();}); } catch(e){process.exit();}\"", ipath);
CreateProcessA(NULL, ILibScratchPad, NULL, NULL, TRUE, CREATE_NO_WINDOW, NULL, NULL, &info, &processInfo);
CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);
free(ipath);
}
RegCloseKey(hKey);
}
// Stop and remove the service
StopService(serviceFile);
// Wait for the service to stop
int serviceStateLoopCount = 0;;
int serviceState;
do {
serviceStateLoopCount++;
Sleep(100);
serviceState = GetServiceState(serviceFile);
} while ((serviceState == 3) && (serviceStateLoopCount < 400));
UninstallService(serviceFile);
UninstallService(serviceFileOld);
// Get our own executable
selfexelen = GetModuleFileNameA(NULL, selfexe, _MAX_PATH);
// Get the target executable
if (SHGetFolderPathA(NULL, CSIDL_PROGRAM_FILES | CSIDL_FLAG_CREATE, NULL, SHGFP_TYPE_CURRENT, targetexe) != S_FALSE)
{
targetexe2[0] = '\"';
targetexelen = strnlen_s(targetexe, _MAX_PATH + 40);
if (targetexelen <= MAX_PATH) memcpy_s(targetexe + targetexelen, _MAX_PATH + 40 - targetexelen, "\\Mesh Agent\\MeshAgent.exe\" -uninstall", 38);
targetexelen += 25;
}
// Check if we are uninstalling ourself
if ((uninstallonly != 0) && (targetexelen == selfexelen) && (memcmp(selfexe, targetexe, targetexelen) == 0)) {
// Copy ourself to a temp folder and run full uninstall.
char tempPath[_MAX_PATH + 40];
int tempPathLen = GetTempPathA(_MAX_PATH, tempPath);
memcpy_s(tempPath + tempPathLen, _MAX_PATH + 40 - tempPathLen, "MeshAgent.exe\0", 15);
remove(tempPath);
util_CopyFile(selfexe, tempPath, FALSE);
memcpy_s(tempPath + tempPathLen, _MAX_PATH + 40 - tempPathLen, "MeshAgent.exe -fulluninstall\0", 30);
RunProcess(tempPath, 0); // Don't wait for the process to terminate since we want to self-delete.
return;
}
// Call uninstall, this will remove the firewall rules.
RunProcess(targetexe2, 1);
#ifdef _MINCORE
// Remove the MeshAgent registry keys
RegDeleteKeyEx(HKEY_LOCAL_MACHINE, "Software\\Open Source\\MeshAgent2", KEY_WOW64_32KEY, 0);
RegDeleteKeyEx(HKEY_CURRENT_USER, "Software\\Open Source\\MeshAgent2", KEY_WOW64_32KEY, 0);
#else
// Remove the MeshAgent registry keys
RegDeleteKey(HKEY_LOCAL_MACHINE, "Software\\Open Source\\MeshAgent2");
RegDeleteKey(HKEY_CURRENT_USER, "Software\\Open Source\\MeshAgent2");
#endif
// Remove the uninstall icon from the control panel if present
RemoveUninstallIcon();
// Check if selfexe is already located at the target, if so, skip to copy steps.
if ((uninstallonly != 0) || (targetexelen != selfexelen) || (memcmp(selfexe, targetexe, targetexelen) != 0))
{
// Remove the target executable, wait if needed
int selfExeDelLoopCount = 0;;
int selfExeDel;
targetexe[targetexelen] = 0;
do {
Sleep(100);
selfExeDelLoopCount++;
selfExeDel = remove(targetexe);
} while ((selfExeDel != 0) && (selfExeDel != -1) && (selfExeDelLoopCount < 400));
// Remove "[Executable].msh" file
if ((setup2len = (int)strnlen_s(targetexe, _MAX_PATH + 40)) < 4 || setup2len > 259) return;
memcpy_s(setup2, sizeof(setup2), targetexe, setup2len);
memcpy_s(setup2 + (setup2len - 3), sizeof(setup2) - setup2len - 3, "msh", 4);
setup2[setup2len] = 0;
remove(setup2);
// Remove "[Executable].mshx" file
if ((setup2len = (int)strnlen_s(targetexe, _MAX_PATH + 40)) < 4 || setup2len > 259) return;
memcpy_s(setup2, sizeof(setup2), targetexe, setup2len);
memcpy_s(setup2 + (setup2len - 3), sizeof(setup2) - setup2len - 3, "mshx", 5);
setup2[setup2len + 1] = 0;
remove(setup2);
// Remove "[Executable].proxy" file
if ((setup2len = (int)strnlen_s(targetexe, _MAX_PATH + 40)) < 4 || setup2len > 257) return;
memcpy_s(setup2, sizeof(setup2), targetexe, setup2len);
memcpy_s(setup2 + (setup2len - 3), sizeof(setup2) - setup2len - 3, "proxy", 6);
setup2[setup2len + 2] = 0;
remove(setup2);
// Remove "[Executable].tag" file
if ((setup2len = (int)strnlen_s(targetexe, _MAX_PATH + 40)) < 4 || setup2len > 259) return;
memcpy_s(setup2, sizeof(setup2), targetexe, setup2len);
memcpy_s(setup2 + (setup2len - 3), sizeof(setup2) - setup2len - 3, "tag", 4);
setup2[setup2len] = 0;
remove(setup2);
// Remove "[Executable].log" file
if ((setup2len = (int)strnlen_s(targetexe, _MAX_PATH + 40)) < 4 || setup2len > 259) return;
memcpy_s(setup2, sizeof(setup2), targetexe, setup2len);
memcpy_s(setup2 + (setup2len - 3), sizeof(setup2) - setup2len - 3, "log", 4);
setup2[setup2len] = 0;
remove(setup2);
// Remove "[Executable].db" file
if ((setup2len = (int)strnlen_s(targetexe, _MAX_PATH + 40)) < 4 || setup2len > 256) return;
memcpy_s(setup2, sizeof(setup2), targetexe, setup2len);
memcpy_s(setup2 + (setup2len - 3), sizeof(setup2) - setup2len - 3, "db", 3);
setup2[setup2len] = 0;
remove(setup2);
// Remove the folder.
targetexe[targetexelen - 14] = 0;
RemoveDirectoryA(targetexe);
if (uninstallonly != 0) return;
// Get the target executable, create folders if needed
if (!CreateDirectoryA(targetexe, NULL) && GetLastError() == ERROR_ACCESS_DENIED) { ILIBMESSAGE("Access denied (1)"); return; }
targetexe[targetexelen - 14] = '\\';
// Attempt to copy our own exe over the original exe
loops = 0;
while (!util_CopyFile(selfexe, targetexe, TRUE))
{
if (GetLastError() == ERROR_ACCESS_DENIED) { ILIBMESSAGE("Access denied (2)"); return; }
if (loops++ > 5) { ILIBMESSAGE("Error copying executable file"); return; }
Sleep(5000);
}
// Try to copy "[Executable].msh" file to target directory
if ((setup1len = (int)strnlen_s(selfexe, sizeof(selfexe))) < 4) return;
memcpy_s(setup1, sizeof(setup1), selfexe, setup1len);
memcpy_s(setup1 + (setup1len - 3), sizeof(setup1) - setup1len - 3, "msh", 4);
if ((setup2len = (int)strnlen_s(targetexe, _MAX_PATH + 40)) < 4 || setup2len > 259) return;
memcpy_s(setup2, sizeof(setup2), targetexe, setup2len);
memcpy_s(setup2 + (setup2len - 3), sizeof(setup2) - setup2len - 3, "msh", 4);
util_CopyFile(setup1, setup2, TRUE);
// Write the tag if one was passed
if (tag != NULL)
{
FILE *SourceFile = NULL;
if ((setup2len = (int)strnlen_s(targetexe, _MAX_PATH + 40)) < 4 || setup2len > 259) return;
memcpy_s(setup2, sizeof(setup2), targetexe, setup2len);
memcpy_s(setup2 + (setup2len - 3), sizeof(setup2) - setup2len - 3, "tag", 4);
if (taglen > 0) {
fopen_s(&SourceFile, setup2, "wb");
if (SourceFile != NULL)
{
if (fwrite(tag, sizeof(char), taglen, SourceFile)) {}
fclose(SourceFile);
}
}
else
{
remove(setup2);
}
}
// Setup proxy filenames
if ((setup1len = (int)strnlen_s(selfexe, sizeof(selfexe))) < 4) return;
memcpy_s(setup1, sizeof(setup1), selfexe, setup1len);
memcpy_s(setup1 + (setup1len - 3), sizeof(setup1) - setup1len - 3, "proxy", 6);
if ((setup2len = (int)strnlen_s(targetexe, _MAX_PATH + 40)) < 4 || setup2len > 259) return;
memcpy_s(setup2, sizeof(setup2), targetexe, setup2len);
memcpy_s(setup2 + (setup2len - 3), sizeof(setup2) - setup2len - 3, "proxy", 6);
if (proxy != NULL && proxylen > 0)
{
// Use the specified proxy in the command line switch
FILE *SourceFile = NULL;
fopen_s(&SourceFile, setup2, "wb");
if (SourceFile != NULL)
{
if (fwrite(proxy, sizeof(char), proxylen, SourceFile)) {}
fclose(SourceFile);
}
}
else
{
// Try to copy "[Executable].proxy" file to target directory
if (util_CopyFile(setup1, setup2, TRUE) == FALSE)
{
// Failed to copy proxy file, lets try to create one.
WINHTTP_CURRENT_USER_IE_PROXY_CONFIG proxyEx;
if (WinHttpGetIEProxyConfigForCurrentUser(&proxyEx))
{
if (proxyEx.lpszProxy != NULL)
{
FILE *SourceFile = NULL;
size_t len;
if (wcstombs_s(&len, ILibScratchPad, 4095, proxyEx.lpszProxy, 2000) == 0)
{
char* ptr = strstr(ILibScratchPad, "https=");
if (ptr != NULL) {
char* ptr2 = strstr(ptr, ";");
ptr += 6;
if (ptr2 != NULL) ptr2[0] = 0;
} else {
ptr = ILibScratchPad;
}
fopen_s(&SourceFile, setup2, "wb");
if (SourceFile != NULL)
{
if (fwrite(ptr, sizeof(char), strnlen_s(ptr, sizeof(ILibScratchPad)), SourceFile)) {}
fclose(SourceFile);
}
}
GlobalFree(proxyEx.lpszProxy);
}
// Release the rest of the proxy settings
if (proxyEx.lpszAutoConfigUrl != NULL) GlobalFree(proxyEx.lpszAutoConfigUrl);
if (proxyEx.lpszProxyBypass != NULL) GlobalFree(proxyEx.lpszProxyBypass);
}
}
}
}
// Add the uninstall icon in the control panel
AddUninstallIcon();
UpdateOwnerData();
/*
#if defined(_LINKVM)
// Setup the SendSAS permission
kvm_setupSasPermissions();
#endif
*/
// Attempt to start the updated service up again
memcpy(targetexe + targetexelen, "\" -install", 11);
r = RunProcess(targetexe2, 1);
memcpy(targetexe + targetexelen, "\" -start", 9);
r = RunProcess(targetexe2, 1);
}
#endif
ILibTransport_DoneState kvm_serviceWriteSink(char *buffer, int bufferLen, void *reserved)
{
@@ -1366,14 +1065,6 @@ int wmain(int argc, char* wargv[])
int len = MeshInfo_GetSystemInformation(&data);
if (len > 0) { printf(data); }
}
else if (argc > 1 && (strcasecmp(argv[1], "-fullinstall") == 0))
{
fullinstall( 0, proxyarg, (int)strnlen_s(proxyarg, _MAX_PATH), tagarg, (int)strnlen_s(tagarg, _MAX_PATH));
}
else if (argc > 1 && (strcasecmp(argv[1], "-fulluninstall") == 0))
{
fullinstall(1, NULL, 0, NULL, 0);
}
else if (argc > 1 && (strcasecmp(argv[1], "-setfirewall") == 0))
{
// Reset the firewall rules
@@ -1548,7 +1239,7 @@ char* getMshSettings(char* fileName, char* selfexe, char** meshname, char** mesh
char exeMeshPolicyGuid[] = { 0xB9, 0x96, 0x01, 0x58, 0x80, 0x54, 0x4A, 0x19, 0xB7, 0xF7, 0xE9, 0xBE, 0x44, 0x91, 0x4C, 0x19 };
char tmpHash[16];
fopen_s(&tmpFile, selfexe, "rb");
_wfopen_s(&tmpFile, ILibUTF8ToWide(selfexe, -1), L"rb");
if (tmpFile == NULL) { return NULL; } // Could not open our own executable
fseek(tmpFile, -16, SEEK_END);
@@ -1653,7 +1344,7 @@ DWORD WINAPI StartTempAgent(_In_ LPVOID lpParameter)
{
ptr = ILibScratchPad;
}
fopen_s(&SourceFile, setup1, "wb");
_wfopen_s(&SourceFile, ILibUTF8ToWide(setup1, -1), L"wb");
if (SourceFile != NULL)
{
if (fwrite(ptr, sizeof(char), strnlen_s(ptr, sizeof(ILibScratchPad)), SourceFile)) {}
@@ -1830,16 +1521,13 @@ INT_PTR CALLBACK DialogHandler(HWND hDlg, UINT message, WPARAM wParam, LPARAM lP
EnableWindow( GetDlgItem( hDlg, IDC_UNINSTALLBUTTON ), FALSE );
EnableWindow( GetDlgItem( hDlg, IDCANCEL ), FALSE );
if (IsAdmin() == TRUE)
if (LOWORD(wParam) == IDC_INSTALLBUTTON)
{
// We are already administrator, just install/uninstall now.
if (LOWORD(wParam) == IDC_INSTALLBUTTON) { fullinstall(0, NULL, 0, NULL, 0); } else { fullinstall(1, NULL, 0, NULL, 0); }
result = TRUE;
result = RunAsAdmin("-fullinstall", IsAdmin() == TRUE);
}
else
{
// We need to request admin escalation
if (LOWORD(wParam) == IDC_INSTALLBUTTON) { result = RunAsAdmin("-fullinstall"); } else { result = RunAsAdmin("-fulluninstall"); }
result = RunAsAdmin("-fulluninstall", IsAdmin() == TRUE);
}
if (result)

View File

@@ -260,7 +260,7 @@ void ILibDuktape_ScriptContainer_GetEmbeddedJS_Raw(char *exePath, char **script,
int integratedJavaScriptLen = 0;
FILE* tmpFile = NULL;
fopen_s(&tmpFile, exePath, "rb");
_wfopen_s(&tmpFile, ILibUTF8ToWide(exePath, -1), L"rb");
if (tmpFile != NULL)
{
// Read the PE Headers, to determine where to look for the Embedded JS
@@ -338,7 +338,7 @@ void ILibDuktape_ScriptContainer_CheckEmbeddedEx(char *exePath, char **script, i
{
i = sprintf_s(g_AgentCrashID, sizeof(g_AgentCrashID), "%s_", exePath);
sprintf_s(ILibScratchPad, sizeof(ILibScratchPad), "%s.exe", exePath);
fopen_s(&tmpFile, ILibScratchPad, "rb");
_wfopen_s(&tmpFile, ILibUTF8ToWide(ILibScratchPad, -1), L"rb");
}
else
{
@@ -373,7 +373,7 @@ void ILibDuktape_ScriptContainer_CheckEmbeddedEx(char *exePath, char **script, i
}
#ifdef WIN32
fopen_s(&tmpFile, exePath, "rb");
_wfopen_s(&tmpFile, ILibUTF8ToWide(exePath, -1), L"rb");
#else
tmpFile = fopen(exePath, "rb");
#endif
@@ -3144,6 +3144,7 @@ void ILibDuktape_ScriptContainer_StdErrSink_MicrostackThread(void *chain, void *
int bufferLen = ((int*)buffer)[0];
void *ptr;
int i;
duk_context *ctx = master->ctx;
if (ILibDuktape_ScriptContainer_DecodeJSON(master->ctx, buffer+4, bufferLen-4) == 0)
{
@@ -3160,7 +3161,7 @@ void ILibDuktape_ScriptContainer_StdErrSink_MicrostackThread(void *chain, void *
duk_push_string(master->ctx, json); // [emit][this][data][str]
duk_json_decode(master->ctx, -1); // [emit][this][data][json]
if (duk_pcall_method(master->ctx, 2) != 0) { ILibDuktape_Process_UncaughtExceptionEx(master->ctx, "ScriptContainer.OnData(): "); }
duk_pop(master->ctx);
duk_pop(ctx);
}
}
break;
@@ -3181,7 +3182,7 @@ void ILibDuktape_ScriptContainer_StdErrSink_MicrostackThread(void *chain, void *
ILibDuktape_EventEmitter_SetupEmit(master->ctx, master->emitter->object, "error"); // [emit][this][error]
duk_get_prop_string(master->ctx, -4, "error"); // [emit][this][error][errorObj]
if (duk_pcall_method(master->ctx, 2) != 0) { ILibDuktape_Process_UncaughtExceptionEx(master->ctx, "ScriptContainer_OnError_Dispatch(): "); }
duk_pop(master->ctx); // ...
duk_pop(ctx); // ...
}
else
{
@@ -3193,7 +3194,7 @@ void ILibDuktape_ScriptContainer_StdErrSink_MicrostackThread(void *chain, void *
duk_push_false(master->ctx); // [func][this][false]
duk_get_prop_string(master->ctx, -4, "error"); // [func][this][false][error]
if (duk_pcall_method(master->ctx, 2) != 0) { ILibDuktape_Process_UncaughtExceptionEx(master->ctx, "ScriptContainer_OnError_Dispatch(): "); }
duk_pop(master->ctx); // ...
duk_pop(ctx); // ...
}
}
}
@@ -3215,7 +3216,7 @@ void ILibDuktape_ScriptContainer_StdErrSink_MicrostackThread(void *chain, void *
duk_push_undefined(master->ctx); // [func][this][true][undefined]
}
if (duk_pcall_method(master->ctx, 2) != 0) { ILibDuktape_Process_UncaughtExceptionEx(master->ctx, "ScriptContainer_OnExec_Dispatch(): "); }
duk_pop(master->ctx); // ...
duk_pop(ctx); // ...
}
}
break;
@@ -3223,11 +3224,14 @@ void ILibDuktape_ScriptContainer_StdErrSink_MicrostackThread(void *chain, void *
default:
break;
}
duk_pop(master->ctx); // ...
duk_pop(ctx); // ...
}
#ifdef WIN32
if (master->child != NULL) { ILibProcessPipe_Pipe_Resume(ILibProcessPipe_Process_GetStdErr(master->child)); }
if (ILibMemory_CanaryOK(master))
{
if (master->child != NULL) { ILibProcessPipe_Pipe_Resume(ILibProcessPipe_Process_GetStdErr(master->child)); }
}
#endif
}
void ILibDuktape_ScriptContainer_StdErrSink(ILibProcessPipe_Process sender, char *buffer, int bufferLen, int* bytesConsumed, void* user)

View File

@@ -1287,7 +1287,7 @@ void ILibduktape_fs_watch_appleWorker(void *obj)
duk_ret_t ILibDuktape_fs_watch(duk_context *ctx)
{
#ifdef WIN32
char *path = (char*)duk_require_string(ctx, 0);
WCHAR *path = (WCHAR*)ILibDuktape_String_AsWide(ctx, 0, NULL);
#else
char *path = ILibDuktape_fs_fixLinuxPath((char*)duk_require_string(ctx, 0));
#endif
@@ -1408,7 +1408,7 @@ duk_ret_t ILibDuktape_fs_watch(duk_context *ctx)
#if defined(WIN32)
if ((data->overlapped.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL)) == NULL) { return(ILibDuktape_Error(ctx, "Could not create handle")); }
data->h = CreateFile(path, FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, NULL);
data->h = CreateFileW(path, FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, NULL);
if (data->h == INVALID_HANDLE_VALUE) { return(ILibDuktape_Error(ctx, "fs.watch(): Invalid Path or Access Denied")); }
if (ReadDirectoryChangesW(data->h, data->results, sizeof(data->results), recursive, FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_CREATION | FILE_NOTIFY_CHANGE_SIZE | FILE_NOTIFY_CHANGE_LAST_WRITE | FILE_NOTIFY_CHANGE_LAST_ACCESS, NULL, &(data->overlapped), NULL) == 0)

View File

@@ -125,7 +125,7 @@ int __fastcall util_sha384file(char* filename, char* result)
if (filename == NULL) return -1;
#ifdef WIN32
fopen_s(&pFile, filename, "rbN");
_wfopen_s(&pFile, ILibUTF8ToWide(filename, -1), L"rbN");
#else
pFile = fopen(filename, "rb");
#endif
@@ -264,7 +264,7 @@ size_t __fastcall util_writefile(char* filename, char* data, int datalen)
size_t count = 0;
#ifdef WIN32
fopen_s(&pFile, filename, "wbN");
_wfopen_s(&pFile, ILibUTF8ToWide(filename, -1), L"wbN");
#else
pFile = fopen(filename, "wb");
#endif
@@ -283,7 +283,7 @@ size_t __fastcall util_appendfile(char* filename, char* data, int datalen)
size_t count = 0;
#ifdef WIN32
fopen_s(&pFile, filename, "abN");
_wfopen_s(&pFile, ILibUTF8ToWide(filename, -1), L"abN");
#else
pFile = fopen(filename, "ab");
#endif
@@ -306,7 +306,7 @@ size_t __fastcall util_readfile(char* filename, char** data, size_t maxlen)
if (filename == NULL) return 0;
#ifdef WIN32
fopen_s(&pFile, filename, "rbN");
_wfopen_s(&pFile, ILibUTF8ToWide(filename, -1), L"rbN");
#else
pFile = fopen(filename, "rb");
#endif
@@ -367,7 +367,11 @@ int __fastcall util_readfile2(char* filename, char** data)
int __fastcall util_deletefile(char* filename)
{
if (filename == NULL) return 0;
return remove(filename);
#ifdef WIN32
return(_wremove(ILibUTF8ToWide(filename, -1)));
#else
return(remove(filename));
#endif
}
#ifdef WIN32
@@ -407,8 +411,26 @@ BOOL util_CopyFile(_In_ LPCSTR lpExistingFileName, _In_ LPCSTR lpNewFileName, _I
return (CopyFile2(lpExistingFileNameW, lpNewFileNameW, NULL) == S_OK);
}
#else
BOOL util_MoveFile(_In_ LPCSTR lpExistingFileName, _In_ LPCSTR lpNewFileName) { return MoveFileA(lpExistingFileName, lpNewFileName); }
BOOL util_CopyFile(_In_ LPCSTR lpExistingFileName, _In_ LPCSTR lpNewFileName, _In_ BOOL bFailIfExists) { return CopyFileA(lpExistingFileName, lpNewFileName, bFailIfExists); }
BOOL util_MoveFile(_In_ LPCSTR lpExistingFileName, _In_ LPCSTR lpNewFileName)
{
WCHAR wExisting[4096];
WCHAR wNew[4096];
MultiByteToWideChar(CP_UTF8, 0, (LPCCH)lpExistingFileName, -1, (LPWSTR)wExisting, (int)sizeof(wExisting) / 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCCH)lpNewFileName, -1, (LPWSTR)wNew, (int)sizeof(wNew) / 2);
return MoveFileW(wExisting, wNew);
}
BOOL util_CopyFile(_In_ LPCSTR lpExistingFileName, _In_ LPCSTR lpNewFileName, _In_ BOOL bFailIfExists)
{
WCHAR wExisting[4096];
WCHAR wNew[4096];
MultiByteToWideChar(CP_UTF8, 0, (LPCCH)lpExistingFileName, -1, (LPWSTR)wExisting, (int)sizeof(wExisting) / 2);
MultiByteToWideChar(CP_UTF8, 0, (LPCCH)lpNewFileName, -1, (LPWSTR)wNew, (int)sizeof(wNew) / 2);
return CopyFileW(wExisting, wNew, bFailIfExists);
}
#endif
#endif
@@ -593,7 +615,7 @@ int __fastcall util_from_pem(char* filename, struct util_cert* cert)
if (filename == NULL) return -1;
#ifdef WIN32
fopen_s(&pFile, filename, "rbN");
_wfopen_s(&pFile, ILibUTF8ToWide(filename, -1), L"rbN");
#else
pFile = fopen(filename, "rb");
#endif

View File

@@ -179,6 +179,22 @@ int ILibWhichPowerOfTwo(int number)
return retVal;
}
#ifdef WIN32
WCHAR ILibWideScratchPad[4096];
char ILibUTF8ScratchPad[4096];
char *ILibWideToUTF8(WCHAR* wstr, int len)
{
WideCharToMultiByte(CP_UTF8, 0, (LPCWCH)wstr, len, (LPSTR)ILibUTF8ScratchPad, (int)sizeof(ILibUTF8ScratchPad), NULL, NULL);
return((char*)ILibUTF8ScratchPad);
}
WCHAR* ILibUTF8ToWideEx(char* str, int len, WCHAR* buffer, int bufferCharacterSize)
{
if (buffer == NULL) { buffer = (WCHAR*)ILibWideScratchPad; bufferCharacterSize = (int)sizeof(ILibWideScratchPad) / 2; }
MultiByteToWideChar(CP_UTF8, 0, (LPCCH)str, len, (LPWSTR)buffer, bufferCharacterSize);
return(buffer);
}
#endif
//
// All of the following structures are meant to be private internal structures
@@ -8062,7 +8078,7 @@ int ILibReadFileFromDiskEx(char **Target, char *FileName)
FILE *SourceFile = NULL;
#ifdef WIN32
fopen_s(&SourceFile, FileName, "rb");
_wfopen_s(&SourceFile, ILibUTF8ToWide(FileName, -1), L"rb");
#else
SourceFile = fopen(FileName, "rb");
#endif
@@ -8111,7 +8127,7 @@ void ILibWriteStringToDiskEx(char *FileName, char *data, int dataLen)
FILE *SourceFile = NULL;
#ifdef WIN32
fopen_s(&SourceFile, FileName, "wb");
_wfopen_s(&SourceFile, ILibUTF8ToWide(FileName, -1), L"wb");
#else
SourceFile = fopen(FileName, "wb");
#endif
@@ -8127,7 +8143,7 @@ void ILibAppendStringToDiskEx(char *FileName, char *data, int dataLen)
FILE *SourceFile = NULL;
#ifdef WIN32
fopen_s(&SourceFile, FileName, "ab");
_wfopen_s(&SourceFile, ILibUTF8ToWide(FileName, -1), L"ab");
#else
SourceFile = fopen(FileName, "ab");
#endif
@@ -9647,7 +9663,7 @@ void ILibLinkedList_FileBacked_Reset(ILibLinkedList_FileBacked_Root *root)
fclose(source);
source = NULL;
#ifdef WIN32
fopen_s(&source, (char*)ptr[1], "wb+N");
_wfopen_s(&source, ILibUTF8ToWide((char*)ptr[1], -1), L"wb+N");
#else
source = fopen((char*)ptr[1], "wb+");
#endif
@@ -9721,9 +9737,9 @@ ILibLinkedList_FileBacked_Root* ILibLinkedList_FileBacked_Create(char* path, uns
ILibLinkedList_FileBacked_Root *retVal = NULL;
#ifdef WIN32
if (fopen_s(&source, path, "rb+N") != 0)
if (_wfopen_s(&source, ILibUTF8ToWide(path, -1), L"rb+N") != 0)
{
fopen_s(&source, path, "wb+N");
_wfopen_s(&source, ILibUTF8ToWide(path, -1), L"wb+N");
}
#else
if ((source = fopen(path, "rb+")) == NULL)

View File

@@ -170,6 +170,21 @@ static inline void ignore_result(uintptr_t result) { (void)result; }
#define PRINTERROR()
#endif
#ifdef WIN32
char *ILibWideToUTF8(WCHAR* wstr, int len);
WCHAR* ILibUTF8ToWideEx(char* str, int len, WCHAR* buffer, int bufferCharacterSize);
#define ILibUTF8ToWide(utf8string, len) ILibUTF8ToWideEx(utf8string, len, NULL, 0)
#else
#define ILibWideToUTF8(wstr, len) (wstr)
#define ILibUTF8toWide(str, len) (str)
#endif
int ILibGetLocalTime(char *dest, int destLen);
long ILibGetTimeStamp();

View File

@@ -797,7 +797,7 @@ ILibProcessPipe_Process ILibProcessPipe_Manager_SpawnProcessEx4(ILibProcessPipe_
if (needSetSid != 0) { spawnType ^= ILibProcessPipe_SpawnTypes_POSIX_DETACHED; }
#ifdef WIN32
STARTUPINFOA info = { 0 };
STARTUPINFOW info = { 0 };
PROCESS_INFORMATION processInfo = { 0 };
SECURITY_ATTRIBUTES saAttr;
char* parms = NULL;
@@ -806,7 +806,7 @@ ILibProcessPipe_Process ILibProcessPipe_Manager_SpawnProcessEx4(ILibProcessPipe_
int allocParms = 0;
ZeroMemory(&processInfo, sizeof(PROCESS_INFORMATION));
ZeroMemory(&info, sizeof(STARTUPINFOA));
ZeroMemory(&info, sizeof(STARTUPINFOW));
if (spawnType != ILibProcessPipe_SpawnTypes_SPECIFIED_USER && spawnType != ILibProcessPipe_SpawnTypes_DEFAULT && (sessionId = WTSGetActiveConsoleSessionId()) == 0xFFFFFFFF) { return(NULL); } // No session attached to console, but requested to execute as logged in user
@@ -817,7 +817,7 @@ ILibProcessPipe_Process ILibProcessPipe_Manager_SpawnProcessEx4(ILibProcessPipe_
if (DuplicateTokenEx(token, MAXIMUM_ALLOWED, 0, SecurityImpersonation, TokenPrimary, &userToken) == 0) { CloseHandle(token); ILIBMARKPOSITION(2); return(NULL); }
if (spawnType == ILibProcessPipe_SpawnTypes_SPECIFIED_USER) { sessionId = (DWORD)(uint64_t)sid; }
if (SetTokenInformation(userToken, (TOKEN_INFORMATION_CLASS)TokenSessionId, &sessionId, sizeof(sessionId)) == 0) { CloseHandle(token); CloseHandle(userToken); ILIBMARKPOSITION(2); return(NULL); }
if (spawnType == ILibProcessPipe_SpawnTypes_WINLOGON) { info.lpDesktop = "Winsta0\\Winlogon"; }
if (spawnType == ILibProcessPipe_SpawnTypes_WINLOGON) { info.lpDesktop = L"Winsta0\\Winlogon"; }
}
if (parameters != NULL && parameters[0] != NULL && parameters[1] == NULL)
{
@@ -861,8 +861,10 @@ ILibProcessPipe_Process ILibProcessPipe_Manager_SpawnProcessEx4(ILibProcessPipe_
retVal->parent = (ILibProcessPipe_Manager_Object*)pipeManager;
retVal->chain = retVal->parent->ChainLink.ParentChain;
#ifdef WIN32
WCHAR tmp1[4096];
WCHAR tmp2[4096];
info.cb = sizeof(STARTUPINFOA);
info.cb = sizeof(STARTUPINFOW);
if (spawnType != ILibProcessPipe_SpawnTypes_DETACHED)
{
retVal->stdIn = ILibProcessPipe_CreatePipe(pipeManager, 4096, NULL, extraMemorySize);
@@ -880,8 +882,8 @@ ILibProcessPipe_Process ILibProcessPipe_Manager_SpawnProcessEx4(ILibProcessPipe_
info.dwFlags |= STARTF_USESTDHANDLES;
}
if (((spawnType == ILibProcessPipe_SpawnTypes_DEFAULT || spawnType == ILibProcessPipe_SpawnTypes_DETACHED) && !CreateProcessA(target, parms, NULL, NULL, spawnType == ILibProcessPipe_SpawnTypes_DETACHED ? FALSE: TRUE, CREATE_NO_WINDOW | (needSetSid !=0? (DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP) : 0x00), envvars, NULL, &info, &processInfo)) ||
(spawnType != ILibProcessPipe_SpawnTypes_DEFAULT && !CreateProcessAsUserA(userToken, target, parms, NULL, NULL, TRUE, CREATE_NO_WINDOW | (needSetSid != 0 ? (DETACHED_PROCESS| CREATE_NEW_PROCESS_GROUP) : 0x00), envvars, NULL, &info, &processInfo)))
if (((spawnType == ILibProcessPipe_SpawnTypes_DEFAULT || spawnType == ILibProcessPipe_SpawnTypes_DETACHED) && !CreateProcessW(ILibUTF8ToWideEx(target, -1, tmp1, (int)sizeof(tmp1)/2), ILibUTF8ToWideEx(parms, -1, tmp2, (int)sizeof(tmp2)/2), NULL, NULL, spawnType == ILibProcessPipe_SpawnTypes_DETACHED ? FALSE: TRUE, CREATE_NO_WINDOW | (needSetSid !=0? (DETACHED_PROCESS | CREATE_NEW_PROCESS_GROUP) : 0x00), envvars, NULL, &info, &processInfo)) ||
(spawnType != ILibProcessPipe_SpawnTypes_DEFAULT && !CreateProcessAsUserW(userToken, ILibUTF8ToWideEx(target, -1, tmp1, (int)sizeof(tmp1)/2), ILibUTF8ToWideEx(parms, -1, tmp2, (int)sizeof(tmp2)/2), NULL, NULL, TRUE, CREATE_NO_WINDOW | (needSetSid != 0 ? (DETACHED_PROCESS| CREATE_NEW_PROCESS_GROUP) : 0x00), envvars, NULL, &info, &processInfo)))
{
if (spawnType != ILibProcessPipe_SpawnTypes_DETACHED)
{

View File

@@ -501,18 +501,18 @@ FILE* ILibSimpleDataStore_OpenFileEx2(char* filePath, int forceTruncateIfNonZero
HANDLE h = NULL;
if (forceTruncateIfNonZero != 0)
{
h = CreateFile(filePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, TRUNCATE_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
h = CreateFileW(ILibUTF8ToWide(filePath, -1), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, TRUNCATE_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND)
{
h = CreateFile(filePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
h = CreateFileW(ILibUTF8ToWide(filePath, -1), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
}
}
else
{
h = CreateFile(filePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
h = CreateFileW(ILibUTF8ToWide(filePath, -1), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE && GetLastError() == ERROR_FILE_NOT_FOUND)
{
h = CreateFile(filePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
h = CreateFileW(ILibUTF8ToWide(filePath, -1), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
}
}
int fd = _open_osfhandle((intptr_t)h, _O_RDWR);
@@ -522,7 +522,7 @@ FILE* ILibSimpleDataStore_OpenFileEx2(char* filePath, int forceTruncateIfNonZero
}
else
{
HANDLE h = CreateFile(filePath, GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
HANDLE h = CreateFileW(ILibUTF8ToWide(filePath, -1), GENERIC_READ, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (h == INVALID_HANDLE_VALUE) { return(NULL); }
int fd = _open_osfhandle((intptr_t)h, _O_RDONLY);
if (fd == -1) { CloseHandle(h); return(NULL); }
@@ -547,7 +547,7 @@ FILE* ILibSimpleDataStore_OpenFileEx2(char* filePath, int forceTruncateIfNonZero
int ILibSimpleDataStore_Exists(char *filePath)
{
#ifdef WIN32
return(_access(filePath, 0) == 0 ? 1 : 0);
return(_waccess(ILibUTF8ToWide(filePath, -1), 0) == 0 ? 1 : 0);
#else
return(access(filePath, 0) == 0 ? 1 : 0);
#endif
@@ -858,8 +858,10 @@ __EXPORT_TYPE int ILibSimpleDataStore_Compact(ILibSimpleDataStore dataStore)
// Now we copy the temporary data store over the data store, making it the new valid version
#ifdef WIN32
if (CopyFileA(tmp, root->filePath, FALSE) == FALSE) { retVal = 1; }
DeleteFile(tmp);
WCHAR tmptmp[4096];
MultiByteToWideChar(CP_UTF8, 0, (LPCCH)tmp, -1, (LPWSTR)tmptmp, (int)sizeof(tmptmp) / 2);
if (CopyFileW(tmptmp, ILibUTF8ToWide(root->filePath, -1), FALSE) == FALSE) { retVal = 1; }
DeleteFileW(tmptmp);
#else
if (rename(tmp, root->filePath) != 0) { retVal = 1; }
#endif

View File

@@ -120,7 +120,6 @@ function messageBox()
ret._container.promise = ret;
ret._container.on('data', function (j)
{
this.promise._container = null;
if(j == IDYES || j == IDOK)
{
this.promise._res();
@@ -129,6 +128,7 @@ function messageBox()
{
this.promise._rej('Denied');
}
this.promise._container = null;
});
ret._container.on('exit', function ()
{