Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 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_OMAHA_REQUEST_BUILDER_XML_H_ |
| 18 | #define UPDATE_ENGINE_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/omaha_response.h" |
| 37 | #include "update_engine/system_state.h" |
| 38 | |
Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 39 | namespace chromeos_update_engine { |
| 40 | |
| 41 | extern const int kNeverPinged; |
Jae Hoon Kim | 5fc00a2 | 2020-01-08 20:15:42 -0800 | [diff] [blame] | 42 | extern const char kNoVersion[]; |
Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 43 | |
| 44 | // This struct encapsulates the Omaha event information. For a |
| 45 | // complete list of defined event types and results, see |
| 46 | // http://code.google.com/p/omaha/wiki/ServerProtocol#event |
| 47 | struct OmahaEvent { |
| 48 | // The Type values correspond to EVENT_TYPE values of Omaha. |
| 49 | enum Type { |
| 50 | kTypeUnknown = 0, |
| 51 | kTypeDownloadComplete = 1, |
| 52 | kTypeInstallComplete = 2, |
| 53 | kTypeUpdateComplete = 3, |
| 54 | kTypeUpdateDownloadStarted = 13, |
| 55 | kTypeUpdateDownloadFinished = 14, |
| 56 | // Chromium OS reserved type sent after the first reboot following an update |
| 57 | // completed. |
| 58 | kTypeRebootedAfterUpdate = 54, |
| 59 | }; |
| 60 | |
| 61 | // The Result values correspond to EVENT_RESULT values of Omaha. |
| 62 | enum Result { |
| 63 | kResultError = 0, |
| 64 | kResultSuccess = 1, |
| 65 | kResultUpdateDeferred = 9, // When we ignore/defer updates due to policy. |
| 66 | }; |
| 67 | |
| 68 | OmahaEvent() |
| 69 | : type(kTypeUnknown), |
| 70 | result(kResultError), |
| 71 | error_code(ErrorCode::kError) {} |
| 72 | explicit OmahaEvent(Type in_type) |
| 73 | : type(in_type), |
| 74 | result(kResultSuccess), |
| 75 | error_code(ErrorCode::kSuccess) {} |
| 76 | OmahaEvent(Type in_type, Result in_result, ErrorCode in_error_code) |
| 77 | : type(in_type), result(in_result), error_code(in_error_code) {} |
| 78 | |
| 79 | Type type; |
| 80 | Result result; |
| 81 | ErrorCode error_code; |
| 82 | }; |
| 83 | |
| 84 | struct OmahaAppData { |
| 85 | std::string id; |
| 86 | std::string version; |
| 87 | std::string product_components; |
Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 88 | bool skip_update; |
Jae Hoon Kim | 37d1537 | 2020-01-08 18:11:26 -0800 | [diff] [blame] | 89 | bool is_dlc; |
Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | // Encodes XML entities in a given string. Input must be ASCII-7 valid. If |
| 93 | // the input is invalid, the default value is used instead. |
| 94 | std::string XmlEncodeWithDefault(const std::string& input, |
Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 95 | const std::string& default_value = ""); |
Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 96 | |
| 97 | // Escapes text so it can be included as character data and attribute |
| 98 | // values. The |input| string must be valid ASCII-7, no UTF-8 supported. |
| 99 | // Returns whether the |input| was valid and escaped properly in |output|. |
| 100 | bool XmlEncode(const std::string& input, std::string* output); |
| 101 | |
Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 102 | // Returns a boolean based on examining each character on whether it's a valid |
| 103 | // component (meaning all characters are an alphanum excluding '-', '_', '.'). |
Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 104 | bool IsValidComponentID(const std::string& id); |
| 105 | |
Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 106 | class OmahaRequestBuilder { |
| 107 | public: |
| 108 | OmahaRequestBuilder() = default; |
| 109 | virtual ~OmahaRequestBuilder() = default; |
Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 110 | |
Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 111 | virtual std::string GetRequest() const = 0; |
Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 112 | |
Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 113 | private: |
| 114 | DISALLOW_COPY_AND_ASSIGN(OmahaRequestBuilder); |
| 115 | }; |
| 116 | |
| 117 | class OmahaRequestBuilderXml : OmahaRequestBuilder { |
| 118 | public: |
| 119 | OmahaRequestBuilderXml(const OmahaEvent* event, |
| 120 | OmahaRequestParams* params, |
| 121 | bool ping_only, |
| 122 | bool include_ping, |
| 123 | int ping_active_days, |
| 124 | int ping_roll_call_days, |
| 125 | int install_date_in_days, |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 126 | PrefsInterface* prefs, |
| 127 | const std::string& session_id) |
Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 128 | : event_(event), |
| 129 | params_(params), |
| 130 | ping_only_(ping_only), |
| 131 | include_ping_(include_ping), |
| 132 | ping_active_days_(ping_active_days), |
| 133 | ping_roll_call_days_(ping_roll_call_days), |
| 134 | install_date_in_days_(install_date_in_days), |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 135 | prefs_(prefs), |
| 136 | session_id_(session_id) {} |
Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 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: |
Jae Hoon Kim | 37d1537 | 2020-01-08 18:11:26 -0800 | [diff] [blame] | 144 | FRIEND_TEST(OmahaRequestBuilderXmlTest, PlatformGetAppTest); |
| 145 | FRIEND_TEST(OmahaRequestBuilderXmlTest, DlcGetAppTest); |
| 146 | |
Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 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 | // The skip_updatecheck argument if set to true will omit the emission of |
| 162 | // the updatecheck xml tag in the body of the <app> element. |
| 163 | std::string GetAppBody(bool skip_updatecheck) const; |
| 164 | |
| 165 | // Returns the cohort* argument to include in the <app> tag for the passed |
| 166 | // |arg_name| and |prefs_key|, if any. The return value is suitable to |
| 167 | // concatenate to the list of arguments and includes a space at the end. |
| 168 | std::string GetCohortArg(const std::string arg_name, |
Askar Aitzhan | 18fff84 | 2019-06-21 23:24:37 +0200 | [diff] [blame] | 169 | const std::string prefs_key, |
| 170 | const std::string override_value = "") const; |
Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 171 | |
| 172 | // Returns an XML ping element if any of the elapsed days need to be |
| 173 | // sent, or an empty string otherwise. |
| 174 | std::string GetPing() const; |
| 175 | |
| 176 | const OmahaEvent* event_; |
| 177 | OmahaRequestParams* params_; |
| 178 | bool ping_only_; |
| 179 | bool include_ping_; |
| 180 | int ping_active_days_; |
| 181 | int ping_roll_call_days_; |
| 182 | int install_date_in_days_; |
| 183 | PrefsInterface* prefs_; |
Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 184 | std::string session_id_; |
Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 185 | |
| 186 | DISALLOW_COPY_AND_ASSIGN(OmahaRequestBuilderXml); |
| 187 | }; |
Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 188 | |
| 189 | } // namespace chromeos_update_engine |
| 190 | |
| 191 | #endif // UPDATE_ENGINE_OMAHA_REQUEST_BUILDER_XML_H_ |