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