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