OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/installer/util/google_update_util.h" | 5 #include "chrome/installer/util/google_update_util.h" |
6 | 6 |
| 7 #include <algorithm> |
| 8 #include <map> |
| 9 #include <utility> |
| 10 #include <vector> |
| 11 |
7 #include "base/command_line.h" | 12 #include "base/command_line.h" |
| 13 #include "base/environment.h" |
8 #include "base/file_util.h" | 14 #include "base/file_util.h" |
9 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
10 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 #include "base/memory/scoped_ptr.h" |
11 #include "base/path_service.h" | 18 #include "base/path_service.h" |
12 #include "base/process/kill.h" | 19 #include "base/process/kill.h" |
13 #include "base/process/launch.h" | 20 #include "base/process/launch.h" |
14 #include "base/strings/string16.h" | 21 #include "base/strings/string16.h" |
| 22 #include "base/strings/string_split.h" |
15 #include "base/time/time.h" | 23 #include "base/time/time.h" |
16 #include "base/win/registry.h" | 24 #include "base/win/registry.h" |
17 #include "base/win/scoped_handle.h" | 25 #include "base/win/scoped_handle.h" |
18 #include "base/win/win_util.h" | 26 #include "base/win/win_util.h" |
19 #include "base/win/windows_version.h" | 27 #include "base/win/windows_version.h" |
| 28 #include "chrome/installer/launcher_support/chrome_launcher_support.h" |
20 #include "chrome/installer/util/browser_distribution.h" | 29 #include "chrome/installer/util/browser_distribution.h" |
21 #include "chrome/installer/util/google_update_constants.h" | 30 #include "chrome/installer/util/google_update_constants.h" |
22 #include "chrome/installer/util/google_update_settings.h" | 31 #include "chrome/installer/util/google_update_settings.h" |
23 #include "chrome/installer/util/install_util.h" | 32 #include "chrome/installer/util/install_util.h" |
24 #include "chrome/installer/util/installation_state.h" | 33 #include "chrome/installer/util/installation_state.h" |
25 #include "chrome/installer/util/product.h" | 34 #include "chrome/installer/util/product.h" |
26 | 35 |
27 using base::win::RegKey; | 36 using base::win::RegKey; |
28 | 37 |
29 namespace google_update { | 38 namespace google_update { |
30 | 39 |
31 namespace { | 40 namespace { |
32 | 41 |
33 const int kGoogleUpdateTimeoutMs = 20 * 1000; | 42 const int kGoogleUpdateTimeoutMs = 20 * 1000; |
34 | 43 |
| 44 const char kEnvVariableUntrustedData[] = "GoogleUpdateUntrustedData"; |
| 45 const int kUntrustedDataMaxLength = 4096; |
| 46 |
35 // Returns true if Google Update is present at the given level. | 47 // Returns true if Google Update is present at the given level. |
36 bool IsGoogleUpdatePresent(bool system_install) { | 48 bool IsGoogleUpdatePresent(bool system_install) { |
37 // Using the existence of version key in the registry to decide. | 49 // Using the existence of version key in the registry to decide. |
38 return GoogleUpdateSettings::GetGoogleUpdateVersion(system_install).IsValid(); | 50 return GoogleUpdateSettings::GetGoogleUpdateVersion(system_install).IsValid(); |
39 } | 51 } |
40 | 52 |
41 // Returns GoogleUpdateSetup.exe's executable path at specified level. | 53 // Returns GoogleUpdateSetup.exe's executable path at specified level. |
42 // or an empty path if none is found. | 54 // or an empty path if none is found. |
43 base::FilePath GetGoogleUpdateSetupExe(bool system_install) { | 55 base::FilePath GetGoogleUpdateSetupExe(bool system_install) { |
44 const HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; | 56 const HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
101 << timeout.InMilliseconds() << " milliseconds to complete."; | 113 << timeout.InMilliseconds() << " milliseconds to complete."; |
102 } else if (exit_code != 0) { | 114 } else if (exit_code != 0) { |
103 LOG(ERROR) << "Command (" << cmd_string << ") exited with code " | 115 LOG(ERROR) << "Command (" << cmd_string << ") exited with code " |
104 << exit_code; | 116 << exit_code; |
105 } else { | 117 } else { |
106 success = true; | 118 success = true; |
107 } | 119 } |
108 return success; | 120 return success; |
109 } | 121 } |
110 | 122 |
| 123 bool IsNotPrintable(unsigned char c) { |
| 124 return c < 32 || c >= 127; |
| 125 } |
| 126 |
| 127 // Returns whether or not |s| consists of printable characters. |
| 128 bool IsStringPrintable(const std::string& s) { |
| 129 return std::find_if(s.begin(), s.end(), IsNotPrintable) == s.end(); |
| 130 } |
| 131 |
| 132 bool IsIllegalUntrustedDataKeyChar(unsigned char c) { |
| 133 return !(c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z' || |
| 134 c >= '0' && c <= '9' || c == '-' || c == '_' || c == '$'); |
| 135 } |
| 136 |
| 137 // Returns true if |key| from untrusted data is valid. |
| 138 bool IsUntrustedDataKeyValid(const std::string& key) { |
| 139 return std::find_if(key.begin(), key.end(), IsIllegalUntrustedDataKeyChar) |
| 140 == key.end(); |
| 141 } |
| 142 |
| 143 // Parses |data_string| as key-value pairs and overwrites |untrusted_data| with |
| 144 // the result. Returns true if the data could be parsed. |
| 145 bool ParseUntrustedData( |
| 146 const std::string& data_string, |
| 147 std::map<std::string, std::string>* untrusted_data) { |
| 148 DCHECK(untrusted_data); |
| 149 if (data_string.length() > kUntrustedDataMaxLength || |
| 150 !IsStringPrintable(data_string)) { |
| 151 LOG(ERROR) << "Invalid value in untrusted data string."; |
| 152 return false; |
| 153 } |
| 154 |
| 155 VLOG(1) << "Untrusted data string: " << data_string; |
| 156 |
| 157 std::vector<std::pair<std::string, std::string> > kv_pairs; |
| 158 if (!base::SplitStringIntoKeyValuePairs(data_string, '=', '&', &kv_pairs)) { |
| 159 LOG(ERROR) << "Failed to parse untrusted data: " << data_string; |
| 160 return false; |
| 161 } |
| 162 |
| 163 untrusted_data->clear(); |
| 164 std::vector<std::pair<std::string, std::string> >::const_iterator it; |
| 165 for (it = kv_pairs.begin(); it != kv_pairs.end(); ++it) { |
| 166 const std::string& key(it->first); |
| 167 // TODO(huangs): URL unescape |value|. |
| 168 const std::string& value(it->second); |
| 169 if (IsUntrustedDataKeyValid(key) && IsStringPrintable(value)) |
| 170 (*untrusted_data)[key] = value; |
| 171 else |
| 172 LOG(ERROR) << "Illegal character found in untrusted data."; |
| 173 } |
| 174 return true; |
| 175 } |
| 176 |
| 177 // Reads and parses untrusted data passed from Google Update as key-value |
| 178 // pairs, then overwrites |untrusted_data_map| with the result. |
| 179 // Returns true if data are successfully read. |
| 180 bool GetGoogleUpdateUntrustedData( |
| 181 std::map<std::string, std::string>* untrusted_data) { |
| 182 scoped_ptr<base::Environment> env(base::Environment::Create()); |
| 183 std::string data_string; |
| 184 if (!env || !env->GetVar(kEnvVariableUntrustedData, &data_string)) |
| 185 return false; |
| 186 |
| 187 return ParseUntrustedData(data_string, untrusted_data); |
| 188 } |
| 189 |
111 } // namespace | 190 } // namespace |
112 | 191 |
113 bool EnsureUserLevelGoogleUpdatePresent() { | 192 bool EnsureUserLevelGoogleUpdatePresent() { |
114 VLOG(0) << "Ensuring Google Update is present at user-level."; | 193 VLOG(0) << "Ensuring Google Update is present at user-level."; |
115 | 194 |
116 bool success = false; | 195 bool success = false; |
117 if (IsGoogleUpdatePresent(false)) { | 196 if (IsGoogleUpdatePresent(false)) { |
118 success = true; | 197 success = true; |
119 } else { | 198 } else { |
120 base::string16 cmd_string; | 199 base::string16 cmd_string; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
180 launch_options.force_breakaway_from_job_ = true; | 259 launch_options.force_breakaway_from_job_ = true; |
181 | 260 |
182 if (base::win::GetVersion() >= base::win::VERSION_VISTA && | 261 if (base::win::GetVersion() >= base::win::VERSION_VISTA && |
183 base::win::UserAccountControlIsEnabled()) { | 262 base::win::UserAccountControlIsEnabled()) { |
184 base::LaunchElevatedProcess(cmd, launch_options, NULL); | 263 base::LaunchElevatedProcess(cmd, launch_options, NULL); |
185 } else { | 264 } else { |
186 base::LaunchProcess(cmd, launch_options, NULL); | 265 base::LaunchProcess(cmd, launch_options, NULL); |
187 } | 266 } |
188 } | 267 } |
189 | 268 |
| 269 std::string GetUntrustedDataValue(const std::string& key) { |
| 270 std::map<std::string, std::string> untrusted_data; |
| 271 if (GetGoogleUpdateUntrustedData(&untrusted_data)) { |
| 272 std::map<std::string, std::string>::const_iterator data_it( |
| 273 untrusted_data.find(key)); |
| 274 if (data_it != untrusted_data.end()) |
| 275 return data_it->second; |
| 276 } |
| 277 |
| 278 return std::string(); |
| 279 } |
| 280 |
| 281 std::string GetUntrustedDataValueFromTag(const std::string& tag, |
| 282 const std::string& key) { |
| 283 std::map<std::string, std::string> untrusted_data; |
| 284 if (ParseUntrustedData(tag, &untrusted_data)) |
| 285 return untrusted_data[key]; |
| 286 |
| 287 return std::string(); |
| 288 } |
| 289 |
190 } // namespace google_update | 290 } // namespace google_update |
OLD | NEW |