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