| Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // | 
|  | 2 | // Copyright (C) 2012 The Android Open Source Project | 
|  | 3 | // | 
|  | 4 | // Licensed under the Apache License, Version 2.0 (the "License"); | 
|  | 5 | // you may not use this file except in compliance with the License. | 
|  | 6 | // You may obtain a copy of the License at | 
|  | 7 | // | 
|  | 8 | //      http://www.apache.org/licenses/LICENSE-2.0 | 
|  | 9 | // | 
|  | 10 | // Unless required by applicable law or agreed to in writing, software | 
|  | 11 | // distributed under the License is distributed on an "AS IS" BASIS, | 
|  | 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
|  | 13 | // See the License for the specific language governing permissions and | 
|  | 14 | // limitations under the License. | 
|  | 15 | // | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 |  | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 17 | #include "update_engine/omaha_request_action.h" | 
| Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 18 |  | 
| Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 19 | #include <inttypes.h> | 
| Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 20 |  | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 21 | #include <map> | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 22 | #include <sstream> | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 23 | #include <string> | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 24 | #include <vector> | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 25 |  | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 26 | #include <base/bind.h> | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 27 | #include <base/logging.h> | 
|  | 28 | #include <base/rand_util.h> | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 29 | #include <base/strings/string_number_conversions.h> | 
|  | 30 | #include <base/strings/string_util.h> | 
|  | 31 | #include <base/strings/stringprintf.h> | 
|  | 32 | #include <base/time/time.h> | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 33 | #include <expat.h> | 
| Alex Deymo | a259179 | 2015-11-17 00:39:40 -0300 | [diff] [blame] | 34 | #include <metrics/metrics_library.h> | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 35 |  | 
| Alex Deymo | 39910dc | 2015-11-09 17:04:30 -0800 | [diff] [blame] | 36 | #include "update_engine/common/action_pipe.h" | 
|  | 37 | #include "update_engine/common/constants.h" | 
|  | 38 | #include "update_engine/common/hardware_interface.h" | 
|  | 39 | #include "update_engine/common/hash_calculator.h" | 
|  | 40 | #include "update_engine/common/platform_constants.h" | 
|  | 41 | #include "update_engine/common/prefs_interface.h" | 
|  | 42 | #include "update_engine/common/utils.h" | 
| Alex Deymo | f6ee016 | 2015-07-31 12:35:22 -0700 | [diff] [blame] | 43 | #include "update_engine/connection_manager.h" | 
| Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 44 | #include "update_engine/metrics.h" | 
|  | 45 | #include "update_engine/metrics_utils.h" | 
| Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 46 | #include "update_engine/omaha_request_params.h" | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 47 | #include "update_engine/p2p_manager.h" | 
| Jay Srinivasan | 55f50c2 | 2013-01-10 19:24:35 -0800 | [diff] [blame] | 48 | #include "update_engine/payload_state_interface.h" | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 49 |  | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 50 | using base::Time; | 
|  | 51 | using base::TimeDelta; | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 52 | using std::map; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 53 | using std::string; | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 54 | using std::vector; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 55 |  | 
|  | 56 | namespace chromeos_update_engine { | 
|  | 57 |  | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 58 | // List of custom pair tags that we interpret in the Omaha Response: | 
|  | 59 | static const char* kTagDeadline = "deadline"; | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 60 | static const char* kTagDisablePayloadBackoff = "DisablePayloadBackoff"; | 
| Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 61 | static const char* kTagVersion = "version"; | 
| Jay Srinivasan | d671e97 | 2013-01-11 17:17:19 -0800 | [diff] [blame] | 62 | // Deprecated: "IsDelta" | 
|  | 63 | static const char* kTagIsDeltaPayload = "IsDeltaPayload"; | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 64 | static const char* kTagMaxFailureCountPerUrl = "MaxFailureCountPerUrl"; | 
|  | 65 | static const char* kTagMaxDaysToScatter = "MaxDaysToScatter"; | 
|  | 66 | // Deprecated: "ManifestSignatureRsa" | 
|  | 67 | // Deprecated: "ManifestSize" | 
|  | 68 | static const char* kTagMetadataSignatureRsa = "MetadataSignatureRsa"; | 
|  | 69 | static const char* kTagMetadataSize = "MetadataSize"; | 
|  | 70 | static const char* kTagMoreInfo = "MoreInfo"; | 
| Don Garrett | 42bd3aa | 2013-04-10 18:14:56 -0700 | [diff] [blame] | 71 | // Deprecated: "NeedsAdmin" | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 72 | static const char* kTagPrompt = "Prompt"; | 
|  | 73 | static const char* kTagSha256 = "sha256"; | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 74 | static const char* kTagDisableP2PForDownloading = "DisableP2PForDownloading"; | 
|  | 75 | static const char* kTagDisableP2PForSharing = "DisableP2PForSharing"; | 
| David Zeuthen | e7f8917 | 2013-10-31 10:21:04 -0700 | [diff] [blame] | 76 | static const char* kTagPublicKeyRsa = "PublicKeyRsa"; | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 77 |  | 
| Alex Deymo | ac41a82 | 2015-09-15 20:52:53 -0700 | [diff] [blame] | 78 | static const char* kOmahaUpdaterVersion = "0.1.0.0"; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 79 |  | 
| Alex Deymo | ac41a82 | 2015-09-15 20:52:53 -0700 | [diff] [blame] | 80 | namespace { | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 81 |  | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 82 | // Returns an XML ping element attribute assignment with attribute | 
|  | 83 | // |name| and value |ping_days| if |ping_days| has a value that needs | 
|  | 84 | // to be sent, or an empty string otherwise. | 
|  | 85 | string GetPingAttribute(const string& name, int ping_days) { | 
| Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 86 | if (ping_days > 0 || ping_days == OmahaRequestAction::kNeverPinged) | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 87 | return base::StringPrintf(" %s=\"%d\"", name.c_str(), ping_days); | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 88 | return ""; | 
|  | 89 | } | 
|  | 90 |  | 
|  | 91 | // Returns an XML ping element if any of the elapsed days need to be | 
|  | 92 | // sent, or an empty string otherwise. | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 93 | string GetPingXml(int ping_active_days, int ping_roll_call_days) { | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 94 | string ping_active = GetPingAttribute("a", ping_active_days); | 
|  | 95 | string ping_roll_call = GetPingAttribute("r", ping_roll_call_days); | 
|  | 96 | if (!ping_active.empty() || !ping_roll_call.empty()) { | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 97 | return base::StringPrintf("        <ping active=\"1\"%s%s></ping>\n", | 
| Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 98 | ping_active.c_str(), | 
|  | 99 | ping_roll_call.c_str()); | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 100 | } | 
|  | 101 | return ""; | 
|  | 102 | } | 
|  | 103 |  | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 104 | // Returns an XML that goes into the body of the <app> element of the Omaha | 
|  | 105 | // request based on the given parameters. | 
|  | 106 | string GetAppBody(const OmahaEvent* event, | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 107 | OmahaRequestParams* params, | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 108 | bool ping_only, | 
| Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 109 | bool include_ping, | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 110 | int ping_active_days, | 
|  | 111 | int ping_roll_call_days, | 
|  | 112 | PrefsInterface* prefs) { | 
|  | 113 | string app_body; | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 114 | if (event == nullptr) { | 
| Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 115 | if (include_ping) | 
|  | 116 | app_body = GetPingXml(ping_active_days, ping_roll_call_days); | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 117 | if (!ping_only) { | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 118 | app_body += base::StringPrintf( | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 119 | "        <updatecheck targetversionprefix=\"%s\"" | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 120 | "></updatecheck>\n", | 
| Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 121 | XmlEncodeWithDefault(params->target_version_prefix(), "").c_str()); | 
| Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 122 |  | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 123 | // If this is the first update check after a reboot following a previous | 
|  | 124 | // update, generate an event containing the previous version number. If | 
|  | 125 | // the previous version preference file doesn't exist the event is still | 
|  | 126 | // generated with a previous version of 0.0.0.0 -- this is relevant for | 
|  | 127 | // older clients or new installs. The previous version event is not sent | 
|  | 128 | // for ping-only requests because they come before the client has | 
| Alex Deymo | 87c0886 | 2015-10-30 21:56:55 -0700 | [diff] [blame] | 129 | // rebooted. The previous version event is also not sent if it was already | 
|  | 130 | // sent for this new version with a previous updatecheck. | 
| Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 131 | string prev_version; | 
|  | 132 | if (!prefs->GetString(kPrefsPreviousVersion, &prev_version)) { | 
|  | 133 | prev_version = "0.0.0.0"; | 
|  | 134 | } | 
| Alex Deymo | 87c0886 | 2015-10-30 21:56:55 -0700 | [diff] [blame] | 135 | // We only store a non-empty previous version value after a successful | 
|  | 136 | // update in the previous boot. After reporting it back to the server, | 
|  | 137 | // we clear the previous version value so it doesn't get reported again. | 
|  | 138 | if (!prev_version.empty()) { | 
|  | 139 | app_body += base::StringPrintf( | 
|  | 140 | "        <event eventtype=\"%d\" eventresult=\"%d\" " | 
|  | 141 | "previousversion=\"%s\"></event>\n", | 
| Alex Deymo | 9fded1e | 2015-11-05 12:31:19 -0800 | [diff] [blame] | 142 | OmahaEvent::kTypeRebootedAfterUpdate, | 
|  | 143 | OmahaEvent::kResultSuccess, | 
| Alex Deymo | 87c0886 | 2015-10-30 21:56:55 -0700 | [diff] [blame] | 144 | XmlEncodeWithDefault(prev_version, "0.0.0.0").c_str()); | 
|  | 145 | LOG_IF(WARNING, !prefs->SetString(kPrefsPreviousVersion, "")) | 
|  | 146 | << "Unable to reset the previous version."; | 
|  | 147 | } | 
| Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 148 | } | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 149 | } else { | 
| Darin Petkov | c91dd6b | 2011-01-10 12:31:34 -0800 | [diff] [blame] | 150 | // The error code is an optional attribute so append it only if the result | 
|  | 151 | // is not success. | 
| Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 152 | string error_code; | 
|  | 153 | if (event->result != OmahaEvent::kResultSuccess) { | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 154 | error_code = base::StringPrintf(" errorcode=\"%d\"", | 
|  | 155 | static_cast<int>(event->error_code)); | 
| Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 156 | } | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 157 | app_body = base::StringPrintf( | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 158 | "        <event eventtype=\"%d\" eventresult=\"%d\"%s></event>\n", | 
| Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 159 | event->type, event->result, error_code.c_str()); | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 160 | } | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 161 |  | 
|  | 162 | return app_body; | 
|  | 163 | } | 
|  | 164 |  | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 165 | // Returns the cohort* argument to include in the <app> tag for the passed | 
|  | 166 | // |arg_name| and |prefs_key|, if any. The return value is suitable to | 
|  | 167 | // concatenate to the list of arguments and includes a space at the end. | 
|  | 168 | string GetCohortArgXml(PrefsInterface* prefs, | 
| Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 169 | const string arg_name, | 
|  | 170 | const string prefs_key) { | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 171 | // There's nothing wrong with not having a given cohort setting, so we check | 
|  | 172 | // existance first to avoid the warning log message. | 
|  | 173 | if (!prefs->Exists(prefs_key)) | 
|  | 174 | return ""; | 
|  | 175 | string cohort_value; | 
|  | 176 | if (!prefs->GetString(prefs_key, &cohort_value) || cohort_value.empty()) | 
|  | 177 | return ""; | 
|  | 178 | // This is a sanity check to avoid sending a huge XML file back to Ohama due | 
|  | 179 | // to a compromised stateful partition making the update check fail in low | 
|  | 180 | // network environments envent after a reboot. | 
|  | 181 | if (cohort_value.size() > 1024) { | 
|  | 182 | LOG(WARNING) << "The omaha cohort setting " << arg_name | 
|  | 183 | << " has a too big value, which must be an error or an " | 
|  | 184 | "attacker trying to inhibit updates."; | 
|  | 185 | return ""; | 
|  | 186 | } | 
|  | 187 |  | 
| Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 188 | string escaped_xml_value; | 
|  | 189 | if (!XmlEncode(cohort_value, &escaped_xml_value)) { | 
|  | 190 | LOG(WARNING) << "The omaha cohort setting " << arg_name | 
|  | 191 | << " is ASCII-7 invalid, ignoring it."; | 
|  | 192 | return ""; | 
|  | 193 | } | 
|  | 194 |  | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 195 | return base::StringPrintf("%s=\"%s\" ", | 
| Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 196 | arg_name.c_str(), escaped_xml_value.c_str()); | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 197 | } | 
|  | 198 |  | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 199 | // Returns an XML that corresponds to the entire <app> node of the Omaha | 
|  | 200 | // request based on the given parameters. | 
|  | 201 | string GetAppXml(const OmahaEvent* event, | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 202 | OmahaRequestParams* params, | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 203 | bool ping_only, | 
| Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 204 | bool include_ping, | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 205 | int ping_active_days, | 
|  | 206 | int ping_roll_call_days, | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 207 | int install_date_in_days, | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 208 | SystemState* system_state) { | 
| Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 209 | string app_body = GetAppBody(event, params, ping_only, include_ping, | 
|  | 210 | ping_active_days, ping_roll_call_days, | 
|  | 211 | system_state->prefs()); | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 212 | string app_versions; | 
|  | 213 |  | 
|  | 214 | // If we are upgrading to a more stable channel and we are allowed to do | 
|  | 215 | // powerwash, then pass 0.0.0.0 as the version. This is needed to get the | 
|  | 216 | // highest-versioned payload on the destination channel. | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 217 | if (params->to_more_stable_channel() && params->is_powerwash_allowed()) { | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 218 | LOG(INFO) << "Passing OS version as 0.0.0.0 as we are set to powerwash " | 
|  | 219 | << "on downgrading to the version in the more stable channel"; | 
|  | 220 | app_versions = "version=\"0.0.0.0\" from_version=\"" + | 
| Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 221 | XmlEncodeWithDefault(params->app_version(), "0.0.0.0") + "\" "; | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 222 | } else { | 
| Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 223 | app_versions = "version=\"" + | 
|  | 224 | XmlEncodeWithDefault(params->app_version(), "0.0.0.0") + "\" "; | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 225 | } | 
|  | 226 |  | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 227 | string download_channel = params->download_channel(); | 
| Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 228 | string app_channels = | 
|  | 229 | "track=\"" + XmlEncodeWithDefault(download_channel, "") + "\" "; | 
|  | 230 | if (params->current_channel() != download_channel) { | 
|  | 231 | app_channels += "from_track=\"" + XmlEncodeWithDefault( | 
|  | 232 | params->current_channel(), "") + "\" "; | 
|  | 233 | } | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 234 |  | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 235 | string delta_okay_str = params->delta_okay() ? "true" : "false"; | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 236 |  | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 237 | // If install_date_days is not set (e.g. its value is -1 ), don't | 
|  | 238 | // include the attribute. | 
|  | 239 | string install_date_in_days_str = ""; | 
|  | 240 | if (install_date_in_days >= 0) { | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 241 | install_date_in_days_str = base::StringPrintf("installdate=\"%d\" ", | 
|  | 242 | install_date_in_days); | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 243 | } | 
|  | 244 |  | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 245 | string app_cohort_args; | 
|  | 246 | app_cohort_args += GetCohortArgXml(system_state->prefs(), | 
|  | 247 | "cohort", kPrefsOmahaCohort); | 
|  | 248 | app_cohort_args += GetCohortArgXml(system_state->prefs(), | 
|  | 249 | "cohorthint", kPrefsOmahaCohortHint); | 
|  | 250 | app_cohort_args += GetCohortArgXml(system_state->prefs(), | 
|  | 251 | "cohortname", kPrefsOmahaCohortName); | 
|  | 252 |  | 
| Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 253 | string app_xml = "    <app " | 
|  | 254 | "appid=\"" + XmlEncodeWithDefault(params->GetAppId(), "") + "\" " + | 
|  | 255 | app_cohort_args + | 
|  | 256 | app_versions + | 
|  | 257 | app_channels + | 
|  | 258 | "lang=\"" + XmlEncodeWithDefault(params->app_lang(), "en-US") + "\" " + | 
|  | 259 | "board=\"" + XmlEncodeWithDefault(params->os_board(), "") + "\" " + | 
|  | 260 | "hardware_class=\"" + XmlEncodeWithDefault(params->hwid(), "") + "\" " + | 
|  | 261 | "delta_okay=\"" + delta_okay_str + "\" " | 
|  | 262 | "fw_version=\"" + XmlEncodeWithDefault(params->fw_version(), "") + "\" " + | 
|  | 263 | "ec_version=\"" + XmlEncodeWithDefault(params->ec_version(), "") + "\" " + | 
|  | 264 | install_date_in_days_str + | 
|  | 265 | ">\n" + | 
|  | 266 | app_body + | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 267 | "    </app>\n"; | 
|  | 268 |  | 
|  | 269 | return app_xml; | 
|  | 270 | } | 
|  | 271 |  | 
|  | 272 | // Returns an XML that corresponds to the entire <os> node of the Omaha | 
|  | 273 | // request based on the given parameters. | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 274 | string GetOsXml(OmahaRequestParams* params) { | 
| Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 275 | string os_xml ="    <os " | 
|  | 276 | "version=\"" + XmlEncodeWithDefault(params->os_version(), "") + "\" " + | 
|  | 277 | "platform=\"" + XmlEncodeWithDefault(params->os_platform(), "") + "\" " + | 
|  | 278 | "sp=\"" + XmlEncodeWithDefault(params->os_sp(), "") + "\">" | 
|  | 279 | "</os>\n"; | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 280 | return os_xml; | 
|  | 281 | } | 
|  | 282 |  | 
|  | 283 | // Returns an XML that corresponds to the entire Omaha request based on the | 
|  | 284 | // given parameters. | 
|  | 285 | string GetRequestXml(const OmahaEvent* event, | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 286 | OmahaRequestParams* params, | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 287 | bool ping_only, | 
| Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 288 | bool include_ping, | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 289 | int ping_active_days, | 
|  | 290 | int ping_roll_call_days, | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 291 | int install_date_in_days, | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 292 | SystemState* system_state) { | 
|  | 293 | string os_xml = GetOsXml(params); | 
| Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 294 | string app_xml = GetAppXml(event, params, ping_only, include_ping, | 
|  | 295 | ping_active_days, ping_roll_call_days, | 
|  | 296 | install_date_in_days, system_state); | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 297 |  | 
| Alex Vakulenko | 75039d7 | 2014-03-25 12:36:28 -0700 | [diff] [blame] | 298 | string install_source = base::StringPrintf("installsource=\"%s\" ", | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 299 | (params->interactive() ? "ondemandupdate" : "scheduler")); | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 300 |  | 
| Alex Deymo | ac41a82 | 2015-09-15 20:52:53 -0700 | [diff] [blame] | 301 | string updater_version = XmlEncodeWithDefault( | 
|  | 302 | base::StringPrintf("%s-%s", | 
|  | 303 | constants::kOmahaUpdaterID, | 
|  | 304 | kOmahaUpdaterVersion), ""); | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 305 | string request_xml = | 
|  | 306 | "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" | 
| Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 307 | "<request protocol=\"3.0\" " + ( | 
| Alex Deymo | ac41a82 | 2015-09-15 20:52:53 -0700 | [diff] [blame] | 308 | "version=\"" + updater_version + "\" " | 
|  | 309 | "updaterversion=\"" + updater_version + "\" " + | 
| Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 310 | install_source + | 
|  | 311 | "ismachine=\"1\">\n") + | 
|  | 312 | os_xml + | 
|  | 313 | app_xml + | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 314 | "</request>\n"; | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 315 |  | 
|  | 316 | return request_xml; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 317 | } | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 318 |  | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 319 | }  // namespace | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 320 |  | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 321 | // Struct used for holding data obtained when parsing the XML. | 
|  | 322 | struct OmahaParserData { | 
| David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 323 | explicit OmahaParserData(XML_Parser _xml_parser) : xml_parser(_xml_parser) {} | 
|  | 324 |  | 
|  | 325 | // Pointer to the expat XML_Parser object. | 
|  | 326 | XML_Parser xml_parser; | 
|  | 327 |  | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 328 | // This is the state of the parser as it's processing the XML. | 
|  | 329 | bool failed = false; | 
| David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 330 | bool entity_decl = false; | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 331 | string current_path; | 
|  | 332 |  | 
|  | 333 | // These are the values extracted from the XML. | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 334 | string app_cohort; | 
|  | 335 | string app_cohorthint; | 
|  | 336 | string app_cohortname; | 
|  | 337 | bool app_cohort_set = false; | 
|  | 338 | bool app_cohorthint_set = false; | 
|  | 339 | bool app_cohortname_set = false; | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 340 | string updatecheck_status; | 
|  | 341 | string updatecheck_poll_interval; | 
|  | 342 | string daystart_elapsed_days; | 
|  | 343 | string daystart_elapsed_seconds; | 
|  | 344 | vector<string> url_codebase; | 
|  | 345 | string package_name; | 
|  | 346 | string package_size; | 
|  | 347 | string manifest_version; | 
|  | 348 | map<string, string> action_postinstall_attrs; | 
|  | 349 | }; | 
|  | 350 |  | 
|  | 351 | namespace { | 
|  | 352 |  | 
|  | 353 | // Callback function invoked by expat. | 
|  | 354 | void ParserHandlerStart(void* user_data, const XML_Char* element, | 
|  | 355 | const XML_Char** attr) { | 
|  | 356 | OmahaParserData* data = reinterpret_cast<OmahaParserData*>(user_data); | 
|  | 357 |  | 
|  | 358 | if (data->failed) | 
|  | 359 | return; | 
|  | 360 |  | 
|  | 361 | data->current_path += string("/") + element; | 
|  | 362 |  | 
|  | 363 | map<string, string> attrs; | 
|  | 364 | if (attr != nullptr) { | 
|  | 365 | for (int n = 0; attr[n] != nullptr && attr[n+1] != nullptr; n += 2) { | 
|  | 366 | string key = attr[n]; | 
|  | 367 | string value = attr[n + 1]; | 
|  | 368 | attrs[key] = value; | 
|  | 369 | } | 
|  | 370 | } | 
|  | 371 |  | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 372 | if (data->current_path == "/response/app") { | 
|  | 373 | if (attrs.find("cohort") != attrs.end()) { | 
|  | 374 | data->app_cohort_set = true; | 
|  | 375 | data->app_cohort = attrs["cohort"]; | 
|  | 376 | } | 
|  | 377 | if (attrs.find("cohorthint") != attrs.end()) { | 
|  | 378 | data->app_cohorthint_set = true; | 
|  | 379 | data->app_cohorthint = attrs["cohorthint"]; | 
|  | 380 | } | 
|  | 381 | if (attrs.find("cohortname") != attrs.end()) { | 
|  | 382 | data->app_cohortname_set = true; | 
|  | 383 | data->app_cohortname = attrs["cohortname"]; | 
|  | 384 | } | 
|  | 385 | } else if (data->current_path == "/response/app/updatecheck") { | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 386 | // There is only supposed to be a single <updatecheck> element. | 
|  | 387 | data->updatecheck_status = attrs["status"]; | 
|  | 388 | data->updatecheck_poll_interval = attrs["PollInterval"]; | 
|  | 389 | } else if (data->current_path == "/response/daystart") { | 
|  | 390 | // Get the install-date. | 
|  | 391 | data->daystart_elapsed_days = attrs["elapsed_days"]; | 
|  | 392 | data->daystart_elapsed_seconds = attrs["elapsed_seconds"]; | 
|  | 393 | } else if (data->current_path == "/response/app/updatecheck/urls/url") { | 
|  | 394 | // Look at all <url> elements. | 
|  | 395 | data->url_codebase.push_back(attrs["codebase"]); | 
|  | 396 | } else if (data->package_name.empty() && data->current_path == | 
|  | 397 | "/response/app/updatecheck/manifest/packages/package") { | 
|  | 398 | // Only look at the first <package>. | 
|  | 399 | data->package_name = attrs["name"]; | 
|  | 400 | data->package_size = attrs["size"]; | 
|  | 401 | } else if (data->current_path == "/response/app/updatecheck/manifest") { | 
|  | 402 | // Get the version. | 
|  | 403 | data->manifest_version = attrs[kTagVersion]; | 
|  | 404 | } else if (data->current_path == | 
|  | 405 | "/response/app/updatecheck/manifest/actions/action") { | 
|  | 406 | // We only care about the postinstall action. | 
|  | 407 | if (attrs["event"] == "postinstall") { | 
|  | 408 | data->action_postinstall_attrs = attrs; | 
|  | 409 | } | 
|  | 410 | } | 
|  | 411 | } | 
|  | 412 |  | 
|  | 413 | // Callback function invoked by expat. | 
|  | 414 | void ParserHandlerEnd(void* user_data, const XML_Char* element) { | 
|  | 415 | OmahaParserData* data = reinterpret_cast<OmahaParserData*>(user_data); | 
|  | 416 | if (data->failed) | 
|  | 417 | return; | 
|  | 418 |  | 
|  | 419 | const string path_suffix = string("/") + element; | 
|  | 420 |  | 
| Alex Vakulenko | a3cf75a | 2016-01-20 07:56:15 -0800 | [diff] [blame] | 421 | if (!base::EndsWith(data->current_path, path_suffix, | 
|  | 422 | base::CompareCase::SENSITIVE)) { | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 423 | LOG(ERROR) << "Unexpected end element '" << element | 
|  | 424 | << "' with current_path='" << data->current_path << "'"; | 
|  | 425 | data->failed = true; | 
|  | 426 | return; | 
|  | 427 | } | 
|  | 428 | data->current_path.resize(data->current_path.size() - path_suffix.size()); | 
|  | 429 | } | 
|  | 430 |  | 
| David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 431 | // Callback function invoked by expat. | 
|  | 432 | // | 
|  | 433 | // This is called for entity declarations. Since Omaha is guaranteed | 
|  | 434 | // to never return any XML with entities our course of action is to | 
|  | 435 | // just stop parsing. This avoids potential resource exhaustion | 
|  | 436 | // problems AKA the "billion laughs". CVE-2013-0340. | 
|  | 437 | void ParserHandlerEntityDecl(void *user_data, | 
|  | 438 | const XML_Char *entity_name, | 
|  | 439 | int is_parameter_entity, | 
|  | 440 | const XML_Char *value, | 
|  | 441 | int value_length, | 
|  | 442 | const XML_Char *base, | 
|  | 443 | const XML_Char *system_id, | 
|  | 444 | const XML_Char *public_id, | 
|  | 445 | const XML_Char *notation_name) { | 
|  | 446 | OmahaParserData* data = reinterpret_cast<OmahaParserData*>(user_data); | 
|  | 447 |  | 
|  | 448 | LOG(ERROR) << "XML entities are not supported. Aborting parsing."; | 
|  | 449 | data->failed = true; | 
|  | 450 | data->entity_decl = true; | 
|  | 451 | XML_StopParser(data->xml_parser, false); | 
|  | 452 | } | 
|  | 453 |  | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 454 | }  // namespace | 
|  | 455 |  | 
| Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 456 | bool XmlEncode(const string& input, string* output) { | 
|  | 457 | if (std::find_if(input.begin(), input.end(), | 
|  | 458 | [](const char c){return c & 0x80;}) != input.end()) { | 
|  | 459 | LOG(WARNING) << "Invalid ASCII-7 string passed to the XML encoder:"; | 
|  | 460 | utils::HexDumpString(input); | 
|  | 461 | return false; | 
|  | 462 | } | 
| Alex Deymo | cc45785 | 2015-06-18 18:35:50 -0700 | [diff] [blame] | 463 | output->clear(); | 
|  | 464 | // We need at least input.size() space in the output, but the code below will | 
|  | 465 | // handle it if we need more. | 
|  | 466 | output->reserve(input.size()); | 
|  | 467 | for (char c : input) { | 
|  | 468 | switch (c) { | 
|  | 469 | case '\"': | 
|  | 470 | output->append("""); | 
|  | 471 | break; | 
|  | 472 | case '\'': | 
|  | 473 | output->append("'"); | 
|  | 474 | break; | 
|  | 475 | case '&': | 
|  | 476 | output->append("&"); | 
|  | 477 | break; | 
|  | 478 | case '<': | 
|  | 479 | output->append("<"); | 
|  | 480 | break; | 
|  | 481 | case '>': | 
|  | 482 | output->append(">"); | 
|  | 483 | break; | 
|  | 484 | default: | 
|  | 485 | output->push_back(c); | 
|  | 486 | } | 
|  | 487 | } | 
| Alex Deymo | b0d74eb | 2015-03-30 17:59:17 -0700 | [diff] [blame] | 488 | return true; | 
|  | 489 | } | 
|  | 490 |  | 
|  | 491 | string XmlEncodeWithDefault(const string& input, const string& default_value) { | 
|  | 492 | string output; | 
|  | 493 | if (XmlEncode(input, &output)) | 
|  | 494 | return output; | 
|  | 495 | return default_value; | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 496 | } | 
|  | 497 |  | 
| Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 498 | OmahaRequestAction::OmahaRequestAction( | 
|  | 499 | SystemState* system_state, | 
|  | 500 | OmahaEvent* event, | 
|  | 501 | std::unique_ptr<HttpFetcher> http_fetcher, | 
|  | 502 | bool ping_only) | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 503 | : system_state_(system_state), | 
| Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 504 | event_(event), | 
| Alex Deymo | c1c17b4 | 2015-11-23 03:53:15 -0300 | [diff] [blame] | 505 | http_fetcher_(std::move(http_fetcher)), | 
| Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 506 | ping_only_(ping_only), | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 507 | ping_active_days_(0), | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 508 | ping_roll_call_days_(0) { | 
|  | 509 | params_ = system_state->request_params(); | 
|  | 510 | } | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 511 |  | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 512 | OmahaRequestAction::~OmahaRequestAction() {} | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 513 |  | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 514 | // Calculates the value to use for the ping days parameter. | 
|  | 515 | int OmahaRequestAction::CalculatePingDays(const string& key) { | 
|  | 516 | int days = kNeverPinged; | 
|  | 517 | int64_t last_ping = 0; | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 518 | if (system_state_->prefs()->GetInt64(key, &last_ping) && last_ping >= 0) { | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 519 | days = (Time::Now() - Time::FromInternalValue(last_ping)).InDays(); | 
|  | 520 | if (days < 0) { | 
|  | 521 | // If |days| is negative, then the system clock must have jumped | 
|  | 522 | // back in time since the ping was sent. Mark the value so that | 
|  | 523 | // it doesn't get sent to the server but we still update the | 
|  | 524 | // last ping daystart preference. This way the next ping time | 
|  | 525 | // will be correct, hopefully. | 
|  | 526 | days = kPingTimeJump; | 
|  | 527 | LOG(WARNING) << | 
|  | 528 | "System clock jumped back in time. Resetting ping daystarts."; | 
|  | 529 | } | 
|  | 530 | } | 
|  | 531 | return days; | 
|  | 532 | } | 
|  | 533 |  | 
|  | 534 | void OmahaRequestAction::InitPingDays() { | 
|  | 535 | // We send pings only along with update checks, not with events. | 
|  | 536 | if (IsEvent()) { | 
|  | 537 | return; | 
|  | 538 | } | 
|  | 539 | // TODO(petkov): Figure a way to distinguish active use pings | 
|  | 540 | // vs. roll call pings. Currently, the two pings are identical. A | 
| Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 541 | // fix needs to change this code as well as UpdateLastPingDays and ShouldPing. | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 542 | ping_active_days_ = CalculatePingDays(kPrefsLastActivePingDay); | 
|  | 543 | ping_roll_call_days_ = CalculatePingDays(kPrefsLastRollCallPingDay); | 
|  | 544 | } | 
|  | 545 |  | 
| Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 546 | bool OmahaRequestAction::ShouldPing() const { | 
|  | 547 | if (ping_active_days_ == OmahaRequestAction::kNeverPinged && | 
|  | 548 | ping_roll_call_days_ == OmahaRequestAction::kNeverPinged) { | 
|  | 549 | int powerwash_count = system_state_->hardware()->GetPowerwashCount(); | 
|  | 550 | if (powerwash_count > 0) { | 
|  | 551 | LOG(INFO) << "Not sending ping with a=-1 r=-1 to omaha because " | 
|  | 552 | << "powerwash_count is " << powerwash_count; | 
|  | 553 | return false; | 
|  | 554 | } | 
|  | 555 | return true; | 
|  | 556 | } | 
|  | 557 | return ping_active_days_ > 0 || ping_roll_call_days_ > 0; | 
|  | 558 | } | 
|  | 559 |  | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 560 | // static | 
|  | 561 | int OmahaRequestAction::GetInstallDate(SystemState* system_state) { | 
|  | 562 | PrefsInterface* prefs = system_state->prefs(); | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 563 | if (prefs == nullptr) | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 564 | return -1; | 
|  | 565 |  | 
|  | 566 | // If we have the value stored on disk, just return it. | 
|  | 567 | int64_t stored_value; | 
|  | 568 | if (prefs->GetInt64(kPrefsInstallDateDays, &stored_value)) { | 
|  | 569 | // Convert and sanity-check. | 
|  | 570 | int install_date_days = static_cast<int>(stored_value); | 
|  | 571 | if (install_date_days >= 0) | 
|  | 572 | return install_date_days; | 
|  | 573 | LOG(ERROR) << "Dropping stored Omaha InstallData since its value num_days=" | 
|  | 574 | << install_date_days << " looks suspicious."; | 
|  | 575 | prefs->Delete(kPrefsInstallDateDays); | 
|  | 576 | } | 
|  | 577 |  | 
|  | 578 | // Otherwise, if OOBE is not complete then do nothing and wait for | 
|  | 579 | // ParseResponse() to call ParseInstallDate() and then | 
|  | 580 | // PersistInstallDate() to set the kPrefsInstallDateDays state | 
|  | 581 | // variable. Once that is done, we'll then report back in future | 
|  | 582 | // Omaha requests.  This works exactly because OOBE triggers an | 
|  | 583 | // update check. | 
|  | 584 | // | 
|  | 585 | // However, if OOBE is complete and the kPrefsInstallDateDays state | 
|  | 586 | // variable is not set, there are two possibilities | 
|  | 587 | // | 
|  | 588 | //   1. The update check in OOBE failed so we never got a response | 
|  | 589 | //      from Omaha (no network etc.); or | 
|  | 590 | // | 
|  | 591 | //   2. OOBE was done on an older version that didn't write to the | 
|  | 592 | //      kPrefsInstallDateDays state variable. | 
|  | 593 | // | 
|  | 594 | // In both cases, we approximate the install date by simply | 
|  | 595 | // inspecting the timestamp of when OOBE happened. | 
|  | 596 |  | 
|  | 597 | Time time_of_oobe; | 
| Alex Deymo | bccbc38 | 2014-04-03 13:38:55 -0700 | [diff] [blame] | 598 | if (!system_state->hardware()->IsOOBEComplete(&time_of_oobe)) { | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 599 | LOG(INFO) << "Not generating Omaha InstallData as we have " | 
|  | 600 | << "no prefs file and OOBE is not complete."; | 
|  | 601 | return -1; | 
|  | 602 | } | 
|  | 603 |  | 
|  | 604 | int num_days; | 
|  | 605 | if (!utils::ConvertToOmahaInstallDate(time_of_oobe, &num_days)) { | 
|  | 606 | LOG(ERROR) << "Not generating Omaha InstallData from time of OOBE " | 
|  | 607 | << "as its value '" << utils::ToString(time_of_oobe) | 
|  | 608 | << "' looks suspicious."; | 
|  | 609 | return -1; | 
|  | 610 | } | 
|  | 611 |  | 
|  | 612 | // Persist this to disk, for future use. | 
|  | 613 | if (!OmahaRequestAction::PersistInstallDate(system_state, | 
|  | 614 | num_days, | 
|  | 615 | kProvisionedFromOOBEMarker)) | 
|  | 616 | return -1; | 
|  | 617 |  | 
|  | 618 | LOG(INFO) << "Set the Omaha InstallDate from OOBE time-stamp to " | 
|  | 619 | << num_days << " days"; | 
|  | 620 |  | 
|  | 621 | return num_days; | 
|  | 622 | } | 
|  | 623 |  | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 624 | void OmahaRequestAction::PerformAction() { | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 625 | http_fetcher_->set_delegate(this); | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 626 | InitPingDays(); | 
| Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 627 | if (ping_only_ && !ShouldPing()) { | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 628 | processor_->ActionComplete(this, ErrorCode::kSuccess); | 
| Thieu Le | b44e9e8 | 2011-06-06 14:34:04 -0700 | [diff] [blame] | 629 | return; | 
|  | 630 | } | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 631 |  | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 632 | string request_post(GetRequestXml(event_.get(), | 
| Jay Srinivasan | 1c0fe79 | 2013-03-28 16:45:25 -0700 | [diff] [blame] | 633 | params_, | 
| Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 634 | ping_only_, | 
| Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 635 | ShouldPing(),  // include_ping | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 636 | ping_active_days_, | 
| Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 637 | ping_roll_call_days_, | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 638 | GetInstallDate(system_state_), | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 639 | system_state_)); | 
| Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 640 |  | 
| Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 641 | http_fetcher_->SetPostData(request_post.data(), request_post.size(), | 
|  | 642 | kHttpContentTypeTextXml); | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 643 | LOG(INFO) << "Posting an Omaha request to " << params_->update_url(); | 
| Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 644 | LOG(INFO) << "Request: " << request_post; | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 645 | http_fetcher_->BeginTransfer(params_->update_url()); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 646 | } | 
|  | 647 |  | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 648 | void OmahaRequestAction::TerminateProcessing() { | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 649 | http_fetcher_->TerminateTransfer(); | 
|  | 650 | } | 
|  | 651 |  | 
|  | 652 | // We just store the response in the buffer. Once we've received all bytes, | 
|  | 653 | // we'll look in the buffer and decide what to do. | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 654 | void OmahaRequestAction::ReceivedBytes(HttpFetcher *fetcher, | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 655 | const void* bytes, | 
|  | 656 | size_t length) { | 
|  | 657 | const uint8_t* byte_ptr = reinterpret_cast<const uint8_t*>(bytes); | 
|  | 658 | response_buffer_.insert(response_buffer_.end(), byte_ptr, byte_ptr + length); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 659 | } | 
|  | 660 |  | 
|  | 661 | namespace { | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 662 |  | 
|  | 663 | // Parses a 64 bit base-10 int from a string and returns it. Returns 0 | 
|  | 664 | // on error. If the string contains "0", that's indistinguishable from | 
|  | 665 | // error. | 
|  | 666 | off_t ParseInt(const string& str) { | 
|  | 667 | off_t ret = 0; | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 668 | int rc = sscanf(str.c_str(), "%" PRIi64, &ret);  // NOLINT(runtime/printf) | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 669 | if (rc < 1) { | 
|  | 670 | // failure | 
|  | 671 | return 0; | 
|  | 672 | } | 
|  | 673 | return ret; | 
|  | 674 | } | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 675 |  | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 676 | // Parses |str| and returns |true| if, and only if, its value is "true". | 
|  | 677 | bool ParseBool(const string& str) { | 
|  | 678 | return str == "true"; | 
|  | 679 | } | 
|  | 680 |  | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 681 | // Update the last ping day preferences based on the server daystart | 
|  | 682 | // response. Returns true on success, false otherwise. | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 683 | bool UpdateLastPingDays(OmahaParserData *parser_data, PrefsInterface* prefs) { | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 684 | int64_t elapsed_seconds = 0; | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 685 | TEST_AND_RETURN_FALSE( | 
|  | 686 | base::StringToInt64(parser_data->daystart_elapsed_seconds, | 
|  | 687 | &elapsed_seconds)); | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 688 | TEST_AND_RETURN_FALSE(elapsed_seconds >= 0); | 
|  | 689 |  | 
|  | 690 | // Remember the local time that matches the server's last midnight | 
|  | 691 | // time. | 
|  | 692 | Time daystart = Time::Now() - TimeDelta::FromSeconds(elapsed_seconds); | 
|  | 693 | prefs->SetInt64(kPrefsLastActivePingDay, daystart.ToInternalValue()); | 
|  | 694 | prefs->SetInt64(kPrefsLastRollCallPingDay, daystart.ToInternalValue()); | 
|  | 695 | return true; | 
|  | 696 | } | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 697 | }  // namespace | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 698 |  | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 699 | bool OmahaRequestAction::ParseResponse(OmahaParserData* parser_data, | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 700 | OmahaResponse* output_object, | 
|  | 701 | ScopedActionCompleter* completer) { | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 702 | if (parser_data->updatecheck_status.empty()) { | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 703 | completer->set_code(ErrorCode::kOmahaResponseInvalid); | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 704 | return false; | 
|  | 705 | } | 
|  | 706 |  | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 707 | // chromium-os:37289: The PollInterval is not supported by Omaha server | 
|  | 708 | // currently.  But still keeping this existing code in case we ever decide to | 
| Gilad Arnold | ec7f916 | 2014-07-15 13:24:46 -0700 | [diff] [blame] | 709 | // slow down the request rate from the server-side. Note that the PollInterval | 
|  | 710 | // is not persisted, so it has to be sent by the server on every response to | 
|  | 711 | // guarantee that the scheduler uses this value (otherwise, if the device got | 
|  | 712 | // rebooted after the last server-indicated value, it'll revert to the default | 
|  | 713 | // value). Also kDefaultMaxUpdateChecks value for the scattering logic is | 
|  | 714 | // based on the assumption that we perform an update check every hour so that | 
|  | 715 | // the max value of 8 will roughly be equivalent to one work day. If we decide | 
|  | 716 | // to use PollInterval permanently, we should update the | 
|  | 717 | // max_update_checks_allowed to take PollInterval into account.  Note: The | 
|  | 718 | // parsing for PollInterval happens even before parsing of the status because | 
|  | 719 | // we may want to specify the PollInterval even when there's no update. | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 720 | base::StringToInt(parser_data->updatecheck_poll_interval, | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 721 | &output_object->poll_interval); | 
|  | 722 |  | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 723 | // Check for the "elapsed_days" attribute in the "daystart" | 
|  | 724 | // element. This is the number of days since Jan 1 2007, 0:00 | 
|  | 725 | // PST. If we don't have a persisted value of the Omaha InstallDate, | 
|  | 726 | // we'll use it to calculate it and then persist it. | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 727 | if (ParseInstallDate(parser_data, output_object) && | 
|  | 728 | !HasInstallDate(system_state_)) { | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 729 | // Since output_object->install_date_days is never negative, the | 
|  | 730 | // elapsed_days -> install-date calculation is reduced to simply | 
|  | 731 | // rounding down to the nearest number divisible by 7. | 
|  | 732 | int remainder = output_object->install_date_days % 7; | 
|  | 733 | int install_date_days_rounded = | 
|  | 734 | output_object->install_date_days - remainder; | 
|  | 735 | if (PersistInstallDate(system_state_, | 
|  | 736 | install_date_days_rounded, | 
|  | 737 | kProvisionedFromOmahaResponse)) { | 
|  | 738 | LOG(INFO) << "Set the Omaha InstallDate from Omaha Response to " | 
|  | 739 | << install_date_days_rounded << " days"; | 
|  | 740 | } | 
|  | 741 | } | 
|  | 742 |  | 
| Alex Deymo | 00d79ac | 2015-06-29 15:41:49 -0700 | [diff] [blame] | 743 | // We persist the cohorts sent by omaha even if the status is "noupdate". | 
|  | 744 | if (parser_data->app_cohort_set) | 
|  | 745 | PersistCohortData(kPrefsOmahaCohort, parser_data->app_cohort); | 
|  | 746 | if (parser_data->app_cohorthint_set) | 
|  | 747 | PersistCohortData(kPrefsOmahaCohortHint, parser_data->app_cohorthint); | 
|  | 748 | if (parser_data->app_cohortname_set) | 
|  | 749 | PersistCohortData(kPrefsOmahaCohortName, parser_data->app_cohortname); | 
|  | 750 |  | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 751 | if (!ParseStatus(parser_data, output_object, completer)) | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 752 | return false; | 
|  | 753 |  | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 754 | // Note: ParseUrls MUST be called before ParsePackage as ParsePackage | 
|  | 755 | // appends the package name to the URLs populated in this method. | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 756 | if (!ParseUrls(parser_data, output_object, completer)) | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 757 | return false; | 
|  | 758 |  | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 759 | if (!ParsePackage(parser_data, output_object, completer)) | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 760 | return false; | 
|  | 761 |  | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 762 | if (!ParseParams(parser_data, output_object, completer)) | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 763 | return false; | 
|  | 764 |  | 
|  | 765 | return true; | 
|  | 766 | } | 
|  | 767 |  | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 768 | bool OmahaRequestAction::ParseStatus(OmahaParserData* parser_data, | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 769 | OmahaResponse* output_object, | 
|  | 770 | ScopedActionCompleter* completer) { | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 771 | const string& status = parser_data->updatecheck_status; | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 772 | if (status == "noupdate") { | 
|  | 773 | LOG(INFO) << "No update."; | 
|  | 774 | output_object->update_exists = false; | 
|  | 775 | SetOutputObject(*output_object); | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 776 | completer->set_code(ErrorCode::kSuccess); | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 777 | return false; | 
|  | 778 | } | 
|  | 779 |  | 
|  | 780 | if (status != "ok") { | 
|  | 781 | LOG(ERROR) << "Unknown Omaha response status: " << status; | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 782 | completer->set_code(ErrorCode::kOmahaResponseInvalid); | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 783 | return false; | 
|  | 784 | } | 
|  | 785 |  | 
|  | 786 | return true; | 
|  | 787 | } | 
|  | 788 |  | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 789 | bool OmahaRequestAction::ParseUrls(OmahaParserData* parser_data, | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 790 | OmahaResponse* output_object, | 
|  | 791 | ScopedActionCompleter* completer) { | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 792 | if (parser_data->url_codebase.empty()) { | 
|  | 793 | LOG(ERROR) << "No Omaha Response URLs"; | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 794 | completer->set_code(ErrorCode::kOmahaResponseInvalid); | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 795 | return false; | 
|  | 796 | } | 
|  | 797 |  | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 798 | LOG(INFO) << "Found " << parser_data->url_codebase.size() << " url(s)"; | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 799 | output_object->payload_urls.clear(); | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 800 | for (const auto& codebase : parser_data->url_codebase) { | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 801 | if (codebase.empty()) { | 
|  | 802 | LOG(ERROR) << "Omaha Response URL has empty codebase"; | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 803 | completer->set_code(ErrorCode::kOmahaResponseInvalid); | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 804 | return false; | 
|  | 805 | } | 
|  | 806 | output_object->payload_urls.push_back(codebase); | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 807 | } | 
|  | 808 |  | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 809 | return true; | 
|  | 810 | } | 
|  | 811 |  | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 812 | bool OmahaRequestAction::ParsePackage(OmahaParserData* parser_data, | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 813 | OmahaResponse* output_object, | 
|  | 814 | ScopedActionCompleter* completer) { | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 815 | if (parser_data->package_name.empty()) { | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 816 | LOG(ERROR) << "Omaha Response has empty package name"; | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 817 | completer->set_code(ErrorCode::kOmahaResponseInvalid); | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 818 | return false; | 
|  | 819 | } | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 820 |  | 
|  | 821 | // Append the package name to each URL in our list so that we don't | 
|  | 822 | // propagate the urlBase vs packageName distinctions beyond this point. | 
|  | 823 | // From now on, we only need to use payload_urls. | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 824 | for (auto& payload_url : output_object->payload_urls) | 
|  | 825 | payload_url += parser_data->package_name; | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 826 |  | 
|  | 827 | // Parse the payload size. | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 828 | off_t size = ParseInt(parser_data->package_size); | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 829 | if (size <= 0) { | 
|  | 830 | LOG(ERROR) << "Omaha Response has invalid payload size: " << size; | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 831 | completer->set_code(ErrorCode::kOmahaResponseInvalid); | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 832 | return false; | 
|  | 833 | } | 
|  | 834 | output_object->size = size; | 
|  | 835 |  | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 836 | LOG(INFO) << "Payload size = " << output_object->size << " bytes"; | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 837 |  | 
|  | 838 | return true; | 
|  | 839 | } | 
|  | 840 |  | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 841 | bool OmahaRequestAction::ParseParams(OmahaParserData* parser_data, | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 842 | OmahaResponse* output_object, | 
|  | 843 | ScopedActionCompleter* completer) { | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 844 | output_object->version = parser_data->manifest_version; | 
| Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 845 | if (output_object->version.empty()) { | 
| Chris Sosa | aa18e16 | 2013-06-20 13:20:30 -0700 | [diff] [blame] | 846 | LOG(ERROR) << "Omaha Response does not have version in manifest!"; | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 847 | completer->set_code(ErrorCode::kOmahaResponseInvalid); | 
| Chris Sosa | 3b74843 | 2013-06-20 16:42:59 -0700 | [diff] [blame] | 848 | return false; | 
|  | 849 | } | 
|  | 850 |  | 
|  | 851 | LOG(INFO) << "Received omaha response to update to version " | 
|  | 852 | << output_object->version; | 
|  | 853 |  | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 854 | map<string, string> attrs = parser_data->action_postinstall_attrs; | 
|  | 855 | if (attrs.empty()) { | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 856 | LOG(ERROR) << "Omaha Response has no postinstall event action"; | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 857 | completer->set_code(ErrorCode::kOmahaResponseInvalid); | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 858 | return false; | 
|  | 859 | } | 
|  | 860 |  | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 861 | output_object->hash = attrs[kTagSha256]; | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 862 | if (output_object->hash.empty()) { | 
|  | 863 | LOG(ERROR) << "Omaha Response has empty sha256 value"; | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 864 | completer->set_code(ErrorCode::kOmahaResponseInvalid); | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 865 | return false; | 
|  | 866 | } | 
|  | 867 |  | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 868 | // Get the optional properties one by one. | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 869 | output_object->more_info_url = attrs[kTagMoreInfo]; | 
|  | 870 | output_object->metadata_size = ParseInt(attrs[kTagMetadataSize]); | 
|  | 871 | output_object->metadata_signature = attrs[kTagMetadataSignatureRsa]; | 
|  | 872 | output_object->prompt = ParseBool(attrs[kTagPrompt]); | 
|  | 873 | output_object->deadline = attrs[kTagDeadline]; | 
|  | 874 | output_object->max_days_to_scatter = ParseInt(attrs[kTagMaxDaysToScatter]); | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 875 | output_object->disable_p2p_for_downloading = | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 876 | ParseBool(attrs[kTagDisableP2PForDownloading]); | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 877 | output_object->disable_p2p_for_sharing = | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 878 | ParseBool(attrs[kTagDisableP2PForSharing]); | 
|  | 879 | output_object->public_key_rsa = attrs[kTagPublicKeyRsa]; | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 880 |  | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 881 | string max = attrs[kTagMaxFailureCountPerUrl]; | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 882 | if (!base::StringToUint(max, &output_object->max_failure_count_per_url)) | 
| Jay Srinivasan | 2b5a0f0 | 2012-12-19 17:25:56 -0800 | [diff] [blame] | 883 | output_object->max_failure_count_per_url = kDefaultMaxFailureCountPerUrl; | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 884 |  | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 885 | output_object->is_delta_payload = ParseBool(attrs[kTagIsDeltaPayload]); | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 886 |  | 
|  | 887 | output_object->disable_payload_backoff = | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 888 | ParseBool(attrs[kTagDisablePayloadBackoff]); | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 889 |  | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 890 | return true; | 
|  | 891 | } | 
|  | 892 |  | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 893 | // If the transfer was successful, this uses expat to parse the response | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 894 | // and fill in the appropriate fields of the output object. Also, notifies | 
|  | 895 | // the processor that we're done. | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 896 | void OmahaRequestAction::TransferComplete(HttpFetcher *fetcher, | 
|  | 897 | bool successful) { | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 898 | ScopedActionCompleter completer(processor_, this); | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 899 | string current_response(response_buffer_.begin(), response_buffer_.end()); | 
|  | 900 | LOG(INFO) << "Omaha request response: " << current_response; | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 901 |  | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 902 | PayloadStateInterface* const payload_state = system_state_->payload_state(); | 
|  | 903 |  | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 904 | // Events are best effort transactions -- assume they always succeed. | 
|  | 905 | if (IsEvent()) { | 
|  | 906 | CHECK(!HasOutputPipe()) << "No output pipe allowed for event requests."; | 
| Andrew de los Reyes | 2008e4c | 2011-01-12 10:17:52 -0800 | [diff] [blame] | 907 | if (event_->result == OmahaEvent::kResultError && successful && | 
| J. Richard Barnette | 056b0ab | 2013-10-29 15:24:56 -0700 | [diff] [blame] | 908 | system_state_->hardware()->IsOfficialBuild()) { | 
| Andrew de los Reyes | 2008e4c | 2011-01-12 10:17:52 -0800 | [diff] [blame] | 909 | LOG(INFO) << "Signalling Crash Reporter."; | 
|  | 910 | utils::ScheduleCrashReporterUpload(); | 
|  | 911 | } | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 912 | completer.set_code(ErrorCode::kSuccess); | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 913 | return; | 
|  | 914 | } | 
|  | 915 |  | 
| Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 916 | if (!successful) { | 
| Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 917 | LOG(ERROR) << "Omaha request network transfer failed."; | 
| Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 918 | int code = GetHTTPResponseCode(); | 
|  | 919 | // Makes sure we send sane error values. | 
|  | 920 | if (code < 0 || code >= 1000) { | 
|  | 921 | code = 999; | 
|  | 922 | } | 
| David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 923 | completer.set_code(static_cast<ErrorCode>( | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 924 | static_cast<int>(ErrorCode::kOmahaRequestHTTPResponseBase) + code)); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 925 | return; | 
| Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 926 | } | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 927 |  | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 928 | XML_Parser parser = XML_ParserCreate(nullptr); | 
| David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 929 | OmahaParserData parser_data(parser); | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 930 | XML_SetUserData(parser, &parser_data); | 
|  | 931 | XML_SetElementHandler(parser, ParserHandlerStart, ParserHandlerEnd); | 
| David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 932 | XML_SetEntityDeclHandler(parser, ParserHandlerEntityDecl); | 
| Alex Vakulenko | f68bbbc | 2015-02-09 12:53:18 -0800 | [diff] [blame] | 933 | XML_Status res = XML_Parse( | 
|  | 934 | parser, | 
|  | 935 | reinterpret_cast<const char*>(response_buffer_.data()), | 
|  | 936 | response_buffer_.size(), | 
|  | 937 | XML_TRUE); | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 938 | XML_ParserFree(parser); | 
|  | 939 |  | 
|  | 940 | if (res != XML_STATUS_OK || parser_data.failed) { | 
| Alex Deymo | a9bb7dc | 2015-06-19 09:50:23 -0700 | [diff] [blame] | 941 | LOG(ERROR) << "Omaha response not valid XML: " | 
|  | 942 | << XML_ErrorString(XML_GetErrorCode(parser)) | 
|  | 943 | << " at line " << XML_GetCurrentLineNumber(parser) | 
|  | 944 | << " col " << XML_GetCurrentColumnNumber(parser); | 
| David Zeuthen | f3e2801 | 2014-08-26 18:23:52 -0400 | [diff] [blame] | 945 | ErrorCode error_code = ErrorCode::kOmahaRequestXMLParseError; | 
|  | 946 | if (response_buffer_.empty()) { | 
|  | 947 | error_code = ErrorCode::kOmahaRequestEmptyResponseError; | 
|  | 948 | } else if (parser_data.entity_decl) { | 
|  | 949 | error_code = ErrorCode::kOmahaRequestXMLHasEntityDecl; | 
|  | 950 | } | 
|  | 951 | completer.set_code(error_code); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 952 | return; | 
|  | 953 | } | 
|  | 954 |  | 
| Alex Deymo | ebbe7ef | 2014-10-30 13:02:49 -0700 | [diff] [blame] | 955 | // Update the last ping day preferences based on the server daystart response | 
|  | 956 | // even if we didn't send a ping. Omaha always includes the daystart in the | 
|  | 957 | // response, but log the error if it didn't. | 
|  | 958 | LOG_IF(ERROR, !UpdateLastPingDays(&parser_data, system_state_->prefs())) | 
|  | 959 | << "Failed to update the last ping day preferences!"; | 
| Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 960 |  | 
| Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 961 | if (!HasOutputPipe()) { | 
|  | 962 | // Just set success to whether or not the http transfer succeeded, | 
|  | 963 | // which must be true at this point in the code. | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 964 | completer.set_code(ErrorCode::kSuccess); | 
| Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 965 | return; | 
|  | 966 | } | 
|  | 967 |  | 
| Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 968 | OmahaResponse output_object; | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 969 | if (!ParseResponse(&parser_data, &output_object, &completer)) | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 970 | return; | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 971 | output_object.update_exists = true; | 
|  | 972 | SetOutputObject(output_object); | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 973 |  | 
| Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 974 | if (ShouldIgnoreUpdate(output_object)) { | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 975 | output_object.update_exists = false; | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 976 | completer.set_code(ErrorCode::kOmahaUpdateIgnoredPerPolicy); | 
| Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 977 | return; | 
|  | 978 | } | 
|  | 979 |  | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 980 | // If Omaha says to disable p2p, respect that | 
|  | 981 | if (output_object.disable_p2p_for_downloading) { | 
|  | 982 | LOG(INFO) << "Forcibly disabling use of p2p for downloading as " | 
|  | 983 | << "requested by Omaha."; | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 984 | payload_state->SetUsingP2PForDownloading(false); | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 985 | } | 
|  | 986 | if (output_object.disable_p2p_for_sharing) { | 
|  | 987 | LOG(INFO) << "Forcibly disabling use of p2p for sharing as " | 
|  | 988 | << "requested by Omaha."; | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 989 | payload_state->SetUsingP2PForSharing(false); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 990 | } | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 991 |  | 
|  | 992 | // Update the payload state with the current response. The payload state | 
|  | 993 | // will automatically reset all stale state if this response is different | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 994 | // from what's stored already. We are updating the payload state as late | 
|  | 995 | // as possible in this method so that if a new release gets pushed and then | 
|  | 996 | // got pulled back due to some issues, we don't want to clear our internal | 
|  | 997 | // state unnecessarily. | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 998 | payload_state->SetResponse(output_object); | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 999 |  | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1000 | // It could be we've already exceeded the deadline for when p2p is | 
|  | 1001 | // allowed or that we've tried too many times with p2p. Check that. | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1002 | if (payload_state->GetUsingP2PForDownloading()) { | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1003 | payload_state->P2PNewAttempt(); | 
|  | 1004 | if (!payload_state->P2PAttemptAllowed()) { | 
|  | 1005 | LOG(INFO) << "Forcibly disabling use of p2p for downloading because " | 
|  | 1006 | << "of previous failures when using p2p."; | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1007 | payload_state->SetUsingP2PForDownloading(false); | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1008 | } | 
|  | 1009 | } | 
|  | 1010 |  | 
|  | 1011 | // From here on, we'll complete stuff in CompleteProcessing() so | 
|  | 1012 | // disable |completer| since we'll create a new one in that | 
|  | 1013 | // function. | 
|  | 1014 | completer.set_should_complete(false); | 
|  | 1015 |  | 
|  | 1016 | // If we're allowed to use p2p for downloading we do not pay | 
|  | 1017 | // attention to wall-clock-based waiting if the URL is indeed | 
|  | 1018 | // available via p2p. Therefore, check if the file is available via | 
|  | 1019 | // p2p before deferring... | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1020 | if (payload_state->GetUsingP2PForDownloading()) { | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1021 | LookupPayloadViaP2P(output_object); | 
|  | 1022 | } else { | 
|  | 1023 | CompleteProcessing(); | 
|  | 1024 | } | 
|  | 1025 | } | 
|  | 1026 |  | 
|  | 1027 | void OmahaRequestAction::CompleteProcessing() { | 
|  | 1028 | ScopedActionCompleter completer(processor_, this); | 
|  | 1029 | OmahaResponse& output_object = const_cast<OmahaResponse&>(GetOutputObject()); | 
|  | 1030 | PayloadStateInterface* payload_state = system_state_->payload_state(); | 
|  | 1031 |  | 
|  | 1032 | if (ShouldDeferDownload(&output_object)) { | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 1033 | output_object.update_exists = false; | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1034 | LOG(INFO) << "Ignoring Omaha updates as updates are deferred by policy."; | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1035 | completer.set_code(ErrorCode::kOmahaUpdateDeferredPerPolicy); | 
| Jay Srinivasan | 0826288 | 2012-12-28 19:29:43 -0800 | [diff] [blame] | 1036 | return; | 
|  | 1037 | } | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1038 |  | 
| Chris Sosa | 20f005c | 2013-09-05 13:53:08 -0700 | [diff] [blame] | 1039 | if (payload_state->ShouldBackoffDownload()) { | 
|  | 1040 | output_object.update_exists = false; | 
|  | 1041 | LOG(INFO) << "Ignoring Omaha updates in order to backoff our retry " | 
|  | 1042 | << "attempts"; | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1043 | completer.set_code(ErrorCode::kOmahaUpdateDeferredForBackoff); | 
| Chris Sosa | 20f005c | 2013-09-05 13:53:08 -0700 | [diff] [blame] | 1044 | return; | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1045 | } | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1046 | completer.set_code(ErrorCode::kSuccess); | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1047 | } | 
|  | 1048 |  | 
|  | 1049 | void OmahaRequestAction::OnLookupPayloadViaP2PCompleted(const string& url) { | 
|  | 1050 | LOG(INFO) << "Lookup complete, p2p-client returned URL '" << url << "'"; | 
|  | 1051 | if (!url.empty()) { | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1052 | system_state_->payload_state()->SetP2PUrl(url); | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1053 | } else { | 
|  | 1054 | LOG(INFO) << "Forcibly disabling use of p2p for downloading " | 
|  | 1055 | << "because no suitable peer could be found."; | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1056 | system_state_->payload_state()->SetUsingP2PForDownloading(false); | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1057 | } | 
|  | 1058 | CompleteProcessing(); | 
|  | 1059 | } | 
|  | 1060 |  | 
|  | 1061 | void OmahaRequestAction::LookupPayloadViaP2P(const OmahaResponse& response) { | 
| David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1062 | // If the device is in the middle of an update, the state variables | 
|  | 1063 | // kPrefsUpdateStateNextDataOffset, kPrefsUpdateStateNextDataLength | 
|  | 1064 | // tracks the offset and length of the operation currently in | 
|  | 1065 | // progress. The offset is based from the end of the manifest which | 
|  | 1066 | // is kPrefsManifestMetadataSize bytes long. | 
|  | 1067 | // | 
|  | 1068 | // To make forward progress and avoid deadlocks, we need to find a | 
|  | 1069 | // peer that has at least the entire operation we're currently | 
|  | 1070 | // working on. Otherwise we may end up in a situation where two | 
|  | 1071 | // devices bounce back and forth downloading from each other, | 
|  | 1072 | // neither making any forward progress until one of them decides to | 
|  | 1073 | // stop using p2p (via kMaxP2PAttempts and kMaxP2PAttemptTimeSeconds | 
|  | 1074 | // safe-guards). See http://crbug.com/297170 for an example) | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1075 | size_t minimum_size = 0; | 
| David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1076 | int64_t manifest_metadata_size = 0; | 
|  | 1077 | int64_t next_data_offset = 0; | 
|  | 1078 | int64_t next_data_length = 0; | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1079 | if (system_state_ && | 
| David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1080 | system_state_->prefs()->GetInt64(kPrefsManifestMetadataSize, | 
|  | 1081 | &manifest_metadata_size) && | 
|  | 1082 | manifest_metadata_size != -1 && | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1083 | system_state_->prefs()->GetInt64(kPrefsUpdateStateNextDataOffset, | 
| David Zeuthen | 41996ad | 2013-09-24 15:43:24 -0700 | [diff] [blame] | 1084 | &next_data_offset) && | 
|  | 1085 | next_data_offset != -1 && | 
|  | 1086 | system_state_->prefs()->GetInt64(kPrefsUpdateStateNextDataLength, | 
|  | 1087 | &next_data_length)) { | 
|  | 1088 | minimum_size = manifest_metadata_size + next_data_offset + next_data_length; | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1089 | } | 
|  | 1090 |  | 
|  | 1091 | string file_id = utils::CalculateP2PFileId(response.hash, response.size); | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1092 | if (system_state_->p2p_manager()) { | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1093 | LOG(INFO) << "Checking if payload is available via p2p, file_id=" | 
|  | 1094 | << file_id << " minimum_size=" << minimum_size; | 
|  | 1095 | system_state_->p2p_manager()->LookupUrlForFile( | 
|  | 1096 | file_id, | 
|  | 1097 | minimum_size, | 
| David Zeuthen | 4cc5ed2 | 2014-01-15 12:35:03 -0800 | [diff] [blame] | 1098 | TimeDelta::FromSeconds(kMaxP2PNetworkWaitTimeSeconds), | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1099 | base::Bind(&OmahaRequestAction::OnLookupPayloadViaP2PCompleted, | 
|  | 1100 | base::Unretained(this))); | 
|  | 1101 | } | 
| rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 1102 | } | 
|  | 1103 |  | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1104 | bool OmahaRequestAction::ShouldDeferDownload(OmahaResponse* output_object) { | 
| Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 1105 | if (params_->interactive()) { | 
|  | 1106 | LOG(INFO) << "Not deferring download because update is interactive."; | 
|  | 1107 | return false; | 
|  | 1108 | } | 
|  | 1109 |  | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1110 | // If we're using p2p to download _and_ we have a p2p URL, we never | 
|  | 1111 | // defer the download. This is because the download will always | 
|  | 1112 | // happen from a peer on the LAN and we've been waiting in line for | 
|  | 1113 | // our turn. | 
| Gilad Arnold | 74b5f55 | 2014-10-07 08:17:16 -0700 | [diff] [blame] | 1114 | const PayloadStateInterface* payload_state = system_state_->payload_state(); | 
|  | 1115 | if (payload_state->GetUsingP2PForDownloading() && | 
|  | 1116 | !payload_state->GetP2PUrl().empty()) { | 
| David Zeuthen | 8f191b2 | 2013-08-06 12:27:50 -0700 | [diff] [blame] | 1117 | LOG(INFO) << "Download not deferred because download " | 
|  | 1118 | << "will happen from a local peer (via p2p)."; | 
|  | 1119 | return false; | 
|  | 1120 | } | 
|  | 1121 |  | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1122 | // We should defer the downloads only if we've first satisfied the | 
|  | 1123 | // wall-clock-based-waiting period and then the update-check-based waiting | 
|  | 1124 | // period, if required. | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1125 | if (!params_->wall_clock_based_wait_enabled()) { | 
| Chris Sosa | 968d057 | 2013-08-23 14:46:02 -0700 | [diff] [blame] | 1126 | LOG(INFO) << "Wall-clock-based waiting period is not enabled," | 
|  | 1127 | << " so no deferring needed."; | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1128 | return false; | 
|  | 1129 | } | 
|  | 1130 |  | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1131 | switch (IsWallClockBasedWaitingSatisfied(output_object)) { | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1132 | case kWallClockWaitNotSatisfied: | 
|  | 1133 | // We haven't even satisfied the first condition, passing the | 
|  | 1134 | // wall-clock-based waiting period, so we should defer the downloads | 
|  | 1135 | // until that happens. | 
|  | 1136 | LOG(INFO) << "wall-clock-based-wait not satisfied."; | 
|  | 1137 | return true; | 
|  | 1138 |  | 
|  | 1139 | case kWallClockWaitDoneButUpdateCheckWaitRequired: | 
|  | 1140 | LOG(INFO) << "wall-clock-based-wait satisfied and " | 
|  | 1141 | << "update-check-based-wait required."; | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1142 | return !IsUpdateCheckCountBasedWaitingSatisfied(); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1143 |  | 
|  | 1144 | case kWallClockWaitDoneAndUpdateCheckWaitNotRequired: | 
|  | 1145 | // Wall-clock-based waiting period is satisfied, and it's determined | 
|  | 1146 | // that we do not need the update-check-based wait. so no need to | 
|  | 1147 | // defer downloads. | 
|  | 1148 | LOG(INFO) << "wall-clock-based-wait satisfied and " | 
|  | 1149 | << "update-check-based-wait is not required."; | 
|  | 1150 | return false; | 
|  | 1151 |  | 
|  | 1152 | default: | 
|  | 1153 | // Returning false for this default case so we err on the | 
|  | 1154 | // side of downloading updates than deferring in case of any bugs. | 
|  | 1155 | NOTREACHED(); | 
|  | 1156 | return false; | 
|  | 1157 | } | 
|  | 1158 | } | 
|  | 1159 |  | 
|  | 1160 | OmahaRequestAction::WallClockWaitResult | 
|  | 1161 | OmahaRequestAction::IsWallClockBasedWaitingSatisfied( | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1162 | OmahaResponse* output_object) { | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1163 | Time update_first_seen_at; | 
| Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 1164 | int64_t update_first_seen_at_int; | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1165 |  | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1166 | if (system_state_->prefs()->Exists(kPrefsUpdateFirstSeenAt)) { | 
|  | 1167 | if (system_state_->prefs()->GetInt64(kPrefsUpdateFirstSeenAt, | 
|  | 1168 | &update_first_seen_at_int)) { | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1169 | // Note: This timestamp could be that of ANY update we saw in the past | 
|  | 1170 | // (not necessarily this particular update we're considering to apply) | 
|  | 1171 | // but never got to apply because of some reason (e.g. stop AU policy, | 
|  | 1172 | // updates being pulled out from Omaha, changes in target version prefix, | 
|  | 1173 | // new update being rolled out, etc.). But for the purposes of scattering | 
|  | 1174 | // it doesn't matter which update the timestamp corresponds to. i.e. | 
|  | 1175 | // the clock starts ticking the first time we see an update and we're | 
|  | 1176 | // ready to apply when the random wait period is satisfied relative to | 
|  | 1177 | // that first seen timestamp. | 
|  | 1178 | update_first_seen_at = Time::FromInternalValue(update_first_seen_at_int); | 
|  | 1179 | LOG(INFO) << "Using persisted value of UpdateFirstSeenAt: " | 
|  | 1180 | << utils::ToString(update_first_seen_at); | 
|  | 1181 | } else { | 
|  | 1182 | // This seems like an unexpected error where the persisted value exists | 
|  | 1183 | // but it's not readable for some reason. Just skip scattering in this | 
|  | 1184 | // case to be safe. | 
|  | 1185 | LOG(INFO) << "Not scattering as UpdateFirstSeenAt value cannot be read"; | 
|  | 1186 | return kWallClockWaitDoneAndUpdateCheckWaitNotRequired; | 
|  | 1187 | } | 
|  | 1188 | } else { | 
|  | 1189 | update_first_seen_at = Time::Now(); | 
|  | 1190 | update_first_seen_at_int = update_first_seen_at.ToInternalValue(); | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1191 | if (system_state_->prefs()->SetInt64(kPrefsUpdateFirstSeenAt, | 
|  | 1192 | update_first_seen_at_int)) { | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1193 | LOG(INFO) << "Persisted the new value for UpdateFirstSeenAt: " | 
|  | 1194 | << utils::ToString(update_first_seen_at); | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1195 | } else { | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1196 | // This seems like an unexpected error where the value cannot be | 
|  | 1197 | // persisted for some reason. Just skip scattering in this | 
|  | 1198 | // case to be safe. | 
|  | 1199 | LOG(INFO) << "Not scattering as UpdateFirstSeenAt value " | 
|  | 1200 | << utils::ToString(update_first_seen_at) | 
|  | 1201 | << " cannot be persisted"; | 
|  | 1202 | return kWallClockWaitDoneAndUpdateCheckWaitNotRequired; | 
|  | 1203 | } | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1204 | } | 
|  | 1205 |  | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1206 | TimeDelta elapsed_time = Time::Now() - update_first_seen_at; | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1207 | TimeDelta max_scatter_period = TimeDelta::FromDays( | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1208 | output_object->max_days_to_scatter); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1209 |  | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1210 | LOG(INFO) << "Waiting Period = " | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1211 | << utils::FormatSecs(params_->waiting_period().InSeconds()) | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1212 | << ", Time Elapsed = " | 
|  | 1213 | << utils::FormatSecs(elapsed_time.InSeconds()) | 
|  | 1214 | << ", MaxDaysToScatter = " | 
|  | 1215 | << max_scatter_period.InDays(); | 
|  | 1216 |  | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1217 | if (!output_object->deadline.empty()) { | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1218 | // The deadline is set for all rules which serve a delta update from a | 
|  | 1219 | // previous FSI, which means this update will be applied mostly in OOBE | 
|  | 1220 | // cases. For these cases, we shouldn't scatter so as to finish the OOBE | 
|  | 1221 | // quickly. | 
|  | 1222 | LOG(INFO) << "Not scattering as deadline flag is set"; | 
|  | 1223 | return kWallClockWaitDoneAndUpdateCheckWaitNotRequired; | 
|  | 1224 | } | 
|  | 1225 |  | 
|  | 1226 | if (max_scatter_period.InDays() == 0) { | 
|  | 1227 | // This means the Omaha rule creator decides that this rule | 
|  | 1228 | // should not be scattered irrespective of the policy. | 
|  | 1229 | LOG(INFO) << "Not scattering as MaxDaysToScatter in rule is 0."; | 
|  | 1230 | return kWallClockWaitDoneAndUpdateCheckWaitNotRequired; | 
|  | 1231 | } | 
|  | 1232 |  | 
|  | 1233 | if (elapsed_time > max_scatter_period) { | 
| Jay Srinivasan | 34b5d86 | 2012-07-23 11:43:22 -0700 | [diff] [blame] | 1234 | // This means we've waited more than the upperbound wait in the rule | 
|  | 1235 | // from the time we first saw a valid update available to us. | 
|  | 1236 | // This will prevent update starvation. | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1237 | LOG(INFO) << "Not scattering as we're past the MaxDaysToScatter limit."; | 
|  | 1238 | return kWallClockWaitDoneAndUpdateCheckWaitNotRequired; | 
|  | 1239 | } | 
|  | 1240 |  | 
|  | 1241 | // This means we are required to participate in scattering. | 
|  | 1242 | // See if our turn has arrived now. | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1243 | TimeDelta remaining_wait_time = params_->waiting_period() - elapsed_time; | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1244 | if (remaining_wait_time.InSeconds() <= 0) { | 
|  | 1245 | // Yes, it's our turn now. | 
|  | 1246 | LOG(INFO) << "Successfully passed the wall-clock-based-wait."; | 
|  | 1247 |  | 
|  | 1248 | // But we can't download until the update-check-count-based wait is also | 
|  | 1249 | // satisfied, so mark it as required now if update checks are enabled. | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1250 | return params_->update_check_count_wait_enabled() ? | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1251 | kWallClockWaitDoneButUpdateCheckWaitRequired : | 
|  | 1252 | kWallClockWaitDoneAndUpdateCheckWaitNotRequired; | 
|  | 1253 | } | 
|  | 1254 |  | 
|  | 1255 | // Not our turn yet, so we have to wait until our turn to | 
|  | 1256 | // help scatter the downloads across all clients of the enterprise. | 
|  | 1257 | LOG(INFO) << "Update deferred for another " | 
|  | 1258 | << utils::FormatSecs(remaining_wait_time.InSeconds()) | 
|  | 1259 | << " per policy."; | 
|  | 1260 | return kWallClockWaitNotSatisfied; | 
|  | 1261 | } | 
|  | 1262 |  | 
| Jay Srinivasan | 23b92a5 | 2012-10-27 02:00:21 -0700 | [diff] [blame] | 1263 | bool OmahaRequestAction::IsUpdateCheckCountBasedWaitingSatisfied() { | 
| Ben Chan | 9abb763 | 2014-08-07 00:10:53 -0700 | [diff] [blame] | 1264 | int64_t update_check_count_value; | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1265 |  | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1266 | if (system_state_->prefs()->Exists(kPrefsUpdateCheckCount)) { | 
|  | 1267 | if (!system_state_->prefs()->GetInt64(kPrefsUpdateCheckCount, | 
|  | 1268 | &update_check_count_value)) { | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1269 | // We are unable to read the update check count from file for some reason. | 
|  | 1270 | // So let's proceed anyway so as to not stall the update. | 
|  | 1271 | LOG(ERROR) << "Unable to read update check count. " | 
|  | 1272 | << "Skipping update-check-count-based-wait."; | 
|  | 1273 | return true; | 
|  | 1274 | } | 
|  | 1275 | } else { | 
|  | 1276 | // This file does not exist. This means we haven't started our update | 
|  | 1277 | // check count down yet, so this is the right time to start the count down. | 
|  | 1278 | update_check_count_value = base::RandInt( | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1279 | params_->min_update_checks_needed(), | 
|  | 1280 | params_->max_update_checks_allowed()); | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1281 |  | 
|  | 1282 | LOG(INFO) << "Randomly picked update check count value = " | 
|  | 1283 | << update_check_count_value; | 
|  | 1284 |  | 
|  | 1285 | // Write out the initial value of update_check_count_value. | 
| Jay Srinivasan | 6f6ea00 | 2012-12-14 11:26:28 -0800 | [diff] [blame] | 1286 | if (!system_state_->prefs()->SetInt64(kPrefsUpdateCheckCount, | 
|  | 1287 | update_check_count_value)) { | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1288 | // We weren't able to write the update check count file for some reason. | 
|  | 1289 | // So let's proceed anyway so as to not stall the update. | 
|  | 1290 | LOG(ERROR) << "Unable to write update check count. " | 
|  | 1291 | << "Skipping update-check-count-based-wait."; | 
|  | 1292 | return true; | 
|  | 1293 | } | 
|  | 1294 | } | 
|  | 1295 |  | 
|  | 1296 | if (update_check_count_value == 0) { | 
|  | 1297 | LOG(INFO) << "Successfully passed the update-check-based-wait."; | 
|  | 1298 | return true; | 
|  | 1299 | } | 
|  | 1300 |  | 
|  | 1301 | if (update_check_count_value < 0 || | 
| Jay Srinivasan | ae4697c | 2013-03-18 17:08:08 -0700 | [diff] [blame] | 1302 | update_check_count_value > params_->max_update_checks_allowed()) { | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1303 | // We err on the side of skipping scattering logic instead of stalling | 
|  | 1304 | // a machine from receiving any updates in case of any unexpected state. | 
|  | 1305 | LOG(ERROR) << "Invalid value for update check count detected. " | 
|  | 1306 | << "Skipping update-check-count-based-wait."; | 
|  | 1307 | return true; | 
|  | 1308 | } | 
|  | 1309 |  | 
|  | 1310 | // Legal value, we need to wait for more update checks to happen | 
|  | 1311 | // until this becomes 0. | 
|  | 1312 | LOG(INFO) << "Deferring Omaha updates for another " | 
|  | 1313 | << update_check_count_value | 
|  | 1314 | << " update checks per policy"; | 
|  | 1315 | return false; | 
|  | 1316 | } | 
|  | 1317 |  | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1318 | // static | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 1319 | bool OmahaRequestAction::ParseInstallDate(OmahaParserData* parser_data, | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1320 | OmahaResponse* output_object) { | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1321 | int64_t elapsed_days = 0; | 
| David Zeuthen | e8ed863 | 2014-07-24 13:38:10 -0400 | [diff] [blame] | 1322 | if (!base::StringToInt64(parser_data->daystart_elapsed_days, | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1323 | &elapsed_days)) | 
|  | 1324 | return false; | 
|  | 1325 |  | 
|  | 1326 | if (elapsed_days < 0) | 
|  | 1327 | return false; | 
|  | 1328 |  | 
|  | 1329 | output_object->install_date_days = elapsed_days; | 
|  | 1330 | return true; | 
|  | 1331 | } | 
|  | 1332 |  | 
|  | 1333 | // static | 
|  | 1334 | bool OmahaRequestAction::HasInstallDate(SystemState *system_state) { | 
|  | 1335 | PrefsInterface* prefs = system_state->prefs(); | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1336 | if (prefs == nullptr) | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1337 | return false; | 
|  | 1338 |  | 
|  | 1339 | return prefs->Exists(kPrefsInstallDateDays); | 
|  | 1340 | } | 
|  | 1341 |  | 
|  | 1342 | // static | 
|  | 1343 | bool OmahaRequestAction::PersistInstallDate( | 
|  | 1344 | SystemState *system_state, | 
|  | 1345 | int install_date_days, | 
|  | 1346 | InstallDateProvisioningSource source) { | 
|  | 1347 | TEST_AND_RETURN_FALSE(install_date_days >= 0); | 
|  | 1348 |  | 
|  | 1349 | PrefsInterface* prefs = system_state->prefs(); | 
| Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 1350 | if (prefs == nullptr) | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1351 | return false; | 
|  | 1352 |  | 
|  | 1353 | if (!prefs->SetInt64(kPrefsInstallDateDays, install_date_days)) | 
|  | 1354 | return false; | 
|  | 1355 |  | 
| Alex Deymo | af9a863 | 2015-09-23 18:51:48 -0700 | [diff] [blame] | 1356 | string metric_name = metrics::kMetricInstallDateProvisioningSource; | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1357 | system_state->metrics_lib()->SendEnumToUMA( | 
|  | 1358 | metric_name, | 
|  | 1359 | static_cast<int>(source),  // Sample. | 
|  | 1360 | kProvisionedMax);          // Maximum. | 
| David Zeuthen | 639aa36 | 2014-02-03 16:23:44 -0800 | [diff] [blame] | 1361 |  | 
|  | 1362 | return true; | 
|  | 1363 | } | 
|  | 1364 |  | 
| Alex Deymo | 8e18f93 | 2015-03-27 16:16:59 -0700 | [diff] [blame] | 1365 | bool OmahaRequestAction::PersistCohortData( | 
|  | 1366 | const string& prefs_key, | 
|  | 1367 | const string& new_value) { | 
|  | 1368 | if (new_value.empty() && system_state_->prefs()->Exists(prefs_key)) { | 
|  | 1369 | LOG(INFO) << "Removing stored " << prefs_key << " value."; | 
|  | 1370 | return system_state_->prefs()->Delete(prefs_key); | 
|  | 1371 | } else if (!new_value.empty()) { | 
|  | 1372 | LOG(INFO) << "Storing new setting " << prefs_key << " as " << new_value; | 
|  | 1373 | return system_state_->prefs()->SetString(prefs_key, new_value); | 
|  | 1374 | } | 
|  | 1375 | return true; | 
|  | 1376 | } | 
|  | 1377 |  | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1378 | void OmahaRequestAction::ActionCompleted(ErrorCode code) { | 
|  | 1379 | // We only want to report this on "update check". | 
|  | 1380 | if (ping_only_ || event_ != nullptr) | 
|  | 1381 | return; | 
|  | 1382 |  | 
|  | 1383 | metrics::CheckResult result = metrics::CheckResult::kUnset; | 
|  | 1384 | metrics::CheckReaction reaction = metrics::CheckReaction::kUnset; | 
|  | 1385 | metrics::DownloadErrorCode download_error_code = | 
|  | 1386 | metrics::DownloadErrorCode::kUnset; | 
|  | 1387 |  | 
|  | 1388 | // Regular update attempt. | 
|  | 1389 | switch (code) { | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1390 | case ErrorCode::kSuccess: | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1391 | // OK, we parsed the response successfully but that does | 
|  | 1392 | // necessarily mean that an update is available. | 
|  | 1393 | if (HasOutputPipe()) { | 
|  | 1394 | const OmahaResponse& response = GetOutputObject(); | 
|  | 1395 | if (response.update_exists) { | 
|  | 1396 | result = metrics::CheckResult::kUpdateAvailable; | 
|  | 1397 | reaction = metrics::CheckReaction::kUpdating; | 
|  | 1398 | } else { | 
|  | 1399 | result = metrics::CheckResult::kNoUpdateAvailable; | 
|  | 1400 | } | 
|  | 1401 | } else { | 
|  | 1402 | result = metrics::CheckResult::kNoUpdateAvailable; | 
|  | 1403 | } | 
|  | 1404 | break; | 
|  | 1405 |  | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1406 | case ErrorCode::kOmahaUpdateIgnoredPerPolicy: | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1407 | result = metrics::CheckResult::kUpdateAvailable; | 
|  | 1408 | reaction = metrics::CheckReaction::kIgnored; | 
|  | 1409 | break; | 
|  | 1410 |  | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1411 | case ErrorCode::kOmahaUpdateDeferredPerPolicy: | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1412 | result = metrics::CheckResult::kUpdateAvailable; | 
|  | 1413 | reaction = metrics::CheckReaction::kDeferring; | 
|  | 1414 | break; | 
|  | 1415 |  | 
| Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 1416 | case ErrorCode::kOmahaUpdateDeferredForBackoff: | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1417 | result = metrics::CheckResult::kUpdateAvailable; | 
|  | 1418 | reaction = metrics::CheckReaction::kBackingOff; | 
|  | 1419 | break; | 
|  | 1420 |  | 
|  | 1421 | default: | 
|  | 1422 | // We report two flavors of errors, "Download errors" and "Parsing | 
|  | 1423 | // error". Try to convert to the former and if that doesn't work | 
|  | 1424 | // we know it's the latter. | 
| Alex Deymo | 38429cf | 2015-11-11 18:27:22 -0800 | [diff] [blame] | 1425 | metrics::DownloadErrorCode tmp_error = | 
|  | 1426 | metrics_utils::GetDownloadErrorCode(code); | 
| David Zeuthen | 33bae49 | 2014-02-25 16:16:18 -0800 | [diff] [blame] | 1427 | if (tmp_error != metrics::DownloadErrorCode::kInputMalformed) { | 
|  | 1428 | result = metrics::CheckResult::kDownloadError; | 
|  | 1429 | download_error_code = tmp_error; | 
|  | 1430 | } else { | 
|  | 1431 | result = metrics::CheckResult::kParsingError; | 
|  | 1432 | } | 
|  | 1433 | break; | 
|  | 1434 | } | 
|  | 1435 |  | 
|  | 1436 | metrics::ReportUpdateCheckMetrics(system_state_, | 
|  | 1437 | result, reaction, download_error_code); | 
|  | 1438 | } | 
|  | 1439 |  | 
| Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1440 | bool OmahaRequestAction::ShouldIgnoreUpdate( | 
|  | 1441 | const OmahaResponse& response) const { | 
| Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1442 | // Note: policy decision to not update to a version we rolled back from. | 
|  | 1443 | string rollback_version = | 
|  | 1444 | system_state_->payload_state()->GetRollbackVersion(); | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1445 | if (!rollback_version.empty()) { | 
| Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1446 | LOG(INFO) << "Detected previous rollback from version " << rollback_version; | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1447 | if (rollback_version == response.version) { | 
| Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1448 | LOG(INFO) << "Received version that we rolled back from. Ignoring."; | 
|  | 1449 | return true; | 
|  | 1450 | } | 
|  | 1451 | } | 
|  | 1452 |  | 
| Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 1453 | if (!IsUpdateAllowedOverCurrentConnection()) { | 
| Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1454 | LOG(INFO) << "Update is not allowed over current connection."; | 
|  | 1455 | return true; | 
|  | 1456 | } | 
|  | 1457 |  | 
|  | 1458 | // Note: We could technically delete the UpdateFirstSeenAt state when we | 
|  | 1459 | // return true. If we do, it'll mean a device has to restart the | 
|  | 1460 | // UpdateFirstSeenAt and thus help scattering take effect when the AU is | 
|  | 1461 | // turned on again. On the other hand, it also increases the chance of update | 
|  | 1462 | // starvation if an admin turns AU on/off more frequently. We choose to err on | 
|  | 1463 | // the side of preventing starvation at the cost of not applying scattering in | 
|  | 1464 | // those cases. | 
|  | 1465 | return false; | 
|  | 1466 | } | 
|  | 1467 |  | 
|  | 1468 | bool OmahaRequestAction::IsUpdateAllowedOverCurrentConnection() const { | 
|  | 1469 | NetworkConnectionType type; | 
|  | 1470 | NetworkTethering tethering; | 
| Alex Deymo | f6ee016 | 2015-07-31 12:35:22 -0700 | [diff] [blame] | 1471 | ConnectionManagerInterface* connection_manager = | 
|  | 1472 | system_state_->connection_manager(); | 
| Alex Deymo | 3053450 | 2015-07-20 15:06:33 -0700 | [diff] [blame] | 1473 | if (!connection_manager->GetConnectionProperties(&type, &tethering)) { | 
| Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1474 | LOG(INFO) << "We could not determine our connection type. " | 
|  | 1475 | << "Defaulting to allow updates."; | 
|  | 1476 | return true; | 
|  | 1477 | } | 
|  | 1478 | bool is_allowed = connection_manager->IsUpdateAllowedOver(type, tethering); | 
|  | 1479 | LOG(INFO) << "We are connected via " | 
| Alex Deymo | f6ee016 | 2015-07-31 12:35:22 -0700 | [diff] [blame] | 1480 | << ConnectionManager::StringForConnectionType(type) | 
| Chris Sosa | 77f79e8 | 2014-06-02 18:16:24 -0700 | [diff] [blame] | 1481 | << ", Updates allowed: " << (is_allowed ? "Yes" : "No"); | 
|  | 1482 | return is_allowed; | 
|  | 1483 | } | 
|  | 1484 |  | 
| Jay Srinivasan | 480ddfa | 2012-06-01 19:15:26 -0700 | [diff] [blame] | 1485 | }  // namespace chromeos_update_engine |