• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 // Copyright (C) 2019 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16 
17 #ifndef UPDATE_ENGINE_CROS_OMAHA_REQUEST_BUILDER_XML_H_
18 #define UPDATE_ENGINE_CROS_OMAHA_REQUEST_BUILDER_XML_H_
19 
20 #include <fcntl.h>
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 
24 #include <map>
25 #include <memory>
26 #include <string>
27 #include <vector>
28 
29 #include <gtest/gtest_prod.h>  // for FRIEND_TEST
30 
31 #include <brillo/secure_blob.h>
32 #include <curl/curl.h>
33 
34 #include "update_engine/common/action.h"
35 #include "update_engine/common/http_fetcher.h"
36 #include "update_engine/cros/omaha_request_params.h"
37 #include "update_engine/cros/omaha_response.h"
38 
39 namespace chromeos_update_engine {
40 
41 extern const char kNoVersion[];
42 extern const int kPingNeverPinged;
43 extern const int kPingUnknownValue;
44 extern const int kPingActiveValue;
45 extern const int kPingInactiveValue;
46 
47 // This struct encapsulates the Omaha event information. For a
48 // complete list of defined event types and results, see
49 // http://code.google.com/p/omaha/wiki/ServerProtocol#event
50 struct OmahaEvent {
51   // The Type values correspond to EVENT_TYPE values of Omaha.
52   enum Type {
53     kTypeUnknown = 0,
54     kTypeDownloadComplete = 1,
55     kTypeInstallComplete = 2,
56     kTypeUpdateComplete = 3,
57     kTypeUpdateDownloadStarted = 13,
58     kTypeUpdateDownloadFinished = 14,
59     // Chromium OS reserved type sent after the first reboot following an update
60     // completed.
61     kTypeRebootedAfterUpdate = 54,
62   };
63 
64   // The Result values correspond to EVENT_RESULT values of Omaha.
65   enum Result {
66     kResultError = 0,
67     kResultSuccess = 1,
68     kResultUpdateDeferred = 9,  // When we ignore/defer updates due to policy.
69   };
70 
OmahaEventOmahaEvent71   OmahaEvent()
72       : type(kTypeUnknown),
73         result(kResultError),
74         error_code(ErrorCode::kError) {}
OmahaEventOmahaEvent75   explicit OmahaEvent(Type in_type)
76       : type(in_type),
77         result(kResultSuccess),
78         error_code(ErrorCode::kSuccess) {}
OmahaEventOmahaEvent79   OmahaEvent(Type in_type, Result in_result, ErrorCode in_error_code)
80       : type(in_type), result(in_result), error_code(in_error_code) {}
81 
82   Type type;
83   Result result;
84   ErrorCode error_code;
85 };
86 
87 struct OmahaAppData {
88   std::string id;
89   std::string version;
90   std::string product_components;
91   bool skip_update;
92   bool is_dlc;
93   OmahaRequestParams::AppParams app_params;
94 };
95 
96 // Encodes XML entities in a given string. Input must be ASCII-7 valid. If
97 // the input is invalid, the default value is used instead.
98 std::string XmlEncodeWithDefault(const std::string& input,
99                                  const std::string& default_value = "");
100 
101 // Escapes text so it can be included as character data and attribute
102 // values. The |input| string must be valid ASCII-7, no UTF-8 supported.
103 // Returns whether the |input| was valid and escaped properly in |output|.
104 bool XmlEncode(const std::string& input, std::string* output);
105 
106 // Returns a boolean based on examining each character on whether it's a valid
107 // component (meaning all characters are an alphanum excluding '-', '_', '.').
108 bool IsValidComponentID(const std::string& id);
109 
110 class OmahaRequestBuilder {
111  public:
112   OmahaRequestBuilder() = default;
113   virtual ~OmahaRequestBuilder() = default;
114 
115   virtual std::string GetRequest() const = 0;
116 
117  private:
118   DISALLOW_COPY_AND_ASSIGN(OmahaRequestBuilder);
119 };
120 
121 class OmahaRequestBuilderXml : OmahaRequestBuilder {
122  public:
OmahaRequestBuilderXml(const OmahaEvent * event,bool ping_only,bool include_ping,int ping_active_days,int ping_roll_call_days,int install_date_in_days,const std::string & session_id)123   OmahaRequestBuilderXml(const OmahaEvent* event,
124                          bool ping_only,
125                          bool include_ping,
126                          int ping_active_days,
127                          int ping_roll_call_days,
128                          int install_date_in_days,
129                          const std::string& session_id)
130       : event_(event),
131         ping_only_(ping_only),
132         include_ping_(include_ping),
133         ping_active_days_(ping_active_days),
134         ping_roll_call_days_(ping_roll_call_days),
135         install_date_in_days_(install_date_in_days),
136         session_id_(session_id) {}
137 
138   ~OmahaRequestBuilderXml() override = default;
139 
140   // Returns an XML that corresponds to the entire Omaha request.
141   std::string GetRequest() const override;
142 
143  private:
144   FRIEND_TEST(OmahaRequestBuilderXmlTest, PlatformGetAppTest);
145   FRIEND_TEST(OmahaRequestBuilderXmlTest, DlcGetAppTest);
146 
147   // Returns an XML that corresponds to the entire <os> node of the Omaha
148   // request based on the member variables.
149   std::string GetOs() const;
150 
151   // Returns an XML that corresponds to all <app> nodes of the Omaha
152   // request based on the given parameters.
153   std::string GetApps() const;
154 
155   // Returns an XML that corresponds to the single <app> node of the Omaha
156   // request based on the given parameters.
157   std::string GetApp(const OmahaAppData& app_data) const;
158 
159   // Returns an XML that goes into the body of the <app> element of the Omaha
160   // request based on the given parameters.
161   std::string GetAppBody(const OmahaAppData& app_data) const;
162 
163   // Returns the cohort* argument to include in the <app> tag for the passed
164   // |arg_name| and |prefs_key|, if any. The return value is suitable to
165   // concatenate to the list of arguments and includes a space at the end.
166   std::string GetCohortArg(const std::string& arg_name,
167                            const std::string& prefs_key,
168                            const std::string& override_value = "") const;
169 
170   // Returns an XML ping element if any of the elapsed days need to be
171   // sent, or an empty string otherwise.
172   std::string GetPing() const;
173 
174   // Returns an XML ping element if any of the elapsed days need to be
175   // sent, or an empty string otherwise.
176   std::string GetPingDateBased(
177       const OmahaRequestParams::AppParams& app_params) const;
178 
179   const OmahaEvent* event_;
180   bool ping_only_;
181   bool include_ping_;
182   int ping_active_days_;
183   int ping_roll_call_days_;
184   int install_date_in_days_;
185   std::string session_id_;
186 
187   DISALLOW_COPY_AND_ASSIGN(OmahaRequestBuilderXml);
188 };
189 
190 }  // namespace chromeos_update_engine
191 
192 #endif  // UPDATE_ENGINE_CROS_OMAHA_REQUEST_BUILDER_XML_H_
193