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 | |
12 #include "base/command_line.h" | 7 #include "base/command_line.h" |
13 #include "base/environment.h" | |
14 #include "base/file_util.h" | 8 #include "base/file_util.h" |
15 #include "base/files/file_path.h" | 9 #include "base/files/file_path.h" |
16 #include "base/logging.h" | 10 #include "base/logging.h" |
17 #include "base/memory/scoped_ptr.h" | |
18 #include "base/path_service.h" | 11 #include "base/path_service.h" |
19 #include "base/process/kill.h" | 12 #include "base/process/kill.h" |
20 #include "base/process/launch.h" | 13 #include "base/process/launch.h" |
21 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
22 #include "base/strings/string_split.h" | |
23 #include "base/time/time.h" | 15 #include "base/time/time.h" |
24 #include "base/win/registry.h" | 16 #include "base/win/registry.h" |
25 #include "base/win/scoped_handle.h" | 17 #include "base/win/scoped_handle.h" |
26 #include "base/win/win_util.h" | 18 #include "base/win/win_util.h" |
27 #include "base/win/windows_version.h" | 19 #include "base/win/windows_version.h" |
28 #include "chrome/installer/launcher_support/chrome_launcher_support.h" | |
29 #include "chrome/installer/util/browser_distribution.h" | 20 #include "chrome/installer/util/browser_distribution.h" |
30 #include "chrome/installer/util/google_update_constants.h" | 21 #include "chrome/installer/util/google_update_constants.h" |
31 #include "chrome/installer/util/google_update_settings.h" | 22 #include "chrome/installer/util/google_update_settings.h" |
32 #include "chrome/installer/util/install_util.h" | 23 #include "chrome/installer/util/install_util.h" |
33 #include "chrome/installer/util/installation_state.h" | 24 #include "chrome/installer/util/installation_state.h" |
34 #include "chrome/installer/util/product.h" | 25 #include "chrome/installer/util/product.h" |
35 | 26 |
36 using base::win::RegKey; | 27 using base::win::RegKey; |
37 | 28 |
38 namespace google_update { | 29 namespace google_update { |
39 | 30 |
40 namespace { | 31 namespace { |
41 | 32 |
42 const int kGoogleUpdateTimeoutMs = 20 * 1000; | 33 const int kGoogleUpdateTimeoutMs = 20 * 1000; |
43 | 34 |
44 const char kEnvVariableUntrustedData[] = "GoogleUpdateUntrustedData"; | |
45 const int kUntrustedDataMaxLength = 4096; | |
46 | |
47 // Returns true if Google Update is present at the given level. | 35 // Returns true if Google Update is present at the given level. |
48 bool IsGoogleUpdatePresent(bool system_install) { | 36 bool IsGoogleUpdatePresent(bool system_install) { |
49 // Using the existence of version key in the registry to decide. | 37 // Using the existence of version key in the registry to decide. |
50 return GoogleUpdateSettings::GetGoogleUpdateVersion(system_install).IsValid(); | 38 return GoogleUpdateSettings::GetGoogleUpdateVersion(system_install).IsValid(); |
51 } | 39 } |
52 | 40 |
53 // Returns GoogleUpdateSetup.exe's executable path at specified level. | 41 // Returns GoogleUpdateSetup.exe's executable path at specified level. |
54 // or an empty path if none is found. | 42 // or an empty path if none is found. |
55 base::FilePath GetGoogleUpdateSetupExe(bool system_install) { | 43 base::FilePath GetGoogleUpdateSetupExe(bool system_install) { |
56 const HKEY root_key = system_install ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; | 44 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... |
113 << timeout.InMilliseconds() << " milliseconds to complete."; | 101 << timeout.InMilliseconds() << " milliseconds to complete."; |
114 } else if (exit_code != 0) { | 102 } else if (exit_code != 0) { |
115 LOG(ERROR) << "Command (" << cmd_string << ") exited with code " | 103 LOG(ERROR) << "Command (" << cmd_string << ") exited with code " |
116 << exit_code; | 104 << exit_code; |
117 } else { | 105 } else { |
118 success = true; | 106 success = true; |
119 } | 107 } |
120 return success; | 108 return success; |
121 } | 109 } |
122 | 110 |
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 | |
190 } // namespace | 111 } // namespace |
191 | 112 |
192 bool EnsureUserLevelGoogleUpdatePresent() { | 113 bool EnsureUserLevelGoogleUpdatePresent() { |
193 VLOG(0) << "Ensuring Google Update is present at user-level."; | 114 VLOG(0) << "Ensuring Google Update is present at user-level."; |
194 | 115 |
195 bool success = false; | 116 bool success = false; |
196 if (IsGoogleUpdatePresent(false)) { | 117 if (IsGoogleUpdatePresent(false)) { |
197 success = true; | 118 success = true; |
198 } else { | 119 } else { |
199 base::string16 cmd_string; | 120 base::string16 cmd_string; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 launch_options.force_breakaway_from_job_ = true; | 180 launch_options.force_breakaway_from_job_ = true; |
260 | 181 |
261 if (base::win::GetVersion() >= base::win::VERSION_VISTA && | 182 if (base::win::GetVersion() >= base::win::VERSION_VISTA && |
262 base::win::UserAccountControlIsEnabled()) { | 183 base::win::UserAccountControlIsEnabled()) { |
263 base::LaunchElevatedProcess(cmd, launch_options, NULL); | 184 base::LaunchElevatedProcess(cmd, launch_options, NULL); |
264 } else { | 185 } else { |
265 base::LaunchProcess(cmd, launch_options, NULL); | 186 base::LaunchProcess(cmd, launch_options, NULL); |
266 } | 187 } |
267 } | 188 } |
268 | 189 |
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 | |
290 } // namespace google_update | 190 } // namespace google_update |
OLD | NEW |