-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathomaha_request_action.sh
executable file
·277 lines (247 loc) · 8.73 KB
/
omaha_request_action.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/bin/bash
# 2019 (c) Muntashir Al-Islam. All rights reserved.
# This file is converted from the original omaha_request_action.cc
# located at https://chromium.googlesource.com/chromiumos/platform/update_engine/+/refs/heads/master/omaha_request_action.cc
# fetched at 28 Jun 2019
# Get script directory
SCRIPT_DIR="$(dirname "$(readlink -f "$0")")"
[ command -v debug >/dev/null 2>&1 ] || source "${SCRIPT_DIR}/debug_utils.sh"
# List of custom pair tags that we interpret in the Omaha Response:
kTagDeadline="deadline"
kTagDisablePayloadBackoff="DisablePayloadBackoff"
kTagVersion="version"
# Deprecated: "IsDelta"
kTagIsDeltaPayload="IsDeltaPayload"
kTagMaxFailureCountPerUrl="MaxFailureCountPerUrl"
kTagMaxDaysToScatter="MaxDaysToScatter"
# Deprecated: "ManifestSignatureRsa"
# Deprecated: "ManifestSize"
kTagMetadataSignatureRsa="MetadataSignatureRsa"
kTagMetadataSize="MetadataSize"
kTagMoreInfo="MoreInfo"
# Deprecated: "NeedsAdmin"
kTagPrompt="Prompt"
kTagSha256="sha256"
kTagDisableP2PForDownloading="DisableP2PForDownloading"
kTagDisableP2PForSharing="DisableP2PForSharing"
kTagPublicKeyRsa="PublicKeyRsa"
# Global var
kGupdateVersion="ChromeOSUpdateEngine-0.1.0.0"
#
# GetOsXml
#
function GetOsXml {
echo " <os version=\"${os_version_}\" platform=\"${os_platform_}\" sp=\"${os_sp_}\"></os>"
}
#
# GetAppXml
#
function GetAppXml {
local app_body="<ping active=\"1\" a=\"-1\" r=\"-1\"></ping>
<updatecheck targetversionprefix=\"\"></updatecheck>" # For now, I'm getting tired
local app_versions="version=\"${app_version_}\"" # The conditional in the original code isn't use since it pw doesn't work
local app_channels="track=\"${download_channel_}\""
if ! [ "${current_channel_}" == "${download_channel_}" ]; then
app_channels="${app_channels} from_track=\"${current_channel_}\" "
fi
local install_date_in_days_str= # installdate="%d" or nothing
cat <<EOL
<app appid="$(OmahaRequestDeviceParams_GetAppId)" ${app_versions} ${app_channels} lang="${app_lang_}" board="${os_board_}" hardware_class="${hwid_}" delta_okay="${delta_okay_}" fw_version="${fw_version_}" ec_version="${ec_version_}" ${install_date_in_days_str}>
${app_body}
</app>
EOL
}
#
# GetRequestXml
#
function GetRequestXml {
local os_xml=$(GetOsXml)
local app_xml=$(GetAppXml)
local install_source=
if [ ${interactive_} ]; then
install_source='ondemandupdate'
else
install_source='scheduler'
fi
cat <<EOL
<?xml version="1.0" encoding="UTF-8"?>
<request protocol="3.0" version="${kGupdateVersion}" updaterversion="${kGupdateVersion}" installsource="${install_source}" ismachine="1">
${os_xml}
${app_xml}
</request>
EOL
}
response='/tmp/response.xml'
# These values are inside output_object, most of them doesn't need but still kept
ORA_payload_urls=()
ORA_package_name= # Not included in the output_object, but required for us
ORA_version=
ORA_hash=
ORA_update_exists=
ORA_size=
ORA_more_info_url=
ORA_metadata_size=
ORA_metadata_signature=
ORA_prompt=
ORA_deadline=
ORA_max_days_to_scatter=
ORA_disable_p2p_for_downloading=
ORA_disable_p2p_for_sharing=
ORA_public_key_rsa=
ORA_max_failure_count_per_url=
ORA_is_delta_payload=
ORA_disable_payload_backoff=
#
# OmahaRequestAction::PerformAction
#
function OmahaRequestAction_PerformAction {
curl -sL -X POST --data "$(GetRequestXml)" "${update_url_}" -o "${response}"
if [ $? -ne 0 ]; then
echo_stderr "Omaha request network transfer failed."
return 1
fi
return 0
}
#
# OmahaRequestAction::ParseStatus
#
function OmahaRequestAction_ParseStatus {
local status=`/usr/bin/xmllint --xpath 'string(//updatecheck/@status)' "${response}"`
if [ "${status}" == "noupdate" ]; then
echo_stderr "No update available."
ORA_update_exists=false
return 1
fi
if [ "${status}" != "ok" ]; then
echo_stderr "Unknown Omaha response status: ${status}"
return 1
fi
return 0
}
#
# OmahaRequestAction::ParseUrls
#
function OmahaRequestAction_ParseUrls {
local kUpdateUrlNodeXPath='/response/app/updatecheck/urls/url'
/usr/bin/xmllint --xpath "${kUpdateUrlNodeXPath}" "${response}" > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo_stderr "XPath missing ${kUpdateUrlNodeXPath}"
return 1
fi
local c_urls=`/usr/bin/xmllint --xpath "count(${kUpdateUrlNodeXPath})" "${response}" 2> /dev/null`
for (( i=1; i<=c_urls; i++ )); do
local url=`/usr/bin/xmllint --xpath "string(${kUpdateUrlNodeXPath}[${i}]/@codebase)" "${response}" 2> /dev/null`
if [ "${url}" == "" ]; then
echo_stderr "Omaha Response URL has empty codebase"
return 1
fi
ORA_payload_urls+=("${url}")
done
return 0
}
#
# OmahaRequestAction::ParsePackage
#
function OmahaRequestAction_ParsePackage {
local kPackageNodeXPath='/response/app/updatecheck/manifest/packages/package'
/usr/bin/xmllint --xpath "${kPackageNodeXPath}" "${response}" > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo_stderr "XPath missing ${kPackageNodeXPath}"
return 1
fi
ORA_package_name=`/usr/bin/xmllint --xpath "string(${kPackageNodeXPath}[1]/@name)" "${response}" 2> /dev/null`
if [ "${ORA_package_name}" == "" ]; then
echo_stderr "Omaha Response has empty package name"
return 1
fi
# Append package name
local c_urls=${#ORA_payload_urls[@]}
for (( i=0; i<c_urls; i++ )); do
ORA_payload_urls[${i}]="${ORA_payload_urls[${i}]}${ORA_package_name}"
done
ORA_size=`/usr/bin/xmllint --xpath "string(${kPackageNodeXPath}[1]/@size)" "${response}" 2> /dev/null`
# NOTE: hash_sha256 attribute under package is the output of sha256sum <file>
return 0
}
#
# OmahaRequestAction::ParseParams
#
function OmahaRequestAction_ParseParams {
local kManifestNodeXPath='/response/app/updatecheck/manifest'
local kActionNodeXPath='/response/app/updatecheck/manifest/actions/action'
/usr/bin/xmllint --xpath "${kManifestNodeXPath}" "${response}" > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo_stderr "XPath missing ${kManifestNodeXPath}"
return 1
fi
ORA_version=`/usr/bin/xmllint --xpath "string(${kManifestNodeXPath}/@${kTagVersion})" "${response}" 2> /dev/null`
if [ "${ORA_version}" == "" ]; then
echo_stderr "Omaha Response does not have version in manifest!"
return 1
fi
/usr/bin/xmllint --xpath "${kActionNodeXPath}" "${response}" > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo_stderr "XPath missing ${kActionNodeXPath}"
return 1
fi
local c_action=`/usr/bin/xmllint --xpath "count(${kActionNodeXPath})" "${response}" 2> /dev/null`
local postinstall_index=0
for (( i=1; i<=c_action; i++ )); do
local event=`/usr/bin/xmllint --xpath "string(${kActionNodeXPath}[${i}]/@event)" "${response}" 2> /dev/null`
if [ "${event}" == "postinstall" ]; then
postinstall_index=${i}
break
fi
done
if [ ${postinstall_index} -le 0 ]; then
echo_stderr "Omaha Response has no postinstall event action"
return 1
fi
ORA_hash=`/usr/bin/xmllint --xpath "string(${kActionNodeXPath}[${postinstall_index}]/@${kTagSha256})" "${response}" 2> /dev/null`
if [ "${ORA_hash}" == "" ]; then
echo_stderr "Omaha Response has empty sha256 value"
return 1
fi
# TODO: Get the optional properties one by one.
ORA_is_delta_payload=`/usr/bin/xmllint --xpath "string(${kActionNodeXPath}[${postinstall_index}]/@${kTagIsDeltaPayload})" "${response}" 2> /dev/null`
return 0
}
#
# OmahaRequestAction::ParseResponse
#
function OmahaRequestAction_ParseResponse {
/usr/bin/xmllint --xpath '/response/app/updatecheck' "${response}" > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo_stderr "XPath missing UpdateCheck NodeSet"
return 1
fi
# Date time and other not needed things are not included
OmahaRequestAction_ParseStatus || return 1
OmahaRequestAction_ParseUrls || return 1
OmahaRequestAction_ParsePackage || return 1
OmahaRequestAction_ParseParams || return 1
return 0
}
#
# utils::CalculateP2PFileId, Kept it though I may not provide any support for P2P
#
function utils_CalculateP2PFileId {
local encoded_hash=`cat "${ORA_hash}" | base64 2> /dev/null`
echo "cros_update_size_${ORA_size}_hash_${encoded_hash}"
}
#
# OmahaRequestAction::TransferComplete
#
function OmahaRequestAction_TransferComplete {
OmahaRequestAction_PerformAction || return 1 # Get update response, not part of the original method
/usr/bin/xmllint "${response}" > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo_stderr "Omaha response not valid XML"
return 1
fi
# Don't need to check stupid ping values!
OmahaRequestAction_ParseResponse || return 1 # set ORA_ vars
ORA_update_exists=true
# No need to check ShouldIgnoreUpdate()
return 0
}