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/.
13 #pragma warning(push, 1) /* disable warnings within system headers */
15 #define WIN32_LEAN_AND_MEAN
22 #include <com/sun/star/uno/Any.hxx>
23 #include <com/sun/star/uno/Reference.hxx>
24 #include <com/sun/star/uno/RuntimeException.hpp>
25 #include <com/sun/star/uno/Sequence.hxx>
26 #include <com/sun/star/uno/XInterface.hpp>
27 #include <rtl/ustring.hxx>
28 #include <sal/log.hxx>
30 #include <osl/file.hxx>
32 #include "writemodfile.hxx"
34 #define MAX_KEY_LENGTH 255
39 // This is not a generic registry reader. We assume the following structure:
40 // Last element of Key becomes prop, first part is the path and optionally nodes,
41 // when the node has oor:op attribute.
42 // Values can be the following: Value (string) and Final (dword, optional)
44 // For example the following registry setting:
45 // [HKEY_LOCAL_MACHINE\SOFTWARE\Policies\LibreOffice\org.openoffice.UserProfile\Data\o]
46 // "Value"="Example Corp."
47 // "Final"=dword:00000001
48 // becomes the following in configuration:
49 // <!-- set the Company name -->
50 // <item oor:path="/org.openoffice.UserProfile/Data">
51 // <prop oor:name="o" oor:finalized="true">
52 // <value>Example Corp.</value>
57 // [HKEY_LOCAL_MACHINE\SOFTWARE\Policies\LibreOffice\org.openoffice.Office.OptionsDialog\OptionsDialogGroups\ProductName/#fuse\Pages\Java/#fuse\Hide]
59 // becomes the following in configuration:
60 // <!-- Hide Tools - Options - LibreOffice - Advanced panel -->
61 // <item oor:path="/org.openoffice.Office.OptionsDialog/OptionsDialogGroups">
62 // <node oor:name="ProductName" oor:op="fuse">
63 // <node oor:name="Pages">
64 // <node oor:name="Java" oor:op="fuse">
65 // <prop oor:name="Hide">
66 // <value>true</value>
73 void dumpWindowsRegistryKey(HKEY hKey
, OUString aKeyName
, oslFileHandle aFileHandle
)
77 if(RegOpenKeyExW(hKey
, aKeyName
.getStr(), 0, KEY_READ
, &hCurKey
) == ERROR_SUCCESS
)
81 DWORD nLongestValueNameLen
, nLongestValueLen
;
82 // Query the number of subkeys
83 RegQueryInfoKeyW(hCurKey
, NULL
, NULL
, NULL
, &nSubKeys
, NULL
, NULL
, &nValues
, &nLongestValueNameLen
, &nLongestValueLen
, NULL
, NULL
);
86 //Look for subkeys in this key
87 for(DWORD i
= 0; i
< nSubKeys
; i
++)
89 wchar_t buffKeyName
[MAX_KEY_LENGTH
];
90 buffKeyName
[0] = '\0';
91 DWORD buffSize
=MAX_KEY_LENGTH
;
94 RegEnumKeyExW(hCurKey
, i
, buffKeyName
, &buffSize
, NULL
, NULL
, NULL
, NULL
);
96 //Make up full key name
97 if(aKeyName
.isEmpty())
98 aSubkeyName
= aKeyName
+ OUString(buffKeyName
);
100 aSubkeyName
= aKeyName
+ "\\" + OUString(buffKeyName
);
102 //Recursion, until no more subkeys are found
103 dumpWindowsRegistryKey(hKey
, aSubkeyName
, aFileHandle
);
108 // No more subkeys, we are at a leaf
109 wchar_t* pValueName
= new wchar_t[nLongestValueNameLen
+ 1];
110 wchar_t* pValue
= new wchar_t[nLongestValueLen
+ 1];
115 for(DWORD i
= 0; i
< nValues
; ++i
)
117 DWORD nValueNameLen
= nLongestValueNameLen
+ 1;
118 DWORD nValueLen
= nLongestValueLen
+ 1;
120 RegEnumValueW(hCurKey
, i
, pValueName
, &nValueNameLen
, NULL
, NULL
, (LPBYTE
)pValue
, &nValueLen
);
121 const wchar_t wsValue
[] = L
"Value";
122 const wchar_t wsFinal
[] = L
"Final";
124 if(!wcscmp(pValueName
, wsValue
))
125 aValue
= OUString(pValue
);
126 if(!wcscmp(pValueName
, wsFinal
) && *(DWORD
*)pValue
== 1)
129 sal_Int32 aLastSeparator
= aKeyName
.lastIndexOf('\\');
130 OUString aPathAndNodes
= aKeyName
.copy(0, aLastSeparator
);
131 OUString aProp
= aKeyName
.copy(aLastSeparator
+ 1);
132 bool bHasNode
= false;
133 sal_Int32 nCloseNode
= 0;
135 writeData(aFileHandle
, "<item oor:path=\"");
136 for(sal_Int32 nIndex
= 0;; ++nIndex
)
138 OUString aNextPathPart
= aPathAndNodes
.getToken(nIndex
, '\\');
140 if(!aNextPathPart
.isEmpty())
142 if((aNextPathPart
.lastIndexOf("/#") != -1) || bHasNode
)
146 writeData(aFileHandle
, "\"><node oor:name=\"");
147 sal_Int32 nCommandSeparator
= aNextPathPart
.lastIndexOf('#');
148 if(nCommandSeparator
!= -1)
150 OUString aNodeOp
= aNextPathPart
.copy(nCommandSeparator
+ 1);
151 writeAttributeValue(aFileHandle
, aNextPathPart
.copy(0, nCommandSeparator
- 1));
152 writeData(aFileHandle
, "\" oor:op=\"");
153 writeAttributeValue(aFileHandle
, aNodeOp
);
157 writeAttributeValue(aFileHandle
, aNextPathPart
);
162 writeAttributeValue(aFileHandle
, "/" + aNextPathPart
);
167 writeData(aFileHandle
, "\">");
172 writeData(aFileHandle
, "<prop oor:name=\"");
173 writeAttributeValue(aFileHandle
, aProp
);
174 writeData(aFileHandle
, "\"");
176 writeData(aFileHandle
, " oor:finalized=\"true\"");
177 writeData(aFileHandle
, "><value>");
178 writeValueContent(aFileHandle
, aValue
);
179 writeData(aFileHandle
, "</value></prop>");
180 for(; nCloseNode
> 0; nCloseNode
--)
181 writeData(aFileHandle
, "</node>");
182 writeData(aFileHandle
, "</item>\n");
186 RegCloseKey(hCurKey
);
191 bool dumpWindowsRegistry(OUString
* pFileURL
, WinRegType eType
)
194 HKEY hDomain
= eType
== LOCAL_MACHINE
? HKEY_LOCAL_MACHINE
: HKEY_CURRENT_USER
;
195 if(RegOpenKeyExW(hDomain
, L
"SOFTWARE\\Policies\\LibreOffice", 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
)
199 ("Windows registry settings do not exist in HKLM\\SOFTWARE\\Policies\\LibreOffice"));
203 oslFileHandle aFileHandle
;
204 switch (osl::FileBase::createTempFile(0, &aFileHandle
, pFileURL
)) {
205 case osl::FileBase::E_None
:
207 case osl::FileBase::E_ACCES
:
210 ("cannot create temp Windows registry dump (E_ACCES)"));
213 throw css::uno::RuntimeException(
214 "cannot create temporary file");
218 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<oor:items"
219 " xmlns:oor=\"http://openoffice.org/2001/registry\""
220 " xmlns:xs=\"http://www.w3.org/2001/XMLSchema\""
221 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n");
222 dumpWindowsRegistryKey(hKey
, "", aFileHandle
);
223 writeData(aFileHandle
, "</oor:items>");
224 oslFileError e
= osl_closeFile(aFileHandle
);
225 if (e
!= osl_File_E_None
)
226 SAL_WARN("configmgr", "osl_closeFile failed with " << +e
);
233 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */