RTDECL(int) RTPathUserDocuments(char *pszPath, size_t cchPath) { /* * Validate input */ AssertPtrReturn(pszPath, VERR_INVALID_POINTER); AssertReturn(cchPath, VERR_INVALID_PARAMETER); RTLDRMOD hShell32; int rc = RTLdrLoadSystem("Shell32.dll", true /*fNoUnload*/, &hShell32); if (RT_SUCCESS(rc)) { PFNSHGETFOLDERPATHW pfnSHGetFolderPathW; rc = RTLdrGetSymbol(hShell32, "SHGetFolderPathW", (void**)&pfnSHGetFolderPathW); if (RT_SUCCESS(rc)) { RTUTF16 wszPath[RTPATH_MAX]; HRESULT hrc = pfnSHGetFolderPathW(0, CSIDL_PERSONAL, NULL, SHGFP_TYPE_CURRENT, wszPath); if ( hrc == S_OK /* Found */ || hrc == S_FALSE) /* Found, but doesn't exist */ { /* * Convert and return. */ RTLdrClose(hShell32); return RTUtf16ToUtf8Ex(&wszPath[0], RTSTR_MAX, &pszPath, cchPath, NULL); } } RTLdrClose(hShell32); } return VERR_PATH_NOT_FOUND; }
static void vboxClipboardInitNewAPI(VBOXCLIPBOARDCONTEXT *pCtx) { RTLDRMOD hUser32 = NIL_RTLDRMOD; int rc = RTLdrLoadSystem("User32.dll", /* fNoUnload = */ true, &hUser32); if (RT_SUCCESS(rc)) { rc = RTLdrGetSymbol(hUser32, "AddClipboardFormatListener", (void**)&pCtx->pfnAddClipboardFormatListener); if (RT_SUCCESS(rc)) { rc = RTLdrGetSymbol(hUser32, "RemoveClipboardFormatListener", (void**)&pCtx->pfnRemoveClipboardFormatListener); } RTLdrClose(hUser32); } if (RT_SUCCESS(rc)) { Log(("New Clipboard API is enabled\n")); } else { pCtx->pfnAddClipboardFormatListener = NULL; pCtx->pfnRemoveClipboardFormatListener = NULL; Log(("New Clipboard API is not available. rc = %Rrc\n", rc)); } }
int VBoxVRDPInit(const VBOXSERVICEENV *pEnv, void **ppInstance, bool *pfStartThread) { Log(("VBoxTray: VBoxVRDPInit\n")); gCtx.pEnv = pEnv; gCtx.level = VRDP_EXPERIENCE_LEVEL_FULL; gCtx.fSavedThemeEnabled = FALSE; int rc = RTLdrLoadSystem("UxTheme.dll", false /*fNoUnload*/, &gCtx.hModUxTheme); if (RT_SUCCESS(rc)) { *(PFNRT *)&gCtx.pfnEnableTheming = RTLdrGetFunction(gCtx.hModUxTheme, "EnableTheming"); *(PFNRT *)&gCtx.pfnIsThemeActive = RTLdrGetFunction(gCtx.hModUxTheme, "IsThemeActive"); } else { gCtx.hModUxTheme = NIL_RTLDRMOD; gCtx.pfnEnableTheming = NULL; gCtx.pfnIsThemeActive = NULL; } *pfStartThread = true; *ppInstance = &gCtx; return VINF_SUCCESS; }
/** * Builds and allocates the security descriptor required for securing the local pipe. * * @return IPRT status code. * @param ppDesc Where to store the allocated security descriptor on success. * Must be free'd using LocalFree(). */ static int rtLocalIpcServerWinAllocSecurityDescriptior(PSECURITY_DESCRIPTOR *ppDesc, bool fServer) { /** @todo Stuff this into RTInitOnce? Later. */ PFNCONVERTSTRINGSECURITYDESCRIPTORTOSECURITYDESCRIPTOR pfnConvertStringSecurityDescriptorToSecurityDescriptor = NULL; RTLDRMOD hAdvApi32 = NIL_RTLDRMOD; int rc = RTLdrLoadSystem("Advapi32.dll", true /*fNoUnload*/, &hAdvApi32); if (RT_SUCCESS(rc)) rc = RTLdrGetSymbol(hAdvApi32, "ConvertStringSecurityDescriptorToSecurityDescriptorW", (void**)&pfnConvertStringSecurityDescriptorToSecurityDescriptor); PSECURITY_DESCRIPTOR pSecDesc = NULL; if (RT_SUCCESS(rc)) { AssertPtr(pfnConvertStringSecurityDescriptorToSecurityDescriptor); /* * We'll create a security descriptor from a SDDL that denies * access to network clients (this is local IPC after all), it * makes some further restrictions to prevent non-authenticated * users from screwing around. */ PRTUTF16 pwszSDDL; rc = RTStrToUtf16(fServer ? RTLOCALIPC_WIN_SDDL_SERVER : RTLOCALIPC_WIN_SDDL_CLIENT, &pwszSDDL); if (RT_SUCCESS(rc)) { if (!pfnConvertStringSecurityDescriptorToSecurityDescriptor((LPCTSTR)pwszSDDL, SDDL_REVISION_1, &pSecDesc, NULL)) { rc = RTErrConvertFromWin32(GetLastError()); } RTUtf16Free(pwszSDDL); } } else { /* Windows OSes < W2K SP2 not supported for now, bail out. */ /** @todo Implement me! */ rc = VERR_NOT_SUPPORTED; } if (hAdvApi32 != NIL_RTLDRMOD) RTLdrClose(hAdvApi32); if (RT_SUCCESS(rc)) { AssertPtr(pSecDesc); *ppDesc = pSecDesc; } return rc; }
/** * Gets the user home directory. * * @returns iprt status code. * @param pszPath Buffer where to store the path. * @param cchPath Buffer size in bytes. */ RTDECL(int) RTPathUserHome(char *pszPath, size_t cchPath) { /* * Validate input */ AssertPtrReturn(pszPath, VERR_INVALID_POINTER); AssertReturn(cchPath, VERR_INVALID_PARAMETER); RTUTF16 wszPath[RTPATH_MAX]; bool fValidFolderPath = false; /* * Try with Windows XP+ functionality first. */ RTLDRMOD hShell32; int rc = RTLdrLoadSystem("Shell32.dll", true /*fNoUnload*/, &hShell32); if (RT_SUCCESS(rc)) { PFNSHGETFOLDERPATHW pfnSHGetFolderPathW; rc = RTLdrGetSymbol(hShell32, "SHGetFolderPathW", (void**)&pfnSHGetFolderPathW); if (RT_SUCCESS(rc)) { HRESULT hrc = pfnSHGetFolderPathW(0, CSIDL_PROFILE, NULL, SHGFP_TYPE_CURRENT, wszPath); fValidFolderPath = (hrc == S_OK); } RTLdrClose(hShell32); } DWORD dwAttr; if ( !fValidFolderPath || (dwAttr = GetFileAttributesW(&wszPath[0])) == INVALID_FILE_ATTRIBUTES || !(dwAttr & FILE_ATTRIBUTE_DIRECTORY)) { /* * Fall back to Windows specific environment variables. HOME is not used. */ if ( !GetEnvironmentVariableW(L"USERPROFILE", &wszPath[0], RTPATH_MAX) || (dwAttr = GetFileAttributesW(&wszPath[0])) == INVALID_FILE_ATTRIBUTES || !(dwAttr & FILE_ATTRIBUTE_DIRECTORY)) { /* %HOMEDRIVE%%HOMEPATH% */ if (!GetEnvironmentVariableW(L"HOMEDRIVE", &wszPath[0], RTPATH_MAX)) return VERR_PATH_NOT_FOUND; size_t const cwc = RTUtf16Len(&wszPath[0]); if ( !GetEnvironmentVariableW(L"HOMEPATH", &wszPath[cwc], RTPATH_MAX - (DWORD)cwc) || (dwAttr = GetFileAttributesW(&wszPath[0])) == INVALID_FILE_ATTRIBUTES || !(dwAttr & FILE_ATTRIBUTE_DIRECTORY)) return VERR_PATH_NOT_FOUND; } } /* * Convert and return. */ return RTUtf16ToUtf8Ex(&wszPath[0], RTSTR_MAX, &pszPath, cchPath, NULL); }
RTDECL(void *) RTLdrGetSystemSymbol(const char *pszFilename, const char *pszSymbol) { void *pvRet = NULL; RTLDRMOD hLdrMod; int rc = RTLdrLoadSystem(pszFilename, true /*fNoUnload*/, &hLdrMod); if (RT_SUCCESS(rc)) { rc = RTLdrGetSymbol(hLdrMod, pszSymbol, &pvRet); if (RT_FAILURE(rc)) pvRet = NULL; /* paranoia */ RTLdrClose(hLdrMod); } return pvRet; }
RTDECL(int) RTCrStoreCreateSnapshotById(PRTCRSTORE phStore, RTCRSTOREID enmStoreId, PRTERRINFO pErrInfo) { AssertReturn(enmStoreId > RTCRSTOREID_INVALID && enmStoreId < RTCRSTOREID_END, VERR_INVALID_PARAMETER); /* * Create an empty in-memory store. */ RTCRSTORE hStore; int rc = RTCrStoreCreateInMem(&hStore, 128); if (RT_SUCCESS(rc)) { *phStore = hStore; /* * Resolve the APIs we need to do this job. */ RTLDRMOD hLdrMod; int rc2 = RTLdrLoadSystem("crypt32.dll", false /*NoUnload*/, &hLdrMod); if (RT_SUCCESS(rc2)) { PFNCERTOPENSTORE pfnOpenStore = NULL; rc2 = RTLdrGetSymbol(hLdrMod, "CertOpenStore", (void **)&pfnOpenStore); PFNCERTCLOSESTORE pfnCloseStore = NULL; if (RT_SUCCESS(rc2)) rc2 = RTLdrGetSymbol(hLdrMod, "CertCloseStore", (void **)&pfnCloseStore); PFNCERTENUMCERTIFICATESINSTORE pfnEnumCerts = NULL; if (RT_SUCCESS(rc2)) rc2 = RTLdrGetSymbol(hLdrMod, "CertEnumCertificatesInStore", (void **)&pfnEnumCerts); if (RT_SUCCESS(rc2)) { /* * Do the work. */ switch (enmStoreId) { case RTCRSTOREID_USER_TRUSTED_CAS_AND_CERTIFICATES: case RTCRSTOREID_SYSTEM_TRUSTED_CAS_AND_CERTIFICATES: { DWORD fStore = enmStoreId == RTCRSTOREID_USER_TRUSTED_CAS_AND_CERTIFICATES ? CERT_SYSTEM_STORE_CURRENT_USER : CERT_SYSTEM_STORE_LOCAL_MACHINE; static PCRTUTF16 const s_apwszStores[] = { L"AuthRoot", L"CA", L"MY", L"Root" }; for (uint32_t i = 0; i < RT_ELEMENTS(s_apwszStores); i++) rc = rtCrStoreAddCertsFromNative(hStore, fStore, s_apwszStores[i], pfnOpenStore, pfnCloseStore, pfnEnumCerts, rc, pErrInfo); break; } default: AssertFailed(); /* implement me */ } } else rc = RTErrInfoSetF(pErrInfo, -rc2, "Error resolving crypt32.dll APIs"); RTLdrClose(hLdrMod); } else rc = RTErrInfoSetF(pErrInfo, -rc2, "Error loading crypt32.dll"); } else RTErrInfoSet(pErrInfo, rc, "RTCrStoreCreateInMem failed"); return rc; }
/** * Resolve APIs not present on older windows versions. */ void VGSvcWinResolveApis(void) { RTLDRMOD hLdrMod; #define RESOLVE_SYMBOL(a_fn) do { RT_CONCAT(g_pfn, a_fn) = (decltype(a_fn) *)RTLdrGetFunction(hLdrMod, #a_fn); } while (0) /* From ADVAPI32.DLL: */ int rc = RTLdrLoadSystem("advapi32.dll", true /*fNoUnload*/, &hLdrMod); AssertRC(rc); if (RT_SUCCESS(rc)) { RESOLVE_SYMBOL(RegisterServiceCtrlHandlerExA); RESOLVE_SYMBOL(ChangeServiceConfig2A); RESOLVE_SYMBOL(GetNamedSecurityInfoA); RESOLVE_SYMBOL(SetEntriesInAclA); RESOLVE_SYMBOL(SetNamedSecurityInfoA); RESOLVE_SYMBOL(LsaNtStatusToWinError); RTLdrClose(hLdrMod); } /* From KERNEL32.DLL: */ rc = RTLdrLoadSystem("kernel32.dll", true /*fNoUnload*/, &hLdrMod); AssertRC(rc); if (RT_SUCCESS(rc)) { RESOLVE_SYMBOL(CreateToolhelp32Snapshot); RESOLVE_SYMBOL(Process32First); RESOLVE_SYMBOL(Process32Next); RESOLVE_SYMBOL(Module32First); RESOLVE_SYMBOL(Module32Next); RESOLVE_SYMBOL(GetSystemTimeAdjustment); RESOLVE_SYMBOL(SetSystemTimeAdjustment); RTLdrClose(hLdrMod); } /* From NTDLL.DLL: */ rc = RTLdrLoadSystem("ntdll.dll", true /*fNoUnload*/, &hLdrMod); AssertRC(rc); if (RT_SUCCESS(rc)) { RESOLVE_SYMBOL(ZwQuerySystemInformation); RTLdrClose(hLdrMod); } /* From IPHLPAPI.DLL: */ rc = RTLdrLoadSystem("iphlpapi.dll", true /*fNoUnload*/, &hLdrMod); if (RT_SUCCESS(rc)) { RESOLVE_SYMBOL(GetAdaptersInfo); RTLdrClose(hLdrMod); } /* From WS2_32.DLL: */ rc = RTLdrLoadSystem("ws2_32.dll", true /*fNoUnload*/, &hLdrMod); if (RT_SUCCESS(rc)) { RESOLVE_SYMBOL(WSAStartup); RESOLVE_SYMBOL(WSACleanup); RESOLVE_SYMBOL(WSASocketA); RESOLVE_SYMBOL(WSAIoctl); RESOLVE_SYMBOL(WSAGetLastError); RESOLVE_SYMBOL(closesocket); RESOLVE_SYMBOL(inet_ntoa); RTLdrClose(hLdrMod); } }