blob: a9d303cb293e0d6e8666afe86a5e71702f253624 [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"
6#include <sstream>
7
8#include <libxml/parser.h>
9#include <libxml/xpath.h>
10#include <libxml/xpathInternals.h>
11
adlr@google.comc98a7ed2009-12-04 18:54:03 +000012#include "chromeos/obsolete_logging.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000013#include "update_engine/action_pipe.h"
adlr@google.comc98a7ed2009-12-04 18:54:03 +000014#include "update_engine/utils.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000015
16using std::string;
17
18namespace chromeos_update_engine {
19
20const char* const UpdateCheckParams::kAppId(
adlr@google.comc98a7ed2009-12-04 18:54:03 +000021 "{87efface-864d-49a5-9bb3-4b050a7c227a}");
rspangler@google.com49fdf182009-10-10 00:57:34 +000022const char* const UpdateCheckParams::kOsPlatform("Chrome OS");
23const char* const UpdateCheckParams::kOsVersion("Indy");
24
25namespace {
26
27const string kGupdateVersion("ChromeOSUpdateEngine-0.1.0.0");
28
29// This is handy for passing strings into libxml2
30#define ConstXMLStr(x) (reinterpret_cast<const xmlChar*>(x))
31
32// These are for scoped_ptr_malloc, which is like scoped_ptr, but allows
33// a custom free() function to be specified.
34class ScopedPtrXmlDocFree {
35 public:
36 inline void operator()(void* x) const {
37 xmlFreeDoc(reinterpret_cast<xmlDoc*>(x));
38 }
39};
40class ScopedPtrXmlFree {
41 public:
42 inline void operator()(void* x) const {
43 xmlFree(x);
44 }
45};
46class ScopedPtrXmlXPathObjectFree {
47 public:
48 inline void operator()(void* x) const {
49 xmlXPathFreeObject(reinterpret_cast<xmlXPathObject*>(x));
50 }
51};
52class ScopedPtrXmlXPathContextFree {
53 public:
54 inline void operator()(void* x) const {
55 xmlXPathFreeContext(reinterpret_cast<xmlXPathContext*>(x));
56 }
57};
58
59// Returns a properly formatted omaha request for an update check
60string FormatRequest(const UpdateCheckParams& params) {
61 return string("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
62 "<o:gupdate xmlns:o=\"http://www.google.com/update2/request\" "
63 "version=\"" + XmlEncode(kGupdateVersion) + "\" protocol=\"2.0\" "
64 "machineid=\"") + XmlEncode(params.machine_id) + "\" ismachine=\"1\" "
65 "userid=\"" + XmlEncode(params.user_id) + "\">\n"
66 " <o:os version=\"" + XmlEncode(params.os_version) + "\" platform=\"" +
67 XmlEncode(params.os_platform) + "\" sp=\"" +
68 XmlEncode(params.os_sp) + "\"></o:os>\n"
69 " <o:app appid=\"" + XmlEncode(params.app_id) + "\" version=\"" +
70 XmlEncode(params.app_version) + "\" "
71 "lang=\"" + XmlEncode(params.app_lang) + "\" track=\"" +
72 XmlEncode(params.app_track) + "\">\n"
73 " <o:ping active=\"0\"></o:ping>\n"
74 " <o:updatecheck></o:updatecheck>\n"
75 " </o:app>\n"
76 "</o:gupdate>\n";
77}
78} // namespace {}
79
80// Encodes XML entities in a given string with libxml2. input must be
81// UTF-8 formatted. Output will be UTF-8 formatted.
82string XmlEncode(const string& input) {
83// // TODO(adlr): if allocating a new xmlDoc each time is taking up too much
84// // cpu, considering creating one and caching it.
85// scoped_ptr_malloc<xmlDoc, ScopedPtrXmlDocFree> xml_doc(
86// xmlNewDoc(ConstXMLStr("1.0")));
87// if (!xml_doc.get()) {
88// LOG(ERROR) << "Unable to create xmlDoc";
89// return "";
90// }
91 scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str(
92 xmlEncodeEntitiesReentrant(NULL, ConstXMLStr(input.c_str())));
93 return string(reinterpret_cast<const char *>(str.get()));
94}
95
adlr@google.comc98a7ed2009-12-04 18:54:03 +000096UpdateCheckAction::UpdateCheckAction(HttpFetcher* http_fetcher)
97 : http_fetcher_(http_fetcher) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000098
99UpdateCheckAction::~UpdateCheckAction() {}
100
101void UpdateCheckAction::PerformAction() {
adlr@google.comc98a7ed2009-12-04 18:54:03 +0000102 CHECK(HasInputObject());
103 params_ = GetInputObject();
rspangler@google.com49fdf182009-10-10 00:57:34 +0000104 http_fetcher_->set_delegate(this);
105 string request_post(FormatRequest(params_));
106 http_fetcher_->SetPostData(request_post.data(), request_post.size());
107 http_fetcher_->BeginTransfer("https://tools.google.com/service/update2");
108}
109
110void UpdateCheckAction::TerminateProcessing() {
111 http_fetcher_->TerminateTransfer();
112}
113
114// We just store the response in the buffer. Once we've received all bytes,
115// we'll look in the buffer and decide what to do.
116void UpdateCheckAction::ReceivedBytes(HttpFetcher *fetcher,
117 const char* bytes,
118 int length) {
119 response_buffer_.reserve(response_buffer_.size() + length);
120 response_buffer_.insert(response_buffer_.end(), bytes, bytes + length);
121}
122
123namespace {
rspangler@google.com49fdf182009-10-10 00:57:34 +0000124// If non-NULL response, caller is responsible for calling xmlXPathFreeObject()
125// on the returned object.
126// This code is roughly based on the libxml tutorial at:
127// http://xmlsoft.org/tutorial/apd.html
128xmlXPathObject* GetNodeSet(xmlDoc* doc, const xmlChar* xpath,
129 const xmlChar* ns, const xmlChar* ns_url) {
130 xmlXPathObject* result = NULL;
131
132 scoped_ptr_malloc<xmlXPathContext, ScopedPtrXmlXPathContextFree> context(
133 xmlXPathNewContext(doc));
134 if (!context.get()) {
135 LOG(ERROR) << "xmlXPathNewContext() returned NULL";
136 return NULL;
137 }
138 if (xmlXPathRegisterNs(context.get(), ns, ns_url) < 0) {
139 LOG(ERROR) << "xmlXPathRegisterNs() returned error";
140 return NULL;
141 }
142
143 result = xmlXPathEvalExpression(xpath, context.get());
144
145 if (result == NULL) {
146 LOG(ERROR) << "xmlXPathEvalExpression returned error";
147 return NULL;
148 }
149 if(xmlXPathNodeSetIsEmpty(result->nodesetval)){
150 LOG(INFO) << "xpath not found in doc";
151 xmlXPathFreeObject(result);
152 return NULL;
153 }
154 return result;
155}
156
157// Returns the string value of a named attribute on a node, or empty string
158// if no such node exists. If the attribute exists and has a value of
159// empty string, there's no way to distinguish that from the attribute
160// not existing.
161string XmlGetProperty(xmlNode* node, const char* name) {
162 if (!xmlHasProp(node, ConstXMLStr(name)))
163 return "";
164 scoped_ptr_malloc<xmlChar, ScopedPtrXmlFree> str(
165 xmlGetProp(node, ConstXMLStr(name)));
166 string ret(reinterpret_cast<const char *>(str.get()));
167 return ret;
168}
169
170// Parses a 64 bit base-10 int from a string and returns it. Returns 0
171// on error. If the string contains "0", that's indistinguishable from
172// error.
173off_t ParseInt(const string& str) {
174 off_t ret = 0;
175
176 int rc = sscanf(str.c_str(), "%lld", &ret);
177 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