Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Module containing common autoupdate utilities and protocol dictionaries.""" |
| 6 | |
Don Garrett | fb15e32 | 2016-06-22 02:12:08 | [diff] [blame] | 7 | from __future__ import print_function |
| 8 | |
Amin Hassani | 9e2adae | 2018-03-13 23:19:47 | [diff] [blame] | 9 | import base64 |
| 10 | import binascii |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 11 | import datetime |
| 12 | import os |
| 13 | import time |
| 14 | from xml.dom import minidom |
| 15 | |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 16 | # Update events and result codes. |
| 17 | EVENT_TYPE_UNKNOWN = 0 |
| 18 | EVENT_TYPE_DOWNLOAD_COMPLETE = 1 |
| 19 | EVENT_TYPE_INSTALL_COMPLETE = 2 |
| 20 | EVENT_TYPE_UPDATE_COMPLETE = 3 |
| 21 | EVENT_TYPE_UPDATE_DOWNLOAD_STARTED = 13 |
| 22 | EVENT_TYPE_UPDATE_DOWNLOAD_FINISHED = 14 |
| 23 | |
| 24 | EVENT_RESULT_ERROR = 0 |
| 25 | EVENT_RESULT_SUCCESS = 1 |
| 26 | EVENT_RESULT_SUCCESS_REBOOT = 2 |
| 27 | EVENT_RESULT_UPDATE_DEFERRED = 9 |
| 28 | |
| 29 | |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 30 | # Responses for the various Omaha protocols indexed by the protocol version. |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 31 | # |
| 32 | # Update available responses: |
| 33 | _UPDATE_RESPONSE = {} |
| 34 | _UPDATE_RESPONSE['2.0'] = """<?xml version="1.0" encoding="UTF-8"?> |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 35 | <gupdate xmlns="http://www.google.com/update2/response" protocol="2.0"> |
| 36 | <daystart elapsed_seconds="%(time_elapsed)s"/> |
Amin Hassani | d7a913a | 2018-03-13 22:19:24 | [diff] [blame] | 37 | <app appid="%(appid)s" status="ok"> |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 38 | <ping status="ok"/> |
| 39 | <updatecheck |
Paul Hobbs | 5e7b5a7 | 2017-10-04 18:02:39 | [diff] [blame] | 40 | ChromeOSVersion="999999.0.0" |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 41 | codebase="%(url)s" |
| 42 | hash="%(sha1)s" |
| 43 | sha256="%(sha256)s" |
| 44 | needsadmin="false" |
| 45 | size="%(size)s" |
Gilad Arnold | e96e865 | 2013-05-22 18:36:32 | [diff] [blame] | 46 | IsDeltaPayload="%(is_delta_format)s" |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 47 | status="ok" |
| 48 | %(extra_attr)s/> |
| 49 | </app> |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 50 | </gupdate>""" |
| 51 | _UPDATE_RESPONSE['3.0'] = """<?xml version="1.0" encoding="UTF-8"?> |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 52 | <response protocol="3.0"> |
| 53 | <daystart elapsed_seconds="%(time_elapsed)s"/> |
Amin Hassani | d7a913a | 2018-03-13 22:19:24 | [diff] [blame] | 54 | <app appid="%(appid)s" status="ok"> |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 55 | <ping status="ok"/> |
| 56 | <updatecheck status="ok"> |
| 57 | <urls> |
| 58 | <url codebase="%(codebase)s/"/> |
| 59 | </urls> |
Paul Hobbs | 5e7b5a7 | 2017-10-04 18:02:39 | [diff] [blame] | 60 | <manifest version="999999.0.0"> |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 61 | <packages> |
| 62 | <package hash="%(sha1)s" name="%(filename)s" size="%(size)s" |
Amin Hassani | 9e2adae | 2018-03-13 23:19:47 | [diff] [blame] | 63 | hash_sha256="%(hash_sha256)s" required="true"/> |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 64 | </packages> |
| 65 | <actions> |
| 66 | <action event="postinstall" |
Paul Hobbs | 5e7b5a7 | 2017-10-04 18:02:39 | [diff] [blame] | 67 | ChromeOSVersion="999999.0.0" |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 68 | sha256="%(sha256)s" |
| 69 | needsadmin="false" |
Gilad Arnold | e96e865 | 2013-05-22 18:36:32 | [diff] [blame] | 70 | IsDeltaPayload="%(is_delta_format)s" |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 71 | %(extra_attr)s /> |
| 72 | </actions> |
| 73 | </manifest> |
| 74 | </updatecheck> |
| 75 | </app> |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 76 | </response>""" |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 77 | |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 78 | # No update responses: |
| 79 | _NO_UPDATE_RESPONSE = {} |
| 80 | _NO_UPDATE_RESPONSE['2.0'] = """<?xml version="1.0" encoding="UTF-8"?> |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 81 | <gupdate xmlns="http://www.google.com/update2/response" protocol="2.0"> |
| 82 | <daystart elapsed_seconds="%(time_elapsed)s"/> |
Amin Hassani | d7a913a | 2018-03-13 22:19:24 | [diff] [blame] | 83 | <app appid="%(appid)s" status="ok"> |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 84 | <ping status="ok"/> |
| 85 | <updatecheck status="noupdate"/> |
| 86 | </app> |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 87 | </gupdate>""" |
| 88 | _NO_UPDATE_RESPONSE['3.0'] = """<?xml version="1.0" encoding="UTF-8"?> |
Jay Srinivasan | 0024495 | 2013-03-26 18:05:06 | [diff] [blame] | 89 | <response protocol="3.0"> |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 90 | <daystart elapsed_seconds="%(time_elapsed)s"/> |
Amin Hassani | d7a913a | 2018-03-13 22:19:24 | [diff] [blame] | 91 | <app appid="%(appid)s" status="ok"> |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 92 | <ping status="ok"/> |
| 93 | <updatecheck status="noupdate"/> |
| 94 | </app> |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 95 | </response>""" |
| 96 | |
| 97 | |
| 98 | # Non-update event responses: |
| 99 | _EVENT_RESPONSE = {} |
| 100 | _EVENT_RESPONSE['2.0'] = """<?xml version="1.0" encoding="UTF-8"?> |
| 101 | <gupdate xmlns="http://www.google.com/update2/response" protocol="2.0"> |
| 102 | <daystart elapsed_seconds="%(time_elapsed)s"/> |
Amin Hassani | d7a913a | 2018-03-13 22:19:24 | [diff] [blame] | 103 | <app appid="%(appid)s" status="ok"> |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 104 | <ping status="ok"/> |
| 105 | <event status="ok"/> |
| 106 | </app> |
| 107 | </gupdate>""" |
| 108 | _EVENT_RESPONSE['3.0'] = """<?xml version="1.0" encoding="UTF-8"?> |
| 109 | <response protocol="3.0"> |
| 110 | <daystart elapsed_seconds="%(time_elapsed)s"/> |
Amin Hassani | d7a913a | 2018-03-13 22:19:24 | [diff] [blame] | 111 | <app appid="%(appid)s" status="ok"> |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 112 | <ping status="ok"/> |
| 113 | <event status="ok"/> |
| 114 | </app> |
| 115 | </response>""" |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 116 | |
| 117 | |
| 118 | class UnknownProtocolRequestedException(Exception): |
| 119 | """Raised when an supported protocol is specified.""" |
| 120 | |
| 121 | |
| 122 | def GetSecondsSinceMidnight(): |
| 123 | """Returns the seconds since midnight as a decimal value.""" |
| 124 | now = time.localtime() |
| 125 | return now[3] * 3600 + now[4] * 60 + now[5] |
| 126 | |
| 127 | |
Amin Hassani | d7a913a | 2018-03-13 22:19:24 | [diff] [blame] | 128 | def GetCommonResponseValues(appid): |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 129 | """Returns a dictionary of default values for the response.""" |
| 130 | response_values = {} |
Amin Hassani | d7a913a | 2018-03-13 22:19:24 | [diff] [blame] | 131 | response_values['appid'] = appid |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 132 | response_values['time_elapsed'] = GetSecondsSinceMidnight() |
| 133 | return response_values |
| 134 | |
| 135 | |
| 136 | def GetSubstitutedResponse(response_dict, protocol, response_values): |
| 137 | """Substitutes the protocol-specific response with response_values. |
| 138 | |
| 139 | Args: |
| 140 | response_dict: Canned response messages indexed by protocol. |
| 141 | protocol: client's protocol version from the request Xml. |
| 142 | response_values: Values to be substituted in the canned response. |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 143 | |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 144 | Returns: |
| 145 | Xml string to be passed back to client. |
| 146 | """ |
| 147 | response_xml = response_dict[protocol] % response_values |
| 148 | return response_xml |
| 149 | |
| 150 | |
David Zeuthen | 52ccd01 | 2013-10-31 19:58:26 | [diff] [blame] | 151 | def GetUpdateResponse(sha1, sha256, size, url, is_delta_format, metadata_size, |
Amin Hassani | d7a913a | 2018-03-13 22:19:24 | [diff] [blame] | 152 | signed_metadata_hash, public_key, protocol, appid, |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 153 | critical_update=False): |
| 154 | """Returns a protocol-specific response to the client for a new update. |
| 155 | |
| 156 | Args: |
| 157 | sha1: SHA1 hash of update blob |
| 158 | sha256: SHA256 hash of update blob |
| 159 | size: size of update blob |
| 160 | url: where to find update blob |
| 161 | is_delta_format: true if url refers to a delta payload |
David Zeuthen | 52ccd01 | 2013-10-31 19:58:26 | [diff] [blame] | 162 | metadata_size: the size of the metadata, in bytes. |
| 163 | signed_metadata_hash: the signed metadata hash or None if not signed. |
| 164 | public_key: the public key to transmit to the client or None if no key. |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 165 | protocol: client's protocol version from the request Xml. |
Amin Hassani | d7a913a | 2018-03-13 22:19:24 | [diff] [blame] | 166 | appid: the appid associated with the response. |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 167 | critical_update: whether this is a critical update. |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 168 | |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 169 | Returns: |
| 170 | Xml string to be passed back to client. |
| 171 | """ |
Amin Hassani | d7a913a | 2018-03-13 22:19:24 | [diff] [blame] | 172 | response_values = GetCommonResponseValues(appid) |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 173 | response_values['sha1'] = sha1 |
| 174 | response_values['sha256'] = sha256 |
Amin Hassani | 9e2adae | 2018-03-13 23:19:47 | [diff] [blame] | 175 | # sha256 is base64 encoded, encode it to hex. |
| 176 | response_values['hash_sha256'] = binascii.hexlify(base64.b64decode(sha256)) |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 177 | response_values['size'] = size |
| 178 | response_values['url'] = url |
| 179 | (codebase, filename) = os.path.split(url) |
| 180 | response_values['codebase'] = codebase |
| 181 | response_values['filename'] = filename |
Gilad Arnold | e96e865 | 2013-05-22 18:36:32 | [diff] [blame] | 182 | response_values['is_delta_format'] = str(is_delta_format).lower() |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 183 | extra_attributes = [] |
| 184 | if critical_update: |
| 185 | # The date string looks like '20111115' (2011-11-15). As of writing, |
| 186 | # there's no particular format for the deadline value that the |
| 187 | # client expects -- it's just empty vs. non-empty. |
| 188 | date_str = datetime.date.today().strftime('%Y%m%d') |
| 189 | extra_attributes.append('deadline="%s"' % date_str) |
| 190 | |
David Zeuthen | 52ccd01 | 2013-10-31 19:58:26 | [diff] [blame] | 191 | if metadata_size: |
| 192 | extra_attributes.append('MetadataSize="%d"' % metadata_size) |
| 193 | if signed_metadata_hash: |
| 194 | extra_attributes.append('MetadataSignatureRsa="%s"' % signed_metadata_hash) |
| 195 | if public_key: |
| 196 | extra_attributes.append('PublicKeyRsa="%s"' % public_key) |
| 197 | |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 198 | response_values['extra_attr'] = ' '.join(extra_attributes) |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 199 | return GetSubstitutedResponse(_UPDATE_RESPONSE, protocol, response_values) |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 200 | |
| 201 | |
Amin Hassani | d7a913a | 2018-03-13 22:19:24 | [diff] [blame] | 202 | def GetNoUpdateResponse(protocol, appid): |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 203 | """Returns a protocol-specific response to the client for no update. |
| 204 | |
| 205 | Args: |
| 206 | protocol: client's protocol version from the request Xml. |
Amin Hassani | d7a913a | 2018-03-13 22:19:24 | [diff] [blame] | 207 | appid: the appid associated with the response. |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 208 | |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 209 | Returns: |
| 210 | Xml string to be passed back to client. |
| 211 | """ |
Amin Hassani | d7a913a | 2018-03-13 22:19:24 | [diff] [blame] | 212 | response_values = GetCommonResponseValues(appid) |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 213 | return GetSubstitutedResponse(_NO_UPDATE_RESPONSE, protocol, response_values) |
| 214 | |
| 215 | |
Amin Hassani | d7a913a | 2018-03-13 22:19:24 | [diff] [blame] | 216 | def GetEventResponse(protocol, appid): |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 217 | """Returns a protocol-specific response to a client event notification. |
| 218 | |
| 219 | Args: |
| 220 | protocol: client's protocol version from the request Xml. |
Amin Hassani | d7a913a | 2018-03-13 22:19:24 | [diff] [blame] | 221 | appid: the appid associated with the response. |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 222 | |
| 223 | Returns: |
| 224 | Xml string to be passed back to client. |
| 225 | """ |
Amin Hassani | d7a913a | 2018-03-13 22:19:24 | [diff] [blame] | 226 | response_values = GetCommonResponseValues(appid) |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 227 | return GetSubstitutedResponse(_EVENT_RESPONSE, protocol, response_values) |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 228 | |
| 229 | |
| 230 | def ParseUpdateRequest(request_string): |
| 231 | """Returns a tuple containing information parsed from an update request. |
| 232 | |
| 233 | Args: |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 234 | request_string: an xml string containing the update request. |
| 235 | |
joychen | 121fc9b | 2013-08-02 21:30:30 | [diff] [blame] | 236 | Returns: |
| 237 | Tuple consisting of protocol string, app element, event element, and |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 238 | update_check element. |
Gilad Arnold | e7819e7 | 2014-03-21 19:50:48 | [diff] [blame] | 239 | |
Chris Sosa | 5214858 | 2012-11-15 23:35:58 | [diff] [blame] | 240 | Raises UnknownProtocolRequestedException if we do not understand the |
| 241 | protocol. |
| 242 | """ |
| 243 | request_dom = minidom.parseString(request_string) |
| 244 | protocol = request_dom.firstChild.getAttribute('protocol') |
| 245 | supported_protocols = '2.0', '3.0' |
| 246 | if protocol not in supported_protocols: |
| 247 | raise UnknownProtocolRequestedException('Supported protocols are %s' % |
| 248 | supported_protocols) |
| 249 | |
| 250 | element_dict = {} |
| 251 | for name in ['event', 'app', 'updatecheck']: |
| 252 | element_dict[name] = 'o:' + name if protocol == '2.0' else name |
| 253 | |
| 254 | app = request_dom.firstChild.getElementsByTagName(element_dict['app'])[0] |
| 255 | event = request_dom.getElementsByTagName(element_dict['event']) |
| 256 | update_check = request_dom.getElementsByTagName(element_dict['updatecheck']) |
| 257 | |
| 258 | return protocol, app, event, update_check |