/rlz/win/lib/machine_deal.cc

https://gitlab.com/0072016/Facebook-SDK- · C++ · 301 lines · 220 code · 60 blank · 21 comment · 51 complexity · ac6f5de9ffbc28287266e0262610b0a0 MD5 · raw file

  1. // Copyright (c) 2012 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. // Library functions related to the OEM Deal Confirmation Code.
  6. #include "rlz/win/lib/machine_deal.h"
  7. #include <windows.h>
  8. #include <stddef.h>
  9. #include <vector>
  10. #include "base/macros.h"
  11. #include "base/strings/string_split.h"
  12. #include "base/strings/string_util.h"
  13. #include "base/strings/stringprintf.h"
  14. #include "base/win/registry.h"
  15. #include "rlz/lib/assert.h"
  16. #include "rlz/lib/lib_values.h"
  17. #include "rlz/win/lib/lib_mutex.h"
  18. #include "rlz/win/lib/registry_util.h"
  19. #include "rlz/win/lib/rlz_value_store_registry.h"
  20. namespace {
  21. const wchar_t kDccValueName[] = L"DCC";
  22. // Current DCC can only uses [a-zA-Z0-9_-!@$*();.<>,:]
  23. // We will be more liberal and allow some additional chars, but not url meta
  24. // chars.
  25. bool IsGoodDccChar(char ch) {
  26. if (base::IsAsciiAlpha(ch) || base::IsAsciiDigit(ch))
  27. return true;
  28. switch (ch) {
  29. case '_':
  30. case '-':
  31. case '!':
  32. case '@':
  33. case '$':
  34. case '*':
  35. case '(':
  36. case ')':
  37. case ';':
  38. case '.':
  39. case '<':
  40. case '>':
  41. case ',':
  42. case ':':
  43. return true;
  44. }
  45. return false;
  46. }
  47. // This function will remove bad rlz chars and also limit the max rlz to some
  48. // reasonable size. It also assumes that normalized_dcc is at least
  49. // kMaxDccLength+1 long.
  50. void NormalizeDcc(const char* raw_dcc, char* normalized_dcc) {
  51. size_t index = 0;
  52. for (; raw_dcc[index] != 0 && index < rlz_lib::kMaxDccLength; ++index) {
  53. char current = raw_dcc[index];
  54. if (IsGoodDccChar(current)) {
  55. normalized_dcc[index] = current;
  56. } else {
  57. normalized_dcc[index] = '.';
  58. }
  59. }
  60. normalized_dcc[index] = 0;
  61. }
  62. bool GetResponseLine(const char* response_text, int response_length,
  63. int* search_index, std::string* response_line) {
  64. if (!response_line || !search_index || *search_index > response_length)
  65. return false;
  66. response_line->clear();
  67. if (*search_index < 0)
  68. return false;
  69. int line_begin = *search_index;
  70. const char* line_end = strchr(response_text + line_begin, '\n');
  71. if (line_end == NULL || line_end - response_text > response_length) {
  72. line_end = response_text + response_length;
  73. *search_index = -1;
  74. } else {
  75. *search_index = line_end - response_text + 1;
  76. }
  77. response_line->assign(response_text + line_begin,
  78. line_end - response_text - line_begin);
  79. return true;
  80. }
  81. bool GetResponseValue(const std::string& response_line,
  82. const std::string& response_key,
  83. std::string* value) {
  84. if (!value)
  85. return false;
  86. value->clear();
  87. if (!base::StartsWith(response_line, response_key,
  88. base::CompareCase::SENSITIVE))
  89. return false;
  90. std::vector<std::string> tokens = base::SplitString(
  91. response_line, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
  92. if (tokens.size() != 2)
  93. return false;
  94. // The first token is the key, the second is the value. The value is already
  95. // trimmed for whitespace.
  96. *value = tokens[1];
  97. return true;
  98. }
  99. } // namespace
  100. namespace rlz_lib {
  101. bool MachineDealCode::Set(const char* dcc) {
  102. LibMutex lock;
  103. if (lock.failed())
  104. return false;
  105. // TODO: if (!ProcessInfo::CanWriteMachineKey()) return false;
  106. // Validate the new dcc value.
  107. size_t length = strlen(dcc);
  108. if (length > kMaxDccLength) {
  109. ASSERT_STRING("MachineDealCode::Set: DCC length is exceeds max allowed.");
  110. return false;
  111. }
  112. base::win::RegKey hklm_key(HKEY_LOCAL_MACHINE,
  113. RlzValueStoreRegistry::GetWideLibKeyName().c_str(),
  114. KEY_READ | KEY_WRITE | KEY_WOW64_32KEY);
  115. if (!hklm_key.Valid()) {
  116. ASSERT_STRING("MachineDealCode::Set: Unable to create / open machine key."
  117. " Did you call rlz_lib::CreateMachineState()?");
  118. return false;
  119. }
  120. char normalized_dcc[kMaxDccLength + 1];
  121. NormalizeDcc(dcc, normalized_dcc);
  122. VERIFY(length == strlen(normalized_dcc));
  123. // Write the DCC to HKLM. Note that we need to include the null character
  124. // when writing the string.
  125. if (!RegKeyWriteValue(&hklm_key, kDccValueName, normalized_dcc)) {
  126. ASSERT_STRING("MachineDealCode::Set: Could not write the DCC value");
  127. return false;
  128. }
  129. return true;
  130. }
  131. bool MachineDealCode::GetNewCodeFromPingResponse(const char* response,
  132. bool* has_new_dcc, char* new_dcc, int new_dcc_size) {
  133. if (!has_new_dcc || !new_dcc || !new_dcc_size)
  134. return false;
  135. *has_new_dcc = false;
  136. new_dcc[0] = 0;
  137. int response_length = -1;
  138. if (!IsPingResponseValid(response, &response_length))
  139. return false;
  140. // Get the current DCC value to compare to later)
  141. char stored_dcc[kMaxDccLength + 1];
  142. if (!Get(stored_dcc, arraysize(stored_dcc)))
  143. stored_dcc[0] = 0;
  144. int search_index = 0;
  145. std::string response_line;
  146. std::string new_dcc_value;
  147. bool old_dcc_confirmed = false;
  148. const std::string dcc_cgi(kDccCgiVariable);
  149. const std::string dcc_cgi_response(kSetDccResponseVariable);
  150. while (GetResponseLine(response, response_length, &search_index,
  151. &response_line)) {
  152. std::string value;
  153. if (!old_dcc_confirmed &&
  154. GetResponseValue(response_line, dcc_cgi, &value)) {
  155. // This is the old DCC confirmation - should match value in registry.
  156. if (value != stored_dcc)
  157. return false; // Corrupted DCC - ignore this response.
  158. else
  159. old_dcc_confirmed = true;
  160. continue;
  161. }
  162. if (!(*has_new_dcc) &&
  163. GetResponseValue(response_line, dcc_cgi_response, &value)) {
  164. // This is the new DCC.
  165. if (value.size() > kMaxDccLength) continue; // Too long
  166. *has_new_dcc = true;
  167. new_dcc_value = value;
  168. }
  169. }
  170. old_dcc_confirmed |= (NULL == stored_dcc[0]);
  171. base::strlcpy(new_dcc, new_dcc_value.c_str(), new_dcc_size);
  172. return old_dcc_confirmed;
  173. }
  174. bool MachineDealCode::SetFromPingResponse(const char* response) {
  175. bool has_new_dcc = false;
  176. char new_dcc[kMaxDccLength + 1];
  177. bool response_valid = GetNewCodeFromPingResponse(
  178. response, &has_new_dcc, new_dcc, arraysize(new_dcc));
  179. if (response_valid && has_new_dcc)
  180. return Set(new_dcc);
  181. return response_valid;
  182. }
  183. bool MachineDealCode::GetAsCgi(char* cgi, int cgi_size) {
  184. if (!cgi || cgi_size <= 0) {
  185. ASSERT_STRING("MachineDealCode::GetAsCgi: Invalid buffer");
  186. return false;
  187. }
  188. cgi[0] = 0;
  189. std::string cgi_arg;
  190. base::StringAppendF(&cgi_arg, "%s=", kDccCgiVariable);
  191. int cgi_arg_length = cgi_arg.size();
  192. if (cgi_arg_length >= cgi_size) {
  193. ASSERT_STRING("MachineDealCode::GetAsCgi: Insufficient buffer size");
  194. return false;
  195. }
  196. base::strlcpy(cgi, cgi_arg.c_str(), cgi_size);
  197. if (!Get(cgi + cgi_arg_length, cgi_size - cgi_arg_length)) {
  198. cgi[0] = 0;
  199. return false;
  200. }
  201. return true;
  202. }
  203. bool MachineDealCode::Get(char* dcc, int dcc_size) {
  204. LibMutex lock;
  205. if (lock.failed())
  206. return false;
  207. if (!dcc || dcc_size <= 0) {
  208. ASSERT_STRING("MachineDealCode::Get: Invalid buffer");
  209. return false;
  210. }
  211. dcc[0] = 0;
  212. base::win::RegKey dcc_key(HKEY_LOCAL_MACHINE,
  213. RlzValueStoreRegistry::GetWideLibKeyName().c_str(),
  214. KEY_READ | KEY_WOW64_32KEY);
  215. if (!dcc_key.Valid())
  216. return false; // no DCC key.
  217. size_t size = dcc_size;
  218. if (!RegKeyReadValue(dcc_key, kDccValueName, dcc, &size)) {
  219. ASSERT_STRING("MachineDealCode::Get: Insufficient buffer size");
  220. dcc[0] = 0;
  221. return false;
  222. }
  223. return true;
  224. }
  225. bool MachineDealCode::Clear() {
  226. base::win::RegKey dcc_key(HKEY_LOCAL_MACHINE,
  227. RlzValueStoreRegistry::GetWideLibKeyName().c_str(),
  228. KEY_READ | KEY_WRITE | KEY_WOW64_32KEY);
  229. if (!dcc_key.Valid())
  230. return false; // no DCC key.
  231. dcc_key.DeleteValue(kDccValueName);
  232. // Verify deletion.
  233. wchar_t dcc[kMaxDccLength + 1];
  234. DWORD dcc_size = arraysize(dcc);
  235. if (dcc_key.ReadValue(kDccValueName, dcc, &dcc_size, NULL) == ERROR_SUCCESS) {
  236. ASSERT_STRING("MachineDealCode::Clear: Could not delete the DCC value.");
  237. return false;
  238. }
  239. return true;
  240. }
  241. } // namespace rlz_lib