Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | package com.android.server.updates; |
| 18 | |
Ben Gruver | 12964bf | 2015-04-06 10:55:08 -0700 | [diff] [blame] | 19 | import com.android.server.EventLogTags; |
Przemyslaw Szczepaniak | 3f72604 | 2015-10-15 11:04:19 +0100 | [diff] [blame] | 20 | import com.android.internal.util.HexDump; |
Ben Gruver | 12964bf | 2015-04-06 10:55:08 -0700 | [diff] [blame] | 21 | |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 22 | import android.content.BroadcastReceiver; |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
Nick Kralevich | 103173e | 2014-06-28 12:18:03 -0700 | [diff] [blame] | 25 | import android.net.Uri; |
Geremy Condra | 7c65e39 | 2012-09-11 16:57:17 -0700 | [diff] [blame] | 26 | import android.util.EventLog; |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 27 | import android.util.Slog; |
| 28 | |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 29 | import java.io.File; |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 30 | import java.io.FileOutputStream; |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 31 | import java.io.IOException; |
Ben Gruver | 12964bf | 2015-04-06 10:55:08 -0700 | [diff] [blame] | 32 | import java.io.InputStream; |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 33 | import java.security.MessageDigest; |
| 34 | import java.security.NoSuchAlgorithmException; |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 35 | |
| 36 | import libcore.io.IoUtils; |
Nick Kralevich | 103173e | 2014-06-28 12:18:03 -0700 | [diff] [blame] | 37 | import libcore.io.Streams; |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 38 | |
| 39 | public class ConfigUpdateInstallReceiver extends BroadcastReceiver { |
| 40 | |
| 41 | private static final String TAG = "ConfigUpdateInstallReceiver"; |
| 42 | |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 43 | private static final String EXTRA_REQUIRED_HASH = "REQUIRED_HASH"; |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 44 | private static final String EXTRA_VERSION_NUMBER = "VERSION"; |
| 45 | |
Geremy Condra | a2dffda | 2013-04-06 14:01:40 -0700 | [diff] [blame] | 46 | protected final File updateDir; |
| 47 | protected final File updateContent; |
| 48 | protected final File updateVersion; |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 49 | |
| 50 | public ConfigUpdateInstallReceiver(String updateDir, String updateContentPath, |
| 51 | String updateMetadataPath, String updateVersionPath) { |
| 52 | this.updateDir = new File(updateDir); |
| 53 | this.updateContent = new File(updateDir, updateContentPath); |
| 54 | File updateMetadataDir = new File(updateDir, updateMetadataPath); |
| 55 | this.updateVersion = new File(updateMetadataDir, updateVersionPath); |
| 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public void onReceive(final Context context, final Intent intent) { |
| 60 | new Thread() { |
| 61 | @Override |
| 62 | public void run() { |
| 63 | try { |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 64 | // get the content path from the extras |
Nick Kralevich | 103173e | 2014-06-28 12:18:03 -0700 | [diff] [blame] | 65 | byte[] altContent = getAltContent(context, intent); |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 66 | // get the version from the extras |
| 67 | int altVersion = getVersionFromIntent(intent); |
| 68 | // get the previous value from the extras |
| 69 | String altRequiredHash = getRequiredHashFromIntent(intent); |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 70 | // get the version currently being used |
| 71 | int currentVersion = getCurrentVersion(); |
| 72 | // get the hash of the currently used value |
| 73 | String currentHash = getCurrentHash(getCurrentContent()); |
| 74 | if (!verifyVersion(currentVersion, altVersion)) { |
Geremy Condra | 0967a9e | 2012-09-19 21:22:42 -0700 | [diff] [blame] | 75 | Slog.i(TAG, "Not installing, new version is <= current version"); |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 76 | } else if (!verifyPreviousHash(currentHash, altRequiredHash)) { |
Geremy Condra | beb9d539 | 2012-09-17 13:29:56 -0700 | [diff] [blame] | 77 | EventLog.writeEvent(EventLogTags.CONFIG_INSTALL_FAILED, |
| 78 | "Current hash did not match required value"); |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 79 | } else { |
| 80 | // install the new content |
| 81 | Slog.i(TAG, "Found new update, installing..."); |
| 82 | install(altContent, altVersion); |
| 83 | Slog.i(TAG, "Installation successful"); |
Geremy Condra | 4e7f7e8 | 2013-03-26 21:09:01 -0700 | [diff] [blame] | 84 | postInstall(context, intent); |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 85 | } |
| 86 | } catch (Exception e) { |
| 87 | Slog.e(TAG, "Could not update content!", e); |
Geremy Condra | beb9d539 | 2012-09-17 13:29:56 -0700 | [diff] [blame] | 88 | // keep the error message <= 100 chars |
| 89 | String errMsg = e.toString(); |
| 90 | if (errMsg.length() > 100) { |
| 91 | errMsg = errMsg.substring(0, 99); |
| 92 | } |
| 93 | EventLog.writeEvent(EventLogTags.CONFIG_INSTALL_FAILED, errMsg); |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | }.start(); |
| 97 | } |
| 98 | |
Nick Kralevich | 103173e | 2014-06-28 12:18:03 -0700 | [diff] [blame] | 99 | private Uri getContentFromIntent(Intent i) { |
| 100 | Uri data = i.getData(); |
| 101 | if (data == null) { |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 102 | throw new IllegalStateException("Missing required content path, ignoring."); |
| 103 | } |
Nick Kralevich | 103173e | 2014-06-28 12:18:03 -0700 | [diff] [blame] | 104 | return data; |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | private int getVersionFromIntent(Intent i) throws NumberFormatException { |
| 108 | String extraValue = i.getStringExtra(EXTRA_VERSION_NUMBER); |
| 109 | if (extraValue == null) { |
| 110 | throw new IllegalStateException("Missing required version number, ignoring."); |
| 111 | } |
| 112 | return Integer.parseInt(extraValue.trim()); |
| 113 | } |
| 114 | |
| 115 | private String getRequiredHashFromIntent(Intent i) { |
| 116 | String extraValue = i.getStringExtra(EXTRA_REQUIRED_HASH); |
| 117 | if (extraValue == null) { |
| 118 | throw new IllegalStateException("Missing required previous hash, ignoring."); |
| 119 | } |
| 120 | return extraValue.trim(); |
| 121 | } |
| 122 | |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 123 | private int getCurrentVersion() throws NumberFormatException { |
| 124 | try { |
| 125 | String strVersion = IoUtils.readFileAsString(updateVersion.getCanonicalPath()).trim(); |
| 126 | return Integer.parseInt(strVersion); |
| 127 | } catch (IOException e) { |
Robert Greenwalt | c6fa237 | 2012-09-24 13:57:16 -0700 | [diff] [blame] | 128 | Slog.i(TAG, "Couldn't find current metadata, assuming first update"); |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | } |
| 132 | |
Nick Kralevich | 103173e | 2014-06-28 12:18:03 -0700 | [diff] [blame] | 133 | private byte[] getAltContent(Context c, Intent i) throws IOException { |
| 134 | Uri content = getContentFromIntent(i); |
| 135 | InputStream is = c.getContentResolver().openInputStream(content); |
| 136 | try { |
| 137 | return Streams.readFullyNoClose(is); |
| 138 | } finally { |
| 139 | is.close(); |
| 140 | } |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Geremy Condra | ad462d2 | 2013-01-30 11:19:54 -0800 | [diff] [blame] | 143 | private byte[] getCurrentContent() { |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 144 | try { |
Geremy Condra | ad462d2 | 2013-01-30 11:19:54 -0800 | [diff] [blame] | 145 | return IoUtils.readFileAsByteArray(updateContent.getCanonicalPath()); |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 146 | } catch (IOException e) { |
Robert Greenwalt | c6fa237 | 2012-09-24 13:57:16 -0700 | [diff] [blame] | 147 | Slog.i(TAG, "Failed to read current content, assuming first update!"); |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 148 | return null; |
| 149 | } |
| 150 | } |
| 151 | |
Geremy Condra | ad462d2 | 2013-01-30 11:19:54 -0800 | [diff] [blame] | 152 | private static String getCurrentHash(byte[] content) { |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 153 | if (content == null) { |
| 154 | return "0"; |
| 155 | } |
| 156 | try { |
| 157 | MessageDigest dgst = MessageDigest.getInstance("SHA512"); |
Geremy Condra | ad462d2 | 2013-01-30 11:19:54 -0800 | [diff] [blame] | 158 | byte[] fingerprint = dgst.digest(content); |
Przemyslaw Szczepaniak | 3f72604 | 2015-10-15 11:04:19 +0100 | [diff] [blame] | 159 | return HexDump.toHexString(fingerprint, false); |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 160 | } catch (NoSuchAlgorithmException e) { |
| 161 | throw new AssertionError(e); |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | private boolean verifyVersion(int current, int alternative) { |
| 166 | return (current < alternative); |
| 167 | } |
| 168 | |
| 169 | private boolean verifyPreviousHash(String current, String required) { |
| 170 | // this is an optional value- if the required field is NONE then we ignore it |
| 171 | if (required.equals("NONE")) { |
| 172 | return true; |
| 173 | } |
| 174 | // otherwise, verify that we match correctly |
| 175 | return current.equals(required); |
| 176 | } |
| 177 | |
Geremy Condra | a2dffda | 2013-04-06 14:01:40 -0700 | [diff] [blame] | 178 | protected void writeUpdate(File dir, File file, byte[] content) throws IOException { |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 179 | FileOutputStream out = null; |
| 180 | File tmp = null; |
| 181 | try { |
Geremy Condra | 755b877 | 2012-09-11 01:11:02 -0700 | [diff] [blame] | 182 | // create the parents for the destination file |
| 183 | File parent = file.getParentFile(); |
| 184 | parent.mkdirs(); |
| 185 | // check that they were created correctly |
| 186 | if (!parent.exists()) { |
| 187 | throw new IOException("Failed to create directory " + parent.getCanonicalPath()); |
| 188 | } |
Geremy Condra | 9765f94 | 2013-04-04 17:48:09 -0700 | [diff] [blame] | 189 | // create the temporary file |
| 190 | tmp = File.createTempFile("journal", "", dir); |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 191 | // mark tmp -rw-r--r-- |
| 192 | tmp.setReadable(true, false); |
| 193 | // write to it |
| 194 | out = new FileOutputStream(tmp); |
Geremy Condra | ad462d2 | 2013-01-30 11:19:54 -0800 | [diff] [blame] | 195 | out.write(content); |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 196 | // sync to disk |
Geremy Condra | 755b877 | 2012-09-11 01:11:02 -0700 | [diff] [blame] | 197 | out.getFD().sync(); |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 198 | // atomic rename |
Geremy Condra | 755b877 | 2012-09-11 01:11:02 -0700 | [diff] [blame] | 199 | if (!tmp.renameTo(file)) { |
| 200 | throw new IOException("Failed to atomically rename " + file.getCanonicalPath()); |
| 201 | } |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 202 | } finally { |
| 203 | if (tmp != null) { |
| 204 | tmp.delete(); |
| 205 | } |
| 206 | IoUtils.closeQuietly(out); |
| 207 | } |
| 208 | } |
| 209 | |
Geremy Condra | 78a4c71 | 2013-01-30 11:20:26 -0800 | [diff] [blame] | 210 | protected void install(byte[] content, int version) throws IOException { |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 211 | writeUpdate(updateDir, updateContent, content); |
Geremy Condra | ad462d2 | 2013-01-30 11:19:54 -0800 | [diff] [blame] | 212 | writeUpdate(updateDir, updateVersion, Long.toString(version).getBytes()); |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 213 | } |
Geremy Condra | 4e7f7e8 | 2013-03-26 21:09:01 -0700 | [diff] [blame] | 214 | |
| 215 | protected void postInstall(Context context, Intent intent) { |
| 216 | } |
Geremy Condra | b631084 | 2012-08-23 22:00:15 -0700 | [diff] [blame] | 217 | } |