blob: 3c203e271478904b5b1496cf61c55bdf859eef5c [file] [log] [blame]
rspangler@google.com49fdf182009-10-10 00:57:34 +00001// Copyright (c) 2009 The Chromium OS Authors. All rights reserved.
2// 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
adlr@google.comc98a7ed2009-12-04 18:54:03 +000013#include "chromeos/obsolete_logging.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000014#include "update_engine/action_pipe.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000015#include "update_engine/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000016
17using std::string;
18
19namespace chromeos_update_engine {
20
Darin Petkov6a5b3222010-07-13 14:55:28 -070021const char* const OmahaRequestParams::kAppId(
adlr@google.comc98a7ed2009-12-04 18:54:03 +000022 "{87efface-864d-49a5-9bb3-4b050a7c227a}");
Darin Petkov6a5b3222010-07-13 14:55:28 -070023const char* const OmahaRequestParams::kOsPlatform("Chrome OS");
24const char* const OmahaRequestParams::kOsVersion("Indy");
25const char* const OmahaRequestParams::kUpdateUrl(
Andrew de los Reyesf9714432010-05-04 10:21:23 -070026 "https://tools.google.com/service/update2");
rspangler@google.com49fdf182009-10-10 00:57:34 +000027
28namespace {
29
30const string kGupdateVersion("ChromeOSUpdateEngine-0.1.0.0");
31
32// This is handy for passing strings into libxml2
33#define ConstXMLStr(x) (reinterpret_cast<const xmlChar*>(x))
34
35// These are for scoped_ptr_malloc, which is like scoped_ptr, but allows
36// a custom free() function to be specified.
37class ScopedPtrXmlDocFree {
38 public:
39 inline void operator()(void* x) const {
40 xmlFreeDoc(reinterpret_cast<xmlDoc*>(x));
41 }
42};
43class ScopedPtrXmlFree {
44 public:
45 inline void operator()(void* x) const {
46 xmlFree(x);
47 }
48};
49class ScopedPtrXmlXPathObjectFree {
50 public:
51 inline void operator()(void* x) const {
52 xmlXPathFreeObject(reinterpret_cast<xmlXPathObject*>(x));
53 }
54};
55class ScopedPtrXmlXPathContextFree {
56 public:
57 inline void operator()(void* x) const {
58 xmlXPathFreeContext(reinterpret_cast<xmlXPathContext*>(x));
59 }
60};
61
Darin Petkov6a5b3222010-07-13 14:55:28 -070062// Returns a properly formatted omaha request for a request to Omaha.
63string FormatRequest(const OmahaRequestParams& params) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000064 return string("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
Darin Petkov6a5b3222010-07-13 14:55:28 -070065 "<o:gupdate xmlns:o=\"http://www.google.com/update2/request\" "
66 "version=\"" + XmlEncode(kGupdateVersion) + "\" "
67 "updaterversion=\"" + XmlEncode(kGupdateVersion) + "\" "
68 "protocol=\"2.0\" "
69 "machineid=\"") + XmlEncode(params.machine_id) + "\" ismachine=\"1\" "
rspangler@google.com49fdf182009-10-10 00:57:34 +000070 "userid=\"" + XmlEncode(params.user_id) + "\">\n"
71 " <o:os version=\"" + XmlEncode(params.os_version) + "\" platform=\"" +
72 XmlEncode(params.os_platform) + "\" sp=\"" +
73 XmlEncode(params.os_sp) + "\"></o:os>\n"
74 " <o:app appid=\"" + XmlEncode(params.app_id) + "\" version=\"" +
75 XmlEncode(params.app_version) + "\" "
76 "lang=\"" + XmlEncode(params.app_lang) + "\" track=\"" +
Andrew de los Reyes37c20322010-06-30 13:27:19 -070077 XmlEncode(params.app_track) + "\" board=\"" +
78 XmlEncode(params.os_board) + "\">\n"
rspangler@google.com49fdf182009-10-10 00:57:34 +000079 " <o:ping active=\"0\"></o:ping>\n"
80 " <o:updatecheck></o:updatecheck>\n"
81 " </o:app>\n"
82 "</o:gupdate>\n";
83}
84} // namespace {}
85
86// Encodes XML entities in a given string with libxml2. input must be
87// UTF-8 formatted. Output will be UTF-8 formatted.
88string XmlEncode(const string& input) {
Darin Petkov6a5b3222010-07-13 14:55:28 -070089 // // TODO(adlr): if allocating a new xmlDoc each time is taking up too much
90 // // cpu, considering creating one and caching it.
91 // scoped_ptr_malloc<xmlDoc, ScopedPtrXmlDocFree> xml_doc(
92 // xmlNewDoc(ConstXMLStr("1.0")));
93 // if (!xml_doc.get()) {
94 // LOG(ERROR) << "Unable to create xmlDoc";
95 // return "";
96 // }
rspangler@google.com49fdf182009-10-10 00:57:34 +000097 scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str(
98 xmlEncodeEntitiesReentrant(NULL, ConstXMLStr(input.c_str())));
99 return string(reinterpret_cast<const char *>(str.get()));
100}
101
Darin Petkov6a5b3222010-07-13 14:55:28 -0700102OmahaRequestAction::OmahaRequestAction(HttpFetcher* http_fetcher)
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000103 : http_fetcher_(http_fetcher) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000104
Darin Petkov6a5b3222010-07-13 14:55:28 -0700105OmahaRequestAction::~OmahaRequestAction() {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000106
Darin Petkov6a5b3222010-07-13 14:55:28 -0700107void OmahaRequestAction::PerformAction() {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000108 CHECK(HasInputObject());
109 params_ = GetInputObject();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000110 http_fetcher_->set_delegate(this);
111 string request_post(FormatRequest(params_));
112 http_fetcher_->SetPostData(request_post.data(), request_post.size());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700113 LOG(INFO) << "Checking for update at " << params_.update_url;
114 LOG(INFO) << "Request: " << request_post;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700115 http_fetcher_->BeginTransfer(params_.update_url);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000116}
117
Darin Petkov6a5b3222010-07-13 14:55:28 -0700118void OmahaRequestAction::TerminateProcessing() {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000119 http_fetcher_->TerminateTransfer();
120}
121
122// We just store the response in the buffer. Once we've received all bytes,
123// we'll look in the buffer and decide what to do.
Darin Petkov6a5b3222010-07-13 14:55:28 -0700124void OmahaRequestAction::ReceivedBytes(HttpFetcher *fetcher,
125 const char* bytes,
126 int length) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000127 response_buffer_.reserve(response_buffer_.size() + length);
128 response_buffer_.insert(response_buffer_.end(), bytes, bytes + length);
129}
130
131namespace {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000132// If non-NULL response, caller is responsible for calling xmlXPathFreeObject()
133// on the returned object.
134// This code is roughly based on the libxml tutorial at:
135// http://xmlsoft.org/tutorial/apd.html
136xmlXPathObject* GetNodeSet(xmlDoc* doc, const xmlChar* xpath,
137 const xmlChar* ns, const xmlChar* ns_url) {
138 xmlXPathObject* result = NULL;
139
140 scoped_ptr_malloc<xmlXPathContext, ScopedPtrXmlXPathContextFree> context(
141 xmlXPathNewContext(doc));
142 if (!context.get()) {
143 LOG(ERROR) << "xmlXPathNewContext() returned NULL";
144 return NULL;
145 }
146 if (xmlXPathRegisterNs(context.get(), ns, ns_url) < 0) {
147 LOG(ERROR) << "xmlXPathRegisterNs() returned error";
148 return NULL;
149 }
150
151 result = xmlXPathEvalExpression(xpath, context.get());
152
153 if (result == NULL) {
154 LOG(ERROR) << "xmlXPathEvalExpression returned error";
155 return NULL;
156 }
157 if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
158 LOG(INFO) << "xpath not found in doc";
159 xmlXPathFreeObject(result);
160 return NULL;
161 }
162 return result;
163}
164
165// Returns the string value of a named attribute on a node, or empty string
166// if no such node exists. If the attribute exists and has a value of
167// empty string, there's no way to distinguish that from the attribute
168// not existing.
169string XmlGetProperty(xmlNode* node, const char* name) {
170 if (!xmlHasProp(node, ConstXMLStr(name)))
171 return "";
172 scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str(
173 xmlGetProp(node, ConstXMLStr(name)));
174 string ret(reinterpret_cast<const char *>(str.get()));
175 return ret;
176}
177
178// Parses a 64 bit base-10 int from a string and returns it. Returns 0
179// on error. If the string contains "0", that's indistinguishable from
180// error.
181off_t ParseInt(const string& str) {
182 off_t ret = 0;
Andrew de los Reyes08c4e272010-04-15 14:02:17 -0700183 int rc = sscanf(str.c_str(), "%" PRIi64, &ret);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000184 if (rc < 1) {
185 // failure
186 return 0;
187 }
188 return ret;
189}
190} // namespace {}
191
192// If the transfer was successful, this uses libxml2 to parse the response
193// and fill in the appropriate fields of the output object. Also, notifies
194// the processor that we're done.
Darin Petkov6a5b3222010-07-13 14:55:28 -0700195void OmahaRequestAction::TransferComplete(HttpFetcher *fetcher,
196 bool successful) {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000197 ScopedActionCompleter completer(processor_, this);
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700198 LOG(INFO) << "Update check response: " << string(response_buffer_.begin(),
199 response_buffer_.end());
200 if (!successful) {
201 LOG(ERROR) << "Update check network transfer failed.";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000202 return;
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700203 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000204 if (!HasOutputPipe()) {
205 // Just set success to whether or not the http transfer succeeded,
206 // which must be true at this point in the code.
207 completer.set_success(true);
208 return;
209 }
210
211 // parse our response and fill the fields in the output object
212 scoped_ptr_malloc<xmlDoc, ScopedPtrXmlDocFree> doc(
213 xmlParseMemory(&response_buffer_[0], response_buffer_.size()));
214 if (!doc.get()) {
215 LOG(ERROR) << "Omaha response not valid XML";
216 return;
217 }
218
219 static const char* kNamespace("x");
220 static const char* kUpdatecheckNodeXpath("/x:gupdate/x:app/x:updatecheck");
221 static const char* kNsUrl("http://www.google.com/update2/response");
222
223 scoped_ptr_malloc<xmlXPathObject, ScopedPtrXmlXPathObjectFree>
224 xpath_nodeset(GetNodeSet(doc.get(),
225 ConstXMLStr(kUpdatecheckNodeXpath),
226 ConstXMLStr(kNamespace),
227 ConstXMLStr(kNsUrl)));
228 if (!xpath_nodeset.get()) {
229 return;
230 }
231 xmlNodeSet* nodeset = xpath_nodeset->nodesetval;
232 CHECK(nodeset) << "XPath missing NodeSet";
233 CHECK_GE(nodeset->nodeNr, 1);
234
235 xmlNode* updatecheck_node = nodeset->nodeTab[0];
236 // get status
237 if (!xmlHasProp(updatecheck_node, ConstXMLStr("status"))) {
238 LOG(ERROR) << "Response missing status";
239 return;
240 }
241
242 const string status(XmlGetProperty(updatecheck_node, "status"));
Darin Petkov6a5b3222010-07-13 14:55:28 -0700243 OmahaResponse output_object;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000244 if (status == "noupdate") {
245 LOG(INFO) << "No update.";
246 output_object.update_exists = false;
247 SetOutputObject(output_object);
248 completer.set_success(true);
249 return;
250 }
251
252 if (status != "ok") {
253 LOG(ERROR) << "Unknown status: " << status;
254 return;
255 }
256
257 // In best-effort fashion, fetch the rest of the expected attributes
258 // from the updatecheck node, then return the object
259 output_object.update_exists = true;
260 completer.set_success(true);
261
262 output_object.display_version =
263 XmlGetProperty(updatecheck_node, "DisplayVersion");
264 output_object.codebase = XmlGetProperty(updatecheck_node, "codebase");
265 output_object.more_info_url = XmlGetProperty(updatecheck_node, "MoreInfo");
266 output_object.hash = XmlGetProperty(updatecheck_node, "hash");
267 output_object.size = ParseInt(XmlGetProperty(updatecheck_node, "size"));
268 output_object.needs_admin =
269 XmlGetProperty(updatecheck_node, "needsadmin") == "true";
270 output_object.prompt = XmlGetProperty(updatecheck_node, "Prompt") == "true";
271 SetOutputObject(output_object);
272 return;
273}
274
275}; // namespace chromeos_update_engine