Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // This API is a usability layer for direct registry access via NTDLL. | |
| 6 // It allows for "advapi32-free" registry access, which is especially | |
| 7 // useful for accessing registy from DllMain (holding loader lock), | |
| 8 // or if a dependency on/linkage of ADVAPI32.dll is not desired. | |
| 9 | |
| 10 // The implementation of this API should only use ntdll and kernel32 system | |
| 11 // DLLs. | |
| 12 | |
| 13 // Note that this API is currently lazy initialized. Any function that is | |
| 14 // NOT merely a wrapper function (i.e. any function that directly interacts with | |
| 15 // NTDLL) will immediately check: | |
| 16 // if (!g_initialized) | |
| 17 // InitNativeRegApi(); | |
| 18 // There is currently no multi-threading lock around the lazy initialization, | |
| 19 // as the main client for this API (chrome_elf) does not introduce | |
| 20 // a multi-threading concern. This can easily be changed if needed. | |
| 21 | |
| 22 #ifndef CHROME_ELF_NT_REGISTRY_NT_REGISTRY_H_ | |
| 23 #define CHROME_ELF_NT_REGISTRY_NT_REGISTRY_H_ | |
| 24 | |
| 25 #include <vector> | |
| 26 | |
| 27 #include "sandbox/win/src/nt_internals.h" // NTSTATUS | |
| 28 | |
| 29 namespace nt { | |
| 30 | |
| 31 extern const size_t g_kRegMaxPathLen; | |
| 32 extern wchar_t HKLM_override[]; | |
| 33 extern wchar_t HKCU_override[]; | |
| 34 | |
| 35 // AUTO will choose depending on system install or not. | |
| 36 // Use HKLM or HKCU to override. | |
| 37 typedef enum _ROOT_KEY { AUTO = 0, HKLM, HKCU } ROOT_KEY; | |
| 38 | |
| 39 // Create and/or open a registry key. | |
| 40 // - This function will recursively create multiple sub-keys if required for | |
| 41 // |key_path|. | |
| 42 // - If the key doesn't need to be left open, pass in nullptr for |out_handle|. | |
| 43 // - This function will happily succeed if the key already exists. | |
| 44 // - Optional |out_handle|. If nullptr, function will close handle when done. | |
|
robertshield
2016/07/12 04:26:24
Specify that, if provided, out_handle will refer t
penny
2016/07/13 00:27:55
Done.
| |
| 45 // - Caller must call CloseRegKey on returned handle (on success). | |
| 46 bool CreateRegKey(ROOT_KEY root, | |
| 47 const wchar_t* key_path, | |
| 48 ACCESS_MASK access, | |
| 49 HANDLE* out_handle OPTIONAL); | |
| 50 | |
| 51 // Open existing registry key. | |
| 52 // - Caller must call CloseRegKey on returned handle (on success). | |
| 53 // - Optional error code can be returned on failure for extra detail. | |
| 54 bool OpenRegKey(ROOT_KEY root, | |
| 55 const wchar_t* key_path, | |
| 56 ACCESS_MASK access, | |
| 57 HANDLE* out_handle, | |
| 58 NTSTATUS* error_code OPTIONAL); | |
| 59 | |
| 60 // Delete a registry key. | |
| 61 // - Caller must still call CloseRegKey after the delete. | |
| 62 bool DeleteRegKey(HANDLE key); | |
| 63 | |
| 64 // Delete a registry key. | |
| 65 // - WRAPPER: Function opens and closes the target key for caller. | |
| 66 bool DeleteRegKey(ROOT_KEY root, const wchar_t* key_path); | |
| 67 | |
| 68 // Close a registry key handle that was opened with CreateRegKey or OpenRegKey. | |
| 69 void CloseRegKey(HANDLE key); | |
| 70 | |
| 71 //------------------------------------------------------------------------------ | |
| 72 // Getter functions | |
| 73 //------------------------------------------------------------------------------ | |
| 74 | |
| 75 // Main function to query a registry value. | |
| 76 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | |
| 77 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ. | |
| 78 // - Caller is responsible for calling "delete[] *out_buffer" (on success). | |
| 79 bool QueryRegKeyValue(HANDLE key, | |
| 80 const wchar_t* value_name, | |
| 81 ULONG* out_type, | |
| 82 BYTE** out_buffer, | |
| 83 DWORD* out_size); | |
| 84 | |
| 85 // Query DWORD value. | |
| 86 // - WRAPPER: Function works with DWORD data type. | |
| 87 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | |
| 88 // - Handle will be left open. Caller must still call CloseRegKey when done. | |
| 89 bool QueryRegValueDWORD(HANDLE key, | |
| 90 const wchar_t* value_name, | |
| 91 DWORD* out_dword); | |
| 92 | |
| 93 // Query DWORD value. | |
| 94 // - WRAPPER: Function opens and closes the target key for caller, and works | |
| 95 // with DWORD data type. | |
| 96 bool QueryRegValueDWORD(ROOT_KEY root, | |
| 97 const wchar_t* key_path, | |
| 98 const wchar_t* value_name, | |
| 99 DWORD* out_dword); | |
| 100 | |
| 101 // Query SZ (string) value. | |
| 102 // - WRAPPER: Function works with SZ data type. | |
| 103 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | |
| 104 // - Handle will be left open. Caller must still call CloseRegKey when done. | |
| 105 bool QueryRegValueSZ(HANDLE key, | |
| 106 const wchar_t* value_name, | |
| 107 std::wstring* out_sz); | |
| 108 | |
| 109 // Query SZ (string) value. | |
| 110 // - WRAPPER: Function opens and closes the target key for caller, and works | |
| 111 // with SZ data type. | |
| 112 bool QueryRegValueSZ(ROOT_KEY root, | |
| 113 const wchar_t* key_path, | |
| 114 const wchar_t* value_name, | |
| 115 std::wstring* out_sz); | |
| 116 | |
| 117 // Query MULTI_SZ (multiple strings) value. | |
| 118 // - WRAPPER: Function works with MULTI_SZ data type. | |
| 119 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | |
| 120 // - Handle will be left open. Caller must still call CloseRegKey when done. | |
| 121 bool QueryRegValueMULTISZ(HANDLE key, | |
| 122 const wchar_t* value_name, | |
| 123 std::vector<std::wstring>* out_multi_sz); | |
| 124 | |
| 125 // Query MULTI_SZ (multiple strings) value. | |
| 126 // - WRAPPER: Function opens and closes the target key for caller, and works | |
| 127 // with MULTI_SZ data type. | |
| 128 bool QueryRegValueMULTISZ(ROOT_KEY root, | |
| 129 const wchar_t* key_path, | |
| 130 const wchar_t* value_name, | |
| 131 std::vector<std::wstring>* out_multi_sz); | |
| 132 | |
| 133 //------------------------------------------------------------------------------ | |
| 134 // Setter functions | |
| 135 //------------------------------------------------------------------------------ | |
| 136 | |
| 137 // Main function to set a registry value. | |
| 138 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | |
| 139 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ. | |
| 140 bool SetRegKeyValue(HANDLE key, | |
| 141 const wchar_t* value_name, | |
| 142 ULONG type, | |
| 143 const BYTE* data, | |
| 144 DWORD data_size); | |
| 145 | |
| 146 // Set DWORD value. | |
| 147 // - WRAPPER: Function works with DWORD data type. | |
| 148 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | |
| 149 // - Handle will be left open. Caller must still call CloseRegKey when done. | |
| 150 bool SetRegValueDWORD(HANDLE key, const wchar_t* value_name, DWORD value); | |
| 151 | |
| 152 // Set DWORD value. | |
| 153 // - WRAPPER: Function opens and closes the target key for caller, and works | |
| 154 // with DWORD data type. | |
| 155 bool SetRegValueDWORD(ROOT_KEY root, | |
| 156 const wchar_t* key_path, | |
| 157 const wchar_t* value_name, | |
| 158 DWORD value); | |
| 159 | |
| 160 // Set SZ (string) value. | |
| 161 // - WRAPPER: Function works with SZ data type. | |
| 162 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | |
| 163 // - Handle will be left open. Caller must still call CloseRegKey when done. | |
| 164 bool SetRegValueSZ(HANDLE key, | |
| 165 const wchar_t* value_name, | |
| 166 const std::wstring& value); | |
| 167 | |
| 168 // Set SZ (string) value. | |
| 169 // - WRAPPER: Function opens and closes the target key for caller, and works | |
| 170 // with SZ data type. | |
| 171 bool SetRegValueSZ(ROOT_KEY root, | |
| 172 const wchar_t* key_path, | |
| 173 const wchar_t* value_name, | |
| 174 const std::wstring& value); | |
| 175 | |
| 176 // Set MULTI_SZ (multiple strings) value. | |
| 177 // - WRAPPER: Function works with MULTI_SZ data type. | |
| 178 // - Key handle should have been opened with CreateRegKey or OpenRegKey. | |
| 179 // - Handle will be left open. Caller must still call CloseRegKey when done. | |
| 180 bool SetRegValueMULTISZ(HANDLE key, | |
| 181 const wchar_t* value_name, | |
| 182 const std::vector<std::wstring>& values); | |
| 183 | |
| 184 // Set MULTI_SZ (multiple strings) value. | |
| 185 // - WRAPPER: Function opens and closes the target key for caller, and works | |
| 186 // with MULTI_SZ data type. | |
| 187 bool SetRegValueMULTISZ(ROOT_KEY root, | |
| 188 const wchar_t* key_path, | |
| 189 const wchar_t* value_name, | |
| 190 const std::vector<std::wstring>& values); | |
| 191 | |
| 192 //------------------------------------------------------------------------------ | |
| 193 // Utils | |
| 194 //------------------------------------------------------------------------------ | |
| 195 | |
| 196 // Returns the current user SID in string form. | |
| 197 const wchar_t* GetCurrentUserSidString(); | |
| 198 | |
| 199 }; // namespace nt | |
| 200 | |
| 201 #endif // CHROME_ELF_NT_REGISTRY_NT_REGISTRY_H_ | |
| OLD | NEW |