| 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 |  | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 17 | #ifndef UPDATE_ENGINE_CROS_OMAHA_REQUEST_BUILDER_XML_H_ | 
|  | 18 | #define UPDATE_ENGINE_CROS_OMAHA_REQUEST_BUILDER_XML_H_ | 
| Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 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" | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 36 | #include "update_engine/cros/omaha_request_params.h" | 
|  | 37 | #include "update_engine/cros/omaha_response.h" | 
| Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 38 |  | 
| Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 39 | namespace chromeos_update_engine { | 
|  | 40 |  | 
| Jae Hoon Kim | 5fc00a2 | 2020-01-08 20:15:42 -0800 | [diff] [blame] | 41 | extern const char kNoVersion[]; | 
| Andrew | e045aef | 2020-01-08 16:29:22 -0800 | [diff] [blame] | 42 | extern const int kPingNeverPinged; | 
|  | 43 | extern const int kPingUnknownValue; | 
|  | 44 | extern const int kPingActiveValue; | 
|  | 45 | extern const int kPingInactiveValue; | 
| Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 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 |  | 
|  | 71 | OmahaEvent() | 
|  | 72 | : type(kTypeUnknown), | 
|  | 73 | result(kResultError), | 
|  | 74 | error_code(ErrorCode::kError) {} | 
|  | 75 | explicit OmahaEvent(Type in_type) | 
|  | 76 | : type(in_type), | 
|  | 77 | result(kResultSuccess), | 
|  | 78 | error_code(ErrorCode::kSuccess) {} | 
|  | 79 | 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; | 
| Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 91 | bool skip_update; | 
| Jae Hoon Kim | 37d1537 | 2020-01-08 18:11:26 -0800 | [diff] [blame] | 92 | bool is_dlc; | 
| Andrew | e045aef | 2020-01-08 16:29:22 -0800 | [diff] [blame] | 93 | OmahaRequestParams::AppParams app_params; | 
| Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 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, | 
| Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 99 | const std::string& default_value = ""); | 
| Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 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 |  | 
| Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 106 | // Returns a boolean based on examining each character on whether it's a valid | 
|  | 107 | // component (meaning all characters are an alphanum excluding '-', '_', '.'). | 
| Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 108 | bool IsValidComponentID(const std::string& id); | 
|  | 109 |  | 
| Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 110 | class OmahaRequestBuilder { | 
|  | 111 | public: | 
|  | 112 | OmahaRequestBuilder() = default; | 
|  | 113 | virtual ~OmahaRequestBuilder() = default; | 
| Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 114 |  | 
| Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 115 | virtual std::string GetRequest() const = 0; | 
| Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 116 |  | 
| Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 117 | private: | 
|  | 118 | DISALLOW_COPY_AND_ASSIGN(OmahaRequestBuilder); | 
|  | 119 | }; | 
|  | 120 |  | 
|  | 121 | class OmahaRequestBuilderXml : OmahaRequestBuilder { | 
|  | 122 | public: | 
|  | 123 | OmahaRequestBuilderXml(const OmahaEvent* event, | 
| Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 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, | 
| Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 129 | const std::string& session_id) | 
| Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 130 | : event_(event), | 
| Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 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), | 
| Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 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. | 
| Andrew | e045aef | 2020-01-08 16:29:22 -0800 | [diff] [blame] | 161 | std::string GetAppBody(const OmahaAppData& app_data) const; | 
| Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 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. | 
| Jae Hoon Kim | e2cac61 | 2020-11-02 18:30:29 -0800 | [diff] [blame] | 166 | std::string GetCohortArg(const std::string& arg_name, | 
|  | 167 | const std::string& prefs_key, | 
|  | 168 | const std::string& override_value = "") const; | 
| Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 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 |  | 
| Andrew | e045aef | 2020-01-08 16:29:22 -0800 | [diff] [blame] | 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 |  | 
| Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 179 | const OmahaEvent* event_; | 
| Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 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_; | 
| Jae Hoon Kim | edb6550 | 2019-06-14 11:52:17 -0700 | [diff] [blame] | 185 | std::string session_id_; | 
| Jae Hoon Kim | b7ee387 | 2019-06-06 14:59:03 -0700 | [diff] [blame] | 186 |  | 
|  | 187 | DISALLOW_COPY_AND_ASSIGN(OmahaRequestBuilderXml); | 
|  | 188 | }; | 
| Amin Hassani | 7fca286 | 2019-03-28 16:09:22 -0700 | [diff] [blame] | 189 |  | 
|  | 190 | }  // namespace chromeos_update_engine | 
|  | 191 |  | 
| Amin Hassani | ec7bc11 | 2020-10-29 16:47:58 -0700 | [diff] [blame] | 192 | #endif  // UPDATE_ENGINE_CROS_OMAHA_REQUEST_BUILDER_XML_H_ |