blob: 106400b90c280d57bac8ffe4ff60f2b3c9506e45 [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 Reyesf9714432010-05-04 10:21:23 -0700110 http_fetcher_->BeginTransfer(params_.update_url);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000111}
112
113void UpdateCheckAction::TerminateProcessing() {
114 http_fetcher_->TerminateTransfer();
115}
116
117// We just store the response in the buffer. Once we've received all bytes,
118// we'll look in the buffer and decide what to do.
119void UpdateCheckAction::ReceivedBytes(HttpFetcher *fetcher,
120 const char* bytes,
121 int length) {
122 response_buffer_.reserve(response_buffer_.size() + length);
123 response_buffer_.insert(response_buffer_.end(), bytes, bytes + length);
124}
125
126namespace {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000127// If non-NULL response, caller is responsible for calling xmlXPathFreeObject()
128// on the returned object.
129// This code is roughly based on the libxml tutorial at:
130// http://xmlsoft.org/tutorial/apd.html
131xmlXPathObject* GetNodeSet(xmlDoc* doc, const xmlChar* xpath,
132 const xmlChar* ns, const xmlChar* ns_url) {
133 xmlXPathObject* result = NULL;
134
135 scoped_ptr_malloc<xmlXPathContext, ScopedPtrXmlXPathContextFree> context(
136 xmlXPathNewContext(doc));
137 if (!context.get()) {
138 LOG(ERROR) << "xmlXPathNewContext() returned NULL";
139 return NULL;
140 }
141 if (xmlXPathRegisterNs(context.get(), ns, ns_url) < 0) {
142 LOG(ERROR) << "xmlXPathRegisterNs() returned error";
143 return NULL;
144 }
145
146 result = xmlXPathEvalExpression(xpath, context.get());
147
148 if (result == NULL) {
149 LOG(ERROR) << "xmlXPathEvalExpression returned error";
150 return NULL;
151 }
152 if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
153 LOG(INFO) << "xpath not found in doc";
154 xmlXPathFreeObject(result);
155 return NULL;
156 }
157 return result;
158}
159
160// Returns the string value of a named attribute on a node, or empty string
161// if no such node exists. If the attribute exists and has a value of
162// empty string, there's no way to distinguish that from the attribute
163// not existing.
164string XmlGetProperty(xmlNode* node, const char* name) {
165 if (!xmlHasProp(node, ConstXMLStr(name)))
166 return "";
167 scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str(
168 xmlGetProp(node, ConstXMLStr(name)));
169 string ret(reinterpret_cast<const char *>(str.get()));
170 return ret;
171}
172
173// Parses a 64 bit base-10 int from a string and returns it. Returns 0
174// on error. If the string contains "0", that's indistinguishable from
175// error.
176off_t ParseInt(const string& str) {
177 off_t ret = 0;
Andrew de los Reyes08c4e272010-04-15 14:02:17 -0700178 int rc = sscanf(str.c_str(), "%" PRIi64, &ret);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000179 if (rc < 1) {
180 // failure
181 return 0;
182 }
183 return ret;
184}
185} // namespace {}
186
187// If the transfer was successful, this uses libxml2 to parse the response
188// and fill in the appropriate fields of the output object. Also, notifies
189// the processor that we're done.
190void UpdateCheckAction::TransferComplete(HttpFetcher *fetcher,
191 bool successful) {
192 ScopedActionCompleter completer(processor_, this);
193 if (!successful)
194 return;
195 if (!HasOutputPipe()) {
196 // Just set success to whether or not the http transfer succeeded,
197 // which must be true at this point in the code.
198 completer.set_success(true);
199 return;
200 }
201
202 // parse our response and fill the fields in the output object
203 scoped_ptr_malloc<xmlDoc, ScopedPtrXmlDocFree> doc(
204 xmlParseMemory(&response_buffer_[0], response_buffer_.size()));
205 if (!doc.get()) {
206 LOG(ERROR) << "Omaha response not valid XML";
207 return;
208 }
209
210 static const char* kNamespace("x");
211 static const char* kUpdatecheckNodeXpath("/x:gupdate/x:app/x:updatecheck");
212 static const char* kNsUrl("http://www.google.com/update2/response");
213
214 scoped_ptr_malloc<xmlXPathObject, ScopedPtrXmlXPathObjectFree>
215 xpath_nodeset(GetNodeSet(doc.get(),
216 ConstXMLStr(kUpdatecheckNodeXpath),
217 ConstXMLStr(kNamespace),
218 ConstXMLStr(kNsUrl)));
219 if (!xpath_nodeset.get()) {
220 return;
221 }
222 xmlNodeSet* nodeset = xpath_nodeset->nodesetval;
223 CHECK(nodeset) << "XPath missing NodeSet";
224 CHECK_GE(nodeset->nodeNr, 1);
225
226 xmlNode* updatecheck_node = nodeset->nodeTab[0];
227 // get status
228 if (!xmlHasProp(updatecheck_node, ConstXMLStr("status"))) {
229 LOG(ERROR) << "Response missing status";
230 return;
231 }
232
233 const string status(XmlGetProperty(updatecheck_node, "status"));
234 UpdateCheckResponse output_object;
235 if (status == "noupdate") {
236 LOG(INFO) << "No update.";
237 output_object.update_exists = false;
238 SetOutputObject(output_object);
239 completer.set_success(true);
240 return;
241 }
242
243 if (status != "ok") {
244 LOG(ERROR) << "Unknown status: " << status;
245 return;
246 }
247
248 // In best-effort fashion, fetch the rest of the expected attributes
249 // from the updatecheck node, then return the object
250 output_object.update_exists = true;
251 completer.set_success(true);
252
253 output_object.display_version =
254 XmlGetProperty(updatecheck_node, "DisplayVersion");
255 output_object.codebase = XmlGetProperty(updatecheck_node, "codebase");
256 output_object.more_info_url = XmlGetProperty(updatecheck_node, "MoreInfo");
257 output_object.hash = XmlGetProperty(updatecheck_node, "hash");
258 output_object.size = ParseInt(XmlGetProperty(updatecheck_node, "size"));
259 output_object.needs_admin =
260 XmlGetProperty(updatecheck_node, "needsadmin") == "true";
261 output_object.prompt = XmlGetProperty(updatecheck_node, "Prompt") == "true";
262 SetOutputObject(output_object);
263 return;
264}
265
266}; // namespace chromeos_update_engine