build fix
[LibreOffice.git] / configmgr / source / winreg.cxx
blobb4c2cf0fb06c01021f160c32bb3636161bdb22f0
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 */
11 #include <cwchar>
12 #ifdef _MSC_VER
13 #pragma warning(push, 1) /* disable warnings within system headers */
14 #endif
15 #define WIN32_LEAN_AND_MEAN
16 #include <windows.h>
17 #include <msiquery.h>
18 #ifdef _MSC_VER
19 #pragma warning(pop)
20 #endif
22 #include <com/sun/star/uno/RuntimeException.hpp>
23 #include <com/sun/star/uno/XInterface.hpp>
24 #include <rtl/ustring.hxx>
25 #include <sal/log.hxx>
26 #include <osl/file.h>
27 #include <osl/file.hxx>
28 #include "winreg.hxx"
29 #include "writemodfile.hxx"
31 #define MAX_KEY_LENGTH 255
33 namespace configmgr {
35 namespace {
36 // This is not a generic registry reader. We assume the following structure:
37 // Last element of Key becomes prop, first part is the path and optionally nodes,
38 // when the node has oor:op attribute.
39 // Values can be the following: Value (string), Type (string, optional),
40 // Final (dword, optional), External (dword, optional), ExternalBackend (string, optional)
42 // For example the following registry setting:
43 // [HKEY_LOCAL_MACHINE\SOFTWARE\Policies\LibreOffice\org.openoffice.UserProfile\Data\o]
44 // "Value"="Example Corp."
45 // "Final"=dword:00000001
46 // becomes the following in configuration:
47 // <!-- set the Company name -->
48 // <item oor:path="/org.openoffice.UserProfile/Data">
49 // <prop oor:name="o" oor:finalized="true">
50 // <value>Example Corp.</value>
51 // </prop>
52 // </item>
54 // Another example:
55 // [HKEY_LOCAL_MACHINE\SOFTWARE\Policies\LibreOffice\org.openoffice.Office.OptionsDialog\OptionsDialogGroups\ProductName/#fuse\Pages\Java/#fuse\Hide]
56 // "Value"="true"
57 // becomes the following in configuration:
58 // <!-- Hide Tools - Options - LibreOffice - Advanced panel -->
59 // <item oor:path="/org.openoffice.Office.OptionsDialog/OptionsDialogGroups">
60 // <node oor:name="ProductName" oor:op="fuse">
61 // <node oor:name="Pages">
62 // <node oor:name="Java" oor:op="fuse">
63 // <prop oor:name="Hide">
64 // <value>true</value>
65 // </prop>
66 // </node>
67 // </node>
68 // </node>
69 // </item>
71 // Third example (property of an extensible group -> needs type):
72 // [HKEY_LOCAL_MACHINE\SOFTWARE\Policies\LibreOffice\org.openoffice.Office.Jobs\Jobs\org.openoffice.Office.Jobs:Job['UpdateCheck']\Arguments\AutoCheckEnabled]
73 // "Value"="false"
74 // "Final"=dword:00000001
75 // "Type"="xs:boolean"
76 // becomes the following in configuration:
77 // <item oor:path="/org.openoffice.Office.Jobs/Jobs/org.openoffice.Office.Jobs:Job['UpdateCheck']/Arguments">
78 // <prop oor:name="AutoCheckEnabled" oor:type="xs::boolean" oor:finalized="true">
79 // <value>false</value>
80 // </prop>
81 // </item>
83 // External (component data) example:
84 // [HKEY_CURRENT_USER\Software\Policies\LibreOffice\org.openoffice.UserProfile\Data\o]
85 // "Value"="company"
86 // "Final"=dword:00000001
87 // "External"=dword:00000001
88 // "ExternalBackend"="com.sun.star.configuration.backend.LdapUserProfileBe"
89 // becomes the following in configuration:
90 // <item oor:path="/org.openoffice.UserProfile/Data">
91 // <prop oor:name="o" oor:finalized="true">
92 // <value oor:external="com.sun.star.configuration.backend.LdapUserProfileBe company"/>
93 // </prop>
94 // </item>
96 void dumpWindowsRegistryKey(HKEY hKey, OUString const & aKeyName, TempFile &aFileHandle)
98 HKEY hCurKey;
100 if(RegOpenKeyExW(hKey, aKeyName.getStr(), 0, KEY_READ, &hCurKey) == ERROR_SUCCESS)
102 DWORD nSubKeys = 0;
103 DWORD nValues = 0;
104 DWORD nLongestValueNameLen, nLongestValueLen;
105 // Query the number of subkeys
106 RegQueryInfoKeyW(hCurKey, nullptr, nullptr, nullptr, &nSubKeys, nullptr, nullptr, &nValues, &nLongestValueNameLen, &nLongestValueLen, nullptr, nullptr);
107 if(nSubKeys)
109 //Look for subkeys in this key
110 for(DWORD i = 0; i < nSubKeys; i++)
112 wchar_t buffKeyName[MAX_KEY_LENGTH];
113 buffKeyName[0] = '\0';
114 DWORD buffSize=MAX_KEY_LENGTH;
115 OUString aSubkeyName;
116 //Get subkey name
117 RegEnumKeyExW(hCurKey, i, buffKeyName, &buffSize, nullptr, nullptr, nullptr, nullptr);
119 //Make up full key name
120 if(aKeyName.isEmpty())
121 aSubkeyName = aKeyName + OUString(buffKeyName);
122 else
123 aSubkeyName = aKeyName + "\\" + OUString(buffKeyName);
125 //Recursion, until no more subkeys are found
126 dumpWindowsRegistryKey(hKey, aSubkeyName, aFileHandle);
129 else if(nValues)
131 // No more subkeys, we are at a leaf
132 wchar_t* pValueName = new wchar_t[nLongestValueNameLen + 1];
133 wchar_t* pValue = new wchar_t[nLongestValueLen + 1];
135 bool bFinal = false;
136 bool bExternal = false;
137 OUString aValue;
138 OUString aType;
139 OUString aExternalBackend;
141 for(DWORD i = 0; i < nValues; ++i)
143 DWORD nValueNameLen = nLongestValueNameLen + 1;
144 DWORD nValueLen = nLongestValueLen + 1;
146 RegEnumValueW(hCurKey, i, pValueName, &nValueNameLen, nullptr, nullptr, reinterpret_cast<LPBYTE>(pValue), &nValueLen);
148 if (!wcscmp(pValueName, L"Value"))
149 aValue = OUString(pValue);
150 else if (!wcscmp(pValueName, L"Type"))
151 aType = OUString(pValue);
152 else if (!wcscmp(pValueName, L"Final"))
154 if (*reinterpret_cast<DWORD*>(pValue) == 1)
155 bFinal = true;
157 else if (!wcscmp(pValueName, L"External"))
159 if (*reinterpret_cast<DWORD*>(pValue) == 1)
160 bExternal = true;
162 else if (!wcscmp(pValueName, L"ExternalBackend"))
163 aExternalBackend = OUString(pValue);
165 if (bExternal)
167 // type and external are mutually exclusive
168 aType.clear();
170 // Prepend backend, like in
171 // "com.sun.star.configuration.backend.LdapUserProfileBe company"
172 if (!aExternalBackend.isEmpty())
173 aValue = aExternalBackend + " " + aValue;
176 sal_Int32 aLastSeparator = aKeyName.lastIndexOf('\\');
177 OUString aPathAndNodes = aKeyName.copy(0, aLastSeparator);
178 OUString aProp = aKeyName.copy(aLastSeparator + 1);
179 bool bHasNode = false;
180 sal_Int32 nCloseNode = 0;
182 writeData(aFileHandle, "<item oor:path=\"");
183 for(sal_Int32 nIndex = 0;; ++nIndex)
185 OUString aNextPathPart = aPathAndNodes.getToken(nIndex, '\\');
187 if(!aNextPathPart.isEmpty())
189 if((aNextPathPart.lastIndexOf("/#") != -1) || bHasNode)
191 bHasNode = true;
192 nCloseNode++;
193 writeData(aFileHandle, "\"><node oor:name=\"");
194 sal_Int32 nCommandSeparator = aNextPathPart.lastIndexOf('#');
195 if(nCommandSeparator != -1)
197 OUString aNodeOp = aNextPathPart.copy(nCommandSeparator + 1);
198 writeAttributeValue(aFileHandle, aNextPathPart.copy(0, nCommandSeparator - 1));
199 writeData(aFileHandle, "\" oor:op=\"");
200 writeAttributeValue(aFileHandle, aNodeOp);
202 else
204 writeAttributeValue(aFileHandle, aNextPathPart);
207 else
209 writeAttributeValue(aFileHandle, "/" + aNextPathPart);
212 else
214 writeData(aFileHandle, "\">");
215 break;
219 writeData(aFileHandle, "<prop oor:name=\"");
220 writeAttributeValue(aFileHandle, aProp);
221 writeData(aFileHandle, "\"");
222 if(!aType.isEmpty())
224 writeData(aFileHandle, " oor:type=\"");
225 writeAttributeValue(aFileHandle, aType);
226 writeData(aFileHandle, "\"");
228 if(bFinal)
229 writeData(aFileHandle, " oor:finalized=\"true\"");
230 writeData(aFileHandle, "><value");
231 if (bExternal)
233 writeData(aFileHandle, " oor:external=\"");
234 writeAttributeValue(aFileHandle, aValue);
235 writeData(aFileHandle, "\"/");
237 else
239 writeData(aFileHandle, ">");
240 writeValueContent(aFileHandle, aValue);
241 writeData(aFileHandle, "</value");
243 writeData(aFileHandle, "></prop>");
244 for(; nCloseNode > 0; nCloseNode--)
245 writeData(aFileHandle, "</node>");
246 writeData(aFileHandle, "</item>\n");
247 delete[] pValueName;
248 delete[] pValue;
250 RegCloseKey(hCurKey);
255 bool dumpWindowsRegistry(OUString* pFileURL, WinRegType eType)
257 HKEY hKey;
258 HKEY hDomain = eType == LOCAL_MACHINE ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
259 if(RegOpenKeyExW(hDomain, L"SOFTWARE\\Policies\\LibreOffice", 0, KEY_READ, &hKey) != ERROR_SUCCESS)
261 SAL_INFO(
262 "configmgr",
263 ("Windows registry settings do not exist in HKLM\\SOFTWARE\\Policies\\LibreOffice"));
264 return false;
267 TempFile aFileHandle;
268 switch (osl::FileBase::createTempFile(nullptr, &aFileHandle.handle, pFileURL)) {
269 case osl::FileBase::E_None:
270 break;
271 case osl::FileBase::E_ACCES:
272 SAL_INFO(
273 "configmgr",
274 ("cannot create temp Windows registry dump (E_ACCES)"));
275 return false;
276 default:
277 throw css::uno::RuntimeException(
278 "cannot create temporary file");
280 aFileHandle.url = *pFileURL;
281 writeData(
282 aFileHandle,
283 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<oor:items"
284 " xmlns:oor=\"http://openoffice.org/2001/registry\""
285 " xmlns:xs=\"http://www.w3.org/2001/XMLSchema\""
286 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n");
287 dumpWindowsRegistryKey(hKey, "", aFileHandle);
288 writeData(aFileHandle, "</oor:items>");
289 oslFileError e = aFileHandle.closeWithoutUnlink();
290 if (e != osl_File_E_None)
291 SAL_WARN("configmgr", "osl_closeFile failed with " << +e);
292 RegCloseKey(hKey);
293 return true;
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */