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