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