blob: 98ff3dc9bffd6355e5cecf4e00840f47cdeed3ba [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
5#include "update_engine/update_check_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
21const char* const UpdateCheckParams::kAppId(
adlr@google.comc98a7ed2009-12-04 18:54:03 +000022 "{87efface-864d-49a5-9bb3-4b050a7c227a}");
rspangler@google.com49fdf182009-10-10 00:57:34 +000023const char* const UpdateCheckParams::kOsPlatform("Chrome OS");
24const char* const UpdateCheckParams::kOsVersion("Indy");
Andrew de los Reyesf9714432010-05-04 10:21:23 -070025const char* const UpdateCheckParams::kUpdateUrl(
26 "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
62// Returns a properly formatted omaha request for an update check
63string FormatRequest(const UpdateCheckParams& params) {
64 return string("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
65 "<o:gupdate xmlns:o=\"http://www.google.com/update2/request\" "
Andrew de los Reyes73520672010-05-10 13:44:58 -070066 "version=\"" + XmlEncode(kGupdateVersion) + "\" "
67 "updaterversion=\"" + XmlEncode(kGupdateVersion) + "\" "
68 "protocol=\"2.0\" "
rspangler@google.com49fdf182009-10-10 00:57:34 +000069 "machineid=\"") + XmlEncode(params.machine_id) + "\" ismachine=\"1\" "
70 "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=\"" +
77 XmlEncode(params.app_track) + "\">\n"
78 " <o:ping active=\"0\"></o:ping>\n"
79 " <o:updatecheck></o:updatecheck>\n"
80 " </o:app>\n"
81 "</o:gupdate>\n";
82}
83} // namespace {}
84
85// Encodes XML entities in a given string with libxml2. input must be
86// UTF-8 formatted. Output will be UTF-8 formatted.
87string XmlEncode(const string& input) {
88// // TODO(adlr): if allocating a new xmlDoc each time is taking up too much
89// // cpu, considering creating one and caching it.
90// scoped_ptr_malloc<xmlDoc, ScopedPtrXmlDocFree> xml_doc(
91// xmlNewDoc(ConstXMLStr("1.0")));
92// if (!xml_doc.get()) {
93// LOG(ERROR) << "Unable to create xmlDoc";
94// return "";
95// }
96 scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str(
97 xmlEncodeEntitiesReentrant(NULL, ConstXMLStr(input.c_str())));
98 return string(reinterpret_cast<const char *>(str.get()));
99}
100
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000101UpdateCheckAction::UpdateCheckAction(HttpFetcher* http_fetcher)
102 : http_fetcher_(http_fetcher) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +0000103
104UpdateCheckAction::~UpdateCheckAction() {}
105
106void UpdateCheckAction::PerformAction() {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000107 CHECK(HasInputObject());
108 params_ = GetInputObject();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000109 http_fetcher_->set_delegate(this);
110 string request_post(FormatRequest(params_));
111 http_fetcher_->SetPostData(request_post.data(), request_post.size());
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700112 LOG(INFO) << "Checking for update at " << params_.update_url;
113 LOG(INFO) << "Request: " << request_post;
Andrew de los Reyesf9714432010-05-04 10:21:23 -0700114 http_fetcher_->BeginTransfer(params_.update_url);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000115}
116
117void UpdateCheckAction::TerminateProcessing() {
118 http_fetcher_->TerminateTransfer();
119}
120
121// We just store the response in the buffer. Once we've received all bytes,
122// we'll look in the buffer and decide what to do.
123void UpdateCheckAction::ReceivedBytes(HttpFetcher *fetcher,
124 const char* bytes,
125 int length) {
126 response_buffer_.reserve(response_buffer_.size() + length);
127 response_buffer_.insert(response_buffer_.end(), bytes, bytes + length);
128}
129
130namespace {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000131// If non-NULL response, caller is responsible for calling xmlXPathFreeObject()
132// on the returned object.
133// This code is roughly based on the libxml tutorial at:
134// http://xmlsoft.org/tutorial/apd.html
135xmlXPathObject* GetNodeSet(xmlDoc* doc, const xmlChar* xpath,
136 const xmlChar* ns, const xmlChar* ns_url) {
137 xmlXPathObject* result = NULL;
138
139 scoped_ptr_malloc<xmlXPathContext, ScopedPtrXmlXPathContextFree> context(
140 xmlXPathNewContext(doc));
141 if (!context.get()) {
142 LOG(ERROR) << "xmlXPathNewContext() returned NULL";
143 return NULL;
144 }
145 if (xmlXPathRegisterNs(context.get(), ns, ns_url) < 0) {
146 LOG(ERROR) << "xmlXPathRegisterNs() returned error";
147 return NULL;
148 }
149
150 result = xmlXPathEvalExpression(xpath, context.get());
151
152 if (result == NULL) {
153 LOG(ERROR) << "xmlXPathEvalExpression returned error";
154 return NULL;
155 }
156 if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
157 LOG(INFO) << "xpath not found in doc";
158 xmlXPathFreeObject(result);
159 return NULL;
160 }
161 return result;
162}
163
164// Returns the string value of a named attribute on a node, or empty string
165// if no such node exists. If the attribute exists and has a value of
166// empty string, there's no way to distinguish that from the attribute
167// not existing.
168string XmlGetProperty(xmlNode* node, const char* name) {
169 if (!xmlHasProp(node, ConstXMLStr(name)))
170 return "";
171 scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str(
172 xmlGetProp(node, ConstXMLStr(name)));
173 string ret(reinterpret_cast<const char *>(str.get()));
174 return ret;
175}
176
177// Parses a 64 bit base-10 int from a string and returns it. Returns 0
178// on error. If the string contains "0", that's indistinguishable from
179// error.
180off_t ParseInt(const string& str) {
181 off_t ret = 0;
Andrew de los Reyes08c4e272010-04-15 14:02:17 -0700182 int rc = sscanf(str.c_str(), "%" PRIi64, &ret);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000183 if (rc < 1) {
184 // failure
185 return 0;
186 }
187 return ret;
188}
189} // namespace {}
190
191// If the transfer was successful, this uses libxml2 to parse the response
192// and fill in the appropriate fields of the output object. Also, notifies
193// the processor that we're done.
194void UpdateCheckAction::TransferComplete(HttpFetcher *fetcher,
195 bool successful) {
196 ScopedActionCompleter completer(processor_, this);
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700197 LOG(INFO) << "Update check response: " << string(response_buffer_.begin(),
198 response_buffer_.end());
199 if (!successful) {
200 LOG(ERROR) << "Update check network transfer failed.";
rspangler@google.com49fdf182009-10-10 00:57:34 +0000201 return;
Andrew de los Reyesf98bff82010-05-06 13:33:25 -0700202 }
rspangler@google.com49fdf182009-10-10 00:57:34 +0000203 if (!HasOutputPipe()) {
204 // Just set success to whether or not the http transfer succeeded,
205 // which must be true at this point in the code.
206 completer.set_success(true);
207 return;
208 }
209
210 // parse our response and fill the fields in the output object
211 scoped_ptr_malloc<xmlDoc, ScopedPtrXmlDocFree> doc(
212 xmlParseMemory(&response_buffer_[0], response_buffer_.size()));
213 if (!doc.get()) {
214 LOG(ERROR) << "Omaha response not valid XML";
215 return;
216 }
217
218 static const char* kNamespace("x");
219 static const char* kUpdatecheckNodeXpath("/x:gupdate/x:app/x:updatecheck");
220 static const char* kNsUrl("http://www.google.com/update2/response");
221
222 scoped_ptr_malloc<xmlXPathObject, ScopedPtrXmlXPathObjectFree>
223 xpath_nodeset(GetNodeSet(doc.get(),
224 ConstXMLStr(kUpdatecheckNodeXpath),
225 ConstXMLStr(kNamespace),
226 ConstXMLStr(kNsUrl)));
227 if (!xpath_nodeset.get()) {
228 return;
229 }
230 xmlNodeSet* nodeset = xpath_nodeset->nodesetval;
231 CHECK(nodeset) << "XPath missing NodeSet";
232 CHECK_GE(nodeset->nodeNr, 1);
233
234 xmlNode* updatecheck_node = nodeset->nodeTab[0];
235 // get status
236 if (!xmlHasProp(updatecheck_node, ConstXMLStr("status"))) {
237 LOG(ERROR) << "Response missing status";
238 return;
239 }
240
241 const string status(XmlGetProperty(updatecheck_node, "status"));
242 UpdateCheckResponse output_object;
243 if (status == "noupdate") {
244 LOG(INFO) << "No update.";
245 output_object.update_exists = false;
246 SetOutputObject(output_object);
247 completer.set_success(true);
248 return;
249 }
250
251 if (status != "ok") {
252 LOG(ERROR) << "Unknown status: " << status;
253 return;
254 }
255
256 // In best-effort fashion, fetch the rest of the expected attributes
257 // from the updatecheck node, then return the object
258 output_object.update_exists = true;
259 completer.set_success(true);
260
261 output_object.display_version =
262 XmlGetProperty(updatecheck_node, "DisplayVersion");
263 output_object.codebase = XmlGetProperty(updatecheck_node, "codebase");
264 output_object.more_info_url = XmlGetProperty(updatecheck_node, "MoreInfo");
265 output_object.hash = XmlGetProperty(updatecheck_node, "hash");
266 output_object.size = ParseInt(XmlGetProperty(updatecheck_node, "size"));
267 output_object.needs_admin =
268 XmlGetProperty(updatecheck_node, "needsadmin") == "true";
269 output_object.prompt = XmlGetProperty(updatecheck_node, "Prompt") == "true";
270 SetOutputObject(output_object);
271 return;
272}
273
274}; // namespace chromeos_update_engine