bool FileSystemManager::launchFile(QString filePath, LPWSTR verb)
{
	int rcVal = 0;

	// check if we should use the pidl
	LPCITEMIDLIST pidl = NULL;	
	int virtualIconIndex = winOS->GetIconTypeFromFileName(filePath);
	if (virtualIconIndex > -1)
		pidl = winOS->GetPidlFromName(virtualIconIndex);

	// ensure that all the arguments are quoted
	QString wPath(filePath);
	ensureQuoted(wPath);

	SHELLEXECUTEINFO sei = {0};

	sei.cbSize = sizeof(sei);
	sei.hwnd = winOS->GetWindowsHandle();
	sei.lpVerb = verb;

	if (pidl)
		sei.lpIDList = (LPVOID) pidl;
	else
		sei.lpFile = (LPCTSTR) wPath.utf16();

	sei.nShow = SW_SHOWNORMAL;

	return launchFile(sei,filePath);
}
Exemple #2
0
	std::vector<std::string> local::listdir(std::string path) {
		std::vector<std::string> files;
#if defined(_WIN32)
		/* append "\*" to path and create wstring */
		path += "\\*";
		std::wstring wPath(path.begin(), path.end());

		WIN32_FIND_DATAW fdd;
		HANDLE handle = FindFirstFileW(wPath.c_str(), &fdd);
		if(handle == INVALID_HANDLE_VALUE) {
			debugmanager()->fatal(path + ": failed to find first file");
		}
		do {
			if(!(fdd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
				std::wstring wFile = fdd.cFileName;
				files.emplace_back(wFile.begin(), wFile.end());
			}
		} while(FindNextFileW(handle, &fdd));

		/* check for errors */
		DWORD dwError = GetLastError();
		if(dwError != ERROR_NO_MORE_FILES) {
			FindClose(handle);
			debugmanager()->fatal("failed to find next file");
		}

		FindClose(handle);
#elif defined(__APPLE__) || defined(__linux__)
		DIR *dp = opendir(path.c_str());
		if(dp == nullptr) {
			debugmanager()->fatal(path, ": failed to open directory");
		}

		struct dirent *ep;
		while((ep = readdir(dp))) {
			struct stat st;
			int result =
			    fstatat(dirfd(dp), ep->d_name, &st, AT_SYMLINK_NOFOLLOW);
			if(result < 0) {
				debugmanager()->fatal(path, ": fstatat failed on ", ep->d_name);
			}

			if(!S_ISDIR(st.st_mode)) {
				debugmanager()->warning(ep->d_name);
				files.emplace_back(ep->d_name);
			}
		}
		closedir(dp);
#else
		debugmanager()->fatal("FileSystem::ListDir: not implemented");
#endif
		return files;
	}
void installService(const char* szName, const char* szPath, const char* szDispName)
{
	gcAssert(szName);
	gcAssert(szPath);

	gcWString wName(szName);
	gcWString wPath(szPath);
	gcWString wDispName(szDispName?szDispName:szName);

	SC_HANDLE newService;
	SC_HANDLE scm; 

	//open connection to SCM
	scm = OpenSCManager(nullptr, nullptr, SC_MANAGER_CREATE_SERVICE);

	if (!scm)
		throw gcException(ERR_NULLSCMANAGER, GetLastError(), "Failed to open the Service Control Manager");

	//install service
	newService = CreateService(
		scm,						//scm database
		wName.c_str(),					//service name
		wDispName.c_str(),				//display name
		GENERIC_READ|GENERIC_EXECUTE,	//access rights to the service
		SERVICE_WIN32_OWN_PROCESS,	//service type
		SERVICE_DEMAND_START,		//service start type
		SERVICE_ERROR_NORMAL,		//error control type
		wPath.c_str(),				//service path
		nullptr,						//no load ordering group 
		nullptr,						//no tag identifier
		nullptr,						//no dependencies	
		nullptr,						//LocalSystem account
		nullptr);						//no password

	if(!newService)
	{
		CloseServiceHandle(scm);
		throw gcException(ERR_NULLSERVICE, GetLastError(), gcString("Failed to create new service: {0}", szName));
	}

	//clean up
	CloseServiceHandle(newService);
	CloseServiceHandle(scm);
}
Exemple #4
0
	void local::createdir_impl(std::string path) {
#if defined(_WIN32)
		std::wstring wPath(path.begin(), path.end());
		SetLastError(ERROR_SUCCESS);
		if(::CreateDirectoryW(wPath.c_str(), NULL) == 0) {
			DWORD dwError = GetLastError();
			if(dwError != ERROR_ALREADY_EXISTS) {
				debugmanager()->fatal("failed to create directory ", path,
				                      " (error ", dwError, ')');
			}
		}
#elif defined(__APPLE__) || defined(__linux__)
		if(mkdir(path.c_str(), 0755) == -1 && errno != EEXIST) {
			debugmanager()->fatal("failed to create directory ", path);
		}
#else
		debugmanager()->fatal("FileSystem::CreateDirImpl: not implemented");
#endif
	}
bool FileSystemManager::launchFile(QString filePath, QString arguments, QString workingDir, bool runAsElevated, bool quoteArgs, bool async)
{
	QString errMsg;
	int rcVal = 0;

	// check if we should use the pidl
	LPCITEMIDLIST pidl = NULL;	
	int virtualIconIndex = winOS->GetIconTypeFromFileName(filePath);
	if (virtualIconIndex > -1)
		pidl = winOS->GetPidlFromName(virtualIconIndex);

	// ensure that all the arguments are quoted
	QString wPath(filePath), wArgs(arguments), wDir(workingDir);
	ensureQuoted(wPath);
	if (quoteArgs)
		ensureQuoted(wArgs);
	ensureQuoted(wDir);
	
	// Try executing the file
	// Trying with ShellExecuteEx to see if we can fix the problems with .lnk not launching
	SHELLEXECUTEINFO sei = {0};
	

	sei.cbSize = sizeof(sei);
	// #define SEE_MASK_NOZONECHECKS      0x00800000
	sei.fMask = SEE_MASK_FLAG_LOG_USAGE | (pidl ? SEE_MASK_IDLIST : 0) | (async ? SEE_MASK_ASYNCOK : 0) | 0x00800000;
	sei.hwnd = winOS->GetWindowsHandle();
	if (winOS->IsWindowsVersionGreaterThanOrEqualTo(WindowsVista) && runAsElevated)
		sei.lpVerb =  L"runas"; // 'secret' verb to prompt for elevation on Vista
	else
		sei.lpVerb = NULL; // giving a null value for the verb forces it to use the default verb
	if (pidl)
		sei.lpIDList = (LPVOID) pidl;
	else
		sei.lpFile = (LPCTSTR) wPath.utf16();
	sei.lpParameters = (LPCTSTR) wArgs.utf16();
	sei.lpDirectory = (LPCTSTR) wDir.utf16();
	sei.nShow = SW_SHOWNORMAL;

	return launchFile(sei,filePath);
}
Exemple #6
0
//The Main function of program
int main(int argc, char *argv[])
{
#ifdef _DEBUG
//Handle the system signal.
	SetConsoleCtrlHandler((PHANDLER_ROUTINE)CtrlHandler, TRUE);
#endif

	if (argc > 0)
	{
		std::shared_ptr<wchar_t> wPath(new wchar_t[MAX_PATH]());
	//Path initialization and Winsock initialization.
		MultiByteToWideChar(CP_ACP, NULL, argv[0], MBSTOWCS_NULLTERMINATE, wPath.get(), MAX_PATH);
		if (FileInit(wPath.get()) == EXIT_FAILURE)
			return EXIT_FAILURE;
		wPath.reset();

	//Windows Firewall Test in first start.
		if (argc > 1 && strlen(argv[1U]) == strlen("--FirstStart") && memcmp(argv[1], ("--FirstStart"), strlen("--FirstStart")) == 0)
		{
			if (FirewallTest(AF_INET6) == EXIT_FAILURE && FirewallTest(AF_INET) == EXIT_FAILURE)
			{
				PrintError(WINSOCK_ERROR, L"Windows Firewall Test error", NULL, nullptr, NULL);

				WSACleanup();
				return EXIT_FAILURE;
			}
			else {
				return EXIT_SUCCESS;
			}
		}
	}
	else {
		return EXIT_FAILURE;
	}

//Read configuration file and WinPcap initialization.
	if (Parameter.ReadParameter() == EXIT_FAILURE)
	{
		WSACleanup();
		return EXIT_FAILURE;
	}
	std::thread CaptureInitializationThread(CaptureInit);
	CaptureInitializationThread.detach();

//Get Localhost DNS PTR Records.
	std::thread IPv6LocalAddressThread(LocalAddressToPTR, AF_INET6);
	std::thread IPv4LocalAddressThread(LocalAddressToPTR, AF_INET);
	IPv6LocalAddressThread.detach();
	IPv4LocalAddressThread.detach();

//DNSCurve initialization
	if (Parameter.DNSCurve && DNSCurveParameter.Encryption)
	{
		randombytes_set_implementation(&randombytes_salsa20_implementation);
		DNSCurveInit();
	}
	
//Read IPFilter, start DNS Cache monitor(Timer type) and read Hosts.
	if (Parameter.FileRefreshTime > 0)
	{
		if (Parameter.OperationMode == LISTEN_CUSTOMMODE)
		{
			std::thread IPFilterThread(&Configuration::ReadIPFilter, std::ref(Parameter));
			IPFilterThread.detach();
		}

		if (Parameter.CacheType != 0)
		{
			std::thread DNSCacheTimerThread(DNSCacheTimerMonitor, Parameter.CacheType);
			DNSCacheTimerThread.detach();
		}

		std::thread HostsThread(&Configuration::ReadHosts, std::ref(Parameter));
		HostsThread.detach();
	}

//Service initialization and start service.
	SERVICE_TABLE_ENTRYW ServiceTable[] = {{LOCAL_SERVICENAME, (LPSERVICE_MAIN_FUNCTIONW)ServiceMain}, {nullptr, NULL}};
	if (!StartServiceCtrlDispatcherW(ServiceTable))
	{
		PrintError(SYSTEM_ERROR, L"Service start error", GetLastError(), nullptr, NULL);

		WSACleanup();
		return EXIT_FAILURE;
	}

	WSACleanup();
	return EXIT_SUCCESS;
}