1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et cindent: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "Registry.h"
#include <windows.h>
#include <shlwapi.h>
#include "common.h"
#include "EventLog.h"
#include "UtfConvert.h"
#include "mozilla/Buffer.h"
#include "mozilla/Try.h"
namespace mozilla::default_agent {
using WStringResult = mozilla::WindowsErrorResult<std::wstring>;
static WStringResult MaybePrefixRegistryValueName(
IsPrefixed isPrefixed, const wchar_t* registryValueNameSuffix) {
if (isPrefixed == IsPrefixed::Unprefixed) {
std::wstring registryValueName = registryValueNameSuffix;
return registryValueName;
}
mozilla::UniquePtr<wchar_t[]> installPath = mozilla::GetFullBinaryPath();
if (!PathRemoveFileSpecW(installPath.get())) {
HRESULT hr = HRESULT_FROM_WIN32(ERROR_BAD_PATHNAME);
LOG_ERROR(hr);
return mozilla::Err(mozilla::WindowsError::FromHResult(hr));
}
std::wstring registryValueName(installPath.get());
registryValueName.append(L"|");
registryValueName.append(registryValueNameSuffix);
return registryValueName;
}
// Creates a sub key of AGENT_REGKEY_NAME by appending the passed subKey. If
// subKey is null, nothing is appended.
static std::wstring MakeKeyName(const wchar_t* subKey) {
std::wstring keyName = AGENT_REGKEY_NAME;
if (subKey) {
keyName += L"\\";
keyName += subKey;
}
return keyName;
}
MaybeStringResult RegistryGetValueString(
IsPrefixed isPrefixed, const wchar_t* registryValueName,
const wchar_t* subKey /* = nullptr */) {
// Get the full registry value name
WStringResult registryValueNameResult =
MaybePrefixRegistryValueName(isPrefixed, registryValueName);
if (registryValueNameResult.isErr()) {
return mozilla::Err(registryValueNameResult.unwrapErr());
}
std::wstring valueName = registryValueNameResult.unwrap();
std::wstring keyName = MakeKeyName(subKey);
// Get the string size
DWORD wideDataSize = 0;
LSTATUS ls =
RegGetValueW(HKEY_CURRENT_USER, keyName.c_str(), valueName.c_str(),
RRF_RT_REG_SZ, nullptr, nullptr, &wideDataSize);
if (ls == ERROR_FILE_NOT_FOUND) {
return mozilla::Maybe<std::string>(mozilla::Nothing());
} else if (ls != ERROR_SUCCESS) {
HRESULT hr = HRESULT_FROM_WIN32(ls);
LOG_ERROR(hr);
return mozilla::Err(mozilla::WindowsError::FromHResult(hr));
}
// Convert bytes to characters. The extra character should be unnecessary, but
// addresses the possible rounding problem inherent with integer division.
DWORD charCount = (wideDataSize / sizeof(wchar_t)) + 1;
// Read the data from the registry into a wide string
mozilla::Buffer<wchar_t> wideData(charCount);
ls = RegGetValueW(HKEY_CURRENT_USER, keyName.c_str(), valueName.c_str(),
RRF_RT_REG_SZ, nullptr, wideData.Elements(), &wideDataSize);
if (ls != ERROR_SUCCESS) {
HRESULT hr = HRESULT_FROM_WIN32(ls);
LOG_ERROR(hr);
return mozilla::Err(mozilla::WindowsError::FromHResult(hr));
}
// Convert to narrow string and return.
std::string narrowData;
MOZ_TRY_VAR(narrowData, Utf16ToUtf8(wideData.Elements()));
return mozilla::Some(narrowData);
}
VoidResult RegistrySetValueString(IsPrefixed isPrefixed,
const wchar_t* registryValueName,
const char* newValue,
const wchar_t* subKey /* = nullptr */) {
// Get the full registry value name
WStringResult registryValueNameResult =
MaybePrefixRegistryValueName(isPrefixed, registryValueName);
if (registryValueNameResult.isErr()) {
return mozilla::Err(registryValueNameResult.unwrapErr());
}
std::wstring valueName = registryValueNameResult.unwrap();
std::wstring keyName = MakeKeyName(subKey);
// Convert the value from a narrow string to a wide string
std::wstring wideValue;
MOZ_TRY_VAR(wideValue, Utf8ToUtf16(newValue));
// Store the value
LSTATUS ls = RegSetKeyValueW(HKEY_CURRENT_USER, keyName.c_str(),
valueName.c_str(), REG_SZ, wideValue.c_str(),
(wideValue.size() + 1) * sizeof(wchar_t));
if (ls != ERROR_SUCCESS) {
HRESULT hr = HRESULT_FROM_WIN32(ls);
LOG_ERROR(hr);
return mozilla::Err(mozilla::WindowsError::FromHResult(hr));
}
return mozilla::Ok();
}
MaybeBoolResult RegistryGetValueBool(IsPrefixed isPrefixed,
const wchar_t* registryValueName,
const wchar_t* subKey /* = nullptr */) {
// Get the full registry value name
WStringResult registryValueNameResult =
MaybePrefixRegistryValueName(isPrefixed, registryValueName);
if (registryValueNameResult.isErr()) {
return mozilla::Err(registryValueNameResult.unwrapErr());
}
std::wstring valueName = registryValueNameResult.unwrap();
std::wstring keyName = MakeKeyName(subKey);
// Read the integer value from the registry
DWORD value;
DWORD valueSize = sizeof(DWORD);
LSTATUS ls =
RegGetValueW(HKEY_CURRENT_USER, keyName.c_str(), valueName.c_str(),
RRF_RT_REG_DWORD, nullptr, &value, &valueSize);
if (ls == ERROR_FILE_NOT_FOUND) {
return mozilla::Maybe<bool>(mozilla::Nothing());
}
if (ls != ERROR_SUCCESS) {
HRESULT hr = HRESULT_FROM_WIN32(ls);
LOG_ERROR(hr);
return mozilla::Err(mozilla::WindowsError::FromHResult(hr));
}
return mozilla::Some(value != 0);
}
VoidResult RegistrySetValueBool(IsPrefixed isPrefixed,
const wchar_t* registryValueName, bool newValue,
const wchar_t* subKey /* = nullptr */) {
// Get the full registry value name
WStringResult registryValueNameResult =
MaybePrefixRegistryValueName(isPrefixed, registryValueName);
if (registryValueNameResult.isErr()) {
return mozilla::Err(registryValueNameResult.unwrapErr());
}
std::wstring valueName = registryValueNameResult.unwrap();
std::wstring keyName = MakeKeyName(subKey);
// Write the value to the registry
DWORD value = newValue ? 1 : 0;
LSTATUS ls =
RegSetKeyValueW(HKEY_CURRENT_USER, keyName.c_str(), valueName.c_str(),
REG_DWORD, &value, sizeof(DWORD));
if (ls != ERROR_SUCCESS) {
HRESULT hr = HRESULT_FROM_WIN32(ls);
LOG_ERROR(hr);
return mozilla::Err(mozilla::WindowsError::FromHResult(hr));
}
return mozilla::Ok();
}
MaybeQwordResult RegistryGetValueQword(IsPrefixed isPrefixed,
const wchar_t* registryValueName,
const wchar_t* subKey /* = nullptr */) {
// Get the full registry value name
WStringResult registryValueNameResult =
MaybePrefixRegistryValueName(isPrefixed, registryValueName);
if (registryValueNameResult.isErr()) {
return mozilla::Err(registryValueNameResult.unwrapErr());
}
std::wstring valueName = registryValueNameResult.unwrap();
std::wstring keyName = MakeKeyName(subKey);
// Read the integer value from the registry
ULONGLONG value;
DWORD valueSize = sizeof(ULONGLONG);
LSTATUS ls =
RegGetValueW(HKEY_CURRENT_USER, keyName.c_str(), valueName.c_str(),
RRF_RT_REG_QWORD, nullptr, &value, &valueSize);
if (ls == ERROR_FILE_NOT_FOUND) {
return mozilla::Maybe<ULONGLONG>(mozilla::Nothing());
}
if (ls != ERROR_SUCCESS) {
HRESULT hr = HRESULT_FROM_WIN32(ls);
LOG_ERROR(hr);
return mozilla::Err(mozilla::WindowsError::FromHResult(hr));
}
return mozilla::Some(value);
}
VoidResult RegistrySetValueQword(IsPrefixed isPrefixed,
const wchar_t* registryValueName,
ULONGLONG newValue,
const wchar_t* subKey /* = nullptr */) {
// Get the full registry value name
WStringResult registryValueNameResult =
MaybePrefixRegistryValueName(isPrefixed, registryValueName);
if (registryValueNameResult.isErr()) {
return mozilla::Err(registryValueNameResult.unwrapErr());
}
std::wstring valueName = registryValueNameResult.unwrap();
std::wstring keyName = MakeKeyName(subKey);
// Write the value to the registry
LSTATUS ls =
RegSetKeyValueW(HKEY_CURRENT_USER, keyName.c_str(), valueName.c_str(),
REG_QWORD, &newValue, sizeof(ULONGLONG));
if (ls != ERROR_SUCCESS) {
HRESULT hr = HRESULT_FROM_WIN32(ls);
LOG_ERROR(hr);
return mozilla::Err(mozilla::WindowsError::FromHResult(hr));
}
return mozilla::Ok();
}
MaybeDwordResult RegistryGetValueDword(IsPrefixed isPrefixed,
const wchar_t* registryValueName,
const wchar_t* subKey /* = nullptr */) {
// Get the full registry value name
WStringResult registryValueNameResult =
MaybePrefixRegistryValueName(isPrefixed, registryValueName);
if (registryValueNameResult.isErr()) {
return mozilla::Err(registryValueNameResult.unwrapErr());
}
std::wstring valueName = registryValueNameResult.unwrap();
std::wstring keyName = MakeKeyName(subKey);
// Read the integer value from the registry
uint32_t value;
DWORD valueSize = sizeof(uint32_t);
LSTATUS ls =
RegGetValueW(HKEY_CURRENT_USER, keyName.c_str(), valueName.c_str(),
RRF_RT_DWORD, nullptr, &value, &valueSize);
if (ls == ERROR_FILE_NOT_FOUND) {
return mozilla::Maybe<uint32_t>(mozilla::Nothing());
}
if (ls != ERROR_SUCCESS) {
HRESULT hr = HRESULT_FROM_WIN32(ls);
LOG_ERROR(hr);
return mozilla::Err(mozilla::WindowsError::FromHResult(hr));
}
return mozilla::Some(value);
}
VoidResult RegistrySetValueDword(IsPrefixed isPrefixed,
const wchar_t* registryValueName,
uint32_t newValue,
const wchar_t* subKey /* = nullptr */) {
// Get the full registry value name
WStringResult registryValueNameResult =
MaybePrefixRegistryValueName(isPrefixed, registryValueName);
if (registryValueNameResult.isErr()) {
return mozilla::Err(registryValueNameResult.unwrapErr());
}
std::wstring valueName = registryValueNameResult.unwrap();
std::wstring keyName = MakeKeyName(subKey);
// Write the value to the registry
LSTATUS ls =
RegSetKeyValueW(HKEY_CURRENT_USER, keyName.c_str(), valueName.c_str(),
REG_DWORD, &newValue, sizeof(uint32_t));
if (ls != ERROR_SUCCESS) {
HRESULT hr = HRESULT_FROM_WIN32(ls);
LOG_ERROR(hr);
return mozilla::Err(mozilla::WindowsError::FromHResult(hr));
}
return mozilla::Ok();
}
VoidResult RegistryDeleteValue(IsPrefixed isPrefixed,
const wchar_t* registryValueName,
const wchar_t* subKey /* = nullptr */) {
// Get the full registry value name
WStringResult registryValueNameResult =
MaybePrefixRegistryValueName(isPrefixed, registryValueName);
if (registryValueNameResult.isErr()) {
return mozilla::Err(registryValueNameResult.unwrapErr());
}
std::wstring valueName = registryValueNameResult.unwrap();
std::wstring keyName = MakeKeyName(subKey);
LSTATUS ls =
RegDeleteKeyValueW(HKEY_CURRENT_USER, keyName.c_str(), valueName.c_str());
if (ls != ERROR_SUCCESS) {
HRESULT hr = HRESULT_FROM_WIN32(ls);
LOG_ERROR(hr);
return mozilla::Err(mozilla::WindowsError::FromHResult(hr));
}
return mozilla::Ok();
}
} // namespace mozilla::default_agent
|