Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame^] | 1 | // Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 5 | #include "update_engine/omaha_request_action.h" |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 6 | |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 7 | #include <inttypes.h> |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 8 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 9 | #include <sstream> |
| 10 | |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 11 | #include <base/string_number_conversions.h> |
| 12 | #include <base/string_util.h> |
Mike Frysinger | 8155d08 | 2012-04-06 15:23:18 -0400 | [diff] [blame^] | 13 | #include <base/stringprintf.h> |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 14 | #include <base/time.h> |
| 15 | #include <base/logging.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 | #include <libxml/parser.h> |
| 17 | #include <libxml/xpath.h> |
| 18 | #include <libxml/xpathInternals.h> |
| 19 | |
| 20 | #include "update_engine/action_pipe.h" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 21 | #include "update_engine/omaha_request_params.h" |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 22 | #include "update_engine/prefs_interface.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 23 | #include "update_engine/utils.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 24 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 25 | using base::Time; |
| 26 | using base::TimeDelta; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 27 | using std::string; |
| 28 | |
| 29 | namespace chromeos_update_engine { |
| 30 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 31 | namespace { |
| 32 | |
| 33 | const string kGupdateVersion("ChromeOSUpdateEngine-0.1.0.0"); |
| 34 | |
| 35 | // This is handy for passing strings into libxml2 |
| 36 | #define ConstXMLStr(x) (reinterpret_cast<const xmlChar*>(x)) |
| 37 | |
| 38 | // These are for scoped_ptr_malloc, which is like scoped_ptr, but allows |
| 39 | // a custom free() function to be specified. |
| 40 | class ScopedPtrXmlDocFree { |
| 41 | public: |
| 42 | inline void operator()(void* x) const { |
| 43 | xmlFreeDoc(reinterpret_cast<xmlDoc*>(x)); |
| 44 | } |
| 45 | }; |
| 46 | class ScopedPtrXmlFree { |
| 47 | public: |
| 48 | inline void operator()(void* x) const { |
| 49 | xmlFree(x); |
| 50 | } |
| 51 | }; |
| 52 | class ScopedPtrXmlXPathObjectFree { |
| 53 | public: |
| 54 | inline void operator()(void* x) const { |
| 55 | xmlXPathFreeObject(reinterpret_cast<xmlXPathObject*>(x)); |
| 56 | } |
| 57 | }; |
| 58 | class ScopedPtrXmlXPathContextFree { |
| 59 | public: |
| 60 | inline void operator()(void* x) const { |
| 61 | xmlXPathFreeContext(reinterpret_cast<xmlXPathContext*>(x)); |
| 62 | } |
| 63 | }; |
| 64 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 65 | // Returns true if |ping_days| has a value that needs to be sent, |
| 66 | // false otherwise. |
| 67 | bool ShouldPing(int ping_days) { |
| 68 | return ping_days > 0 || ping_days == OmahaRequestAction::kNeverPinged; |
| 69 | } |
| 70 | |
| 71 | // Returns an XML ping element attribute assignment with attribute |
| 72 | // |name| and value |ping_days| if |ping_days| has a value that needs |
| 73 | // to be sent, or an empty string otherwise. |
| 74 | string GetPingAttribute(const string& name, int ping_days) { |
| 75 | if (ShouldPing(ping_days)) { |
| 76 | return StringPrintf(" %s=\"%d\"", name.c_str(), ping_days); |
| 77 | } |
| 78 | return ""; |
| 79 | } |
| 80 | |
| 81 | // Returns an XML ping element if any of the elapsed days need to be |
| 82 | // sent, or an empty string otherwise. |
| 83 | string GetPingBody(int ping_active_days, int ping_roll_call_days) { |
| 84 | string ping_active = GetPingAttribute("a", ping_active_days); |
| 85 | string ping_roll_call = GetPingAttribute("r", ping_roll_call_days); |
| 86 | if (!ping_active.empty() || !ping_roll_call.empty()) { |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 87 | return StringPrintf(" <o:ping active=\"1\"%s%s></o:ping>\n", |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 88 | ping_active.c_str(), |
| 89 | ping_roll_call.c_str()); |
| 90 | } |
| 91 | return ""; |
| 92 | } |
| 93 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 94 | string FormatRequest(const OmahaEvent* event, |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 95 | const OmahaRequestParams& params, |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 96 | bool ping_only, |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 97 | int ping_active_days, |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 98 | int ping_roll_call_days, |
| 99 | PrefsInterface* prefs) { |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 100 | string body; |
| 101 | if (event == NULL) { |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 102 | body = GetPingBody(ping_active_days, ping_roll_call_days); |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 103 | if (!ping_only) { |
Jay Srinivasan | 56d5aa4 | 2012-03-26 14:27:59 -0700 | [diff] [blame] | 104 | // not passing update_disabled to Omaha because we want to |
| 105 | // get the update and report with UpdateDeferred result so that |
| 106 | // borgmon charts show up updates that are deferred. This is also |
| 107 | // the expected behavior when we move to Omaha v3.0 protocol, so it'll |
| 108 | // be consistent. |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 109 | body += StringPrintf( |
| 110 | " <o:updatecheck" |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 111 | " targetversionprefix=\"%s\"" |
| 112 | "></o:updatecheck>\n", |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 113 | XmlEncode(params.target_version_prefix).c_str()); |
| 114 | |
Darin Petkov | 265f290 | 2011-05-09 15:17:40 -0700 | [diff] [blame] | 115 | // If this is the first update check after a reboot following a previous |
| 116 | // update, generate an event containing the previous version number. If |
| 117 | // the previous version preference file doesn't exist the event is still |
| 118 | // generated with a previous version of 0.0.0.0 -- this is relevant for |
| 119 | // older clients or new installs. The previous version event is not sent |
| 120 | // for ping-only requests because they come before the client has |
| 121 | // rebooted. |
| 122 | string prev_version; |
| 123 | if (!prefs->GetString(kPrefsPreviousVersion, &prev_version)) { |
| 124 | prev_version = "0.0.0.0"; |
| 125 | } |
| 126 | if (!prev_version.empty()) { |
| 127 | body += StringPrintf( |
| 128 | " <o:event eventtype=\"%d\" eventresult=\"%d\" " |
| 129 | "previousversion=\"%s\"></o:event>\n", |
| 130 | OmahaEvent::kTypeUpdateComplete, |
| 131 | OmahaEvent::kResultSuccessReboot, |
| 132 | XmlEncode(prev_version).c_str()); |
| 133 | LOG_IF(WARNING, !prefs->SetString(kPrefsPreviousVersion, "")) |
| 134 | << "Unable to reset the previous version."; |
| 135 | } |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 136 | } |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 137 | } else { |
Darin Petkov | c91dd6b | 2011-01-10 12:31:34 -0800 | [diff] [blame] | 138 | // The error code is an optional attribute so append it only if the result |
| 139 | // is not success. |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 140 | string error_code; |
| 141 | if (event->result != OmahaEvent::kResultSuccess) { |
Darin Petkov | 18c7bce | 2011-06-16 14:07:00 -0700 | [diff] [blame] | 142 | error_code = StringPrintf(" errorcode=\"%d\"", event->error_code); |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 143 | } |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 144 | body = StringPrintf( |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame] | 145 | " <o:event eventtype=\"%d\" eventresult=\"%d\"%s></o:event>\n", |
| 146 | event->type, event->result, error_code.c_str()); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 147 | } |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 148 | return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
| 149 | "<o:gupdate xmlns:o=\"http://www.google.com/update2/request\" " |
| 150 | "version=\"" + XmlEncode(kGupdateVersion) + "\" " |
| 151 | "updaterversion=\"" + XmlEncode(kGupdateVersion) + "\" " |
| 152 | "protocol=\"2.0\" ismachine=\"1\">\n" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 153 | " <o:os version=\"" + XmlEncode(params.os_version) + "\" platform=\"" + |
| 154 | XmlEncode(params.os_platform) + "\" sp=\"" + |
| 155 | XmlEncode(params.os_sp) + "\"></o:os>\n" |
| 156 | " <o:app appid=\"" + XmlEncode(params.app_id) + "\" version=\"" + |
| 157 | XmlEncode(params.app_version) + "\" " |
| 158 | "lang=\"" + XmlEncode(params.app_lang) + "\" track=\"" + |
Andrew de los Reyes | 37c2032 | 2010-06-30 13:27:19 -0700 | [diff] [blame] | 159 | XmlEncode(params.app_track) + "\" board=\"" + |
Darin Petkov | fbb4009 | 2010-07-29 17:05:50 -0700 | [diff] [blame] | 160 | XmlEncode(params.os_board) + "\" hardware_class=\"" + |
| 161 | XmlEncode(params.hardware_class) + "\" delta_okay=\"" + |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 162 | (params.delta_okay ? "true" : "false") + "\">\n" + body + |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 163 | " </o:app>\n" |
| 164 | "</o:gupdate>\n"; |
| 165 | } |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 166 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 167 | } // namespace {} |
| 168 | |
| 169 | // Encodes XML entities in a given string with libxml2. input must be |
| 170 | // UTF-8 formatted. Output will be UTF-8 formatted. |
| 171 | string XmlEncode(const string& input) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 172 | // // TODO(adlr): if allocating a new xmlDoc each time is taking up too much |
| 173 | // // cpu, considering creating one and caching it. |
| 174 | // scoped_ptr_malloc<xmlDoc, ScopedPtrXmlDocFree> xml_doc( |
| 175 | // xmlNewDoc(ConstXMLStr("1.0"))); |
| 176 | // if (!xml_doc.get()) { |
| 177 | // LOG(ERROR) << "Unable to create xmlDoc"; |
| 178 | // return ""; |
| 179 | // } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 180 | scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str( |
| 181 | xmlEncodeEntitiesReentrant(NULL, ConstXMLStr(input.c_str()))); |
| 182 | return string(reinterpret_cast<const char *>(str.get())); |
| 183 | } |
| 184 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 185 | OmahaRequestAction::OmahaRequestAction(PrefsInterface* prefs, |
| 186 | const OmahaRequestParams& params, |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 187 | OmahaEvent* event, |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 188 | HttpFetcher* http_fetcher, |
| 189 | bool ping_only) |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 190 | : prefs_(prefs), |
| 191 | params_(params), |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 192 | event_(event), |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 193 | http_fetcher_(http_fetcher), |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 194 | ping_only_(ping_only), |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 195 | ping_active_days_(0), |
Andrew de los Reyes | 771e1bd | 2011-08-30 14:47:23 -0700 | [diff] [blame] | 196 | ping_roll_call_days_(0) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 197 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 198 | OmahaRequestAction::~OmahaRequestAction() {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 199 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 200 | // Calculates the value to use for the ping days parameter. |
| 201 | int OmahaRequestAction::CalculatePingDays(const string& key) { |
| 202 | int days = kNeverPinged; |
| 203 | int64_t last_ping = 0; |
| 204 | if (prefs_->GetInt64(key, &last_ping) && last_ping >= 0) { |
| 205 | days = (Time::Now() - Time::FromInternalValue(last_ping)).InDays(); |
| 206 | if (days < 0) { |
| 207 | // If |days| is negative, then the system clock must have jumped |
| 208 | // back in time since the ping was sent. Mark the value so that |
| 209 | // it doesn't get sent to the server but we still update the |
| 210 | // last ping daystart preference. This way the next ping time |
| 211 | // will be correct, hopefully. |
| 212 | days = kPingTimeJump; |
| 213 | LOG(WARNING) << |
| 214 | "System clock jumped back in time. Resetting ping daystarts."; |
| 215 | } |
| 216 | } |
| 217 | return days; |
| 218 | } |
| 219 | |
| 220 | void OmahaRequestAction::InitPingDays() { |
| 221 | // We send pings only along with update checks, not with events. |
| 222 | if (IsEvent()) { |
| 223 | return; |
| 224 | } |
| 225 | // TODO(petkov): Figure a way to distinguish active use pings |
| 226 | // vs. roll call pings. Currently, the two pings are identical. A |
| 227 | // fix needs to change this code as well as UpdateLastPingDays. |
| 228 | ping_active_days_ = CalculatePingDays(kPrefsLastActivePingDay); |
| 229 | ping_roll_call_days_ = CalculatePingDays(kPrefsLastRollCallPingDay); |
| 230 | } |
| 231 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 232 | void OmahaRequestAction::PerformAction() { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 233 | http_fetcher_->set_delegate(this); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 234 | InitPingDays(); |
Thieu Le | b44e9e8 | 2011-06-06 14:34:04 -0700 | [diff] [blame] | 235 | if (ping_only_ && |
| 236 | !ShouldPing(ping_active_days_) && |
| 237 | !ShouldPing(ping_roll_call_days_)) { |
| 238 | processor_->ActionComplete(this, kActionCodeSuccess); |
| 239 | return; |
| 240 | } |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 241 | string request_post(FormatRequest(event_.get(), |
| 242 | params_, |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 243 | ping_only_, |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 244 | ping_active_days_, |
Darin Petkov | 95508da | 2011-01-05 12:42:29 -0800 | [diff] [blame] | 245 | ping_roll_call_days_, |
| 246 | prefs_)); |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 247 | |
Gilad Arnold | 9dd1e7c | 2012-02-16 12:13:36 -0800 | [diff] [blame] | 248 | http_fetcher_->SetPostData(request_post.data(), request_post.size(), |
| 249 | kHttpContentTypeTextXml); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 250 | LOG(INFO) << "Posting an Omaha request to " << params_.update_url; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 251 | LOG(INFO) << "Request: " << request_post; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 252 | http_fetcher_->BeginTransfer(params_.update_url); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 255 | void OmahaRequestAction::TerminateProcessing() { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 256 | http_fetcher_->TerminateTransfer(); |
| 257 | } |
| 258 | |
| 259 | // We just store the response in the buffer. Once we've received all bytes, |
| 260 | // we'll look in the buffer and decide what to do. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 261 | void OmahaRequestAction::ReceivedBytes(HttpFetcher *fetcher, |
| 262 | const char* bytes, |
| 263 | int length) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 264 | response_buffer_.reserve(response_buffer_.size() + length); |
| 265 | response_buffer_.insert(response_buffer_.end(), bytes, bytes + length); |
| 266 | } |
| 267 | |
| 268 | namespace { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 269 | // If non-NULL response, caller is responsible for calling xmlXPathFreeObject() |
| 270 | // on the returned object. |
| 271 | // This code is roughly based on the libxml tutorial at: |
| 272 | // http://xmlsoft.org/tutorial/apd.html |
| 273 | xmlXPathObject* GetNodeSet(xmlDoc* doc, const xmlChar* xpath, |
| 274 | const xmlChar* ns, const xmlChar* ns_url) { |
| 275 | xmlXPathObject* result = NULL; |
| 276 | |
| 277 | scoped_ptr_malloc<xmlXPathContext, ScopedPtrXmlXPathContextFree> context( |
| 278 | xmlXPathNewContext(doc)); |
| 279 | if (!context.get()) { |
| 280 | LOG(ERROR) << "xmlXPathNewContext() returned NULL"; |
| 281 | return NULL; |
| 282 | } |
| 283 | if (xmlXPathRegisterNs(context.get(), ns, ns_url) < 0) { |
| 284 | LOG(ERROR) << "xmlXPathRegisterNs() returned error"; |
| 285 | return NULL; |
| 286 | } |
| 287 | |
| 288 | result = xmlXPathEvalExpression(xpath, context.get()); |
| 289 | |
| 290 | if (result == NULL) { |
| 291 | LOG(ERROR) << "xmlXPathEvalExpression returned error"; |
| 292 | return NULL; |
| 293 | } |
| 294 | if(xmlXPathNodeSetIsEmpty(result->nodesetval)){ |
| 295 | LOG(INFO) << "xpath not found in doc"; |
| 296 | xmlXPathFreeObject(result); |
| 297 | return NULL; |
| 298 | } |
| 299 | return result; |
| 300 | } |
| 301 | |
| 302 | // Returns the string value of a named attribute on a node, or empty string |
| 303 | // if no such node exists. If the attribute exists and has a value of |
| 304 | // empty string, there's no way to distinguish that from the attribute |
| 305 | // not existing. |
| 306 | string XmlGetProperty(xmlNode* node, const char* name) { |
| 307 | if (!xmlHasProp(node, ConstXMLStr(name))) |
| 308 | return ""; |
| 309 | scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str( |
| 310 | xmlGetProp(node, ConstXMLStr(name))); |
| 311 | string ret(reinterpret_cast<const char *>(str.get())); |
| 312 | return ret; |
| 313 | } |
| 314 | |
| 315 | // Parses a 64 bit base-10 int from a string and returns it. Returns 0 |
| 316 | // on error. If the string contains "0", that's indistinguishable from |
| 317 | // error. |
| 318 | off_t ParseInt(const string& str) { |
| 319 | off_t ret = 0; |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 320 | int rc = sscanf(str.c_str(), "%" PRIi64, &ret); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 321 | if (rc < 1) { |
| 322 | // failure |
| 323 | return 0; |
| 324 | } |
| 325 | return ret; |
| 326 | } |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 327 | |
| 328 | // Update the last ping day preferences based on the server daystart |
| 329 | // response. Returns true on success, false otherwise. |
| 330 | bool UpdateLastPingDays(xmlDoc* doc, PrefsInterface* prefs) { |
| 331 | static const char kNamespace[] = "x"; |
| 332 | static const char kDaystartNodeXpath[] = "/x:gupdate/x:daystart"; |
| 333 | static const char kNsUrl[] = "http://www.google.com/update2/response"; |
| 334 | |
| 335 | scoped_ptr_malloc<xmlXPathObject, ScopedPtrXmlXPathObjectFree> |
| 336 | xpath_nodeset(GetNodeSet(doc, |
| 337 | ConstXMLStr(kDaystartNodeXpath), |
| 338 | ConstXMLStr(kNamespace), |
| 339 | ConstXMLStr(kNsUrl))); |
| 340 | TEST_AND_RETURN_FALSE(xpath_nodeset.get()); |
| 341 | xmlNodeSet* nodeset = xpath_nodeset->nodesetval; |
| 342 | TEST_AND_RETURN_FALSE(nodeset && nodeset->nodeNr >= 1); |
| 343 | xmlNode* daystart_node = nodeset->nodeTab[0]; |
| 344 | TEST_AND_RETURN_FALSE(xmlHasProp(daystart_node, |
| 345 | ConstXMLStr("elapsed_seconds"))); |
| 346 | |
| 347 | int64_t elapsed_seconds = 0; |
Chris Masone | 790e62e | 2010-08-12 10:41:18 -0700 | [diff] [blame] | 348 | TEST_AND_RETURN_FALSE(base::StringToInt64(XmlGetProperty(daystart_node, |
| 349 | "elapsed_seconds"), |
| 350 | &elapsed_seconds)); |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 351 | TEST_AND_RETURN_FALSE(elapsed_seconds >= 0); |
| 352 | |
| 353 | // Remember the local time that matches the server's last midnight |
| 354 | // time. |
| 355 | Time daystart = Time::Now() - TimeDelta::FromSeconds(elapsed_seconds); |
| 356 | prefs->SetInt64(kPrefsLastActivePingDay, daystart.ToInternalValue()); |
| 357 | prefs->SetInt64(kPrefsLastRollCallPingDay, daystart.ToInternalValue()); |
| 358 | return true; |
| 359 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 360 | } // namespace {} |
| 361 | |
| 362 | // If the transfer was successful, this uses libxml2 to parse the response |
| 363 | // and fill in the appropriate fields of the output object. Also, notifies |
| 364 | // the processor that we're done. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 365 | void OmahaRequestAction::TransferComplete(HttpFetcher *fetcher, |
| 366 | bool successful) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 367 | ScopedActionCompleter completer(processor_, this); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 368 | LOG(INFO) << "Omaha request response: " << string(response_buffer_.begin(), |
| 369 | response_buffer_.end()); |
| 370 | |
| 371 | // Events are best effort transactions -- assume they always succeed. |
| 372 | if (IsEvent()) { |
| 373 | CHECK(!HasOutputPipe()) << "No output pipe allowed for event requests."; |
Andrew de los Reyes | 2008e4c | 2011-01-12 10:17:52 -0800 | [diff] [blame] | 374 | if (event_->result == OmahaEvent::kResultError && successful && |
| 375 | utils::IsOfficialBuild()) { |
| 376 | LOG(INFO) << "Signalling Crash Reporter."; |
| 377 | utils::ScheduleCrashReporterUpload(); |
| 378 | } |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 379 | completer.set_code(kActionCodeSuccess); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 380 | return; |
| 381 | } |
| 382 | |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 383 | if (!successful) { |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 384 | LOG(ERROR) << "Omaha request network transfer failed."; |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 385 | int code = GetHTTPResponseCode(); |
| 386 | // Makes sure we send sane error values. |
| 387 | if (code < 0 || code >= 1000) { |
| 388 | code = 999; |
| 389 | } |
| 390 | completer.set_code(static_cast<ActionExitCode>( |
| 391 | kActionCodeOmahaRequestHTTPResponseBase + code)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 392 | return; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 393 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 394 | |
| 395 | // parse our response and fill the fields in the output object |
| 396 | scoped_ptr_malloc<xmlDoc, ScopedPtrXmlDocFree> doc( |
| 397 | xmlParseMemory(&response_buffer_[0], response_buffer_.size())); |
| 398 | if (!doc.get()) { |
| 399 | LOG(ERROR) << "Omaha response not valid XML"; |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 400 | completer.set_code(response_buffer_.empty() ? |
| 401 | kActionCodeOmahaRequestEmptyResponseError : |
| 402 | kActionCodeOmahaRequestXMLParseError); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 403 | return; |
| 404 | } |
| 405 | |
Darin Petkov | 1cbd78f | 2010-07-29 12:38:34 -0700 | [diff] [blame] | 406 | // If a ping was sent, update the last ping day preferences based on |
| 407 | // the server daystart response. |
| 408 | if (ShouldPing(ping_active_days_) || |
| 409 | ShouldPing(ping_roll_call_days_) || |
| 410 | ping_active_days_ == kPingTimeJump || |
| 411 | ping_roll_call_days_ == kPingTimeJump) { |
| 412 | LOG_IF(ERROR, !UpdateLastPingDays(doc.get(), prefs_)) |
| 413 | << "Failed to update the last ping day preferences!"; |
| 414 | } |
| 415 | |
Thieu Le | 116fda3 | 2011-04-19 11:01:54 -0700 | [diff] [blame] | 416 | if (!HasOutputPipe()) { |
| 417 | // Just set success to whether or not the http transfer succeeded, |
| 418 | // which must be true at this point in the code. |
| 419 | completer.set_code(kActionCodeSuccess); |
| 420 | return; |
| 421 | } |
| 422 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 423 | static const char* kNamespace("x"); |
| 424 | static const char* kUpdatecheckNodeXpath("/x:gupdate/x:app/x:updatecheck"); |
| 425 | static const char* kNsUrl("http://www.google.com/update2/response"); |
| 426 | |
| 427 | scoped_ptr_malloc<xmlXPathObject, ScopedPtrXmlXPathObjectFree> |
| 428 | xpath_nodeset(GetNodeSet(doc.get(), |
| 429 | ConstXMLStr(kUpdatecheckNodeXpath), |
| 430 | ConstXMLStr(kNamespace), |
| 431 | ConstXMLStr(kNsUrl))); |
| 432 | if (!xpath_nodeset.get()) { |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 433 | completer.set_code(kActionCodeOmahaRequestNoUpdateCheckNode); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 434 | return; |
| 435 | } |
| 436 | xmlNodeSet* nodeset = xpath_nodeset->nodesetval; |
| 437 | CHECK(nodeset) << "XPath missing NodeSet"; |
| 438 | CHECK_GE(nodeset->nodeNr, 1); |
| 439 | |
| 440 | xmlNode* updatecheck_node = nodeset->nodeTab[0]; |
| 441 | // get status |
| 442 | if (!xmlHasProp(updatecheck_node, ConstXMLStr("status"))) { |
| 443 | LOG(ERROR) << "Response missing status"; |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 444 | completer.set_code(kActionCodeOmahaRequestNoUpdateCheckStatus); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 445 | return; |
| 446 | } |
| 447 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 448 | OmahaResponse output_object; |
Darin Petkov | 85ced13 | 2010-09-01 10:20:56 -0700 | [diff] [blame] | 449 | base::StringToInt(XmlGetProperty(updatecheck_node, "PollInterval"), |
| 450 | &output_object.poll_interval); |
| 451 | const string status(XmlGetProperty(updatecheck_node, "status")); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 452 | if (status == "noupdate") { |
| 453 | LOG(INFO) << "No update."; |
| 454 | output_object.update_exists = false; |
| 455 | SetOutputObject(output_object); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 456 | completer.set_code(kActionCodeSuccess); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 457 | return; |
| 458 | } |
| 459 | |
| 460 | if (status != "ok") { |
| 461 | LOG(ERROR) << "Unknown status: " << status; |
Darin Petkov | edc522e | 2010-11-05 09:35:17 -0700 | [diff] [blame] | 462 | completer.set_code(kActionCodeOmahaRequestBadUpdateCheckStatus); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 463 | return; |
| 464 | } |
| 465 | |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 466 | if (params_.update_disabled) { |
Jay Srinivasan | 56d5aa4 | 2012-03-26 14:27:59 -0700 | [diff] [blame] | 467 | LOG(INFO) << "Ignoring Omaha updates as updates are disabled by policy."; |
Jay Srinivasan | 0a70874 | 2012-03-20 11:26:12 -0700 | [diff] [blame] | 468 | completer.set_code(kActionCodeOmahaUpdateIgnoredPerPolicy); |
| 469 | return; |
| 470 | } |
| 471 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 472 | // In best-effort fashion, fetch the rest of the expected attributes |
| 473 | // from the updatecheck node, then return the object |
| 474 | output_object.update_exists = true; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 475 | completer.set_code(kActionCodeSuccess); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 476 | |
| 477 | output_object.display_version = |
| 478 | XmlGetProperty(updatecheck_node, "DisplayVersion"); |
| 479 | output_object.codebase = XmlGetProperty(updatecheck_node, "codebase"); |
| 480 | output_object.more_info_url = XmlGetProperty(updatecheck_node, "MoreInfo"); |
Darin Petkov | d22cb29 | 2010-09-29 10:02:29 -0700 | [diff] [blame] | 481 | output_object.hash = XmlGetProperty(updatecheck_node, "sha256"); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 482 | output_object.size = ParseInt(XmlGetProperty(updatecheck_node, "size")); |
| 483 | output_object.needs_admin = |
| 484 | XmlGetProperty(updatecheck_node, "needsadmin") == "true"; |
| 485 | output_object.prompt = XmlGetProperty(updatecheck_node, "Prompt") == "true"; |
Darin Petkov | 6c11864 | 2010-10-21 12:06:30 -0700 | [diff] [blame] | 486 | output_object.deadline = XmlGetProperty(updatecheck_node, "deadline"); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 487 | SetOutputObject(output_object); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | }; // namespace chromeos_update_engine |