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 std::wstring HKLM_override; |
| 32 extern std::wstring HKCU_override; |
| 33 |
| 34 // AUTO will choose depending on system install or not. |
| 35 // Use HKLM or HKCU to override. |
| 36 typedef enum _ROOT_KEY { AUTO = 0, HKLM, HKCU } ROOT_KEY; |
| 37 |
| 38 // Create and/or open a registry key. |
| 39 // - This function will recursively create multiple sub-keys if required for |
| 40 // |key_path|. |
| 41 // - If the key doesn't need to be left open, pass in nullptr for |out_handle|. |
| 42 // - This function will happily succeed if the key already exists. |
| 43 // - Optional |out_handle|. If nullptr, function will close handle when done. |
| 44 // - Caller must call CloseRegKey on returned handle (on success). |
| 45 bool CreateRegKey(ROOT_KEY root, |
| 46 const wchar_t* key_path, |
| 47 ACCESS_MASK access, |
| 48 HANDLE* out_handle OPTIONAL); |
| 49 |
| 50 // Open existing registry key. |
| 51 // - Caller must call CloseRegKey on returned handle (on success). |
| 52 // - Optional error code can be returned on failure for extra detail. |
| 53 bool OpenRegKey(ROOT_KEY root, |
| 54 const wchar_t* key_path, |
| 55 ACCESS_MASK access, |
| 56 HANDLE* out_handle, |
| 57 NTSTATUS* error_code OPTIONAL); |
| 58 |
| 59 // Delete a registry key. |
| 60 // - Caller must still call CloseRegKey after the delete. |
| 61 bool DeleteRegKey(HANDLE key); |
| 62 |
| 63 // Delete a registry key. |
| 64 // - WRAPPER: Function opens and closes the target key for caller. |
| 65 bool DeleteRegKey(ROOT_KEY root, const wchar_t* key_path); |
| 66 |
| 67 // Close a registry key handle that was opened with CreateRegKey or OpenRegKey. |
| 68 void CloseRegKey(HANDLE key); |
| 69 |
| 70 //------------------------------------------------------------------------------ |
| 71 // Getter functions |
| 72 //------------------------------------------------------------------------------ |
| 73 |
| 74 // Main function to query a registry value. |
| 75 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 76 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ. |
| 77 // - Caller is responsible for calling "delete[] *out_buffer" (on success). |
| 78 bool QueryRegKeyValue(HANDLE key, |
| 79 const wchar_t* value_name, |
| 80 ULONG* out_type, |
| 81 BYTE** out_buffer, |
| 82 DWORD* out_size); |
| 83 |
| 84 // Query DWORD value. |
| 85 // - WRAPPER: Function works with DWORD data type. |
| 86 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 87 // - Handle will be left open. Caller must still call CloseRegKey when done. |
| 88 bool QueryRegValueDWORD(HANDLE key, |
| 89 const wchar_t* value_name, |
| 90 DWORD* out_dword); |
| 91 |
| 92 // Query DWORD value. |
| 93 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 94 // with DWORD data type. |
| 95 bool QueryRegValueDWORD(ROOT_KEY root, |
| 96 const wchar_t* key_path, |
| 97 const wchar_t* value_name, |
| 98 DWORD* out_dword); |
| 99 |
| 100 // Query SZ (string) value. |
| 101 // - WRAPPER: Function works with SZ data type. |
| 102 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 103 // - Handle will be left open. Caller must still call CloseRegKey when done. |
| 104 bool QueryRegValueSZ(HANDLE key, |
| 105 const wchar_t* value_name, |
| 106 std::wstring* out_sz); |
| 107 |
| 108 // Query SZ (string) value. |
| 109 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 110 // with SZ data type. |
| 111 bool QueryRegValueSZ(ROOT_KEY root, |
| 112 const wchar_t* key_path, |
| 113 const wchar_t* value_name, |
| 114 std::wstring* out_sz); |
| 115 |
| 116 // Query MULTI_SZ (multiple strings) value. |
| 117 // - WRAPPER: Function works with MULTI_SZ data type. |
| 118 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 119 // - Handle will be left open. Caller must still call CloseRegKey when done. |
| 120 bool QueryRegValueMULTISZ(HANDLE key, |
| 121 const wchar_t* value_name, |
| 122 std::vector<std::wstring>* out_multi_sz); |
| 123 |
| 124 // Query MULTI_SZ (multiple strings) value. |
| 125 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 126 // with MULTI_SZ data type. |
| 127 bool QueryRegValueMULTISZ(ROOT_KEY root, |
| 128 const wchar_t* key_path, |
| 129 const wchar_t* value_name, |
| 130 std::vector<std::wstring>* out_multi_sz); |
| 131 |
| 132 //------------------------------------------------------------------------------ |
| 133 // Setter functions |
| 134 //------------------------------------------------------------------------------ |
| 135 |
| 136 // Main function to set a registry value. |
| 137 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 138 // - Types defined in winnt.h. E.g.: REG_DWORD, REG_SZ. |
| 139 bool SetRegKeyValue(HANDLE key, |
| 140 const wchar_t* value_name, |
| 141 ULONG type, |
| 142 const BYTE* data, |
| 143 DWORD data_size); |
| 144 |
| 145 // Set DWORD value. |
| 146 // - WRAPPER: Function works with DWORD data type. |
| 147 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 148 // - Handle will be left open. Caller must still call CloseRegKey when done. |
| 149 bool SetRegValueDWORD(HANDLE key, const wchar_t* value_name, DWORD value); |
| 150 |
| 151 // Set DWORD value. |
| 152 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 153 // with DWORD data type. |
| 154 bool SetRegValueDWORD(ROOT_KEY root, |
| 155 const wchar_t* key_path, |
| 156 const wchar_t* value_name, |
| 157 DWORD value); |
| 158 |
| 159 // Set SZ (string) value. |
| 160 // - WRAPPER: Function works with SZ data type. |
| 161 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 162 // - Handle will be left open. Caller must still call CloseRegKey when done. |
| 163 bool SetRegValueSZ(HANDLE key, |
| 164 const wchar_t* value_name, |
| 165 const std::wstring& value); |
| 166 |
| 167 // Set SZ (string) value. |
| 168 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 169 // with SZ data type. |
| 170 bool SetRegValueSZ(ROOT_KEY root, |
| 171 const wchar_t* key_path, |
| 172 const wchar_t* value_name, |
| 173 const std::wstring& value); |
| 174 |
| 175 // Set MULTI_SZ (multiple strings) value. |
| 176 // - WRAPPER: Function works with MULTI_SZ data type. |
| 177 // - Key handle should have been opened with CreateRegKey or OpenRegKey. |
| 178 // - Handle will be left open. Caller must still call CloseRegKey when done. |
| 179 bool SetRegValueMULTISZ(HANDLE key, |
| 180 const wchar_t* value_name, |
| 181 const std::vector<std::wstring>& values); |
| 182 |
| 183 // Set MULTI_SZ (multiple strings) value. |
| 184 // - WRAPPER: Function opens and closes the target key for caller, and works |
| 185 // with MULTI_SZ data type. |
| 186 bool SetRegValueMULTISZ(ROOT_KEY root, |
| 187 const wchar_t* key_path, |
| 188 const wchar_t* value_name, |
| 189 const std::vector<std::wstring>& values); |
| 190 |
| 191 //------------------------------------------------------------------------------ |
| 192 // Utils |
| 193 //------------------------------------------------------------------------------ |
| 194 |
| 195 // Returns the current user SID in string form. |
| 196 std::wstring GetCurrentUserSidString(); |
| 197 |
| 198 }; // namespace nt |
| 199 |
| 200 #endif // CHROME_ELF_NT_REGISTRY_NT_REGISTRY_H_ |
OLD | NEW |