LibreOffice Module configmgr (master) 1
winreg.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 */
10
11#include <cwchar>
12#include <memory>
13
14#define WIN32_LEAN_AND_MEAN
15#include <windows.h>
16#include <msiquery.h>
17
18#include <com/sun/star/uno/RuntimeException.hpp>
19#include <com/sun/star/uno/XInterface.hpp>
20#include <rtl/ustring.hxx>
21#include <sal/log.hxx>
22#include <osl/file.h>
23#include <osl/file.hxx>
25#include "winreg.hxx"
26#include "writemodfile.hxx"
27
28#define MAX_KEY_LENGTH 255
29
30namespace configmgr {
31
32namespace {
33// This is not a generic registry reader. We assume the following structure:
34// Last element of Key becomes prop, first part is the path and optionally nodes,
35// when the node has oor:op attribute.
36// Values can be the following: Value (string), Type (string, optional),
37// Final (dword, optional), External (dword, optional), ExternalBackend (string, optional),
38// Nil (dword, optional)
39//
40// For example the following registry setting:
41// [HKEY_LOCAL_MACHINE\SOFTWARE\Policies\LibreOffice\org.openoffice.UserProfile\Data\o]
42// "Value"="Example Corp."
43// "Final"=dword:00000001
44// becomes the following in configuration:
45// <!-- set the Company name -->
46// <item oor:path="/org.openoffice.UserProfile/Data">
47// <prop oor:name="o" oor:finalized="true">
48// <value>Example Corp.</value>
49// </prop>
50// </item>
51//
52// Another example:
53// [HKEY_LOCAL_MACHINE\SOFTWARE\Policies\LibreOffice\org.openoffice.Office.OptionsDialog\OptionsDialogGroups\ProductName/#fuse\Pages\Java/#fuse\Hide]
54// "Value"="true"
55// becomes the following in configuration:
56// <!-- Hide Tools - Options - LibreOffice - Advanced panel -->
57// <item oor:path="/org.openoffice.Office.OptionsDialog/OptionsDialogGroups">
58// <node oor:name="ProductName" oor:op="fuse">
59// <node oor:name="Pages">
60// <node oor:name="Java" oor:op="fuse">
61// <prop oor:name="Hide">
62// <value>true</value>
63// </prop>
64// </node>
65// </node>
66// </node>
67// </item>
68//
69// Third example (property of an extensible group -> needs type):
70// [HKEY_LOCAL_MACHINE\SOFTWARE\Policies\LibreOffice\org.openoffice.Office.Jobs\Jobs\org.openoffice.Office.Jobs:Job['UpdateCheck']\Arguments\AutoCheckEnabled]
71// "Value"="false"
72// "Final"=dword:00000001
73// "Type"="xs:boolean"
74// becomes the following in configuration:
75// <item oor:path="/org.openoffice.Office.Jobs/Jobs/org.openoffice.Office.Jobs:Job['UpdateCheck']/Arguments">
76// <prop oor:name="AutoCheckEnabled" oor:type="xs:boolean" oor:finalized="true">
77// <value>false</value>
78// </prop>
79// </item>
80//
81// External (component data) example:
82// [HKEY_CURRENT_USER\Software\Policies\LibreOffice\org.openoffice.UserProfile\Data\o]
83// "Value"="company"
84// "Final"=dword:00000001
85// "External"=dword:00000001
86// "ExternalBackend"="com.sun.star.configuration.backend.LdapUserProfileBe"
87// becomes the following in configuration:
88// <item oor:path="/org.openoffice.UserProfile/Data">
89// <prop oor:name="o" oor:finalized="true">
90// <value oor:external="com.sun.star.configuration.backend.LdapUserProfileBe company"/>
91// </prop>
92// </item>
93//
94// Nil example:
95// Empty value (<value></value>) and nil value (<value xsi:nil="true"/>) are different.
96// In case of some path settings, the base path setting has to be cleared.
97// [HKEY_CURRENT_USER\Software\Policies\LibreOffice\org.openoffice.Office.Common\Path\Current\Work]
98// "Value"=""
99// "Final"=dword:00000001
100// "Nil"=dword:00000001
101// [HKEY_CURRENT_USER\Software\Policies\LibreOffice\org.openoffice.Office.Paths\Paths\org.openoffice.Office.Paths:NamedPath['Work']\WritePath]
102// "Value"="file:///H:/"
103// "Final"=dword:00000001
104// becomes the following in configuration:
105// <item oor:path="/org.openoffice.Office.Common/Path/Current">
106// <prop oor:name="Work" oor:finalized="true">
107// <value xsi:nil="true"/>
108// </prop>
109// </item>
110// <item oor:path="/org.openoffice.Office.Paths/Paths/org.openoffice.Office.Paths:NamedPath['Work']">
111// <prop oor:name="WritePath" oor:finalized="true">
112// <value>file:///H:/</value>
113// </prop>
114// </item>
115
116void dumpWindowsRegistryKey(HKEY hKey, OUString const & aKeyName, TempFile &aFileHandle)
117{
118 HKEY hCurKey;
119
120 if(RegOpenKeyExW(
121 hKey, o3tl::toW(aKeyName.getStr()), 0,
122 KEY_READ, &hCurKey)
123 == ERROR_SUCCESS)
124 {
125 DWORD nSubKeys = 0;
126 DWORD nValues = 0;
127 DWORD nLongestValueNameLen, nLongestValueLen;
128 // Query the number of subkeys
129 RegQueryInfoKeyW(hCurKey, nullptr, nullptr, nullptr, &nSubKeys, nullptr, nullptr, &nValues, &nLongestValueNameLen, &nLongestValueLen, nullptr, nullptr);
130 if(nSubKeys)
131 {
132 //Look for subkeys in this key
133 for(DWORD i = 0; i < nSubKeys; i++)
134 {
135 wchar_t buffKeyName[MAX_KEY_LENGTH];
136 buffKeyName[0] = '\0';
137 DWORD buffSize=MAX_KEY_LENGTH;
138 OUString aSubkeyName;
139 //Get subkey name
140 RegEnumKeyExW(hCurKey, i, buffKeyName, &buffSize, nullptr, nullptr, nullptr, nullptr);
141
142 //Make up full key name
143 if(aKeyName.isEmpty())
144 aSubkeyName = aKeyName + o3tl::toU(buffKeyName);
145 else
146 aSubkeyName = aKeyName + "\\" + o3tl::toU(buffKeyName);
147
148 //Recursion, until no more subkeys are found
149 dumpWindowsRegistryKey(hKey, aSubkeyName, aFileHandle);
150 }
151 }
152 else if(nValues)
153 {
154 // No more subkeys, we are at a leaf
155 auto pValueName = std::unique_ptr<wchar_t[]>(
156 new wchar_t[nLongestValueNameLen + 1]);
157 auto pValue = std::unique_ptr<wchar_t[]>(
158 new wchar_t[nLongestValueLen/sizeof(wchar_t) + 1]);
159
160 bool bFinal = false;
161 bool bExternal = false;
162 bool bNil = false;
163 OUString aValue;
164 OUString aType;
165 OUString aExternalBackend;
166
167 for(DWORD i = 0; i < nValues; ++i)
168 {
169 DWORD nValueNameLen = nLongestValueNameLen + 1;
170 DWORD nValueLen = nLongestValueLen + 1;
171
172 RegEnumValueW(hCurKey, i, pValueName.get(), &nValueNameLen, nullptr, nullptr, reinterpret_cast<LPBYTE>(pValue.get()), &nValueLen);
173
174 if (!wcscmp(pValueName.get(), L"Value"))
175 aValue = o3tl::toU(pValue.get());
176 else if (!wcscmp(pValueName.get(), L"Type"))
177 aType = o3tl::toU(pValue.get());
178 else if (!wcscmp(pValueName.get(), L"Final"))
179 {
180 if (*reinterpret_cast<DWORD*>(pValue.get()) == 1)
181 bFinal = true;
182 }
183 else if (!wcscmp(pValueName.get(), L"Nil"))
184 {
185 if (*reinterpret_cast<DWORD*>(pValue.get()) == 1)
186 bNil = true;
187 }
188 else if (!wcscmp(pValueName.get(), L"External"))
189 {
190 if (*reinterpret_cast<DWORD*>(pValue.get()) == 1)
191 bExternal = true;
192 }
193 else if (!wcscmp(pValueName.get(), L"ExternalBackend"))
194 aExternalBackend = o3tl::toU(pValue.get());
195 }
196 if (bExternal)
197 {
198 // type and external are mutually exclusive
199 aType.clear();
200
201 // Prepend backend, like in
202 // "com.sun.star.configuration.backend.LdapUserProfileBe company"
203 if (!aExternalBackend.isEmpty())
204 aValue = aExternalBackend + " " + aValue;
205 }
206
207 sal_Int32 aLastSeparator = aKeyName.lastIndexOf('\\');
208 OUString aPathAndNodes = aKeyName.copy(0, aLastSeparator);
209 OUString aProp = aKeyName.copy(aLastSeparator + 1);
210 bool bHasNode = false;
211 sal_Int32 nCloseNode = 0;
212
213 aFileHandle.writeString("<item oor:path=\"");
214 for(sal_Int32 nIndex = 0;;)
215 {
216 OUString aNextPathPart = aPathAndNodes.getToken(0, '\\', nIndex);
217
218 if(!aNextPathPart.isEmpty())
219 {
220 if((aNextPathPart.lastIndexOf("/#") != -1) || bHasNode)
221 {
222 bHasNode = true;
223 nCloseNode++;
224 aFileHandle.writeString("\"><node oor:name=\"");
225 sal_Int32 nCommandSeparator = aNextPathPart.lastIndexOf('#');
226 if(nCommandSeparator != -1)
227 {
228 OUString aNodeOp = aNextPathPart.copy(nCommandSeparator + 1);
229 writeAttributeValue(aFileHandle, aNextPathPart.subView(0, nCommandSeparator - 1));
230 aFileHandle.writeString("\" oor:op=\"");
231 writeAttributeValue(aFileHandle, aNodeOp);
232 }
233 else
234 {
235 writeAttributeValue(aFileHandle, aNextPathPart);
236 }
237 }
238 else
239 {
241 aFileHandle, OUStringConcatenation("/" + aNextPathPart));
242 }
243 }
244 else
245 {
246 aFileHandle.writeString("\">");
247 break;
248 }
249 }
250
251 aFileHandle.writeString("<prop oor:name=\"");
252 writeAttributeValue(aFileHandle, aProp);
253 aFileHandle.writeString("\"");
254 if(!aType.isEmpty())
255 {
256 aFileHandle.writeString(" oor:type=\"");
257 writeAttributeValue(aFileHandle, aType);
258 aFileHandle.writeString("\"");
259 }
260 if(bFinal)
261 aFileHandle.writeString(" oor:finalized=\"true\"");
262 aFileHandle.writeString("><value");
263 if (aValue.isEmpty() && bNil)
264 {
265 aFileHandle.writeString(" xsi:nil=\"true\"/");
266 }
267 else if (bExternal)
268 {
269 aFileHandle.writeString(" oor:external=\"");
270 writeAttributeValue(aFileHandle, aValue);
271 aFileHandle.writeString("\"/");
272 }
273 else
274 {
275 aFileHandle.writeString(">");
276 writeValueContent(aFileHandle, aValue);
277 aFileHandle.writeString("</value");
278 }
279 aFileHandle.writeString("></prop>");
280 for(; nCloseNode > 0; nCloseNode--)
281 aFileHandle.writeString("</node>");
282 aFileHandle.writeString("</item>\n");
283 }
284 RegCloseKey(hCurKey);
285 }
286}
287}
288
289bool dumpWindowsRegistry(OUString* pFileURL, WinRegType eType)
290{
291 HKEY hKey;
292 HKEY hDomain = eType == LOCAL_MACHINE ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER;
293 if(RegOpenKeyExW(hDomain, L"SOFTWARE\\Policies\\LibreOffice", 0, KEY_READ, &hKey) != ERROR_SUCCESS)
294 {
295 SAL_INFO(
296 "configmgr",
297 ("Windows registry settings do not exist in HKLM\\SOFTWARE\\Policies\\LibreOffice"));
298 return false;
299 }
300
301 TempFile aFileHandle;
302 switch (osl::FileBase::createTempFile(nullptr, &aFileHandle.handle, pFileURL)) {
303 case osl::FileBase::E_None:
304 break;
305 case osl::FileBase::E_ACCES:
306 SAL_INFO(
307 "configmgr",
308 ("cannot create temp Windows registry dump (E_ACCES)"));
309 return false;
310 default:
311 throw css::uno::RuntimeException(
312 "cannot create temporary file");
313 }
314 aFileHandle.url = *pFileURL;
315 aFileHandle.writeString(
316 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<oor:items"
317 " xmlns:oor=\"http://openoffice.org/2001/registry\""
318 " xmlns:xs=\"http://www.w3.org/2001/XMLSchema\""
319 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n");
320 dumpWindowsRegistryKey(hKey, "", aFileHandle);
321 aFileHandle.writeString("</oor:items>");
322 oslFileError e = aFileHandle.closeWithoutUnlink();
323 if (e != osl_File_E_None)
324 SAL_WARN("configmgr", "osl_closeFile failed with " << +e);
325 RegCloseKey(hKey);
326 return true;
327}
328
329}
330
331/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
DocumentType eType
sal_Int32 nIndex
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
void writeValueContent(TempFile &handle, std::u16string_view value)
@ LOCAL_MACHINE
Definition: winreg.hxx:15
bool dumpWindowsRegistry(OUString *pFileURL, WinRegType eType)
Definition: winreg.cxx:289
void writeAttributeValue(TempFile &handle, std::u16string_view value)
int i
void writeString(std::string_view text)
oslFileHandle handle
#define MAX_KEY_LENGTH
Definition: winreg.cxx:28