Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 1 | // Copyright (c) 2010 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" |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 6 | #include <inttypes.h> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 7 | #include <sstream> |
| 8 | |
| 9 | #include <libxml/parser.h> |
| 10 | #include <libxml/xpath.h> |
| 11 | #include <libxml/xpathInternals.h> |
| 12 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 13 | #include "base/string_util.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 14 | #include "chromeos/obsolete_logging.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 15 | #include "update_engine/action_pipe.h" |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 16 | #include "update_engine/omaha_request_params.h" |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 17 | #include "update_engine/utils.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 18 | |
| 19 | using std::string; |
| 20 | |
| 21 | namespace chromeos_update_engine { |
| 22 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 23 | const char* const OmahaRequestParams::kAppId( |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 24 | "{87efface-864d-49a5-9bb3-4b050a7c227a}"); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 25 | const char* const OmahaRequestParams::kOsPlatform("Chrome OS"); |
| 26 | const char* const OmahaRequestParams::kOsVersion("Indy"); |
| 27 | const char* const OmahaRequestParams::kUpdateUrl( |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 28 | "https://tools.google.com/service/update2"); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 29 | |
| 30 | namespace { |
| 31 | |
| 32 | const string kGupdateVersion("ChromeOSUpdateEngine-0.1.0.0"); |
| 33 | |
| 34 | // This is handy for passing strings into libxml2 |
| 35 | #define ConstXMLStr(x) (reinterpret_cast<const xmlChar*>(x)) |
| 36 | |
| 37 | // These are for scoped_ptr_malloc, which is like scoped_ptr, but allows |
| 38 | // a custom free() function to be specified. |
| 39 | class ScopedPtrXmlDocFree { |
| 40 | public: |
| 41 | inline void operator()(void* x) const { |
| 42 | xmlFreeDoc(reinterpret_cast<xmlDoc*>(x)); |
| 43 | } |
| 44 | }; |
| 45 | class ScopedPtrXmlFree { |
| 46 | public: |
| 47 | inline void operator()(void* x) const { |
| 48 | xmlFree(x); |
| 49 | } |
| 50 | }; |
| 51 | class ScopedPtrXmlXPathObjectFree { |
| 52 | public: |
| 53 | inline void operator()(void* x) const { |
| 54 | xmlXPathFreeObject(reinterpret_cast<xmlXPathObject*>(x)); |
| 55 | } |
| 56 | }; |
| 57 | class ScopedPtrXmlXPathContextFree { |
| 58 | public: |
| 59 | inline void operator()(void* x) const { |
| 60 | xmlXPathFreeContext(reinterpret_cast<xmlXPathContext*>(x)); |
| 61 | } |
| 62 | }; |
| 63 | |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 64 | string FormatRequest(const OmahaEvent* event, |
| 65 | const OmahaRequestParams& params) { |
| 66 | string body; |
| 67 | if (event == NULL) { |
| 68 | body = string( |
| 69 | " <o:ping active=\"0\"></o:ping>\n" |
| 70 | " <o:updatecheck></o:updatecheck>\n"); |
| 71 | } else { |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame^] | 72 | // The error code is an optional attribute so append it only if |
| 73 | // the result is not success. |
| 74 | string error_code; |
| 75 | if (event->result != OmahaEvent::kResultSuccess) { |
| 76 | error_code = StringPrintf(" errorcode=\"%d\"", event->error_code); |
| 77 | } |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 78 | body = StringPrintf( |
Darin Petkov | e17f86b | 2010-07-20 09:12:01 -0700 | [diff] [blame^] | 79 | " <o:event eventtype=\"%d\" eventresult=\"%d\"%s></o:event>\n", |
| 80 | event->type, event->result, error_code.c_str()); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 81 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 82 | return string("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 83 | "<o:gupdate xmlns:o=\"http://www.google.com/update2/request\" " |
| 84 | "version=\"" + XmlEncode(kGupdateVersion) + "\" " |
| 85 | "updaterversion=\"" + XmlEncode(kGupdateVersion) + "\" " |
| 86 | "protocol=\"2.0\" " |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 87 | "machineid=\"") + XmlEncode(params.machine_id) + |
| 88 | "\" ismachine=\"1\" userid=\"" + XmlEncode(params.user_id) + "\">\n" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 89 | " <o:os version=\"" + XmlEncode(params.os_version) + "\" platform=\"" + |
| 90 | XmlEncode(params.os_platform) + "\" sp=\"" + |
| 91 | XmlEncode(params.os_sp) + "\"></o:os>\n" |
| 92 | " <o:app appid=\"" + XmlEncode(params.app_id) + "\" version=\"" + |
| 93 | XmlEncode(params.app_version) + "\" " |
| 94 | "lang=\"" + XmlEncode(params.app_lang) + "\" track=\"" + |
Andrew de los Reyes | 37c2032 | 2010-06-30 13:27:19 -0700 | [diff] [blame] | 95 | XmlEncode(params.app_track) + "\" board=\"" + |
Andrew de los Reyes | 3f0303a | 2010-07-15 22:35:35 -0700 | [diff] [blame] | 96 | XmlEncode(params.os_board) + "\" delta_okay=\"" + |
| 97 | (params.delta_okay ? "true" : "false") + "\">\n" + body + |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 98 | " </o:app>\n" |
| 99 | "</o:gupdate>\n"; |
| 100 | } |
| 101 | } // namespace {} |
| 102 | |
| 103 | // Encodes XML entities in a given string with libxml2. input must be |
| 104 | // UTF-8 formatted. Output will be UTF-8 formatted. |
| 105 | string XmlEncode(const string& input) { |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 106 | // // TODO(adlr): if allocating a new xmlDoc each time is taking up too much |
| 107 | // // cpu, considering creating one and caching it. |
| 108 | // scoped_ptr_malloc<xmlDoc, ScopedPtrXmlDocFree> xml_doc( |
| 109 | // xmlNewDoc(ConstXMLStr("1.0"))); |
| 110 | // if (!xml_doc.get()) { |
| 111 | // LOG(ERROR) << "Unable to create xmlDoc"; |
| 112 | // return ""; |
| 113 | // } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 114 | scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str( |
| 115 | xmlEncodeEntitiesReentrant(NULL, ConstXMLStr(input.c_str()))); |
| 116 | return string(reinterpret_cast<const char *>(str.get())); |
| 117 | } |
| 118 | |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 119 | OmahaRequestAction::OmahaRequestAction(const OmahaRequestParams& params, |
| 120 | OmahaEvent* event, |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 121 | HttpFetcher* http_fetcher) |
Darin Petkov | a4a8a8c | 2010-07-15 22:21:12 -0700 | [diff] [blame] | 122 | : params_(params), |
| 123 | event_(event), |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 124 | http_fetcher_(http_fetcher) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 125 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 126 | OmahaRequestAction::~OmahaRequestAction() {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 127 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 128 | void OmahaRequestAction::PerformAction() { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 129 | http_fetcher_->set_delegate(this); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 130 | string request_post(FormatRequest(event_.get(), params_)); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 131 | http_fetcher_->SetPostData(request_post.data(), request_post.size()); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 132 | LOG(INFO) << "Posting an Omaha request to " << params_.update_url; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 133 | LOG(INFO) << "Request: " << request_post; |
Andrew de los Reyes | f971443 | 2010-05-04 10:21:23 -0700 | [diff] [blame] | 134 | http_fetcher_->BeginTransfer(params_.update_url); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 137 | void OmahaRequestAction::TerminateProcessing() { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 138 | http_fetcher_->TerminateTransfer(); |
| 139 | } |
| 140 | |
| 141 | // We just store the response in the buffer. Once we've received all bytes, |
| 142 | // we'll look in the buffer and decide what to do. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 143 | void OmahaRequestAction::ReceivedBytes(HttpFetcher *fetcher, |
| 144 | const char* bytes, |
| 145 | int length) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 146 | response_buffer_.reserve(response_buffer_.size() + length); |
| 147 | response_buffer_.insert(response_buffer_.end(), bytes, bytes + length); |
| 148 | } |
| 149 | |
| 150 | namespace { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 151 | // If non-NULL response, caller is responsible for calling xmlXPathFreeObject() |
| 152 | // on the returned object. |
| 153 | // This code is roughly based on the libxml tutorial at: |
| 154 | // http://xmlsoft.org/tutorial/apd.html |
| 155 | xmlXPathObject* GetNodeSet(xmlDoc* doc, const xmlChar* xpath, |
| 156 | const xmlChar* ns, const xmlChar* ns_url) { |
| 157 | xmlXPathObject* result = NULL; |
| 158 | |
| 159 | scoped_ptr_malloc<xmlXPathContext, ScopedPtrXmlXPathContextFree> context( |
| 160 | xmlXPathNewContext(doc)); |
| 161 | if (!context.get()) { |
| 162 | LOG(ERROR) << "xmlXPathNewContext() returned NULL"; |
| 163 | return NULL; |
| 164 | } |
| 165 | if (xmlXPathRegisterNs(context.get(), ns, ns_url) < 0) { |
| 166 | LOG(ERROR) << "xmlXPathRegisterNs() returned error"; |
| 167 | return NULL; |
| 168 | } |
| 169 | |
| 170 | result = xmlXPathEvalExpression(xpath, context.get()); |
| 171 | |
| 172 | if (result == NULL) { |
| 173 | LOG(ERROR) << "xmlXPathEvalExpression returned error"; |
| 174 | return NULL; |
| 175 | } |
| 176 | if(xmlXPathNodeSetIsEmpty(result->nodesetval)){ |
| 177 | LOG(INFO) << "xpath not found in doc"; |
| 178 | xmlXPathFreeObject(result); |
| 179 | return NULL; |
| 180 | } |
| 181 | return result; |
| 182 | } |
| 183 | |
| 184 | // Returns the string value of a named attribute on a node, or empty string |
| 185 | // if no such node exists. If the attribute exists and has a value of |
| 186 | // empty string, there's no way to distinguish that from the attribute |
| 187 | // not existing. |
| 188 | string XmlGetProperty(xmlNode* node, const char* name) { |
| 189 | if (!xmlHasProp(node, ConstXMLStr(name))) |
| 190 | return ""; |
| 191 | scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str( |
| 192 | xmlGetProp(node, ConstXMLStr(name))); |
| 193 | string ret(reinterpret_cast<const char *>(str.get())); |
| 194 | return ret; |
| 195 | } |
| 196 | |
| 197 | // Parses a 64 bit base-10 int from a string and returns it. Returns 0 |
| 198 | // on error. If the string contains "0", that's indistinguishable from |
| 199 | // error. |
| 200 | off_t ParseInt(const string& str) { |
| 201 | off_t ret = 0; |
Andrew de los Reyes | 08c4e27 | 2010-04-15 14:02:17 -0700 | [diff] [blame] | 202 | int rc = sscanf(str.c_str(), "%" PRIi64, &ret); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 203 | if (rc < 1) { |
| 204 | // failure |
| 205 | return 0; |
| 206 | } |
| 207 | return ret; |
| 208 | } |
| 209 | } // namespace {} |
| 210 | |
| 211 | // If the transfer was successful, this uses libxml2 to parse the response |
| 212 | // and fill in the appropriate fields of the output object. Also, notifies |
| 213 | // the processor that we're done. |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 214 | void OmahaRequestAction::TransferComplete(HttpFetcher *fetcher, |
| 215 | bool successful) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 216 | ScopedActionCompleter completer(processor_, this); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 217 | LOG(INFO) << "Omaha request response: " << string(response_buffer_.begin(), |
| 218 | response_buffer_.end()); |
| 219 | |
| 220 | // Events are best effort transactions -- assume they always succeed. |
| 221 | if (IsEvent()) { |
| 222 | CHECK(!HasOutputPipe()) << "No output pipe allowed for event requests."; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 223 | completer.set_code(kActionCodeSuccess); |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 227 | if (!successful) { |
Darin Petkov | 0dc8e9a | 2010-07-14 14:51:57 -0700 | [diff] [blame] | 228 | LOG(ERROR) << "Omaha request network transfer failed."; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 229 | return; |
Andrew de los Reyes | f98bff8 | 2010-05-06 13:33:25 -0700 | [diff] [blame] | 230 | } |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 231 | if (!HasOutputPipe()) { |
| 232 | // Just set success to whether or not the http transfer succeeded, |
| 233 | // which must be true at this point in the code. |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 234 | completer.set_code(kActionCodeSuccess); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 235 | return; |
| 236 | } |
| 237 | |
| 238 | // parse our response and fill the fields in the output object |
| 239 | scoped_ptr_malloc<xmlDoc, ScopedPtrXmlDocFree> doc( |
| 240 | xmlParseMemory(&response_buffer_[0], response_buffer_.size())); |
| 241 | if (!doc.get()) { |
| 242 | LOG(ERROR) << "Omaha response not valid XML"; |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | static const char* kNamespace("x"); |
| 247 | static const char* kUpdatecheckNodeXpath("/x:gupdate/x:app/x:updatecheck"); |
| 248 | static const char* kNsUrl("http://www.google.com/update2/response"); |
| 249 | |
| 250 | scoped_ptr_malloc<xmlXPathObject, ScopedPtrXmlXPathObjectFree> |
| 251 | xpath_nodeset(GetNodeSet(doc.get(), |
| 252 | ConstXMLStr(kUpdatecheckNodeXpath), |
| 253 | ConstXMLStr(kNamespace), |
| 254 | ConstXMLStr(kNsUrl))); |
| 255 | if (!xpath_nodeset.get()) { |
| 256 | return; |
| 257 | } |
| 258 | xmlNodeSet* nodeset = xpath_nodeset->nodesetval; |
| 259 | CHECK(nodeset) << "XPath missing NodeSet"; |
| 260 | CHECK_GE(nodeset->nodeNr, 1); |
| 261 | |
| 262 | xmlNode* updatecheck_node = nodeset->nodeTab[0]; |
| 263 | // get status |
| 264 | if (!xmlHasProp(updatecheck_node, ConstXMLStr("status"))) { |
| 265 | LOG(ERROR) << "Response missing status"; |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | const string status(XmlGetProperty(updatecheck_node, "status")); |
Darin Petkov | 6a5b322 | 2010-07-13 14:55:28 -0700 | [diff] [blame] | 270 | OmahaResponse output_object; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 271 | if (status == "noupdate") { |
| 272 | LOG(INFO) << "No update."; |
| 273 | output_object.update_exists = false; |
| 274 | SetOutputObject(output_object); |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 275 | completer.set_code(kActionCodeSuccess); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 276 | return; |
| 277 | } |
| 278 | |
| 279 | if (status != "ok") { |
| 280 | LOG(ERROR) << "Unknown status: " << status; |
| 281 | return; |
| 282 | } |
| 283 | |
| 284 | // In best-effort fashion, fetch the rest of the expected attributes |
| 285 | // from the updatecheck node, then return the object |
| 286 | output_object.update_exists = true; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 287 | completer.set_code(kActionCodeSuccess); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 288 | |
| 289 | output_object.display_version = |
| 290 | XmlGetProperty(updatecheck_node, "DisplayVersion"); |
| 291 | output_object.codebase = XmlGetProperty(updatecheck_node, "codebase"); |
| 292 | output_object.more_info_url = XmlGetProperty(updatecheck_node, "MoreInfo"); |
| 293 | output_object.hash = XmlGetProperty(updatecheck_node, "hash"); |
| 294 | output_object.size = ParseInt(XmlGetProperty(updatecheck_node, "size")); |
| 295 | output_object.needs_admin = |
| 296 | XmlGetProperty(updatecheck_node, "needsadmin") == "true"; |
| 297 | output_object.prompt = XmlGetProperty(updatecheck_node, "Prompt") == "true"; |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 298 | output_object.is_delta = |
| 299 | XmlGetProperty(updatecheck_node, "IsDelta") == "true"; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 300 | SetOutputObject(output_object); |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | }; // namespace chromeos_update_engine |