/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. * */ #include #include #ifdef _MSC_VER #pragma warning(push, 1) /* disable warnings within system headers */ #endif #define WIN32_LEAN_AND_MEAN #include #include #ifdef _MSC_VER #pragma warning(pop) #endif #include #include #include #include #include #include #include #include "winreg.hxx" #include "writemodfile.hxx" #define MAX_KEY_LENGTH 255 namespace configmgr { namespace { // This is not a generic registry reader. We assume the following structure: // Last element of Key becomes prop, first part is the path and optionally nodes, // when the node has oor:op attribute. // Values can be the following: Value (string), Type (string, optional), // Final (dword, optional), External (dword, optional) // // For example the following registry setting: // [HKEY_LOCAL_MACHINE\SOFTWARE\Policies\LibreOffice\org.openoffice.UserProfile\Data\o] // "Value"="Example Corp." // "Final"=dword:00000001 // becomes the following in configuration: // // // // Example Corp. // // // // Another example: // [HKEY_LOCAL_MACHINE\SOFTWARE\Policies\LibreOffice\org.openoffice.Office.OptionsDialog\OptionsDialogGroups\ProductName/#fuse\Pages\Java/#fuse\Hide] // "Value"="true" // becomes the following in configuration: // // // // // // // true // // // // // // // Third example (property of an extensible group -> needs type): // [HKEY_LOCAL_MACHINE\SOFTWARE\Policies\LibreOffice\org.openoffice.Office.Jobs\Jobs\org.openoffice.Office.Jobs:Job['UpdateCheck']\Arguments\AutoCheckEnabled] // "Value"="false" // "Final"=dword:00000001 // "Type"="xs:boolean" // becomes the following in configuration: // // // false // // // // External (component data) example: // [HKEY_CURRENT_USER\Software\Policies\LibreOffice\org.openoffice.UserProfile\Data\o] // "Value"="com.sun.star.configuration.backend.LdapUserProfileBe company" // "Final"=dword:00000001 // "External"=dword:00000001 // becomes the following in configuration: // // // // // void dumpWindowsRegistryKey(HKEY hKey, OUString const & aKeyName, TempFile &aFileHandle) { HKEY hCurKey; if(RegOpenKeyExW( hKey, o3tl::toW(aKeyName.getStr()), 0, KEY_READ, &hCurKey) == ERROR_SUCCESS) { DWORD nSubKeys = 0; DWORD nValues = 0; DWORD nLongestValueNameLen, nLongestValueLen; // Query the number of subkeys RegQueryInfoKeyW(hCurKey, nullptr, nullptr, nullptr, &nSubKeys, nullptr, nullptr, &nValues, &nLongestValueNameLen, &nLongestValueLen, nullptr, nullptr); if(nSubKeys) { //Look for subkeys in this key for(DWORD i = 0; i < nSubKeys; i++) { wchar_t buffKeyName[MAX_KEY_LENGTH]; buffKeyName[0] = '\0'; DWORD buffSize=MAX_KEY_LENGTH; OUString aSubkeyName; //Get subkey name RegEnumKeyExW(hCurKey, i, buffKeyName, &buffSize, nullptr, nullptr, nullptr, nullptr); //Make up full key name if(aKeyName.isEmpty()) aSubkeyName = aKeyName + OUString(o3tl::toU(buffKeyName)); else aSubkeyName = aKeyName + "\\" + OUString(o3tl::toU(buffKeyName)); //Recursion, until no more subkeys are found dumpWindowsRegistryKey(hKey, aSubkeyName, aFileHandle); } } else if(nValues) { // No more subkeys, we are at a leaf auto pValueName = std::unique_ptr( new wchar_t[nLongestValueNameLen + 1]); auto pValue = std::unique_ptr( new wchar_t[nLongestValueLen/sizeof(wchar_t) + 1]); bool bFinal = false; bool bExternal = false; OUString aValue; OUString aType; for(DWORD i = 0; i < nValues; ++i) { DWORD nValueNameLen = nLongestValueNameLen + 1; DWORD nValueLen = nLongestValueLen + 1; RegEnumValueW(hCurKey, i, pValueName.get(), &nValueNameLen, nullptr, nullptr, reinterpret_cast(pValue.get()), &nValueLen); if (!wcscmp(pValueName.get(), L"Value")) aValue = o3tl::toU(pValue.get()); else if (!wcscmp(pValueName.get(), L"Type")) aType = o3tl::toU(pValue.get()); else if (!wcscmp(pValueName.get(), L"Final")) { if (*reinterpret_cast(pValue.get()) == 1) bFinal = true; } else if (!wcscmp(pValueName.get(), L"External")) { if (*reinterpret_cast(pValue.get()) == 1) bExternal = true; } } // type and external are mutually exclusive if (bExternal) aType.clear(); sal_Int32 aLastSeparator = aKeyName.lastIndexOf('\\'); OUString aPathAndNodes = aKeyName.copy(0, aLastSeparator); OUString aProp = aKeyName.copy(aLastSeparator + 1); bool bHasNode = false; sal_Int32 nCloseNode = 0; aFileHandle.writeString(""); break; } } aFileHandle.writeString(""); writeValueContent(aFileHandle, aValue); aFileHandle.writeString(""); for(; nCloseNode > 0; nCloseNode--) aFileHandle.writeString(""); aFileHandle.writeString("\n"); } RegCloseKey(hCurKey); } } } bool dumpWindowsRegistry(OUString* pFileURL, WinRegType eType) { HKEY hKey; HKEY hDomain = eType == LOCAL_MACHINE ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; if(RegOpenKeyExW(hDomain, L"SOFTWARE\\Policies\\LibreOffice", 0, KEY_READ, &hKey) != ERROR_SUCCESS) { SAL_INFO( "configmgr", ("Windows registry settings do not exist in HKLM\\SOFTWARE\\Policies\\LibreOffice")); return false; } TempFile aFileHandle; switch (osl::FileBase::createTempFile(nullptr, &aFileHandle.handle, pFileURL)) { case osl::FileBase::E_None: break; case osl::FileBase::E_ACCES: SAL_INFO( "configmgr", ("cannot create temp Windows registry dump (E_ACCES)")); return false; default: throw css::uno::RuntimeException( "cannot create temporary file"); } aFileHandle.url = *pFileURL; aFileHandle.writeString( "\n\n"); dumpWindowsRegistryKey(hKey, "", aFileHandle); aFileHandle.writeString(""); oslFileError e = aFileHandle.closeWithoutUnlink(); if (e != osl_File_E_None) SAL_WARN("configmgr", "osl_closeFile failed with " << +e); RegCloseKey(hKey); return true; } } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */