1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
14 #define WIN32_LEAN_AND_MEAN
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>
23 #include <osl/file.hxx>
24 #include <o3tl/char16_t2wchar_t.hxx>
26 #include "writemodfile.hxx"
28 #define MAX_KEY_LENGTH 255
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)
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>
53 // [HKEY_LOCAL_MACHINE\SOFTWARE\Policies\LibreOffice\org.openoffice.Office.OptionsDialog\OptionsDialogGroups\ProductName/#fuse\Pages\Java/#fuse\Hide]
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>
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]
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>
81 // External (component data) example:
82 // [HKEY_CURRENT_USER\Software\Policies\LibreOffice\org.openoffice.UserProfile\Data\o]
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"/>
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]
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"/>
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>
116 void dumpWindowsRegistryKey(HKEY hKey
, OUString
const & aKeyName
, TempFile
&aFileHandle
)
121 hKey
, o3tl::toW(aKeyName
.getStr()), 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);
132 //Look for subkeys in this key
133 for(DWORD i
= 0; i
< nSubKeys
; i
++)
135 wchar_t buffKeyName
[MAX_KEY_LENGTH
];
136 buffKeyName
[0] = '\0';
137 DWORD buffSize
=MAX_KEY_LENGTH
;
138 OUString aSubkeyName
;
140 RegEnumKeyExW(hCurKey
, i
, buffKeyName
, &buffSize
, nullptr, nullptr, nullptr, nullptr);
142 //Make up full key name
143 if(aKeyName
.isEmpty())
144 aSubkeyName
= aKeyName
+ o3tl::toU(buffKeyName
);
146 aSubkeyName
= aKeyName
+ "\\" + o3tl::toU(buffKeyName
);
148 //Recursion, until no more subkeys are found
149 dumpWindowsRegistryKey(hKey
, aSubkeyName
, aFileHandle
);
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]);
161 bool bExternal
= false;
165 OUString aExternalBackend
;
167 for(DWORD i
= 0; i
< nValues
; ++i
)
169 DWORD nValueNameLen
= nLongestValueNameLen
+ 1;
170 DWORD nValueLen
= nLongestValueLen
+ 1;
172 RegEnumValueW(hCurKey
, i
, pValueName
.get(), &nValueNameLen
, nullptr, nullptr, reinterpret_cast<LPBYTE
>(pValue
.get()), &nValueLen
);
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"))
180 if (*reinterpret_cast<DWORD
*>(pValue
.get()) == 1)
183 else if (!wcscmp(pValueName
.get(), L
"Nil"))
185 if (*reinterpret_cast<DWORD
*>(pValue
.get()) == 1)
188 else if (!wcscmp(pValueName
.get(), L
"External"))
190 if (*reinterpret_cast<DWORD
*>(pValue
.get()) == 1)
193 else if (!wcscmp(pValueName
.get(), L
"ExternalBackend"))
194 aExternalBackend
= o3tl::toU(pValue
.get());
198 // type and external are mutually exclusive
201 // Prepend backend, like in
202 // "com.sun.star.configuration.backend.LdapUserProfileBe company"
203 if (!aExternalBackend
.isEmpty())
204 aValue
= aExternalBackend
+ " " + aValue
;
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;
213 aFileHandle
.writeString("<item oor:path=\"");
214 for(sal_Int32 nIndex
= 0;;)
216 OUString aNextPathPart
= aPathAndNodes
.getToken(0, '\\', nIndex
);
218 if(!aNextPathPart
.isEmpty())
220 if((aNextPathPart
.lastIndexOf("/#") != -1) || bHasNode
)
224 aFileHandle
.writeString("\"><node oor:name=\"");
225 sal_Int32 nCommandSeparator
= aNextPathPart
.lastIndexOf('#');
226 if(nCommandSeparator
!= -1)
228 OUString aNodeOp
= aNextPathPart
.copy(nCommandSeparator
+ 1);
229 writeAttributeValue(aFileHandle
, aNextPathPart
.copy(0, nCommandSeparator
- 1));
230 aFileHandle
.writeString("\" oor:op=\"");
231 writeAttributeValue(aFileHandle
, aNodeOp
);
235 writeAttributeValue(aFileHandle
, aNextPathPart
);
241 aFileHandle
, OUString("/" + aNextPathPart
));
246 aFileHandle
.writeString("\">");
251 aFileHandle
.writeString("<prop oor:name=\"");
252 writeAttributeValue(aFileHandle
, aProp
);
253 aFileHandle
.writeString("\"");
256 aFileHandle
.writeString(" oor:type=\"");
257 writeAttributeValue(aFileHandle
, aType
);
258 aFileHandle
.writeString("\"");
261 aFileHandle
.writeString(" oor:finalized=\"true\"");
262 aFileHandle
.writeString("><value");
263 if (aValue
.isEmpty() && bNil
)
265 aFileHandle
.writeString(" xsi:nil=\"true\"/");
269 aFileHandle
.writeString(" oor:external=\"");
270 writeAttributeValue(aFileHandle
, aValue
);
271 aFileHandle
.writeString("\"/");
275 aFileHandle
.writeString(">");
276 writeValueContent(aFileHandle
, aValue
);
277 aFileHandle
.writeString("</value");
279 aFileHandle
.writeString("></prop>");
280 for(; nCloseNode
> 0; nCloseNode
--)
281 aFileHandle
.writeString("</node>");
282 aFileHandle
.writeString("</item>\n");
284 RegCloseKey(hCurKey
);
289 bool dumpWindowsRegistry(OUString
* pFileURL
, WinRegType eType
)
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
)
297 ("Windows registry settings do not exist in HKLM\\SOFTWARE\\Policies\\LibreOffice"));
301 TempFile aFileHandle
;
302 switch (osl::FileBase::createTempFile(nullptr, &aFileHandle
.handle
, pFileURL
)) {
303 case osl::FileBase::E_None
:
305 case osl::FileBase::E_ACCES
:
308 ("cannot create temp Windows registry dump (E_ACCES)"));
311 throw css::uno::RuntimeException(
312 "cannot create temporary file");
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
);
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */