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