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