blob: 9d53a4ea63699fbb7dcc5af2867f88d3125b404c [file] [log] [blame]
Darin Petkova4a8a8c2010-07-15 22:21:12 -07001// Copyright (c) 2010 The Chromium OS Authors. All rights reserved.
rspangler@google.com49fdf182009-10-10 00:57:34 +00002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Darin Petkov6a5b3222010-07-13 14:55:28 -07005#include "update_engine/omaha_request_action.h"
Andrew de los Reyes08c4e272010-04-15 14:02:17 -07006#include <inttypes.h>
rspangler@google.com49fdf182009-10-10 00:57:34 +00007#include <sstream>
8
9#include <libxml/parser.h>
10#include <libxml/xpath.h>
11#include <libxml/xpathInternals.h>
12
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070013#include "base/string_util.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000014#include "chromeos/obsolete_logging.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000015#include "update_engine/action_pipe.h"
Darin Petkova4a8a8c2010-07-15 22:21:12 -070016#include "update_engine/omaha_request_params.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000017#include "update_engine/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000018
19using std::string;
20
21namespace chromeos_update_engine {
22
Darin Petkov6a5b3222010-07-13 14:55:28 -070023const char* const OmahaRequestParams::kAppId(
adlr@google.comc98a7ed2009-12-04 18:54:03 +000024 "{87efface-864d-49a5-9bb3-4b050a7c227a}");
Darin Petkov6a5b3222010-07-13 14:55:28 -070025const char* const OmahaRequestParams::kOsPlatform("Chrome OS");
26const char* const OmahaRequestParams::kOsVersion("Indy");
27const char* const OmahaRequestParams::kUpdateUrl(
Andrew de los Reyesf9714432010-05-04 10:21:23 -070028 "https://tools.google.com/service/update2");
rspangler@google.com49fdf182009-10-10 00:57:34 +000029
30namespace {
31
32const 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.
39class ScopedPtrXmlDocFree {
40 public:
41 inline void operator()(void* x) const {
42 xmlFreeDoc(reinterpret_cast<xmlDoc*>(x));
43 }
44};
45class ScopedPtrXmlFree {
46 public:
47 inline void operator()(void* x) const {
48 xmlFree(x);
49 }
50};
51class ScopedPtrXmlXPathObjectFree {
52 public:
53 inline void operator()(void* x) const {
54 xmlXPathFreeObject(reinterpret_cast<xmlXPathObject*>(x));
55 }
56};
57class ScopedPtrXmlXPathContextFree {
58 public:
59 inline void operator()(void* x) const {
60 xmlXPathFreeContext(reinterpret_cast<xmlXPathContext*>(x));
61 }
62};
63
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070064string 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 {
72 body = StringPrintf(
73 " <o:event eventtype=\"%d\" eventresult=\"%d\" "
74 "errorcode=\"%d\"></o:event>\n",
75 event->type, event->result, event->error_code);
76 }
rspangler@google.com49fdf182009-10-10 00:57:34 +000077 return string("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
Darin Petkov6a5b3222010-07-13 14:55:28 -070078 "<o:gupdate xmlns:o=\"http://www.google.com/update2/request\" "
79 "version=\"" + XmlEncode(kGupdateVersion) + "\" "
80 "updaterversion=\"" + XmlEncode(kGupdateVersion) + "\" "
81 "protocol=\"2.0\" "
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070082 "machineid=\"") + XmlEncode(params.machine_id) +
83 "\" ismachine=\"1\" userid=\"" + XmlEncode(params.user_id) + "\">\n"
rspangler@google.com49fdf182009-10-10 00:57:34 +000084 " <o:os version=\"" + XmlEncode(params.os_version) + "\" platform=\"" +
85 XmlEncode(params.os_platform) + "\" sp=\"" +
86 XmlEncode(params.os_sp) + "\"></o:os>\n"
87 " <o:app appid=\"" + XmlEncode(params.app_id) + "\" version=\"" +
88 XmlEncode(params.app_version) + "\" "
89 "lang=\"" + XmlEncode(params.app_lang) + "\" track=\"" +
Andrew de los Reyes37c20322010-06-30 13:27:19 -070090 XmlEncode(params.app_track) + "\" board=\"" +
Darin Petkov0dc8e9a2010-07-14 14:51:57 -070091 XmlEncode(params.os_board) + "\">\n" + body +
rspangler@google.com49fdf182009-10-10 00:57:34 +000092 " </o:app>\n"
93 "</o:gupdate>\n";
94}
95} // namespace {}
96
97// Encodes XML entities in a given string with libxml2. input must be
98// UTF-8 formatted. Output will be UTF-8 formatted.
99string XmlEncode(const string& input) {
Darin Petkov6a5b3222010-07-13 14:55:28 -0700100 // // TODO(adlr): if allocating a new xmlDoc each time is taking up too much
101 // // cpu, considering creating one and caching it.
102 // scoped_ptr_malloc<xmlDoc, ScopedPtrXmlDocFree> xml_doc(
103 // xmlNewDoc(ConstXMLStr("1.0")));
104 // if (!xml_doc.get()) {
105 // LOG(ERROR) << "Unable to create xmlDoc";
106 // return "";
107 // }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000108 scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str(
109 xmlEncodeEntitiesReentrant(NULL, ConstXMLStr(input.c_str())));
110 return string(reinterpret_cast<const char *>(str.get()));
111}
112
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700113OmahaRequestAction::OmahaRequestAction(const OmahaRequestParams& params,
114 OmahaEvent* event,
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700115 HttpFetcher* http_fetcher)
Darin Petkova4a8a8c2010-07-15 22:21:12 -0700116 : params_(params),
117 event_(event),
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700118 http_fetcher_(http_fetcher) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000119
Darin Petkov6a5b3222010-07-13 14:55:28 -0700120OmahaRequestAction::~OmahaRequestAction() {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000121
Darin Petkov6a5b3222010-07-13 14:55:28 -0700122void OmahaRequestAction::PerformAction() {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000123 http_fetcher_->set_delegate(this);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700124 string request_post(FormatRequest(event_.get(), params_));
rspangler@google.com49fdf182009-10-10 00:57:34 +0000125 http_fetcher_->SetPostData(request_post.data(), request_post.size());
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700126 LOG(INFO) << "Posting an Omaha request to " << params_.update_url;
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700127 LOG(INFO) << "Request: " << request_post;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700128 http_fetcher_->BeginTransfer(params_.update_url);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000129}
130
Darin Petkov6a5b3222010-07-13 14:55:28 -0700131void OmahaRequestAction::TerminateProcessing() {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000132 http_fetcher_->TerminateTransfer();
133}
134
135// We just store the response in the buffer. Once we've received all bytes,
136// we'll look in the buffer and decide what to do.
Darin Petkov6a5b3222010-07-13 14:55:28 -0700137void OmahaRequestAction::ReceivedBytes(HttpFetcher *fetcher,
138 const char* bytes,
139 int length) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000140 response_buffer_.reserve(response_buffer_.size() + length);
141 response_buffer_.insert(response_buffer_.end(), bytes, bytes + length);
142}
143
144namespace {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000145// If non-NULL response, caller is responsible for calling xmlXPathFreeObject()
146// on the returned object.
147// This code is roughly based on the libxml tutorial at:
148// http://xmlsoft.org/tutorial/apd.html
149xmlXPathObject* GetNodeSet(xmlDoc* doc, const xmlChar* xpath,
150 const xmlChar* ns, const xmlChar* ns_url) {
151 xmlXPathObject* result = NULL;
152
153 scoped_ptr_malloc<xmlXPathContext, ScopedPtrXmlXPathContextFree> context(
154 xmlXPathNewContext(doc));
155 if (!context.get()) {
156 LOG(ERROR) << "xmlXPathNewContext() returned NULL";
157 return NULL;
158 }
159 if (xmlXPathRegisterNs(context.get(), ns, ns_url) < 0) {
160 LOG(ERROR) << "xmlXPathRegisterNs() returned error";
161 return NULL;
162 }
163
164 result = xmlXPathEvalExpression(xpath, context.get());
165
166 if (result == NULL) {
167 LOG(ERROR) << "xmlXPathEvalExpression returned error";
168 return NULL;
169 }
170 if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
171 LOG(INFO) << "xpath not found in doc";
172 xmlXPathFreeObject(result);
173 return NULL;
174 }
175 return result;
176}
177
178// Returns the string value of a named attribute on a node, or empty string
179// if no such node exists. If the attribute exists and has a value of
180// empty string, there's no way to distinguish that from the attribute
181// not existing.
182string XmlGetProperty(xmlNode* node, const char* name) {
183 if (!xmlHasProp(node, ConstXMLStr(name)))
184 return "";
185 scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str(
186 xmlGetProp(node, ConstXMLStr(name)));
187 string ret(reinterpret_cast<const char *>(str.get()));
188 return ret;
189}
190
191// Parses a 64 bit base-10 int from a string and returns it. Returns 0
192// on error. If the string contains "0", that's indistinguishable from
193// error.
194off_t ParseInt(const string& str) {
195 off_t ret = 0;
Andrew de los Reyes08c4e272010-04-15 14:02:17 -0700196 int rc = sscanf(str.c_str(), "%" PRIi64, &ret);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000197 if (rc < 1) {
198 // failure
199 return 0;
200 }
201 return ret;
202}
203} // namespace {}
204
205// If the transfer was successful, this uses libxml2 to parse the response
206// and fill in the appropriate fields of the output object. Also, notifies
207// the processor that we're done.
Darin Petkov6a5b3222010-07-13 14:55:28 -0700208void OmahaRequestAction::TransferComplete(HttpFetcher *fetcher,
209 bool successful) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000210 ScopedActionCompleter completer(processor_, this);
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700211 LOG(INFO) << "Omaha request response: " << string(response_buffer_.begin(),
212 response_buffer_.end());
213
214 // Events are best effort transactions -- assume they always succeed.
215 if (IsEvent()) {
216 CHECK(!HasOutputPipe()) << "No output pipe allowed for event requests.";
217 completer.set_success(true);
218 return;
219 }
220
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700221 if (!successful) {
Darin Petkov0dc8e9a2010-07-14 14:51:57 -0700222 LOG(ERROR) << "Omaha request network transfer failed.";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000223 return;
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700224 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000225 if (!HasOutputPipe()) {
226 // Just set success to whether or not the http transfer succeeded,
227 // which must be true at this point in the code.
228 completer.set_success(true);
229 return;
230 }
231
232 // parse our response and fill the fields in the output object
233 scoped_ptr_malloc<xmlDoc, ScopedPtrXmlDocFree> doc(
234 xmlParseMemory(&response_buffer_[0], response_buffer_.size()));
235 if (!doc.get()) {
236 LOG(ERROR) << "Omaha response not valid XML";
237 return;
238 }
239
240 static const char* kNamespace("x");
241 static const char* kUpdatecheckNodeXpath("/x:gupdate/x:app/x:updatecheck");
242 static const char* kNsUrl("http://www.google.com/update2/response");
243
244 scoped_ptr_malloc<xmlXPathObject, ScopedPtrXmlXPathObjectFree>
245 xpath_nodeset(GetNodeSet(doc.get(),
246 ConstXMLStr(kUpdatecheckNodeXpath),
247 ConstXMLStr(kNamespace),
248 ConstXMLStr(kNsUrl)));
249 if (!xpath_nodeset.get()) {
250 return;
251 }
252 xmlNodeSet* nodeset = xpath_nodeset->nodesetval;
253 CHECK(nodeset) << "XPath missing NodeSet";
254 CHECK_GE(nodeset->nodeNr, 1);
255
256 xmlNode* updatecheck_node = nodeset->nodeTab[0];
257 // get status
258 if (!xmlHasProp(updatecheck_node, ConstXMLStr("status"))) {
259 LOG(ERROR) << "Response missing status";
260 return;
261 }
262
263 const string status(XmlGetProperty(updatecheck_node, "status"));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700264 OmahaResponse output_object;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000265 if (status == "noupdate") {
266 LOG(INFO) << "No update.";
267 output_object.update_exists = false;
268 SetOutputObject(output_object);
269 completer.set_success(true);
270 return;
271 }
272
273 if (status != "ok") {
274 LOG(ERROR) << "Unknown status: " << status;
275 return;
276 }
277
278 // In best-effort fashion, fetch the rest of the expected attributes
279 // from the updatecheck node, then return the object
280 output_object.update_exists = true;
281 completer.set_success(true);
282
283 output_object.display_version =
284 XmlGetProperty(updatecheck_node, "DisplayVersion");
285 output_object.codebase = XmlGetProperty(updatecheck_node, "codebase");
286 output_object.more_info_url = XmlGetProperty(updatecheck_node, "MoreInfo");
287 output_object.hash = XmlGetProperty(updatecheck_node, "hash");
288 output_object.size = ParseInt(XmlGetProperty(updatecheck_node, "size"));
289 output_object.needs_admin =
290 XmlGetProperty(updatecheck_node, "needsadmin") == "true";
291 output_object.prompt = XmlGetProperty(updatecheck_node, "Prompt") == "true";
Andrew de los Reyes3270f742010-07-15 22:28:14 -0700292 output_object.is_delta =
293 XmlGetProperty(updatecheck_node, "IsDelta") == "true";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000294 SetOutputObject(output_object);
295 return;
296}
297
298}; // namespace chromeos_update_engine